libncurses: updated to 6.0
[tomato.git] / release / src / router / libncurses / progs / infocmp.c
blobd3fdc526175e797ed59ed45f72ca9445280a0c8c
1 /****************************************************************************
2 * Copyright (c) 1998-2014,2015 Free Software Foundation, Inc. *
3 * *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
11 * *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
14 * *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
22 * *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
26 * authorization. *
27 ****************************************************************************/
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 ****************************************************************************/
36 * infocmp.c -- decompile an entry, or compare two entries
37 * written by Eric S. Raymond
38 * and Thomas E Dickey
41 #include <progs.priv.h>
43 #include <dump_entry.h>
45 MODULE_ID("$Id: infocmp.c,v 1.133 2015/05/27 00:57:41 tom Exp $")
47 #define L_CURL "{"
48 #define R_CURL "}"
50 #define MAX_STRING 1024 /* maximum formatted string */
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 ENTRY *entries; /* terminfo entries */
64 static int termcount; /* count of terminal entries */
66 static bool limited = TRUE; /* "-r" option is not set */
67 static bool quiet = FALSE;
68 static bool literal = FALSE;
69 static const char *bool_sep = ":";
70 static const char *s_absent = "NULL";
71 static const char *s_cancel = "NULL";
72 static const char *tversion; /* terminfo version selected */
73 static unsigned itrace; /* trace flag for debugging */
74 static int mwidth = 60;
75 static int mheight = 65535;
76 static int numbers = 0; /* format "%'char'" to/from "%{number}" */
77 static int outform = F_TERMINFO; /* output format */
78 static int sortmode; /* sort_mode */
80 /* main comparison mode */
81 static int compare;
82 #define C_DEFAULT 0 /* don't force comparison mode */
83 #define C_DIFFERENCE 1 /* list differences between two terminals */
84 #define C_COMMON 2 /* list common capabilities */
85 #define C_NAND 3 /* list capabilities in neither terminal */
86 #define C_USEALL 4 /* generate relative use-form entry */
87 static bool ignorepads; /* ignore pad prefixes when diffing */
89 #if NO_LEAKS
91 typedef struct {
92 ENTRY *head;
93 ENTRY *tail;
94 } ENTERED;
96 static ENTERED *entered;
98 #undef ExitProgram
99 static void ExitProgram(int code) GCC_NORETURN;
100 /* prototype is to get gcc to accept the noreturn attribute */
101 static void
102 ExitProgram(int code)
104 int n;
106 for (n = 0; n < termcount; ++n) {
107 ENTRY *new_head = _nc_head;
108 ENTRY *new_tail = _nc_tail;
109 _nc_head = entered[n].head;
110 _nc_tail = entered[n].tail;
111 _nc_free_entries(entered[n].head);
112 _nc_head = new_head;
113 _nc_tail = new_tail;
115 _nc_leaks_dump_entry();
116 free(entries);
117 free(entered);
118 _nc_free_tic(code);
120 #endif
122 static void
123 failed(const char *s)
125 perror(s);
126 ExitProgram(EXIT_FAILURE);
129 static char *
130 canonical_name(char *ptr, char *buf)
131 /* extract the terminal type's primary name */
133 char *bp;
135 _nc_STRCPY(buf, ptr, NAMESIZE);
136 if ((bp = strchr(buf, '|')) != 0)
137 *bp = '\0';
139 return (buf);
142 /***************************************************************************
144 * Predicates for dump function
146 ***************************************************************************/
148 static int
149 capcmp(PredIdx idx, const char *s, const char *t)
150 /* capability comparison function */
152 if (!VALID_STRING(s) && !VALID_STRING(t))
153 return (s != t);
154 else if (!VALID_STRING(s) || !VALID_STRING(t))
155 return (1);
157 if ((idx == acs_chars_index) || !ignorepads)
158 return (strcmp(s, t));
159 else
160 return (_nc_capcmp(s, t));
163 static int
164 use_predicate(unsigned type, PredIdx idx)
165 /* predicate function to use for use decompilation */
167 ENTRY *ep;
169 switch (type) {
170 case BOOLEAN:
172 int is_set = FALSE;
175 * This assumes that multiple use entries are supposed
176 * to contribute the logical or of their boolean capabilities.
177 * This is true if we take the semantics of multiple uses to
178 * be 'each capability gets the first non-default value found
179 * in the sequence of use entries'.
181 * Note that cancelled or absent booleans are stored as FALSE,
182 * unlike numbers and strings, whose cancelled/absent state is
183 * recorded in the terminfo database.
185 for (ep = &entries[1]; ep < entries + termcount; ep++)
186 if (ep->tterm.Booleans[idx] == TRUE) {
187 is_set = entries[0].tterm.Booleans[idx];
188 break;
190 if (is_set != entries[0].tterm.Booleans[idx])
191 return (!is_set);
192 else
193 return (FAIL);
196 case NUMBER:
198 int value = ABSENT_NUMERIC;
201 * We take the semantics of multiple uses to be 'each
202 * capability gets the first non-default value found
203 * in the sequence of use entries'.
205 for (ep = &entries[1]; ep < entries + termcount; ep++)
206 if (VALID_NUMERIC(ep->tterm.Numbers[idx])) {
207 value = ep->tterm.Numbers[idx];
208 break;
211 if (value != entries[0].tterm.Numbers[idx])
212 return (value != ABSENT_NUMERIC);
213 else
214 return (FAIL);
217 case STRING:
219 char *termstr, *usestr = ABSENT_STRING;
221 termstr = entries[0].tterm.Strings[idx];
224 * We take the semantics of multiple uses to be 'each
225 * capability gets the first non-default value found
226 * in the sequence of use entries'.
228 for (ep = &entries[1]; ep < entries + termcount; ep++)
229 if (ep->tterm.Strings[idx]) {
230 usestr = ep->tterm.Strings[idx];
231 break;
234 if (usestr == ABSENT_STRING && termstr == ABSENT_STRING)
235 return (FAIL);
236 else if (!usestr || !termstr || capcmp(idx, usestr, termstr))
237 return (TRUE);
238 else
239 return (FAIL);
243 return (FALSE); /* pacify compiler */
246 static bool
247 useeq(ENTRY * e1, ENTRY * e2)
248 /* are the use references in two entries equivalent? */
250 unsigned i, j;
252 if (e1->nuses != e2->nuses)
253 return (FALSE);
255 /* Ugh...this is quadratic again */
256 for (i = 0; i < e1->nuses; i++) {
257 bool foundmatch = FALSE;
259 /* search second entry for given use reference */
260 for (j = 0; j < e2->nuses; j++)
261 if (!strcmp(e1->uses[i].name, e2->uses[j].name)) {
262 foundmatch = TRUE;
263 break;
266 if (!foundmatch)
267 return (FALSE);
270 return (TRUE);
273 static bool
274 entryeq(TERMTYPE *t1, TERMTYPE *t2)
275 /* are two entries equivalent? */
277 unsigned i;
279 for (i = 0; i < NUM_BOOLEANS(t1); i++)
280 if (t1->Booleans[i] != t2->Booleans[i])
281 return (FALSE);
283 for (i = 0; i < NUM_NUMBERS(t1); i++)
284 if (t1->Numbers[i] != t2->Numbers[i])
285 return (FALSE);
287 for (i = 0; i < NUM_STRINGS(t1); i++)
288 if (capcmp((PredIdx) i, t1->Strings[i], t2->Strings[i]))
289 return (FALSE);
291 return (TRUE);
294 #define TIC_EXPAND(result) _nc_tic_expand(result, outform==F_TERMINFO, numbers)
296 static void
297 print_uses(ENTRY * ep, FILE *fp)
298 /* print an entry's use references */
300 unsigned i;
302 if (!ep->nuses)
303 fputs("NULL", fp);
304 else
305 for (i = 0; i < ep->nuses; i++) {
306 fputs(ep->uses[i].name, fp);
307 if (i < ep->nuses - 1)
308 fputs(" ", fp);
312 static const char *
313 dump_boolean(int val)
314 /* display the value of a boolean capability */
316 switch (val) {
317 case ABSENT_BOOLEAN:
318 return (s_absent);
319 case CANCELLED_BOOLEAN:
320 return (s_cancel);
321 case FALSE:
322 return ("F");
323 case TRUE:
324 return ("T");
325 default:
326 return ("?");
330 static void
331 dump_numeric(int val, char *buf)
332 /* display the value of a boolean capability */
334 switch (val) {
335 case ABSENT_NUMERIC:
336 _nc_STRCPY(buf, s_absent, MAX_STRING);
337 break;
338 case CANCELLED_NUMERIC:
339 _nc_STRCPY(buf, s_cancel, MAX_STRING);
340 break;
341 default:
342 _nc_SPRINTF(buf, _nc_SLIMIT(MAX_STRING) "%d", val);
343 break;
347 static void
348 dump_string(char *val, char *buf)
349 /* display the value of a string capability */
351 if (val == ABSENT_STRING)
352 _nc_STRCPY(buf, s_absent, MAX_STRING);
353 else if (val == CANCELLED_STRING)
354 _nc_STRCPY(buf, s_cancel, MAX_STRING);
355 else {
356 _nc_SPRINTF(buf, _nc_SLIMIT(MAX_STRING)
357 "'%.*s'", MAX_STRING - 3, TIC_EXPAND(val));
362 * Show "comparing..." message for the given terminal names.
364 static void
365 show_comparing(char **names)
367 if (itrace) {
368 switch (compare) {
369 case C_DIFFERENCE:
370 (void) fprintf(stderr, "%s: dumping differences\n", _nc_progname);
371 break;
373 case C_COMMON:
374 (void) fprintf(stderr, "%s: dumping common capabilities\n", _nc_progname);
375 break;
377 case C_NAND:
378 (void) fprintf(stderr, "%s: dumping differences\n", _nc_progname);
379 break;
382 if (*names) {
383 printf("comparing %s", *names++);
384 if (*names) {
385 printf(" to %s", *names++);
386 while (*names) {
387 printf(", %s", *names++);
390 printf(".\n");
395 * ncurses stores two types of non-standard capabilities:
396 * a) capabilities listed past the "STOP-HERE" comment in the Caps file.
397 * These are used in the terminfo source file to provide data for termcaps,
398 * e.g., when there is no equivalent capability in terminfo, as well as for
399 * widely-used non-standard capabilities.
400 * b) user-definable capabilities, via "tic -x".
402 * However, if "-x" is omitted from the tic command, both types of
403 * non-standard capability are not loaded into the terminfo database. This
404 * macro is used for limit-checks against the symbols that tic uses to omit
405 * the two types of non-standard entry.
407 #if NCURSES_XNAMES
408 #define check_user_definable(n,limit) if (!_nc_user_definable && (n) > (limit)) break
409 #else
410 #define check_user_definable(n,limit) if ((n) > (limit)) break
411 #endif
414 * Use these macros to simplify loops on C_COMMON and C_NAND:
416 #define for_each_entry() while (entries[extra].tterm.term_names)
417 #define next_entry (&(entries[extra++].tterm))
419 static void
420 compare_predicate(PredType type, PredIdx idx, const char *name)
421 /* predicate function to use for entry difference reports */
423 ENTRY *e1 = &entries[0];
424 ENTRY *e2 = &entries[1];
425 char buf1[MAX_STRING];
426 char buf2[MAX_STRING];
427 int b1, b2;
428 int n1, n2;
429 char *s1, *s2;
430 bool found;
431 int extra = 1;
433 switch (type) {
434 case CMP_BOOLEAN:
435 check_user_definable(idx, BOOLWRITE);
436 b1 = e1->tterm.Booleans[idx];
437 switch (compare) {
438 case C_DIFFERENCE:
439 b2 = next_entry->Booleans[idx];
440 if (!(b1 == ABSENT_BOOLEAN && b2 == ABSENT_BOOLEAN) && b1 != b2)
441 (void) printf("\t%s: %s%s%s.\n",
442 name,
443 dump_boolean(b1),
444 bool_sep,
445 dump_boolean(b2));
446 break;
448 case C_COMMON:
449 if (b1 != ABSENT_BOOLEAN) {
450 found = TRUE;
451 for_each_entry() {
452 b2 = next_entry->Booleans[idx];
453 if (b1 != b2) {
454 found = FALSE;
455 break;
458 if (found) {
459 (void) printf("\t%s= %s.\n", name, dump_boolean(b1));
462 break;
464 case C_NAND:
465 if (b1 == ABSENT_BOOLEAN) {
466 found = TRUE;
467 for_each_entry() {
468 b2 = next_entry->Booleans[idx];
469 if (b1 != b2) {
470 found = FALSE;
471 break;
474 if (found) {
475 (void) printf("\t!%s.\n", name);
478 break;
480 break;
482 case CMP_NUMBER:
483 check_user_definable(idx, NUMWRITE);
484 n1 = e1->tterm.Numbers[idx];
485 switch (compare) {
486 case C_DIFFERENCE:
487 n2 = next_entry->Numbers[idx];
488 if (!((n1 == ABSENT_NUMERIC && n2 == ABSENT_NUMERIC)) && n1 != n2) {
489 dump_numeric(n1, buf1);
490 dump_numeric(n2, buf2);
491 (void) printf("\t%s: %s, %s.\n", name, buf1, buf2);
493 break;
495 case C_COMMON:
496 if (n1 != ABSENT_NUMERIC) {
497 found = TRUE;
498 for_each_entry() {
499 n2 = next_entry->Numbers[idx];
500 if (n1 != n2) {
501 found = FALSE;
502 break;
505 if (found) {
506 dump_numeric(n1, buf1);
507 (void) printf("\t%s= %s.\n", name, buf1);
510 break;
512 case C_NAND:
513 if (n1 == ABSENT_NUMERIC) {
514 found = TRUE;
515 for_each_entry() {
516 n2 = next_entry->Numbers[idx];
517 if (n1 != n2) {
518 found = FALSE;
519 break;
522 if (found) {
523 (void) printf("\t!%s.\n", name);
526 break;
528 break;
530 case CMP_STRING:
531 check_user_definable(idx, STRWRITE);
532 s1 = e1->tterm.Strings[idx];
533 switch (compare) {
534 case C_DIFFERENCE:
535 s2 = next_entry->Strings[idx];
536 if (capcmp(idx, s1, s2)) {
537 dump_string(s1, buf1);
538 dump_string(s2, buf2);
539 if (strcmp(buf1, buf2))
540 (void) printf("\t%s: %s, %s.\n", name, buf1, buf2);
542 break;
544 case C_COMMON:
545 if (s1 != ABSENT_STRING) {
546 found = TRUE;
547 for_each_entry() {
548 s2 = next_entry->Strings[idx];
549 if (capcmp(idx, s1, s2) != 0) {
550 found = FALSE;
551 break;
554 if (found) {
555 (void) printf("\t%s= '%s'.\n", name, TIC_EXPAND(s1));
558 break;
560 case C_NAND:
561 if (s1 == ABSENT_STRING) {
562 found = TRUE;
563 for_each_entry() {
564 s2 = next_entry->Strings[idx];
565 if (s2 != s1) {
566 found = FALSE;
567 break;
570 if (found) {
571 (void) printf("\t!%s.\n", name);
574 break;
576 break;
578 case CMP_USE:
579 /* unlike the other modes, this compares *all* use entries */
580 switch (compare) {
581 case C_DIFFERENCE:
582 if (!useeq(e1, e2)) {
583 (void) fputs("\tuse: ", stdout);
584 print_uses(e1, stdout);
585 fputs(", ", stdout);
586 print_uses(e2, stdout);
587 fputs(".\n", stdout);
589 break;
591 case C_COMMON:
592 if (e1->nuses) {
593 found = TRUE;
594 for_each_entry() {
595 e2 = &entries[extra++];
596 if (e2->nuses != e1->nuses || !useeq(e1, e2)) {
597 found = FALSE;
598 break;
601 if (found) {
602 (void) fputs("\tuse: ", stdout);
603 print_uses(e1, stdout);
604 fputs(".\n", stdout);
607 break;
609 case C_NAND:
610 if (!e1->nuses) {
611 found = TRUE;
612 for_each_entry() {
613 e2 = &entries[extra++];
614 if (e2->nuses != e1->nuses) {
615 found = FALSE;
616 break;
619 if (found) {
620 (void) printf("\t!use.\n");
623 break;
628 /***************************************************************************
630 * Init string analysis
632 ***************************************************************************/
634 #define DATA(from, to) { { from }, { to } }
635 #define DATAX() DATA("", "")
637 typedef struct {
638 const char from[4];
639 const char to[12];
640 } assoc;
642 static const assoc std_caps[] =
644 /* these are specified by X.364 and iBCS2 */
645 DATA("\033c", "RIS"), /* full reset */
646 DATA("\0337", "SC"), /* save cursor */
647 DATA("\0338", "RC"), /* restore cursor */
648 DATA("\033[r", "RSR"), /* not an X.364 mnemonic */
649 DATA("\033[m", "SGR0"), /* not an X.364 mnemonic */
650 DATA("\033[2J", "ED2"), /* clear page */
652 /* this group is specified by ISO 2022 */
653 DATA("\033(0", "ISO DEC G0"), /* enable DEC graphics for G0 */
654 DATA("\033(A", "ISO UK G0"), /* enable UK chars for G0 */
655 DATA("\033(B", "ISO US G0"), /* enable US chars for G0 */
656 DATA("\033)0", "ISO DEC G1"), /* enable DEC graphics for G1 */
657 DATA("\033)A", "ISO UK G1"), /* enable UK chars for G1 */
658 DATA("\033)B", "ISO US G1"), /* enable US chars for G1 */
660 /* these are DEC private controls widely supported by emulators */
661 DATA("\033=", "DECPAM"), /* application keypad mode */
662 DATA("\033>", "DECPNM"), /* normal keypad mode */
663 DATA("\033<", "DECANSI"), /* enter ANSI mode */
664 DATA("\033[!p", "DECSTR"), /* soft reset */
665 DATA("\033 F", "S7C1T"), /* 7-bit controls */
667 DATAX()
670 static const assoc std_modes[] =
671 /* ECMA \E[ ... [hl] modes recognized by many emulators */
673 DATA("2", "AM"), /* keyboard action mode */
674 DATA("4", "IRM"), /* insert/replace mode */
675 DATA("12", "SRM"), /* send/receive mode */
676 DATA("20", "LNM"), /* linefeed mode */
677 DATAX()
680 static const assoc private_modes[] =
681 /* DEC \E[ ... [hl] modes recognized by many emulators */
683 DATA("1", "CKM"), /* application cursor keys */
684 DATA("2", "ANM"), /* set VT52 mode */
685 DATA("3", "COLM"), /* 132-column mode */
686 DATA("4", "SCLM"), /* smooth scroll */
687 DATA("5", "SCNM"), /* reverse video mode */
688 DATA("6", "OM"), /* origin mode */
689 DATA("7", "AWM"), /* wraparound mode */
690 DATA("8", "ARM"), /* auto-repeat mode */
691 DATAX()
694 static const assoc ecma_highlights[] =
695 /* recognize ECMA attribute sequences */
697 DATA("0", "NORMAL"), /* normal */
698 DATA("1", "+BOLD"), /* bold on */
699 DATA("2", "+DIM"), /* dim on */
700 DATA("3", "+ITALIC"), /* italic on */
701 DATA("4", "+UNDERLINE"), /* underline on */
702 DATA("5", "+BLINK"), /* blink on */
703 DATA("6", "+FASTBLINK"), /* fastblink on */
704 DATA("7", "+REVERSE"), /* reverse on */
705 DATA("8", "+INVISIBLE"), /* invisible on */
706 DATA("9", "+DELETED"), /* deleted on */
707 DATA("10", "MAIN-FONT"), /* select primary font */
708 DATA("11", "ALT-FONT-1"), /* select alternate font 1 */
709 DATA("12", "ALT-FONT-2"), /* select alternate font 2 */
710 DATA("13", "ALT-FONT-3"), /* select alternate font 3 */
711 DATA("14", "ALT-FONT-4"), /* select alternate font 4 */
712 DATA("15", "ALT-FONT-5"), /* select alternate font 5 */
713 DATA("16", "ALT-FONT-6"), /* select alternate font 6 */
714 DATA("17", "ALT-FONT-7"), /* select alternate font 7 */
715 DATA("18", "ALT-FONT-1"), /* select alternate font 1 */
716 DATA("19", "ALT-FONT-1"), /* select alternate font 1 */
717 DATA("20", "FRAKTUR"), /* Fraktur font */
718 DATA("21", "DOUBLEUNDER"), /* double underline */
719 DATA("22", "-DIM"), /* dim off */
720 DATA("23", "-ITALIC"), /* italic off */
721 DATA("24", "-UNDERLINE"), /* underline off */
722 DATA("25", "-BLINK"), /* blink off */
723 DATA("26", "-FASTBLINK"), /* fastblink off */
724 DATA("27", "-REVERSE"), /* reverse off */
725 DATA("28", "-INVISIBLE"), /* invisible off */
726 DATA("29", "-DELETED"), /* deleted off */
727 DATAX()
730 #undef DATA
732 static int
733 skip_csi(const char *cap)
735 int result = 0;
736 if (cap[0] == '\033' && cap[1] == '[')
737 result = 2;
738 else if (UChar(cap[0]) == 0233)
739 result = 1;
740 return result;
743 static bool
744 same_param(const char *table, const char *param, size_t length)
746 bool result = FALSE;
747 if (strncmp(table, param, length) == 0) {
748 result = !isdigit(UChar(param[length]));
750 return result;
753 static char *
754 lookup_params(const assoc * table, char *dst, char *src)
756 char *result = 0;
757 const char *ep = strtok(src, ";");
759 if (ep != 0) {
760 const assoc *ap;
762 do {
763 bool found = FALSE;
765 for (ap = table; ap->from[0]; ap++) {
766 size_t tlen = strlen(ap->from);
768 if (same_param(ap->from, ep, tlen)) {
769 _nc_STRCAT(dst, ap->to, MAX_TERMINFO_LENGTH);
770 found = TRUE;
771 break;
775 if (!found)
776 _nc_STRCAT(dst, ep, MAX_TERMINFO_LENGTH);
777 _nc_STRCAT(dst, ";", MAX_TERMINFO_LENGTH);
778 } while
779 ((ep = strtok((char *) 0, ";")));
781 dst[strlen(dst) - 1] = '\0';
783 result = dst;
785 return result;
788 static void
789 analyze_string(const char *name, const char *cap, TERMTYPE *tp)
791 char buf2[MAX_TERMINFO_LENGTH];
792 const char *sp;
793 const assoc *ap;
794 int tp_lines = tp->Numbers[2];
796 if (!VALID_STRING(cap))
797 return;
798 (void) printf("%s: ", name);
800 for (sp = cap; *sp; sp++) {
801 int i;
802 int csi;
803 size_t len = 0;
804 size_t next;
805 const char *expansion = 0;
806 char buf3[MAX_TERMINFO_LENGTH];
808 /* first, check other capabilities in this entry */
809 for (i = 0; i < STRCOUNT; i++) {
810 char *cp = tp->Strings[i];
812 /* don't use function-key capabilities */
813 if (strnames[i][0] == 'k' && strnames[i][1] == 'f')
814 continue;
816 if (VALID_STRING(cp) &&
817 cp[0] != '\0' &&
818 cp != cap) {
819 len = strlen(cp);
820 (void) strncpy(buf2, sp, len);
821 buf2[len] = '\0';
823 if (_nc_capcmp(cp, buf2))
824 continue;
826 #define ISRS(s) (!strncmp((s), "is", (size_t) 2) || !strncmp((s), "rs", (size_t) 2))
828 * Theoretically we just passed the test for translation
829 * (equality once the padding is stripped). However, there
830 * are a few more hoops that need to be jumped so that
831 * identical pairs of initialization and reset strings
832 * don't just refer to each other.
834 if (ISRS(name) || ISRS(strnames[i]))
835 if (cap < cp)
836 continue;
837 #undef ISRS
839 expansion = strnames[i];
840 break;
844 /* now check the standard capabilities */
845 if (!expansion) {
846 csi = skip_csi(sp);
847 for (ap = std_caps; ap->from[0]; ap++) {
848 size_t adj = (size_t) (csi ? 2 : 0);
850 len = strlen(ap->from);
851 if (csi && skip_csi(ap->from) != csi)
852 continue;
853 if (len > adj
854 && strncmp(ap->from + adj, sp + csi, len - adj) == 0) {
855 expansion = ap->to;
856 len -= adj;
857 len += (size_t) csi;
858 break;
863 /* now check for standard-mode sequences */
864 if (!expansion
865 && (csi = skip_csi(sp)) != 0
866 && (len = (strspn) (sp + csi, "0123456789;"))
867 && (len < sizeof(buf3))
868 && (next = (size_t) csi + len)
869 && ((sp[next] == 'h') || (sp[next] == 'l'))) {
871 _nc_STRCPY(buf2,
872 ((sp[next] == 'h')
873 ? "ECMA+"
874 : "ECMA-"),
875 sizeof(buf2));
876 (void) strncpy(buf3, sp + csi, len);
877 buf3[len] = '\0';
878 len += (size_t) csi + 1;
880 expansion = lookup_params(std_modes, buf2, buf3);
883 /* now check for private-mode sequences */
884 if (!expansion
885 && (csi = skip_csi(sp)) != 0
886 && sp[csi] == '?'
887 && (len = (strspn) (sp + csi + 1, "0123456789;"))
888 && (len < sizeof(buf3))
889 && (next = (size_t) csi + 1 + len)
890 && ((sp[next] == 'h') || (sp[next] == 'l'))) {
892 _nc_STRCPY(buf2,
893 ((sp[next] == 'h')
894 ? "DEC+"
895 : "DEC-"),
896 sizeof(buf2));
897 (void) strncpy(buf3, sp + csi + 1, len);
898 buf3[len] = '\0';
899 len += (size_t) csi + 2;
901 expansion = lookup_params(private_modes, buf2, buf3);
904 /* now check for ECMA highlight sequences */
905 if (!expansion
906 && (csi = skip_csi(sp)) != 0
907 && (len = (strspn) (sp + csi, "0123456789;")) != 0
908 && (len < sizeof(buf3))
909 && (next = (size_t) csi + len)
910 && sp[next] == 'm') {
912 _nc_STRCPY(buf2, "SGR:", sizeof(buf2));
913 (void) strncpy(buf3, sp + csi, len);
914 buf3[len] = '\0';
915 len += (size_t) csi + 1;
917 expansion = lookup_params(ecma_highlights, buf2, buf3);
920 if (!expansion
921 && (csi = skip_csi(sp)) != 0
922 && sp[csi] == 'm') {
923 len = (size_t) csi + 1;
924 _nc_STRCPY(buf2, "SGR:", sizeof(buf2));
925 _nc_STRCAT(buf2, ecma_highlights[0].to, sizeof(buf2));
926 expansion = buf2;
929 /* now check for scroll region reset */
930 if (!expansion
931 && (csi = skip_csi(sp)) != 0) {
932 if (sp[csi] == 'r') {
933 expansion = "RSR";
934 len = 1;
935 } else {
936 _nc_SPRINTF(buf2, _nc_SLIMIT(sizeof(buf2)) "1;%dr", tp_lines);
937 len = strlen(buf2);
938 if (strncmp(buf2, sp + csi, len) == 0)
939 expansion = "RSR";
941 len += (size_t) csi;
944 /* now check for home-down */
945 if (!expansion
946 && (csi = skip_csi(sp)) != 0) {
947 _nc_SPRINTF(buf2, _nc_SLIMIT(sizeof(buf2)) "%d;1H", tp_lines);
948 len = strlen(buf2);
949 if (strncmp(buf2, sp + csi, len) == 0) {
950 expansion = "LL";
951 } else {
952 _nc_SPRINTF(buf2, _nc_SLIMIT(sizeof(buf2)) "%dH", tp_lines);
953 len = strlen(buf2);
954 if (strncmp(buf2, sp + csi, len) == 0) {
955 expansion = "LL";
958 len += (size_t) csi;
961 /* now look at the expansion we got, if any */
962 if (expansion) {
963 printf("{%s}", expansion);
964 sp += len - 1;
965 } else {
966 /* couldn't match anything */
967 buf2[0] = *sp;
968 buf2[1] = '\0';
969 fputs(TIC_EXPAND(buf2), stdout);
972 putchar('\n');
975 /***************************************************************************
977 * File comparison
979 ***************************************************************************/
981 static void
982 file_comparison(int argc, char *argv[])
984 #define MAXCOMPARE 2
985 /* someday we may allow comparisons on more files */
986 int filecount = 0;
987 ENTRY *heads[MAXCOMPARE];
988 ENTRY *qp, *rp;
989 int i, n;
991 memset(heads, 0, sizeof(heads));
992 dump_init((char *) 0, F_LITERAL, S_TERMINFO, 0, 65535, itrace, FALSE, FALSE);
994 for (n = 0; n < argc && n < MAXCOMPARE; n++) {
995 if (freopen(argv[n], "r", stdin) == 0)
996 _nc_err_abort("Can't open %s", argv[n]);
998 #if NO_LEAKS
999 entered[n].head = _nc_head;
1000 entered[n].tail = _nc_tail;
1001 #endif
1002 _nc_head = _nc_tail = 0;
1004 /* parse entries out of the source file */
1005 _nc_set_source(argv[n]);
1006 _nc_read_entry_source(stdin, NULL, TRUE, literal, NULLHOOK);
1008 if (itrace)
1009 (void) fprintf(stderr, "Resolving file %d...\n", n - 0);
1011 /* maybe do use resolution */
1012 if (!_nc_resolve_uses2(!limited, literal)) {
1013 (void) fprintf(stderr,
1014 "There are unresolved use entries in %s:\n",
1015 argv[n]);
1016 for_entry_list(qp) {
1017 if (qp->nuses) {
1018 (void) fputs(qp->tterm.term_names, stderr);
1019 (void) fputc('\n', stderr);
1022 ExitProgram(EXIT_FAILURE);
1025 heads[filecount] = _nc_head;
1026 filecount++;
1029 /* OK, all entries are in core. Ready to do the comparison */
1030 if (itrace)
1031 (void) fprintf(stderr, "Entries are now in core...\n");
1033 /* The entry-matching loop. Sigh, this is intrinsically quadratic. */
1034 for (qp = heads[0]; qp; qp = qp->next) {
1035 for (rp = heads[1]; rp; rp = rp->next)
1036 if (_nc_entry_match(qp->tterm.term_names, rp->tterm.term_names)) {
1037 if (qp->ncrosslinks < MAX_CROSSLINKS)
1038 qp->crosslinks[qp->ncrosslinks] = rp;
1039 qp->ncrosslinks++;
1041 if (rp->ncrosslinks < MAX_CROSSLINKS)
1042 rp->crosslinks[rp->ncrosslinks] = qp;
1043 rp->ncrosslinks++;
1047 /* now we have two circular lists with crosslinks */
1048 if (itrace)
1049 (void) fprintf(stderr, "Name matches are done...\n");
1051 for (qp = heads[0]; qp; qp = qp->next) {
1052 if (qp->ncrosslinks > 1) {
1053 (void) fprintf(stderr,
1054 "%s in file 1 (%s) has %d matches in file 2 (%s):\n",
1055 _nc_first_name(qp->tterm.term_names),
1056 argv[0],
1057 qp->ncrosslinks,
1058 argv[1]);
1059 for (i = 0; i < qp->ncrosslinks; i++)
1060 (void) fprintf(stderr,
1061 "\t%s\n",
1062 _nc_first_name((qp->crosslinks[i])->tterm.term_names));
1066 for (rp = heads[1]; rp; rp = rp->next) {
1067 if (rp->ncrosslinks > 1) {
1068 (void) fprintf(stderr,
1069 "%s in file 2 (%s) has %d matches in file 1 (%s):\n",
1070 _nc_first_name(rp->tterm.term_names),
1071 argv[1],
1072 rp->ncrosslinks,
1073 argv[0]);
1074 for (i = 0; i < rp->ncrosslinks; i++)
1075 (void) fprintf(stderr,
1076 "\t%s\n",
1077 _nc_first_name((rp->crosslinks[i])->tterm.term_names));
1081 (void) printf("In file 1 (%s) only:\n", argv[0]);
1082 for (qp = heads[0]; qp; qp = qp->next)
1083 if (qp->ncrosslinks == 0)
1084 (void) printf("\t%s\n",
1085 _nc_first_name(qp->tterm.term_names));
1087 (void) printf("In file 2 (%s) only:\n", argv[1]);
1088 for (rp = heads[1]; rp; rp = rp->next)
1089 if (rp->ncrosslinks == 0)
1090 (void) printf("\t%s\n",
1091 _nc_first_name(rp->tterm.term_names));
1093 (void) printf("The following entries are equivalent:\n");
1094 for (qp = heads[0]; qp; qp = qp->next) {
1095 if (qp->ncrosslinks == 1) {
1096 rp = qp->crosslinks[0];
1098 repair_acsc(&qp->tterm);
1099 repair_acsc(&rp->tterm);
1100 #if NCURSES_XNAMES
1101 _nc_align_termtype(&qp->tterm, &rp->tterm);
1102 #endif
1103 if (entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp)) {
1104 char name1[NAMESIZE], name2[NAMESIZE];
1106 (void) canonical_name(qp->tterm.term_names, name1);
1107 (void) canonical_name(rp->tterm.term_names, name2);
1109 (void) printf("%s = %s\n", name1, name2);
1114 (void) printf("Differing entries:\n");
1115 termcount = 2;
1116 for (qp = heads[0]; qp; qp = qp->next) {
1118 if (qp->ncrosslinks == 1) {
1119 rp = qp->crosslinks[0];
1120 #if NCURSES_XNAMES
1121 /* sorry - we have to do this on each pass */
1122 _nc_align_termtype(&qp->tterm, &rp->tterm);
1123 #endif
1124 if (!(entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp))) {
1125 char name1[NAMESIZE], name2[NAMESIZE];
1126 char *names[3];
1128 names[0] = name1;
1129 names[1] = name2;
1130 names[2] = 0;
1132 entries[0] = *qp;
1133 entries[1] = *rp;
1135 (void) canonical_name(qp->tterm.term_names, name1);
1136 (void) canonical_name(rp->tterm.term_names, name2);
1138 switch (compare) {
1139 case C_DIFFERENCE:
1140 show_comparing(names);
1141 compare_entry(compare_predicate, &entries->tterm, quiet);
1142 break;
1144 case C_COMMON:
1145 show_comparing(names);
1146 compare_entry(compare_predicate, &entries->tterm, quiet);
1147 break;
1149 case C_NAND:
1150 show_comparing(names);
1151 compare_entry(compare_predicate, &entries->tterm, quiet);
1152 break;
1160 static void
1161 usage(void)
1163 #define DATA(s) s "\n"
1164 static const char head[] =
1166 DATA("Usage: infocmp [options] [-A directory] [-B directory] [termname...]")
1167 DATA("")
1168 DATA("Options:")
1170 #undef DATA
1171 #define DATA(s) s
1172 static const char options[][45] =
1174 " -0 print single-row"
1175 ," -1 print single-column"
1176 ," -K use termcap-names and BSD syntax"
1177 ," -C use termcap-names"
1178 ," -F compare terminfo-files"
1179 ," -I use terminfo-names"
1180 ," -L use long names"
1181 ," -R subset (see manpage)"
1182 ," -T eliminate size limits (test)"
1183 ," -U do not post-process entries"
1184 ," -D print database locations"
1185 ," -V print version"
1186 #if NCURSES_XNAMES
1187 ," -a with -F, list commented-out caps"
1188 #endif
1189 ," -c list common capabilities"
1190 ," -d list different capabilities"
1191 ," -e format output for C initializer"
1192 ," -E format output as C tables"
1193 ," -f with -1, format complex strings"
1194 ," -G format %{number} to %'char'"
1195 ," -g format %'char' to %{number}"
1196 ," -i analyze initialization/reset"
1197 ," -l output terminfo names"
1198 ," -n list capabilities in neither"
1199 ," -p ignore padding specifiers"
1200 ," -q brief listing, removes headers"
1201 ," -r with -C, output in termcap form"
1202 ," -r with -F, resolve use-references"
1203 ," -s [d|i|l|c] sort fields"
1204 #if NCURSES_XNAMES
1205 ," -t suppress commented-out capabilities"
1206 #endif
1207 ," -u produce source with 'use='"
1208 ," -v number (verbose)"
1209 ," -w number (width)"
1210 #if NCURSES_XNAMES
1211 ," -x unknown capabilities are user-defined"
1212 #endif
1214 #undef DATA
1215 const size_t last = SIZEOF(options);
1216 const size_t left = (last + 1) / 2;
1217 size_t n;
1219 fputs(head, stderr);
1220 for (n = 0; n < left; n++) {
1221 size_t m = n + left;
1222 if (m < last)
1223 fprintf(stderr, "%-40.40s%s\n", options[n], options[m]);
1224 else
1225 fprintf(stderr, "%s\n", options[n]);
1227 ExitProgram(EXIT_FAILURE);
1230 static char *
1231 any_initializer(const char *fmt, const char *type)
1233 static char *initializer;
1234 static size_t need;
1235 char *s;
1237 if (initializer == 0) {
1238 need = (strlen(entries->tterm.term_names)
1239 + strlen(type)
1240 + strlen(fmt));
1241 initializer = (char *) malloc(need + 1);
1242 if (initializer == 0)
1243 failed("any_initializer");
1246 _nc_STRCPY(initializer, entries->tterm.term_names, need);
1247 for (s = initializer; *s != 0 && *s != '|'; s++) {
1248 if (!isalnum(UChar(*s)))
1249 *s = '_';
1251 *s = 0;
1252 _nc_SPRINTF(s, _nc_SLIMIT(need) fmt, type);
1253 return initializer;
1256 static char *
1257 name_initializer(const char *type)
1259 return any_initializer("_%s_data", type);
1262 static char *
1263 string_variable(const char *type)
1265 return any_initializer("_s_%s", type);
1268 /* dump C initializers for the terminal type */
1269 static void
1270 dump_initializers(TERMTYPE *term)
1272 unsigned n;
1273 const char *str = 0;
1275 printf("\nstatic char %s[] = \"%s\";\n\n",
1276 name_initializer("alias"), entries->tterm.term_names);
1278 for_each_string(n, term) {
1279 char buf[MAX_STRING], *sp, *tp;
1281 if (VALID_STRING(term->Strings[n])) {
1282 tp = buf;
1283 #define TP_LIMIT ((MAX_STRING - 5) - (size_t)(tp - buf))
1284 *tp++ = '"';
1285 for (sp = term->Strings[n];
1286 *sp != 0 && TP_LIMIT > 2;
1287 sp++) {
1288 if (isascii(UChar(*sp))
1289 && isprint(UChar(*sp))
1290 && *sp != '\\'
1291 && *sp != '"')
1292 *tp++ = *sp;
1293 else {
1294 _nc_SPRINTF(tp, _nc_SLIMIT(TP_LIMIT) "\\%03o", UChar(*sp));
1295 tp += 4;
1298 *tp++ = '"';
1299 *tp = '\0';
1300 (void) printf("static char %-20s[] = %s;\n",
1301 string_variable(ExtStrname(term, (int) n, strnames)),
1302 buf);
1305 printf("\n");
1307 (void) printf("static char %s[] = %s\n", name_initializer("bool"), L_CURL);
1309 for_each_boolean(n, term) {
1310 switch ((int) (term->Booleans[n])) {
1311 case TRUE:
1312 str = "TRUE";
1313 break;
1315 case FALSE:
1316 str = "FALSE";
1317 break;
1319 case ABSENT_BOOLEAN:
1320 str = "ABSENT_BOOLEAN";
1321 break;
1323 case CANCELLED_BOOLEAN:
1324 str = "CANCELLED_BOOLEAN";
1325 break;
1327 (void) printf("\t/* %3u: %-8s */\t%s,\n",
1328 n, ExtBoolname(term, (int) n, boolnames), str);
1330 (void) printf("%s;\n", R_CURL);
1332 (void) printf("static short %s[] = %s\n", name_initializer("number"), L_CURL);
1334 for_each_number(n, term) {
1335 char buf[BUFSIZ];
1336 switch (term->Numbers[n]) {
1337 case ABSENT_NUMERIC:
1338 str = "ABSENT_NUMERIC";
1339 break;
1340 case CANCELLED_NUMERIC:
1341 str = "CANCELLED_NUMERIC";
1342 break;
1343 default:
1344 _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf)) "%d", term->Numbers[n]);
1345 str = buf;
1346 break;
1348 (void) printf("\t/* %3u: %-8s */\t%s,\n", n,
1349 ExtNumname(term, (int) n, numnames), str);
1351 (void) printf("%s;\n", R_CURL);
1353 (void) printf("static char * %s[] = %s\n", name_initializer("string"), L_CURL);
1355 for_each_string(n, term) {
1357 if (term->Strings[n] == ABSENT_STRING)
1358 str = "ABSENT_STRING";
1359 else if (term->Strings[n] == CANCELLED_STRING)
1360 str = "CANCELLED_STRING";
1361 else {
1362 str = string_variable(ExtStrname(term, (int) n, strnames));
1364 (void) printf("\t/* %3u: %-8s */\t%s,\n", n,
1365 ExtStrname(term, (int) n, strnames), str);
1367 (void) printf("%s;\n", R_CURL);
1369 #if NCURSES_XNAMES
1370 if ((NUM_BOOLEANS(term) != BOOLCOUNT)
1371 || (NUM_NUMBERS(term) != NUMCOUNT)
1372 || (NUM_STRINGS(term) != STRCOUNT)) {
1373 (void) printf("static char * %s[] = %s\n",
1374 name_initializer("string_ext"), L_CURL);
1375 for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
1376 (void) printf("\t/* %3u: bool */\t\"%s\",\n",
1377 n, ExtBoolname(term, (int) n, boolnames));
1379 for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
1380 (void) printf("\t/* %3u: num */\t\"%s\",\n",
1381 n, ExtNumname(term, (int) n, numnames));
1383 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
1384 (void) printf("\t/* %3u: str */\t\"%s\",\n",
1385 n, ExtStrname(term, (int) n, strnames));
1387 (void) printf("%s;\n", R_CURL);
1389 #endif
1392 /* dump C initializers for the terminal type */
1393 static void
1394 dump_termtype(TERMTYPE *term)
1396 (void) printf("\t%s\n\t\t%s,\n", L_CURL, name_initializer("alias"));
1397 (void) printf("\t\t(char *)0,\t/* pointer to string table */\n");
1399 (void) printf("\t\t%s,\n", name_initializer("bool"));
1400 (void) printf("\t\t%s,\n", name_initializer("number"));
1402 (void) printf("\t\t%s,\n", name_initializer("string"));
1404 #if NCURSES_XNAMES
1405 (void) printf("#if NCURSES_XNAMES\n");
1406 (void) printf("\t\t(char *)0,\t/* pointer to extended string table */\n");
1407 (void) printf("\t\t%s,\t/* ...corresponding names */\n",
1408 ((NUM_BOOLEANS(term) != BOOLCOUNT)
1409 || (NUM_NUMBERS(term) != NUMCOUNT)
1410 || (NUM_STRINGS(term) != STRCOUNT))
1411 ? name_initializer("string_ext")
1412 : "(char **)0");
1414 (void) printf("\t\t%d,\t\t/* count total Booleans */\n", NUM_BOOLEANS(term));
1415 (void) printf("\t\t%d,\t\t/* count total Numbers */\n", NUM_NUMBERS(term));
1416 (void) printf("\t\t%d,\t\t/* count total Strings */\n", NUM_STRINGS(term));
1418 (void) printf("\t\t%d,\t\t/* count extensions to Booleans */\n",
1419 NUM_BOOLEANS(term) - BOOLCOUNT);
1420 (void) printf("\t\t%d,\t\t/* count extensions to Numbers */\n",
1421 NUM_NUMBERS(term) - NUMCOUNT);
1422 (void) printf("\t\t%d,\t\t/* count extensions to Strings */\n",
1423 NUM_STRINGS(term) - STRCOUNT);
1425 (void) printf("#endif /* NCURSES_XNAMES */\n");
1426 #else
1427 (void) term;
1428 #endif /* NCURSES_XNAMES */
1429 (void) printf("\t%s\n", R_CURL);
1432 static int
1433 optarg_to_number(void)
1435 char *temp = 0;
1436 long value = strtol(optarg, &temp, 0);
1438 if (temp == 0 || temp == optarg || *temp != 0) {
1439 fprintf(stderr, "Expected a number, not \"%s\"\n", optarg);
1440 ExitProgram(EXIT_FAILURE);
1442 return (int) value;
1445 static char *
1446 terminal_env(void)
1448 char *terminal;
1450 if ((terminal = getenv("TERM")) == 0) {
1451 (void) fprintf(stderr,
1452 "%s: environment variable TERM not set\n",
1453 _nc_progname);
1454 exit(EXIT_FAILURE);
1456 return terminal;
1460 * Show the databases that infocmp knows about. The location to which it writes is
1462 static void
1463 show_databases(void)
1465 DBDIRS state;
1466 int offset;
1467 const char *path2;
1469 _nc_first_db(&state, &offset);
1470 while ((path2 = _nc_next_db(&state, &offset)) != 0) {
1471 printf("%s\n", path2);
1473 _nc_last_db();
1476 /***************************************************************************
1478 * Main sequence
1480 ***************************************************************************/
1482 #if NO_LEAKS
1483 #define MAIN_LEAKS() \
1484 free(myargv); \
1485 free(tfile); \
1486 free(tname)
1487 #else
1488 #define MAIN_LEAKS() /* nothing */
1489 #endif
1492 main(int argc, char *argv[])
1494 /* Avoid "local data >32k" error with mwcc */
1495 /* Also avoid overflowing smaller stacks on systems like AmigaOS */
1496 path *tfile = 0;
1497 char **tname = 0;
1498 size_t maxterms;
1500 char **myargv;
1502 char *firstdir, *restdir;
1503 int c, i, len;
1504 bool formatted = FALSE;
1505 bool filecompare = FALSE;
1506 int initdump = 0;
1507 bool init_analyze = FALSE;
1508 bool suppress_untranslatable = FALSE;
1510 /* where is the terminfo database location going to default to? */
1511 restdir = firstdir = 0;
1513 #if NCURSES_XNAMES
1514 use_extended_names(FALSE);
1515 #endif
1516 _nc_strict_bsd = 0;
1518 _nc_progname = _nc_rootname(argv[0]);
1520 /* make sure we have enough space to add two terminal entries */
1521 myargv = typeCalloc(char *, (size_t) (argc + 3));
1522 if (myargv == 0)
1523 failed("myargv");
1525 memcpy(myargv, argv, (sizeof(char *) * (size_t) argc));
1526 argv = myargv;
1528 while ((c = getopt(argc,
1529 argv,
1530 "01A:aB:CcDdEeFfGgIiKLlnpqR:rs:TtUuVv:w:x")) != -1) {
1531 switch (c) {
1532 case '0':
1533 mwidth = 65535;
1534 mheight = 1;
1535 break;
1537 case '1':
1538 mwidth = 0;
1539 break;
1541 case 'A':
1542 firstdir = optarg;
1543 break;
1545 #if NCURSES_XNAMES
1546 case 'a':
1547 _nc_disable_period = TRUE;
1548 use_extended_names(TRUE);
1549 break;
1550 #endif
1551 case 'B':
1552 restdir = optarg;
1553 break;
1555 case 'K':
1556 _nc_strict_bsd = 1;
1557 /* FALLTHRU */
1558 case 'C':
1559 outform = F_TERMCAP;
1560 tversion = "BSD";
1561 if (sortmode == S_DEFAULT)
1562 sortmode = S_TERMCAP;
1563 break;
1565 case 'D':
1566 show_databases();
1567 ExitProgram(EXIT_SUCCESS);
1568 break;
1570 case 'c':
1571 compare = C_COMMON;
1572 break;
1574 case 'd':
1575 compare = C_DIFFERENCE;
1576 break;
1578 case 'E':
1579 initdump |= 2;
1580 break;
1582 case 'e':
1583 initdump |= 1;
1584 break;
1586 case 'F':
1587 filecompare = TRUE;
1588 break;
1590 case 'f':
1591 formatted = TRUE;
1592 break;
1594 case 'G':
1595 numbers = 1;
1596 break;
1598 case 'g':
1599 numbers = -1;
1600 break;
1602 case 'I':
1603 outform = F_TERMINFO;
1604 if (sortmode == S_DEFAULT)
1605 sortmode = S_VARIABLE;
1606 tversion = 0;
1607 break;
1609 case 'i':
1610 init_analyze = TRUE;
1611 break;
1613 case 'L':
1614 outform = F_VARIABLE;
1615 if (sortmode == S_DEFAULT)
1616 sortmode = S_VARIABLE;
1617 break;
1619 case 'l':
1620 outform = F_TERMINFO;
1621 break;
1623 case 'n':
1624 compare = C_NAND;
1625 break;
1627 case 'p':
1628 ignorepads = TRUE;
1629 break;
1631 case 'q':
1632 quiet = TRUE;
1633 s_absent = "-";
1634 s_cancel = "@";
1635 bool_sep = ", ";
1636 break;
1638 case 'R':
1639 tversion = optarg;
1640 break;
1642 case 'r':
1643 tversion = 0;
1644 break;
1646 case 's':
1647 if (*optarg == 'd')
1648 sortmode = S_NOSORT;
1649 else if (*optarg == 'i')
1650 sortmode = S_TERMINFO;
1651 else if (*optarg == 'l')
1652 sortmode = S_VARIABLE;
1653 else if (*optarg == 'c')
1654 sortmode = S_TERMCAP;
1655 else {
1656 (void) fprintf(stderr,
1657 "%s: unknown sort mode\n",
1658 _nc_progname);
1659 ExitProgram(EXIT_FAILURE);
1661 break;
1663 case 'T':
1664 limited = FALSE;
1665 break;
1667 #if NCURSES_XNAMES
1668 case 't':
1669 _nc_disable_period = FALSE;
1670 suppress_untranslatable = TRUE;
1671 break;
1672 #endif
1674 case 'U':
1675 literal = TRUE;
1676 break;
1678 case 'u':
1679 compare = C_USEALL;
1680 break;
1682 case 'V':
1683 puts(curses_version());
1684 ExitProgram(EXIT_SUCCESS);
1686 case 'v':
1687 itrace = (unsigned) optarg_to_number();
1688 set_trace_level(itrace);
1689 break;
1691 case 'w':
1692 mwidth = optarg_to_number();
1693 break;
1695 #if NCURSES_XNAMES
1696 case 'x':
1697 use_extended_names(TRUE);
1698 break;
1699 #endif
1701 default:
1702 usage();
1706 maxterms = (size_t) (argc + 2 - optind);
1707 if ((tfile = typeMalloc(path, maxterms)) == 0)
1708 failed("tfile");
1709 if ((tname = typeCalloc(char *, maxterms)) == 0)
1710 failed("tname");
1711 if ((entries = typeCalloc(ENTRY, maxterms)) == 0)
1712 failed("entries");
1713 #if NO_LEAKS
1714 if ((entered = typeCalloc(ENTERED, maxterms)) == 0)
1715 failed("entered");
1716 #endif
1718 if (tfile == 0
1719 || tname == 0
1720 || entries == 0) {
1721 fprintf(stderr, "%s: not enough memory\n", _nc_progname);
1722 ExitProgram(EXIT_FAILURE);
1725 /* by default, sort by terminfo name */
1726 if (sortmode == S_DEFAULT)
1727 sortmode = S_TERMINFO;
1729 /* make sure we have at least one terminal name to work with */
1730 if (optind >= argc)
1731 argv[argc++] = terminal_env();
1733 /* if user is after a comparison, make sure we have two entries */
1734 if (compare != C_DEFAULT && optind >= argc - 1)
1735 argv[argc++] = terminal_env();
1737 /* exactly one terminal name with no options means display it */
1738 /* exactly two terminal names with no options means do -d */
1739 if (compare == C_DEFAULT) {
1740 switch (argc - optind) {
1741 default:
1742 fprintf(stderr, "%s: too many names to compare\n", _nc_progname);
1743 ExitProgram(EXIT_FAILURE);
1744 case 1:
1745 break;
1746 case 2:
1747 compare = C_DIFFERENCE;
1748 break;
1752 /* set up for display */
1753 dump_init(tversion, outform, sortmode, mwidth, mheight, itrace,
1754 formatted, FALSE);
1756 if (!filecompare) {
1757 /* grab the entries */
1758 termcount = 0;
1759 for (; optind < argc; optind++) {
1760 const char *directory = termcount ? restdir : firstdir;
1761 int status;
1763 tname[termcount] = argv[optind];
1765 if (directory) {
1766 #if NCURSES_USE_DATABASE
1767 #if MIXEDCASE_FILENAMES
1768 #define LEAF_FMT "%c"
1769 #else
1770 #define LEAF_FMT "%02x"
1771 #endif
1772 _nc_SPRINTF(tfile[termcount],
1773 _nc_SLIMIT(sizeof(path))
1774 "%s/" LEAF_FMT "/%s",
1775 directory,
1776 UChar(*argv[optind]), argv[optind]);
1777 if (itrace)
1778 (void) fprintf(stderr,
1779 "%s: reading entry %s from file %s\n",
1780 _nc_progname,
1781 argv[optind], tfile[termcount]);
1783 status = _nc_read_file_entry(tfile[termcount],
1784 &entries[termcount].tterm);
1785 #else
1786 (void) fprintf(stderr, "%s: terminfo files not supported\n",
1787 _nc_progname);
1788 MAIN_LEAKS();
1789 ExitProgram(EXIT_FAILURE);
1790 #endif
1791 } else {
1792 if (itrace)
1793 (void) fprintf(stderr,
1794 "%s: reading entry %s from database\n",
1795 _nc_progname,
1796 tname[termcount]);
1798 status = _nc_read_entry(tname[termcount],
1799 tfile[termcount],
1800 &entries[termcount].tterm);
1803 if (status <= 0) {
1804 (void) fprintf(stderr,
1805 "%s: couldn't open terminfo file %s.\n",
1806 _nc_progname,
1807 tfile[termcount]);
1808 MAIN_LEAKS();
1809 ExitProgram(EXIT_FAILURE);
1811 repair_acsc(&entries[termcount].tterm);
1812 termcount++;
1815 #if NCURSES_XNAMES
1816 if (termcount > 1)
1817 _nc_align_termtype(&entries[0].tterm, &entries[1].tterm);
1818 #endif
1820 /* dump as C initializer for the terminal type */
1821 if (initdump) {
1822 if (initdump & 1)
1823 dump_termtype(&entries[0].tterm);
1824 if (initdump & 2)
1825 dump_initializers(&entries[0].tterm);
1828 /* analyze the init strings */
1829 else if (init_analyze) {
1830 #undef CUR
1831 #define CUR entries[0].tterm.
1832 analyze_string("is1", init_1string, &entries[0].tterm);
1833 analyze_string("is2", init_2string, &entries[0].tterm);
1834 analyze_string("is3", init_3string, &entries[0].tterm);
1835 analyze_string("rs1", reset_1string, &entries[0].tterm);
1836 analyze_string("rs2", reset_2string, &entries[0].tterm);
1837 analyze_string("rs3", reset_3string, &entries[0].tterm);
1838 analyze_string("smcup", enter_ca_mode, &entries[0].tterm);
1839 analyze_string("rmcup", exit_ca_mode, &entries[0].tterm);
1840 analyze_string("smkx", keypad_xmit, &entries[0].tterm);
1841 analyze_string("rmkx", keypad_local, &entries[0].tterm);
1842 #undef CUR
1843 } else {
1846 * Here's where the real work gets done
1848 switch (compare) {
1849 case C_DEFAULT:
1850 if (itrace)
1851 (void) fprintf(stderr,
1852 "%s: about to dump %s\n",
1853 _nc_progname,
1854 tname[0]);
1855 (void) printf("#\tReconstructed via infocmp from file: %s\n",
1856 tfile[0]);
1857 dump_entry(&entries[0].tterm,
1858 suppress_untranslatable,
1859 limited,
1860 numbers,
1861 NULL);
1862 len = show_entry();
1863 if (itrace)
1864 (void) fprintf(stderr, "%s: length %d\n", _nc_progname, len);
1865 break;
1867 case C_DIFFERENCE:
1868 show_comparing(tname);
1869 compare_entry(compare_predicate, &entries->tterm, quiet);
1870 break;
1872 case C_COMMON:
1873 show_comparing(tname);
1874 compare_entry(compare_predicate, &entries->tterm, quiet);
1875 break;
1877 case C_NAND:
1878 show_comparing(tname);
1879 compare_entry(compare_predicate, &entries->tterm, quiet);
1880 break;
1882 case C_USEALL:
1883 if (itrace)
1884 (void) fprintf(stderr, "%s: dumping use entry\n", _nc_progname);
1885 dump_entry(&entries[0].tterm,
1886 suppress_untranslatable,
1887 limited,
1888 numbers,
1889 use_predicate);
1890 for (i = 1; i < termcount; i++)
1891 dump_uses(tname[i], !(outform == F_TERMCAP
1892 || outform == F_TCONVERR));
1893 len = show_entry();
1894 if (itrace)
1895 (void) fprintf(stderr, "%s: length %d\n", _nc_progname, len);
1896 break;
1899 } else if (compare == C_USEALL) {
1900 (void) fprintf(stderr, "Sorry, -u doesn't work with -F\n");
1901 } else if (compare == C_DEFAULT) {
1902 (void) fprintf(stderr, "Use `tic -[CI] <file>' for this.\n");
1903 } else if (argc - optind != 2) {
1904 (void) fprintf(stderr,
1905 "File comparison needs exactly two file arguments.\n");
1906 } else {
1907 file_comparison(argc - optind, argv + optind);
1910 MAIN_LEAKS();
1911 ExitProgram(EXIT_SUCCESS);
1914 /* infocmp.c ends here */