xref: /dragonfly/contrib/ncurses/progs/infocmp.c (revision e5a92d33)
1 /****************************************************************************
2  * Copyright (c) 1998-2014,2015 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  *	infocmp.c -- decompile an entry, or compare two entries
37  *		written by Eric S. Raymond
38  *		and Thomas E Dickey
39  */
40 
41 #include <progs.priv.h>
42 
43 #include <dump_entry.h>
44 
45 MODULE_ID("$Id: infocmp.c,v 1.136 2015/09/05 15:49:57 tom Exp $")
46 
47 #define L_CURL "{"
48 #define R_CURL "}"
49 
50 #define MAX_STRING	1024	/* maximum formatted string */
51 
52 const char *_nc_progname = "infocmp";
53 
54 typedef char path[PATH_MAX];
55 
56 /***************************************************************************
57  *
58  * The following control variables, together with the contents of the
59  * terminfo entries, completely determine the actions of the program.
60  *
61  ***************************************************************************/
62 
63 static ENTRY *entries;		/* terminfo entries */
64 static int termcount;		/* count of terminal entries */
65 
66 static bool limited = TRUE;	/* "-r" option is not set */
67 static bool quiet = FALSE;
68 static bool literal = FALSE;
69 static const char *bool_sep = ":";
70 static const char *s_absent = "NULL";
71 static const char *s_cancel = "NULL";
72 static const char *tversion;	/* terminfo version selected */
73 static unsigned itrace;		/* trace flag for debugging */
74 static int mwidth = 60;
75 static int mheight = 65535;
76 static int numbers = 0;		/* format "%'char'" to/from "%{number}" */
77 static int outform = F_TERMINFO;	/* output format */
78 static int sortmode;		/* sort_mode */
79 
80 /* main comparison mode */
81 static int compare;
82 #define C_DEFAULT	0	/* don't force comparison mode */
83 #define C_DIFFERENCE	1	/* list differences between two terminals */
84 #define C_COMMON	2	/* list common capabilities */
85 #define C_NAND		3	/* list capabilities in neither terminal */
86 #define C_USEALL	4	/* generate relative use-form entry */
87 static bool ignorepads;		/* ignore pad prefixes when diffing */
88 
89 #if NO_LEAKS
90 
91 typedef struct {
92     ENTRY *head;
93     ENTRY *tail;
94 } ENTERED;
95 
96 static ENTERED *entered;
97 
98 #undef ExitProgram
99 static void ExitProgram(int code) GCC_NORETURN;
100 /* prototype is to get gcc to accept the noreturn attribute */
101 static void
102 ExitProgram(int code)
103 {
104     int n;
105 
106     for (n = 0; n < termcount; ++n) {
107 	ENTRY *new_head = _nc_head;
108 	ENTRY *new_tail = _nc_tail;
109 	_nc_head = entered[n].head;
110 	_nc_tail = entered[n].tail;
111 	_nc_free_entries(entered[n].head);
112 	_nc_head = new_head;
113 	_nc_tail = new_tail;
114     }
115     _nc_leaks_dump_entry();
116     free(entries);
117     free(entered);
118     _nc_free_tic(code);
119 }
120 #endif
121 
122 static void
123 failed(const char *s)
124 {
125     perror(s);
126     ExitProgram(EXIT_FAILURE);
127 }
128 
129 static char *
130 canonical_name(char *ptr, char *buf)
131 /* extract the terminal type's primary name */
132 {
133     char *bp;
134 
135     _nc_STRCPY(buf, ptr, NAMESIZE);
136     if ((bp = strchr(buf, '|')) != 0)
137 	*bp = '\0';
138 
139     return (buf);
140 }
141 
142 /***************************************************************************
143  *
144  * Predicates for dump function
145  *
146  ***************************************************************************/
147 
148 static int
149 capcmp(PredIdx idx, const char *s, const char *t)
150 /* capability comparison function */
151 {
152     if (!VALID_STRING(s) && !VALID_STRING(t))
153 	return (s != t);
154     else if (!VALID_STRING(s) || !VALID_STRING(t))
155 	return (1);
156 
157     if ((idx == acs_chars_index) || !ignorepads)
158 	return (strcmp(s, t));
159     else
160 	return (_nc_capcmp(s, t));
161 }
162 
163 static int
164 use_predicate(unsigned type, PredIdx idx)
165 /* predicate function to use for use decompilation */
166 {
167     ENTRY *ep;
168 
169     switch (type) {
170     case BOOLEAN:
171 	{
172 	    int is_set = FALSE;
173 
174 	    /*
175 	     * This assumes that multiple use entries are supposed
176 	     * to contribute the logical or of their boolean capabilities.
177 	     * This is true if we take the semantics of multiple uses to
178 	     * be 'each capability gets the first non-default value found
179 	     * in the sequence of use entries'.
180 	     *
181 	     * Note that cancelled or absent booleans are stored as FALSE,
182 	     * unlike numbers and strings, whose cancelled/absent state is
183 	     * recorded in the terminfo database.
184 	     */
185 	    for (ep = &entries[1]; ep < entries + termcount; ep++)
186 		if (ep->tterm.Booleans[idx] == TRUE) {
187 		    is_set = entries[0].tterm.Booleans[idx];
188 		    break;
189 		}
190 	    if (is_set != entries[0].tterm.Booleans[idx])
191 		return (!is_set);
192 	    else
193 		return (FAIL);
194 	}
195 
196     case NUMBER:
197 	{
198 	    int value = ABSENT_NUMERIC;
199 
200 	    /*
201 	     * We take the semantics of multiple uses to be 'each
202 	     * capability gets the first non-default value found
203 	     * in the sequence of use entries'.
204 	     */
205 	    for (ep = &entries[1]; ep < entries + termcount; ep++)
206 		if (VALID_NUMERIC(ep->tterm.Numbers[idx])) {
207 		    value = ep->tterm.Numbers[idx];
208 		    break;
209 		}
210 
211 	    if (value != entries[0].tterm.Numbers[idx])
212 		return (value != ABSENT_NUMERIC);
213 	    else
214 		return (FAIL);
215 	}
216 
217     case STRING:
218 	{
219 	    char *termstr, *usestr = ABSENT_STRING;
220 
221 	    termstr = entries[0].tterm.Strings[idx];
222 
223 	    /*
224 	     * We take the semantics of multiple uses to be 'each
225 	     * capability gets the first non-default value found
226 	     * in the sequence of use entries'.
227 	     */
228 	    for (ep = &entries[1]; ep < entries + termcount; ep++)
229 		if (ep->tterm.Strings[idx]) {
230 		    usestr = ep->tterm.Strings[idx];
231 		    break;
232 		}
233 
234 	    if (usestr == ABSENT_STRING && termstr == ABSENT_STRING)
235 		return (FAIL);
236 	    else if (!usestr || !termstr || capcmp(idx, usestr, termstr))
237 		return (TRUE);
238 	    else
239 		return (FAIL);
240 	}
241     }
242 
243     return (FALSE);		/* pacify compiler */
244 }
245 
246 static bool
247 useeq(ENTRY * e1, ENTRY * e2)
248 /* are the use references in two entries equivalent? */
249 {
250     unsigned i, j;
251 
252     if (e1->nuses != e2->nuses)
253 	return (FALSE);
254 
255     /* Ugh...this is quadratic again */
256     for (i = 0; i < e1->nuses; i++) {
257 	bool foundmatch = FALSE;
258 
259 	/* search second entry for given use reference */
260 	for (j = 0; j < e2->nuses; j++)
261 	    if (!strcmp(e1->uses[i].name, e2->uses[j].name)) {
262 		foundmatch = TRUE;
263 		break;
264 	    }
265 
266 	if (!foundmatch)
267 	    return (FALSE);
268     }
269 
270     return (TRUE);
271 }
272 
273 static bool
274 entryeq(TERMTYPE *t1, TERMTYPE *t2)
275 /* are two entries equivalent? */
276 {
277     unsigned i;
278 
279     for (i = 0; i < NUM_BOOLEANS(t1); i++)
280 	if (t1->Booleans[i] != t2->Booleans[i])
281 	    return (FALSE);
282 
283     for (i = 0; i < NUM_NUMBERS(t1); i++)
284 	if (t1->Numbers[i] != t2->Numbers[i])
285 	    return (FALSE);
286 
287     for (i = 0; i < NUM_STRINGS(t1); i++)
288 	if (capcmp((PredIdx) i, t1->Strings[i], t2->Strings[i]))
289 	    return (FALSE);
290 
291     return (TRUE);
292 }
293 
294 #define TIC_EXPAND(result) _nc_tic_expand(result, outform==F_TERMINFO, numbers)
295 
296 static void
297 print_uses(ENTRY * ep, FILE *fp)
298 /* print an entry's use references */
299 {
300     unsigned i;
301 
302     if (!ep->nuses)
303 	fputs("NULL", fp);
304     else
305 	for (i = 0; i < ep->nuses; i++) {
306 	    fputs(ep->uses[i].name, fp);
307 	    if (i < ep->nuses - 1)
308 		fputs(" ", fp);
309 	}
310 }
311 
312 static const char *
313 dump_boolean(int val)
314 /* display the value of a boolean capability */
315 {
316     switch (val) {
317     case ABSENT_BOOLEAN:
318 	return (s_absent);
319     case CANCELLED_BOOLEAN:
320 	return (s_cancel);
321     case FALSE:
322 	return ("F");
323     case TRUE:
324 	return ("T");
325     default:
326 	return ("?");
327     }
328 }
329 
330 static void
331 dump_numeric(int val, char *buf)
332 /* display the value of a boolean capability */
333 {
334     switch (val) {
335     case ABSENT_NUMERIC:
336 	_nc_STRCPY(buf, s_absent, MAX_STRING);
337 	break;
338     case CANCELLED_NUMERIC:
339 	_nc_STRCPY(buf, s_cancel, MAX_STRING);
340 	break;
341     default:
342 	_nc_SPRINTF(buf, _nc_SLIMIT(MAX_STRING) "%d", val);
343 	break;
344     }
345 }
346 
347 static void
348 dump_string(char *val, char *buf)
349 /* display the value of a string capability */
350 {
351     if (val == ABSENT_STRING)
352 	_nc_STRCPY(buf, s_absent, MAX_STRING);
353     else if (val == CANCELLED_STRING)
354 	_nc_STRCPY(buf, s_cancel, MAX_STRING);
355     else {
356 	_nc_SPRINTF(buf, _nc_SLIMIT(MAX_STRING)
357 		    "'%.*s'", MAX_STRING - 3, TIC_EXPAND(val));
358     }
359 }
360 
361 /*
362  * Show "comparing..." message for the given terminal names.
363  */
364 static void
365 show_comparing(char **names)
366 {
367     if (itrace) {
368 	switch (compare) {
369 	case C_DIFFERENCE:
370 	    (void) fprintf(stderr, "%s: dumping differences\n", _nc_progname);
371 	    break;
372 
373 	case C_COMMON:
374 	    (void) fprintf(stderr, "%s: dumping common capabilities\n", _nc_progname);
375 	    break;
376 
377 	case C_NAND:
378 	    (void) fprintf(stderr, "%s: dumping differences\n", _nc_progname);
379 	    break;
380 	}
381     }
382     if (*names) {
383 	printf("comparing %s", *names++);
384 	if (*names) {
385 	    printf(" to %s", *names++);
386 	    while (*names) {
387 		printf(", %s", *names++);
388 	    }
389 	}
390 	printf(".\n");
391     }
392 }
393 
394 /*
395  * ncurses stores two types of non-standard capabilities:
396  * a) capabilities listed past the "STOP-HERE" comment in the Caps file.
397  *    These are used in the terminfo source file to provide data for termcaps,
398  *    e.g., when there is no equivalent capability in terminfo, as well as for
399  *    widely-used non-standard capabilities.
400  * b) user-definable capabilities, via "tic -x".
401  *
402  * However, if "-x" is omitted from the tic command, both types of
403  * non-standard capability are not loaded into the terminfo database.  This
404  * macro is used for limit-checks against the symbols that tic uses to omit
405  * the two types of non-standard entry.
406  */
407 #if NCURSES_XNAMES
408 #define check_user_definable(n,limit) if (!_nc_user_definable && (n) > (limit)) break
409 #else
410 #define check_user_definable(n,limit) if ((n) > (limit)) break
411 #endif
412 
413 /*
414  * Use these macros to simplify loops on C_COMMON and C_NAND:
415  */
416 #define for_each_entry() while (entries[extra].tterm.term_names)
417 #define next_entry           (&(entries[extra++].tterm))
418 
419 static void
420 compare_predicate(PredType type, PredIdx idx, const char *name)
421 /* predicate function to use for entry difference reports */
422 {
423     ENTRY *e1 = &entries[0];
424     ENTRY *e2 = &entries[1];
425     char buf1[MAX_STRING];
426     char buf2[MAX_STRING];
427     int b1, b2;
428     int n1, n2;
429     char *s1, *s2;
430     bool found;
431     int extra = 1;
432 
433     switch (type) {
434     case CMP_BOOLEAN:
435 	check_user_definable(idx, BOOLWRITE);
436 	b1 = e1->tterm.Booleans[idx];
437 	switch (compare) {
438 	case C_DIFFERENCE:
439 	    b2 = next_entry->Booleans[idx];
440 	    if (!(b1 == ABSENT_BOOLEAN && b2 == ABSENT_BOOLEAN) && b1 != b2)
441 		(void) printf("\t%s: %s%s%s.\n",
442 			      name,
443 			      dump_boolean(b1),
444 			      bool_sep,
445 			      dump_boolean(b2));
446 	    break;
447 
448 	case C_COMMON:
449 	    if (b1 != ABSENT_BOOLEAN) {
450 		found = TRUE;
451 		for_each_entry() {
452 		    b2 = next_entry->Booleans[idx];
453 		    if (b1 != b2) {
454 			found = FALSE;
455 			break;
456 		    }
457 		}
458 		if (found) {
459 		    (void) printf("\t%s= %s.\n", name, dump_boolean(b1));
460 		}
461 	    }
462 	    break;
463 
464 	case C_NAND:
465 	    if (b1 == ABSENT_BOOLEAN) {
466 		found = TRUE;
467 		for_each_entry() {
468 		    b2 = next_entry->Booleans[idx];
469 		    if (b1 != b2) {
470 			found = FALSE;
471 			break;
472 		    }
473 		}
474 		if (found) {
475 		    (void) printf("\t!%s.\n", name);
476 		}
477 	    }
478 	    break;
479 	}
480 	break;
481 
482     case CMP_NUMBER:
483 	check_user_definable(idx, NUMWRITE);
484 	n1 = e1->tterm.Numbers[idx];
485 	switch (compare) {
486 	case C_DIFFERENCE:
487 	    n2 = next_entry->Numbers[idx];
488 	    if (!((n1 == ABSENT_NUMERIC && n2 == ABSENT_NUMERIC)) && n1 != n2) {
489 		dump_numeric(n1, buf1);
490 		dump_numeric(n2, buf2);
491 		(void) printf("\t%s: %s, %s.\n", name, buf1, buf2);
492 	    }
493 	    break;
494 
495 	case C_COMMON:
496 	    if (n1 != ABSENT_NUMERIC) {
497 		found = TRUE;
498 		for_each_entry() {
499 		    n2 = next_entry->Numbers[idx];
500 		    if (n1 != n2) {
501 			found = FALSE;
502 			break;
503 		    }
504 		}
505 		if (found) {
506 		    dump_numeric(n1, buf1);
507 		    (void) printf("\t%s= %s.\n", name, buf1);
508 		}
509 	    }
510 	    break;
511 
512 	case C_NAND:
513 	    if (n1 == ABSENT_NUMERIC) {
514 		found = TRUE;
515 		for_each_entry() {
516 		    n2 = next_entry->Numbers[idx];
517 		    if (n1 != n2) {
518 			found = FALSE;
519 			break;
520 		    }
521 		}
522 		if (found) {
523 		    (void) printf("\t!%s.\n", name);
524 		}
525 	    }
526 	    break;
527 	}
528 	break;
529 
530     case CMP_STRING:
531 	check_user_definable(idx, STRWRITE);
532 	s1 = e1->tterm.Strings[idx];
533 	switch (compare) {
534 	case C_DIFFERENCE:
535 	    s2 = next_entry->Strings[idx];
536 	    if (capcmp(idx, s1, s2)) {
537 		dump_string(s1, buf1);
538 		dump_string(s2, buf2);
539 		if (strcmp(buf1, buf2))
540 		    (void) printf("\t%s: %s, %s.\n", name, buf1, buf2);
541 	    }
542 	    break;
543 
544 	case C_COMMON:
545 	    if (s1 != ABSENT_STRING) {
546 		found = TRUE;
547 		for_each_entry() {
548 		    s2 = next_entry->Strings[idx];
549 		    if (capcmp(idx, s1, s2) != 0) {
550 			found = FALSE;
551 			break;
552 		    }
553 		}
554 		if (found) {
555 		    (void) printf("\t%s= '%s'.\n", name, TIC_EXPAND(s1));
556 		}
557 	    }
558 	    break;
559 
560 	case C_NAND:
561 	    if (s1 == ABSENT_STRING) {
562 		found = TRUE;
563 		for_each_entry() {
564 		    s2 = next_entry->Strings[idx];
565 		    if (s2 != s1) {
566 			found = FALSE;
567 			break;
568 		    }
569 		}
570 		if (found) {
571 		    (void) printf("\t!%s.\n", name);
572 		}
573 	    }
574 	    break;
575 	}
576 	break;
577 
578     case CMP_USE:
579 	/* unlike the other modes, this compares *all* use entries */
580 	switch (compare) {
581 	case C_DIFFERENCE:
582 	    if (!useeq(e1, e2)) {
583 		(void) fputs("\tuse: ", stdout);
584 		print_uses(e1, stdout);
585 		fputs(", ", stdout);
586 		print_uses(e2, stdout);
587 		fputs(".\n", stdout);
588 	    }
589 	    break;
590 
591 	case C_COMMON:
592 	    if (e1->nuses) {
593 		found = TRUE;
594 		for_each_entry() {
595 		    e2 = &entries[extra++];
596 		    if (e2->nuses != e1->nuses || !useeq(e1, e2)) {
597 			found = FALSE;
598 			break;
599 		    }
600 		}
601 		if (found) {
602 		    (void) fputs("\tuse: ", stdout);
603 		    print_uses(e1, stdout);
604 		    fputs(".\n", stdout);
605 		}
606 	    }
607 	    break;
608 
609 	case C_NAND:
610 	    if (!e1->nuses) {
611 		found = TRUE;
612 		for_each_entry() {
613 		    e2 = &entries[extra++];
614 		    if (e2->nuses != e1->nuses) {
615 			found = FALSE;
616 			break;
617 		    }
618 		}
619 		if (found) {
620 		    (void) printf("\t!use.\n");
621 		}
622 	    }
623 	    break;
624 	}
625     }
626 }
627 
628 /***************************************************************************
629  *
630  * Init string analysis
631  *
632  ***************************************************************************/
633 
634 #define DATA(from, to) { { from }, { to } }
635 #define DATAX()        DATA("", "")
636 
637 typedef struct {
638     const char from[4];
639     const char to[12];
640 } assoc;
641 
642 static const assoc std_caps[] =
643 {
644     /* these are specified by X.364 and iBCS2 */
645     DATA("\033c", "RIS"),	/* full reset */
646     DATA("\0337", "SC"),	/* save cursor */
647     DATA("\0338", "RC"),	/* restore cursor */
648     DATA("\033[r", "RSR"),	/* not an X.364 mnemonic */
649     DATA("\033[m", "SGR0"),	/* not an X.364 mnemonic */
650     DATA("\033[2J", "ED2"),	/* clear page */
651 
652     /* this group is specified by ISO 2022 */
653     DATA("\033(0", "ISO DEC G0"),	/* enable DEC graphics for G0 */
654     DATA("\033(A", "ISO UK G0"),	/* enable UK chars for G0 */
655     DATA("\033(B", "ISO US G0"),	/* enable US chars for G0 */
656     DATA("\033)0", "ISO DEC G1"),	/* enable DEC graphics for G1 */
657     DATA("\033)A", "ISO UK G1"),	/* enable UK chars for G1 */
658     DATA("\033)B", "ISO US G1"),	/* enable US chars for G1 */
659 
660     /* these are DEC private controls widely supported by emulators */
661     DATA("\033=", "DECPAM"),	/* application keypad mode */
662     DATA("\033>", "DECPNM"),	/* normal keypad mode */
663     DATA("\033<", "DECANSI"),	/* enter ANSI mode */
664     DATA("\033[!p", "DECSTR"),	/* soft reset */
665     DATA("\033 F", "S7C1T"),	/* 7-bit controls */
666 
667     DATAX()
668 };
669 
670 static const assoc std_modes[] =
671 /* ECMA \E[ ... [hl] modes recognized by many emulators */
672 {
673     DATA("2", "AM"),		/* keyboard action mode */
674     DATA("4", "IRM"),		/* insert/replace mode */
675     DATA("12", "SRM"),		/* send/receive mode */
676     DATA("20", "LNM"),		/* linefeed mode */
677     DATAX()
678 };
679 
680 static const assoc private_modes[] =
681 /* DEC \E[ ... [hl] modes recognized by many emulators */
682 {
683     DATA("1", "CKM"),		/* application cursor keys */
684     DATA("2", "ANM"),		/* set VT52 mode */
685     DATA("3", "COLM"),		/* 132-column mode */
686     DATA("4", "SCLM"),		/* smooth scroll */
687     DATA("5", "SCNM"),		/* reverse video mode */
688     DATA("6", "OM"),		/* origin mode */
689     DATA("7", "AWM"),		/* wraparound mode */
690     DATA("8", "ARM"),		/* auto-repeat mode */
691     DATAX()
692 };
693 
694 static const assoc ecma_highlights[] =
695 /* recognize ECMA attribute sequences */
696 {
697     DATA("0", "NORMAL"),	/* normal */
698     DATA("1", "+BOLD"),		/* bold on */
699     DATA("2", "+DIM"),		/* dim on */
700     DATA("3", "+ITALIC"),	/* italic on */
701     DATA("4", "+UNDERLINE"),	/* underline on */
702     DATA("5", "+BLINK"),	/* blink on */
703     DATA("6", "+FASTBLINK"),	/* fastblink on */
704     DATA("7", "+REVERSE"),	/* reverse on */
705     DATA("8", "+INVISIBLE"),	/* invisible on */
706     DATA("9", "+DELETED"),	/* deleted on */
707     DATA("10", "MAIN-FONT"),	/* select primary font */
708     DATA("11", "ALT-FONT-1"),	/* select alternate font 1 */
709     DATA("12", "ALT-FONT-2"),	/* select alternate font 2 */
710     DATA("13", "ALT-FONT-3"),	/* select alternate font 3 */
711     DATA("14", "ALT-FONT-4"),	/* select alternate font 4 */
712     DATA("15", "ALT-FONT-5"),	/* select alternate font 5 */
713     DATA("16", "ALT-FONT-6"),	/* select alternate font 6 */
714     DATA("17", "ALT-FONT-7"),	/* select alternate font 7 */
715     DATA("18", "ALT-FONT-1"),	/* select alternate font 1 */
716     DATA("19", "ALT-FONT-1"),	/* select alternate font 1 */
717     DATA("20", "FRAKTUR"),	/* Fraktur font */
718     DATA("21", "DOUBLEUNDER"),	/* double underline */
719     DATA("22", "-DIM"),		/* dim off */
720     DATA("23", "-ITALIC"),	/* italic off */
721     DATA("24", "-UNDERLINE"),	/* underline off */
722     DATA("25", "-BLINK"),	/* blink off */
723     DATA("26", "-FASTBLINK"),	/* fastblink off */
724     DATA("27", "-REVERSE"),	/* reverse off */
725     DATA("28", "-INVISIBLE"),	/* invisible off */
726     DATA("29", "-DELETED"),	/* deleted off */
727     DATAX()
728 };
729 
730 #undef DATA
731 
732 static int
733 skip_csi(const char *cap)
734 {
735     int result = 0;
736     if (cap[0] == '\033' && cap[1] == '[')
737 	result = 2;
738     else if (UChar(cap[0]) == 0233)
739 	result = 1;
740     return result;
741 }
742 
743 static bool
744 same_param(const char *table, const char *param, size_t length)
745 {
746     bool result = FALSE;
747     if (strncmp(table, param, length) == 0) {
748 	result = !isdigit(UChar(param[length]));
749     }
750     return result;
751 }
752 
753 static char *
754 lookup_params(const assoc * table, char *dst, char *src)
755 {
756     char *result = 0;
757     const char *ep = strtok(src, ";");
758 
759     if (ep != 0) {
760 	const assoc *ap;
761 
762 	do {
763 	    bool found = FALSE;
764 
765 	    for (ap = table; ap->from[0]; ap++) {
766 		size_t tlen = strlen(ap->from);
767 
768 		if (same_param(ap->from, ep, tlen)) {
769 		    _nc_STRCAT(dst, ap->to, MAX_TERMINFO_LENGTH);
770 		    found = TRUE;
771 		    break;
772 		}
773 	    }
774 
775 	    if (!found)
776 		_nc_STRCAT(dst, ep, MAX_TERMINFO_LENGTH);
777 	    _nc_STRCAT(dst, ";", MAX_TERMINFO_LENGTH);
778 	} while
779 	    ((ep = strtok((char *) 0, ";")));
780 
781 	dst[strlen(dst) - 1] = '\0';
782 
783 	result = dst;
784     }
785     return result;
786 }
787 
788 static void
789 analyze_string(const char *name, const char *cap, TERMTYPE *tp)
790 {
791     char buf2[MAX_TERMINFO_LENGTH];
792     const char *sp;
793     const assoc *ap;
794     int tp_lines = tp->Numbers[2];
795 
796     if (!VALID_STRING(cap))
797 	return;
798     (void) printf("%s: ", name);
799 
800     for (sp = cap; *sp; sp++) {
801 	int i;
802 	int csi;
803 	size_t len = 0;
804 	size_t next;
805 	const char *expansion = 0;
806 	char buf3[MAX_TERMINFO_LENGTH];
807 
808 	/* first, check other capabilities in this entry */
809 	for (i = 0; i < STRCOUNT; i++) {
810 	    char *cp = tp->Strings[i];
811 
812 	    /* don't use function-key capabilities */
813 	    if (strnames[i][0] == 'k' && strnames[i][1] == 'f')
814 		continue;
815 
816 	    if (VALID_STRING(cp) &&
817 		cp[0] != '\0' &&
818 		cp != cap) {
819 		len = strlen(cp);
820 		(void) strncpy(buf2, sp, len);
821 		buf2[len] = '\0';
822 
823 		if (_nc_capcmp(cp, buf2))
824 		    continue;
825 
826 #define ISRS(s)	(!strncmp((s), "is", (size_t) 2) || !strncmp((s), "rs", (size_t) 2))
827 		/*
828 		 * Theoretically we just passed the test for translation
829 		 * (equality once the padding is stripped).  However, there
830 		 * are a few more hoops that need to be jumped so that
831 		 * identical pairs of initialization and reset strings
832 		 * don't just refer to each other.
833 		 */
834 		if (ISRS(name) || ISRS(strnames[i]))
835 		    if (cap < cp)
836 			continue;
837 #undef ISRS
838 
839 		expansion = strnames[i];
840 		break;
841 	    }
842 	}
843 
844 	/* now check the standard capabilities */
845 	if (!expansion) {
846 	    csi = skip_csi(sp);
847 	    for (ap = std_caps; ap->from[0]; ap++) {
848 		size_t adj = (size_t) (csi ? 2 : 0);
849 
850 		len = strlen(ap->from);
851 		if (csi && skip_csi(ap->from) != csi)
852 		    continue;
853 		if (len > adj
854 		    && strncmp(ap->from + adj, sp + csi, len - adj) == 0) {
855 		    expansion = ap->to;
856 		    len -= adj;
857 		    len += (size_t) csi;
858 		    break;
859 		}
860 	    }
861 	}
862 
863 	/* now check for standard-mode sequences */
864 	if (!expansion
865 	    && (csi = skip_csi(sp)) != 0
866 	    && (len = (strspn) (sp + csi, "0123456789;"))
867 	    && (len < sizeof(buf3))
868 	    && (next = (size_t) csi + len)
869 	    && ((sp[next] == 'h') || (sp[next] == 'l'))) {
870 
871 	    _nc_STRCPY(buf2,
872 		       ((sp[next] == 'h')
873 			? "ECMA+"
874 			: "ECMA-"),
875 		       sizeof(buf2));
876 	    (void) strncpy(buf3, sp + csi, len);
877 	    buf3[len] = '\0';
878 	    len += (size_t) csi + 1;
879 
880 	    expansion = lookup_params(std_modes, buf2, buf3);
881 	}
882 
883 	/* now check for private-mode sequences */
884 	if (!expansion
885 	    && (csi = skip_csi(sp)) != 0
886 	    && sp[csi] == '?'
887 	    && (len = (strspn) (sp + csi + 1, "0123456789;"))
888 	    && (len < sizeof(buf3))
889 	    && (next = (size_t) csi + 1 + len)
890 	    && ((sp[next] == 'h') || (sp[next] == 'l'))) {
891 
892 	    _nc_STRCPY(buf2,
893 		       ((sp[next] == 'h')
894 			? "DEC+"
895 			: "DEC-"),
896 		       sizeof(buf2));
897 	    (void) strncpy(buf3, sp + csi + 1, len);
898 	    buf3[len] = '\0';
899 	    len += (size_t) csi + 2;
900 
901 	    expansion = lookup_params(private_modes, buf2, buf3);
902 	}
903 
904 	/* now check for ECMA highlight sequences */
905 	if (!expansion
906 	    && (csi = skip_csi(sp)) != 0
907 	    && (len = (strspn) (sp + csi, "0123456789;")) != 0
908 	    && (len < sizeof(buf3))
909 	    && (next = (size_t) csi + len)
910 	    && sp[next] == 'm') {
911 
912 	    _nc_STRCPY(buf2, "SGR:", sizeof(buf2));
913 	    (void) strncpy(buf3, sp + csi, len);
914 	    buf3[len] = '\0';
915 	    len += (size_t) csi + 1;
916 
917 	    expansion = lookup_params(ecma_highlights, buf2, buf3);
918 	}
919 
920 	if (!expansion
921 	    && (csi = skip_csi(sp)) != 0
922 	    && sp[csi] == 'm') {
923 	    len = (size_t) csi + 1;
924 	    _nc_STRCPY(buf2, "SGR:", sizeof(buf2));
925 	    _nc_STRCAT(buf2, ecma_highlights[0].to, sizeof(buf2));
926 	    expansion = buf2;
927 	}
928 
929 	/* now check for scroll region reset */
930 	if (!expansion
931 	    && (csi = skip_csi(sp)) != 0) {
932 	    if (sp[csi] == 'r') {
933 		expansion = "RSR";
934 		len = 1;
935 	    } else {
936 		_nc_SPRINTF(buf2, _nc_SLIMIT(sizeof(buf2)) "1;%dr", tp_lines);
937 		len = strlen(buf2);
938 		if (strncmp(buf2, sp + csi, len) == 0)
939 		    expansion = "RSR";
940 	    }
941 	    len += (size_t) csi;
942 	}
943 
944 	/* now check for home-down */
945 	if (!expansion
946 	    && (csi = skip_csi(sp)) != 0) {
947 	    _nc_SPRINTF(buf2, _nc_SLIMIT(sizeof(buf2)) "%d;1H", tp_lines);
948 	    len = strlen(buf2);
949 	    if (strncmp(buf2, sp + csi, len) == 0) {
950 		expansion = "LL";
951 	    } else {
952 		_nc_SPRINTF(buf2, _nc_SLIMIT(sizeof(buf2)) "%dH", tp_lines);
953 		len = strlen(buf2);
954 		if (strncmp(buf2, sp + csi, len) == 0) {
955 		    expansion = "LL";
956 		}
957 	    }
958 	    len += (size_t) csi;
959 	}
960 
961 	/* now look at the expansion we got, if any */
962 	if (expansion) {
963 	    printf("{%s}", expansion);
964 	    sp += len - 1;
965 	} else {
966 	    /* couldn't match anything */
967 	    buf2[0] = *sp;
968 	    buf2[1] = '\0';
969 	    fputs(TIC_EXPAND(buf2), stdout);
970 	}
971     }
972     putchar('\n');
973 }
974 
975 /***************************************************************************
976  *
977  * File comparison
978  *
979  ***************************************************************************/
980 
981 static void
982 file_comparison(int argc, char *argv[])
983 {
984 #define MAXCOMPARE	2
985     /* someday we may allow comparisons on more files */
986     int filecount = 0;
987     ENTRY *heads[MAXCOMPARE];
988     ENTRY *qp, *rp;
989     int i, n;
990 
991     memset(heads, 0, sizeof(heads));
992     dump_init((char *) 0, F_LITERAL, S_TERMINFO, 0, 65535, itrace, FALSE,
993 	      FALSE, FALSE);
994 
995     for (n = 0; n < argc && n < MAXCOMPARE; n++) {
996 	if (freopen(argv[n], "r", stdin) == 0)
997 	    _nc_err_abort("Can't open %s", argv[n]);
998 
999 #if NO_LEAKS
1000 	entered[n].head = _nc_head;
1001 	entered[n].tail = _nc_tail;
1002 #endif
1003 	_nc_head = _nc_tail = 0;
1004 
1005 	/* parse entries out of the source file */
1006 	_nc_set_source(argv[n]);
1007 	_nc_read_entry_source(stdin, NULL, TRUE, literal, NULLHOOK);
1008 
1009 	if (itrace)
1010 	    (void) fprintf(stderr, "Resolving file %d...\n", n - 0);
1011 
1012 	/* maybe do use resolution */
1013 	if (!_nc_resolve_uses2(!limited, literal)) {
1014 	    (void) fprintf(stderr,
1015 			   "There are unresolved use entries in %s:\n",
1016 			   argv[n]);
1017 	    for_entry_list(qp) {
1018 		if (qp->nuses) {
1019 		    (void) fputs(qp->tterm.term_names, stderr);
1020 		    (void) fputc('\n', stderr);
1021 		}
1022 	    }
1023 	    ExitProgram(EXIT_FAILURE);
1024 	}
1025 
1026 	heads[filecount] = _nc_head;
1027 	filecount++;
1028     }
1029 
1030     /* OK, all entries are in core.  Ready to do the comparison */
1031     if (itrace)
1032 	(void) fprintf(stderr, "Entries are now in core...\n");
1033 
1034     /* The entry-matching loop. Sigh, this is intrinsically quadratic. */
1035     for (qp = heads[0]; qp; qp = qp->next) {
1036 	for (rp = heads[1]; rp; rp = rp->next)
1037 	    if (_nc_entry_match(qp->tterm.term_names, rp->tterm.term_names)) {
1038 		if (qp->ncrosslinks < MAX_CROSSLINKS)
1039 		    qp->crosslinks[qp->ncrosslinks] = rp;
1040 		qp->ncrosslinks++;
1041 
1042 		if (rp->ncrosslinks < MAX_CROSSLINKS)
1043 		    rp->crosslinks[rp->ncrosslinks] = qp;
1044 		rp->ncrosslinks++;
1045 	    }
1046     }
1047 
1048     /* now we have two circular lists with crosslinks */
1049     if (itrace)
1050 	(void) fprintf(stderr, "Name matches are done...\n");
1051 
1052     for (qp = heads[0]; qp; qp = qp->next) {
1053 	if (qp->ncrosslinks > 1) {
1054 	    (void) fprintf(stderr,
1055 			   "%s in file 1 (%s) has %d matches in file 2 (%s):\n",
1056 			   _nc_first_name(qp->tterm.term_names),
1057 			   argv[0],
1058 			   qp->ncrosslinks,
1059 			   argv[1]);
1060 	    for (i = 0; i < qp->ncrosslinks; i++)
1061 		(void) fprintf(stderr,
1062 			       "\t%s\n",
1063 			       _nc_first_name((qp->crosslinks[i])->tterm.term_names));
1064 	}
1065     }
1066 
1067     for (rp = heads[1]; rp; rp = rp->next) {
1068 	if (rp->ncrosslinks > 1) {
1069 	    (void) fprintf(stderr,
1070 			   "%s in file 2 (%s) has %d matches in file 1 (%s):\n",
1071 			   _nc_first_name(rp->tterm.term_names),
1072 			   argv[1],
1073 			   rp->ncrosslinks,
1074 			   argv[0]);
1075 	    for (i = 0; i < rp->ncrosslinks; i++)
1076 		(void) fprintf(stderr,
1077 			       "\t%s\n",
1078 			       _nc_first_name((rp->crosslinks[i])->tterm.term_names));
1079 	}
1080     }
1081 
1082     (void) printf("In file 1 (%s) only:\n", argv[0]);
1083     for (qp = heads[0]; qp; qp = qp->next)
1084 	if (qp->ncrosslinks == 0)
1085 	    (void) printf("\t%s\n",
1086 			  _nc_first_name(qp->tterm.term_names));
1087 
1088     (void) printf("In file 2 (%s) only:\n", argv[1]);
1089     for (rp = heads[1]; rp; rp = rp->next)
1090 	if (rp->ncrosslinks == 0)
1091 	    (void) printf("\t%s\n",
1092 			  _nc_first_name(rp->tterm.term_names));
1093 
1094     (void) printf("The following entries are equivalent:\n");
1095     for (qp = heads[0]; qp; qp = qp->next) {
1096 	if (qp->ncrosslinks == 1) {
1097 	    rp = qp->crosslinks[0];
1098 
1099 	    repair_acsc(&qp->tterm);
1100 	    repair_acsc(&rp->tterm);
1101 #if NCURSES_XNAMES
1102 	    _nc_align_termtype(&qp->tterm, &rp->tterm);
1103 #endif
1104 	    if (entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp)) {
1105 		char name1[NAMESIZE], name2[NAMESIZE];
1106 
1107 		(void) canonical_name(qp->tterm.term_names, name1);
1108 		(void) canonical_name(rp->tterm.term_names, name2);
1109 
1110 		(void) printf("%s = %s\n", name1, name2);
1111 	    }
1112 	}
1113     }
1114 
1115     (void) printf("Differing entries:\n");
1116     termcount = 2;
1117     for (qp = heads[0]; qp; qp = qp->next) {
1118 
1119 	if (qp->ncrosslinks == 1) {
1120 	    rp = qp->crosslinks[0];
1121 #if NCURSES_XNAMES
1122 	    /* sorry - we have to do this on each pass */
1123 	    _nc_align_termtype(&qp->tterm, &rp->tterm);
1124 #endif
1125 	    if (!(entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp))) {
1126 		char name1[NAMESIZE], name2[NAMESIZE];
1127 		char *names[3];
1128 
1129 		names[0] = name1;
1130 		names[1] = name2;
1131 		names[2] = 0;
1132 
1133 		entries[0] = *qp;
1134 		entries[1] = *rp;
1135 
1136 		(void) canonical_name(qp->tterm.term_names, name1);
1137 		(void) canonical_name(rp->tterm.term_names, name2);
1138 
1139 		switch (compare) {
1140 		case C_DIFFERENCE:
1141 		    show_comparing(names);
1142 		    compare_entry(compare_predicate, &entries->tterm, quiet);
1143 		    break;
1144 
1145 		case C_COMMON:
1146 		    show_comparing(names);
1147 		    compare_entry(compare_predicate, &entries->tterm, quiet);
1148 		    break;
1149 
1150 		case C_NAND:
1151 		    show_comparing(names);
1152 		    compare_entry(compare_predicate, &entries->tterm, quiet);
1153 		    break;
1154 
1155 		}
1156 	    }
1157 	}
1158     }
1159 }
1160 
1161 static void
1162 usage(void)
1163 {
1164 #define DATA(s) s "\n"
1165     static const char head[] =
1166     {
1167 	DATA("Usage: infocmp [options] [-A directory] [-B directory] [termname...]")
1168 	DATA("")
1169 	DATA("Options:")
1170     };
1171 #undef DATA
1172 #define DATA(s) s
1173     static const char options[][45] =
1174     {
1175 	"  -0    print single-row"
1176 	,"  -1    print single-column"
1177 	,"  -C    use termcap-names"
1178 	,"  -D    print database locations"
1179 	,"  -E    format output as C tables"
1180 	,"  -F    compare terminfo-files"
1181 	,"  -G    format %{number} to %'char'"
1182 	,"  -I    use terminfo-names"
1183 	,"  -K    use termcap-names and BSD syntax"
1184 	,"  -L    use long names"
1185 	,"  -R subset (see manpage)"
1186 	,"  -T    eliminate size limits (test)"
1187 	,"  -U    do not post-process entries"
1188 	,"  -V    print version"
1189 #if NCURSES_XNAMES
1190 	,"  -a    with -F, list commented-out caps"
1191 #endif
1192 	,"  -c    list common capabilities"
1193 	,"  -d    list different capabilities"
1194 	,"  -e    format output for C initializer"
1195 	,"  -f    with -1, format complex strings"
1196 	,"  -g    format %'char' to %{number}"
1197 	,"  -i    analyze initialization/reset"
1198 	,"  -l    output terminfo names"
1199 	,"  -n    list capabilities in neither"
1200 	,"  -p    ignore padding specifiers"
1201 	,"  -Q number  dump compiled description"
1202 	,"  -q    brief listing, removes headers"
1203 	,"  -r    with -C, output in termcap form"
1204 	,"  -r    with -F, resolve use-references"
1205 	,"  -s [d|i|l|c] sort fields"
1206 #if NCURSES_XNAMES
1207 	,"  -t    suppress commented-out capabilities"
1208 #endif
1209 	,"  -u    produce source with 'use='"
1210 	,"  -v number  (verbose)"
1211 	,"  -w number  (width)"
1212 #if NCURSES_XNAMES
1213 	,"  -x    unknown capabilities are user-defined"
1214 #endif
1215     };
1216 #undef DATA
1217     const size_t last = SIZEOF(options);
1218     const size_t left = (last + 1) / 2;
1219     size_t n;
1220 
1221     fputs(head, stderr);
1222     for (n = 0; n < left; n++) {
1223 	size_t m = n + left;
1224 	if (m < last)
1225 	    fprintf(stderr, "%-40.40s%s\n", options[n], options[m]);
1226 	else
1227 	    fprintf(stderr, "%s\n", options[n]);
1228     }
1229     ExitProgram(EXIT_FAILURE);
1230 }
1231 
1232 static char *
1233 any_initializer(const char *fmt, const char *type)
1234 {
1235     static char *initializer;
1236     static size_t need;
1237     char *s;
1238 
1239     if (initializer == 0) {
1240 	need = (strlen(entries->tterm.term_names)
1241 		+ strlen(type)
1242 		+ strlen(fmt));
1243 	initializer = (char *) malloc(need + 1);
1244 	if (initializer == 0)
1245 	    failed("any_initializer");
1246     }
1247 
1248     _nc_STRCPY(initializer, entries->tterm.term_names, need);
1249     for (s = initializer; *s != 0 && *s != '|'; s++) {
1250 	if (!isalnum(UChar(*s)))
1251 	    *s = '_';
1252     }
1253     *s = 0;
1254     _nc_SPRINTF(s, _nc_SLIMIT(need) fmt, type);
1255     return initializer;
1256 }
1257 
1258 static char *
1259 name_initializer(const char *type)
1260 {
1261     return any_initializer("_%s_data", type);
1262 }
1263 
1264 static char *
1265 string_variable(const char *type)
1266 {
1267     return any_initializer("_s_%s", type);
1268 }
1269 
1270 /* dump C initializers for the terminal type */
1271 static void
1272 dump_initializers(TERMTYPE *term)
1273 {
1274     unsigned n;
1275     const char *str = 0;
1276 
1277     printf("\nstatic char %s[] = \"%s\";\n\n",
1278 	   name_initializer("alias"), entries->tterm.term_names);
1279 
1280     for_each_string(n, term) {
1281 	char buf[MAX_STRING], *sp, *tp;
1282 
1283 	if (VALID_STRING(term->Strings[n])) {
1284 	    tp = buf;
1285 #define TP_LIMIT	((MAX_STRING - 5) - (size_t)(tp - buf))
1286 	    *tp++ = '"';
1287 	    for (sp = term->Strings[n];
1288 		 *sp != 0 && TP_LIMIT > 2;
1289 		 sp++) {
1290 		if (isascii(UChar(*sp))
1291 		    && isprint(UChar(*sp))
1292 		    && *sp != '\\'
1293 		    && *sp != '"')
1294 		    *tp++ = *sp;
1295 		else {
1296 		    _nc_SPRINTF(tp, _nc_SLIMIT(TP_LIMIT) "\\%03o", UChar(*sp));
1297 		    tp += 4;
1298 		}
1299 	    }
1300 	    *tp++ = '"';
1301 	    *tp = '\0';
1302 	    (void) printf("static char %-20s[] = %s;\n",
1303 			  string_variable(ExtStrname(term, (int) n, strnames)),
1304 			  buf);
1305 	}
1306     }
1307     printf("\n");
1308 
1309     (void) printf("static char %s[] = %s\n", name_initializer("bool"), L_CURL);
1310 
1311     for_each_boolean(n, term) {
1312 	switch ((int) (term->Booleans[n])) {
1313 	case TRUE:
1314 	    str = "TRUE";
1315 	    break;
1316 
1317 	case FALSE:
1318 	    str = "FALSE";
1319 	    break;
1320 
1321 	case ABSENT_BOOLEAN:
1322 	    str = "ABSENT_BOOLEAN";
1323 	    break;
1324 
1325 	case CANCELLED_BOOLEAN:
1326 	    str = "CANCELLED_BOOLEAN";
1327 	    break;
1328 	}
1329 	(void) printf("\t/* %3u: %-8s */\t%s,\n",
1330 		      n, ExtBoolname(term, (int) n, boolnames), str);
1331     }
1332     (void) printf("%s;\n", R_CURL);
1333 
1334     (void) printf("static short %s[] = %s\n", name_initializer("number"), L_CURL);
1335 
1336     for_each_number(n, term) {
1337 	char buf[BUFSIZ];
1338 	switch (term->Numbers[n]) {
1339 	case ABSENT_NUMERIC:
1340 	    str = "ABSENT_NUMERIC";
1341 	    break;
1342 	case CANCELLED_NUMERIC:
1343 	    str = "CANCELLED_NUMERIC";
1344 	    break;
1345 	default:
1346 	    _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf)) "%d", term->Numbers[n]);
1347 	    str = buf;
1348 	    break;
1349 	}
1350 	(void) printf("\t/* %3u: %-8s */\t%s,\n", n,
1351 		      ExtNumname(term, (int) n, numnames), str);
1352     }
1353     (void) printf("%s;\n", R_CURL);
1354 
1355     (void) printf("static char * %s[] = %s\n", name_initializer("string"), L_CURL);
1356 
1357     for_each_string(n, term) {
1358 
1359 	if (term->Strings[n] == ABSENT_STRING)
1360 	    str = "ABSENT_STRING";
1361 	else if (term->Strings[n] == CANCELLED_STRING)
1362 	    str = "CANCELLED_STRING";
1363 	else {
1364 	    str = string_variable(ExtStrname(term, (int) n, strnames));
1365 	}
1366 	(void) printf("\t/* %3u: %-8s */\t%s,\n", n,
1367 		      ExtStrname(term, (int) n, strnames), str);
1368     }
1369     (void) printf("%s;\n", R_CURL);
1370 
1371 #if NCURSES_XNAMES
1372     if ((NUM_BOOLEANS(term) != BOOLCOUNT)
1373 	|| (NUM_NUMBERS(term) != NUMCOUNT)
1374 	|| (NUM_STRINGS(term) != STRCOUNT)) {
1375 	(void) printf("static char * %s[] = %s\n",
1376 		      name_initializer("string_ext"), L_CURL);
1377 	for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
1378 	    (void) printf("\t/* %3u: bool */\t\"%s\",\n",
1379 			  n, ExtBoolname(term, (int) n, boolnames));
1380 	}
1381 	for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
1382 	    (void) printf("\t/* %3u: num */\t\"%s\",\n",
1383 			  n, ExtNumname(term, (int) n, numnames));
1384 	}
1385 	for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
1386 	    (void) printf("\t/* %3u: str */\t\"%s\",\n",
1387 			  n, ExtStrname(term, (int) n, strnames));
1388 	}
1389 	(void) printf("%s;\n", R_CURL);
1390     }
1391 #endif
1392 }
1393 
1394 /* dump C initializers for the terminal type */
1395 static void
1396 dump_termtype(TERMTYPE *term)
1397 {
1398     (void) printf("\t%s\n\t\t%s,\n", L_CURL, name_initializer("alias"));
1399     (void) printf("\t\t(char *)0,\t/* pointer to string table */\n");
1400 
1401     (void) printf("\t\t%s,\n", name_initializer("bool"));
1402     (void) printf("\t\t%s,\n", name_initializer("number"));
1403 
1404     (void) printf("\t\t%s,\n", name_initializer("string"));
1405 
1406 #if NCURSES_XNAMES
1407     (void) printf("#if NCURSES_XNAMES\n");
1408     (void) printf("\t\t(char *)0,\t/* pointer to extended string table */\n");
1409     (void) printf("\t\t%s,\t/* ...corresponding names */\n",
1410 		  ((NUM_BOOLEANS(term) != BOOLCOUNT)
1411 		   || (NUM_NUMBERS(term) != NUMCOUNT)
1412 		   || (NUM_STRINGS(term) != STRCOUNT))
1413 		  ? name_initializer("string_ext")
1414 		  : "(char **)0");
1415 
1416     (void) printf("\t\t%d,\t\t/* count total Booleans */\n", NUM_BOOLEANS(term));
1417     (void) printf("\t\t%d,\t\t/* count total Numbers */\n", NUM_NUMBERS(term));
1418     (void) printf("\t\t%d,\t\t/* count total Strings */\n", NUM_STRINGS(term));
1419 
1420     (void) printf("\t\t%d,\t\t/* count extensions to Booleans */\n",
1421 		  NUM_BOOLEANS(term) - BOOLCOUNT);
1422     (void) printf("\t\t%d,\t\t/* count extensions to Numbers */\n",
1423 		  NUM_NUMBERS(term) - NUMCOUNT);
1424     (void) printf("\t\t%d,\t\t/* count extensions to Strings */\n",
1425 		  NUM_STRINGS(term) - STRCOUNT);
1426 
1427     (void) printf("#endif /* NCURSES_XNAMES */\n");
1428 #else
1429     (void) term;
1430 #endif /* NCURSES_XNAMES */
1431     (void) printf("\t%s\n", R_CURL);
1432 }
1433 
1434 static int
1435 optarg_to_number(void)
1436 {
1437     char *temp = 0;
1438     long value = strtol(optarg, &temp, 0);
1439 
1440     if (temp == 0 || temp == optarg || *temp != 0) {
1441 	fprintf(stderr, "Expected a number, not \"%s\"\n", optarg);
1442 	ExitProgram(EXIT_FAILURE);
1443     }
1444     return (int) value;
1445 }
1446 
1447 static char *
1448 terminal_env(void)
1449 {
1450     char *terminal;
1451 
1452     if ((terminal = getenv("TERM")) == 0) {
1453 	(void) fprintf(stderr,
1454 		       "%s: environment variable TERM not set\n",
1455 		       _nc_progname);
1456 	exit(EXIT_FAILURE);
1457     }
1458     return terminal;
1459 }
1460 
1461 /*
1462  * Show the databases that infocmp knows about.  The location to which it writes is
1463  */
1464 static void
1465 show_databases(void)
1466 {
1467     DBDIRS state;
1468     int offset;
1469     const char *path2;
1470 
1471     _nc_first_db(&state, &offset);
1472     while ((path2 = _nc_next_db(&state, &offset)) != 0) {
1473 	printf("%s\n", path2);
1474     }
1475     _nc_last_db();
1476 }
1477 
1478 /***************************************************************************
1479  *
1480  * Main sequence
1481  *
1482  ***************************************************************************/
1483 
1484 #if NO_LEAKS
1485 #define MAIN_LEAKS() \
1486     free(myargv); \
1487     free(tfile); \
1488     free(tname)
1489 #else
1490 #define MAIN_LEAKS()		/* nothing */
1491 #endif
1492 
1493 int
1494 main(int argc, char *argv[])
1495 {
1496     /* Avoid "local data >32k" error with mwcc */
1497     /* Also avoid overflowing smaller stacks on systems like AmigaOS */
1498     path *tfile = 0;
1499     char **tname = 0;
1500     size_t maxterms;
1501 
1502     char **myargv;
1503 
1504     char *firstdir, *restdir;
1505     int c, i, len;
1506     bool formatted = FALSE;
1507     bool filecompare = FALSE;
1508     int initdump = 0;
1509     bool init_analyze = FALSE;
1510     bool suppress_untranslatable = FALSE;
1511     int quickdump = 0;
1512 
1513     /* where is the terminfo database location going to default to? */
1514     restdir = firstdir = 0;
1515 
1516 #if NCURSES_XNAMES
1517     use_extended_names(FALSE);
1518 #endif
1519     _nc_strict_bsd = 0;
1520 
1521     _nc_progname = _nc_rootname(argv[0]);
1522 
1523     /* make sure we have enough space to add two terminal entries */
1524     myargv = typeCalloc(char *, (size_t) (argc + 3));
1525     if (myargv == 0)
1526 	failed("myargv");
1527 
1528     memcpy(myargv, argv, (sizeof(char *) * (size_t) argc));
1529     argv = myargv;
1530 
1531     while ((c = getopt(argc,
1532 		       argv,
1533 		       "01A:aB:CcDdEeFfGgIiKLlnpQ:qR:rs:TtUuVv:w:x")) != -1) {
1534 	switch (c) {
1535 	case '0':
1536 	    mwidth = 65535;
1537 	    mheight = 1;
1538 	    break;
1539 
1540 	case '1':
1541 	    mwidth = 0;
1542 	    break;
1543 
1544 	case 'A':
1545 	    firstdir = optarg;
1546 	    break;
1547 
1548 #if NCURSES_XNAMES
1549 	case 'a':
1550 	    _nc_disable_period = TRUE;
1551 	    use_extended_names(TRUE);
1552 	    break;
1553 #endif
1554 	case 'B':
1555 	    restdir = optarg;
1556 	    break;
1557 
1558 	case 'K':
1559 	    _nc_strict_bsd = 1;
1560 	    /* FALLTHRU */
1561 	case 'C':
1562 	    outform = F_TERMCAP;
1563 	    tversion = "BSD";
1564 	    if (sortmode == S_DEFAULT)
1565 		sortmode = S_TERMCAP;
1566 	    break;
1567 
1568 	case 'D':
1569 	    show_databases();
1570 	    ExitProgram(EXIT_SUCCESS);
1571 	    break;
1572 
1573 	case 'c':
1574 	    compare = C_COMMON;
1575 	    break;
1576 
1577 	case 'd':
1578 	    compare = C_DIFFERENCE;
1579 	    break;
1580 
1581 	case 'E':
1582 	    initdump |= 2;
1583 	    break;
1584 
1585 	case 'e':
1586 	    initdump |= 1;
1587 	    break;
1588 
1589 	case 'F':
1590 	    filecompare = TRUE;
1591 	    break;
1592 
1593 	case 'f':
1594 	    formatted = TRUE;
1595 	    break;
1596 
1597 	case 'G':
1598 	    numbers = 1;
1599 	    break;
1600 
1601 	case 'g':
1602 	    numbers = -1;
1603 	    break;
1604 
1605 	case 'I':
1606 	    outform = F_TERMINFO;
1607 	    if (sortmode == S_DEFAULT)
1608 		sortmode = S_VARIABLE;
1609 	    tversion = 0;
1610 	    break;
1611 
1612 	case 'i':
1613 	    init_analyze = TRUE;
1614 	    break;
1615 
1616 	case 'L':
1617 	    outform = F_VARIABLE;
1618 	    if (sortmode == S_DEFAULT)
1619 		sortmode = S_VARIABLE;
1620 	    break;
1621 
1622 	case 'l':
1623 	    outform = F_TERMINFO;
1624 	    break;
1625 
1626 	case 'n':
1627 	    compare = C_NAND;
1628 	    break;
1629 
1630 	case 'p':
1631 	    ignorepads = TRUE;
1632 	    break;
1633 
1634 	case 'Q':
1635 	    quickdump = optarg_to_number();
1636 	    break;
1637 
1638 	case 'q':
1639 	    quiet = TRUE;
1640 	    s_absent = "-";
1641 	    s_cancel = "@";
1642 	    bool_sep = ", ";
1643 	    break;
1644 
1645 	case 'R':
1646 	    tversion = optarg;
1647 	    break;
1648 
1649 	case 'r':
1650 	    tversion = 0;
1651 	    break;
1652 
1653 	case 's':
1654 	    if (*optarg == 'd')
1655 		sortmode = S_NOSORT;
1656 	    else if (*optarg == 'i')
1657 		sortmode = S_TERMINFO;
1658 	    else if (*optarg == 'l')
1659 		sortmode = S_VARIABLE;
1660 	    else if (*optarg == 'c')
1661 		sortmode = S_TERMCAP;
1662 	    else {
1663 		(void) fprintf(stderr,
1664 			       "%s: unknown sort mode\n",
1665 			       _nc_progname);
1666 		ExitProgram(EXIT_FAILURE);
1667 	    }
1668 	    break;
1669 
1670 	case 'T':
1671 	    limited = FALSE;
1672 	    break;
1673 
1674 #if NCURSES_XNAMES
1675 	case 't':
1676 	    _nc_disable_period = FALSE;
1677 	    suppress_untranslatable = TRUE;
1678 	    break;
1679 #endif
1680 
1681 	case 'U':
1682 	    literal = TRUE;
1683 	    break;
1684 
1685 	case 'u':
1686 	    compare = C_USEALL;
1687 	    break;
1688 
1689 	case 'V':
1690 	    puts(curses_version());
1691 	    ExitProgram(EXIT_SUCCESS);
1692 
1693 	case 'v':
1694 	    itrace = (unsigned) optarg_to_number();
1695 	    set_trace_level(itrace);
1696 	    break;
1697 
1698 	case 'w':
1699 	    mwidth = optarg_to_number();
1700 	    break;
1701 
1702 #if NCURSES_XNAMES
1703 	case 'x':
1704 	    use_extended_names(TRUE);
1705 	    break;
1706 #endif
1707 
1708 	default:
1709 	    usage();
1710 	}
1711     }
1712 
1713     maxterms = (size_t) (argc + 2 - optind);
1714     if ((tfile = typeMalloc(path, maxterms)) == 0)
1715 	failed("tfile");
1716     if ((tname = typeCalloc(char *, maxterms)) == 0)
1717 	  failed("tname");
1718     if ((entries = typeCalloc(ENTRY, maxterms)) == 0)
1719 	failed("entries");
1720 #if NO_LEAKS
1721     if ((entered = typeCalloc(ENTERED, maxterms)) == 0)
1722 	failed("entered");
1723 #endif
1724 
1725     if (tfile == 0
1726 	|| tname == 0
1727 	|| entries == 0) {
1728 	fprintf(stderr, "%s: not enough memory\n", _nc_progname);
1729 	ExitProgram(EXIT_FAILURE);
1730     }
1731 
1732     /* by default, sort by terminfo name */
1733     if (sortmode == S_DEFAULT)
1734 	sortmode = S_TERMINFO;
1735 
1736     /* make sure we have at least one terminal name to work with */
1737     if (optind >= argc)
1738 	argv[argc++] = terminal_env();
1739 
1740     /* if user is after a comparison, make sure we have two entries */
1741     if (compare != C_DEFAULT && optind >= argc - 1)
1742 	argv[argc++] = terminal_env();
1743 
1744     /* exactly one terminal name with no options means display it */
1745     /* exactly two terminal names with no options means do -d */
1746     if (compare == C_DEFAULT) {
1747 	switch (argc - optind) {
1748 	default:
1749 	    fprintf(stderr, "%s: too many names to compare\n", _nc_progname);
1750 	    ExitProgram(EXIT_FAILURE);
1751 	case 1:
1752 	    break;
1753 	case 2:
1754 	    compare = C_DIFFERENCE;
1755 	    break;
1756 	}
1757     }
1758 
1759     /* set up for display */
1760     dump_init(tversion, outform, sortmode, mwidth, mheight, itrace,
1761 	      formatted, FALSE, quickdump);
1762 
1763     if (!filecompare) {
1764 	/* grab the entries */
1765 	termcount = 0;
1766 	for (; optind < argc; optind++) {
1767 	    const char *directory = termcount ? restdir : firstdir;
1768 	    int status;
1769 
1770 	    tname[termcount] = argv[optind];
1771 
1772 	    if (directory) {
1773 #if NCURSES_USE_DATABASE
1774 #if MIXEDCASE_FILENAMES
1775 #define LEAF_FMT "%c"
1776 #else
1777 #define LEAF_FMT "%02x"
1778 #endif
1779 		_nc_SPRINTF(tfile[termcount],
1780 			    _nc_SLIMIT(sizeof(path))
1781 			    "%s/" LEAF_FMT "/%s",
1782 			    directory,
1783 			    UChar(*argv[optind]), argv[optind]);
1784 		if (itrace)
1785 		    (void) fprintf(stderr,
1786 				   "%s: reading entry %s from file %s\n",
1787 				   _nc_progname,
1788 				   argv[optind], tfile[termcount]);
1789 
1790 		status = _nc_read_file_entry(tfile[termcount],
1791 					     &entries[termcount].tterm);
1792 #else
1793 		(void) fprintf(stderr, "%s: terminfo files not supported\n",
1794 			       _nc_progname);
1795 		MAIN_LEAKS();
1796 		ExitProgram(EXIT_FAILURE);
1797 #endif
1798 	    } else {
1799 		if (itrace)
1800 		    (void) fprintf(stderr,
1801 				   "%s: reading entry %s from database\n",
1802 				   _nc_progname,
1803 				   tname[termcount]);
1804 
1805 		status = _nc_read_entry(tname[termcount],
1806 					tfile[termcount],
1807 					&entries[termcount].tterm);
1808 	    }
1809 
1810 	    if (status <= 0) {
1811 		(void) fprintf(stderr,
1812 			       "%s: couldn't open terminfo file %s.\n",
1813 			       _nc_progname,
1814 			       tfile[termcount]);
1815 		MAIN_LEAKS();
1816 		ExitProgram(EXIT_FAILURE);
1817 	    }
1818 	    repair_acsc(&entries[termcount].tterm);
1819 	    termcount++;
1820 	}
1821 
1822 #if NCURSES_XNAMES
1823 	if (termcount > 1)
1824 	    _nc_align_termtype(&entries[0].tterm, &entries[1].tterm);
1825 #endif
1826 
1827 	/* dump as C initializer for the terminal type */
1828 	if (initdump) {
1829 	    if (initdump & 1)
1830 		dump_termtype(&entries[0].tterm);
1831 	    if (initdump & 2)
1832 		dump_initializers(&entries[0].tterm);
1833 	}
1834 
1835 	/* analyze the init strings */
1836 	else if (init_analyze) {
1837 #undef CUR
1838 #define CUR	entries[0].tterm.
1839 	    analyze_string("is1", init_1string, &entries[0].tterm);
1840 	    analyze_string("is2", init_2string, &entries[0].tterm);
1841 	    analyze_string("is3", init_3string, &entries[0].tterm);
1842 	    analyze_string("rs1", reset_1string, &entries[0].tterm);
1843 	    analyze_string("rs2", reset_2string, &entries[0].tterm);
1844 	    analyze_string("rs3", reset_3string, &entries[0].tterm);
1845 	    analyze_string("smcup", enter_ca_mode, &entries[0].tterm);
1846 	    analyze_string("rmcup", exit_ca_mode, &entries[0].tterm);
1847 	    analyze_string("smkx", keypad_xmit, &entries[0].tterm);
1848 	    analyze_string("rmkx", keypad_local, &entries[0].tterm);
1849 #undef CUR
1850 	} else {
1851 
1852 	    /*
1853 	     * Here's where the real work gets done
1854 	     */
1855 	    switch (compare) {
1856 	    case C_DEFAULT:
1857 		if (itrace)
1858 		    (void) fprintf(stderr,
1859 				   "%s: about to dump %s\n",
1860 				   _nc_progname,
1861 				   tname[0]);
1862 		if (!quiet)
1863 		    (void)
1864 				  printf("#\tReconstructed via infocmp from file: %s\n",
1865 				  tfile[0]);
1866 		dump_entry(&entries[0].tterm,
1867 			   suppress_untranslatable,
1868 			   limited,
1869 			   numbers,
1870 			   NULL);
1871 		len = show_entry();
1872 		if (itrace)
1873 		    (void) fprintf(stderr, "%s: length %d\n", _nc_progname, len);
1874 		break;
1875 
1876 	    case C_DIFFERENCE:
1877 		show_comparing(tname);
1878 		compare_entry(compare_predicate, &entries->tterm, quiet);
1879 		break;
1880 
1881 	    case C_COMMON:
1882 		show_comparing(tname);
1883 		compare_entry(compare_predicate, &entries->tterm, quiet);
1884 		break;
1885 
1886 	    case C_NAND:
1887 		show_comparing(tname);
1888 		compare_entry(compare_predicate, &entries->tterm, quiet);
1889 		break;
1890 
1891 	    case C_USEALL:
1892 		if (itrace)
1893 		    (void) fprintf(stderr, "%s: dumping use entry\n", _nc_progname);
1894 		dump_entry(&entries[0].tterm,
1895 			   suppress_untranslatable,
1896 			   limited,
1897 			   numbers,
1898 			   use_predicate);
1899 		for (i = 1; i < termcount; i++)
1900 		    dump_uses(tname[i], !(outform == F_TERMCAP
1901 					  || outform == F_TCONVERR));
1902 		len = show_entry();
1903 		if (itrace)
1904 		    (void) fprintf(stderr, "%s: length %d\n", _nc_progname, len);
1905 		break;
1906 	    }
1907 	}
1908     } else if (compare == C_USEALL) {
1909 	(void) fprintf(stderr, "Sorry, -u doesn't work with -F\n");
1910     } else if (compare == C_DEFAULT) {
1911 	(void) fprintf(stderr, "Use `tic -[CI] <file>' for this.\n");
1912     } else if (argc - optind != 2) {
1913 	(void) fprintf(stderr,
1914 		       "File comparison needs exactly two file arguments.\n");
1915     } else {
1916 	file_comparison(argc - optind, argv + optind);
1917     }
1918 
1919     MAIN_LEAKS();
1920     ExitProgram(EXIT_SUCCESS);
1921 }
1922 
1923 /* infocmp.c ends here */
1924