| 1 |
/*************************************************************************** |
/*************************************************************************** |
| 2 |
io.c - description |
io.c - description |
| 3 |
------------------- |
------------------- |
| 4 |
begin : Mon Oct 18 2004 |
Copyright : (C) 2004-2025 by Leaflet |
| 5 |
copyright : (C) 2004 by Leaflet |
Email : leaflet@leafok.com |
|
email : leaflet@leafok.com |
|
| 6 |
***************************************************************************/ |
***************************************************************************/ |
| 7 |
|
|
| 8 |
/*************************************************************************** |
/*************************************************************************** |
| 9 |
* * |
* * |
| 10 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
| 11 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
| 12 |
* the Free Software Foundation; either version 2 of the License, or * |
* the Free Software Foundation; either version 3 of the License, or * |
| 13 |
* (at your option) any later version. * |
* (at your option) any later version. * |
| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 17 |
#include "io.h" |
#include "io.h" |
| 18 |
#include "log.h" |
#include "log.h" |
| 19 |
#include "common.h" |
#include "common.h" |
| 20 |
#include "tcplib.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> |
| 28 |
#include <sys/ioctl.h> |
#include <sys/ioctl.h> |
| 29 |
|
#include <sys/epoll.h> |
| 30 |
|
|
| 31 |
int outc(char c) |
static char stdout_buf[BUFSIZ]; |
| 32 |
{ |
static int stdout_buf_len = 0; |
|
int retval; |
|
|
|
|
|
retval = fprintf(stdout, "%c", c); |
|
|
|
|
|
return retval; |
|
|
} |
|
| 33 |
|
|
| 34 |
int prints(const char *format, ...) |
int prints(const char *format, ...) |
| 35 |
{ |
{ |
| 36 |
|
char buf[BUFSIZ]; |
| 37 |
va_list args; |
va_list args; |
| 38 |
int retval; |
int ret; |
| 39 |
|
|
| 40 |
va_start(args, format); |
va_start(args, format); |
| 41 |
retval = vfprintf(stdout, format, args); |
ret = vsnprintf(buf, sizeof(buf), format, args); |
| 42 |
va_end(args); |
va_end(args); |
| 43 |
|
|
| 44 |
return retval; |
if (ret > 0 && stdout_buf_len + ret < BUFSIZ) |
| 45 |
|
{ |
| 46 |
|
memcpy(stdout_buf + stdout_buf_len, buf, (size_t)(ret + 1)); |
| 47 |
|
stdout_buf_len += ret; |
| 48 |
|
} |
| 49 |
|
else if (ret > 0) // No enough free buffer |
| 50 |
|
{ |
| 51 |
|
ret = (BUFSIZ - stdout_buf_len - ret - 1); |
| 52 |
|
} |
| 53 |
|
|
| 54 |
|
return ret; |
| 55 |
} |
} |
| 56 |
|
|
| 57 |
int iflush() |
int iflush() |
| 58 |
{ |
{ |
| 59 |
int retval; |
int flags; |
| 60 |
|
struct epoll_event ev, events[MAX_EVENTS]; |
| 61 |
|
int nfds, epollfd; |
| 62 |
|
int stdout_buf_offset = 0; |
| 63 |
|
int loop = 1; |
| 64 |
|
int ret = 0; |
| 65 |
|
|
| 66 |
|
epollfd = epoll_create1(0); |
| 67 |
|
if (epollfd < 0) |
| 68 |
|
{ |
| 69 |
|
log_error("epoll_create1() error (%d)\n", errno); |
| 70 |
|
return -1; |
| 71 |
|
} |
| 72 |
|
|
| 73 |
|
ev.events = EPOLLOUT; |
| 74 |
|
ev.data.fd = STDOUT_FILENO; |
| 75 |
|
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1) |
| 76 |
|
{ |
| 77 |
|
log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno); |
| 78 |
|
return -1; |
| 79 |
|
} |
| 80 |
|
|
| 81 |
|
// Set STDOUT as non-blocking |
| 82 |
|
flags = fcntl(STDOUT_FILENO, F_GETFL, 0); |
| 83 |
|
fcntl(STDOUT_FILENO, F_SETFL, flags | O_NONBLOCK); |
| 84 |
|
|
| 85 |
retval = fflush(stdout); |
while (loop && !SYS_server_exit) |
| 86 |
|
{ |
| 87 |
|
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second |
| 88 |
|
|
| 89 |
|
if (nfds < 0) |
| 90 |
|
{ |
| 91 |
|
if (errno != EINTR) |
| 92 |
|
{ |
| 93 |
|
log_error("epoll_wait() error (%d)\n", errno); |
| 94 |
|
break; |
| 95 |
|
} |
| 96 |
|
continue; |
| 97 |
|
} |
| 98 |
|
else if (nfds == 0) // timeout |
| 99 |
|
{ |
| 100 |
|
continue; |
| 101 |
|
} |
| 102 |
|
|
| 103 |
return retval; |
for (int i = 0; i < nfds; i++) |
| 104 |
|
{ |
| 105 |
|
if (events[i].data.fd == STDOUT_FILENO) |
| 106 |
|
{ |
| 107 |
|
while (1) // write until complete or error |
| 108 |
|
{ |
| 109 |
|
ret = (int)write(STDOUT_FILENO, stdout_buf + stdout_buf_offset, (size_t)(stdout_buf_len - stdout_buf_offset)); |
| 110 |
|
if (ret < 0) |
| 111 |
|
{ |
| 112 |
|
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 113 |
|
{ |
| 114 |
|
break; |
| 115 |
|
} |
| 116 |
|
else if (errno == EINTR) |
| 117 |
|
{ |
| 118 |
|
continue; |
| 119 |
|
} |
| 120 |
|
else |
| 121 |
|
{ |
| 122 |
|
log_error("write(STDOUT) error (%d)\n", errno); |
| 123 |
|
loop = 0; |
| 124 |
|
break; |
| 125 |
|
} |
| 126 |
|
} |
| 127 |
|
else if (ret == 0) // broken pipe |
| 128 |
|
{ |
| 129 |
|
loop = 0; |
| 130 |
|
break; |
| 131 |
|
} |
| 132 |
|
else |
| 133 |
|
{ |
| 134 |
|
stdout_buf_offset += ret; |
| 135 |
|
if (stdout_buf_offset >= stdout_buf_len) // Flush buffer complete |
| 136 |
|
{ |
| 137 |
|
ret = 0; |
| 138 |
|
stdout_buf_len = 0; |
| 139 |
|
loop = 0; |
| 140 |
|
break; |
| 141 |
|
} |
| 142 |
|
continue; |
| 143 |
|
} |
| 144 |
|
} |
| 145 |
|
} |
| 146 |
|
} |
| 147 |
|
} |
| 148 |
|
|
| 149 |
|
// Restore STDOUT flags |
| 150 |
|
fcntl(STDOUT_FILENO, F_SETFL, flags); |
| 151 |
|
|
| 152 |
|
return ret; |
| 153 |
} |
} |
| 154 |
|
|
| 155 |
int igetch() |
int igetch(int clear_buf) |
| 156 |
{ |
{ |
| 157 |
static char buf[256]; |
// static input buffer |
| 158 |
unsigned char c, tmp[256]; |
static unsigned char buf[LINE_BUFFER_LEN]; |
| 159 |
int out = KEY_NULL, loop = 1, in_esc = 0, in_ascii = 0, in_control = 0, i = 0, j, result; |
static ssize_t len = 0; |
| 160 |
static int len = 0, pos = 0; |
static int pos = 0; |
| 161 |
fd_set testfds; |
|
| 162 |
struct timeval timeout; |
struct epoll_event ev, events[MAX_EVENTS]; |
| 163 |
|
int nfds, epollfd; |
| 164 |
// Stop on system exit |
|
| 165 |
if (SYS_exit) |
unsigned char tmp[LINE_BUFFER_LEN]; |
| 166 |
return KEY_NULL; |
int out = '\0'; |
| 167 |
|
int in_esc = 0; |
| 168 |
|
int in_ascii = 0; |
| 169 |
|
int in_control = 0; |
| 170 |
|
int i = 0; |
| 171 |
|
int flags; |
| 172 |
|
|
| 173 |
if (pos >= len) |
if (clear_buf) |
| 174 |
{ |
{ |
| 175 |
pos = 0; |
pos = 0; |
| 176 |
len = 0; |
len = 0; |
| 177 |
|
|
| 178 |
FD_ZERO(&testfds); |
return '\0'; |
| 179 |
FD_SET(0, &testfds); |
} |
| 180 |
|
|
| 181 |
|
epollfd = epoll_create1(0); |
| 182 |
|
if (epollfd < 0) |
| 183 |
|
{ |
| 184 |
|
log_error("epoll_create1() error (%d)\n", errno); |
| 185 |
|
return -1; |
| 186 |
|
} |
| 187 |
|
|
| 188 |
|
ev.events = EPOLLIN; |
| 189 |
|
ev.data.fd = STDIN_FILENO; |
| 190 |
|
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1) |
| 191 |
|
{ |
| 192 |
|
log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); |
| 193 |
|
return -1; |
| 194 |
|
} |
| 195 |
|
|
| 196 |
timeout.tv_sec = 1; |
flags = fcntl(STDIN_FILENO, F_GETFL, 0); |
| 197 |
timeout.tv_usec = 0; |
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); |
| 198 |
|
|
| 199 |
result = SignalSafeSelect(FD_SETSIZE, &testfds, (fd_set *)NULL, |
while (!SYS_server_exit && pos >= len) |
| 200 |
(fd_set *)NULL, &timeout); |
{ |
| 201 |
|
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second |
| 202 |
|
|
| 203 |
if (result == 0) |
if (nfds < 0) |
| 204 |
{ |
{ |
| 205 |
return KEY_TIMEOUT; |
if (errno != EINTR) |
| 206 |
|
{ |
| 207 |
|
log_error("epoll_wait() error (%d)\n", errno); |
| 208 |
|
fcntl(STDIN_FILENO, F_SETFL, flags); |
| 209 |
|
return KEY_NULL; |
| 210 |
|
} |
| 211 |
|
continue; |
| 212 |
} |
} |
| 213 |
if (result < 0) |
else if (nfds == 0) |
| 214 |
{ |
{ |
| 215 |
log_error("select() error (%d) !\n", result); |
fcntl(STDIN_FILENO, F_SETFL, flags); |
| 216 |
return KEY_NULL; |
return KEY_TIMEOUT; |
| 217 |
} |
} |
| 218 |
if (result > 0) |
|
| 219 |
|
for (int i = 0; i < nfds; i++) |
| 220 |
{ |
{ |
| 221 |
if (FD_ISSET(0, &testfds)) |
if (events[i].data.fd == STDIN_FILENO) |
| 222 |
{ |
{ |
| 223 |
len = read(0, buf, 255); |
while (1) // read until complete or error |
| 224 |
|
{ |
| 225 |
|
len = read(STDIN_FILENO, buf, sizeof(buf)); |
| 226 |
|
if (len < 0) |
| 227 |
|
{ |
| 228 |
|
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 229 |
|
{ |
| 230 |
|
fcntl(STDIN_FILENO, F_SETFL, flags); |
| 231 |
|
return KEY_TIMEOUT; |
| 232 |
|
} |
| 233 |
|
else if (errno == EINTR) |
| 234 |
|
{ |
| 235 |
|
continue; |
| 236 |
|
} |
| 237 |
|
else |
| 238 |
|
{ |
| 239 |
|
log_error("read(STDIN) error (%d)\n", errno); |
| 240 |
|
fcntl(STDIN_FILENO, F_SETFL, flags); |
| 241 |
|
return KEY_NULL; |
| 242 |
|
} |
| 243 |
|
} |
| 244 |
|
else if (len == 0) // broken pipe |
| 245 |
|
{ |
| 246 |
|
fcntl(STDIN_FILENO, F_SETFL, flags); |
| 247 |
|
return KEY_NULL; |
| 248 |
|
} |
| 249 |
|
|
| 250 |
|
pos = 0; |
| 251 |
|
break; |
| 252 |
|
} |
| 253 |
} |
} |
| 254 |
} |
} |
| 255 |
|
|
| 258 |
// log_std ("<--[%u]\n", (buf[j] + 256) % 256); |
// log_std ("<--[%u]\n", (buf[j] + 256) % 256); |
| 259 |
} |
} |
| 260 |
|
|
| 261 |
|
fcntl(STDIN_FILENO, F_SETFL, flags); |
| 262 |
|
|
| 263 |
while (pos < len) |
while (pos < len) |
| 264 |
{ |
{ |
| 265 |
c = buf[pos++]; |
unsigned char c = buf[pos++]; |
|
|
|
|
if (c == '\0') |
|
|
{ |
|
|
out = c; |
|
|
break; |
|
|
} |
|
| 266 |
|
|
| 267 |
if (c == KEY_CONTROL) |
if (c == KEY_CONTROL) |
| 268 |
{ |
{ |
| 376 |
|
|
| 377 |
do |
do |
| 378 |
{ |
{ |
| 379 |
ch = igetch(); |
ch = igetch(0); |
| 380 |
} while ((ch == KEY_TIMEOUT || ch == 0xa || ch == 0x0) && (time(0) - t_begin < sec)); |
} while (!SYS_server_exit && ch == KEY_TIMEOUT && (time(0) - t_begin < sec)); |
| 381 |
|
|
| 382 |
return ch; |
return ch; |
| 383 |
} |
} |
|
|
|
|
int ikbhit() |
|
|
{ |
|
|
int len; |
|
|
|
|
|
ioctl(0, FIONREAD, &len); |
|
|
|
|
|
return len; |
|
|
} |
|