xref: /openbsd-src/usr.bin/infocmp/infocmp.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: infocmp.c,v 1.17 2003/04/08 19:08:58 deraadt Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc.              *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30 
31 /****************************************************************************
32  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
33  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
34  ****************************************************************************/
35 
36 /*
37  *	infocmp.c -- decompile an entry, or compare two entries
38  *		written by Eric S. Raymond
39  */
40 
41 #include <progs.priv.h>
42 
43 #include <term_entry.h>
44 #include <dump_entry.h>
45 
46 MODULE_ID("$From: infocmp.c,v 1.60 2001/02/24 22:03:12 tom Exp $")
47 
48 #define L_CURL "{"
49 #define R_CURL "}"
50 
51 #define MAXTERMS	32	/* max # terminal arguments we can handle */
52 #define MAX_STRING	1024	/* maximum formatted string */
53 
54 const char *_nc_progname = "infocmp";
55 
56 typedef char path[PATH_MAX];
57 
58 /***************************************************************************
59  *
60  * The following control variables, together with the contents of the
61  * terminfo entries, completely determine the actions of the program.
62  *
63  ***************************************************************************/
64 
65 static char *tname[MAXTERMS];	/* terminal type names */
66 static ENTRY entries[MAXTERMS];	/* terminfo entries */
67 static int termcount;		/* count of terminal entries */
68 
69 static bool limited = TRUE;	/* "-r" option is not set */
70 static bool quiet = FALSE;
71 static const char *bool_sep = ":";
72 static const char *s_absent = "NULL";
73 static const char *s_cancel = "NULL";
74 static const char *tversion;	/* terminfo version selected */
75 static int itrace;		/* trace flag for debugging */
76 static int mwidth = 60;
77 static int numbers = 0;		/* format "%'char'" to/from "%{number}" */
78 static int outform = F_TERMINFO;	/* output format */
79 static int sortmode;		/* sort_mode */
80 
81 /* main comparison mode */
82 static int compare;
83 #define C_DEFAULT	0	/* don't force comparison mode */
84 #define C_DIFFERENCE	1	/* list differences between two terminals */
85 #define C_COMMON	2	/* list common capabilities */
86 #define C_NAND		3	/* list capabilities in neither terminal */
87 #define C_USEALL	4	/* generate relative use-form entry */
88 static bool ignorepads;		/* ignore pad prefixes when diffing */
89 
90 #if NO_LEAKS
91 #undef ExitProgram
92 static void
93 ExitProgram(int code) GCC_NORETURN;
94 /* prototype is to get gcc to accept the noreturn attribute */
95      static void
96        ExitProgram(int code)
97 {
98     while (termcount-- > 0)
99 	_nc_free_termtype(&entries[termcount].tterm);
100     _nc_leaks_dump_entry();
101     _nc_free_and_exit(code);
102 }
103 #endif
104 
105 static char *
106 canonical_name(char *ptr, char *buf, size_t bufl)
107 /* extract the terminal type's primary name */
108 {
109     char *bp;
110 
111     (void) strlcpy(buf, ptr, bufl);
112     if ((bp = strchr(buf, '|')) != 0)
113 	*bp = '\0';
114 
115     return (buf);
116 }
117 
118 /***************************************************************************
119  *
120  * Predicates for dump function
121  *
122  ***************************************************************************/
123 
124 static int
125 capcmp(int idx, const char *s, const char *t)
126 /* capability comparison function */
127 {
128     if (!VALID_STRING(s) && !VALID_STRING(t))
129 	return (s != t);
130     else if (!VALID_STRING(s) || !VALID_STRING(t))
131 	return (1);
132 
133     if ((idx == acs_chars_index) || !ignorepads)
134 	return (strcmp(s, t));
135     else
136 	return (_nc_capcmp(s, t));
137 }
138 
139 static int
140 use_predicate(int type, int idx)
141 /* predicate function to use for use decompilation */
142 {
143     ENTRY *ep;
144 
145     switch (type) {
146     case BOOLEAN:
147 	{
148 	    int is_set = FALSE;
149 
150 	    /*
151 	     * This assumes that multiple use entries are supposed
152 	     * to contribute the logical or of their boolean capabilities.
153 	     * This is true if we take the semantics of multiple uses to
154 	     * be 'each capability gets the first non-default value found
155 	     * in the sequence of use entries'.
156 	     *
157 	     * Note that cancelled or absent booleans are stored as FALSE,
158 	     * unlike numbers and strings, whose cancelled/absent state is
159 	     * recorded in the terminfo database.
160 	     */
161 	    for (ep = &entries[1]; ep < entries + termcount; ep++)
162 		if (ep->tterm.Booleans[idx] == TRUE) {
163 		    is_set = entries[0].tterm.Booleans[idx];
164 		    break;
165 		}
166 	    if (is_set != entries[0].tterm.Booleans[idx])
167 		return (!is_set);
168 	    else
169 		return (FAIL);
170 	}
171 
172     case NUMBER:
173 	{
174 	    int value = ABSENT_NUMERIC;
175 
176 	    /*
177 	     * We take the semantics of multiple uses to be 'each
178 	     * capability gets the first non-default value found
179 	     * in the sequence of use entries'.
180 	     */
181 	    for (ep = &entries[1]; ep < entries + termcount; ep++)
182 		if (VALID_NUMERIC(ep->tterm.Numbers[idx])) {
183 		    value = ep->tterm.Numbers[idx];
184 		    break;
185 		}
186 
187 	    if (value != entries[0].tterm.Numbers[idx])
188 		return (value != ABSENT_NUMERIC);
189 	    else
190 		return (FAIL);
191 	}
192 
193     case STRING:
194 	{
195 	    char *termstr, *usestr = ABSENT_STRING;
196 
197 	    termstr = entries[0].tterm.Strings[idx];
198 
199 	    /*
200 	     * We take the semantics of multiple uses to be 'each
201 	     * capability gets the first non-default value found
202 	     * in the sequence of use entries'.
203 	     */
204 	    for (ep = &entries[1]; ep < entries + termcount; ep++)
205 		if (ep->tterm.Strings[idx]) {
206 		    usestr = ep->tterm.Strings[idx];
207 		    break;
208 		}
209 
210 	    if (usestr == ABSENT_STRING && termstr == ABSENT_STRING)
211 		return (FAIL);
212 	    else if (!usestr || !termstr || capcmp(idx, usestr, termstr))
213 		return (TRUE);
214 	    else
215 		return (FAIL);
216 	}
217     }
218 
219     return (FALSE);		/* pacify compiler */
220 }
221 
222 static bool
223 useeq(ENTRY * e1, ENTRY * e2)
224 /* are the use references in two entries equivalent? */
225 {
226     int i, j;
227 
228     if (e1->nuses != e2->nuses)
229 	return (FALSE);
230 
231     /* Ugh...this is quadratic again */
232     for (i = 0; i < e1->nuses; i++) {
233 	bool foundmatch = FALSE;
234 
235 	/* search second entry for given use reference */
236 	for (j = 0; j < e2->nuses; j++)
237 	    if (!strcmp(e1->uses[i].name, e2->uses[j].name)) {
238 		foundmatch = TRUE;
239 		break;
240 	    }
241 
242 	if (!foundmatch)
243 	    return (FALSE);
244     }
245 
246     return (TRUE);
247 }
248 
249 static bool
250 entryeq(TERMTYPE * t1, TERMTYPE * t2)
251 /* are two entries equivalent? */
252 {
253     int i;
254 
255     for (i = 0; i < NUM_BOOLEANS(t1); i++)
256 	if (t1->Booleans[i] != t2->Booleans[i])
257 	    return (FALSE);
258 
259     for (i = 0; i < NUM_NUMBERS(t1); i++)
260 	if (t1->Numbers[i] != t2->Numbers[i])
261 	    return (FALSE);
262 
263     for (i = 0; i < NUM_STRINGS(t1); i++)
264 	if (capcmp(i, t1->Strings[i], t2->Strings[i]))
265 	    return (FALSE);
266 
267     return (TRUE);
268 }
269 
270 #define TIC_EXPAND(result) _nc_tic_expand(result, outform==F_TERMINFO, numbers)
271 
272 static void
273 print_uses(ENTRY * ep, FILE * fp)
274 /* print an entry's use references */
275 {
276     int i;
277 
278     if (!ep->nuses)
279 	fputs("NULL", fp);
280     else
281 	for (i = 0; i < ep->nuses; i++) {
282 	    fputs(ep->uses[i].name, fp);
283 	    if (i < ep->nuses - 1)
284 		fputs(" ", fp);
285 	}
286 }
287 
288 static const char *
289 dump_boolean(int val)
290 /* display the value of a boolean capability */
291 {
292     switch (val) {
293     case ABSENT_BOOLEAN:
294 	return (s_absent);
295     case CANCELLED_BOOLEAN:
296 	return (s_cancel);
297     case FALSE:
298 	return ("F");
299     case TRUE:
300 	return ("T");
301     default:
302 	return ("?");
303     }
304 }
305 
306 static void
307 dump_numeric(int val, char *buf, size_t bufl)
308 /* display the value of a boolean capability */
309 {
310     switch (val) {
311     case ABSENT_NUMERIC:
312 	strlcpy(buf, s_absent, bufl);
313 	break;
314     case CANCELLED_NUMERIC:
315 	strlcpy(buf, s_cancel, bufl);
316 	break;
317     default:
318 	snprintf(buf, bufl, "%d", val);
319 	break;
320     }
321 }
322 
323 static void
324 dump_string(char *val, char *buf, size_t bufl)
325 /* display the value of a string capability */
326 {
327     if (val == ABSENT_STRING)
328 	strlcpy(buf, s_absent, bufl);
329     else if (val == CANCELLED_STRING)
330 	strlcpy(buf, s_cancel, bufl);
331     else {
332 	snprintf(buf, bufl, "'%.*s'", MAX_STRING - 3, TIC_EXPAND(val));
333     }
334 }
335 
336 static void
337 compare_predicate(int type, int idx, const char *name)
338 /* predicate function to use for entry difference reports */
339 {
340     ENTRY *e1 = &entries[0];
341     ENTRY *e2 = &entries[1];
342     char buf1[MAX_STRING], buf2[MAX_STRING];
343     int b1, b2;
344     int n1, n2;
345     char *s1, *s2;
346 
347     switch (type) {
348     case CMP_BOOLEAN:
349 	b1 = e1->tterm.Booleans[idx];
350 	b2 = e2->tterm.Booleans[idx];
351 	switch (compare) {
352 	case C_DIFFERENCE:
353 	    if (!(b1 == ABSENT_BOOLEAN && b2 == ABSENT_BOOLEAN) && b1 != b2)
354 		(void) printf("\t%s: %s%s%s.\n",
355 			      name,
356 			      dump_boolean(b1),
357 			      bool_sep,
358 			      dump_boolean(b2));
359 	    break;
360 
361 	case C_COMMON:
362 	    if (b1 == b2 && b1 != ABSENT_BOOLEAN)
363 		(void) printf("\t%s= %s.\n", name, dump_boolean(b1));
364 	    break;
365 
366 	case C_NAND:
367 	    if (b1 == ABSENT_BOOLEAN && b2 == ABSENT_BOOLEAN)
368 		(void) printf("\t!%s.\n", name);
369 	    break;
370 	}
371 	break;
372 
373     case CMP_NUMBER:
374 	n1 = e1->tterm.Numbers[idx];
375 	n2 = e2->tterm.Numbers[idx];
376 	dump_numeric(n1, buf1, sizeof buf1);
377 	dump_numeric(n2, buf2, sizeof buf2);
378 	switch (compare) {
379 	case C_DIFFERENCE:
380 	    if (!((n1 == ABSENT_NUMERIC && n2 == ABSENT_NUMERIC)) && n1 != n2)
381 		(void) printf("\t%s: %s, %s.\n", name, buf1, buf2);
382 	    break;
383 
384 	case C_COMMON:
385 	    if (n1 != ABSENT_NUMERIC && n2 != ABSENT_NUMERIC && n1 == n2)
386 		(void) printf("\t%s= %s.\n", name, buf1);
387 	    break;
388 
389 	case C_NAND:
390 	    if (n1 == ABSENT_NUMERIC && n2 == ABSENT_NUMERIC)
391 		(void) printf("\t!%s.\n", name);
392 	    break;
393 	}
394 	break;
395 
396     case CMP_STRING:
397 	s1 = e1->tterm.Strings[idx];
398 	s2 = e2->tterm.Strings[idx];
399 	switch (compare) {
400 	case C_DIFFERENCE:
401 	    if (capcmp(idx, s1, s2)) {
402 		dump_string(s1, buf1, sizeof buf1);
403 		dump_string(s2, buf2, sizeof buf2);
404 		if (strcmp(buf1, buf2))
405 		    (void) printf("\t%s: %s, %s.\n", name, buf1, buf2);
406 	    }
407 	    break;
408 
409 	case C_COMMON:
410 	    if (s1 && s2 && !capcmp(idx, s1, s2))
411 		(void) printf("\t%s= '%s'.\n", name, TIC_EXPAND(s1));
412 	    break;
413 
414 	case C_NAND:
415 	    if (!s1 && !s2)
416 		(void) printf("\t!%s.\n", name);
417 	    break;
418 	}
419 	break;
420 
421     case CMP_USE:
422 	/* unlike the other modes, this compares *all* use entries */
423 	switch (compare) {
424 	case C_DIFFERENCE:
425 	    if (!useeq(e1, e2)) {
426 		(void) fputs("\tuse: ", stdout);
427 		print_uses(e1, stdout);
428 		fputs(", ", stdout);
429 		print_uses(e2, stdout);
430 		fputs(".\n", stdout);
431 	    }
432 	    break;
433 
434 	case C_COMMON:
435 	    if (e1->nuses && e2->nuses && useeq(e1, e2)) {
436 		(void) fputs("\tuse: ", stdout);
437 		print_uses(e1, stdout);
438 		fputs(".\n", stdout);
439 	    }
440 	    break;
441 
442 	case C_NAND:
443 	    if (!e1->nuses && !e2->nuses)
444 		(void) printf("\t!use.\n");
445 	    break;
446 	}
447     }
448 }
449 
450 /***************************************************************************
451  *
452  * Init string analysis
453  *
454  ***************************************************************************/
455 
456 typedef struct {
457     const char *from;
458     const char *to;
459 } assoc;
460 
461 static const assoc std_caps[] =
462 {
463     /* these are specified by X.364 and iBCS2 */
464     {"\033c", "RIS"},		/* full reset */
465     {"\0337", "SC"},		/* save cursor */
466     {"\0338", "RC"},		/* restore cursor */
467     {"\033[r", "RSR"},		/* not an X.364 mnemonic */
468     {"\033[m", "SGR0"},		/* not an X.364 mnemonic */
469     {"\033[2J", "ED2"},		/* clear page */
470 
471     /* this group is specified by ISO 2022 */
472     {"\033(0", "ISO DEC G0"},	/* enable DEC graphics for G0 */
473     {"\033(A", "ISO UK G0"},	/* enable UK chars for G0 */
474     {"\033(B", "ISO US G0"},	/* enable US chars for G0 */
475     {"\033)0", "ISO DEC G1"},	/* enable DEC graphics for G1 */
476     {"\033)A", "ISO UK G1"},	/* enable UK chars for G1 */
477     {"\033)B", "ISO US G1"},	/* enable US chars for G1 */
478 
479     /* these are DEC private modes widely supported by emulators */
480     {"\033=", "DECPAM"},	/* application keypad mode */
481     {"\033>", "DECPNM"},	/* normal keypad mode */
482     {"\033<", "DECANSI"},	/* enter ANSI mode */
483 
484     {(char *) 0, (char *) 0}
485 };
486 
487 static const assoc private_modes[] =
488 /* DEC \E[ ... [hl] modes recognized by many emulators */
489 {
490     {"1", "CKM"},		/* application cursor keys */
491     {"2", "ANM"},		/* set VT52 mode */
492     {"3", "COLM"},		/* 132-column mode */
493     {"4", "SCLM"},		/* smooth scroll */
494     {"5", "SCNM"},		/* reverse video mode */
495     {"6", "OM"},		/* origin mode */
496     {"7", "AWM"},		/* wraparound mode */
497     {"8", "ARM"},		/* auto-repeat mode */
498     {(char *) 0, (char *) 0}
499 };
500 
501 static const assoc ecma_highlights[] =
502 /* recognize ECMA attribute sequences */
503 {
504     {"0", "NORMAL"},		/* normal */
505     {"1", "+BOLD"},		/* bold on */
506     {"2", "+DIM"},		/* dim on */
507     {"3", "+ITALIC"},		/* italic on */
508     {"4", "+UNDERLINE"},	/* underline on */
509     {"5", "+BLINK"},		/* blink on */
510     {"6", "+FASTBLINK"},	/* fastblink on */
511     {"7", "+REVERSE"},		/* reverse on */
512     {"8", "+INVISIBLE"},	/* invisible on */
513     {"9", "+DELETED"},		/* deleted on */
514     {"10", "MAIN-FONT"},	/* select primary font */
515     {"11", "ALT-FONT-1"},	/* select alternate font 1 */
516     {"12", "ALT-FONT-2"},	/* select alternate font 2 */
517     {"13", "ALT-FONT-3"},	/* select alternate font 3 */
518     {"14", "ALT-FONT-4"},	/* select alternate font 4 */
519     {"15", "ALT-FONT-5"},	/* select alternate font 5 */
520     {"16", "ALT-FONT-6"},	/* select alternate font 6 */
521     {"17", "ALT-FONT-7"},	/* select alternate font 7 */
522     {"18", "ALT-FONT-1"},	/* select alternate font 1 */
523     {"19", "ALT-FONT-1"},	/* select alternate font 1 */
524     {"20", "FRAKTUR"},		/* Fraktur font */
525     {"21", "DOUBLEUNDER"},	/* double underline */
526     {"22", "-DIM"},		/* dim off */
527     {"23", "-ITALIC"},		/* italic off */
528     {"24", "-UNDERLINE"},	/* underline off */
529     {"25", "-BLINK"},		/* blink off */
530     {"26", "-FASTBLINK"},	/* fastblink off */
531     {"27", "-REVERSE"},		/* reverse off */
532     {"28", "-INVISIBLE"},	/* invisible off */
533     {"29", "-DELETED"},		/* deleted off */
534     {(char *) 0, (char *) 0}
535 };
536 
537 static void
538 analyze_string(const char *name, const char *cap, TERMTYPE * tp)
539 {
540     char buf[MAX_TERMINFO_LENGTH];
541     char buf2[MAX_TERMINFO_LENGTH];
542     const char *sp, *ep;
543     const assoc *ap;
544 
545     if (cap == ABSENT_STRING || cap == CANCELLED_STRING)
546 	return;
547     (void) printf("%s: ", name);
548 
549     buf[0] = '\0';
550     for (sp = cap; *sp; sp++) {
551 	int i;
552 	size_t len = 0;
553 	const char *expansion = 0;
554 
555 	/* first, check other capabilities in this entry */
556 	for (i = 0; i < STRCOUNT; i++) {
557 	    char *cp = tp->Strings[i];
558 
559 	    /* don't use soft-key capabilities */
560 	    if (strnames[i][0] == 'k' && strnames[i][0] == 'f')
561 		continue;
562 
563 	    if (cp != ABSENT_STRING && cp != CANCELLED_STRING && cp[0] && cp
564 		!= cap) {
565 		len = strlen(cp);
566 		(void) strncpy(buf2, sp, len);
567 		buf2[len] = '\0';
568 
569 		if (_nc_capcmp(cp, buf2))
570 		    continue;
571 
572 #define ISRS(s)	(!strncmp((s), "is", 2) || !strncmp((s), "rs", 2))
573 		/*
574 		 * Theoretically we just passed the test for translation
575 		 * (equality once the padding is stripped).  However, there
576 		 * are a few more hoops that need to be jumped so that
577 		 * identical pairs of initialization and reset strings
578 		 * don't just refer to each other.
579 		 */
580 		if (ISRS(name) || ISRS(strnames[i]))
581 		    if (cap < cp)
582 			continue;
583 #undef ISRS
584 
585 		expansion = strnames[i];
586 		break;
587 	    }
588 	}
589 
590 	/* now check the standard capabilities */
591 	if (!expansion)
592 	    for (ap = std_caps; ap->from; ap++) {
593 		len = strlen(ap->from);
594 
595 		if (strncmp(ap->from, sp, len) == 0) {
596 		    expansion = ap->to;
597 		    break;
598 		}
599 	    }
600 
601 	/* now check for private-mode sequences */
602 	if (!expansion
603 	    && sp[0] == '\033' && sp[1] == '[' && sp[2] == '?'
604 	    && (len = strspn(sp + 3, "0123456789;"))
605 	    && ((sp[3 + len] == 'h') || (sp[3 + len] == 'l'))) {
606 	    char buf3[MAX_TERMINFO_LENGTH];
607 
608 	    (void) strlcpy(buf2, (sp[3 + len] == 'h') ? "DEC+" : "DEC-",
609 		sizeof buf2);
610 	    (void) strncpy(buf3, sp + 3, len);
611 	    len += 4;
612 	    buf3[len] = '\0';
613 
614 	    ep = strtok(buf3, ";");
615 	    do {
616 		bool found = FALSE;
617 
618 		for (ap = private_modes; ap->from; ap++) {
619 		    size_t tlen = strlen(ap->from);
620 
621 		    if (strncmp(ap->from, ep, tlen) == 0) {
622 			(void) strlcat(buf2, ap->to, sizeof buf2);
623 			found = TRUE;
624 			break;
625 		    }
626 		}
627 
628 		if (!found)
629 		    (void) strlcat(buf2, ep, sizeof buf2);
630 		(void) strlcat(buf2, ";", sizeof buf2);
631 	    } while
632 		((ep = strtok((char *) 0, ";")));
633 	    buf2[strlen(buf2) - 1] = '\0';
634 	    expansion = buf2;
635 	}
636 
637 	/* now check for ECMA highlight sequences */
638 	if (!expansion
639 	    && sp[0] == '\033' && sp[1] == '['
640 	    && (len = strspn(sp + 2, "0123456789;"))
641 	    && sp[2 + len] == 'm') {
642 	    char buf3[MAX_TERMINFO_LENGTH];
643 
644 	    (void) strlcpy(buf2, "SGR:", sizeof buf2);
645 	    (void) strncpy(buf3, sp + 2, len);
646 	    len += 3;
647 	    buf3[len] = '\0';
648 
649 	    ep = strtok(buf3, ";");
650 	    do {
651 		bool found = FALSE;
652 
653 		for (ap = ecma_highlights; ap->from; ap++) {
654 		    size_t tlen = strlen(ap->from);
655 
656 		    if (strncmp(ap->from, ep, tlen) == 0) {
657 			(void) strlcat(buf2, ap->to, sizeof buf2);
658 			found = TRUE;
659 			break;
660 		    }
661 		}
662 
663 		if (!found)
664 		    (void) strlcat(buf2, ep, sizeof buf2);
665 		(void) strlcat(buf2, ";", sizeof buf2);
666 	    } while
667 		((ep = strtok((char *) 0, ";")));
668 
669 	    buf2[strlen(buf2) - 1] = '\0';
670 	    expansion = buf2;
671 	}
672 	/* now check for scroll region reset */
673 	if (!expansion) {
674 	    (void) snprintf(buf2, sizeof buf2, "\033[1;%dr", tp->Numbers[2]);
675 	    len = strlen(buf2);
676 	    if (strncmp(buf2, sp, len) == 0)
677 		expansion = "RSR";
678 	}
679 
680 	/* now check for home-down */
681 	if (!expansion) {
682 	    (void) snprintf(buf2, sizeof buf2, "\033[%d;1H", tp->Numbers[2]);
683 	    len = strlen(buf2);
684 	    if (strncmp(buf2, sp, len) == 0)
685 		expansion = "LL";
686 	}
687 
688 	/* now look at the expansion we got, if any */
689 	if (expansion) {
690 	    (void) snprintf(buf + strlen(buf), sizeof buf - strlen(buf),
691 		"{%s}", expansion);
692 	    sp += len - 1;
693 	    continue;
694 	} else {
695 	    /* couldn't match anything */
696 	    buf2[0] = *sp;
697 	    buf2[1] = '\0';
698 	    (void) strlcat(buf, TIC_EXPAND(buf2), sizeof buf);
699 	}
700     }
701     (void) printf("%s\n", buf);
702 }
703 
704 /***************************************************************************
705  *
706  * File comparison
707  *
708  ***************************************************************************/
709 
710 static void
711 file_comparison(int argc, char *argv[])
712 {
713 #define MAXCOMPARE	2
714     /* someday we may allow comparisons on more files */
715     int filecount = 0;
716     ENTRY *heads[MAXCOMPARE];
717     ENTRY *qp, *rp;
718     int i, n;
719 
720     dump_init((char *) 0, F_LITERAL, S_TERMINFO, 0, itrace, FALSE);
721 
722     for (n = 0; n < argc && n < MAXCOMPARE; n++) {
723 	if (freopen(argv[n], "r", stdin) == 0)
724 	    _nc_err_abort("Can't open %s", argv[n]);
725 
726 	_nc_head = _nc_tail = 0;
727 
728 	/* parse entries out of the source file */
729 	_nc_set_source(argv[n]);
730 	_nc_read_entry_source(stdin, NULL, TRUE, FALSE, NULLHOOK);
731 
732 	if (itrace)
733 	    (void) fprintf(stderr, "Resolving file %d...\n", n - 0);
734 
735 	/* maybe do use resolution */
736 	if (!_nc_resolve_uses(!limited)) {
737 	    (void) fprintf(stderr,
738 			   "There are unresolved use entries in %s:\n",
739 			   argv[n]);
740 	    for_entry_list(qp) {
741 		if (qp->nuses) {
742 		    (void) fputs(qp->tterm.term_names, stderr);
743 		    (void) fputc('\n', stderr);
744 		}
745 	    }
746 	    exit(EXIT_FAILURE);
747 	}
748 
749 	heads[filecount] = _nc_head;
750 	filecount++;
751     }
752 
753     /* OK, all entries are in core.  Ready to do the comparison */
754     if (itrace)
755 	(void) fprintf(stderr, "Entries are now in core...\n");
756 
757     /* The entry-matching loop. Sigh, this is intrinsically quadratic. */
758     for (qp = heads[0]; qp; qp = qp->next) {
759 	for (rp = heads[1]; rp; rp = rp->next)
760 	    if (_nc_entry_match(qp->tterm.term_names, rp->tterm.term_names)) {
761 		if (qp->ncrosslinks < MAX_CROSSLINKS)
762 		    qp->crosslinks[qp->ncrosslinks] = rp;
763 		qp->ncrosslinks++;
764 
765 		if (rp->ncrosslinks < MAX_CROSSLINKS)
766 		    rp->crosslinks[rp->ncrosslinks] = qp;
767 		rp->ncrosslinks++;
768 	    }
769     }
770 
771     /* now we have two circular lists with crosslinks */
772     if (itrace)
773 	(void) fprintf(stderr, "Name matches are done...\n");
774 
775     for (qp = heads[0]; qp; qp = qp->next) {
776 	if (qp->ncrosslinks > 1) {
777 	    (void) fprintf(stderr,
778 			   "%s in file 1 (%s) has %d matches in file 2 (%s):\n",
779 			   _nc_first_name(qp->tterm.term_names),
780 			   argv[0],
781 			   qp->ncrosslinks,
782 			   argv[1]);
783 	    for (i = 0; i < qp->ncrosslinks; i++)
784 		(void) fprintf(stderr,
785 			       "\t%s\n",
786 			       _nc_first_name((qp->crosslinks[i])->tterm.term_names));
787 	}
788     }
789 
790     for (rp = heads[1]; rp; rp = rp->next) {
791 	if (rp->ncrosslinks > 1) {
792 	    (void) fprintf(stderr,
793 			   "%s in file 2 (%s) has %d matches in file 1 (%s):\n",
794 			   _nc_first_name(rp->tterm.term_names),
795 			   argv[1],
796 			   rp->ncrosslinks,
797 			   argv[0]);
798 	    for (i = 0; i < rp->ncrosslinks; i++)
799 		(void) fprintf(stderr,
800 			       "\t%s\n",
801 			       _nc_first_name((rp->crosslinks[i])->tterm.term_names));
802 	}
803     }
804 
805     (void) printf("In file 1 (%s) only:\n", argv[0]);
806     for (qp = heads[0]; qp; qp = qp->next)
807 	if (qp->ncrosslinks == 0)
808 	    (void) printf("\t%s\n",
809 			  _nc_first_name(qp->tterm.term_names));
810 
811     (void) printf("In file 2 (%s) only:\n", argv[1]);
812     for (rp = heads[1]; rp; rp = rp->next)
813 	if (rp->ncrosslinks == 0)
814 	    (void) printf("\t%s\n",
815 			  _nc_first_name(rp->tterm.term_names));
816 
817     (void) printf("The following entries are equivalent:\n");
818     for (qp = heads[0]; qp; qp = qp->next) {
819 	rp = qp->crosslinks[0];
820 
821 	if (qp->ncrosslinks == 1) {
822 	    rp = qp->crosslinks[0];
823 
824 	    repair_acsc(&qp->tterm);
825 	    repair_acsc(&rp->tterm);
826 #if NCURSES_XNAMES
827 	    _nc_align_termtype(&qp->tterm, &rp->tterm);
828 #endif
829 	    if (entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp)) {
830 		char name1[NAMESIZE], name2[NAMESIZE];
831 
832 		(void) canonical_name(qp->tterm.term_names, name1, sizeof name1);
833 		(void) canonical_name(rp->tterm.term_names, name2, sizeof name2);
834 
835 		(void) printf("%s = %s\n", name1, name2);
836 	    }
837 	}
838     }
839 
840     (void) printf("Differing entries:\n");
841     termcount = 2;
842     for (qp = heads[0]; qp; qp = qp->next) {
843 
844 	if (qp->ncrosslinks == 1) {
845 	    rp = qp->crosslinks[0];
846 #if NCURSES_XNAMES
847 	    /* sorry - we have to do this on each pass */
848 	    _nc_align_termtype(&qp->tterm, &rp->tterm);
849 #endif
850 	    if (!(entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp))) {
851 		char name1[NAMESIZE], name2[NAMESIZE];
852 
853 		entries[0] = *qp;
854 		entries[1] = *rp;
855 
856 		(void) canonical_name(qp->tterm.term_names, name1, sizeof name1);
857 		(void) canonical_name(rp->tterm.term_names, name2, sizeof name2);
858 
859 		switch (compare) {
860 		case C_DIFFERENCE:
861 		    if (itrace)
862 			(void) fprintf(stderr,
863 				       "infocmp: dumping differences\n");
864 		    (void) printf("comparing %s to %s.\n", name1, name2);
865 		    compare_entry(compare_predicate, &entries->tterm, quiet);
866 		    break;
867 
868 		case C_COMMON:
869 		    if (itrace)
870 			(void) fprintf(stderr,
871 				       "infocmp: dumping common capabilities\n");
872 		    (void) printf("comparing %s to %s.\n", name1, name2);
873 		    compare_entry(compare_predicate, &entries->tterm, quiet);
874 		    break;
875 
876 		case C_NAND:
877 		    if (itrace)
878 			(void) fprintf(stderr,
879 				       "infocmp: dumping differences\n");
880 		    (void) printf("comparing %s to %s.\n", name1, name2);
881 		    compare_entry(compare_predicate, &entries->tterm, quiet);
882 		    break;
883 
884 		}
885 	    }
886 	}
887     }
888 }
889 
890 static void
891 usage(void)
892 {
893     static const char *tbl[] =
894     {
895 	"Usage: infocmp [options] [-A directory] [-B directory] [termname...]"
896 	,""
897 	,"Options:"
898 	,"  -1    print single-column"
899 	,"  -C    use termcap-names"
900 	,"  -F    compare terminfo-files"
901 	,"  -I    use terminfo-names"
902 	,"  -L    use long names"
903 	,"  -R subset (see manpage)"
904 	,"  -T    eliminate size limits (test)"
905 	,"  -V    print version"
906 #if NCURSES_XNAMES
907 	,"  -a    with -F, list commented-out caps"
908 #endif
909 	,"  -c    list common capabilities"
910 	,"  -d    list different capabilities"
911 	,"  -e    format output for C initializer"
912 	,"  -E    format output as C tables"
913 	,"  -f    with -1, format complex strings"
914 	,"  -G    format %{number} to %'char'"
915 	,"  -g    format %'char' to %{number}"
916 	,"  -i    analyze initialization/reset"
917 	,"  -l    output terminfo names"
918 	,"  -n    list capabilities in neither"
919 	,"  -p    ignore padding specifiers"
920 	,"  -q    brief listing, removes headers"
921 	,"  -r    with -C, output in termcap form"
922 	,"  -r    with -F, resolve use-references"
923 	,"  -s [d|i|l|c] sort fields"
924 	,"  -u    produce source with 'use='"
925 	,"  -v number  (verbose)"
926 	,"  -w number  (width)"
927     };
928     const size_t first = 3;
929     const size_t last = SIZEOF(tbl);
930     const size_t left = (last - first + 1) / 2 + first;
931     size_t n;
932 
933     for (n = 0; n < left; n++) {
934 	size_t m = (n < first) ? last : n + left - first;
935 	if (m < last)
936 	    fprintf(stderr, "%-40.40s%s\n", tbl[n], tbl[m]);
937 	else
938 	    fprintf(stderr, "%s\n", tbl[n]);
939     }
940     exit(EXIT_FAILURE);
941 }
942 
943 static char *
944 name_initializer(const char *type)
945 {
946     static char *initializer;
947     static size_t len;
948     char *s;
949 
950     if (initializer == 0) {
951 	len = strlen(entries->tterm.term_names) + 20;
952 	initializer = (char *) malloc(len);
953     }
954 
955     (void) snprintf(initializer, len, "%s_data_%s", type, entries->tterm.term_names);
956     for (s = initializer; *s != 0 && *s != '|'; s++) {
957 	if (!isalnum(CharOf(*s)))
958 	    *s = '_';
959     }
960     *s = 0;
961     return initializer;
962 }
963 
964 /* dump C initializers for the terminal type */
965 static void
966 dump_initializers(TERMTYPE * term)
967 {
968     int n;
969     const char *str = 0;
970     int size;
971 
972     (void) printf("static char %s[] = %s\n", name_initializer("bool"), L_CURL);
973 
974     for_each_boolean(n, term) {
975 	switch ((int) (term->Booleans[n])) {
976 	case TRUE:
977 	    str = "TRUE";
978 	    break;
979 
980 	case FALSE:
981 	    str = "FALSE";
982 	    break;
983 
984 	case ABSENT_BOOLEAN:
985 	    str = "ABSENT_BOOLEAN";
986 	    break;
987 
988 	case CANCELLED_BOOLEAN:
989 	    str = "CANCELLED_BOOLEAN";
990 	    break;
991 	}
992 	(void) printf("\t/* %3d: %-8s */\t%s,\n",
993 		      n, ExtBoolname(term, n, boolnames), str);
994     }
995     (void) printf("%s;\n", R_CURL);
996 
997     (void) printf("static short %s[] = %s\n", name_initializer("number"), L_CURL);
998 
999     for_each_number(n, term) {
1000 	char buf[BUFSIZ];
1001 	switch (term->Numbers[n]) {
1002 	case ABSENT_NUMERIC:
1003 	    str = "ABSENT_NUMERIC";
1004 	    break;
1005 	case CANCELLED_NUMERIC:
1006 	    str = "CANCELLED_NUMERIC";
1007 	    break;
1008 	default:
1009 	    snprintf(buf, sizeof buf, "%d", term->Numbers[n]);
1010 	    str = buf;
1011 	    break;
1012 	}
1013 	(void) printf("\t/* %3d: %-8s */\t%s,\n", n,
1014 		      ExtNumname(term, n, numnames), str);
1015     }
1016     (void) printf("%s;\n", R_CURL);
1017 
1018     size = sizeof(TERMTYPE)
1019 	+ (NUM_BOOLEANS(term) * sizeof(term->Booleans[0]))
1020 	+ (NUM_NUMBERS(term) * sizeof(term->Numbers[0]));
1021 
1022     (void) printf("static char * %s[] = %s\n", name_initializer("string"), L_CURL);
1023 
1024     for_each_string(n, term) {
1025 	char buf[MAX_STRING], *sp, *tp;
1026 
1027 	if (term->Strings[n] == ABSENT_STRING)
1028 	    str = "ABSENT_STRING";
1029 	else if (term->Strings[n] == CANCELLED_STRING)
1030 	    str = "CANCELLED_STRING";
1031 	else {
1032 	    tp = buf;
1033 	    *tp++ = '"';
1034 	    for (sp = term->Strings[n];
1035 		 *sp != 0 && (tp - buf) < MAX_STRING - 6;
1036 		 sp++) {
1037 		if (isascii(CharOf(*sp))
1038 		    && isprint(CharOf(*sp))
1039 		    && *sp != '\\'
1040 		    && *sp != '"')
1041 		    *tp++ = *sp;
1042 		else {
1043 		    (void) snprintf(tp, buf + sizeof buf - tp, "\\%03o", CharOf(*sp));
1044 		    tp += 4;
1045 		}
1046 	    }
1047 	    *tp++ = '"';
1048 	    *tp = '\0';
1049 	    size += (strlen(term->Strings[n]) + 1);
1050 	    str = buf;
1051 	}
1052 #if NCURSES_XNAMES
1053 	if (n == STRCOUNT) {
1054 	    (void) printf("%s;\n", R_CURL);
1055 
1056 	    (void) printf("static char * %s[] = %s\n",
1057 			  name_initializer("string_ext"), L_CURL);
1058 	}
1059 #endif
1060 	(void) printf("\t/* %3d: %-8s */\t%s,\n", n,
1061 		      ExtStrname(term, n, strnames), str);
1062     }
1063     (void) printf("%s;\n", R_CURL);
1064 }
1065 
1066 /* dump C initializers for the terminal type */
1067 static void
1068 dump_termtype(TERMTYPE * term)
1069 {
1070     (void) printf("\t%s\n\t\t\"%s\",\n", L_CURL, term->term_names);
1071     (void) printf("\t\t(char *)0,\t/* pointer to string table */\n");
1072 
1073     (void) printf("\t\t%s,\n", name_initializer("bool"));
1074     (void) printf("\t\t%s,\n", name_initializer("number"));
1075 
1076     (void) printf("\t\t%s,\n", name_initializer("string"));
1077 
1078 #if NCURSES_XNAMES
1079     (void) printf("#if NCURSES_XNAMES\n");
1080     (void) printf("\t\t(char *)0,\t/* pointer to extended string table */\n");
1081     (void) printf("\t\t%s,\t/* ...corresponding names */\n",
1082 		  (NUM_STRINGS(term) != STRCOUNT)
1083 		  ? name_initializer("string_ext")
1084 		  : "(char **)0");
1085 
1086     (void) printf("\t\t%d,\t\t/* count total Booleans */\n", NUM_BOOLEANS(term));
1087     (void) printf("\t\t%d,\t\t/* count total Numbers */\n", NUM_NUMBERS(term));
1088     (void) printf("\t\t%d,\t\t/* count total Strings */\n", NUM_STRINGS(term));
1089 
1090     (void) printf("\t\t%d,\t\t/* count extensions to Booleans */\n",
1091 		  NUM_BOOLEANS(term) - BOOLCOUNT);
1092     (void) printf("\t\t%d,\t\t/* count extensions to Numbers */\n",
1093 		  NUM_NUMBERS(term) - NUMCOUNT);
1094     (void) printf("\t\t%d,\t\t/* count extensions to Strings */\n",
1095 		  NUM_STRINGS(term) - STRCOUNT);
1096 
1097     (void) printf("#endif /* NCURSES_XNAMES */\n");
1098 #endif /* NCURSES_XNAMES */
1099     (void) printf("\t%s\n", R_CURL);
1100 }
1101 
1102 static int
1103 optarg_to_number(void)
1104 {
1105     char *temp = 0;
1106     long value = strtol(optarg, &temp, 0);
1107 
1108     if (temp == 0 || temp == optarg || *temp != 0) {
1109 	fprintf(stderr, "Expected a number, not \"%s\"\n", optarg);
1110 	exit(EXIT_FAILURE);
1111     }
1112     return (int) value;
1113 }
1114 
1115 /***************************************************************************
1116  *
1117  * Main sequence
1118  *
1119  ***************************************************************************/
1120 
1121 int
1122 main(int argc, char *argv[])
1123 {
1124     char *terminal, *firstdir, *restdir;
1125     /* Avoid "local data >32k" error with mwcc */
1126     /* Also avoid overflowing smaller stacks on systems like AmigaOS */
1127     size_t tlen = sizeof(path) * MAXTERMS;
1128     path *tfile = (path *) malloc(tlen);
1129     int c, i, len;
1130     bool formatted = FALSE;
1131     bool filecompare = FALSE;
1132     int initdump = 0;
1133     bool init_analyze = FALSE;
1134 
1135     if ((terminal = getenv("TERM")) == 0) {
1136 	(void) fprintf(stderr,
1137 		       "infocmp: environment variable TERM not set\n");
1138 	return EXIT_FAILURE;
1139     }
1140 
1141     /* where is the terminfo database location going to default to? */
1142     restdir = firstdir = 0;
1143 
1144     while ((c = getopt(argc, argv, "adeEcCfFGgIinlLpqrR:s:uv:Vw:A:B:1T")) != -1)
1145 	switch (c) {
1146 #if NCURSES_XNAMES
1147 	case 'a':
1148 	    _nc_disable_period = TRUE;
1149 	    use_extended_names(TRUE);
1150 	    break;
1151 #endif
1152 	case 'd':
1153 	    compare = C_DIFFERENCE;
1154 	    break;
1155 
1156 	case 'e':
1157 	    initdump |= 1;
1158 	    break;
1159 
1160 	case 'E':
1161 	    initdump |= 2;
1162 	    break;
1163 
1164 	case 'c':
1165 	    compare = C_COMMON;
1166 	    break;
1167 
1168 	case 'C':
1169 	    outform = F_TERMCAP;
1170 	    tversion = "BSD";
1171 	    if (sortmode == S_DEFAULT)
1172 		sortmode = S_TERMCAP;
1173 	    break;
1174 
1175 	case 'f':
1176 	    formatted = TRUE;
1177 	    break;
1178 
1179 	case 'G':
1180 	    numbers = 1;
1181 	    break;
1182 
1183 	case 'g':
1184 	    numbers = -1;
1185 	    break;
1186 
1187 	case 'F':
1188 	    filecompare = TRUE;
1189 	    break;
1190 
1191 	case 'I':
1192 	    outform = F_TERMINFO;
1193 	    if (sortmode == S_DEFAULT)
1194 		sortmode = S_VARIABLE;
1195 	    tversion = 0;
1196 	    break;
1197 
1198 	case 'i':
1199 	    init_analyze = TRUE;
1200 	    break;
1201 
1202 	case 'l':
1203 	    outform = F_TERMINFO;
1204 	    break;
1205 
1206 	case 'L':
1207 	    outform = F_VARIABLE;
1208 	    if (sortmode == S_DEFAULT)
1209 		sortmode = S_VARIABLE;
1210 	    break;
1211 
1212 	case 'n':
1213 	    compare = C_NAND;
1214 	    break;
1215 
1216 	case 'p':
1217 	    ignorepads = TRUE;
1218 	    break;
1219 
1220 	case 'q':
1221 	    quiet = TRUE;
1222 	    s_absent = "-";
1223 	    s_cancel = "@";
1224 	    bool_sep = ", ";
1225 	    break;
1226 
1227 	case 'r':
1228 	    tversion = 0;
1229 	    limited = FALSE;
1230 	    break;
1231 
1232 	case 'R':
1233 	    tversion = optarg;
1234 	    break;
1235 
1236 	case 's':
1237 	    if (*optarg == 'd')
1238 		sortmode = S_NOSORT;
1239 	    else if (*optarg == 'i')
1240 		sortmode = S_TERMINFO;
1241 	    else if (*optarg == 'l')
1242 		sortmode = S_VARIABLE;
1243 	    else if (*optarg == 'c')
1244 		sortmode = S_TERMCAP;
1245 	    else {
1246 		(void) fprintf(stderr,
1247 			       "infocmp: unknown sort mode\n");
1248 		return EXIT_FAILURE;
1249 	    }
1250 	    break;
1251 
1252 	case 'u':
1253 	    compare = C_USEALL;
1254 	    break;
1255 
1256 	case 'v':
1257 	    itrace = optarg_to_number();
1258 	    set_trace_level(itrace);
1259 	    break;
1260 
1261 	case 'V':
1262 	    puts(curses_version());
1263 	    ExitProgram(EXIT_SUCCESS);
1264 
1265 	case 'w':
1266 	    mwidth = optarg_to_number();
1267 	    break;
1268 
1269 	case 'A':
1270 	    firstdir = optarg;
1271 	    break;
1272 
1273 	case 'B':
1274 	    restdir = optarg;
1275 	    break;
1276 
1277 	case '1':
1278 	    mwidth = 0;
1279 	    break;
1280 
1281 	case 'T':
1282 	    limited = FALSE;
1283 	    break;
1284 	default:
1285 	    usage();
1286 	}
1287 
1288     /* by default, sort by terminfo name */
1289     if (sortmode == S_DEFAULT)
1290 	sortmode = S_TERMINFO;
1291 
1292     /* set up for display */
1293     dump_init(tversion, outform, sortmode, mwidth, itrace, formatted);
1294 
1295     /* make sure we have at least one terminal name to work with */
1296     if (optind >= argc)
1297 	argv[argc++] = terminal;
1298 
1299     /* if user is after a comparison, make sure we have two entries */
1300     if (compare != C_DEFAULT && optind >= argc - 1)
1301 	argv[argc++] = terminal;
1302 
1303     /* exactly two terminal names with no options means do -d */
1304     if (argc - optind == 2 && compare == C_DEFAULT)
1305 	compare = C_DIFFERENCE;
1306 
1307     if (!filecompare) {
1308 	/* grab the entries */
1309 	termcount = 0;
1310 	for (; optind < argc; optind++) {
1311 	    if (termcount >= MAXTERMS) {
1312 		(void) fprintf(stderr,
1313 			       "infocmp: too many terminal type arguments\n");
1314 		return EXIT_FAILURE;
1315 	    } else {
1316 		const char *directory = termcount ? restdir : firstdir;
1317 		int status;
1318 
1319 		tname[termcount] = argv[optind];
1320 
1321 		if (directory) {
1322 		    (void) snprintf(tfile[termcount],
1323 				    tlen - termcount,
1324 				   "%s/%c/%s",
1325 				   directory,
1326 				   *argv[optind], argv[optind]);
1327 		    if (itrace)
1328 			(void) fprintf(stderr,
1329 				       "infocmp: reading entry %s from file %s\n",
1330 				       argv[optind], tfile[termcount]);
1331 
1332 		    status = _nc_read_file_entry(tfile[termcount],
1333 						 &entries[termcount].tterm);
1334 		} else {
1335 		    if (itrace)
1336 			(void) fprintf(stderr,
1337 				       "infocmp: reading entry %s from system directories %s\n",
1338 				       argv[optind], tname[termcount]);
1339 
1340 		    status = _nc_read_entry(tname[termcount],
1341 					    tfile[termcount],
1342 					    &entries[termcount].tterm);
1343 		    directory = TERMINFO;	/* for error message */
1344 		}
1345 
1346 		if (status <= 0) {
1347 		    (void) fprintf(stderr,
1348 				   "infocmp: couldn't open terminfo file %s.\n",
1349 				   tfile[termcount]);
1350 		    return EXIT_FAILURE;
1351 		}
1352 		repair_acsc(&entries[termcount].tterm);
1353 		termcount++;
1354 	    }
1355 	}
1356 
1357 #if NCURSES_XNAMES
1358 	if (termcount > 1)
1359 	    _nc_align_termtype(&entries[0].tterm, &entries[1].tterm);
1360 #endif
1361 
1362 	/* dump as C initializer for the terminal type */
1363 	if (initdump) {
1364 	    if (initdump & 1)
1365 		dump_termtype(&entries[0].tterm);
1366 	    if (initdump & 2)
1367 		dump_initializers(&entries[0].tterm);
1368 	    ExitProgram(EXIT_SUCCESS);
1369 	}
1370 
1371 	/* analyze the init strings */
1372 	if (init_analyze) {
1373 #undef CUR
1374 #define CUR	entries[0].tterm.
1375 	    analyze_string("is1", init_1string, &entries[0].tterm);
1376 	    analyze_string("is2", init_2string, &entries[0].tterm);
1377 	    analyze_string("is3", init_3string, &entries[0].tterm);
1378 	    analyze_string("rs1", reset_1string, &entries[0].tterm);
1379 	    analyze_string("rs2", reset_2string, &entries[0].tterm);
1380 	    analyze_string("rs3", reset_3string, &entries[0].tterm);
1381 	    analyze_string("smcup", enter_ca_mode, &entries[0].tterm);
1382 	    analyze_string("rmcup", exit_ca_mode, &entries[0].tterm);
1383 #undef CUR
1384 	    ExitProgram(EXIT_SUCCESS);
1385 	}
1386 
1387 	/*
1388 	 * Here's where the real work gets done
1389 	 */
1390 	switch (compare) {
1391 	case C_DEFAULT:
1392 	    if (itrace)
1393 		(void) fprintf(stderr,
1394 			       "infocmp: about to dump %s\n",
1395 			       tname[0]);
1396 	    (void) printf("#\tReconstructed via infocmp from file: %s\n",
1397 			  tfile[0]);
1398 	    len = dump_entry(&entries[0].tterm, limited, numbers, NULL);
1399 	    putchar('\n');
1400 	    if (itrace)
1401 		(void) fprintf(stderr, "infocmp: length %d\n", len);
1402 	    break;
1403 
1404 	case C_DIFFERENCE:
1405 	    if (itrace)
1406 		(void) fprintf(stderr, "infocmp: dumping differences\n");
1407 	    (void) printf("comparing %s to %s.\n", tname[0], tname[1]);
1408 	    compare_entry(compare_predicate, &entries->tterm, quiet);
1409 	    break;
1410 
1411 	case C_COMMON:
1412 	    if (itrace)
1413 		(void) fprintf(stderr,
1414 			       "infocmp: dumping common capabilities\n");
1415 	    (void) printf("comparing %s to %s.\n", tname[0], tname[1]);
1416 	    compare_entry(compare_predicate, &entries->tterm, quiet);
1417 	    break;
1418 
1419 	case C_NAND:
1420 	    if (itrace)
1421 		(void) fprintf(stderr,
1422 			       "infocmp: dumping differences\n");
1423 	    (void) printf("comparing %s to %s.\n", tname[0], tname[1]);
1424 	    compare_entry(compare_predicate, &entries->tterm, quiet);
1425 	    break;
1426 
1427 	case C_USEALL:
1428 	    if (itrace)
1429 		(void) fprintf(stderr, "infocmp: dumping use entry\n");
1430 	    len = dump_entry(&entries[0].tterm, limited, numbers, use_predicate);
1431 	    for (i = 1; i < termcount; i++)
1432 		len += dump_uses(tname[i], !(outform == F_TERMCAP || outform
1433 					     == F_TCONVERR));
1434 	    putchar('\n');
1435 	    if (itrace)
1436 		(void) fprintf(stderr, "infocmp: length %d\n", len);
1437 	    break;
1438 	}
1439     } else if (compare == C_USEALL)
1440 	(void) fprintf(stderr, "Sorry, -u doesn't work with -F\n");
1441     else if (compare == C_DEFAULT)
1442 	(void) fprintf(stderr, "Use `tic -[CI] <file>' for this.\n");
1443     else if (argc - optind != 2)
1444 	(void) fprintf(stderr,
1445 		       "File comparison needs exactly two file arguments.\n");
1446     else
1447 	file_comparison(argc - optind, argv + optind);
1448 
1449     ExitProgram(EXIT_SUCCESS);
1450 }
1451 
1452 /* infocmp.c ends here */
1453