Debian Codegraph @

main.c

raw
1 /* $Id: main.c,v 1.155 2024/12/05 00:37:39 tom Exp $ */
2
3 /*
4 VTTEST.C
5
6 Written November 1983 - July 1984 by Per Lindberg,
7 Stockholm University Computer Center (QZ), Sweden.
8
9 THE MAD PROGRAMMER STRIKES AGAIN!
10
11 Copyright (c) 1984, Per Lindberg
12 All rights reserved.
13
14 Redistribution and use in source and binary forms, with or without
15 modification, are permitted provided that the following conditions are
16 met:
17 1. Redistributions of source code must retain the above copyright
18 notice, this list of conditions and the following disclaimer.
19 2. Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in
21 the documentation and/or other materials provided with the
22 distribution.
23 3. Neither the name of Per Lindberg nor the names of contributors may
24 be used to endorse or promote products derived from this software
25 without specific prior written permission.
26
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include <vttest.h>
41 #include <ttymodes.h>
42 #include <esc.h>
43
44 #ifdef LOCALE
45 #include <locale.h>
46 #endif
47
48 #ifdef HAVE_LANGINFO_CODESET
49 #include <langinfo.h>
50 #endif
51
52 /* *INDENT-EQLS* */
53 FILE *log_fp = NULL;
54 int allows_utf8 = FALSE;
55 int assume_utf8 = FALSE;
56 int brkrd = FALSE;
57 int debug_level = 0;
58 int decac_bg = -1;
59 int decac_fg = -1;
60 int input_8bits = FALSE;
61 int log_disabled = FALSE;
62 int max_cols = 132;
63 int max_lines = 24;
64 int min_cols = 80;
65 int output_8bits = FALSE;
66 int parse_7bits = FALSE;
67 int quick_reply = FALSE;
68 int reading = FALSE;
69 int slow_motion = FALSE;
70 int tty_speed = DEFAULT_SPEED; /* nominal speed, for padding */
71 int use_decac = FALSE;
72 int use_padding = FALSE;
73
74 jmp_buf intrenv;
75
76 static char *program;
77 static char empty[1];
78 static char *current_menu = empty;
79
80 GCC_NORETURN void
81 failed(const char *msg)
82 {
83 #ifdef HAVE_STRERROR
84 const char *why = strerror(errno);
85 fprintf(stderr, "%s: %s: %s\n", program, msg, why ? why : "?");
86 #elif defined(HAVE_SYS_NERR) && defined(HAVE_SYS_ERRLIST)
87 const char *why = (errno > 0 && errno < sys_nerr) ? sys_errlist[errno] : "?";
88 fprintf(stderr, "%s: %s: %s\n", program, msg, why ? why : "?");
89 #else
90 perror(msg);
91 #endif
92 exit(EXIT_FAILURE);
93 }
94
95 GCC_NORETURN void
96 no_memory(void)
97 {
98 failed("no memory");
99 }
100
101 static void
102 usage(void)
103 {
104 static const char *msg[] =
105 {
106 "Usage: vttest [options] [24x80.132]"
107 ,""
108 ,"Options:"
109 ," -V print the program version, and exit"
110 ," -8 use 8-bit controls"
111 ," -d debug (repeat for more detail)"
112 ," -c cmdfile read commands from file"
113 ," -f fontfile load DRCS data from file"
114 ," -l logfile log test results to vttest.log"
115 ," -p use padding"
116 ," -q filter replies to show only the most recent"
117 ," -s add time delay for scrolling"
118 ," -u allow some tests to use UTF-8"
119 };
120 size_t n;
121
122 for (n = 0; n < TABLESIZE(msg); ++n)
123 fprintf(stderr, "%s\n", msg[n]);
124
125 exit(EXIT_FAILURE);
126 }
127
128 static int
129 version(void)
130 {
131 printxx("VT100 test program, version %d.%d", RELEASE, PATCHLEVEL);
132 #ifdef PATCH_DATE
133 if (PATCH_DATE)
134 printxx(" (%d)", PATCH_DATE);
135 #endif
136 return 1; /* used for indenting */
137 }
138
139 int
140 main(int argc, char *argv[])
141 {
142 /* *INDENT-OFF* */
143 static MENU mainmenu[] = {
144 { "Exit", NULL },
145 { "Test of cursor movements", tst_movements },
146 { "Test of screen features", tst_screen },
147 { "Test of character sets", tst_characters },
148 { "Test of double-sized characters", tst_doublesize },
149 { "Test of keyboard", tst_keyboard },
150 { "Test of terminal reports", tst_reports },
151 { "Test of VT52 mode", tst_vt52 },
152 { "Test of VT102 features (Insert/Delete Char/Line)", tst_insdel },
153 { "Test of known bugs", tst_bugs },
154 { "Test of reset and self-test", tst_rst },
155 { "Test non-VT100 (e.g., VT220, XTERM) terminals", tst_nonvt100 },
156 { "Modify test-parameters", tst_setup },
157 { "", NULL }
158 };
159 /* *INDENT-ON* */
160 char *opt_command = NULL;
161 char *opt_softchr = NULL;
162 char *opt_logging = NULL;
163
164 program = strrchr(argv[0], '/');
165 if (program != NULL)
166 ++program;
167 else
168 program = argv[0];
169
170 #define OPT_ARG(value) \
171 do { \
172 if (!*++opt) { \
173 if (--argc < 1) \
174 usage(); \
175 value = *++argv; \
176 } \
177 opt = "?"; \
178 } while (0)
179
180 while (argc-- > 1) {
181 const char *opt = *++argv;
182 if (*opt == '-') {
183 while (*++opt != '\0') {
184 switch (*opt) {
185 case 'V':
186 version();
187 putchar('\n');
188 exit(EXIT_SUCCESS);
189 case 'c':
190 OPT_ARG(opt_command);
191 break;
192 case 'd':
193 debug_level++;
194 break;
195 case 'f':
196 OPT_ARG(opt_softchr);
197 break;
198 case 'l':
199 OPT_ARG(opt_logging);
200 break;
201 case 'p':
202 use_padding = TRUE;
203 break;
204 case 'q':
205 quick_reply = TRUE;
206 break;
207 case 's':
208 slow_motion = TRUE;
209 break;
210 case 'u':
211 allows_utf8 = TRUE;
212 break;
213 case '8':
214 output_8bits = TRUE;
215 break;
216 default:
217 usage();
218 }
219 }
220 } else {
221 /*
222 * Allow user to specify geometry of terminal to accommodate quasi-VT100
223 * terminals such as Linux console and xterm.
224 */
225 char *p = argv[0];
226 char *q;
227 int values[3], n;
228
229 for (n = 0; n < 3; n++) {
230 int m;
231
232 if (!*p)
233 break;
234 if ((m = (int) strtol(p, &q, 10)) > 0) {
235 values[n] = m;
236 p = q;
237 if (*p)
238 p++;
239 } else {
240 break;
241 }
242 }
243 switch (n) {
244 case 3:
245 max_cols = values[2];
246 /* FALLTHRU */
247 case 2:
248 min_cols = values[1];
249 /* FALLTHRU */
250 case 1:
251 max_lines = values[0];
252 break;
253 }
254 if ((max_cols < min_cols) || (n == 0)) {
255 usage();
256 }
257 }
258 }
259
260 /* do this first, to capture results from other options */
261 if (opt_logging)
262 enable_logging(opt_logging);
263
264 if (opt_command)
265 setup_replay(opt_command);
266
267 if (opt_softchr)
268 setup_softchars(opt_softchr);
269
270 #ifdef UNIX
271 initterminal(setjmp(intrenv));
272 signal(SIGINT, onbrk);
273 signal(SIGTERM, onterm);
274 reading = FALSE;
275 #else
276 initterminal(0);
277 #endif
278 do {
279 vt_clear(2);
280 __(title(0), version());
281
282 title(1);
283 if (max_lines != 24
284 || min_cols != 80
285 || max_cols != 132)
286 printxx("Screen size %dx%d (%d max) ", max_lines, min_cols, max_cols);
287 if (tty_speed != DEFAULT_SPEED)
288 printxx("Line speed %dbd ", tty_speed);
289 if (use_padding)
290 printxx(" (padded)");
291
292 __(title(2), println("Choose test type:"));
293 } while (menu(mainmenu));
294 bye();
295 return EXIT_SUCCESS;
296 }
297
298 int
299 tst_movements(MENU_ARGS)
300 {
301 /* Test of:
302 CUF (Cursor Forward)
303 CUB (Cursor Backward)
304 CUD (Cursor Down) IND (Index) NEL (Next Line)
305 CUU (Cursor Up) RI (Reverse Index)
306 CUP (Cursor Position) HVP (Horizontal and Vertical Position)
307 ED (Erase in Display)
308 EL (Erase in Line)
309 DECALN (Screen Alignment Display)
310 DECAWM (Autowrap)
311 <CR> <BS>
312 Cursor control characters inside CSI sequences
313 */
314
315 int i, row, col, pass, width;
316 const char *ctext = "This is a correct sentence";
317
318 set_tty_crmod(TRUE); /* want to disable tab/space conversion */
319
320 for (pass = 0; pass <= 1; pass++) {
321 int hlfxtra;
322 int inner_l, inner_r;
323
324 if (pass == 0) {
325 deccolm(FALSE);
326 width = min_cols;
327 } else {
328 deccolm(TRUE);
329 width = max_cols;
330 }
331
332 /* Compute left/right columns for a 60-column box centered in 'width' */
333 inner_l = (width - 60) / 2;
334 inner_r = 61 + inner_l;
335 hlfxtra = (width - 80) / 2;
336
337 if (LOG_ENABLED)
338 fprintf(log_fp, NOTE_STR "tst_movements box(%d cols)\n",
339 pass ? max_cols : min_cols);
340
341 decaln();
342 cup(9, inner_l);
343 ed(1);
344 cup(18, 60 + hlfxtra);
345 ed(0);
346 el(1);
347 cup(9, inner_r);
348 el(0);
349 /* 132: 36..97 */
350 /* 80: 10..71 */
351 for (row = 10; row <= 16; row++) {
352 cup(row, inner_l);
353 el(1);
354 cup(row, inner_r);
355 el(0);
356 }
357 cup(17, 30);
358 el(2);
359 for (col = 1; col <= width; col++) {
360 hvp(max_lines, col);
361 tprintf("*");
362 hvp(1, col);
363 tprintf("*");
364 }
365 cup(2, 2);
366 for (row = 2; row <= max_lines - 1; row++) {
367 tprintf("+");
368 cub(1);
369 ind();
370 }
371 cup(max_lines - 1, width - 1);
372 for (row = max_lines - 1; row >= 2; row--) {
373 tprintf("+");
374 cub(1);
375 ri();
376 }
377 cup(2, 1);
378 for (row = 2; row <= max_lines - 1; row++) {
379 tprintf("*");
380 cup(row, width);
381 tprintf("*");
382 cub(10);
383 if (row < 10)
384 nel();
385 else
386 tprintf("\n");
387 }
388 cup(2, 10);
389 cub(42 + hlfxtra);
390 cuf(2);
391 for (col = 3; col <= width - 2; col++) {
392 tprintf("+");
393 cuf(0);
394 cub(2);
395 cuf(1);
396 }
397 cup(max_lines - 1, inner_r - 1);
398 cuf(42 + hlfxtra);
399 cub(2);
400 for (col = width - 2; col >= 3; col--) {
401 tprintf("+");
402 cub(1);
403 cuf(1);
404 cub(0);
405 tprintf("%c", 8);
406 }
407 cup(1, 1);
408 cuu(10);
409 cuu(1);
410 cuu(0);
411 cup(max_lines, width);
412 cud(10);
413 cud(1);
414 cud(0);
415
416 cup(10, 2 + inner_l);
417 for (row = 10; row <= 15; row++) {
418 for (col = 2 + inner_l; col <= inner_r - 2; col++)
419 tprintf(" ");
420 cud(1);
421 cub(58);
422 }
423 cuu(5);
424 cuf(1);
425 printxx("The screen should be cleared, and have an unbroken bor-");
426 cup(12, inner_l + 3);
427 printxx("der of *'s and +'s around the edge, and exactly in the");
428 cup(13, inner_l + 3);
429 printxx("middle there should be a frame of E's around this text");
430 cup(14, inner_l + 3);
431 printxx("with one (1) free position around it. ");
432 holdit();
433 }
434 deccolm(FALSE);
435
436 /* DECAWM demo */
437 for (pass = 0; pass <= 1; pass++) {
438 static char on_left[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
439 static char on_right[] = "abcdefghijklmnopqrstuvwxyz";
440 int height = sizeof(on_left) - 1;
441 int region = max_lines - 6;
442
443 if (LOG_ENABLED)
444 fprintf(log_fp, NOTE_STR "tst_movements wrap(%d cols)\n",
445 pass ? max_cols : min_cols);
446
447 /* note: DECCOLM clears the screen */
448 if (pass == 0) {
449 deccolm(FALSE);
450 width = min_cols;
451 } else {
452 deccolm(TRUE);
453 width = max_cols;
454 }
455
456 println("Test of autowrap, mixing control and print characters.");
457 println("The left/right margins should have letters in order:");
458
459 decstbm(3, region + 3);
460 decom(TRUE); /* this also homes the cursor */
461 for (i = 0; i < height; ++i) {
462 switch (i % 4) {
463 case 0:
464 /* draw characters as-is, for reference */
465 __(cup(region + 1, 1), tprintf("%c", on_left[i]));
466 __(cup(region + 1, width), tprintf("%c", on_right[i]));
467 tprintf("\n");
468 break;
469 case 1:
470 /* simple wrapping */
471 __(cup(region, width), tprintf("%c%c", on_right[i - 1], on_left[i]));
472 /* backspace at right margin */
473 __(cup(region + 1, width), tprintf("%c%c %c",
474 on_left[i], BS, on_right[i]));
475 tprintf("\n");
476 break;
477 case 2:
478 /* tab to right margin */
479 __(cup(region + 1, width), tprintf("%c%c%c%c%c%c",
480 on_left[i], BS, BS,
481 TAB, TAB, on_right[i]));
482 __(cup(region + 1, 2), tprintf("%c%c\n", BS, on_left[i]));
483 break;
484 default:
485 /* newline at right margin */
486 __(cup(region + 1, width), tprintf("\n"));
487 __(cup(region, 1), tprintf("%c", on_left[i]));
488 __(cup(region, width), tprintf("%c", on_right[i]));
489 break;
490 }
491 }
492 decom(FALSE);
493 decstbm(0, 0);
494 cup(max_lines - 2, 1);
495 holdit();
496 }
497 deccolm(FALSE); /* 80 cols */
498
499 if (LOG_ENABLED)
500 fprintf(log_fp,
501 NOTE_STR "tst_movements cursor-controls in ESC sequences\n");
502
503 vt_clear(2);
504 vt_move(1, 1);
505 println("Test of cursor-control characters inside ESC sequences.");
506 println("Below should be four identical lines:");
507 println("");
508 println("A B C D E F G H I");
509 for (i = 1; i < 10; i++) {
510 tprintf("%c", '@' + i);
511 do_csi("2%cC", BS); /* Two forward, one backspace */
512 }
513 println("");
514 /* Now put CR in CUF sequence. */
515 tprintf("A ");
516 for (i = 2; i < 10; i++) {
517 cprintf("%s%c%dC", csi_output(), CR, 2 * i - 2);
518 tprintf("%c", '@' + i);
519 }
520 println("");
521 /* Now put VT in CUU sequence. */
522 rm("20");
523 for (i = 1; i < 10; i++) {
524 tprintf("%c ", '@' + i);
525 do_csi("1\013A");
526 }
527 println("");
528 println("");
529 holdit();
530
531 if (LOG_ENABLED)
532 fprintf(log_fp,
533 NOTE_STR "tst_movements leading zeros in ESC sequences\n");
534
535 vt_clear(2);
536 vt_move(1, 1);
537 println("Test of leading zeros in ESC sequences.");
538 printxx("Two lines below you should see the sentence \"%s\".", ctext);
539 for (col = 1; *ctext; col++) {
540 cprintf("%s00000000004;00000000%dH", csi_output(), col);
541 tprintf("%c", *ctext++);
542 }
543 cup(20, 1);
544
545 restore_ttymodes();
546 return MENU_HOLD;
547 }
548
549 /* Scrolling test (used also in color-testing) */
550 void
551 do_scrolling(void)
552 {
553 int soft, first, last, down, i, pass;
554
555 ed(2);
556 decom(TRUE); /* Origin mode (relative) */
557 for (soft = -1; soft <= 0; soft++) {
558 if (soft)
559 decsclm(TRUE);
560 else
561 decsclm(FALSE);
562 for (pass = 0; pass < 2; ++pass) {
563 if (pass == 0) {
564 first = max_lines / 2;
565 last = first + 1;
566 } else {
567 first = 1;
568 last = max_lines;
569 }
570 decstbm(first, last);
571 ed(2);
572 for (down = 0; down >= -1; down--) {
573 if (down)
574 cuu(max_lines);
575 else
576 cud(max_lines);
577 for (i = 1; i <= max_lines + 5; i++) {
578 printxx("%s scroll %s region [%d..%d] size %d Line %d\n",
579 soft ? "Soft" : "Jump",
580 down ? "down" : "up",
581 first, last, last - first + 1, i);
582 if (down) {
583 ri();
584 ri();
585 } else if (soft)
586 extra_padding(10);
587 }
588 }
589 holdit();
590 }
591 }
592 }
593
594 int
595 tst_screen(MENU_ARGS)
596 {
597 /* Test of:
598 - DECSTBM (Set Top and Bottom Margins)
599 - TBC (Tabulation Clear)
600 - HTS (Horizontal Tabulation Set)
601 - SM RM (Set/Reset mode): - 80/132 chars
602 . - Origin: Relative/absolute
603 . - Scroll: Smooth/jump
604 . - Wraparound
605 - SGR (Select Graphic Rendition)
606 - SM RM (Set/Reset Mode) - Inverse
607 - DECSC (Save Cursor)
608 - DECRC (Restore Cursor)
609 */
610
611 int i, j, cset, row, col, background;
612
613 static const char *tststr = "*qx`";
614 static const char *attr[5] =
615 {
616 ";0", ";1", ";4", ";5", ";7"
617 };
618
619 set_tty_crmod(TRUE); /* want to disable tab/space conversion */
620
621 cup(1, 1);
622 decawm(TRUE); /* DECAWM: Wrap Around ON */
623 for (col = 1; col <= min_cols * 2; col++)
624 tprintf("*");
625 decawm(FALSE); /* DECAWM: Wrap Around OFF */
626 cup(3, 1);
627 for (col = 1; col <= min_cols * 2; col++)
628 tprintf("*");
629 decawm(TRUE); /* DECAWM: Wrap Around ON */
630 cup(5, 1);
631 println("This should be three identical lines of *'s completely filling");
632 println("the top of the screen without any empty lines between.");
633 println("(Test of WRAP AROUND mode setting.)");
634 holdit();
635
636 ed(2);
637 tbc(3);
638 cup(1, 1);
639 for (col = 1; col <= min_cols - 2; col += 3) {
640 cuf(3);
641 hts();
642 }
643 cup(1, 4);
644 for (col = 4; col <= min_cols - 2; col += 6) {
645 tbc(0);
646 cuf(6);
647 }
648 cup(1, 7);
649 tbc(1);
650 tbc(2); /* no-op */
651 cup(1, 1);
652 for (col = 1; col <= min_cols - 2; col += 6)
653 tprintf("%c*", TAB);
654 cup(2, 2);
655 for (col = 2; col <= min_cols - 2; col += 6)
656 tprintf(" *");
657 cup(4, 1);
658 println("Test of TAB setting/resetting. These two lines");
659 printxx("should look the same. ");
660 holdit();
661 for (background = 0; background <= 1; background++) {
662 if (background)
663 decscnm(FALSE);
664 else
665 decscnm(TRUE);
666 deccolm(TRUE); /* 132 cols */
667 ed(2); /* VT100 clears screen on SM3/RM3, but not obviously, so... */
668 cup(1, 1);
669 tbc(3);
670 for (col = 1; col <= max_cols; col += TABWIDTH) {
671 cuf(TABWIDTH);
672 hts();
673 }
674 cup(1, 1);
675 for (col = 1; col <= max_cols; col += 10)
676 tprintf("%.*s", (max_cols > col) ? (max_cols - col) : 10, "1234567890");
677 for (row = 3; row <= 20; row++) {
678 cup(row, row);
679 tprintf("This is %d column mode, %s background.", max_cols,
680 background ? "dark" : "light");
681 }
682 holdit();
683 deccolm(FALSE); /* 80 cols */
684 ed(2); /* VT100 clears screen on SM3/RM3, but not obviously, so... */
685 cup(1, 1);
686 for (col = 1; col <= min_cols; col += 10)
687 tprintf("%.*s", (min_cols > col) ? (min_cols - col) : 10, "1234567890");
688 for (row = 3; row <= 20; row++) {
689 cup(row, row);
690 tprintf("This is %d column mode, %s background.", min_cols,
691 background ? "dark" : "light");
692 }
693 holdit();
694 }
695 do_scrolling();
696 ed(2);
697 decstbm(max_lines - 1, max_lines);
698 printxx(
699 "\nOrigin mode test. This line should be at the bottom of the screen.");
700 cup(1, 1);
701 tprintf("%s",
702 "This line should be the one above the bottom of the screen. ");
703 holdit();
704 ed(2);
705 decom(FALSE); /* Origin mode (absolute) */
706 cup(max_lines, 1);
707 tprintf(
708 "Origin mode test. This line should be at the bottom of the screen.");
709 cup(1, 1);
710 tprintf("%s", "This line should be at the top of the screen. ");
711 holdit();
712 decstbm(1, max_lines);
713
714 ed(2);
715 /* *INDENT-OFF* */
716 cup( 1,20); tprintf("Graphic rendition test pattern:");
717 cup( 4, 1); sgr("0"); tprintf("vanilla");
718 cup( 4,40); sgr("0;1"); tprintf("bold");
719 cup( 6, 6); sgr(";4"); tprintf("underline");
720 cup( 6,45);sgr(";1");sgr("4");tprintf("bold underline");
721 cup( 8, 1); sgr("0;5"); tprintf("blink");
722 cup( 8,40); sgr("0;5;1"); tprintf("bold blink");
723 cup(10, 6); sgr("0;4;5"); tprintf("underline blink");
724 cup(10,45); sgr("0;1;4;5"); tprintf("bold underline blink");
725 cup(12, 1); sgr("1;4;5;0;7"); tprintf("negative");
726 cup(12,40); sgr("0;1;7"); tprintf("bold negative");
727 cup(14, 6); sgr("0;4;7"); tprintf("underline negative");
728 cup(14,45); sgr("0;1;4;7"); tprintf("bold underline negative");
729 cup(16, 1); sgr("1;4;;5;7"); tprintf("blink negative");
730 cup(16,40); sgr("0;1;5;7"); tprintf("bold blink negative");
731 cup(18, 6); sgr("0;4;5;7"); tprintf("underline blink negative");
732 cup(18,45); sgr("0;1;4;5;7"); tprintf("bold underline blink negative");
733 /* *INDENT-ON* */
734
735 sgr("");
736
737 decscnm(FALSE); /* Inverse video off */
738 cup(max_lines - 1, 1);
739 el(0);
740 tprintf("Dark background. ");
741 holdit();
742
743 decscnm(TRUE); /* Inverse video */
744 cup(max_lines - 1, 1);
745 el(0);
746 tprintf("Light background. ");
747 holdit();
748
749 decscnm(FALSE);
750
751 ed(2);
752 /* *INDENT-OFF* */
753 cup(8,12); tprintf("normal");
754 cup(8,24); tprintf("bold");
755 cup(8,36); tprintf("underscored");
756 cup(8,48); tprintf("blinking");
757 cup(8,60); tprintf("reversed");
758 cup(10,1); tprintf("stars:");
759 cup(12,1); tprintf("line:");
760 cup(14,1); tprintf("x'es:");
761 cup(16,1); tprintf("diamonds:");
762 /* *INDENT-ON* */
763
764 for (cset = 0; cset <= 3; cset++) {
765 for (i = 0; i <= 4; i++) {
766 cup(10 + 2 * cset, 12 + 12 * i);
767 sgr(attr[i]);
768 if (cset == 0 || cset == 2)
769 scs_normal();
770 else
771 scs_graphics();
772 for (j = 0; j <= 4; j++) {
773 tprintf("%c", tststr[cset]);
774 }
775 decsc();
776 cup(cset + 1, i + 1);
777 sgr("");
778 scs_normal();
779 tprintf("A");
780 decrc();
781 for (j = 0; j <= 4; j++) {
782 tprintf("%c", tststr[cset]);
783 }
784 }
785 }
786
787 sgr("0");
788 scs_normal();
789 cup(21, 1);
790 println("Test of the SAVE/RESTORE CURSOR feature. There should");
791 println("be ten characters of each flavour, and a rectangle");
792 println("of 5 x 4 A's filling the top left of the screen.");
793
794 restore_ttymodes();
795 return MENU_HOLD;
796 }
797
798 int
799 tst_doublesize(MENU_ARGS)
800 {
801 /* Test of:
802 DECSWL (Single Width Line)
803 DECDWL (Double Width Line)
804 DECDHL (Double Height Line) (also implicit double width)
805 */
806
807 int col, i, w;
808
809 /* Print the test pattern in both 80 and 132 character width */
810
811 for (w = 0; w <= 1; w++) {
812 int w1 = 13 * w;
813
814 ed(2);
815 cup(1, 1);
816 if (w) {
817 deccolm(TRUE);
818 tprintf("%3d column mode", max_cols);
819 } else {
820 deccolm(FALSE);
821 tprintf("%3d column mode", min_cols);
822 }
823
824 cup(5, 3 + 2 * w1);
825 tprintf("v------- left margin");
826
827 cup(7, 3 + 2 * w1);
828 tprintf("This is a normal-sized line");
829
830 decdhl(0);
831 decdhl(1);
832 decdwl();
833 decswl();
834 cup(9, 2 + w1);
835 tprintf("This is a Double-width line");
836
837 decswl();
838 decdhl(0);
839 decdhl(1);
840 decdwl();
841
842 cup(11, 2 + w1);
843 decdwl();
844 decswl();
845 decdhl(1);
846 decdhl(0);
847 tprintf("This is a Double-width-and-height line");
848
849 cup(12, 2 + w1);
850 decdwl();
851 decswl();
852 decdhl(0);
853 decdhl(1);
854 tprintf("This is a Double-width-and-height line");
855
856 cup(14, 2 + w1);
857 decdwl();
858 decswl();
859 decdhl(1);
860 decdhl(0);
861 el(2);
862 tprintf("This is another such line");
863
864 cup(15, 2 + w1);
865 decdwl();
866 decswl();
867 decdhl(0);
868 decdhl(1);
869 tprintf("This is another such line");
870
871 cup(17, 3 + 2 * w1);
872 tprintf("^------- left margin");
873
874 cup(21, 1);
875 tprintf("This is not a double-width line");
876 for (i = 0; i <= 1; i++) {
877 cup(21, 6);
878 if (i) {
879 tprintf("**is**");
880 decdwl();
881 } else {
882 tprintf("is not");
883 decswl();
884 }
885 cup(max_lines - 1, 1);
886 holdit();
887 }
888 }
889 /* Set vanilla tabs for next test */
890 cup(1, 1);
891 tbc(3);
892 for (col = 1; col <= max_cols; col += TABWIDTH) {
893 cuf(TABWIDTH);
894 hts();
895 }
896 deccolm(FALSE);
897 ed(2);
898 /* *INDENT-OFF* */
899 scs_graphics();
900 cup( 8,1); decdhl(0); tprintf("lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk");
901 cup( 9,1); decdhl(1); tprintf("lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk");
902 cup(10,1); decdhl(0); tprintf("x%c%c%c%c%cx",9,9,9,9,9);
903 cup(11,1); decdhl(1); tprintf("x%c%c%c%c%cx",9,9,9,9,9);
904 cup(12,1); decdhl(0); tprintf("x%c%c%c%c%cx",9,9,9,9,9);
905 cup(13,1); decdhl(1); tprintf("x%c%c%c%c%cx",9,9,9,9,9);
906 scs(1, '0'); /* should look the same as scs_graphics() */
907 cup(14,1); decdhl(0); tprintf("x x");
908 cup(15,1); decdhl(1); tprintf("x x");
909 cup(16,1); decdhl(0); tprintf("mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj");
910 cup(17,1); decdhl(1); tprintf("mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj");
911 scs_normal();
912 /* *INDENT-ON* */
913
914 sgr("1;5");
915 cup(12, 3);
916 tprintf("* The mad programmer strikes again * ");
917
918 cup(13, 3);
919 tprintf("%c", 9);
920
921 cub(6);
922 tprintf("* The mad programmer strikes again *");
923 sgr("0");
924
925 cup(max_lines - 2, 1);
926 println("Another test pattern... a frame with blinking bold text,");
927 printxx("all in double-height double-width size. ");
928 holdit();
929
930 decstbm(8, max_lines); /* Absolute origin mode, so cursor is set at (1,1) */
931 cup(8, 1);
932 for (i = 1; i <= 12; i++)
933 ri();
934 decstbm(0, 0); /* No scroll region */
935 cup(1, 1);
936 printxx("%s", "Exactly half of the box should remain. ");
937 return MENU_HOLD;
938 }
939
940 int
941 tst_insdel(MENU_ARGS)
942 {
943 /* Test of:
944 SM/RM(4) (= IRM (Insertion/replacement mode))
945 ICH (Insert Character)
946 DCH (Delete character)
947 IL (Insert line)
948 DL (Delete line)
949 */
950
951 int i, row, col, sw, dblchr, scr132;
952
953 for (scr132 = 0; scr132 <= 1; scr132++) {
954 if (scr132) {
955 deccolm(TRUE);
956 sw = max_cols;
957 } else {
958 deccolm(FALSE);
959 sw = min_cols;
960 }
961 ed(2);
962 cup(1, 1);
963 for (row = 1; row <= max_lines; row++) {
964 cup(row, 1);
965 for (col = 1; col <= sw; col++)
966 tprintf("%c", 'A' - 1 + row);
967 }
968 cup(4, 1);
969 printxx("Screen accordion test (Insert & Delete Line). ");
970 holdit();
971
972 ri();
973 el(2);
974 decstbm(2, max_lines - 1);
975 decom(TRUE);
976 cup(1, 1);
977 for (row = 1; row <= max_lines; row++) {
978 il(row);
979 dl(row);
980 }
981 decom(FALSE);
982 decstbm(0, 0);
983 cup(2, 1);
984 printxx("Top line: A's, bottom line: %c's, this line, nothing more. ",
985 'A' - 1 + max_lines);
986 holdit();
987 cup(2, 1);
988 ed(0);
989 cup(1, 2);
990 tprintf("B");
991 cub(1);
992 sm("4");
993 for (col = 2; col <= sw - 1; col++)
994 tprintf("*");
995 rm("4");
996 cup(4, 1);
997 printxx("Test of 'Insert Mode'. The top line should be 'A*** ... ***B'. ");
998 holdit();
999 ri();
1000 el(2);
1001 cup(1, 2);
1002 dch(sw - 2);
1003 cup(4, 1);
1004 printxx("Test of 'Delete Character'. The top line should be 'AB'. ");
1005 holdit();
1006
1007 for (dblchr = 1; dblchr <= 2; dblchr++) {
1008 ed(2);
1009 for (row = 1; row <= max_lines; row++) {
1010 cup(row, 1);
1011 if (dblchr == 2)
1012 decdwl();
1013 for (col = 1; col <= sw / dblchr; col++)
1014 tprintf("%c", 'A' - 1 + row);
1015 cup(row, sw / dblchr - row);
1016 dch(row);
1017 }
1018 cup(4, 1);
1019 println("The right column should be staggered ");
1020 printxx("by one. ");
1021 holdit();
1022 }
1023 ed(2);
1024 cup(1, 1);
1025 println("If your terminal has the ANSI 'Insert Character' function");
1026 println("(the VT102 does not), then you should see a line like this");
1027 println(" A B C D E F G H I J K L M N O P Q R S T U V W X Y Z");
1028 println("below:");
1029 println("");
1030 for (i = 'Z'; i >= 'A'; i--) {
1031 tprintf("%c%c", i, BS);
1032 ich(2);
1033 }
1034 cup(10, 1);
1035 holdit();
1036
1037 if (sw == max_cols)
1038 deccolm(FALSE);
1039 }
1040 return MENU_NOHOLD;
1041 }
1042
1043 /* Test of some known VT100 bugs and misfeatures */
1044
1045 int
1046 tst_bugs(MENU_ARGS)
1047 {
1048 int i;
1049 /* *INDENT-OFF* */
1050 static MENU menutable[] = {
1051 { "Exit to main menu", NULL },
1052 { "Bug A: Smooth scroll to jump scroll", bug_a },
1053 { "Bug B: Scrolling region", bug_b },
1054 { "Bug C: Wide to narrow screen", bug_c },
1055 { "Bug D: Narrow to wide screen", bug_d },
1056 { "Bug E: Cursor move from double- to single-wide line", bug_e },
1057 { "Bug F: Column mode escape sequence", bug_f },
1058 { "Wrap around with cursor addressing", bug_w },
1059 { "Erase right half of double width lines", bug_l },
1060 { "Funny scroll regions", bug_s },
1061 /* Add more here */
1062 { "", NULL }
1063 };
1064 /* *INDENT-ON* */
1065
1066 static const char *hmsg[] =
1067 {
1068 "Test of known bugs in the DEC VT100 series. The numbering of some of",
1069 "the bugs (A-F) refers to the article 'VT100 MAGIC' by Sami Tabih in",
1070 "the 'Proceedings of the DEC Users Society' at St. Louis, Missouri, May",
1071 "1983. To understand some of the tests, you have to look at the source",
1072 "code or the article. Of course, a good VT100-compatible terminal",
1073 "should not have these bugs (or have some means of disabling them)! If",
1074 "a bug appears, you might want to RESET the terminal before continuing",
1075 "the test. There is a test of the RESET function in the main menu.",
1076 ""};
1077
1078 do {
1079 vt_clear(2);
1080 vt_move(1, 1);
1081 for (i = 0; *hmsg[i]; i++)
1082 println(hmsg[i]);
1083 println("");
1084 println(" Choose bug test number:");
1085 } while (menu2(menutable, i + 1));
1086 return MENU_NOHOLD;
1087 }
1088
1089 /* Bug A: Smooth scroll to jump scroll */
1090
1091 int
1092 bug_a(MENU_ARGS)
1093 {
1094 int i;
1095
1096 cup(10, 1);
1097 println("This is a test of the VT100 'Scroll while toggle softscroll'");
1098 println("bug. The cursor may disappear, or move UP the screen, or");
1099 println("multiple copies of some lines may appear.");
1100 holdit();
1101
1102 /* Invoke the bug */
1103
1104 esc("[24H"); /* Simplified cursor movement */
1105 decsclm(FALSE);
1106 for (i = 1; i <= 20; i++)
1107 tprintf("\n");
1108
1109 decsclm(TRUE);
1110 for (i = 1; i <= 10; i++)
1111 tprintf("\n");
1112
1113 decsclm(FALSE);
1114 for (i = 1; i <= 5; i++)
1115 tprintf("\n");
1116
1117 /* That should be enough to show the bug. But we'll try another way: */
1118 decsclm(TRUE); /* Set soft scroll */
1119 nel(); /* "NextLine", move down */
1120 decsclm(FALSE); /* Reset soft scroll */
1121 nel(); /* "NextLine", move down */
1122 for (i = 1; i <= 10; i++) { /* Show the bug */
1123 printxx("Softscroll bug test, line %d. ", i);
1124 holdit();
1125 }
1126 println("That should have been enough to show the bug, if present.");
1127 return MENU_HOLD;
1128 }
1129
1130 /* Bug B: Scrolling region */
1131
1132 int
1133 bug_b(MENU_ARGS)
1134 {
1135 char c;
1136
1137 decaln();
1138
1139 cup(1, 1);
1140 el(0);
1141 printxx("Line 11 should be double-wide, line 12 should be cleared.");
1142
1143 cup(2, 1);
1144 el(0);
1145 printxx("Then, the letters A-P should be written at the beginning");
1146
1147 cup(3, 1);
1148 el(0);
1149 printxx("of lines 12-%d, and the empty line and A-E are scrolled away.", max_lines);
1150
1151 cup(4, 1);
1152 el(0);
1153 printxx("If the bug is present, some lines are confused, look at K-P.");
1154
1155 cup(11, 1);
1156 decdwl();
1157 decstbm(12, max_lines);
1158
1159 cup(12, 1);
1160 el(0);
1161 printxx("Here we go... ");
1162 holdit();
1163
1164 cup(12, 1);
1165 ri(); /* Bug comes here */
1166 for (c = 'A'; c <= 'P'; c++)
1167 tprintf("%c\n", c); /* Bug shows here */
1168 holdit();
1169 decstbm(0, 0); /* No scr. region */
1170 return MENU_NOHOLD;
1171 }
1172
1173 /* Bug C: Wide to narrow screen */
1174
1175 int
1176 bug_c(MENU_ARGS)
1177 {
1178 deccolm(TRUE); /* 132 column mode */
1179 cup(1, 81);
1180 deccolm(FALSE); /* 80 column mode */
1181 cup(12, 5);
1182 printxx("Except for this line, the screen should be blank. ");
1183 return MENU_HOLD;
1184 }
1185
1186 /* Bug D: Narrow to wide screen */
1187
1188 int
1189 bug_d(MENU_ARGS)
1190 {
1191 int i;
1192 char result;
1193 /* Make the bug appear */
1194 do {
1195 cup(14, 1);
1196
1197 /* The original code in the article says
1198 * PRINT ESC$; "[13;1H"; CHR$(10%);
1199 * but I guess a cup(14,1); would do.
1200 * (To output a pure LF might be tricky).
1201 */
1202
1203 deccolm(TRUE); /* Make the bug visible */
1204
1205 cup(1, 9);
1206 decdwl();
1207 println("You should see blinking text at the bottom line.");
1208
1209 cup(3, 9);
1210 decdwl();
1211 println("Enter 0 to exit, 1 to try to invoke the bug again.");
1212
1213 cup(max_lines, 9);
1214 decdwl();
1215 sgr("1;5;7");
1216 printxx("If you can see this then the bug did not appear.");
1217 sgr("");
1218
1219 cup(4, 9);
1220 decdwl();
1221 result = (char) get_char();
1222 readnl();
1223 deccolm(FALSE);
1224 } while (result == '1');
1225 decsclm(TRUE); /* Syrup scroll */
1226 cup(max_lines - 1, 1);
1227 for (i = 1; i <= 5; i++)
1228 println("If the bug is present, this should make things much worse!");
1229 holdit();
1230 decsclm(FALSE); /* Jump scroll */
1231 return MENU_NOHOLD;
1232 }
1233
1234 /* Bug E: Cursor move from double- to single-wide line */
1235
1236 int
1237 bug_e(MENU_ARGS)
1238 {
1239 int i;
1240 static const char *rend[2] =
1241 {"m", "7m"};
1242 deccolm(TRUE);
1243 cup(1, 1);
1244 decdwl();
1245 println("This test should put an 'X' at line 3 column 100.");
1246 for (i = 1; i <= 12; i++)
1247 tprintf("1234567890%s%s", csi_output(), rend[i & 1]);
1248 cup(1, 1); /* The bug appears when we jump from a double-wide line */
1249 cup(3, 100); /* to a single-wide line, column > 66. */
1250 printxx("X");
1251 cup(4, max_cols / 2);
1252 printxx("! !");
1253 cup(5, 1);
1254 printxx("--------------------------- The 'X' should NOT be above here -");
1255 printxx("---+------------ but above here -----+");
1256 cup(10, 1);
1257 decdwl();
1258 holdit();
1259 deccolm(FALSE);
1260 return MENU_NOHOLD;
1261 }
1262
1263 /* Bug F: Column mode escape sequence */
1264
1265 int
1266 bug_f(MENU_ARGS)
1267 {
1268 /*
1269 * VT100 "toggle origin mode, forget rest" bug. If you try to set
1270 * (or clear) parameters and one of them is the "origin mode"
1271 * ("?6") parameter, parameters that appear after the "?6"
1272 * remain unaffected. This is also true on CIT-101 terminals.
1273 */
1274 decscnm(TRUE); /* Set reverse mode */
1275 deccolm(TRUE); /* Set 132 column mode */
1276 println("Test VT100 'Toggle origin mode, forget rest' bug, part 1.");
1277 printxx("The screen should be in reverse, %d column mode.\n", max_cols);
1278 holdit();
1279 ed(2);
1280 rm("?6;5;3"); /* Reset (origin, reverse, 132 col) */
1281 println("Test VT100 'Toggle origin mode, forget rest' bug, part 2.\n");
1282 printxx("The screen should be in non-reverse, %d column mode.\n", min_cols);
1283 return MENU_HOLD;
1284 }
1285
1286 /* Bug W:
1287 * The dreaded "wraparound" bug! You CUP to col 80, write a char,
1288 * CUP to another line in col 80, write a char. And the brain-damaged
1289 * terminal thinks that "Hokay, so he's written a char in col 80, so
1290 * I stay in col 80 and wait for next character. Let's see now, here
1291 * comes another character, and I'm still in col 80, so I must make
1292 * a NewLine first." -- It doesn't clear that "still in col 80" flag
1293 * on a CUP. Argh!
1294 */
1295
1296 int
1297 bug_w(MENU_ARGS)
1298 {
1299 int row, col;
1300
1301 cup(16, 1);
1302 println(" This illustrates the \"wrap around bug\" which exists on a");
1303 println(" standard VT100. At the top of the screen there should be");
1304 println(" a row of +'s, and the rightmost column should be filled");
1305 println(" with *'s. But if the bug is present, some of the *'s may");
1306 println(" be placed in other places, e.g. in the leftmost column,");
1307 println(" and the top line of +'s may be scrolled away.");
1308
1309 cup(1, 1);
1310 for (col = 1; col <= min_cols - 1; col++)
1311 tprintf("+");
1312 for (row = 1; row <= max_lines; row++) {
1313 hvp(row, min_cols);
1314 tprintf("*");
1315 }
1316 cup(max_lines, 1);
1317 return MENU_HOLD;
1318 }
1319
1320 /* Bug L:
1321 * Check if the right half of double-width lines comes back
1322 * when a line is first set to single-width, filled with stuff,
1323 * set to double-width, and finally reset to single-width.
1324 *
1325 * A VT100 has this misfeature, and many others. Foo!
1326 */
1327
1328 int
1329 bug_l(MENU_ARGS)
1330 {
1331 cup(15, 1);
1332 printxx("This-is-a-long-line-This-is-a-long-line-");
1333 printxx("This-is-a-long-line-This-is-a-long-line-");
1334 cup(1, 1);
1335 printxx("This is a test of what happens to the right half of double-width");
1336 println(" lines.");
1337 printxx("A common misfeature is that the right half does not come back");
1338 println(" when a long");
1339 printxx("single-width line is set to double-width and then reset to");
1340 println(" single-width.");
1341
1342 cup(5, 1);
1343 println("Now the line below should contain 80 characters in single width.");
1344 holdit();
1345 cup(15, 1);
1346 decdwl();
1347 cup(8, 1);
1348 println("Now the line below should contain 40 characters in double width.");
1349 holdit();
1350 cup(15, 1);
1351 decswl();
1352 cup(11, 1);
1353 println("Now the line below should contain 80 characters in single width.");
1354 holdit();
1355
1356 /* ...and in 132 column mode */
1357
1358 deccolm(TRUE);
1359 ed(2);
1360 cup(15, 1);
1361 printxx("This-is-a-long-line-This-is-a-long-line-");
1362 printxx("This-is-a-long-line-This-is-a-long-line-");
1363 printxx("This-is-a-long-line-This-is-a-long-line-");
1364 printxx("ending-here-");
1365
1366 cup(1, 1);
1367 printxx("This is the same test in %d column mode.", max_cols);
1368
1369 cup(5, 1);
1370 printxx("Now the line below should contain %d characters in single width.\n", max_cols);
1371 holdit();
1372
1373 cup(15, 1);
1374 decdwl();
1375
1376 cup(8, 1);
1377 printxx("Now the line below should contain %d characters in double width.\n",
1378 max_cols / 2);
1379 holdit();
1380
1381 cup(15, 1);
1382 decswl();
1383
1384 cup(11, 1);
1385 printxx("Now the line below should contain %d characters in single width.\n", max_cols);
1386
1387 holdit();
1388 deccolm(FALSE);
1389 return MENU_NOHOLD;
1390 }
1391
1392 int
1393 bug_s(MENU_ARGS)
1394 {
1395 int i;
1396 decstbm(20, 10); /* 20-10=-10, < 2, so no scroll region. */
1397 cup(1, 1);
1398 for (i = 1; i <= 20; i++)
1399 tprintf("This is 20 lines of text (line %d), no scroll region.\n", i);
1400 holdit();
1401 ed(2);
1402 decstbm(0, 1); /* Should be interpreted as decstbm(1,1) = none */
1403 cup(1, 1);
1404 for (i = 1; i <= 20; i++)
1405 tprintf("This is 20 lines of text (line %d), no scroll region.\n", i);
1406 holdit();
1407 decstbm(0, 0); /* No scroll region (just in case...) */
1408 return MENU_NOHOLD;
1409 }
1410
1411 void
1412 initterminal(int pn)
1413 {
1414 init_ttymodes(pn);
1415 setup_terminal("");
1416 }
1417
1418 static void
1419 enable_iso2022(void)
1420 {
1421 const char *env;
1422
1423 #ifdef HAVE_LANGINFO_CODESET
1424 (void) setlocale(LC_CTYPE, "");
1425 env = nl_langinfo(CODESET);
1426 assume_utf8 = !strcmp(env, "UTF-8");
1427 #else
1428 #if defined(LOCALE)
1429 /*
1430 * This is preferable to using getenv() since it ensures that we are using
1431 * the locale which was actually initialized by the application.
1432 */
1433 env = setlocale(LC_CTYPE, 0);
1434 #else
1435 if (((env = getenv("LANG")) != 0 && *env != '\0')
1436 || ((env = getenv("LC_CTYPE")) != 0 && *env != '\0')
1437 || ((env = getenv("LC_ALL")) != 0 && *env != '\0')) {
1438 ;
1439 }
1440 #endif
1441 if (env != NULL && strstr(env, ".UTF-8") != NULL) {
1442 assume_utf8 = TRUE;
1443 }
1444 #endif
1445
1446 if (assume_utf8) {
1447 if (LOG_ENABLED) {
1448 fprintf(log_fp, NOTE_STR "%senable ISO-2022 (%s)\n",
1449 allows_utf8 ? "Do not " : "",
1450 env);
1451 }
1452 if (!allows_utf8)
1453 esc("%@");
1454 } else {
1455 if (LOG_ENABLED) {
1456 fprintf(log_fp, NOTE_STR "UTF-8 is not enabled\n");
1457 }
1458 }
1459 }
1460
1461 static void
1462 disable_iso2022(void)
1463 {
1464 if (assume_utf8 && !allows_utf8) {
1465 esc("%G");
1466 }
1467 }
1468
1469 /* Set up my personal prejudices */
1470 int
1471 setup_terminal(MENU_ARGS)
1472 {
1473 if (LOG_ENABLED)
1474 fprintf(log_fp, NOTE_STR "Setup Terminal with test-defaults\n");
1475
1476 enable_iso2022();
1477
1478 default_level(); /* Enter ANSI mode (if in VT52 mode) */
1479 decckm(FALSE); /* cursor keys normal */
1480 deccolm(FALSE); /* 80 col mode */
1481 decsclm(FALSE); /* Jump scroll */
1482 decscnm(FALSE); /* Normal screen */
1483 decom(FALSE); /* Absolute origin mode */
1484 decawm(TRUE); /* Wrap around on */
1485 decarm(FALSE); /* Auto repeat off */
1486 sm("?40"); /* Enable 80/132 switch (xterm) */
1487 rm("?45"); /* Disable reverse wrap (xterm) */
1488 decstbm(0, 0); /* No scroll region */
1489 sgr("0"); /* Normal character attributes */
1490
1491 disable_iso2022();
1492
1493 return MENU_NOHOLD;
1494 }
1495
1496 void
1497 bye(void)
1498 {
1499 /* Force my personal prejudices upon the poor luser */
1500 if (LOG_ENABLED)
1501 fprintf(log_fp, NOTE_STR "Cleanup & exit\n");
1502
1503 default_level(); /* Enter ANSI mode (if in VT52 mode) */
1504 decckm(FALSE); /* cursor keys normal */
1505 deccolm(FALSE); /* 80 col mode */
1506 decscnm(FALSE); /* Normal screen */
1507 decom(FALSE); /* Absolute origin mode */
1508 decawm(TRUE); /* Wrap around on */
1509 decarm(TRUE); /* Auto repeat on */
1510 decstbm(0, 0); /* No scroll region */
1511 sgr("0"); /* Normal character attributes */
1512
1513 /* Say goodbye */
1514
1515 vt_clear(2);
1516 vt_move(12, 30);
1517 printxx("That's all, folks!\n");
1518 printxx("\n\n\n");
1519 inflush();
1520 close_tty();
1521
1522 if (LOG_ENABLED) {
1523 fclose(log_fp);
1524 }
1525
1526 exit(EXIT_SUCCESS);
1527 }
1528
1529 #ifdef UNIX
1530 RETSIGTYPE
1531 onbrk(SIG_ARGS GCC_UNUSED)
1532 {
1533 signal(SIGINT, onbrk);
1534 if (reading) {
1535 brkrd = TRUE;
1536 #ifdef HAVE_ALARM
1537 alarm(0);
1538 #endif
1539 } else {
1540 longjmp(intrenv, 1);
1541 }
1542 }
1543
1544 RETSIGTYPE
1545 onterm(SIG_ARGS GCC_UNUSED)
1546 {
1547 signal(SIGTERM, onterm);
1548 longjmp(intrenv, 1);
1549 }
1550 #endif
1551
1552 int
1553 scanto(const char *str, int *pos, int toc)
1554 {
1555 char c;
1556 int result = 0;
1557 int save = *pos;
1558
1559 while (toc != (c = str[(*pos)])) {
1560 *pos += 1;
1561 if (isdigit(CharOf(c)))
1562 result = result * 10 + c - '0';
1563 else
1564 break;
1565 }
1566 if (c == toc) {
1567 *pos += 1; /* point past delimiter */
1568 } else {
1569 result = 0;
1570 *pos = save;
1571 }
1572 return (result);
1573 }
1574
1575 int
1576 scan_DA(const char *str, int *pos)
1577 {
1578 int value = -1;
1579 if (str[*pos] != '\0') {
1580 int save = *pos;
1581 value = scanto(str, pos, ';');
1582 if (value == 0 && *pos == save) {
1583 value = scanto(str, pos, 'c');
1584 if (str[*pos] != '\0')
1585 value = -1;
1586 }
1587 }
1588 return value;
1589 }
1590
1591 int
1592 scan_any(const char *str, int *pos, int toc)
1593 {
1594 int value = 0;
1595
1596 if (str[*pos] != '\0') {
1597 int save = *pos;
1598
1599 value = scanto(str, pos, ';');
1600 if (value == 0 && (save == *pos)) {
1601 *pos = save;
1602 value = scanto(str, pos, toc);
1603 if (str[*pos] != '\0')
1604 value = 0;
1605 }
1606 }
1607 return value;
1608 }
1609
1610 static const char *
1611 push_menu(int number)
1612 {
1613 const char *saved = current_menu;
1614 if ((current_menu = (char *) malloc(strlen(saved) + 10)) == NULL)
1615 no_memory();
1616 sprintf(current_menu, "%s%s%d", saved, *saved ? "." : "", number);
1617 return saved;
1618 }
1619
1620 static void
1621 pop_menu(const char *saved)
1622 {
1623 if (current_menu && *current_menu)
1624 free(current_menu);
1625 current_menu = (char *) saved;
1626 }
1627
1628 #define end_of_menu(table, number) \
1629 (table[number].description[0] == '\0')
1630
1631 static void
1632 show_entry(MENU *table, int number)
1633 {
1634 printxx(" %d%c %s\n", number,
1635 (table[number].dispatch == not_impl) ? '*' : '.',
1636 table[number].description);
1637 }
1638
1639 static int
1640 next_menu(const MENU *table, int top, int size)
1641 {
1642 int last;
1643 int next = top + size;
1644 for (last = top; last <= next && !end_of_menu(table, last); ++last) {
1645 ;
1646 }
1647 return (last >= next) ? next : top;
1648 }
1649
1650 static int
1651 prev_menu(int top, int size)
1652 {
1653 return (top > 1) ? (top - size) : top;
1654 }
1655
1656 int
1657 menu2(MENU *table, int top)
1658 {
1659 int i, tablesize, choice;
1660 char c;
1661 char storage[BUF_SIZE];
1662 int pagesize = max_lines - 7 - TITLE_LINE;
1663 int pagetop = 1;
1664
1665 tablesize = 0;
1666 for (i = 0; !end_of_menu(table, i); i++) {
1667 tablesize++;
1668 }
1669 tablesize--;
1670
1671 for (;;) {
1672 vt_move(top, 1);
1673 vt_clear(0);
1674
1675 println("");
1676 show_entry(table, 0);
1677 for (i = 0; i < pagesize; i++) {
1678 int j = pagetop + i;
1679 if (end_of_menu(table, j))
1680 break;
1681 show_entry(table, pagetop + i);
1682 }
1683
1684 println("");
1685 printxx(" Enter choice number (0 - %d): ", tablesize);
1686 for (;;) {
1687 char *s = storage;
1688 int redraw = FALSE;
1689
1690 inputline(s);
1691 choice = 0;
1692 while ((c = *s++) != '\0') {
1693 if (c == '*') {
1694 choice = -1;
1695 break;
1696 } else if (c == '?') {
1697 redraw = TRUE;
1698 break;
1699 } else if (tablesize > pagesize && c == 'n') {
1700 pagetop = next_menu(table, pagetop, pagesize);
1701 redraw = TRUE;
1702 break;
1703 } else if (tablesize > pagesize && c == 'p') {
1704 pagetop = prev_menu(pagetop, pagesize);
1705 redraw = TRUE;
1706 break;
1707 } else if (c >= '0' && c <= '9') {
1708 choice = 10 * choice + c - '0';
1709 } else {
1710 choice = tablesize + 1;
1711 break;
1712 }
1713 }
1714
1715 if (redraw) {
1716 if (LOG_ENABLED)
1717 fprintf(log_fp, NOTE_STR "Redrawing screen\n");
1718 break;
1719 }
1720
1721 if (choice < 0) {
1722 if (LOG_ENABLED)
1723 fprintf(log_fp, NOTE_STR "Selecting all choices\n");
1724 for (choice = 0; choice <= tablesize; choice++) {
1725 vt_clear(2);
1726 if (table[choice].dispatch != NULL) {
1727 const char *save = push_menu(choice);
1728 const char *name = table[choice].description;
1729 if (LOG_ENABLED)
1730 fprintf(log_fp, NOTE_STR "choice %s: %s\n", current_menu, name);
1731 if ((*table[choice].dispatch) (name) == MENU_HOLD)
1732 holdit();
1733 pop_menu(save);
1734 }
1735 }
1736 if (LOG_ENABLED)
1737 fflush(log_fp);
1738 return 1;
1739 } else if (choice <= tablesize) {
1740 vt_clear(2);
1741 if (table[choice].dispatch != NULL) {
1742 const char *save = push_menu(choice);
1743 const char *name = table[choice].description;
1744 if (LOG_ENABLED)
1745 fprintf(log_fp, NOTE_STR "choice %s: %s\n", current_menu, name);
1746 if ((*table[choice].dispatch) (name) != MENU_NOHOLD)
1747 holdit();
1748 pop_menu(save);
1749 }
1750 if (LOG_ENABLED)
1751 fflush(log_fp);
1752 return (table[choice].dispatch != NULL);
1753 }
1754 printxx(" Bad choice, try again: ");
1755 }
1756 }
1757 }
1758
1759 int
1760 menu(MENU *table)
1761 {
1762 return menu2(table, 6);
1763 }
1764
1765 /*
1766 * Format a response-string, to highlight in chrprint2() and print. It may be
1767 * multi-line, depending on the length and the column limits.
1768 */
1769 char *
1770 chrformat(const char *s, int col, int first)
1771 {
1772 int pass;
1773 int wrap = (min_cols - col);
1774 char *result = NULL;
1775
1776 if (quick_reply) {
1777 const char *quicker = s;
1778 int j;
1779 for (j = 0; s[j] != '\0'; ++j) {
1780 int c = CharOf(s[j]);
1781 int d = CharOf(s[j + 1]);
1782 /* reset on CSI, OSC, DCS, SOS, PM, APC */
1783 if ((strchr("\233\235\220\230\236\237", c) != NULL)
1784 || (c == '\033' && d != '\0' && strchr("[]PX^_", d) != NULL)) {
1785 if (j != 0)
1786 quicker = s + j;
1787 } else if (c == '\n') {
1788 if (s[j + 1] != '\0')
1789 quicker = s + j + 1;
1790 }
1791 }
1792 s = quicker;
1793 }
1794 for (pass = 0; pass < 2; ++pass) {
1795 int j, k;
1796 int base;
1797
1798 for (j = k = base = 0; s[j] != '\0'; ++j) {
1799 int c = CharOf(s[j]);
1800 char temp[80];
1801
1802 if (c <= ' ' || c >= '\177') {
1803 sprintf(temp, "<%d> ", c);
1804 } else {
1805 sprintf(temp, "%c ", c);
1806 }
1807 if ((k + first - base) >= wrap) {
1808 if (pass) {
1809 result[k] = '\n';
1810 }
1811 base = ++k;
1812 first = 0;
1813 }
1814 if (pass) {
1815 strcpy(result + k, temp);
1816 }
1817 k += (int) strlen(temp);
1818 }
1819 if (!pass) {
1820 if ((result = malloc((size_t) k + 2)) == NULL)
1821 no_memory();
1822 *result = '\0';
1823 }
1824 }
1825 return result;
1826 }
1827
1828 /*
1829 * Return updated row-number based on the number of characters printed to the
1830 * screen, e.g., for test_report_ops() to handle very long results.
1831 */
1832 int
1833 chrprint2(const char *s, int row, int col)
1834 {
1835 int i;
1836 int result = row;
1837 char *temp;
1838
1839 vt_hilite(TRUE);
1840 printxx(" ");
1841 temp = chrformat(s, col, 1);
1842
1843 for (i = 0; temp[i] != '\0'; ++i) {
1844 if (temp[i] == '\n') {
1845 vt_move(++result, col);
1846 } else {
1847 putchar(temp[i]);
1848 }
1849 }
1850 if (quick_reply)
1851 vt_el(0);
1852
1853 vt_hilite(FALSE);
1854 free(temp);
1855
1856 return result + 1;
1857 }
1858
1859 /*
1860 * Returns a pointer past the prefix, or null if no match is found
1861 */
1862 char *
1863 skip_prefix(const char *prefix, char *input)
1864 {
1865 while (*prefix != '\0') {
1866 if (*prefix++ != *input++)
1867 return NULL;
1868 }
1869 return input;
1870 }
1871
1872 char *
1873 skip_csi(char *input)
1874 {
1875 if (CharOf(*input) == CSI) {
1876 return input + 1;
1877 }
1878 return skip_prefix(csi_input(), input);
1879 }
1880
1881 char *
1882 skip_dcs(char *input)
1883 {
1884 if (CharOf(*input) == DCS) {
1885 return input + 1;
1886 }
1887 return skip_prefix(dcs_input(), input);
1888 }
1889
1890 char *
1891 skip_osc(char *input)
1892 {
1893 if (CharOf(*input) == OSC) {
1894 return input + 1;
1895 }
1896 return skip_prefix(osc_input(), input);
1897 }
1898
1899 char *
1900 skip_ss3(char *input)
1901 {
1902 if (CharOf(*input) == SS3) {
1903 return input + 1;
1904 }
1905 return skip_prefix(ss3_input(), input);
1906 }
1907
1908 /*
1909 * Variant with const params
1910 */
1911 const char *
1912 skip_prefix_2(const char *prefix, const char *input)
1913 {
1914 while (*prefix != '\0') {
1915 if (*prefix++ != *input++)
1916 return NULL;
1917 }
1918 return input;
1919 }
1920
1921 const char *
1922 skip_csi_2(const char *input)
1923 {
1924 if (CharOf(*input) == CSI) {
1925 return input + 1;
1926 }
1927 return skip_prefix_2(csi_input(), input);
1928 }
1929
1930 const char *
1931 skip_dcs_2(const char *input)
1932 {
1933 if (CharOf(*input) == DCS) {
1934 return input + 1;
1935 }
1936 return skip_prefix_2(dcs_input(), input);
1937 }
1938
1939 const char *
1940 skip_ss3_2(const char *input)
1941 {
1942 if (CharOf(*input) == SS3) {
1943 return input + 1;
1944 }
1945 return skip_prefix_2(ss3_input(), input);
1946 }
1947
1948 /*
1949 * Returns a pointer past digits, or null if none are found
1950 */
1951 char *
1952 skip_digits(char *src)
1953 {
1954 char *base = src;
1955 while (*src != '\0' && isdigit(CharOf(*src)))
1956 src++;
1957 return (base == src) ? NULL : src;
1958 }
1959
1960 #define xdigitOf(c) \
1961 (((c) >= '0' && (c) <= '9') \
1962 ? ((c) - '0') \
1963 : (((c) >= 'A' && (c) <= 'F') \
1964 ? ((c) + 10 - 'A') \
1965 : ((c) + 10 - 'a')))
1966
1967 const char *
1968 skip_xdigits(const char *src, int *value)
1969 {
1970 const char *base = src;
1971 *value = 0;
1972 while (*src != '\0' && isxdigit(CharOf(*src))) {
1973 int ch = CharOf(*src);
1974 *value <<= 4;
1975 *value += xdigitOf(ch);
1976 src++;
1977 }
1978 return (base == src) ? NULL : src;
1979 }
1980
1981 const char *
1982 skip_digits_2(const char *src)
1983 {
1984 const char *base = src;
1985 while (*src != '\0' && isdigit(CharOf(*src)))
1986 src++;
1987 return (base == src) ? NULL : src;
1988 }
1989
1990 /*
1991 * Strip the string terminator (ST) from the given string, returning true if
1992 * we did this.
1993 */
1994 int
1995 strip_suffix(char *src, const char *suffix)
1996 {
1997 int have = (int) strlen(src);
1998 int want = (int) strlen(suffix);
1999 if (have > want) {
2000 have -= want;
2001 if (!strcmp(src + have, suffix)) {
2002 src[have] = '\0';
2003 return TRUE;
2004 }
2005 }
2006 return FALSE;
2007 }
2008
2009 /*
2010 * Strip the string terminator (ST) from the given string, returning true if
2011 * we did this.
2012 */
2013 int
2014 strip_terminator(char *src)
2015 {
2016 int ok = strip_suffix(src, st_input());
2017 if (!ok) {
2018 int have = (int) strlen(src);
2019 if (have > 0 && CharOf(src[have - 1]) == ST) {
2020 ok = TRUE;
2021 src[--have] = '\0';
2022 }
2023 }
2024 if (!ok && LOG_ENABLED)
2025 fprintf(log_fp, NOTE_STR "Missing ST\n");
2026 return ok;
2027 }
2028
2029 /* Parse the contents of a report from DECRQSS, returning the data as well */
2030 int
2031 parse_decrqss(char *report, const char *func)
2032 {
2033 int code = -1;
2034 char *parse = report;
2035
2036 if ((parse = skip_dcs(parse)) != NULL
2037 && strip_terminator(parse)
2038 && strip_suffix(parse, func)) {
2039 if (!strncmp(parse, "1$r", (size_t) 3))
2040 code = 1;
2041 else if (!strncmp(parse, "0$r", (size_t) 3))
2042 code = 0;
2043 }
2044
2045 if (code >= 0) {
2046 while ((*report++ = parse[3]) != '\0')
2047 parse++;
2048 }
2049 return code;
2050 }
2051
2052 int
2053 title(int offset)
2054 {
2055 vt_move(TITLE_LINE + offset, 10);
2056 if (offset == 0 && *current_menu)
2057 printxx("Menu %s: ", current_menu);
2058 return 1; /* used for indenting */
2059 }
2060
2061 static void
2062 my_vfprintf(FILE *fp, va_list ap, const char *fmt)
2063 {
2064 while (*fmt != '\0') {
2065 if (*fmt == '%') {
2066 switch (*++fmt) {
2067 case 'c':
2068 fputc(va_arg(ap, int), fp);
2069 break;
2070 case 'x':
2071 fprintf(fp, "%x", va_arg(ap, int));
2072 break;
2073 case 'd':
2074 fprintf(fp, "%d", va_arg(ap, int));
2075 break;
2076 case 'u':
2077 fprintf(fp, "%u", va_arg(ap, unsigned));
2078 break;
2079 case 's':
2080 fputs(va_arg(ap, char *), fp);
2081 break;
2082 }
2083 } else if (*fmt != '\n') {
2084 fputc(*fmt, fp);
2085 }
2086 fmt++;
2087 }
2088 }
2089
2090 /*
2091 * Show a test-result, optionally logging it as well.
2092 */
2093 void
2094 show_result(const char *fmt, ...)
2095 {
2096 va_list ap;
2097
2098 if (*fmt != ' ')
2099 fputc(' ', stdout);
2100 va_start(ap, fmt);
2101 my_vfprintf(stdout, ap, fmt);
2102 va_end(ap);
2103
2104 if (LOG_ENABLED) {
2105 fputs(NOTE_STR "result ", log_fp);
2106 va_start(ap, fmt);
2107 my_vfprintf(log_fp, ap, fmt);
2108 va_end(ap);
2109 fputc('\n', log_fp);
2110 }
2111 }
2112
2113 /*
2114 * Use this to make some complex stuff (such as scrolling) slow enough to see.
2115 */
2116 void
2117 slowly(void)
2118 {
2119 if (slow_motion) {
2120 #ifdef HAVE_USLEEP
2121 fflush(stdout);
2122 zleep(100);
2123 #endif
2124 }
2125 }
2126
2127 /*
2128 * Bypass normal logging for control sequences that are used only to format
2129 * the test results.
2130 */
2131 void
2132 vt_clear(int code)
2133 {
2134 log_disabled++;
2135 ed(code);
2136 log_disabled--;
2137 }
2138
2139 void
2140 vt_el(int code)
2141 {
2142 log_disabled++;
2143 el(code);
2144 log_disabled--;
2145 }
2146
2147 int
2148 vt_move(int row, int col)
2149 {
2150 log_disabled++;
2151 cup(row, col);
2152 log_disabled--;
2153 return 1;
2154 }
2155
2156 void
2157 vt_hilite(int flag)
2158 {
2159 log_disabled++;
2160 sgr(flag ? "7" : "");
2161 log_disabled--;
2162 }