1 /****************************************************************************
2 * Copyright (c) 1998-2017,2018 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 <tparm_type.h>
47 #include <hashed_db.h>
48 #include <parametrized.h>
49 #include <transform.h>
51 MODULE_ID("$Id: tic.c,v 1.252 2018/01/25 19:16:50 tom Exp $")
53 #define STDIN_NAME "<stdin>"
55 const char *_nc_progname = "tic";
59 static bool capdump = FALSE; /* running as infotocap? */
60 static bool infodump = FALSE; /* running as captoinfo? */
61 static bool showsummary = FALSE;
62 static char **namelst = 0;
63 static const char *to_remove;
66 static bool using_extensions = FALSE;
69 static void (*save_check_termtype) (TERMTYPE2 *, bool);
70 static void check_termtype(TERMTYPE2 *tt, bool);
72 static const char usage_string[] = "\
103 free_namelist(char **src)
107 for (n = 0; src[n] != 0; ++n)
120 free_namelist(namelst);
121 _nc_leaks_dump_entry();
125 if (to_remove != 0) {
127 rc = remove(to_remove);
129 rc = unlink(to_remove);
137 failed(const char *msg)
140 ExitProgram(EXIT_FAILURE);
146 #define DATA(s) s "\n"
147 static const char options_string[] =
150 DATA(" -0 format translation output all capabilities on one line")
151 DATA(" -1 format translation output one capability per line")
153 DATA(" -a retain commented-out capabilities (sets -x also)")
155 DATA(" -C translate entries to termcap source form")
156 DATA(" -D print list of tic's database locations (first must be writable)")
157 DATA(" -c check only, validate input without compiling or translating")
158 DATA(" -e<names> translate/compile only entries named by comma-separated list")
159 DATA(" -f format complex strings for readability")
160 DATA(" -G format %{number} to %'char'")
161 DATA(" -g format %'char' to %{number}")
162 DATA(" -I translate entries to terminfo source form")
163 DATA(" -K translate entries to termcap source form with BSD syntax")
164 DATA(" -L translate entries to full terminfo source form")
165 DATA(" -N disable smart defaults for source translation")
166 DATA(" -o<dir> set output directory for compiled entry writes")
167 DATA(" -Q[n] dump compiled description")
168 DATA(" -q brief listing, removes headers")
169 DATA(" -R<name> restrict translation to given terminfo/termcap version")
170 DATA(" -r force resolution of all use entries in source translation")
171 DATA(" -s print summary statistics")
172 DATA(" -T remove size-restrictions on compiled description")
174 DATA(" -t suppress commented-out capabilities")
176 DATA(" -U suppress post-processing of entries")
177 DATA(" -V print version")
178 DATA(" -W wrap long strings according to -w[n] option")
179 DATA(" -v[n] set verbosity level")
180 DATA(" -w[n] set format width for translation output")
182 DATA(" -x treat unknown capabilities as user-defined")
186 DATA(" <file> file to translate or compile")
190 fprintf(stderr, "Usage: %s %s\n", _nc_progname, usage_string);
191 fputs(options_string, stderr);
192 ExitProgram(EXIT_FAILURE);
205 char result[MAX_ENTRY_SIZE];
208 * Look for strings that contain %{number}, convert them to %'char',
209 * which is shorter and runs a little faster.
211 for (n = 0; n < STRCOUNT; n++) {
212 s = ep->tterm.Strings[n];
214 && strchr(s, L_BRACE) != 0) {
217 while ((ch = *t++) != 0) {
221 } else if ((ch == '%')
222 && (*t == L_BRACE)) {
224 long value = strtol(t + 1, &v, 0);
228 && value != '\\' /* FIXME */
230 && isprint((int) value)) {
239 if (strlen(result) < strlen(s))
240 _nc_STRCPY(s, result, strlen(s) + 1);
244 _nc_set_type(_nc_first_name(ep->tterm.term_names));
245 _nc_curr_line = (int) ep->startline;
246 _nc_write_entry(&ep->tterm);
250 immedhook(ENTRY * ep GCC_UNUSED)
251 /* write out entries with no use capabilities immediately to save storage */
255 * This is strictly a core-economy kluge. The really clean way to handle
256 * compilation is to slurp the whole file into core and then do all the
257 * name-collision checks and entry writes in one swell foop. But the
258 * terminfo master file is large enough that some core-poor systems swap
259 * like crazy when you compile it this way...there have been reports of
260 * this process taking *three hours*, rather than the twenty seconds or
261 * less typical on my development box.
263 * So. This hook *immediately* writes out the referenced entry if it
264 * has no use capabilities. The compiler main loop refrains from
265 * adding the entry to the in-core list when this hook fires. If some
266 * other entry later needs to reference an entry that got written
267 * immediately, that's OK; the resolution code will fetch it off disk
268 * when it can't find it in core.
270 * Name collisions will still be detected, just not as cleanly. The
271 * write_entry() code complains before overwriting an entry that
272 * postdates the time of tic's first call to write_entry(). Thus
273 * it will complain about overwriting entries newly made during the
274 * tic run, but not about overwriting ones that predate it.
276 * The reason this is a hook, and not in line with the rest of the
277 * compiler code, is that the support for termcap fallback cannot assume
278 * it has anywhere to spool out these entries!
280 * The _nc_set_type() call here requires a compensating one in
283 * If you define HAVE_BIG_CORE, you'll disable this kluge. This will
284 * make tic a bit faster (because the resolution code won't have to do
285 * disk I/O nearly as often).
287 if (ep->nuses == 0) {
288 int oldline = _nc_curr_line;
291 _nc_curr_line = oldline;
292 free(ep->tterm.str_table);
295 #endif /* HAVE_BIG_CORE */
301 /* emit a comment char, translating terminfo names to termcap names */
303 static bool in_name = FALSE;
304 static size_t have, used;
305 static char *namebuf, *suffix;
308 if (used + 1 >= have) {
310 if ((namebuf = typeRealloc(char, have, namebuf)) == 0)
311 failed("put_translate namebuf");
312 if ((suffix = typeRealloc(char, have, suffix)) == 0)
313 failed("put_translate suffix");
315 if (c == '\n' || c == '@') {
316 namebuf[used++] = '\0';
318 (void) fputs(namebuf, stdout);
321 } else if (c != '>') {
322 namebuf[used++] = (char) c;
323 } else { /* ah! candidate name! */
325 NCURSES_CONST char *tp;
327 namebuf[used++] = '\0';
331 if ((up = strchr(namebuf, '#')) != 0
332 || (up = strchr(namebuf, '=')) != 0
333 || ((up = strchr(namebuf, '@')) != 0 && up[1] == '>')) {
334 _nc_STRCPY(suffix, up, have);
338 if ((tp = nametrans(namebuf)) != 0) {
340 (void) fputs(tp, stdout);
341 (void) fputs(suffix, stdout);
344 /* couldn't find a translation, just dump the name */
346 (void) fputs(namebuf, stdout);
347 (void) fputs(suffix, stdout);
361 /* Returns a string, stripped of leading/trailing whitespace */
367 while (isspace(UChar(*src)))
373 if ((dst = strdup(src)) == NULL) {
377 while (--len != 0 && isspace(UChar(dst[len])))
385 open_tempfile(char *filename)
389 _nc_STRCPY(filename, "/tmp/XXXXXX", PATH_MAX);
392 int oldmask = (int) umask(077);
393 int fd = mkstemp(filename);
395 result = fdopen(fd, "w");
396 umask((mode_t) oldmask);
399 if (tmpnam(filename) != 0)
400 result = fopen(filename, "w");
406 copy_input(FILE *source, const char *filename, char *alt_file)
408 char my_altfile[PATH_MAX];
414 alt_file = my_altfile;
417 failed("copy_input (source)");
418 } else if ((target = open_tempfile(alt_file)) == 0) {
419 failed("copy_input (target)");
426 } else if (ferror(source)) {
428 } else if (ch == 0) {
429 /* don't loop in case someone wants to convert /dev/zero */
430 fprintf(stderr, "%s: %s is not a text-file\n", _nc_progname, filename);
431 ExitProgram(EXIT_FAILURE);
437 * rewind() does not force the target file's data to disk (not does
438 * fflush()...). So open a second stream on the data and then close
439 * the one that we were writing on before starting to read from the
442 result = fopen(alt_file, "r+");
444 to_remove = strdup(alt_file);
450 open_input(const char *filename, char *alt_file)
456 if (!strcmp(filename, "-")) {
457 fp = copy_input(stdin, STDIN_NAME, alt_file);
458 } else if (stat(filename, &sb) < 0) {
459 fprintf(stderr, "%s: %s %s\n", _nc_progname, filename, strerror(errno));
460 ExitProgram(EXIT_FAILURE);
461 } else if ((mode = (sb.st_mode & S_IFMT)) == S_IFDIR
462 || (mode != S_IFREG && mode != S_IFCHR && mode != S_IFIFO)) {
463 fprintf(stderr, "%s: %s is not a file\n", _nc_progname, filename);
464 ExitProgram(EXIT_FAILURE);
466 fp = fopen(filename, "r");
469 fprintf(stderr, "%s: Can't open %s\n", _nc_progname, filename);
470 ExitProgram(EXIT_FAILURE);
472 if (mode != S_IFREG) {
474 FILE *fp2 = copy_input(fp, filename, alt_file);
477 fprintf(stderr, "%s: %s is not a file\n", _nc_progname, filename);
478 ExitProgram(EXIT_FAILURE);
485 /* Parse the "-e" option-value into a list of names */
487 make_namelist(char *src)
492 unsigned pass, n, nn;
497 } else if (strchr(src, '/') != 0) { /* a filename */
498 FILE *fp = open_input(src, (char *) 0);
500 for (pass = 1; pass <= 2; pass++) {
502 while (fgets(buffer, sizeof(buffer), fp) != 0) {
503 if ((s = stripped(buffer)) != 0) {
512 if ((dst = typeCalloc(char *, nn + 1)) == 0)
513 failed("make_namelist");
518 } else { /* literal list of names */
519 for (pass = 1; pass <= 2; pass++) {
520 for (n = nn = 0, base = src;; n++) {
522 if (mark == ',' || mark == '\0') {
527 if ((s = stripped(base)) != 0)
536 if ((dst = typeCalloc(char *, nn + 1)) == 0)
537 failed("make_namelist");
541 if (showsummary && (dst != 0)) {
542 fprintf(log_fp, "Entries that will be compiled:\n");
543 for (n = 0; dst[n] != 0; n++)
544 fprintf(log_fp, "%u:%s\n", n + 1, dst[n]);
550 matches(char **needle, const char *haystack)
551 /* does entry in needle list match |-separated field in haystack? */
557 for (n = 0; needle[n] != 0; n++) {
558 if (_nc_name_match(haystack, needle[n], "|")) {
569 valid_db_path(const char *nominal)
573 char suffix[] = DBM_SUFFIX;
574 size_t need = strlen(nominal) + sizeof(suffix);
575 char *result = malloc(need);
578 failed("valid_db_path");
579 _nc_STRCPY(result, nominal, need);
580 if (strcmp(result + need - sizeof(suffix), suffix)) {
581 _nc_STRCAT(result, suffix, need);
584 char *result = strdup(nominal);
587 DEBUG(1, ("** stat(%s)", result));
588 if (stat(result, &sb) >= 0) {
590 if (!S_ISREG(sb.st_mode)
591 || access(result, R_OK | W_OK) != 0) {
592 DEBUG(1, ("...not a writable file"));
597 if (!S_ISDIR(sb.st_mode)
598 || access(result, R_OK | W_OK | X_OK) != 0) {
599 DEBUG(1, ("...not a writable directory"));
605 /* check if parent is directory and is writable */
606 unsigned leaf = _nc_pathlast(result);
608 DEBUG(1, ("...not found"));
610 char save = result[leaf];
612 if (stat(result, &sb) >= 0
613 && S_ISDIR(sb.st_mode)
614 && access(result, R_OK | W_OK | X_OK) == 0) {
617 DEBUG(1, ("...parent directory %s is not writable", result));
622 DEBUG(1, ("... no parent directory"));
631 * Show the databases to which tic could write. The location to which it
632 * writes is always the first one. If none are writable, print an error
636 show_databases(const char *outdir)
638 bool specific = (outdir != 0) || getenv("TERMINFO") != 0;
640 const char *tried = 0;
643 outdir = _nc_tic_dir(0);
645 if ((result = valid_db_path(outdir)) != 0) {
646 printf("%s\n", result);
652 if ((outdir = _nc_home_terminfo())) {
653 if ((result = valid_db_path(outdir)) != 0) {
654 printf("%s\n", result);
656 } else if (!specific) {
662 * If we can write in neither location, give an error message.
666 fprintf(stderr, "%s: %s (no permission)\n", _nc_progname, tried);
667 ExitProgram(EXIT_FAILURE);
672 add_digit(int *target, int source)
674 *target = (*target * 10) + (source - '0');
678 main(int argc, char *argv[])
680 char my_tmpname[PATH_MAX];
681 char my_altfile[PATH_MAX];
683 unsigned debug_level;
684 int smart_defaults = TRUE;
688 int this_opt, last_opt = '?';
690 int outform = F_TERMINFO; /* output format */
691 int sortmode = S_TERMINFO; /* sort_mode */
695 bool formatted = FALSE; /* reformat complex strings? */
696 bool literal = FALSE; /* suppress post-processing? */
697 int numbers = 0; /* format "%'char'" to/from "%{number}" */
698 bool forceresolve = FALSE; /* force resolution */
700 char *tversion = (char *) NULL;
701 const char *source_file = "terminfo";
702 char *outdir = (char *) NULL;
703 bool check_only = FALSE;
704 bool suppress_untranslatable = FALSE;
707 bool wrap_strings = FALSE;
711 _nc_progname = _nc_rootname(argv[0]);
714 if ((infodump = same_program(_nc_progname, PROG_CAPTOINFO)) != FALSE) {
715 outform = F_TERMINFO;
716 sortmode = S_TERMINFO;
718 if ((capdump = same_program(_nc_progname, PROG_INFOTOCAP)) != FALSE) {
720 sortmode = S_TERMCAP;
723 use_extended_names(FALSE);
728 * Processing arguments is a little complicated, since someone made a
729 * design decision to allow the numeric values for -w, -v options to
732 while ((this_opt = getopt(argc, argv,
733 "0123456789CDIKLNQR:TUVWace:fGgo:qrstvwx")) != -1) {
734 if (isdigit(this_opt)) {
737 add_digit(&quickdump, this_opt);
740 add_digit(&v_opt, this_opt);
743 add_digit(&width, this_opt);
765 /* the initial version of -K in 20110730 fell-thru here, but the
766 * same flag is useful when reading sources -TD
772 sortmode = S_TERMCAP;
775 debug_level = VtoTrace(v_opt);
776 set_trace_level(debug_level);
777 show_databases(outdir);
778 ExitProgram(EXIT_SUCCESS);
782 outform = F_TERMINFO;
783 sortmode = S_TERMINFO;
787 outform = F_VARIABLE;
788 sortmode = S_VARIABLE;
791 smart_defaults = FALSE;
807 puts(curses_version());
808 ExitProgram(EXIT_SUCCESS);
816 namelst = make_namelist(optarg);
847 _nc_disable_period = FALSE;
848 suppress_untranslatable = TRUE;
851 _nc_disable_period = TRUE;
854 use_extended_names(TRUE);
855 using_extensions = TRUE;
864 debug_level = VtoTrace(v_opt);
865 set_trace_level(debug_level);
868 save_check_termtype = _nc_check_termtype2;
869 _nc_check_termtype2 = check_termtype;
873 * Aaargh! immedhook seriously hoses us!
875 * One problem with immedhook is it means we can't do -e. Problem
876 * is that we can't guarantee that for each terminal listed, all the
877 * terminals it depends on will have been kept in core for reference
878 * resolution -- in fact it's certain the primitive types at the end
879 * of reference chains *won't* be in core unless they were explicitly
880 * in the select list themselves.
882 if (namelst && (!infodump && !capdump)) {
883 (void) fprintf(stderr,
884 "%s: Sorry, -e can't be used without -I or -C\n",
886 ExitProgram(EXIT_FAILURE);
888 #endif /* HAVE_BIG_CORE */
891 source_file = argv[optind++];
894 "%s: Too many file names. Usage:\n\t%s %s",
898 ExitProgram(EXIT_FAILURE);
901 if (infodump == TRUE) {
902 /* captoinfo's no-argument case */
903 source_file = "/etc/termcap";
904 if ((termcap = getenv("TERMCAP")) != 0
905 && (namelst = make_namelist(getenv("TERM"))) != 0) {
906 if (access(termcap, F_OK) == 0) {
908 source_file = termcap;
910 if ((tmp_fp = open_tempfile(my_tmpname)) != 0) {
911 source_file = my_tmpname;
912 fprintf(tmp_fp, "%s\n", termcap);
914 tmp_fp = open_input(source_file, (char *) 0);
915 to_remove = source_file;
924 "%s: File name needed. Usage:\n\t%s %s",
928 ExitProgram(EXIT_FAILURE);
933 tmp_fp = open_input(source_file, my_altfile);
934 if (!strcmp(source_file, "-")) {
935 source_file = STDIN_NAME;
939 if (infodump || check_only) {
945 wrap_strings, width, height,
946 debug_level, formatted || check_only, check_only, quickdump);
947 } else if (capdump) {
951 wrap_strings, width, height,
952 debug_level, FALSE, FALSE, FALSE);
955 /* parse entries out of the source file */
956 _nc_set_source(source_file);
958 if (!(check_only || infodump || capdump))
959 _nc_set_writedir(outdir);
960 #endif /* HAVE_BIG_CORE */
961 _nc_read_entry_source(tmp_fp, (char *) NULL,
962 !smart_defaults || literal, FALSE,
963 ((check_only || infodump || capdump)
967 /* do use resolution */
968 if (check_only || (!infodump && !capdump) || forceresolve) {
969 if (!_nc_resolve_uses2(TRUE, literal) && !check_only) {
970 ExitProgram(EXIT_FAILURE);
975 if (check_only && limited && (capdump || infodump)) {
977 if (matches(namelst, qp->tterm.term_names)) {
978 int len = fmt_entry(&qp->tterm, NULL, FALSE, TRUE, infodump, numbers);
980 if (len > (infodump ? MAX_TERMINFO_LENGTH : MAX_TERMCAP_LENGTH))
981 (void) fprintf(stderr,
982 "%s: resolved %s entry is %d bytes long\n",
984 _nc_first_name(qp->tterm.term_names),
990 /* write or dump all entries */
992 /* this is in case infotocap() generates warnings */
993 _nc_curr_col = _nc_curr_line = -1;
996 if (matches(namelst, qp->tterm.term_names)) {
997 /* this is in case infotocap() generates warnings */
998 _nc_set_type(_nc_first_name(qp->tterm.term_names));
999 _nc_curr_line = (int) qp->startline;
1000 repair_acsc(&qp->tterm);
1001 dump_entry(&qp->tterm, suppress_untranslatable,
1002 limited, numbers, NULL);
1006 if (!infodump && !capdump) {
1007 _nc_set_writedir(outdir);
1008 for_entry_list(qp) {
1009 if (matches(namelst, qp->tterm.term_names))
1013 /* this is in case infotocap() generates warnings */
1014 _nc_curr_col = _nc_curr_line = -1;
1016 for_entry_list(qp) {
1017 if (matches(namelst, qp->tterm.term_names)) {
1018 long j = qp->cend - qp->cstart;
1021 /* this is in case infotocap() generates warnings */
1022 _nc_set_type(_nc_first_name(qp->tterm.term_names));
1025 (void) fseek(tmp_fp, qp->cstart, SEEK_SET);
1028 (void) putchar(fgetc(tmp_fp));
1030 put_translate(fgetc(tmp_fp));
1034 repair_acsc(&qp->tterm);
1035 dump_entry(&qp->tterm, suppress_untranslatable,
1036 limited, numbers, NULL);
1037 for (j = 0; j < (long) qp->nuses; j++)
1038 dump_uses(qp->uses[j].name, !capdump);
1040 if (debug_level != 0 && !limited)
1041 printf("# length=%d\n", len);
1044 if (!namelst && _nc_tail && !quiet) {
1046 bool in_comment = FALSE;
1047 bool trailing_comment = FALSE;
1049 (void) fseek(tmp_fp, _nc_tail->cend, SEEK_SET);
1050 while ((c = fgetc(tmp_fp)) != EOF) {
1053 trailing_comment = TRUE;
1059 if (trailing_comment
1060 && (in_comment || (oldc == '\n' && c == '\n')))
1068 /* Show the directory into which entries were written, and the total
1072 && (!(check_only || infodump || capdump))) {
1073 int total = _nc_tic_written();
1075 fprintf(log_fp, "%d entries written to %s\n",
1077 _nc_tic_dir((char *) 0));
1079 fprintf(log_fp, "No entries written\n");
1081 ExitProgram(EXIT_SUCCESS);
1085 * This bit of legerdemain turns all the terminfo variable names into
1086 * references to locations in the arrays Booleans, Numbers, and Strings ---
1087 * precisely what's needed (see comp_parse.c).
1093 * Check if the alternate character-set capabilities are consistent.
1096 check_acs(TERMTYPE2 *tp)
1098 if (VALID_STRING(acs_chars)) {
1099 const char *boxes = "lmkjtuvwqxn";
1105 memset(mapped, 0, sizeof(mapped));
1106 for (p = acs_chars; *p != '\0'; p += 2) {
1108 _nc_warning("acsc has odd number of characters");
1111 mapped[UChar(p[0])] = p[1];
1114 if (mapped[UChar('I')] && !mapped[UChar('i')]) {
1115 _nc_warning("acsc refers to 'I', which is probably an error");
1118 for (p = boxes, q = missing; *p != '\0'; ++p) {
1119 if (!mapped[UChar(p[0])]) {
1125 assert(strlen(missing) <= strlen(boxes));
1126 if (*missing != '\0' && strcmp(missing, boxes)) {
1127 _nc_warning("acsc is missing some line-drawing mapping: %s", missing);
1133 * Check if the color capabilities are consistent
1136 check_colors(TERMTYPE2 *tp)
1138 if ((max_colors > 0) != (max_pairs > 0)
1139 || ((max_colors > max_pairs) && (initialize_pair == 0)))
1140 _nc_warning("inconsistent values for max_colors (%d) and max_pairs (%d)",
1141 max_colors, max_pairs);
1143 PAIRED(set_foreground, set_background);
1144 PAIRED(set_a_foreground, set_a_background);
1145 PAIRED(set_color_pair, initialize_pair);
1147 if (VALID_STRING(set_foreground)
1148 && VALID_STRING(set_a_foreground)
1149 && !_nc_capcmp(set_foreground, set_a_foreground))
1150 _nc_warning("expected setf/setaf to be different");
1152 if (VALID_STRING(set_background)
1153 && VALID_STRING(set_a_background)
1154 && !_nc_capcmp(set_background, set_a_background))
1155 _nc_warning("expected setb/setab to be different");
1157 /* see: has_colors() */
1158 if (VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
1159 && (((set_foreground != NULL)
1160 && (set_background != NULL))
1161 || ((set_a_foreground != NULL)
1162 && (set_a_background != NULL))
1163 || set_color_pair)) {
1164 if (!VALID_STRING(orig_pair) && !VALID_STRING(orig_colors))
1165 _nc_warning("expected either op/oc string for resetting colors");
1168 if (!VALID_STRING(initialize_pair) &&
1169 !VALID_STRING(initialize_color)) {
1170 _nc_warning("expected initc or initp because ccc is given");
1173 if (VALID_STRING(initialize_pair) ||
1174 VALID_STRING(initialize_color)) {
1175 _nc_warning("expected ccc because initc is given");
1181 csi_length(const char *value)
1185 if (value[0] == '\033' && value[1] == '[') {
1187 } else if (UChar(value[0]) == 0x9a) {
1194 keypad_final(const char *string)
1198 if (VALID_STRING(string)
1199 && *string++ == '\033'
1201 && strlen(string) == 1) {
1209 keypad_index(const char *string)
1212 const char *list = "PQRSwxymtuvlqrsPpn"; /* app-keypad except "Enter" */
1216 if ((ch = keypad_final(string)) != '\0') {
1217 test = (strchr) (list, ch);
1219 result = (long) (test - list);
1225 * list[] is down, up, left, right
1226 * "left" may be ^H rather than \E[D
1227 * "down" may be ^J rather than \E[B
1228 * But up/right are generally consistently escape sequences for ANSI terminals.
1231 check_ansi_cursor(char *list[4])
1237 bool repeated = FALSE;
1239 for (j = 0; j < 4; ++j) {
1241 for (k = 0; k < j; ++k) {
1243 && !strcmp(list[j], list[k])) {
1244 char *value = _nc_tic_expand(list[k], TRUE, 0);
1245 _nc_warning("repeated cursor control %s\n", value);
1252 size_t prefix = (size_t) csi_length(up);
1256 while (up[suffix] && isdigit(UChar(up[suffix])))
1259 if (prefix && up[suffix] == 'A') {
1261 if (!strcmp(list[0], "\n"))
1263 if (!strcmp(list[2], "\b"))
1266 for (j = 0; j < 4; ++j) {
1267 if (skip[j] || strlen(list[j]) == 1)
1269 if (memcmp(list[j], up, prefix)) {
1270 char *value = _nc_tic_expand(list[j], TRUE, 0);
1271 _nc_warning("inconsistent prefix for %s\n", value);
1274 if (strlen(list[j]) < suffix) {
1275 char *value = _nc_tic_expand(list[j], TRUE, 0);
1276 _nc_warning("inconsistent length for %s, expected %d\n",
1277 value, (int) suffix + 1);
1281 if (list[j][suffix] != want) {
1282 char *value = _nc_tic_expand(list[j], TRUE, 0);
1283 _nc_warning("inconsistent suffix for %s, expected %c, have %c\n",
1284 value, want, list[j][suffix]);
1291 #define EXPECTED(name) if (!PRESENT(name)) _nc_warning("expected " #name)
1292 #define UNEXPECTED(name) if (PRESENT(name)) _nc_warning("unexpected " #name ", for %s", why)
1295 check_noaddress(TERMTYPE2 *tp, const char *why)
1297 UNEXPECTED(column_address);
1298 UNEXPECTED(cursor_address);
1299 UNEXPECTED(cursor_home);
1300 UNEXPECTED(cursor_mem_address);
1301 UNEXPECTED(cursor_to_ll);
1302 UNEXPECTED(row_address);
1303 UNEXPECTED(row_address);
1307 check_cursor(TERMTYPE2 *tp)
1313 check_noaddress(tp, "hard_copy");
1314 } else if (generic_type) {
1315 check_noaddress(tp, "generic_type");
1316 } else if (strchr(tp->term_names, '+') == 0) {
1319 if (PRESENT(column_address))
1321 if (PRESENT(cursor_address))
1323 if (PRESENT(cursor_home))
1325 if (PRESENT(cursor_mem_address))
1327 if (PRESENT(cursor_to_ll))
1329 if (PRESENT(row_address))
1331 if (PRESENT(cursor_down))
1333 if (PRESENT(cursor_up))
1335 if (PRESENT(cursor_left))
1337 if (PRESENT(cursor_right))
1339 if (x < 2 && y < 2) {
1340 _nc_warning("terminal lacks cursor addressing");
1343 _nc_warning("terminal lacks cursor column-addressing");
1345 _nc_warning("terminal lacks cursor row-addressing");
1349 /* it is rare to have an insert-line feature without a matching delete */
1350 ANDMISSING(parm_insert_line, insert_line);
1351 ANDMISSING(parm_delete_line, delete_line);
1352 ANDMISSING(parm_insert_line, parm_delete_line);
1354 /* if we have a parameterized form, then the non-parameterized is easy */
1355 ANDMISSING(parm_down_cursor, cursor_down);
1356 ANDMISSING(parm_up_cursor, cursor_up);
1357 ANDMISSING(parm_left_cursor, cursor_left);
1358 ANDMISSING(parm_right_cursor, cursor_right);
1360 /* Given any of a set of cursor movement, the whole set should be present.
1361 * Technically this is not true (we could use cursor_address to fill in
1362 * unsupported controls), but it is likely.
1365 if (PRESENT(parm_down_cursor)) {
1366 list[count++] = parm_down_cursor;
1368 if (PRESENT(parm_up_cursor)) {
1369 list[count++] = parm_up_cursor;
1371 if (PRESENT(parm_left_cursor)) {
1372 list[count++] = parm_left_cursor;
1374 if (PRESENT(parm_right_cursor)) {
1375 list[count++] = parm_right_cursor;
1378 check_ansi_cursor(list);
1379 } else if (count != 0) {
1380 EXPECTED(parm_down_cursor);
1381 EXPECTED(parm_up_cursor);
1382 EXPECTED(parm_left_cursor);
1383 EXPECTED(parm_right_cursor);
1387 if (PRESENT(cursor_down)) {
1388 list[count++] = cursor_down;
1390 if (PRESENT(cursor_up)) {
1391 list[count++] = cursor_up;
1393 if (PRESENT(cursor_left)) {
1394 list[count++] = cursor_left;
1396 if (PRESENT(cursor_right)) {
1397 list[count++] = cursor_right;
1400 check_ansi_cursor(list);
1401 } else if (count != 0) {
1403 if (PRESENT(cursor_down) && strcmp(cursor_down, "\n"))
1405 if (PRESENT(cursor_left) && strcmp(cursor_left, "\b"))
1407 if (PRESENT(cursor_up) && strlen(cursor_up) > 1)
1409 if (PRESENT(cursor_right) && strlen(cursor_right) > 1)
1412 EXPECTED(cursor_down);
1413 EXPECTED(cursor_up);
1414 EXPECTED(cursor_left);
1415 EXPECTED(cursor_right);
1422 * Do a quick sanity-check for vt100-style keypads to see if the 5-key keypad
1423 * is mapped inconsistently.
1426 check_keypad(TERMTYPE2 *tp)
1430 if (VALID_STRING(key_a1) &&
1431 VALID_STRING(key_a3) &&
1432 VALID_STRING(key_b2) &&
1433 VALID_STRING(key_c1) &&
1434 VALID_STRING(key_c3)) {
1435 char final[MAX_KP + 1];
1442 final[0] = keypad_final(key_a1);
1443 final[1] = keypad_final(key_a3);
1444 final[2] = keypad_final(key_b2);
1445 final[3] = keypad_final(key_c1);
1446 final[4] = keypad_final(key_c3);
1449 /* special case: legacy coding using 1,2,3,0,. on the bottom */
1450 assert(strlen(final) <= MAX_KP);
1451 if (!strcmp(final, "qsrpn"))
1454 list[0] = keypad_index(key_a1);
1455 list[1] = keypad_index(key_a3);
1456 list[2] = keypad_index(key_b2);
1457 list[3] = keypad_index(key_c1);
1458 list[4] = keypad_index(key_c3);
1460 /* check that they're all vt100 keys */
1461 for (j = 0; j < MAX_KP; ++j) {
1467 /* check if they're all in increasing order */
1468 for (j = 1; j < MAX_KP; ++j) {
1469 if (list[j] > list[j - 1]) {
1473 if (increase != (MAX_KP - 1)) {
1476 for (j = 0, last = -1; j < MAX_KP; ++j) {
1477 for (k = 0, kk = -1, test = 100; k < 5; ++k) {
1478 if (list[k] > last &&
1485 assert(strlen(show) < (MAX_KP * 4));
1488 _nc_STRCAT(show, " ka1", sizeof(show));
1491 _nc_STRCAT(show, " ka3", sizeof(show));
1494 _nc_STRCAT(show, " kb2", sizeof(show));
1497 _nc_STRCAT(show, " kc1", sizeof(show));
1500 _nc_STRCAT(show, " kc3", sizeof(show));
1505 _nc_warning("vt100 keypad order inconsistent: %s", show);
1508 } else if (VALID_STRING(key_a1) ||
1509 VALID_STRING(key_a3) ||
1510 VALID_STRING(key_b2) ||
1511 VALID_STRING(key_c1) ||
1512 VALID_STRING(key_c3)) {
1514 if (keypad_index(key_a1) >= 0)
1515 _nc_STRCAT(show, " ka1", sizeof(show));
1516 if (keypad_index(key_a3) >= 0)
1517 _nc_STRCAT(show, " ka3", sizeof(show));
1518 if (keypad_index(key_b2) >= 0)
1519 _nc_STRCAT(show, " kb2", sizeof(show));
1520 if (keypad_index(key_c1) >= 0)
1521 _nc_STRCAT(show, " kc1", sizeof(show));
1522 if (keypad_index(key_c3) >= 0)
1523 _nc_STRCAT(show, " kc3", sizeof(show));
1525 _nc_warning("vt100 keypad map incomplete:%s", show);
1529 * These warnings are useful for consistency checks - it is possible that
1530 * there are real terminals with mismatches in these
1532 ANDMISSING(key_ic, key_dc);
1536 check_printer(TERMTYPE2 *tp)
1539 #if defined(enter_doublewide_mode) && defined(exit_doublewide_mode)
1540 PAIRED(enter_doublewide_mode, exit_doublewide_mode);
1542 #if defined(enter_italics_mode) && defined(exit_italics_mode)
1543 PAIRED(enter_italics_mode, exit_italics_mode);
1545 #if defined(enter_leftward_mode) && defined(exit_leftward_mode)
1546 PAIRED(enter_leftward_mode, exit_leftward_mode);
1548 #if defined(enter_micro_mode) && defined(exit_micro_mode)
1549 PAIRED(enter_micro_mode, exit_micro_mode);
1551 #if defined(enter_shadow_mode) && defined(exit_shadow_mode)
1552 PAIRED(enter_shadow_mode, exit_shadow_mode);
1554 #if defined(enter_subscript_mode) && defined(exit_subscript_mode)
1555 PAIRED(enter_subscript_mode, exit_subscript_mode);
1557 #if defined(enter_superscript_mode) && defined(exit_superscript_mode)
1558 PAIRED(enter_superscript_mode, exit_superscript_mode);
1560 #if defined(enter_upward_mode) && defined(exit_upward_mode)
1561 PAIRED(enter_upward_mode, exit_upward_mode);
1564 #if defined(start_char_set_def) && defined(stop_char_set_def)
1565 ANDMISSING(start_char_set_def, stop_char_set_def);
1568 /* if we have a parameterized form, then the non-parameterized is easy */
1569 #if defined(set_bottom_margin_parm) && defined(set_bottom_margin)
1570 ANDMISSING(set_bottom_margin_parm, set_bottom_margin);
1572 #if defined(set_left_margin_parm) && defined(set_left_margin)
1573 ANDMISSING(set_left_margin_parm, set_left_margin);
1575 #if defined(set_right_margin_parm) && defined(set_right_margin)
1576 ANDMISSING(set_right_margin_parm, set_right_margin);
1578 #if defined(set_top_margin_parm) && defined(set_top_margin)
1579 ANDMISSING(set_top_margin_parm, set_top_margin);
1582 #if defined(parm_down_micro) && defined(micro_down)
1583 ANDMISSING(parm_down_micro, micro_down);
1585 #if defined(parm_left_micro) && defined(micro_left)
1586 ANDMISSING(parm_left_micro, micro_left);
1588 #if defined(parm_right_micro) && defined(micro_right)
1589 ANDMISSING(parm_right_micro, micro_right);
1591 #if defined(parm_up_micro) && defined(micro_up)
1592 ANDMISSING(parm_up_micro, micro_up);
1597 uses_SGR_39_49(const char *value)
1599 return (strstr(value, "39;49") != 0
1600 || strstr(value, "49;39") != 0);
1604 * Check consistency of termcap extensions related to "screen".
1607 check_screen(TERMTYPE2 *tp)
1610 if (_nc_user_definable) {
1611 int have_XT = tigetflag("XT");
1612 int have_XM = tigetflag("XM");
1613 int have_bce = back_color_erase;
1614 bool have_kmouse = FALSE;
1615 bool use_sgr_39_49 = FALSE;
1616 char *name = _nc_first_name(tp->term_names);
1617 bool is_screen = !strncmp(name, "screen", 6);
1618 bool screen_base = (is_screen
1619 && strchr(name, '.') == 0);
1621 if (!VALID_BOOLEAN(have_bce)) {
1624 if (!VALID_BOOLEAN(have_XM)) {
1627 if (!VALID_BOOLEAN(have_XT)) {
1630 if (VALID_STRING(key_mouse)) {
1631 have_kmouse = !strcmp("\033[M", key_mouse);
1633 if (VALID_STRING(orig_colors)) {
1634 use_sgr_39_49 = uses_SGR_39_49(orig_colors);
1635 } else if (VALID_STRING(orig_pair)) {
1636 use_sgr_39_49 = uses_SGR_39_49(orig_pair);
1639 if (have_XM && have_XT) {
1640 _nc_warning("Screen's XT capability conflicts with XM");
1641 } else if (have_XT && screen_base) {
1642 _nc_warning("Screen's \"screen\" entries should not have XT set");
1643 } else if (have_XT) {
1644 if (!have_kmouse && is_screen) {
1645 if (VALID_STRING(key_mouse)) {
1646 _nc_warning("Value of kmous inconsistent with screen's usage");
1648 _nc_warning("Expected kmous capability with XT");
1651 if (!have_bce && max_colors > 0)
1652 _nc_warning("Expected bce capability with XT");
1653 if (!use_sgr_39_49 && have_bce && max_colors > 0)
1654 _nc_warning("Expected orig_colors capability with XT to have 39/49 parameters");
1655 if (VALID_STRING(to_status_line))
1656 _nc_warning("\"tsl\" capability is redundant, given XT");
1660 && !screen_base && strchr(name, '+') == 0) {
1661 _nc_warning("Expected XT to be set, given kmous");
1669 * Returns the expected number of parameters for the given capability.
1672 expected_params(const char *name)
1674 #define DATA(name,count) { { name }, count }
1676 static const struct {
1680 DATA( "S0", 1 ), /* 'screen' extension */
1683 DATA( "colornm", 1 ),
1727 DATA( "setcolor", 1 ),
1731 DATA( "slength", 1 ),
1732 DATA( "slines", 1 ),
1733 DATA( "smgbp", 1 ), /* 2 if smgtp is not given */
1750 int result = 0; /* function-keys, etc., use none */
1752 for (n = 0; n < SIZEOF(table); n++) {
1753 if (!strcmp(name, table[n].name)) {
1754 result = table[n].count;
1763 * ncurses assumes that u6 could be used for getting the cursor-position, but
1764 * that is not implemented. Make a special case for that, to quiet needless
1767 * There are other string-capability extensions (see terminfo.src) which could
1768 * have parameters such as "Ss", "%u", but are not used by ncurses.
1771 is_user_capability(const char *name)
1774 if (name[0] == 'u' &&
1775 (name[1] >= '0' && name[1] <= '9') &&
1777 result = (name[1] == '6') ? 2 : 0;
1782 * Make a quick sanity check for the parameters which are used in the given
1783 * strings. If there are no "%p" tokens, then there should be no other "%"
1787 check_params(TERMTYPE2 *tp, const char *name, char *value, int extended)
1789 int expected = expected_params(name);
1792 bool params[NUM_PARM];
1795 #ifdef set_top_margin_parm
1796 if (!strcmp(name, "smgbp")
1797 && set_top_margin_parm == 0)
1801 for (n = 0; n < NUM_PARM; n++)
1807 _nc_warning("expected character after %% in %s", name);
1809 } else if (*s == 'p') {
1810 if (*++s == '\0' || !isdigit((int) *s)) {
1811 _nc_warning("expected digit after %%p in %s", name);
1826 _nc_warning("extended %s capability has %d parameters",
1833 _nc_warning("%s refers to parameter 0 (%%p0), which is not allowed", name);
1835 if (value == set_attributes || expected < 0) {
1837 } else if (expected != actual) {
1838 _nc_warning("%s uses %d parameters, expected %d", name,
1840 for (n = 1; n < actual; n++) {
1842 _nc_warning("%s omits parameter %d", name, n);
1847 * Counting "%p" markers does not account for termcap expressions which
1848 * may not have been fully translated. Also, tparm does its own analysis.
1849 * Report differences here.
1852 char *p_is_s[NUM_PARM];
1854 int analyzed = _nc_tparm_analyze(value, p_is_s, &popcount);
1855 if (analyzed < popcount) {
1856 analyzed = popcount;
1858 if (actual != analyzed && expected != analyzed) {
1859 int user_cap = is_user_capability(name);
1860 if ((user_cap == analyzed) && using_extensions) {
1862 } else if (user_cap >= 0) {
1863 _nc_warning("tparm will use %d parameters for %s",
1866 _nc_warning("tparm analyzed %d parameters for %s, expected %d",
1867 analyzed, name, actual);
1874 line_capability(const char *name)
1876 bool result = FALSE;
1877 static const char *table[] =
1879 "csr", /* change_scroll_region */
1880 "clear", /* clear_screen */
1882 "cwin", /* create_window */
1883 "cup", /* cursor_address */
1884 "cud1", /* cursor_down */
1885 "home", /* cursor_home */
1886 "mrcup", /* cursor_mem_address */
1887 "ll", /* cursor_to_ll */
1888 "cuu1", /* cursor_up */
1889 "dl1", /* delete_line */
1890 "hd", /* down_half_line */
1891 "flash", /* flash_screen */
1892 "ff", /* form_feed */
1893 "il1", /* insert_line */
1894 "nel", /* newline */
1895 "dl", /* parm_delete_line */
1896 "cud", /* parm_down_cursor */
1897 "indn", /* parm_index */
1898 "il", /* parm_insert_line */
1899 "rin", /* parm_rindex */
1900 "cuu", /* parm_up_cursor */
1901 "mc0", /* print_screen */
1902 "vpa", /* row_address */
1903 "ind", /* scroll_forward */
1904 "ri", /* scroll_reverse */
1905 "hu", /* up_half_line */
1908 for (n = 0; n < SIZEOF(table); ++n) {
1909 if (!strcmp(name, table[n])) {
1918 * Check for DEC VT100 private mode for reverse video.
1921 skip_DECSCNM(const char *value, int *flag)
1925 int skip = csi_length(value);
1927 value[skip++] == '?' &&
1928 value[skip++] == '5') {
1929 if (value[skip] == 'h') {
1931 } else if (value[skip] == 'l') {
1941 check_delays(TERMTYPE2 *tp, const char *name, const char *value)
1944 const char *first = 0;
1945 const char *last = 0;
1947 for (p = value; *p != '\0'; ++p) {
1948 if (p[0] == '$' && p[1] == '<') {
1949 const char *base = p + 2;
1950 const char *mark = 0;
1953 int proportional = 0;
1958 for (q = base; *q != '\0'; ++q) {
1963 } else if (*q == '*' || *q == '/') {
1970 } else if (!(isalnum(UChar(*q)) || strchr("+-.", *q) != 0)) {
1973 } else if (proportional || mandatory) {
1977 last = *q ? (q + 1) : q;
1979 maybe = FALSE; /* just an isolated "$<" */
1983 int rc = sscanf(base, "%f%c", &check_f, &check_c);
1984 if ((rc != 2) || (check_c != *mark) || mixed) {
1985 _nc_warning("syntax error in %s delay '%.*s'", name,
1986 (int) (q - base), base);
1987 } else if (*name == 'k') {
1988 _nc_warning("function-key %s has delay", name);
1989 } else if (proportional && !line_capability(name)) {
1990 _nc_warning("non-line capability using proportional delay: %s", name);
1991 } else if (!xon_xoff &&
1993 strchr(_nc_first_name(tp->term_names), '+') == 0) {
1994 _nc_warning("%s in %s is used since no xon/xoff",
1996 ? "proportional delay"
2001 p = q - 1; /* restart scan */
2006 if (!strcmp(name, "flash") ||
2007 !strcmp(name, "beep")) {
2010 if (first == value || *last == 0) {
2012 * Delay is on one end or the other.
2014 _nc_warning("expected delay embedded within %s", name);
2020 * Check for missing delay when using VT100 reverse-video.
2021 * A real VT100 might not need this, but terminal emulators do.
2023 if ((p = skip_DECSCNM(value, &flag)) != 0 &&
2025 (q = skip_DECSCNM(p, &flag)) != 0 &&
2027 _nc_warning("expected a delay in %s", name);
2034 check_1_infotocap(const char *name, NCURSES_CONST char *value, int count)
2038 long numbers[1 + NUM_PARM];
2039 char *strings[1 + NUM_PARM];
2040 char *p_is_s[NUM_PARM];
2042 char blob[NUM_PARM * 10];
2046 for (k = 1; k <= NUM_PARM; k++) {
2049 _nc_SLIMIT(sizeof(blob) - (size_t) (next - blob))
2052 next += strlen(next) + 1;
2055 switch (tparm_type(name)) {
2057 result = TPARM_2(value, numbers[1], strings[2]);
2060 result = TPARM_3(value, numbers[1], strings[2], strings[3]);
2064 (void) _nc_tparm_analyze(value, p_is_s, &ignored);
2065 #define myParam(n) (p_is_s[n - 1] != 0 ? ((TPARM_ARG) strings[n]) : numbers[n])
2066 result = TPARM_9(value,
2081 #define IsDelay(ch) ((ch) == '.' || isdigit(UChar(ch)))
2084 parse_delay_value(const char *src, double *delays, int *always)
2092 while (isdigit(UChar(*src))) {
2093 (*delays) = (*delays) * 10 + (*src++ - '0');
2099 while (isdigit(UChar(*src))) {
2101 (*delays) += (*src++ - '0') / gotdot;
2104 while (*src == '*' || *src == '/') {
2105 if (always == 0 && *src == '/')
2107 if (*src++ == '*') {
2114 *delays = -(*delays);
2119 parse_ti_delay(const char *ti, double *delays)
2122 while (*ti != '\0') {
2128 && IsDelay(UChar(ti[2]))) {
2130 const char *last = parse_delay_value(ti + 2, delays, &ignored);
2142 parse_tc_delay(const char *tc, double *delays)
2144 return parse_delay_value(tc, delays, (int *) 0);
2148 * Compare terminfo- and termcap-strings, factoring out delays.
2151 same_ti_tc(const char *ti, const char *tc, bool * embedded)
2154 double ti_delay = 0.0;
2155 double tc_delay = 0.0;
2156 const char *ti_last;
2159 ti_last = parse_ti_delay(ti, &ti_delay);
2160 tc = parse_tc_delay(tc, &tc_delay);
2162 while ((ti < ti_last) && *tc) {
2163 if (*ti == '\\' && ispunct(UChar(ti[1]))) {
2165 if ((*ti == '^') && !strncmp(tc, "\\136", 4)) {
2170 } else if (ti[0] == '$' && ti[1] == '<') {
2172 const char *ss = parse_ti_delay(ti, &no_delay);
2179 if (*tc == '\\' && ispunct(UChar(tc[1]))) {
2182 if (*ti++ != *tc++) {
2192 *embedded = FALSE; /* report only one problem */
2200 * Check terminfo to termcap translation.
2203 check_infotocap(TERMTYPE2 *tp, int i, const char *value)
2205 const char *name = ExtStrname(tp, i, strnames);
2206 int params = (((i < (int) SIZEOF(parametrized)) &&
2211 : has_params(value)));
2217 if ((ti_value = _nc_tic_expand(value, TRUE, to_char)) == ABSENT_STRING) {
2218 _nc_warning("tic-expansion of %s failed", name);
2219 } else if ((tc_value = _nc_infotocap(name, ti_value, params)) == ABSENT_STRING) {
2220 _nc_warning("tic-conversion of %s failed", name);
2221 } else if (params > 0) {
2226 if (!strcmp(name, "setf")
2227 || !strcmp(name, "setb")
2228 || !strcmp(name, "setaf")
2229 || !strcmp(name, "setab")) {
2232 for (count = 0; count < limit; ++count) {
2233 char *ti_check = check_1_infotocap(name, ti_value, count);
2234 char *tc_check = check_1_infotocap(name, tc_value, count);
2236 if (strcmp(ti_check, tc_check)) {
2238 fprintf(stderr, "check_infotocap(%s)\n", name);
2239 fprintf(stderr, "...ti '%s'\n", ti_value);
2240 fprintf(stderr, "...tc '%s'\n", tc_value);
2243 _nc_warning("tparm-conversion of %s(%d) differs between\n\tterminfo %s\n\ttermcap %s",
2244 name, count, ti_check, tc_check);
2247 } else if (params == 0 && !same_ti_tc(ti_value, tc_value, &embedded)) {
2249 _nc_warning("termcap equivalent of %s cannot use embedded delay", name);
2251 _nc_warning("tic-conversion of %s changed value\n\tfrom %s\n\tto %s",
2252 name, ti_value, tc_value);
2260 while (*s == '/' || isdigit(UChar(*s)))
2266 * Skip a delay altogether, e.g., when comparing a simple string to sgr,
2267 * the latter may have a worst-case delay on the end.
2270 ignore_delays(char *s)
2302 #define DATA(name) { #name }
2303 static const char sgr_names[][11] =
2320 * An sgr string may contain several settings other than the one we're
2321 * interested in, essentially sgr0 + rmacs + whatever. As long as the
2322 * "whatever" is contained in the sgr string, that is close enough for our
2326 similar_sgr(int num, char *a, char *b)
2337 } else if (b[0] == '$'
2339 _nc_warning("Did not find delay %s", _nc_visbuf(b));
2341 _nc_warning("checking sgr(%s) %s\n\tcompare to %s\n\tunmatched %s",
2342 sgr_names[num], _nc_visbuf2(1, base_a),
2343 _nc_visbuf2(2, base_b),
2347 } else if (delaying) {
2350 } else if ((*b == '0' || (*b == ';')) && *a == 'm') {
2372 /* ignore delays on the end of the string */
2373 a = ignore_delays(a);
2374 return ((num != 0) || (*a == 0));
2378 check_sgr(TERMTYPE2 *tp, char *zero, int num, char *cap, const char *name)
2383 test = TPARM_9(set_attributes,
2395 if (!similar_sgr(num, test, cap)) {
2396 _nc_warning("%s differs from sgr(%d)\n\t%s=%s\n\tsgr(%d)=%s",
2398 name, _nc_visbuf2(1, cap),
2399 num, _nc_visbuf2(2, test));
2401 } else if (_nc_capcmp(test, zero)) {
2402 _nc_warning("sgr(%d) present, but not %s", num, name);
2404 } else if (PRESENT(cap)) {
2405 _nc_warning("sgr(%d) missing, but %s present", num, name);
2408 _nc_warning("stack error in sgr(%d) string", num);
2412 #define CHECK_SGR(num,name) check_sgr(tp, zero, num, name, #name)
2416 * If tic is compiled with TRACE, we'll be able to see the output from the
2417 * DEBUG() macro. But since it doesn't use traceon(), it always goes to
2418 * the standard error. Use this function to make it simpler to follow the
2419 * resulting debug traces.
2422 show_where(unsigned level)
2424 if (_nc_tracing >= DEBUG_LEVEL(level)) {
2425 char my_name[MAX_NAME_SIZE];
2426 _nc_get_type(my_name);
2427 _tracef("\"%s\", line %d, '%s'",
2429 _nc_curr_line, my_name);
2434 #define show_where(level) /* nothing */
2444 get_fkey_list(TERMTYPE2 *tp)
2446 NAME_VALUE *result = typeMalloc(NAME_VALUE, NUM_STRINGS(tp) + 1);
2447 const struct tinfo_fkeys *all_fkeys = _nc_tinfo_fkeys;
2452 failed("get_fkey_list");
2454 for (j = 0; all_fkeys[j].code; j++) {
2455 char *a = tp->Strings[all_fkeys[j].offset];
2456 if (VALID_STRING(a)) {
2457 result[used].keycode = (int) all_fkeys[j].code;
2458 result[used].name = strnames[all_fkeys[j].offset];
2459 result[used].value = a;
2464 for (j = STRCOUNT; j < NUM_STRINGS(tp); ++j) {
2465 const char *name = ExtStrname(tp, (int) j, strnames);
2467 result[used].keycode = -1;
2468 result[used].name = name;
2469 result[used].value = tp->Strings[j];
2474 result[used].keycode = 0;
2479 show_fkey_name(NAME_VALUE * data)
2481 if (data->keycode > 0) {
2482 fprintf(stderr, " %s", keyname(data->keycode));
2483 fprintf(stderr, " (capability \"%s\")", data->name);
2485 fprintf(stderr, " capability \"%s\"", data->name);
2490 * A terminal entry may contain more than one keycode assigned to a given
2491 * string (e.g., KEY_END and KEY_LL). But curses will only return one (the
2492 * last one assigned).
2495 check_conflict(TERMTYPE2 *tp)
2497 bool conflict = FALSE;
2500 if (!(_nc_syntax == SYN_TERMCAP && capdump)) {
2501 char *check = calloc((size_t) (NUM_STRINGS(tp) + 1), sizeof(char));
2502 NAME_VALUE *given = get_fkey_list(tp);
2505 failed("check_conflict");
2507 for (j = 0; given[j].keycode; ++j) {
2508 const char *a = given[j].value;
2511 if (!VALID_STRING(a))
2514 for (k = j + 1; given[k].keycode; k++) {
2515 const char *b = given[k].value;
2517 if (!VALID_STRING(b))
2522 if (!_nc_capcmp(a, b)) {
2527 _nc_warning("Conflicting key definitions (using the last)");
2530 fprintf(stderr, "...");
2531 show_fkey_name(given + j);
2532 fprintf(stderr, " is the same as");
2533 show_fkey_name(given + k);
2536 fprintf(stderr, ", ");
2537 show_fkey_name(given + k);
2542 fprintf(stderr, "\n");
2545 if (using_extensions) {
2548 const char *xcurses;
2549 const char *shifted;
2565 * SVr4 curses defines the "xcurses" names listed above except for
2566 * the special cases in the "shifted" column. When using these
2567 * names for xterm's extensions, that was confusing, and resulted
2568 * in adding extended capabilities with "2" (shift) suffix. This
2569 * check warns about unnecessary use of extensions for this quirk.
2571 for (j = 0; given[j].keycode; ++j) {
2572 const char *find = given[j].name;
2576 if (!VALID_STRING(given[j].value))
2579 for (k = 0; table[k].xcurses; ++k) {
2580 const char *test = table[k].xcurses;
2581 size_t size = strlen(test);
2583 if (!strncmp(find, test, size) && strcmp(find, test)) {
2584 switch (sscanf(find + size, "%d%c", &value, &ch)) {
2587 _nc_warning("expected '%s' rather than '%s'",
2591 } else if (value < 2 || value > 15) {
2592 _nc_warning("expected numeric 2..15 '%s'", find);
2596 _nc_warning("expected numeric suffix for '%s'", find);
2611 * Exiting a video mode should not duplicate sgr0
2614 check_exit_attribute(const char *name, char *test, char *trimmed, char *untrimmed)
2616 if (VALID_STRING(test) && (trimmed != 0)) {
2617 if (similar_sgr(-1, trimmed, test) ||
2618 similar_sgr(-1, untrimmed, test)) {
2619 _nc_warning("%s matches exit_attribute_mode", name);
2625 * Returns true if the string looks like a standard SGR string.
2628 is_sgr_string(char *value)
2630 bool result = FALSE;
2632 if (VALID_STRING(value)) {
2633 int skip = csi_length(value);
2640 while ((ch = UChar(*value++)) != '\0') {
2641 if (isdigit(ch) || ch == ';') {
2643 } else if (ch == 'm' && *value == '\0') {
2656 * Check if the given capability contains a given SGR attribute.
2659 check_sgr_param(TERMTYPE2 *tp, int code, const char *name, char *value)
2661 if (VALID_STRING(value)) {
2662 int ncv = ((code != 0) ? (1 << (code - 1)) : 0);
2663 char *test = tgoto(value, 0, 0);
2664 if (is_sgr_string(test)) {
2668 int color = (value == set_a_foreground ||
2669 value == set_a_background ||
2670 value == set_foreground ||
2671 value == set_background);
2672 while (*test != 0) {
2673 if (isdigit(UChar(*test))) {
2674 param = 10 * param + (*test - '0');
2679 * Avoid unnecessary warning for xterm 256color codes.
2681 if (color && (param == 38 || param == 48))
2683 if ((skips-- <= 0) && (param == code))
2691 if (count != 0 && param == code) {
2693 no_color_video < 0 ||
2694 !(no_color_video & ncv)) {
2695 _nc_warning("\"%s\" SGR-attribute used in %s",
2704 /* other sanity-checks (things that we don't want in the normal
2705 * logic that reads a terminfo entry)
2708 check_termtype(TERMTYPE2 *tp, bool literal)
2714 for_each_string(j, tp) {
2715 char *a = tp->Strings[j];
2716 if (VALID_STRING(a)) {
2717 const char *name = ExtStrname(tp, (int) j, strnames);
2719 * If we expect parameters, or if there might be parameters,
2720 * check for consistent number of parameters.
2722 if (j >= SIZEOF(parametrized) ||
2723 is_user_capability(name) > 0 ||
2724 parametrized[j] > 0) {
2725 check_params(tp, name, a, (j >= STRCOUNT));
2727 check_delays(tp, ExtStrname(tp, (int) j, strnames), a);
2729 check_infotocap(tp, (int) j, a);
2742 * These may be mismatched because the terminal description relies on
2743 * restoring the cursor visibility by resetting it.
2745 ANDMISSING(cursor_invisible, cursor_normal);
2746 ANDMISSING(cursor_visible, cursor_normal);
2748 if (PRESENT(cursor_visible) && PRESENT(cursor_normal)
2749 && !_nc_capcmp(cursor_visible, cursor_normal))
2750 _nc_warning("cursor_visible is same as cursor_normal");
2753 * From XSI & O'Reilly, we gather that sc/rc are required if csr is
2754 * given, because the cursor position after the scrolling operation is
2755 * performed is undefined.
2757 ANDMISSING(change_scroll_region, save_cursor);
2758 ANDMISSING(change_scroll_region, restore_cursor);
2761 * If we can clear tabs, we should be able to initialize them.
2763 ANDMISSING(clear_all_tabs, set_tab);
2765 if (PRESENT(set_attributes)) {
2769 if (PRESENT(exit_attribute_mode)) {
2770 zero = strdup(CHECK_SGR(0, exit_attribute_mode));
2772 zero = strdup(TPARM_9(set_attributes, 0, 0, 0, 0, 0, 0, 0, 0, 0));
2775 _nc_warning("stack error in sgr(0) string");
2778 CHECK_SGR(1, enter_standout_mode);
2779 CHECK_SGR(2, enter_underline_mode);
2780 CHECK_SGR(3, enter_reverse_mode);
2781 CHECK_SGR(4, enter_blink_mode);
2782 CHECK_SGR(5, enter_dim_mode);
2783 CHECK_SGR(6, enter_bold_mode);
2784 CHECK_SGR(7, enter_secure_mode);
2785 CHECK_SGR(8, enter_protected_mode);
2786 CHECK_SGR(9, enter_alt_charset_mode);
2789 _nc_warning("sgr(0) did not return a value");
2791 } else if (PRESENT(exit_attribute_mode) &&
2792 set_attributes != CANCELLED_STRING) {
2793 if (_nc_syntax == SYN_TERMINFO)
2794 _nc_warning("missing sgr string");
2796 #define CHECK_SGR0(name) check_exit_attribute(#name, name, check_sgr0, exit_attribute_mode)
2797 if (PRESENT(exit_attribute_mode)) {
2798 char *check_sgr0 = _nc_trim_sgr0(tp);
2800 if (check_sgr0 == 0 || *check_sgr0 == '\0') {
2801 _nc_warning("trimmed sgr0 is empty");
2804 if (check_sgr0 != exit_attribute_mode) {
2806 ("will trim sgr0\n\toriginal sgr0=%s\n\ttrimmed sgr0=%s",
2807 _nc_visbuf2(1, exit_attribute_mode),
2808 _nc_visbuf2(2, check_sgr0)));
2811 ("will not trim sgr0\n\toriginal sgr0=%s",
2812 _nc_visbuf(exit_attribute_mode)));
2815 #if defined(exit_italics_mode)
2816 CHECK_SGR0(exit_italics_mode);
2818 CHECK_SGR0(exit_standout_mode);
2819 CHECK_SGR0(exit_underline_mode);
2820 if (check_sgr0 != exit_attribute_mode) {
2824 #define CHECK_SGR_PARAM(code, name) check_sgr_param(tp, (int)code, #name, name)
2825 for (j = 0; *sgr_names[j] != '\0'; ++j) {
2826 CHECK_SGR_PARAM(j, set_a_foreground);
2827 CHECK_SGR_PARAM(j, set_a_background);
2828 CHECK_SGR_PARAM(j, set_foreground);
2829 CHECK_SGR_PARAM(j, set_background);
2833 if (!auto_right_margin) {
2835 ("can write to lower-right directly"));
2836 } else if (PRESENT(enter_am_mode) && PRESENT(exit_am_mode)) {
2838 ("can write to lower-right by suppressing automargin"));
2839 } else if ((PRESENT(enter_insert_mode) && PRESENT(exit_insert_mode))
2840 || PRESENT(insert_character) || PRESENT(parm_ich)) {
2842 ("can write to lower-right by using inserts"));
2845 ("cannot write to lower-right"));
2850 * Some standard applications (e.g., vi) and some non-curses
2851 * applications (e.g., jove) get confused if we have both ich1 and
2852 * smir/rmir. Let's be nice and warn about that, too, even though
2853 * ncurses handles it.
2855 if ((PRESENT(enter_insert_mode) || PRESENT(exit_insert_mode))
2856 && PRESENT(parm_ich)) {
2857 _nc_warning("non-curses applications may be confused by ich1 with smir/rmir");
2861 * Finally, do the non-verbose checks
2863 if (save_check_termtype != 0)
2864 save_check_termtype(tp, literal);