Move write_all convenience function to common.

This commit is contained in:
Michael Jumper 2013-05-21 22:09:42 -07:00
parent 5a0b8b2ea7
commit 142b526a97
3 changed files with 34 additions and 30 deletions

View File

@ -58,5 +58,11 @@ int guac_terminal_encode_utf8(int codepoint, char* utf8);
*/ */
bool guac_terminal_has_glyph(int codepoint); bool guac_terminal_has_glyph(int codepoint);
/**
* Similar to write, but automatically retries the write operation until
* an error occurs.
*/
int guac_terminal_write_all(int fd, const char* buffer, int size);
#endif #endif

View File

@ -36,6 +36,7 @@
* ***** END LICENSE BLOCK ***** */ * ***** END LICENSE BLOCK ***** */
#include <stdbool.h> #include <stdbool.h>
#include <unistd.h>
int guac_terminal_fit_to_range(int value, int min, int max) { int guac_terminal_fit_to_range(int value, int min, int max) {
@ -98,3 +99,23 @@ bool guac_terminal_has_glyph(int codepoint) {
&& codepoint != ' '; && codepoint != ' ';
} }
int guac_terminal_write_all(int fd, const char* buffer, int size) {
int remaining = size;
while (remaining > 0) {
/* Attempt to write data */
int ret_val = write(fd, buffer, remaining);
if (ret_val <= 0)
return -1;
/* If successful, contine with what data remains (if any) */
remaining -= ret_val;
buffer += ret_val;
}
return size;
}

View File

@ -45,30 +45,7 @@
#include <guacamole/socket.h> #include <guacamole/socket.h>
#include "client.h" #include "client.h"
#include "common.h"
/**
* Similar to write, but automatically retries the write operation until
* an error occurs.
*/
static int __write_all(int fd, const char* buffer, int size) {
int remaining = size;
while (remaining > 0) {
/* Attempt to write data */
int ret_val = write(fd, buffer, remaining);
if (ret_val <= 0)
return -1;
/* If successful, contine with what data remains (if any) */
remaining -= ret_val;
buffer += ret_val;
}
return size;
}
/** /**
* Reads a single line from STDIN. * Reads a single line from STDIN.
@ -85,7 +62,7 @@ static char* prompt(guac_client* client, const char* title, char* str, int size,
int stdout_fd = client_data->term->stdout_pipe_fd[1]; int stdout_fd = client_data->term->stdout_pipe_fd[1];
/* Print title */ /* Print title */
__write_all(stdout_fd, title, strlen(title)); guac_terminal_write_all(stdout_fd, title, strlen(title));
/* Make room for null terminator */ /* Make room for null terminator */
size--; size--;
@ -98,14 +75,14 @@ static char* prompt(guac_client* client, const char* title, char* str, int size,
if (in_byte == 0x08) { if (in_byte == 0x08) {
if (pos > 0) { if (pos > 0) {
__write_all(stdout_fd, "\b \b", 3); guac_terminal_write_all(stdout_fd, "\b \b", 3);
pos--; pos--;
} }
} }
/* Newline (end of input */ /* Newline (end of input */
else if (in_byte == 0x0A) { else if (in_byte == 0x0A) {
__write_all(stdout_fd, "\r\n", 2); guac_terminal_write_all(stdout_fd, "\r\n", 2);
break; break;
} }
@ -116,9 +93,9 @@ static char* prompt(guac_client* client, const char* title, char* str, int size,
/* Print character if echoing */ /* Print character if echoing */
if (echo) if (echo)
__write_all(stdout_fd, &in_byte, 1); guac_terminal_write_all(stdout_fd, &in_byte, 1);
else else
__write_all(stdout_fd, "*", 1); guac_terminal_write_all(stdout_fd, "*", 1);
} }
@ -251,7 +228,7 @@ void* ssh_client_thread(void* data) {
continue; continue;
if (bytes_read > 0) if (bytes_read > 0)
__write_all(stdout_fd, buffer, bytes_read); guac_terminal_write_all(stdout_fd, buffer, bytes_read);
} }