]> ncurses.scripts.mit.edu Git - ncurses.git/blob - tack/ansi.c
ncurses 5.2
[ncurses.git] / tack / ansi.c
1 /*
2 ** Copyright (C) 1991, 1997 Free Software Foundation, Inc.
3 ** 
4 ** This file is part of TACK.
5 ** 
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)
9 ** any later version.
10 ** 
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.
15 ** 
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.
20 */
21
22 #include <tack.h>
23
24 MODULE_ID("$Id: ansi.c,v 1.5 2000/04/22 21:06:57 tom Exp $")
25
26 /*
27  * Standalone tests for ANSI terminals.  Three entry points:
28  * test_ansi_graphics(), test_ansi_reports() and test_ansi_sgr().
29  */
30
31 /*****************************************************************************
32  *
33  * Test ANSI status reports
34  *
35  *****************************************************************************/
36
37 /* ASCII control characters */
38 #define A_DC1 0x11              /* Control Q */
39 #define A_DC3 0x13              /* Control S */
40 #define A_ESC 0x1b
41 #define A_DCS 0x90
42 #define A_CSI 0x9b
43 #define A_ST  0x9c
44
45 #define MAX_MODES 256
46
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];
51
52 struct ansi_reports {
53         int lvl, final;
54         const char *text;
55         const char *request;
56 };
57
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"},
91         {0, 0, 0, 0}
92 };
93
94 struct request_control {
95         const char *text;
96         const char *expect;
97         const char *request;
98         const char *set_mode;
99         const char *reset_mode;
100 };
101
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},
123         {0, 0, 0, 0, 0}
124 };
125
126
127 /*
128 **      read_ansi()
129 **
130 **      read an ANSI status report from terminal
131 */
132 static void
133 read_ansi(void)
134 {
135         int ch, i, j, last_escape;
136
137         fflush(stdout);
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++) {
142                 if (ch == A_ESC) {
143                         if (last_escape == A_ESC) {
144                                 pack_buf[j++] = A_ESC;
145                         }
146                         last_escape = A_ESC;
147                 } else
148                 if (last_escape == A_ESC && ch >= '@' && ch <= '_') {
149                         pack_buf[j++] = last_escape = ch + 0x40;
150                 } else
151                 if (last_escape != A_CSI || (ch > 0x20 && ch != 0x80)) {
152                         if (last_escape == A_ESC) {
153                                 pack_buf[j++] = A_ESC;
154                         }
155                         if (ch > 0x80 && ch < 0xa0) {
156                                 last_escape = ch;
157                         }
158                         pack_buf[j++] = ch;
159                 }
160         }
161         if (last_escape == A_ESC) {
162                 pack_buf[j++] = A_ESC;
163         }
164         pack_buf[j] = '\0';
165         return;
166 }
167
168 /*
169 **      valid_mode(expected)
170 **
171 **      read a terminal mode status report and parse the result
172 **      Return TRUE if we got the expected terminating character.
173 */
174 static int
175 valid_mode(int expected)
176 {
177         char *s;
178         int ch, terminator;
179
180         read_ansi();
181
182         ape = 0;
183         ch = pack_buf[0] & 0xff;
184         ansi_value[0] = 0;
185         if (ch != A_CSI && ch != A_DCS)
186                 return FALSE;
187
188         s = pack_buf + 1;
189         private_use = 0;
190         if ((*s >= '<') & (*s <= '?')) {
191                 private_use = *s++;
192         }
193         terminator = 0;
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 <= '?')
200                         private_use = ch;
201                 else if (ch >= ' ')
202                         terminator = (terminator << 8) | ch;
203                 else
204                         break;
205         }
206         return terminator == expected;
207 }
208
209 /*
210 **      read_reports()
211 **
212 **      read all the reports in the ANSI report structure
213 */
214 static int
215 read_reports(void)
216 {
217         int i, j, k, tc, vcr, lc;
218         char *s;
219
220         lc = 5;
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) {
225                         put_crlf();
226                         menu_prompt();
227                         ptext("/status [q] > ");
228                         j = wait_here();
229                         if (j != 'n' && j != 'N')
230                                 return 0;
231                         tc = report_list[i].lvl;
232                         lc = 1;
233                 } else if (lc + 2 >= lines) {
234                         put_crlf();
235                         ptext("Hit any key to continue ");
236                         (void) wait_here();
237                         lc = 1;
238                 }
239                 sprintf(temp, "%s (%s) ", report_list[i].text,
240                         expand_command(report_list[i].request));
241                 ptext(temp);
242                 for (j = strlen(temp); j < 49; j++)
243                         putchp(' ');
244                 tc_putp(report_list[i].request);
245                 vcr = 0;
246                 if (report_list[i].final == 0) {
247                         read_ansi();
248                 } else if (valid_mode(report_list[i].final))
249                         switch (report_list[i].final) {
250                         case 'c':
251                                 terminal_class = ansi_value[0];
252                                 break;
253                         case 'R':
254                                 vcr = TRUE;
255                                 break;
256                         }
257                 j = pack_buf[0] & 0xff;
258                 if (j != A_CSI && j != A_DCS) {
259                         put_crlf();
260                         s = "*** The above request gives illegal response ***";
261                         ptext(s);
262                         for (j = strlen(s); j < 49; j++)
263                                 putchp(' ');
264                 }
265                 s = expand(ansi_buf);
266                 if (char_count + expand_chars >= columns) {
267                         put_str("\r\n        ");
268                         lc++;
269                 }
270                 putln(s);
271                 if (vcr) {      /* find out how big the screen is */
272                         tc_putp(report_list[i].request);
273                         if (!valid_mode('R'))
274                                 continue;
275                         j = ansi_value[0];
276                         k = ansi_value[1];
277                         tc_putp("\033[255B\033[255C\033[6n");
278                         if (!valid_mode('R'))
279                                 continue;
280                         sprintf(temp, "\033[%d;%dH", j, k);
281                         tc_putp(temp);
282                         ptext("(DSR) Screen size (CSI 6 n)");
283                         for (j = char_count; j < 50; j++)
284                                 putchp(' ');
285                         sprintf(temp, "%d x %d", ansi_value[1], ansi_value[0]);
286                         ptextln(temp);
287
288                 }
289         }
290         menu_prompt();
291         ptext("/status r->repeat test, <return> to continue > ");
292         return wait_here();
293 }
294
295 /*
296 **      request_cfss()
297 **
298 **      Request Control function selection or settings
299 */
300 static int
301 request_cfss(void)
302 {
303         int i, j, k, l, ch;
304         char *s;
305
306         put_clear();
307         ptextln("Request                         Expected  Received");
308         put_crlf();
309         for (i = 0; rqss[i].text; i++) {
310                 ptext(rqss[i].text);
311                 j = strlen(rqss[i].text) + strlen(rqss[i].expect);
312                 putchp(' ');
313                 for (j++; j < 40; j++)
314                         putchp(' ');
315                 ptext(rqss[i].expect);
316                 putchp(' ');
317                 tc_putp(rqss[i].set_mode);
318                 sprintf(temp, "\033P$q%s\033\\", rqss[i].request);
319                 tc_putp(temp);
320                 read_ansi();
321                 tc_putp(rqss[i].reset_mode);
322                 putchp(' ');
323                 for (j = 0; ansi_buf[j]; j++) {
324                         if (ansi_buf[j] == 'r') {
325                                 for (k = j++; (ch = (ansi_buf[k] & 0xff)); k++)
326                                         if (ch == A_ESC) {
327                                                 break;
328                                         } else if (ch == A_ST) {
329                                                 break;
330                                         }
331                                 ansi_buf[k] = '\0';
332                                 s = expand(&ansi_buf[j]);
333                                 if (char_count + expand_chars >= columns)
334                                         put_str("\r\n        ");
335                                 put_str(s);
336                         }
337                 }
338                 put_crlf();
339         }
340         /* calculate the valid attributes */
341         ptext("Valid attributes:         0");
342         j = 0;
343         for (i = 1; i < 20; i++) {
344                 sprintf(temp, "\033[0;%dm\033P$qm\033\\", i);
345                 tc_putp(temp);
346                 (void) valid_mode('m');
347                 if (ape > 0) {
348                         j = i;
349                         sprintf(temp, "\033[0m; %d", i);
350                         tc_putp(temp);
351                 }
352         }
353         put_crlf();
354         /* calculate how many parameters can be sent */
355         ptext("Max number of parameters: ");
356         sprintf(temp, "%dm\033P$qm\033\\", j);
357         l = -1;
358         if (j > 0)
359                 for (l = 1; l < 33; l++) {
360                         tc_putp("\033[0");
361                         for (ch = 1; ch <= l; ch++)
362                                 put_this(';');
363                         tc_putp(temp);
364                         (void) valid_mode('m');
365                         if (ape == 0)
366                                 break;
367                 }
368         tc_putp("\033[m");
369         if (l >= 0) {
370                 sprintf(temp, "%d", l);
371                 ptext(temp);
372         } else
373                 ptext("unknown");
374         put_crlf();
375         return wait_here();
376 }
377
378 /*
379 **      mode_display(puc, mode, initial, set, reset)
380 **
381 **      print the mode display entry
382 */
383 static void
384 mode_display(const char *p, int n, int c, char s, char r)
385 {
386         int k;
387
388         sprintf(temp, "%s%d (%c, %c, %c)", p, n, c, s, r);
389         k = strlen(temp);
390         if (char_count + k >= columns)
391                 put_crlf();
392         for (; k < 14; k++)
393                 putchp(' ');
394         put_str(temp);
395 }
396
397 /*
398 **      terminal_state()
399 **
400 **      test DECRQM status reports
401 */
402 static void
403 terminal_state(void)
404 {
405         static const char *puc[] = {"", "<", "=", ">", "?", 0};
406
407         int i, j, k, l, modes_found;
408         char *s;
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];
413
414         ptext("Testing terminal mode status. (CSI 0 $ p)");
415         tc_putp("\033[0$p");
416         modes_found = 0;
417         tms[0] = '\0';
418         if (valid_mode(('$' << 8) | 'y')) {
419                 for (i = 0; puc[i]; i++) {
420                         put_crlf();
421                         if (i) {
422                                 sprintf(temp, "Private use: %c", puc[i][0]);
423                         } else {
424                                 strcpy(temp, "Standard modes:");
425                         }
426                         k = strlen(temp);
427                         ptext(temp);
428                         for (j = 0; j < (int) sizeof(buf); buf[j++] = ' ')
429                                 ;
430                         for (j = l = 0; j < 255 && j - l < 50; j++) {
431                                 sprintf(temp, "\033[%s%d$p", puc[i], j);
432                                 tc_putp(temp);
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,
437                                                 puc[i], j, s);
438                                         break;
439                                 }
440                                 if (private_use != puc[i][0])
441                                         break;
442                                 if (ansi_value[0] != j)
443                                         break;
444                                 if (ansi_value[1]) {
445                                         l = j;
446                                         if (k > 70) {
447                                                 buf[k] = '\0';
448                                                 put_crlf();
449                                                 ptextln(buf);
450                                                 for (k = 0; k < (int) sizeof(buf);) {
451                                                         buf[k++] = ' ';
452                                                 }
453                                                 k = 0;
454                                         }
455                                         sprintf(temp, " %d", j);
456                                         ptext(temp);
457                                         k += strlen(temp);
458                                         buf[k - 1] = ansi_value[1] + '0';
459                                         if (modes_found >= MAX_MODES)
460                                                 continue;
461                                         current_value[modes_found] =
462                                                 ansi_value[1] + '0';
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] = '-';
468                                         else
469                                                 set_value[modes_found] =
470                                                         reset_value[modes_found] = ' ';
471                                         mode_puc[modes_found] = i;
472                                         mode_number[modes_found++] = j;
473                                 }
474                         }
475                         buf[k] = '\0';
476                         if (buf[k - 1] != ' ') {
477                                 put_crlf();
478                                 ptext(buf);
479                         }
480                 }
481
482         if ((i = modes_found) != 0) {
483                 put_crlf();
484                 put_crlf();
485                 if (tms[0]) {
486                         ptextln(tms);
487                 }
488                 ptext("Hit 'Y' to test mode set/reset states: ");
489                 i = wait_here();
490         }
491         if (i == 'y' || i == 'Y')
492                 while (1) {
493 #ifdef STATUSFIX
494                         FILE *fp;
495
496 #ifdef TEDANSI
497                         fp = fopen("ted.ansi", "w");
498 #else
499                         fp = fopen("/dev/console", "w");
500 #endif
501 #endif
502                         for (i = j = 0; j < modes_found; j = ++i >> 1) {
503                                 if (set_value[j] == '-')
504                                         continue;
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],
508                                         k ? 'l' : 'h',
509                                         puc[mode_puc[j]], mode_number[j]);
510 #ifdef STATUSFIX
511                                 if (fp) {
512                                         fprintf(fp, "%s\n", expand(temp));
513                                         fflush(fp);
514                                 }
515 #endif
516                                 tc_putp(temp);
517                                 if (!valid_mode(('$' << 8) | 'y'))
518                                         continue;
519                                 if (k) {
520                                         reset_value[j] = ansi_value[1] + '0';
521                                 } else {
522                                         set_value[j] = ansi_value[1] + '0';
523                                 }
524                         }
525                         put_str("\033[30l");    /* added for GORT bug
526                                                    (WY-185) */
527 #ifdef STATUSFIX
528                         if (fp)
529                                 fclose(fp);
530 #endif
531                         tty_set();
532                         /* print the results */
533                         put_clear();
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]);
538                         }
539                         ptext("\n\nHit 'R' to repeat test.  'S' to sort results: ");
540                         i = wait_here();
541                         if (i == 's' || i == 'S') {     /* print the same stuff,
542                                                            sorted by
543                                                            current_value */
544                                 put_crlf();
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]);
551                                         }
552                                 }
553                                 ptext("\n\nHit 'R' to repeat test: ");
554                                 i = wait_here();
555                         }
556                         if (i != 'r' && i != 'R')
557                                 break;
558                         tty_raw(1, char_mask);
559                 }
560         } else {
561                 tty_set();
562         }
563 }
564
565
566 /*
567 **      ansi_report_help()
568 **
569 **      Display the informational data for the ANSI report test.
570 */
571 static void
572 ansi_report_help(void)
573 {
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.");
578         put_crlf();
579 }
580
581 /*
582 **      test_ansi_reports()
583 **
584 **      Test the ANSI status report functions
585 */
586 void
587 tools_status(
588         struct test_list *t GCC_UNUSED,
589         int *state GCC_UNUSED,
590         int *ch)
591 {
592         int i;
593
594         put_clear();
595         ansi_report_help();
596         tty_raw(1, char_mask);
597
598         do {
599                 i = read_reports();
600                 if (i != 'r' && i != 'R') {
601                         *ch = i;
602                         return;
603                 }
604         } while (i);
605
606         if (terminal_class >= 63) {
607                 do {
608                         i = request_cfss();
609                 } while (i == 'r' || i == 'R');
610                 *ch = i;
611                 terminal_state();
612         } else {
613                 tty_set();
614         }
615 }
616
617
618 /*
619 **      display_sgr()
620 **
621 **      Test a range of ANSI sgr attributes
622 **      puc -> Private Use Character
623 */
624 static void 
625 display_sgr(int puc)
626 {
627         int k;
628
629         temp[0] = puc;
630         temp[1] = '\0';
631         for (k = 0; k < 80; k++) {
632                 if (char_count + 8 > 80)
633                         put_crlf();
634                 else if (char_count + 8 > columns)
635                         put_crlf();
636                 else if (k > 0)
637                         printf(" ");
638                 printf("\033[%s%dmMode %2d\033[0m", temp, k, k);
639                 char_count += 8;
640                 if (puc == '\0') {
641                         if (k == 19)
642                                 printf("\033[10m");
643                         if (k == 39)
644                                 printf("\033[37m");
645                         if (k == 49)
646                                 printf("\033[40m");
647                 }
648         }
649         put_crlf();
650         if (puc == '<')
651                 printf("\033[<1m");
652         else if (puc)
653                 printf("\033[%s0m", temp);
654         set_attr(0);
655 }
656
657 /*
658 **      print_sgr20(on, off)
659 **
660 **      print the sgr line for sgr20()
661 */
662 static void 
663 print_sgr20(int on, int off)
664 {
665         if (char_count > columns - 13) {
666                 put_crlf();
667         } else if (char_count) {
668                 put_str("  ");
669         }
670         char_count += 11;
671         printf("%d/%d \033[%dmon\033[%dm off\033[0m", on, off, on, off);
672 }
673
674 /*
675 **      sgr20(void)
676 **
677 **      display the enter/exit attributes 1-9 and 20-29
678 */
679 static void 
680 sgr20(void)
681 {
682         int k;
683
684         put_crlf();
685         ptextln("Test enter/exit attributes 1-9 and 21-29.");
686         for (k = 1; k < 10; k++) {
687                 print_sgr20(k, k + 20);
688         }
689         print_sgr20(1, 22);     /* bold */
690         print_sgr20(2, 22);     /* dim */
691         print_sgr20(8, 22);     /* blank */
692         printf("\033[0m");
693         set_attr(0);
694 }
695
696 /*
697 **      tools_sgr(testlist, state, ch)
698 **
699 **      Run the ANSI graphics rendition mode tool
700 **      Return the last character typed.
701 */
702 void
703 tools_sgr(
704         struct test_list *t GCC_UNUSED,
705         int *state GCC_UNUSED,
706         int *ch)
707 {
708         int k;
709
710         put_clear();
711         for (k = 0;;) {
712                 display_sgr(k);
713                 put_crlf();
714                 menu_prompt();
715                 ptext("/sgr Enter =><?r [<cr>] > ");
716                 k = wait_here();
717                 if ((k == 'r') || (k == 'R')) {
718                         k = 0;
719                 } else if ((k < '<') || (k > '?')) {
720                         break;
721                 }
722         }
723         sgr20();
724
725         put_newlines(2);
726         *ch = REQUEST_PROMPT;
727 }
728
729 /*****************************************************************************
730  *
731  * Test ANSI graphics
732  *
733  *****************************************************************************/
734 /*
735 **      select_bank(bank)
736 **
737 **      select a graphics character set for ANSI terminals
738 */
739 static void
740 select_bank(char *bank)
741 {
742         tc_putp(bank);
743         switch (bank[1] & 3) {
744         case 0:
745                 putchp('O' & 0x1f);     /* control O */
746                 break;
747         case 1:
748                 putchp('N' & 0x1f);     /* control N */
749                 tc_putp("\033~");
750                 break;
751         case 2:
752                 tc_putp("\033n\033}");
753                 break;
754         case 3:
755                 tc_putp("\033o\033|");
756                 break;
757         }
758 }
759
760 /*
761 **      show_characters(bank, bias)
762 **
763 **      print the ANSI graphics characters
764 */
765 static void
766 show_characters(char *bank, int bias)
767 {
768         int i;
769
770         sprintf(temp, "G%d GL   ", bank[1] & 3);
771         ptext(temp);
772         select_bank(bank);
773         for (i = ' '; i < 0x80; i++) {
774                 if (char_count >= columns ||
775                         (i != ' ' && (i & 31) == 0))
776                         put_str("\n        ");
777                 putchp(i + bias);
778         }
779         select_bank(default_bank);
780         put_str("   DEL <");
781         select_bank(bank);
782         putchp(0x7f + bias);
783         select_bank(default_bank);
784         putchp('>');
785         put_crlf();
786         put_crlf();
787 }
788
789
790 /* ANSI graphics test
791         94     96   character sets
792    G0   (      ,
793    G1   )      -
794    G2   *      .
795    G3   +      /
796
797 Standard Definitions
798    A    UK
799    B    US ASCII
800
801 Dec extended definitions
802    0    Special graphics
803
804  */
805
806 /*
807 **      tools_charset(testlist, state, ch)
808 **
809 **      Run the ANSI alt-charset mode tool
810 */
811 void
812 tools_charset(
813         struct test_list *t GCC_UNUSED,
814         int *state GCC_UNUSED,
815         int *chp GCC_UNUSED)
816 {
817         int j, ch;
818         char bank[32];
819
820         put_clear();
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");
825         for (; bank[0];) {
826                 put_crlf();
827                 show_characters(bank, 0);
828
829                 /* G0 will not print in GR */
830                 if (bank[1] & 3) {
831                         show_characters(bank, 0x80);
832                 }
833                 ptext("bank+set> ");
834                 for (j = 1; (ch = getchp(char_mask)); j++) {
835                         if (ch == EOF)
836                                 break;
837                         putchp(ch);
838                         if (j == 1 && ch > '/')
839                                 j++;
840                         bank[j] = ch;
841                         if (ch < ' ' || ch > '/')
842                                 break;
843                         if (j + 1 >= (int) sizeof(bank))
844                                 break;
845                 }
846                 if (j == 1)
847                         break;
848                 if (bank[j] < '0' || bank[j] > '~')
849                         break;
850                 bank[j + 1] = '\0';
851         }
852         put_crlf();
853 }