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