1 /****************************************************************************
2 * Copyright (c) 1998-2011,2012 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 * and: Thomas E. Dickey 1996 on *
33 ****************************************************************************/
36 * tic.c --- Main program for terminfo compiler
42 #include <progs.priv.h>
45 #include <dump_entry.h>
46 #include <hashed_db.h>
47 #include <transform.h>
49 MODULE_ID("$Id: tic.c,v 1.182 2012/12/16 00:03:12 tom Exp $")
51 #define STDIN_NAME "<stdin>"
53 const char *_nc_progname = "tic";
57 static bool capdump = FALSE; /* running as infotocap? */
58 static bool infodump = FALSE; /* running as captoinfo? */
59 static bool showsummary = FALSE;
60 static char **namelst = 0;
61 static const char *to_remove;
63 static void (*save_check_termtype) (TERMTYPE *, bool);
64 static void check_termtype(TERMTYPE *tt, bool);
66 static const char usage_string[] = "\
97 free_namelist(char **src)
101 for (n = 0; src[n] != 0; ++n)
114 free_namelist(namelst);
118 if (to_remove != 0) {
120 rc = remove(to_remove);
122 rc = unlink(to_remove);
130 failed(const char *msg)
133 ExitProgram(EXIT_FAILURE);
139 static const char *const tbl[] =
142 " -1 format translation output one capability per line",
144 " -a retain commented-out capabilities (sets -x also)",
146 " -K translate entries to termcap source form with BSD syntax",
147 " -C translate entries to termcap source form",
148 " -D print list of tic's database locations (first must be writable)",
149 " -c check only, validate input without compiling or translating",
150 " -e<names> translate/compile only entries named by comma-separated list",
151 " -f format complex strings for readability",
152 " -G format %{number} to %'char'",
153 " -g format %'char' to %{number}",
154 " -I translate entries to terminfo source form",
155 " -L translate entries to full terminfo source form",
156 " -N disable smart defaults for source translation",
157 " -o<dir> set output directory for compiled entry writes",
158 " -R<name> restrict translation to given terminfo/termcap version",
159 " -r force resolution of all use entries in source translation",
160 " -s print summary statistics",
161 " -T remove size-restrictions on compiled description",
163 " -t suppress commented-out capabilities",
165 " -U suppress post-processing of entries",
167 " -v[n] set verbosity level",
168 " -w[n] set format width for translation output",
170 " -x treat unknown capabilities as user-defined",
174 " <file> file to translate or compile"
178 fprintf(stderr, "Usage: %s %s\n", _nc_progname, usage_string);
179 for (j = 0; j < SIZEOF(tbl); j++) {
180 fputs(tbl[j], stderr);
183 ExitProgram(EXIT_FAILURE);
196 char result[MAX_ENTRY_SIZE];
199 * Look for strings that contain %{number}, convert them to %'char',
200 * which is shorter and runs a little faster.
202 for (n = 0; n < STRCOUNT; n++) {
203 s = ep->tterm.Strings[n];
205 && strchr(s, L_BRACE) != 0) {
208 while ((ch = *t++) != 0) {
212 } else if ((ch == '%')
213 && (*t == L_BRACE)) {
215 long value = strtol(t + 1, &v, 0);
219 && value != '\\' /* FIXME */
221 && isprint((int) value)) {
230 if (strlen(result) < strlen(s))
231 _nc_STRCPY(s, result, strlen(s) + 1);
235 _nc_set_type(_nc_first_name(ep->tterm.term_names));
236 _nc_curr_line = (int) ep->startline;
237 _nc_write_entry(&ep->tterm);
241 immedhook(ENTRY * ep GCC_UNUSED)
242 /* write out entries with no use capabilities immediately to save storage */
246 * This is strictly a core-economy kluge. The really clean way to handle
247 * compilation is to slurp the whole file into core and then do all the
248 * name-collision checks and entry writes in one swell foop. But the
249 * terminfo master file is large enough that some core-poor systems swap
250 * like crazy when you compile it this way...there have been reports of
251 * this process taking *three hours*, rather than the twenty seconds or
252 * less typical on my development box.
254 * So. This hook *immediately* writes out the referenced entry if it
255 * has no use capabilities. The compiler main loop refrains from
256 * adding the entry to the in-core list when this hook fires. If some
257 * other entry later needs to reference an entry that got written
258 * immediately, that's OK; the resolution code will fetch it off disk
259 * when it can't find it in core.
261 * Name collisions will still be detected, just not as cleanly. The
262 * write_entry() code complains before overwriting an entry that
263 * postdates the time of tic's first call to write_entry(). Thus
264 * it will complain about overwriting entries newly made during the
265 * tic run, but not about overwriting ones that predate it.
267 * The reason this is a hook, and not in line with the rest of the
268 * compiler code, is that the support for termcap fallback cannot assume
269 * it has anywhere to spool out these entries!
271 * The _nc_set_type() call here requires a compensating one in
274 * If you define HAVE_BIG_CORE, you'll disable this kluge. This will
275 * make tic a bit faster (because the resolution code won't have to do
276 * disk I/O nearly as often).
278 if (ep->nuses == 0) {
279 int oldline = _nc_curr_line;
282 _nc_curr_line = oldline;
283 free(ep->tterm.str_table);
286 #endif /* HAVE_BIG_CORE */
292 /* emit a comment char, translating terminfo names to termcap names */
294 static bool in_name = FALSE;
295 static size_t have, used;
296 static char *namebuf, *suffix;
299 if (used + 1 >= have) {
301 if ((namebuf = typeRealloc(char, have, namebuf)) == 0)
302 failed("put_translate namebuf");
303 if ((suffix = typeRealloc(char, have, suffix)) == 0)
304 failed("put_translate suffix");
306 if (c == '\n' || c == '@') {
307 namebuf[used++] = '\0';
309 (void) fputs(namebuf, stdout);
312 } else if (c != '>') {
313 namebuf[used++] = (char) c;
314 } else { /* ah! candidate name! */
316 NCURSES_CONST char *tp;
318 namebuf[used++] = '\0';
322 if ((up = strchr(namebuf, '#')) != 0
323 || (up = strchr(namebuf, '=')) != 0
324 || ((up = strchr(namebuf, '@')) != 0 && up[1] == '>')) {
325 _nc_STRCPY(suffix, up, have);
329 if ((tp = nametrans(namebuf)) != 0) {
331 (void) fputs(tp, stdout);
332 (void) fputs(suffix, stdout);
335 /* couldn't find a translation, just dump the name */
337 (void) fputs(namebuf, stdout);
338 (void) fputs(suffix, stdout);
352 /* Returns a string, stripped of leading/trailing whitespace */
358 while (isspace(UChar(*src)))
364 if ((dst = strdup(src)) == NULL) {
368 while (--len != 0 && isspace(UChar(dst[len])))
376 open_tempfile(char *filename)
380 _nc_STRCPY(filename, "/tmp/XXXXXX", PATH_MAX);
383 int oldmask = umask(077);
384 int fd = mkstemp(filename);
386 result = fdopen(fd, "w");
390 if (tmpnam(filename) != 0)
391 result = fopen(filename, "w");
397 copy_input(FILE *source, const char *filename, char *alt_file)
400 FILE *target = open_tempfile(alt_file);
404 failed("copy_input (source)");
405 } else if (target == 0) {
406 failed("copy_input (target)");
413 } else if (ferror(source)) {
415 } else if (ch == 0) {
416 /* don't loop in case someone wants to convert /dev/zero */
417 fprintf(stderr, "%s: %s is not a text-file\n", _nc_progname, filename);
418 ExitProgram(EXIT_FAILURE);
424 * rewind() does not force the target file's data to disk (not does
425 * fflush()...). So open a second stream on the data and then close
426 * the one that we were writing on before starting to read from the
429 result = fopen(alt_file, "r+");
431 to_remove = alt_file;
437 open_input(const char *filename, char *alt_file)
443 if (!strcmp(filename, "-")) {
444 fp = copy_input(stdin, STDIN_NAME, alt_file);
445 } else if (stat(filename, &sb) < 0) {
446 fprintf(stderr, "%s: %s %s\n", _nc_progname, filename, strerror(errno));
447 ExitProgram(EXIT_FAILURE);
448 } else if ((mode = (sb.st_mode & S_IFMT)) == S_IFDIR
449 || (mode != S_IFREG && mode != S_IFCHR)) {
450 fprintf(stderr, "%s: %s is not a file\n", _nc_progname, filename);
451 ExitProgram(EXIT_FAILURE);
453 fp = fopen(filename, "r");
456 fprintf(stderr, "%s: Can't open %s\n", _nc_progname, filename);
457 ExitProgram(EXIT_FAILURE);
459 if (mode != S_IFREG) {
461 FILE *fp2 = copy_input(fp, filename, alt_file);
464 fprintf(stderr, "%s: %s is not a file\n", _nc_progname, filename);
465 ExitProgram(EXIT_FAILURE);
472 /* Parse the "-e" option-value into a list of names */
474 make_namelist(char *src)
479 unsigned pass, n, nn;
484 } else if (strchr(src, '/') != 0) { /* a filename */
485 FILE *fp = open_input(src, (char *) 0);
487 for (pass = 1; pass <= 2; pass++) {
489 while (fgets(buffer, sizeof(buffer), fp) != 0) {
490 if ((s = stripped(buffer)) != 0) {
499 if ((dst = typeCalloc(char *, nn + 1)) == 0)
500 failed("make_namelist");
505 } else { /* literal list of names */
506 for (pass = 1; pass <= 2; pass++) {
507 for (n = nn = 0, base = src;; n++) {
509 if (mark == ',' || mark == '\0') {
514 if ((s = stripped(base)) != 0)
523 if ((dst = typeCalloc(char *, nn + 1)) == 0)
524 failed("make_namelist");
528 if (showsummary && (dst != 0)) {
529 fprintf(log_fp, "Entries that will be compiled:\n");
530 for (n = 0; dst[n] != 0; n++)
531 fprintf(log_fp, "%u:%s\n", n + 1, dst[n]);
537 matches(char **needle, const char *haystack)
538 /* does entry in needle list match |-separated field in haystack? */
544 for (n = 0; needle[n] != 0; n++) {
545 if (_nc_name_match(haystack, needle[n], "|")) {
556 valid_db_path(const char *nominal)
560 char suffix[] = DBM_SUFFIX;
561 size_t need = strlen(nominal) + sizeof(suffix);
562 char *result = malloc(need);
565 failed("valid_db_path");
566 _nc_STRCPY(result, nominal, need);
567 if (strcmp(result + need - sizeof(suffix), suffix)) {
568 _nc_STRCAT(result, suffix, need);
571 char *result = strdup(nominal);
574 DEBUG(1, ("** stat(%s)", result));
575 if (stat(result, &sb) >= 0) {
577 if (!S_ISREG(sb.st_mode)
578 || access(result, R_OK | W_OK) != 0) {
579 DEBUG(1, ("...not a writable file"));
584 if (!S_ISDIR(sb.st_mode)
585 || access(result, R_OK | W_OK | X_OK) != 0) {
586 DEBUG(1, ("...not a writable directory"));
592 /* check if parent is directory and is writable */
593 unsigned leaf = _nc_pathlast(result);
595 DEBUG(1, ("...not found"));
597 char save = result[leaf];
599 if (stat(result, &sb) >= 0
600 && S_ISDIR(sb.st_mode)
601 && access(result, R_OK | W_OK | X_OK) == 0) {
604 DEBUG(1, ("...parent directory %s is not writable", result));
609 DEBUG(1, ("... no parent directory"));
618 * Show the databases to which tic could write. The location to which it
619 * writes is always the first one. If none are writable, print an error
623 show_databases(const char *outdir)
625 bool specific = (outdir != 0) || getenv("TERMINFO") != 0;
627 const char *tried = 0;
630 outdir = _nc_tic_dir(0);
632 if ((result = valid_db_path(outdir)) != 0) {
633 printf("%s\n", result);
639 if ((outdir = _nc_home_terminfo())) {
640 if ((result = valid_db_path(outdir)) != 0) {
641 printf("%s\n", result);
643 } else if (!specific) {
649 * If we can write in neither location, give an error message.
653 fprintf(stderr, "%s: %s (no permission)\n", _nc_progname, tried);
654 ExitProgram(EXIT_FAILURE);
658 #define VtoTrace(opt) (unsigned) ((opt > 0) ? opt : (opt == 0))
661 main(int argc, char *argv[])
663 char my_tmpname[PATH_MAX];
664 char my_altfile[PATH_MAX];
666 unsigned debug_level;
667 int smart_defaults = TRUE;
671 int this_opt, last_opt = '?';
673 int outform = F_TERMINFO; /* output format */
674 int sortmode = S_TERMINFO; /* sort_mode */
678 bool formatted = FALSE; /* reformat complex strings? */
679 bool literal = FALSE; /* suppress post-processing? */
680 int numbers = 0; /* format "%'char'" to/from "%{number}" */
681 bool forceresolve = FALSE; /* force resolution */
683 char *tversion = (char *) NULL;
684 const char *source_file = "terminfo";
685 char *outdir = (char *) NULL;
686 bool check_only = FALSE;
687 bool suppress_untranslatable = FALSE;
691 _nc_progname = _nc_rootname(argv[0]);
694 if ((infodump = same_program(_nc_progname, PROG_CAPTOINFO)) != FALSE) {
695 outform = F_TERMINFO;
696 sortmode = S_TERMINFO;
698 if ((capdump = same_program(_nc_progname, PROG_INFOTOCAP)) != FALSE) {
700 sortmode = S_TERMCAP;
703 use_extended_names(FALSE);
708 * Processing arguments is a little complicated, since someone made a
709 * design decision to allow the numeric values for -w, -v options to
712 while ((this_opt = getopt(argc, argv,
713 "0123456789CDIKLNR:TUVace:fGgo:rstvwx")) != -1) {
714 if (isdigit(this_opt)) {
717 v_opt = (v_opt * 10) + (this_opt - '0');
720 width = (width * 10) + (this_opt - '0');
742 /* the initial version of -K in 20110730 fell-thru here, but the
743 * same flag is useful when reading sources -TD
749 sortmode = S_TERMCAP;
752 debug_level = VtoTrace(v_opt);
753 set_trace_level(debug_level);
754 show_databases(outdir);
755 ExitProgram(EXIT_SUCCESS);
759 outform = F_TERMINFO;
760 sortmode = S_TERMINFO;
764 outform = F_VARIABLE;
765 sortmode = S_VARIABLE;
768 smart_defaults = FALSE;
781 puts(curses_version());
782 ExitProgram(EXIT_SUCCESS);
787 namelst = make_namelist(optarg);
815 _nc_disable_period = FALSE;
816 suppress_untranslatable = TRUE;
819 _nc_disable_period = TRUE;
822 use_extended_names(TRUE);
831 debug_level = VtoTrace(v_opt);
832 set_trace_level(debug_level);
835 save_check_termtype = _nc_check_termtype2;
836 _nc_check_termtype2 = check_termtype;
840 * Aaargh! immedhook seriously hoses us!
842 * One problem with immedhook is it means we can't do -e. Problem
843 * is that we can't guarantee that for each terminal listed, all the
844 * terminals it depends on will have been kept in core for reference
845 * resolution -- in fact it's certain the primitive types at the end
846 * of reference chains *won't* be in core unless they were explicitly
847 * in the select list themselves.
849 if (namelst && (!infodump && !capdump)) {
850 (void) fprintf(stderr,
851 "%s: Sorry, -e can't be used without -I or -C\n",
853 ExitProgram(EXIT_FAILURE);
855 #endif /* HAVE_BIG_CORE */
858 source_file = argv[optind++];
861 "%s: Too many file names. Usage:\n\t%s %s",
865 ExitProgram(EXIT_FAILURE);
868 if (infodump == TRUE) {
869 /* captoinfo's no-argument case */
870 source_file = "/etc/termcap";
871 if ((termcap = getenv("TERMCAP")) != 0
872 && (namelst = make_namelist(getenv("TERM"))) != 0) {
873 if (access(termcap, F_OK) == 0) {
875 source_file = termcap;
877 if ((tmp_fp = open_tempfile(my_tmpname)) != 0) {
878 source_file = my_tmpname;
879 fprintf(tmp_fp, "%s\n", termcap);
881 tmp_fp = open_input(source_file, (char *) 0);
882 to_remove = source_file;
891 "%s: File name needed. Usage:\n\t%s %s",
895 ExitProgram(EXIT_FAILURE);
900 tmp_fp = open_input(source_file, my_altfile);
901 if (!strcmp(source_file, "-")) {
902 source_file = STDIN_NAME;
911 sortmode, width, height, debug_level, formatted);
912 } else if (capdump) {
915 sortmode, width, height, debug_level, FALSE);
918 /* parse entries out of the source file */
919 _nc_set_source(source_file);
921 if (!(check_only || infodump || capdump))
922 _nc_set_writedir(outdir);
923 #endif /* HAVE_BIG_CORE */
924 _nc_read_entry_source(tmp_fp, (char *) NULL,
925 !smart_defaults || literal, FALSE,
926 ((check_only || infodump || capdump)
930 /* do use resolution */
931 if (check_only || (!infodump && !capdump) || forceresolve) {
932 if (!_nc_resolve_uses2(TRUE, literal) && !check_only) {
933 ExitProgram(EXIT_FAILURE);
938 if (check_only && (capdump || infodump)) {
940 if (matches(namelst, qp->tterm.term_names)) {
941 int len = fmt_entry(&qp->tterm, NULL, FALSE, TRUE, infodump, numbers);
943 if (len > (infodump ? MAX_TERMINFO_LENGTH : MAX_TERMCAP_LENGTH))
944 (void) fprintf(stderr,
945 "warning: resolved %s entry is %d bytes long\n",
946 _nc_first_name(qp->tterm.term_names),
952 /* write or dump all entries */
954 if (!infodump && !capdump) {
955 _nc_set_writedir(outdir);
957 if (matches(namelst, qp->tterm.term_names))
961 /* this is in case infotocap() generates warnings */
962 _nc_curr_col = _nc_curr_line = -1;
965 if (matches(namelst, qp->tterm.term_names)) {
966 long j = qp->cend - qp->cstart;
969 /* this is in case infotocap() generates warnings */
970 _nc_set_type(_nc_first_name(qp->tterm.term_names));
972 (void) fseek(tmp_fp, qp->cstart, SEEK_SET);
975 (void) putchar(fgetc(tmp_fp));
977 put_translate(fgetc(tmp_fp));
980 repair_acsc(&qp->tterm);
981 dump_entry(&qp->tterm, suppress_untranslatable,
982 limited, numbers, NULL);
983 for (j = 0; j < (long) qp->nuses; j++)
984 dump_uses(qp->uses[j].name, !capdump);
986 if (debug_level != 0 && !limited)
987 printf("# length=%d\n", len);
990 if (!namelst && _nc_tail) {
992 bool in_comment = FALSE;
993 bool trailing_comment = FALSE;
995 (void) fseek(tmp_fp, _nc_tail->cend, SEEK_SET);
996 while ((c = fgetc(tmp_fp)) != EOF) {
999 trailing_comment = TRUE;
1005 if (trailing_comment
1006 && (in_comment || (oldc == '\n' && c == '\n')))
1014 /* Show the directory into which entries were written, and the total
1018 && (!(check_only || infodump || capdump))) {
1019 int total = _nc_tic_written();
1021 fprintf(log_fp, "%d entries written to %s\n",
1023 _nc_tic_dir((char *) 0));
1025 fprintf(log_fp, "No entries written\n");
1027 ExitProgram(EXIT_SUCCESS);
1031 * This bit of legerdemain turns all the terminfo variable names into
1032 * references to locations in the arrays Booleans, Numbers, and Strings ---
1033 * precisely what's needed (see comp_parse.c).
1039 * Check if the alternate character-set capabilities are consistent.
1042 check_acs(TERMTYPE *tp)
1044 if (VALID_STRING(acs_chars)) {
1045 const char *boxes = "lmkjtuvwqxn";
1051 memset(mapped, 0, sizeof(mapped));
1052 for (p = acs_chars; *p != '\0'; p += 2) {
1054 _nc_warning("acsc has odd number of characters");
1057 mapped[UChar(p[0])] = p[1];
1060 if (mapped[UChar('I')] && !mapped[UChar('i')]) {
1061 _nc_warning("acsc refers to 'I', which is probably an error");
1064 for (p = boxes, q = missing; *p != '\0'; ++p) {
1065 if (!mapped[UChar(p[0])]) {
1071 assert(strlen(missing) <= strlen(boxes));
1072 if (*missing != '\0' && strcmp(missing, boxes)) {
1073 _nc_warning("acsc is missing some line-drawing mapping: %s", missing);
1079 * Check if the color capabilities are consistent
1082 check_colors(TERMTYPE *tp)
1084 if ((max_colors > 0) != (max_pairs > 0)
1085 || ((max_colors > max_pairs) && (initialize_pair == 0)))
1086 _nc_warning("inconsistent values for max_colors (%d) and max_pairs (%d)",
1087 max_colors, max_pairs);
1089 PAIRED(set_foreground, set_background);
1090 PAIRED(set_a_foreground, set_a_background);
1091 PAIRED(set_color_pair, initialize_pair);
1093 if (VALID_STRING(set_foreground)
1094 && VALID_STRING(set_a_foreground)
1095 && !_nc_capcmp(set_foreground, set_a_foreground))
1096 _nc_warning("expected setf/setaf to be different");
1098 if (VALID_STRING(set_background)
1099 && VALID_STRING(set_a_background)
1100 && !_nc_capcmp(set_background, set_a_background))
1101 _nc_warning("expected setb/setab to be different");
1103 /* see: has_colors() */
1104 if (VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
1105 && (((set_foreground != NULL)
1106 && (set_background != NULL))
1107 || ((set_a_foreground != NULL)
1108 && (set_a_background != NULL))
1109 || set_color_pair)) {
1110 if (!VALID_STRING(orig_pair) && !VALID_STRING(orig_colors))
1111 _nc_warning("expected either op/oc string for resetting colors");
1116 keypad_final(const char *string)
1120 if (VALID_STRING(string)
1121 && *string++ == '\033'
1123 && strlen(string) == 1) {
1131 keypad_index(const char *string)
1134 const char *list = "PQRSwxymtuvlqrsPpn"; /* app-keypad except "Enter" */
1138 if ((ch = keypad_final(string)) != '\0') {
1139 test = strchr(list, ch);
1141 result = (long) (test - list);
1147 * list[] is down, up, left, right
1148 * "left" may be ^H rather than \E[D
1149 * "down" may be ^J rather than \E[B
1150 * But up/right are generally consistently escape sequences for ANSI terminals.
1153 check_ansi_cursor(char *list[4])
1160 bool repeated = FALSE;
1162 for (j = 0; j < 4; ++j) {
1164 for (k = 0; k < j; ++k) {
1166 && !strcmp(list[j], list[k])) {
1167 char *value = _nc_tic_expand(list[k], TRUE, 0);
1168 _nc_warning("repeated cursor control %s\n", value);
1176 if (UChar(up[0]) == '\033') {
1182 } else if (UChar(up[0]) == UChar('\233')) {
1187 while (up[suffix] && isdigit(UChar(up[suffix])))
1190 if (prefix && up[suffix] == 'A') {
1192 if (!strcmp(list[0], "\n"))
1194 if (!strcmp(list[2], "\b"))
1197 for (j = 0; j < 4; ++j) {
1198 if (skip[j] || strlen(list[j]) == 1)
1200 if (memcmp(list[j], up, prefix)) {
1201 char *value = _nc_tic_expand(list[j], TRUE, 0);
1202 _nc_warning("inconsistent prefix for %s\n", value);
1205 if (strlen(list[j]) < suffix) {
1206 char *value = _nc_tic_expand(list[j], TRUE, 0);
1207 _nc_warning("inconsistent length for %s, expected %d\n",
1208 value, (int) suffix + 1);
1212 if (list[j][suffix] != want) {
1213 char *value = _nc_tic_expand(list[j], TRUE, 0);
1214 _nc_warning("inconsistent suffix for %s, expected %c, have %c\n",
1215 value, want, list[j][suffix]);
1222 #define EXPECTED(name) if (!PRESENT(name)) _nc_warning("expected " #name)
1225 check_cursor(TERMTYPE *tp)
1230 /* it is rare to have an insert-line feature without a matching delete */
1231 ANDMISSING(parm_insert_line, insert_line);
1232 ANDMISSING(parm_delete_line, delete_line);
1233 ANDMISSING(parm_insert_line, parm_delete_line);
1235 /* if we have a parameterized form, then the non-parameterized is easy */
1236 ANDMISSING(parm_down_cursor, cursor_down);
1237 ANDMISSING(parm_up_cursor, cursor_up);
1238 ANDMISSING(parm_left_cursor, cursor_left);
1239 ANDMISSING(parm_right_cursor, cursor_right);
1241 /* Given any of a set of cursor movement, the whole set should be present.
1242 * Technically this is not true (we could use cursor_address to fill in
1243 * unsupported controls), but it is likely.
1246 if (PRESENT(parm_down_cursor)) {
1247 list[count++] = parm_down_cursor;
1249 if (PRESENT(parm_up_cursor)) {
1250 list[count++] = parm_up_cursor;
1252 if (PRESENT(parm_left_cursor)) {
1253 list[count++] = parm_left_cursor;
1255 if (PRESENT(parm_right_cursor)) {
1256 list[count++] = parm_right_cursor;
1259 check_ansi_cursor(list);
1260 } else if (count != 0) {
1261 EXPECTED(parm_down_cursor);
1262 EXPECTED(parm_up_cursor);
1263 EXPECTED(parm_left_cursor);
1264 EXPECTED(parm_right_cursor);
1268 if (PRESENT(cursor_down)) {
1269 list[count++] = cursor_down;
1271 if (PRESENT(cursor_up)) {
1272 list[count++] = cursor_up;
1274 if (PRESENT(cursor_left)) {
1275 list[count++] = cursor_left;
1277 if (PRESENT(cursor_right)) {
1278 list[count++] = cursor_right;
1281 check_ansi_cursor(list);
1282 } else if (count != 0) {
1284 if (PRESENT(cursor_down) && strcmp(cursor_down, "\n"))
1286 if (PRESENT(cursor_left) && strcmp(cursor_left, "\b"))
1288 if (PRESENT(cursor_up) && strlen(cursor_up) > 1)
1290 if (PRESENT(cursor_right) && strlen(cursor_right) > 1)
1293 EXPECTED(cursor_down);
1294 EXPECTED(cursor_up);
1295 EXPECTED(cursor_left);
1296 EXPECTED(cursor_right);
1303 * Do a quick sanity-check for vt100-style keypads to see if the 5-key keypad
1304 * is mapped inconsistently.
1307 check_keypad(TERMTYPE *tp)
1311 if (VALID_STRING(key_a1) &&
1312 VALID_STRING(key_a3) &&
1313 VALID_STRING(key_b2) &&
1314 VALID_STRING(key_c1) &&
1315 VALID_STRING(key_c3)) {
1316 char final[MAX_KP + 1];
1323 final[0] = keypad_final(key_a1);
1324 final[1] = keypad_final(key_a3);
1325 final[2] = keypad_final(key_b2);
1326 final[3] = keypad_final(key_c1);
1327 final[4] = keypad_final(key_c3);
1330 /* special case: legacy coding using 1,2,3,0,. on the bottom */
1331 assert(strlen(final) <= MAX_KP);
1332 if (!strcmp(final, "qsrpn"))
1335 list[0] = keypad_index(key_a1);
1336 list[1] = keypad_index(key_a3);
1337 list[2] = keypad_index(key_b2);
1338 list[3] = keypad_index(key_c1);
1339 list[4] = keypad_index(key_c3);
1341 /* check that they're all vt100 keys */
1342 for (j = 0; j < MAX_KP; ++j) {
1348 /* check if they're all in increasing order */
1349 for (j = 1; j < MAX_KP; ++j) {
1350 if (list[j] > list[j - 1]) {
1354 if (increase != (MAX_KP - 1)) {
1357 for (j = 0, last = -1; j < MAX_KP; ++j) {
1358 for (k = 0, kk = -1, test = 100; k < 5; ++k) {
1359 if (list[k] > last &&
1366 assert(strlen(show) < (MAX_KP * 4));
1369 _nc_STRCAT(show, " ka1", sizeof(show));
1372 _nc_STRCAT(show, " ka3", sizeof(show));
1375 _nc_STRCAT(show, " kb2", sizeof(show));
1378 _nc_STRCAT(show, " kc1", sizeof(show));
1381 _nc_STRCAT(show, " kc3", sizeof(show));
1386 _nc_warning("vt100 keypad order inconsistent: %s", show);
1389 } else if (VALID_STRING(key_a1) ||
1390 VALID_STRING(key_a3) ||
1391 VALID_STRING(key_b2) ||
1392 VALID_STRING(key_c1) ||
1393 VALID_STRING(key_c3)) {
1395 if (keypad_index(key_a1) >= 0)
1396 _nc_STRCAT(show, " ka1", sizeof(show));
1397 if (keypad_index(key_a3) >= 0)
1398 _nc_STRCAT(show, " ka3", sizeof(show));
1399 if (keypad_index(key_b2) >= 0)
1400 _nc_STRCAT(show, " kb2", sizeof(show));
1401 if (keypad_index(key_c1) >= 0)
1402 _nc_STRCAT(show, " kc1", sizeof(show));
1403 if (keypad_index(key_c3) >= 0)
1404 _nc_STRCAT(show, " kc3", sizeof(show));
1406 _nc_warning("vt100 keypad map incomplete:%s", show);
1410 * These warnings are useful for consistency checks - it is possible that
1411 * there are real terminals with mismatches in these
1413 ANDMISSING(key_ic, key_dc);
1417 check_printer(TERMTYPE *tp)
1419 PAIRED(enter_doublewide_mode, exit_doublewide_mode);
1420 PAIRED(enter_italics_mode, exit_italics_mode);
1421 PAIRED(enter_leftward_mode, exit_leftward_mode);
1422 PAIRED(enter_micro_mode, exit_micro_mode);
1423 PAIRED(enter_shadow_mode, exit_shadow_mode);
1424 PAIRED(enter_subscript_mode, exit_subscript_mode);
1425 PAIRED(enter_superscript_mode, exit_superscript_mode);
1426 PAIRED(enter_upward_mode, exit_upward_mode);
1428 ANDMISSING(start_char_set_def, stop_char_set_def);
1430 /* if we have a parameterized form, then the non-parameterized is easy */
1431 ANDMISSING(set_bottom_margin_parm, set_bottom_margin);
1432 ANDMISSING(set_left_margin_parm, set_left_margin);
1433 ANDMISSING(set_right_margin_parm, set_right_margin);
1434 ANDMISSING(set_top_margin_parm, set_top_margin);
1436 ANDMISSING(parm_down_micro, micro_down);
1437 ANDMISSING(parm_left_micro, micro_left);
1438 ANDMISSING(parm_right_micro, micro_right);
1439 ANDMISSING(parm_up_micro, micro_up);
1443 uses_SGR_39_49(const char *value)
1445 return (strstr(value, "39;49") != 0
1446 || strstr(value, "49;39") != 0);
1450 * Check consistency of termcap extensions related to "screen".
1453 check_screen(TERMTYPE *tp)
1455 if (_nc_user_definable) {
1456 int have_XT = tigetflag("XT");
1457 int have_XM = tigetflag("XM");
1458 int have_bce = back_color_erase;
1459 bool have_kmouse = FALSE;
1460 bool use_sgr_39_49 = FALSE;
1461 char *name = _nc_first_name(tp->term_names);
1463 if (!VALID_BOOLEAN(have_bce)) {
1466 if (!VALID_BOOLEAN(have_XM)) {
1469 if (!VALID_BOOLEAN(have_XT)) {
1472 if (VALID_STRING(key_mouse)) {
1473 have_kmouse = !strcmp("\033[M", key_mouse);
1475 if (VALID_STRING(orig_colors)) {
1476 use_sgr_39_49 = uses_SGR_39_49(orig_colors);
1477 } else if (VALID_STRING(orig_pair)) {
1478 use_sgr_39_49 = uses_SGR_39_49(orig_pair);
1481 if (have_XM && have_XT) {
1482 _nc_warning("Screen's XT capability conflicts with XM");
1484 && strstr(name, "screen") != 0
1485 && strchr(name, '.') != 0) {
1486 _nc_warning("Screen's \"screen\" entries should not have XT set");
1487 } else if (have_XT) {
1488 if (!have_kmouse && have_bce) {
1489 if (VALID_STRING(key_mouse)) {
1490 _nc_warning("Value of kmous inconsistent with screen's usage");
1492 _nc_warning("Expected kmous capability with XT");
1495 if (!have_bce && max_colors > 0)
1496 _nc_warning("Expected bce capability with XT");
1497 if (!use_sgr_39_49 && have_bce && max_colors > 0)
1498 _nc_warning("Expected orig_colors capability with XT to have 39/49 parameters");
1499 if (VALID_STRING(to_status_line))
1500 _nc_warning("\"tsl\" capability is redundant, given XT");
1502 if (have_kmouse && !have_XM)
1503 _nc_warning("Expected XT to be set, given kmous");
1509 * Returns the expected number of parameters for the given capability.
1512 expected_params(const char *name)
1515 static const struct {
1519 { "S0", 1 }, /* 'screen' extension */
1572 { "smgbp", 1 }, /* 2 if smgtp is not given */
1587 int result = 0; /* function-keys, etc., use none */
1589 for (n = 0; n < SIZEOF(table); n++) {
1590 if (!strcmp(name, table[n].name)) {
1591 result = table[n].count;
1600 * Make a quick sanity check for the parameters which are used in the given
1601 * strings. If there are no "%p" tokens, then there should be no other "%"
1605 check_params(TERMTYPE *tp, const char *name, char *value)
1607 int expected = expected_params(name);
1613 #ifdef set_top_margin_parm
1614 if (!strcmp(name, "smgbp")
1615 && set_top_margin_parm == 0)
1619 for (n = 0; n < 10; n++)
1625 _nc_warning("expected character after %% in %s", name);
1627 } else if (*s == 'p') {
1628 if (*++s == '\0' || !isdigit((int) *s)) {
1629 _nc_warning("expected digit after %%p in %s", name);
1643 _nc_warning("%s refers to parameter 0 (%%p0), which is not allowed", name);
1645 if (value == set_attributes || expected < 0) {
1647 } else if (expected != actual) {
1648 _nc_warning("%s uses %d parameters, expected %d", name,
1650 for (n = 1; n < actual; n++) {
1652 _nc_warning("%s omits parameter %d", name, n);
1660 while (*s == '/' || isdigit(UChar(*s)))
1666 * Skip a delay altogether, e.g., when comparing a simple string to sgr,
1667 * the latter may have a worst-case delay on the end.
1670 ignore_delays(char *s)
1703 * An sgr string may contain several settings other than the one we're
1704 * interested in, essentially sgr0 + rmacs + whatever. As long as the
1705 * "whatever" is contained in the sgr string, that is close enough for our
1709 similar_sgr(int num, char *a, char *b)
1711 static const char *names[] =
1733 _nc_warning("Did not find delay %s", _nc_visbuf(b));
1735 _nc_warning("checking sgr(%s) %s\n\tcompare to %s\n\tunmatched %s",
1736 names[num], _nc_visbuf2(1, base_a),
1737 _nc_visbuf2(2, base_b),
1741 } else if (delaying) {
1744 } else if ((*b == '0' || (*b == ';')) && *a == 'm') {
1766 /* ignore delays on the end of the string */
1767 a = ignore_delays(a);
1768 return ((num != 0) || (*a == 0));
1772 check_sgr(TERMTYPE *tp, char *zero, int num, char *cap, const char *name)
1777 test = TPARM_9(set_attributes,
1789 if (!similar_sgr(num, test, cap)) {
1790 _nc_warning("%s differs from sgr(%d)\n\t%s=%s\n\tsgr(%d)=%s",
1792 name, _nc_visbuf2(1, cap),
1793 num, _nc_visbuf2(2, test));
1795 } else if (_nc_capcmp(test, zero)) {
1796 _nc_warning("sgr(%d) present, but not %s", num, name);
1798 } else if (PRESENT(cap)) {
1799 _nc_warning("sgr(%d) missing, but %s present", num, name);
1802 _nc_warning("stack error in sgr(%d) string", num);
1806 #define CHECK_SGR(num,name) check_sgr(tp, zero, num, name, #name)
1810 * If tic is compiled with TRACE, we'll be able to see the output from the
1811 * DEBUG() macro. But since it doesn't use traceon(), it always goes to
1812 * the standard error. Use this function to make it simpler to follow the
1813 * resulting debug traces.
1816 show_where(unsigned level)
1818 if (_nc_tracing >= DEBUG_LEVEL(level)) {
1819 char my_name[MAX_NAME_SIZE];
1820 _nc_get_type(my_name);
1821 _tracef("\"%s\", line %d, '%s'",
1823 _nc_curr_line, my_name);
1828 #define show_where(level) /* nothing */
1838 get_fkey_list(TERMTYPE *tp)
1840 NAME_VALUE *result = typeMalloc(NAME_VALUE, NUM_STRINGS(tp) + 1);
1841 const struct tinfo_fkeys *all_fkeys = _nc_tinfo_fkeys;
1846 failed("get_fkey_list");
1848 for (j = 0; all_fkeys[j].code; j++) {
1849 char *a = tp->Strings[all_fkeys[j].offset];
1850 if (VALID_STRING(a)) {
1851 result[used].keycode = (int) all_fkeys[j].code;
1852 result[used].name = strnames[all_fkeys[j].offset];
1853 result[used].value = a;
1858 for (j = STRCOUNT; j < NUM_STRINGS(tp); ++j) {
1859 const char *name = ExtStrname(tp, j, strnames);
1861 result[used].keycode = -1;
1862 result[used].name = name;
1863 result[used].value = tp->Strings[j];
1868 result[used].keycode = 0;
1873 show_fkey_name(NAME_VALUE * data)
1875 if (data->keycode > 0) {
1876 fprintf(stderr, " %s", keyname(data->keycode));
1877 fprintf(stderr, " (capability \"%s\")", data->name);
1879 fprintf(stderr, " capability \"%s\"", data->name);
1883 /* other sanity-checks (things that we don't want in the normal
1884 * logic that reads a terminfo entry)
1887 check_termtype(TERMTYPE *tp, bool literal)
1889 bool conflict = FALSE;
1893 * A terminal entry may contain more than one keycode assigned to
1894 * a given string (e.g., KEY_END and KEY_LL). But curses will only
1895 * return one (the last one assigned).
1897 if (!(_nc_syntax == SYN_TERMCAP && capdump)) {
1898 char *check = calloc((size_t) (NUM_STRINGS(tp) + 1), sizeof(char));
1899 NAME_VALUE *given = get_fkey_list(tp);
1902 failed("check_termtype");
1904 for (j = 0; given[j].keycode; ++j) {
1905 const char *a = given[j].value;
1908 for (k = j + 1; given[k].keycode; k++) {
1909 const char *b = given[k].value;
1912 if (!_nc_capcmp(a, b)) {
1917 _nc_warning("Conflicting key definitions (using the last)");
1920 fprintf(stderr, "...");
1921 show_fkey_name(given + j);
1922 fprintf(stderr, " is the same as");
1923 show_fkey_name(given + k);
1926 fprintf(stderr, ", ");
1927 show_fkey_name(given + k);
1932 fprintf(stderr, "\n");
1938 for_each_string(j, tp) {
1939 char *a = tp->Strings[j];
1940 if (VALID_STRING(a))
1941 check_params(tp, ExtStrname(tp, (int) j, strnames), a);
1952 * These may be mismatched because the terminal description relies on
1953 * restoring the cursor visibility by resetting it.
1955 ANDMISSING(cursor_invisible, cursor_normal);
1956 ANDMISSING(cursor_visible, cursor_normal);
1958 if (PRESENT(cursor_visible) && PRESENT(cursor_normal)
1959 && !_nc_capcmp(cursor_visible, cursor_normal))
1960 _nc_warning("cursor_visible is same as cursor_normal");
1963 * From XSI & O'Reilly, we gather that sc/rc are required if csr is
1964 * given, because the cursor position after the scrolling operation is
1965 * performed is undefined.
1967 ANDMISSING(change_scroll_region, save_cursor);
1968 ANDMISSING(change_scroll_region, restore_cursor);
1971 * If we can clear tabs, we should be able to initialize them.
1973 ANDMISSING(clear_all_tabs, set_tab);
1975 if (PRESENT(set_attributes)) {
1979 if (PRESENT(exit_attribute_mode)) {
1980 zero = strdup(CHECK_SGR(0, exit_attribute_mode));
1982 zero = strdup(TPARM_9(set_attributes, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1985 _nc_warning("stack error in sgr(0) string");
1988 CHECK_SGR(1, enter_standout_mode);
1989 CHECK_SGR(2, enter_underline_mode);
1990 CHECK_SGR(3, enter_reverse_mode);
1991 CHECK_SGR(4, enter_blink_mode);
1992 CHECK_SGR(5, enter_dim_mode);
1993 CHECK_SGR(6, enter_bold_mode);
1994 CHECK_SGR(7, enter_secure_mode);
1995 CHECK_SGR(8, enter_protected_mode);
1996 CHECK_SGR(9, enter_alt_charset_mode);
1999 _nc_warning("sgr(0) did not return a value");
2001 } else if (PRESENT(exit_attribute_mode) &&
2002 set_attributes != CANCELLED_STRING) {
2003 if (_nc_syntax == SYN_TERMINFO)
2004 _nc_warning("missing sgr string");
2007 if (PRESENT(exit_attribute_mode)) {
2008 char *check_sgr0 = _nc_trim_sgr0(tp);
2010 if (check_sgr0 == 0 || *check_sgr0 == '\0') {
2011 _nc_warning("trimmed sgr0 is empty");
2014 if (check_sgr0 != exit_attribute_mode) {
2016 ("will trim sgr0\n\toriginal sgr0=%s\n\ttrimmed sgr0=%s",
2017 _nc_visbuf2(1, exit_attribute_mode),
2018 _nc_visbuf2(2, check_sgr0)));
2022 ("will not trim sgr0\n\toriginal sgr0=%s",
2023 _nc_visbuf(exit_attribute_mode)));
2029 if (!auto_right_margin) {
2031 ("can write to lower-right directly"));
2032 } else if (PRESENT(enter_am_mode) && PRESENT(exit_am_mode)) {
2034 ("can write to lower-right by suppressing automargin"));
2035 } else if ((PRESENT(enter_insert_mode) && PRESENT(exit_insert_mode))
2036 || PRESENT(insert_character) || PRESENT(parm_ich)) {
2038 ("can write to lower-right by using inserts"));
2041 ("cannot write to lower-right"));
2046 * Some standard applications (e.g., vi) and some non-curses
2047 * applications (e.g., jove) get confused if we have both ich1 and
2048 * smir/rmir. Let's be nice and warn about that, too, even though
2049 * ncurses handles it.
2051 if ((PRESENT(enter_insert_mode) || PRESENT(exit_insert_mode))
2052 && PRESENT(parm_ich)) {
2053 _nc_warning("non-curses applications may be confused by ich1 with smir/rmir");
2057 * Finally, do the non-verbose checks
2059 if (save_check_termtype != 0)
2060 save_check_termtype(tp, literal);