1 /****************************************************************************
2 * Copyright (c) 1998-2012,2013 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.189 2013/11/16 19:58:09 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 = (int) umask(077);
384 int fd = mkstemp(filename);
386 result = fdopen(fd, "w");
387 umask((mode_t) oldmask);
390 if (tmpnam(filename) != 0)
391 result = fopen(filename, "w");
397 copy_input(FILE *source, const char *filename, char *alt_file)
399 char my_altfile[PATH_MAX];
405 alt_file = my_altfile;
408 failed("copy_input (source)");
409 } else if ((target = open_tempfile(alt_file)) == 0) {
410 failed("copy_input (target)");
417 } else if (ferror(source)) {
419 } else if (ch == 0) {
420 /* don't loop in case someone wants to convert /dev/zero */
421 fprintf(stderr, "%s: %s is not a text-file\n", _nc_progname, filename);
422 ExitProgram(EXIT_FAILURE);
428 * rewind() does not force the target file's data to disk (not does
429 * fflush()...). So open a second stream on the data and then close
430 * the one that we were writing on before starting to read from the
433 result = fopen(alt_file, "r+");
435 to_remove = strdup(alt_file);
441 open_input(const char *filename, char *alt_file)
447 if (!strcmp(filename, "-")) {
448 fp = copy_input(stdin, STDIN_NAME, alt_file);
449 } else if (stat(filename, &sb) < 0) {
450 fprintf(stderr, "%s: %s %s\n", _nc_progname, filename, strerror(errno));
451 ExitProgram(EXIT_FAILURE);
452 } else if ((mode = (sb.st_mode & S_IFMT)) == S_IFDIR
453 || (mode != S_IFREG && mode != S_IFCHR)) {
454 fprintf(stderr, "%s: %s is not a file\n", _nc_progname, filename);
455 ExitProgram(EXIT_FAILURE);
457 fp = fopen(filename, "r");
460 fprintf(stderr, "%s: Can't open %s\n", _nc_progname, filename);
461 ExitProgram(EXIT_FAILURE);
463 if (mode != S_IFREG) {
465 FILE *fp2 = copy_input(fp, filename, alt_file);
468 fprintf(stderr, "%s: %s is not a file\n", _nc_progname, filename);
469 ExitProgram(EXIT_FAILURE);
476 /* Parse the "-e" option-value into a list of names */
478 make_namelist(char *src)
483 unsigned pass, n, nn;
488 } else if (strchr(src, '/') != 0) { /* a filename */
489 FILE *fp = open_input(src, (char *) 0);
491 for (pass = 1; pass <= 2; pass++) {
493 while (fgets(buffer, sizeof(buffer), fp) != 0) {
494 if ((s = stripped(buffer)) != 0) {
503 if ((dst = typeCalloc(char *, nn + 1)) == 0)
504 failed("make_namelist");
509 } else { /* literal list of names */
510 for (pass = 1; pass <= 2; pass++) {
511 for (n = nn = 0, base = src;; n++) {
513 if (mark == ',' || mark == '\0') {
518 if ((s = stripped(base)) != 0)
527 if ((dst = typeCalloc(char *, nn + 1)) == 0)
528 failed("make_namelist");
532 if (showsummary && (dst != 0)) {
533 fprintf(log_fp, "Entries that will be compiled:\n");
534 for (n = 0; dst[n] != 0; n++)
535 fprintf(log_fp, "%u:%s\n", n + 1, dst[n]);
541 matches(char **needle, const char *haystack)
542 /* does entry in needle list match |-separated field in haystack? */
548 for (n = 0; needle[n] != 0; n++) {
549 if (_nc_name_match(haystack, needle[n], "|")) {
560 valid_db_path(const char *nominal)
564 char suffix[] = DBM_SUFFIX;
565 size_t need = strlen(nominal) + sizeof(suffix);
566 char *result = malloc(need);
569 failed("valid_db_path");
570 _nc_STRCPY(result, nominal, need);
571 if (strcmp(result + need - sizeof(suffix), suffix)) {
572 _nc_STRCAT(result, suffix, need);
575 char *result = strdup(nominal);
578 DEBUG(1, ("** stat(%s)", result));
579 if (stat(result, &sb) >= 0) {
581 if (!S_ISREG(sb.st_mode)
582 || access(result, R_OK | W_OK) != 0) {
583 DEBUG(1, ("...not a writable file"));
588 if (!S_ISDIR(sb.st_mode)
589 || access(result, R_OK | W_OK | X_OK) != 0) {
590 DEBUG(1, ("...not a writable directory"));
596 /* check if parent is directory and is writable */
597 unsigned leaf = _nc_pathlast(result);
599 DEBUG(1, ("...not found"));
601 char save = result[leaf];
603 if (stat(result, &sb) >= 0
604 && S_ISDIR(sb.st_mode)
605 && access(result, R_OK | W_OK | X_OK) == 0) {
608 DEBUG(1, ("...parent directory %s is not writable", result));
613 DEBUG(1, ("... no parent directory"));
622 * Show the databases to which tic could write. The location to which it
623 * writes is always the first one. If none are writable, print an error
627 show_databases(const char *outdir)
629 bool specific = (outdir != 0) || getenv("TERMINFO") != 0;
631 const char *tried = 0;
634 outdir = _nc_tic_dir(0);
636 if ((result = valid_db_path(outdir)) != 0) {
637 printf("%s\n", result);
643 if ((outdir = _nc_home_terminfo())) {
644 if ((result = valid_db_path(outdir)) != 0) {
645 printf("%s\n", result);
647 } else if (!specific) {
653 * If we can write in neither location, give an error message.
657 fprintf(stderr, "%s: %s (no permission)\n", _nc_progname, tried);
658 ExitProgram(EXIT_FAILURE);
662 #define VtoTrace(opt) (unsigned) ((opt > 0) ? opt : (opt == 0))
665 main(int argc, char *argv[])
667 char my_tmpname[PATH_MAX];
668 char my_altfile[PATH_MAX];
670 unsigned debug_level;
671 int smart_defaults = TRUE;
675 int this_opt, last_opt = '?';
677 int outform = F_TERMINFO; /* output format */
678 int sortmode = S_TERMINFO; /* sort_mode */
682 bool formatted = FALSE; /* reformat complex strings? */
683 bool literal = FALSE; /* suppress post-processing? */
684 int numbers = 0; /* format "%'char'" to/from "%{number}" */
685 bool forceresolve = FALSE; /* force resolution */
687 char *tversion = (char *) NULL;
688 const char *source_file = "terminfo";
689 char *outdir = (char *) NULL;
690 bool check_only = FALSE;
691 bool suppress_untranslatable = FALSE;
695 _nc_progname = _nc_rootname(argv[0]);
698 if ((infodump = same_program(_nc_progname, PROG_CAPTOINFO)) != FALSE) {
699 outform = F_TERMINFO;
700 sortmode = S_TERMINFO;
702 if ((capdump = same_program(_nc_progname, PROG_INFOTOCAP)) != FALSE) {
704 sortmode = S_TERMCAP;
707 use_extended_names(FALSE);
712 * Processing arguments is a little complicated, since someone made a
713 * design decision to allow the numeric values for -w, -v options to
716 while ((this_opt = getopt(argc, argv,
717 "0123456789CDIKLNR:TUVace:fGgo:rstvwx")) != -1) {
718 if (isdigit(this_opt)) {
721 v_opt = (v_opt * 10) + (this_opt - '0');
724 width = (width * 10) + (this_opt - '0');
746 /* the initial version of -K in 20110730 fell-thru here, but the
747 * same flag is useful when reading sources -TD
753 sortmode = S_TERMCAP;
756 debug_level = VtoTrace(v_opt);
757 set_trace_level(debug_level);
758 show_databases(outdir);
759 ExitProgram(EXIT_SUCCESS);
763 outform = F_TERMINFO;
764 sortmode = S_TERMINFO;
768 outform = F_VARIABLE;
769 sortmode = S_VARIABLE;
772 smart_defaults = FALSE;
785 puts(curses_version());
786 ExitProgram(EXIT_SUCCESS);
791 namelst = make_namelist(optarg);
819 _nc_disable_period = FALSE;
820 suppress_untranslatable = TRUE;
823 _nc_disable_period = TRUE;
826 use_extended_names(TRUE);
835 debug_level = VtoTrace(v_opt);
836 set_trace_level(debug_level);
839 save_check_termtype = _nc_check_termtype2;
840 _nc_check_termtype2 = check_termtype;
844 * Aaargh! immedhook seriously hoses us!
846 * One problem with immedhook is it means we can't do -e. Problem
847 * is that we can't guarantee that for each terminal listed, all the
848 * terminals it depends on will have been kept in core for reference
849 * resolution -- in fact it's certain the primitive types at the end
850 * of reference chains *won't* be in core unless they were explicitly
851 * in the select list themselves.
853 if (namelst && (!infodump && !capdump)) {
854 (void) fprintf(stderr,
855 "%s: Sorry, -e can't be used without -I or -C\n",
857 ExitProgram(EXIT_FAILURE);
859 #endif /* HAVE_BIG_CORE */
862 source_file = argv[optind++];
865 "%s: Too many file names. Usage:\n\t%s %s",
869 ExitProgram(EXIT_FAILURE);
872 if (infodump == TRUE) {
873 /* captoinfo's no-argument case */
874 source_file = "/etc/termcap";
875 if ((termcap = getenv("TERMCAP")) != 0
876 && (namelst = make_namelist(getenv("TERM"))) != 0) {
877 if (access(termcap, F_OK) == 0) {
879 source_file = termcap;
881 if ((tmp_fp = open_tempfile(my_tmpname)) != 0) {
882 source_file = my_tmpname;
883 fprintf(tmp_fp, "%s\n", termcap);
885 tmp_fp = open_input(source_file, (char *) 0);
886 to_remove = source_file;
895 "%s: File name needed. Usage:\n\t%s %s",
899 ExitProgram(EXIT_FAILURE);
904 tmp_fp = open_input(source_file, my_altfile);
905 if (!strcmp(source_file, "-")) {
906 source_file = STDIN_NAME;
915 sortmode, width, height, debug_level, formatted);
916 } else if (capdump) {
919 sortmode, width, height, debug_level, FALSE);
922 /* parse entries out of the source file */
923 _nc_set_source(source_file);
925 if (!(check_only || infodump || capdump))
926 _nc_set_writedir(outdir);
927 #endif /* HAVE_BIG_CORE */
928 _nc_read_entry_source(tmp_fp, (char *) NULL,
929 !smart_defaults || literal, FALSE,
930 ((check_only || infodump || capdump)
934 /* do use resolution */
935 if (check_only || (!infodump && !capdump) || forceresolve) {
936 if (!_nc_resolve_uses2(TRUE, literal) && !check_only) {
937 ExitProgram(EXIT_FAILURE);
942 if (check_only && (capdump || infodump)) {
944 if (matches(namelst, qp->tterm.term_names)) {
945 int len = fmt_entry(&qp->tterm, NULL, FALSE, TRUE, infodump, numbers);
947 if (len > (infodump ? MAX_TERMINFO_LENGTH : MAX_TERMCAP_LENGTH))
948 (void) fprintf(stderr,
949 "warning: resolved %s entry is %d bytes long\n",
950 _nc_first_name(qp->tterm.term_names),
956 /* write or dump all entries */
958 if (!infodump && !capdump) {
959 _nc_set_writedir(outdir);
961 if (matches(namelst, qp->tterm.term_names))
965 /* this is in case infotocap() generates warnings */
966 _nc_curr_col = _nc_curr_line = -1;
969 if (matches(namelst, qp->tterm.term_names)) {
970 long j = qp->cend - qp->cstart;
973 /* this is in case infotocap() generates warnings */
974 _nc_set_type(_nc_first_name(qp->tterm.term_names));
976 (void) fseek(tmp_fp, qp->cstart, SEEK_SET);
979 (void) putchar(fgetc(tmp_fp));
981 put_translate(fgetc(tmp_fp));
984 repair_acsc(&qp->tterm);
985 dump_entry(&qp->tterm, suppress_untranslatable,
986 limited, numbers, NULL);
987 for (j = 0; j < (long) qp->nuses; j++)
988 dump_uses(qp->uses[j].name, !capdump);
990 if (debug_level != 0 && !limited)
991 printf("# length=%d\n", len);
994 if (!namelst && _nc_tail) {
996 bool in_comment = FALSE;
997 bool trailing_comment = FALSE;
999 (void) fseek(tmp_fp, _nc_tail->cend, SEEK_SET);
1000 while ((c = fgetc(tmp_fp)) != EOF) {
1003 trailing_comment = TRUE;
1009 if (trailing_comment
1010 && (in_comment || (oldc == '\n' && c == '\n')))
1018 /* Show the directory into which entries were written, and the total
1022 && (!(check_only || infodump || capdump))) {
1023 int total = _nc_tic_written();
1025 fprintf(log_fp, "%d entries written to %s\n",
1027 _nc_tic_dir((char *) 0));
1029 fprintf(log_fp, "No entries written\n");
1031 ExitProgram(EXIT_SUCCESS);
1035 * This bit of legerdemain turns all the terminfo variable names into
1036 * references to locations in the arrays Booleans, Numbers, and Strings ---
1037 * precisely what's needed (see comp_parse.c).
1043 * Check if the alternate character-set capabilities are consistent.
1046 check_acs(TERMTYPE *tp)
1048 if (VALID_STRING(acs_chars)) {
1049 const char *boxes = "lmkjtuvwqxn";
1055 memset(mapped, 0, sizeof(mapped));
1056 for (p = acs_chars; *p != '\0'; p += 2) {
1058 _nc_warning("acsc has odd number of characters");
1061 mapped[UChar(p[0])] = p[1];
1064 if (mapped[UChar('I')] && !mapped[UChar('i')]) {
1065 _nc_warning("acsc refers to 'I', which is probably an error");
1068 for (p = boxes, q = missing; *p != '\0'; ++p) {
1069 if (!mapped[UChar(p[0])]) {
1075 assert(strlen(missing) <= strlen(boxes));
1076 if (*missing != '\0' && strcmp(missing, boxes)) {
1077 _nc_warning("acsc is missing some line-drawing mapping: %s", missing);
1083 * Check if the color capabilities are consistent
1086 check_colors(TERMTYPE *tp)
1088 if ((max_colors > 0) != (max_pairs > 0)
1089 || ((max_colors > max_pairs) && (initialize_pair == 0)))
1090 _nc_warning("inconsistent values for max_colors (%d) and max_pairs (%d)",
1091 max_colors, max_pairs);
1093 PAIRED(set_foreground, set_background);
1094 PAIRED(set_a_foreground, set_a_background);
1095 PAIRED(set_color_pair, initialize_pair);
1097 if (VALID_STRING(set_foreground)
1098 && VALID_STRING(set_a_foreground)
1099 && !_nc_capcmp(set_foreground, set_a_foreground))
1100 _nc_warning("expected setf/setaf to be different");
1102 if (VALID_STRING(set_background)
1103 && VALID_STRING(set_a_background)
1104 && !_nc_capcmp(set_background, set_a_background))
1105 _nc_warning("expected setb/setab to be different");
1107 /* see: has_colors() */
1108 if (VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
1109 && (((set_foreground != NULL)
1110 && (set_background != NULL))
1111 || ((set_a_foreground != NULL)
1112 && (set_a_background != NULL))
1113 || set_color_pair)) {
1114 if (!VALID_STRING(orig_pair) && !VALID_STRING(orig_colors))
1115 _nc_warning("expected either op/oc string for resetting colors");
1120 keypad_final(const char *string)
1124 if (VALID_STRING(string)
1125 && *string++ == '\033'
1127 && strlen(string) == 1) {
1135 keypad_index(const char *string)
1138 const char *list = "PQRSwxymtuvlqrsPpn"; /* app-keypad except "Enter" */
1142 if ((ch = keypad_final(string)) != '\0') {
1143 test = (strchr) (list, ch);
1145 result = (long) (test - list);
1151 * list[] is down, up, left, right
1152 * "left" may be ^H rather than \E[D
1153 * "down" may be ^J rather than \E[B
1154 * But up/right are generally consistently escape sequences for ANSI terminals.
1157 check_ansi_cursor(char *list[4])
1164 bool repeated = FALSE;
1166 for (j = 0; j < 4; ++j) {
1168 for (k = 0; k < j; ++k) {
1170 && !strcmp(list[j], list[k])) {
1171 char *value = _nc_tic_expand(list[k], TRUE, 0);
1172 _nc_warning("repeated cursor control %s\n", value);
1180 if (UChar(up[0]) == '\033') {
1186 } else if (UChar(up[0]) == UChar('\233')) {
1191 while (up[suffix] && isdigit(UChar(up[suffix])))
1194 if (prefix && up[suffix] == 'A') {
1196 if (!strcmp(list[0], "\n"))
1198 if (!strcmp(list[2], "\b"))
1201 for (j = 0; j < 4; ++j) {
1202 if (skip[j] || strlen(list[j]) == 1)
1204 if (memcmp(list[j], up, prefix)) {
1205 char *value = _nc_tic_expand(list[j], TRUE, 0);
1206 _nc_warning("inconsistent prefix for %s\n", value);
1209 if (strlen(list[j]) < suffix) {
1210 char *value = _nc_tic_expand(list[j], TRUE, 0);
1211 _nc_warning("inconsistent length for %s, expected %d\n",
1212 value, (int) suffix + 1);
1216 if (list[j][suffix] != want) {
1217 char *value = _nc_tic_expand(list[j], TRUE, 0);
1218 _nc_warning("inconsistent suffix for %s, expected %c, have %c\n",
1219 value, want, list[j][suffix]);
1226 #define EXPECTED(name) if (!PRESENT(name)) _nc_warning("expected " #name)
1227 #define UNEXPECTED(name) if (PRESENT(name)) _nc_warning("unexpected " #name ", for %s", why)
1230 check_noaddress(TERMTYPE *tp, const char *why)
1232 UNEXPECTED(column_address);
1233 UNEXPECTED(cursor_address);
1234 UNEXPECTED(cursor_home);
1235 UNEXPECTED(cursor_mem_address);
1236 UNEXPECTED(cursor_to_ll);
1237 UNEXPECTED(row_address);
1238 UNEXPECTED(row_address);
1242 check_cursor(TERMTYPE *tp)
1248 check_noaddress(tp, "hard_copy");
1249 } else if (generic_type) {
1250 check_noaddress(tp, "generic_type");
1251 } else if (strchr(tp->term_names, '+') == 0) {
1254 if (PRESENT(column_address))
1256 if (PRESENT(cursor_address))
1258 if (PRESENT(cursor_home))
1260 if (PRESENT(cursor_mem_address))
1262 if (PRESENT(cursor_to_ll))
1264 if (PRESENT(row_address))
1266 if (PRESENT(cursor_down))
1268 if (PRESENT(cursor_up))
1270 if (PRESENT(cursor_left))
1272 if (PRESENT(cursor_right))
1274 if (x < 2 && y < 2) {
1275 _nc_warning("terminal lacks cursor addressing");
1278 _nc_warning("terminal lacks cursor column-addressing");
1280 _nc_warning("terminal lacks cursor row-addressing");
1284 /* it is rare to have an insert-line feature without a matching delete */
1285 ANDMISSING(parm_insert_line, insert_line);
1286 ANDMISSING(parm_delete_line, delete_line);
1287 ANDMISSING(parm_insert_line, parm_delete_line);
1289 /* if we have a parameterized form, then the non-parameterized is easy */
1290 ANDMISSING(parm_down_cursor, cursor_down);
1291 ANDMISSING(parm_up_cursor, cursor_up);
1292 ANDMISSING(parm_left_cursor, cursor_left);
1293 ANDMISSING(parm_right_cursor, cursor_right);
1295 /* Given any of a set of cursor movement, the whole set should be present.
1296 * Technically this is not true (we could use cursor_address to fill in
1297 * unsupported controls), but it is likely.
1300 if (PRESENT(parm_down_cursor)) {
1301 list[count++] = parm_down_cursor;
1303 if (PRESENT(parm_up_cursor)) {
1304 list[count++] = parm_up_cursor;
1306 if (PRESENT(parm_left_cursor)) {
1307 list[count++] = parm_left_cursor;
1309 if (PRESENT(parm_right_cursor)) {
1310 list[count++] = parm_right_cursor;
1313 check_ansi_cursor(list);
1314 } else if (count != 0) {
1315 EXPECTED(parm_down_cursor);
1316 EXPECTED(parm_up_cursor);
1317 EXPECTED(parm_left_cursor);
1318 EXPECTED(parm_right_cursor);
1322 if (PRESENT(cursor_down)) {
1323 list[count++] = cursor_down;
1325 if (PRESENT(cursor_up)) {
1326 list[count++] = cursor_up;
1328 if (PRESENT(cursor_left)) {
1329 list[count++] = cursor_left;
1331 if (PRESENT(cursor_right)) {
1332 list[count++] = cursor_right;
1335 check_ansi_cursor(list);
1336 } else if (count != 0) {
1338 if (PRESENT(cursor_down) && strcmp(cursor_down, "\n"))
1340 if (PRESENT(cursor_left) && strcmp(cursor_left, "\b"))
1342 if (PRESENT(cursor_up) && strlen(cursor_up) > 1)
1344 if (PRESENT(cursor_right) && strlen(cursor_right) > 1)
1347 EXPECTED(cursor_down);
1348 EXPECTED(cursor_up);
1349 EXPECTED(cursor_left);
1350 EXPECTED(cursor_right);
1357 * Do a quick sanity-check for vt100-style keypads to see if the 5-key keypad
1358 * is mapped inconsistently.
1361 check_keypad(TERMTYPE *tp)
1365 if (VALID_STRING(key_a1) &&
1366 VALID_STRING(key_a3) &&
1367 VALID_STRING(key_b2) &&
1368 VALID_STRING(key_c1) &&
1369 VALID_STRING(key_c3)) {
1370 char final[MAX_KP + 1];
1377 final[0] = keypad_final(key_a1);
1378 final[1] = keypad_final(key_a3);
1379 final[2] = keypad_final(key_b2);
1380 final[3] = keypad_final(key_c1);
1381 final[4] = keypad_final(key_c3);
1384 /* special case: legacy coding using 1,2,3,0,. on the bottom */
1385 assert(strlen(final) <= MAX_KP);
1386 if (!strcmp(final, "qsrpn"))
1389 list[0] = keypad_index(key_a1);
1390 list[1] = keypad_index(key_a3);
1391 list[2] = keypad_index(key_b2);
1392 list[3] = keypad_index(key_c1);
1393 list[4] = keypad_index(key_c3);
1395 /* check that they're all vt100 keys */
1396 for (j = 0; j < MAX_KP; ++j) {
1402 /* check if they're all in increasing order */
1403 for (j = 1; j < MAX_KP; ++j) {
1404 if (list[j] > list[j - 1]) {
1408 if (increase != (MAX_KP - 1)) {
1411 for (j = 0, last = -1; j < MAX_KP; ++j) {
1412 for (k = 0, kk = -1, test = 100; k < 5; ++k) {
1413 if (list[k] > last &&
1420 assert(strlen(show) < (MAX_KP * 4));
1423 _nc_STRCAT(show, " ka1", sizeof(show));
1426 _nc_STRCAT(show, " ka3", sizeof(show));
1429 _nc_STRCAT(show, " kb2", sizeof(show));
1432 _nc_STRCAT(show, " kc1", sizeof(show));
1435 _nc_STRCAT(show, " kc3", sizeof(show));
1440 _nc_warning("vt100 keypad order inconsistent: %s", show);
1443 } else if (VALID_STRING(key_a1) ||
1444 VALID_STRING(key_a3) ||
1445 VALID_STRING(key_b2) ||
1446 VALID_STRING(key_c1) ||
1447 VALID_STRING(key_c3)) {
1449 if (keypad_index(key_a1) >= 0)
1450 _nc_STRCAT(show, " ka1", sizeof(show));
1451 if (keypad_index(key_a3) >= 0)
1452 _nc_STRCAT(show, " ka3", sizeof(show));
1453 if (keypad_index(key_b2) >= 0)
1454 _nc_STRCAT(show, " kb2", sizeof(show));
1455 if (keypad_index(key_c1) >= 0)
1456 _nc_STRCAT(show, " kc1", sizeof(show));
1457 if (keypad_index(key_c3) >= 0)
1458 _nc_STRCAT(show, " kc3", sizeof(show));
1460 _nc_warning("vt100 keypad map incomplete:%s", show);
1464 * These warnings are useful for consistency checks - it is possible that
1465 * there are real terminals with mismatches in these
1467 ANDMISSING(key_ic, key_dc);
1471 check_printer(TERMTYPE *tp)
1473 PAIRED(enter_doublewide_mode, exit_doublewide_mode);
1474 PAIRED(enter_italics_mode, exit_italics_mode);
1475 PAIRED(enter_leftward_mode, exit_leftward_mode);
1476 PAIRED(enter_micro_mode, exit_micro_mode);
1477 PAIRED(enter_shadow_mode, exit_shadow_mode);
1478 PAIRED(enter_subscript_mode, exit_subscript_mode);
1479 PAIRED(enter_superscript_mode, exit_superscript_mode);
1480 PAIRED(enter_upward_mode, exit_upward_mode);
1482 ANDMISSING(start_char_set_def, stop_char_set_def);
1484 /* if we have a parameterized form, then the non-parameterized is easy */
1485 ANDMISSING(set_bottom_margin_parm, set_bottom_margin);
1486 ANDMISSING(set_left_margin_parm, set_left_margin);
1487 ANDMISSING(set_right_margin_parm, set_right_margin);
1488 ANDMISSING(set_top_margin_parm, set_top_margin);
1490 ANDMISSING(parm_down_micro, micro_down);
1491 ANDMISSING(parm_left_micro, micro_left);
1492 ANDMISSING(parm_right_micro, micro_right);
1493 ANDMISSING(parm_up_micro, micro_up);
1497 uses_SGR_39_49(const char *value)
1499 return (strstr(value, "39;49") != 0
1500 || strstr(value, "49;39") != 0);
1504 * Check consistency of termcap extensions related to "screen".
1507 check_screen(TERMTYPE *tp)
1510 if (_nc_user_definable) {
1511 int have_XT = tigetflag("XT");
1512 int have_XM = tigetflag("XM");
1513 int have_bce = back_color_erase;
1514 bool have_kmouse = FALSE;
1515 bool use_sgr_39_49 = FALSE;
1516 char *name = _nc_first_name(tp->term_names);
1518 if (!VALID_BOOLEAN(have_bce)) {
1521 if (!VALID_BOOLEAN(have_XM)) {
1524 if (!VALID_BOOLEAN(have_XT)) {
1527 if (VALID_STRING(key_mouse)) {
1528 have_kmouse = !strcmp("\033[M", key_mouse);
1530 if (VALID_STRING(orig_colors)) {
1531 use_sgr_39_49 = uses_SGR_39_49(orig_colors);
1532 } else if (VALID_STRING(orig_pair)) {
1533 use_sgr_39_49 = uses_SGR_39_49(orig_pair);
1536 if (have_XM && have_XT) {
1537 _nc_warning("Screen's XT capability conflicts with XM");
1539 && strstr(name, "screen") != 0
1540 && strchr(name, '.') != 0) {
1541 _nc_warning("Screen's \"screen\" entries should not have XT set");
1542 } else if (have_XT) {
1543 if (!have_kmouse && have_bce) {
1544 if (VALID_STRING(key_mouse)) {
1545 _nc_warning("Value of kmous inconsistent with screen's usage");
1547 _nc_warning("Expected kmous capability with XT");
1550 if (!have_bce && max_colors > 0)
1551 _nc_warning("Expected bce capability with XT");
1552 if (!use_sgr_39_49 && have_bce && max_colors > 0)
1553 _nc_warning("Expected orig_colors capability with XT to have 39/49 parameters");
1554 if (VALID_STRING(to_status_line))
1555 _nc_warning("\"tsl\" capability is redundant, given XT");
1557 if (have_kmouse && !have_XM)
1558 _nc_warning("Expected XT to be set, given kmous");
1565 * Returns the expected number of parameters for the given capability.
1568 expected_params(const char *name)
1571 static const struct {
1575 { "S0", 1 }, /* 'screen' extension */
1628 { "smgbp", 1 }, /* 2 if smgtp is not given */
1643 int result = 0; /* function-keys, etc., use none */
1645 for (n = 0; n < SIZEOF(table); n++) {
1646 if (!strcmp(name, table[n].name)) {
1647 result = table[n].count;
1656 * Make a quick sanity check for the parameters which are used in the given
1657 * strings. If there are no "%p" tokens, then there should be no other "%"
1661 check_params(TERMTYPE *tp, const char *name, char *value)
1663 int expected = expected_params(name);
1669 #ifdef set_top_margin_parm
1670 if (!strcmp(name, "smgbp")
1671 && set_top_margin_parm == 0)
1675 for (n = 0; n < 10; n++)
1681 _nc_warning("expected character after %% in %s", name);
1683 } else if (*s == 'p') {
1684 if (*++s == '\0' || !isdigit((int) *s)) {
1685 _nc_warning("expected digit after %%p in %s", name);
1699 _nc_warning("%s refers to parameter 0 (%%p0), which is not allowed", name);
1701 if (value == set_attributes || expected < 0) {
1703 } else if (expected != actual) {
1704 _nc_warning("%s uses %d parameters, expected %d", name,
1706 for (n = 1; n < actual; n++) {
1708 _nc_warning("%s omits parameter %d", name, n);
1716 while (*s == '/' || isdigit(UChar(*s)))
1722 * Skip a delay altogether, e.g., when comparing a simple string to sgr,
1723 * the latter may have a worst-case delay on the end.
1726 ignore_delays(char *s)
1759 * An sgr string may contain several settings other than the one we're
1760 * interested in, essentially sgr0 + rmacs + whatever. As long as the
1761 * "whatever" is contained in the sgr string, that is close enough for our
1765 similar_sgr(int num, char *a, char *b)
1767 static const char *names[] =
1789 _nc_warning("Did not find delay %s", _nc_visbuf(b));
1791 _nc_warning("checking sgr(%s) %s\n\tcompare to %s\n\tunmatched %s",
1792 names[num], _nc_visbuf2(1, base_a),
1793 _nc_visbuf2(2, base_b),
1797 } else if (delaying) {
1800 } else if ((*b == '0' || (*b == ';')) && *a == 'm') {
1822 /* ignore delays on the end of the string */
1823 a = ignore_delays(a);
1824 return ((num != 0) || (*a == 0));
1828 check_sgr(TERMTYPE *tp, char *zero, int num, char *cap, const char *name)
1833 test = TPARM_9(set_attributes,
1845 if (!similar_sgr(num, test, cap)) {
1846 _nc_warning("%s differs from sgr(%d)\n\t%s=%s\n\tsgr(%d)=%s",
1848 name, _nc_visbuf2(1, cap),
1849 num, _nc_visbuf2(2, test));
1851 } else if (_nc_capcmp(test, zero)) {
1852 _nc_warning("sgr(%d) present, but not %s", num, name);
1854 } else if (PRESENT(cap)) {
1855 _nc_warning("sgr(%d) missing, but %s present", num, name);
1858 _nc_warning("stack error in sgr(%d) string", num);
1862 #define CHECK_SGR(num,name) check_sgr(tp, zero, num, name, #name)
1866 * If tic is compiled with TRACE, we'll be able to see the output from the
1867 * DEBUG() macro. But since it doesn't use traceon(), it always goes to
1868 * the standard error. Use this function to make it simpler to follow the
1869 * resulting debug traces.
1872 show_where(unsigned level)
1874 if (_nc_tracing >= DEBUG_LEVEL(level)) {
1875 char my_name[MAX_NAME_SIZE];
1876 _nc_get_type(my_name);
1877 _tracef("\"%s\", line %d, '%s'",
1879 _nc_curr_line, my_name);
1884 #define show_where(level) /* nothing */
1894 get_fkey_list(TERMTYPE *tp)
1896 NAME_VALUE *result = typeMalloc(NAME_VALUE, NUM_STRINGS(tp) + 1);
1897 const struct tinfo_fkeys *all_fkeys = _nc_tinfo_fkeys;
1902 failed("get_fkey_list");
1904 for (j = 0; all_fkeys[j].code; j++) {
1905 char *a = tp->Strings[all_fkeys[j].offset];
1906 if (VALID_STRING(a)) {
1907 result[used].keycode = (int) all_fkeys[j].code;
1908 result[used].name = strnames[all_fkeys[j].offset];
1909 result[used].value = a;
1914 for (j = STRCOUNT; j < NUM_STRINGS(tp); ++j) {
1915 const char *name = ExtStrname(tp, j, strnames);
1917 result[used].keycode = -1;
1918 result[used].name = name;
1919 result[used].value = tp->Strings[j];
1924 result[used].keycode = 0;
1929 show_fkey_name(NAME_VALUE * data)
1931 if (data->keycode > 0) {
1932 fprintf(stderr, " %s", keyname(data->keycode));
1933 fprintf(stderr, " (capability \"%s\")", data->name);
1935 fprintf(stderr, " capability \"%s\"", data->name);
1939 /* other sanity-checks (things that we don't want in the normal
1940 * logic that reads a terminfo entry)
1943 check_termtype(TERMTYPE *tp, bool literal)
1945 bool conflict = FALSE;
1949 * A terminal entry may contain more than one keycode assigned to
1950 * a given string (e.g., KEY_END and KEY_LL). But curses will only
1951 * return one (the last one assigned).
1953 if (!(_nc_syntax == SYN_TERMCAP && capdump)) {
1954 char *check = calloc((size_t) (NUM_STRINGS(tp) + 1), sizeof(char));
1955 NAME_VALUE *given = get_fkey_list(tp);
1958 failed("check_termtype");
1960 for (j = 0; given[j].keycode; ++j) {
1961 const char *a = given[j].value;
1964 for (k = j + 1; given[k].keycode; k++) {
1965 const char *b = given[k].value;
1968 if (!_nc_capcmp(a, b)) {
1973 _nc_warning("Conflicting key definitions (using the last)");
1976 fprintf(stderr, "...");
1977 show_fkey_name(given + j);
1978 fprintf(stderr, " is the same as");
1979 show_fkey_name(given + k);
1982 fprintf(stderr, ", ");
1983 show_fkey_name(given + k);
1988 fprintf(stderr, "\n");
1994 for_each_string(j, tp) {
1995 char *a = tp->Strings[j];
1996 if (VALID_STRING(a))
1997 check_params(tp, ExtStrname(tp, (int) j, strnames), a);
2008 * These may be mismatched because the terminal description relies on
2009 * restoring the cursor visibility by resetting it.
2011 ANDMISSING(cursor_invisible, cursor_normal);
2012 ANDMISSING(cursor_visible, cursor_normal);
2014 if (PRESENT(cursor_visible) && PRESENT(cursor_normal)
2015 && !_nc_capcmp(cursor_visible, cursor_normal))
2016 _nc_warning("cursor_visible is same as cursor_normal");
2019 * From XSI & O'Reilly, we gather that sc/rc are required if csr is
2020 * given, because the cursor position after the scrolling operation is
2021 * performed is undefined.
2023 ANDMISSING(change_scroll_region, save_cursor);
2024 ANDMISSING(change_scroll_region, restore_cursor);
2027 * If we can clear tabs, we should be able to initialize them.
2029 ANDMISSING(clear_all_tabs, set_tab);
2031 if (PRESENT(set_attributes)) {
2035 if (PRESENT(exit_attribute_mode)) {
2036 zero = strdup(CHECK_SGR(0, exit_attribute_mode));
2038 zero = strdup(TPARM_9(set_attributes, 0, 0, 0, 0, 0, 0, 0, 0, 0));
2041 _nc_warning("stack error in sgr(0) string");
2044 CHECK_SGR(1, enter_standout_mode);
2045 CHECK_SGR(2, enter_underline_mode);
2046 CHECK_SGR(3, enter_reverse_mode);
2047 CHECK_SGR(4, enter_blink_mode);
2048 CHECK_SGR(5, enter_dim_mode);
2049 CHECK_SGR(6, enter_bold_mode);
2050 CHECK_SGR(7, enter_secure_mode);
2051 CHECK_SGR(8, enter_protected_mode);
2052 CHECK_SGR(9, enter_alt_charset_mode);
2055 _nc_warning("sgr(0) did not return a value");
2057 } else if (PRESENT(exit_attribute_mode) &&
2058 set_attributes != CANCELLED_STRING) {
2059 if (_nc_syntax == SYN_TERMINFO)
2060 _nc_warning("missing sgr string");
2063 if (PRESENT(exit_attribute_mode)) {
2064 char *check_sgr0 = _nc_trim_sgr0(tp);
2066 if (check_sgr0 == 0 || *check_sgr0 == '\0') {
2067 _nc_warning("trimmed sgr0 is empty");
2070 if (check_sgr0 != exit_attribute_mode) {
2072 ("will trim sgr0\n\toriginal sgr0=%s\n\ttrimmed sgr0=%s",
2073 _nc_visbuf2(1, exit_attribute_mode),
2074 _nc_visbuf2(2, check_sgr0)));
2078 ("will not trim sgr0\n\toriginal sgr0=%s",
2079 _nc_visbuf(exit_attribute_mode)));
2085 if (!auto_right_margin) {
2087 ("can write to lower-right directly"));
2088 } else if (PRESENT(enter_am_mode) && PRESENT(exit_am_mode)) {
2090 ("can write to lower-right by suppressing automargin"));
2091 } else if ((PRESENT(enter_insert_mode) && PRESENT(exit_insert_mode))
2092 || PRESENT(insert_character) || PRESENT(parm_ich)) {
2094 ("can write to lower-right by using inserts"));
2097 ("cannot write to lower-right"));
2102 * Some standard applications (e.g., vi) and some non-curses
2103 * applications (e.g., jove) get confused if we have both ich1 and
2104 * smir/rmir. Let's be nice and warn about that, too, even though
2105 * ncurses handles it.
2107 if ((PRESENT(enter_insert_mode) || PRESENT(exit_insert_mode))
2108 && PRESENT(parm_ich)) {
2109 _nc_warning("non-curses applications may be confused by ich1 with smir/rmir");
2113 * Finally, do the non-verbose checks
2115 if (save_check_termtype != 0)
2116 save_check_termtype(tp, literal);