Debian Codegraph @

vms_io.c

raw
1 /* $Id: vms_io.c,v 1.32 2024/10/16 00:19:36 tom Exp $ */
2
3 #define DEBUG
4
5 #include <stdarg.h>
6 #include <vttest.h>
7 #include <esc.h>
8 #include <ttymodes.h>
9
10 #include <starlet.h>
11 #include <lib$routines.h>
12 #include <stsdef.h>
13 #include <ssdef.h>
14 #include <descrip.h>
15 #include <iodef.h>
16 #include <ttdef.h>
17 #include <tt2def.h>
18 #include <cvtdef.h> /* for float constants */
19
20 typedef struct {
21 unsigned short int status; /* I/O completion status */
22 unsigned short int count; /* byte transfer count */
23 int dev_dep_data; /* device dependent data */
24 } QIO_SB; /* This is a QIO I/O Status Block */
25
26 #define NIBUF 1024 /* Input buffer size */
27 #define NOBUF 1024 /* MM says big buffers win! */
28 #define EFN 0 /* Event flag */
29
30 static char obuf[NOBUF]; /* Output buffer */
31 static int nobuf; /* # of bytes in above */
32 static char ibuf[NIBUF]; /* Input buffer */
33 static int nibuf; /* # of bytes in above */
34 static int oldmode[3]; /* Old TTY mode bits */
35 static int newmode[3]; /* New TTY mode bits */
36 static short iochan; /* TTY I/O channel */
37
38 static int in_flags;
39 static int cr_flag = TRUE;
40
41 static void
42 give_up(int status)
43 {
44 if (LOG_ENABLED)
45 fprintf(log_fp, NOTE_STR "status=%#x\n", status);
46 close_tty();
47 exit(status);
48 }
49
50 static int
51 lookup_speed(int code)
52 {
53 /* *INDENT-OFF* */
54 static struct {
55 int code;
56 int speed;
57 } table[] = {
58 {TT$C_BAUD_50, 50 }
59 ,{TT$C_BAUD_75, 75 }
60 ,{TT$C_BAUD_110, 110 }
61 ,{TT$C_BAUD_134, 134 }
62 ,{TT$C_BAUD_150, 150 }
63 ,{TT$C_BAUD_300, 300 }
64 ,{TT$C_BAUD_600, 600 }
65 ,{TT$C_BAUD_1200, 1200 }
66 ,{TT$C_BAUD_1800, 1800 }
67 ,{TT$C_BAUD_2000, 2000 }
68 ,{TT$C_BAUD_2400, 2400 }
69 ,{TT$C_BAUD_3600, 3600 }
70 ,{TT$C_BAUD_4800, 4800 }
71 ,{TT$C_BAUD_7200, 7200 }
72 ,{TT$C_BAUD_9600, 9600 }
73 ,{TT$C_BAUD_19200, 19200 }
74 ,{TT$C_BAUD_38400, 38400 }
75 #ifdef TT$C_BAUD_57600
76 ,{TT$C_BAUD_57600, 57600 }
77 ,{TT$C_BAUD_76800, 76800 }
78 ,{TT$C_BAUD_115200, 115200 }
79 #endif
80 };
81 /* *INDENT-ON* */
82
83 int n;
84 int speed = DEFAULT_SPEED;
85 for (n = 0; n < TABLESIZE(table); n++) {
86 if (table[n].code == code) {
87 if (table[n].speed > DEFAULT_SPEED)
88 speed = table[n].speed;
89 break;
90 }
91 }
92 return speed;
93 }
94
95 /*
96 * Read the tty-in. If we're looking for a single character, wait. Otherwise,
97 * read whatever is available, assuming that it's queued and ready.
98 *
99 * The VMS terminal driver operates with a one-second timer, and (from the
100 * manual) we must give at least two seconds for the timeout value. If we
101 * give a 0-second timeout, we'll only get (from testing) 16 bytes, which
102 * is enough for a CPR, but not for DA, etc.
103 */
104 static void
105 read_vms_tty(int length, int timed)
106 {
107 int status;
108 QIO_SB iosb;
109 int term[2] =
110 {0, 0};
111 int my_flags = IO$_READLBLK | in_flags;
112 int timeout = 0;
113
114 if (length < 1)
115 return;
116 if (length > 1) {
117 my_flags |= IO$M_TIMED;
118 timeout = 1; /* seconds */
119 }
120 if (timed)
121 timeout = 2; /* seconds */
122
123 #ifdef DEBUG
124 if (LOG_ENABLED) {
125 fprintf(log_fp, NOTE_STR "reading: len=%d, flags=%#x\n", length, my_flags);
126 fflush(log_fp);
127 }
128 #endif
129 status = sys$qiow(EFN, iochan, my_flags,
130 &iosb, 0, 0, ibuf, length, timeout, term, 0, 0);
131 #ifdef DEBUG
132 if (LOG_ENABLED) {
133 fprintf(log_fp,
134 NOTE_STR "read: st=%d, cnt=%#x, dev=%#x\n",
135 iosb.status, iosb.count, iosb.dev_dep_data);
136 fflush(log_fp);
137 }
138 #endif
139
140 if (status != SS$_NORMAL
141 || iosb.status == SS$_ENDOFFILE)
142 give_up(status);
143
144 nibuf = iosb.count + (iosb.dev_dep_data >> 16);
145 }
146
147 /******************************************************************************/
148
149 void
150 reset_inchar(void)
151 {
152 /* FIXME */
153 }
154
155 /*
156 * Wait until a character is typed on the terminal then read it, without
157 * waiting for CR.
158 */
159 char
160 inchar(void)
161 {
162 int c;
163
164 fflush(stdout);
165 read_vms_tty(1, FALSE);
166 c = (ibuf[0] & 0xff);
167 if (c == '\r' && cr_flag && !(in_flags & IO$M_NOFILTR)) {
168 c = '\n';
169 if (in_flags & IO$M_NOECHO)
170 putchar(c);
171 }
172
173 if (active)
174 pause_replay();
175 if (LOG_ENABLED) {
176 fputs(READ_STR, log_fp);
177 put_char(log_fp, c);
178 fputs("\n", log_fp);
179 }
180 if (active)
181 resume_replay();
182
183 return c;
184 }
185
186 /*
187 * Get an unfinished string from the terminal: wait until a character is typed
188 * on the terminal, then read it, and all other available characters. Return a
189 * pointer to that string.
190 *
191 * This function won't read long strings, but we use it for instances where
192 * we only expect short (16 bytes or less) replies (i.e., a function-key).
193 */
194 char *
195 instr(void)
196 {
197 static char result[BUF_SIZE];
198
199 FILE *save = log_fp;
200
201 pause_replay();
202 log_fp = NULL;
203 result[0] = inchar();
204 zleep(100); /* Wait 0.1 seconds */
205 log_fp = save;
206
207 read_vms_tty(sizeof(result) - 3, FALSE);
208 memcpy(result + 1, ibuf, nibuf);
209 result[1 + nibuf] = '\0';
210
211 if (LOG_ENABLED) {
212 fputs(READ_STR, log_fp);
213 put_string(log_fp, result);
214 fputs("\n", log_fp);
215 }
216 resume_replay();
217
218 return (result);
219 }
220
221 /*
222 * Get an unfinished string from the terminal: wait until a character is typed
223 * on the terminal, then read it, and all other available characters. Return a
224 * pointer to that string.
225 *
226 * Unlike 'instr()', this will really read long strings.
227 */
228 char *
229 get_reply(void)
230 {
231 static char result[256];
232
233 result[0] = inchar();
234 zleep(100); /* Wait 0.1 seconds */
235 fflush(stdout);
236 pause_replay();
237
238 read_vms_tty(sizeof(result) - 3, TRUE);
239 memcpy(result + 1, ibuf, nibuf);
240 result[1 + nibuf] = '\0';
241
242 if (LOG_ENABLED) {
243 fputs(READ_STR, log_fp);
244 put_string(log_fp, result);
245 fputs("\n", log_fp);
246 }
247
248 resume_replay();
249
250 return (result);
251 }
252
253 /*
254 * Read to the next newline, truncating the buffer at BUF_SIZE-1 characters
255 */
256 void
257 inputline(char *s)
258 {
259 char *result = s;
260
261 if (is_replaying() && (result = replay_string()) != NULL) {
262 strcpy(s, result);
263 puts(result);
264 fflush(stdout);
265 zleep(1000);
266 } else {
267 do {
268 int ch;
269 char *d = s;
270 while ((ch = getchar()) != EOF && ch != '\n') {
271 if ((d - s) < BUF_SIZE - 2)
272 *d++ = ch;
273 }
274 *d = 0;
275 } while (!*s);
276 }
277
278 if (LOG_ENABLED)
279 fprintf(log_fp, READ_STR "%s\n", result);
280 }
281
282 /*
283 * Flush input buffer, make sure no pending input character
284 */
285 void
286 inflush(void)
287 {
288 nibuf = 0;
289 }
290
291 void
292 outflush(void)
293 {
294 fflush(stdout);
295 }
296
297 void
298 holdit(void)
299 {
300 inflush();
301 tprintf("Push <RETURN>");
302 readnl();
303 }
304
305 void
306 readnl(void)
307 {
308 if (is_replaying() && (result = replay_string()) != NULL) {
309 puts(result);
310 fflush(stdout);
311 zleep(1000);
312 } else {
313 fflush(stdout);
314 while (inchar() != '\n') ;
315 }
316 if (LOG_ENABLED)
317 fputs(READ_STR "\n", log_fp);
318 }
319
320 /*
321 * Sleep and do nothing (don't waste CPU) for t milliseconds
322 */
323 void
324 zleep(int t)
325 {
326 float seconds = t / 1000.;
327 unsigned flags = 0;
328 #ifdef USE_IEEE_FLOAT
329 unsigned type = CVT$K_IEEE_S;
330 #else
331 unsigned type = CVT$K_VAX_F;
332 #endif
333 lib$wait(&seconds, &flags, &type);
334 }
335
336 /******************************************************************************/
337
338 void
339 init_ttymodes(int pn)
340 {
341 struct dsc$descriptor idsc;
342 struct dsc$descriptor odsc;
343 char oname[40];
344 QIO_SB iosb;
345 int status;
346
347 /* *INDENT-EQLS* */
348 odsc.dsc$a_pointer = "TT";
349 odsc.dsc$w_length = strlen(odsc.dsc$a_pointer);
350 odsc.dsc$b_dtype = DSC$K_DTYPE_T;
351 odsc.dsc$b_class = DSC$K_CLASS_S;
352 idsc.dsc$b_dtype = DSC$K_DTYPE_T;
353 idsc.dsc$b_class = DSC$K_CLASS_S;
354
355 do {
356 /* *INDENT-EQLS* */
357 idsc.dsc$a_pointer = odsc.dsc$a_pointer;
358 idsc.dsc$w_length = odsc.dsc$w_length;
359 odsc.dsc$a_pointer = &oname[0];
360 odsc.dsc$w_length = sizeof(oname);
361
362 status = lib$sys_trnlog(&idsc, &odsc.dsc$w_length, &odsc);
363 if (status != SS$_NORMAL
364 && status != SS$_NOTRAN)
365 give_up(status);
366 if (oname[0] == 0x1B) {
367 odsc.dsc$a_pointer += 4;
368 odsc.dsc$w_length -= 4;
369 }
370 } while (status == SS$_NORMAL);
371
372 status = sys$assign(&odsc, &iochan, 0, 0);
373 if (status != SS$_NORMAL)
374 give_up(status);
375
376 status = sys$qiow(EFN, iochan, IO$_SENSEMODE, &iosb, 0, 0,
377 oldmode, sizeof(oldmode), 0, 0, 0, 0);
378 if (status != SS$_NORMAL
379 || iosb.status != SS$_NORMAL)
380 give_up(status);
381 #ifdef DEBUG
382 if (LOG_ENABLED)
383 fprintf(log_fp,
384 NOTE_STR "sense: st=%d, cnt=%#x, dev=%#x\n",
385 iosb.status, iosb.count, iosb.dev_dep_data);
386 #endif
387
388 newmode[0] = oldmode[0];
389 newmode[1] = oldmode[1] | TT$M_EIGHTBIT;
390 newmode[1] &= ~(TT$M_TTSYNC | TT$M_HOSTSYNC);
391
392 /* Note: this assumes we're doing IO$_SETCHAR */
393 newmode[2] = oldmode[2] | TT2$M_PASTHRU;
394
395 status = sys$qiow(EFN, iochan, IO$_SETMODE, &iosb, 0, 0,
396 newmode, sizeof(newmode), 0, 0, 0, 0);
397 if (status != SS$_NORMAL
398 || iosb.status != SS$_NORMAL)
399 give_up(status);
400
401 max_lines = (newmode[1] >> 24);
402 min_cols = newmode[0] >> 16;
403 tty_speed = lookup_speed(iosb.count & 0xff);
404
405 if (LOG_ENABLED) {
406 fprintf(log_fp, NOTE_STR "TTY modes %#x, %#x, %#x\n",
407 oldmode[0], oldmode[1], oldmode[2]);
408 fprintf(log_fp, NOTE_STR "iosb.count = %#x\n", iosb.count);
409 fprintf(log_fp, NOTE_STR "iosb.dev_dep_data = %#x\n", iosb.dev_dep_data);
410 fprintf(log_fp, NOTE_STR "TTY speed = %d\n", tty_speed);
411 }
412 }
413
414 /*
415 * Restore state to "normal", for menu-prompting
416 */
417 void
418 restore_ttymodes(void)
419 {
420 outflush();
421 in_flags = 0;
422 cr_flag = TRUE;
423 }
424
425 void
426 close_tty(void)
427 {
428 int status;
429 QIO_SB iosb;
430
431 status = sys$qiow(EFN, iochan, IO$_SETMODE, &iosb, 0, 0,
432 oldmode, sizeof(oldmode), 0, 0, 0, 0);
433 if (status == SS$_IVCHAN)
434 return; /* already closed it */
435 (void) sys$dassgn(iochan);
436 }
437
438 void
439 set_tty_crmod(int enabled)
440 {
441 cr_flag = enabled;
442 }
443
444 void
445 set_tty_echo(int enabled)
446 {
447 if (enabled)
448 in_flags &= ~IO$M_NOECHO;
449 else
450 in_flags |= IO$M_NOECHO;
451 }
452
453 void
454 set_tty_raw(int enabled)
455 {
456 if (enabled)
457 in_flags |= IO$M_NOFILTR;
458 else
459 in_flags &= ~IO$M_NOFILTR;
460 }