Annotation of src/usr.bin/tic/dump_entry.c, Revision 1.18
1.18 ! millert 1: /* $OpenBSD: dump_entry.c,v 1.17 2003/04/06 18:38:42 deraadt 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.18 ! millert 658: char *d, *s = srccap;
! 659: snprintf(buffer, sizeof buffer, "..%s=", name);
1.17 deraadt 660: d = buffer + strlen(buffer);
661: while ((*d = *s++) != 0) { /* XXX overflow? */
1.10 millert 662: if (*d == ':') {
663: *d++ = '\\';
664: *d = ':';
665: } else if (*d == '\\') {
666: *++d = *s++;
667: }
668: d++;
669: }
670: }
1.15 millert 671: } else {
1.17 deraadt 672: snprintf(buffer, sizeof buffer, "%s=%s", name, cv);
1.15 millert 673: }
1.1 millert 674: len += strlen(tterm->Strings[i]) + 1;
1.15 millert 675: WRAP_CONCAT;
1.10 millert 676: } else {
677: char *src = _nc_tic_expand(tterm->Strings[i],
678: outform == F_TERMINFO, numbers);
1.15 millert 679:
680: strcpy_DYN(&tmpbuf, 0);
681: strcpy_DYN(&tmpbuf, name);
682: strcpy_DYN(&tmpbuf, "=");
1.8 millert 683: if (pretty
1.10 millert 684: && (outform == F_TERMINFO
1.15 millert 685: || outform == F_VARIABLE)) {
686: fmt_complex(src, 1);
687: } else {
688: strcpy_DYN(&tmpbuf, src);
689: }
1.1 millert 690: len += strlen(tterm->Strings[i]) + 1;
1.15 millert 691: wrap_concat(tmpbuf.text);
692: outcount = TRUE;
1.1 millert 693: }
694: }
695: }
696: len += num_strings * 2;
697:
698: /*
699: * This piece of code should be an effective inverse of the functions
700: * postprocess_terminfo and postprocess_terminfo in parse_entry.c.
701: * Much more work should be done on this to support dumping termcaps.
702: */
1.10 millert 703: if (tversion == V_HPUX) {
704: if (memory_lock) {
1.17 deraadt 705: (void) snprintf(buffer, sizeof buffer, "meml=%s", memory_lock);
1.1 millert 706: WRAP_CONCAT;
707: }
1.10 millert 708: if (memory_unlock) {
1.17 deraadt 709: (void) snprintf(buffer, sizeof buffer, "memu=%s", memory_unlock);
1.1 millert 710: WRAP_CONCAT;
711: }
1.10 millert 712: } else if (tversion == V_AIX) {
713: if (VALID_STRING(acs_chars)) {
714: bool box_ok = TRUE;
715: const char *acstrans = "lqkxjmwuvtn";
716: const char *cp;
717: char *tp, *sp, boxchars[11];
1.1 millert 718:
719: tp = boxchars;
1.10 millert 720: for (cp = acstrans; *cp; cp++) {
1.1 millert 721: sp = strchr(acs_chars, *cp);
722: if (sp)
723: *tp++ = sp[1];
1.10 millert 724: else {
1.1 millert 725: box_ok = FALSE;
726: break;
727: }
728: }
729: tp[0] = '\0';
730:
1.10 millert 731: if (box_ok) {
1.17 deraadt 732: (void) strlcpy(buffer, "box1=", sizeof buffer);
733: (void) strlcat(buffer, _nc_tic_expand(boxchars,
734: outform == F_TERMINFO, numbers), sizeof buffer);
1.1 millert 735: WRAP_CONCAT;
736: }
737: }
738: }
739:
740: /*
741: * kludge: trim off trailer to avoid an extra blank line
742: * in infocmp -u output when there are no string differences
743: */
1.10 millert 744: if (outcount) {
1.15 millert 745: bool trimmed = FALSE;
746: j = outbuf.used;
1.1 millert 747: if (j >= 2
1.15 millert 748: && outbuf.text[j - 1] == '\t'
749: && outbuf.text[j - 2] == '\n') {
750: outbuf.used -= 2;
751: trimmed = TRUE;
1.1 millert 752: } else if (j >= 4
1.15 millert 753: && outbuf.text[j - 1] == ':'
754: && outbuf.text[j - 2] == '\t'
755: && outbuf.text[j - 3] == '\n'
756: && outbuf.text[j - 4] == '\\') {
757: outbuf.used -= 4;
758: trimmed = TRUE;
759: }
760: if (trimmed) {
761: outbuf.text[outbuf.used] = '\0';
762: column = oldcol;
1.1 millert 763: }
764: }
765: #if 0
766: fprintf(stderr, "num_bools = %d\n", num_bools);
767: fprintf(stderr, "num_values = %d\n", num_values);
768: fprintf(stderr, "num_strings = %d\n", num_strings);
769: fprintf(stderr, "term_names=%s, len=%d, strlen(outbuf)=%d, outbuf=%s\n",
1.15 millert 770: tterm->term_names, len, outbuf.used, outbuf.text);
1.1 millert 771: #endif
772: /*
773: * Here's where we use infodump to trigger a more stringent length check
774: * for termcap-translation purposes.
775: * Return the length of the raw entry, without tc= expansions,
776: * It gives an idea of which entries are deadly to even *scan past*,
777: * as opposed to *use*.
778: */
1.15 millert 779: return (infodump ? len : termcap_length(outbuf.text));
1.1 millert 780: }
781:
1.10 millert 782: int
783: dump_entry(TERMTYPE * tterm, bool limited, int numbers, int (*pred) (int
784: type, int idx))
1.1 millert 785: /* dump a single entry */
786: {
1.10 millert 787: int len, critlen;
788: const char *legend;
789: bool infodump;
1.1 millert 790:
1.10 millert 791: if (outform == F_TERMCAP || outform == F_TCONVERR) {
1.1 millert 792: critlen = MAX_TERMCAP_LENGTH;
793: legend = "older termcap";
794: infodump = FALSE;
795: set_obsolete_termcaps(tterm);
1.10 millert 796: } else {
1.1 millert 797: critlen = MAX_TERMINFO_LENGTH;
798: legend = "terminfo";
799: infodump = TRUE;
800: }
801:
1.10 millert 802: if (((len = fmt_entry(tterm, pred, FALSE, infodump, numbers)) > critlen)
803: && limited) {
1.11 millert 804: PRINTF("# (untranslatable capabilities removed to fit entry within %d bytes)\n",
1.10 millert 805: critlen);
806: if ((len = fmt_entry(tterm, pred, TRUE, infodump, numbers)) > critlen) {
1.1 millert 807: /*
808: * We pick on sgr because it's a nice long string capability that
1.12 millert 809: * is really just an optimization hack. Another good candidate is
810: * acsc since it is both long and unused by BSD termcap.
1.1 millert 811: */
812: char *oldsgr = set_attributes;
1.12 millert 813: char *oldacsc = acs_chars;
1.3 millert 814: set_attributes = ABSENT_STRING;
1.11 millert 815: PRINTF("# (sgr removed to fit entry within %d bytes)\n",
1.10 millert 816: critlen);
817: if ((len = fmt_entry(tterm, pred, TRUE, infodump, numbers)) > critlen) {
1.12 millert 818: acs_chars = ABSENT_STRING;
819: PRINTF("# (acsc removed to fit entry within %d bytes)\n",
820: critlen);
821: }
822: if ((len = fmt_entry(tterm, pred, TRUE, infodump, numbers)) > critlen) {
1.1 millert 823: int oldversion = tversion;
824:
825: tversion = V_BSD;
1.11 millert 826: PRINTF("# (terminfo-only capabilities suppressed to fit entry within %d bytes)\n",
1.10 millert 827: critlen);
1.1 millert 828:
1.10 millert 829: if ((len = fmt_entry(tterm, pred, TRUE, infodump, numbers))
830: > critlen) {
1.1 millert 831: (void) fprintf(stderr,
1.10 millert 832: "warning: %s entry is %d bytes long\n",
833: _nc_first_name(tterm->term_names),
834: len);
1.11 millert 835: PRINTF(
1.10 millert 836: "# WARNING: this entry, %d bytes long, may core-dump %s libraries!\n",
837: len, legend);
1.1 millert 838: }
839: tversion = oldversion;
840: }
841: set_attributes = oldsgr;
1.12 millert 842: acs_chars = oldacsc;
1.1 millert 843: }
844: }
845:
1.15 millert 846: (void) fputs(outbuf.text, stdout);
1.1 millert 847: return len;
848: }
849:
1.10 millert 850: int
851: dump_uses(const char *name, bool infodump)
1.1 millert 852: /* dump "use=" clauses in the appropriate format */
853: {
854: char buffer[MAX_TERMINFO_LENGTH];
855:
1.15 millert 856: strcpy_DYN(&outbuf, 0);
1.17 deraadt 857: (void) snprintf(buffer, sizeof buffer,
858: "%s%s", infodump ? "use=" : "tc=", name);
1.1 millert 859: wrap_concat(buffer);
1.15 millert 860: (void) fputs(outbuf.text, stdout);
861: return outbuf.used;
1.1 millert 862: }
863:
1.10 millert 864: void
1.15 millert 865: compare_entry(void (*hook) (int t, int i, const char *name), TERMTYPE * tp
866: GCC_UNUSED, bool quiet)
1.1 millert 867: /* compare two entries */
868: {
1.10 millert 869: int i, j;
870: NCURSES_CONST char *name;
1.1 millert 871:
1.15 millert 872: if (!quiet)
873: fputs(" comparing booleans.\n", stdout);
1.10 millert 874: for_each_boolean(j, tp) {
1.4 millert 875: i = BoolIndirect(j);
1.10 millert 876: name = ExtBoolname(tp, i, bool_names);
1.1 millert 877:
1.10 millert 878: if (isObsolete(outform, name))
1.1 millert 879: continue;
880:
1.14 millert 881: (*hook) (CMP_BOOLEAN, i, name);
1.1 millert 882: }
883:
1.15 millert 884: if (!quiet)
885: fputs(" comparing numbers.\n", stdout);
1.10 millert 886: for_each_number(j, tp) {
1.4 millert 887: i = NumIndirect(j);
1.10 millert 888: name = ExtNumname(tp, i, num_names);
1.1 millert 889:
1.10 millert 890: if (isObsolete(outform, name))
1.1 millert 891: continue;
892:
1.14 millert 893: (*hook) (CMP_NUMBER, i, name);
1.1 millert 894: }
895:
1.15 millert 896: if (!quiet)
897: fputs(" comparing strings.\n", stdout);
1.10 millert 898: for_each_string(j, tp) {
1.4 millert 899: i = StrIndirect(j);
1.10 millert 900: name = ExtStrname(tp, i, str_names);
1.1 millert 901:
1.10 millert 902: if (isObsolete(outform, name))
1.1 millert 903: continue;
904:
1.14 millert 905: (*hook) (CMP_STRING, i, name);
1.1 millert 906: }
1.14 millert 907:
908: /* (void) fputs(" comparing use entries.\n", stdout); */
909: (*hook) (CMP_USE, 0, "use");
910:
1.1 millert 911: }
912:
913: #define NOTSET(s) ((s) == 0)
914:
915: /*
916: * This bit of legerdemain turns all the terminfo variable names into
917: * references to locations in the arrays Booleans, Numbers, and Strings ---
918: * precisely what's needed.
919: */
920: #undef CUR
921: #define CUR tp->
922:
1.10 millert 923: static void
924: set_obsolete_termcaps(TERMTYPE * tp)
1.1 millert 925: {
926: #include "capdefaults.c"
927: }
928:
929: /*
930: * Convert an alternate-character-set string to canonical form: sorted and
931: * unique.
932: */
1.14 millert 933: void
1.10 millert 934: repair_acsc(TERMTYPE * tp)
1.1 millert 935: {
1.10 millert 936: if (VALID_STRING(acs_chars)) {
937: size_t n, m;
938: char mapped[256];
939: char extra = 0;
940: unsigned source;
941: unsigned target;
942: bool fix_needed = FALSE;
943:
944: for (n = 0, source = 0; acs_chars[n] != 0; n++) {
945: target = acs_chars[n];
946: if (source >= target) {
947: fix_needed = TRUE;
948: break;
949: }
950: source = target;
951: if (acs_chars[n + 1])
952: n++;
953: }
954: if (fix_needed) {
955: memset(mapped, 0, sizeof(mapped));
956: for (n = 0; acs_chars[n] != 0; n++) {
957: source = acs_chars[n];
958: if ((target = (unsigned char) acs_chars[n + 1]) != 0) {
959: mapped[source] = target;
960: n++;
961: } else {
962: extra = source;
1.1 millert 963: }
964: }
1.10 millert 965: for (n = m = 0; n < sizeof(mapped); n++) {
966: if (mapped[n]) {
967: acs_chars[m++] = n;
968: acs_chars[m++] = mapped[n];
1.1 millert 969: }
970: }
1.10 millert 971: if (extra)
972: acs_chars[m++] = extra; /* garbage in, garbage out */
973: acs_chars[m] = 0;
1.1 millert 974: }
1.10 millert 975: }
1.1 millert 976: }