| 20 |
#include <errno.h> |
#include <errno.h> |
| 21 |
#include <stdio.h> |
#include <stdio.h> |
| 22 |
#include <stdarg.h> |
#include <stdarg.h> |
| 23 |
|
#include <string.h> |
| 24 |
#include <time.h> |
#include <time.h> |
| 25 |
#include <fcntl.h> |
#include <fcntl.h> |
| 26 |
#include <unistd.h> |
#include <unistd.h> |
| 27 |
#include <sys/select.h> |
#include <sys/select.h> |
| 28 |
#include <sys/ioctl.h> |
#include <sys/ioctl.h> |
| 29 |
|
|
| 30 |
int outc(char c) |
static char stdout_buf[BUFSIZ]; |
| 31 |
{ |
static int stdout_buf_len = 0; |
|
int retval; |
|
|
|
|
|
retval = fprintf(stdout, "%c", c); |
|
|
|
|
|
return retval; |
|
|
} |
|
| 32 |
|
|
| 33 |
int prints(const char *format, ...) |
int prints(const char *format, ...) |
| 34 |
{ |
{ |
| 35 |
|
char buf[BUFSIZ]; |
| 36 |
va_list args; |
va_list args; |
| 37 |
int retval; |
int ret; |
| 38 |
|
|
| 39 |
va_start(args, format); |
va_start(args, format); |
| 40 |
retval = vfprintf(stdout, format, args); |
ret = vsnprintf(buf, sizeof(buf), format, args); |
| 41 |
va_end(args); |
va_end(args); |
| 42 |
|
|
| 43 |
return retval; |
if (ret > 0 && stdout_buf_len + ret < BUFSIZ) |
| 44 |
|
{ |
| 45 |
|
memcpy(stdout_buf + stdout_buf_len, buf, (size_t)(ret + 1)); |
| 46 |
|
stdout_buf_len += ret; |
| 47 |
|
} |
| 48 |
|
else if (ret > 0) // No enough free buffer |
| 49 |
|
{ |
| 50 |
|
ret = (BUFSIZ - stdout_buf_len - ret - 1); |
| 51 |
|
} |
| 52 |
|
|
| 53 |
|
return ret; |
| 54 |
} |
} |
| 55 |
|
|
| 56 |
int iflush() |
int iflush() |
| 57 |
{ |
{ |
| 58 |
int retval; |
int flags; |
| 59 |
|
fd_set write_fds; |
| 60 |
|
struct timeval timeout; |
| 61 |
|
int stdout_buf_offset = 0; |
| 62 |
|
int loop = 1; |
| 63 |
|
int ret = 0; |
| 64 |
|
|
| 65 |
|
// Set STDOUT as non-blocking |
| 66 |
|
flags = fcntl(STDOUT_FILENO, F_GETFL, 0); |
| 67 |
|
fcntl(STDOUT_FILENO, F_SETFL, flags | O_NONBLOCK); |
| 68 |
|
|
| 69 |
|
while (loop && !SYS_server_exit) |
| 70 |
|
{ |
| 71 |
|
FD_ZERO(&write_fds); |
| 72 |
|
FD_SET(STDOUT_FILENO, &write_fds); |
| 73 |
|
|
| 74 |
|
timeout.tv_sec = 0; |
| 75 |
|
timeout.tv_usec = 100 * 1000; // 0.1 second |
| 76 |
|
|
| 77 |
|
ret = select(STDOUT_FILENO + 1, NULL, &write_fds, NULL, &timeout); |
| 78 |
|
|
| 79 |
|
if (ret == 0) // timeout |
| 80 |
|
{ |
| 81 |
|
continue; |
| 82 |
|
} |
| 83 |
|
else if (ret < 0) |
| 84 |
|
{ |
| 85 |
|
if (errno != EINTR) |
| 86 |
|
{ |
| 87 |
|
log_error("select() error (%d) !\n", errno); |
| 88 |
|
loop = 0; |
| 89 |
|
} |
| 90 |
|
} |
| 91 |
|
else if (FD_ISSET(STDOUT_FILENO, &write_fds)) |
| 92 |
|
{ |
| 93 |
|
ret = (int)write(STDOUT_FILENO, stdout_buf + stdout_buf_offset, (size_t)(stdout_buf_len - stdout_buf_offset)); |
| 94 |
|
if (ret < 0) |
| 95 |
|
{ |
| 96 |
|
if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) |
| 97 |
|
{ |
| 98 |
|
log_error("write(STDOUT) error (%d)\n", errno); |
| 99 |
|
loop = 0; |
| 100 |
|
} |
| 101 |
|
} |
| 102 |
|
else if (ret == 0) // broken pipe |
| 103 |
|
{ |
| 104 |
|
loop = 0; |
| 105 |
|
} |
| 106 |
|
else |
| 107 |
|
{ |
| 108 |
|
stdout_buf_offset += ret; |
| 109 |
|
if (stdout_buf_offset >= stdout_buf_len) // Flush buffer complete |
| 110 |
|
{ |
| 111 |
|
ret = 0; |
| 112 |
|
stdout_buf_len = 0; |
| 113 |
|
loop = 0; |
| 114 |
|
} |
| 115 |
|
} |
| 116 |
|
} |
| 117 |
|
} |
| 118 |
|
|
| 119 |
retval = fflush(stdout); |
// Restore STDOUT flags |
| 120 |
|
fcntl(STDOUT_FILENO, F_SETFL, flags); |
| 121 |
|
|
| 122 |
return retval; |
return ret; |
| 123 |
} |
} |
| 124 |
|
|
| 125 |
int igetch(int clear_buf) |
int igetch(int clear_buf) |
| 129 |
static ssize_t len = 0; |
static ssize_t len = 0; |
| 130 |
static int pos = 0; |
static int pos = 0; |
| 131 |
|
|
| 132 |
fd_set testfds; |
fd_set readfds; |
| 133 |
struct timeval timeout; |
struct timeval timeout; |
| 134 |
|
|
| 135 |
unsigned char tmp[LINE_BUFFER_LEN]; |
unsigned char tmp[LINE_BUFFER_LEN]; |
| 136 |
int ret; |
int ret; |
| 137 |
int out = KEY_NULL; |
int out = '\0'; |
| 138 |
int in_esc = 0; |
int in_esc = 0; |
| 139 |
int in_ascii = 0; |
int in_ascii = 0; |
| 140 |
int in_control = 0; |
int in_control = 0; |
| 141 |
int i = 0; |
int i = 0; |
| 142 |
|
int flags; |
| 143 |
|
|
| 144 |
if (clear_buf) |
if (clear_buf) |
| 145 |
{ |
{ |
| 146 |
pos = 0; |
pos = 0; |
| 147 |
len = 0; |
len = 0; |
| 148 |
|
|
| 149 |
return 0; |
return '\0'; |
| 150 |
} |
} |
| 151 |
|
|
| 152 |
while (!SYS_server_exit && pos >= len) |
while (!SYS_server_exit && pos >= len) |
| 153 |
{ |
{ |
| 154 |
FD_ZERO(&testfds); |
FD_ZERO(&readfds); |
| 155 |
FD_SET(STDIN_FILENO, &testfds); |
FD_SET(STDIN_FILENO, &readfds); |
| 156 |
|
|
| 157 |
timeout.tv_sec = 0; |
timeout.tv_sec = 0; |
| 158 |
timeout.tv_usec = 100 * 1000; // 0.1 second |
timeout.tv_usec = 100 * 1000; // 0.1 second |
| 159 |
|
|
| 160 |
ret = select(FD_SETSIZE, &testfds, NULL, NULL, &timeout); |
ret = select(STDIN_FILENO + 1, &readfds, NULL, NULL, &timeout); |
| 161 |
|
|
| 162 |
if (ret < 0) |
if (ret < 0) |
| 163 |
{ |
{ |
| 164 |
if (errno != EINTR) |
if (errno != EINTR) |
| 165 |
{ |
{ |
| 166 |
log_error("Select error in igetch: !\n", errno); |
log_error("select() error (%d) !\n", errno); |
| 167 |
return KEY_NULL; |
return KEY_NULL; |
| 168 |
} |
} |
| 169 |
continue; |
continue; |
| 173 |
return KEY_TIMEOUT; |
return KEY_TIMEOUT; |
| 174 |
} |
} |
| 175 |
|
|
| 176 |
if (FD_ISSET(STDIN_FILENO, &testfds)) |
if (FD_ISSET(STDIN_FILENO, &readfds)) |
| 177 |
{ |
{ |
| 178 |
|
flags = fcntl(STDIN_FILENO, F_GETFL, 0); |
| 179 |
|
fcntl(socket_server, F_SETFL, flags | O_NONBLOCK); |
| 180 |
|
|
| 181 |
len = read(STDIN_FILENO, buf, sizeof(buf)); |
len = read(STDIN_FILENO, buf, sizeof(buf)); |
| 182 |
|
if (len < 0) |
| 183 |
|
{ |
| 184 |
|
if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) |
| 185 |
|
{ |
| 186 |
|
log_error("read(STDIN) error (%d)\n", errno); |
| 187 |
|
} |
| 188 |
|
} |
| 189 |
|
else if (len == 0) |
| 190 |
|
{ |
| 191 |
|
out = KEY_NULL; // broken pipe |
| 192 |
|
} |
| 193 |
|
|
| 194 |
pos = 0; |
pos = 0; |
| 195 |
|
|
| 196 |
|
fcntl(STDIN_FILENO, F_SETFL, flags); |
| 197 |
|
|
| 198 |
break; |
break; |
| 199 |
} |
} |
| 200 |
|
|
| 207 |
{ |
{ |
| 208 |
unsigned char c = buf[pos++]; |
unsigned char c = buf[pos++]; |
| 209 |
|
|
|
if (c == '\0') |
|
|
{ |
|
|
out = c; |
|
|
break; |
|
|
} |
|
|
|
|
| 210 |
if (c == KEY_CONTROL) |
if (c == KEY_CONTROL) |
| 211 |
{ |
{ |
| 212 |
if (in_control == 0) |
if (in_control == 0) |
| 320 |
do |
do |
| 321 |
{ |
{ |
| 322 |
ch = igetch(0); |
ch = igetch(0); |
| 323 |
} while ((ch == KEY_TIMEOUT || ch == 0xa) && (time(0) - t_begin < sec)); |
} while (!SYS_server_exit && ch == KEY_TIMEOUT && (time(0) - t_begin < sec)); |
| 324 |
|
|
| 325 |
return ch; |
return ch; |
| 326 |
} |
} |