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.177 2012/06/02 17:19:31 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)
112 free_namelist(namelst);
116 if (to_remove != 0) {
126 failed(const char *msg)
129 ExitProgram(EXIT_FAILURE);
135 static const char *const tbl[] =
138 " -1 format translation output one capability per line",
140 " -a retain commented-out capabilities (sets -x also)",
142 " -K translate entries to termcap source form with BSD syntax",
143 " -C translate entries to termcap source form",
144 " -D print list of tic's database locations (first must be writable)",
145 " -c check only, validate input without compiling or translating",
146 " -e<names> translate/compile only entries named by comma-separated list",
147 " -f format complex strings for readability",
148 " -G format %{number} to %'char'",
149 " -g format %'char' to %{number}",
150 " -I translate entries to terminfo source form",
151 " -L translate entries to full terminfo source form",
152 " -N disable smart defaults for source translation",
153 " -o<dir> set output directory for compiled entry writes",
154 " -R<name> restrict translation to given terminfo/termcap version",
155 " -r force resolution of all use entries in source translation",
156 " -s print summary statistics",
157 " -T remove size-restrictions on compiled description",
159 " -t suppress commented-out capabilities",
161 " -U suppress post-processing of entries",
163 " -v[n] set verbosity level",
164 " -w[n] set format width for translation output",
166 " -x treat unknown capabilities as user-defined",
170 " <file> file to translate or compile"
174 fprintf(stderr, "Usage: %s %s\n", _nc_progname, usage_string);
175 for (j = 0; j < SIZEOF(tbl); j++) {
176 fputs(tbl[j], stderr);
179 ExitProgram(EXIT_FAILURE);
192 char result[MAX_ENTRY_SIZE];
195 * Look for strings that contain %{number}, convert them to %'char',
196 * which is shorter and runs a little faster.
198 for (n = 0; n < STRCOUNT; n++) {
199 s = ep->tterm.Strings[n];
201 && strchr(s, L_BRACE) != 0) {
204 while ((ch = *t++) != 0) {
208 } else if ((ch == '%')
209 && (*t == L_BRACE)) {
211 long value = strtol(t + 1, &v, 0);
215 && value != '\\' /* FIXME */
217 && isprint((int) value)) {
226 if (strlen(result) < strlen(s))
227 _nc_STRCPY(s, result, strlen(s) + 1);
231 _nc_set_type(_nc_first_name(ep->tterm.term_names));
232 _nc_curr_line = (int) ep->startline;
233 _nc_write_entry(&ep->tterm);
237 immedhook(ENTRY * ep GCC_UNUSED)
238 /* write out entries with no use capabilities immediately to save storage */
242 * This is strictly a core-economy kluge. The really clean way to handle
243 * compilation is to slurp the whole file into core and then do all the
244 * name-collision checks and entry writes in one swell foop. But the
245 * terminfo master file is large enough that some core-poor systems swap
246 * like crazy when you compile it this way...there have been reports of
247 * this process taking *three hours*, rather than the twenty seconds or
248 * less typical on my development box.
250 * So. This hook *immediately* writes out the referenced entry if it
251 * has no use capabilities. The compiler main loop refrains from
252 * adding the entry to the in-core list when this hook fires. If some
253 * other entry later needs to reference an entry that got written
254 * immediately, that's OK; the resolution code will fetch it off disk
255 * when it can't find it in core.
257 * Name collisions will still be detected, just not as cleanly. The
258 * write_entry() code complains before overwriting an entry that
259 * postdates the time of tic's first call to write_entry(). Thus
260 * it will complain about overwriting entries newly made during the
261 * tic run, but not about overwriting ones that predate it.
263 * The reason this is a hook, and not in line with the rest of the
264 * compiler code, is that the support for termcap fallback cannot assume
265 * it has anywhere to spool out these entries!
267 * The _nc_set_type() call here requires a compensating one in
270 * If you define HAVE_BIG_CORE, you'll disable this kluge. This will
271 * make tic a bit faster (because the resolution code won't have to do
272 * disk I/O nearly as often).
274 if (ep->nuses == 0) {
275 int oldline = _nc_curr_line;
278 _nc_curr_line = oldline;
279 free(ep->tterm.str_table);
282 #endif /* HAVE_BIG_CORE */
288 /* emit a comment char, translating terminfo names to termcap names */
290 static bool in_name = FALSE;
291 static size_t have, used;
292 static char *namebuf, *suffix;
295 if (used + 1 >= have) {
297 namebuf = typeRealloc(char, have, namebuf);
298 suffix = typeRealloc(char, have, suffix);
300 if (c == '\n' || c == '@') {
301 namebuf[used++] = '\0';
303 (void) fputs(namebuf, stdout);
306 } else if (c != '>') {
307 namebuf[used++] = (char) c;
308 } else { /* ah! candidate name! */
310 NCURSES_CONST char *tp;
312 namebuf[used++] = '\0';
316 if ((up = strchr(namebuf, '#')) != 0
317 || (up = strchr(namebuf, '=')) != 0
318 || ((up = strchr(namebuf, '@')) != 0 && up[1] == '>')) {
319 _nc_STRCPY(suffix, up, have);
323 if ((tp = nametrans(namebuf)) != 0) {
325 (void) fputs(tp, stdout);
326 (void) fputs(suffix, stdout);
329 /* couldn't find a translation, just dump the name */
331 (void) fputs(namebuf, stdout);
332 (void) fputs(suffix, stdout);
346 /* Returns a string, stripped of leading/trailing whitespace */
352 while (isspace(UChar(*src)))
358 if ((dst = strdup(src)) == NULL) {
362 while (--len != 0 && isspace(UChar(dst[len])))
370 open_tempfile(char *filename)
374 _nc_STRCPY(filename, "/tmp/XXXXXX", PATH_MAX);
377 int fd = mkstemp(filename);
379 result = fdopen(fd, "w");
382 if (tmpnam(filename) != 0)
383 result = fopen(filename, "w");
389 copy_input(FILE *source, const char *filename, char *alt_file)
392 FILE *target = open_tempfile(alt_file);
396 failed("copy_input (source)");
397 } else if (target == 0) {
398 failed("copy_input (target)");
405 } else if (ferror(source)) {
407 } else if (ch == 0) {
408 /* don't loop in case someone wants to convert /dev/zero */
409 fprintf(stderr, "%s: %s is not a text-file\n", _nc_progname, filename);
410 ExitProgram(EXIT_FAILURE);
416 * rewind() does not force the target file's data to disk (not does
417 * fflush()...). So open a second stream on the data and then close
418 * the one that we were writing on before starting to read from the
421 result = fopen(alt_file, "r+");
423 to_remove = alt_file;
429 open_input(const char *filename, char *alt_file)
435 if (!strcmp(filename, "-")) {
436 fp = copy_input(stdin, STDIN_NAME, alt_file);
437 } else if (stat(filename, &sb) < 0) {
438 fprintf(stderr, "%s: %s %s\n", _nc_progname, filename, strerror(errno));
439 ExitProgram(EXIT_FAILURE);
440 } else if ((mode = (sb.st_mode & S_IFMT)) == S_IFDIR
441 || (mode != S_IFREG && mode != S_IFCHR)) {
442 fprintf(stderr, "%s: %s is not a file\n", _nc_progname, filename);
443 ExitProgram(EXIT_FAILURE);
445 fp = fopen(filename, "r");
448 fprintf(stderr, "%s: Can't open %s\n", _nc_progname, filename);
449 ExitProgram(EXIT_FAILURE);
451 if (mode != S_IFREG) {
453 FILE *fp2 = copy_input(fp, filename, alt_file);
456 fprintf(stderr, "%s: %s is not a file\n", _nc_progname, filename);
457 ExitProgram(EXIT_FAILURE);
464 /* Parse the "-e" option-value into a list of names */
466 make_namelist(char *src)
471 unsigned pass, n, nn;
476 } else if (strchr(src, '/') != 0) { /* a filename */
477 FILE *fp = open_input(src, (char *) 0);
479 for (pass = 1; pass <= 2; pass++) {
481 while (fgets(buffer, sizeof(buffer), fp) != 0) {
482 if ((s = stripped(buffer)) != 0) {
491 dst = typeCalloc(char *, nn + 1);
496 } else { /* literal list of names */
497 for (pass = 1; pass <= 2; pass++) {
498 for (n = nn = 0, base = src;; n++) {
500 if (mark == ',' || mark == '\0') {
505 if ((s = stripped(base)) != 0)
514 dst = typeCalloc(char *, nn + 1);
517 if (showsummary && (dst != 0)) {
518 fprintf(log_fp, "Entries that will be compiled:\n");
519 for (n = 0; dst[n] != 0; n++)
520 fprintf(log_fp, "%u:%s\n", n + 1, dst[n]);
526 matches(char **needle, const char *haystack)
527 /* does entry in needle list match |-separated field in haystack? */
533 for (n = 0; needle[n] != 0; n++) {
534 if (_nc_name_match(haystack, needle[n], "|")) {
545 valid_db_path(const char *nominal)
549 char suffix[] = DBM_SUFFIX;
550 size_t need = strlen(nominal) + sizeof(suffix);
551 char *result = malloc(need);
553 _nc_STRCPY(result, nominal, need);
554 if (strcmp(result + need - sizeof(suffix), suffix)) {
555 _nc_STRCAT(result, suffix, need);
558 char *result = strdup(nominal);
561 DEBUG(1, ("** stat(%s)", result));
562 if (stat(result, &sb) >= 0) {
564 if (!S_ISREG(sb.st_mode)
565 || access(result, R_OK | W_OK) != 0) {
566 DEBUG(1, ("...not a writable file"));
571 if (!S_ISDIR(sb.st_mode)
572 || access(result, R_OK | W_OK | X_OK) != 0) {
573 DEBUG(1, ("...not a writable directory"));
579 /* check if parent is directory and is writable */
580 unsigned leaf = _nc_pathlast(result);
582 DEBUG(1, ("...not found"));
584 char save = result[leaf];
586 if (stat(result, &sb) >= 0
587 && S_ISDIR(sb.st_mode)
588 && access(result, R_OK | W_OK | X_OK) == 0) {
591 DEBUG(1, ("...parent directory %s is not writable", result));
596 DEBUG(1, ("... no parent directory"));
605 * Show the databases to which tic could write. The location to which it
606 * writes is always the first one. If none are writable, print an error
610 show_databases(const char *outdir)
612 bool specific = (outdir != 0) || getenv("TERMINFO") != 0;
614 const char *tried = 0;
617 outdir = _nc_tic_dir(0);
619 if ((result = valid_db_path(outdir)) != 0) {
620 printf("%s\n", result);
625 if ((outdir = _nc_home_terminfo())) {
626 if ((result = valid_db_path(outdir)) != 0) {
627 printf("%s\n", result);
628 } else if (!specific) {
634 * If we can write in neither location, give an error message.
638 fprintf(stderr, "%s: %s (no permission)\n", _nc_progname, tried);
639 ExitProgram(EXIT_FAILURE);
643 #define VtoTrace(opt) (unsigned) ((opt > 0) ? opt : (opt == 0))
646 main(int argc, char *argv[])
648 char my_tmpname[PATH_MAX];
649 char my_altfile[PATH_MAX];
651 unsigned debug_level;
652 int smart_defaults = TRUE;
656 int this_opt, last_opt = '?';
658 int outform = F_TERMINFO; /* output format */
659 int sortmode = S_TERMINFO; /* sort_mode */
663 bool formatted = FALSE; /* reformat complex strings? */
664 bool literal = FALSE; /* suppress post-processing? */
665 int numbers = 0; /* format "%'char'" to/from "%{number}" */
666 bool forceresolve = FALSE; /* force resolution */
668 char *tversion = (char *) NULL;
669 const char *source_file = "terminfo";
670 char *outdir = (char *) NULL;
671 bool check_only = FALSE;
672 bool suppress_untranslatable = FALSE;
676 _nc_progname = _nc_rootname(argv[0]);
679 if ((infodump = same_program(_nc_progname, PROG_CAPTOINFO)) != FALSE) {
680 outform = F_TERMINFO;
681 sortmode = S_TERMINFO;
683 if ((capdump = same_program(_nc_progname, PROG_INFOTOCAP)) != FALSE) {
685 sortmode = S_TERMCAP;
688 use_extended_names(FALSE);
693 * Processing arguments is a little complicated, since someone made a
694 * design decision to allow the numeric values for -w, -v options to
697 while ((this_opt = getopt(argc, argv,
698 "0123456789CDIKLNR:TUVace:fGgo:rstvwx")) != -1) {
699 if (isdigit(this_opt)) {
702 v_opt = (v_opt * 10) + (this_opt - '0');
705 width = (width * 10) + (this_opt - '0');
727 /* the initial version of -K in 20110730 fell-thru here, but the
728 * same flag is useful when reading sources -TD
734 sortmode = S_TERMCAP;
737 debug_level = VtoTrace(v_opt);
738 set_trace_level(debug_level);
739 show_databases(outdir);
740 ExitProgram(EXIT_SUCCESS);
744 outform = F_TERMINFO;
745 sortmode = S_TERMINFO;
749 outform = F_VARIABLE;
750 sortmode = S_VARIABLE;
753 smart_defaults = FALSE;
766 puts(curses_version());
767 ExitProgram(EXIT_SUCCESS);
772 namelst = make_namelist(optarg);
800 _nc_disable_period = FALSE;
801 suppress_untranslatable = TRUE;
804 _nc_disable_period = TRUE;
807 use_extended_names(TRUE);
816 debug_level = VtoTrace(v_opt);
817 set_trace_level(debug_level);
820 save_check_termtype = _nc_check_termtype2;
821 _nc_check_termtype2 = check_termtype;
825 * Aaargh! immedhook seriously hoses us!
827 * One problem with immedhook is it means we can't do -e. Problem
828 * is that we can't guarantee that for each terminal listed, all the
829 * terminals it depends on will have been kept in core for reference
830 * resolution -- in fact it's certain the primitive types at the end
831 * of reference chains *won't* be in core unless they were explicitly
832 * in the select list themselves.
834 if (namelst && (!infodump && !capdump)) {
835 (void) fprintf(stderr,
836 "%s: Sorry, -e can't be used without -I or -C\n",
838 ExitProgram(EXIT_FAILURE);
840 #endif /* HAVE_BIG_CORE */
843 source_file = argv[optind++];
846 "%s: Too many file names. Usage:\n\t%s %s",
850 ExitProgram(EXIT_FAILURE);
853 if (infodump == TRUE) {
854 /* captoinfo's no-argument case */
855 source_file = "/etc/termcap";
856 if ((termcap = getenv("TERMCAP")) != 0
857 && (namelst = make_namelist(getenv("TERM"))) != 0) {
858 if (access(termcap, F_OK) == 0) {
860 source_file = termcap;
862 if ((tmp_fp = open_tempfile(my_tmpname)) != 0) {
863 source_file = my_tmpname;
864 fprintf(tmp_fp, "%s\n", termcap);
866 tmp_fp = open_input(source_file, (char *) 0);
867 to_remove = source_file;
876 "%s: File name needed. Usage:\n\t%s %s",
880 ExitProgram(EXIT_FAILURE);
885 tmp_fp = open_input(source_file, my_altfile);
886 if (!strcmp(source_file, "-")) {
887 source_file = STDIN_NAME;
896 sortmode, width, height, debug_level, formatted);
897 } else if (capdump) {
900 sortmode, width, height, debug_level, FALSE);
903 /* parse entries out of the source file */
904 _nc_set_source(source_file);
906 if (!(check_only || infodump || capdump))
907 _nc_set_writedir(outdir);
908 #endif /* HAVE_BIG_CORE */
909 _nc_read_entry_source(tmp_fp, (char *) NULL,
910 !smart_defaults || literal, FALSE,
911 ((check_only || infodump || capdump)
915 /* do use resolution */
916 if (check_only || (!infodump && !capdump) || forceresolve) {
917 if (!_nc_resolve_uses2(TRUE, literal) && !check_only) {
918 ExitProgram(EXIT_FAILURE);
923 if (check_only && (capdump || infodump)) {
925 if (matches(namelst, qp->tterm.term_names)) {
926 int len = fmt_entry(&qp->tterm, NULL, FALSE, TRUE, infodump, numbers);
928 if (len > (infodump ? MAX_TERMINFO_LENGTH : MAX_TERMCAP_LENGTH))
929 (void) fprintf(stderr,
930 "warning: resolved %s entry is %d bytes long\n",
931 _nc_first_name(qp->tterm.term_names),
937 /* write or dump all entries */
939 if (!infodump && !capdump) {
940 _nc_set_writedir(outdir);
942 if (matches(namelst, qp->tterm.term_names))
946 /* this is in case infotocap() generates warnings */
947 _nc_curr_col = _nc_curr_line = -1;
950 if (matches(namelst, qp->tterm.term_names)) {
951 long j = qp->cend - qp->cstart;
954 /* this is in case infotocap() generates warnings */
955 _nc_set_type(_nc_first_name(qp->tterm.term_names));
957 (void) fseek(tmp_fp, qp->cstart, SEEK_SET);
960 (void) putchar(fgetc(tmp_fp));
962 put_translate(fgetc(tmp_fp));
965 repair_acsc(&qp->tterm);
966 dump_entry(&qp->tterm, suppress_untranslatable,
967 limited, numbers, NULL);
968 for (j = 0; j < (long) qp->nuses; j++)
969 dump_uses(qp->uses[j].name, !capdump);
971 if (debug_level != 0 && !limited)
972 printf("# length=%d\n", len);
975 if (!namelst && _nc_tail) {
977 bool in_comment = FALSE;
978 bool trailing_comment = FALSE;
980 (void) fseek(tmp_fp, _nc_tail->cend, SEEK_SET);
981 while ((c = fgetc(tmp_fp)) != EOF) {
984 trailing_comment = TRUE;
991 && (in_comment || (oldc == '\n' && c == '\n')))
999 /* Show the directory into which entries were written, and the total
1003 && (!(check_only || infodump || capdump))) {
1004 int total = _nc_tic_written();
1006 fprintf(log_fp, "%d entries written to %s\n",
1008 _nc_tic_dir((char *) 0));
1010 fprintf(log_fp, "No entries written\n");
1012 ExitProgram(EXIT_SUCCESS);
1016 * This bit of legerdemain turns all the terminfo variable names into
1017 * references to locations in the arrays Booleans, Numbers, and Strings ---
1018 * precisely what's needed (see comp_parse.c).
1024 * Check if the alternate character-set capabilities are consistent.
1027 check_acs(TERMTYPE *tp)
1029 if (VALID_STRING(acs_chars)) {
1030 const char *boxes = "lmkjtuvwqxn";
1036 memset(mapped, 0, sizeof(mapped));
1037 for (p = acs_chars; *p != '\0'; p += 2) {
1039 _nc_warning("acsc has odd number of characters");
1042 mapped[UChar(p[0])] = p[1];
1045 if (mapped[UChar('I')] && !mapped[UChar('i')]) {
1046 _nc_warning("acsc refers to 'I', which is probably an error");
1049 for (p = boxes, q = missing; *p != '\0'; ++p) {
1050 if (!mapped[UChar(p[0])]) {
1056 assert(strlen(missing) <= strlen(boxes));
1057 if (*missing != '\0' && strcmp(missing, boxes)) {
1058 _nc_warning("acsc is missing some line-drawing mapping: %s", missing);
1064 * Check if the color capabilities are consistent
1067 check_colors(TERMTYPE *tp)
1069 if ((max_colors > 0) != (max_pairs > 0)
1070 || ((max_colors > max_pairs) && (initialize_pair == 0)))
1071 _nc_warning("inconsistent values for max_colors (%d) and max_pairs (%d)",
1072 max_colors, max_pairs);
1074 PAIRED(set_foreground, set_background);
1075 PAIRED(set_a_foreground, set_a_background);
1076 PAIRED(set_color_pair, initialize_pair);
1078 if (VALID_STRING(set_foreground)
1079 && VALID_STRING(set_a_foreground)
1080 && !_nc_capcmp(set_foreground, set_a_foreground))
1081 _nc_warning("expected setf/setaf to be different");
1083 if (VALID_STRING(set_background)
1084 && VALID_STRING(set_a_background)
1085 && !_nc_capcmp(set_background, set_a_background))
1086 _nc_warning("expected setb/setab to be different");
1088 /* see: has_colors() */
1089 if (VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
1090 && (((set_foreground != NULL)
1091 && (set_background != NULL))
1092 || ((set_a_foreground != NULL)
1093 && (set_a_background != NULL))
1094 || set_color_pair)) {
1095 if (!VALID_STRING(orig_pair) && !VALID_STRING(orig_colors))
1096 _nc_warning("expected either op/oc string for resetting colors");
1101 keypad_final(const char *string)
1105 if (VALID_STRING(string)
1106 && *string++ == '\033'
1108 && strlen(string) == 1) {
1116 keypad_index(const char *string)
1119 const char *list = "PQRSwxymtuvlqrsPpn"; /* app-keypad except "Enter" */
1123 if ((ch = keypad_final(string)) != '\0') {
1124 test = strchr(list, ch);
1126 result = (long) (test - list);
1132 * list[] is down, up, left, right
1133 * "left" may be ^H rather than \E[D
1134 * "down" may be ^J rather than \E[B
1135 * But up/right are generally consistently escape sequences for ANSI terminals.
1138 check_ansi_cursor(char *list[4])
1145 bool repeated = FALSE;
1147 for (j = 0; j < 4; ++j) {
1149 for (k = 0; k < j; ++k) {
1151 && !strcmp(list[j], list[k])) {
1152 char *value = _nc_tic_expand(list[k], TRUE, 0);
1153 _nc_warning("repeated cursor control %s\n", value);
1161 if (UChar(up[0]) == '\033') {
1167 } else if (UChar(up[0]) == UChar('\233')) {
1172 while (up[suffix] && isdigit(UChar(up[suffix])))
1175 if (prefix && up[suffix] == 'A') {
1177 if (!strcmp(list[0], "\n"))
1179 if (!strcmp(list[2], "\b"))
1182 for (j = 0; j < 4; ++j) {
1183 if (skip[j] || strlen(list[j]) == 1)
1185 if (memcmp(list[j], up, prefix)) {
1186 char *value = _nc_tic_expand(list[j], TRUE, 0);
1187 _nc_warning("inconsistent prefix for %s\n", value);
1190 if (strlen(list[j]) < suffix) {
1191 char *value = _nc_tic_expand(list[j], TRUE, 0);
1192 _nc_warning("inconsistent length for %s, expected %d\n",
1193 value, (int) suffix + 1);
1197 if (list[j][suffix] != want) {
1198 char *value = _nc_tic_expand(list[j], TRUE, 0);
1199 _nc_warning("inconsistent suffix for %s, expected %c, have %c\n",
1200 value, want, list[j][suffix]);
1207 #define EXPECTED(name) if (!PRESENT(name)) _nc_warning("expected " #name)
1210 check_cursor(TERMTYPE *tp)
1215 /* it is rare to have an insert-line feature without a matching delete */
1216 ANDMISSING(parm_insert_line, insert_line);
1217 ANDMISSING(parm_delete_line, delete_line);
1218 ANDMISSING(parm_insert_line, parm_delete_line);
1220 /* if we have a parameterized form, then the non-parameterized is easy */
1221 ANDMISSING(parm_down_cursor, cursor_down);
1222 ANDMISSING(parm_up_cursor, cursor_up);
1223 ANDMISSING(parm_left_cursor, cursor_left);
1224 ANDMISSING(parm_right_cursor, cursor_right);
1226 /* Given any of a set of cursor movement, the whole set should be present.
1227 * Technically this is not true (we could use cursor_address to fill in
1228 * unsupported controls), but it is likely.
1231 if (PRESENT(parm_down_cursor)) {
1232 list[count++] = parm_down_cursor;
1234 if (PRESENT(parm_up_cursor)) {
1235 list[count++] = parm_up_cursor;
1237 if (PRESENT(parm_left_cursor)) {
1238 list[count++] = parm_left_cursor;
1240 if (PRESENT(parm_right_cursor)) {
1241 list[count++] = parm_right_cursor;
1244 check_ansi_cursor(list);
1245 } else if (count != 0) {
1246 EXPECTED(parm_down_cursor);
1247 EXPECTED(parm_up_cursor);
1248 EXPECTED(parm_left_cursor);
1249 EXPECTED(parm_right_cursor);
1253 if (PRESENT(cursor_down)) {
1254 list[count++] = cursor_down;
1256 if (PRESENT(cursor_up)) {
1257 list[count++] = cursor_up;
1259 if (PRESENT(cursor_left)) {
1260 list[count++] = cursor_left;
1262 if (PRESENT(cursor_right)) {
1263 list[count++] = cursor_right;
1266 check_ansi_cursor(list);
1267 } else if (count != 0) {
1269 if (PRESENT(cursor_down) && strcmp(cursor_down, "\n"))
1271 if (PRESENT(cursor_left) && strcmp(cursor_left, "\b"))
1273 if (PRESENT(cursor_up) && strlen(cursor_up) > 1)
1275 if (PRESENT(cursor_right) && strlen(cursor_right) > 1)
1278 EXPECTED(cursor_down);
1279 EXPECTED(cursor_up);
1280 EXPECTED(cursor_left);
1281 EXPECTED(cursor_right);
1288 * Do a quick sanity-check for vt100-style keypads to see if the 5-key keypad
1289 * is mapped inconsistently.
1292 check_keypad(TERMTYPE *tp)
1296 if (VALID_STRING(key_a1) &&
1297 VALID_STRING(key_a3) &&
1298 VALID_STRING(key_b2) &&
1299 VALID_STRING(key_c1) &&
1300 VALID_STRING(key_c3)) {
1301 char final[MAX_KP + 1];
1308 final[0] = keypad_final(key_a1);
1309 final[1] = keypad_final(key_a3);
1310 final[2] = keypad_final(key_b2);
1311 final[3] = keypad_final(key_c1);
1312 final[4] = keypad_final(key_c3);
1315 /* special case: legacy coding using 1,2,3,0,. on the bottom */
1316 assert(strlen(final) <= MAX_KP);
1317 if (!strcmp(final, "qsrpn"))
1320 list[0] = keypad_index(key_a1);
1321 list[1] = keypad_index(key_a3);
1322 list[2] = keypad_index(key_b2);
1323 list[3] = keypad_index(key_c1);
1324 list[4] = keypad_index(key_c3);
1326 /* check that they're all vt100 keys */
1327 for (j = 0; j < MAX_KP; ++j) {
1333 /* check if they're all in increasing order */
1334 for (j = 1; j < MAX_KP; ++j) {
1335 if (list[j] > list[j - 1]) {
1339 if (increase != (MAX_KP - 1)) {
1342 for (j = 0, last = -1; j < MAX_KP; ++j) {
1343 for (k = 0, kk = -1, test = 100; k < 5; ++k) {
1344 if (list[k] > last &&
1351 assert(strlen(show) < (MAX_KP * 4));
1354 _nc_STRCAT(show, " ka1", sizeof(show));
1357 _nc_STRCAT(show, " ka3", sizeof(show));
1360 _nc_STRCAT(show, " kb2", sizeof(show));
1363 _nc_STRCAT(show, " kc1", sizeof(show));
1366 _nc_STRCAT(show, " kc3", sizeof(show));
1371 _nc_warning("vt100 keypad order inconsistent: %s", show);
1374 } else if (VALID_STRING(key_a1) ||
1375 VALID_STRING(key_a3) ||
1376 VALID_STRING(key_b2) ||
1377 VALID_STRING(key_c1) ||
1378 VALID_STRING(key_c3)) {
1380 if (keypad_index(key_a1) >= 0)
1381 _nc_STRCAT(show, " ka1", sizeof(show));
1382 if (keypad_index(key_a3) >= 0)
1383 _nc_STRCAT(show, " ka3", sizeof(show));
1384 if (keypad_index(key_b2) >= 0)
1385 _nc_STRCAT(show, " kb2", sizeof(show));
1386 if (keypad_index(key_c1) >= 0)
1387 _nc_STRCAT(show, " kc1", sizeof(show));
1388 if (keypad_index(key_c3) >= 0)
1389 _nc_STRCAT(show, " kc3", sizeof(show));
1391 _nc_warning("vt100 keypad map incomplete:%s", show);
1395 * These warnings are useful for consistency checks - it is possible that
1396 * there are real terminals with mismatches in these
1398 ANDMISSING(key_ic, key_dc);
1402 check_printer(TERMTYPE *tp)
1404 PAIRED(enter_doublewide_mode, exit_doublewide_mode);
1405 PAIRED(enter_italics_mode, exit_italics_mode);
1406 PAIRED(enter_leftward_mode, exit_leftward_mode);
1407 PAIRED(enter_micro_mode, exit_micro_mode);
1408 PAIRED(enter_shadow_mode, exit_shadow_mode);
1409 PAIRED(enter_subscript_mode, exit_subscript_mode);
1410 PAIRED(enter_superscript_mode, exit_superscript_mode);
1411 PAIRED(enter_upward_mode, exit_upward_mode);
1413 ANDMISSING(start_char_set_def, stop_char_set_def);
1415 /* if we have a parameterized form, then the non-parameterized is easy */
1416 ANDMISSING(set_bottom_margin_parm, set_bottom_margin);
1417 ANDMISSING(set_left_margin_parm, set_left_margin);
1418 ANDMISSING(set_right_margin_parm, set_right_margin);
1419 ANDMISSING(set_top_margin_parm, set_top_margin);
1421 ANDMISSING(parm_down_micro, micro_down);
1422 ANDMISSING(parm_left_micro, micro_left);
1423 ANDMISSING(parm_right_micro, micro_right);
1424 ANDMISSING(parm_up_micro, micro_up);
1428 uses_SGR_39_49(const char *value)
1430 return (strstr(value, "39;49") != 0
1431 || strstr(value, "49;39") != 0);
1435 * Check consistency of termcap extensions related to "screen".
1438 check_screen(TERMTYPE *tp)
1440 if (_nc_user_definable) {
1441 int have_XT = tigetflag("XT");
1442 int have_XM = tigetflag("XM");
1443 int have_bce = back_color_erase;
1444 bool have_kmouse = FALSE;
1445 bool use_sgr_39_49 = FALSE;
1446 char *name = _nc_first_name(tp->term_names);
1448 if (!VALID_BOOLEAN(have_bce)) {
1451 if (!VALID_BOOLEAN(have_XM)) {
1454 if (!VALID_BOOLEAN(have_XT)) {
1457 if (VALID_STRING(key_mouse)) {
1458 have_kmouse = !strcmp("\033[M", key_mouse);
1460 if (VALID_STRING(orig_colors)) {
1461 use_sgr_39_49 = uses_SGR_39_49(orig_colors);
1462 } else if (VALID_STRING(orig_pair)) {
1463 use_sgr_39_49 = uses_SGR_39_49(orig_pair);
1466 if (have_XM && have_XT) {
1467 _nc_warning("Screen's XT capability conflicts with XM");
1469 && strstr(name, "screen") != 0
1470 && strchr(name, '.') != 0) {
1471 _nc_warning("Screen's \"screen\" entries should not have XT set");
1472 } else if (have_XT) {
1473 if (!have_kmouse && have_bce) {
1474 if (VALID_STRING(key_mouse)) {
1475 _nc_warning("Value of kmous inconsistent with screen's usage");
1477 _nc_warning("Expected kmous capability with XT");
1480 if (!have_bce && max_colors > 0)
1481 _nc_warning("Expected bce capability with XT");
1482 if (!use_sgr_39_49 && have_bce && max_colors > 0)
1483 _nc_warning("Expected orig_colors capability with XT to have 39/49 parameters");
1484 if (VALID_STRING(to_status_line))
1485 _nc_warning("\"tsl\" capability is redundant, given XT");
1487 if (have_kmouse && !have_XM)
1488 _nc_warning("Expected XT to be set, given kmous");
1494 * Returns the expected number of parameters for the given capability.
1497 expected_params(const char *name)
1500 static const struct {
1504 { "S0", 1 }, /* 'screen' extension */
1557 { "smgbp", 1 }, /* 2 if smgtp is not given */
1572 int result = 0; /* function-keys, etc., use none */
1574 for (n = 0; n < SIZEOF(table); n++) {
1575 if (!strcmp(name, table[n].name)) {
1576 result = table[n].count;
1585 * Make a quick sanity check for the parameters which are used in the given
1586 * strings. If there are no "%p" tokens, then there should be no other "%"
1590 check_params(TERMTYPE *tp, const char *name, char *value)
1592 int expected = expected_params(name);
1598 #ifdef set_top_margin_parm
1599 if (!strcmp(name, "smgbp")
1600 && set_top_margin_parm == 0)
1604 for (n = 0; n < 10; n++)
1610 _nc_warning("expected character after %% in %s", name);
1612 } else if (*s == 'p') {
1613 if (*++s == '\0' || !isdigit((int) *s)) {
1614 _nc_warning("expected digit after %%p in %s", name);
1628 _nc_warning("%s refers to parameter 0 (%%p0), which is not allowed", name);
1630 if (value == set_attributes || expected < 0) {
1632 } else if (expected != actual) {
1633 _nc_warning("%s uses %d parameters, expected %d", name,
1635 for (n = 1; n < actual; n++) {
1637 _nc_warning("%s omits parameter %d", name, n);
1645 while (*s == '/' || isdigit(UChar(*s)))
1651 * Skip a delay altogether, e.g., when comparing a simple string to sgr,
1652 * the latter may have a worst-case delay on the end.
1655 ignore_delays(char *s)
1688 * An sgr string may contain several settings other than the one we're
1689 * interested in, essentially sgr0 + rmacs + whatever. As long as the
1690 * "whatever" is contained in the sgr string, that is close enough for our
1694 similar_sgr(int num, char *a, char *b)
1696 static const char *names[] =
1718 _nc_warning("Did not find delay %s", _nc_visbuf(b));
1720 _nc_warning("checking sgr(%s) %s\n\tcompare to %s\n\tunmatched %s",
1721 names[num], _nc_visbuf2(1, base_a),
1722 _nc_visbuf2(2, base_b),
1726 } else if (delaying) {
1729 } else if ((*b == '0' || (*b == ';')) && *a == 'm') {
1751 /* ignore delays on the end of the string */
1752 a = ignore_delays(a);
1753 return ((num != 0) || (*a == 0));
1757 check_sgr(TERMTYPE *tp, char *zero, int num, char *cap, const char *name)
1762 test = TPARM_9(set_attributes,
1774 if (!similar_sgr(num, test, cap)) {
1775 _nc_warning("%s differs from sgr(%d)\n\t%s=%s\n\tsgr(%d)=%s",
1777 name, _nc_visbuf2(1, cap),
1778 num, _nc_visbuf2(2, test));
1780 } else if (_nc_capcmp(test, zero)) {
1781 _nc_warning("sgr(%d) present, but not %s", num, name);
1783 } else if (PRESENT(cap)) {
1784 _nc_warning("sgr(%d) missing, but %s present", num, name);
1787 _nc_warning("stack error in sgr(%d) string", num);
1791 #define CHECK_SGR(num,name) check_sgr(tp, zero, num, name, #name)
1795 * If tic is compiled with TRACE, we'll be able to see the output from the
1796 * DEBUG() macro. But since it doesn't use traceon(), it always goes to
1797 * the standard error. Use this function to make it simpler to follow the
1798 * resulting debug traces.
1801 show_where(unsigned level)
1803 if (_nc_tracing >= DEBUG_LEVEL(level)) {
1804 char my_name[MAX_NAME_SIZE];
1805 _nc_get_type(my_name);
1806 _tracef("\"%s\", line %d, '%s'",
1808 _nc_curr_line, my_name);
1813 #define show_where(level) /* nothing */
1823 get_fkey_list(TERMTYPE *tp)
1825 NAME_VALUE *result = typeMalloc(NAME_VALUE, NUM_STRINGS(tp) + 1);
1826 const struct tinfo_fkeys *all_fkeys = _nc_tinfo_fkeys;
1830 for (j = 0; all_fkeys[j].code; j++) {
1831 char *a = tp->Strings[all_fkeys[j].offset];
1832 if (VALID_STRING(a)) {
1833 result[used].keycode = (int) all_fkeys[j].code;
1834 result[used].name = strnames[all_fkeys[j].offset];
1835 result[used].value = a;
1840 for (j = STRCOUNT; j < NUM_STRINGS(tp); ++j) {
1841 const char *name = ExtStrname(tp, j, strnames);
1843 result[used].keycode = -1;
1844 result[used].name = name;
1845 result[used].value = tp->Strings[j];
1850 result[used].keycode = 0;
1855 show_fkey_name(NAME_VALUE * data)
1857 if (data->keycode > 0) {
1858 fprintf(stderr, " %s", keyname(data->keycode));
1859 fprintf(stderr, " (capability \"%s\")", data->name);
1861 fprintf(stderr, " capability \"%s\"", data->name);
1865 /* other sanity-checks (things that we don't want in the normal
1866 * logic that reads a terminfo entry)
1869 check_termtype(TERMTYPE *tp, bool literal)
1871 bool conflict = FALSE;
1875 * A terminal entry may contain more than one keycode assigned to
1876 * a given string (e.g., KEY_END and KEY_LL). But curses will only
1877 * return one (the last one assigned).
1879 if (!(_nc_syntax == SYN_TERMCAP && capdump)) {
1880 char *check = calloc((size_t) (NUM_STRINGS(tp) + 1), sizeof(char));
1881 NAME_VALUE *given = get_fkey_list(tp);
1883 for (j = 0; given[j].keycode; ++j) {
1884 const char *a = given[j].value;
1887 for (k = j + 1; given[k].keycode; k++) {
1888 const char *b = given[k].value;
1891 if (!_nc_capcmp(a, b)) {
1896 _nc_warning("Conflicting key definitions (using the last)");
1899 fprintf(stderr, "...");
1900 show_fkey_name(given + j);
1901 fprintf(stderr, " is the same as");
1902 show_fkey_name(given + k);
1905 fprintf(stderr, ", ");
1906 show_fkey_name(given + k);
1911 fprintf(stderr, "\n");
1917 for_each_string(j, tp) {
1918 char *a = tp->Strings[j];
1919 if (VALID_STRING(a))
1920 check_params(tp, ExtStrname(tp, (int) j, strnames), a);
1931 * These may be mismatched because the terminal description relies on
1932 * restoring the cursor visibility by resetting it.
1934 ANDMISSING(cursor_invisible, cursor_normal);
1935 ANDMISSING(cursor_visible, cursor_normal);
1937 if (PRESENT(cursor_visible) && PRESENT(cursor_normal)
1938 && !_nc_capcmp(cursor_visible, cursor_normal))
1939 _nc_warning("cursor_visible is same as cursor_normal");
1942 * From XSI & O'Reilly, we gather that sc/rc are required if csr is
1943 * given, because the cursor position after the scrolling operation is
1944 * performed is undefined.
1946 ANDMISSING(change_scroll_region, save_cursor);
1947 ANDMISSING(change_scroll_region, restore_cursor);
1950 * If we can clear tabs, we should be able to initialize them.
1952 ANDMISSING(clear_all_tabs, set_tab);
1954 if (PRESENT(set_attributes)) {
1958 if (PRESENT(exit_attribute_mode)) {
1959 zero = strdup(CHECK_SGR(0, exit_attribute_mode));
1961 zero = strdup(TPARM_9(set_attributes, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1964 _nc_warning("stack error in sgr(0) string");
1967 CHECK_SGR(1, enter_standout_mode);
1968 CHECK_SGR(2, enter_underline_mode);
1969 CHECK_SGR(3, enter_reverse_mode);
1970 CHECK_SGR(4, enter_blink_mode);
1971 CHECK_SGR(5, enter_dim_mode);
1972 CHECK_SGR(6, enter_bold_mode);
1973 CHECK_SGR(7, enter_secure_mode);
1974 CHECK_SGR(8, enter_protected_mode);
1975 CHECK_SGR(9, enter_alt_charset_mode);
1978 _nc_warning("sgr(0) did not return a value");
1980 } else if (PRESENT(exit_attribute_mode) &&
1981 set_attributes != CANCELLED_STRING) {
1982 if (_nc_syntax == SYN_TERMINFO)
1983 _nc_warning("missing sgr string");
1986 if (PRESENT(exit_attribute_mode)) {
1987 char *check_sgr0 = _nc_trim_sgr0(tp);
1989 if (check_sgr0 == 0 || *check_sgr0 == '\0') {
1990 _nc_warning("trimmed sgr0 is empty");
1993 if (check_sgr0 != exit_attribute_mode) {
1995 ("will trim sgr0\n\toriginal sgr0=%s\n\ttrimmed sgr0=%s",
1996 _nc_visbuf2(1, exit_attribute_mode),
1997 _nc_visbuf2(2, check_sgr0)));
2001 ("will not trim sgr0\n\toriginal sgr0=%s",
2002 _nc_visbuf(exit_attribute_mode)));
2008 if (!auto_right_margin) {
2010 ("can write to lower-right directly"));
2011 } else if (PRESENT(enter_am_mode) && PRESENT(exit_am_mode)) {
2013 ("can write to lower-right by suppressing automargin"));
2014 } else if ((PRESENT(enter_insert_mode) && PRESENT(exit_insert_mode))
2015 || PRESENT(insert_character) || PRESENT(parm_ich)) {
2017 ("can write to lower-right by using inserts"));
2020 ("cannot write to lower-right"));
2025 * Some standard applications (e.g., vi) and some non-curses
2026 * applications (e.g., jove) get confused if we have both ich1 and
2027 * smir/rmir. Let's be nice and warn about that, too, even though
2028 * ncurses handles it.
2030 if ((PRESENT(enter_insert_mode) || PRESENT(exit_insert_mode))
2031 && PRESENT(parm_ich)) {
2032 _nc_warning("non-curses applications may be confused by ich1 with smir/rmir");
2036 * Finally, do the non-verbose checks
2038 if (save_check_termtype != 0)
2039 save_check_termtype(tp, literal);