1 /****************************************************************************
2 * Copyright 2018-2020,2021 Thomas E. Dickey *
3 * Copyright 1998-2017,2018 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1996 on *
34 ****************************************************************************/
37 * tic.c --- Main program for terminfo compiler
43 #include <progs.priv.h>
46 #include <dump_entry.h>
47 #include <tparm_type.h>
48 #include <hashed_db.h>
49 #include <parametrized.h>
50 #include <transform.h>
52 MODULE_ID("$Id: tic.c,v 1.296 2021/06/26 19:44:08 tom Exp $")
54 #define STDIN_NAME "<stdin>"
56 const char *_nc_progname = "tic";
60 static bool capdump = FALSE; /* running as infotocap? */
61 static bool infodump = FALSE; /* running as captoinfo? */
62 static bool showsummary = FALSE;
63 static unsigned debug_level;
64 static char **namelst = 0;
65 static const char *to_remove;
68 static bool using_extensions = FALSE;
71 static void (*save_check_termtype) (TERMTYPE2 *, bool);
72 static void check_termtype(TERMTYPE2 *tt, bool);
74 static const char usage_string[] = "\
105 free_namelist(char **src)
109 for (n = 0; src[n] != 0; ++n)
120 free_namelist(namelst);
121 _nc_leaks_dump_entry();
125 if (to_remove != 0) {
129 rc = remove(to_remove);
131 rc = unlink(to_remove);
139 failed(const char *msg)
142 ExitProgram(EXIT_FAILURE);
148 #define DATA(s) s "\n"
149 static const char options_string[] =
152 DATA(" -0 format translation output all capabilities on one line")
153 DATA(" -1 format translation output one capability per line")
155 DATA(" -a retain commented-out capabilities (sets -x also)")
157 DATA(" -C translate entries to termcap source form")
158 DATA(" -D print list of tic's database locations (first must be writable)")
159 DATA(" -c check only, validate input without compiling or translating")
160 DATA(" -e<names> translate/compile only entries named by comma-separated list")
161 DATA(" -f format complex strings for readability")
162 DATA(" -G format %{number} to %'char'")
163 DATA(" -g format %'char' to %{number}")
164 DATA(" -I translate entries to terminfo source form")
165 DATA(" -K translate entries to termcap source form with BSD syntax")
166 DATA(" -L translate entries to full terminfo source form")
167 DATA(" -N disable smart defaults for source translation")
168 DATA(" -o<dir> set output directory for compiled entry writes")
169 DATA(" -Q[n] dump compiled description")
170 DATA(" -q brief listing, removes headers")
171 DATA(" -R<name> restrict translation to given terminfo/termcap version")
172 DATA(" -r force resolution of all use entries in source translation")
173 DATA(" -s print summary statistics")
174 DATA(" -T remove size-restrictions on compiled description")
176 DATA(" -t suppress commented-out capabilities")
178 DATA(" -U suppress post-processing of entries")
179 DATA(" -V print version")
180 DATA(" -W wrap long strings according to -w[n] option")
181 DATA(" -v[n] set verbosity level")
182 DATA(" -w[n] set format width for translation output")
184 DATA(" -x treat unknown capabilities as user-defined")
188 DATA(" <file> file to translate or compile")
192 fprintf(stderr, "Usage: %s %s\n", _nc_progname, usage_string);
193 fputs(options_string, stderr);
194 ExitProgram(EXIT_FAILURE);
207 char result[MAX_ENTRY_SIZE];
210 * Look for strings that contain %{number}, convert them to %'char',
211 * which is shorter and runs a little faster.
213 for (n = 0; n < STRCOUNT; n++) {
214 s = ep->tterm.Strings[n];
216 && strchr(s, L_BRACE) != 0) {
219 while ((ch = *t++) != 0) {
222 if ((*d++ = *t++) == '\0')
224 } else if ((ch == '%')
225 && (*t == L_BRACE)) {
227 long value = strtol(t + 1, &v, 0);
231 && value != '\\' /* FIXME */
233 && isprint((int) value)) {
242 if (strlen(result) < strlen(s))
243 _nc_STRCPY(s, result, strlen(s) + 1);
247 _nc_set_type(_nc_first_name(ep->tterm.term_names));
248 _nc_curr_line = (int) ep->startline;
249 _nc_write_entry(&ep->tterm);
253 immedhook(ENTRY * ep GCC_UNUSED)
254 /* write out entries with no use capabilities immediately to save storage */
258 * This is strictly a core-economy kluge. The really clean way to handle
259 * compilation is to slurp the whole file into core and then do all the
260 * name-collision checks and entry writes in one swell foop. But the
261 * terminfo master file is large enough that some core-poor systems swap
262 * like crazy when you compile it this way...there have been reports of
263 * this process taking *three hours*, rather than the twenty seconds or
264 * less typical on my development box.
266 * So. This hook *immediately* writes out the referenced entry if it
267 * has no use capabilities. The compiler main loop refrains from
268 * adding the entry to the in-core list when this hook fires. If some
269 * other entry later needs to reference an entry that got written
270 * immediately, that's OK; the resolution code will fetch it off disk
271 * when it can't find it in core.
273 * Name collisions will still be detected, just not as cleanly. The
274 * write_entry() code complains before overwriting an entry that
275 * postdates the time of tic's first call to write_entry(). Thus
276 * it will complain about overwriting entries newly made during the
277 * tic run, but not about overwriting ones that predate it.
279 * The reason this is a hook, and not in line with the rest of the
280 * compiler code, is that the support for termcap fallback cannot assume
281 * it has anywhere to spool out these entries!
283 * The _nc_set_type() call here requires a compensating one in
286 * If you define HAVE_BIG_CORE, you'll disable this kluge. This will
287 * make tic a bit faster (because the resolution code won't have to do
288 * disk I/O nearly as often).
290 if (ep->nuses == 0) {
291 int oldline = _nc_curr_line;
294 _nc_curr_line = oldline;
295 free(ep->tterm.str_table);
298 #endif /* HAVE_BIG_CORE */
304 /* emit a comment char, translating terminfo names to termcap names */
306 static bool in_name = FALSE;
311 static char *namebuf, *suffix;
313 if (used + 1 >= have) {
315 if ((namebuf = typeRealloc(char, have, namebuf)) == 0)
316 failed("put_translate namebuf");
317 if ((suffix = typeRealloc(char, have, suffix)) == 0)
318 failed("put_translate suffix");
320 if (c == '\n' || c == '@') {
321 namebuf[used++] = '\0';
323 (void) fputs(namebuf, stdout);
326 } else if (c != '>') {
327 namebuf[used++] = (char) c;
328 } else { /* ah! candidate name! */
330 NCURSES_CONST char *tp;
332 namebuf[used++] = '\0';
336 if ((up = strchr(namebuf, '#')) != 0
337 || (up = strchr(namebuf, '=')) != 0
338 || ((up = strchr(namebuf, '@')) != 0 && up[1] == '>')) {
339 _nc_STRCPY(suffix, up, have);
343 if ((tp = nametrans(namebuf)) != 0) {
345 (void) fputs(tp, stdout);
346 (void) fputs(suffix, stdout);
349 /* couldn't find a translation, just dump the name */
351 (void) fputs(namebuf, stdout);
352 (void) fputs(suffix, stdout);
366 /* Returns a string, stripped of leading/trailing whitespace */
372 while (isspace(UChar(*src)))
376 if ((dst = strdup(src)) == NULL) {
379 size_t len = strlen(dst);
380 while (--len != 0 && isspace(UChar(dst[len])))
388 open_tempfile(char *filename)
392 _nc_STRCPY(filename, "/tmp/XXXXXX", PATH_MAX);
395 int oldmask = (int) umask(077);
396 int fd = mkstemp(filename);
398 result = fdopen(fd, "w");
399 umask((mode_t) oldmask);
402 if (tmpnam(filename) != 0)
403 result = safe_fopen(filename, "w");
409 copy_input(FILE *source, const char *filename, char *alt_file)
411 char my_altfile[PATH_MAX];
417 alt_file = my_altfile;
420 failed("copy_input (source)");
421 } else if ((target = open_tempfile(alt_file)) == 0) {
422 failed("copy_input (target)");
429 } else if (ferror(source)) {
431 } else if (ch == 0) {
432 /* don't loop in case someone wants to convert /dev/zero */
433 fprintf(stderr, "%s: %s is not a text-file\n", _nc_progname, filename);
434 ExitProgram(EXIT_FAILURE);
440 * rewind() does not force the target file's data to disk (not does
441 * fflush()...). So open a second stream on the data and then close
442 * the one that we were writing on before starting to read from the
445 result = safe_fopen(alt_file, "r+");
447 to_remove = strdup(alt_file);
453 open_input(const char *filename, char *alt_file)
459 if (!strcmp(filename, "-")) {
460 fp = copy_input(stdin, STDIN_NAME, alt_file);
461 } else if (stat(filename, &sb) < 0) {
462 fprintf(stderr, "%s: %s %s\n", _nc_progname, filename, strerror(errno));
463 ExitProgram(EXIT_FAILURE);
464 } else if ((mode = (sb.st_mode & S_IFMT)) == S_IFDIR
465 || (mode != S_IFREG && mode != S_IFCHR && mode != S_IFIFO)) {
466 fprintf(stderr, "%s: %s is not a file\n", _nc_progname, filename);
467 ExitProgram(EXIT_FAILURE);
469 fp = safe_fopen(filename, "r");
472 fprintf(stderr, "%s: Can't open %s\n", _nc_progname, filename);
473 ExitProgram(EXIT_FAILURE);
475 if (mode != S_IFREG) {
477 FILE *fp2 = copy_input(fp, filename, alt_file);
480 fprintf(stderr, "%s: %s is not a file\n", _nc_progname, filename);
481 ExitProgram(EXIT_FAILURE);
488 /* Parse the "-e" option-value into a list of names */
490 make_namelist(char *src)
495 unsigned pass, n, nn;
500 } else if (strchr(src, '/') != 0) { /* a filename */
501 FILE *fp = open_input(src, (char *) 0);
503 for (pass = 1; pass <= 2; pass++) {
505 while (fgets(buffer, sizeof(buffer), fp) != 0) {
506 if ((s = stripped(buffer)) != 0) {
515 if ((dst = typeCalloc(char *, nn + 1)) == 0)
516 failed("make_namelist");
521 } else { /* literal list of names */
522 for (pass = 1; pass <= 2; pass++) {
523 for (n = nn = 0, base = src;; n++) {
525 if (mark == ',' || mark == '\0') {
530 if ((s = stripped(base)) != 0)
539 if ((dst = typeCalloc(char *, nn + 1)) == 0)
540 failed("make_namelist");
544 if (showsummary && (dst != 0)) {
545 fprintf(log_fp, "Entries that will be compiled:\n");
546 for (n = 0; dst[n] != 0; n++)
547 fprintf(log_fp, "%u:%s\n", n + 1, dst[n]);
553 matches(char **needle, const char *haystack)
554 /* does entry in needle list match |-separated field in haystack? */
561 for (n = 0; needle[n] != 0; n++) {
562 if (_nc_name_match(haystack, needle[n], "|")) {
573 valid_db_path(const char *nominal)
577 char suffix[] = DBM_SUFFIX;
578 size_t need = strlen(nominal) + sizeof(suffix);
579 char *result = malloc(need);
582 failed("valid_db_path");
583 _nc_STRCPY(result, nominal, need);
584 if (strcmp(result + need - sizeof(suffix), suffix)) {
585 _nc_STRCAT(result, suffix, need);
588 char *result = strdup(nominal);
591 DEBUG(1, ("** stat(%s)", result));
592 if (stat(result, &sb) >= 0) {
594 if (!S_ISREG(sb.st_mode)
595 || access(result, R_OK | W_OK) != 0) {
596 DEBUG(1, ("...not a writable file"));
601 if (!S_ISDIR(sb.st_mode)
602 || access(result, R_OK | W_OK | X_OK) != 0) {
603 DEBUG(1, ("...not a writable directory"));
609 /* check if parent is directory and is writable */
610 unsigned leaf = _nc_pathlast(result);
612 DEBUG(1, ("...not found"));
614 char save = result[leaf];
616 if (stat(result, &sb) >= 0
617 && S_ISDIR(sb.st_mode)
618 && access(result, R_OK | W_OK | X_OK) == 0) {
621 DEBUG(1, ("...parent directory %s is not writable", result));
626 DEBUG(1, ("... no parent directory"));
635 * Show the databases to which tic could write. The location to which it
636 * writes is always the first one. If none are writable, print an error
640 show_databases(const char *outdir)
642 bool specific = (outdir != 0) || getenv("TERMINFO") != 0;
644 const char *tried = 0;
647 outdir = _nc_tic_dir(0);
649 if ((result = valid_db_path(outdir)) != 0) {
650 printf("%s\n", result);
656 if ((outdir = _nc_home_terminfo())) {
657 if ((result = valid_db_path(outdir)) != 0) {
658 printf("%s\n", result);
660 } else if (!specific) {
666 * If we can write in neither location, give an error message.
670 fprintf(stderr, "%s: %s (no permission)\n", _nc_progname, tried);
671 ExitProgram(EXIT_FAILURE);
676 add_digit(int *target, int source)
678 *target = (*target * 10) + (source - '0');
682 main(int argc, char *argv[])
684 char my_tmpname[PATH_MAX];
685 char my_altfile[PATH_MAX];
687 int smart_defaults = TRUE;
691 int this_opt, last_opt = '?';
693 int outform = F_TERMINFO; /* output format */
694 int sortmode = S_TERMINFO; /* sort_mode */
698 bool formatted = FALSE; /* reformat complex strings? */
699 bool literal = FALSE; /* suppress post-processing? */
700 int numbers = 0; /* format "%'char'" to/from "%{number}" */
701 bool forceresolve = FALSE; /* force resolution */
703 char *tversion = (char *) NULL;
704 const char *source_file = "terminfo";
705 char *outdir = (char *) NULL;
706 bool check_only = FALSE;
707 bool suppress_untranslatable = FALSE;
710 bool wrap_strings = FALSE;
714 _nc_progname = _nc_rootname(argv[0]);
717 if ((infodump = same_program(_nc_progname, PROG_CAPTOINFO)) != FALSE) {
718 outform = F_TERMINFO;
719 sortmode = S_TERMINFO;
721 if ((capdump = same_program(_nc_progname, PROG_INFOTOCAP)) != FALSE) {
723 sortmode = S_TERMCAP;
726 use_extended_names(FALSE);
731 * Processing arguments is a little complicated, since someone made a
732 * design decision to allow the numeric values for -w, -v options to
735 while ((this_opt = getopt(argc, argv,
736 "0123456789CDIKLNQR:TUVWace:fGgo:qrstvwx")) != -1) {
737 if (isdigit(this_opt)) {
740 add_digit(&quickdump, this_opt);
743 add_digit(&v_opt, this_opt);
746 add_digit(&width, this_opt);
768 /* the initial version of -K in 20110730 fell-thru here, but the
769 * same flag is useful when reading sources -TD
775 sortmode = S_TERMCAP;
778 debug_level = VtoTrace(v_opt);
779 set_trace_level(debug_level);
780 show_databases(outdir);
781 ExitProgram(EXIT_SUCCESS);
785 outform = F_TERMINFO;
786 sortmode = S_TERMINFO;
790 outform = F_VARIABLE;
791 sortmode = S_VARIABLE;
794 smart_defaults = FALSE;
810 puts(curses_version());
811 ExitProgram(EXIT_SUCCESS);
819 namelst = make_namelist(optarg);
850 _nc_disable_period = FALSE;
851 suppress_untranslatable = TRUE;
854 _nc_disable_period = TRUE;
857 use_extended_names(TRUE);
858 using_extensions = TRUE;
867 debug_level = VtoTrace(v_opt);
868 set_trace_level(debug_level);
871 save_check_termtype = _nc_check_termtype2;
872 _nc_check_termtype2 = check_termtype;
876 * Aaargh! immedhook seriously hoses us!
878 * One problem with immedhook is it means we can't do -e. Problem
879 * is that we can't guarantee that for each terminal listed, all the
880 * terminals it depends on will have been kept in core for reference
881 * resolution -- in fact it's certain the primitive types at the end
882 * of reference chains *won't* be in core unless they were explicitly
883 * in the select list themselves.
885 if (namelst && (!infodump && !capdump)) {
886 (void) fprintf(stderr,
887 "%s: Sorry, -e can't be used without -I or -C\n",
889 ExitProgram(EXIT_FAILURE);
891 #endif /* HAVE_BIG_CORE */
894 source_file = argv[optind++];
897 "%s: Too many file names. Usage:\n\t%s %s",
901 ExitProgram(EXIT_FAILURE);
904 if (infodump == TRUE) {
905 /* captoinfo's no-argument case */
906 source_file = "/etc/termcap";
907 if ((termcap = getenv("TERMCAP")) != 0
908 && (namelst = make_namelist(getenv("TERM"))) != 0) {
909 if (access(termcap, F_OK) == 0) {
911 source_file = termcap;
913 if ((tmp_fp = open_tempfile(my_tmpname)) != 0) {
914 source_file = my_tmpname;
915 fprintf(tmp_fp, "%s\n", termcap);
917 tmp_fp = open_input(source_file, (char *) 0);
918 to_remove = source_file;
927 "%s: File name needed. Usage:\n\t%s %s",
931 ExitProgram(EXIT_FAILURE);
936 tmp_fp = open_input(source_file, my_altfile);
937 if (!strcmp(source_file, "-")) {
938 source_file = STDIN_NAME;
942 if (infodump || check_only) {
948 wrap_strings, width, height,
949 debug_level, formatted || check_only, check_only, quickdump);
950 } else if (capdump) {
954 wrap_strings, width, height,
955 debug_level, FALSE, FALSE, FALSE);
958 /* parse entries out of the source file */
959 _nc_set_source(source_file);
961 if (!(check_only || infodump || capdump))
962 _nc_set_writedir(outdir);
963 #endif /* HAVE_BIG_CORE */
964 _nc_read_entry_source(tmp_fp, (char *) NULL,
965 !smart_defaults || literal, FALSE,
966 ((check_only || infodump || capdump)
970 /* do use resolution */
971 if (check_only || (!infodump && !capdump) || forceresolve) {
972 if (!_nc_resolve_uses2(TRUE, literal) && !check_only) {
973 ExitProgram(EXIT_FAILURE);
978 if (check_only && limited && (capdump || infodump)) {
980 if (matches(namelst, qp->tterm.term_names)) {
981 int len = fmt_entry(&qp->tterm, NULL, FALSE, TRUE, infodump, numbers);
983 if (len > (infodump ? MAX_TERMINFO_LENGTH : MAX_TERMCAP_LENGTH))
984 (void) fprintf(stderr,
985 "%s: resolved %s entry is %d bytes long\n",
987 _nc_first_name(qp->tterm.term_names),
993 /* write or dump all entries */
995 /* this is in case infotocap() generates warnings */
996 _nc_curr_col = _nc_curr_line = -1;
999 if (matches(namelst, qp->tterm.term_names)) {
1000 /* this is in case infotocap() generates warnings */
1001 _nc_set_type(_nc_first_name(qp->tterm.term_names));
1002 _nc_curr_line = (int) qp->startline;
1003 repair_acsc(&qp->tterm);
1004 dump_entry(&qp->tterm, suppress_untranslatable,
1005 limited, numbers, NULL);
1009 if (!infodump && !capdump) {
1010 _nc_set_writedir(outdir);
1011 for_entry_list(qp) {
1012 if (matches(namelst, qp->tterm.term_names))
1016 /* this is in case infotocap() generates warnings */
1017 _nc_curr_col = _nc_curr_line = -1;
1019 for_entry_list(qp) {
1020 if (matches(namelst, qp->tterm.term_names)) {
1021 long j = qp->cend - qp->cstart;
1024 /* this is in case infotocap() generates warnings */
1025 _nc_set_type(_nc_first_name(qp->tterm.term_names));
1028 (void) fseek(tmp_fp, qp->cstart, SEEK_SET);
1030 int ch = fgetc(tmp_fp);
1031 if (ch == EOF || ferror(tmp_fp)) {
1033 } else if (infodump) {
1041 repair_acsc(&qp->tterm);
1042 dump_entry(&qp->tterm, suppress_untranslatable,
1043 limited, numbers, NULL);
1044 for (j = 0; j < (long) qp->nuses; j++)
1045 dump_uses(qp->uses[j].name, !capdump);
1047 if (debug_level != 0 && !limited)
1048 printf("# length=%d\n", len);
1051 if (!namelst && _nc_tail && !quiet) {
1053 bool in_comment = FALSE;
1054 bool trailing_comment = FALSE;
1056 (void) fseek(tmp_fp, _nc_tail->cend, SEEK_SET);
1057 while ((c = fgetc(tmp_fp)) != EOF) {
1060 trailing_comment = TRUE;
1066 if (trailing_comment
1067 && (in_comment || (oldc == '\n' && c == '\n')))
1075 /* Show the directory into which entries were written, and the total
1079 && (!(check_only || infodump || capdump))) {
1080 int total = _nc_tic_written();
1082 fprintf(log_fp, "%d entries written to %s\n",
1084 _nc_tic_dir((char *) 0));
1086 fprintf(log_fp, "No entries written\n");
1088 ExitProgram(EXIT_SUCCESS);
1092 * This bit of legerdemain turns all the terminfo variable names into
1093 * references to locations in the arrays Booleans, Numbers, and Strings ---
1094 * precisely what's needed (see comp_parse.c).
1100 * Check if the alternate character-set capabilities are consistent.
1103 check_acs(TERMTYPE2 *tp)
1105 int vt100_smacs = 0;
1106 int vt100_rmacs = 0;
1107 int vt100_enacs = 0;
1110 * ena_acs is not always necessary, but if it is present, the enter/exit
1111 * capabilities should be.
1113 ANDMISSING(ena_acs, enter_alt_charset_mode);
1114 ANDMISSING(ena_acs, exit_alt_charset_mode);
1115 PAIRED(exit_alt_charset_mode, exit_alt_charset_mode);
1118 * vt100-like is frequently used, but perhaps ena_acs is missing, etc.
1120 if (VALID_STRING(enter_alt_charset_mode)) {
1121 vt100_smacs = (!strcmp("\033(0", enter_alt_charset_mode)
1123 : (!strcmp("\016", enter_alt_charset_mode)
1127 if (VALID_STRING(exit_alt_charset_mode)) {
1128 vt100_rmacs = (!strcmp("\033(B", exit_alt_charset_mode)
1130 : (!strcmp("\017", exit_alt_charset_mode)
1134 if (VALID_STRING(ena_acs)) {
1135 vt100_enacs = (!strcmp("\033(B\033)0", ena_acs)
1139 if (vt100_rmacs && vt100_smacs && (vt100_rmacs != vt100_smacs)) {
1140 _nc_warning("rmacs/smacs are inconsistent");
1142 if ((vt100_rmacs == 2) && (vt100_smacs == 2) && vt100_enacs) {
1143 _nc_warning("rmacs/smacs make enacs redundant");
1145 if ((vt100_rmacs == 1) && (vt100_smacs == 1) && !vt100_enacs) {
1146 _nc_warning("VT100-style rmacs/smacs require enacs");
1149 if (VALID_STRING(acs_chars)) {
1150 const char *boxes = "lmkjtuvwqxn";
1156 memset(mapped, 0, sizeof(mapped));
1157 for (p = acs_chars; *p != '\0'; p += 2) {
1159 _nc_warning("acsc has odd number of characters");
1162 mapped[UChar(p[0])] = p[1];
1165 if (mapped[UChar('I')] && !mapped[UChar('i')]) {
1166 _nc_warning("acsc refers to 'I', which is probably an error");
1169 for (p = boxes, q = missing; *p != '\0'; ++p) {
1170 if (!mapped[UChar(p[0])]) {
1176 assert(strlen(missing) <= strlen(boxes));
1177 if (*missing != '\0' && strcmp(missing, boxes)) {
1178 _nc_warning("acsc is missing some line-drawing mapping: %s", missing);
1184 safe_strdup(const char *value)
1188 return strdup(value);
1192 same_color(NCURSES_CONST char *oldcap, NCURSES_CONST char *newcap, int limit)
1194 bool result = FALSE;
1200 for (n = same = 0; n < limit; ++n) {
1201 char *oldvalue = safe_strdup(TIPARM_1(oldcap, n));
1202 char *newvalue = safe_strdup(TIPARM_1(newcap, n));
1203 same += !strcmp(oldvalue, newvalue);
1207 result = (same == limit);
1213 * Check if the color capabilities are consistent
1216 check_colors(TERMTYPE2 *tp)
1220 if ((max_colors > 0) != (max_pairs > 0)
1221 || ((max_colors > max_pairs) && (initialize_pair == 0)))
1222 _nc_warning("inconsistent values for max_colors (%d) and max_pairs (%d)",
1223 max_colors, max_pairs);
1225 PAIRED(set_foreground, set_background);
1226 PAIRED(set_a_foreground, set_a_background);
1227 PAIRED(set_color_pair, initialize_pair);
1229 if (VALID_STRING(set_foreground)
1230 && VALID_STRING(set_a_foreground)) {
1231 if (!_nc_capcmp(set_foreground, set_a_foreground)) {
1232 _nc_warning("expected setf/setaf to be different");
1233 } else if (same_color(set_foreground, set_a_foreground, max_colors)) {
1234 _nc_warning("setf/setaf are equivalent");
1238 if (VALID_STRING(set_background)
1239 && VALID_STRING(set_a_background)) {
1240 if (!_nc_capcmp(set_background, set_a_background)) {
1241 _nc_warning("expected setb/setab to be different");
1242 } else if (same_color(set_background, set_a_background, max_colors)) {
1243 _nc_warning("setb/setab are equivalent");
1247 /* see: has_colors() */
1248 if (VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
1249 && ((VALID_STRING(set_foreground)
1250 && VALID_STRING(set_background))
1251 || (VALID_STRING(set_a_foreground)
1252 && VALID_STRING(set_a_background))
1253 || set_color_pair)) {
1254 if (!VALID_STRING(orig_pair) && !VALID_STRING(orig_colors))
1255 _nc_warning("expected either op/oc string for resetting colors");
1258 if (!VALID_STRING(initialize_pair) &&
1259 !VALID_STRING(initialize_color)) {
1260 _nc_warning("expected initc or initp because ccc is given");
1263 if (VALID_STRING(initialize_pair) ||
1264 VALID_STRING(initialize_color)) {
1265 _nc_warning("expected ccc because initc is given");
1268 value = tigetstr("RGB");
1269 if (VALID_STRING(value)) {
1272 int code = sscanf(value, "%d/%d/%d%c", &r, &g, &b, &bad);
1273 if (code != 3 || r <= 0 || g <= 0 || b <= 0) {
1274 _nc_warning("unexpected value for RGB capability: %s", value);
1280 csi_length(const char *value)
1284 if (value[0] == '\033' && value[1] == '[') {
1286 } else if (UChar(value[0]) == 0x9a) {
1293 keypad_final(const char *string)
1297 if (VALID_STRING(string)
1298 && *string++ == '\033'
1300 && strlen(string) == 1) {
1308 keypad_index(const char *string)
1313 if ((ch = keypad_final(string)) != '\0') {
1314 const char *list = "PQRSwxymtuvlqrsPpn"; /* app-keypad except "Enter" */
1315 char *test = (strchr) (list, ch);
1317 result = (long) (test - list);
1323 * list[] is down, up, left, right
1324 * "left" may be ^H rather than \E[D
1325 * "down" may be ^J rather than \E[B
1326 * But up/right are generally consistently escape sequences for ANSI terminals.
1329 check_ansi_cursor(char *list[4])
1333 bool repeated = FALSE;
1335 for (j = 0; j < 4; ++j) {
1337 for (k = 0; k < j; ++k) {
1339 && !strcmp(list[j], list[k])) {
1340 char *value = _nc_tic_expand(list[k], TRUE, 0);
1341 _nc_warning("repeated cursor control %s\n", value);
1348 size_t prefix = (size_t) csi_length(up);
1353 while (up[suffix] && isdigit(UChar(up[suffix])))
1356 if (prefix && up[suffix] == 'A') {
1358 if (!strcmp(list[0], "\n"))
1360 if (!strcmp(list[2], "\b"))
1363 for (j = 0; j < 4; ++j) {
1366 if (skip[j] || strlen(list[j]) == 1)
1368 if (memcmp(list[j], up, prefix)) {
1369 char *value = _nc_tic_expand(list[j], TRUE, 0);
1370 _nc_warning("inconsistent prefix for %s\n", value);
1373 if (strlen(list[j]) < suffix) {
1374 char *value = _nc_tic_expand(list[j], TRUE, 0);
1375 _nc_warning("inconsistent length for %s, expected %d\n",
1376 value, (int) suffix + 1);
1380 if (list[j][suffix] != want) {
1381 char *value = _nc_tic_expand(list[j], TRUE, 0);
1382 _nc_warning("inconsistent suffix for %s, expected %c, have %c\n",
1383 value, want, list[j][suffix]);
1390 #define EXPECTED(name) if (!PRESENT(name)) _nc_warning("expected " #name)
1391 #define UNEXPECTED(name) if (PRESENT(name)) _nc_warning("unexpected " #name ", for %s", why)
1394 check_noaddress(TERMTYPE2 *tp, const char *why)
1396 UNEXPECTED(column_address);
1397 UNEXPECTED(cursor_address);
1398 UNEXPECTED(cursor_home);
1399 UNEXPECTED(cursor_mem_address);
1400 UNEXPECTED(cursor_to_ll);
1401 UNEXPECTED(row_address);
1402 UNEXPECTED(row_address);
1406 check_cursor(TERMTYPE2 *tp)
1412 check_noaddress(tp, "hard_copy");
1413 } else if (generic_type) {
1414 check_noaddress(tp, "generic_type");
1415 } else if (strchr(tp->term_names, '+') == 0) {
1418 if (PRESENT(column_address))
1420 if (PRESENT(cursor_address))
1422 if (PRESENT(cursor_home))
1424 if (PRESENT(cursor_mem_address))
1426 if (PRESENT(cursor_to_ll))
1428 if (PRESENT(row_address))
1430 if (PRESENT(cursor_down))
1432 if (PRESENT(cursor_up))
1434 if (PRESENT(cursor_left))
1436 if (PRESENT(cursor_right))
1438 if (x < 2 && y < 2) {
1439 _nc_warning("terminal lacks cursor addressing");
1442 _nc_warning("terminal lacks cursor column-addressing");
1444 _nc_warning("terminal lacks cursor row-addressing");
1448 /* it is rare to have an insert-line feature without a matching delete */
1449 ANDMISSING(parm_insert_line, insert_line);
1450 ANDMISSING(parm_delete_line, delete_line);
1451 ANDMISSING(parm_insert_line, parm_delete_line);
1453 /* if we have a parameterized form, then the non-parameterized is easy */
1454 ANDMISSING(parm_down_cursor, cursor_down);
1455 ANDMISSING(parm_up_cursor, cursor_up);
1456 ANDMISSING(parm_left_cursor, cursor_left);
1457 ANDMISSING(parm_right_cursor, cursor_right);
1459 /* Given any of a set of cursor movement, the whole set should be present.
1460 * Technically this is not true (we could use cursor_address to fill in
1461 * unsupported controls), but it is likely.
1464 if (PRESENT(parm_down_cursor)) {
1465 list[count++] = parm_down_cursor;
1467 if (PRESENT(parm_up_cursor)) {
1468 list[count++] = parm_up_cursor;
1470 if (PRESENT(parm_left_cursor)) {
1471 list[count++] = parm_left_cursor;
1473 if (PRESENT(parm_right_cursor)) {
1474 list[count++] = parm_right_cursor;
1477 check_ansi_cursor(list);
1478 } else if (count != 0) {
1479 EXPECTED(parm_down_cursor);
1480 EXPECTED(parm_up_cursor);
1481 EXPECTED(parm_left_cursor);
1482 EXPECTED(parm_right_cursor);
1486 if (PRESENT(cursor_down)) {
1487 list[count++] = cursor_down;
1489 if (PRESENT(cursor_up)) {
1490 list[count++] = cursor_up;
1492 if (PRESENT(cursor_left)) {
1493 list[count++] = cursor_left;
1495 if (PRESENT(cursor_right)) {
1496 list[count++] = cursor_right;
1499 check_ansi_cursor(list);
1500 } else if (count != 0) {
1502 if (PRESENT(cursor_down) && strcmp(cursor_down, "\n"))
1504 if (PRESENT(cursor_left) && strcmp(cursor_left, "\b"))
1506 if (PRESENT(cursor_up) && strlen(cursor_up) > 1)
1508 if (PRESENT(cursor_right) && strlen(cursor_right) > 1)
1511 EXPECTED(cursor_down);
1512 EXPECTED(cursor_up);
1513 EXPECTED(cursor_left);
1514 EXPECTED(cursor_right);
1521 * Do a quick sanity-check for vt100-style keypads to see if the 5-key keypad
1522 * is mapped inconsistently.
1525 check_keypad(TERMTYPE2 *tp)
1529 if (VALID_STRING(key_a1) &&
1530 VALID_STRING(key_a3) &&
1531 VALID_STRING(key_b2) &&
1532 VALID_STRING(key_c1) &&
1533 VALID_STRING(key_c3)) {
1534 char final[MAX_KP + 1];
1539 final[0] = keypad_final(key_a1);
1540 final[1] = keypad_final(key_a3);
1541 final[2] = keypad_final(key_b2);
1542 final[3] = keypad_final(key_c1);
1543 final[4] = keypad_final(key_c3);
1546 /* special case: legacy coding using 1,2,3,0,. on the bottom */
1547 assert(strlen(final) <= MAX_KP);
1548 if (!strcmp(final, "qsrpn"))
1551 list[0] = keypad_index(key_a1);
1552 list[1] = keypad_index(key_a3);
1553 list[2] = keypad_index(key_b2);
1554 list[3] = keypad_index(key_c1);
1555 list[4] = keypad_index(key_c3);
1557 /* check that they're all vt100 keys */
1558 for (j = 0; j < MAX_KP; ++j) {
1564 /* check if they're all in increasing order */
1565 for (j = 1; j < MAX_KP; ++j) {
1566 if (list[j] > list[j - 1]) {
1571 if (increase != (MAX_KP - 1)) {
1576 for (j = 0, last = -1; j < MAX_KP; ++j) {
1581 for (k = 0, kk = -1, test = 100; k < 5; ++k) {
1582 if (list[k] > last &&
1589 assert(strlen(show) < (MAX_KP * 4));
1592 _nc_STRCAT(show, " ka1", sizeof(show));
1595 _nc_STRCAT(show, " ka3", sizeof(show));
1598 _nc_STRCAT(show, " kb2", sizeof(show));
1601 _nc_STRCAT(show, " kc1", sizeof(show));
1604 _nc_STRCAT(show, " kc3", sizeof(show));
1609 _nc_warning("vt100 keypad order inconsistent: %s", show);
1612 } else if (VALID_STRING(key_a1) ||
1613 VALID_STRING(key_a3) ||
1614 VALID_STRING(key_b2) ||
1615 VALID_STRING(key_c1) ||
1616 VALID_STRING(key_c3)) {
1618 if (keypad_index(key_a1) >= 0)
1619 _nc_STRCAT(show, " ka1", sizeof(show));
1620 if (keypad_index(key_a3) >= 0)
1621 _nc_STRCAT(show, " ka3", sizeof(show));
1622 if (keypad_index(key_b2) >= 0)
1623 _nc_STRCAT(show, " kb2", sizeof(show));
1624 if (keypad_index(key_c1) >= 0)
1625 _nc_STRCAT(show, " kc1", sizeof(show));
1626 if (keypad_index(key_c3) >= 0)
1627 _nc_STRCAT(show, " kc3", sizeof(show));
1629 _nc_warning("vt100 keypad map incomplete:%s", show);
1633 * These warnings are useful for consistency checks - it is possible that
1634 * there are real terminals with mismatches in these
1636 ANDMISSING(key_ic, key_dc);
1640 check_printer(TERMTYPE2 *tp)
1643 #if defined(enter_doublewide_mode) && defined(exit_doublewide_mode)
1644 PAIRED(enter_doublewide_mode, exit_doublewide_mode);
1646 #if defined(enter_italics_mode) && defined(exit_italics_mode)
1647 PAIRED(enter_italics_mode, exit_italics_mode);
1649 #if defined(enter_leftward_mode) && defined(exit_leftward_mode)
1650 PAIRED(enter_leftward_mode, exit_leftward_mode);
1652 #if defined(enter_micro_mode) && defined(exit_micro_mode)
1653 PAIRED(enter_micro_mode, exit_micro_mode);
1655 #if defined(enter_shadow_mode) && defined(exit_shadow_mode)
1656 PAIRED(enter_shadow_mode, exit_shadow_mode);
1658 #if defined(enter_subscript_mode) && defined(exit_subscript_mode)
1659 PAIRED(enter_subscript_mode, exit_subscript_mode);
1661 #if defined(enter_superscript_mode) && defined(exit_superscript_mode)
1662 PAIRED(enter_superscript_mode, exit_superscript_mode);
1664 #if defined(enter_upward_mode) && defined(exit_upward_mode)
1665 PAIRED(enter_upward_mode, exit_upward_mode);
1668 #if defined(start_char_set_def) && defined(stop_char_set_def)
1669 ANDMISSING(start_char_set_def, stop_char_set_def);
1672 /* if we have a parameterized form, then the non-parameterized is easy */
1673 #if defined(set_bottom_margin_parm) && defined(set_bottom_margin)
1674 ANDMISSING(set_bottom_margin_parm, set_bottom_margin);
1676 #if defined(set_left_margin_parm) && defined(set_left_margin)
1677 ANDMISSING(set_left_margin_parm, set_left_margin);
1679 #if defined(set_right_margin_parm) && defined(set_right_margin)
1680 ANDMISSING(set_right_margin_parm, set_right_margin);
1682 #if defined(set_top_margin_parm) && defined(set_top_margin)
1683 ANDMISSING(set_top_margin_parm, set_top_margin);
1686 #if defined(parm_down_micro) && defined(micro_down)
1687 ANDMISSING(parm_down_micro, micro_down);
1689 #if defined(parm_left_micro) && defined(micro_left)
1690 ANDMISSING(parm_left_micro, micro_left);
1692 #if defined(parm_right_micro) && defined(micro_right)
1693 ANDMISSING(parm_right_micro, micro_right);
1695 #if defined(parm_up_micro) && defined(micro_up)
1696 ANDMISSING(parm_up_micro, micro_up);
1702 uses_SGR_39_49(const char *value)
1704 return (strstr(value, "39;49") != 0
1705 || strstr(value, "49;39") != 0);
1709 * Check consistency of termcap extensions related to "screen".
1712 check_screen(TERMTYPE2 *tp)
1714 if (_nc_user_definable) {
1715 int have_XT = tigetflag("XT");
1716 int have_XM = tigetflag("XM");
1717 int have_bce = back_color_erase;
1718 bool have_kmouse = FALSE;
1719 bool use_sgr_39_49 = FALSE;
1720 const char *name_39_49 = "orig_pair or orig_colors";
1721 char *name = _nc_first_name(tp->term_names);
1722 bool is_screen = !strncmp(name, "screen", 6);
1723 bool screen_base = (is_screen
1724 && strchr(name, '.') == 0);
1726 if (!VALID_BOOLEAN(have_bce)) {
1729 if (!VALID_BOOLEAN(have_XM)) {
1732 if (!VALID_BOOLEAN(have_XT)) {
1735 if (VALID_STRING(key_mouse)) {
1736 have_kmouse = !strcmp("\033[M", key_mouse);
1739 if (VALID_STRING(orig_pair)) {
1740 name_39_49 = "orig_pair";
1741 use_sgr_39_49 = uses_SGR_39_49(orig_pair);
1743 if (!use_sgr_39_49 && VALID_STRING(orig_colors)) {
1744 name_39_49 = "orig_colors";
1745 use_sgr_39_49 = uses_SGR_39_49(orig_colors);
1749 if (have_XM && have_XT) {
1750 _nc_warning("screen's XT capability conflicts with XM");
1751 } else if (have_XT && screen_base) {
1752 _nc_warning("screen's \"screen\" entries should not have XT set");
1753 } else if (have_XT) {
1754 if (!have_kmouse && is_screen) {
1755 if (VALID_STRING(key_mouse)) {
1756 _nc_warning("value of kmous inconsistent with screen's usage");
1758 _nc_warning("expected kmous capability with XT");
1761 if (max_colors > 0) {
1763 _nc_warning("expected bce capability with XT");
1764 } else if (!use_sgr_39_49) {
1765 _nc_warning("expected %s capability with XT "
1766 "to have 39/49 parameters", name_39_49);
1769 if (VALID_STRING(to_status_line))
1770 _nc_warning("\"tsl\" capability is redundant, given XT");
1774 && !screen_base && strchr(name, '+') == 0) {
1775 _nc_warning("expected XT to be set, given kmous");
1781 #define check_screen(tp) /* nothing */
1785 * Returns the expected number of parameters for the given capability.
1788 expected_params(const char *name)
1790 #define DATA(name,count) { { name }, count }
1792 static const struct {
1796 DATA( "S0", 1 ), /* 'screen' extension */
1799 DATA( "colornm", 1 ),
1843 DATA( "setcolor", 1 ),
1847 DATA( "slength", 1 ),
1848 DATA( "slines", 1 ),
1849 DATA( "smgbp", 1 ), /* 2 if smgtp is not given */
1865 int result = 0; /* function-keys, etc., use none */
1867 for (n = 0; n < SIZEOF(table); n++) {
1868 if (!strcmp(name, table[n].name)) {
1869 result = table[n].count;
1878 * Check for user-capabilities that happen to be used in ncurses' terminal
1882 static struct user_table_entry const *
1883 lookup_user_capability(const char *name)
1885 struct user_table_entry const *result = 0;
1887 result = _nc_find_user_entry(name);
1894 * If a given name is likely to be a user-capability, return the number of
1895 * parameters it would be used with. If not, return -1.
1897 * ncurses assumes that u6 could be used for getting the cursor-position, but
1898 * that is not implemented. Make a special case for that, to quiet needless
1901 * The other string-capability extensions (see terminfo.src) which could have
1902 * parameters such as "Ss", "%u", are not used by ncurses. But we check those
1903 * anyway, to validate the terminfo database.
1906 is_user_capability(const char *name)
1909 if (name[0] == 'u' &&
1910 (name[1] >= '0' && name[1] <= '9') &&
1912 result = (name[1] == '6') ? 2 : 0;
1915 else if (using_extensions) {
1916 struct user_table_entry const *p = lookup_user_capability(name);
1918 result = (int) p->ute_argc;
1926 * Make a quick sanity check for the parameters which are used in the given
1927 * strings. If there are no "%p" tokens, then there should be no other "%"
1931 check_params(TERMTYPE2 *tp, const char *name, char *value, int extended)
1933 int expected = expected_params(name);
1936 bool params[1 + NUM_PARM];
1939 #ifdef set_top_margin_parm
1940 if (!strcmp(name, "smgbp")
1941 && set_top_margin_parm == 0)
1945 for (n = 0; n <= NUM_PARM; n++)
1951 _nc_warning("expected character after %% in %s", name);
1953 } else if (*s == 'p') {
1954 if (*++s == '\0' || !isdigit((int) *s)) {
1955 _nc_warning("expected digit after %%p in %s", name);
1970 int check = is_user_capability(name);
1971 if (check != actual && (check >= 0 && actual >= 0)) {
1972 _nc_warning("extended %s capability has %d parameters, expected %d",
1973 name, actual, check);
1974 } else if (debug_level > 1) {
1975 _nc_warning("extended %s capability has %d parameters, as expected",
1985 _nc_warning("%s refers to parameter 0 (%%p0), which is not allowed", name);
1987 if (value == set_attributes || expected < 0) {
1989 } else if (expected != actual) {
1990 _nc_warning("%s uses %d parameters, expected %d", name,
1992 for (n = 1; n < actual; n++) {
1994 _nc_warning("%s omits parameter %d", name, n);
1999 * Counting "%p" markers does not account for termcap expressions which
2000 * may not have been fully translated. Also, tparm does its own analysis.
2001 * Report differences here.
2004 char *p_is_s[NUM_PARM];
2006 int analyzed = _nc_tparm_analyze(value, p_is_s, &popcount);
2007 if (analyzed < popcount) {
2008 analyzed = popcount;
2010 if (actual != analyzed && expected != analyzed) {
2012 int user_cap = is_user_capability(name);
2013 if ((user_cap == analyzed) && using_extensions) {
2015 } else if (user_cap >= 0) {
2016 _nc_warning("tparm will use %d parameters for %s, expected %d",
2017 analyzed, name, user_cap);
2021 _nc_warning("tparm analyzed %d parameters for %s, expected %d",
2022 analyzed, name, actual);
2029 line_capability(const char *name)
2031 bool result = FALSE;
2032 static const char *table[] =
2034 "csr", /* change_scroll_region */
2035 "clear", /* clear_screen */
2037 "cwin", /* create_window */
2038 "cup", /* cursor_address */
2039 "cud1", /* cursor_down */
2040 "home", /* cursor_home */
2041 "mrcup", /* cursor_mem_address */
2042 "ll", /* cursor_to_ll */
2043 "cuu1", /* cursor_up */
2044 "dl1", /* delete_line */
2045 "hd", /* down_half_line */
2046 "flash", /* flash_screen */
2047 "ff", /* form_feed */
2048 "il1", /* insert_line */
2049 "nel", /* newline */
2050 "dl", /* parm_delete_line */
2051 "cud", /* parm_down_cursor */
2052 "indn", /* parm_index */
2053 "il", /* parm_insert_line */
2054 "rin", /* parm_rindex */
2055 "cuu", /* parm_up_cursor */
2056 "mc0", /* print_screen */
2057 "vpa", /* row_address */
2058 "ind", /* scroll_forward */
2059 "ri", /* scroll_reverse */
2060 "hu", /* up_half_line */
2063 for (n = 0; n < SIZEOF(table); ++n) {
2064 if (!strcmp(name, table[n])) {
2073 * Check for DEC VT100 private mode for reverse video.
2076 skip_DECSCNM(const char *value, int *flag)
2080 int skip = csi_length(value);
2082 value[skip++] == '?' &&
2083 value[skip++] == '5') {
2084 if (value[skip] == 'h') {
2086 } else if (value[skip] == 'l') {
2096 check_delays(TERMTYPE2 *tp, const char *name, const char *value)
2099 const char *first = 0;
2100 const char *last = 0;
2102 for (p = value; *p != '\0'; ++p) {
2103 if (p[0] == '$' && p[1] == '<') {
2104 const char *base = p + 2;
2105 const char *mark = 0;
2107 int proportional = 0;
2112 for (q = base; *q != '\0'; ++q) {
2117 } else if (*q == '*' || *q == '/') {
2124 } else if (!(isalnum(UChar(*q)) || strchr("+-.", *q) != 0)) {
2126 } else if (proportional || mandatory) {
2130 last = *q ? (q + 1) : q;
2134 int rc = sscanf(base, "%f%c", &check_f, &check_c);
2135 if ((rc != 2) || (mark != NULL && (check_c != *mark)) || mixed) {
2136 _nc_warning("syntax error in %s delay '%.*s'", name,
2137 (int) (q - base), base);
2138 } else if (*name == 'k') {
2139 _nc_warning("function-key %s has delay", name);
2140 } else if (proportional && !line_capability(name)) {
2141 _nc_warning("non-line capability using proportional delay: %s", name);
2142 } else if (!xon_xoff &&
2144 strchr(_nc_first_name(tp->term_names), '+') == 0) {
2145 _nc_warning("%s in %s is used since no xon/xoff",
2147 ? "proportional delay"
2152 p = q - 1; /* restart scan */
2157 if (!strcmp(name, "flash") ||
2158 !strcmp(name, "beep")) {
2161 if (first == value || *last == 0) {
2163 * Delay is on one end or the other.
2165 _nc_warning("expected delay embedded within %s", name);
2171 * Check for missing delay when using VT100 reverse-video.
2172 * A real VT100 might not need this, but terminal emulators do.
2174 if ((p = skip_DECSCNM(value, &flag)) != 0 &&
2176 skip_DECSCNM(p, &flag) != 0 &&
2178 _nc_warning("expected a delay in %s", name);
2185 check_1_infotocap(const char *name, NCURSES_CONST char *value, int count)
2189 long numbers[1 + NUM_PARM];
2190 char *strings[1 + NUM_PARM];
2191 char *p_is_s[NUM_PARM];
2193 char blob[NUM_PARM * 10];
2200 for (k = 1; k <= NUM_PARM; k++) {
2203 _nc_SLIMIT(sizeof(blob) - (size_t) (next - blob))
2206 next += strlen(next) + 1;
2209 expect = tparm_type(name);
2210 nparam = _nc_tparm_analyze(value, p_is_s, &ignored);
2211 actual = guess_tparm_type(nparam, p_is_s);
2213 if (expect != actual) {
2214 _nc_warning("%s has mismatched parameters", name);
2220 result = TPARM_2(value, numbers[1], strings[2]);
2223 result = TPARM_3(value, numbers[1], strings[2], strings[3]);
2226 #define myParam(n) numbers[n]
2227 result = TIPARM_9(value,
2241 #define myParam(n) (p_is_s[n - 1] != 0 ? ((TPARM_ARG) strings[n]) : numbers[n])
2242 result = TPARM_9(value,
2255 return strdup(result);
2258 #define IsDelay(ch) ((ch) == '.' || isdigit(UChar(ch)))
2261 parse_delay_value(const char *src, double *delays, int *always)
2269 while (isdigit(UChar(*src))) {
2270 (*delays) = (*delays) * 10 + (*src++ - '0');
2276 while (isdigit(UChar(*src))) {
2278 (*delays) += (*src++ - '0') / gotdot;
2281 while (*src == '*' || *src == '/') {
2282 if (always == 0 && *src == '/')
2284 if (*src++ == '*') {
2291 *delays = -(*delays);
2296 parse_ti_delay(const char *ti, double *delays)
2299 while (*ti != '\0') {
2305 && IsDelay(UChar(ti[2]))) {
2307 const char *last = parse_delay_value(ti + 2, delays, &ignored);
2319 parse_tc_delay(const char *tc, double *delays)
2321 return parse_delay_value(tc, delays, (int *) 0);
2325 * Compare terminfo- and termcap-strings, factoring out delays.
2328 same_ti_tc(const char *ti, const char *tc, bool * embedded)
2331 double ti_delay = 0.0;
2332 double tc_delay = 0.0;
2333 const char *ti_last;
2336 ti_last = parse_ti_delay(ti, &ti_delay);
2337 tc = parse_tc_delay(tc, &tc_delay);
2339 while ((ti < ti_last) && *tc) {
2340 if (*ti == '\\' && ispunct(UChar(ti[1]))) {
2342 if ((*ti == '^') && !strncmp(tc, "\\136", 4)) {
2347 } else if (ti[0] == '$' && ti[1] == '<') {
2349 const char *ss = parse_ti_delay(ti, &no_delay);
2356 if (*tc == '\\' && ispunct(UChar(tc[1]))) {
2359 if (*ti++ != *tc++) {
2369 *embedded = FALSE; /* report only one problem */
2377 * Check terminfo to termcap translation.
2380 check_infotocap(TERMTYPE2 *tp, int i, const char *value)
2382 const char *name = ExtStrname(tp, i, strnames);
2383 int params = ((i < (int) SIZEOF(parametrized))
2387 : has_params(value, FALSE)));
2388 char *ti_value = NULL;
2392 assert(SIZEOF(parametrized) == STRCOUNT);
2393 if (!VALID_STRING(value) || (ti_value = strdup(value)) == NULL) {
2394 _nc_warning("tic-expansion of %s failed", name);
2395 } else if ((tc_value = _nc_infotocap(name, ti_value, params)) == ABSENT_STRING) {
2396 _nc_warning("tic-conversion of %s failed", name);
2397 } else if (params > 0) {
2402 if (!strcmp(name, "setf")
2403 || !strcmp(name, "setb")
2404 || !strcmp(name, "setaf")
2405 || !strcmp(name, "setab")) {
2406 if ((limit = max_colors) > 16)
2409 for (count = 0; count < limit; ++count) {
2410 char *ti_check = check_1_infotocap(name, ti_value, count);
2411 char *tc_check = check_1_infotocap(name, tc_value, count);
2413 if (strcmp(ti_check, tc_check)) {
2415 fprintf(stderr, "check_infotocap(%s)\n", name);
2416 fprintf(stderr, "...ti '%s'\n", _nc_visbuf2(0, ti_value));
2417 fprintf(stderr, "...tc '%s'\n", _nc_visbuf2(0, tc_value));
2420 _nc_warning("tparm-conversion of %s(%d) differs between\n\tterminfo %s\n\ttermcap %s",
2422 _nc_visbuf2(0, ti_check),
2423 _nc_visbuf2(1, tc_check));
2428 } else if (params == 0 && !same_ti_tc(ti_value, tc_value, &embedded)) {
2430 _nc_warning("termcap equivalent of %s cannot use embedded delay", name);
2432 _nc_warning("tic-conversion of %s changed value\n\tfrom %s\n\tto %s",
2433 name, ti_value, tc_value);
2442 while (*s == '/' || isdigit(UChar(*s)))
2448 * Skip a delay altogether, e.g., when comparing a simple string to sgr,
2449 * the latter may have a worst-case delay on the end.
2452 ignore_delays(char *s)
2484 #define DATA(name) { #name }
2485 static const char sgr_names[][11] =
2502 * An sgr string may contain several settings other than the one we're
2503 * interested in, essentially sgr0 + rmacs + whatever. As long as the
2504 * "whatever" is contained in the sgr string, that is close enough for our
2508 similar_sgr(int num, char *a, char *b)
2519 } else if (b[0] == '$'
2521 _nc_warning("did not find delay %s", _nc_visbuf(b));
2523 _nc_warning("checking sgr(%s) %s\n\tcompare to %s\n\tunmatched %s",
2524 sgr_names[num], _nc_visbuf2(1, base_a),
2525 _nc_visbuf2(2, base_b),
2529 } else if (delaying) {
2532 } else if ((*b == '0' || (*b == ';')) && *a == 'm') {
2554 /* ignore delays on the end of the string */
2555 a = ignore_delays(a);
2556 return ((num != 0) || (*a == 0));
2560 check_tparm_err(int num)
2563 _nc_warning("tparam error in sgr(%d): %s", num, sgr_names[num]);
2567 check_sgr(TERMTYPE2 *tp, char *zero, int num, char *cap, const char *name)
2572 test = TIPARM_9(set_attributes,
2584 if (!similar_sgr(num, test, cap)) {
2585 _nc_warning("%s differs from sgr(%d)\n\t%s=%s\n\tsgr(%d)=%s",
2587 name, _nc_visbuf2(1, cap),
2588 num, _nc_visbuf2(2, test));
2590 } else if (_nc_capcmp(test, zero)) {
2591 _nc_warning("sgr(%d) present, but not %s", num, name);
2593 } else if (PRESENT(cap)) {
2594 _nc_warning("sgr(%d) missing, but %s present", num, name);
2596 check_tparm_err(num);
2600 #define CHECK_SGR(num,name) check_sgr(tp, zero, num, name, #name)
2604 * If tic is compiled with TRACE, we'll be able to see the output from the
2605 * DEBUG() macro. But since it doesn't use traceon(), it always goes to
2606 * the standard error. Use this function to make it simpler to follow the
2607 * resulting debug traces.
2610 show_where(unsigned level)
2612 if (_nc_tracing >= DEBUG_LEVEL(level)) {
2613 char my_name[MAX_NAME_SIZE];
2614 _nc_get_type(my_name);
2615 _tracef("\"%s\", line %d, '%s'",
2617 _nc_curr_line, my_name);
2622 #define show_where(level) /* nothing */
2632 get_fkey_list(TERMTYPE2 *tp)
2634 NAME_VALUE *result = typeMalloc(NAME_VALUE, NUM_STRINGS(tp) + 1);
2635 const struct tinfo_fkeys *all_fkeys = _nc_tinfo_fkeys;
2640 failed("get_fkey_list");
2642 for (j = 0; all_fkeys[j].code; j++) {
2643 char *a = tp->Strings[all_fkeys[j].offset];
2644 if (VALID_STRING(a)) {
2645 result[used].keycode = (int) all_fkeys[j].code;
2646 result[used].name = strnames[all_fkeys[j].offset];
2647 result[used].value = a;
2652 for (j = STRCOUNT; j < NUM_STRINGS(tp); ++j) {
2653 const char *name = ExtStrname(tp, (int) j, strnames);
2655 result[used].keycode = -1;
2656 result[used].name = name;
2657 result[used].value = tp->Strings[j];
2662 result[used].keycode = 0;
2667 show_fkey_name(NAME_VALUE * data)
2669 if (data->keycode > 0) {
2670 fprintf(stderr, " %s", keyname(data->keycode));
2671 fprintf(stderr, " (capability \"%s\")", data->name);
2673 fprintf(stderr, " capability \"%s\"", data->name);
2678 * A terminal entry may contain more than one keycode assigned to a given
2679 * string (e.g., KEY_END and KEY_LL). But curses will only return one (the
2680 * last one assigned).
2683 check_conflict(TERMTYPE2 *tp)
2685 bool conflict = FALSE;
2687 if (!(_nc_syntax == SYN_TERMCAP && capdump)) {
2688 char *check = calloc((size_t) (NUM_STRINGS(tp) + 1), sizeof(char));
2689 NAME_VALUE *given = get_fkey_list(tp);
2693 failed("check_conflict");
2695 for (j = 0; given[j].keycode; ++j) {
2696 const char *a = given[j].value;
2699 if (!VALID_STRING(a))
2702 for (k = j + 1; given[k].keycode; k++) {
2703 const char *b = given[k].value;
2705 if (!VALID_STRING(b))
2710 if (!_nc_capcmp(a, b)) {
2715 _nc_warning("conflicting key definitions (using the last)");
2718 fprintf(stderr, "...");
2719 show_fkey_name(given + j);
2720 fprintf(stderr, " is the same as");
2721 show_fkey_name(given + k);
2724 fprintf(stderr, ", ");
2725 show_fkey_name(given + k);
2730 fprintf(stderr, "\n");
2733 if (using_extensions) {
2736 const char *xcurses;
2737 const char *shifted;
2752 * SVr4 curses defines the "xcurses" names listed above except for
2753 * the special cases in the "shifted" column. When using these
2754 * names for xterm's extensions, that was confusing, and resulted
2755 * in adding extended capabilities with "2" (shift) suffix. This
2756 * check warns about unnecessary use of extensions for this quirk.
2758 for (j = 0; given[j].keycode; ++j) {
2759 const char *find = given[j].name;
2763 if (!VALID_STRING(given[j].value))
2766 for (k = 0; table[k].xcurses; ++k) {
2767 const char *test = table[k].xcurses;
2768 size_t size = strlen(test);
2770 if (!strncmp(find, test, size) && strcmp(find, test)) {
2771 switch (sscanf(find + size, "%d%c", &value, &ch)) {
2774 _nc_warning("expected '%s' rather than '%s'",
2778 } else if (value < 2 || value > 15) {
2779 _nc_warning("expected numeric 2..15 '%s'", find);
2783 _nc_warning("expected numeric suffix for '%s'", find);
2798 * Exiting a video mode should not duplicate sgr0
2801 check_exit_attribute(const char *name, char *test, char *trimmed, char *untrimmed)
2803 if (VALID_STRING(test) && (trimmed != 0)) {
2804 if (similar_sgr(-1, trimmed, test) ||
2805 similar_sgr(-1, untrimmed, test)) {
2806 _nc_warning("%s matches exit_attribute_mode", name);
2812 * Returns true if the string looks like a standard SGR string.
2815 is_sgr_string(char *value)
2817 bool result = FALSE;
2819 if (VALID_STRING(value)) {
2820 int skip = csi_length(value);
2827 while ((ch = UChar(*value++)) != '\0') {
2828 if (isdigit(ch) || ch == ';') {
2830 } else if (ch == 'm' && *value == '\0') {
2843 * Check if the given capability contains a given SGR attribute.
2846 check_sgr_param(TERMTYPE2 *tp, int code, const char *name, char *value)
2848 if (VALID_STRING(value)) {
2849 int ncv = ((code != 0) ? (1 << (code - 1)) : 0);
2850 char *test = tgoto(value, 0, 0);
2851 if (is_sgr_string(test)) {
2855 int color = (value == set_a_foreground ||
2856 value == set_a_background ||
2857 value == set_foreground ||
2858 value == set_background);
2859 while (*test != 0) {
2860 if (isdigit(UChar(*test))) {
2861 param = 10 * param + (*test - '0');
2866 * Avoid unnecessary warning for xterm 256color codes.
2868 if (color && (param == 38 || param == 48))
2870 if ((skips-- <= 0) && (param == code))
2878 if (count != 0 && param == code) {
2880 no_color_video < 0 ||
2881 !(no_color_video & ncv)) {
2882 _nc_warning("\"%s\" SGR-attribute used in %s",
2893 standard_type(const char *name)
2896 const struct name_table_entry *np;
2898 if ((np = _nc_find_entry(name, _nc_get_hash_table(0))) != 0) {
2899 result = np->nte_type;
2905 name_of_type(int type)
2907 const char *result = "unknown";
2923 check_user_capability_type(const char *name, int actual)
2925 if (lookup_user_capability(name) == 0) {
2926 int expected = standard_type(name);
2927 if (expected >= 0) {
2928 _nc_warning("expected %s to be %s, but actually %s",
2930 name_of_type(actual),
2931 name_of_type(expected)
2933 } else if (*name != 'k') {
2934 _nc_warning("undocumented %s capability %s",
2935 name_of_type(actual),
2942 /* other sanity-checks (things that we don't want in the normal
2943 * logic that reads a terminfo entry)
2946 check_termtype(TERMTYPE2 *tp, bool literal)
2952 for_each_string(j, tp) {
2953 char *a = tp->Strings[j];
2954 if (VALID_STRING(a)) {
2955 const char *name = ExtStrname(tp, (int) j, strnames);
2957 * If we expect parameters, or if there might be parameters,
2958 * check for consistent number of parameters.
2960 if (j >= SIZEOF(parametrized) ||
2961 is_user_capability(name) >= 0 ||
2962 parametrized[j] > 0) {
2963 check_params(tp, name, a, (j >= STRCOUNT));
2965 check_delays(tp, ExtStrname(tp, (int) j, strnames), a);
2967 check_infotocap(tp, (int) j, a);
2972 /* in extended mode, verify that each extension is expected type */
2973 for_each_ext_boolean(j, tp) {
2974 check_user_capability_type(ExtBoolname(tp, (int) j, strnames), BOOLEAN);
2976 for_each_ext_number(j, tp) {
2977 check_user_capability_type(ExtNumname(tp, (int) j, strnames), NUMBER);
2979 for_each_ext_string(j, tp) {
2980 check_user_capability_type(ExtStrname(tp, (int) j, strnames), STRING);
2982 #endif /* NCURSES_XNAMES */
2992 * These are probably both or none.
2994 PAIRED(parm_index, parm_rindex);
2995 PAIRED(parm_ich, parm_dch);
2998 * These may be mismatched because the terminal description relies on
2999 * restoring the cursor visibility by resetting it.
3001 ANDMISSING(cursor_invisible, cursor_normal);
3002 ANDMISSING(cursor_visible, cursor_normal);
3004 if (PRESENT(cursor_visible) && PRESENT(cursor_normal)
3005 && !_nc_capcmp(cursor_visible, cursor_normal))
3006 _nc_warning("cursor_visible is same as cursor_normal");
3009 * From XSI & O'Reilly, we gather that sc/rc are required if csr is
3010 * given, because the cursor position after the scrolling operation is
3011 * performed is undefined.
3013 ANDMISSING(change_scroll_region, save_cursor);
3014 ANDMISSING(change_scroll_region, restore_cursor);
3017 * If we can clear tabs, we should be able to initialize them.
3019 ANDMISSING(clear_all_tabs, set_tab);
3021 if (PRESENT(set_attributes)) {
3025 if (PRESENT(exit_attribute_mode)) {
3026 zero = strdup(CHECK_SGR(0, exit_attribute_mode));
3028 zero = strdup(TIPARM_9(set_attributes, 0, 0, 0, 0, 0, 0, 0, 0, 0));
3033 CHECK_SGR(1, enter_standout_mode);
3034 CHECK_SGR(2, enter_underline_mode);
3035 CHECK_SGR(3, enter_reverse_mode);
3036 CHECK_SGR(4, enter_blink_mode);
3037 CHECK_SGR(5, enter_dim_mode);
3038 CHECK_SGR(6, enter_bold_mode);
3039 CHECK_SGR(7, enter_secure_mode);
3040 CHECK_SGR(8, enter_protected_mode);
3041 CHECK_SGR(9, enter_alt_charset_mode);
3044 _nc_warning("sgr(0) did not return a value");
3046 } else if (PRESENT(exit_attribute_mode) &&
3047 set_attributes != CANCELLED_STRING) {
3048 if (_nc_syntax == SYN_TERMINFO)
3049 _nc_warning("missing sgr string");
3051 #define CHECK_SGR0(name) check_exit_attribute(#name, name, check_sgr0, exit_attribute_mode)
3052 if (PRESENT(exit_attribute_mode)) {
3053 char *check_sgr0 = _nc_trim_sgr0(tp);
3055 if (check_sgr0 == 0 || *check_sgr0 == '\0') {
3056 _nc_warning("trimmed sgr0 is empty");
3059 if (check_sgr0 != exit_attribute_mode) {
3061 ("will trim sgr0\n\toriginal sgr0=%s\n\ttrimmed sgr0=%s",
3062 _nc_visbuf2(1, exit_attribute_mode),
3063 _nc_visbuf2(2, check_sgr0)));
3066 ("will not trim sgr0\n\toriginal sgr0=%s",
3067 _nc_visbuf(exit_attribute_mode)));
3070 #if defined(exit_italics_mode)
3071 CHECK_SGR0(exit_italics_mode);
3073 CHECK_SGR0(exit_standout_mode);
3074 CHECK_SGR0(exit_underline_mode);
3075 if (check_sgr0 != exit_attribute_mode) {
3079 #define CHECK_SGR_PARAM(code, name) check_sgr_param(tp, (int)code, #name, name)
3080 for (j = 0; *sgr_names[j] != '\0'; ++j) {
3081 CHECK_SGR_PARAM(j, set_a_foreground);
3082 CHECK_SGR_PARAM(j, set_a_background);
3083 CHECK_SGR_PARAM(j, set_foreground);
3084 CHECK_SGR_PARAM(j, set_background);
3088 if (!auto_right_margin) {
3090 ("can write to lower-right directly"));
3091 } else if (PRESENT(enter_am_mode) && PRESENT(exit_am_mode)) {
3093 ("can write to lower-right by suppressing automargin"));
3094 } else if ((PRESENT(enter_insert_mode) && PRESENT(exit_insert_mode))
3095 || PRESENT(insert_character) || PRESENT(parm_ich)) {
3097 ("can write to lower-right by using inserts"));
3100 ("cannot write to lower-right"));
3105 * Some standard applications (e.g., vi) and some non-curses
3106 * applications (e.g., jove) get confused if we have both ich1 and
3107 * smir/rmir. Let's be nice and warn about that, too, even though
3108 * ncurses handles it.
3110 if ((PRESENT(enter_insert_mode) || PRESENT(exit_insert_mode))
3111 && PRESENT(insert_character)) {
3112 _nc_warning("non-curses applications may be confused by ich1 with smir/rmir");
3116 * Finally, do the non-verbose checks
3118 if (save_check_termtype != 0)
3119 save_check_termtype(tp, literal);