Annotation of src/usr.bin/tic/dump_entry.c, Revision 1.17
1.17 ! deraadt 1: /* $OpenBSD: dump_entry.c,v 1.16 2000/10/08 22:47:10 millert Exp $ */
1.2 millert 2:
1.1 millert 3: /****************************************************************************
1.11 millert 4: * Copyright (c) 1998-2000 Free Software Foundation, Inc. *
1.1 millert 5: * *
6: * Permission is hereby granted, free of charge, to any person obtaining a *
7: * copy of this software and associated documentation files (the *
8: * "Software"), to deal in the Software without restriction, including *
9: * without limitation the rights to use, copy, modify, merge, publish, *
10: * distribute, distribute with modifications, sublicense, and/or sell *
11: * copies of the Software, and to permit persons to whom the Software is *
12: * furnished to do so, subject to the following conditions: *
13: * *
14: * The above copyright notice and this permission notice shall be included *
15: * in all copies or substantial portions of the Software. *
16: * *
17: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
18: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
19: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
20: * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
21: * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
22: * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
23: * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24: * *
25: * Except as contained in this notice, the name(s) of the above copyright *
26: * holders shall not be used in advertising or otherwise to promote the *
27: * sale, use or other dealings in this Software without prior written *
28: * authorization. *
29: ****************************************************************************/
30:
31: /****************************************************************************
32: * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
33: * and: Eric S. Raymond <esr@snark.thyrsus.com> *
34: ****************************************************************************/
35:
36: #define __INTERNAL_CAPS_VISIBLE
37: #include <progs.priv.h>
38:
39: #include "dump_entry.h"
40: #include <termsort.c> /* this C file is generated */
41: #include <parametrized.h> /* so is this */
42:
1.16 millert 43: MODULE_ID("$From: dump_entry.c,v 1.54 2000/10/01 01:34:06 tom Exp $")
1.11 millert 44:
1.1 millert 45: #define INDENT 8
1.2 millert 46: #define DISCARD(string) string = ABSENT_STRING
1.11 millert 47: #define PRINTF (void) printf
1.2 millert 48:
1.15 millert 49: typedef struct {
50: char *text;
51: size_t used;
52: size_t size;
53: } DYNBUF;
54:
1.11 millert 55: static int tversion; /* terminfo version */
56: static int outform; /* output format to use */
57: static int sortmode; /* sort mode to use */
58: static int width = 60; /* max line width for listings */
59: static int column; /* current column, limited by 'width' */
60: static int oldcol; /* last value of column before wrap */
61: static int tracelevel; /* level of debug output */
62: static bool pretty; /* true if we format if-then-else strings */
63:
1.15 millert 64: static DYNBUF outbuf;
65: static DYNBUF tmpbuf;
1.1 millert 66:
67: /* indirection pointers for implementing sort and display modes */
1.11 millert 68: static const int *bool_indirect, *num_indirect, *str_indirect;
69: static NCURSES_CONST char *const *bool_names;
70: static NCURSES_CONST char *const *num_names;
71: static NCURSES_CONST char *const *str_names;
1.1 millert 72:
1.11 millert 73: static const char *separator, *trailer;
1.1 millert 74:
75: /* cover various ports and variants of terminfo */
76: #define V_ALLCAPS 0 /* all capabilities (SVr4, XSI, ncurses) */
77: #define V_SVR1 1 /* SVR1, Ultrix */
78: #define V_HPUX 2 /* HP/UX */
79: #define V_AIX 3 /* AIX */
80: #define V_BSD 4 /* BSD */
81:
1.9 millert 82: #if NCURSES_XNAMES
83: #define OBSOLETE(n) (!_nc_user_definable && (n[0] == 'O' && n[1] == 'T'))
84: #else
1.1 millert 85: #define OBSOLETE(n) (n[0] == 'O' && n[1] == 'T')
1.9 millert 86: #endif
87:
88: #define isObsolete(f,n) ((f == F_TERMINFO || f == F_VARIABLE) && OBSOLETE(n))
1.1 millert 89:
1.4 millert 90: #if NCURSES_XNAMES
91: #define BoolIndirect(j) ((j >= BOOLCOUNT) ? (j) : ((sortmode == S_NOSORT) ? j : bool_indirect[j]))
92: #define NumIndirect(j) ((j >= NUMCOUNT) ? (j) : ((sortmode == S_NOSORT) ? j : num_indirect[j]))
93: #define StrIndirect(j) ((j >= STRCOUNT) ? (j) : ((sortmode == S_NOSORT) ? j : str_indirect[j]))
94: #else
95: #define BoolIndirect(j) ((sortmode == S_NOSORT) ? (j) : bool_indirect[j])
96: #define NumIndirect(j) ((sortmode == S_NOSORT) ? (j) : num_indirect[j])
97: #define StrIndirect(j) ((sortmode == S_NOSORT) ? (j) : str_indirect[j])
98: #endif
99:
1.15 millert 100: static void
101: strncpy_DYN(DYNBUF * dst, const char *src, size_t need)
102: {
103: size_t want = need + dst->used + 1;
104: if (want > dst->size) {
105: dst->size += (want + 1024); /* be generous */
106: dst->text = typeRealloc(char, dst->size, dst->text);
107: }
108: (void) strncpy(dst->text + dst->used, src, need);
109: dst->used += need;
110: dst->text[dst->used] = 0;
111: }
112:
113: static void
114: strcpy_DYN(DYNBUF * dst, const char *src)
115: {
116: if (src == 0) {
117: dst->used = 0;
118: strcpy_DYN(dst, "");
119: } else {
120: strncpy_DYN(dst, src, strlen(src));
121: }
122: }
123:
1.1 millert 124: #if NO_LEAKS
1.15 millert 125: static void
126: free_DYN(DYNBUF * p)
127: {
128: if (p->text != 0)
129: free(p->text);
130: p->text = 0;
131: p->size = 0;
132: p->used = 0;
133: }
134:
1.11 millert 135: void
136: _nc_leaks_dump_entry(void)
1.1 millert 137: {
1.15 millert 138: free_DYN(&outbuf);
139: free_DYN(&tmpbuf);
1.1 millert 140: }
141: #endif
142:
1.10 millert 143: NCURSES_CONST char *
144: nametrans(const char *name)
1.1 millert 145: /* translate a capability name from termcap to terminfo */
146: {
1.10 millert 147: const struct name_table_entry *np;
1.1 millert 148:
1.3 millert 149: if ((np = _nc_find_entry(name, _nc_get_hash_table(0))) != 0)
1.10 millert 150: switch (np->nte_type) {
1.1 millert 151: case BOOLEAN:
152: if (bool_from_termcap[np->nte_index])
1.10 millert 153: return (boolcodes[np->nte_index]);
1.1 millert 154: break;
155:
156: case NUMBER:
157: if (num_from_termcap[np->nte_index])
1.10 millert 158: return (numcodes[np->nte_index]);
1.1 millert 159: break;
160:
161: case STRING:
162: if (str_from_termcap[np->nte_index])
1.10 millert 163: return (strcodes[np->nte_index]);
1.1 millert 164: break;
165: }
166:
1.10 millert 167: return (0);
1.1 millert 168: }
169:
1.10 millert 170: void
171: dump_init(const char *version, int mode, int sort, int twidth, int traceval,
172: bool formatted)
1.1 millert 173: /* set up for entry display */
174: {
175: width = twidth;
176: pretty = formatted;
177: tracelevel = traceval;
178:
179: /* versions */
180: if (version == 0)
181: tversion = V_ALLCAPS;
182: else if (!strcmp(version, "SVr1") || !strcmp(version, "SVR1")
1.10 millert 183: || !strcmp(version, "Ultrix"))
1.1 millert 184: tversion = V_SVR1;
185: else if (!strcmp(version, "HP"))
186: tversion = V_HPUX;
187: else if (!strcmp(version, "AIX"))
188: tversion = V_AIX;
189: else if (!strcmp(version, "BSD"))
190: tversion = V_BSD;
191: else
192: tversion = V_ALLCAPS;
193:
194: /* implement display modes */
1.10 millert 195: switch (outform = mode) {
1.1 millert 196: case F_LITERAL:
197: case F_TERMINFO:
198: bool_names = boolnames;
199: num_names = numnames;
200: str_names = strnames;
201: separator = twidth ? ", " : ",";
202: trailer = "\n\t";
203: break;
204:
205: case F_VARIABLE:
206: bool_names = boolfnames;
207: num_names = numfnames;
208: str_names = strfnames;
209: separator = twidth ? ", " : ",";
210: trailer = "\n\t";
211: break;
212:
213: case F_TERMCAP:
214: case F_TCONVERR:
215: bool_names = boolcodes;
216: num_names = numcodes;
217: str_names = strcodes;
218: separator = ":";
219: trailer = "\\\n\t:";
220: break;
221: }
222:
223: /* implement sort modes */
1.10 millert 224: switch (sortmode = sort) {
1.1 millert 225: case S_NOSORT:
226: if (traceval)
227: (void) fprintf(stderr,
1.10 millert 228: "%s: sorting by term structure order\n", _nc_progname);
1.1 millert 229: break;
230:
231: case S_TERMINFO:
232: if (traceval)
233: (void) fprintf(stderr,
1.10 millert 234: "%s: sorting by terminfo name order\n", _nc_progname);
1.1 millert 235: bool_indirect = bool_terminfo_sort;
236: num_indirect = num_terminfo_sort;
237: str_indirect = str_terminfo_sort;
238: break;
239:
240: case S_VARIABLE:
241: if (traceval)
242: (void) fprintf(stderr,
1.10 millert 243: "%s: sorting by C variable order\n", _nc_progname);
1.1 millert 244: bool_indirect = bool_variable_sort;
245: num_indirect = num_variable_sort;
246: str_indirect = str_variable_sort;
247: break;
248:
249: case S_TERMCAP:
250: if (traceval)
251: (void) fprintf(stderr,
1.10 millert 252: "%s: sorting by termcap name order\n", _nc_progname);
1.1 millert 253: bool_indirect = bool_termcap_sort;
254: num_indirect = num_termcap_sort;
255: str_indirect = str_termcap_sort;
256: break;
257: }
258:
259: if (traceval)
260: (void) fprintf(stderr,
1.10 millert 261: "%s: width = %d, tversion = %d, outform = %d\n",
262: _nc_progname, width, tversion, outform);
1.1 millert 263: }
264:
1.10 millert 265: static TERMTYPE *cur_type;
1.1 millert 266:
1.10 millert 267: static int
268: dump_predicate(int type, int idx)
1.1 millert 269: /* predicate function to use for ordinary decompilation */
270: {
1.10 millert 271: switch (type) {
272: case BOOLEAN:
273: return (cur_type->Booleans[idx] == FALSE)
274: ? FAIL : cur_type->Booleans[idx];
1.1 millert 275:
1.10 millert 276: case NUMBER:
277: return (cur_type->Numbers[idx] == ABSENT_NUMERIC)
278: ? FAIL : cur_type->Numbers[idx];
1.1 millert 279:
1.10 millert 280: case STRING:
281: return (cur_type->Strings[idx] != ABSENT_STRING)
282: ? (int) TRUE : FAIL;
283: }
1.1 millert 284:
1.10 millert 285: return (FALSE); /* pacify compiler */
1.1 millert 286: }
287:
1.10 millert 288: static void set_obsolete_termcaps(TERMTYPE * tp);
1.1 millert 289:
290: /* is this the index of a function key string? */
291: #define FNKEY(i) (((i)<= 65 && (i)>= 75) || ((i)<= 216 && (i)>= 268))
292:
1.10 millert 293: static bool
294: version_filter(int type, int idx)
1.1 millert 295: /* filter out capabilities we may want to suppress */
296: {
1.10 millert 297: switch (tversion) {
298: case V_ALLCAPS: /* SVr4, XSI Curses */
299: return (TRUE);
300:
301: case V_SVR1: /* System V Release 1, Ultrix */
302: switch (type) {
1.1 millert 303: case BOOLEAN:
304: /* below and including xon_xoff */
305: return ((idx <= 20) ? TRUE : FALSE);
306: case NUMBER:
307: /* below and including width_status_line */
308: return ((idx <= 7) ? TRUE : FALSE);
309: case STRING:
310: /* below and including prtr_non */
311: return ((idx <= 144) ? TRUE : FALSE);
312: }
313: break;
314:
315: case V_HPUX: /* Hewlett-Packard */
1.10 millert 316: switch (type) {
1.1 millert 317: case BOOLEAN:
318: /* below and including xon_xoff */
319: return ((idx <= 20) ? TRUE : FALSE);
320: case NUMBER:
321: /* below and including label_width */
322: return ((idx <= 10) ? TRUE : FALSE);
323: case STRING:
324: if (idx <= 144) /* below and including prtr_non */
1.10 millert 325: return (TRUE);
1.1 millert 326: else if (FNKEY(idx)) /* function keys */
1.10 millert 327: return (TRUE);
328: else if (idx == 147 || idx == 156 || idx == 157) /* plab_norm,label_on,label_off */
329: return (TRUE);
1.1 millert 330: else
1.10 millert 331: return (FALSE);
1.1 millert 332: }
333: break;
334:
335: case V_AIX: /* AIX */
1.10 millert 336: switch (type) {
1.1 millert 337: case BOOLEAN:
338: /* below and including xon_xoff */
339: return ((idx <= 20) ? TRUE : FALSE);
340: case NUMBER:
341: /* below and including width_status_line */
342: return ((idx <= 7) ? TRUE : FALSE);
343: case STRING:
344: if (idx <= 144) /* below and including prtr_non */
1.10 millert 345: return (TRUE);
1.1 millert 346: else if (FNKEY(idx)) /* function keys */
1.10 millert 347: return (TRUE);
1.1 millert 348: else
1.10 millert 349: return (FALSE);
1.1 millert 350: }
351: break;
352:
353: case V_BSD: /* BSD */
1.10 millert 354: switch (type) {
1.1 millert 355: case BOOLEAN:
356: return bool_from_termcap[idx];
357: case NUMBER:
358: return num_from_termcap[idx];
359: case STRING:
360: return str_from_termcap[idx];
361: }
362: break;
363: }
364:
1.10 millert 365: return (FALSE); /* pacify the compiler */
1.1 millert 366: }
367:
1.11 millert 368: static void
1.10 millert 369: force_wrap(void)
1.1 millert 370: {
1.10 millert 371: oldcol = column;
1.15 millert 372: strcpy_DYN(&outbuf, trailer);
1.10 millert 373: column = INDENT;
1.1 millert 374: }
375:
1.11 millert 376: static void
1.10 millert 377: wrap_concat(const char *src)
1.1 millert 378: {
1.10 millert 379: int need = strlen(src);
380: int want = strlen(separator) + need;
1.1 millert 381:
1.10 millert 382: if (column > INDENT
383: && column + want > width) {
384: force_wrap();
385: }
1.15 millert 386: strcpy_DYN(&outbuf, src);
387: strcpy_DYN(&outbuf, separator);
1.10 millert 388: column += need;
1.1 millert 389: }
390:
391: #define IGNORE_SEP_TRAIL(first,last,sep_trail) \
392: if ((size_t)(last - first) > sizeof(sep_trail)-1 \
393: && !strncmp(first, sep_trail, sizeof(sep_trail)-1)) \
394: first += sizeof(sep_trail)-2
395:
396: /* Returns the nominal length of the buffer assuming it is termcap format,
397: * i.e., the continuation sequence is treated as a single character ":".
398: *
399: * There are several implementations of termcap which read the text into a
400: * fixed-size buffer. Generally they strip the newlines from the text, but may
401: * not do it until after the buffer is read. Also, "tc=" resolution may be
402: * expanded in the same buffer. This function is useful for measuring the size
403: * of the best fixed-buffer implementation; the worst case may be much worse.
404: */
405: #ifdef TEST_TERMCAP_LENGTH
1.10 millert 406: static int
407: termcap_length(const char *src)
1.1 millert 408: {
1.10 millert 409: static const char pattern[] = ":\\\n\t:";
1.1 millert 410:
1.10 millert 411: int len = 0;
412: const char *const t = src + strlen(src);
1.1 millert 413:
1.10 millert 414: while (*src != '\0') {
415: IGNORE_SEP_TRAIL(src, t, pattern);
416: src++;
417: len++;
418: }
419: return len;
1.1 millert 420: }
421: #else
422: #define termcap_length(src) strlen(src)
423: #endif
424:
1.10 millert 425: static char *
1.15 millert 426: fmt_complex(char *src, int level)
1.1 millert 427: {
1.10 millert 428: int percent = 0;
429: int n;
430: bool if_then = strstr(src, "%?") != 0;
431: bool params = !if_then && (strlen(src) > 50) && (strstr(src, "%p") != 0);
432:
433: while (*src != '\0') {
434: switch (*src) {
435: case '\\':
436: percent = 0;
1.15 millert 437: strncpy_DYN(&tmpbuf, src++, 1);
1.10 millert 438: break;
439: case '%':
440: percent = 1;
441: break;
442: case '?': /* "if" */
443: case 't': /* "then" */
444: case 'e': /* "else" */
445: if (percent) {
446: percent = 0;
1.15 millert 447: tmpbuf.text[tmpbuf.used - 1] = '\n';
448: /* treat a "%e%?" as else-if, on the same level */
449: if (!strncmp(src, "e%?", 3)) {
450: for (n = 0; n < level; n++)
451: strncpy_DYN(&tmpbuf, "\t", 1);
452: strncpy_DYN(&tmpbuf, "%", 1);
453: strncpy_DYN(&tmpbuf, src, 3);
454: src += 3;
455: } else {
456: for (n = 0; n <= level; n++)
457: strncpy_DYN(&tmpbuf, "\t", 1);
458: strncpy_DYN(&tmpbuf, "%", 1);
459: strncpy_DYN(&tmpbuf, src, 1);
460: if (*src++ == '?') {
461: src = fmt_complex(src, level + 1);
462: } else if (level == 1) {
463: _nc_warning("%%%c without %%?", *src);
464: }
1.10 millert 465: }
466: continue;
467: }
468: break;
469: case ';': /* "endif" */
470: if (percent) {
471: percent = 0;
472: if (level > 1) {
1.15 millert 473: tmpbuf.text[tmpbuf.used - 1] = '\n';
1.10 millert 474: for (n = 0; n < level; n++)
1.15 millert 475: strncpy_DYN(&tmpbuf, "\t", 1);
476: strncpy_DYN(&tmpbuf, "%", 1);
477: strncpy_DYN(&tmpbuf, src++, 1);
1.10 millert 478: return src;
1.1 millert 479: }
1.10 millert 480: _nc_warning("%%; without %%?");
481: }
482: break;
483: case 'p':
484: if (percent && params) {
1.15 millert 485: tmpbuf.text[tmpbuf.used - 1] = '\n';
1.10 millert 486: for (n = 0; n <= level; n++)
1.15 millert 487: strncpy_DYN(&tmpbuf, "\t", 1);
488: strncpy_DYN(&tmpbuf, "%", 1);
1.10 millert 489: }
490: percent = 0;
491: break;
492: default:
493: percent = 0;
494: break;
1.1 millert 495: }
1.15 millert 496: strncpy_DYN(&tmpbuf, src++, 1);
1.10 millert 497: }
498: return src;
1.1 millert 499: }
500:
1.10 millert 501: int
502: fmt_entry(TERMTYPE * tterm,
503: int (*pred) (int type, int idx),
504: bool suppress_untranslatable,
505: bool infodump,
506: int numbers)
507: {
508: int i, j;
509: char buffer[MAX_TERMINFO_LENGTH];
510: NCURSES_CONST char *name;
511: int predval, len;
512: int num_bools = 0;
513: int num_values = 0;
514: int num_strings = 0;
515: bool outcount = 0;
1.1 millert 516:
517: #define WRAP_CONCAT \
518: wrap_concat(buffer); \
519: outcount = TRUE
520:
521: len = 12; /* terminfo file-header */
522:
523: if (pred == 0) {
524: cur_type = tterm;
525: pred = dump_predicate;
526: }
527:
1.15 millert 528: strcpy_DYN(&outbuf, 0);
529: strcpy_DYN(&outbuf, tterm->term_names);
530: strcpy_DYN(&outbuf, separator);
531: column = outbuf.used;
1.1 millert 532: force_wrap();
533:
1.10 millert 534: for_each_boolean(j, tterm) {
1.4 millert 535: i = BoolIndirect(j);
1.10 millert 536: name = ExtBoolname(tterm, i, bool_names);
1.1 millert 537:
538: if (!version_filter(BOOLEAN, i))
539: continue;
1.10 millert 540: else if (isObsolete(outform, name))
1.1 millert 541: continue;
542:
543: predval = pred(BOOLEAN, i);
544: if (predval != FAIL) {
1.17 ! deraadt 545: (void) strlcpy(buffer, name, sizeof buffer);
1.1 millert 546: if (predval <= 0)
1.17 ! deraadt 547: (void) strlcat(buffer, "@", sizeof buffer);
1.1 millert 548: else if (i + 1 > num_bools)
549: num_bools = i + 1;
550: WRAP_CONCAT;
551: }
552: }
553:
554: if (column != INDENT)
555: force_wrap();
556:
1.10 millert 557: for_each_number(j, tterm) {
1.4 millert 558: i = NumIndirect(j);
1.10 millert 559: name = ExtNumname(tterm, i, num_names);
1.1 millert 560:
561: if (!version_filter(NUMBER, i))
562: continue;
1.10 millert 563: else if (isObsolete(outform, name))
1.1 millert 564: continue;
565:
566: predval = pred(NUMBER, i);
567: if (predval != FAIL) {
568: if (tterm->Numbers[i] < 0) {
1.17 ! deraadt 569: snprintf(buffer, sizeof buffer, "%s@", name);
1.1 millert 570: } else {
1.17 ! deraadt 571: snprintf(buffer, sizeof buffer, "%s#%d", name, tterm->Numbers[i]);
1.1 millert 572: if (i + 1 > num_values)
573: num_values = i + 1;
574: }
575: WRAP_CONCAT;
576: }
577: }
578:
579: if (column != INDENT)
580: force_wrap();
581:
582: len += num_bools
1.10 millert 583: + num_values * 2
584: + strlen(tterm->term_names) + 1;
1.1 millert 585: if (len & 1)
1.10 millert 586: len++;
1.1 millert 587:
1.15 millert 588: #undef CUR
589: #define CUR tterm->
590: if (outform == F_TERMCAP) {
591: if (termcap_reset != ABSENT_STRING) {
592: if (init_3string != ABSENT_STRING
593: && !strcmp(init_3string, termcap_reset))
594: DISCARD(init_3string);
595:
596: if (reset_2string != ABSENT_STRING
597: && !strcmp(reset_2string, termcap_reset))
598: DISCARD(reset_2string);
599: }
600: }
601:
1.4 millert 602: for_each_string(j, tterm) {
603: i = StrIndirect(j);
1.10 millert 604: name = ExtStrname(tterm, i, str_names);
1.1 millert 605:
606: if (!version_filter(STRING, i))
607: continue;
1.10 millert 608: else if (isObsolete(outform, name))
1.1 millert 609: continue;
610:
611: /*
612: * Some older versions of vi want rmir/smir to be defined
613: * for ich/ich1 to work. If they're not defined, force
614: * them to be output as defined and empty.
615: */
1.10 millert 616: if (outform == F_TERMCAP) {
617: if (insert_character || parm_ich) {
1.1 millert 618: if (&tterm->Strings[i] == &enter_insert_mode
1.10 millert 619: && enter_insert_mode == ABSENT_STRING) {
1.17 ! deraadt 620: (void) strlcpy(buffer, "im=", sizeof buffer);
1.15 millert 621: WRAP_CONCAT;
622: continue;
1.1 millert 623: }
624:
625: if (&tterm->Strings[i] == &exit_insert_mode
1.10 millert 626: && exit_insert_mode == ABSENT_STRING) {
1.17 ! deraadt 627: (void) strlcpy(buffer, "ei=", sizeof buffer);
1.15 millert 628: WRAP_CONCAT;
629: continue;
1.1 millert 630: }
631: }
1.2 millert 632: }
633:
1.1 millert 634: predval = pred(STRING, i);
635: buffer[0] = '\0';
1.15 millert 636:
1.1 millert 637: if (predval != FAIL) {
638: if (tterm->Strings[i] != ABSENT_STRING
1.10 millert 639: && i + 1 > num_strings)
1.1 millert 640: num_strings = i + 1;
1.15 millert 641:
642: if (!VALID_STRING(tterm->Strings[i])) {
1.17 ! deraadt 643: snprintf(buffer, sizeof buffer, "%s@", name);
1.15 millert 644: WRAP_CONCAT;
645: } else if (outform == F_TERMCAP || outform == F_TCONVERR) {
1.16 millert 646: int params = (i < (int) SIZEOF(parametrized)) ? parametrized[i] : 0;
1.9 millert 647: char *srccap = _nc_tic_expand(tterm->Strings[i], TRUE, numbers);
1.16 millert 648: char *cv = _nc_infotocap(name, srccap, params);
1.1 millert 649:
1.10 millert 650: if (cv == 0) {
1.15 millert 651: if (outform == F_TCONVERR) {
1.17 ! deraadt 652: snprintf(buffer, sizeof buffer,
! 653: "%s=!!! %s WILL NOT CONVERT !!!",
1.10 millert 654: name, srccap);
1.15 millert 655: } else if (suppress_untranslatable) {
1.1 millert 656: continue;
1.15 millert 657: } else {
1.10 millert 658: char *s = srccap, *d = buffer;
1.17 ! deraadt 659: snprintf(buffer, buffer + sizeof buffer - d, "..%s=",
! 660: name);
! 661: d = buffer + strlen(buffer);
! 662: while ((*d = *s++) != 0) { /* XXX overflow? */
1.10 millert 663: if (*d == ':') {
664: *d++ = '\\';
665: *d = ':';
666: } else if (*d == '\\') {
667: *++d = *s++;
668: }
669: d++;
670: }
671: }
1.15 millert 672: } else {
1.17 ! deraadt 673: snprintf(buffer, sizeof buffer, "%s=%s", name, cv);
1.15 millert 674: }
1.1 millert 675: len += strlen(tterm->Strings[i]) + 1;
1.15 millert 676: WRAP_CONCAT;
1.10 millert 677: } else {
678: char *src = _nc_tic_expand(tterm->Strings[i],
679: outform == F_TERMINFO, numbers);
1.15 millert 680:
681: strcpy_DYN(&tmpbuf, 0);
682: strcpy_DYN(&tmpbuf, name);
683: strcpy_DYN(&tmpbuf, "=");
1.8 millert 684: if (pretty
1.10 millert 685: && (outform == F_TERMINFO
1.15 millert 686: || outform == F_VARIABLE)) {
687: fmt_complex(src, 1);
688: } else {
689: strcpy_DYN(&tmpbuf, src);
690: }
1.1 millert 691: len += strlen(tterm->Strings[i]) + 1;
1.15 millert 692: wrap_concat(tmpbuf.text);
693: outcount = TRUE;
1.1 millert 694: }
695: }
696: }
697: len += num_strings * 2;
698:
699: /*
700: * This piece of code should be an effective inverse of the functions
701: * postprocess_terminfo and postprocess_terminfo in parse_entry.c.
702: * Much more work should be done on this to support dumping termcaps.
703: */
1.10 millert 704: if (tversion == V_HPUX) {
705: if (memory_lock) {
1.17 ! deraadt 706: (void) snprintf(buffer, sizeof buffer, "meml=%s", memory_lock);
1.1 millert 707: WRAP_CONCAT;
708: }
1.10 millert 709: if (memory_unlock) {
1.17 ! deraadt 710: (void) snprintf(buffer, sizeof buffer, "memu=%s", memory_unlock);
1.1 millert 711: WRAP_CONCAT;
712: }
1.10 millert 713: } else if (tversion == V_AIX) {
714: if (VALID_STRING(acs_chars)) {
715: bool box_ok = TRUE;
716: const char *acstrans = "lqkxjmwuvtn";
717: const char *cp;
718: char *tp, *sp, boxchars[11];
1.1 millert 719:
720: tp = boxchars;
1.10 millert 721: for (cp = acstrans; *cp; cp++) {
1.1 millert 722: sp = strchr(acs_chars, *cp);
723: if (sp)
724: *tp++ = sp[1];
1.10 millert 725: else {
1.1 millert 726: box_ok = FALSE;
727: break;
728: }
729: }
730: tp[0] = '\0';
731:
1.10 millert 732: if (box_ok) {
1.17 ! deraadt 733: (void) strlcpy(buffer, "box1=", sizeof buffer);
! 734: (void) strlcat(buffer, _nc_tic_expand(boxchars,
! 735: outform == F_TERMINFO, numbers), sizeof buffer);
1.1 millert 736: WRAP_CONCAT;
737: }
738: }
739: }
740:
741: /*
742: * kludge: trim off trailer to avoid an extra blank line
743: * in infocmp -u output when there are no string differences
744: */
1.10 millert 745: if (outcount) {
1.15 millert 746: bool trimmed = FALSE;
747: j = outbuf.used;
1.1 millert 748: if (j >= 2
1.15 millert 749: && outbuf.text[j - 1] == '\t'
750: && outbuf.text[j - 2] == '\n') {
751: outbuf.used -= 2;
752: trimmed = TRUE;
1.1 millert 753: } else if (j >= 4
1.15 millert 754: && outbuf.text[j - 1] == ':'
755: && outbuf.text[j - 2] == '\t'
756: && outbuf.text[j - 3] == '\n'
757: && outbuf.text[j - 4] == '\\') {
758: outbuf.used -= 4;
759: trimmed = TRUE;
760: }
761: if (trimmed) {
762: outbuf.text[outbuf.used] = '\0';
763: column = oldcol;
1.1 millert 764: }
765: }
766: #if 0
767: fprintf(stderr, "num_bools = %d\n", num_bools);
768: fprintf(stderr, "num_values = %d\n", num_values);
769: fprintf(stderr, "num_strings = %d\n", num_strings);
770: fprintf(stderr, "term_names=%s, len=%d, strlen(outbuf)=%d, outbuf=%s\n",
1.15 millert 771: tterm->term_names, len, outbuf.used, outbuf.text);
1.1 millert 772: #endif
773: /*
774: * Here's where we use infodump to trigger a more stringent length check
775: * for termcap-translation purposes.
776: * Return the length of the raw entry, without tc= expansions,
777: * It gives an idea of which entries are deadly to even *scan past*,
778: * as opposed to *use*.
779: */
1.15 millert 780: return (infodump ? len : termcap_length(outbuf.text));
1.1 millert 781: }
782:
1.10 millert 783: int
784: dump_entry(TERMTYPE * tterm, bool limited, int numbers, int (*pred) (int
785: type, int idx))
1.1 millert 786: /* dump a single entry */
787: {
1.10 millert 788: int len, critlen;
789: const char *legend;
790: bool infodump;
1.1 millert 791:
1.10 millert 792: if (outform == F_TERMCAP || outform == F_TCONVERR) {
1.1 millert 793: critlen = MAX_TERMCAP_LENGTH;
794: legend = "older termcap";
795: infodump = FALSE;
796: set_obsolete_termcaps(tterm);
1.10 millert 797: } else {
1.1 millert 798: critlen = MAX_TERMINFO_LENGTH;
799: legend = "terminfo";
800: infodump = TRUE;
801: }
802:
1.10 millert 803: if (((len = fmt_entry(tterm, pred, FALSE, infodump, numbers)) > critlen)
804: && limited) {
1.11 millert 805: PRINTF("# (untranslatable capabilities removed to fit entry within %d bytes)\n",
1.10 millert 806: critlen);
807: if ((len = fmt_entry(tterm, pred, TRUE, infodump, numbers)) > critlen) {
1.1 millert 808: /*
809: * We pick on sgr because it's a nice long string capability that
1.12 millert 810: * is really just an optimization hack. Another good candidate is
811: * acsc since it is both long and unused by BSD termcap.
1.1 millert 812: */
813: char *oldsgr = set_attributes;
1.12 millert 814: char *oldacsc = acs_chars;
1.3 millert 815: set_attributes = ABSENT_STRING;
1.11 millert 816: PRINTF("# (sgr removed to fit entry within %d bytes)\n",
1.10 millert 817: critlen);
818: if ((len = fmt_entry(tterm, pred, TRUE, infodump, numbers)) > critlen) {
1.12 millert 819: acs_chars = ABSENT_STRING;
820: PRINTF("# (acsc removed to fit entry within %d bytes)\n",
821: critlen);
822: }
823: if ((len = fmt_entry(tterm, pred, TRUE, infodump, numbers)) > critlen) {
1.1 millert 824: int oldversion = tversion;
825:
826: tversion = V_BSD;
1.11 millert 827: PRINTF("# (terminfo-only capabilities suppressed to fit entry within %d bytes)\n",
1.10 millert 828: critlen);
1.1 millert 829:
1.10 millert 830: if ((len = fmt_entry(tterm, pred, TRUE, infodump, numbers))
831: > critlen) {
1.1 millert 832: (void) fprintf(stderr,
1.10 millert 833: "warning: %s entry is %d bytes long\n",
834: _nc_first_name(tterm->term_names),
835: len);
1.11 millert 836: PRINTF(
1.10 millert 837: "# WARNING: this entry, %d bytes long, may core-dump %s libraries!\n",
838: len, legend);
1.1 millert 839: }
840: tversion = oldversion;
841: }
842: set_attributes = oldsgr;
1.12 millert 843: acs_chars = oldacsc;
1.1 millert 844: }
845: }
846:
1.15 millert 847: (void) fputs(outbuf.text, stdout);
1.1 millert 848: return len;
849: }
850:
1.10 millert 851: int
852: dump_uses(const char *name, bool infodump)
1.1 millert 853: /* dump "use=" clauses in the appropriate format */
854: {
855: char buffer[MAX_TERMINFO_LENGTH];
856:
1.15 millert 857: strcpy_DYN(&outbuf, 0);
1.17 ! deraadt 858: (void) snprintf(buffer, sizeof buffer,
! 859: "%s%s", infodump ? "use=" : "tc=", name);
1.1 millert 860: wrap_concat(buffer);
1.15 millert 861: (void) fputs(outbuf.text, stdout);
862: return outbuf.used;
1.1 millert 863: }
864:
1.10 millert 865: void
1.15 millert 866: compare_entry(void (*hook) (int t, int i, const char *name), TERMTYPE * tp
867: GCC_UNUSED, bool quiet)
1.1 millert 868: /* compare two entries */
869: {
1.10 millert 870: int i, j;
871: NCURSES_CONST char *name;
1.1 millert 872:
1.15 millert 873: if (!quiet)
874: fputs(" comparing booleans.\n", stdout);
1.10 millert 875: for_each_boolean(j, tp) {
1.4 millert 876: i = BoolIndirect(j);
1.10 millert 877: name = ExtBoolname(tp, i, bool_names);
1.1 millert 878:
1.10 millert 879: if (isObsolete(outform, name))
1.1 millert 880: continue;
881:
1.14 millert 882: (*hook) (CMP_BOOLEAN, i, name);
1.1 millert 883: }
884:
1.15 millert 885: if (!quiet)
886: fputs(" comparing numbers.\n", stdout);
1.10 millert 887: for_each_number(j, tp) {
1.4 millert 888: i = NumIndirect(j);
1.10 millert 889: name = ExtNumname(tp, i, num_names);
1.1 millert 890:
1.10 millert 891: if (isObsolete(outform, name))
1.1 millert 892: continue;
893:
1.14 millert 894: (*hook) (CMP_NUMBER, i, name);
1.1 millert 895: }
896:
1.15 millert 897: if (!quiet)
898: fputs(" comparing strings.\n", stdout);
1.10 millert 899: for_each_string(j, tp) {
1.4 millert 900: i = StrIndirect(j);
1.10 millert 901: name = ExtStrname(tp, i, str_names);
1.1 millert 902:
1.10 millert 903: if (isObsolete(outform, name))
1.1 millert 904: continue;
905:
1.14 millert 906: (*hook) (CMP_STRING, i, name);
1.1 millert 907: }
1.14 millert 908:
909: /* (void) fputs(" comparing use entries.\n", stdout); */
910: (*hook) (CMP_USE, 0, "use");
911:
1.1 millert 912: }
913:
914: #define NOTSET(s) ((s) == 0)
915:
916: /*
917: * This bit of legerdemain turns all the terminfo variable names into
918: * references to locations in the arrays Booleans, Numbers, and Strings ---
919: * precisely what's needed.
920: */
921: #undef CUR
922: #define CUR tp->
923:
1.10 millert 924: static void
925: set_obsolete_termcaps(TERMTYPE * tp)
1.1 millert 926: {
927: #include "capdefaults.c"
928: }
929:
930: /*
931: * Convert an alternate-character-set string to canonical form: sorted and
932: * unique.
933: */
1.14 millert 934: void
1.10 millert 935: repair_acsc(TERMTYPE * tp)
1.1 millert 936: {
1.10 millert 937: if (VALID_STRING(acs_chars)) {
938: size_t n, m;
939: char mapped[256];
940: char extra = 0;
941: unsigned source;
942: unsigned target;
943: bool fix_needed = FALSE;
944:
945: for (n = 0, source = 0; acs_chars[n] != 0; n++) {
946: target = acs_chars[n];
947: if (source >= target) {
948: fix_needed = TRUE;
949: break;
950: }
951: source = target;
952: if (acs_chars[n + 1])
953: n++;
954: }
955: if (fix_needed) {
956: memset(mapped, 0, sizeof(mapped));
957: for (n = 0; acs_chars[n] != 0; n++) {
958: source = acs_chars[n];
959: if ((target = (unsigned char) acs_chars[n + 1]) != 0) {
960: mapped[source] = target;
961: n++;
962: } else {
963: extra = source;
1.1 millert 964: }
965: }
1.10 millert 966: for (n = m = 0; n < sizeof(mapped); n++) {
967: if (mapped[n]) {
968: acs_chars[m++] = n;
969: acs_chars[m++] = mapped[n];
1.1 millert 970: }
971: }
1.10 millert 972: if (extra)
973: acs_chars[m++] = extra; /* garbage in, garbage out */
974: acs_chars[m] = 0;
1.1 millert 975: }
1.10 millert 976: }
1.1 millert 977: }