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