Debian Codegraph @

charsets.c

raw
1 /* $Id: charsets.c,v 1.108 2025/12/02 00:55:36 tom Exp $ */
2
3 /*
4 * Test character-sets (e.g., SCS control, DECNRCM mode)
5 */
6 #include <vttest.h>
7 #include <esc.h>
8 #include <ttymodes.h>
9
10 /* the values, where specified, correspond to the keyboard-language codes */
11 typedef enum {
12 ASCII = 1,
13 British = 2,
14 Flemish = 3,
15 French_Canadian = 4,
16 Danish = 5,
17 Finnish = 6,
18 German = 7,
19 Dutch = 8,
20 Italian = 9,
21 Swiss_French = 10,
22 Swiss_German = 11,
23 Swiss,
24 Swedish = 12,
25 Norwegian_Danish = 13,
26 French = 14,
27 Spanish = 15,
28 Portuguese = 16,
29 Hebrew = 17,
30 British_Latin_1,
31 Cyrillic_DEC,
32 DEC_Alt_Chars,
33 DEC_Alt_Graphics,
34 DEC_Spec_Graphic,
35 DEC_Supp,
36 DEC_Supp_Graphic,
37 DEC_Technical,
38 DEC_UPSS,
39 Greek,
40 Greek_DEC,
41 Greek_Supp,
42 Hebrew_DEC,
43 Hebrew_Supp,
44 JIS_Roman,
45 JIS_Katakana,
46 Latin_2_Supp,
47 Latin_5_Supp,
48 Latin_Cyrillic,
49 Russian,
50 Turkish,
51 Turkish_DEC,
52 SCS_NRCS,
53 Unknown
54 } National;
55
56 typedef struct {
57 National code; /* internal name (chosen to sort 'name' member) */
58 int cs_type; /* 0=94-charset, 1=NRCS(94-charset), 2=96-charset */
59 int order; /* check-column so we can mechanically-sort this table */
60 int first; /* first model: 0=base, 2=vt220, 3=vt320, etc. */
61 int last; /* last model: 0=base, 2=vt220, 3=vt320, etc. */
62 const char *final; /* end of SCS string */
63 const char *name; /* the string we'll show the user */
64 const char *not11; /* cells which are not 1-1 with ISO-8859-1 */
65 } CHARSETS;
66 /* *INDENT-OFF* */
67
68 /* compare mappings using only 7-bits */
69 #define Only7(c) ((c) & 0x7f)
70 #define Not11(a,b) (Only7(a) == Only7(b))
71
72 /*
73 * User-preferred selection set is unknown until reset.
74 */
75 static National current_upss = Unknown;
76
77 /*
78 * The "map_XXX" strings list the characters that should be replaced for the
79 * given NRCS. Use that to highlight them for clarity.
80 */
81 static const char map_pound[] = "#";
82 static const char map_all94[] = "!\"#$%&'()*+,-./0123456789:;<=>?"
83 "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
84 "`abcdefghijklmnopqrstuvwxyz{|}~";
85 static const char map_all96[] = " !\"#$%&'()*+,-./0123456789:;<=>?"
86 "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
87 "`abcdefghijklmnopqrstuvwxyz{|}~\177";
88 /* National Replacement Character Sets */
89 static const char map_DEC_Supp[] = "$&,-./48>GHIJKLMNOPW^pw}~";
90 static const char map_Spec_Graphic[] = "`abcdefghijklmnopqrstuvwxyz{|}~";
91 static const char map_Supp_Graphic[] = "$&(,-./48>PW]^pw}~\177";
92 static const char map_Dutch[] = "#@[\\]{|}~";
93 static const char map_Finnish[] = "[\\]^`{|}~";
94 static const char map_French[] = "#@[\\]{|}~";
95 static const char map_French_Canadian[] = "@[\\]^`{|}~";
96 static const char map_German[] = "@[\\]{|}~";
97 static const char map_Greek[] = "abcdefghijklmnopqrstuvwxyz";
98 static const char map_Hebrew[] = "`abcdefghijklmnopqrstuvwxyz";
99 static const char map_Italian[] = "#@[\\]`{|}~";
100 static const char map_Norwegian[] = "@[\\]^`{|}~";
101 static const char map_Portuguese[] = "[\\]{|}";
102 static const char map_Russian[] = "`abcdefghijklmnopqrstuvwxyz{|}~";
103 static const char map_SCS[] = "@[\\]^`{|}~";
104 static const char map_Spanish[] = "#@[\\]{|}";
105 static const char map_Swedish[] = "@[\\]^`{|}~";
106 static const char map_Swiss[] = "#@[\\]^_`{|}~";
107 static const char map_Turkish[] = "&@[\\]^`{|}~";
108
109 /* DEC pre-ISO 94-Character Sets */
110 static const char map_DEC_Greek[] = "$&,-./48>"
111 "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
112 "`abcdefghijklmnopqrstuvwxyz{|}~";
113 static const char map_DEC_Hebrew[] = "$&,-./48>"
114 "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
115 "`abcdefghijklmnopqrstuvwxyz{|}~";
116 static const char map_DEC_Turkish[] = "$&,-./48>?"
117 "PW]^"
118 "pw~";
119
120 /* DEC VT382 94-character sets */
121 static const char map_JIS_Roman[] = "\\~";
122 static const char map_JIS_Katakana[] = "!\"#$%&'()*+,-./0123456789:;<=>?"
123 "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
124
125 /* ISO 96-Character Sets */
126 static const char map_ISO_Greek[] = "!\"$%*./45689:<>?"
127 "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
128 "`abcdefghijklmnopqrstuvwxyz{|}~\177";
129 static const char map_ISO_Hebrew[] = "!*:?"
130 "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
131 "`abcdefghijklmnopqrstuvwxyz{|}~\177";
132 static const char map_ISO_Latin_2[] = "!\"#%&)*+,./1235679:;<=>?"
133 "@CEFHJLOPQRUXY[^"
134 "`cefhjlopqruxy{~\177";
135 static const char map_ISO_Latin_5[] = "P]^p}~";
136
137 static const CHARSETS KnownCharsets[] = {
138 { ASCII, 0, 0, 0, 9, "B", "US ASCII", NULL },
139 { British, 1, 0, 0, 9, "A", "British", map_pound },
140 { British_Latin_1, 2, 0, 3, 9, "A", "ISO Latin-1", NULL },
141 { Cyrillic_DEC, 1, 0, 5, 9, "&4", "Cyrillic (DEC)", map_all94 },
142 { DEC_Spec_Graphic, 0, 0, 0, 9, "0", "DEC Special graphics and line drawing", map_Spec_Graphic },
143 { DEC_Alt_Chars, 0, 0, 0, 0, "1", "DEC Alternate character ROM standard characters", NULL },
144 { DEC_Alt_Graphics, 0, 0, 0, 0, "2", "DEC Alternate character ROM special graphics", NULL },
145 { DEC_Supp, 0, 0, 2, 2, "<", "DEC Supplemental", map_DEC_Supp },
146 { DEC_UPSS, 0, 0, 3, 9, "<", "User-preferred supplemental", NULL },
147 { DEC_Supp_Graphic, 1, 0, 3, 9, "%5", "DEC Supplemental Graphic", map_Supp_Graphic },
148 { DEC_Technical, 0, 0, 3, 9, ">", "DEC Technical", map_all94 },
149 { Dutch, 1, 0, 2, 9, "4", "Dutch", map_Dutch },
150 { Finnish, 1, 0, 2, 9, "5", "Finnish", map_Finnish },
151 { Finnish, 1, 1, 2, 9, "C", "Finnish", map_French },
152 { French, 1, 0, 2, 9, "R", "French", map_French },
153 { French, 1, 1, 2, 9, "f", "French", map_French }, /* Kermit (vt340 model?) */
154 { French_Canadian, 1, 0, 2, 9, "Q", "French Canadian", map_French_Canadian },
155 { French_Canadian, 1, 1, 3, 9, "9", "French Canadian", map_French_Canadian },
156 { German, 1, 0, 2, 9, "K", "German", map_German },
157 { Greek, 1, 0, 5, 9, "\">", "Greek", map_Greek },
158 { Greek_DEC, 1, 0, 5, 9, "\"?", "Greek (DEC)", map_DEC_Greek },
159 { Greek_Supp, 2, 0, 5, 9, "F", "ISO Greek Supplemental", map_ISO_Greek },
160 { Hebrew, 1, 0, 5, 9, "%=", "Hebrew", map_Hebrew },
161 { Hebrew_DEC, 1, 0, 5, 9, "\"4", "Hebrew (DEC)", map_DEC_Hebrew },
162 { Hebrew_Supp, 2, 0, 5, 9, "H", "ISO Hebrew Supplemental", map_ISO_Hebrew },
163 { Italian, 1, 0, 2, 9, "Y", "Italian", map_Italian },
164 { JIS_Roman, 0, 0, 3, 3, "J", "JIS-Roman", map_JIS_Roman },
165 { JIS_Katakana, 0, 0, 3, 3, "I", "JIS-Katakana", map_JIS_Katakana },
166 { Latin_2_Supp, 2, 0, 5, 9, "B", "ISO Latin-2 Supplemental", map_ISO_Latin_2 },
167 { Latin_5_Supp, 2, 0, 5, 9, "M", "ISO Latin-5 Supplemental", map_ISO_Latin_5 },
168 { Latin_Cyrillic, 2, 0, 5, 9, "L", "ISO Latin-Cyrillic", map_all96 },
169 { Norwegian_Danish, 1, 0, 3, 9, "`", "Norwegian/Danish", map_Norwegian },
170 { Norwegian_Danish, 1, 1, 2, 9, "E", "Norwegian/Danish", map_Norwegian },
171 { Norwegian_Danish, 1, 2, 2, 9, "6", "Norwegian/Danish", map_Norwegian },
172 { Portuguese, 1, 0, 3, 9, "%6", "Portuguese", map_Portuguese },
173 { Russian, 1, 0, 5, 9, "&5", "Russian", map_Russian },
174 { SCS_NRCS, 1, 0, 5, 9, "%3", "SCS", map_SCS },
175 { Spanish, 1, 0, 2, 9, "Z", "Spanish", map_Spanish },
176 { Swedish, 1, 0, 2, 9, "7", "Swedish", map_Swedish },
177 { Swedish, 1, 1, 2, 9, "H", "Swedish", map_Swedish },
178 { Swiss, 1, 0, 2, 9, "=", "Swiss", map_Swiss },
179 { Turkish, 1, 0, 5, 9, "%2", "Turkish", map_Turkish },
180 { Turkish_DEC, 1, 0, 5, 9, "%0", "Turkish (DEC)", map_DEC_Turkish },
181 { Unknown, 0, 0,-1,-1, "?", "Unknown", NULL }
182 };
183 /* *INDENT-ON* */
184
185 static int hilite_not11;
186
187 static char sgr_hilite[10];
188 static char sgr_reset[10];
189
190 int scs_national; /* true if NRCS should be tested */
191 int current_Gx[4];
192
193 static int
194 lookupCode(National code)
195 {
196 int n;
197 for (n = 0; n < TABLESIZE(KnownCharsets); n++) {
198 if (KnownCharsets[n].code == code)
199 return n;
200 }
201 return lookupCode(ASCII);
202 }
203
204 static const CHARSETS *
205 lookupCharset(int g, int n)
206 {
207 const CHARSETS *result = NULL;
208 if (n >= 0 && n < TABLESIZE(KnownCharsets)) {
209 if (!strcmp(KnownCharsets[n].final, "A")) {
210 if (scs_national || (g == 0)) {
211 n = lookupCode(British);
212 } else {
213 n = lookupCode(British_Latin_1);
214 }
215 }
216 result = &KnownCharsets[n];
217 }
218 return result;
219 }
220
221 static const char *
222 code2name(National code)
223 {
224 int n = lookupCode(code);
225 return KnownCharsets[n].name;
226 }
227
228 const char *
229 charset_name(int g, int n)
230 {
231 return lookupCharset(g, n)->name;
232 }
233
234 static int
235 append_sgr(char *buffer, int used, const char *sgr_string)
236 {
237 strcpy(buffer + used, sgr_string);
238 used += (int) strlen(sgr_string);
239 return used;
240 }
241
242 static void
243 send32(int row, int upper, const char *not11, int ok_GL)
244 {
245 int col;
246 int used = 0;
247 int hilited = 0;
248 char buffer[33 * 8];
249
250 if (LOG_ENABLED) {
251 fprintf(log_fp, NOTE_STR "send32 row %d, upper %d, not11:", row, upper);
252 if (not11) {
253 int lo = -1;
254 int hi = -1;
255 int nn;
256 for (nn = 0; not11[nn]; ++nn) {
257 int cc = Only7(not11[nn]);
258 if (lo < 0) {
259 lo = cc;
260 } else if (cc > hi + 1) { /* a gap after the existing range? */
261 if (lo == hi)
262 fprintf(log_fp, " %d", hi);
263 else
264 fprintf(log_fp, " %d-%d", lo, hi);
265 lo = cc;
266 }
267 hi = cc;
268 }
269 if (lo > 0 && hi > 0) {
270 if (lo == hi)
271 fprintf(log_fp, " %d", hi);
272 else
273 fprintf(log_fp, " %d-%d", lo, hi);
274 }
275 }
276 fprintf(log_fp, "\n");
277 }
278 for (col = 0; col <= 31; col++) {
279 char ch = (char) (row * 32 + upper + col);
280 if (not11 != NULL && hilite_not11 && (ok_GL || upper)) {
281 const char *p;
282 int found = 0;
283 for (p = not11; *p; ++p) {
284 if (Not11(*p, ch)) {
285 found = 1;
286 break;
287 }
288 }
289 if (found) {
290 if (!hilited) {
291 used = append_sgr(buffer, used, sgr_hilite);
292 hilited = 1;
293 }
294 } else {
295 if (hilited) {
296 used = append_sgr(buffer, used, sgr_reset);
297 hilited = 0;
298 }
299 }
300 }
301 buffer[used++] = ch;
302 }
303 if (hilited) {
304 used = append_sgr(buffer, used, sgr_reset);
305 }
306 buffer[used] = 0;
307 cprintf("%s", buffer);
308 }
309
310 static char *
311 scs_params(char *dst, int g)
312 {
313 const CHARSETS *tbl = lookupCharset(g, current_Gx[g]);
314
315 sprintf(dst, "%c%s",
316 (((g != 0) && (tbl->cs_type == 2) && (get_level() > 2))
317 ? "?-./"[g]
318 : "()*+"[g]),
319 tbl->final);
320 return dst;
321 }
322
323 int
324 do_scs(int g)
325 {
326 int rc = 0;
327 char buffer[80];
328
329 scs_params(buffer, g);
330 /* "?" is a placeholder: you cannot designate a 96-character set into G0 */
331 if (*buffer != '?') {
332 esc(buffer);
333 rc = 1;
334 }
335 return rc;
336 }
337
338 /* reset given Gg back to sane setting */
339 int
340 sane_cs(int g)
341 {
342 return lookupCode(((g == 0) || (get_level() <= 1))
343 ? ASCII
344 : (get_level() < 3
345 ? British
346 : British_Latin_1)); /* ...to get 8-bit codes 128-255 */
347 }
348
349 /* reset given Gg back to sane setting */
350 static int
351 reset_scs(int g)
352 {
353 return do_scs(sane_cs(g));
354 }
355
356 /* reset all of the Gn to sane settings */
357 int
358 reset_charset(MENU_ARGS)
359 {
360 int n;
361
362 scs_national = FALSE;
363 decnrcm(scs_national);
364 for (n = 0; n < 4; n++) {
365 int m = sane_cs(n);
366 if (m != current_Gx[n] || (m == 0)) {
367 int save = current_Gx[n];
368 current_Gx[n] = n ? m : 0;
369 if (!do_scs(n)) {
370 current_Gx[n] = save;
371 }
372 }
373 }
374 return MENU_NOHOLD;
375 }
376
377 static int the_code;
378 static int the_list[TABLESIZE(KnownCharsets) + 2];
379
380 static int
381 lookup_Gx(MENU_ARGS)
382 {
383 int n;
384 the_code = -1;
385 for (n = 0; n < TABLESIZE(KnownCharsets); n++) {
386 if (the_list[n]
387 && !strcmp(the_title, KnownCharsets[n].name)) {
388 the_code = n;
389 break;
390 }
391 }
392 return MENU_NOHOLD;
393 }
394
395 static void
396 specify_any_Gx(MENU_ARGS, int g)
397 {
398 MENU my_menu[TABLESIZE(KnownCharsets) + 2];
399 int n, m;
400
401 /*
402 * Build up a menu of the character sets we will allow the user to specify.
403 * There are a couple of tentative table entries (the "?" ones), which we
404 * won't show in any event. Beyond that, we limit some of the character sets
405 * based on the emulation level (vt220 implements national replacement
406 * character sets, for example, but not the 96-character ISO Latin-1).
407 */
408 for (n = m = 0; n < TABLESIZE(KnownCharsets); n++) {
409 the_list[n] = 0;
410 if (!strcmp(KnownCharsets[n].final, "?"))
411 continue;
412 if (get_level() < KnownCharsets[n].first)
413 continue;
414 if (get_level() > KnownCharsets[n].last)
415 continue;
416 if (scs_national && (KnownCharsets[n].cs_type == 2))
417 continue;
418 if (!scs_national && (KnownCharsets[n].cs_type == 1))
419 continue;
420 if (m && !strcmp(my_menu[m - 1].description, KnownCharsets[n].name))
421 continue;
422 my_menu[m].description = KnownCharsets[n].name;
423 my_menu[m].dispatch = lookup_Gx;
424 the_list[n] = 1;
425 m++;
426 }
427 my_menu[m].description = "";
428 my_menu[m].dispatch = NULL;
429
430 do {
431 vt_clear(2);
432 __(title(0), println(the_title));
433 __(title(2), println("Choose character-set:"));
434 } while (menu(my_menu) && the_code < 0);
435
436 current_Gx[g] = the_code;
437 }
438
439 static int
440 toggle_hilite(MENU_ARGS)
441 {
442 hilite_not11 = !hilite_not11;
443 if (hilite_not11) {
444 sprintf(sgr_hilite, "%s7m", csi_output());
445 sprintf(sgr_reset, "%sm", csi_output());
446 }
447 return MENU_NOHOLD;
448 }
449
450 int
451 toggle_nrc(MENU_ARGS)
452 {
453 scs_national = !scs_national;
454 decnrcm(scs_national);
455 return MENU_NOHOLD;
456 }
457
458 int
459 specify_G0(MENU_ARGS)
460 {
461 specify_any_Gx(PASS_ARGS, 0);
462 return MENU_NOHOLD;
463 }
464
465 int
466 specify_G1(MENU_ARGS)
467 {
468 specify_any_Gx(PASS_ARGS, 1);
469 return MENU_NOHOLD;
470 }
471
472 int
473 specify_G2(MENU_ARGS)
474 {
475 specify_any_Gx(PASS_ARGS, 2);
476 return MENU_NOHOLD;
477 }
478
479 int
480 specify_G3(MENU_ARGS)
481 {
482 specify_any_Gx(PASS_ARGS, 3);
483 return MENU_NOHOLD;
484 }
485
486 static int
487 tst_layout(MENU_ARGS)
488 {
489 char buffer[80];
490 return tst_keyboard_layout(scs_params(buffer, 0));
491 }
492
493 static int
494 tst_vt100_charsets(MENU_ARGS)
495 {
496 /* Test of:
497 * SCS (Select character Set)
498 */
499 int i, g, count, cset;
500
501 __(cup(1, 10), printxx("Selected as G0 (with SI)"));
502 __(cup(1, 48), printxx("Selected as G1 (with SO)"));
503 for (count = cset = 0; count < TABLESIZE(KnownCharsets); count++) {
504 const CHARSETS *tbl = KnownCharsets + count;
505 if (tbl->first == 0) {
506 int row = 3 + (4 * cset);
507
508 scs(1, 'B');
509 cup(row, 1);
510 sgr("1");
511 tprintf("Character set %s (%s)", tbl->final, tbl->name);
512 sgr("0");
513 for (g = 0; g <= 1; g++) {
514 int set_nrc = (get_level() >= 2 && tbl->final[0] == 'A');
515 if (set_nrc)
516 decnrcm(TRUE);
517 scs(g, (int) tbl->final[0]);
518 for (i = 1; i <= 3; i++) {
519 cup(row + i, 10 + 38 * g);
520 send32(i, 0, tbl->not11, 1);
521 }
522 if (set_nrc != scs_national)
523 decnrcm(scs_national);
524 }
525 ++cset;
526 }
527 }
528 scs_normal();
529 __(cup(max_lines, 1), printxx("These are the installed character sets. "));
530 return MENU_HOLD;
531 }
532
533 static int
534 tst_shift_in_out(MENU_ARGS)
535 {
536 /* Test of:
537 SCS (Select character Set)
538 */
539 static const char *label[] =
540 {
541 "Selected as G0 (with SI)",
542 "Selected as G1 (with SO)"
543 };
544 int i, cset;
545 char buffer[80];
546
547 __(cup(1, 10), printxx("These are the G0 and G1 character sets."));
548 for (cset = 0; cset < 2; cset++) {
549 const CHARSETS *tbl = lookupCharset(cset, current_Gx[cset]);
550 int row = 3 + (4 * cset);
551
552 scs(cset, 'B');
553 cup(row, 1);
554 sgr("1");
555 tprintf("Character set %s (%s)", tbl->final, tbl->name);
556 sgr("0");
557
558 cup(row, 48);
559 tprintf("%s", label[cset]);
560
561 esc(scs_params(buffer, cset));
562 for (i = 1; i <= 3; i++) {
563 cup(row + i, 10);
564 send32(i, 0, tbl->not11, cset || (tbl->cs_type < 2));
565 }
566 scs(cset, 'B');
567 }
568 cup(max_lines, 1);
569 scs_normal();
570 return MENU_HOLD;
571 }
572
573 #define map_g1_to_gr() esc("~") /* LS1R */
574
575 static int
576 tst_vt220_locking(MENU_ARGS)
577 {
578 /* *INDENT-OFF* */
579 static const struct {
580 int upper;
581 int mapped;
582 const char *code;
583 const char *msg;
584 } table[] = {
585 { 1, 1, "~", "G1 into GR (LS1R)" },
586 { 0, 2, "n", "G2 into GL (LS2)" }, /* "{" vi */
587 { 1, 2, "}", "G2 into GR (LS2R)" },
588 { 0, 3, "o", "G3 into GL (LS3)" },
589 { 1, 3, "|", "G3 into GR (LS3R)" },
590 };
591 /* *INDENT-ON* */
592
593 int i, cset;
594
595 __(cup(1, 10), tprintf("Locking shifts, with NRC %s:",
596 STR_ENABLED(scs_national)));
597 for (cset = 0; cset < TABLESIZE(table); cset++) {
598 int row = 3 + (4 * cset);
599 int map = table[cset].mapped;
600 const CHARSETS *tbl = lookupCharset(map, current_Gx[map]);
601 int map_gl = (strstr(table[cset].msg, "into GL") != NULL);
602
603 scs_normal();
604 cup(row, 1);
605 sgr("1");
606 tprintf("Character set %s (%s) in G%d", tbl->final, tbl->name, map);
607 sgr("0");
608
609 cup(row, 48);
610 tprintf("Maps %s", table[cset].msg);
611
612 for (i = 1; i <= 3; i++) {
613 if (table[cset].upper) {
614 scs_normal();
615 map_g1_to_gr();
616 } else {
617 do_scs(map);
618 esc(table[cset].code);
619 }
620 cup(row + i, 5);
621 send32(i, 0, map_gl ? tbl->not11 : NULL, map || (tbl->cs_type < 2));
622
623 if (table[cset].upper) {
624 do_scs(map);
625 esc(table[cset].code);
626 } else {
627 scs_normal();
628 /*
629 * That set G1 to ASCII, just like G0. While that works for VT100 (a
630 * 7-bit device), the later 8-bit models use Latin-1 for the default
631 * value of G1.
632 */
633 switch (get_level()) {
634 case 0:
635 case 1:
636 break;
637 case 2:
638 esc(")A"); /* select the 94-character NRCS, closest to MCS */
639 break;
640 default:
641 esc("-A"); /* select the 96-character set */
642 break;
643 }
644 map_g1_to_gr();
645 }
646 cup(row + i, 40);
647 send32(i, 128, map_gl ? NULL : tbl->not11, 1);
648 }
649 reset_scs(cset);
650 }
651 scs_normal();
652 cup(max_lines, 1);
653 return MENU_HOLD;
654 }
655
656 static int
657 tst_vt220_single(MENU_ARGS)
658 {
659 int pass, x, y;
660
661 for (pass = 0; pass < 2; pass++) {
662 int g = pass + 2;
663 const CHARSETS *tbl = lookupCharset(g, current_Gx[g]);
664
665 vt_clear(2);
666 cup(1, 1);
667 tprintf("Testing single-shift G%d into GL (SS%d) with NRC %s\n",
668 g, g, STR_ENABLED(scs_national));
669 tprintf("G%d is %s", g, tbl->name);
670
671 do_scs(g);
672 for (y = 0; y < 16; y++) {
673 for (x = 0; x < 6; x++) {
674 int ch = y + (x * 16) + 32;
675 int hilited = 0;
676
677 cup(y + 5, (x * 12) + 5);
678 tprintf("%3d: (", ch);
679 esc(pass ? "O" : "N"); /* SS3 or SS2 */
680 if (tbl->not11 && hilite_not11) {
681 const char *p;
682 for (p = tbl->not11; *p; ++p) {
683 if (Not11(*p, ch)) {
684 cprintf("%s", sgr_hilite);
685 hilited = 1;
686 break;
687 }
688 }
689 }
690 tprintf("%c", ch);
691 if (ch == 127 && (tbl->cs_type != 2))
692 tprintf(" "); /* DEL should have been eaten - skip past */
693 if (hilited) {
694 cprintf("%s", sgr_reset);
695 }
696 tprintf(")");
697 }
698 }
699
700 cup(max_lines, 1);
701 holdit();
702 }
703
704 return MENU_NOHOLD;
705 }
706
707 /******************************************************************************/
708
709 /*
710 * For parsing DECCIR response. The end of the response consists of so-called
711 * intermediate and final bytes as used by the SCS controls. Most of the
712 * strings fit into that description, but note that '<', '=' and '>' do not,
713 * since they are used to denote private parameters rather than final bytes.
714 * (But ECMA-48 hedges this by stating that the format in those cases is not
715 * specified).
716 */
717 const char *
718 parse_Sdesig(const char *source, int *offset)
719 {
720 int j;
721 const char *first = source + (*offset);
722 const char *result = NULL;
723 size_t limit = strlen(first);
724
725 for (j = 0; j < TABLESIZE(KnownCharsets); ++j) {
726 if (KnownCharsets[j].code != Unknown) {
727 size_t check = strlen(KnownCharsets[j].final);
728 if (check <= limit
729 && (strcmp(KnownCharsets[j].final, "A")
730 || (scs_national != (KnownCharsets[j].cs_type == 2)))
731 && !strncmp(KnownCharsets[j].final, first, check)) {
732 result = KnownCharsets[j].name;
733 *offset += (int) check;
734 break;
735 }
736 }
737 }
738 if (result == NULL) {
739 static char temp[80];
740 sprintf(temp, "? %#x\n", *source);
741 *offset += 1;
742 result = temp;
743 }
744 return result;
745 }
746
747 /*
748 * Lookup character set name for DECRQUPSS.
749 */
750 const char *
751 parse_upss_name(const char *source, int size)
752 {
753 const char *result = NULL;
754 int j;
755 int allow96 = (size == 96);
756
757 for (j = 0; j < TABLESIZE(KnownCharsets); ++j) {
758 if (allow96 != (KnownCharsets[j].cs_type == 2))
759 continue;
760 if (!strcmp(source, KnownCharsets[j].final)) {
761 result = KnownCharsets[j].name;
762 current_upss = KnownCharsets[j].code;
763 break;
764 }
765 }
766 /*
767 * Work around a contradiction between DEC 070 and the reference manuals.
768 */
769 if (result == NULL
770 && allow96
771 && (!strcmp(source, "A")
772 || !strcmp(source, "B"))) {
773 result = parse_upss_name(source, 94);
774 }
775 return result;
776 }
777
778 /*
779 * Reset G0 to ASCII
780 * Reset G1 to ASCII
781 * Shift-in.
782 */
783 void
784 scs_normal(void)
785 {
786 scs(0, 'B');
787 }
788
789 /*
790 * Set G0 to Line Graphics
791 * Reset G1 to ASCII
792 * Shift-in.
793 */
794 void
795 scs_graphics(void)
796 {
797 scs(0, '0');
798 }
799
800 static void
801 assign_upss(National code, int allow96)
802 {
803 if (code != current_upss) {
804 int n;
805 for (n = 0; n < TABLESIZE(KnownCharsets); ++n) {
806 if (KnownCharsets[n].code == code) {
807 if (get_level() >= KnownCharsets[n].first
808 && get_level() <= KnownCharsets[n].last) {
809 set_tty_raw(TRUE);
810 set_tty_echo(FALSE);
811 do_dcs("%d!u%s", allow96, KnownCharsets[n].final);
812 current_upss = code;
813 }
814 break;
815 }
816 }
817 }
818 restore_ttymodes();
819 }
820
821 static int
822 reset_upss(MENU_ARGS)
823 {
824 assign_upss(DEC_Supp_Graphic, 0);
825 return MENU_NOHOLD;
826 }
827
828 /* Test Assign User-Preferred Supplemental Set - VT320 and up */
829 static int
830 tst_DECAUPSS(MENU_ARGS)
831 {
832 MENU my_menu[TABLESIZE(KnownCharsets) + 2];
833 int n, m;
834 int level = get_level();
835
836 /*
837 * Build up a menu of the character sets we will allow the user to specify.
838 */
839 for (n = m = 0; n < TABLESIZE(KnownCharsets); n++) {
840 the_list[n] = 0;
841 if (!strcmp(KnownCharsets[n].final, "?"))
842 continue;
843 if (KnownCharsets[n].cs_type != 2)
844 switch (KnownCharsets[n].code) {
845 case ASCII: /* undocumented */
846 case DEC_Spec_Graphic: /* undocumented */
847 case DEC_Supp_Graphic:
848 case DEC_Technical: /* undocumented */
849 case Greek_DEC:
850 case Hebrew_DEC:
851 case Turkish_DEC:
852 case Cyrillic_DEC:
853 break;
854 default:
855 continue;
856 }
857 if (KnownCharsets[n].first == 2) /* exclude NRCS */
858 continue;
859 if (level < KnownCharsets[n].first)
860 continue;
861 if (level > KnownCharsets[n].last)
862 continue;
863 if (m && !strcmp(my_menu[m - 1].description, KnownCharsets[n].name))
864 continue;
865 my_menu[m].description = KnownCharsets[n].name;
866 my_menu[m].dispatch = lookup_Gx;
867 the_list[n] = 1;
868 m++;
869 }
870 my_menu[m].description = "";
871 my_menu[m].dispatch = NULL;
872
873 do {
874 vt_clear(2);
875 __(title(0), println(the_title));
876 __(title(2), println("Choose character-set:"));
877 } while (menu(my_menu) && the_code < 0);
878
879 assign_upss(KnownCharsets[the_code].code,
880 KnownCharsets[the_code].cs_type == 2);
881 return MENU_HOLD;
882 }
883
884 /* Test Report User-Preferred Supplemental Set - VT320 and up */
885 static int
886 tst_DECRQUPSS(MENU_ARGS)
887 {
888 int row, col;
889 char *report;
890 const char *show;
891 char buffer[80];
892
893 __(vt_move(1, 1), println("Testing DECRQUPSS Window Report"));
894
895 set_tty_raw(TRUE);
896 set_tty_echo(FALSE);
897
898 do_csi("&u");
899 report = get_reply();
900 vt_move(row = 3, col = 10);
901 chrprint2(report, row, col);
902 if ((report = skip_dcs(report)) != NULL
903 && strip_terminator(report)) {
904 int cs_size;
905 const char *cs_name;
906
907 if (*report == '0')
908 cs_size = 94;
909 else if (*report == '1')
910 cs_size = 96;
911 else
912 cs_size = -1;
913 show = "unknown";
914 if (cs_size > 0
915 && *++report == '!'
916 && *++report == 'u'
917 && (cs_name = parse_upss_name(++report, cs_size)) != NULL) {
918 sprintf(buffer, "%s (%d characters)", cs_name, cs_size);
919 show = buffer;
920 }
921 } else {
922 show = SHOW_FAILURE;
923 }
924 show_result("%s", show);
925
926 restore_ttymodes();
927 vt_move(max_lines - 1, 1);
928 return MENU_HOLD;
929 }
930
931 int
932 tst_characters(MENU_ARGS)
933 {
934 static char whatis_Gx[4][80];
935 static char hilite_mesg[80];
936 static char nrc_mesg[80];
937 /* *INDENT-OFF* */
938 static MENU my_menu[] = {
939 { "Exit", NULL },
940 { "Reset (G0 ASCII, G1 Latin-1, no NRC mode)", reset_charset },
941 { hilite_mesg, toggle_hilite },
942 { nrc_mesg, toggle_nrc },
943 { whatis_Gx[0], specify_G0 },
944 { whatis_Gx[1], specify_G1 },
945 { whatis_Gx[2], specify_G2 },
946 { whatis_Gx[3], specify_G3 },
947 { "Test VT100 Character Sets", tst_vt100_charsets },
948 { "Test Shift In/Shift Out (SI/SO)", tst_shift_in_out },
949 { "Test VT220 Locking Shifts", tst_vt220_locking },
950 { "Test VT220 Single Shifts", tst_vt220_single },
951 { "Test Soft Character Sets", not_impl },
952 { "Test Keyboard Layout with G0 Selection", tst_layout },
953 { "", NULL }
954 };
955 /* *INDENT-ON* */
956
957 hilite_not11 = 1;
958 toggle_hilite(PASS_ARGS);
959 reset_charset(PASS_ARGS); /* make the menu consistent */
960
961 if (get_level() > 1 || input_8bits || output_8bits) {
962 int n;
963
964 do {
965 vt_clear(2);
966 __(title(0), printxx("Character-Set Tests"));
967 __(title(2), println("Choose test type:"));
968 sprintf(hilite_mesg, "%s highlighting of non-ISO-8859-1 mapping",
969 STR_ENABLE(hilite_not11));
970 sprintf(nrc_mesg, "%s National Replacement Character (NRC) mode",
971 STR_ENABLE(scs_national));
972 for (n = 0; n < 4; n++) {
973 sprintf(whatis_Gx[n], "Specify G%d (now %s)",
974 n, charset_name(n, current_Gx[n]));
975 }
976 } while (menu(my_menu));
977 /* tidy in case a "vt100" emulator does not ignore SCS */
978 vt_clear(1);
979 return reset_charset(PASS_ARGS);
980 } else {
981 return tst_vt100_charsets(PASS_ARGS);
982 }
983 }
984
985 int
986 tst_upss(MENU_ARGS)
987 {
988 static char upss_mesg[120];
989 /* *INDENT-OFF* */
990 static MENU my_menu[] = {
991 { "Exit", NULL },
992 { "Test character sets", tst_characters },
993 { "Reset UPSS (User-Preferred Supplemental Sets)", reset_upss },
994 { upss_mesg, tst_DECAUPSS },
995 { "Test DECRQUPSS", tst_DECRQUPSS },
996 { "", NULL }
997 };
998 /* *INDENT-ON* */
999
1000 hilite_not11 = 1;
1001 toggle_hilite(PASS_ARGS);
1002 reset_charset(PASS_ARGS); /* make the menu consistent */
1003
1004 if (get_level() > 2) {
1005 do {
1006 sprintf(upss_mesg,
1007 "Assign UPSS (now %s)",
1008 code2name(current_upss));
1009 vt_clear(2);
1010 __(title(0), printxx("User-Preferred Supplemental Sets Tests"));
1011 __(title(2), println("Choose test type:"));
1012 } while (menu(my_menu));
1013 /* tidy in case a "vt100" emulator does not ignore SCS */
1014 vt_clear(1);
1015 return reset_upss(PASS_ARGS);
1016 } else {
1017 return tst_characters(PASS_ARGS);
1018 }
1019 }