| 1 |
/* $Id: unix_io.c,v 1.39 2024/10/20 17:18:13 tom Exp $ */ |
| 2 |
|
| 3 |
#include <stdarg.h> |
| 4 |
#include <unistd.h> |
| 5 |
#include <vttest.h> |
| 6 |
#include <esc.h> |
| 7 |
|
| 8 |
static void |
| 9 |
give_up(SIG_ARGS GCC_UNUSED) |
| 10 |
{ |
| 11 |
if (LOG_ENABLED) { |
| 12 |
fprintf(log_fp, "** killing program due to timeout\n"); |
| 13 |
fflush(log_fp); |
| 14 |
} |
| 15 |
kill(getpid(), (int) SIGTERM); |
| 16 |
} |
| 17 |
|
| 18 |
static int last_char; |
| 19 |
|
| 20 |
void |
| 21 |
reset_inchar(void) |
| 22 |
{ |
| 23 |
last_char = -1; |
| 24 |
} |
| 25 |
|
| 26 |
/* |
| 27 |
* Wait until a character is typed on the terminal then read it, without |
| 28 |
* waiting for CR. |
| 29 |
*/ |
| 30 |
char |
| 31 |
inchar(void) |
| 32 |
{ |
| 33 |
int lval; |
| 34 |
int ch; |
| 35 |
char one_byte = '\0'; |
| 36 |
int active = is_replaying(); |
| 37 |
|
| 38 |
fflush(stdout); |
| 39 |
lval = last_char; |
| 40 |
brkrd = FALSE; |
| 41 |
reading = TRUE; |
| 42 |
#ifdef HAVE_ALARM |
| 43 |
signal(SIGALRM, give_up); |
| 44 |
alarm(60); /* timeout after 1 minute, in case user's keyboard is hung */ |
| 45 |
#endif |
| 46 |
if (read(0, &one_byte, (size_t) 1) < 0) |
| 47 |
ch = EOF; |
| 48 |
else |
| 49 |
ch = (int) one_byte; |
| 50 |
#ifdef HAVE_ALARM |
| 51 |
alarm(0); |
| 52 |
#endif |
| 53 |
reading = FALSE; |
| 54 |
#ifdef DEBUG |
| 55 |
{ |
| 56 |
FILE *fp = fopen("ttymodes.log", "a"); |
| 57 |
if (fp != 0) { |
| 58 |
fprintf(fp, "%d>%#x\n", brkrd, ch); |
| 59 |
fclose(fp); |
| 60 |
} |
| 61 |
} |
| 62 |
#endif |
| 63 |
if (brkrd) |
| 64 |
last_char = 0177; |
| 65 |
else |
| 66 |
last_char = ch; |
| 67 |
if ((last_char == 0177) && (last_char == lval)) |
| 68 |
give_up(SIGTERM); |
| 69 |
|
| 70 |
if (active) |
| 71 |
pause_replay(); |
| 72 |
if (LOG_ENABLED) { |
| 73 |
fputs(READ_STR, log_fp); |
| 74 |
put_char(log_fp, last_char); |
| 75 |
fputs("\n", log_fp); |
| 76 |
} |
| 77 |
if (active) |
| 78 |
resume_replay(); |
| 79 |
return (char) (last_char); |
| 80 |
} |
| 81 |
|
| 82 |
static int |
| 83 |
read_buffer(char *result, int want) |
| 84 |
{ |
| 85 |
#if USE_FIONREAD |
| 86 |
int l1; |
| 87 |
#endif |
| 88 |
int i = 0; |
| 89 |
|
| 90 |
/* Wait 0.1 seconds (1 second in vanilla UNIX) */ |
| 91 |
zleep(100); |
| 92 |
fflush(stdout); |
| 93 |
#ifdef HAVE_RDCHK |
| 94 |
while (rdchk(0)) { |
| 95 |
read(0, result + i, 1); |
| 96 |
if (i++ >= want) |
| 97 |
break; |
| 98 |
} |
| 99 |
#else |
| 100 |
#if USE_FIONREAD |
| 101 |
while (ioctl(0, FIONREAD, &l1), l1 > 0L) { |
| 102 |
while (l1-- > 0L) { |
| 103 |
if ((int) read(0, result + i, (size_t) 1) <= 0) |
| 104 |
goto out1; |
| 105 |
if (i++ >= want) |
| 106 |
goto out1; |
| 107 |
} |
| 108 |
} |
| 109 |
out1: |
| 110 |
#else |
| 111 |
while (read(2, result + i, 1) == 1) |
| 112 |
if (i++ >= want) |
| 113 |
break; |
| 114 |
#endif |
| 115 |
#endif |
| 116 |
result[i] = '\0'; |
| 117 |
|
| 118 |
return i; |
| 119 |
} |
| 120 |
|
| 121 |
/* |
| 122 |
* Get an unfinished string from the terminal: wait until a character is typed |
| 123 |
* on the terminal, then read it, and all other available characters. Return a |
| 124 |
* pointer to that string. |
| 125 |
*/ |
| 126 |
char * |
| 127 |
instr(void) |
| 128 |
{ |
| 129 |
static char result[BUF_SIZE]; |
| 130 |
|
| 131 |
FILE *save = log_fp; |
| 132 |
int i = 0; |
| 133 |
|
| 134 |
pause_replay(); |
| 135 |
log_fp = NULL; |
| 136 |
result[i++] = inchar(); |
| 137 |
(void) read_buffer(result + i, (int) sizeof(result) - 2); |
| 138 |
log_fp = save; |
| 139 |
|
| 140 |
if (LOG_ENABLED) { |
| 141 |
fputs(READ_STR, log_fp); |
| 142 |
put_string(log_fp, result); |
| 143 |
fputs("\n", log_fp); |
| 144 |
} |
| 145 |
resume_replay(); |
| 146 |
|
| 147 |
return (result); |
| 148 |
} |
| 149 |
|
| 150 |
/* cf: vms_io.c */ |
| 151 |
char * |
| 152 |
get_reply(void) |
| 153 |
{ |
| 154 |
static char result[BUF_SIZE * 2]; |
| 155 |
int old_len = 0; |
| 156 |
int new_len = 0; |
| 157 |
|
| 158 |
fflush(stdout); |
| 159 |
pause_replay(); |
| 160 |
|
| 161 |
zleep(100); |
| 162 |
do { |
| 163 |
new_len = read_buffer(result + old_len, (int) sizeof(result) - 2 - old_len); |
| 164 |
old_len += new_len; |
| 165 |
} while (new_len != 0 && old_len < (BUF_SIZE - 2)); |
| 166 |
|
| 167 |
if (LOG_ENABLED) { |
| 168 |
fputs(READ_STR, log_fp); |
| 169 |
put_string(log_fp, result); |
| 170 |
fputs("\n", log_fp); |
| 171 |
} |
| 172 |
|
| 173 |
resume_replay(); |
| 174 |
|
| 175 |
return (result); |
| 176 |
} |
| 177 |
|
| 178 |
/* |
| 179 |
* Read to the next newline, truncating the buffer at BUF_SIZE-1 characters |
| 180 |
*/ |
| 181 |
void |
| 182 |
inputline(char *s) |
| 183 |
{ |
| 184 |
char *result; |
| 185 |
|
| 186 |
if (is_replaying() && (result = replay_string()) != NULL) { |
| 187 |
strcpy(s, result); |
| 188 |
puts(result); |
| 189 |
fflush(stdout); |
| 190 |
zleep(1000); |
| 191 |
} else { |
| 192 |
do { |
| 193 |
int ch; |
| 194 |
char *d = s; |
| 195 |
result = s; |
| 196 |
while ((ch = getchar()) != EOF && ch != '\n') { |
| 197 |
if ((d - s) < BUF_SIZE - 2) |
| 198 |
*d++ = (char) ch; |
| 199 |
} |
| 200 |
*d = 0; |
| 201 |
} while (!*s); |
| 202 |
} |
| 203 |
|
| 204 |
if (LOG_ENABLED) { |
| 205 |
fputs(READ_STR, log_fp); |
| 206 |
put_string(log_fp, result); |
| 207 |
fputs("\n", log_fp); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
/* |
| 212 |
* Flush input buffer, make sure no pending input character |
| 213 |
*/ |
| 214 |
void |
| 215 |
inflush(void) |
| 216 |
{ |
| 217 |
int val; |
| 218 |
|
| 219 |
#ifdef HAVE_RDCHK |
| 220 |
while (rdchk(0)) |
| 221 |
read(0, &val, 1); |
| 222 |
#else |
| 223 |
#if USE_FIONREAD |
| 224 |
int l1; |
| 225 |
ioctl(0, FIONREAD, &l1); |
| 226 |
while (l1-- > 0L) { |
| 227 |
if ((int) read(0, &val, (size_t) 1) <= 0) |
| 228 |
break; |
| 229 |
} |
| 230 |
#else |
| 231 |
while (read(2, &val, (size_t) 1) > 0) ; |
| 232 |
#endif |
| 233 |
#endif |
| 234 |
} |
| 235 |
|
| 236 |
void |
| 237 |
holdit(void) |
| 238 |
{ |
| 239 |
inflush(); |
| 240 |
tprintf("Push <RETURN>"); |
| 241 |
readnl(); |
| 242 |
} |
| 243 |
|
| 244 |
void |
| 245 |
readnl(void) |
| 246 |
{ |
| 247 |
char *result; |
| 248 |
|
| 249 |
if (is_replaying() && (result = replay_string()) != NULL) { |
| 250 |
puts(result); |
| 251 |
fflush(stdout); |
| 252 |
zleep(1000); |
| 253 |
} else { |
| 254 |
int ch = '\0'; |
| 255 |
char one_byte = '\0'; |
| 256 |
|
| 257 |
fflush(stdout); |
| 258 |
brkrd = FALSE; |
| 259 |
reading = TRUE; |
| 260 |
do { |
| 261 |
if (read(0, &one_byte, (size_t) 1) < 0) { |
| 262 |
break; |
| 263 |
} else { |
| 264 |
ch = (int) one_byte; |
| 265 |
} |
| 266 |
} while (ch != '\n' && !brkrd); |
| 267 |
if (brkrd) |
| 268 |
give_up(SIGTERM); |
| 269 |
reading = FALSE; |
| 270 |
} |
| 271 |
|
| 272 |
if (LOG_ENABLED) { |
| 273 |
fputs(READ_STR "\n", log_fp); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
/* |
| 278 |
* Sleep and do nothing (don't waste CPU) for t milliseconds |
| 279 |
*/ |
| 280 |
void |
| 281 |
zleep(int t) |
| 282 |
{ |
| 283 |
#ifdef HAVE_USLEEP |
| 284 |
unsigned msecs = (unsigned) (t * 1000); |
| 285 |
usleep(msecs); |
| 286 |
#else |
| 287 |
unsigned secs = (unsigned) (t / 1000); |
| 288 |
if (secs == 0) |
| 289 |
secs = 1; |
| 290 |
sleep(secs); /* UNIX can only sleep whole seconds */ |
| 291 |
#endif |
| 292 |
} |