[BACK]Return to dump_entry.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tic

Annotation of src/usr.bin/tic/dump_entry.c, Revision 1.14

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