Debian Codegraph @

setup.c

raw
1 /* $Id: setup.c,v 1.55 2024/12/05 00:40:24 tom Exp $ */
2
3 #include <vttest.h>
4 #include <esc.h>
5 #include <ttymodes.h>
6
7 #define PARSE_NAME(mode) \
8 ((mode > 1) \
9 ? "C2" \
10 : (mode == 1 \
11 ? "8-bit" \
12 : "7-bit"))
13
14 static int cur_level = -1; /* current operating level (VT100=1) */
15 static int max_level = -1; /* maximum operating level */
16
17 static int
18 check_8bit_toggle(void)
19 {
20 int result = FALSE;
21 char *report;
22
23 set_tty_raw(TRUE);
24
25 pause_replay();
26 cup(1, 1);
27 dsr(6);
28 padding(5); /* may not be needed */
29 report = get_reply();
30 resume_replay();
31
32 restore_ttymodes();
33
34 if ((report = skip_csi(report)) != NULL
35 && !strcmp(report, "1;1R"))
36 result = TRUE;
37 if (LOG_ENABLED) {
38 fprintf(log_fp, NOTE_STR "%svalid response from DSR 6\n",
39 result ? "" : "no ");
40 }
41 return result;
42 }
43
44 /*
45 * Determine the current and maximum operating levels of the terminal
46 */
47 static void
48 find_levels(void)
49 {
50 char *report;
51 int actual_id = -1;
52 int has_color = 0;
53
54 set_tty_raw(TRUE);
55 set_tty_echo(FALSE);
56
57 da();
58 report = get_reply();
59
60 cur_level =
61 max_level = 1; /* assume a VT100 */
62
63 if (!strcmp(report, "\033/Z")) {
64 actual_id = 52;
65 cur_level =
66 max_level = 0; /* must be a VT52 */
67 } else if ((report = skip_csi(report)) == NULL || strlen(report) < 3) {
68 actual_id = 100;
69 } else if (*report == '?') {
70 int pos = 1;
71 int check = scan_DA(report, &pos);
72 switch (check) {
73 default:
74 case 1:
75 actual_id = 100;
76 break;
77 case 2:
78 case 6:
79 actual_id = 102;
80 break;
81 case 4:
82 actual_id = 132;
83 break;
84 case 7:
85 actual_id = 131;
86 break;
87 case 12:
88 actual_id = 125;
89 break;
90 case 62:
91 actual_id = 220;
92 break;
93 case 63:
94 actual_id = 320;
95 break;
96 case 64:
97 actual_id = 420;
98 break;
99 case 65:
100 actual_id = 520;
101 break;
102 }
103 cur_level = max_level = (actual_id / 100);
104 }
105
106 if (max_level >= 4) {
107 int pos = 1; /* index of first digit */
108 int check;
109 /*
110 * Check for color feature (see below) needed in initialization of VT525.
111 * Other terminals may advertise this feature, but only VT525 will change
112 * its response in DECRQCRA according to color.
113 */
114 while ((check = scan_DA(report, &pos)) >= 0) {
115 if (check == 22) {
116 has_color = 1;
117 break;
118 }
119 }
120 decrqss("\"p");
121 report = get_reply();
122 if ((report = skip_dcs(report)) != NULL
123 && isdigit(CharOf(*report++)) /* 0 or 1 (by observation, though 1 is an err) */
124 &&*report++ == '$'
125 && *report++ == 'r'
126 && *report++ == '6'
127 && isdigit(CharOf(*report)))
128 cur_level = *report - '0';
129 }
130
131 if (max_level >= 2) {
132 do_csi(">c"); /* or "CSI > 0 c" */
133 report = get_reply();
134 if ((report = skip_csi(report)) != NULL && *report == '>') {
135 int pos = 1; /* index of first digit */
136 switch (scan_DA(report, &pos)) {
137 case 1:
138 actual_id = 220;
139 break;
140 case 2:
141 actual_id = 240;
142 break;
143 case 18:
144 actual_id = 330;
145 break;
146 case 19:
147 actual_id = 340;
148 break;
149 case 24:
150 actual_id = 320;
151 break;
152 case 28:
153 actual_id = 330;
154 break;
155 case 32:
156 actual_id = 382;
157 break;
158 case 48:
159 actual_id = 382;
160 break;
161 case 41:
162 actual_id = 420;
163 break;
164 case 61:
165 actual_id = 510;
166 break;
167 case 64:
168 actual_id = 520;
169 break;
170 case 65:
171 actual_id = 525;
172 break;
173 }
174 }
175 }
176
177 /*
178 * If the terminal advertises color (a VT525 feature), initialize the
179 * text color to the default values, to simplify the DECRQCRA test.
180 * Defaults are documented in EK-VT520-RM, p 2-32.
181 */
182 if (has_color && actual_id == 525) {
183 use_decac = 1;
184 decac(1, 7, 0); /* normal text: assign white-on-black */
185 }
186
187 if (LOG_ENABLED) {
188 fprintf(log_fp, NOTE_STR "Max Operating Level: %d\n", max_level);
189 fprintf(log_fp, NOTE_STR "Cur Operating Level: %d\n", cur_level);
190 fprintf(log_fp, NOTE_STR "Derived terminal-id: %d\n", actual_id);
191 }
192
193 restore_ttymodes();
194 }
195
196 static int
197 toggle_DECSCL(MENU_ARGS)
198 {
199 int request = cur_level;
200
201 if (max_level <= 1) {
202 vt_move(1, 1);
203 printxx("Sorry, terminal supports only VT%d", terminal_id());
204 vt_move(max_lines - 1, 1);
205 return MENU_HOLD;
206 }
207
208 if (++request > max_level)
209 request = 1;
210 set_level(request);
211
212 restore_ttymodes();
213 return MENU_NOHOLD;
214 }
215
216 static int
217 toggle_Logging(MENU_ARGS)
218 {
219 if (log_fp == NULL)
220 enable_logging(NULL);
221 else
222 log_disabled = !log_disabled;
223 return MENU_NOHOLD;
224 }
225
226 static int
227 toggle_Padding(MENU_ARGS)
228 {
229 use_padding = !use_padding;
230 return MENU_NOHOLD;
231 }
232
233 static int
234 toggle_Slowly(MENU_ARGS)
235 {
236 slow_motion = !slow_motion;
237 return MENU_NOHOLD;
238 }
239
240 static int
241 toggle_8bit_in(MENU_ARGS)
242 {
243 int old = input_8bits;
244
245 s8c1t(!old);
246 fflush(stdout);
247 if (!check_8bit_toggle()) {
248 input_8bits = old;
249 vt_clear(2);
250 vt_move(1, 1);
251 println("Sorry, this terminal does not support 8-bit input controls");
252 return MENU_HOLD;
253 }
254 return MENU_NOHOLD;
255 }
256
257 static int
258 toggle_7bit_fsm(MENU_ARGS)
259 {
260 int save_7bits = parse_7bits;
261
262 /*
263 * See comment in put_char(). There are two cases for the parse_7bits value.
264 * In both cases, 7-bit controls should work when parse_7bits is zero. The
265 * tests relate to the nonzero value:
266 *
267 * parse_7bits == 1:
268 * This is for the 8-bit environment, i.e., 8-bit controls should work. But
269 * ECMA-49 section 9 comes into play because 7-bit controls are parsed
270 * ignoring the 8th bit. The C1 controls have specific definitons which
271 * override that, but by ignoring the 8th bit that guarantees that after the
272 * initial C0 or C1 byte, all of the following characters will be parsed
273 * without the 8th bit.
274 *
275 * This case is tested by setting the 8th bit in the controls to ensure that
276 * the parser has to deal with that situation.
277 *
278 * parse_7bits == 2:
279 * This is for the UTF-8 environment. C1 controls in put_char() are recoded
280 * to UTF-8 to test if the terminal can interpret those. Normally vttest
281 * resets UTF-8 mode on startup; the "-u" option suppresses that, allowing
282 * this test.
283 */
284 if (assume_utf8 && allows_utf8) {
285 parse_7bits = parse_7bits ? 0 : 2;
286 } else {
287 parse_7bits = parse_7bits ? 0 : 1;
288 }
289 if (LOG_ENABLED) {
290 fprintf(log_fp, NOTE_STR "toggle parser from %s to %s\n",
291 PARSE_NAME(save_7bits),
292 PARSE_NAME(parse_7bits));
293 }
294
295 /*
296 * If we're told to switch to 8-bit or C2 mode, check that we can do that.
297 */
298 if (parse_7bits != 0) {
299 int save_output = output_8bits;
300 int rc;
301 output_8bits = 1;
302 rc = check_8bit_toggle();
303 output_8bits = save_output;
304 if (!rc) {
305 int requested = parse_7bits;
306 char msg[80];
307 parse_7bits = save_7bits;
308 vt_clear(2);
309 vt_move(1, 1);
310 sprintf(msg,
311 "Sorry, this terminal does not fully support %s parsing",
312 PARSE_NAME(requested));
313 println(msg);
314 return MENU_HOLD;
315 }
316 output_8bits = save_output;
317 } else {
318 output_8bits = 0;
319 }
320
321 return MENU_NOHOLD;
322 }
323
324 /*
325 * This changes the CSI code to/from an escape sequence.
326 */
327 static int
328 toggle_8bit_out(MENU_ARGS)
329 {
330 int old = output_8bits;
331
332 fflush(stdout);
333 output_8bits = !output_8bits;
334 if (!check_8bit_toggle()) {
335 output_8bits = old;
336 vt_clear(2);
337 vt_move(1, 1);
338 println("Sorry, this terminal does not support 8-bit output controls");
339 return MENU_HOLD;
340 }
341 return MENU_NOHOLD;
342 }
343
344 /******************************************************************************/
345
346 /*
347 * While processing command-lines, "pathname" is non-null. After that, it can
348 * be null to temporarily disable logging.
349 */
350 void
351 enable_logging(const char *pathname)
352 {
353 static char *my_name;
354 if (pathname != NULL) {
355 free(my_name);
356 my_name = strdup(pathname);
357 }
358 if (my_name != NULL) {
359 log_fp = fopen(my_name, "w");
360 if (log_fp == NULL) {
361 failed(my_name);
362 }
363 }
364 }
365
366 void
367 reset_level(void)
368 {
369 cur_level = max_level;
370 }
371
372 void
373 restore_level(const VTLEVEL *save)
374 {
375 set_level(save->cur_level);
376 if (cur_level > 1
377 && save->input_8bits != input_8bits) /* just in case level didn't change */
378 s8c1t(save->input_8bits);
379 output_8bits = save->output_8bits; /* in case we thought this was VT100 */
380 parse_7bits = save->parse_7bits; /* in case we ignored ECMA-48 */
381 }
382
383 void
384 save_level(VTLEVEL *save)
385 {
386 save->cur_level = cur_level;
387 save->input_8bits = input_8bits;
388 save->output_8bits = output_8bits;
389 save->parse_7bits = parse_7bits;
390
391 if (LOG_ENABLED)
392 fprintf(log_fp, NOTE_STR "save_level(%d) in=%d, out=%d, fsm=%d\n",
393 cur_level,
394 input_8bits ? 8 : 7,
395 output_8bits ? 8 : 7,
396 parse_7bits ? 8 : 7);
397 }
398
399 int
400 get_level(void)
401 {
402 return cur_level;
403 }
404
405 int
406 set_level(int request)
407 {
408 if (cur_level < 0)
409 find_levels();
410
411 if (LOG_ENABLED)
412 fprintf(log_fp, NOTE_STR "set_level(%d)\n", request);
413
414 if (request > max_level) {
415 printxx("Sorry, this terminal supports only VT%d\n", terminal_id());
416 return FALSE;
417 }
418
419 if (request != cur_level) {
420 if (request == 0) {
421 rm("?2"); /* Reset ANSI (VT100) mode, Set VT52 mode */
422 input_8bits = FALSE;
423 output_8bits = FALSE;
424 parse_7bits = FALSE;
425 } else {
426 if (cur_level == 0) {
427 esc("<"); /* Enter ANSI mode (VT100 mode) */
428 }
429 if (request == 1) {
430 input_8bits = FALSE;
431 output_8bits = FALSE;
432 parse_7bits = FALSE;
433 }
434 if (max_level > 1) {
435 if (request > 1)
436 do_csi("6%d;%d\"p", request, !input_8bits);
437 else
438 do_csi("61\"p");
439 }
440 }
441 padding(5); /* may not be needed */
442
443 cur_level = request;
444 }
445
446 if (LOG_ENABLED)
447 fprintf(log_fp, NOTE_STR "...set_level(%d) in=%d, out=%d, fsm=%d\n",
448 cur_level,
449 input_8bits ? 8 : 7,
450 output_8bits ? 8 : 7,
451 parse_7bits ? 8 : 7);
452
453 return TRUE;
454 }
455
456 /*
457 * Set the terminal's operating level to the default (i.e., based on what the
458 * terminal returns as a response to DA).
459 */
460 void
461 default_level(void)
462 {
463 if (max_level < 0)
464 find_levels();
465 set_level(max_level);
466 }
467
468 int
469 terminal_id(void)
470 {
471 if (max_level >= 1)
472 return max_level * 100;
473 else if (max_level == 0)
474 return 52;
475 return 100;
476 }
477
478 int
479 tst_setup(MENU_ARGS)
480 {
481 static char txt_output[80] = "send 7/8";
482 static char txt_input8[80] = "receive 7/8";
483 static char txt_parse7[80] = "parser 7/8";
484 static char txt_DECSCL[80] = "DECSCL";
485 static char txt_logging[80] = "logging";
486 static char txt_padded[80] = "padding";
487 static char txt_slowly[80];
488 /* *INDENT-OFF* */
489 static MENU my_menu[] = {
490 { "Exit", NULL },
491 { "Setup terminal to original test-configuration", setup_terminal },
492 { txt_output, toggle_8bit_out },
493 { txt_input8, toggle_8bit_in },
494 { txt_parse7, toggle_7bit_fsm },
495 { txt_DECSCL, toggle_DECSCL },
496 { txt_logging, toggle_Logging },
497 { txt_padded, toggle_Padding },
498 { txt_slowly, toggle_Slowly },
499 { "", NULL }
500 };
501 /* *INDENT-ON* */
502
503 if (cur_level < 0)
504 find_levels();
505
506 do {
507 sprintf(txt_output, "Send %s controls",
508 (output_8bits
509 ? ((parse_7bits > 1)
510 ? "C2"
511 : "8-bit")
512 : "7-bit"));
513 sprintf(txt_input8, "Receive %d-bit controls", input_8bits ? 8 : 7);
514 switch (parse_7bits) {
515 default:
516 case 1:
517 sprintf(txt_parse7, "Assume %d-bit parser (see ECMA-48 section 9)",
518 parse_7bits ? 7 : 8);
519 break;
520 case 2:
521 sprintf(txt_parse7,
522 "Assume parser accepts C2 (Unicode.org two-byte encoding)");
523 break;
524 }
525 sprintf(txt_DECSCL, "Operating level %d (VT%d)",
526 cur_level, cur_level ? cur_level * 100 : 52);
527 sprintf(txt_logging, "Logging %s", STR_ENABLED(LOG_ENABLED));
528 sprintf(txt_padded, "Padding %s", STR_ENABLED(use_padding));
529 sprintf(txt_slowly, "Slow-movement/scrolling %s", STR_ENABLED(slow_motion));
530
531 vt_clear(2);
532 __(title(0), println("Modify test-parameters"));
533 __(title(2), println("Select a number to modify it:"));
534 } while (menu(my_menu));
535 return MENU_NOHOLD;
536 }