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