Add ncurses 5.4 source code.
[dragonfly.git] / contrib / ncurses-5.4 / progs / infocmp.c
1 /****************************************************************************
2  * Copyright (c) 1998-2002,2003 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  ****************************************************************************/
33
34 /*
35  *      infocmp.c -- decompile an entry, or compare two entries
36  *              written by Eric S. Raymond
37  */
38
39 #include <progs.priv.h>
40
41 #include <term_entry.h>
42 #include <dump_entry.h>
43
44 MODULE_ID("$Id: infocmp.c,v 1.71 2003/10/18 18:01:54 tom Exp $")
45
46 #define L_CURL "{"
47 #define R_CURL "}"
48
49 #define MAXTERMS        32      /* max # terminal arguments we can handle */
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 char *tname[MAXTERMS];   /* terminal type names */
64 static ENTRY entries[MAXTERMS]; /* terminfo entries */
65 static int termcount;           /* count of terminal entries */
66
67 static bool limited = TRUE;     /* "-r" option is not set */
68 static bool quiet = 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
91 ExitProgram(int code) GCC_NORETURN;
92 /* prototype is to get gcc to accept the noreturn attribute */
93      static void
94        ExitProgram(int code)
95 {
96     while (termcount-- > 0)
97         _nc_free_termtype(&entries[termcount].tterm);
98     _nc_leaks_dump_entry();
99     _nc_free_and_exit(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(unsigned 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(int type, int 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(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(int type, int 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 modes widely supported by emulators */
478     {"\033=", "DECPAM"},        /* application keypad mode */
479     {"\033>", "DECPNM"},        /* normal keypad mode */
480     {"\033<", "DECANSI"},       /* enter ANSI mode */
481
482     {(char *) 0, (char *) 0}
483 };
484
485 static const assoc private_modes[] =
486 /* DEC \E[ ... [hl] modes recognized by many emulators */
487 {
488     {"1", "CKM"},               /* application cursor keys */
489     {"2", "ANM"},               /* set VT52 mode */
490     {"3", "COLM"},              /* 132-column mode */
491     {"4", "SCLM"},              /* smooth scroll */
492     {"5", "SCNM"},              /* reverse video mode */
493     {"6", "OM"},                /* origin mode */
494     {"7", "AWM"},               /* wraparound mode */
495     {"8", "ARM"},               /* auto-repeat mode */
496     {(char *) 0, (char *) 0}
497 };
498
499 static const assoc ecma_highlights[] =
500 /* recognize ECMA attribute sequences */
501 {
502     {"0", "NORMAL"},            /* normal */
503     {"1", "+BOLD"},             /* bold on */
504     {"2", "+DIM"},              /* dim on */
505     {"3", "+ITALIC"},           /* italic on */
506     {"4", "+UNDERLINE"},        /* underline on */
507     {"5", "+BLINK"},            /* blink on */
508     {"6", "+FASTBLINK"},        /* fastblink on */
509     {"7", "+REVERSE"},          /* reverse on */
510     {"8", "+INVISIBLE"},        /* invisible on */
511     {"9", "+DELETED"},          /* deleted on */
512     {"10", "MAIN-FONT"},        /* select primary font */
513     {"11", "ALT-FONT-1"},       /* select alternate font 1 */
514     {"12", "ALT-FONT-2"},       /* select alternate font 2 */
515     {"13", "ALT-FONT-3"},       /* select alternate font 3 */
516     {"14", "ALT-FONT-4"},       /* select alternate font 4 */
517     {"15", "ALT-FONT-5"},       /* select alternate font 5 */
518     {"16", "ALT-FONT-6"},       /* select alternate font 6 */
519     {"17", "ALT-FONT-7"},       /* select alternate font 7 */
520     {"18", "ALT-FONT-1"},       /* select alternate font 1 */
521     {"19", "ALT-FONT-1"},       /* select alternate font 1 */
522     {"20", "FRAKTUR"},          /* Fraktur font */
523     {"21", "DOUBLEUNDER"},      /* double underline */
524     {"22", "-DIM"},             /* dim off */
525     {"23", "-ITALIC"},          /* italic off */
526     {"24", "-UNDERLINE"},       /* underline off */
527     {"25", "-BLINK"},           /* blink off */
528     {"26", "-FASTBLINK"},       /* fastblink off */
529     {"27", "-REVERSE"},         /* reverse off */
530     {"28", "-INVISIBLE"},       /* invisible off */
531     {"29", "-DELETED"},         /* deleted off */
532     {(char *) 0, (char *) 0}
533 };
534
535 static void
536 analyze_string(const char *name, const char *cap, TERMTYPE * tp)
537 {
538     char buf[MAX_TERMINFO_LENGTH];
539     char buf2[MAX_TERMINFO_LENGTH];
540     const char *sp, *ep;
541     const assoc *ap;
542
543     if (cap == ABSENT_STRING || cap == CANCELLED_STRING)
544         return;
545     (void) printf("%s: ", name);
546
547     buf[0] = '\0';
548     for (sp = cap; *sp; sp++) {
549         int i;
550         size_t len = 0;
551         const char *expansion = 0;
552
553         /* first, check other capabilities in this entry */
554         for (i = 0; i < STRCOUNT; i++) {
555             char *cp = tp->Strings[i];
556
557             /* don't use soft-key capabilities */
558             if (strnames[i][0] == 'k' && strnames[i][0] == 'f')
559                 continue;
560
561             if (cp != ABSENT_STRING && cp != CANCELLED_STRING && cp[0] && cp
562                 != cap) {
563                 len = strlen(cp);
564                 (void) strncpy(buf2, sp, len);
565                 buf2[len] = '\0';
566
567                 if (_nc_capcmp(cp, buf2))
568                     continue;
569
570 #define ISRS(s) (!strncmp((s), "is", 2) || !strncmp((s), "rs", 2))
571                 /*
572                  * Theoretically we just passed the test for translation
573                  * (equality once the padding is stripped).  However, there
574                  * are a few more hoops that need to be jumped so that
575                  * identical pairs of initialization and reset strings
576                  * don't just refer to each other.
577                  */
578                 if (ISRS(name) || ISRS(strnames[i]))
579                     if (cap < cp)
580                         continue;
581 #undef ISRS
582
583                 expansion = strnames[i];
584                 break;
585             }
586         }
587
588         /* now check the standard capabilities */
589         if (!expansion)
590             for (ap = std_caps; ap->from; ap++) {
591                 len = strlen(ap->from);
592
593                 if (strncmp(ap->from, sp, len) == 0) {
594                     expansion = ap->to;
595                     break;
596                 }
597             }
598
599         /* now check for private-mode sequences */
600         if (!expansion
601             && sp[0] == '\033' && sp[1] == '[' && sp[2] == '?'
602             && (len = strspn(sp + 3, "0123456789;"))
603             && ((sp[3 + len] == 'h') || (sp[3 + len] == 'l'))) {
604             char buf3[MAX_TERMINFO_LENGTH];
605
606             (void) strcpy(buf2, (sp[3 + len] == 'h') ? "DEC+" : "DEC-");
607             (void) strncpy(buf3, sp + 3, len);
608             len += 4;
609             buf3[len] = '\0';
610
611             ep = strtok(buf3, ";");
612             do {
613                 bool found = FALSE;
614
615                 for (ap = private_modes; ap->from; ap++) {
616                     size_t tlen = strlen(ap->from);
617
618                     if (strncmp(ap->from, ep, tlen) == 0) {
619                         (void) strcat(buf2, ap->to);
620                         found = TRUE;
621                         break;
622                     }
623                 }
624
625                 if (!found)
626                     (void) strcat(buf2, ep);
627                 (void) strcat(buf2, ";");
628             } while
629                 ((ep = strtok((char *) 0, ";")));
630             buf2[strlen(buf2) - 1] = '\0';
631             expansion = buf2;
632         }
633
634         /* now check for ECMA highlight sequences */
635         if (!expansion
636             && sp[0] == '\033' && sp[1] == '['
637             && (len = strspn(sp + 2, "0123456789;"))
638             && sp[2 + len] == 'm') {
639             char buf3[MAX_TERMINFO_LENGTH];
640
641             (void) strcpy(buf2, "SGR:");
642             (void) strncpy(buf3, sp + 2, len);
643             len += 3;
644             buf3[len] = '\0';
645
646             ep = strtok(buf3, ";");
647             do {
648                 bool found = FALSE;
649
650                 for (ap = ecma_highlights; ap->from; ap++) {
651                     size_t tlen = strlen(ap->from);
652
653                     if (strncmp(ap->from, ep, tlen) == 0) {
654                         (void) strcat(buf2, ap->to);
655                         found = TRUE;
656                         break;
657                     }
658                 }
659
660                 if (!found)
661                     (void) strcat(buf2, ep);
662                 (void) strcat(buf2, ";");
663             } while
664                 ((ep = strtok((char *) 0, ";")));
665
666             buf2[strlen(buf2) - 1] = '\0';
667             expansion = buf2;
668         }
669         /* now check for scroll region reset */
670         if (!expansion) {
671             (void) sprintf(buf2, "\033[1;%dr", tp->Numbers[2]);
672             len = strlen(buf2);
673             if (strncmp(buf2, sp, len) == 0)
674                 expansion = "RSR";
675         }
676
677         /* now check for home-down */
678         if (!expansion) {
679             (void) sprintf(buf2, "\033[%d;1H", tp->Numbers[2]);
680             len = strlen(buf2);
681             if (strncmp(buf2, sp, len) == 0)
682                 expansion = "LL";
683         }
684
685         /* now look at the expansion we got, if any */
686         if (expansion) {
687             (void) sprintf(buf + strlen(buf), "{%s}", expansion);
688             sp += len - 1;
689             continue;
690         } else {
691             /* couldn't match anything */
692             buf2[0] = *sp;
693             buf2[1] = '\0';
694             (void) strcat(buf, TIC_EXPAND(buf2));
695         }
696     }
697     (void) printf("%s\n", buf);
698 }
699
700 /***************************************************************************
701  *
702  * File comparison
703  *
704  ***************************************************************************/
705
706 static void
707 file_comparison(int argc, char *argv[])
708 {
709 #define MAXCOMPARE      2
710     /* someday we may allow comparisons on more files */
711     int filecount = 0;
712     ENTRY *heads[MAXCOMPARE];
713     ENTRY *qp, *rp;
714     int i, n;
715
716     dump_init((char *) 0, F_LITERAL, S_TERMINFO, 0, itrace, FALSE);
717
718     for (n = 0; n < argc && n < MAXCOMPARE; n++) {
719         if (freopen(argv[n], "r", stdin) == 0)
720             _nc_err_abort("Can't open %s", argv[n]);
721
722         _nc_head = _nc_tail = 0;
723
724         /* parse entries out of the source file */
725         _nc_set_source(argv[n]);
726         _nc_read_entry_source(stdin, NULL, TRUE, FALSE, NULLHOOK);
727
728         if (itrace)
729             (void) fprintf(stderr, "Resolving file %d...\n", n - 0);
730
731         /* maybe do use resolution */
732         if (!_nc_resolve_uses(!limited)) {
733             (void) fprintf(stderr,
734                            "There are unresolved use entries in %s:\n",
735                            argv[n]);
736             for_entry_list(qp) {
737                 if (qp->nuses) {
738                     (void) fputs(qp->tterm.term_names, stderr);
739                     (void) fputc('\n', stderr);
740                 }
741             }
742             ExitProgram(EXIT_FAILURE);
743         }
744
745         heads[filecount] = _nc_head;
746         filecount++;
747     }
748
749     /* OK, all entries are in core.  Ready to do the comparison */
750     if (itrace)
751         (void) fprintf(stderr, "Entries are now in core...\n");
752
753     /* The entry-matching loop. Sigh, this is intrinsically quadratic. */
754     for (qp = heads[0]; qp; qp = qp->next) {
755         for (rp = heads[1]; rp; rp = rp->next)
756             if (_nc_entry_match(qp->tterm.term_names, rp->tterm.term_names)) {
757                 if (qp->ncrosslinks < MAX_CROSSLINKS)
758                     qp->crosslinks[qp->ncrosslinks] = rp;
759                 qp->ncrosslinks++;
760
761                 if (rp->ncrosslinks < MAX_CROSSLINKS)
762                     rp->crosslinks[rp->ncrosslinks] = qp;
763                 rp->ncrosslinks++;
764             }
765     }
766
767     /* now we have two circular lists with crosslinks */
768     if (itrace)
769         (void) fprintf(stderr, "Name matches are done...\n");
770
771     for (qp = heads[0]; qp; qp = qp->next) {
772         if (qp->ncrosslinks > 1) {
773             (void) fprintf(stderr,
774                            "%s in file 1 (%s) has %d matches in file 2 (%s):\n",
775                            _nc_first_name(qp->tterm.term_names),
776                            argv[0],
777                            qp->ncrosslinks,
778                            argv[1]);
779             for (i = 0; i < qp->ncrosslinks; i++)
780                 (void) fprintf(stderr,
781                                "\t%s\n",
782                                _nc_first_name((qp->crosslinks[i])->tterm.term_names));
783         }
784     }
785
786     for (rp = heads[1]; rp; rp = rp->next) {
787         if (rp->ncrosslinks > 1) {
788             (void) fprintf(stderr,
789                            "%s in file 2 (%s) has %d matches in file 1 (%s):\n",
790                            _nc_first_name(rp->tterm.term_names),
791                            argv[1],
792                            rp->ncrosslinks,
793                            argv[0]);
794             for (i = 0; i < rp->ncrosslinks; i++)
795                 (void) fprintf(stderr,
796                                "\t%s\n",
797                                _nc_first_name((rp->crosslinks[i])->tterm.term_names));
798         }
799     }
800
801     (void) printf("In file 1 (%s) only:\n", argv[0]);
802     for (qp = heads[0]; qp; qp = qp->next)
803         if (qp->ncrosslinks == 0)
804             (void) printf("\t%s\n",
805                           _nc_first_name(qp->tterm.term_names));
806
807     (void) printf("In file 2 (%s) only:\n", argv[1]);
808     for (rp = heads[1]; rp; rp = rp->next)
809         if (rp->ncrosslinks == 0)
810             (void) printf("\t%s\n",
811                           _nc_first_name(rp->tterm.term_names));
812
813     (void) printf("The following entries are equivalent:\n");
814     for (qp = heads[0]; qp; qp = qp->next) {
815         rp = qp->crosslinks[0];
816
817         if (qp->ncrosslinks == 1) {
818             rp = qp->crosslinks[0];
819
820             repair_acsc(&qp->tterm);
821             repair_acsc(&rp->tterm);
822 #if NCURSES_XNAMES
823             _nc_align_termtype(&qp->tterm, &rp->tterm);
824 #endif
825             if (entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp)) {
826                 char name1[NAMESIZE], name2[NAMESIZE];
827
828                 (void) canonical_name(qp->tterm.term_names, name1);
829                 (void) canonical_name(rp->tterm.term_names, name2);
830
831                 (void) printf("%s = %s\n", name1, name2);
832             }
833         }
834     }
835
836     (void) printf("Differing entries:\n");
837     termcount = 2;
838     for (qp = heads[0]; qp; qp = qp->next) {
839
840         if (qp->ncrosslinks == 1) {
841             rp = qp->crosslinks[0];
842 #if NCURSES_XNAMES
843             /* sorry - we have to do this on each pass */
844             _nc_align_termtype(&qp->tterm, &rp->tterm);
845 #endif
846             if (!(entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp))) {
847                 char name1[NAMESIZE], name2[NAMESIZE];
848
849                 entries[0] = *qp;
850                 entries[1] = *rp;
851
852                 (void) canonical_name(qp->tterm.term_names, name1);
853                 (void) canonical_name(rp->tterm.term_names, name2);
854
855                 switch (compare) {
856                 case C_DIFFERENCE:
857                     if (itrace)
858                         (void) fprintf(stderr,
859                                        "infocmp: dumping differences\n");
860                     (void) printf("comparing %s to %s.\n", name1, name2);
861                     compare_entry(compare_predicate, &entries->tterm, quiet);
862                     break;
863
864                 case C_COMMON:
865                     if (itrace)
866                         (void) fprintf(stderr,
867                                        "infocmp: dumping common capabilities\n");
868                     (void) printf("comparing %s to %s.\n", name1, name2);
869                     compare_entry(compare_predicate, &entries->tterm, quiet);
870                     break;
871
872                 case C_NAND:
873                     if (itrace)
874                         (void) fprintf(stderr,
875                                        "infocmp: dumping differences\n");
876                     (void) printf("comparing %s to %s.\n", name1, name2);
877                     compare_entry(compare_predicate, &entries->tterm, quiet);
878                     break;
879
880                 }
881             }
882         }
883     }
884 }
885
886 static void
887 usage(void)
888 {
889     static const char *tbl[] =
890     {
891         "Usage: infocmp [options] [-A directory] [-B directory] [termname...]"
892         ,""
893         ,"Options:"
894         ,"  -1    print single-column"
895         ,"  -C    use termcap-names"
896         ,"  -F    compare terminfo-files"
897         ,"  -I    use terminfo-names"
898         ,"  -L    use long names"
899         ,"  -R subset (see manpage)"
900         ,"  -T    eliminate size limits (test)"
901         ,"  -V    print version"
902 #if NCURSES_XNAMES
903         ,"  -a    with -F, list commented-out caps"
904 #endif
905         ,"  -c    list common capabilities"
906         ,"  -d    list different capabilities"
907         ,"  -e    format output for C initializer"
908         ,"  -E    format output as C tables"
909         ,"  -f    with -1, format complex strings"
910         ,"  -G    format %{number} to %'char'"
911         ,"  -g    format %'char' to %{number}"
912         ,"  -i    analyze initialization/reset"
913         ,"  -l    output terminfo names"
914         ,"  -n    list capabilities in neither"
915         ,"  -p    ignore padding specifiers"
916         ,"  -q    brief listing, removes headers"
917         ,"  -r    with -C, output in termcap form"
918         ,"  -r    with -F, resolve use-references"
919         ,"  -s [d|i|l|c] sort fields"
920 #if NCURSES_XNAMES
921         ,"  -t    suppress commented-out capabilities"
922 #endif
923         ,"  -u    produce source with 'use='"
924         ,"  -v number  (verbose)"
925         ,"  -w number  (width)"
926     };
927     const size_t first = 3;
928     const size_t last = SIZEOF(tbl);
929     const size_t left = (last - first + 1) / 2 + first;
930     size_t n;
931
932     for (n = 0; n < left; n++) {
933         size_t m = (n < first) ? last : n + left - first;
934         if (m < last)
935             fprintf(stderr, "%-40.40s%s\n", tbl[n], tbl[m]);
936         else
937             fprintf(stderr, "%s\n", tbl[n]);
938     }
939     ExitProgram(EXIT_FAILURE);
940 }
941
942 static char *
943 any_initializer(const char *fmt, const char *type)
944 {
945     static char *initializer;
946     char *s;
947
948     if (initializer == 0)
949         initializer = (char *) malloc(strlen(entries->tterm.term_names) +
950                                       strlen(type) + strlen(fmt));
951
952     (void) strcpy(initializer, entries->tterm.term_names);
953     for (s = initializer; *s != 0 && *s != '|'; s++) {
954         if (!isalnum(UChar(*s)))
955             *s = '_';
956     }
957     *s = 0;
958     (void) sprintf(s, fmt, type);
959     return initializer;
960 }
961
962 static char *
963 name_initializer(const char *type)
964 {
965     return any_initializer("_%s_data", type);
966 }
967
968 static char *
969 string_variable(const char *type)
970 {
971     return any_initializer("_s_%s", type);
972 }
973
974 /* dump C initializers for the terminal type */
975 static void
976 dump_initializers(TERMTYPE * term)
977 {
978     unsigned n;
979     int size;
980     const char *str = 0;
981
982     printf("\nstatic char %s[] = \"%s\";\n\n",
983            name_initializer("alias"), entries->tterm.term_names);
984
985     for_each_string(n, term) {
986         char buf[MAX_STRING], *sp, *tp;
987
988         if (VALID_STRING(term->Strings[n])) {
989             tp = buf;
990             *tp++ = '"';
991             for (sp = term->Strings[n];
992                  *sp != 0 && (tp - buf) < MAX_STRING - 6;
993                  sp++) {
994                 if (isascii(UChar(*sp))
995                     && isprint(UChar(*sp))
996                     && *sp != '\\'
997                     && *sp != '"')
998                     *tp++ = *sp;
999                 else {
1000                     (void) sprintf(tp, "\\%03o", UChar(*sp));
1001                     tp += 4;
1002                 }
1003             }
1004             *tp++ = '"';
1005             *tp = '\0';
1006             size += (strlen(term->Strings[n]) + 1);
1007             (void) printf("static char %-20s[] = %s;\n",
1008                           string_variable(ExtStrname(term, n, strnames)), buf);
1009         }
1010     }
1011     printf("\n");
1012
1013     (void) printf("static char %s[] = %s\n", name_initializer("bool"), L_CURL);
1014
1015     for_each_boolean(n, term) {
1016         switch ((int) (term->Booleans[n])) {
1017         case TRUE:
1018             str = "TRUE";
1019             break;
1020
1021         case FALSE:
1022             str = "FALSE";
1023             break;
1024
1025         case ABSENT_BOOLEAN:
1026             str = "ABSENT_BOOLEAN";
1027             break;
1028
1029         case CANCELLED_BOOLEAN:
1030             str = "CANCELLED_BOOLEAN";
1031             break;
1032         }
1033         (void) printf("\t/* %3d: %-8s */\t%s,\n",
1034                       n, ExtBoolname(term, n, boolnames), str);
1035     }
1036     (void) printf("%s;\n", R_CURL);
1037
1038     (void) printf("static short %s[] = %s\n", name_initializer("number"), L_CURL);
1039
1040     for_each_number(n, term) {
1041         char buf[BUFSIZ];
1042         switch (term->Numbers[n]) {
1043         case ABSENT_NUMERIC:
1044             str = "ABSENT_NUMERIC";
1045             break;
1046         case CANCELLED_NUMERIC:
1047             str = "CANCELLED_NUMERIC";
1048             break;
1049         default:
1050             sprintf(buf, "%d", term->Numbers[n]);
1051             str = buf;
1052             break;
1053         }
1054         (void) printf("\t/* %3d: %-8s */\t%s,\n", n,
1055                       ExtNumname(term, n, numnames), str);
1056     }
1057     (void) printf("%s;\n", R_CURL);
1058
1059     size = sizeof(TERMTYPE)
1060         + (NUM_BOOLEANS(term) * sizeof(term->Booleans[0]))
1061         + (NUM_NUMBERS(term) * sizeof(term->Numbers[0]));
1062
1063     (void) printf("static char * %s[] = %s\n", name_initializer("string"), L_CURL);
1064
1065     for_each_string(n, term) {
1066
1067         if (term->Strings[n] == ABSENT_STRING)
1068             str = "ABSENT_STRING";
1069         else if (term->Strings[n] == CANCELLED_STRING)
1070             str = "CANCELLED_STRING";
1071         else {
1072             str = string_variable(ExtStrname(term, n, strnames));
1073         }
1074         (void) printf("\t/* %3d: %-8s */\t%s,\n", n,
1075                       ExtStrname(term, n, strnames), str);
1076     }
1077     (void) printf("%s;\n", R_CURL);
1078
1079 #if NCURSES_XNAMES
1080     if ((NUM_BOOLEANS(term) != BOOLCOUNT)
1081         || (NUM_NUMBERS(term) != NUMCOUNT)
1082         || (NUM_STRINGS(term) != STRCOUNT)) {
1083         (void) printf("static char * %s[] = %s\n",
1084                       name_initializer("string_ext"), L_CURL);
1085         for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
1086             (void) printf("\t/* %3d: bool */\t\"%s\",\n",
1087                           n, ExtBoolname(term, n, boolnames));
1088         }
1089         for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
1090             (void) printf("\t/* %3d: num */\t\"%s\",\n",
1091                           n, ExtNumname(term, n, numnames));
1092         }
1093         for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
1094             (void) printf("\t/* %3d: str */\t\"%s\",\n",
1095                           n, ExtStrname(term, n, strnames));
1096         }
1097         (void) printf("%s;\n", R_CURL);
1098     }
1099 #endif
1100 }
1101
1102 /* dump C initializers for the terminal type */
1103 static void
1104 dump_termtype(TERMTYPE * term)
1105 {
1106     (void) printf("\t%s\n\t\t%s,\n", L_CURL, name_initializer("alias"));
1107     (void) printf("\t\t(char *)0,\t/* pointer to string table */\n");
1108
1109     (void) printf("\t\t%s,\n", name_initializer("bool"));
1110     (void) printf("\t\t%s,\n", name_initializer("number"));
1111
1112     (void) printf("\t\t%s,\n", name_initializer("string"));
1113
1114 #if NCURSES_XNAMES
1115     (void) printf("#if NCURSES_XNAMES\n");
1116     (void) printf("\t\t(char *)0,\t/* pointer to extended string table */\n");
1117     (void) printf("\t\t%s,\t/* ...corresponding names */\n",
1118                   ((NUM_BOOLEANS(term) != BOOLCOUNT)
1119                    || (NUM_NUMBERS(term) != NUMCOUNT)
1120                    || (NUM_STRINGS(term) != STRCOUNT))
1121                   ? name_initializer("string_ext")
1122                   : "(char **)0");
1123
1124     (void) printf("\t\t%d,\t\t/* count total Booleans */\n", NUM_BOOLEANS(term));
1125     (void) printf("\t\t%d,\t\t/* count total Numbers */\n", NUM_NUMBERS(term));
1126     (void) printf("\t\t%d,\t\t/* count total Strings */\n", NUM_STRINGS(term));
1127
1128     (void) printf("\t\t%d,\t\t/* count extensions to Booleans */\n",
1129                   NUM_BOOLEANS(term) - BOOLCOUNT);
1130     (void) printf("\t\t%d,\t\t/* count extensions to Numbers */\n",
1131                   NUM_NUMBERS(term) - NUMCOUNT);
1132     (void) printf("\t\t%d,\t\t/* count extensions to Strings */\n",
1133                   NUM_STRINGS(term) - STRCOUNT);
1134
1135     (void) printf("#endif /* NCURSES_XNAMES */\n");
1136 #endif /* NCURSES_XNAMES */
1137     (void) printf("\t%s\n", R_CURL);
1138 }
1139
1140 static int
1141 optarg_to_number(void)
1142 {
1143     char *temp = 0;
1144     long value = strtol(optarg, &temp, 0);
1145
1146     if (temp == 0 || temp == optarg || *temp != 0) {
1147         fprintf(stderr, "Expected a number, not \"%s\"\n", optarg);
1148         ExitProgram(EXIT_FAILURE);
1149     }
1150     return (int) value;
1151 }
1152
1153 static char *
1154 terminal_env(void)
1155 {
1156     char *terminal;
1157
1158     if ((terminal = getenv("TERM")) == 0) {
1159         (void) fprintf(stderr,
1160                        "infocmp: environment variable TERM not set\n");
1161         exit(EXIT_FAILURE);
1162     }
1163     return terminal;
1164 }
1165
1166 /***************************************************************************
1167  *
1168  * Main sequence
1169  *
1170  ***************************************************************************/
1171
1172 int
1173 main(int argc, char *argv[])
1174 {
1175     char *firstdir, *restdir;
1176     /* Avoid "local data >32k" error with mwcc */
1177     /* Also avoid overflowing smaller stacks on systems like AmigaOS */
1178     path *tfile = (path *) malloc(sizeof(path) * MAXTERMS);
1179     int c, i, len;
1180     bool formatted = FALSE;
1181     bool filecompare = FALSE;
1182     int initdump = 0;
1183     bool init_analyze = FALSE;
1184     bool suppress_untranslatable = FALSE;
1185
1186     /* where is the terminfo database location going to default to? */
1187     restdir = firstdir = 0;
1188
1189     while ((c = getopt(argc,
1190                        argv,
1191                        "1A:aB:CcdEeFfGgIiLlnpqR:rs:TtuVv:w:")) != EOF)
1192         switch (c) {
1193         case '1':
1194             mwidth = 0;
1195             break;
1196
1197         case 'A':
1198             firstdir = optarg;
1199             break;
1200
1201 #if NCURSES_XNAMES
1202         case 'a':
1203             _nc_disable_period = TRUE;
1204             use_extended_names(TRUE);
1205             break;
1206 #endif
1207         case 'B':
1208             restdir = optarg;
1209             break;
1210
1211         case 'C':
1212             outform = F_TERMCAP;
1213             tversion = "BSD";
1214             if (sortmode == S_DEFAULT)
1215                 sortmode = S_TERMCAP;
1216             break;
1217
1218         case 'c':
1219             compare = C_COMMON;
1220             break;
1221
1222         case 'd':
1223             compare = C_DIFFERENCE;
1224             break;
1225
1226         case 'E':
1227             initdump |= 2;
1228             break;
1229
1230         case 'e':
1231             initdump |= 1;
1232             break;
1233
1234         case 'F':
1235             filecompare = TRUE;
1236             break;
1237
1238         case 'f':
1239             formatted = TRUE;
1240             break;
1241
1242         case 'G':
1243             numbers = 1;
1244             break;
1245
1246         case 'g':
1247             numbers = -1;
1248             break;
1249
1250         case 'I':
1251             outform = F_TERMINFO;
1252             if (sortmode == S_DEFAULT)
1253                 sortmode = S_VARIABLE;
1254             tversion = 0;
1255             break;
1256
1257         case 'i':
1258             init_analyze = TRUE;
1259             break;
1260
1261         case 'L':
1262             outform = F_VARIABLE;
1263             if (sortmode == S_DEFAULT)
1264                 sortmode = S_VARIABLE;
1265             break;
1266
1267         case 'l':
1268             outform = F_TERMINFO;
1269             break;
1270
1271         case 'n':
1272             compare = C_NAND;
1273             break;
1274
1275         case 'p':
1276             ignorepads = TRUE;
1277             break;
1278
1279         case 'q':
1280             quiet = TRUE;
1281             s_absent = "-";
1282             s_cancel = "@";
1283             bool_sep = ", ";
1284             break;
1285
1286         case 'R':
1287             tversion = optarg;
1288             break;
1289
1290         case 'r':
1291             tversion = 0;
1292             limited = FALSE;
1293             break;
1294
1295         case 's':
1296             if (*optarg == 'd')
1297                 sortmode = S_NOSORT;
1298             else if (*optarg == 'i')
1299                 sortmode = S_TERMINFO;
1300             else if (*optarg == 'l')
1301                 sortmode = S_VARIABLE;
1302             else if (*optarg == 'c')
1303                 sortmode = S_TERMCAP;
1304             else {
1305                 (void) fprintf(stderr,
1306                                "infocmp: unknown sort mode\n");
1307                 return EXIT_FAILURE;
1308             }
1309             break;
1310
1311         case 'T':
1312             limited = FALSE;
1313             break;
1314
1315 #if NCURSES_XNAMES
1316         case 't':
1317             _nc_disable_period = FALSE;
1318             suppress_untranslatable = TRUE;
1319             break;
1320 #endif
1321
1322         case 'u':
1323             compare = C_USEALL;
1324             break;
1325
1326         case 'V':
1327             puts(curses_version());
1328             ExitProgram(EXIT_SUCCESS);
1329
1330         case 'v':
1331             itrace = optarg_to_number();
1332             set_trace_level(itrace);
1333             break;
1334
1335         case 'w':
1336             mwidth = optarg_to_number();
1337             break;
1338
1339         default:
1340             usage();
1341         }
1342
1343     /* by default, sort by terminfo name */
1344     if (sortmode == S_DEFAULT)
1345         sortmode = S_TERMINFO;
1346
1347     /* set up for display */
1348     dump_init(tversion, outform, sortmode, mwidth, itrace, formatted);
1349
1350     /* make sure we have at least one terminal name to work with */
1351     if (optind >= argc)
1352         argv[argc++] = terminal_env();
1353
1354     /* if user is after a comparison, make sure we have two entries */
1355     if (compare != C_DEFAULT && optind >= argc - 1)
1356         argv[argc++] = terminal_env();
1357
1358     /* exactly two terminal names with no options means do -d */
1359     if (argc - optind == 2 && compare == C_DEFAULT)
1360         compare = C_DIFFERENCE;
1361
1362     if (!filecompare) {
1363         /* grab the entries */
1364         termcount = 0;
1365         for (; optind < argc; optind++) {
1366             if (termcount >= MAXTERMS) {
1367                 (void) fprintf(stderr,
1368                                "infocmp: too many terminal type arguments\n");
1369                 return EXIT_FAILURE;
1370             } else {
1371                 const char *directory = termcount ? restdir : firstdir;
1372                 int status;
1373
1374                 tname[termcount] = argv[optind];
1375
1376                 if (directory) {
1377                     (void) sprintf(tfile[termcount], "%s/%c/%s",
1378                                    directory,
1379                                    *argv[optind], argv[optind]);
1380                     if (itrace)
1381                         (void) fprintf(stderr,
1382                                        "infocmp: reading entry %s from file %s\n",
1383                                        argv[optind], tfile[termcount]);
1384
1385                     status = _nc_read_file_entry(tfile[termcount],
1386                                                  &entries[termcount].tterm);
1387                 } else {
1388                     if (itrace)
1389                         (void) fprintf(stderr,
1390                                        "infocmp: reading entry %s from system directories %s\n",
1391                                        argv[optind], tname[termcount]);
1392
1393                     status = _nc_read_entry(tname[termcount],
1394                                             tfile[termcount],
1395                                             &entries[termcount].tterm);
1396                     directory = TERMINFO;       /* for error message */
1397                 }
1398
1399                 if (status <= 0) {
1400                     (void) fprintf(stderr,
1401                                    "infocmp: couldn't open terminfo file %s.\n",
1402                                    tfile[termcount]);
1403                     return EXIT_FAILURE;
1404                 }
1405                 repair_acsc(&entries[termcount].tterm);
1406                 termcount++;
1407             }
1408         }
1409
1410 #if NCURSES_XNAMES
1411         if (termcount > 1)
1412             _nc_align_termtype(&entries[0].tterm, &entries[1].tterm);
1413 #endif
1414
1415         /* dump as C initializer for the terminal type */
1416         if (initdump) {
1417             if (initdump & 1)
1418                 dump_termtype(&entries[0].tterm);
1419             if (initdump & 2)
1420                 dump_initializers(&entries[0].tterm);
1421             ExitProgram(EXIT_SUCCESS);
1422         }
1423
1424         /* analyze the init strings */
1425         if (init_analyze) {
1426 #undef CUR
1427 #define CUR     entries[0].tterm.
1428             analyze_string("is1", init_1string, &entries[0].tterm);
1429             analyze_string("is2", init_2string, &entries[0].tterm);
1430             analyze_string("is3", init_3string, &entries[0].tterm);
1431             analyze_string("rs1", reset_1string, &entries[0].tterm);
1432             analyze_string("rs2", reset_2string, &entries[0].tterm);
1433             analyze_string("rs3", reset_3string, &entries[0].tterm);
1434             analyze_string("smcup", enter_ca_mode, &entries[0].tterm);
1435             analyze_string("rmcup", exit_ca_mode, &entries[0].tterm);
1436 #undef CUR
1437             ExitProgram(EXIT_SUCCESS);
1438         }
1439
1440         /*
1441          * Here's where the real work gets done
1442          */
1443         switch (compare) {
1444         case C_DEFAULT:
1445             if (itrace)
1446                 (void) fprintf(stderr,
1447                                "infocmp: about to dump %s\n",
1448                                tname[0]);
1449             (void) printf("#\tReconstructed via infocmp from file: %s\n",
1450                           tfile[0]);
1451             len = dump_entry(&entries[0].tterm,
1452                              suppress_untranslatable,
1453                              limited,
1454                              0,
1455                              numbers,
1456                              NULL);
1457             putchar('\n');
1458             if (itrace)
1459                 (void) fprintf(stderr, "infocmp: length %d\n", len);
1460             break;
1461
1462         case C_DIFFERENCE:
1463             if (itrace)
1464                 (void) fprintf(stderr, "infocmp: dumping differences\n");
1465             (void) printf("comparing %s to %s.\n", tname[0], tname[1]);
1466             compare_entry(compare_predicate, &entries->tterm, quiet);
1467             break;
1468
1469         case C_COMMON:
1470             if (itrace)
1471                 (void) fprintf(stderr,
1472                                "infocmp: dumping common capabilities\n");
1473             (void) printf("comparing %s to %s.\n", tname[0], tname[1]);
1474             compare_entry(compare_predicate, &entries->tterm, quiet);
1475             break;
1476
1477         case C_NAND:
1478             if (itrace)
1479                 (void) fprintf(stderr,
1480                                "infocmp: dumping differences\n");
1481             (void) printf("comparing %s to %s.\n", tname[0], tname[1]);
1482             compare_entry(compare_predicate, &entries->tterm, quiet);
1483             break;
1484
1485         case C_USEALL:
1486             if (itrace)
1487                 (void) fprintf(stderr, "infocmp: dumping use entry\n");
1488             len = dump_entry(&entries[0].tterm,
1489                              suppress_untranslatable,
1490                              limited,
1491                              0,
1492                              numbers,
1493                              use_predicate);
1494             for (i = 1; i < termcount; i++)
1495                 len += dump_uses(tname[i], !(outform == F_TERMCAP
1496                                              || outform == F_TCONVERR));
1497             putchar('\n');
1498             if (itrace)
1499                 (void) fprintf(stderr, "infocmp: length %d\n", len);
1500             break;
1501         }
1502     } else if (compare == C_USEALL)
1503         (void) fprintf(stderr, "Sorry, -u doesn't work with -F\n");
1504     else if (compare == C_DEFAULT)
1505         (void) fprintf(stderr, "Use `tic -[CI] <file>' for this.\n");
1506     else if (argc - optind != 2)
1507         (void) fprintf(stderr,
1508                        "File comparison needs exactly two file arguments.\n");
1509     else
1510         file_comparison(argc - optind, argv + optind);
1511
1512     ExitProgram(EXIT_SUCCESS);
1513 }
1514
1515 /* infocmp.c ends here */