2 ** Copyright (C) 1991, 1997 Free Software Foundation, Inc.
4 ** This file is part of TACK.
6 ** TACK is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2, or (at your option)
11 ** TACK is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
16 ** You should have received a copy of the GNU General Public License
17 ** along with TACK; see the file COPYING. If not, write to
18 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 ** Boston, MA 02111-1307, USA.
24 MODULE_ID("$Id: ansi.c,v 1.5 2000/04/22 21:06:57 tom Exp $")
27 * Standalone tests for ANSI terminals. Three entry points:
28 * test_ansi_graphics(), test_ansi_reports() and test_ansi_sgr().
31 /*****************************************************************************
33 * Test ANSI status reports
35 *****************************************************************************/
37 /* ASCII control characters */
38 #define A_DC1 0x11 /* Control Q */
39 #define A_DC3 0x13 /* Control S */
47 static char default_bank[] = "\033(B\017";
48 static int private_use, ape, terminal_class;
49 static short ansi_value[256];
50 static unsigned char ansi_buf[512], pack_buf[512];
58 static struct ansi_reports report_list[] = {
59 {0, 'c', "(DA) Primary device attributes", "\033[0c"},
60 {1, 0, "(DSR) Terminal status", "\033[5n"},
61 {1, 'R', "(DSR) Cursor position", "\033[6n"},
62 {62, 0, "(DA) Secondary device attributes", "\033[>0c"},
63 {62, 0, "(DSR) Printer status", "\033[?15n"},
64 {62, 0, "(DSR) Function key definition", "\033[?25n"},
65 {62, 0, "(DSR) Keyboard language", "\033[?26n"},
66 {63, 0, "(DECRQSS) Data destination", "\033P$q$}\033\\"},
67 {63, 0, "(DECRQSS) Status line type", "\033P$q$~\033\\"},
68 {63, 0, "(DECRQSS) Erase attribute", "\033P$q\"q\033\\"},
69 {63, 0, "(DECRQSS) Personality", "\033P$q\"p\033\\"},
70 {63, 0, "(DECRQSS) Top and bottom margins", "\033P$qr\033\\"},
71 {63, 0, "(DECRQSS) Character attributes", "\033P$qm\033\\"},
72 {63, 0, "(DECRQSS) Illegal request", "\033P$q@\033\\"},
73 {63, 0, "(DECRQUPSS) User pref supplemental set", "\033[&u"},
74 {63, 0, "(DECRQPSR) Cursor information", "\033[1$w"},
75 {63, 0, "(DECRQPSR) Tab stop information", "\033[2$w"},
76 {64, 0, "(DA) Tertiary device attributes", "\033[=0c"},
77 {64, 0, "(DSR) Extended cursor position", "\033[?6n"},
78 {64, 0, "(DSR) Macro space", "\033[?62n"},
79 {64, 0, "(DSR) Memory checksum", "\033[?63n"},
80 {64, 0, "(DSR) Data integrity", "\033[?75n"},
81 {64, 0, "(DSR) Multiple session status", "\033[?85n"},
82 {64, 0, "(DECRQSS) Attribute change extent", "\033P$q*x\033\\"},
83 {64, 0, "(DECRQSS) Columns per page", "\033P$q$|\033\\"},
84 {64, 0, "(DECRQSS) Lines per page", "\033P$qt\033\\"},
85 {64, 0, "(DECRQSS) Lines per screen", "\033P$q*|\033\\"},
86 {64, 0, "(DECRQSS) Left and right margins", "\033P$qs\033\\"},
87 {64, 0, "(DECRQSS) Local functions", "\033P$q+q\033\\"},
88 {64, 0, "(DECRQSS) Local function key control", "\033P$q=}\033\\"},
89 {64, 0, "(DECRQSS) Select modifier key reporting", "\033P$q+r\033\\"},
90 {64, 0, "(DECRQDE) Window report", "\033[\"v"},
94 struct request_control {
99 const char *reset_mode;
102 /* Request control function selection or setting */
103 static const struct request_control rqss[] = {
104 {"Data sent to screen", "0", "$}", "\033[0$}", 0},
105 {"Data sent to disabled status line", "0", "$}", 0, 0},
106 {"\033[0$~\033[1$}", "\033[0$}", 0, 0, 0},
107 {"Data sent to enabled status line", "1", "$}", 0, 0},
108 {"\033[2$~\033[1$}", "\033[0$}", 0, 0, 0},
109 {"Disable status line", "0", "$~", "\033[0$~", 0},
110 {"Top status line", "1", "$~", "\033[1$~", 0},
111 {"Bottom status line", "2", "$~", "\033[2$~", 0},
112 {"Erasable character", "0", "\"q", "\033[0\"q", 0},
113 {"Nonerasable character", "1", "\"q", "\033[1\"q", "\033[0\"q"},
114 {"Top and bottom margins", "3;10", "r", "\0337\033[3;10r", 0},
115 {"\033[r\0338", 0, 0, 0, 0},
116 {"Top and bottom margins", "default", "r", "\0337\033[r", "\0338"},
117 {"Character attributes, dim, bold", "1", "m", "\033[2;1m", "\033[m"},
118 {"Character attributes, bold, dim", "2", "m", "\033[1;2m", "\033[m"},
119 {"Character attributes, under, rev", "4;7", "m", "\033[4;7m", "\033[m"},
120 {"Character attributes, color", "35;42", "m", "\033[35;42m", "\033[m"},
121 {"All character attributes", "", "m", "\033[1;2;3;4;5;6;7;8;9m", 0},
122 {"\033[m", 0, 0, 0, 0},
130 ** read an ANSI status report from terminal
135 int ch, i, j, last_escape;
138 read_key(ansi_buf, sizeof(ansi_buf));
139 /* Throw away control characters inside CSI sequences.
140 Convert two character 7-bit sequences into 8-bit sequences. */
141 for (i = j = last_escape = 0; (ch = ansi_buf[i]) != 0; i++) {
143 if (last_escape == A_ESC) {
144 pack_buf[j++] = A_ESC;
148 if (last_escape == A_ESC && ch >= '@' && ch <= '_') {
149 pack_buf[j++] = last_escape = ch + 0x40;
151 if (last_escape != A_CSI || (ch > 0x20 && ch != 0x80)) {
152 if (last_escape == A_ESC) {
153 pack_buf[j++] = A_ESC;
155 if (ch > 0x80 && ch < 0xa0) {
161 if (last_escape == A_ESC) {
162 pack_buf[j++] = A_ESC;
169 ** valid_mode(expected)
171 ** read a terminal mode status report and parse the result
172 ** Return TRUE if we got the expected terminating character.
175 valid_mode(int expected)
183 ch = pack_buf[0] & 0xff;
185 if (ch != A_CSI && ch != A_DCS)
190 if ((*s >= '<') & (*s <= '?')) {
194 for (; (ch = *s); s++) {
195 if (ch >= '0' && ch <= '9')
196 ansi_value[ape] = ansi_value[ape] * 10 + ch - '0';
197 else if (ch == ';' || ch == ':')
198 ansi_value[++ape] = 0;
199 else if (ch >= '<' && ch <= '?')
202 terminator = (terminator << 8) | ch;
206 return terminator == expected;
212 ** read all the reports in the ANSI report structure
217 int i, j, k, tc, vcr, lc;
221 terminal_class = tc = 0;
222 for (i = 0; report_list[i].text; i++, lc++) {
223 if (terminal_class < report_list[i].lvl &&
224 tc < report_list[i].lvl) {
227 ptext("/status [q] > ");
229 if (j != 'n' && j != 'N')
231 tc = report_list[i].lvl;
233 } else if (lc + 2 >= lines) {
235 ptext("Hit any key to continue ");
239 sprintf(temp, "%s (%s) ", report_list[i].text,
240 expand_command(report_list[i].request));
242 for (j = strlen(temp); j < 49; j++)
244 tc_putp(report_list[i].request);
246 if (report_list[i].final == 0) {
248 } else if (valid_mode(report_list[i].final))
249 switch (report_list[i].final) {
251 terminal_class = ansi_value[0];
257 j = pack_buf[0] & 0xff;
258 if (j != A_CSI && j != A_DCS) {
260 s = "*** The above request gives illegal response ***";
262 for (j = strlen(s); j < 49; j++)
265 s = expand(ansi_buf);
266 if (char_count + expand_chars >= columns) {
271 if (vcr) { /* find out how big the screen is */
272 tc_putp(report_list[i].request);
273 if (!valid_mode('R'))
277 tc_putp("\033[255B\033[255C\033[6n");
278 if (!valid_mode('R'))
280 sprintf(temp, "\033[%d;%dH", j, k);
282 ptext("(DSR) Screen size (CSI 6 n)");
283 for (j = char_count; j < 50; j++)
285 sprintf(temp, "%d x %d", ansi_value[1], ansi_value[0]);
291 ptext("/status r->repeat test, <return> to continue > ");
298 ** Request Control function selection or settings
307 ptextln("Request Expected Received");
309 for (i = 0; rqss[i].text; i++) {
311 j = strlen(rqss[i].text) + strlen(rqss[i].expect);
313 for (j++; j < 40; j++)
315 ptext(rqss[i].expect);
317 tc_putp(rqss[i].set_mode);
318 sprintf(temp, "\033P$q%s\033\\", rqss[i].request);
321 tc_putp(rqss[i].reset_mode);
323 for (j = 0; ansi_buf[j]; j++) {
324 if (ansi_buf[j] == 'r') {
325 for (k = j++; (ch = (ansi_buf[k] & 0xff)); k++)
328 } else if (ch == A_ST) {
332 s = expand(&ansi_buf[j]);
333 if (char_count + expand_chars >= columns)
340 /* calculate the valid attributes */
341 ptext("Valid attributes: 0");
343 for (i = 1; i < 20; i++) {
344 sprintf(temp, "\033[0;%dm\033P$qm\033\\", i);
346 (void) valid_mode('m');
349 sprintf(temp, "\033[0m; %d", i);
354 /* calculate how many parameters can be sent */
355 ptext("Max number of parameters: ");
356 sprintf(temp, "%dm\033P$qm\033\\", j);
359 for (l = 1; l < 33; l++) {
361 for (ch = 1; ch <= l; ch++)
364 (void) valid_mode('m');
370 sprintf(temp, "%d", l);
379 ** mode_display(puc, mode, initial, set, reset)
381 ** print the mode display entry
384 mode_display(const char *p, int n, int c, char s, char r)
388 sprintf(temp, "%s%d (%c, %c, %c)", p, n, c, s, r);
390 if (char_count + k >= columns)
400 ** test DECRQM status reports
405 static const char *puc[] = {"", "<", "=", ">", "?", 0};
407 int i, j, k, l, modes_found;
409 char buf[256], tms[256];
410 int mode_puc[MAX_MODES], mode_number[MAX_MODES];
411 char set_value[MAX_MODES], reset_value[MAX_MODES];
412 char current_value[MAX_MODES];
414 ptext("Testing terminal mode status. (CSI 0 $ p)");
418 if (valid_mode(('$' << 8) | 'y')) {
419 for (i = 0; puc[i]; i++) {
422 sprintf(temp, "Private use: %c", puc[i][0]);
424 strcpy(temp, "Standard modes:");
428 for (j = 0; j < (int) sizeof(buf); buf[j++] = ' ')
430 for (j = l = 0; j < 255 && j - l < 50; j++) {
431 sprintf(temp, "\033[%s%d$p", puc[i], j);
433 if (!valid_mode(('$' << 8) | 'y')) {
434 /* not valid, save terminating value */
435 s = expand(ansi_buf);
436 sprintf(tms, "%s%s%d %s ", tms,
440 if (private_use != puc[i][0])
442 if (ansi_value[0] != j)
450 for (k = 0; k < (int) sizeof(buf);) {
455 sprintf(temp, " %d", j);
458 buf[k - 1] = ansi_value[1] + '0';
459 if (modes_found >= MAX_MODES)
461 current_value[modes_found] =
463 /* some modes never return */
464 if ((i == 0 && j == 13) /* control execution */
465 || (puc[i][0] == '?' && j == 2)) /* VT52 */
466 set_value[modes_found] =
467 reset_value[modes_found] = '-';
469 set_value[modes_found] =
470 reset_value[modes_found] = ' ';
471 mode_puc[modes_found] = i;
472 mode_number[modes_found++] = j;
476 if (buf[k - 1] != ' ') {
482 if ((i = modes_found) != 0) {
488 ptext("Hit 'Y' to test mode set/reset states: ");
491 if (i == 'y' || i == 'Y')
497 fp = fopen("ted.ansi", "w");
499 fp = fopen("/dev/console", "w");
502 for (i = j = 0; j < modes_found; j = ++i >> 1) {
503 if (set_value[j] == '-')
505 k = (current_value[j] ^ i) & 1;
506 sprintf(temp, "\033[%s%d%c\033[%s%d$p",
507 puc[mode_puc[j]], mode_number[j],
509 puc[mode_puc[j]], mode_number[j]);
512 fprintf(fp, "%s\n", expand(temp));
517 if (!valid_mode(('$' << 8) | 'y'))
520 reset_value[j] = ansi_value[1] + '0';
522 set_value[j] = ansi_value[1] + '0';
525 put_str("\033[30l"); /* added for GORT bug
532 /* print the results */
534 putln("mode (initial, set, reset)");
535 for (j = 0; j < modes_found; j++) {
536 mode_display(puc[mode_puc[j]], mode_number[j],
537 current_value[j], set_value[j], reset_value[j]);
539 ptext("\n\nHit 'R' to repeat test. 'S' to sort results: ");
541 if (i == 's' || i == 'S') { /* print the same stuff,
545 for (i = '1'; i <= '4'; i++) {
546 for (j = 0; j < modes_found; j++) {
547 if (current_value[j] == i)
548 mode_display(puc[mode_puc[j]],
549 mode_number[j], current_value[j],
550 set_value[j], reset_value[j]);
553 ptext("\n\nHit 'R' to repeat test: ");
556 if (i != 'r' && i != 'R')
558 tty_raw(1, char_mask);
567 ** ansi_report_help()
569 ** Display the informational data for the ANSI report test.
572 ansi_report_help(void)
574 ptext("Begin ANSI status report testing. ");
575 ptext(" Parity bit set will be displayed in reverse video. ");
576 ptext(" If the terminal hangs, hit any alphabetic key. ");
577 ptextln(" Use n to continue testing. Use q to quit.");
582 ** test_ansi_reports()
584 ** Test the ANSI status report functions
588 struct test_list *t GCC_UNUSED,
589 int *state GCC_UNUSED,
596 tty_raw(1, char_mask);
600 if (i != 'r' && i != 'R') {
606 if (terminal_class >= 63) {
609 } while (i == 'r' || i == 'R');
621 ** Test a range of ANSI sgr attributes
622 ** puc -> Private Use Character
631 for (k = 0; k < 80; k++) {
632 if (char_count + 8 > 80)
634 else if (char_count + 8 > columns)
638 printf("\033[%s%dmMode %2d\033[0m", temp, k, k);
653 printf("\033[%s0m", temp);
658 ** print_sgr20(on, off)
660 ** print the sgr line for sgr20()
663 print_sgr20(int on, int off)
665 if (char_count > columns - 13) {
667 } else if (char_count) {
671 printf("%d/%d \033[%dmon\033[%dm off\033[0m", on, off, on, off);
677 ** display the enter/exit attributes 1-9 and 20-29
685 ptextln("Test enter/exit attributes 1-9 and 21-29.");
686 for (k = 1; k < 10; k++) {
687 print_sgr20(k, k + 20);
689 print_sgr20(1, 22); /* bold */
690 print_sgr20(2, 22); /* dim */
691 print_sgr20(8, 22); /* blank */
697 ** tools_sgr(testlist, state, ch)
699 ** Run the ANSI graphics rendition mode tool
700 ** Return the last character typed.
704 struct test_list *t GCC_UNUSED,
705 int *state GCC_UNUSED,
715 ptext("/sgr Enter =><?r [<cr>] > ");
717 if ((k == 'r') || (k == 'R')) {
719 } else if ((k < '<') || (k > '?')) {
726 *ch = REQUEST_PROMPT;
729 /*****************************************************************************
733 *****************************************************************************/
737 ** select a graphics character set for ANSI terminals
740 select_bank(char *bank)
743 switch (bank[1] & 3) {
745 putchp('O' & 0x1f); /* control O */
748 putchp('N' & 0x1f); /* control N */
752 tc_putp("\033n\033}");
755 tc_putp("\033o\033|");
761 ** show_characters(bank, bias)
763 ** print the ANSI graphics characters
766 show_characters(char *bank, int bias)
770 sprintf(temp, "G%d GL ", bank[1] & 3);
773 for (i = ' '; i < 0x80; i++) {
774 if (char_count >= columns ||
775 (i != ' ' && (i & 31) == 0))
779 select_bank(default_bank);
783 select_bank(default_bank);
790 /* ANSI graphics test
801 Dec extended definitions
807 ** tools_charset(testlist, state, ch)
809 ** Run the ANSI alt-charset mode tool
813 struct test_list *t GCC_UNUSED,
814 int *state GCC_UNUSED,
821 ptext("Enter the bank ()*+,-./ followed by the character set");
822 ptext(" 0123456789:;<=>? for private use, and");
823 ptextln(" @A...Z[\\]^_`a...z{|}~ for standard sets.");
824 strcpy(bank, "\033)0");
827 show_characters(bank, 0);
829 /* G0 will not print in GR */
831 show_characters(bank, 0x80);
834 for (j = 1; (ch = getchp(char_mask)); j++) {
838 if (j == 1 && ch > '/')
841 if (ch < ' ' || ch > '/')
843 if (j + 1 >= (int) sizeof(bank))
848 if (bank[j] < '0' || bank[j] > '~')