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.180 2012/12/08 22:17:22 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);
638 if ((outdir = _nc_home_terminfo())) {
639 if ((result = valid_db_path(outdir)) != 0) {
640 printf("%s\n", result);
641 } else if (!specific) {
647 * If we can write in neither location, give an error message.
651 fprintf(stderr, "%s: %s (no permission)\n", _nc_progname, tried);
652 ExitProgram(EXIT_FAILURE);
656 #define VtoTrace(opt) (unsigned) ((opt > 0) ? opt : (opt == 0))
659 main(int argc, char *argv[])
661 char my_tmpname[PATH_MAX];
662 char my_altfile[PATH_MAX];
664 unsigned debug_level;
665 int smart_defaults = TRUE;
669 int this_opt, last_opt = '?';
671 int outform = F_TERMINFO; /* output format */
672 int sortmode = S_TERMINFO; /* sort_mode */
676 bool formatted = FALSE; /* reformat complex strings? */
677 bool literal = FALSE; /* suppress post-processing? */
678 int numbers = 0; /* format "%'char'" to/from "%{number}" */
679 bool forceresolve = FALSE; /* force resolution */
681 char *tversion = (char *) NULL;
682 const char *source_file = "terminfo";
683 char *outdir = (char *) NULL;
684 bool check_only = FALSE;
685 bool suppress_untranslatable = FALSE;
689 _nc_progname = _nc_rootname(argv[0]);
692 if ((infodump = same_program(_nc_progname, PROG_CAPTOINFO)) != FALSE) {
693 outform = F_TERMINFO;
694 sortmode = S_TERMINFO;
696 if ((capdump = same_program(_nc_progname, PROG_INFOTOCAP)) != FALSE) {
698 sortmode = S_TERMCAP;
701 use_extended_names(FALSE);
706 * Processing arguments is a little complicated, since someone made a
707 * design decision to allow the numeric values for -w, -v options to
710 while ((this_opt = getopt(argc, argv,
711 "0123456789CDIKLNR:TUVace:fGgo:rstvwx")) != -1) {
712 if (isdigit(this_opt)) {
715 v_opt = (v_opt * 10) + (this_opt - '0');
718 width = (width * 10) + (this_opt - '0');
740 /* the initial version of -K in 20110730 fell-thru here, but the
741 * same flag is useful when reading sources -TD
747 sortmode = S_TERMCAP;
750 debug_level = VtoTrace(v_opt);
751 set_trace_level(debug_level);
752 show_databases(outdir);
753 ExitProgram(EXIT_SUCCESS);
757 outform = F_TERMINFO;
758 sortmode = S_TERMINFO;
762 outform = F_VARIABLE;
763 sortmode = S_VARIABLE;
766 smart_defaults = FALSE;
779 puts(curses_version());
780 ExitProgram(EXIT_SUCCESS);
785 namelst = make_namelist(optarg);
813 _nc_disable_period = FALSE;
814 suppress_untranslatable = TRUE;
817 _nc_disable_period = TRUE;
820 use_extended_names(TRUE);
829 debug_level = VtoTrace(v_opt);
830 set_trace_level(debug_level);
833 save_check_termtype = _nc_check_termtype2;
834 _nc_check_termtype2 = check_termtype;
838 * Aaargh! immedhook seriously hoses us!
840 * One problem with immedhook is it means we can't do -e. Problem
841 * is that we can't guarantee that for each terminal listed, all the
842 * terminals it depends on will have been kept in core for reference
843 * resolution -- in fact it's certain the primitive types at the end
844 * of reference chains *won't* be in core unless they were explicitly
845 * in the select list themselves.
847 if (namelst && (!infodump && !capdump)) {
848 (void) fprintf(stderr,
849 "%s: Sorry, -e can't be used without -I or -C\n",
851 ExitProgram(EXIT_FAILURE);
853 #endif /* HAVE_BIG_CORE */
856 source_file = argv[optind++];
859 "%s: Too many file names. Usage:\n\t%s %s",
863 ExitProgram(EXIT_FAILURE);
866 if (infodump == TRUE) {
867 /* captoinfo's no-argument case */
868 source_file = "/etc/termcap";
869 if ((termcap = getenv("TERMCAP")) != 0
870 && (namelst = make_namelist(getenv("TERM"))) != 0) {
871 if (access(termcap, F_OK) == 0) {
873 source_file = termcap;
875 if ((tmp_fp = open_tempfile(my_tmpname)) != 0) {
876 source_file = my_tmpname;
877 fprintf(tmp_fp, "%s\n", termcap);
879 tmp_fp = open_input(source_file, (char *) 0);
880 to_remove = source_file;
889 "%s: File name needed. Usage:\n\t%s %s",
893 ExitProgram(EXIT_FAILURE);
898 tmp_fp = open_input(source_file, my_altfile);
899 if (!strcmp(source_file, "-")) {
900 source_file = STDIN_NAME;
909 sortmode, width, height, debug_level, formatted);
910 } else if (capdump) {
913 sortmode, width, height, debug_level, FALSE);
916 /* parse entries out of the source file */
917 _nc_set_source(source_file);
919 if (!(check_only || infodump || capdump))
920 _nc_set_writedir(outdir);
921 #endif /* HAVE_BIG_CORE */
922 _nc_read_entry_source(tmp_fp, (char *) NULL,
923 !smart_defaults || literal, FALSE,
924 ((check_only || infodump || capdump)
928 /* do use resolution */
929 if (check_only || (!infodump && !capdump) || forceresolve) {
930 if (!_nc_resolve_uses2(TRUE, literal) && !check_only) {
931 ExitProgram(EXIT_FAILURE);
936 if (check_only && (capdump || infodump)) {
938 if (matches(namelst, qp->tterm.term_names)) {
939 int len = fmt_entry(&qp->tterm, NULL, FALSE, TRUE, infodump, numbers);
941 if (len > (infodump ? MAX_TERMINFO_LENGTH : MAX_TERMCAP_LENGTH))
942 (void) fprintf(stderr,
943 "warning: resolved %s entry is %d bytes long\n",
944 _nc_first_name(qp->tterm.term_names),
950 /* write or dump all entries */
952 if (!infodump && !capdump) {
953 _nc_set_writedir(outdir);
955 if (matches(namelst, qp->tterm.term_names))
959 /* this is in case infotocap() generates warnings */
960 _nc_curr_col = _nc_curr_line = -1;
963 if (matches(namelst, qp->tterm.term_names)) {
964 long j = qp->cend - qp->cstart;
967 /* this is in case infotocap() generates warnings */
968 _nc_set_type(_nc_first_name(qp->tterm.term_names));
970 (void) fseek(tmp_fp, qp->cstart, SEEK_SET);
973 (void) putchar(fgetc(tmp_fp));
975 put_translate(fgetc(tmp_fp));
978 repair_acsc(&qp->tterm);
979 dump_entry(&qp->tterm, suppress_untranslatable,
980 limited, numbers, NULL);
981 for (j = 0; j < (long) qp->nuses; j++)
982 dump_uses(qp->uses[j].name, !capdump);
984 if (debug_level != 0 && !limited)
985 printf("# length=%d\n", len);
988 if (!namelst && _nc_tail) {
990 bool in_comment = FALSE;
991 bool trailing_comment = FALSE;
993 (void) fseek(tmp_fp, _nc_tail->cend, SEEK_SET);
994 while ((c = fgetc(tmp_fp)) != EOF) {
997 trailing_comment = TRUE;
1003 if (trailing_comment
1004 && (in_comment || (oldc == '\n' && c == '\n')))
1012 /* Show the directory into which entries were written, and the total
1016 && (!(check_only || infodump || capdump))) {
1017 int total = _nc_tic_written();
1019 fprintf(log_fp, "%d entries written to %s\n",
1021 _nc_tic_dir((char *) 0));
1023 fprintf(log_fp, "No entries written\n");
1025 ExitProgram(EXIT_SUCCESS);
1029 * This bit of legerdemain turns all the terminfo variable names into
1030 * references to locations in the arrays Booleans, Numbers, and Strings ---
1031 * precisely what's needed (see comp_parse.c).
1037 * Check if the alternate character-set capabilities are consistent.
1040 check_acs(TERMTYPE *tp)
1042 if (VALID_STRING(acs_chars)) {
1043 const char *boxes = "lmkjtuvwqxn";
1049 memset(mapped, 0, sizeof(mapped));
1050 for (p = acs_chars; *p != '\0'; p += 2) {
1052 _nc_warning("acsc has odd number of characters");
1055 mapped[UChar(p[0])] = p[1];
1058 if (mapped[UChar('I')] && !mapped[UChar('i')]) {
1059 _nc_warning("acsc refers to 'I', which is probably an error");
1062 for (p = boxes, q = missing; *p != '\0'; ++p) {
1063 if (!mapped[UChar(p[0])]) {
1069 assert(strlen(missing) <= strlen(boxes));
1070 if (*missing != '\0' && strcmp(missing, boxes)) {
1071 _nc_warning("acsc is missing some line-drawing mapping: %s", missing);
1077 * Check if the color capabilities are consistent
1080 check_colors(TERMTYPE *tp)
1082 if ((max_colors > 0) != (max_pairs > 0)
1083 || ((max_colors > max_pairs) && (initialize_pair == 0)))
1084 _nc_warning("inconsistent values for max_colors (%d) and max_pairs (%d)",
1085 max_colors, max_pairs);
1087 PAIRED(set_foreground, set_background);
1088 PAIRED(set_a_foreground, set_a_background);
1089 PAIRED(set_color_pair, initialize_pair);
1091 if (VALID_STRING(set_foreground)
1092 && VALID_STRING(set_a_foreground)
1093 && !_nc_capcmp(set_foreground, set_a_foreground))
1094 _nc_warning("expected setf/setaf to be different");
1096 if (VALID_STRING(set_background)
1097 && VALID_STRING(set_a_background)
1098 && !_nc_capcmp(set_background, set_a_background))
1099 _nc_warning("expected setb/setab to be different");
1101 /* see: has_colors() */
1102 if (VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
1103 && (((set_foreground != NULL)
1104 && (set_background != NULL))
1105 || ((set_a_foreground != NULL)
1106 && (set_a_background != NULL))
1107 || set_color_pair)) {
1108 if (!VALID_STRING(orig_pair) && !VALID_STRING(orig_colors))
1109 _nc_warning("expected either op/oc string for resetting colors");
1114 keypad_final(const char *string)
1118 if (VALID_STRING(string)
1119 && *string++ == '\033'
1121 && strlen(string) == 1) {
1129 keypad_index(const char *string)
1132 const char *list = "PQRSwxymtuvlqrsPpn"; /* app-keypad except "Enter" */
1136 if ((ch = keypad_final(string)) != '\0') {
1137 test = strchr(list, ch);
1139 result = (long) (test - list);
1145 * list[] is down, up, left, right
1146 * "left" may be ^H rather than \E[D
1147 * "down" may be ^J rather than \E[B
1148 * But up/right are generally consistently escape sequences for ANSI terminals.
1151 check_ansi_cursor(char *list[4])
1158 bool repeated = FALSE;
1160 for (j = 0; j < 4; ++j) {
1162 for (k = 0; k < j; ++k) {
1164 && !strcmp(list[j], list[k])) {
1165 char *value = _nc_tic_expand(list[k], TRUE, 0);
1166 _nc_warning("repeated cursor control %s\n", value);
1174 if (UChar(up[0]) == '\033') {
1180 } else if (UChar(up[0]) == UChar('\233')) {
1185 while (up[suffix] && isdigit(UChar(up[suffix])))
1188 if (prefix && up[suffix] == 'A') {
1190 if (!strcmp(list[0], "\n"))
1192 if (!strcmp(list[2], "\b"))
1195 for (j = 0; j < 4; ++j) {
1196 if (skip[j] || strlen(list[j]) == 1)
1198 if (memcmp(list[j], up, prefix)) {
1199 char *value = _nc_tic_expand(list[j], TRUE, 0);
1200 _nc_warning("inconsistent prefix for %s\n", value);
1203 if (strlen(list[j]) < suffix) {
1204 char *value = _nc_tic_expand(list[j], TRUE, 0);
1205 _nc_warning("inconsistent length for %s, expected %d\n",
1206 value, (int) suffix + 1);
1210 if (list[j][suffix] != want) {
1211 char *value = _nc_tic_expand(list[j], TRUE, 0);
1212 _nc_warning("inconsistent suffix for %s, expected %c, have %c\n",
1213 value, want, list[j][suffix]);
1220 #define EXPECTED(name) if (!PRESENT(name)) _nc_warning("expected " #name)
1223 check_cursor(TERMTYPE *tp)
1228 /* it is rare to have an insert-line feature without a matching delete */
1229 ANDMISSING(parm_insert_line, insert_line);
1230 ANDMISSING(parm_delete_line, delete_line);
1231 ANDMISSING(parm_insert_line, parm_delete_line);
1233 /* if we have a parameterized form, then the non-parameterized is easy */
1234 ANDMISSING(parm_down_cursor, cursor_down);
1235 ANDMISSING(parm_up_cursor, cursor_up);
1236 ANDMISSING(parm_left_cursor, cursor_left);
1237 ANDMISSING(parm_right_cursor, cursor_right);
1239 /* Given any of a set of cursor movement, the whole set should be present.
1240 * Technically this is not true (we could use cursor_address to fill in
1241 * unsupported controls), but it is likely.
1244 if (PRESENT(parm_down_cursor)) {
1245 list[count++] = parm_down_cursor;
1247 if (PRESENT(parm_up_cursor)) {
1248 list[count++] = parm_up_cursor;
1250 if (PRESENT(parm_left_cursor)) {
1251 list[count++] = parm_left_cursor;
1253 if (PRESENT(parm_right_cursor)) {
1254 list[count++] = parm_right_cursor;
1257 check_ansi_cursor(list);
1258 } else if (count != 0) {
1259 EXPECTED(parm_down_cursor);
1260 EXPECTED(parm_up_cursor);
1261 EXPECTED(parm_left_cursor);
1262 EXPECTED(parm_right_cursor);
1266 if (PRESENT(cursor_down)) {
1267 list[count++] = cursor_down;
1269 if (PRESENT(cursor_up)) {
1270 list[count++] = cursor_up;
1272 if (PRESENT(cursor_left)) {
1273 list[count++] = cursor_left;
1275 if (PRESENT(cursor_right)) {
1276 list[count++] = cursor_right;
1279 check_ansi_cursor(list);
1280 } else if (count != 0) {
1282 if (PRESENT(cursor_down) && strcmp(cursor_down, "\n"))
1284 if (PRESENT(cursor_left) && strcmp(cursor_left, "\b"))
1286 if (PRESENT(cursor_up) && strlen(cursor_up) > 1)
1288 if (PRESENT(cursor_right) && strlen(cursor_right) > 1)
1291 EXPECTED(cursor_down);
1292 EXPECTED(cursor_up);
1293 EXPECTED(cursor_left);
1294 EXPECTED(cursor_right);
1301 * Do a quick sanity-check for vt100-style keypads to see if the 5-key keypad
1302 * is mapped inconsistently.
1305 check_keypad(TERMTYPE *tp)
1309 if (VALID_STRING(key_a1) &&
1310 VALID_STRING(key_a3) &&
1311 VALID_STRING(key_b2) &&
1312 VALID_STRING(key_c1) &&
1313 VALID_STRING(key_c3)) {
1314 char final[MAX_KP + 1];
1321 final[0] = keypad_final(key_a1);
1322 final[1] = keypad_final(key_a3);
1323 final[2] = keypad_final(key_b2);
1324 final[3] = keypad_final(key_c1);
1325 final[4] = keypad_final(key_c3);
1328 /* special case: legacy coding using 1,2,3,0,. on the bottom */
1329 assert(strlen(final) <= MAX_KP);
1330 if (!strcmp(final, "qsrpn"))
1333 list[0] = keypad_index(key_a1);
1334 list[1] = keypad_index(key_a3);
1335 list[2] = keypad_index(key_b2);
1336 list[3] = keypad_index(key_c1);
1337 list[4] = keypad_index(key_c3);
1339 /* check that they're all vt100 keys */
1340 for (j = 0; j < MAX_KP; ++j) {
1346 /* check if they're all in increasing order */
1347 for (j = 1; j < MAX_KP; ++j) {
1348 if (list[j] > list[j - 1]) {
1352 if (increase != (MAX_KP - 1)) {
1355 for (j = 0, last = -1; j < MAX_KP; ++j) {
1356 for (k = 0, kk = -1, test = 100; k < 5; ++k) {
1357 if (list[k] > last &&
1364 assert(strlen(show) < (MAX_KP * 4));
1367 _nc_STRCAT(show, " ka1", sizeof(show));
1370 _nc_STRCAT(show, " ka3", sizeof(show));
1373 _nc_STRCAT(show, " kb2", sizeof(show));
1376 _nc_STRCAT(show, " kc1", sizeof(show));
1379 _nc_STRCAT(show, " kc3", sizeof(show));
1384 _nc_warning("vt100 keypad order inconsistent: %s", show);
1387 } else if (VALID_STRING(key_a1) ||
1388 VALID_STRING(key_a3) ||
1389 VALID_STRING(key_b2) ||
1390 VALID_STRING(key_c1) ||
1391 VALID_STRING(key_c3)) {
1393 if (keypad_index(key_a1) >= 0)
1394 _nc_STRCAT(show, " ka1", sizeof(show));
1395 if (keypad_index(key_a3) >= 0)
1396 _nc_STRCAT(show, " ka3", sizeof(show));
1397 if (keypad_index(key_b2) >= 0)
1398 _nc_STRCAT(show, " kb2", sizeof(show));
1399 if (keypad_index(key_c1) >= 0)
1400 _nc_STRCAT(show, " kc1", sizeof(show));
1401 if (keypad_index(key_c3) >= 0)
1402 _nc_STRCAT(show, " kc3", sizeof(show));
1404 _nc_warning("vt100 keypad map incomplete:%s", show);
1408 * These warnings are useful for consistency checks - it is possible that
1409 * there are real terminals with mismatches in these
1411 ANDMISSING(key_ic, key_dc);
1415 check_printer(TERMTYPE *tp)
1417 PAIRED(enter_doublewide_mode, exit_doublewide_mode);
1418 PAIRED(enter_italics_mode, exit_italics_mode);
1419 PAIRED(enter_leftward_mode, exit_leftward_mode);
1420 PAIRED(enter_micro_mode, exit_micro_mode);
1421 PAIRED(enter_shadow_mode, exit_shadow_mode);
1422 PAIRED(enter_subscript_mode, exit_subscript_mode);
1423 PAIRED(enter_superscript_mode, exit_superscript_mode);
1424 PAIRED(enter_upward_mode, exit_upward_mode);
1426 ANDMISSING(start_char_set_def, stop_char_set_def);
1428 /* if we have a parameterized form, then the non-parameterized is easy */
1429 ANDMISSING(set_bottom_margin_parm, set_bottom_margin);
1430 ANDMISSING(set_left_margin_parm, set_left_margin);
1431 ANDMISSING(set_right_margin_parm, set_right_margin);
1432 ANDMISSING(set_top_margin_parm, set_top_margin);
1434 ANDMISSING(parm_down_micro, micro_down);
1435 ANDMISSING(parm_left_micro, micro_left);
1436 ANDMISSING(parm_right_micro, micro_right);
1437 ANDMISSING(parm_up_micro, micro_up);
1441 uses_SGR_39_49(const char *value)
1443 return (strstr(value, "39;49") != 0
1444 || strstr(value, "49;39") != 0);
1448 * Check consistency of termcap extensions related to "screen".
1451 check_screen(TERMTYPE *tp)
1453 if (_nc_user_definable) {
1454 int have_XT = tigetflag("XT");
1455 int have_XM = tigetflag("XM");
1456 int have_bce = back_color_erase;
1457 bool have_kmouse = FALSE;
1458 bool use_sgr_39_49 = FALSE;
1459 char *name = _nc_first_name(tp->term_names);
1461 if (!VALID_BOOLEAN(have_bce)) {
1464 if (!VALID_BOOLEAN(have_XM)) {
1467 if (!VALID_BOOLEAN(have_XT)) {
1470 if (VALID_STRING(key_mouse)) {
1471 have_kmouse = !strcmp("\033[M", key_mouse);
1473 if (VALID_STRING(orig_colors)) {
1474 use_sgr_39_49 = uses_SGR_39_49(orig_colors);
1475 } else if (VALID_STRING(orig_pair)) {
1476 use_sgr_39_49 = uses_SGR_39_49(orig_pair);
1479 if (have_XM && have_XT) {
1480 _nc_warning("Screen's XT capability conflicts with XM");
1482 && strstr(name, "screen") != 0
1483 && strchr(name, '.') != 0) {
1484 _nc_warning("Screen's \"screen\" entries should not have XT set");
1485 } else if (have_XT) {
1486 if (!have_kmouse && have_bce) {
1487 if (VALID_STRING(key_mouse)) {
1488 _nc_warning("Value of kmous inconsistent with screen's usage");
1490 _nc_warning("Expected kmous capability with XT");
1493 if (!have_bce && max_colors > 0)
1494 _nc_warning("Expected bce capability with XT");
1495 if (!use_sgr_39_49 && have_bce && max_colors > 0)
1496 _nc_warning("Expected orig_colors capability with XT to have 39/49 parameters");
1497 if (VALID_STRING(to_status_line))
1498 _nc_warning("\"tsl\" capability is redundant, given XT");
1500 if (have_kmouse && !have_XM)
1501 _nc_warning("Expected XT to be set, given kmous");
1507 * Returns the expected number of parameters for the given capability.
1510 expected_params(const char *name)
1513 static const struct {
1517 { "S0", 1 }, /* 'screen' extension */
1570 { "smgbp", 1 }, /* 2 if smgtp is not given */
1585 int result = 0; /* function-keys, etc., use none */
1587 for (n = 0; n < SIZEOF(table); n++) {
1588 if (!strcmp(name, table[n].name)) {
1589 result = table[n].count;
1598 * Make a quick sanity check for the parameters which are used in the given
1599 * strings. If there are no "%p" tokens, then there should be no other "%"
1603 check_params(TERMTYPE *tp, const char *name, char *value)
1605 int expected = expected_params(name);
1611 #ifdef set_top_margin_parm
1612 if (!strcmp(name, "smgbp")
1613 && set_top_margin_parm == 0)
1617 for (n = 0; n < 10; n++)
1623 _nc_warning("expected character after %% in %s", name);
1625 } else if (*s == 'p') {
1626 if (*++s == '\0' || !isdigit((int) *s)) {
1627 _nc_warning("expected digit after %%p in %s", name);
1641 _nc_warning("%s refers to parameter 0 (%%p0), which is not allowed", name);
1643 if (value == set_attributes || expected < 0) {
1645 } else if (expected != actual) {
1646 _nc_warning("%s uses %d parameters, expected %d", name,
1648 for (n = 1; n < actual; n++) {
1650 _nc_warning("%s omits parameter %d", name, n);
1658 while (*s == '/' || isdigit(UChar(*s)))
1664 * Skip a delay altogether, e.g., when comparing a simple string to sgr,
1665 * the latter may have a worst-case delay on the end.
1668 ignore_delays(char *s)
1701 * An sgr string may contain several settings other than the one we're
1702 * interested in, essentially sgr0 + rmacs + whatever. As long as the
1703 * "whatever" is contained in the sgr string, that is close enough for our
1707 similar_sgr(int num, char *a, char *b)
1709 static const char *names[] =
1731 _nc_warning("Did not find delay %s", _nc_visbuf(b));
1733 _nc_warning("checking sgr(%s) %s\n\tcompare to %s\n\tunmatched %s",
1734 names[num], _nc_visbuf2(1, base_a),
1735 _nc_visbuf2(2, base_b),
1739 } else if (delaying) {
1742 } else if ((*b == '0' || (*b == ';')) && *a == 'm') {
1764 /* ignore delays on the end of the string */
1765 a = ignore_delays(a);
1766 return ((num != 0) || (*a == 0));
1770 check_sgr(TERMTYPE *tp, char *zero, int num, char *cap, const char *name)
1775 test = TPARM_9(set_attributes,
1787 if (!similar_sgr(num, test, cap)) {
1788 _nc_warning("%s differs from sgr(%d)\n\t%s=%s\n\tsgr(%d)=%s",
1790 name, _nc_visbuf2(1, cap),
1791 num, _nc_visbuf2(2, test));
1793 } else if (_nc_capcmp(test, zero)) {
1794 _nc_warning("sgr(%d) present, but not %s", num, name);
1796 } else if (PRESENT(cap)) {
1797 _nc_warning("sgr(%d) missing, but %s present", num, name);
1800 _nc_warning("stack error in sgr(%d) string", num);
1804 #define CHECK_SGR(num,name) check_sgr(tp, zero, num, name, #name)
1808 * If tic is compiled with TRACE, we'll be able to see the output from the
1809 * DEBUG() macro. But since it doesn't use traceon(), it always goes to
1810 * the standard error. Use this function to make it simpler to follow the
1811 * resulting debug traces.
1814 show_where(unsigned level)
1816 if (_nc_tracing >= DEBUG_LEVEL(level)) {
1817 char my_name[MAX_NAME_SIZE];
1818 _nc_get_type(my_name);
1819 _tracef("\"%s\", line %d, '%s'",
1821 _nc_curr_line, my_name);
1826 #define show_where(level) /* nothing */
1836 get_fkey_list(TERMTYPE *tp)
1838 NAME_VALUE *result = typeMalloc(NAME_VALUE, NUM_STRINGS(tp) + 1);
1839 const struct tinfo_fkeys *all_fkeys = _nc_tinfo_fkeys;
1844 failed("get_fkey_list");
1846 for (j = 0; all_fkeys[j].code; j++) {
1847 char *a = tp->Strings[all_fkeys[j].offset];
1848 if (VALID_STRING(a)) {
1849 result[used].keycode = (int) all_fkeys[j].code;
1850 result[used].name = strnames[all_fkeys[j].offset];
1851 result[used].value = a;
1856 for (j = STRCOUNT; j < NUM_STRINGS(tp); ++j) {
1857 const char *name = ExtStrname(tp, j, strnames);
1859 result[used].keycode = -1;
1860 result[used].name = name;
1861 result[used].value = tp->Strings[j];
1866 result[used].keycode = 0;
1871 show_fkey_name(NAME_VALUE * data)
1873 if (data->keycode > 0) {
1874 fprintf(stderr, " %s", keyname(data->keycode));
1875 fprintf(stderr, " (capability \"%s\")", data->name);
1877 fprintf(stderr, " capability \"%s\"", data->name);
1881 /* other sanity-checks (things that we don't want in the normal
1882 * logic that reads a terminfo entry)
1885 check_termtype(TERMTYPE *tp, bool literal)
1887 bool conflict = FALSE;
1891 * A terminal entry may contain more than one keycode assigned to
1892 * a given string (e.g., KEY_END and KEY_LL). But curses will only
1893 * return one (the last one assigned).
1895 if (!(_nc_syntax == SYN_TERMCAP && capdump)) {
1896 char *check = calloc((size_t) (NUM_STRINGS(tp) + 1), sizeof(char));
1897 NAME_VALUE *given = get_fkey_list(tp);
1900 failed("check_termtype");
1902 for (j = 0; given[j].keycode; ++j) {
1903 const char *a = given[j].value;
1906 for (k = j + 1; given[k].keycode; k++) {
1907 const char *b = given[k].value;
1910 if (!_nc_capcmp(a, b)) {
1915 _nc_warning("Conflicting key definitions (using the last)");
1918 fprintf(stderr, "...");
1919 show_fkey_name(given + j);
1920 fprintf(stderr, " is the same as");
1921 show_fkey_name(given + k);
1924 fprintf(stderr, ", ");
1925 show_fkey_name(given + k);
1930 fprintf(stderr, "\n");
1936 for_each_string(j, tp) {
1937 char *a = tp->Strings[j];
1938 if (VALID_STRING(a))
1939 check_params(tp, ExtStrname(tp, (int) j, strnames), a);
1950 * These may be mismatched because the terminal description relies on
1951 * restoring the cursor visibility by resetting it.
1953 ANDMISSING(cursor_invisible, cursor_normal);
1954 ANDMISSING(cursor_visible, cursor_normal);
1956 if (PRESENT(cursor_visible) && PRESENT(cursor_normal)
1957 && !_nc_capcmp(cursor_visible, cursor_normal))
1958 _nc_warning("cursor_visible is same as cursor_normal");
1961 * From XSI & O'Reilly, we gather that sc/rc are required if csr is
1962 * given, because the cursor position after the scrolling operation is
1963 * performed is undefined.
1965 ANDMISSING(change_scroll_region, save_cursor);
1966 ANDMISSING(change_scroll_region, restore_cursor);
1969 * If we can clear tabs, we should be able to initialize them.
1971 ANDMISSING(clear_all_tabs, set_tab);
1973 if (PRESENT(set_attributes)) {
1977 if (PRESENT(exit_attribute_mode)) {
1978 zero = strdup(CHECK_SGR(0, exit_attribute_mode));
1980 zero = strdup(TPARM_9(set_attributes, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1983 _nc_warning("stack error in sgr(0) string");
1986 CHECK_SGR(1, enter_standout_mode);
1987 CHECK_SGR(2, enter_underline_mode);
1988 CHECK_SGR(3, enter_reverse_mode);
1989 CHECK_SGR(4, enter_blink_mode);
1990 CHECK_SGR(5, enter_dim_mode);
1991 CHECK_SGR(6, enter_bold_mode);
1992 CHECK_SGR(7, enter_secure_mode);
1993 CHECK_SGR(8, enter_protected_mode);
1994 CHECK_SGR(9, enter_alt_charset_mode);
1997 _nc_warning("sgr(0) did not return a value");
1999 } else if (PRESENT(exit_attribute_mode) &&
2000 set_attributes != CANCELLED_STRING) {
2001 if (_nc_syntax == SYN_TERMINFO)
2002 _nc_warning("missing sgr string");
2005 if (PRESENT(exit_attribute_mode)) {
2006 char *check_sgr0 = _nc_trim_sgr0(tp);
2008 if (check_sgr0 == 0 || *check_sgr0 == '\0') {
2009 _nc_warning("trimmed sgr0 is empty");
2012 if (check_sgr0 != exit_attribute_mode) {
2014 ("will trim sgr0\n\toriginal sgr0=%s\n\ttrimmed sgr0=%s",
2015 _nc_visbuf2(1, exit_attribute_mode),
2016 _nc_visbuf2(2, check_sgr0)));
2020 ("will not trim sgr0\n\toriginal sgr0=%s",
2021 _nc_visbuf(exit_attribute_mode)));
2027 if (!auto_right_margin) {
2029 ("can write to lower-right directly"));
2030 } else if (PRESENT(enter_am_mode) && PRESENT(exit_am_mode)) {
2032 ("can write to lower-right by suppressing automargin"));
2033 } else if ((PRESENT(enter_insert_mode) && PRESENT(exit_insert_mode))
2034 || PRESENT(insert_character) || PRESENT(parm_ich)) {
2036 ("can write to lower-right by using inserts"));
2039 ("cannot write to lower-right"));
2044 * Some standard applications (e.g., vi) and some non-curses
2045 * applications (e.g., jove) get confused if we have both ich1 and
2046 * smir/rmir. Let's be nice and warn about that, too, even though
2047 * ncurses handles it.
2049 if ((PRESENT(enter_insert_mode) || PRESENT(exit_insert_mode))
2050 && PRESENT(parm_ich)) {
2051 _nc_warning("non-curses applications may be confused by ich1 with smir/rmir");
2055 * Finally, do the non-verbose checks
2057 if (save_check_termtype != 0)
2058 save_check_termtype(tp, literal);