1 /****************************************************************************
2 * Copyright (c) 1998-2016,2017 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.232 2017/04/13 09:12:05 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;
65 static void (*save_check_termtype) (TERMTYPE2 *, bool);
66 static void check_termtype(TERMTYPE2 *tt, bool);
68 static const char usage_string[] = "\
99 free_namelist(char **src)
103 for (n = 0; src[n] != 0; ++n)
116 free_namelist(namelst);
117 _nc_leaks_dump_entry();
121 if (to_remove != 0) {
123 rc = remove(to_remove);
125 rc = unlink(to_remove);
133 failed(const char *msg)
136 ExitProgram(EXIT_FAILURE);
142 #define DATA(s) s "\n"
143 static const char options_string[] =
146 DATA(" -0 format translation output all capabilities on one line")
147 DATA(" -1 format translation output one capability per line")
149 DATA(" -a retain commented-out capabilities (sets -x also)")
151 DATA(" -C translate entries to termcap source form")
152 DATA(" -D print list of tic's database locations (first must be writable)")
153 DATA(" -c check only, validate input without compiling or translating")
154 DATA(" -e<names> translate/compile only entries named by comma-separated list")
155 DATA(" -f format complex strings for readability")
156 DATA(" -G format %{number} to %'char'")
157 DATA(" -g format %'char' to %{number}")
158 DATA(" -I translate entries to terminfo source form")
159 DATA(" -K translate entries to termcap source form with BSD syntax")
160 DATA(" -L translate entries to full terminfo source form")
161 DATA(" -N disable smart defaults for source translation")
162 DATA(" -o<dir> set output directory for compiled entry writes")
163 DATA(" -Q[n] dump compiled description")
164 DATA(" -q brief listing, removes headers")
165 DATA(" -R<name> restrict translation to given terminfo/termcap version")
166 DATA(" -r force resolution of all use entries in source translation")
167 DATA(" -s print summary statistics")
168 DATA(" -T remove size-restrictions on compiled description")
170 DATA(" -t suppress commented-out capabilities")
172 DATA(" -U suppress post-processing of entries")
173 DATA(" -V print version")
174 DATA(" -W wrap long strings according to -w[n] option")
175 DATA(" -v[n] set verbosity level")
176 DATA(" -w[n] set format width for translation output")
178 DATA(" -x treat unknown capabilities as user-defined")
182 DATA(" <file> file to translate or compile")
186 fprintf(stderr, "Usage: %s %s\n", _nc_progname, usage_string);
187 fputs(options_string, stderr);
188 ExitProgram(EXIT_FAILURE);
201 char result[MAX_ENTRY_SIZE];
204 * Look for strings that contain %{number}, convert them to %'char',
205 * which is shorter and runs a little faster.
207 for (n = 0; n < STRCOUNT; n++) {
208 s = ep->tterm.Strings[n];
210 && strchr(s, L_BRACE) != 0) {
213 while ((ch = *t++) != 0) {
217 } else if ((ch == '%')
218 && (*t == L_BRACE)) {
220 long value = strtol(t + 1, &v, 0);
224 && value != '\\' /* FIXME */
226 && isprint((int) value)) {
235 if (strlen(result) < strlen(s))
236 _nc_STRCPY(s, result, strlen(s) + 1);
240 _nc_set_type(_nc_first_name(ep->tterm.term_names));
241 _nc_curr_line = (int) ep->startline;
242 _nc_write_entry(&ep->tterm);
246 immedhook(ENTRY * ep GCC_UNUSED)
247 /* write out entries with no use capabilities immediately to save storage */
251 * This is strictly a core-economy kluge. The really clean way to handle
252 * compilation is to slurp the whole file into core and then do all the
253 * name-collision checks and entry writes in one swell foop. But the
254 * terminfo master file is large enough that some core-poor systems swap
255 * like crazy when you compile it this way...there have been reports of
256 * this process taking *three hours*, rather than the twenty seconds or
257 * less typical on my development box.
259 * So. This hook *immediately* writes out the referenced entry if it
260 * has no use capabilities. The compiler main loop refrains from
261 * adding the entry to the in-core list when this hook fires. If some
262 * other entry later needs to reference an entry that got written
263 * immediately, that's OK; the resolution code will fetch it off disk
264 * when it can't find it in core.
266 * Name collisions will still be detected, just not as cleanly. The
267 * write_entry() code complains before overwriting an entry that
268 * postdates the time of tic's first call to write_entry(). Thus
269 * it will complain about overwriting entries newly made during the
270 * tic run, but not about overwriting ones that predate it.
272 * The reason this is a hook, and not in line with the rest of the
273 * compiler code, is that the support for termcap fallback cannot assume
274 * it has anywhere to spool out these entries!
276 * The _nc_set_type() call here requires a compensating one in
279 * If you define HAVE_BIG_CORE, you'll disable this kluge. This will
280 * make tic a bit faster (because the resolution code won't have to do
281 * disk I/O nearly as often).
283 if (ep->nuses == 0) {
284 int oldline = _nc_curr_line;
287 _nc_curr_line = oldline;
288 free(ep->tterm.str_table);
291 #endif /* HAVE_BIG_CORE */
297 /* emit a comment char, translating terminfo names to termcap names */
299 static bool in_name = FALSE;
300 static size_t have, used;
301 static char *namebuf, *suffix;
304 if (used + 1 >= have) {
306 if ((namebuf = typeRealloc(char, have, namebuf)) == 0)
307 failed("put_translate namebuf");
308 if ((suffix = typeRealloc(char, have, suffix)) == 0)
309 failed("put_translate suffix");
311 if (c == '\n' || c == '@') {
312 namebuf[used++] = '\0';
314 (void) fputs(namebuf, stdout);
317 } else if (c != '>') {
318 namebuf[used++] = (char) c;
319 } else { /* ah! candidate name! */
321 NCURSES_CONST char *tp;
323 namebuf[used++] = '\0';
327 if ((up = strchr(namebuf, '#')) != 0
328 || (up = strchr(namebuf, '=')) != 0
329 || ((up = strchr(namebuf, '@')) != 0 && up[1] == '>')) {
330 _nc_STRCPY(suffix, up, have);
334 if ((tp = nametrans(namebuf)) != 0) {
336 (void) fputs(tp, stdout);
337 (void) fputs(suffix, stdout);
340 /* couldn't find a translation, just dump the name */
342 (void) fputs(namebuf, stdout);
343 (void) fputs(suffix, stdout);
357 /* Returns a string, stripped of leading/trailing whitespace */
363 while (isspace(UChar(*src)))
369 if ((dst = strdup(src)) == NULL) {
373 while (--len != 0 && isspace(UChar(dst[len])))
381 open_tempfile(char *filename)
385 _nc_STRCPY(filename, "/tmp/XXXXXX", PATH_MAX);
388 int oldmask = (int) umask(077);
389 int fd = mkstemp(filename);
391 result = fdopen(fd, "w");
392 umask((mode_t) oldmask);
395 if (tmpnam(filename) != 0)
396 result = fopen(filename, "w");
402 copy_input(FILE *source, const char *filename, char *alt_file)
404 char my_altfile[PATH_MAX];
410 alt_file = my_altfile;
413 failed("copy_input (source)");
414 } else if ((target = open_tempfile(alt_file)) == 0) {
415 failed("copy_input (target)");
422 } else if (ferror(source)) {
424 } else if (ch == 0) {
425 /* don't loop in case someone wants to convert /dev/zero */
426 fprintf(stderr, "%s: %s is not a text-file\n", _nc_progname, filename);
427 ExitProgram(EXIT_FAILURE);
433 * rewind() does not force the target file's data to disk (not does
434 * fflush()...). So open a second stream on the data and then close
435 * the one that we were writing on before starting to read from the
438 result = fopen(alt_file, "r+");
440 to_remove = strdup(alt_file);
446 open_input(const char *filename, char *alt_file)
452 if (!strcmp(filename, "-")) {
453 fp = copy_input(stdin, STDIN_NAME, alt_file);
454 } else if (stat(filename, &sb) < 0) {
455 fprintf(stderr, "%s: %s %s\n", _nc_progname, filename, strerror(errno));
456 ExitProgram(EXIT_FAILURE);
457 } else if ((mode = (sb.st_mode & S_IFMT)) == S_IFDIR
458 || (mode != S_IFREG && mode != S_IFCHR && mode != S_IFIFO)) {
459 fprintf(stderr, "%s: %s is not a file\n", _nc_progname, filename);
460 ExitProgram(EXIT_FAILURE);
462 fp = fopen(filename, "r");
465 fprintf(stderr, "%s: Can't open %s\n", _nc_progname, filename);
466 ExitProgram(EXIT_FAILURE);
468 if (mode != S_IFREG) {
470 FILE *fp2 = copy_input(fp, filename, alt_file);
473 fprintf(stderr, "%s: %s is not a file\n", _nc_progname, filename);
474 ExitProgram(EXIT_FAILURE);
481 /* Parse the "-e" option-value into a list of names */
483 make_namelist(char *src)
488 unsigned pass, n, nn;
493 } else if (strchr(src, '/') != 0) { /* a filename */
494 FILE *fp = open_input(src, (char *) 0);
496 for (pass = 1; pass <= 2; pass++) {
498 while (fgets(buffer, sizeof(buffer), fp) != 0) {
499 if ((s = stripped(buffer)) != 0) {
508 if ((dst = typeCalloc(char *, nn + 1)) == 0)
509 failed("make_namelist");
514 } else { /* literal list of names */
515 for (pass = 1; pass <= 2; pass++) {
516 for (n = nn = 0, base = src;; n++) {
518 if (mark == ',' || mark == '\0') {
523 if ((s = stripped(base)) != 0)
532 if ((dst = typeCalloc(char *, nn + 1)) == 0)
533 failed("make_namelist");
537 if (showsummary && (dst != 0)) {
538 fprintf(log_fp, "Entries that will be compiled:\n");
539 for (n = 0; dst[n] != 0; n++)
540 fprintf(log_fp, "%u:%s\n", n + 1, dst[n]);
546 matches(char **needle, const char *haystack)
547 /* does entry in needle list match |-separated field in haystack? */
553 for (n = 0; needle[n] != 0; n++) {
554 if (_nc_name_match(haystack, needle[n], "|")) {
565 valid_db_path(const char *nominal)
569 char suffix[] = DBM_SUFFIX;
570 size_t need = strlen(nominal) + sizeof(suffix);
571 char *result = malloc(need);
574 failed("valid_db_path");
575 _nc_STRCPY(result, nominal, need);
576 if (strcmp(result + need - sizeof(suffix), suffix)) {
577 _nc_STRCAT(result, suffix, need);
580 char *result = strdup(nominal);
583 DEBUG(1, ("** stat(%s)", result));
584 if (stat(result, &sb) >= 0) {
586 if (!S_ISREG(sb.st_mode)
587 || access(result, R_OK | W_OK) != 0) {
588 DEBUG(1, ("...not a writable file"));
593 if (!S_ISDIR(sb.st_mode)
594 || access(result, R_OK | W_OK | X_OK) != 0) {
595 DEBUG(1, ("...not a writable directory"));
601 /* check if parent is directory and is writable */
602 unsigned leaf = _nc_pathlast(result);
604 DEBUG(1, ("...not found"));
606 char save = result[leaf];
608 if (stat(result, &sb) >= 0
609 && S_ISDIR(sb.st_mode)
610 && access(result, R_OK | W_OK | X_OK) == 0) {
613 DEBUG(1, ("...parent directory %s is not writable", result));
618 DEBUG(1, ("... no parent directory"));
627 * Show the databases to which tic could write. The location to which it
628 * writes is always the first one. If none are writable, print an error
632 show_databases(const char *outdir)
634 bool specific = (outdir != 0) || getenv("TERMINFO") != 0;
636 const char *tried = 0;
639 outdir = _nc_tic_dir(0);
641 if ((result = valid_db_path(outdir)) != 0) {
642 printf("%s\n", result);
648 if ((outdir = _nc_home_terminfo())) {
649 if ((result = valid_db_path(outdir)) != 0) {
650 printf("%s\n", result);
652 } else if (!specific) {
658 * If we can write in neither location, give an error message.
662 fprintf(stderr, "%s: %s (no permission)\n", _nc_progname, tried);
663 ExitProgram(EXIT_FAILURE);
668 add_digit(int *target, int source)
670 *target = (*target * 10) + (source - '0');
673 #define VtoTrace(opt) (unsigned) ((opt > 0) ? opt : (opt == 0))
676 main(int argc, char *argv[])
678 char my_tmpname[PATH_MAX];
679 char my_altfile[PATH_MAX];
681 unsigned debug_level;
682 int smart_defaults = TRUE;
686 int this_opt, last_opt = '?';
688 int outform = F_TERMINFO; /* output format */
689 int sortmode = S_TERMINFO; /* sort_mode */
693 bool formatted = FALSE; /* reformat complex strings? */
694 bool literal = FALSE; /* suppress post-processing? */
695 int numbers = 0; /* format "%'char'" to/from "%{number}" */
696 bool forceresolve = FALSE; /* force resolution */
698 char *tversion = (char *) NULL;
699 const char *source_file = "terminfo";
700 char *outdir = (char *) NULL;
701 bool check_only = FALSE;
702 bool suppress_untranslatable = FALSE;
705 bool wrap_strings = FALSE;
709 _nc_progname = _nc_rootname(argv[0]);
712 if ((infodump = same_program(_nc_progname, PROG_CAPTOINFO)) != FALSE) {
713 outform = F_TERMINFO;
714 sortmode = S_TERMINFO;
716 if ((capdump = same_program(_nc_progname, PROG_INFOTOCAP)) != FALSE) {
718 sortmode = S_TERMCAP;
721 use_extended_names(FALSE);
726 * Processing arguments is a little complicated, since someone made a
727 * design decision to allow the numeric values for -w, -v options to
730 while ((this_opt = getopt(argc, argv,
731 "0123456789CDIKLNQR:TUVWace:fGgo:qrstvwx")) != -1) {
732 if (isdigit(this_opt)) {
735 add_digit(&quickdump, this_opt);
738 add_digit(&v_opt, this_opt);
741 add_digit(&width, this_opt);
763 /* the initial version of -K in 20110730 fell-thru here, but the
764 * same flag is useful when reading sources -TD
770 sortmode = S_TERMCAP;
773 debug_level = VtoTrace(v_opt);
774 set_trace_level(debug_level);
775 show_databases(outdir);
776 ExitProgram(EXIT_SUCCESS);
780 outform = F_TERMINFO;
781 sortmode = S_TERMINFO;
785 outform = F_VARIABLE;
786 sortmode = S_VARIABLE;
789 smart_defaults = FALSE;
805 puts(curses_version());
806 ExitProgram(EXIT_SUCCESS);
814 namelst = make_namelist(optarg);
845 _nc_disable_period = FALSE;
846 suppress_untranslatable = TRUE;
849 _nc_disable_period = TRUE;
852 use_extended_names(TRUE);
861 debug_level = VtoTrace(v_opt);
862 set_trace_level(debug_level);
865 save_check_termtype = _nc_check_termtype2;
866 _nc_check_termtype2 = check_termtype;
870 * Aaargh! immedhook seriously hoses us!
872 * One problem with immedhook is it means we can't do -e. Problem
873 * is that we can't guarantee that for each terminal listed, all the
874 * terminals it depends on will have been kept in core for reference
875 * resolution -- in fact it's certain the primitive types at the end
876 * of reference chains *won't* be in core unless they were explicitly
877 * in the select list themselves.
879 if (namelst && (!infodump && !capdump)) {
880 (void) fprintf(stderr,
881 "%s: Sorry, -e can't be used without -I or -C\n",
883 ExitProgram(EXIT_FAILURE);
885 #endif /* HAVE_BIG_CORE */
888 source_file = argv[optind++];
891 "%s: Too many file names. Usage:\n\t%s %s",
895 ExitProgram(EXIT_FAILURE);
898 if (infodump == TRUE) {
899 /* captoinfo's no-argument case */
900 source_file = "/etc/termcap";
901 if ((termcap = getenv("TERMCAP")) != 0
902 && (namelst = make_namelist(getenv("TERM"))) != 0) {
903 if (access(termcap, F_OK) == 0) {
905 source_file = termcap;
907 if ((tmp_fp = open_tempfile(my_tmpname)) != 0) {
908 source_file = my_tmpname;
909 fprintf(tmp_fp, "%s\n", termcap);
911 tmp_fp = open_input(source_file, (char *) 0);
912 to_remove = source_file;
921 "%s: File name needed. Usage:\n\t%s %s",
925 ExitProgram(EXIT_FAILURE);
930 tmp_fp = open_input(source_file, my_altfile);
931 if (!strcmp(source_file, "-")) {
932 source_file = STDIN_NAME;
936 if (infodump || check_only) {
942 wrap_strings, width, height,
943 debug_level, formatted || check_only, check_only, quickdump);
944 } else if (capdump) {
948 wrap_strings, width, height,
949 debug_level, FALSE, FALSE, FALSE);
952 /* parse entries out of the source file */
953 _nc_set_source(source_file);
955 if (!(check_only || infodump || capdump))
956 _nc_set_writedir(outdir);
957 #endif /* HAVE_BIG_CORE */
958 _nc_read_entry_source(tmp_fp, (char *) NULL,
959 !smart_defaults || literal, FALSE,
960 ((check_only || infodump || capdump)
964 /* do use resolution */
965 if (check_only || (!infodump && !capdump) || forceresolve) {
966 if (!_nc_resolve_uses2(TRUE, literal) && !check_only) {
967 ExitProgram(EXIT_FAILURE);
972 if (check_only && limited && (capdump || infodump)) {
974 if (matches(namelst, qp->tterm.term_names)) {
975 int len = fmt_entry(&qp->tterm, NULL, FALSE, TRUE, infodump, numbers);
977 if (len > (infodump ? MAX_TERMINFO_LENGTH : MAX_TERMCAP_LENGTH))
978 (void) fprintf(stderr,
979 "warning: resolved %s entry is %d bytes long\n",
980 _nc_first_name(qp->tterm.term_names),
986 /* write or dump all entries */
988 /* this is in case infotocap() generates warnings */
989 _nc_curr_col = _nc_curr_line = -1;
992 if (matches(namelst, qp->tterm.term_names)) {
993 /* this is in case infotocap() generates warnings */
994 _nc_set_type(_nc_first_name(qp->tterm.term_names));
995 _nc_curr_line = (int) qp->startline;
996 repair_acsc(&qp->tterm);
997 dump_entry(&qp->tterm, suppress_untranslatable,
998 limited, numbers, NULL);
1002 if (!infodump && !capdump) {
1003 _nc_set_writedir(outdir);
1004 for_entry_list(qp) {
1005 if (matches(namelst, qp->tterm.term_names))
1009 /* this is in case infotocap() generates warnings */
1010 _nc_curr_col = _nc_curr_line = -1;
1012 for_entry_list(qp) {
1013 if (matches(namelst, qp->tterm.term_names)) {
1014 long j = qp->cend - qp->cstart;
1017 /* this is in case infotocap() generates warnings */
1018 _nc_set_type(_nc_first_name(qp->tterm.term_names));
1021 (void) fseek(tmp_fp, qp->cstart, SEEK_SET);
1024 (void) putchar(fgetc(tmp_fp));
1026 put_translate(fgetc(tmp_fp));
1030 repair_acsc(&qp->tterm);
1031 dump_entry(&qp->tterm, suppress_untranslatable,
1032 limited, numbers, NULL);
1033 for (j = 0; j < (long) qp->nuses; j++)
1034 dump_uses(qp->uses[j].name, !capdump);
1036 if (debug_level != 0 && !limited)
1037 printf("# length=%d\n", len);
1040 if (!namelst && _nc_tail && !quiet) {
1042 bool in_comment = FALSE;
1043 bool trailing_comment = FALSE;
1045 (void) fseek(tmp_fp, _nc_tail->cend, SEEK_SET);
1046 while ((c = fgetc(tmp_fp)) != EOF) {
1049 trailing_comment = TRUE;
1055 if (trailing_comment
1056 && (in_comment || (oldc == '\n' && c == '\n')))
1064 /* Show the directory into which entries were written, and the total
1068 && (!(check_only || infodump || capdump))) {
1069 int total = _nc_tic_written();
1071 fprintf(log_fp, "%d entries written to %s\n",
1073 _nc_tic_dir((char *) 0));
1075 fprintf(log_fp, "No entries written\n");
1077 ExitProgram(EXIT_SUCCESS);
1081 * This bit of legerdemain turns all the terminfo variable names into
1082 * references to locations in the arrays Booleans, Numbers, and Strings ---
1083 * precisely what's needed (see comp_parse.c).
1089 * Check if the alternate character-set capabilities are consistent.
1092 check_acs(TERMTYPE2 *tp)
1094 if (VALID_STRING(acs_chars)) {
1095 const char *boxes = "lmkjtuvwqxn";
1101 memset(mapped, 0, sizeof(mapped));
1102 for (p = acs_chars; *p != '\0'; p += 2) {
1104 _nc_warning("acsc has odd number of characters");
1107 mapped[UChar(p[0])] = p[1];
1110 if (mapped[UChar('I')] && !mapped[UChar('i')]) {
1111 _nc_warning("acsc refers to 'I', which is probably an error");
1114 for (p = boxes, q = missing; *p != '\0'; ++p) {
1115 if (!mapped[UChar(p[0])]) {
1121 assert(strlen(missing) <= strlen(boxes));
1122 if (*missing != '\0' && strcmp(missing, boxes)) {
1123 _nc_warning("acsc is missing some line-drawing mapping: %s", missing);
1129 * Check if the color capabilities are consistent
1132 check_colors(TERMTYPE2 *tp)
1134 if ((max_colors > 0) != (max_pairs > 0)
1135 || ((max_colors > max_pairs) && (initialize_pair == 0)))
1136 _nc_warning("inconsistent values for max_colors (%d) and max_pairs (%d)",
1137 max_colors, max_pairs);
1139 PAIRED(set_foreground, set_background);
1140 PAIRED(set_a_foreground, set_a_background);
1141 PAIRED(set_color_pair, initialize_pair);
1143 if (VALID_STRING(set_foreground)
1144 && VALID_STRING(set_a_foreground)
1145 && !_nc_capcmp(set_foreground, set_a_foreground))
1146 _nc_warning("expected setf/setaf to be different");
1148 if (VALID_STRING(set_background)
1149 && VALID_STRING(set_a_background)
1150 && !_nc_capcmp(set_background, set_a_background))
1151 _nc_warning("expected setb/setab to be different");
1153 /* see: has_colors() */
1154 if (VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
1155 && (((set_foreground != NULL)
1156 && (set_background != NULL))
1157 || ((set_a_foreground != NULL)
1158 && (set_a_background != NULL))
1159 || set_color_pair)) {
1160 if (!VALID_STRING(orig_pair) && !VALID_STRING(orig_colors))
1161 _nc_warning("expected either op/oc string for resetting colors");
1164 if (!VALID_STRING(initialize_pair) &&
1165 !VALID_STRING(initialize_color)) {
1166 _nc_warning("expected initc or initp because ccc is given");
1169 if (VALID_STRING(initialize_pair) ||
1170 VALID_STRING(initialize_color)) {
1171 _nc_warning("expected ccc because initc is given");
1177 csi_length(const char *value)
1181 if (value[0] == '\033' && value[1] == '[') {
1183 } else if (UChar(value[0]) == 0x9a) {
1190 keypad_final(const char *string)
1194 if (VALID_STRING(string)
1195 && *string++ == '\033'
1197 && strlen(string) == 1) {
1205 keypad_index(const char *string)
1208 const char *list = "PQRSwxymtuvlqrsPpn"; /* app-keypad except "Enter" */
1212 if ((ch = keypad_final(string)) != '\0') {
1213 test = (strchr) (list, ch);
1215 result = (long) (test - list);
1221 * list[] is down, up, left, right
1222 * "left" may be ^H rather than \E[D
1223 * "down" may be ^J rather than \E[B
1224 * But up/right are generally consistently escape sequences for ANSI terminals.
1227 check_ansi_cursor(char *list[4])
1233 bool repeated = FALSE;
1235 for (j = 0; j < 4; ++j) {
1237 for (k = 0; k < j; ++k) {
1239 && !strcmp(list[j], list[k])) {
1240 char *value = _nc_tic_expand(list[k], TRUE, 0);
1241 _nc_warning("repeated cursor control %s\n", value);
1248 size_t prefix = (size_t) csi_length(up);
1252 while (up[suffix] && isdigit(UChar(up[suffix])))
1255 if (prefix && up[suffix] == 'A') {
1257 if (!strcmp(list[0], "\n"))
1259 if (!strcmp(list[2], "\b"))
1262 for (j = 0; j < 4; ++j) {
1263 if (skip[j] || strlen(list[j]) == 1)
1265 if (memcmp(list[j], up, prefix)) {
1266 char *value = _nc_tic_expand(list[j], TRUE, 0);
1267 _nc_warning("inconsistent prefix for %s\n", value);
1270 if (strlen(list[j]) < suffix) {
1271 char *value = _nc_tic_expand(list[j], TRUE, 0);
1272 _nc_warning("inconsistent length for %s, expected %d\n",
1273 value, (int) suffix + 1);
1277 if (list[j][suffix] != want) {
1278 char *value = _nc_tic_expand(list[j], TRUE, 0);
1279 _nc_warning("inconsistent suffix for %s, expected %c, have %c\n",
1280 value, want, list[j][suffix]);
1287 #define EXPECTED(name) if (!PRESENT(name)) _nc_warning("expected " #name)
1288 #define UNEXPECTED(name) if (PRESENT(name)) _nc_warning("unexpected " #name ", for %s", why)
1291 check_noaddress(TERMTYPE2 *tp, const char *why)
1293 UNEXPECTED(column_address);
1294 UNEXPECTED(cursor_address);
1295 UNEXPECTED(cursor_home);
1296 UNEXPECTED(cursor_mem_address);
1297 UNEXPECTED(cursor_to_ll);
1298 UNEXPECTED(row_address);
1299 UNEXPECTED(row_address);
1303 check_cursor(TERMTYPE2 *tp)
1309 check_noaddress(tp, "hard_copy");
1310 } else if (generic_type) {
1311 check_noaddress(tp, "generic_type");
1312 } else if (strchr(tp->term_names, '+') == 0) {
1315 if (PRESENT(column_address))
1317 if (PRESENT(cursor_address))
1319 if (PRESENT(cursor_home))
1321 if (PRESENT(cursor_mem_address))
1323 if (PRESENT(cursor_to_ll))
1325 if (PRESENT(row_address))
1327 if (PRESENT(cursor_down))
1329 if (PRESENT(cursor_up))
1331 if (PRESENT(cursor_left))
1333 if (PRESENT(cursor_right))
1335 if (x < 2 && y < 2) {
1336 _nc_warning("terminal lacks cursor addressing");
1339 _nc_warning("terminal lacks cursor column-addressing");
1341 _nc_warning("terminal lacks cursor row-addressing");
1345 /* it is rare to have an insert-line feature without a matching delete */
1346 ANDMISSING(parm_insert_line, insert_line);
1347 ANDMISSING(parm_delete_line, delete_line);
1348 ANDMISSING(parm_insert_line, parm_delete_line);
1350 /* if we have a parameterized form, then the non-parameterized is easy */
1351 ANDMISSING(parm_down_cursor, cursor_down);
1352 ANDMISSING(parm_up_cursor, cursor_up);
1353 ANDMISSING(parm_left_cursor, cursor_left);
1354 ANDMISSING(parm_right_cursor, cursor_right);
1356 /* Given any of a set of cursor movement, the whole set should be present.
1357 * Technically this is not true (we could use cursor_address to fill in
1358 * unsupported controls), but it is likely.
1361 if (PRESENT(parm_down_cursor)) {
1362 list[count++] = parm_down_cursor;
1364 if (PRESENT(parm_up_cursor)) {
1365 list[count++] = parm_up_cursor;
1367 if (PRESENT(parm_left_cursor)) {
1368 list[count++] = parm_left_cursor;
1370 if (PRESENT(parm_right_cursor)) {
1371 list[count++] = parm_right_cursor;
1374 check_ansi_cursor(list);
1375 } else if (count != 0) {
1376 EXPECTED(parm_down_cursor);
1377 EXPECTED(parm_up_cursor);
1378 EXPECTED(parm_left_cursor);
1379 EXPECTED(parm_right_cursor);
1383 if (PRESENT(cursor_down)) {
1384 list[count++] = cursor_down;
1386 if (PRESENT(cursor_up)) {
1387 list[count++] = cursor_up;
1389 if (PRESENT(cursor_left)) {
1390 list[count++] = cursor_left;
1392 if (PRESENT(cursor_right)) {
1393 list[count++] = cursor_right;
1396 check_ansi_cursor(list);
1397 } else if (count != 0) {
1399 if (PRESENT(cursor_down) && strcmp(cursor_down, "\n"))
1401 if (PRESENT(cursor_left) && strcmp(cursor_left, "\b"))
1403 if (PRESENT(cursor_up) && strlen(cursor_up) > 1)
1405 if (PRESENT(cursor_right) && strlen(cursor_right) > 1)
1408 EXPECTED(cursor_down);
1409 EXPECTED(cursor_up);
1410 EXPECTED(cursor_left);
1411 EXPECTED(cursor_right);
1418 * Do a quick sanity-check for vt100-style keypads to see if the 5-key keypad
1419 * is mapped inconsistently.
1422 check_keypad(TERMTYPE2 *tp)
1426 if (VALID_STRING(key_a1) &&
1427 VALID_STRING(key_a3) &&
1428 VALID_STRING(key_b2) &&
1429 VALID_STRING(key_c1) &&
1430 VALID_STRING(key_c3)) {
1431 char final[MAX_KP + 1];
1438 final[0] = keypad_final(key_a1);
1439 final[1] = keypad_final(key_a3);
1440 final[2] = keypad_final(key_b2);
1441 final[3] = keypad_final(key_c1);
1442 final[4] = keypad_final(key_c3);
1445 /* special case: legacy coding using 1,2,3,0,. on the bottom */
1446 assert(strlen(final) <= MAX_KP);
1447 if (!strcmp(final, "qsrpn"))
1450 list[0] = keypad_index(key_a1);
1451 list[1] = keypad_index(key_a3);
1452 list[2] = keypad_index(key_b2);
1453 list[3] = keypad_index(key_c1);
1454 list[4] = keypad_index(key_c3);
1456 /* check that they're all vt100 keys */
1457 for (j = 0; j < MAX_KP; ++j) {
1463 /* check if they're all in increasing order */
1464 for (j = 1; j < MAX_KP; ++j) {
1465 if (list[j] > list[j - 1]) {
1469 if (increase != (MAX_KP - 1)) {
1472 for (j = 0, last = -1; j < MAX_KP; ++j) {
1473 for (k = 0, kk = -1, test = 100; k < 5; ++k) {
1474 if (list[k] > last &&
1481 assert(strlen(show) < (MAX_KP * 4));
1484 _nc_STRCAT(show, " ka1", sizeof(show));
1487 _nc_STRCAT(show, " ka3", sizeof(show));
1490 _nc_STRCAT(show, " kb2", sizeof(show));
1493 _nc_STRCAT(show, " kc1", sizeof(show));
1496 _nc_STRCAT(show, " kc3", sizeof(show));
1501 _nc_warning("vt100 keypad order inconsistent: %s", show);
1504 } else if (VALID_STRING(key_a1) ||
1505 VALID_STRING(key_a3) ||
1506 VALID_STRING(key_b2) ||
1507 VALID_STRING(key_c1) ||
1508 VALID_STRING(key_c3)) {
1510 if (keypad_index(key_a1) >= 0)
1511 _nc_STRCAT(show, " ka1", sizeof(show));
1512 if (keypad_index(key_a3) >= 0)
1513 _nc_STRCAT(show, " ka3", sizeof(show));
1514 if (keypad_index(key_b2) >= 0)
1515 _nc_STRCAT(show, " kb2", sizeof(show));
1516 if (keypad_index(key_c1) >= 0)
1517 _nc_STRCAT(show, " kc1", sizeof(show));
1518 if (keypad_index(key_c3) >= 0)
1519 _nc_STRCAT(show, " kc3", sizeof(show));
1521 _nc_warning("vt100 keypad map incomplete:%s", show);
1525 * These warnings are useful for consistency checks - it is possible that
1526 * there are real terminals with mismatches in these
1528 ANDMISSING(key_ic, key_dc);
1532 check_printer(TERMTYPE2 *tp)
1534 PAIRED(enter_doublewide_mode, exit_doublewide_mode);
1535 PAIRED(enter_italics_mode, exit_italics_mode);
1536 PAIRED(enter_leftward_mode, exit_leftward_mode);
1537 PAIRED(enter_micro_mode, exit_micro_mode);
1538 PAIRED(enter_shadow_mode, exit_shadow_mode);
1539 PAIRED(enter_subscript_mode, exit_subscript_mode);
1540 PAIRED(enter_superscript_mode, exit_superscript_mode);
1541 PAIRED(enter_upward_mode, exit_upward_mode);
1543 ANDMISSING(start_char_set_def, stop_char_set_def);
1545 /* if we have a parameterized form, then the non-parameterized is easy */
1546 ANDMISSING(set_bottom_margin_parm, set_bottom_margin);
1547 ANDMISSING(set_left_margin_parm, set_left_margin);
1548 ANDMISSING(set_right_margin_parm, set_right_margin);
1549 ANDMISSING(set_top_margin_parm, set_top_margin);
1551 ANDMISSING(parm_down_micro, micro_down);
1552 ANDMISSING(parm_left_micro, micro_left);
1553 ANDMISSING(parm_right_micro, micro_right);
1554 ANDMISSING(parm_up_micro, micro_up);
1558 uses_SGR_39_49(const char *value)
1560 return (strstr(value, "39;49") != 0
1561 || strstr(value, "49;39") != 0);
1565 * Check consistency of termcap extensions related to "screen".
1568 check_screen(TERMTYPE2 *tp)
1571 if (_nc_user_definable) {
1572 int have_XT = tigetflag("XT");
1573 int have_XM = tigetflag("XM");
1574 int have_bce = back_color_erase;
1575 bool have_kmouse = FALSE;
1576 bool use_sgr_39_49 = FALSE;
1577 char *name = _nc_first_name(tp->term_names);
1579 if (!VALID_BOOLEAN(have_bce)) {
1582 if (!VALID_BOOLEAN(have_XM)) {
1585 if (!VALID_BOOLEAN(have_XT)) {
1588 if (VALID_STRING(key_mouse)) {
1589 have_kmouse = !strcmp("\033[M", key_mouse);
1591 if (VALID_STRING(orig_colors)) {
1592 use_sgr_39_49 = uses_SGR_39_49(orig_colors);
1593 } else if (VALID_STRING(orig_pair)) {
1594 use_sgr_39_49 = uses_SGR_39_49(orig_pair);
1597 if (have_XM && have_XT) {
1598 _nc_warning("Screen's XT capability conflicts with XM");
1600 && strstr(name, "screen") != 0
1601 && strchr(name, '.') != 0) {
1602 _nc_warning("Screen's \"screen\" entries should not have XT set");
1603 } else if (have_XT) {
1604 if (!have_kmouse && have_bce) {
1605 if (VALID_STRING(key_mouse)) {
1606 _nc_warning("Value of kmous inconsistent with screen's usage");
1608 _nc_warning("Expected kmous capability with XT");
1611 if (!have_bce && max_colors > 0)
1612 _nc_warning("Expected bce capability with XT");
1613 if (!use_sgr_39_49 && have_bce && max_colors > 0)
1614 _nc_warning("Expected orig_colors capability with XT to have 39/49 parameters");
1615 if (VALID_STRING(to_status_line))
1616 _nc_warning("\"tsl\" capability is redundant, given XT");
1618 if (have_kmouse && !have_XM)
1619 _nc_warning("Expected XT to be set, given kmous");
1626 * Returns the expected number of parameters for the given capability.
1629 expected_params(const char *name)
1631 #define DATA(name,count) { { name }, count }
1633 static const struct {
1637 DATA( "S0", 1 ), /* 'screen' extension */
1640 DATA( "colornm", 1 ),
1684 DATA( "setcolor", 1 ),
1688 DATA( "slength", 1 ),
1689 DATA( "slines", 1 ),
1690 DATA( "smgbp", 1 ), /* 2 if smgtp is not given */
1707 int result = 0; /* function-keys, etc., use none */
1709 for (n = 0; n < SIZEOF(table); n++) {
1710 if (!strcmp(name, table[n].name)) {
1711 result = table[n].count;
1720 * Make a quick sanity check for the parameters which are used in the given
1721 * strings. If there are no "%p" tokens, then there should be no other "%"
1725 check_params(TERMTYPE2 *tp, const char *name, char *value)
1727 int expected = expected_params(name);
1730 bool params[NUM_PARM];
1733 #ifdef set_top_margin_parm
1734 if (!strcmp(name, "smgbp")
1735 && set_top_margin_parm == 0)
1739 for (n = 0; n < NUM_PARM; n++)
1745 _nc_warning("expected character after %% in %s", name);
1747 } else if (*s == 'p') {
1748 if (*++s == '\0' || !isdigit((int) *s)) {
1749 _nc_warning("expected digit after %%p in %s", name);
1763 _nc_warning("%s refers to parameter 0 (%%p0), which is not allowed", name);
1765 if (value == set_attributes || expected < 0) {
1767 } else if (expected != actual) {
1768 _nc_warning("%s uses %d parameters, expected %d", name,
1770 for (n = 1; n < actual; n++) {
1772 _nc_warning("%s omits parameter %d", name, n);
1778 line_capability(const char *name)
1780 bool result = FALSE;
1781 static const char *table[] =
1783 "csr", /* change_scroll_region */
1784 "clear", /* clear_screen */
1786 "cwin", /* create_window */
1787 "cup", /* cursor_address */
1788 "cud1", /* cursor_down */
1789 "home", /* cursor_home */
1790 "mrcup", /* cursor_mem_address */
1791 "ll", /* cursor_to_ll */
1792 "cuu1", /* cursor_up */
1793 "dl1", /* delete_line */
1794 "hd", /* down_half_line */
1795 "flash", /* flash_screen */
1796 "ff", /* form_feed */
1797 "il1", /* insert_line */
1798 "nel", /* newline */
1799 "dl", /* parm_delete_line */
1800 "cud", /* parm_down_cursor */
1801 "indn", /* parm_index */
1802 "il", /* parm_insert_line */
1803 "rin", /* parm_rindex */
1804 "cuu", /* parm_up_cursor */
1805 "mc0", /* print_screen */
1806 "vpa", /* row_address */
1807 "ind", /* scroll_forward */
1808 "ri", /* scroll_reverse */
1809 "hu", /* up_half_line */
1812 for (n = 0; n < SIZEOF(table); ++n) {
1813 if (!strcmp(name, table[n])) {
1822 * Check for DEC VT100 private mode for reverse video.
1825 skip_DECSCNM(const char *value, int *flag)
1829 int skip = csi_length(value);
1831 value[skip++] == '?' &&
1832 value[skip++] == '5') {
1833 if (value[skip] == 'h') {
1835 } else if (value[skip] == 'l') {
1845 check_delays(const char *name, const char *value)
1848 const char *first = 0;
1849 const char *last = 0;
1851 for (p = value; *p != '\0'; ++p) {
1852 if (p[0] == '$' && p[1] == '<') {
1853 const char *base = p + 2;
1854 const char *mark = 0;
1857 int proportional = 0;
1862 for (q = base; *q != '\0'; ++q) {
1867 } else if (*q == '*' || *q == '/') {
1874 } else if (!(isalnum(UChar(*q)) || strchr("+-.", *q) != 0)) {
1877 } else if (proportional || mandatory) {
1881 last = *q ? (q + 1) : q;
1883 maybe = FALSE; /* just an isolated "$<" */
1887 int rc = sscanf(base, "%f%c", &check_f, &check_c);
1888 if ((rc != 2) || (check_c != *mark) || mixed) {
1889 _nc_warning("syntax error in %s delay '%.*s'", name,
1890 (int) (q - base), base);
1891 } else if (*name == 'k') {
1892 _nc_warning("function-key %s has delay", name);
1893 } else if (proportional && !line_capability(name)) {
1894 _nc_warning("non-line capability using proportional delay: %s", name);
1897 p = q - 1; /* restart scan */
1902 if (!strcmp(name, "flash") ||
1903 !strcmp(name, "beep")) {
1906 if (first == value || *last == 0) {
1908 * Delay is on one end or the other.
1910 _nc_warning("expected delay embedded within %s", name);
1916 * Check for missing delay when using VT100 reverse-video.
1917 * A real VT100 might not need this, but terminal emulators do.
1919 if ((p = skip_DECSCNM(value, &flag)) != 0 &&
1921 (q = skip_DECSCNM(p, &flag)) != 0 &&
1923 _nc_warning("expected a delay in %s", name);
1930 check_1_infotocap(const char *name, NCURSES_CONST char *value, int count)
1934 long numbers[1 + NUM_PARM];
1935 char *strings[1 + NUM_PARM];
1936 char *p_is_s[NUM_PARM];
1938 char blob[NUM_PARM * 10];
1942 for (k = 1; k <= NUM_PARM; k++) {
1945 _nc_SLIMIT(sizeof(blob) - (size_t) (next - blob))
1948 next += strlen(next) + 1;
1951 switch (tparm_type(name)) {
1953 result = TPARM_2(value, numbers[1], strings[2]);
1956 result = TPARM_3(value, numbers[1], strings[2], strings[3]);
1960 (void) _nc_tparm_analyze(value, p_is_s, &ignored);
1961 #define myParam(n) (p_is_s[n - 1] != 0 ? ((TPARM_ARG) strings[n]) : numbers[n])
1962 result = TPARM_9(value,
1977 #define IsDelay(ch) ((ch) == '.' || isdigit(UChar(ch)))
1980 parse_delay_value(const char *src, double *delays, int *always)
1988 while (isdigit(UChar(*src))) {
1989 (*delays) = (*delays) * 10 + (*src++ - '0');
1995 while (isdigit(UChar(*src))) {
1997 (*delays) += (*src++ - '0') / gotdot;
2000 while (*src == '*' || *src == '/') {
2001 if (always == 0 && *src == '/')
2003 if (*src++ == '*') {
2010 *delays = -(*delays);
2015 parse_ti_delay(const char *ti, double *delays)
2018 while (*ti != '\0') {
2024 && IsDelay(UChar(ti[2]))) {
2026 const char *last = parse_delay_value(ti + 2, delays, &ignored);
2038 parse_tc_delay(const char *tc, double *delays)
2040 return parse_delay_value(tc, delays, (int *) 0);
2044 * Compare terminfo- and termcap-strings, factoring out delays.
2047 same_ti_tc(const char *ti, const char *tc, bool * embedded)
2050 double ti_delay = 0.0;
2051 double tc_delay = 0.0;
2052 const char *ti_last;
2055 ti_last = parse_ti_delay(ti, &ti_delay);
2056 tc = parse_tc_delay(tc, &tc_delay);
2058 while ((ti < ti_last) && *tc) {
2059 if (*ti == '\\' && ispunct(UChar(ti[1]))) {
2061 if ((*ti == '^') && !strncmp(tc, "\\136", 4)) {
2066 } else if (ti[0] == '$' && ti[1] == '<') {
2068 const char *ss = parse_ti_delay(ti, &no_delay);
2075 if (*tc == '\\' && ispunct(UChar(tc[1]))) {
2078 if (*ti++ != *tc++) {
2088 *embedded = FALSE; /* report only one problem */
2096 * Check terminfo to termcap translation.
2099 check_infotocap(TERMTYPE2 *tp, int i, const char *value)
2101 const char *name = ExtStrname(tp, i, strnames);
2102 int params = (((i < (int) SIZEOF(parametrized)) &&
2107 : has_params(value)));
2113 if ((ti_value = _nc_tic_expand(value, TRUE, to_char)) == ABSENT_STRING) {
2114 _nc_warning("tic-expansion of %s failed", name);
2115 } else if ((tc_value = _nc_infotocap(name, ti_value, params)) == ABSENT_STRING) {
2116 _nc_warning("tic-conversion of %s failed", name);
2117 } else if (params > 0) {
2122 if (!strcmp(name, "setf")
2123 || !strcmp(name, "setb")
2124 || !strcmp(name, "setaf")
2125 || !strcmp(name, "setab")) {
2128 for (count = 0; count < limit; ++count) {
2129 char *ti_check = check_1_infotocap(name, ti_value, count);
2130 char *tc_check = check_1_infotocap(name, tc_value, count);
2132 if (strcmp(ti_check, tc_check)) {
2134 fprintf(stderr, "check_infotocap(%s)\n", name);
2135 fprintf(stderr, "...ti '%s'\n", ti_value);
2136 fprintf(stderr, "...tc '%s'\n", tc_value);
2139 _nc_warning("tparm-conversion of %s(%d) differs between\n\tterminfo %s\n\ttermcap %s",
2140 name, count, ti_check, tc_check);
2143 } else if (params == 0 && !same_ti_tc(ti_value, tc_value, &embedded)) {
2145 _nc_warning("termcap equivalent of %s cannot use embedded delay", name);
2147 _nc_warning("tic-conversion of %s changed value\n\tfrom %s\n\tto %s",
2148 name, ti_value, tc_value);
2156 while (*s == '/' || isdigit(UChar(*s)))
2162 * Skip a delay altogether, e.g., when comparing a simple string to sgr,
2163 * the latter may have a worst-case delay on the end.
2166 ignore_delays(char *s)
2198 #define DATA(name) { #name }
2199 static const char sgr_names[][11] =
2216 * An sgr string may contain several settings other than the one we're
2217 * interested in, essentially sgr0 + rmacs + whatever. As long as the
2218 * "whatever" is contained in the sgr string, that is close enough for our
2222 similar_sgr(int num, char *a, char *b)
2233 } else if (b[0] == '$'
2235 _nc_warning("Did not find delay %s", _nc_visbuf(b));
2237 _nc_warning("checking sgr(%s) %s\n\tcompare to %s\n\tunmatched %s",
2238 sgr_names[num], _nc_visbuf2(1, base_a),
2239 _nc_visbuf2(2, base_b),
2243 } else if (delaying) {
2246 } else if ((*b == '0' || (*b == ';')) && *a == 'm') {
2268 /* ignore delays on the end of the string */
2269 a = ignore_delays(a);
2270 return ((num != 0) || (*a == 0));
2274 check_sgr(TERMTYPE2 *tp, char *zero, int num, char *cap, const char *name)
2279 test = TPARM_9(set_attributes,
2291 if (!similar_sgr(num, test, cap)) {
2292 _nc_warning("%s differs from sgr(%d)\n\t%s=%s\n\tsgr(%d)=%s",
2294 name, _nc_visbuf2(1, cap),
2295 num, _nc_visbuf2(2, test));
2297 } else if (_nc_capcmp(test, zero)) {
2298 _nc_warning("sgr(%d) present, but not %s", num, name);
2300 } else if (PRESENT(cap)) {
2301 _nc_warning("sgr(%d) missing, but %s present", num, name);
2304 _nc_warning("stack error in sgr(%d) string", num);
2308 #define CHECK_SGR(num,name) check_sgr(tp, zero, num, name, #name)
2312 * If tic is compiled with TRACE, we'll be able to see the output from the
2313 * DEBUG() macro. But since it doesn't use traceon(), it always goes to
2314 * the standard error. Use this function to make it simpler to follow the
2315 * resulting debug traces.
2318 show_where(unsigned level)
2320 if (_nc_tracing >= DEBUG_LEVEL(level)) {
2321 char my_name[MAX_NAME_SIZE];
2322 _nc_get_type(my_name);
2323 _tracef("\"%s\", line %d, '%s'",
2325 _nc_curr_line, my_name);
2330 #define show_where(level) /* nothing */
2340 get_fkey_list(TERMTYPE2 *tp)
2342 NAME_VALUE *result = typeMalloc(NAME_VALUE, NUM_STRINGS(tp) + 1);
2343 const struct tinfo_fkeys *all_fkeys = _nc_tinfo_fkeys;
2348 failed("get_fkey_list");
2350 for (j = 0; all_fkeys[j].code; j++) {
2351 char *a = tp->Strings[all_fkeys[j].offset];
2352 if (VALID_STRING(a)) {
2353 result[used].keycode = (int) all_fkeys[j].code;
2354 result[used].name = strnames[all_fkeys[j].offset];
2355 result[used].value = a;
2360 for (j = STRCOUNT; j < NUM_STRINGS(tp); ++j) {
2361 const char *name = ExtStrname(tp, (int) j, strnames);
2363 result[used].keycode = -1;
2364 result[used].name = name;
2365 result[used].value = tp->Strings[j];
2370 result[used].keycode = 0;
2375 show_fkey_name(NAME_VALUE * data)
2377 if (data->keycode > 0) {
2378 fprintf(stderr, " %s", keyname(data->keycode));
2379 fprintf(stderr, " (capability \"%s\")", data->name);
2381 fprintf(stderr, " capability \"%s\"", data->name);
2386 * A terminal entry may contain more than one keycode assigned to a given
2387 * string (e.g., KEY_END and KEY_LL). But curses will only return one (the
2388 * last one assigned).
2391 check_conflict(TERMTYPE2 *tp)
2393 bool conflict = FALSE;
2396 if (!(_nc_syntax == SYN_TERMCAP && capdump)) {
2397 char *check = calloc((size_t) (NUM_STRINGS(tp) + 1), sizeof(char));
2398 NAME_VALUE *given = get_fkey_list(tp);
2401 failed("check_termtype");
2403 for (j = 0; given[j].keycode; ++j) {
2404 const char *a = given[j].value;
2407 for (k = j + 1; given[k].keycode; k++) {
2408 const char *b = given[k].value;
2411 if (!_nc_capcmp(a, b)) {
2416 _nc_warning("Conflicting key definitions (using the last)");
2419 fprintf(stderr, "...");
2420 show_fkey_name(given + j);
2421 fprintf(stderr, " is the same as");
2422 show_fkey_name(given + k);
2425 fprintf(stderr, ", ");
2426 show_fkey_name(given + k);
2431 fprintf(stderr, "\n");
2439 * Exiting a video mode should not duplicate sgr0
2442 check_exit_attribute(const char *name, char *test, char *trimmed, char *untrimmed)
2444 if (VALID_STRING(test) && (trimmed != 0)) {
2445 if (similar_sgr(-1, trimmed, test) ||
2446 similar_sgr(-1, untrimmed, test)) {
2447 _nc_warning("%s matches exit_attribute_mode", name);
2453 * Returns true if the string looks like a standard SGR string.
2456 is_sgr_string(char *value)
2458 bool result = FALSE;
2460 if (VALID_STRING(value)) {
2461 int skip = csi_length(value);
2468 while ((ch = UChar(*value++)) != '\0') {
2469 if (isdigit(ch) || ch == ';') {
2471 } else if (ch == 'm' && *value == '\0') {
2484 * Check if the given capability contains a given SGR attribute.
2487 check_sgr_param(TERMTYPE2 *tp, int code, const char *name, char *value)
2489 if (VALID_STRING(value)) {
2490 int ncv = ((code != 0) ? (1 << (code - 1)) : 0);
2491 char *test = tgoto(value, 0, 0);
2492 if (is_sgr_string(test)) {
2496 int color = (value == set_a_foreground ||
2497 value == set_a_background ||
2498 value == set_foreground ||
2499 value == set_background);
2500 while (*test != 0) {
2501 if (isdigit(UChar(*test))) {
2502 param = 10 * param + (*test - '0');
2507 * Avoid unnecessary warning for xterm 256color codes.
2509 if (color && (param == 38 || param == 48))
2511 if ((skips-- <= 0) && (param == code))
2519 if (count != 0 && param == code) {
2521 no_color_video < 0 ||
2522 !(no_color_video & ncv)) {
2523 _nc_warning("\"%s\" SGR-attribute used in %s",
2532 /* other sanity-checks (things that we don't want in the normal
2533 * logic that reads a terminfo entry)
2536 check_termtype(TERMTYPE2 *tp, bool literal)
2542 for_each_string(j, tp) {
2543 char *a = tp->Strings[j];
2544 if (VALID_STRING(a)) {
2545 check_params(tp, ExtStrname(tp, (int) j, strnames), a);
2546 check_delays(ExtStrname(tp, (int) j, strnames), a);
2548 check_infotocap(tp, (int) j, a);
2561 * These may be mismatched because the terminal description relies on
2562 * restoring the cursor visibility by resetting it.
2564 ANDMISSING(cursor_invisible, cursor_normal);
2565 ANDMISSING(cursor_visible, cursor_normal);
2567 if (PRESENT(cursor_visible) && PRESENT(cursor_normal)
2568 && !_nc_capcmp(cursor_visible, cursor_normal))
2569 _nc_warning("cursor_visible is same as cursor_normal");
2572 * From XSI & O'Reilly, we gather that sc/rc are required if csr is
2573 * given, because the cursor position after the scrolling operation is
2574 * performed is undefined.
2576 ANDMISSING(change_scroll_region, save_cursor);
2577 ANDMISSING(change_scroll_region, restore_cursor);
2580 * If we can clear tabs, we should be able to initialize them.
2582 ANDMISSING(clear_all_tabs, set_tab);
2584 if (PRESENT(set_attributes)) {
2588 if (PRESENT(exit_attribute_mode)) {
2589 zero = strdup(CHECK_SGR(0, exit_attribute_mode));
2591 zero = strdup(TPARM_9(set_attributes, 0, 0, 0, 0, 0, 0, 0, 0, 0));
2594 _nc_warning("stack error in sgr(0) string");
2597 CHECK_SGR(1, enter_standout_mode);
2598 CHECK_SGR(2, enter_underline_mode);
2599 CHECK_SGR(3, enter_reverse_mode);
2600 CHECK_SGR(4, enter_blink_mode);
2601 CHECK_SGR(5, enter_dim_mode);
2602 CHECK_SGR(6, enter_bold_mode);
2603 CHECK_SGR(7, enter_secure_mode);
2604 CHECK_SGR(8, enter_protected_mode);
2605 CHECK_SGR(9, enter_alt_charset_mode);
2608 _nc_warning("sgr(0) did not return a value");
2610 } else if (PRESENT(exit_attribute_mode) &&
2611 set_attributes != CANCELLED_STRING) {
2612 if (_nc_syntax == SYN_TERMINFO)
2613 _nc_warning("missing sgr string");
2615 #define CHECK_SGR0(name) check_exit_attribute(#name, name, check_sgr0, exit_attribute_mode)
2616 if (PRESENT(exit_attribute_mode)) {
2617 char *check_sgr0 = _nc_trim_sgr0(tp);
2619 if (check_sgr0 == 0 || *check_sgr0 == '\0') {
2620 _nc_warning("trimmed sgr0 is empty");
2623 if (check_sgr0 != exit_attribute_mode) {
2625 ("will trim sgr0\n\toriginal sgr0=%s\n\ttrimmed sgr0=%s",
2626 _nc_visbuf2(1, exit_attribute_mode),
2627 _nc_visbuf2(2, check_sgr0)));
2630 ("will not trim sgr0\n\toriginal sgr0=%s",
2631 _nc_visbuf(exit_attribute_mode)));
2634 CHECK_SGR0(exit_italics_mode);
2635 CHECK_SGR0(exit_standout_mode);
2636 CHECK_SGR0(exit_underline_mode);
2637 if (check_sgr0 != exit_attribute_mode) {
2641 #define CHECK_SGR_PARAM(code, name) check_sgr_param(tp, (int)code, #name, name)
2642 for (j = 0; *sgr_names[j] != '\0'; ++j) {
2643 CHECK_SGR_PARAM(j, set_a_foreground);
2644 CHECK_SGR_PARAM(j, set_a_background);
2645 CHECK_SGR_PARAM(j, set_foreground);
2646 CHECK_SGR_PARAM(j, set_background);
2650 if (!auto_right_margin) {
2652 ("can write to lower-right directly"));
2653 } else if (PRESENT(enter_am_mode) && PRESENT(exit_am_mode)) {
2655 ("can write to lower-right by suppressing automargin"));
2656 } else if ((PRESENT(enter_insert_mode) && PRESENT(exit_insert_mode))
2657 || PRESENT(insert_character) || PRESENT(parm_ich)) {
2659 ("can write to lower-right by using inserts"));
2662 ("cannot write to lower-right"));
2667 * Some standard applications (e.g., vi) and some non-curses
2668 * applications (e.g., jove) get confused if we have both ich1 and
2669 * smir/rmir. Let's be nice and warn about that, too, even though
2670 * ncurses handles it.
2672 if ((PRESENT(enter_insert_mode) || PRESENT(exit_insert_mode))
2673 && PRESENT(parm_ich)) {
2674 _nc_warning("non-curses applications may be confused by ich1 with smir/rmir");
2678 * Finally, do the non-verbose checks
2680 if (save_check_termtype != 0)
2681 save_check_termtype(tp, literal);