xref: /openbsd-src/usr.bin/tic/dump_entry.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: dump_entry.c,v 1.16 2000/10/08 22:47:10 millert Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998-2000 Free Software Foundation, Inc.                   *
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 
43 MODULE_ID("$From: dump_entry.c,v 1.54 2000/10/01 01:34:06 tom Exp $")
44 
45 #define INDENT			8
46 #define DISCARD(string) string = ABSENT_STRING
47 #define PRINTF (void) printf
48 
49 typedef struct {
50     char *text;
51     size_t used;
52     size_t size;
53 } DYNBUF;
54 
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 
64 static DYNBUF outbuf;
65 static DYNBUF tmpbuf;
66 
67 /* indirection pointers for implementing sort and display modes */
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;
72 
73 static const char *separator, *trailer;
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 
82 #if NCURSES_XNAMES
83 #define OBSOLETE(n) (!_nc_user_definable && (n[0] == 'O' && n[1] == 'T'))
84 #else
85 #define OBSOLETE(n) (n[0] == 'O' && n[1] == 'T')
86 #endif
87 
88 #define isObsolete(f,n) ((f == F_TERMINFO || f == F_VARIABLE) && OBSOLETE(n))
89 
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 
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 
124 #if NO_LEAKS
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 
135 void
136 _nc_leaks_dump_entry(void)
137 {
138     free_DYN(&outbuf);
139     free_DYN(&tmpbuf);
140 }
141 #endif
142 
143 NCURSES_CONST char *
144 nametrans(const char *name)
145 /* translate a capability name from termcap to terminfo */
146 {
147     const struct name_table_entry *np;
148 
149     if ((np = _nc_find_entry(name, _nc_get_hash_table(0))) != 0)
150 	switch (np->nte_type) {
151 	case BOOLEAN:
152 	    if (bool_from_termcap[np->nte_index])
153 		return (boolcodes[np->nte_index]);
154 	    break;
155 
156 	case NUMBER:
157 	    if (num_from_termcap[np->nte_index])
158 		return (numcodes[np->nte_index]);
159 	    break;
160 
161 	case STRING:
162 	    if (str_from_termcap[np->nte_index])
163 		return (strcodes[np->nte_index]);
164 	    break;
165 	}
166 
167     return (0);
168 }
169 
170 void
171 dump_init(const char *version, int mode, int sort, int twidth, int traceval,
172     bool formatted)
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")
183 	|| !strcmp(version, "Ultrix"))
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 */
195     switch (outform = mode) {
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 */
224     switch (sortmode = sort) {
225     case S_NOSORT:
226 	if (traceval)
227 	    (void) fprintf(stderr,
228 		"%s: sorting by term structure order\n", _nc_progname);
229 	break;
230 
231     case S_TERMINFO:
232 	if (traceval)
233 	    (void) fprintf(stderr,
234 		"%s: sorting by terminfo name order\n", _nc_progname);
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,
243 		"%s: sorting by C variable order\n", _nc_progname);
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,
252 		"%s: sorting by termcap name order\n", _nc_progname);
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,
261 	    "%s: width = %d, tversion = %d, outform = %d\n",
262 	    _nc_progname, width, tversion, outform);
263 }
264 
265 static TERMTYPE *cur_type;
266 
267 static int
268 dump_predicate(int type, int idx)
269 /* predicate function to use for ordinary decompilation */
270 {
271     switch (type) {
272     case BOOLEAN:
273 	return (cur_type->Booleans[idx] == FALSE)
274 	    ? FAIL : cur_type->Booleans[idx];
275 
276     case NUMBER:
277 	return (cur_type->Numbers[idx] == ABSENT_NUMERIC)
278 	    ? FAIL : cur_type->Numbers[idx];
279 
280     case STRING:
281 	return (cur_type->Strings[idx] != ABSENT_STRING)
282 	    ? (int) TRUE : FAIL;
283     }
284 
285     return (FALSE);		/* pacify compiler */
286 }
287 
288 static void set_obsolete_termcaps(TERMTYPE * tp);
289 
290 /* is this the index of a function key string? */
291 #define FNKEY(i)	(((i)<= 65 && (i)>= 75) || ((i)<= 216 && (i)>= 268))
292 
293 static bool
294 version_filter(int type, int idx)
295 /* filter out capabilities we may want to suppress */
296 {
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) {
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 */
316 	switch (type) {
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 */
325 		return (TRUE);
326 	    else if (FNKEY(idx))	/* function keys */
327 		return (TRUE);
328 	    else if (idx == 147 || idx == 156 || idx == 157)	/* plab_norm,label_on,label_off */
329 		return (TRUE);
330 	    else
331 		return (FALSE);
332 	}
333 	break;
334 
335     case V_AIX:		/* AIX */
336 	switch (type) {
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 */
345 		return (TRUE);
346 	    else if (FNKEY(idx))	/* function keys */
347 		return (TRUE);
348 	    else
349 		return (FALSE);
350 	}
351 	break;
352 
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];
361 	}
362 	break;
363     }
364 
365     return (FALSE);		/* pacify the compiler */
366 }
367 
368 static void
369 force_wrap(void)
370 {
371     oldcol = column;
372     strcpy_DYN(&outbuf, trailer);
373     column = INDENT;
374 }
375 
376 static void
377 wrap_concat(const char *src)
378 {
379     int need = strlen(src);
380     int want = strlen(separator) + need;
381 
382     if (column > INDENT
383 	&& column + want > width) {
384 	force_wrap();
385     }
386     strcpy_DYN(&outbuf, src);
387     strcpy_DYN(&outbuf, separator);
388     column += need;
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
406 static int
407 termcap_length(const char *src)
408 {
409     static const char pattern[] = ":\\\n\t:";
410 
411     int len = 0;
412     const char *const t = src + strlen(src);
413 
414     while (*src != '\0') {
415 	IGNORE_SEP_TRAIL(src, t, pattern);
416 	src++;
417 	len++;
418     }
419     return len;
420 }
421 #else
422 #define termcap_length(src) strlen(src)
423 #endif
424 
425 static char *
426 fmt_complex(char *src, int level)
427 {
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;
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);
464 		    }
465 		}
466 		continue;
467 	    }
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;
479 		}
480 		_nc_warning("%%; without %%?");
481 	    }
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);
489 	    }
490 	    percent = 0;
491 	    break;
492 	default:
493 	    percent = 0;
494 	    break;
495 	}
496 	strncpy_DYN(&tmpbuf, src++, 1);
497     }
498     return src;
499 }
500 
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;
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 
528     strcpy_DYN(&outbuf, 0);
529     strcpy_DYN(&outbuf, tterm->term_names);
530     strcpy_DYN(&outbuf, separator);
531     column = outbuf.used;
532     force_wrap();
533 
534     for_each_boolean(j, tterm) {
535 	i = BoolIndirect(j);
536 	name = ExtBoolname(tterm, i, bool_names);
537 
538 	if (!version_filter(BOOLEAN, i))
539 	    continue;
540 	else if (isObsolete(outform, name))
541 	    continue;
542 
543 	predval = pred(BOOLEAN, i);
544 	if (predval != FAIL) {
545 	    (void) strcpy(buffer, name);
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 
557     for_each_number(j, tterm) {
558 	i = NumIndirect(j);
559 	name = ExtNumname(tterm, i, num_names);
560 
561 	if (!version_filter(NUMBER, i))
562 	    continue;
563 	else if (isObsolete(outform, name))
564 	    continue;
565 
566 	predval = pred(NUMBER, i);
567 	if (predval != FAIL) {
568 	    if (tterm->Numbers[i] < 0) {
569 		sprintf(buffer, "%s@", name);
570 	    } else {
571 		sprintf(buffer, "%s#%d", name, tterm->Numbers[i]);
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
583 	+ num_values * 2
584 	+ strlen(tterm->term_names) + 1;
585     if (len & 1)
586 	len++;
587 
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 
602     for_each_string(j, tterm) {
603 	i = StrIndirect(j);
604 	name = ExtStrname(tterm, i, str_names);
605 
606 	if (!version_filter(STRING, i))
607 	    continue;
608 	else if (isObsolete(outform, name))
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 	 */
616 	if (outform == F_TERMCAP) {
617 	    if (insert_character || parm_ich) {
618 		if (&tterm->Strings[i] == &enter_insert_mode
619 		    && enter_insert_mode == ABSENT_STRING) {
620 		    (void) strcpy(buffer, "im=");
621 		    WRAP_CONCAT;
622 		    continue;
623 		}
624 
625 		if (&tterm->Strings[i] == &exit_insert_mode
626 		    && exit_insert_mode == ABSENT_STRING) {
627 		    (void) strcpy(buffer, "ei=");
628 		    WRAP_CONCAT;
629 		    continue;
630 		}
631 	    }
632 	}
633 
634 	predval = pred(STRING, i);
635 	buffer[0] = '\0';
636 
637 	if (predval != FAIL) {
638 	    if (tterm->Strings[i] != ABSENT_STRING
639 		&& i + 1 > num_strings)
640 		num_strings = i + 1;
641 
642 	    if (!VALID_STRING(tterm->Strings[i])) {
643 		sprintf(buffer, "%s@", name);
644 		WRAP_CONCAT;
645 	    } else if (outform == F_TERMCAP || outform == F_TCONVERR) {
646 		int params = (i < (int) SIZEOF(parametrized)) ? parametrized[i] : 0;
647 		char *srccap = _nc_tic_expand(tterm->Strings[i], TRUE, numbers);
648 		char *cv = _nc_infotocap(name, srccap, params);
649 
650 		if (cv == 0) {
651 		    if (outform == F_TCONVERR) {
652 			sprintf(buffer, "%s=!!! %s WILL NOT CONVERT !!!",
653 			    name, srccap);
654 		    } else if (suppress_untranslatable) {
655 			continue;
656 		    } else {
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 		    }
670 		} else {
671 		    sprintf(buffer, "%s=%s", name, cv);
672 		}
673 		len += strlen(tterm->Strings[i]) + 1;
674 		WRAP_CONCAT;
675 	    } else {
676 		char *src = _nc_tic_expand(tterm->Strings[i],
677 		    outform == F_TERMINFO, numbers);
678 
679 		strcpy_DYN(&tmpbuf, 0);
680 		strcpy_DYN(&tmpbuf, name);
681 		strcpy_DYN(&tmpbuf, "=");
682 		if (pretty
683 		    && (outform == F_TERMINFO
684 			|| outform == F_VARIABLE)) {
685 		    fmt_complex(src, 1);
686 		} else {
687 		    strcpy_DYN(&tmpbuf, src);
688 		}
689 		len += strlen(tterm->Strings[i]) + 1;
690 		wrap_concat(tmpbuf.text);
691 		outcount = TRUE;
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      */
702     if (tversion == V_HPUX) {
703 	if (memory_lock) {
704 	    (void) sprintf(buffer, "meml=%s", memory_lock);
705 	    WRAP_CONCAT;
706 	}
707 	if (memory_unlock) {
708 	    (void) sprintf(buffer, "memu=%s", memory_unlock);
709 	    WRAP_CONCAT;
710 	}
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];
717 
718 	    tp = boxchars;
719 	    for (cp = acstrans; *cp; cp++) {
720 		sp = strchr(acs_chars, *cp);
721 		if (sp)
722 		    *tp++ = sp[1];
723 		else {
724 		    box_ok = FALSE;
725 		    break;
726 		}
727 	    }
728 	    tp[0] = '\0';
729 
730 	    if (box_ok) {
731 		(void) strcpy(buffer, "box1=");
732 		(void) strcat(buffer, _nc_tic_expand(boxchars,
733 			outform == F_TERMINFO, numbers));
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      */
743     if (outcount) {
744 	bool trimmed = FALSE;
745 	j = outbuf.used;
746 	if (j >= 2
747 	    && outbuf.text[j - 1] == '\t'
748 	    && outbuf.text[j - 2] == '\n') {
749 	    outbuf.used -= 2;
750 	    trimmed = TRUE;
751 	} else if (j >= 4
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;
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",
769 	tterm->term_names, len, outbuf.used, outbuf.text);
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      */
778     return (infodump ? len : termcap_length(outbuf.text));
779 }
780 
781 int
782 dump_entry(TERMTYPE * tterm, bool limited, int numbers, int (*pred) (int
783 	type, int idx))
784 /* dump a single entry */
785 {
786     int len, critlen;
787     const char *legend;
788     bool infodump;
789 
790     if (outform == F_TERMCAP || outform == F_TCONVERR) {
791 	critlen = MAX_TERMCAP_LENGTH;
792 	legend = "older termcap";
793 	infodump = FALSE;
794 	set_obsolete_termcaps(tterm);
795     } else {
796 	critlen = MAX_TERMINFO_LENGTH;
797 	legend = "terminfo";
798 	infodump = TRUE;
799     }
800 
801     if (((len = fmt_entry(tterm, pred, FALSE, infodump, numbers)) > critlen)
802 	&& limited) {
803 	PRINTF("# (untranslatable capabilities removed to fit entry within %d bytes)\n",
804 	    critlen);
805 	if ((len = fmt_entry(tterm, pred, TRUE, infodump, numbers)) > critlen) {
806 	    /*
807 	     * We pick on sgr because it's a nice long string capability that
808 	     * is really just an optimization hack.  Another good candidate is
809 	     * acsc since it is both long and unused by BSD termcap.
810 	     */
811 	    char *oldsgr = set_attributes;
812 	    char *oldacsc = acs_chars;
813 	    set_attributes = ABSENT_STRING;
814 	    PRINTF("# (sgr removed to fit entry within %d bytes)\n",
815 		critlen);
816 	    if ((len = fmt_entry(tterm, pred, TRUE, infodump, numbers)) > critlen) {
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) {
822 		int oldversion = tversion;
823 
824 		tversion = V_BSD;
825 		PRINTF("# (terminfo-only capabilities suppressed to fit entry within %d bytes)\n",
826 		    critlen);
827 
828 		if ((len = fmt_entry(tterm, pred, TRUE, infodump, numbers))
829 		    > critlen) {
830 		    (void) fprintf(stderr,
831 			"warning: %s entry is %d bytes long\n",
832 			_nc_first_name(tterm->term_names),
833 			len);
834 		    PRINTF(
835 			"# WARNING: this entry, %d bytes long, may core-dump %s libraries!\n",
836 			len, legend);
837 		}
838 		tversion = oldversion;
839 	    }
840 	    set_attributes = oldsgr;
841 	    acs_chars = oldacsc;
842 	}
843     }
844 
845     (void) fputs(outbuf.text, stdout);
846     return len;
847 }
848 
849 int
850 dump_uses(const char *name, bool infodump)
851 /* dump "use=" clauses in the appropriate format */
852 {
853     char buffer[MAX_TERMINFO_LENGTH];
854 
855     strcpy_DYN(&outbuf, 0);
856     (void) sprintf(buffer, "%s%s", infodump ? "use=" : "tc=", name);
857     wrap_concat(buffer);
858     (void) fputs(outbuf.text, stdout);
859     return outbuf.used;
860 }
861 
862 void
863 compare_entry(void (*hook) (int t, int i, const char *name), TERMTYPE * tp
864     GCC_UNUSED, bool quiet)
865 /* compare two entries */
866 {
867     int i, j;
868     NCURSES_CONST char *name;
869 
870     if (!quiet)
871 	fputs("    comparing booleans.\n", stdout);
872     for_each_boolean(j, tp) {
873 	i = BoolIndirect(j);
874 	name = ExtBoolname(tp, i, bool_names);
875 
876 	if (isObsolete(outform, name))
877 	    continue;
878 
879 	(*hook) (CMP_BOOLEAN, i, name);
880     }
881 
882     if (!quiet)
883 	fputs("    comparing numbers.\n", stdout);
884     for_each_number(j, tp) {
885 	i = NumIndirect(j);
886 	name = ExtNumname(tp, i, num_names);
887 
888 	if (isObsolete(outform, name))
889 	    continue;
890 
891 	(*hook) (CMP_NUMBER, i, name);
892     }
893 
894     if (!quiet)
895 	fputs("    comparing strings.\n", stdout);
896     for_each_string(j, tp) {
897 	i = StrIndirect(j);
898 	name = ExtStrname(tp, i, str_names);
899 
900 	if (isObsolete(outform, name))
901 	    continue;
902 
903 	(*hook) (CMP_STRING, i, name);
904     }
905 
906     /* (void) fputs("    comparing use entries.\n", stdout); */
907     (*hook) (CMP_USE, 0, "use");
908 
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 
921 static void
922 set_obsolete_termcaps(TERMTYPE * tp)
923 {
924 #include "capdefaults.c"
925 }
926 
927 /*
928  * Convert an alternate-character-set string to canonical form: sorted and
929  * unique.
930  */
931 void
932 repair_acsc(TERMTYPE * tp)
933 {
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;
961 		}
962 	    }
963 	    for (n = m = 0; n < sizeof(mapped); n++) {
964 		if (mapped[n]) {
965 		    acs_chars[m++] = n;
966 		    acs_chars[m++] = mapped[n];
967 		}
968 	    }
969 	    if (extra)
970 		acs_chars[m++] = extra;		/* garbage in, garbage out */
971 	    acs_chars[m] = 0;
972 	}
973     }
974 }
975