1 /****************************************************************************
2 * Copyright (c) 1998,1999 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 ****************************************************************************/
36 * infocmp.c -- decompile an entry, or compare two entries
37 * written by Eric S. Raymond
40 #include <progs.priv.h>
42 #include <term_entry.h>
43 #include <dump_entry.h>
45 MODULE_ID("$Id: infocmp.c,v 1.44 1999/06/16 00:39:48 tom Exp $")
50 #define MAXTERMS 32 /* max # terminal arguments we can handle */
52 const char *_nc_progname = "infocmp";
54 typedef char path[PATH_MAX];
56 /***************************************************************************
58 * The following control variables, together with the contents of the
59 * terminfo entries, completely determine the actions of the program.
61 ***************************************************************************/
63 static char *tname[MAXTERMS]; /* terminal type names */
64 static TERMTYPE term[MAXTERMS]; /* terminfo entries */
65 static int termcount; /* count of terminal entries */
67 static const char *tversion; /* terminfo version selected */
68 static int numbers = 0; /* format "%'char'" to/from "%{number}" */
69 static int outform; /* output format */
70 static int sortmode; /* sort_mode */
71 static int itrace; /* trace flag for debugging */
72 static int mwidth = 60;
74 /* main comparison mode */
76 #define C_DEFAULT 0 /* don't force comparison mode */
77 #define C_DIFFERENCE 1 /* list differences between two terminals */
78 #define C_COMMON 2 /* list common capabilities */
79 #define C_NAND 3 /* list capabilities in neither terminal */
80 #define C_USEALL 4 /* generate relative use-form entry */
81 static bool ignorepads; /* ignore pad prefixes when diffing */
85 static void ExitProgram(int code) GCC_NORETURN;
86 static void ExitProgram(int code)
88 while (termcount-- > 0)
89 _nc_free_termtype(&term[termcount]);
90 _nc_leaks_dump_entry();
91 _nc_free_and_exit(code);
95 static char *canonical_name(char *ptr, char *buf)
96 /* extract the terminal type's primary name */
100 (void) strcpy(buf, ptr);
101 if ((bp = strchr(buf, '|')) != (char *)NULL)
107 /***************************************************************************
109 * Predicates for dump function
111 ***************************************************************************/
113 static int capcmp(const char *s, const char *t)
114 /* capability comparison function */
116 if (!VALID_STRING(s) && !VALID_STRING(t))
118 else if (!VALID_STRING(s) || !VALID_STRING(t))
122 return(_nc_capcmp(s, t));
124 return(strcmp(s, t));
127 static int use_predicate(int type, int idx)
128 /* predicate function to use for use decompilation */
138 * This assumes that multiple use entries are supposed
139 * to contribute the logical or of their boolean capabilities.
140 * This is true if we take the semantics of multiple uses to
141 * be 'each capability gets the first non-default value found
142 * in the sequence of use entries'.
144 for (tp = &term[1]; tp < term + termcount; tp++)
145 if (tp->Booleans[idx]) {
149 if (is_set != term->Booleans[idx])
156 int value = ABSENT_NUMERIC;
159 * We take the semantics of multiple uses to be 'each
160 * capability gets the first non-default value found
161 * in the sequence of use entries'.
163 for (tp = &term[1]; tp < term + termcount; tp++)
164 if (tp->Numbers[idx] >= 0) {
165 value = tp->Numbers[idx];
169 if (value != term->Numbers[idx])
170 return(value != ABSENT_NUMERIC);
176 char *termstr, *usestr = ABSENT_STRING;
178 termstr = term->Strings[idx];
181 * We take the semantics of multiple uses to be 'each
182 * capability gets the first non-default value found
183 * in the sequence of use entries'.
185 for (tp = &term[1]; tp < term + termcount; tp++)
186 if (tp->Strings[idx])
188 usestr = tp->Strings[idx];
192 if (usestr == ABSENT_STRING && termstr == ABSENT_STRING)
194 else if (!usestr || !termstr || capcmp(usestr, termstr))
201 return(FALSE); /* pacify compiler */
204 static bool entryeq(TERMTYPE *t1, TERMTYPE *t2)
205 /* are two terminal types equal */
209 for (i = 0; i < NUM_BOOLEANS(t1); i++)
210 if (t1->Booleans[i] != t2->Booleans[i])
213 for (i = 0; i < NUM_NUMBERS(t1); i++)
214 if (t1->Numbers[i] != t2->Numbers[i])
217 for (i = 0; i < NUM_STRINGS(t1); i++)
218 if (capcmp(t1->Strings[i], t2->Strings[i]))
224 #define TIC_EXPAND(result) _nc_tic_expand(result, outform==F_TERMINFO, numbers)
226 static void compare_predicate(int type, int idx, const char *name)
227 /* predicate function to use for entry difference reports */
229 register TERMTYPE *t1 = &term[0];
230 register TERMTYPE *t2 = &term[1];
239 if (t1->Booleans[idx] != t2->Booleans[idx])
240 (void) printf("\t%s: %c:%c.\n",
242 t1->Booleans[idx] ? 'T' : 'F',
243 t2->Booleans[idx] ? 'T' : 'F');
247 if (t1->Booleans[idx] && t2->Booleans[idx])
248 (void) printf("\t%s= T.\n", name);
252 if (!t1->Booleans[idx] && !t2->Booleans[idx])
253 (void) printf("\t!%s.\n", name);
262 if (t1->Numbers[idx] != t2->Numbers[idx])
263 (void) printf("\t%s: %d:%d.\n",
264 name, t1->Numbers[idx], t2->Numbers[idx]);
268 if (t1->Numbers[idx]!=-1 && t2->Numbers[idx]!=-1
269 && t1->Numbers[idx] == t2->Numbers[idx])
270 (void) printf("\t%s= %d.\n", name, t1->Numbers[idx]);
274 if (t1->Numbers[idx]==-1 && t2->Numbers[idx] == -1)
275 (void) printf("\t!%s.\n", name);
281 s1 = t1->Strings[idx];
282 s2 = t2->Strings[idx];
288 char buf1[BUFSIZ], buf2[BUFSIZ];
290 if (s1 == (char *)NULL)
291 (void) strcpy(buf1, "NULL");
294 (void) strcpy(buf1, "'");
295 (void) strcat(buf1, TIC_EXPAND(s1));
296 (void) strcat(buf1, "'");
299 if (s2 == (char *)NULL)
300 (void) strcpy(buf2, "NULL");
303 (void) strcpy(buf2, "'");
304 (void) strcat(buf2, TIC_EXPAND(s2));
305 (void) strcat(buf2, "'");
308 if (strcmp(buf1, buf2))
309 (void) printf("\t%s: %s, %s.\n",
315 if (s1 && s2 && !capcmp(s1, s2))
316 (void) printf("\t%s= '%s'.\n", name, TIC_EXPAND(s1));
321 (void) printf("\t!%s.\n", name);
329 /***************************************************************************
331 * Init string analysis
333 ***************************************************************************/
335 typedef struct {const char *from; const char *to;} assoc;
337 static const assoc std_caps[] =
339 /* these are specified by X.364 and iBCS2 */
340 {"\033c", "RIS"}, /* full reset */
341 {"\0337", "SC"}, /* save cursor */
342 {"\0338", "RC"}, /* restore cursor */
343 {"\033[r", "RSR"}, /* not an X.364 mnemonic */
344 {"\033[m", "SGR0"}, /* not an X.364 mnemonic */
345 {"\033[2J", "ED2"}, /* clear page */
347 /* this group is specified by ISO 2022 */
348 {"\033(0", "ISO DEC G0"}, /* enable DEC graphics for G0 */
349 {"\033(A", "ISO UK G0"}, /* enable UK chars for G0 */
350 {"\033(B", "ISO US G0"}, /* enable US chars for G0 */
351 {"\033)0", "ISO DEC G1"}, /* enable DEC graphics for G1 */
352 {"\033)A", "ISO UK G1"}, /* enable UK chars for G1 */
353 {"\033)B", "ISO US G1"}, /* enable US chars for G1 */
355 /* these are DEC private modes widely supported by emulators */
356 {"\033=", "DECPAM"}, /* application keypad mode */
357 {"\033>", "DECPNM"}, /* normal keypad mode */
358 {"\033<", "DECANSI"}, /* enter ANSI mode */
360 { (char *)0, (char *)0}
363 static const assoc private_modes[] =
364 /* DEC \E[ ... [hl] modes recognized by many emulators */
366 {"1", "CKM"}, /* application cursor keys */
367 {"2", "ANM"}, /* set VT52 mode */
368 {"3", "COLM"}, /* 132-column mode */
369 {"4", "SCLM"}, /* smooth scroll */
370 {"5", "SCNM"}, /* reverse video mode */
371 {"6", "OM"}, /* origin mode */
372 {"7", "AWM"}, /* wraparound mode */
373 {"8", "ARM"}, /* auto-repeat mode */
374 {(char *)0, (char *)0}
377 static const assoc ecma_highlights[] =
378 /* recognize ECMA attribute sequences */
380 {"0", "NORMAL"}, /* normal */
381 {"1", "+BOLD"}, /* bold on */
382 {"2", "+DIM"}, /* dim on */
383 {"3", "+ITALIC"}, /* italic on */
384 {"4", "+UNDERLINE"}, /* underline on */
385 {"5", "+BLINK"}, /* blink on */
386 {"6", "+FASTBLINK"}, /* fastblink on */
387 {"7", "+REVERSE"}, /* reverse on */
388 {"8", "+INVISIBLE"}, /* invisible on */
389 {"9", "+DELETED"}, /* deleted on */
390 {"10", "MAIN-FONT"}, /* select primary font */
391 {"11", "ALT-FONT-1"}, /* select alternate font 1 */
392 {"12", "ALT-FONT-2"}, /* select alternate font 2 */
393 {"13", "ALT-FONT-3"}, /* select alternate font 3 */
394 {"14", "ALT-FONT-4"}, /* select alternate font 4 */
395 {"15", "ALT-FONT-5"}, /* select alternate font 5 */
396 {"16", "ALT-FONT-6"}, /* select alternate font 6 */
397 {"17", "ALT-FONT-7"}, /* select alternate font 7 */
398 {"18", "ALT-FONT-1"}, /* select alternate font 1 */
399 {"19", "ALT-FONT-1"}, /* select alternate font 1 */
400 {"20", "FRAKTUR"}, /* Fraktur font */
401 {"21", "DOUBLEUNDER"}, /* double underline */
402 {"22", "-DIM"}, /* dim off */
403 {"23", "-ITALIC"}, /* italic off */
404 {"24", "-UNDERLINE"}, /* underline off */
405 {"25", "-BLINK"}, /* blink off */
406 {"26", "-FASTBLINK"}, /* fastblink off */
407 {"27", "-REVERSE"}, /* reverse off */
408 {"28", "-INVISIBLE"}, /* invisible off */
409 {"29", "-DELETED"}, /* deleted off */
410 {(char *)0, (char *)0}
413 static void analyze_string(const char *name, const char *cap, TERMTYPE *tp)
415 char buf[MAX_TERMINFO_LENGTH];
416 char buf2[MAX_TERMINFO_LENGTH];
420 if (cap == ABSENT_STRING || cap == CANCELLED_STRING)
422 (void) printf("%s: ", name);
425 for (sp = cap; *sp; sp++)
429 const char *expansion = 0;
431 /* first, check other capabilities in this entry */
432 for (i = 0; i < STRCOUNT; i++)
434 char *cp = tp->Strings[i];
436 /* don't use soft-key capabilities */
437 if (strnames[i][0] == 'k' && strnames[i][0] == 'f')
441 if (cp != ABSENT_STRING && cp != CANCELLED_STRING && cp[0] && cp != cap)
444 (void) strncpy(buf2, sp, len);
447 if (_nc_capcmp(cp, buf2))
450 #define ISRS(s) (!strncmp((s), "is", 2) || !strncmp((s), "rs", 2))
452 * Theoretically we just passed the test for translation
453 * (equality once the padding is stripped). However, there
454 * are a few more hoops that need to be jumped so that
455 * identical pairs of initialization and reset strings
456 * don't just refer to each other.
458 if (ISRS(name) || ISRS(strnames[i]))
463 expansion = strnames[i];
468 /* now check the standard capabilities */
470 for (ap = std_caps; ap->from; ap++)
472 len = strlen(ap->from);
474 if (strncmp(ap->from, sp, len) == 0)
481 /* now check for private-mode sequences */
483 && sp[0] == '\033' && sp[1] == '[' && sp[2] == '?'
484 && (len = strspn(sp + 3, "0123456789;"))
485 && ((sp[3 + len] == 'h') || (sp[3 + len] == 'l')))
487 char buf3[MAX_TERMINFO_LENGTH];
489 (void) strcpy(buf2, (sp[3 + len] == 'h') ? "DEC+" : "DEC-");
490 (void) strncpy(buf3, sp + 3, len);
494 ep = strtok(buf3, ";");
498 for (ap = private_modes; ap->from; ap++)
500 size_t tlen = strlen(ap->from);
502 if (strncmp(ap->from, ep, tlen) == 0)
504 (void) strcat(buf2, ap->to);
511 (void) strcat(buf2, ep);
512 (void) strcat(buf2, ";");
514 ((ep = strtok((char *)NULL, ";")));
515 buf2[strlen(buf2) - 1] = '\0';
519 /* now check for ECMA highlight sequences */
521 && sp[0] == '\033' && sp[1] == '['
522 && (len = strspn(sp + 2, "0123456789;"))
523 && sp[2 + len] == 'm')
525 char buf3[MAX_TERMINFO_LENGTH];
527 (void) strcpy(buf2, "SGR:");
528 (void) strncpy(buf3, sp + 2, len);
532 ep = strtok(buf3, ";");
536 for (ap = ecma_highlights; ap->from; ap++)
538 size_t tlen = strlen(ap->from);
540 if (strncmp(ap->from, ep, tlen) == 0)
542 (void) strcat(buf2, ap->to);
549 (void) strcat(buf2, ep);
550 (void) strcat(buf2, ";");
552 ((ep = strtok((char *)NULL, ";")));
554 buf2[strlen(buf2) - 1] = '\0';
557 /* now check for scroll region reset */
560 (void) sprintf(buf2, "\033[1;%dr", tp->Numbers[2]);
562 if (strncmp(buf2, sp, len) == 0)
566 /* now check for home-down */
569 (void) sprintf(buf2, "\033[%d;1H", tp->Numbers[2]);
571 if (strncmp(buf2, sp, len) == 0)
575 /* now look at the expansion we got, if any */
578 (void) sprintf(buf + strlen(buf), "{%s}", expansion);
584 /* couldn't match anything */
587 (void) strcat(buf, TIC_EXPAND(buf2));
590 (void) printf("%s\n", buf);
593 /***************************************************************************
597 ***************************************************************************/
599 static void file_comparison(int argc, char *argv[])
602 /* someday we may allow comparisons on more files */
604 ENTRY *heads[MAXCOMPARE];
605 ENTRY *tails[MAXCOMPARE];
609 dump_init((char *)NULL, F_LITERAL, S_TERMINFO, 0, itrace, FALSE);
611 for (n = 0; n < argc && n < MAXCOMPARE; n++)
613 if (freopen(argv[n], "r", stdin) == NULL)
614 _nc_err_abort("Can't open %s", argv[n]);
616 _nc_head = _nc_tail = (ENTRY *)NULL;
618 /* parse entries out of the source file */
619 _nc_set_source(argv[n]);
620 _nc_read_entry_source(stdin, NULL, TRUE, FALSE, NULLHOOK);
623 (void) fprintf(stderr, "Resolving file %d...\n", n-0);
625 /* do use resolution */
626 if (!_nc_resolve_uses())
628 (void) fprintf(stderr,
629 "There are unresolved use entries in %s:\n",
634 (void) fputs(qp->tterm.term_names, stderr);
635 (void) fputc('\n', stderr);
640 heads[filecount] = _nc_head;
641 tails[filecount] = _nc_tail;
645 /* OK, all entries are in core. Ready to do the comparison */
647 (void) fprintf(stderr, "Entries are now in core...\n");
650 * The entry-matching loop. We're not using the use[]
651 * slots any more (they got zeroed out by resolve_uses) so
652 * we stash each entry's matches in the other file there.
653 * Sigh, this is intrinsically quadratic.
655 for (qp = heads[0]; qp; qp = qp->next)
657 for (rp = heads[1]; rp; rp = rp->next)
658 if (_nc_entry_match(qp->tterm.term_names, rp->tterm.term_names))
661 * This is why the uses structure parent element is
662 * (void *) -- so we can have either (char *) for
663 * names or entry structure pointers in them and still
666 if (qp->nuses < MAX_USES)
667 qp->uses[qp->nuses].parent = (void *)rp;
670 if (rp->nuses < MAX_USES)
671 rp->uses[rp->nuses].parent = (void *)qp;
676 /* now we have two circular lists with crosslinks */
678 (void) fprintf(stderr, "Name matches are done...\n");
680 for (qp = heads[0]; qp; qp = qp->next)
683 (void) fprintf(stderr,
684 "%s in file 1 (%s) has %d matches in file 2 (%s):\n",
685 _nc_first_name(qp->tterm.term_names),
689 for (i = 0; i < qp->nuses; i++)
690 (void) fprintf(stderr,
692 _nc_first_name(((ENTRY *)qp->uses[i].parent)->tterm.term_names));
694 for (rp = heads[1]; rp; rp = rp->next)
697 (void) fprintf(stderr,
698 "%s in file 2 (%s) has %d matches in file 1 (%s):\n",
699 _nc_first_name(rp->tterm.term_names),
703 for (i = 0; i < rp->nuses; i++)
704 (void) fprintf(stderr,
706 _nc_first_name(((ENTRY *)rp->uses[i].parent)->tterm.term_names));
709 (void) printf("In file 1 (%s) only:\n", argv[0]);
710 for (qp = heads[0]; qp; qp = qp->next)
712 (void) printf("\t%s\n",
713 _nc_first_name(qp->tterm.term_names));
715 (void) printf("In file 2 (%s) only:\n", argv[1]);
716 for (rp = heads[1]; rp; rp = rp->next)
718 (void) printf("\t%s\n",
719 _nc_first_name(rp->tterm.term_names));
721 (void) printf("The following entries are equivalent:\n");
722 for (qp = heads[0]; qp; qp = qp->next)
724 rp = (ENTRY *)qp->uses[0].parent;
726 if (qp->nuses == 1 && entryeq(&qp->tterm, &rp->tterm))
728 char name1[NAMESIZE], name2[NAMESIZE];
730 (void) canonical_name(qp->tterm.term_names, name1);
731 (void) canonical_name(rp->tterm.term_names, name2);
733 (void) printf("%s = %s\n", name1, name2);
737 (void) printf("Differing entries:\n");
739 for (qp = heads[0]; qp; qp = qp->next)
741 rp = (ENTRY *)qp->uses[0].parent;
745 _nc_align_termtype(&qp->tterm, &rp->tterm);
747 if (qp->nuses == 1 && !entryeq(&qp->tterm, &rp->tterm))
749 char name1[NAMESIZE], name2[NAMESIZE];
754 (void) canonical_name(qp->tterm.term_names, name1);
755 (void) canonical_name(rp->tterm.term_names, name2);
761 (void)fprintf(stderr, "infocmp: dumping differences\n");
762 (void) printf("comparing %s to %s.\n", name1, name2);
763 compare_entry(compare_predicate, term);
768 (void) fprintf(stderr,
769 "infocmp: dumping common capabilities\n");
770 (void) printf("comparing %s to %s.\n", name1, name2);
771 compare_entry(compare_predicate, term);
776 (void) fprintf(stderr,
777 "infocmp: dumping differences\n");
778 (void) printf("comparing %s to %s.\n", name1, name2);
779 compare_entry(compare_predicate, term);
787 static void usage(void)
789 static const char *tbl[] = {
790 "Usage: infocmp [options] [-A directory] [-B directory] [termname...]"
793 ," -1 print single-column"
794 ," -C use termcap-names"
795 ," -F compare terminfo-files"
796 ," -I use terminfo-names"
797 ," -L use long names"
798 ," -R subset (see manpage)"
799 ," -T eliminate size limits (test)"
801 ," -c list common capabilities"
802 ," -d list different capabilities"
803 ," -e format output for C initializer"
804 ," -E format output as C tables"
805 ," -f with -1, format complex strings"
806 ," -G format %{number} to %'char'"
807 ," -g format %'char' to %{number}"
808 ," -i analyze initialization/reset"
809 ," -l output terminfo names"
810 ," -n list capabilities in neither"
811 ," -p ignore padding specifiers"
812 ," -r with -C, output in termcap form"
813 ," -s [d|i|l|c] sort fields"
814 ," -u produce source with 'use='"
815 ," -v number (verbose)"
816 ," -w number (width)"
818 const size_t first = 3;
819 const size_t last = sizeof(tbl)/sizeof(tbl[0]);
820 const size_t left = (last - first + 1) / 2 + first;
823 for (n = 0; n < left; n++) {
824 size_t m = (n < first) ? last : n + left - first;
826 fprintf(stderr, "%-40.40s%s\n", tbl[n], tbl[m]);
828 fprintf(stderr, "%s\n", tbl[n]);
833 static char * name_initializer(const char *type)
835 static char *initializer;
838 if (initializer == 0)
839 initializer = malloc(strlen(term->term_names) + 20);
841 (void) sprintf(initializer, "%s_data_%s", type, term->term_names);
842 for (s = initializer; *s != 0 && *s != '|'; s++)
851 /* dump C initializers for the terminal type */
852 static void dump_initializers(void)
858 (void) printf("static bool %s[] = %s\n", name_initializer("bool"), L_CURL);
860 for_each_boolean(n,term)
862 switch((int)(term->Booleans[n]))
873 str = "ABSENT_BOOLEAN";
876 case CANCELLED_BOOLEAN:
877 str = "CANCELLED_BOOLEAN";
880 (void) printf("\t/* %3d: %-8s */\t%s,\n",
881 n, ExtBoolname(term,n,boolnames), str);
883 (void) printf("%s;\n", R_CURL);
885 (void) printf("static short %s[] = %s\n", name_initializer("number"), L_CURL);
887 for_each_number(n,term)
890 switch (term->Numbers[n])
893 str = "ABSENT_NUMERIC";
895 case CANCELLED_NUMERIC:
896 str = "CANCELLED_NUMERIC";
899 sprintf(buf, "%d", term->Numbers[n]);
903 (void) printf("\t/* %3d: %-8s */\t%s,\n", n, ExtNumname(term,n,numnames), str);
905 (void) printf("%s;\n", R_CURL);
907 size = sizeof(TERMTYPE)
908 + (NUM_BOOLEANS(term) * sizeof(term->Booleans[0]))
909 + (NUM_NUMBERS(term) * sizeof(term->Numbers[0]));
911 (void) printf("static char * %s[] = %s\n", name_initializer("string"), L_CURL);
913 for_each_string(n,term)
915 char buf[BUFSIZ], *sp, *tp;
917 if (term->Strings[n] == ABSENT_STRING)
918 str = "ABSENT_STRING";
919 else if (term->Strings[n] == CANCELLED_STRING)
920 str = "CANCELLED_STRING";
925 for (sp = term->Strings[n]; *sp; sp++)
927 if (isascii(*sp) && isprint(*sp) && *sp !='\\' && *sp != '"')
931 (void) sprintf(tp, "\\%03o", *sp & 0xff);
937 size += (strlen(term->Strings[n]) + 1);
943 (void) printf("%s;\n", R_CURL);
945 (void) printf("static char * %s[] = %s\n", name_initializer("string_ext"), L_CURL);
948 (void) printf("\t/* %3d: %-8s */\t%s,\n", n, ExtStrname(term,n,strnames), str);
950 (void) printf("%s;\n", R_CURL);
953 /* dump C initializers for the terminal type */
954 static void dump_termtype(void)
956 (void) printf("\t%s\n\t\t\"%s\",\n", L_CURL, term->term_names);
957 (void) printf("\t\t(char *)0,\t/* pointer to string table */\n");
959 (void) printf("\t\t%s,\n", name_initializer("bool"));
960 (void) printf("\t\t%s,\n", name_initializer("number"));
962 (void) printf("\t\t%s,\n", name_initializer("string"));
965 (void) printf("#if NCURSES_XNAMES\n");
966 (void) printf("\t\t(char *)0,\t/* pointer to extended string table */\n");
967 (void) printf("\t\t%s,\t/* ...corresponding names */\n",
968 (NUM_STRINGS(term) != STRCOUNT)
969 ? name_initializer("string_ext")
972 (void) printf("\t\t%d,\t\t/* count total Booleans */\n", NUM_BOOLEANS(term));
973 (void) printf("\t\t%d,\t\t/* count total Numbers */\n", NUM_NUMBERS(term));
974 (void) printf("\t\t%d,\t\t/* count total Strings */\n", NUM_STRINGS(term));
976 (void) printf("\t\t%d,\t\t/* count extensions to Booleans */\n", NUM_BOOLEANS(term) - BOOLCOUNT);
977 (void) printf("\t\t%d,\t\t/* count extensions to Numbers */\n", NUM_NUMBERS(term) - NUMCOUNT);
978 (void) printf("\t\t%d,\t\t/* count extensions to Strings */\n", NUM_STRINGS(term) - STRCOUNT);
980 (void) printf("#endif /* NCURSES_XNAMES */\n");
981 #endif /* NCURSES_XNAMES */
982 (void) printf("\t%s\n", R_CURL);
985 /***************************************************************************
989 ***************************************************************************/
991 int main(int argc, char *argv[])
993 char *terminal, *firstdir, *restdir;
994 /* Avoid "local data >32k" error with mwcc */
995 /* Also avoid overflowing smaller stacks on systems like AmigaOS */
996 path *tfile = malloc(sizeof(path)*MAXTERMS);
998 bool formatted = FALSE;
999 bool filecompare = FALSE;
1001 bool init_analyze = FALSE;
1002 bool limited = TRUE;
1004 if ((terminal = getenv("TERM")) == NULL)
1006 (void) fprintf(stderr,
1007 "infocmp: environment variable TERM not set\n");
1008 return EXIT_FAILURE;
1011 /* where is the terminfo database location going to default to? */
1012 restdir = firstdir = 0;
1014 while ((c = getopt(argc, argv, "deEcCfFGgIinlLprR:s:uv:Vw:A:B:1T")) != EOF)
1018 compare = C_DIFFERENCE;
1034 outform = F_TERMCAP;
1036 if (sortmode == S_DEFAULT)
1037 sortmode = S_TERMCAP;
1057 outform = F_TERMINFO;
1058 if (sortmode == S_DEFAULT)
1059 sortmode = S_VARIABLE;
1064 init_analyze = TRUE;
1068 outform = F_TERMINFO;
1072 outform = F_VARIABLE;
1073 if (sortmode == S_DEFAULT)
1074 sortmode = S_VARIABLE;
1096 sortmode = S_NOSORT;
1097 else if (*optarg == 'i')
1098 sortmode = S_TERMINFO;
1099 else if (*optarg == 'l')
1100 sortmode = S_VARIABLE;
1101 else if (*optarg == 'c')
1102 sortmode = S_TERMCAP;
1105 (void) fprintf(stderr,
1106 "infocmp: unknown sort mode\n");
1107 return EXIT_FAILURE;
1116 itrace = atoi(optarg);
1117 _nc_tracing = (1 << itrace) - 1;
1121 (void) fputs(NCURSES_VERSION, stdout);
1123 ExitProgram(EXIT_SUCCESS);
1126 mwidth = atoi(optarg);
1148 /* by default, sort by terminfo name */
1149 if (sortmode == S_DEFAULT)
1150 sortmode = S_TERMINFO;
1152 /* set up for display */
1153 dump_init(tversion, outform, sortmode, mwidth, itrace, formatted);
1155 /* make sure we have at least one terminal name to work with */
1157 argv[argc++] = terminal;
1159 /* if user is after a comparison, make sure we have two entries */
1160 if (compare != C_DEFAULT && optind >= argc - 1)
1161 argv[argc++] = terminal;
1163 /* exactly two terminal names with no options means do -d */
1164 if (argc - optind == 2 && compare == C_DEFAULT)
1165 compare = C_DIFFERENCE;
1169 /* grab the entries */
1171 for (; optind < argc; optind++)
1173 if (termcount >= MAXTERMS)
1175 (void) fprintf(stderr,
1176 "infocmp: too many terminal type arguments\n");
1177 return EXIT_FAILURE;
1181 const char *directory = termcount ? restdir : firstdir;
1184 tname[termcount] = argv[optind];
1188 (void) sprintf(tfile[termcount], "%s/%c/%s",
1190 *argv[optind], argv[optind]);
1192 (void) fprintf(stderr,
1193 "infocmp: reading entry %s from file %s\n",
1194 argv[optind], tfile[termcount]);
1196 status = _nc_read_file_entry(tfile[termcount],
1202 (void) fprintf(stderr,
1203 "infocmp: reading entry %s from system directories %s\n",
1204 argv[optind], tname[termcount]);
1206 status = _nc_read_entry(tname[termcount],
1209 directory = TERMINFO; /* for error message */
1214 (void) fprintf(stderr,
1215 "infocmp: couldn't open terminfo file %s.\n",
1217 return EXIT_FAILURE;
1225 _nc_align_termtype(&term[0], &term[1]);
1228 /* dump as C initializer for the terminal type */
1234 dump_initializers();
1235 ExitProgram(EXIT_SUCCESS);
1238 /* analyze the init strings */
1242 #define CUR term[0].
1243 analyze_string("is1", init_1string, &term[0]);
1244 analyze_string("is2", init_2string, &term[0]);
1245 analyze_string("is3", init_3string, &term[0]);
1246 analyze_string("rs1", reset_1string, &term[0]);
1247 analyze_string("rs2", reset_2string, &term[0]);
1248 analyze_string("rs3", reset_3string, &term[0]);
1249 analyze_string("smcup", enter_ca_mode, &term[0]);
1250 analyze_string("rmcup", exit_ca_mode, &term[0]);
1252 ExitProgram(EXIT_SUCCESS);
1256 * Here's where the real work gets done
1262 (void) fprintf(stderr,
1263 "infocmp: about to dump %s\n",
1265 (void) printf("#\tReconstructed via infocmp from file: %s\n",
1267 len = dump_entry(&term[0], limited, numbers, NULL);
1270 (void)fprintf(stderr, "infocmp: length %d\n", len);
1275 (void)fprintf(stderr, "infocmp: dumping differences\n");
1276 (void) printf("comparing %s to %s.\n", tname[0], tname[1]);
1277 compare_entry(compare_predicate, term);
1282 (void) fprintf(stderr,
1283 "infocmp: dumping common capabilities\n");
1284 (void) printf("comparing %s to %s.\n", tname[0], tname[1]);
1285 compare_entry(compare_predicate, term);
1290 (void) fprintf(stderr,
1291 "infocmp: dumping differences\n");
1292 (void) printf("comparing %s to %s.\n", tname[0], tname[1]);
1293 compare_entry(compare_predicate, term);
1298 (void) fprintf(stderr, "infocmp: dumping use entry\n");
1299 len = dump_entry(&term[0], limited, numbers, use_predicate);
1300 for (i = 1; i < termcount; i++)
1301 len += dump_uses(tname[i], !(outform==F_TERMCAP || outform==F_TCONVERR));
1304 (void)fprintf(stderr, "infocmp: length %d\n", len);
1308 else if (compare == C_USEALL)
1309 (void) fprintf(stderr, "Sorry, -u doesn't work with -F\n");
1310 else if (compare == C_DEFAULT)
1311 (void) fprintf(stderr, "Use `tic -[CI] <file>' for this.\n");
1312 else if (argc - optind != 2)
1313 (void) fprintf(stderr,
1314 "File comparison needs exactly two file arguments.\n");
1316 file_comparison(argc-optind, argv+optind);
1318 ExitProgram(EXIT_SUCCESS);
1321 /* infocmp.c ends here */