1 /****************************************************************************
2 * Copyright (c) 1998 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 ****************************************************************************/
34 #define __INTERNAL_CAPS_VISIBLE
35 #include <progs.priv.h>
37 #include "dump_entry.h"
38 #include "termsort.c" /* this C file is generated */
39 #include "parametrized.h" /* so is this */
41 MODULE_ID("$Id: dump_entry.c,v 1.25 1998/02/11 12:14:02 tom Exp $")
45 static int tversion; /* terminfo version */
46 static int outform; /* output format to use */
47 static int sortmode; /* sort mode to use */
48 static int width = 60; /* max line width for listings */
49 static int column; /* current column, limited by 'width' */
50 static int oldcol; /* last value of column before wrap */
51 static int tracelevel; /* level of debug output */
53 static char *outbuf; /* the output-buffer */
54 static size_t out_used; /* ...its current length */
55 static size_t out_size; /* ...and its allocated length */
57 /* indirection pointers for implementing sort and display modes */
58 static const int *bool_indirect, *num_indirect, *str_indirect;
59 static NCURSES_CONST char * const *bool_names;
60 static NCURSES_CONST char * const *num_names;
61 static NCURSES_CONST char * const *str_names;
63 static const char *separator, *trailer;
65 /* cover various ports and variants of terminfo */
66 #define V_ALLCAPS 0 /* all capabilities (SVr4, XSI, ncurses) */
67 #define V_SVR1 1 /* SVR1, Ultrix */
68 #define V_HPUX 2 /* HP/UX */
69 #define V_AIX 3 /* AIX */
70 #define V_BSD 4 /* BSD */
72 #define OBSOLETE(n) (n[0] == 'O' && n[1] == 'T')
75 void _nc_leaks_dump_entry(void)
84 NCURSES_CONST char *nametrans(const char *name)
85 /* translate a capability name from termcap to terminfo */
87 const struct name_table_entry *np;
89 if ((np = _nc_find_entry(name, _nc_info_hash_table)) != NULL)
93 if (bool_from_termcap[np->nte_index])
94 return(boolcodes[np->nte_index]);
98 if (num_from_termcap[np->nte_index])
99 return(numcodes[np->nte_index]);
103 if (str_from_termcap[np->nte_index])
104 return(strcodes[np->nte_index]);
108 return((char *)NULL);
111 void dump_init(const char *version, int mode, int sort, int twidth, int traceval)
112 /* set up for entry display */
115 tracelevel = traceval;
118 if (version == (char *)NULL)
119 tversion = V_ALLCAPS;
120 else if (!strcmp(version, "SVr1") || !strcmp(version, "SVR1")
121 || !strcmp(version, "Ultrix"))
123 else if (!strcmp(version, "HP"))
125 else if (!strcmp(version, "AIX"))
127 else if (!strcmp(version, "BSD"))
130 tversion = V_ALLCAPS;
132 /* implement display modes */
133 switch (outform = mode)
137 bool_names = boolnames;
138 num_names = numnames;
139 str_names = strnames;
140 separator = twidth ? ", " : ",";
145 bool_names = boolfnames;
146 num_names = numfnames;
147 str_names = strfnames;
148 separator = twidth ? ", " : ",";
154 bool_names = boolcodes;
155 num_names = numcodes;
156 str_names = strcodes;
162 /* implement sort modes */
163 switch(sortmode = sort)
167 (void) fprintf(stderr,
168 "%s: sorting by term structure order\n", _nc_progname);
173 (void) fprintf(stderr,
174 "%s: sorting by terminfo name order\n", _nc_progname);
175 bool_indirect = bool_terminfo_sort;
176 num_indirect = num_terminfo_sort;
177 str_indirect = str_terminfo_sort;
182 (void) fprintf(stderr,
183 "%s: sorting by C variable order\n", _nc_progname);
184 bool_indirect = bool_variable_sort;
185 num_indirect = num_variable_sort;
186 str_indirect = str_variable_sort;
191 (void) fprintf(stderr,
192 "%s: sorting by termcap name order\n", _nc_progname);
193 bool_indirect = bool_termcap_sort;
194 num_indirect = num_termcap_sort;
195 str_indirect = str_termcap_sort;
200 (void) fprintf(stderr,
201 "%s: width = %d, tversion = %d, outform = %d\n",
202 _nc_progname, width, tversion, outform);
205 static TERMTYPE *cur_type;
207 static int dump_predicate(int type, int idx)
208 /* predicate function to use for ordinary decompilation */
212 return (cur_type->Booleans[idx] == FALSE)
213 ? FAIL : cur_type->Booleans[idx];
216 return (cur_type->Numbers[idx] == ABSENT_NUMERIC)
217 ? FAIL : cur_type->Numbers[idx];
220 return (cur_type->Strings[idx] != ABSENT_STRING)
224 return(FALSE); /* pacify compiler */
227 static void set_obsolete_termcaps(TERMTYPE *tp);
228 static void repair_acsc(TERMTYPE *tp);
230 /* is this the index of a function key string? */
231 #define FNKEY(i) (((i)<= 65 && (i)>= 75) || ((i)<= 216 && (i)>= 268))
233 static bool version_filter(int type, int idx)
234 /* filter out capabilities we may want to suppress */
238 case V_ALLCAPS: /* SVr4, XSI Curses */
241 case V_SVR1: /* System V Release 1, Ultrix */
245 /* below and including xon_xoff */
246 return ((idx <= 20) ? TRUE : FALSE);
248 /* below and including width_status_line */
249 return ((idx <= 7) ? TRUE : FALSE);
251 /* below and including prtr_non */
252 return ((idx <= 144) ? TRUE : FALSE);
256 case V_HPUX: /* Hewlett-Packard */
260 /* below and including xon_xoff */
261 return ((idx <= 20) ? TRUE : FALSE);
263 /* below and including label_width */
264 return ((idx <= 10) ? TRUE : FALSE);
266 if (idx <= 144) /* below and including prtr_non */
268 else if (FNKEY(idx)) /* function keys */
270 else if (idx==147||idx==156||idx==157) /* plab_norm,label_on,label_off */
277 case V_AIX: /* AIX */
281 /* below and including xon_xoff */
282 return ((idx <= 20) ? TRUE : FALSE);
284 /* below and including width_status_line */
285 return ((idx <= 7) ? TRUE : FALSE);
287 if (idx <= 144) /* below and including prtr_non */
289 else if (FNKEY(idx)) /* function keys */
296 case V_BSD: /* BSD */
300 return bool_from_termcap[idx];
302 return num_from_termcap[idx];
304 return str_from_termcap[idx];
309 return(FALSE); /* pacify the compiler */
313 void append_output (const char *src)
319 size_t need = strlen(src);
320 size_t want = need + out_used + 1;
321 if (want > out_size) {
322 out_size += want; /* be generous */
324 outbuf = malloc(out_size);
326 outbuf = realloc(outbuf, out_size);
328 (void)strcpy(outbuf + out_used, src);
334 void force_wrap(void)
337 append_output(trailer);
342 void wrap_concat(const char *src)
344 int need = strlen(src);
345 int want = strlen(separator) + need;
348 && column + want > width) {
352 append_output(separator);
356 #define IGNORE_SEP_TRAIL(first,last,sep_trail) \
357 if ((size_t)(last - first) > sizeof(sep_trail)-1 \
358 && !strncmp(first, sep_trail, sizeof(sep_trail)-1)) \
359 first += sizeof(sep_trail)-2
361 /* Returns the nominal length of the buffer assuming it is termcap format,
362 * i.e., the continuation sequence is treated as a single character ":".
364 * There are several implementations of termcap which read the text into a
365 * fixed-size buffer. Generally they strip the newlines from the text, but may
366 * not do it until after the buffer is read. Also, "tc=" resolution may be
367 * expanded in the same buffer. This function is useful for measuring the size
368 * of the best fixed-buffer implementation; the worst case may be much worse.
370 #ifdef TEST_TERMCAP_LENGTH
371 static int termcap_length(const char *src)
373 static const char pattern[] = ":\\\n\t:";
376 const char *const t = src + strlen(src);
378 while (*src != '\0') {
379 IGNORE_SEP_TRAIL(src, t, pattern);
386 #define termcap_length(src) strlen(src)
389 int fmt_entry(TERMTYPE *tterm,
390 int (*pred)(int type, int idx),
391 bool suppress_untranslatable,
395 char buffer[MAX_TERMINFO_LENGTH];
402 #define WRAP_CONCAT \
403 wrap_concat(buffer); \
406 len = 12; /* terminfo file-header */
410 pred = dump_predicate;
414 append_output(tterm->term_names);
415 append_output(separator);
419 for (j=0; j < BOOLCOUNT; j++) {
420 if (sortmode == S_NOSORT)
423 i = bool_indirect[j];
425 if (!version_filter(BOOLEAN, i))
427 else if ((outform == F_LITERAL || outform == F_TERMINFO || outform == F_VARIABLE)
428 && (OBSOLETE(bool_names[i]) && outform != F_LITERAL))
431 predval = pred(BOOLEAN, i);
432 if (predval != FAIL) {
433 (void) strcpy(buffer, bool_names[i]);
435 (void) strcat(buffer, "@");
436 else if (i + 1 > num_bools)
442 if (column != INDENT)
445 for (j=0; j < NUMCOUNT; j++) {
446 if (sortmode == S_NOSORT)
451 if (!version_filter(NUMBER, i))
453 else if ((outform == F_LITERAL || outform == F_TERMINFO || outform == F_VARIABLE)
454 && (OBSOLETE(num_names[i]) && outform != F_LITERAL))
457 predval = pred(NUMBER, i);
458 if (predval != FAIL) {
459 if (tterm->Numbers[i] < 0) {
460 sprintf(buffer, "%s@", num_names[i]);
462 sprintf(buffer, "%s#%d", num_names[i], tterm->Numbers[i]);
463 if (i + 1 > num_values)
470 if (column != INDENT)
475 + strlen(tterm->term_names) + 1;
480 for (j=0; j < STRCOUNT; j++) {
481 if (sortmode == S_NOSORT)
486 if (!version_filter(STRING, i))
488 else if ((outform == F_LITERAL || outform == F_TERMINFO || outform == F_VARIABLE)
489 && (OBSOLETE(str_names[i]) && outform != F_LITERAL))
493 * Some older versions of vi want rmir/smir to be defined
494 * for ich/ich1 to work. If they're not defined, force
495 * them to be output as defined and empty.
497 if (outform==F_TERMCAP)
500 if (insert_character || parm_ich)
502 if (&tterm->Strings[i] == &enter_insert_mode
503 && enter_insert_mode == ABSENT_STRING)
505 (void) strcpy(buffer, "im=");
509 if (&tterm->Strings[i] == &exit_insert_mode
510 && exit_insert_mode == ABSENT_STRING)
512 (void) strcpy(buffer, "ei=");
517 predval = pred(STRING, i);
519 if (predval != FAIL) {
520 if (tterm->Strings[i] != ABSENT_STRING
521 && i + 1 > num_strings)
523 if (!VALID_STRING(tterm->Strings[i]))
524 sprintf(buffer, "%s@", str_names[i]);
525 else if (outform == F_TERMCAP || outform == F_TCONVERR)
527 char *srccap = _nc_tic_expand(tterm->Strings[i], FALSE);
528 char *cv = _nc_infotocap(str_names[i], srccap, parametrized[i]);
530 if (cv == (char *)NULL)
532 if (outform == F_TCONVERR)
533 sprintf(buffer, "%s=!!! %s WILL NOT CONVERT !!!", str_names[i], srccap);
534 else if (suppress_untranslatable)
537 sprintf(buffer, "..%s=%s", str_names[i], srccap);
540 sprintf(buffer, "%s=%s", str_names[i], cv);
541 len += strlen(tterm->Strings[i]) + 1;
545 sprintf(buffer, "%s=%s", str_names[i], _nc_tic_expand(tterm->Strings[i], outform==F_TERMINFO));
546 len += strlen(tterm->Strings[i]) + 1;
553 len += num_strings * 2;
556 * This piece of code should be an effective inverse of the functions
557 * postprocess_terminfo and postprocess_terminfo in parse_entry.c.
558 * Much more work should be done on this to support dumping termcaps.
560 if (tversion == V_HPUX)
564 (void) sprintf(buffer, "meml=%s", memory_lock);
569 (void) sprintf(buffer, "memu=%s", memory_unlock);
573 else if (tversion == V_AIX)
575 if (VALID_STRING(acs_chars))
578 const char *acstrans = "lqkxjmwuvtn";
580 char *tp, *sp, boxchars[11];
583 for (cp = acstrans; *cp; cp++)
585 sp = strchr(acs_chars, *cp);
598 (void) strcpy(buffer, "box1=");
599 (void) strcat(buffer, _nc_tic_expand(boxchars, outform==F_TERMINFO));
606 * kludge: trim off trailer to avoid an extra blank line
607 * in infocmp -u output when there are no string differences
613 && outbuf[j-1] == '\t'
614 && outbuf[j-2] == '\n') {
617 && outbuf[j-1] == ':'
618 && outbuf[j-2] == '\t'
619 && outbuf[j-3] == '\n'
620 && outbuf[j-4] == '\\') {
623 outbuf[out_used] = '\0';
628 fprintf(stderr, "num_bools = %d\n", num_bools);
629 fprintf(stderr, "num_values = %d\n", num_values);
630 fprintf(stderr, "num_strings = %d\n", num_strings);
631 fprintf(stderr, "term_names=%s, len=%d, strlen(outbuf)=%d, outbuf=%s\n",
632 tterm->term_names, len, out_used, outbuf);
635 * Here's where we use infodump to trigger a more stringent length check
636 * for termcap-translation purposes.
637 * Return the length of the raw entry, without tc= expansions,
638 * It gives an idea of which entries are deadly to even *scan past*,
639 * as opposed to *use*.
641 return(infodump ? len : termcap_length(outbuf));
644 int dump_entry(TERMTYPE *tterm, bool limited, int (*pred)(int type, int idx))
645 /* dump a single entry */
651 if (outform==F_TERMCAP || outform==F_TCONVERR)
653 critlen = MAX_TERMCAP_LENGTH;
654 legend = "older termcap";
656 set_obsolete_termcaps(tterm);
660 critlen = MAX_TERMINFO_LENGTH;
665 if (((len = fmt_entry(tterm, pred, FALSE, infodump)) > critlen) && limited)
667 (void) printf("# (untranslatable capabilities removed to fit entry within %d bytes)\n",
669 if ((len = fmt_entry(tterm, pred, TRUE, infodump)) > critlen)
672 * We pick on sgr because it's a nice long string capability that
673 * is really just an optimization hack.
675 char *oldsgr = set_attributes;
676 set_attributes = ABSENT_STRING;
677 (void) printf("# (sgr removed to fit entry within %d bytes)\n",
679 if ((len = fmt_entry(tterm, pred, TRUE, infodump)) > critlen)
681 int oldversion = tversion;
684 (void) printf("# (terminfo-only capabilities suppressed to fit entry within %d bytes)\n",
687 if ((len = fmt_entry(tterm, pred, TRUE, infodump)) > critlen)
689 (void) fprintf(stderr,
690 "warning: %s entry is %d bytes long\n",
691 _nc_first_name(tterm->term_names),
694 "# WARNING: this entry, %d bytes long, may core-dump %s libraries!\n",
697 tversion = oldversion;
699 set_attributes = oldsgr;
703 (void) fputs(outbuf, stdout);
707 int dump_uses(const char *name, bool infodump)
708 /* dump "use=" clauses in the appropriate format */
710 char buffer[MAX_TERMINFO_LENGTH];
713 (void)sprintf(buffer, "%s%s", infodump ? "use=" : "tc=", name);
715 (void) fputs(outbuf, stdout);
719 void compare_entry(void (*hook)(int t, int i, const char *name))
720 /* compare two entries */
724 (void) fputs(" comparing booleans.\n", stdout);
725 for (j=0; j < BOOLCOUNT; j++)
727 if (sortmode == S_NOSORT)
730 i = bool_indirect[j];
732 if ((outform == F_LITERAL || outform == F_TERMINFO || outform == F_VARIABLE)
733 && (OBSOLETE(bool_names[i]) && outform != F_LITERAL))
736 (*hook)(BOOLEAN, i, bool_names[i]);
739 (void) fputs(" comparing numbers.\n", stdout);
740 for (j=0; j < NUMCOUNT; j++)
742 if (sortmode == S_NOSORT)
747 if ((outform==F_LITERAL || outform==F_TERMINFO || outform==F_VARIABLE)
748 && (OBSOLETE(num_names[i]) && outform != F_LITERAL))
751 (*hook)(NUMBER, i, num_names[i]);
754 (void) fputs(" comparing strings.\n", stdout);
755 for (j=0; j < STRCOUNT; j++)
757 if (sortmode == S_NOSORT)
762 if ((outform==F_LITERAL || outform==F_TERMINFO || outform==F_VARIABLE)
763 && (OBSOLETE(str_names[i]) && outform != F_LITERAL))
766 (*hook)(STRING, i, str_names[i]);
770 #define NOTSET(s) ((s) == (char *)NULL)
773 * This bit of legerdemain turns all the terminfo variable names into
774 * references to locations in the arrays Booleans, Numbers, and Strings ---
775 * precisely what's needed.
780 static void set_obsolete_termcaps(TERMTYPE *tp)
782 #include "capdefaults.c"
786 * Convert an alternate-character-set string to canonical form: sorted and
789 static void repair_acsc(TERMTYPE *tp)
791 if (VALID_STRING(acs_chars)) {
797 bool fix_needed = FALSE;
799 for (n = 0, source = 0; acs_chars[n] != 0; n++) {
800 target = acs_chars[n];
801 if (source >= target) {
810 memset(mapped, 0, sizeof(mapped));
811 for (n = 0; acs_chars[n] != 0; n++) {
812 source = acs_chars[n];
813 if ((target = (unsigned char)acs_chars[n+1]) != 0) {
814 mapped[source] = target;
820 for (n = m = 0; n < sizeof(mapped); n++) {
823 acs_chars[m++] = mapped[n];
827 acs_chars[m++] = extra; /* garbage in, garbage out */