Merge branch 'vendor/GCC44'
[dragonfly.git] / contrib / ncurses-5.4 / progs / dump_entry.c
blob48ef488165b610def2ee07be1f819687b20614a8
1 /****************************************************************************
2 * Copyright (c) 1998-2002,2003 Free Software Foundation, Inc. *
3 * *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
11 * *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
14 * *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
22 * *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
26 * authorization. *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 * and: Thomas E. Dickey 1996 on *
33 ****************************************************************************/
35 #define __INTERNAL_CAPS_VISIBLE
36 #include <progs.priv.h>
38 #include "dump_entry.h"
39 #include "termsort.c" /* this C file is generated */
40 #include <parametrized.h> /* so is this */
42 MODULE_ID("$Id: dump_entry.c,v 1.66 2003/05/24 22:43:59 tom Exp $")
44 #define INDENT 8
45 #define DISCARD(string) string = ABSENT_STRING
46 #define PRINTF (void) printf
48 typedef struct {
49 char *text;
50 size_t used;
51 size_t size;
52 } DYNBUF;
54 static int tversion; /* terminfo version */
55 static int outform; /* output format to use */
56 static int sortmode; /* sort mode to use */
57 static int width = 60; /* max line width for listings */
58 static int column; /* current column, limited by 'width' */
59 static int oldcol; /* last value of column before wrap */
60 static bool pretty; /* true if we format if-then-else strings */
62 static DYNBUF outbuf;
63 static DYNBUF tmpbuf;
65 /* indirection pointers for implementing sort and display modes */
66 static const int *bool_indirect, *num_indirect, *str_indirect;
67 static NCURSES_CONST char *const *bool_names;
68 static NCURSES_CONST char *const *num_names;
69 static NCURSES_CONST char *const *str_names;
71 static const char *separator, *trailer;
73 /* cover various ports and variants of terminfo */
74 #define V_ALLCAPS 0 /* all capabilities (SVr4, XSI, ncurses) */
75 #define V_SVR1 1 /* SVR1, Ultrix */
76 #define V_HPUX 2 /* HP/UX */
77 #define V_AIX 3 /* AIX */
78 #define V_BSD 4 /* BSD */
80 #if NCURSES_XNAMES
81 #define OBSOLETE(n) (!_nc_user_definable && (n[0] == 'O' && n[1] == 'T'))
82 #else
83 #define OBSOLETE(n) (n[0] == 'O' && n[1] == 'T')
84 #endif
86 #define isObsolete(f,n) ((f == F_TERMINFO || f == F_VARIABLE) && OBSOLETE(n))
88 #if NCURSES_XNAMES
89 #define BoolIndirect(j) ((j >= BOOLCOUNT) ? (j) : ((sortmode == S_NOSORT) ? j : bool_indirect[j]))
90 #define NumIndirect(j) ((j >= NUMCOUNT) ? (j) : ((sortmode == S_NOSORT) ? j : num_indirect[j]))
91 #define StrIndirect(j) ((j >= STRCOUNT) ? (j) : ((sortmode == S_NOSORT) ? j : str_indirect[j]))
92 #else
93 #define BoolIndirect(j) ((sortmode == S_NOSORT) ? (j) : bool_indirect[j])
94 #define NumIndirect(j) ((sortmode == S_NOSORT) ? (j) : num_indirect[j])
95 #define StrIndirect(j) ((sortmode == S_NOSORT) ? (j) : str_indirect[j])
96 #endif
98 static void
99 strncpy_DYN(DYNBUF * dst, const char *src, size_t need)
101 size_t want = need + dst->used + 1;
102 if (want > dst->size) {
103 dst->size += (want + 1024); /* be generous */
104 dst->text = typeRealloc(char, dst->size, dst->text);
106 (void) strncpy(dst->text + dst->used, src, need);
107 dst->used += need;
108 dst->text[dst->used] = 0;
111 static void
112 strcpy_DYN(DYNBUF * dst, const char *src)
114 if (src == 0) {
115 dst->used = 0;
116 strcpy_DYN(dst, "");
117 } else {
118 strncpy_DYN(dst, src, strlen(src));
122 #if NO_LEAKS
123 static void
124 free_DYN(DYNBUF * p)
126 if (p->text != 0)
127 free(p->text);
128 p->text = 0;
129 p->size = 0;
130 p->used = 0;
133 void
134 _nc_leaks_dump_entry(void)
136 free_DYN(&outbuf);
137 free_DYN(&tmpbuf);
139 #endif
141 NCURSES_CONST char *
142 nametrans(const char *name)
143 /* translate a capability name from termcap to terminfo */
145 const struct name_table_entry *np;
147 if ((np = _nc_find_entry(name, _nc_get_hash_table(0))) != 0)
148 switch (np->nte_type) {
149 case BOOLEAN:
150 if (bool_from_termcap[np->nte_index])
151 return (boolcodes[np->nte_index]);
152 break;
154 case NUMBER:
155 if (num_from_termcap[np->nte_index])
156 return (numcodes[np->nte_index]);
157 break;
159 case STRING:
160 if (str_from_termcap[np->nte_index])
161 return (strcodes[np->nte_index]);
162 break;
165 return (0);
168 void
169 dump_init(const char *version, int mode, int sort, int twidth, int traceval,
170 bool formatted)
171 /* set up for entry display */
173 width = twidth;
174 pretty = formatted;
176 /* versions */
177 if (version == 0)
178 tversion = V_ALLCAPS;
179 else if (!strcmp(version, "SVr1") || !strcmp(version, "SVR1")
180 || !strcmp(version, "Ultrix"))
181 tversion = V_SVR1;
182 else if (!strcmp(version, "HP"))
183 tversion = V_HPUX;
184 else if (!strcmp(version, "AIX"))
185 tversion = V_AIX;
186 else if (!strcmp(version, "BSD"))
187 tversion = V_BSD;
188 else
189 tversion = V_ALLCAPS;
191 /* implement display modes */
192 switch (outform = mode) {
193 case F_LITERAL:
194 case F_TERMINFO:
195 bool_names = boolnames;
196 num_names = numnames;
197 str_names = strnames;
198 separator = twidth ? ", " : ",";
199 trailer = "\n\t";
200 break;
202 case F_VARIABLE:
203 bool_names = boolfnames;
204 num_names = numfnames;
205 str_names = strfnames;
206 separator = twidth ? ", " : ",";
207 trailer = "\n\t";
208 break;
210 case F_TERMCAP:
211 case F_TCONVERR:
212 bool_names = boolcodes;
213 num_names = numcodes;
214 str_names = strcodes;
215 separator = ":";
216 trailer = "\\\n\t:";
217 break;
220 /* implement sort modes */
221 switch (sortmode = sort) {
222 case S_NOSORT:
223 if (traceval)
224 (void) fprintf(stderr,
225 "%s: sorting by term structure order\n", _nc_progname);
226 break;
228 case S_TERMINFO:
229 if (traceval)
230 (void) fprintf(stderr,
231 "%s: sorting by terminfo name order\n", _nc_progname);
232 bool_indirect = bool_terminfo_sort;
233 num_indirect = num_terminfo_sort;
234 str_indirect = str_terminfo_sort;
235 break;
237 case S_VARIABLE:
238 if (traceval)
239 (void) fprintf(stderr,
240 "%s: sorting by C variable order\n", _nc_progname);
241 bool_indirect = bool_variable_sort;
242 num_indirect = num_variable_sort;
243 str_indirect = str_variable_sort;
244 break;
246 case S_TERMCAP:
247 if (traceval)
248 (void) fprintf(stderr,
249 "%s: sorting by termcap name order\n", _nc_progname);
250 bool_indirect = bool_termcap_sort;
251 num_indirect = num_termcap_sort;
252 str_indirect = str_termcap_sort;
253 break;
256 if (traceval)
257 (void) fprintf(stderr,
258 "%s: width = %d, tversion = %d, outform = %d\n",
259 _nc_progname, width, tversion, outform);
262 static TERMTYPE *cur_type;
264 static int
265 dump_predicate(int type, int idx)
266 /* predicate function to use for ordinary decompilation */
268 switch (type) {
269 case BOOLEAN:
270 return (cur_type->Booleans[idx] == FALSE)
271 ? FAIL : cur_type->Booleans[idx];
273 case NUMBER:
274 return (cur_type->Numbers[idx] == ABSENT_NUMERIC)
275 ? FAIL : cur_type->Numbers[idx];
277 case STRING:
278 return (cur_type->Strings[idx] != ABSENT_STRING)
279 ? (int) TRUE : FAIL;
282 return (FALSE); /* pacify compiler */
285 static void set_obsolete_termcaps(TERMTYPE * tp);
287 /* is this the index of a function key string? */
288 #define FNKEY(i) (((i)<= 65 && (i)>= 75) || ((i)<= 216 && (i)>= 268))
291 * If we configure with a different Caps file, the offsets into the arrays
292 * will change. So we use an address expression.
294 #define BOOL_IDX(name) (&(name) - &(CUR Booleans[0]))
295 #define NUM_IDX(name) (&(name) - &(CUR Numbers[0]))
296 #define STR_IDX(name) (&(name) - &(CUR Strings[0]))
298 static bool
299 version_filter(int type, int idx)
300 /* filter out capabilities we may want to suppress */
302 switch (tversion) {
303 case V_ALLCAPS: /* SVr4, XSI Curses */
304 return (TRUE);
306 case V_SVR1: /* System V Release 1, Ultrix */
307 switch (type) {
308 case BOOLEAN:
309 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
310 case NUMBER:
311 return ((idx <= NUM_IDX(width_status_line)) ? TRUE : FALSE);
312 case STRING:
313 return ((idx <= STR_IDX(prtr_non)) ? TRUE : FALSE);
315 break;
317 case V_HPUX: /* Hewlett-Packard */
318 switch (type) {
319 case BOOLEAN:
320 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
321 case NUMBER:
322 return ((idx <= NUM_IDX(label_width)) ? TRUE : FALSE);
323 case STRING:
324 if (idx <= STR_IDX(prtr_non))
325 return (TRUE);
326 else if (FNKEY(idx)) /* function keys */
327 return (TRUE);
328 else if (idx == STR_IDX(plab_norm)
329 || idx == STR_IDX(label_on)
330 || idx == STR_IDX(label_off))
331 return (TRUE);
332 else
333 return (FALSE);
335 break;
337 case V_AIX: /* AIX */
338 switch (type) {
339 case BOOLEAN:
340 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
341 case NUMBER:
342 return ((idx <= NUM_IDX(width_status_line)) ? TRUE : FALSE);
343 case STRING:
344 if (idx <= STR_IDX(prtr_non))
345 return (TRUE);
346 else if (FNKEY(idx)) /* function keys */
347 return (TRUE);
348 else
349 return (FALSE);
351 break;
353 case V_BSD: /* BSD */
354 switch (type) {
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];
362 break;
365 return (FALSE); /* pacify the compiler */
368 static void
369 force_wrap(void)
371 oldcol = column;
372 strcpy_DYN(&outbuf, trailer);
373 column = INDENT;
376 static void
377 wrap_concat(const char *src)
379 int need = strlen(src);
380 int want = strlen(separator) + need;
382 if (column > INDENT
383 && column + want > width) {
384 force_wrap();
386 strcpy_DYN(&outbuf, src);
387 strcpy_DYN(&outbuf, separator);
388 column += need;
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
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 ":".
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.
405 #ifdef TEST_TERMCAP_LENGTH
406 static int
407 termcap_length(const char *src)
409 static const char pattern[] = ":\\\n\t:";
411 int len = 0;
412 const char *const t = src + strlen(src);
414 while (*src != '\0') {
415 IGNORE_SEP_TRAIL(src, t, pattern);
416 src++;
417 len++;
419 return len;
421 #else
422 #define termcap_length(src) strlen(src)
423 #endif
425 static char *
426 fmt_complex(char *src, int level)
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);
433 while (*src != '\0') {
434 switch (*src) {
435 case '\\':
436 percent = 0;
437 strncpy_DYN(&tmpbuf, src++, 1);
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;
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);
466 continue;
468 break;
469 case ';': /* "endif" */
470 if (percent) {
471 percent = 0;
472 if (level > 1) {
473 tmpbuf.text[tmpbuf.used - 1] = '\n';
474 for (n = 0; n < level; n++)
475 strncpy_DYN(&tmpbuf, "\t", 1);
476 strncpy_DYN(&tmpbuf, "%", 1);
477 strncpy_DYN(&tmpbuf, src++, 1);
478 return src;
480 _nc_warning("%%; without %%?");
482 break;
483 case 'p':
484 if (percent && params) {
485 tmpbuf.text[tmpbuf.used - 1] = '\n';
486 for (n = 0; n <= level; n++)
487 strncpy_DYN(&tmpbuf, "\t", 1);
488 strncpy_DYN(&tmpbuf, "%", 1);
490 percent = 0;
491 break;
492 default:
493 percent = 0;
494 break;
496 strncpy_DYN(&tmpbuf, src++, 1);
498 return src;
502 fmt_entry(TERMTYPE * tterm,
503 int (*pred) (int type, int idx),
504 bool content_only,
505 bool suppress_untranslatable,
506 bool infodump,
507 int numbers)
509 int i, j;
510 char buffer[MAX_TERMINFO_LENGTH];
511 NCURSES_CONST char *name;
512 int predval, len;
513 int num_bools = 0;
514 int num_values = 0;
515 int num_strings = 0;
516 bool outcount = 0;
518 #define WRAP_CONCAT \
519 wrap_concat(buffer); \
520 outcount = TRUE
522 len = 12; /* terminfo file-header */
524 if (pred == 0) {
525 cur_type = tterm;
526 pred = dump_predicate;
529 strcpy_DYN(&outbuf, 0);
530 if (content_only) {
531 column = INDENT; /* FIXME: workaround to prevent empty lines */
532 } else {
533 strcpy_DYN(&outbuf, tterm->term_names);
534 strcpy_DYN(&outbuf, separator);
535 column = outbuf.used;
536 force_wrap();
539 for_each_boolean(j, tterm) {
540 i = BoolIndirect(j);
541 name = ExtBoolname(tterm, i, bool_names);
543 if (!version_filter(BOOLEAN, i))
544 continue;
545 else if (isObsolete(outform, name))
546 continue;
548 predval = pred(BOOLEAN, i);
549 if (predval != FAIL) {
550 (void) strcpy(buffer, name);
551 if (predval <= 0)
552 (void) strcat(buffer, "@");
553 else if (i + 1 > num_bools)
554 num_bools = i + 1;
555 WRAP_CONCAT;
559 if (column != INDENT)
560 force_wrap();
562 for_each_number(j, tterm) {
563 i = NumIndirect(j);
564 name = ExtNumname(tterm, i, num_names);
566 if (!version_filter(NUMBER, i))
567 continue;
568 else if (isObsolete(outform, name))
569 continue;
571 predval = pred(NUMBER, i);
572 if (predval != FAIL) {
573 if (tterm->Numbers[i] < 0) {
574 sprintf(buffer, "%s@", name);
575 } else {
576 sprintf(buffer, "%s#%d", name, tterm->Numbers[i]);
577 if (i + 1 > num_values)
578 num_values = i + 1;
580 WRAP_CONCAT;
584 if (column != INDENT)
585 force_wrap();
587 len += num_bools
588 + num_values * 2
589 + strlen(tterm->term_names) + 1;
590 if (len & 1)
591 len++;
593 #undef CUR
594 #define CUR tterm->
595 if (outform == F_TERMCAP) {
596 if (termcap_reset != ABSENT_STRING) {
597 if (init_3string != ABSENT_STRING
598 && !strcmp(init_3string, termcap_reset))
599 DISCARD(init_3string);
601 if (reset_2string != ABSENT_STRING
602 && !strcmp(reset_2string, termcap_reset))
603 DISCARD(reset_2string);
607 for_each_string(j, tterm) {
608 i = StrIndirect(j);
609 name = ExtStrname(tterm, i, str_names);
611 if (!version_filter(STRING, i))
612 continue;
613 else if (isObsolete(outform, name))
614 continue;
617 * Some older versions of vi want rmir/smir to be defined
618 * for ich/ich1 to work. If they're not defined, force
619 * them to be output as defined and empty.
621 if (outform == F_TERMCAP) {
622 if (insert_character || parm_ich) {
623 if (&tterm->Strings[i] == &enter_insert_mode
624 && enter_insert_mode == ABSENT_STRING) {
625 (void) strcpy(buffer, "im=");
626 WRAP_CONCAT;
627 continue;
630 if (&tterm->Strings[i] == &exit_insert_mode
631 && exit_insert_mode == ABSENT_STRING) {
632 (void) strcpy(buffer, "ei=");
633 WRAP_CONCAT;
634 continue;
639 predval = pred(STRING, i);
640 buffer[0] = '\0';
642 if (predval != FAIL) {
643 if (tterm->Strings[i] != ABSENT_STRING
644 && i + 1 > num_strings)
645 num_strings = i + 1;
647 if (!VALID_STRING(tterm->Strings[i])) {
648 sprintf(buffer, "%s@", name);
649 WRAP_CONCAT;
650 } else if (outform == F_TERMCAP || outform == F_TCONVERR) {
651 int params = ((i < (int) SIZEOF(parametrized))
652 ? parametrized[i]
653 : 0);
654 char *srccap = _nc_tic_expand(tterm->Strings[i], TRUE, numbers);
655 char *cv = _nc_infotocap(name, srccap, params);
657 if (cv == 0) {
658 if (outform == F_TCONVERR) {
659 sprintf(buffer, "%s=!!! %s WILL NOT CONVERT !!!",
660 name, srccap);
661 } else if (suppress_untranslatable) {
662 continue;
663 } else {
664 char *s = srccap, *d = buffer;
665 sprintf(d, "..%s=", name);
666 d += strlen(d);
667 while ((*d = *s++) != 0) {
668 if (*d == ':') {
669 *d++ = '\\';
670 *d = ':';
671 } else if (*d == '\\') {
672 *++d = *s++;
674 d++;
677 } else {
678 sprintf(buffer, "%s=%s", name, cv);
680 len += strlen(tterm->Strings[i]) + 1;
681 WRAP_CONCAT;
682 } else {
683 char *src = _nc_tic_expand(tterm->Strings[i],
684 outform == F_TERMINFO, numbers);
686 strcpy_DYN(&tmpbuf, 0);
687 strcpy_DYN(&tmpbuf, name);
688 strcpy_DYN(&tmpbuf, "=");
689 if (pretty
690 && (outform == F_TERMINFO
691 || outform == F_VARIABLE)) {
692 fmt_complex(src, 1);
693 } else {
694 strcpy_DYN(&tmpbuf, src);
696 len += strlen(tterm->Strings[i]) + 1;
697 wrap_concat(tmpbuf.text);
698 outcount = TRUE;
702 len += num_strings * 2;
705 * This piece of code should be an effective inverse of the functions
706 * postprocess_terminfo and postprocess_terminfo in parse_entry.c.
707 * Much more work should be done on this to support dumping termcaps.
709 if (tversion == V_HPUX) {
710 if (memory_lock) {
711 (void) sprintf(buffer, "meml=%s", memory_lock);
712 WRAP_CONCAT;
714 if (memory_unlock) {
715 (void) sprintf(buffer, "memu=%s", memory_unlock);
716 WRAP_CONCAT;
718 } else if (tversion == V_AIX) {
719 if (VALID_STRING(acs_chars)) {
720 bool box_ok = TRUE;
721 const char *acstrans = "lqkxjmwuvtn";
722 const char *cp;
723 char *tp, *sp, boxchars[11];
725 tp = boxchars;
726 for (cp = acstrans; *cp; cp++) {
727 sp = strchr(acs_chars, *cp);
728 if (sp)
729 *tp++ = sp[1];
730 else {
731 box_ok = FALSE;
732 break;
735 tp[0] = '\0';
737 if (box_ok) {
738 (void) strcpy(buffer, "box1=");
739 (void) strcat(buffer, _nc_tic_expand(boxchars,
740 outform == F_TERMINFO, numbers));
741 WRAP_CONCAT;
747 * kludge: trim off trailer to avoid an extra blank line
748 * in infocmp -u output when there are no string differences
750 if (outcount) {
751 bool trimmed = FALSE;
752 j = outbuf.used;
753 if (j >= 2
754 && outbuf.text[j - 1] == '\t'
755 && outbuf.text[j - 2] == '\n') {
756 outbuf.used -= 2;
757 trimmed = TRUE;
758 } else if (j >= 4
759 && outbuf.text[j - 1] == ':'
760 && outbuf.text[j - 2] == '\t'
761 && outbuf.text[j - 3] == '\n'
762 && outbuf.text[j - 4] == '\\') {
763 outbuf.used -= 4;
764 trimmed = TRUE;
766 if (trimmed) {
767 outbuf.text[outbuf.used] = '\0';
768 column = oldcol;
771 #if 0
772 fprintf(stderr, "num_bools = %d\n", num_bools);
773 fprintf(stderr, "num_values = %d\n", num_values);
774 fprintf(stderr, "num_strings = %d\n", num_strings);
775 fprintf(stderr, "term_names=%s, len=%d, strlen(outbuf)=%d, outbuf=%s\n",
776 tterm->term_names, len, outbuf.used, outbuf.text);
777 #endif
779 * Here's where we use infodump to trigger a more stringent length check
780 * for termcap-translation purposes.
781 * Return the length of the raw entry, without tc= expansions,
782 * It gives an idea of which entries are deadly to even *scan past*,
783 * as opposed to *use*.
785 return (infodump ? len : (int) termcap_length(outbuf.text));
788 static bool
789 kill_string(TERMTYPE * tterm, char *cap)
791 int n;
792 for (n = 0; n < NUM_STRINGS(tterm); ++n) {
793 if (cap == tterm->Strings[n]) {
794 tterm->Strings[n] = ABSENT_STRING;
795 return TRUE;
798 return FALSE;
801 static char *
802 find_string(TERMTYPE * tterm, char *name)
804 int n;
805 for (n = 0; n < NUM_STRINGS(tterm); ++n) {
806 if (version_filter(STRING, n)
807 && !strcmp(name, strnames[n])) {
808 char *cap = tterm->Strings[n];
809 if (VALID_STRING(cap)) {
810 return cap;
812 break;
815 return ABSENT_STRING;
819 * This is used to remove function-key labels from a termcap entry to
820 * make it smaller.
822 static int
823 kill_labels(TERMTYPE * tterm, int target)
825 int n;
826 int result = 0;
827 char *cap;
828 char name[10];
830 for (n = 0; n <= 10; ++n) {
831 sprintf(name, "lf%d", n);
832 if ((cap = find_string(tterm, name)) != ABSENT_STRING
833 && kill_string(tterm, cap)) {
834 target -= (strlen(cap) + 5);
835 ++result;
836 if (target < 0)
837 break;
840 return result;
844 * This is used to remove function-key definitions from a termcap entry to
845 * make it smaller.
847 static int
848 kill_fkeys(TERMTYPE * tterm, int target)
850 int n;
851 int result = 0;
852 char *cap;
853 char name[10];
855 for (n = 60; n >= 0; --n) {
856 sprintf(name, "kf%d", n);
857 if ((cap = find_string(tterm, name)) != ABSENT_STRING
858 && kill_string(tterm, cap)) {
859 target -= (strlen(cap) + 5);
860 ++result;
861 if (target < 0)
862 break;
865 return result;
868 #define FMT_ENTRY() \
869 fmt_entry(tterm, pred, \
870 (already_used > 0), \
871 suppress_untranslatable, \
872 infodump, numbers)
874 #define SHOW_WHY if (!already_used) PRINTF
877 dump_entry(TERMTYPE * tterm,
878 bool suppress_untranslatable,
879 bool limited,
880 int already_used,
881 int numbers,
882 int (*pred) (int type, int idx))
883 /* dump a single entry */
885 int len, critlen;
886 const char *legend;
887 bool infodump;
889 if (outform == F_TERMCAP || outform == F_TCONVERR) {
890 critlen = MAX_TERMCAP_LENGTH;
891 legend = "older termcap";
892 infodump = FALSE;
893 set_obsolete_termcaps(tterm);
894 } else {
895 critlen = MAX_TERMINFO_LENGTH;
896 legend = "terminfo";
897 infodump = TRUE;
899 critlen -= already_used;
901 if (((len = FMT_ENTRY()) > critlen)
902 && limited) {
903 if (!suppress_untranslatable) {
904 SHOW_WHY("# (untranslatable capabilities removed to fit entry within %d bytes)\n",
905 critlen);
906 suppress_untranslatable = TRUE;
908 if ((len = FMT_ENTRY()) > critlen) {
910 * We pick on sgr because it's a nice long string capability that
911 * is really just an optimization hack. Another good candidate is
912 * acsc since it is both long and unused by BSD termcap.
914 char *oldsgr = set_attributes;
915 char *oldacsc = acs_chars;
916 bool changed = FALSE;
918 if (VALID_STRING(set_attributes)) {
919 set_attributes = ABSENT_STRING;
920 SHOW_WHY("# (sgr removed to fit entry within %d bytes)\n",
921 critlen);
922 changed = TRUE;
924 if (!changed || ((len = FMT_ENTRY()) > critlen)) {
925 if (VALID_STRING(acs_chars)) {
926 acs_chars = ABSENT_STRING;
927 SHOW_WHY("# (acsc removed to fit entry within %d bytes)\n",
928 critlen);
929 changed = TRUE;
932 if (!changed || ((len = FMT_ENTRY()) > critlen)) {
933 int oldversion = tversion;
935 tversion = V_BSD;
936 SHOW_WHY("# (terminfo-only capabilities suppressed to fit entry within %d bytes)\n",
937 critlen);
939 len = FMT_ENTRY();
940 if (len > critlen
941 && kill_labels(tterm, len - critlen)) {
942 SHOW_WHY("# (some labels capabilities suppressed to fit entry within %d bytes)\n",
943 critlen);
944 len = FMT_ENTRY();
946 if (len > critlen
947 && kill_fkeys(tterm, len - critlen)) {
948 SHOW_WHY("# (some function-key capabilities suppressed to fit entry within %d bytes)\n",
949 critlen);
950 len = FMT_ENTRY();
952 if (len > critlen && !already_used) {
953 (void) fprintf(stderr,
954 "warning: %s entry is %d bytes long\n",
955 _nc_first_name(tterm->term_names),
956 len);
957 SHOW_WHY("# WARNING: this entry, %d bytes long, may core-dump %s libraries!\n",
958 already_used + len, legend);
960 tversion = oldversion;
962 set_attributes = oldsgr;
963 acs_chars = oldacsc;
967 (void) fputs(outbuf.text, stdout);
968 return len;
972 dump_uses(const char *name, bool infodump)
973 /* dump "use=" clauses in the appropriate format */
975 char buffer[MAX_TERMINFO_LENGTH];
977 strcpy_DYN(&outbuf, 0);
978 (void) sprintf(buffer, "%s%s", infodump ? "use=" : "tc=", name);
979 wrap_concat(buffer);
980 (void) fputs(outbuf.text, stdout);
981 return outbuf.used;
984 void
985 compare_entry(void (*hook) (int t, int i, const char *name), TERMTYPE * tp
986 GCC_UNUSED, bool quiet)
987 /* compare two entries */
989 int i, j;
990 NCURSES_CONST char *name;
992 if (!quiet)
993 fputs(" comparing booleans.\n", stdout);
994 for_each_boolean(j, tp) {
995 i = BoolIndirect(j);
996 name = ExtBoolname(tp, i, bool_names);
998 if (isObsolete(outform, name))
999 continue;
1001 (*hook) (CMP_BOOLEAN, i, name);
1004 if (!quiet)
1005 fputs(" comparing numbers.\n", stdout);
1006 for_each_number(j, tp) {
1007 i = NumIndirect(j);
1008 name = ExtNumname(tp, i, num_names);
1010 if (isObsolete(outform, name))
1011 continue;
1013 (*hook) (CMP_NUMBER, i, name);
1016 if (!quiet)
1017 fputs(" comparing strings.\n", stdout);
1018 for_each_string(j, tp) {
1019 i = StrIndirect(j);
1020 name = ExtStrname(tp, i, str_names);
1022 if (isObsolete(outform, name))
1023 continue;
1025 (*hook) (CMP_STRING, i, name);
1028 /* (void) fputs(" comparing use entries.\n", stdout); */
1029 (*hook) (CMP_USE, 0, "use");
1033 #define NOTSET(s) ((s) == 0)
1036 * This bit of legerdemain turns all the terminfo variable names into
1037 * references to locations in the arrays Booleans, Numbers, and Strings ---
1038 * precisely what's needed.
1040 #undef CUR
1041 #define CUR tp->
1043 static void
1044 set_obsolete_termcaps(TERMTYPE * tp)
1046 #include "capdefaults.c"
1050 * Convert an alternate-character-set string to canonical form: sorted and
1051 * unique.
1053 void
1054 repair_acsc(TERMTYPE * tp)
1056 if (VALID_STRING(acs_chars)) {
1057 size_t n, m;
1058 char mapped[256];
1059 char extra = 0;
1060 unsigned source;
1061 unsigned target;
1062 bool fix_needed = FALSE;
1064 for (n = 0, source = 0; acs_chars[n] != 0; n++) {
1065 target = acs_chars[n];
1066 if (source >= target) {
1067 fix_needed = TRUE;
1068 break;
1070 source = target;
1071 if (acs_chars[n + 1])
1072 n++;
1074 if (fix_needed) {
1075 memset(mapped, 0, sizeof(mapped));
1076 for (n = 0; acs_chars[n] != 0; n++) {
1077 source = acs_chars[n];
1078 if ((target = (unsigned char) acs_chars[n + 1]) != 0) {
1079 mapped[source] = target;
1080 n++;
1081 } else {
1082 extra = source;
1085 for (n = m = 0; n < sizeof(mapped); n++) {
1086 if (mapped[n]) {
1087 acs_chars[m++] = n;
1088 acs_chars[m++] = mapped[n];
1091 if (extra)
1092 acs_chars[m++] = extra; /* garbage in, garbage out */
1093 acs_chars[m] = 0;