xref: /freebsd/contrib/ncurses/progs/tic.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /****************************************************************************
2  * Copyright (c) 1998-2006,2007 Free Software Foundation, Inc.              *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 
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  ****************************************************************************/
34 
35 /*
36  *	tic.c --- Main program for terminfo compiler
37  *			by Eric S. Raymond
38  *
39  */
40 
41 #include <progs.priv.h>
42 #include <sys/stat.h>
43 
44 #include <dump_entry.h>
45 #include <transform.h>
46 
47 MODULE_ID("$Id: tic.c,v 1.133 2007/07/21 17:45:59 tom Exp $")
48 
49 const char *_nc_progname = "tic";
50 
51 static FILE *log_fp;
52 static FILE *tmp_fp;
53 static bool capdump = FALSE;	/* running as infotocap? */
54 static bool infodump = FALSE;	/* running as captoinfo? */
55 static bool showsummary = FALSE;
56 static const char *to_remove;
57 
58 static void (*save_check_termtype) (TERMTYPE *, bool);
59 static void check_termtype(TERMTYPE *tt, bool);
60 
61 static const char usage_string[] = "\
62 [-e names] \
63 [-o dir] \
64 [-R name] \
65 [-v[n]] \
66 [-V] \
67 [-w[n]] \
68 [-\
69 1\
70 a\
71 C\
72 c\
73 f\
74 G\
75 g\
76 I\
77 L\
78 N\
79 r\
80 s\
81 T\
82 t\
83 U\
84 x\
85 ] \
86 source-file\n";
87 
88 static void
89 cleanup(void)
90 {
91     if (tmp_fp != 0)
92 	fclose(tmp_fp);
93     if (to_remove != 0) {
94 #if HAVE_REMOVE
95 	remove(to_remove);
96 #else
97 	unlink(to_remove);
98 #endif
99     }
100 }
101 
102 static void
103 failed(const char *msg)
104 {
105     perror(msg);
106     cleanup();
107     ExitProgram(EXIT_FAILURE);
108 }
109 
110 static void
111 usage(void)
112 {
113     static const char *const tbl[] =
114     {
115 	"Options:",
116 	"  -1         format translation output one capability per line",
117 #if NCURSES_XNAMES
118 	"  -a         retain commented-out capabilities (sets -x also)",
119 #endif
120 	"  -C         translate entries to termcap source form",
121 	"  -c         check only, validate input without compiling or translating",
122 	"  -e<names>  translate/compile only entries named by comma-separated list",
123 	"  -f         format complex strings for readability",
124 	"  -G         format %{number} to %'char'",
125 	"  -g         format %'char' to %{number}",
126 	"  -I         translate entries to terminfo source form",
127 	"  -L         translate entries to full terminfo source form",
128 	"  -N         disable smart defaults for source translation",
129 	"  -o<dir>    set output directory for compiled entry writes",
130 	"  -R<name>   restrict translation to given terminfo/termcap version",
131 	"  -r         force resolution of all use entries in source translation",
132 	"  -s         print summary statistics",
133 	"  -T         remove size-restrictions on compiled description",
134 #if NCURSES_XNAMES
135 	"  -t         suppress commented-out capabilities",
136 #endif
137 	"  -U         suppress post-processing of entries",
138 	"  -V         print version",
139 	"  -v[n]      set verbosity level",
140 	"  -w[n]      set format width for translation output",
141 #if NCURSES_XNAMES
142 	"  -x         treat unknown capabilities as user-defined",
143 #endif
144 	"",
145 	"Parameters:",
146 	"  <file>     file to translate or compile"
147     };
148     size_t j;
149 
150     fprintf(stderr, "Usage: %s %s\n", _nc_progname, usage_string);
151     for (j = 0; j < SIZEOF(tbl); j++) {
152 	fputs(tbl[j], stderr);
153 	putc('\n', stderr);
154     }
155     ExitProgram(EXIT_FAILURE);
156 }
157 
158 #define L_BRACE '{'
159 #define R_BRACE '}'
160 #define S_QUOTE '\'';
161 
162 static void
163 write_it(ENTRY * ep)
164 {
165     unsigned n;
166     int ch;
167     char *s, *d, *t;
168     char result[MAX_ENTRY_SIZE];
169 
170     /*
171      * Look for strings that contain %{number}, convert them to %'char',
172      * which is shorter and runs a little faster.
173      */
174     for (n = 0; n < STRCOUNT; n++) {
175 	s = ep->tterm.Strings[n];
176 	if (VALID_STRING(s)
177 	    && strchr(s, L_BRACE) != 0) {
178 	    d = result;
179 	    t = s;
180 	    while ((ch = *t++) != 0) {
181 		*d++ = ch;
182 		if (ch == '\\') {
183 		    *d++ = *t++;
184 		} else if ((ch == '%')
185 			   && (*t == L_BRACE)) {
186 		    char *v = 0;
187 		    long value = strtol(t + 1, &v, 0);
188 		    if (v != 0
189 			&& *v == R_BRACE
190 			&& value > 0
191 			&& value != '\\'	/* FIXME */
192 			&& value < 127
193 			&& isprint((int) value)) {
194 			*d++ = S_QUOTE;
195 			*d++ = (int) value;
196 			*d++ = S_QUOTE;
197 			t = (v + 1);
198 		    }
199 		}
200 	    }
201 	    *d = 0;
202 	    if (strlen(result) < strlen(s))
203 		strcpy(s, result);
204 	}
205     }
206 
207     _nc_set_type(_nc_first_name(ep->tterm.term_names));
208     _nc_curr_line = ep->startline;
209     _nc_write_entry(&ep->tterm);
210 }
211 
212 static bool
213 immedhook(ENTRY * ep GCC_UNUSED)
214 /* write out entries with no use capabilities immediately to save storage */
215 {
216 #if !HAVE_BIG_CORE
217     /*
218      * This is strictly a core-economy kluge.  The really clean way to handle
219      * compilation is to slurp the whole file into core and then do all the
220      * name-collision checks and entry writes in one swell foop.  But the
221      * terminfo master file is large enough that some core-poor systems swap
222      * like crazy when you compile it this way...there have been reports of
223      * this process taking *three hours*, rather than the twenty seconds or
224      * less typical on my development box.
225      *
226      * So.  This hook *immediately* writes out the referenced entry if it
227      * has no use capabilities.  The compiler main loop refrains from
228      * adding the entry to the in-core list when this hook fires.  If some
229      * other entry later needs to reference an entry that got written
230      * immediately, that's OK; the resolution code will fetch it off disk
231      * when it can't find it in core.
232      *
233      * Name collisions will still be detected, just not as cleanly.  The
234      * write_entry() code complains before overwriting an entry that
235      * postdates the time of tic's first call to write_entry().  Thus
236      * it will complain about overwriting entries newly made during the
237      * tic run, but not about overwriting ones that predate it.
238      *
239      * The reason this is a hook, and not in line with the rest of the
240      * compiler code, is that the support for termcap fallback cannot assume
241      * it has anywhere to spool out these entries!
242      *
243      * The _nc_set_type() call here requires a compensating one in
244      * _nc_parse_entry().
245      *
246      * If you define HAVE_BIG_CORE, you'll disable this kluge.  This will
247      * make tic a bit faster (because the resolution code won't have to do
248      * disk I/O nearly as often).
249      */
250     if (ep->nuses == 0) {
251 	int oldline = _nc_curr_line;
252 
253 	write_it(ep);
254 	_nc_curr_line = oldline;
255 	free(ep->tterm.str_table);
256 	return (TRUE);
257     }
258 #endif /* HAVE_BIG_CORE */
259     return (FALSE);
260 }
261 
262 static void
263 put_translate(int c)
264 /* emit a comment char, translating terminfo names to termcap names */
265 {
266     static bool in_name = FALSE;
267     static size_t have, used;
268     static char *namebuf, *suffix;
269 
270     if (in_name) {
271 	if (used + 1 >= have) {
272 	    have += 132;
273 	    namebuf = typeRealloc(char, have, namebuf);
274 	    suffix = typeRealloc(char, have, suffix);
275 	}
276 	if (c == '\n' || c == '@') {
277 	    namebuf[used++] = '\0';
278 	    (void) putchar('<');
279 	    (void) fputs(namebuf, stdout);
280 	    putchar(c);
281 	    in_name = FALSE;
282 	} else if (c != '>') {
283 	    namebuf[used++] = c;
284 	} else {		/* ah! candidate name! */
285 	    char *up;
286 	    NCURSES_CONST char *tp;
287 
288 	    namebuf[used++] = '\0';
289 	    in_name = FALSE;
290 
291 	    suffix[0] = '\0';
292 	    if ((up = strchr(namebuf, '#')) != 0
293 		|| (up = strchr(namebuf, '=')) != 0
294 		|| ((up = strchr(namebuf, '@')) != 0 && up[1] == '>')) {
295 		(void) strcpy(suffix, up);
296 		*up = '\0';
297 	    }
298 
299 	    if ((tp = nametrans(namebuf)) != 0) {
300 		(void) putchar(':');
301 		(void) fputs(tp, stdout);
302 		(void) fputs(suffix, stdout);
303 		(void) putchar(':');
304 	    } else {
305 		/* couldn't find a translation, just dump the name */
306 		(void) putchar('<');
307 		(void) fputs(namebuf, stdout);
308 		(void) fputs(suffix, stdout);
309 		(void) putchar('>');
310 	    }
311 	}
312     } else {
313 	used = 0;
314 	if (c == '<') {
315 	    in_name = TRUE;
316 	} else {
317 	    putchar(c);
318 	}
319     }
320 }
321 
322 /* Returns a string, stripped of leading/trailing whitespace */
323 static char *
324 stripped(char *src)
325 {
326     while (isspace(UChar(*src)))
327 	src++;
328     if (*src != '\0') {
329 	char *dst = strcpy((char *) malloc(strlen(src) + 1), src);
330 	size_t len = strlen(dst);
331 	while (--len != 0 && isspace(UChar(dst[len])))
332 	    dst[len] = '\0';
333 	return dst;
334     }
335     return 0;
336 }
337 
338 static FILE *
339 open_input(const char *filename)
340 {
341     FILE *fp = fopen(filename, "r");
342     struct stat sb;
343 
344     if (fp == 0) {
345 	fprintf(stderr, "%s: Can't open %s\n", _nc_progname, filename);
346 	ExitProgram(EXIT_FAILURE);
347     }
348     if (fstat(fileno(fp), &sb) < 0
349 	|| (sb.st_mode & S_IFMT) != S_IFREG) {
350 	fprintf(stderr, "%s: %s is not a file\n", _nc_progname, filename);
351 	ExitProgram(EXIT_FAILURE);
352     }
353     return fp;
354 }
355 
356 #if NO_LEAKS
357 static void
358 free_namelist(char **src)
359 {
360     if (src != 0) {
361 	int n;
362 	for (n = 0; src[n] != 0; ++n)
363 	    free(src[n]);
364 	free(src);
365     }
366 }
367 #endif
368 
369 /* Parse the "-e" option-value into a list of names */
370 static char **
371 make_namelist(char *src)
372 {
373     char **dst = 0;
374 
375     char *s, *base;
376     unsigned pass, n, nn;
377     char buffer[BUFSIZ];
378 
379     if (src == 0) {
380 	/* EMPTY */ ;
381     } else if (strchr(src, '/') != 0) {		/* a filename */
382 	FILE *fp = open_input(src);
383 
384 	for (pass = 1; pass <= 2; pass++) {
385 	    nn = 0;
386 	    while (fgets(buffer, sizeof(buffer), fp) != 0) {
387 		if ((s = stripped(buffer)) != 0) {
388 		    if (dst != 0)
389 			dst[nn] = s;
390 		    else
391 			free(s);
392 		    nn++;
393 		}
394 	    }
395 	    if (pass == 1) {
396 		dst = typeCalloc(char *, nn + 1);
397 		rewind(fp);
398 	    }
399 	}
400 	fclose(fp);
401     } else {			/* literal list of names */
402 	for (pass = 1; pass <= 2; pass++) {
403 	    for (n = nn = 0, base = src;; n++) {
404 		int mark = src[n];
405 		if (mark == ',' || mark == '\0') {
406 		    if (pass == 1) {
407 			nn++;
408 		    } else {
409 			src[n] = '\0';
410 			if ((s = stripped(base)) != 0)
411 			    dst[nn++] = s;
412 			base = &src[n + 1];
413 		    }
414 		}
415 		if (mark == '\0')
416 		    break;
417 	    }
418 	    if (pass == 1)
419 		dst = typeCalloc(char *, nn + 1);
420 	}
421     }
422     if (showsummary && (dst != 0)) {
423 	fprintf(log_fp, "Entries that will be compiled:\n");
424 	for (n = 0; dst[n] != 0; n++)
425 	    fprintf(log_fp, "%u:%s\n", n + 1, dst[n]);
426     }
427     return dst;
428 }
429 
430 static bool
431 matches(char **needle, const char *haystack)
432 /* does entry in needle list match |-separated field in haystack? */
433 {
434     bool code = FALSE;
435     size_t n;
436 
437     if (needle != 0) {
438 	for (n = 0; needle[n] != 0; n++) {
439 	    if (_nc_name_match(haystack, needle[n], "|")) {
440 		code = TRUE;
441 		break;
442 	    }
443 	}
444     } else
445 	code = TRUE;
446     return (code);
447 }
448 
449 static FILE *
450 open_tempfile(char *name)
451 {
452     FILE *result = 0;
453 #if HAVE_MKSTEMP
454     int fd = mkstemp(name);
455     if (fd >= 0)
456 	result = fdopen(fd, "w");
457 #else
458     if (tmpnam(name) != 0)
459 	result = fopen(name, "w");
460 #endif
461     return result;
462 }
463 
464 int
465 main(int argc, char *argv[])
466 {
467     char my_tmpname[PATH_MAX];
468     int v_opt = -1, debug_level;
469     int smart_defaults = TRUE;
470     char *termcap;
471     ENTRY *qp;
472 
473     int this_opt, last_opt = '?';
474 
475     int outform = F_TERMINFO;	/* output format */
476     int sortmode = S_TERMINFO;	/* sort_mode */
477 
478     int width = 60;
479     bool formatted = FALSE;	/* reformat complex strings? */
480     bool literal = FALSE;	/* suppress post-processing? */
481     int numbers = 0;		/* format "%'char'" to/from "%{number}" */
482     bool forceresolve = FALSE;	/* force resolution */
483     bool limited = TRUE;
484     char *tversion = (char *) NULL;
485     const char *source_file = "terminfo";
486     char **namelst = 0;
487     char *outdir = (char *) NULL;
488     bool check_only = FALSE;
489     bool suppress_untranslatable = FALSE;
490 
491     log_fp = stderr;
492 
493     _nc_progname = _nc_rootname(argv[0]);
494 
495     if ((infodump = (strcmp(_nc_progname, PROG_CAPTOINFO) == 0)) != FALSE) {
496 	outform = F_TERMINFO;
497 	sortmode = S_TERMINFO;
498     }
499     if ((capdump = (strcmp(_nc_progname, PROG_INFOTOCAP) == 0)) != FALSE) {
500 	outform = F_TERMCAP;
501 	sortmode = S_TERMCAP;
502     }
503 #if NCURSES_XNAMES
504     use_extended_names(FALSE);
505 #endif
506 
507     /*
508      * Processing arguments is a little complicated, since someone made a
509      * design decision to allow the numeric values for -w, -v options to
510      * be optional.
511      */
512     while ((this_opt = getopt(argc, argv,
513 			      "0123456789CILNR:TUVace:fGgo:rstvwx")) != -1) {
514 	if (isdigit(this_opt)) {
515 	    switch (last_opt) {
516 	    case 'v':
517 		v_opt = (v_opt * 10) + (this_opt - '0');
518 		break;
519 	    case 'w':
520 		width = (width * 10) + (this_opt - '0');
521 		break;
522 	    default:
523 		if (this_opt != '1')
524 		    usage();
525 		last_opt = this_opt;
526 		width = 0;
527 	    }
528 	    continue;
529 	}
530 	switch (this_opt) {
531 	case 'C':
532 	    capdump = TRUE;
533 	    outform = F_TERMCAP;
534 	    sortmode = S_TERMCAP;
535 	    break;
536 	case 'I':
537 	    infodump = TRUE;
538 	    outform = F_TERMINFO;
539 	    sortmode = S_TERMINFO;
540 	    break;
541 	case 'L':
542 	    infodump = TRUE;
543 	    outform = F_VARIABLE;
544 	    sortmode = S_VARIABLE;
545 	    break;
546 	case 'N':
547 	    smart_defaults = FALSE;
548 	    literal = TRUE;
549 	    break;
550 	case 'R':
551 	    tversion = optarg;
552 	    break;
553 	case 'T':
554 	    limited = FALSE;
555 	    break;
556 	case 'U':
557 	    literal = TRUE;
558 	    break;
559 	case 'V':
560 	    puts(curses_version());
561 	    return EXIT_SUCCESS;
562 	case 'c':
563 	    check_only = TRUE;
564 	    break;
565 	case 'e':
566 	    namelst = make_namelist(optarg);
567 	    break;
568 	case 'f':
569 	    formatted = TRUE;
570 	    break;
571 	case 'G':
572 	    numbers = 1;
573 	    break;
574 	case 'g':
575 	    numbers = -1;
576 	    break;
577 	case 'o':
578 	    outdir = optarg;
579 	    break;
580 	case 'r':
581 	    forceresolve = TRUE;
582 	    break;
583 	case 's':
584 	    showsummary = TRUE;
585 	    break;
586 	case 'v':
587 	    v_opt = 0;
588 	    break;
589 	case 'w':
590 	    width = 0;
591 	    break;
592 #if NCURSES_XNAMES
593 	case 't':
594 	    _nc_disable_period = FALSE;
595 	    suppress_untranslatable = TRUE;
596 	    break;
597 	case 'a':
598 	    _nc_disable_period = TRUE;
599 	    /* FALLTHRU */
600 	case 'x':
601 	    use_extended_names(TRUE);
602 	    break;
603 #endif
604 	default:
605 	    usage();
606 	}
607 	last_opt = this_opt;
608     }
609 
610     debug_level = (v_opt > 0) ? v_opt : (v_opt == 0);
611     set_trace_level(debug_level);
612 
613     if (_nc_tracing) {
614 	save_check_termtype = _nc_check_termtype2;
615 	_nc_check_termtype2 = check_termtype;
616     }
617 #if !HAVE_BIG_CORE
618     /*
619      * Aaargh! immedhook seriously hoses us!
620      *
621      * One problem with immedhook is it means we can't do -e.  Problem
622      * is that we can't guarantee that for each terminal listed, all the
623      * terminals it depends on will have been kept in core for reference
624      * resolution -- in fact it's certain the primitive types at the end
625      * of reference chains *won't* be in core unless they were explicitly
626      * in the select list themselves.
627      */
628     if (namelst && (!infodump && !capdump)) {
629 	(void) fprintf(stderr,
630 		       "Sorry, -e can't be used without -I or -C\n");
631 	cleanup();
632 	ExitProgram(EXIT_FAILURE);
633     }
634 #endif /* HAVE_BIG_CORE */
635 
636     if (optind < argc) {
637 	source_file = argv[optind++];
638 	if (optind < argc) {
639 	    fprintf(stderr,
640 		    "%s: Too many file names.  Usage:\n\t%s %s",
641 		    _nc_progname,
642 		    _nc_progname,
643 		    usage_string);
644 	    ExitProgram(EXIT_FAILURE);
645 	}
646     } else {
647 	if (infodump == TRUE) {
648 	    /* captoinfo's no-argument case */
649 	    source_file = "/etc/termcap";
650 	    if ((termcap = getenv("TERMCAP")) != 0
651 		&& (namelst = make_namelist(getenv("TERM"))) != 0) {
652 		if (access(termcap, F_OK) == 0) {
653 		    /* file exists */
654 		    source_file = termcap;
655 		} else if ((tmp_fp = open_tempfile(strcpy(my_tmpname,
656 							  "/tmp/XXXXXX")))
657 			   != 0) {
658 		    source_file = my_tmpname;
659 		    fprintf(tmp_fp, "%s\n", termcap);
660 		    fclose(tmp_fp);
661 		    tmp_fp = open_input(source_file);
662 		    to_remove = source_file;
663 		} else {
664 		    failed("tmpnam");
665 		}
666 	    }
667 	} else {
668 	    /* tic */
669 	    fprintf(stderr,
670 		    "%s: File name needed.  Usage:\n\t%s %s",
671 		    _nc_progname,
672 		    _nc_progname,
673 		    usage_string);
674 	    cleanup();
675 	    ExitProgram(EXIT_FAILURE);
676 	}
677     }
678 
679     if (tmp_fp == 0)
680 	tmp_fp = open_input(source_file);
681 
682     if (infodump)
683 	dump_init(tversion,
684 		  smart_defaults
685 		  ? outform
686 		  : F_LITERAL,
687 		  sortmode, width, debug_level, formatted);
688     else if (capdump)
689 	dump_init(tversion,
690 		  outform,
691 		  sortmode, width, debug_level, FALSE);
692 
693     /* parse entries out of the source file */
694     _nc_set_source(source_file);
695 #if !HAVE_BIG_CORE
696     if (!(check_only || infodump || capdump))
697 	_nc_set_writedir(outdir);
698 #endif /* HAVE_BIG_CORE */
699     _nc_read_entry_source(tmp_fp, (char *) NULL,
700 			  !smart_defaults || literal, FALSE,
701 			  ((check_only || infodump || capdump)
702 			   ? NULLHOOK
703 			   : immedhook));
704 
705     /* do use resolution */
706     if (check_only || (!infodump && !capdump) || forceresolve) {
707 	if (!_nc_resolve_uses2(TRUE, literal) && !check_only) {
708 	    cleanup();
709 	    ExitProgram(EXIT_FAILURE);
710 	}
711     }
712 
713     /* length check */
714     if (check_only && (capdump || infodump)) {
715 	for_entry_list(qp) {
716 	    if (matches(namelst, qp->tterm.term_names)) {
717 		int len = fmt_entry(&qp->tterm, NULL, FALSE, TRUE, infodump, numbers);
718 
719 		if (len > (infodump ? MAX_TERMINFO_LENGTH : MAX_TERMCAP_LENGTH))
720 		    (void) fprintf(stderr,
721 				   "warning: resolved %s entry is %d bytes long\n",
722 				   _nc_first_name(qp->tterm.term_names),
723 				   len);
724 	    }
725 	}
726     }
727 
728     /* write or dump all entries */
729     if (!check_only) {
730 	if (!infodump && !capdump) {
731 	    _nc_set_writedir(outdir);
732 	    for_entry_list(qp) {
733 		if (matches(namelst, qp->tterm.term_names))
734 		    write_it(qp);
735 	    }
736 	} else {
737 	    /* this is in case infotocap() generates warnings */
738 	    _nc_curr_col = _nc_curr_line = -1;
739 
740 	    for_entry_list(qp) {
741 		if (matches(namelst, qp->tterm.term_names)) {
742 		    int j = qp->cend - qp->cstart;
743 		    int len = 0;
744 
745 		    /* this is in case infotocap() generates warnings */
746 		    _nc_set_type(_nc_first_name(qp->tterm.term_names));
747 
748 		    (void) fseek(tmp_fp, qp->cstart, SEEK_SET);
749 		    while (j-- > 0) {
750 			if (infodump)
751 			    (void) putchar(fgetc(tmp_fp));
752 			else
753 			    put_translate(fgetc(tmp_fp));
754 		    }
755 
756 		    dump_entry(&qp->tterm, suppress_untranslatable,
757 			       limited, numbers, NULL);
758 		    for (j = 0; j < qp->nuses; j++)
759 			dump_uses(qp->uses[j].name, !capdump);
760 		    len = show_entry();
761 		    if (debug_level != 0 && !limited)
762 			printf("# length=%d\n", len);
763 		}
764 	    }
765 	    if (!namelst && _nc_tail) {
766 		int c, oldc = '\0';
767 		bool in_comment = FALSE;
768 		bool trailing_comment = FALSE;
769 
770 		(void) fseek(tmp_fp, _nc_tail->cend, SEEK_SET);
771 		while ((c = fgetc(tmp_fp)) != EOF) {
772 		    if (oldc == '\n') {
773 			if (c == '#') {
774 			    trailing_comment = TRUE;
775 			    in_comment = TRUE;
776 			} else {
777 			    in_comment = FALSE;
778 			}
779 		    }
780 		    if (trailing_comment
781 			&& (in_comment || (oldc == '\n' && c == '\n')))
782 			putchar(c);
783 		    oldc = c;
784 		}
785 	    }
786 	}
787     }
788 
789     /* Show the directory into which entries were written, and the total
790      * number of entries
791      */
792     if (showsummary
793 	&& (!(check_only || infodump || capdump))) {
794 	int total = _nc_tic_written();
795 	if (total != 0)
796 	    fprintf(log_fp, "%d entries written to %s\n",
797 		    total,
798 		    _nc_tic_dir((char *) 0));
799 	else
800 	    fprintf(log_fp, "No entries written\n");
801     }
802 #if NO_LEAKS
803     free_namelist(namelst);
804 #endif
805     cleanup();
806     ExitProgram(EXIT_SUCCESS);
807 }
808 
809 /*
810  * This bit of legerdemain turns all the terminfo variable names into
811  * references to locations in the arrays Booleans, Numbers, and Strings ---
812  * precisely what's needed (see comp_parse.c).
813  */
814 #undef CUR
815 #define CUR tp->
816 
817 /*
818  * Check if the alternate character-set capabilities are consistent.
819  */
820 static void
821 check_acs(TERMTYPE *tp)
822 {
823     if (VALID_STRING(acs_chars)) {
824 	const char *boxes = "lmkjtuvwqxn";
825 	char mapped[256];
826 	char missing[256];
827 	const char *p;
828 	char *q;
829 
830 	memset(mapped, 0, sizeof(mapped));
831 	for (p = acs_chars; *p != '\0'; p += 2) {
832 	    if (p[1] == '\0') {
833 		_nc_warning("acsc has odd number of characters");
834 		break;
835 	    }
836 	    mapped[UChar(p[0])] = p[1];
837 	}
838 	if (mapped[UChar('I')] && !mapped[UChar('i')]) {
839 	    _nc_warning("acsc refers to 'I', which is probably an error");
840 	}
841 	for (p = boxes, q = missing; *p != '\0'; ++p) {
842 	    if (!mapped[UChar(p[0])]) {
843 		*q++ = p[0];
844 	    }
845 	    *q = '\0';
846 	}
847 	if (*missing != '\0' && strcmp(missing, boxes)) {
848 	    _nc_warning("acsc is missing some line-drawing mapping: %s", missing);
849 	}
850     }
851 }
852 
853 /*
854  * Check if the color capabilities are consistent
855  */
856 static void
857 check_colors(TERMTYPE *tp)
858 {
859     if ((max_colors > 0) != (max_pairs > 0)
860 	|| ((max_colors > max_pairs) && (initialize_pair == 0)))
861 	_nc_warning("inconsistent values for max_colors (%d) and max_pairs (%d)",
862 		    max_colors, max_pairs);
863 
864     PAIRED(set_foreground, set_background);
865     PAIRED(set_a_foreground, set_a_background);
866     PAIRED(set_color_pair, initialize_pair);
867 
868     if (VALID_STRING(set_foreground)
869 	&& VALID_STRING(set_a_foreground)
870 	&& !_nc_capcmp(set_foreground, set_a_foreground))
871 	_nc_warning("expected setf/setaf to be different");
872 
873     if (VALID_STRING(set_background)
874 	&& VALID_STRING(set_a_background)
875 	&& !_nc_capcmp(set_background, set_a_background))
876 	_nc_warning("expected setb/setab to be different");
877 
878     /* see: has_colors() */
879     if (VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
880 	&& (((set_foreground != NULL)
881 	     && (set_background != NULL))
882 	    || ((set_a_foreground != NULL)
883 		&& (set_a_background != NULL))
884 	    || set_color_pair)) {
885 	if (!VALID_STRING(orig_pair) && !VALID_STRING(orig_colors))
886 	    _nc_warning("expected either op/oc string for resetting colors");
887     }
888 }
889 
890 static int
891 keypad_final(const char *string)
892 {
893     int result = '\0';
894 
895     if (VALID_STRING(string)
896 	&& *string++ == '\033'
897 	&& *string++ == 'O'
898 	&& strlen(string) == 1) {
899 	result = *string;
900     }
901 
902     return result;
903 }
904 
905 static int
906 keypad_index(const char *string)
907 {
908     char *test;
909     const char *list = "PQRSwxymtuvlqrsPpn";	/* app-keypad except "Enter" */
910     int ch;
911     int result = -1;
912 
913     if ((ch = keypad_final(string)) != '\0') {
914 	test = strchr(list, ch);
915 	if (test != 0)
916 	    result = (test - list);
917     }
918     return result;
919 }
920 
921 /*
922  * Do a quick sanity-check for vt100-style keypads to see if the 5-key keypad
923  * is mapped inconsistently.
924  */
925 static void
926 check_keypad(TERMTYPE *tp)
927 {
928     char show[80];
929 
930     if (VALID_STRING(key_a1) &&
931 	VALID_STRING(key_a3) &&
932 	VALID_STRING(key_b2) &&
933 	VALID_STRING(key_c1) &&
934 	VALID_STRING(key_c3)) {
935 	char final[6];
936 	int list[5];
937 	int increase = 0;
938 	int j, k, kk;
939 	int last;
940 	int test;
941 
942 	final[0] = keypad_final(key_a1);
943 	final[1] = keypad_final(key_a3);
944 	final[2] = keypad_final(key_b2);
945 	final[3] = keypad_final(key_c1);
946 	final[4] = keypad_final(key_c3);
947 	final[5] = '\0';
948 
949 	/* special case: legacy coding using 1,2,3,0,. on the bottom */
950 	if (!strcmp(final, "qsrpn"))
951 	    return;
952 
953 	list[0] = keypad_index(key_a1);
954 	list[1] = keypad_index(key_a3);
955 	list[2] = keypad_index(key_b2);
956 	list[3] = keypad_index(key_c1);
957 	list[4] = keypad_index(key_c3);
958 
959 	/* check that they're all vt100 keys */
960 	for (j = 0; j < 5; ++j) {
961 	    if (list[j] < 0) {
962 		return;
963 	    }
964 	}
965 
966 	/* check if they're all in increasing order */
967 	for (j = 1; j < 5; ++j) {
968 	    if (list[j] > list[j - 1]) {
969 		++increase;
970 	    }
971 	}
972 	if (increase != 4) {
973 	    show[0] = '\0';
974 
975 	    for (j = 0, last = -1; j < 5; ++j) {
976 		for (k = 0, kk = -1, test = 100; k < 5; ++k) {
977 		    if (list[k] > last &&
978 			list[k] < test) {
979 			test = list[k];
980 			kk = k;
981 		    }
982 		}
983 		last = test;
984 		switch (kk) {
985 		case 0:
986 		    strcat(show, " ka1");
987 		    break;
988 		case 1:
989 		    strcat(show, " ka3");
990 		    break;
991 		case 2:
992 		    strcat(show, " kb2");
993 		    break;
994 		case 3:
995 		    strcat(show, " kc1");
996 		    break;
997 		case 4:
998 		    strcat(show, " kc3");
999 		    break;
1000 		}
1001 	    }
1002 
1003 	    _nc_warning("vt100 keypad order inconsistent: %s", show);
1004 	}
1005 
1006     } else if (VALID_STRING(key_a1) ||
1007 	       VALID_STRING(key_a3) ||
1008 	       VALID_STRING(key_b2) ||
1009 	       VALID_STRING(key_c1) ||
1010 	       VALID_STRING(key_c3)) {
1011 	show[0] = '\0';
1012 	if (keypad_index(key_a1) >= 0)
1013 	    strcat(show, " ka1");
1014 	if (keypad_index(key_a3) >= 0)
1015 	    strcat(show, " ka3");
1016 	if (keypad_index(key_b2) >= 0)
1017 	    strcat(show, " kb2");
1018 	if (keypad_index(key_c1) >= 0)
1019 	    strcat(show, " kc1");
1020 	if (keypad_index(key_c3) >= 0)
1021 	    strcat(show, " kc3");
1022 	if (*show != '\0')
1023 	    _nc_warning("vt100 keypad map incomplete:%s", show);
1024     }
1025 }
1026 
1027 /*
1028  * Returns the expected number of parameters for the given capability.
1029  */
1030 static int
1031 expected_params(const char *name)
1032 {
1033     /* *INDENT-OFF* */
1034     static const struct {
1035 	const char *name;
1036 	int count;
1037     } table[] = {
1038 	{ "S0",			1 },	/* 'screen' extension */
1039 	{ "birep",		2 },
1040 	{ "chr",		1 },
1041 	{ "colornm",		1 },
1042 	{ "cpi",		1 },
1043 	{ "csnm",		1 },
1044 	{ "csr",		2 },
1045 	{ "cub",		1 },
1046 	{ "cud",		1 },
1047 	{ "cuf",		1 },
1048 	{ "cup",		2 },
1049 	{ "cuu",		1 },
1050 	{ "cvr",		1 },
1051 	{ "cwin",		5 },
1052 	{ "dch",		1 },
1053 	{ "defc",		3 },
1054 	{ "dial",		1 },
1055 	{ "dispc",		1 },
1056 	{ "dl",			1 },
1057 	{ "ech",		1 },
1058 	{ "getm",		1 },
1059 	{ "hpa",		1 },
1060 	{ "ich",		1 },
1061 	{ "il",			1 },
1062 	{ "indn",		1 },
1063 	{ "initc",		4 },
1064 	{ "initp",		7 },
1065 	{ "lpi",		1 },
1066 	{ "mc5p",		1 },
1067 	{ "mrcup",		2 },
1068 	{ "mvpa",		1 },
1069 	{ "pfkey",		2 },
1070 	{ "pfloc",		2 },
1071 	{ "pfx",		2 },
1072 	{ "pfxl",		3 },
1073 	{ "pln",		2 },
1074 	{ "qdial",		1 },
1075 	{ "rcsd",		1 },
1076 	{ "rep",		2 },
1077 	{ "rin",		1 },
1078 	{ "sclk",		3 },
1079 	{ "scp",		1 },
1080 	{ "scs",		1 },
1081 	{ "scsd",		2 },
1082 	{ "setab",		1 },
1083 	{ "setaf",		1 },
1084 	{ "setb",		1 },
1085 	{ "setcolor",		1 },
1086 	{ "setf",		1 },
1087 	{ "sgr",		9 },
1088 	{ "sgr1",		6 },
1089 	{ "slength",		1 },
1090 	{ "slines",		1 },
1091 	{ "smgbp",		1 },	/* 2 if smgtp is not given */
1092 	{ "smglp",		1 },
1093 	{ "smglr",		2 },
1094 	{ "smgrp",		1 },
1095 	{ "smgtb",		2 },
1096 	{ "smgtp",		1 },
1097 	{ "tsl",		1 },
1098 	{ "u6",			-1 },
1099 	{ "vpa",		1 },
1100 	{ "wind",		4 },
1101 	{ "wingo",		1 },
1102     };
1103     /* *INDENT-ON* */
1104 
1105     unsigned n;
1106     int result = 0;		/* function-keys, etc., use none */
1107 
1108     for (n = 0; n < SIZEOF(table); n++) {
1109 	if (!strcmp(name, table[n].name)) {
1110 	    result = table[n].count;
1111 	    break;
1112 	}
1113     }
1114 
1115     return result;
1116 }
1117 
1118 /*
1119  * Make a quick sanity check for the parameters which are used in the given
1120  * strings.  If there are no "%p" tokens, then there should be no other "%"
1121  * markers.
1122  */
1123 static void
1124 check_params(TERMTYPE *tp, const char *name, char *value)
1125 {
1126     int expected = expected_params(name);
1127     int actual = 0;
1128     int n;
1129     bool params[10];
1130     char *s = value;
1131 
1132 #ifdef set_top_margin_parm
1133     if (!strcmp(name, "smgbp")
1134 	&& set_top_margin_parm == 0)
1135 	expected = 2;
1136 #endif
1137 
1138     for (n = 0; n < 10; n++)
1139 	params[n] = FALSE;
1140 
1141     while (*s != 0) {
1142 	if (*s == '%') {
1143 	    if (*++s == '\0') {
1144 		_nc_warning("expected character after %% in %s", name);
1145 		break;
1146 	    } else if (*s == 'p') {
1147 		if (*++s == '\0' || !isdigit((int) *s)) {
1148 		    _nc_warning("expected digit after %%p in %s", name);
1149 		    return;
1150 		} else {
1151 		    n = (*s - '0');
1152 		    if (n > actual)
1153 			actual = n;
1154 		    params[n] = TRUE;
1155 		}
1156 	    }
1157 	}
1158 	s++;
1159     }
1160 
1161     if (params[0]) {
1162 	_nc_warning("%s refers to parameter 0 (%%p0), which is not allowed", name);
1163     }
1164     if (value == set_attributes || expected < 0) {
1165 	;
1166     } else if (expected != actual) {
1167 	_nc_warning("%s uses %d parameters, expected %d", name,
1168 		    actual, expected);
1169 	for (n = 1; n < actual; n++) {
1170 	    if (!params[n])
1171 		_nc_warning("%s omits parameter %d", name, n);
1172 	}
1173     }
1174 }
1175 
1176 static char *
1177 skip_delay(char *s)
1178 {
1179     while (*s == '/' || isdigit(UChar(*s)))
1180 	++s;
1181     return s;
1182 }
1183 
1184 /*
1185  * Skip a delay altogether, e.g., when comparing a simple string to sgr,
1186  * the latter may have a worst-case delay on the end.
1187  */
1188 static char *
1189 ignore_delays(char *s)
1190 {
1191     int delaying = 0;
1192 
1193     do {
1194 	switch (*s) {
1195 	case '$':
1196 	    if (delaying == 0)
1197 		delaying = 1;
1198 	    break;
1199 	case '<':
1200 	    if (delaying == 1)
1201 		delaying = 2;
1202 	    break;
1203 	case '\0':
1204 	    delaying = 0;
1205 	    break;
1206 	default:
1207 	    if (delaying) {
1208 		s = skip_delay(s);
1209 		if (*s == '>')
1210 		    ++s;
1211 		delaying = 0;
1212 	    }
1213 	    break;
1214 	}
1215 	if (delaying)
1216 	    ++s;
1217     } while (delaying);
1218     return s;
1219 }
1220 
1221 /*
1222  * An sgr string may contain several settings other than the one we're
1223  * interested in, essentially sgr0 + rmacs + whatever.  As long as the
1224  * "whatever" is contained in the sgr string, that is close enough for our
1225  * sanity check.
1226  */
1227 static bool
1228 similar_sgr(int num, char *a, char *b)
1229 {
1230     static const char *names[] =
1231     {
1232 	"none"
1233 	,"standout"
1234 	,"underline"
1235 	,"reverse"
1236 	,"blink"
1237 	,"dim"
1238 	,"bold"
1239 	,"invis"
1240 	,"protect"
1241 	,"altcharset"
1242     };
1243     char *base_a = a;
1244     char *base_b = b;
1245     int delaying = 0;
1246 
1247     while (*b != 0) {
1248 	while (*a != *b) {
1249 	    if (*a == 0) {
1250 		if (b[0] == '$'
1251 		    && b[1] == '<') {
1252 		    _nc_warning("Did not find delay %s", _nc_visbuf(b));
1253 		} else {
1254 		    _nc_warning("checking sgr(%s) %s\n\tcompare to %s\n\tunmatched %s",
1255 				names[num], _nc_visbuf2(1, base_a),
1256 				_nc_visbuf2(2, base_b),
1257 				_nc_visbuf2(3, b));
1258 		}
1259 		return FALSE;
1260 	    } else if (delaying) {
1261 		a = skip_delay(a);
1262 		b = skip_delay(b);
1263 	    } else {
1264 		a++;
1265 	    }
1266 	}
1267 	switch (*a) {
1268 	case '$':
1269 	    if (delaying == 0)
1270 		delaying = 1;
1271 	    break;
1272 	case '<':
1273 	    if (delaying == 1)
1274 		delaying = 2;
1275 	    break;
1276 	default:
1277 	    delaying = 0;
1278 	    break;
1279 	}
1280 	a++;
1281 	b++;
1282     }
1283     /* ignore delays on the end of the string */
1284     a = ignore_delays(a);
1285     return ((num != 0) || (*a == 0));
1286 }
1287 
1288 static char *
1289 check_sgr(TERMTYPE *tp, char *zero, int num, char *cap, const char *name)
1290 {
1291     char *test;
1292 
1293     _nc_tparm_err = 0;
1294     test = TPARM_9(set_attributes,
1295 		   num == 1,
1296 		   num == 2,
1297 		   num == 3,
1298 		   num == 4,
1299 		   num == 5,
1300 		   num == 6,
1301 		   num == 7,
1302 		   num == 8,
1303 		   num == 9);
1304     if (test != 0) {
1305 	if (PRESENT(cap)) {
1306 	    if (!similar_sgr(num, test, cap)) {
1307 		_nc_warning("%s differs from sgr(%d)\n\t%s=%s\n\tsgr(%d)=%s",
1308 			    name, num,
1309 			    name, _nc_visbuf2(1, cap),
1310 			    num, _nc_visbuf2(2, test));
1311 	    }
1312 	} else if (_nc_capcmp(test, zero)) {
1313 	    _nc_warning("sgr(%d) present, but not %s", num, name);
1314 	}
1315     } else if (PRESENT(cap)) {
1316 	_nc_warning("sgr(%d) missing, but %s present", num, name);
1317     }
1318     if (_nc_tparm_err)
1319 	_nc_warning("stack error in sgr(%d) string", num);
1320     return test;
1321 }
1322 
1323 #define CHECK_SGR(num,name) check_sgr(tp, zero, num, name, #name)
1324 
1325 #ifdef TRACE
1326 /*
1327  * If tic is compiled with TRACE, we'll be able to see the output from the
1328  * DEBUG() macro.  But since it doesn't use traceon(), it always goes to
1329  * the standard error.  Use this function to make it simpler to follow the
1330  * resulting debug traces.
1331  */
1332 static void
1333 show_where(unsigned level)
1334 {
1335     if (_nc_tracing >= DEBUG_LEVEL(level)) {
1336 	char my_name[256];
1337 	_nc_get_type(my_name);
1338 	fprintf(stderr, "\"%s\", line %d, '%s' ",
1339 		_nc_get_source(),
1340 		_nc_curr_line, my_name);
1341     }
1342 }
1343 
1344 #else
1345 #define show_where(level)	/* nothing */
1346 #endif
1347 
1348 /* other sanity-checks (things that we don't want in the normal
1349  * logic that reads a terminfo entry)
1350  */
1351 static void
1352 check_termtype(TERMTYPE *tp, bool literal)
1353 {
1354     bool conflict = FALSE;
1355     unsigned j, k;
1356     char fkeys[STRCOUNT];
1357 
1358     /*
1359      * A terminal entry may contain more than one keycode assigned to
1360      * a given string (e.g., KEY_END and KEY_LL).  But curses will only
1361      * return one (the last one assigned).
1362      */
1363     if (!(_nc_syntax == SYN_TERMCAP && capdump)) {
1364 	memset(fkeys, 0, sizeof(fkeys));
1365 	for (j = 0; _nc_tinfo_fkeys[j].code; j++) {
1366 	    char *a = tp->Strings[_nc_tinfo_fkeys[j].offset];
1367 	    bool first = TRUE;
1368 	    if (!VALID_STRING(a))
1369 		continue;
1370 	    for (k = j + 1; _nc_tinfo_fkeys[k].code; k++) {
1371 		char *b = tp->Strings[_nc_tinfo_fkeys[k].offset];
1372 		if (!VALID_STRING(b)
1373 		    || fkeys[k])
1374 		    continue;
1375 		if (!_nc_capcmp(a, b)) {
1376 		    fkeys[j] = 1;
1377 		    fkeys[k] = 1;
1378 		    if (first) {
1379 			if (!conflict) {
1380 			    _nc_warning("Conflicting key definitions (using the last)");
1381 			    conflict = TRUE;
1382 			}
1383 			fprintf(stderr, "... %s is the same as %s",
1384 				keyname((int) _nc_tinfo_fkeys[j].code),
1385 				keyname((int) _nc_tinfo_fkeys[k].code));
1386 			first = FALSE;
1387 		    } else {
1388 			fprintf(stderr, ", %s",
1389 				keyname((int) _nc_tinfo_fkeys[k].code));
1390 		    }
1391 		}
1392 	    }
1393 	    if (!first)
1394 		fprintf(stderr, "\n");
1395 	}
1396     }
1397 
1398     for (j = 0; j < NUM_STRINGS(tp); j++) {
1399 	char *a = tp->Strings[j];
1400 	if (VALID_STRING(a))
1401 	    check_params(tp, ExtStrname(tp, j, strnames), a);
1402     }
1403 
1404     check_acs(tp);
1405     check_colors(tp);
1406     check_keypad(tp);
1407 
1408     /*
1409      * These may be mismatched because the terminal description relies on
1410      * restoring the cursor visibility by resetting it.
1411      */
1412     ANDMISSING(cursor_invisible, cursor_normal);
1413     ANDMISSING(cursor_visible, cursor_normal);
1414 
1415     if (PRESENT(cursor_visible) && PRESENT(cursor_normal)
1416 	&& !_nc_capcmp(cursor_visible, cursor_normal))
1417 	_nc_warning("cursor_visible is same as cursor_normal");
1418 
1419     /*
1420      * From XSI & O'Reilly, we gather that sc/rc are required if csr is
1421      * given, because the cursor position after the scrolling operation is
1422      * performed is undefined.
1423      */
1424     ANDMISSING(change_scroll_region, save_cursor);
1425     ANDMISSING(change_scroll_region, restore_cursor);
1426 
1427     if (PRESENT(set_attributes)) {
1428 	char *zero = 0;
1429 
1430 	_nc_tparm_err = 0;
1431 	if (PRESENT(exit_attribute_mode)) {
1432 	    zero = strdup(CHECK_SGR(0, exit_attribute_mode));
1433 	} else {
1434 	    zero = strdup(TPARM_9(set_attributes, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1435 	}
1436 	if (_nc_tparm_err)
1437 	    _nc_warning("stack error in sgr(0) string");
1438 
1439 	if (zero != 0) {
1440 	    CHECK_SGR(1, enter_standout_mode);
1441 	    CHECK_SGR(2, enter_underline_mode);
1442 	    CHECK_SGR(3, enter_reverse_mode);
1443 	    CHECK_SGR(4, enter_blink_mode);
1444 	    CHECK_SGR(5, enter_dim_mode);
1445 	    CHECK_SGR(6, enter_bold_mode);
1446 	    CHECK_SGR(7, enter_secure_mode);
1447 	    CHECK_SGR(8, enter_protected_mode);
1448 	    CHECK_SGR(9, enter_alt_charset_mode);
1449 	    free(zero);
1450 	} else {
1451 	    _nc_warning("sgr(0) did not return a value");
1452 	}
1453     } else if (PRESENT(exit_attribute_mode) &&
1454 	       set_attributes != CANCELLED_STRING) {
1455 	if (_nc_syntax == SYN_TERMINFO)
1456 	    _nc_warning("missing sgr string");
1457     }
1458 
1459     if (PRESENT(exit_attribute_mode)) {
1460 	char *check_sgr0 = _nc_trim_sgr0(tp);
1461 
1462 	if (check_sgr0 == 0 || *check_sgr0 == '\0') {
1463 	    _nc_warning("trimmed sgr0 is empty");
1464 	} else {
1465 	    show_where(2);
1466 	    if (check_sgr0 != exit_attribute_mode) {
1467 		DEBUG(2,
1468 		      ("will trim sgr0\n\toriginal sgr0=%s\n\ttrimmed  sgr0=%s",
1469 		       _nc_visbuf2(1, exit_attribute_mode),
1470 		       _nc_visbuf2(2, check_sgr0)));
1471 		free(check_sgr0);
1472 	    } else {
1473 		DEBUG(2,
1474 		      ("will not trim sgr0\n\toriginal sgr0=%s",
1475 		       _nc_visbuf(exit_attribute_mode)));
1476 	    }
1477 	}
1478     }
1479 #ifdef TRACE
1480     show_where(2);
1481     if (!auto_right_margin) {
1482 	DEBUG(2,
1483 	      ("can write to lower-right directly"));
1484     } else if (PRESENT(enter_am_mode) && PRESENT(exit_am_mode)) {
1485 	DEBUG(2,
1486 	      ("can write to lower-right by suppressing automargin"));
1487     } else if ((PRESENT(enter_insert_mode) && PRESENT(exit_insert_mode))
1488 	       || PRESENT(insert_character) || PRESENT(parm_ich)) {
1489 	DEBUG(2,
1490 	      ("can write to lower-right by using inserts"));
1491     } else {
1492 	DEBUG(2,
1493 	      ("cannot write to lower-right"));
1494     }
1495 #endif
1496 
1497     /*
1498      * Some standard applications (e.g., vi) and some non-curses
1499      * applications (e.g., jove) get confused if we have both ich1 and
1500      * smir/rmir.  Let's be nice and warn about that, too, even though
1501      * ncurses handles it.
1502      */
1503     if ((PRESENT(enter_insert_mode) || PRESENT(exit_insert_mode))
1504 	&& PRESENT(parm_ich)) {
1505 	_nc_warning("non-curses applications may be confused by ich1 with smir/rmir");
1506     }
1507 
1508     /*
1509      * Finally, do the non-verbose checks
1510      */
1511     if (save_check_termtype != 0)
1512 	save_check_termtype(tp, literal);
1513 }
1514