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.160 2012/02/05 01:39:39 tom Exp $")
51 const char *_nc_progname = "tic";
55 static bool capdump = FALSE; /* running as infotocap? */
56 static bool infodump = FALSE; /* running as captoinfo? */
57 static bool showsummary = FALSE;
58 static const char *to_remove;
60 static void (*save_check_termtype) (TERMTYPE *, bool);
61 static void check_termtype(TERMTYPE *tt, bool);
63 static const char usage_string[] = "\
94 free_namelist(char **src)
98 for (n = 0; src[n] != 0; ++n)
106 cleanup(char **namelst GCC_UNUSED)
109 free_namelist(namelst);
113 if (to_remove != 0) {
123 failed(const char *msg)
126 cleanup((char **) 0);
127 ExitProgram(EXIT_FAILURE);
133 static const char *const tbl[] =
136 " -1 format translation output one capability per line",
138 " -a retain commented-out capabilities (sets -x also)",
140 " -K translate entries to termcap source form with BSD syntax",
141 " -C translate entries to termcap source form",
142 " -D print list of tic's database locations (first must be writable)",
143 " -c check only, validate input without compiling or translating",
144 " -e<names> translate/compile only entries named by comma-separated list",
145 " -f format complex strings for readability",
146 " -G format %{number} to %'char'",
147 " -g format %'char' to %{number}",
148 " -I translate entries to terminfo source form",
149 " -L translate entries to full terminfo source form",
150 " -N disable smart defaults for source translation",
151 " -o<dir> set output directory for compiled entry writes",
152 " -R<name> restrict translation to given terminfo/termcap version",
153 " -r force resolution of all use entries in source translation",
154 " -s print summary statistics",
155 " -T remove size-restrictions on compiled description",
157 " -t suppress commented-out capabilities",
159 " -U suppress post-processing of entries",
161 " -v[n] set verbosity level",
162 " -w[n] set format width for translation output",
164 " -x treat unknown capabilities as user-defined",
168 " <file> file to translate or compile"
172 fprintf(stderr, "Usage: %s %s\n", _nc_progname, usage_string);
173 for (j = 0; j < SIZEOF(tbl); j++) {
174 fputs(tbl[j], stderr);
177 ExitProgram(EXIT_FAILURE);
190 char result[MAX_ENTRY_SIZE];
193 * Look for strings that contain %{number}, convert them to %'char',
194 * which is shorter and runs a little faster.
196 for (n = 0; n < STRCOUNT; n++) {
197 s = ep->tterm.Strings[n];
199 && strchr(s, L_BRACE) != 0) {
202 while ((ch = *t++) != 0) {
206 } else if ((ch == '%')
207 && (*t == L_BRACE)) {
209 long value = strtol(t + 1, &v, 0);
213 && value != '\\' /* FIXME */
215 && isprint((int) value)) {
224 if (strlen(result) < strlen(s))
229 _nc_set_type(_nc_first_name(ep->tterm.term_names));
230 _nc_curr_line = (int) ep->startline;
231 _nc_write_entry(&ep->tterm);
235 immedhook(ENTRY * ep GCC_UNUSED)
236 /* write out entries with no use capabilities immediately to save storage */
240 * This is strictly a core-economy kluge. The really clean way to handle
241 * compilation is to slurp the whole file into core and then do all the
242 * name-collision checks and entry writes in one swell foop. But the
243 * terminfo master file is large enough that some core-poor systems swap
244 * like crazy when you compile it this way...there have been reports of
245 * this process taking *three hours*, rather than the twenty seconds or
246 * less typical on my development box.
248 * So. This hook *immediately* writes out the referenced entry if it
249 * has no use capabilities. The compiler main loop refrains from
250 * adding the entry to the in-core list when this hook fires. If some
251 * other entry later needs to reference an entry that got written
252 * immediately, that's OK; the resolution code will fetch it off disk
253 * when it can't find it in core.
255 * Name collisions will still be detected, just not as cleanly. The
256 * write_entry() code complains before overwriting an entry that
257 * postdates the time of tic's first call to write_entry(). Thus
258 * it will complain about overwriting entries newly made during the
259 * tic run, but not about overwriting ones that predate it.
261 * The reason this is a hook, and not in line with the rest of the
262 * compiler code, is that the support for termcap fallback cannot assume
263 * it has anywhere to spool out these entries!
265 * The _nc_set_type() call here requires a compensating one in
268 * If you define HAVE_BIG_CORE, you'll disable this kluge. This will
269 * make tic a bit faster (because the resolution code won't have to do
270 * disk I/O nearly as often).
272 if (ep->nuses == 0) {
273 int oldline = _nc_curr_line;
276 _nc_curr_line = oldline;
277 free(ep->tterm.str_table);
280 #endif /* HAVE_BIG_CORE */
286 /* emit a comment char, translating terminfo names to termcap names */
288 static bool in_name = FALSE;
289 static size_t have, used;
290 static char *namebuf, *suffix;
293 if (used + 1 >= have) {
295 namebuf = typeRealloc(char, have, namebuf);
296 suffix = typeRealloc(char, have, suffix);
298 if (c == '\n' || c == '@') {
299 namebuf[used++] = '\0';
301 (void) fputs(namebuf, stdout);
304 } else if (c != '>') {
305 namebuf[used++] = (char) c;
306 } else { /* ah! candidate name! */
308 NCURSES_CONST char *tp;
310 namebuf[used++] = '\0';
314 if ((up = strchr(namebuf, '#')) != 0
315 || (up = strchr(namebuf, '=')) != 0
316 || ((up = strchr(namebuf, '@')) != 0 && up[1] == '>')) {
317 (void) strcpy(suffix, up);
321 if ((tp = nametrans(namebuf)) != 0) {
323 (void) fputs(tp, stdout);
324 (void) fputs(suffix, stdout);
327 /* couldn't find a translation, just dump the name */
329 (void) fputs(namebuf, stdout);
330 (void) fputs(suffix, stdout);
344 /* Returns a string, stripped of leading/trailing whitespace */
350 while (isspace(UChar(*src)))
356 if ((dst = strdup(src)) == NULL) {
360 while (--len != 0 && isspace(UChar(dst[len])))
368 open_input(const char *filename)
370 FILE *fp = fopen(filename, "r");
374 fprintf(stderr, "%s: Can't open %s\n", _nc_progname, filename);
375 ExitProgram(EXIT_FAILURE);
377 if (fstat(fileno(fp), &sb) < 0
378 || (sb.st_mode & S_IFMT) != S_IFREG) {
379 fprintf(stderr, "%s: %s is not a file\n", _nc_progname, filename);
380 ExitProgram(EXIT_FAILURE);
385 /* Parse the "-e" option-value into a list of names */
387 make_namelist(char *src)
392 unsigned pass, n, nn;
397 } else if (strchr(src, '/') != 0) { /* a filename */
398 FILE *fp = open_input(src);
400 for (pass = 1; pass <= 2; pass++) {
402 while (fgets(buffer, sizeof(buffer), fp) != 0) {
403 if ((s = stripped(buffer)) != 0) {
412 dst = typeCalloc(char *, nn + 1);
417 } else { /* literal list of names */
418 for (pass = 1; pass <= 2; pass++) {
419 for (n = nn = 0, base = src;; n++) {
421 if (mark == ',' || mark == '\0') {
426 if ((s = stripped(base)) != 0)
435 dst = typeCalloc(char *, nn + 1);
438 if (showsummary && (dst != 0)) {
439 fprintf(log_fp, "Entries that will be compiled:\n");
440 for (n = 0; dst[n] != 0; n++)
441 fprintf(log_fp, "%u:%s\n", n + 1, dst[n]);
447 matches(char **needle, const char *haystack)
448 /* does entry in needle list match |-separated field in haystack? */
454 for (n = 0; needle[n] != 0; n++) {
455 if (_nc_name_match(haystack, needle[n], "|")) {
466 open_tempfile(char *name)
470 int fd = mkstemp(name);
472 result = fdopen(fd, "w");
474 if (tmpnam(name) != 0)
475 result = fopen(name, "w");
481 valid_db_path(const char *nominal)
485 char suffix[] = DBM_SUFFIX;
486 size_t need = strlen(nominal) + sizeof(suffix);
487 char *result = malloc(need);
489 strcpy(result, nominal);
490 if (strcmp(result + need - sizeof(suffix), suffix)) {
491 strcat(result, suffix);
494 char *result = strdup(nominal);
497 DEBUG(1, ("** stat(%s)", result));
498 if (stat(result, &sb) >= 0) {
500 if (!S_ISREG(sb.st_mode)
501 || access(result, R_OK | W_OK) != 0) {
502 DEBUG(1, ("...not a writable file"));
507 if (!S_ISDIR(sb.st_mode)
508 || access(result, R_OK | W_OK | X_OK) != 0) {
509 DEBUG(1, ("...not a writable directory"));
515 /* check if parent is directory and is writable */
516 unsigned leaf = _nc_pathlast(result);
518 DEBUG(1, ("...not found"));
520 char save = result[leaf];
522 if (stat(result, &sb) >= 0
523 && S_ISDIR(sb.st_mode)
524 && access(result, R_OK | W_OK | X_OK) == 0) {
527 DEBUG(1, ("...parent directory %s is not writable", result));
532 DEBUG(1, ("... no parent directory"));
541 * Show the databases to which tic could write. The location to which it
542 * writes is always the first one. If none are writable, print an error
546 show_databases(const char *outdir)
548 bool specific = (outdir != 0) || getenv("TERMINFO") != 0;
550 const char *tried = 0;
553 outdir = _nc_tic_dir(0);
555 if ((result = valid_db_path(outdir)) != 0) {
556 printf("%s\n", result);
561 if ((outdir = _nc_home_terminfo())) {
562 if ((result = valid_db_path(outdir)) != 0) {
563 printf("%s\n", result);
564 } else if (!specific) {
570 * If we can write in neither location, give an error message.
574 fprintf(stderr, "%s: %s (no permission)\n", _nc_progname, tried);
575 ExitProgram(EXIT_FAILURE);
579 #define VtoTrace(opt) (unsigned) ((opt > 0) ? opt : (opt == 0))
582 main(int argc, char *argv[])
584 char my_tmpname[PATH_MAX];
586 unsigned debug_level;
587 int smart_defaults = TRUE;
591 int this_opt, last_opt = '?';
593 int outform = F_TERMINFO; /* output format */
594 int sortmode = S_TERMINFO; /* sort_mode */
598 bool formatted = FALSE; /* reformat complex strings? */
599 bool literal = FALSE; /* suppress post-processing? */
600 int numbers = 0; /* format "%'char'" to/from "%{number}" */
601 bool forceresolve = FALSE; /* force resolution */
603 char *tversion = (char *) NULL;
604 const char *source_file = "terminfo";
606 char *outdir = (char *) NULL;
607 bool check_only = FALSE;
608 bool suppress_untranslatable = FALSE;
612 _nc_progname = _nc_rootname(argv[0]);
614 if ((infodump = same_program(_nc_progname, PROG_CAPTOINFO)) != FALSE) {
615 outform = F_TERMINFO;
616 sortmode = S_TERMINFO;
618 if ((capdump = same_program(_nc_progname, PROG_INFOTOCAP)) != FALSE) {
620 sortmode = S_TERMCAP;
623 use_extended_names(FALSE);
628 * Processing arguments is a little complicated, since someone made a
629 * design decision to allow the numeric values for -w, -v options to
632 while ((this_opt = getopt(argc, argv,
633 "0123456789CDIKLNR:TUVace:fGgo:rstvwx")) != -1) {
634 if (isdigit(this_opt)) {
637 v_opt = (v_opt * 10) + (this_opt - '0');
640 width = (width * 10) + (this_opt - '0');
662 /* the initial version of -K in 20110730 fell-thru here, but the
663 * same flag is useful when reading sources -TD
669 sortmode = S_TERMCAP;
672 debug_level = VtoTrace(v_opt);
673 set_trace_level(debug_level);
674 show_databases(outdir);
675 ExitProgram(EXIT_SUCCESS);
679 outform = F_TERMINFO;
680 sortmode = S_TERMINFO;
684 outform = F_VARIABLE;
685 sortmode = S_VARIABLE;
688 smart_defaults = FALSE;
701 puts(curses_version());
703 ExitProgram(EXIT_SUCCESS);
708 namelst = make_namelist(optarg);
736 _nc_disable_period = FALSE;
737 suppress_untranslatable = TRUE;
740 _nc_disable_period = TRUE;
743 use_extended_names(TRUE);
752 debug_level = VtoTrace(v_opt);
753 set_trace_level(debug_level);
756 save_check_termtype = _nc_check_termtype2;
757 _nc_check_termtype2 = check_termtype;
761 * Aaargh! immedhook seriously hoses us!
763 * One problem with immedhook is it means we can't do -e. Problem
764 * is that we can't guarantee that for each terminal listed, all the
765 * terminals it depends on will have been kept in core for reference
766 * resolution -- in fact it's certain the primitive types at the end
767 * of reference chains *won't* be in core unless they were explicitly
768 * in the select list themselves.
770 if (namelst && (!infodump && !capdump)) {
771 (void) fprintf(stderr,
772 "%s: Sorry, -e can't be used without -I or -C\n",
775 ExitProgram(EXIT_FAILURE);
777 #endif /* HAVE_BIG_CORE */
780 source_file = argv[optind++];
783 "%s: Too many file names. Usage:\n\t%s %s",
787 ExitProgram(EXIT_FAILURE);
790 if (infodump == TRUE) {
791 /* captoinfo's no-argument case */
792 source_file = "/etc/termcap";
793 if ((termcap = getenv("TERMCAP")) != 0
794 && (namelst = make_namelist(getenv("TERM"))) != 0) {
795 if (access(termcap, F_OK) == 0) {
797 source_file = termcap;
798 } else if ((tmp_fp = open_tempfile(strcpy(my_tmpname,
801 source_file = my_tmpname;
802 fprintf(tmp_fp, "%s\n", termcap);
804 tmp_fp = open_input(source_file);
805 to_remove = source_file;
813 "%s: File name needed. Usage:\n\t%s %s",
818 ExitProgram(EXIT_FAILURE);
823 tmp_fp = open_input(source_file);
830 sortmode, width, height, debug_level, formatted);
834 sortmode, width, height, debug_level, FALSE);
836 /* parse entries out of the source file */
837 _nc_set_source(source_file);
839 if (!(check_only || infodump || capdump))
840 _nc_set_writedir(outdir);
841 #endif /* HAVE_BIG_CORE */
842 _nc_read_entry_source(tmp_fp, (char *) NULL,
843 !smart_defaults || literal, FALSE,
844 ((check_only || infodump || capdump)
848 /* do use resolution */
849 if (check_only || (!infodump && !capdump) || forceresolve) {
850 if (!_nc_resolve_uses2(TRUE, literal) && !check_only) {
852 ExitProgram(EXIT_FAILURE);
857 if (check_only && (capdump || infodump)) {
859 if (matches(namelst, qp->tterm.term_names)) {
860 int len = fmt_entry(&qp->tterm, NULL, FALSE, TRUE, infodump, numbers);
862 if (len > (infodump ? MAX_TERMINFO_LENGTH : MAX_TERMCAP_LENGTH))
863 (void) fprintf(stderr,
864 "warning: resolved %s entry is %d bytes long\n",
865 _nc_first_name(qp->tterm.term_names),
871 /* write or dump all entries */
873 if (!infodump && !capdump) {
874 _nc_set_writedir(outdir);
876 if (matches(namelst, qp->tterm.term_names))
880 /* this is in case infotocap() generates warnings */
881 _nc_curr_col = _nc_curr_line = -1;
884 if (matches(namelst, qp->tterm.term_names)) {
885 long j = qp->cend - qp->cstart;
888 /* this is in case infotocap() generates warnings */
889 _nc_set_type(_nc_first_name(qp->tterm.term_names));
891 (void) fseek(tmp_fp, qp->cstart, SEEK_SET);
894 (void) putchar(fgetc(tmp_fp));
896 put_translate(fgetc(tmp_fp));
899 repair_acsc(&qp->tterm);
900 dump_entry(&qp->tterm, suppress_untranslatable,
901 limited, numbers, NULL);
902 for (j = 0; j < (long) qp->nuses; j++)
903 dump_uses(qp->uses[j].name, !capdump);
905 if (debug_level != 0 && !limited)
906 printf("# length=%d\n", len);
909 if (!namelst && _nc_tail) {
911 bool in_comment = FALSE;
912 bool trailing_comment = FALSE;
914 (void) fseek(tmp_fp, _nc_tail->cend, SEEK_SET);
915 while ((c = fgetc(tmp_fp)) != EOF) {
918 trailing_comment = TRUE;
925 && (in_comment || (oldc == '\n' && c == '\n')))
933 /* Show the directory into which entries were written, and the total
937 && (!(check_only || infodump || capdump))) {
938 int total = _nc_tic_written();
940 fprintf(log_fp, "%d entries written to %s\n",
942 _nc_tic_dir((char *) 0));
944 fprintf(log_fp, "No entries written\n");
947 ExitProgram(EXIT_SUCCESS);
951 * This bit of legerdemain turns all the terminfo variable names into
952 * references to locations in the arrays Booleans, Numbers, and Strings ---
953 * precisely what's needed (see comp_parse.c).
959 * Check if the alternate character-set capabilities are consistent.
962 check_acs(TERMTYPE *tp)
964 if (VALID_STRING(acs_chars)) {
965 const char *boxes = "lmkjtuvwqxn";
971 memset(mapped, 0, sizeof(mapped));
972 for (p = acs_chars; *p != '\0'; p += 2) {
974 _nc_warning("acsc has odd number of characters");
977 mapped[UChar(p[0])] = p[1];
980 if (mapped[UChar('I')] && !mapped[UChar('i')]) {
981 _nc_warning("acsc refers to 'I', which is probably an error");
984 for (p = boxes, q = missing; *p != '\0'; ++p) {
985 if (!mapped[UChar(p[0])]) {
991 assert(strlen(missing) <= strlen(boxes));
992 if (*missing != '\0' && strcmp(missing, boxes)) {
993 _nc_warning("acsc is missing some line-drawing mapping: %s", missing);
999 * Check if the color capabilities are consistent
1002 check_colors(TERMTYPE *tp)
1004 if ((max_colors > 0) != (max_pairs > 0)
1005 || ((max_colors > max_pairs) && (initialize_pair == 0)))
1006 _nc_warning("inconsistent values for max_colors (%d) and max_pairs (%d)",
1007 max_colors, max_pairs);
1009 PAIRED(set_foreground, set_background);
1010 PAIRED(set_a_foreground, set_a_background);
1011 PAIRED(set_color_pair, initialize_pair);
1013 if (VALID_STRING(set_foreground)
1014 && VALID_STRING(set_a_foreground)
1015 && !_nc_capcmp(set_foreground, set_a_foreground))
1016 _nc_warning("expected setf/setaf to be different");
1018 if (VALID_STRING(set_background)
1019 && VALID_STRING(set_a_background)
1020 && !_nc_capcmp(set_background, set_a_background))
1021 _nc_warning("expected setb/setab to be different");
1023 /* see: has_colors() */
1024 if (VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
1025 && (((set_foreground != NULL)
1026 && (set_background != NULL))
1027 || ((set_a_foreground != NULL)
1028 && (set_a_background != NULL))
1029 || set_color_pair)) {
1030 if (!VALID_STRING(orig_pair) && !VALID_STRING(orig_colors))
1031 _nc_warning("expected either op/oc string for resetting colors");
1036 keypad_final(const char *string)
1040 if (VALID_STRING(string)
1041 && *string++ == '\033'
1043 && strlen(string) == 1) {
1051 keypad_index(const char *string)
1054 const char *list = "PQRSwxymtuvlqrsPpn"; /* app-keypad except "Enter" */
1058 if ((ch = keypad_final(string)) != '\0') {
1059 test = strchr(list, ch);
1061 result = (long) (test - list);
1067 * list[] is down, up, left, right
1068 * "left" may be ^H rather than \E[D
1069 * "down" may be ^J rather than \E[B
1070 * But up/right are generally consistently escape sequences for ANSI terminals.
1073 check_ansi_cursor(char *list[4])
1080 bool repeated = FALSE;
1082 for (j = 0; j < 4; ++j) {
1084 for (k = 0; k < j; ++k) {
1086 && !strcmp(list[j], list[k])) {
1087 char *value = _nc_tic_expand(list[k], TRUE, 0);
1088 _nc_warning("repeated cursor control %s\n", value);
1096 if (UChar(up[0]) == '\033') {
1102 } else if (UChar(up[0]) == UChar('\233')) {
1107 while (up[suffix] && isdigit(UChar(up[suffix])))
1110 if (prefix && up[suffix] == 'A') {
1112 if (!strcmp(list[0], "\n"))
1114 if (!strcmp(list[2], "\b"))
1117 for (j = 0; j < 4; ++j) {
1118 if (skip[j] || strlen(list[j]) == 1)
1120 if (memcmp(list[j], up, prefix)) {
1121 char *value = _nc_tic_expand(list[j], TRUE, 0);
1122 _nc_warning("inconsistent prefix for %s\n", value);
1125 if (strlen(list[j]) < suffix) {
1126 char *value = _nc_tic_expand(list[j], TRUE, 0);
1127 _nc_warning("inconsistent length for %s, expected %d\n",
1128 value, (int) suffix + 1);
1132 if (list[j][suffix] != want) {
1133 char *value = _nc_tic_expand(list[j], TRUE, 0);
1134 _nc_warning("inconsistent suffix for %s, expected %c, have %c\n",
1135 value, want, list[j][suffix]);
1142 #define EXPECTED(name) if (!PRESENT(name)) _nc_warning("expected " #name)
1145 check_cursor(TERMTYPE *tp)
1150 /* if we have a parameterized form, then the non-parameterized is easy */
1151 ANDMISSING(parm_down_cursor, cursor_down);
1152 ANDMISSING(parm_up_cursor, cursor_up);
1153 ANDMISSING(parm_left_cursor, cursor_left);
1154 ANDMISSING(parm_right_cursor, cursor_right);
1156 /* Given any of a set of cursor movement, the whole set should be present.
1157 * Technically this is not true (we could use cursor_address to fill in
1158 * unsupported controls), but it is likely.
1161 if (PRESENT(parm_down_cursor)) {
1162 list[count++] = parm_down_cursor;
1164 if (PRESENT(parm_up_cursor)) {
1165 list[count++] = parm_up_cursor;
1167 if (PRESENT(parm_left_cursor)) {
1168 list[count++] = parm_left_cursor;
1170 if (PRESENT(parm_right_cursor)) {
1171 list[count++] = parm_right_cursor;
1174 check_ansi_cursor(list);
1175 } else if (count != 0) {
1176 EXPECTED(parm_down_cursor);
1177 EXPECTED(parm_up_cursor);
1178 EXPECTED(parm_left_cursor);
1179 EXPECTED(parm_right_cursor);
1183 if (PRESENT(cursor_down)) {
1184 list[count++] = cursor_down;
1186 if (PRESENT(cursor_up)) {
1187 list[count++] = cursor_up;
1189 if (PRESENT(cursor_left)) {
1190 list[count++] = cursor_left;
1192 if (PRESENT(cursor_right)) {
1193 list[count++] = cursor_right;
1196 check_ansi_cursor(list);
1197 } else if (count != 0) {
1199 if (PRESENT(cursor_down) && strcmp(cursor_down, "\n"))
1201 if (PRESENT(cursor_left) && strcmp(cursor_left, "\b"))
1203 if (PRESENT(cursor_up) && strlen(cursor_up) > 1)
1205 if (PRESENT(cursor_right) && strlen(cursor_right) > 1)
1208 EXPECTED(cursor_down);
1209 EXPECTED(cursor_up);
1210 EXPECTED(cursor_left);
1211 EXPECTED(cursor_right);
1218 * Do a quick sanity-check for vt100-style keypads to see if the 5-key keypad
1219 * is mapped inconsistently.
1222 check_keypad(TERMTYPE *tp)
1226 if (VALID_STRING(key_a1) &&
1227 VALID_STRING(key_a3) &&
1228 VALID_STRING(key_b2) &&
1229 VALID_STRING(key_c1) &&
1230 VALID_STRING(key_c3)) {
1231 char final[MAX_KP + 1];
1238 final[0] = keypad_final(key_a1);
1239 final[1] = keypad_final(key_a3);
1240 final[2] = keypad_final(key_b2);
1241 final[3] = keypad_final(key_c1);
1242 final[4] = keypad_final(key_c3);
1245 /* special case: legacy coding using 1,2,3,0,. on the bottom */
1246 assert(strlen(final) <= MAX_KP);
1247 if (!strcmp(final, "qsrpn"))
1250 list[0] = keypad_index(key_a1);
1251 list[1] = keypad_index(key_a3);
1252 list[2] = keypad_index(key_b2);
1253 list[3] = keypad_index(key_c1);
1254 list[4] = keypad_index(key_c3);
1256 /* check that they're all vt100 keys */
1257 for (j = 0; j < MAX_KP; ++j) {
1263 /* check if they're all in increasing order */
1264 for (j = 1; j < MAX_KP; ++j) {
1265 if (list[j] > list[j - 1]) {
1269 if (increase != (MAX_KP - 1)) {
1272 for (j = 0, last = -1; j < MAX_KP; ++j) {
1273 for (k = 0, kk = -1, test = 100; k < 5; ++k) {
1274 if (list[k] > last &&
1281 assert(strlen(show) < (MAX_KP * 4));
1284 strcat(show, " ka1");
1287 strcat(show, " ka3");
1290 strcat(show, " kb2");
1293 strcat(show, " kc1");
1296 strcat(show, " kc3");
1301 _nc_warning("vt100 keypad order inconsistent: %s", show);
1304 } else if (VALID_STRING(key_a1) ||
1305 VALID_STRING(key_a3) ||
1306 VALID_STRING(key_b2) ||
1307 VALID_STRING(key_c1) ||
1308 VALID_STRING(key_c3)) {
1310 if (keypad_index(key_a1) >= 0)
1311 strcat(show, " ka1");
1312 if (keypad_index(key_a3) >= 0)
1313 strcat(show, " ka3");
1314 if (keypad_index(key_b2) >= 0)
1315 strcat(show, " kb2");
1316 if (keypad_index(key_c1) >= 0)
1317 strcat(show, " kc1");
1318 if (keypad_index(key_c3) >= 0)
1319 strcat(show, " kc3");
1321 _nc_warning("vt100 keypad map incomplete:%s", show);
1326 check_printer(TERMTYPE *tp)
1328 PAIRED(enter_doublewide_mode, exit_doublewide_mode);
1329 PAIRED(enter_italics_mode, exit_italics_mode);
1330 PAIRED(enter_leftward_mode, exit_leftward_mode);
1331 PAIRED(enter_micro_mode, exit_micro_mode);
1332 PAIRED(enter_shadow_mode, exit_shadow_mode);
1333 PAIRED(enter_subscript_mode, exit_subscript_mode);
1334 PAIRED(enter_superscript_mode, exit_superscript_mode);
1335 PAIRED(enter_upward_mode, exit_upward_mode);
1337 ANDMISSING(start_char_set_def, stop_char_set_def);
1339 /* if we have a parameterized form, then the non-parameterized is easy */
1340 ANDMISSING(set_bottom_margin_parm, set_bottom_margin);
1341 ANDMISSING(set_left_margin_parm, set_left_margin);
1342 ANDMISSING(set_right_margin_parm, set_right_margin);
1343 ANDMISSING(set_top_margin_parm, set_top_margin);
1345 ANDMISSING(parm_down_micro, micro_down);
1346 ANDMISSING(parm_left_micro, micro_left);
1347 ANDMISSING(parm_right_micro, micro_right);
1348 ANDMISSING(parm_up_micro, micro_up);
1352 * Returns the expected number of parameters for the given capability.
1355 expected_params(const char *name)
1358 static const struct {
1362 { "S0", 1 }, /* 'screen' extension */
1415 { "smgbp", 1 }, /* 2 if smgtp is not given */
1430 int result = 0; /* function-keys, etc., use none */
1432 for (n = 0; n < SIZEOF(table); n++) {
1433 if (!strcmp(name, table[n].name)) {
1434 result = table[n].count;
1443 * Make a quick sanity check for the parameters which are used in the given
1444 * strings. If there are no "%p" tokens, then there should be no other "%"
1448 check_params(TERMTYPE *tp, const char *name, char *value)
1450 int expected = expected_params(name);
1456 #ifdef set_top_margin_parm
1457 if (!strcmp(name, "smgbp")
1458 && set_top_margin_parm == 0)
1462 for (n = 0; n < 10; n++)
1468 _nc_warning("expected character after %% in %s", name);
1470 } else if (*s == 'p') {
1471 if (*++s == '\0' || !isdigit((int) *s)) {
1472 _nc_warning("expected digit after %%p in %s", name);
1486 _nc_warning("%s refers to parameter 0 (%%p0), which is not allowed", name);
1488 if (value == set_attributes || expected < 0) {
1490 } else if (expected != actual) {
1491 _nc_warning("%s uses %d parameters, expected %d", name,
1493 for (n = 1; n < actual; n++) {
1495 _nc_warning("%s omits parameter %d", name, n);
1503 while (*s == '/' || isdigit(UChar(*s)))
1509 * Skip a delay altogether, e.g., when comparing a simple string to sgr,
1510 * the latter may have a worst-case delay on the end.
1513 ignore_delays(char *s)
1546 * An sgr string may contain several settings other than the one we're
1547 * interested in, essentially sgr0 + rmacs + whatever. As long as the
1548 * "whatever" is contained in the sgr string, that is close enough for our
1552 similar_sgr(int num, char *a, char *b)
1554 static const char *names[] =
1576 _nc_warning("Did not find delay %s", _nc_visbuf(b));
1578 _nc_warning("checking sgr(%s) %s\n\tcompare to %s\n\tunmatched %s",
1579 names[num], _nc_visbuf2(1, base_a),
1580 _nc_visbuf2(2, base_b),
1584 } else if (delaying) {
1587 } else if ((*b == '0' || (*b == ';')) && *a == 'm') {
1609 /* ignore delays on the end of the string */
1610 a = ignore_delays(a);
1611 return ((num != 0) || (*a == 0));
1615 check_sgr(TERMTYPE *tp, char *zero, int num, char *cap, const char *name)
1620 test = TPARM_9(set_attributes,
1632 if (!similar_sgr(num, test, cap)) {
1633 _nc_warning("%s differs from sgr(%d)\n\t%s=%s\n\tsgr(%d)=%s",
1635 name, _nc_visbuf2(1, cap),
1636 num, _nc_visbuf2(2, test));
1638 } else if (_nc_capcmp(test, zero)) {
1639 _nc_warning("sgr(%d) present, but not %s", num, name);
1641 } else if (PRESENT(cap)) {
1642 _nc_warning("sgr(%d) missing, but %s present", num, name);
1645 _nc_warning("stack error in sgr(%d) string", num);
1649 #define CHECK_SGR(num,name) check_sgr(tp, zero, num, name, #name)
1653 * If tic is compiled with TRACE, we'll be able to see the output from the
1654 * DEBUG() macro. But since it doesn't use traceon(), it always goes to
1655 * the standard error. Use this function to make it simpler to follow the
1656 * resulting debug traces.
1659 show_where(unsigned level)
1661 if (_nc_tracing >= DEBUG_LEVEL(level)) {
1663 _nc_get_type(my_name);
1664 _tracef("\"%s\", line %d, '%s'",
1666 _nc_curr_line, my_name);
1671 #define show_where(level) /* nothing */
1674 /* other sanity-checks (things that we don't want in the normal
1675 * logic that reads a terminfo entry)
1678 check_termtype(TERMTYPE *tp, bool literal)
1680 bool conflict = FALSE;
1682 char fkeys[STRCOUNT];
1685 * A terminal entry may contain more than one keycode assigned to
1686 * a given string (e.g., KEY_END and KEY_LL). But curses will only
1687 * return one (the last one assigned).
1689 if (!(_nc_syntax == SYN_TERMCAP && capdump)) {
1690 memset(fkeys, 0, sizeof(fkeys));
1691 for (j = 0; _nc_tinfo_fkeys[j].code; j++) {
1692 char *a = tp->Strings[_nc_tinfo_fkeys[j].offset];
1694 if (!VALID_STRING(a))
1696 for (k = j + 1; _nc_tinfo_fkeys[k].code; k++) {
1697 char *b = tp->Strings[_nc_tinfo_fkeys[k].offset];
1698 if (!VALID_STRING(b)
1701 if (!_nc_capcmp(a, b)) {
1706 _nc_warning("Conflicting key definitions (using the last)");
1709 fprintf(stderr, "... %s is the same as %s",
1710 keyname((int) _nc_tinfo_fkeys[j].code),
1711 keyname((int) _nc_tinfo_fkeys[k].code));
1714 fprintf(stderr, ", %s",
1715 keyname((int) _nc_tinfo_fkeys[k].code));
1720 fprintf(stderr, "\n");
1724 for (j = 0; j < NUM_STRINGS(tp); j++) {
1725 char *a = tp->Strings[j];
1726 if (VALID_STRING(a))
1727 check_params(tp, ExtStrname(tp, (int) j, strnames), a);
1737 * These may be mismatched because the terminal description relies on
1738 * restoring the cursor visibility by resetting it.
1740 ANDMISSING(cursor_invisible, cursor_normal);
1741 ANDMISSING(cursor_visible, cursor_normal);
1743 if (PRESENT(cursor_visible) && PRESENT(cursor_normal)
1744 && !_nc_capcmp(cursor_visible, cursor_normal))
1745 _nc_warning("cursor_visible is same as cursor_normal");
1748 * From XSI & O'Reilly, we gather that sc/rc are required if csr is
1749 * given, because the cursor position after the scrolling operation is
1750 * performed is undefined.
1752 ANDMISSING(change_scroll_region, save_cursor);
1753 ANDMISSING(change_scroll_region, restore_cursor);
1756 * If we can clear tabs, we should be able to initialize them.
1758 ANDMISSING(clear_all_tabs, set_tab);
1760 if (PRESENT(set_attributes)) {
1764 if (PRESENT(exit_attribute_mode)) {
1765 zero = strdup(CHECK_SGR(0, exit_attribute_mode));
1767 zero = strdup(TPARM_9(set_attributes, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1770 _nc_warning("stack error in sgr(0) string");
1773 CHECK_SGR(1, enter_standout_mode);
1774 CHECK_SGR(2, enter_underline_mode);
1775 CHECK_SGR(3, enter_reverse_mode);
1776 CHECK_SGR(4, enter_blink_mode);
1777 CHECK_SGR(5, enter_dim_mode);
1778 CHECK_SGR(6, enter_bold_mode);
1779 CHECK_SGR(7, enter_secure_mode);
1780 CHECK_SGR(8, enter_protected_mode);
1781 CHECK_SGR(9, enter_alt_charset_mode);
1784 _nc_warning("sgr(0) did not return a value");
1786 } else if (PRESENT(exit_attribute_mode) &&
1787 set_attributes != CANCELLED_STRING) {
1788 if (_nc_syntax == SYN_TERMINFO)
1789 _nc_warning("missing sgr string");
1792 if (PRESENT(exit_attribute_mode)) {
1793 char *check_sgr0 = _nc_trim_sgr0(tp);
1795 if (check_sgr0 == 0 || *check_sgr0 == '\0') {
1796 _nc_warning("trimmed sgr0 is empty");
1799 if (check_sgr0 != exit_attribute_mode) {
1801 ("will trim sgr0\n\toriginal sgr0=%s\n\ttrimmed sgr0=%s",
1802 _nc_visbuf2(1, exit_attribute_mode),
1803 _nc_visbuf2(2, check_sgr0)));
1807 ("will not trim sgr0\n\toriginal sgr0=%s",
1808 _nc_visbuf(exit_attribute_mode)));
1814 if (!auto_right_margin) {
1816 ("can write to lower-right directly"));
1817 } else if (PRESENT(enter_am_mode) && PRESENT(exit_am_mode)) {
1819 ("can write to lower-right by suppressing automargin"));
1820 } else if ((PRESENT(enter_insert_mode) && PRESENT(exit_insert_mode))
1821 || PRESENT(insert_character) || PRESENT(parm_ich)) {
1823 ("can write to lower-right by using inserts"));
1826 ("cannot write to lower-right"));
1831 * Some standard applications (e.g., vi) and some non-curses
1832 * applications (e.g., jove) get confused if we have both ich1 and
1833 * smir/rmir. Let's be nice and warn about that, too, even though
1834 * ncurses handles it.
1836 if ((PRESENT(enter_insert_mode) || PRESENT(exit_insert_mode))
1837 && PRESENT(parm_ich)) {
1838 _nc_warning("non-curses applications may be confused by ich1 with smir/rmir");
1842 * Finally, do the non-verbose checks
1844 if (save_check_termtype != 0)
1845 save_check_termtype(tp, literal);