[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.13

1.13    ! millert     1: /*     $OpenBSD: dump_entry.c,v 1.12 2000/01/05 18:15:51 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.13    ! millert    43: MODULE_ID("$From: dump_entry.c,v 1.45 2000/01/08 22:19:05 Todd.Miller 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);
                    252: static void repair_acsc(TERMTYPE * tp);
1.1       millert   253:
                    254: /* is this the index of a function key string? */
                    255: #define FNKEY(i)       (((i)<= 65 && (i)>= 75) || ((i)<= 216 && (i)>= 268))
                    256:
1.10      millert   257: static bool
                    258: version_filter(int type, int idx)
1.1       millert   259: /* filter out capabilities we may want to suppress */
                    260: {
1.10      millert   261:     switch (tversion) {
                    262:     case V_ALLCAPS:            /* SVr4, XSI Curses */
                    263:        return (TRUE);
                    264:
                    265:     case V_SVR1:               /* System V Release 1, Ultrix */
                    266:        switch (type) {
1.1       millert   267:        case BOOLEAN:
                    268:            /* below and including xon_xoff */
                    269:            return ((idx <= 20) ? TRUE : FALSE);
                    270:        case NUMBER:
                    271:            /* below and including width_status_line */
                    272:            return ((idx <= 7) ? TRUE : FALSE);
                    273:        case STRING:
                    274:            /* below and including prtr_non */
                    275:            return ((idx <= 144) ? TRUE : FALSE);
                    276:        }
                    277:        break;
                    278:
                    279:     case V_HPUX:               /* Hewlett-Packard */
1.10      millert   280:        switch (type) {
1.1       millert   281:        case BOOLEAN:
                    282:            /* below and including xon_xoff */
                    283:            return ((idx <= 20) ? TRUE : FALSE);
                    284:        case NUMBER:
                    285:            /* below and including label_width */
                    286:            return ((idx <= 10) ? TRUE : FALSE);
                    287:        case STRING:
                    288:            if (idx <= 144)     /* below and including prtr_non */
1.10      millert   289:                return (TRUE);
1.1       millert   290:            else if (FNKEY(idx))        /* function keys */
1.10      millert   291:                return (TRUE);
                    292:            else if (idx == 147 || idx == 156 || idx == 157)    /* plab_norm,label_on,label_off */
                    293:                return (TRUE);
1.1       millert   294:            else
1.10      millert   295:                return (FALSE);
1.1       millert   296:        }
                    297:        break;
                    298:
                    299:     case V_AIX:                /* AIX */
1.10      millert   300:        switch (type) {
1.1       millert   301:        case BOOLEAN:
                    302:            /* below and including xon_xoff */
                    303:            return ((idx <= 20) ? TRUE : FALSE);
                    304:        case NUMBER:
                    305:            /* below and including width_status_line */
                    306:            return ((idx <= 7) ? TRUE : FALSE);
                    307:        case STRING:
                    308:            if (idx <= 144)     /* below and including prtr_non */
1.10      millert   309:                return (TRUE);
1.1       millert   310:            else if (FNKEY(idx))        /* function keys */
1.10      millert   311:                return (TRUE);
1.1       millert   312:            else
1.10      millert   313:                return (FALSE);
1.1       millert   314:        }
                    315:        break;
                    316:
                    317:     case V_BSD:                /* BSD */
1.10      millert   318:        switch (type) {
1.1       millert   319:        case BOOLEAN:
                    320:            return bool_from_termcap[idx];
                    321:        case NUMBER:
                    322:            return num_from_termcap[idx];
                    323:        case STRING:
                    324:            return str_from_termcap[idx];
                    325:        }
                    326:        break;
                    327:     }
                    328:
1.10      millert   329:     return (FALSE);            /* pacify the compiler */
1.1       millert   330: }
                    331:
1.11      millert   332: static void
1.10      millert   333: append_output(const char *src)
1.1       millert   334: {
1.10      millert   335:     if (src == 0) {
                    336:        out_used = 0;
                    337:        append_output("");
                    338:     } else {
                    339:        size_t need = strlen(src);
                    340:        size_t want = need + out_used + 1;
                    341:        if (want > out_size) {
                    342:            out_size += want;   /* be generous */
                    343:            if (outbuf == 0)
                    344:                outbuf = malloc(out_size);
                    345:            else
                    346:                outbuf = realloc(outbuf, out_size);
1.1       millert   347:        }
1.10      millert   348:        (void) strcpy(outbuf + out_used, src);
                    349:        out_used += need;
                    350:     }
1.1       millert   351: }
                    352:
1.11      millert   353: static void
1.10      millert   354: force_wrap(void)
1.1       millert   355: {
1.10      millert   356:     oldcol = column;
                    357:     append_output(trailer);
                    358:     column = INDENT;
1.1       millert   359: }
                    360:
1.11      millert   361: static void
1.10      millert   362: wrap_concat(const char *src)
1.1       millert   363: {
1.10      millert   364:     int need = strlen(src);
                    365:     int want = strlen(separator) + need;
1.1       millert   366:
1.10      millert   367:     if (column > INDENT
                    368:        && column + want > width) {
                    369:        force_wrap();
                    370:     }
                    371:     append_output(src);
                    372:     append_output(separator);
                    373:     column += need;
1.1       millert   374: }
                    375:
                    376: #define IGNORE_SEP_TRAIL(first,last,sep_trail) \
                    377:        if ((size_t)(last - first) > sizeof(sep_trail)-1 \
                    378:         && !strncmp(first, sep_trail, sizeof(sep_trail)-1)) \
                    379:                first += sizeof(sep_trail)-2
                    380:
                    381: /* Returns the nominal length of the buffer assuming it is termcap format,
                    382:  * i.e., the continuation sequence is treated as a single character ":".
                    383:  *
                    384:  * There are several implementations of termcap which read the text into a
                    385:  * fixed-size buffer.  Generally they strip the newlines from the text, but may
                    386:  * not do it until after the buffer is read.  Also, "tc=" resolution may be
                    387:  * expanded in the same buffer.  This function is useful for measuring the size
                    388:  * of the best fixed-buffer implementation; the worst case may be much worse.
                    389:  */
                    390: #ifdef TEST_TERMCAP_LENGTH
1.10      millert   391: static int
                    392: termcap_length(const char *src)
1.1       millert   393: {
1.10      millert   394:     static const char pattern[] = ":\\\n\t:";
1.1       millert   395:
1.10      millert   396:     int len = 0;
                    397:     const char *const t = src + strlen(src);
1.1       millert   398:
1.10      millert   399:     while (*src != '\0') {
                    400:        IGNORE_SEP_TRAIL(src, t, pattern);
                    401:        src++;
                    402:        len++;
                    403:     }
                    404:     return len;
1.1       millert   405: }
                    406: #else
                    407: #define termcap_length(src) strlen(src)
                    408: #endif
                    409:
1.10      millert   410: static char *
                    411: fmt_complex(char *dst, char *src, int level)
1.1       millert   412: {
1.10      millert   413:     int percent = 0;
                    414:     int n;
                    415:     bool if_then = strstr(src, "%?") != 0;
                    416:     bool params = !if_then && (strlen(src) > 50) && (strstr(src, "%p") != 0);
                    417:
                    418:     dst += strlen(dst);
                    419:     while (*src != '\0') {
                    420:        switch (*src) {
                    421:        case '\\':
                    422:            percent = 0;
                    423:            *dst++ = *src++;
                    424:            break;
                    425:        case '%':
                    426:            percent = 1;
                    427:            break;
                    428:        case '?':               /* "if" */
                    429:        case 't':               /* "then" */
                    430:        case 'e':               /* "else" */
                    431:            if (percent) {
                    432:                percent = 0;
                    433:                dst[-1] = '\n';
                    434:                for (n = 0; n <= level; n++)
                    435:                    *dst++ = '\t';
                    436:                *dst++ = '%';
                    437:                *dst++ = *src;
                    438:                *dst = '\0';
                    439:                if (*src++ == '?') {
                    440:                    src = fmt_complex(dst, src, level + 1);
                    441:                    dst += strlen(dst);
                    442:                } else if (level == 1) {
                    443:                    _nc_warning("%%%c without %%?", *src);
                    444:                }
                    445:                continue;
                    446:            }
                    447:            break;
                    448:        case ';':               /* "endif" */
                    449:            if (percent) {
                    450:                percent = 0;
                    451:                if (level > 1) {
                    452:                    dst[-1] = '\n';
                    453:                    for (n = 0; n < level; n++)
                    454:                        *dst++ = '\t';
                    455:                    *dst++ = '%';
                    456:                    *dst++ = *src++;
                    457:                    *dst = '\0';
                    458:                    return src;
1.1       millert   459:                }
1.10      millert   460:                _nc_warning("%%; without %%?");
                    461:            }
                    462:            break;
                    463:        case 'p':
                    464:            if (percent && params) {
                    465:                dst[-1] = '\n';
                    466:                for (n = 0; n <= level; n++)
                    467:                    *dst++ = '\t';
                    468:                *dst++ = '%';
                    469:            }
                    470:            percent = 0;
                    471:            break;
                    472:        default:
                    473:            percent = 0;
                    474:            break;
1.1       millert   475:        }
1.10      millert   476:        *dst++ = *src++;
                    477:     }
                    478:     *dst = '\0';
                    479:     return src;
1.1       millert   480: }
                    481:
1.10      millert   482: int
                    483: fmt_entry(TERMTYPE * tterm,
                    484:     int (*pred) (int type, int idx),
                    485:     bool suppress_untranslatable,
                    486:     bool infodump,
                    487:     int numbers)
                    488: {
                    489:     int i, j;
                    490:     char buffer[MAX_TERMINFO_LENGTH];
                    491:     NCURSES_CONST char *name;
                    492:     int predval, len;
                    493:     int num_bools = 0;
                    494:     int num_values = 0;
                    495:     int num_strings = 0;
                    496:     bool outcount = 0;
1.1       millert   497:
                    498: #define WRAP_CONCAT    \
                    499:        wrap_concat(buffer); \
                    500:        outcount = TRUE
                    501:
                    502:     len = 12;                  /* terminfo file-header */
                    503:
                    504:     if (pred == 0) {
                    505:        cur_type = tterm;
                    506:        pred = dump_predicate;
                    507:     }
                    508:
                    509:     append_output(0);
                    510:     append_output(tterm->term_names);
                    511:     append_output(separator);
                    512:     column = out_used;
                    513:     force_wrap();
                    514:
1.10      millert   515:     for_each_boolean(j, tterm) {
1.4       millert   516:        i = BoolIndirect(j);
1.10      millert   517:        name = ExtBoolname(tterm, i, bool_names);
1.1       millert   518:
                    519:        if (!version_filter(BOOLEAN, i))
                    520:            continue;
1.10      millert   521:        else if (isObsolete(outform, name))
1.1       millert   522:            continue;
                    523:
                    524:        predval = pred(BOOLEAN, i);
                    525:        if (predval != FAIL) {
1.4       millert   526:            (void) strcpy(buffer, name);
1.1       millert   527:            if (predval <= 0)
                    528:                (void) strcat(buffer, "@");
                    529:            else if (i + 1 > num_bools)
                    530:                num_bools = i + 1;
                    531:            WRAP_CONCAT;
                    532:        }
                    533:     }
                    534:
                    535:     if (column != INDENT)
                    536:        force_wrap();
                    537:
1.10      millert   538:     for_each_number(j, tterm) {
1.4       millert   539:        i = NumIndirect(j);
1.10      millert   540:        name = ExtNumname(tterm, i, num_names);
1.1       millert   541:
                    542:        if (!version_filter(NUMBER, i))
                    543:            continue;
1.10      millert   544:        else if (isObsolete(outform, name))
1.1       millert   545:            continue;
                    546:
                    547:        predval = pred(NUMBER, i);
                    548:        if (predval != FAIL) {
                    549:            if (tterm->Numbers[i] < 0) {
1.4       millert   550:                sprintf(buffer, "%s@", name);
1.1       millert   551:            } else {
1.4       millert   552:                sprintf(buffer, "%s#%d", name, tterm->Numbers[i]);
1.1       millert   553:                if (i + 1 > num_values)
                    554:                    num_values = i + 1;
                    555:            }
                    556:            WRAP_CONCAT;
                    557:        }
                    558:     }
                    559:
                    560:     if (column != INDENT)
                    561:        force_wrap();
                    562:
                    563:     len += num_bools
1.10      millert   564:        + num_values * 2
                    565:        + strlen(tterm->term_names) + 1;
1.1       millert   566:     if (len & 1)
1.10      millert   567:        len++;
1.1       millert   568:
                    569:     repair_acsc(tterm);
1.4       millert   570:     for_each_string(j, tterm) {
                    571:        i = StrIndirect(j);
1.10      millert   572:        name = ExtStrname(tterm, i, str_names);
1.1       millert   573:
                    574:        if (!version_filter(STRING, i))
                    575:            continue;
1.10      millert   576:        else if (isObsolete(outform, name))
1.1       millert   577:            continue;
                    578:
                    579:        /*
                    580:         * Some older versions of vi want rmir/smir to be defined
                    581:         * for ich/ich1 to work.  If they're not defined, force
                    582:         * them to be output as defined and empty.
                    583:         */
1.10      millert   584:        if (outform == F_TERMCAP) {
1.1       millert   585: #undef CUR
                    586: #define CUR tterm->
1.10      millert   587:            if (insert_character || parm_ich) {
1.1       millert   588:                if (&tterm->Strings[i] == &enter_insert_mode
1.10      millert   589:                    && enter_insert_mode == ABSENT_STRING) {
1.1       millert   590:                    (void) strcpy(buffer, "im=");
                    591:                    goto catenate;
                    592:                }
                    593:
                    594:                if (&tterm->Strings[i] == &exit_insert_mode
1.10      millert   595:                    && exit_insert_mode == ABSENT_STRING) {
1.1       millert   596:                    (void) strcpy(buffer, "ei=");
                    597:                    goto catenate;
                    598:                }
                    599:            }
                    600:
1.11      millert   601:            if (termcap_reset != ABSENT_STRING) {
                    602:                if (init_3string != ABSENT_STRING
                    603:                    && !strcmp(init_3string, termcap_reset))
                    604:                    DISCARD(init_3string);
                    605:
                    606:                if (reset_2string != ABSENT_STRING
                    607:                    && !strcmp(reset_2string, termcap_reset))
                    608:                    DISCARD(reset_2string);
                    609:            }
1.2       millert   610:        }
                    611:
1.1       millert   612:        predval = pred(STRING, i);
                    613:        buffer[0] = '\0';
                    614:        if (predval != FAIL) {
                    615:            if (tterm->Strings[i] != ABSENT_STRING
1.10      millert   616:                && i + 1 > num_strings)
1.1       millert   617:                num_strings = i + 1;
                    618:            if (!VALID_STRING(tterm->Strings[i]))
1.4       millert   619:                sprintf(buffer, "%s@", name);
1.10      millert   620:            else if (outform == F_TERMCAP || outform == F_TCONVERR) {
1.9       millert   621:                char *srccap = _nc_tic_expand(tterm->Strings[i], TRUE, numbers);
1.4       millert   622:                char *cv = _nc_infotocap(name, srccap, parametrized[i]);
1.1       millert   623:
1.10      millert   624:                if (cv == 0) {
1.1       millert   625:                    if (outform == F_TCONVERR)
1.10      millert   626:                        sprintf(buffer, "%s=!!! %s WILL NOT CONVERT !!!",
                    627:                            name, srccap);
1.1       millert   628:                    else if (suppress_untranslatable)
                    629:                        continue;
1.10      millert   630:                    else {
                    631:                        char *s = srccap, *d = buffer;
                    632:                        sprintf(d, "..%s=", name);
                    633:                        d += strlen(d);
                    634:                        while ((*d = *s++) != 0) {
                    635:                            if (*d == ':') {
                    636:                                *d++ = '\\';
                    637:                                *d = ':';
                    638:                            } else if (*d == '\\') {
                    639:                                *++d = *s++;
                    640:                            }
                    641:                            d++;
                    642:                        }
                    643:                    }
                    644:                } else
1.4       millert   645:                    sprintf(buffer, "%s=%s", name, cv);
1.1       millert   646:                len += strlen(tterm->Strings[i]) + 1;
1.10      millert   647:            } else {
                    648:                char *src = _nc_tic_expand(tterm->Strings[i],
                    649:                    outform == F_TERMINFO, numbers);
1.4       millert   650:                sprintf(buffer, "%s=", name);
1.8       millert   651:                if (pretty
1.10      millert   652:                    && (outform == F_TERMINFO
                    653:                        || outform == F_VARIABLE))
1.1       millert   654:                    fmt_complex(buffer + strlen(buffer), src, 1);
                    655:                else
                    656:                    strcat(buffer, src);
                    657:                len += strlen(tterm->Strings[i]) + 1;
                    658:            }
                    659:
1.10      millert   660:          catenate:
1.1       millert   661:            WRAP_CONCAT;
                    662:        }
                    663:     }
                    664:     len += num_strings * 2;
                    665:
                    666:     /*
                    667:      * This piece of code should be an effective inverse of the functions
                    668:      * postprocess_terminfo and postprocess_terminfo in parse_entry.c.
                    669:      * Much more work should be done on this to support dumping termcaps.
                    670:      */
1.10      millert   671:     if (tversion == V_HPUX) {
                    672:        if (memory_lock) {
1.1       millert   673:            (void) sprintf(buffer, "meml=%s", memory_lock);
                    674:            WRAP_CONCAT;
                    675:        }
1.10      millert   676:        if (memory_unlock) {
1.1       millert   677:            (void) sprintf(buffer, "memu=%s", memory_unlock);
                    678:            WRAP_CONCAT;
                    679:        }
1.10      millert   680:     } else if (tversion == V_AIX) {
                    681:        if (VALID_STRING(acs_chars)) {
                    682:            bool box_ok = TRUE;
                    683:            const char *acstrans = "lqkxjmwuvtn";
                    684:            const char *cp;
                    685:            char *tp, *sp, boxchars[11];
1.1       millert   686:
                    687:            tp = boxchars;
1.10      millert   688:            for (cp = acstrans; *cp; cp++) {
1.1       millert   689:                sp = strchr(acs_chars, *cp);
                    690:                if (sp)
                    691:                    *tp++ = sp[1];
1.10      millert   692:                else {
1.1       millert   693:                    box_ok = FALSE;
                    694:                    break;
                    695:                }
                    696:            }
                    697:            tp[0] = '\0';
                    698:
1.10      millert   699:            if (box_ok) {
1.1       millert   700:                (void) strcpy(buffer, "box1=");
1.10      millert   701:                (void) strcat(buffer, _nc_tic_expand(boxchars,
                    702:                        outform == F_TERMINFO, numbers));
1.1       millert   703:                WRAP_CONCAT;
                    704:            }
                    705:        }
                    706:     }
                    707:
                    708:     /*
                    709:      * kludge: trim off trailer to avoid an extra blank line
                    710:      * in infocmp -u output when there are no string differences
                    711:      */
1.10      millert   712:     if (outcount) {
1.1       millert   713:        j = out_used;
                    714:        if (j >= 2
1.10      millert   715:            && outbuf[j - 1] == '\t'
                    716:            && outbuf[j - 2] == '\n') {
1.1       millert   717:            out_used -= 2;
                    718:        } else if (j >= 4
1.10      millert   719:                && outbuf[j - 1] == ':'
                    720:                && outbuf[j - 2] == '\t'
                    721:                && outbuf[j - 3] == '\n'
                    722:            && outbuf[j - 4] == '\\') {
1.1       millert   723:            out_used -= 4;
                    724:        }
                    725:        outbuf[out_used] = '\0';
                    726:        column = oldcol;
                    727:     }
                    728: #if 0
                    729:     fprintf(stderr, "num_bools = %d\n", num_bools);
                    730:     fprintf(stderr, "num_values = %d\n", num_values);
                    731:     fprintf(stderr, "num_strings = %d\n", num_strings);
                    732:     fprintf(stderr, "term_names=%s, len=%d, strlen(outbuf)=%d, outbuf=%s\n",
1.10      millert   733:        tterm->term_names, len, out_used, outbuf);
1.1       millert   734: #endif
                    735:     /*
                    736:      * Here's where we use infodump to trigger a more stringent length check
                    737:      * for termcap-translation purposes.
                    738:      * Return the length of the raw entry, without tc= expansions,
                    739:      * It gives an idea of which entries are deadly to even *scan past*,
                    740:      * as opposed to *use*.
                    741:      */
1.10      millert   742:     return (infodump ? len : termcap_length(outbuf));
1.1       millert   743: }
                    744:
1.10      millert   745: int
                    746: dump_entry(TERMTYPE * tterm, bool limited, int numbers, int (*pred) (int
                    747:        type, int idx))
1.1       millert   748: /* dump a single entry */
                    749: {
1.10      millert   750:     int len, critlen;
                    751:     const char *legend;
                    752:     bool infodump;
1.1       millert   753:
1.10      millert   754:     if (outform == F_TERMCAP || outform == F_TCONVERR) {
1.1       millert   755:        critlen = MAX_TERMCAP_LENGTH;
                    756:        legend = "older termcap";
                    757:        infodump = FALSE;
                    758:        set_obsolete_termcaps(tterm);
1.10      millert   759:     } else {
1.1       millert   760:        critlen = MAX_TERMINFO_LENGTH;
                    761:        legend = "terminfo";
                    762:        infodump = TRUE;
                    763:     }
                    764:
1.10      millert   765:     if (((len = fmt_entry(tterm, pred, FALSE, infodump, numbers)) > critlen)
                    766:        && limited) {
1.11      millert   767:        PRINTF("# (untranslatable capabilities removed to fit entry within %d bytes)\n",
1.10      millert   768:            critlen);
                    769:        if ((len = fmt_entry(tterm, pred, TRUE, infodump, numbers)) > critlen) {
1.1       millert   770:            /*
                    771:             * We pick on sgr because it's a nice long string capability that
1.12      millert   772:             * is really just an optimization hack.  Another good candidate is
                    773:             * acsc since it is both long and unused by BSD termcap.
1.1       millert   774:             */
                    775:            char *oldsgr = set_attributes;
1.12      millert   776:            char *oldacsc = acs_chars;
1.3       millert   777:            set_attributes = ABSENT_STRING;
1.11      millert   778:            PRINTF("# (sgr removed to fit entry within %d bytes)\n",
1.10      millert   779:                critlen);
                    780:            if ((len = fmt_entry(tterm, pred, TRUE, infodump, numbers)) > critlen) {
1.12      millert   781:                acs_chars = ABSENT_STRING;
                    782:                PRINTF("# (acsc removed to fit entry within %d bytes)\n",
                    783:                    critlen);
                    784:            }
                    785:            if ((len = fmt_entry(tterm, pred, TRUE, infodump, numbers)) > critlen) {
1.1       millert   786:                int oldversion = tversion;
                    787:
                    788:                tversion = V_BSD;
1.11      millert   789:                PRINTF("# (terminfo-only capabilities suppressed to fit entry within %d bytes)\n",
1.10      millert   790:                    critlen);
1.1       millert   791:
1.10      millert   792:                if ((len = fmt_entry(tterm, pred, TRUE, infodump, numbers))
                    793:                    > critlen) {
1.1       millert   794:                    (void) fprintf(stderr,
1.10      millert   795:                        "warning: %s entry is %d bytes long\n",
                    796:                        _nc_first_name(tterm->term_names),
                    797:                        len);
1.11      millert   798:                    PRINTF(
1.10      millert   799:                        "# WARNING: this entry, %d bytes long, may core-dump %s libraries!\n",
                    800:                        len, legend);
1.1       millert   801:                }
                    802:                tversion = oldversion;
                    803:            }
                    804:            set_attributes = oldsgr;
1.12      millert   805:            acs_chars = oldacsc;
1.1       millert   806:        }
                    807:     }
                    808:
                    809:     (void) fputs(outbuf, stdout);
                    810:     return len;
                    811: }
                    812:
1.10      millert   813: int
                    814: dump_uses(const char *name, bool infodump)
1.1       millert   815: /* dump "use=" clauses in the appropriate format */
                    816: {
                    817:     char buffer[MAX_TERMINFO_LENGTH];
                    818:
                    819:     append_output(0);
1.10      millert   820:     (void) sprintf(buffer, "%s%s", infodump ? "use=" : "tc=", name);
1.1       millert   821:     wrap_concat(buffer);
                    822:     (void) fputs(outbuf, stdout);
                    823:     return out_used;
                    824: }
                    825:
1.10      millert   826: void
                    827: compare_entry(void (*hook) (int t, int i, const char *name), TERMTYPE * tp GCC_UNUSED)
1.1       millert   828: /* compare two entries */
                    829: {
1.10      millert   830:     int i, j;
                    831:     NCURSES_CONST char *name;
1.1       millert   832:
                    833:     (void) fputs("    comparing booleans.\n", stdout);
1.10      millert   834:     for_each_boolean(j, tp) {
1.4       millert   835:        i = BoolIndirect(j);
1.10      millert   836:        name = ExtBoolname(tp, i, bool_names);
1.1       millert   837:
1.10      millert   838:        if (isObsolete(outform, name))
1.1       millert   839:            continue;
                    840:
1.10      millert   841:        (*hook) (BOOLEAN, i, name);
1.1       millert   842:     }
                    843:
                    844:     (void) fputs("    comparing numbers.\n", stdout);
1.10      millert   845:     for_each_number(j, tp) {
1.4       millert   846:        i = NumIndirect(j);
1.10      millert   847:        name = ExtNumname(tp, i, num_names);
1.1       millert   848:
1.10      millert   849:        if (isObsolete(outform, name))
1.1       millert   850:            continue;
                    851:
1.10      millert   852:        (*hook) (NUMBER, i, name);
1.1       millert   853:     }
                    854:
                    855:     (void) fputs("    comparing strings.\n", stdout);
1.10      millert   856:     for_each_string(j, tp) {
1.4       millert   857:        i = StrIndirect(j);
1.10      millert   858:        name = ExtStrname(tp, i, str_names);
1.1       millert   859:
1.10      millert   860:        if (isObsolete(outform, name))
1.1       millert   861:            continue;
                    862:
1.10      millert   863:        (*hook) (STRING, i, name);
1.1       millert   864:     }
                    865: }
                    866:
                    867: #define NOTSET(s)      ((s) == 0)
                    868:
                    869: /*
                    870:  * This bit of legerdemain turns all the terminfo variable names into
                    871:  * references to locations in the arrays Booleans, Numbers, and Strings ---
                    872:  * precisely what's needed.
                    873:  */
                    874: #undef CUR
                    875: #define CUR tp->
                    876:
1.10      millert   877: static void
                    878: set_obsolete_termcaps(TERMTYPE * tp)
1.1       millert   879: {
                    880: #include "capdefaults.c"
                    881: }
                    882:
                    883: /*
                    884:  * Convert an alternate-character-set string to canonical form: sorted and
                    885:  * unique.
                    886:  */
1.10      millert   887: static void
                    888: repair_acsc(TERMTYPE * tp)
1.1       millert   889: {
1.10      millert   890:     if (VALID_STRING(acs_chars)) {
                    891:        size_t n, m;
                    892:        char mapped[256];
                    893:        char extra = 0;
                    894:        unsigned source;
                    895:        unsigned target;
                    896:        bool fix_needed = FALSE;
                    897:
                    898:        for (n = 0, source = 0; acs_chars[n] != 0; n++) {
                    899:            target = acs_chars[n];
                    900:            if (source >= target) {
                    901:                fix_needed = TRUE;
                    902:                break;
                    903:            }
                    904:            source = target;
                    905:            if (acs_chars[n + 1])
                    906:                n++;
                    907:        }
                    908:        if (fix_needed) {
                    909:            memset(mapped, 0, sizeof(mapped));
                    910:            for (n = 0; acs_chars[n] != 0; n++) {
                    911:                source = acs_chars[n];
                    912:                if ((target = (unsigned char) acs_chars[n + 1]) != 0) {
                    913:                    mapped[source] = target;
                    914:                    n++;
                    915:                } else {
                    916:                    extra = source;
1.1       millert   917:                }
                    918:            }
1.10      millert   919:            for (n = m = 0; n < sizeof(mapped); n++) {
                    920:                if (mapped[n]) {
                    921:                    acs_chars[m++] = n;
                    922:                    acs_chars[m++] = mapped[n];
1.1       millert   923:                }
                    924:            }
1.10      millert   925:            if (extra)
                    926:                acs_chars[m++] = extra;         /* garbage in, garbage out */
                    927:            acs_chars[m] = 0;
1.1       millert   928:        }
1.10      millert   929:     }
1.1       millert   930: }