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

Annotation of src/usr.bin/tic/tic.c, Revision 1.32

1.32    ! deraadt     1: /*     $OpenBSD: tic.c,v 1.31 2013/11/28 18:24:55 deraadt Exp $        */
1.24      niklas      2:
1.1       millert     3: /****************************************************************************
1.30      nicm        4:  * Copyright (c) 1998-2007,2008 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>                         *
1.30      nicm       34:  *     and: Thomas E. Dickey                        1996 on                 *
1.1       millert    35:  ****************************************************************************/
                     36:
                     37: /*
                     38:  *     tic.c --- Main program for terminfo compiler
                     39:  *                     by Eric S. Raymond
                     40:  *
                     41:  */
                     42:
                     43: #include <progs.priv.h>
1.21      millert    44: #include <sys/stat.h>
1.1       millert    45:
                     46: #include <dump_entry.h>
1.21      millert    47: #include <transform.h>
1.1       millert    48:
1.32    ! deraadt    49: MODULE_ID("$Id: tic.c,v 1.31 2013/11/28 18:24:55 deraadt Exp $")
1.1       millert    50:
                     51: const char *_nc_progname = "tic";
                     52:
1.14      millert    53: static FILE *log_fp;
                     54: static FILE *tmp_fp;
1.30      nicm       55: static bool capdump = FALSE;   /* running as infotocap? */
                     56: static bool infodump = FALSE;  /* running as captoinfo? */
1.14      millert    57: static bool showsummary = FALSE;
                     58: static const char *to_remove;
                     59:
1.30      nicm       60: static void (*save_check_termtype) (TERMTYPE *, bool);
                     61: static void check_termtype(TERMTYPE *tt, bool);
1.14      millert    62:
1.30      nicm       63: static const char usage_string[] = "\
                     64: [-e names] \
                     65: [-o dir] \
                     66: [-R name] \
                     67: [-v[n]] \
                     68: [-V] \
                     69: [-w[n]] \
                     70: [-\
                     71: 1\
                     72: a\
                     73: C\
                     74: c\
                     75: f\
                     76: G\
                     77: g\
                     78: I\
                     79: L\
                     80: N\
                     81: r\
                     82: s\
                     83: T\
                     84: t\
                     85: U\
                     86: x\
                     87: ] \
                     88: source-file\n";
1.14      millert    89:
1.30      nicm       90: #if NO_LEAKS
1.14      millert    91: static void
1.30      nicm       92: free_namelist(char **src)
1.14      millert    93: {
1.30      nicm       94:     if (src != 0) {
                     95:        int n;
                     96:        for (n = 0; src[n] != 0; ++n)
                     97:            free(src[n]);
                     98:        free(src);
                     99:     }
                    100: }
                    101: #endif
                    102:
                    103: static void
                    104: cleanup(char **namelst GCC_UNUSED)
                    105: {
                    106: #if NO_LEAKS
                    107:     free_namelist(namelst);
                    108: #endif
1.14      millert   109:     if (tmp_fp != 0)
                    110:        fclose(tmp_fp);
                    111:     if (to_remove != 0) {
1.11      millert   112: #if HAVE_REMOVE
1.14      millert   113:        remove(to_remove);
1.11      millert   114: #else
1.14      millert   115:        unlink(to_remove);
1.11      millert   116: #endif
1.14      millert   117:     }
1.2       millert   118: }
                    119:
1.14      millert   120: static void
                    121: failed(const char *msg)
1.2       millert   122: {
1.14      millert   123:     perror(msg);
1.30      nicm      124:     cleanup((char **) 0);
                    125:     ExitProgram(EXIT_FAILURE);
1.2       millert   126: }
                    127:
1.14      millert   128: static void
                    129: usage(void)
1.1       millert   130: {
1.14      millert   131:     static const char *const tbl[] =
                    132:     {
1.1       millert   133:        "Options:",
                    134:        "  -1         format translation output one capability per line",
1.19      millert   135: #if NCURSES_XNAMES
                    136:        "  -a         retain commented-out capabilities (sets -x also)",
                    137: #endif
1.30      nicm      138:        "  -C         translate entries to termcap source form",
1.1       millert   139:        "  -c         check only, validate input without compiling or translating",
1.30      nicm      140:        "  -e<names>  translate/compile only entries named by comma-separated list",
1.1       millert   141:        "  -f         format complex strings for readability",
1.8       millert   142:        "  -G         format %{number} to %'char'",
1.2       millert   143:        "  -g         format %'char' to %{number}",
1.30      nicm      144:        "  -I         translate entries to terminfo source form",
                    145:        "  -L         translate entries to full terminfo source form",
                    146:        "  -N         disable smart defaults for source translation",
1.1       millert   147:        "  -o<dir>    set output directory for compiled entry writes",
1.30      nicm      148:        "  -R<name>   restrict translation to given terminfo/termcap version",
1.1       millert   149:        "  -r         force resolution of all use entries in source translation",
                    150:        "  -s         print summary statistics",
1.30      nicm      151:        "  -T         remove size-restrictions on compiled description",
                    152: #if NCURSES_XNAMES
                    153:        "  -t         suppress commented-out capabilities",
                    154: #endif
                    155:        "  -U         suppress post-processing of entries",
                    156:        "  -V         print version",
1.1       millert   157:        "  -v[n]      set verbosity level",
                    158:        "  -w[n]      set format width for translation output",
1.7       millert   159: #if NCURSES_XNAMES
                    160:        "  -x         treat unknown capabilities as user-defined",
                    161: #endif
1.1       millert   162:        "",
                    163:        "Parameters:",
                    164:        "  <file>     file to translate or compile"
1.14      millert   165:     };
                    166:     size_t j;
1.1       millert   167:
1.15      millert   168:     fprintf(stderr, "Usage: %s %s\n", _nc_progname, usage_string);
1.21      millert   169:     for (j = 0; j < SIZEOF(tbl); j++) {
1.15      millert   170:        fputs(tbl[j], stderr);
                    171:        putc('\n', stderr);
                    172:     }
1.30      nicm      173:     ExitProgram(EXIT_FAILURE);
1.1       millert   174: }
                    175:
1.2       millert   176: #define L_BRACE '{'
                    177: #define R_BRACE '}'
                    178: #define S_QUOTE '\'';
                    179:
1.14      millert   180: static void
                    181: write_it(ENTRY * ep)
1.2       millert   182: {
1.14      millert   183:     unsigned n;
                    184:     int ch;
                    185:     char *s, *d, *t;
                    186:     char result[MAX_ENTRY_SIZE];
                    187:
                    188:     /*
                    189:      * Look for strings that contain %{number}, convert them to %'char',
                    190:      * which is shorter and runs a little faster.
                    191:      */
                    192:     for (n = 0; n < STRCOUNT; n++) {
                    193:        s = ep->tterm.Strings[n];
                    194:        if (VALID_STRING(s)
                    195:            && strchr(s, L_BRACE) != 0) {
                    196:            d = result;
                    197:            t = s;
                    198:            while ((ch = *t++) != 0) {
1.30      nicm      199:                *d++ = (char) ch;
1.14      millert   200:                if (ch == '\\') {
                    201:                    *d++ = *t++;
                    202:                } else if ((ch == '%')
1.21      millert   203:                           && (*t == L_BRACE)) {
1.14      millert   204:                    char *v = 0;
                    205:                    long value = strtol(t + 1, &v, 0);
                    206:                    if (v != 0
                    207:                        && *v == R_BRACE
                    208:                        && value > 0
                    209:                        && value != '\\'        /* FIXME */
                    210:                        && value < 127
                    211:                        && isprint((int) value)) {
                    212:                        *d++ = S_QUOTE;
1.30      nicm      213:                        *d++ = (char) value;
1.14      millert   214:                        *d++ = S_QUOTE;
                    215:                        t = (v + 1);
                    216:                    }
1.2       millert   217:                }
1.14      millert   218:            }
                    219:            *d = 0;
1.30      nicm      220:             if (strlen(result) < strlen(s)) {
                    221:                    /* new string is same length as what is there, or shorter */
                    222:                    strlcpy(s, result, strlen(s));
                    223:             }
1.2       millert   224:        }
1.14      millert   225:     }
1.2       millert   226:
1.14      millert   227:     _nc_set_type(_nc_first_name(ep->tterm.term_names));
                    228:     _nc_curr_line = ep->startline;
                    229:     _nc_write_entry(&ep->tterm);
1.2       millert   230: }
                    231:
1.14      millert   232: static bool
                    233: immedhook(ENTRY * ep GCC_UNUSED)
1.1       millert   234: /* write out entries with no use capabilities immediately to save storage */
                    235: {
1.21      millert   236: #if !HAVE_BIG_CORE
1.1       millert   237:     /*
                    238:      * This is strictly a core-economy kluge.  The really clean way to handle
                    239:      * compilation is to slurp the whole file into core and then do all the
                    240:      * name-collision checks and entry writes in one swell foop.  But the
                    241:      * terminfo master file is large enough that some core-poor systems swap
                    242:      * like crazy when you compile it this way...there have been reports of
                    243:      * this process taking *three hours*, rather than the twenty seconds or
                    244:      * less typical on my development box.
                    245:      *
                    246:      * So.  This hook *immediately* writes out the referenced entry if it
                    247:      * has no use capabilities.  The compiler main loop refrains from
                    248:      * adding the entry to the in-core list when this hook fires.  If some
                    249:      * other entry later needs to reference an entry that got written
                    250:      * immediately, that's OK; the resolution code will fetch it off disk
                    251:      * when it can't find it in core.
                    252:      *
                    253:      * Name collisions will still be detected, just not as cleanly.  The
                    254:      * write_entry() code complains before overwriting an entry that
                    255:      * postdates the time of tic's first call to write_entry().  Thus
                    256:      * it will complain about overwriting entries newly made during the
                    257:      * tic run, but not about overwriting ones that predate it.
                    258:      *
                    259:      * The reason this is a hook, and not in line with the rest of the
                    260:      * compiler code, is that the support for termcap fallback cannot assume
                    261:      * it has anywhere to spool out these entries!
                    262:      *
                    263:      * The _nc_set_type() call here requires a compensating one in
                    264:      * _nc_parse_entry().
                    265:      *
                    266:      * If you define HAVE_BIG_CORE, you'll disable this kluge.  This will
                    267:      * make tic a bit faster (because the resolution code won't have to do
                    268:      * disk I/O nearly as often).
                    269:      */
1.14      millert   270:     if (ep->nuses == 0) {
                    271:        int oldline = _nc_curr_line;
1.1       millert   272:
1.2       millert   273:        write_it(ep);
1.1       millert   274:        _nc_curr_line = oldline;
                    275:        free(ep->tterm.str_table);
1.14      millert   276:        return (TRUE);
1.1       millert   277:     }
                    278: #endif /* HAVE_BIG_CORE */
1.14      millert   279:     return (FALSE);
1.1       millert   280: }
                    281:
1.14      millert   282: static void
                    283: put_translate(int c)
1.1       millert   284: /* emit a comment char, translating terminfo names to termcap names */
                    285: {
                    286:     static bool in_name = FALSE;
1.14      millert   287:     static size_t have, used;
                    288:     static char *namebuf, *suffix;
1.1       millert   289:
1.14      millert   290:     if (in_name) {
                    291:        if (used + 1 >= have) {
                    292:            have += 132;
1.20      millert   293:            namebuf = typeRealloc(char, have, namebuf);
                    294:            suffix = typeRealloc(char, have, suffix);
1.1       millert   295:        }
1.14      millert   296:        if (c == '\n' || c == '@') {
                    297:            namebuf[used++] = '\0';
                    298:            (void) putchar('<');
                    299:            (void) fputs(namebuf, stdout);
1.1       millert   300:            putchar(c);
1.14      millert   301:            in_name = FALSE;
                    302:        } else if (c != '>') {
1.30      nicm      303:            namebuf[used++] = (char) c;
1.14      millert   304:        } else {                /* ah! candidate name! */
                    305:            char *up;
                    306:            NCURSES_CONST char *tp;
                    307:
                    308:            namebuf[used++] = '\0';
                    309:            in_name = FALSE;
                    310:
                    311:            suffix[0] = '\0';
                    312:            if ((up = strchr(namebuf, '#')) != 0
                    313:                || (up = strchr(namebuf, '=')) != 0
                    314:                || ((up = strchr(namebuf, '@')) != 0 && up[1] == '>')) {
1.30      nicm      315:                    (void) strlcpy(suffix, up, have);
1.14      millert   316:                *up = '\0';
                    317:            }
1.1       millert   318:
1.14      millert   319:            if ((tp = nametrans(namebuf)) != 0) {
                    320:                (void) putchar(':');
                    321:                (void) fputs(tp, stdout);
                    322:                (void) fputs(suffix, stdout);
                    323:                (void) putchar(':');
                    324:            } else {
                    325:                /* couldn't find a translation, just dump the name */
                    326:                (void) putchar('<');
                    327:                (void) fputs(namebuf, stdout);
                    328:                (void) fputs(suffix, stdout);
                    329:                (void) putchar('>');
                    330:            }
1.1       millert   331:        }
1.14      millert   332:     } else {
                    333:        used = 0;
                    334:        if (c == '<') {
                    335:            in_name = TRUE;
                    336:        } else {
                    337:            putchar(c);
1.1       millert   338:        }
                    339:     }
                    340: }
                    341:
                    342: /* Returns a string, stripped of leading/trailing whitespace */
1.14      millert   343: static char *
                    344: stripped(char *src)
1.1       millert   345: {
1.30      nicm      346:     while (isspace(UChar(*src)))
1.14      millert   347:        src++;
                    348:     if (*src != '\0') {
1.28      deraadt   349:        char *dst;
                    350:        size_t len;
                    351:
                    352:        if ((dst = strdup(src)) == NULL)
1.30      nicm      353:            failed("strdup");
1.28      deraadt   354:        len = strlen(dst);
1.30      nicm      355:        while (--len != 0 && isspace(UChar(dst[len])))
1.14      millert   356:            dst[len] = '\0';
                    357:        return dst;
                    358:     }
                    359:     return 0;
1.1       millert   360: }
                    361:
1.21      millert   362: static FILE *
                    363: open_input(const char *filename)
                    364: {
                    365:     FILE *fp = fopen(filename, "r");
                    366:     struct stat sb;
                    367:
                    368:     if (fp == 0) {
                    369:        fprintf(stderr, "%s: Can't open %s\n", _nc_progname, filename);
1.30      nicm      370:        ExitProgram(EXIT_FAILURE);
1.21      millert   371:     }
                    372:     if (fstat(fileno(fp), &sb) < 0
                    373:        || (sb.st_mode & S_IFMT) != S_IFREG) {
                    374:        fprintf(stderr, "%s: %s is not a file\n", _nc_progname, filename);
1.30      nicm      375:        ExitProgram(EXIT_FAILURE);
1.21      millert   376:     }
                    377:     return fp;
                    378: }
                    379:
1.1       millert   380: /* Parse the "-e" option-value into a list of names */
1.30      nicm      381: static char **
1.14      millert   382: make_namelist(char *src)
1.1       millert   383: {
1.30      nicm      384:     char **dst = 0;
1.1       millert   385:
1.14      millert   386:     char *s, *base;
                    387:     unsigned pass, n, nn;
                    388:     char buffer[BUFSIZ];
                    389:
                    390:     if (src == 0) {
                    391:        /* EMPTY */ ;
                    392:     } else if (strchr(src, '/') != 0) {                /* a filename */
1.21      millert   393:        FILE *fp = open_input(src);
1.14      millert   394:
                    395:        for (pass = 1; pass <= 2; pass++) {
                    396:            nn = 0;
1.29      cloder    397:            while (fgets(buffer, sizeof(buffer), fp) != NULL) {
1.14      millert   398:                if ((s = stripped(buffer)) != 0) {
                    399:                    if (dst != 0)
                    400:                        dst[nn] = s;
1.30      nicm      401:                    else
                    402:                        free(s);
1.14      millert   403:                    nn++;
1.1       millert   404:                }
1.14      millert   405:            }
                    406:            if (pass == 1) {
1.30      nicm      407:                dst = typeCalloc(char *, nn + 1);
1.14      millert   408:                rewind(fp);
                    409:            }
                    410:        }
                    411:        fclose(fp);
                    412:     } else {                   /* literal list of names */
                    413:        for (pass = 1; pass <= 2; pass++) {
                    414:            for (n = nn = 0, base = src;; n++) {
                    415:                int mark = src[n];
                    416:                if (mark == ',' || mark == '\0') {
                    417:                    if (pass == 1) {
                    418:                        nn++;
                    419:                    } else {
                    420:                        src[n] = '\0';
                    421:                        if ((s = stripped(base)) != 0)
                    422:                            dst[nn++] = s;
                    423:                        base = &src[n + 1];
                    424:                    }
1.1       millert   425:                }
1.14      millert   426:                if (mark == '\0')
                    427:                    break;
                    428:            }
                    429:            if (pass == 1)
1.30      nicm      430:                dst = typeCalloc(char *, nn + 1);
1.1       millert   431:        }
1.14      millert   432:     }
1.30      nicm      433:     if (showsummary && (dst != 0)) {
1.14      millert   434:        fprintf(log_fp, "Entries that will be compiled:\n");
                    435:        for (n = 0; dst[n] != 0; n++)
1.30      nicm      436:            fprintf(log_fp, "%u:%s\n", n + 1, dst[n]);
1.14      millert   437:     }
                    438:     return dst;
1.1       millert   439: }
                    440:
1.14      millert   441: static bool
1.30      nicm      442: matches(char **needle, const char *haystack)
1.1       millert   443: /* does entry in needle list match |-separated field in haystack? */
                    444: {
1.14      millert   445:     bool code = FALSE;
                    446:     size_t n;
1.1       millert   447:
1.14      millert   448:     if (needle != 0) {
                    449:        for (n = 0; needle[n] != 0; n++) {
                    450:            if (_nc_name_match(haystack, needle[n], "|")) {
                    451:                code = TRUE;
                    452:                break;
                    453:            }
1.1       millert   454:        }
1.14      millert   455:     } else
                    456:        code = TRUE;
                    457:     return (code);
1.1       millert   458: }
                    459:
1.17      millert   460: static FILE *
                    461: open_tempfile(char *name)
                    462: {
                    463:     FILE *result = 0;
                    464: #if HAVE_MKSTEMP
                    465:     int fd = mkstemp(name);
                    466:     if (fd >= 0)
                    467:        result = fdopen(fd, "w");
                    468: #else
                    469:     if (tmpnam(name) != 0)
                    470:        result = fopen(name, "w");
                    471: #endif
                    472:     return result;
                    473: }
                    474:
1.14      millert   475: int
                    476: main(int argc, char *argv[])
1.1       millert   477: {
1.14      millert   478:     char my_tmpname[PATH_MAX];
                    479:     int v_opt = -1, debug_level;
                    480:     int smart_defaults = TRUE;
                    481:     char *termcap;
                    482:     ENTRY *qp;
                    483:
                    484:     int this_opt, last_opt = '?';
                    485:
                    486:     int outform = F_TERMINFO;  /* output format */
                    487:     int sortmode = S_TERMINFO; /* sort_mode */
                    488:
                    489:     int width = 60;
                    490:     bool formatted = FALSE;    /* reformat complex strings? */
1.30      nicm      491:     bool literal = FALSE;      /* suppress post-processing? */
1.14      millert   492:     int numbers = 0;           /* format "%'char'" to/from "%{number}" */
                    493:     bool forceresolve = FALSE; /* force resolution */
                    494:     bool limited = TRUE;
                    495:     char *tversion = (char *) NULL;
                    496:     const char *source_file = "terminfo";
1.30      nicm      497:     char **namelst = 0;
1.14      millert   498:     char *outdir = (char *) NULL;
                    499:     bool check_only = FALSE;
1.30      nicm      500:     bool suppress_untranslatable = FALSE;
1.32    ! deraadt   501:
        !           502:     if (pledge("stdio rpath wpath cpath", NULL) == -1)
        !           503:        perror("pledge");
1.14      millert   504:
                    505:     log_fp = stderr;
                    506:
1.30      nicm      507:     _nc_progname = _nc_rootname(argv[0]);
1.14      millert   508:
1.21      millert   509:     if ((infodump = (strcmp(_nc_progname, PROG_CAPTOINFO) == 0)) != FALSE) {
1.14      millert   510:        outform = F_TERMINFO;
                    511:        sortmode = S_TERMINFO;
                    512:     }
1.21      millert   513:     if ((capdump = (strcmp(_nc_progname, PROG_INFOTOCAP) == 0)) != FALSE) {
1.14      millert   514:        outform = F_TERMCAP;
                    515:        sortmode = S_TERMCAP;
                    516:     }
1.7       millert   517: #if NCURSES_XNAMES
1.14      millert   518:     use_extended_names(FALSE);
1.7       millert   519: #endif
1.1       millert   520:
1.14      millert   521:     /*
                    522:      * Processing arguments is a little complicated, since someone made a
                    523:      * design decision to allow the numeric values for -w, -v options to
                    524:      * be optional.
                    525:      */
                    526:     while ((this_opt = getopt(argc, argv,
1.30      nicm      527:                              "0123456789CILNR:TUVace:fGgo:rstvwx")) != -1) {
1.14      millert   528:        if (isdigit(this_opt)) {
                    529:            switch (last_opt) {
                    530:            case 'v':
                    531:                v_opt = (v_opt * 10) + (this_opt - '0');
                    532:                break;
                    533:            case 'w':
                    534:                width = (width * 10) + (this_opt - '0');
                    535:                break;
                    536:            default:
                    537:                if (this_opt != '1')
                    538:                    usage();
                    539:                last_opt = this_opt;
                    540:                width = 0;
                    541:            }
                    542:            continue;
                    543:        }
                    544:        switch (this_opt) {
                    545:        case 'C':
                    546:            capdump = TRUE;
                    547:            outform = F_TERMCAP;
                    548:            sortmode = S_TERMCAP;
                    549:            break;
                    550:        case 'I':
                    551:            infodump = TRUE;
                    552:            outform = F_TERMINFO;
                    553:            sortmode = S_TERMINFO;
                    554:            break;
                    555:        case 'L':
                    556:            infodump = TRUE;
                    557:            outform = F_VARIABLE;
                    558:            sortmode = S_VARIABLE;
                    559:            break;
                    560:        case 'N':
                    561:            smart_defaults = FALSE;
1.30      nicm      562:            literal = TRUE;
1.14      millert   563:            break;
                    564:        case 'R':
                    565:            tversion = optarg;
                    566:            break;
                    567:        case 'T':
                    568:            limited = FALSE;
                    569:            break;
1.30      nicm      570:        case 'U':
                    571:            literal = TRUE;
                    572:            break;
1.14      millert   573:        case 'V':
1.21      millert   574:            puts(curses_version());
1.30      nicm      575:            cleanup(namelst);
                    576:            ExitProgram(EXIT_SUCCESS);
1.14      millert   577:        case 'c':
                    578:            check_only = TRUE;
                    579:            break;
                    580:        case 'e':
                    581:            namelst = make_namelist(optarg);
                    582:            break;
                    583:        case 'f':
                    584:            formatted = TRUE;
                    585:            break;
                    586:        case 'G':
                    587:            numbers = 1;
                    588:            break;
                    589:        case 'g':
                    590:            numbers = -1;
                    591:            break;
                    592:        case 'o':
                    593:            outdir = optarg;
                    594:            break;
                    595:        case 'r':
                    596:            forceresolve = TRUE;
                    597:            break;
                    598:        case 's':
                    599:            showsummary = TRUE;
                    600:            break;
                    601:        case 'v':
                    602:            v_opt = 0;
                    603:            break;
                    604:        case 'w':
                    605:            width = 0;
                    606:            break;
1.7       millert   607: #if NCURSES_XNAMES
1.30      nicm      608:        case 't':
                    609:            _nc_disable_period = FALSE;
                    610:            suppress_untranslatable = TRUE;
                    611:            break;
1.19      millert   612:        case 'a':
                    613:            _nc_disable_period = TRUE;
                    614:            /* FALLTHRU */
1.14      millert   615:        case 'x':
                    616:            use_extended_names(TRUE);
                    617:            break;
1.7       millert   618: #endif
1.14      millert   619:        default:
                    620:            usage();
1.1       millert   621:        }
1.14      millert   622:        last_opt = this_opt;
                    623:     }
1.1       millert   624:
1.14      millert   625:     debug_level = (v_opt > 0) ? v_opt : (v_opt == 0);
1.16      millert   626:     set_trace_level(debug_level);
1.6       millert   627:
1.14      millert   628:     if (_nc_tracing) {
1.30      nicm      629:        save_check_termtype = _nc_check_termtype2;
                    630:        _nc_check_termtype2 = check_termtype;
1.14      millert   631:     }
1.21      millert   632: #if !HAVE_BIG_CORE
1.14      millert   633:     /*
                    634:      * Aaargh! immedhook seriously hoses us!
                    635:      *
                    636:      * One problem with immedhook is it means we can't do -e.  Problem
                    637:      * is that we can't guarantee that for each terminal listed, all the
                    638:      * terminals it depends on will have been kept in core for reference
                    639:      * resolution -- in fact it's certain the primitive types at the end
                    640:      * of reference chains *won't* be in core unless they were explicitly
                    641:      * in the select list themselves.
                    642:      */
                    643:     if (namelst && (!infodump && !capdump)) {
                    644:        (void) fprintf(stderr,
1.21      millert   645:                       "Sorry, -e can't be used without -I or -C\n");
1.30      nicm      646:        cleanup(namelst);
                    647:        ExitProgram(EXIT_FAILURE);
1.14      millert   648:     }
1.4       millert   649: #endif /* HAVE_BIG_CORE */
                    650:
1.14      millert   651:     if (optind < argc) {
                    652:        source_file = argv[optind++];
1.1       millert   653:        if (optind < argc) {
1.14      millert   654:            fprintf(stderr,
1.21      millert   655:                    "%s: Too many file names.  Usage:\n\t%s %s",
                    656:                    _nc_progname,
                    657:                    _nc_progname,
                    658:                    usage_string);
1.30      nicm      659:            ExitProgram(EXIT_FAILURE);
1.14      millert   660:        }
                    661:     } else {
                    662:        if (infodump == TRUE) {
                    663:            /* captoinfo's no-argument case */
1.30      nicm      664:            source_file = "/etc/termcap";
1.14      millert   665:            if ((termcap = getenv("TERMCAP")) != 0
                    666:                && (namelst = make_namelist(getenv("TERM"))) != 0) {
1.27      avsm      667:                strlcpy(my_tmpname, "/tmp/XXXXXXXXXX", sizeof my_tmpname);
1.14      millert   668:                if (access(termcap, F_OK) == 0) {
                    669:                    /* file exists */
                    670:                    source_file = termcap;
1.26      deraadt   671:                } else if ((tmp_fp = open_tempfile(my_tmpname)) != 0) {
1.17      millert   672:                    source_file = my_tmpname;
1.14      millert   673:                    fprintf(tmp_fp, "%s\n", termcap);
                    674:                    fclose(tmp_fp);
1.21      millert   675:                    tmp_fp = open_input(source_file);
1.14      millert   676:                    to_remove = source_file;
                    677:                } else {
1.30      nicm      678:                    failed("tmpnam");
1.1       millert   679:                }
1.14      millert   680:            }
1.1       millert   681:        } else {
1.14      millert   682:            /* tic */
                    683:            fprintf(stderr,
1.21      millert   684:                    "%s: File name needed.  Usage:\n\t%s %s",
                    685:                    _nc_progname,
                    686:                    _nc_progname,
                    687:                    usage_string);
1.30      nicm      688:            cleanup(namelst);
                    689:            ExitProgram(EXIT_FAILURE);
1.1       millert   690:        }
1.14      millert   691:     }
                    692:
1.21      millert   693:     if (tmp_fp == 0)
                    694:        tmp_fp = open_input(source_file);
1.1       millert   695:
1.14      millert   696:     if (infodump)
                    697:        dump_init(tversion,
1.21      millert   698:                  smart_defaults
                    699:                  ? outform
                    700:                  : F_LITERAL,
                    701:                  sortmode, width, debug_level, formatted);
1.14      millert   702:     else if (capdump)
                    703:        dump_init(tversion,
1.21      millert   704:                  outform,
                    705:                  sortmode, width, debug_level, FALSE);
1.1       millert   706:
1.14      millert   707:     /* parse entries out of the source file */
                    708:     _nc_set_source(source_file);
1.21      millert   709: #if !HAVE_BIG_CORE
1.14      millert   710:     if (!(check_only || infodump || capdump))
                    711:        _nc_set_writedir(outdir);
1.1       millert   712: #endif /* HAVE_BIG_CORE */
1.14      millert   713:     _nc_read_entry_source(tmp_fp, (char *) NULL,
1.30      nicm      714:                          !smart_defaults || literal, FALSE,
                    715:                          ((check_only || infodump || capdump)
                    716:                           ? NULLHOOK
                    717:                           : immedhook));
1.14      millert   718:
                    719:     /* do use resolution */
                    720:     if (check_only || (!infodump && !capdump) || forceresolve) {
1.30      nicm      721:        if (!_nc_resolve_uses2(TRUE, literal) && !check_only) {
                    722:            cleanup(namelst);
                    723:            ExitProgram(EXIT_FAILURE);
1.14      millert   724:        }
                    725:     }
1.1       millert   726:
1.14      millert   727:     /* length check */
                    728:     if (check_only && (capdump || infodump)) {
                    729:        for_entry_list(qp) {
                    730:            if (matches(namelst, qp->tterm.term_names)) {
1.30      nicm      731:                int len = fmt_entry(&qp->tterm, NULL, FALSE, TRUE, infodump, numbers);
1.14      millert   732:
                    733:                if (len > (infodump ? MAX_TERMINFO_LENGTH : MAX_TERMCAP_LENGTH))
                    734:                    (void) fprintf(stderr,
1.21      millert   735:                                   "warning: resolved %s entry is %d bytes long\n",
                    736:                                   _nc_first_name(qp->tterm.term_names),
                    737:                                   len);
1.2       millert   738:            }
                    739:        }
1.14      millert   740:     }
1.1       millert   741:
1.14      millert   742:     /* write or dump all entries */
                    743:     if (!check_only) {
                    744:        if (!infodump && !capdump) {
                    745:            _nc_set_writedir(outdir);
1.19      millert   746:            for_entry_list(qp) {
1.1       millert   747:                if (matches(namelst, qp->tterm.term_names))
1.19      millert   748:                    write_it(qp);
                    749:            }
1.14      millert   750:        } else {
                    751:            /* this is in case infotocap() generates warnings */
                    752:            _nc_curr_col = _nc_curr_line = -1;
1.1       millert   753:
1.14      millert   754:            for_entry_list(qp) {
                    755:                if (matches(namelst, qp->tterm.term_names)) {
                    756:                    int j = qp->cend - qp->cstart;
                    757:                    int len = 0;
                    758:
                    759:                    /* this is in case infotocap() generates warnings */
                    760:                    _nc_set_type(_nc_first_name(qp->tterm.term_names));
                    761:
                    762:                    (void) fseek(tmp_fp, qp->cstart, SEEK_SET);
1.30      nicm      763:                    while (j-- > 0) {
1.14      millert   764:                        if (infodump)
                    765:                            (void) putchar(fgetc(tmp_fp));
                    766:                        else
                    767:                            put_translate(fgetc(tmp_fp));
                    768:                    }
                    769:
1.30      nicm      770:                    dump_entry(&qp->tterm, suppress_untranslatable,
                    771:                               limited, numbers, NULL);
                    772:                    for (j = 0; j < (int) qp->nuses; j++)
                    773:                        dump_uses(qp->uses[j].name, !capdump);
                    774:                    len = show_entry();
1.14      millert   775:                    if (debug_level != 0 && !limited)
                    776:                        printf("# length=%d\n", len);
1.1       millert   777:                }
                    778:            }
1.25      millert   779:            if (!namelst && _nc_tail) {
1.14      millert   780:                int c, oldc = '\0';
                    781:                bool in_comment = FALSE;
                    782:                bool trailing_comment = FALSE;
                    783:
                    784:                (void) fseek(tmp_fp, _nc_tail->cend, SEEK_SET);
                    785:                while ((c = fgetc(tmp_fp)) != EOF) {
                    786:                    if (oldc == '\n') {
                    787:                        if (c == '#') {
                    788:                            trailing_comment = TRUE;
                    789:                            in_comment = TRUE;
                    790:                        } else {
                    791:                            in_comment = FALSE;
1.1       millert   792:                        }
                    793:                    }
1.14      millert   794:                    if (trailing_comment
                    795:                        && (in_comment || (oldc == '\n' && c == '\n')))
                    796:                        putchar(c);
                    797:                    oldc = c;
1.1       millert   798:                }
                    799:            }
                    800:        }
1.14      millert   801:     }
1.1       millert   802:
1.14      millert   803:     /* Show the directory into which entries were written, and the total
                    804:      * number of entries
                    805:      */
                    806:     if (showsummary
                    807:        && (!(check_only || infodump || capdump))) {
                    808:        int total = _nc_tic_written();
                    809:        if (total != 0)
                    810:            fprintf(log_fp, "%d entries written to %s\n",
1.21      millert   811:                    total,
                    812:                    _nc_tic_dir((char *) 0));
1.14      millert   813:        else
                    814:            fprintf(log_fp, "No entries written\n");
                    815:     }
1.30      nicm      816:     cleanup(namelst);
                    817:     ExitProgram(EXIT_SUCCESS);
1.6       millert   818: }
                    819:
                    820: /*
                    821:  * This bit of legerdemain turns all the terminfo variable names into
                    822:  * references to locations in the arrays Booleans, Numbers, and Strings ---
                    823:  * precisely what's needed (see comp_parse.c).
                    824:  */
1.30      nicm      825: #undef CUR
                    826: #define CUR tp->
                    827:
                    828: /*
                    829:  * Check if the alternate character-set capabilities are consistent.
                    830:  */
                    831: static void
                    832: check_acs(TERMTYPE *tp)
                    833: {
                    834:     if (VALID_STRING(acs_chars)) {
                    835:        const char *boxes = "lmkjtuvwqxn";
                    836:        char mapped[256];
                    837:        char missing[256];
                    838:        const char *p;
                    839:        char *q;
                    840:
                    841:        memset(mapped, 0, sizeof(mapped));
                    842:        for (p = acs_chars; *p != '\0'; p += 2) {
                    843:            if (p[1] == '\0') {
                    844:                _nc_warning("acsc has odd number of characters");
                    845:                break;
                    846:            }
                    847:            mapped[UChar(p[0])] = p[1];
                    848:        }
                    849:
                    850:        if (mapped[UChar('I')] && !mapped[UChar('i')]) {
                    851:            _nc_warning("acsc refers to 'I', which is probably an error");
                    852:        }
                    853:
                    854:        for (p = boxes, q = missing; *p != '\0'; ++p) {
                    855:            if (!mapped[UChar(p[0])]) {
                    856:                *q++ = p[0];
                    857:            }
                    858:        }
                    859:        *q = '\0';
                    860:
                    861:        assert(strlen(missing) <= strlen(boxes));
                    862:        if (*missing != '\0' && strcmp(missing, boxes)) {
                    863:            _nc_warning("acsc is missing some line-drawing mapping: %s", missing);
                    864:        }
                    865:     }
                    866: }
                    867:
                    868: /*
                    869:  * Check if the color capabilities are consistent
                    870:  */
                    871: static void
                    872: check_colors(TERMTYPE *tp)
                    873: {
                    874:     if ((max_colors > 0) != (max_pairs > 0)
                    875:        || ((max_colors > max_pairs) && (initialize_pair == 0)))
                    876:        _nc_warning("inconsistent values for max_colors (%d) and max_pairs (%d)",
                    877:                    max_colors, max_pairs);
                    878:
                    879:     PAIRED(set_foreground, set_background);
                    880:     PAIRED(set_a_foreground, set_a_background);
                    881:     PAIRED(set_color_pair, initialize_pair);
                    882:
                    883:     if (VALID_STRING(set_foreground)
                    884:        && VALID_STRING(set_a_foreground)
                    885:        && !_nc_capcmp(set_foreground, set_a_foreground))
                    886:        _nc_warning("expected setf/setaf to be different");
                    887:
                    888:     if (VALID_STRING(set_background)
                    889:        && VALID_STRING(set_a_background)
                    890:        && !_nc_capcmp(set_background, set_a_background))
                    891:        _nc_warning("expected setb/setab to be different");
                    892:
                    893:     /* see: has_colors() */
                    894:     if (VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
                    895:        && (((set_foreground != NULL)
                    896:             && (set_background != NULL))
                    897:            || ((set_a_foreground != NULL)
                    898:                && (set_a_background != NULL))
                    899:            || set_color_pair)) {
                    900:        if (!VALID_STRING(orig_pair) && !VALID_STRING(orig_colors))
                    901:            _nc_warning("expected either op/oc string for resetting colors");
                    902:     }
                    903: }
                    904:
                    905: static char
                    906: keypad_final(const char *string)
                    907: {
                    908:     char result = '\0';
                    909:
                    910:     if (VALID_STRING(string)
                    911:        && *string++ == '\033'
                    912:        && *string++ == 'O'
                    913:        && strlen(string) == 1) {
                    914:        result = *string;
                    915:     }
                    916:
                    917:     return result;
                    918: }
                    919:
                    920: static int
                    921: keypad_index(const char *string)
                    922: {
                    923:     char *test;
                    924:     const char *list = "PQRSwxymtuvlqrsPpn";   /* app-keypad except "Enter" */
                    925:     int ch;
                    926:     int result = -1;
                    927:
                    928:     if ((ch = keypad_final(string)) != '\0') {
                    929:        test = strchr(list, ch);
                    930:        if (test != 0)
                    931:            result = (test - list);
                    932:     }
                    933:     return result;
                    934: }
                    935:
                    936: #define MAX_KP 5
                    937: /*
                    938:  * Do a quick sanity-check for vt100-style keypads to see if the 5-key keypad
                    939:  * is mapped inconsistently.
                    940:  */
                    941: static void
                    942: check_keypad(TERMTYPE *tp)
                    943: {
                    944:     char show[80];
                    945:
                    946:     if (VALID_STRING(key_a1) &&
                    947:        VALID_STRING(key_a3) &&
                    948:        VALID_STRING(key_b2) &&
                    949:        VALID_STRING(key_c1) &&
                    950:        VALID_STRING(key_c3)) {
                    951:        char final[MAX_KP + 1];
                    952:        int list[MAX_KP];
                    953:        int increase = 0;
                    954:        int j, k, kk;
                    955:        int last;
                    956:        int test;
                    957:
                    958:        final[0] = keypad_final(key_a1);
                    959:        final[1] = keypad_final(key_a3);
                    960:        final[2] = keypad_final(key_b2);
                    961:        final[3] = keypad_final(key_c1);
                    962:        final[4] = keypad_final(key_c3);
                    963:        final[5] = '\0';
                    964:
                    965:        /* special case: legacy coding using 1,2,3,0,. on the bottom */
                    966:        assert(strlen(final) <= MAX_KP);
                    967:        if (!strcmp(final, "qsrpn"))
                    968:            return;
                    969:
                    970:        list[0] = keypad_index(key_a1);
                    971:        list[1] = keypad_index(key_a3);
                    972:        list[2] = keypad_index(key_b2);
                    973:        list[3] = keypad_index(key_c1);
                    974:        list[4] = keypad_index(key_c3);
                    975:
                    976:        /* check that they're all vt100 keys */
                    977:        for (j = 0; j < MAX_KP; ++j) {
                    978:            if (list[j] < 0) {
                    979:                return;
                    980:            }
                    981:        }
                    982:
                    983:        /* check if they're all in increasing order */
                    984:        for (j = 1; j < MAX_KP; ++j) {
                    985:            if (list[j] > list[j - 1]) {
                    986:                ++increase;
                    987:            }
                    988:        }
                    989:        if (increase != (MAX_KP - 1)) {
                    990:            show[0] = '\0';
                    991:
                    992:            for (j = 0, last = -1; j < MAX_KP; ++j) {
                    993:                for (k = 0, kk = -1, test = 100; k < 5; ++k) {
                    994:                    if (list[k] > last &&
                    995:                        list[k] < test) {
                    996:                        test = list[k];
                    997:                        kk = k;
                    998:                    }
                    999:                }
                   1000:                last = test;
                   1001:                assert(strlen(show) < (MAX_KP * 4));
                   1002:                switch (kk) {
                   1003:                case 0:
                   1004:                    strlcat(show, " ka1", sizeof(show));
                   1005:                    break;
                   1006:                case 1:
                   1007:                    strlcat(show, " ka3", sizeof(show));
                   1008:                    break;
                   1009:                case 2:
                   1010:                    strlcat(show, " kb2", sizeof(show));
                   1011:                    break;
                   1012:                case 3:
                   1013:                    strlcat(show, " kc1", sizeof(show));
                   1014:                    break;
                   1015:                case 4:
                   1016:                    strlcat(show, " kc3", sizeof(show));
                   1017:                    break;
                   1018:                }
                   1019:            }
1.9       millert  1020:
1.30      nicm     1021:            _nc_warning("vt100 keypad order inconsistent: %s", show);
                   1022:        }
1.6       millert  1023:
1.30      nicm     1024:     } else if (VALID_STRING(key_a1) ||
                   1025:               VALID_STRING(key_a3) ||
                   1026:               VALID_STRING(key_b2) ||
                   1027:               VALID_STRING(key_c1) ||
                   1028:               VALID_STRING(key_c3)) {
                   1029:        show[0] = '\0';
                   1030:        if (keypad_index(key_a1) >= 0)
                   1031:            strlcat(show, " ka1", sizeof(show));
                   1032:        if (keypad_index(key_a3) >= 0)
                   1033:            strlcat(show, " ka3", sizeof(show));
                   1034:        if (keypad_index(key_b2) >= 0)
                   1035:            strlcat(show, " kb2", sizeof(show));
                   1036:        if (keypad_index(key_c1) >= 0)
                   1037:            strlcat(show, " kc1", sizeof(show));
                   1038:        if (keypad_index(key_c3) >= 0)
                   1039:            strlcat(show, " kc3", sizeof(show));
                   1040:        if (*show != '\0')
                   1041:            _nc_warning("vt100 keypad map incomplete:%s", show);
                   1042:     }
                   1043: }
1.6       millert  1044:
1.17      millert  1045: /*
1.21      millert  1046:  * Returns the expected number of parameters for the given capability.
                   1047:  */
                   1048: static int
1.30      nicm     1049: expected_params(const char *name)
1.21      millert  1050: {
                   1051:     /* *INDENT-OFF* */
                   1052:     static const struct {
                   1053:        const char *name;
                   1054:        int count;
                   1055:     } table[] = {
1.30      nicm     1056:        { "S0",                 1 },    /* 'screen' extension */
1.21      millert  1057:        { "birep",              2 },
                   1058:        { "chr",                1 },
                   1059:        { "colornm",            1 },
                   1060:        { "cpi",                1 },
1.22      millert  1061:        { "csnm",               1 },
1.21      millert  1062:        { "csr",                2 },
                   1063:        { "cub",                1 },
                   1064:        { "cud",                1 },
                   1065:        { "cuf",                1 },
                   1066:        { "cup",                2 },
1.22      millert  1067:        { "cuu",                1 },
1.21      millert  1068:        { "cvr",                1 },
                   1069:        { "cwin",               5 },
                   1070:        { "dch",                1 },
1.22      millert  1071:        { "defc",               3 },
1.21      millert  1072:        { "dial",               1 },
                   1073:        { "dispc",              1 },
                   1074:        { "dl",                 1 },
                   1075:        { "ech",                1 },
                   1076:        { "getm",               1 },
                   1077:        { "hpa",                1 },
                   1078:        { "ich",                1 },
                   1079:        { "il",                 1 },
                   1080:        { "indn",               1 },
                   1081:        { "initc",              4 },
                   1082:        { "initp",              7 },
                   1083:        { "lpi",                1 },
                   1084:        { "mc5p",               1 },
                   1085:        { "mrcup",              2 },
                   1086:        { "mvpa",               1 },
                   1087:        { "pfkey",              2 },
                   1088:        { "pfloc",              2 },
                   1089:        { "pfx",                2 },
                   1090:        { "pfxl",               3 },
                   1091:        { "pln",                2 },
                   1092:        { "qdial",              1 },
1.22      millert  1093:        { "rcsd",               1 },
1.21      millert  1094:        { "rep",                2 },
                   1095:        { "rin",                1 },
                   1096:        { "sclk",               3 },
                   1097:        { "scp",                1 },
                   1098:        { "scs",                1 },
1.22      millert  1099:        { "scsd",               2 },
1.21      millert  1100:        { "setab",              1 },
                   1101:        { "setaf",              1 },
                   1102:        { "setb",               1 },
                   1103:        { "setcolor",           1 },
                   1104:        { "setf",               1 },
                   1105:        { "sgr",                9 },
                   1106:        { "sgr1",               6 },
                   1107:        { "slength",            1 },
                   1108:        { "slines",             1 },
1.30      nicm     1109:        { "smgbp",              1 },    /* 2 if smgtp is not given */
                   1110:        { "smglp",              1 },
1.21      millert  1111:        { "smglr",              2 },
                   1112:        { "smgrp",              1 },
                   1113:        { "smgtb",              2 },
1.22      millert  1114:        { "smgtp",              1 },
1.21      millert  1115:        { "tsl",                1 },
                   1116:        { "u6",                 -1 },
                   1117:        { "vpa",                1 },
                   1118:        { "wind",               4 },
                   1119:        { "wingo",              1 },
                   1120:     };
                   1121:     /* *INDENT-ON* */
                   1122:
                   1123:     unsigned n;
                   1124:     int result = 0;            /* function-keys, etc., use none */
                   1125:
                   1126:     for (n = 0; n < SIZEOF(table); n++) {
                   1127:        if (!strcmp(name, table[n].name)) {
                   1128:            result = table[n].count;
                   1129:            break;
                   1130:        }
                   1131:     }
                   1132:
                   1133:     return result;
                   1134: }
                   1135:
                   1136: /*
                   1137:  * Make a quick sanity check for the parameters which are used in the given
                   1138:  * strings.  If there are no "%p" tokens, then there should be no other "%"
                   1139:  * markers.
                   1140:  */
                   1141: static void
1.30      nicm     1142: check_params(TERMTYPE *tp, const char *name, char *value)
1.21      millert  1143: {
                   1144:     int expected = expected_params(name);
                   1145:     int actual = 0;
                   1146:     int n;
                   1147:     bool params[10];
                   1148:     char *s = value;
                   1149:
1.30      nicm     1150: #ifdef set_top_margin_parm
                   1151:     if (!strcmp(name, "smgbp")
                   1152:        && set_top_margin_parm == 0)
                   1153:        expected = 2;
                   1154: #endif
                   1155:
1.21      millert  1156:     for (n = 0; n < 10; n++)
                   1157:        params[n] = FALSE;
                   1158:
                   1159:     while (*s != 0) {
                   1160:        if (*s == '%') {
                   1161:            if (*++s == '\0') {
                   1162:                _nc_warning("expected character after %% in %s", name);
                   1163:                break;
                   1164:            } else if (*s == 'p') {
1.31      deraadt  1165:                if (*++s == '\0' || !isdigit(UChar(*s))) {
1.21      millert  1166:                    _nc_warning("expected digit after %%p in %s", name);
                   1167:                    return;
                   1168:                } else {
                   1169:                    n = (*s - '0');
                   1170:                    if (n > actual)
                   1171:                        actual = n;
                   1172:                    params[n] = TRUE;
                   1173:                }
                   1174:            }
                   1175:        }
                   1176:        s++;
                   1177:     }
                   1178:
                   1179:     if (params[0]) {
                   1180:        _nc_warning("%s refers to parameter 0 (%%p0), which is not allowed", name);
                   1181:     }
                   1182:     if (value == set_attributes || expected < 0) {
                   1183:        ;
                   1184:     } else if (expected != actual) {
                   1185:        _nc_warning("%s uses %d parameters, expected %d", name,
                   1186:                    actual, expected);
                   1187:        for (n = 1; n < actual; n++) {
                   1188:            if (!params[n])
                   1189:                _nc_warning("%s omits parameter %d", name, n);
                   1190:        }
                   1191:     }
                   1192: }
                   1193:
1.30      nicm     1194: static char *
                   1195: skip_delay(char *s)
                   1196: {
                   1197:     while (*s == '/' || isdigit(UChar(*s)))
                   1198:        ++s;
                   1199:     return s;
                   1200: }
                   1201:
                   1202: /*
                   1203:  * Skip a delay altogether, e.g., when comparing a simple string to sgr,
                   1204:  * the latter may have a worst-case delay on the end.
                   1205:  */
                   1206: static char *
                   1207: ignore_delays(char *s)
                   1208: {
                   1209:     int delaying = 0;
                   1210:
                   1211:     do {
                   1212:        switch (*s) {
                   1213:        case '$':
                   1214:            if (delaying == 0)
                   1215:                delaying = 1;
                   1216:            break;
                   1217:        case '<':
                   1218:            if (delaying == 1)
                   1219:                delaying = 2;
                   1220:            break;
                   1221:        case '\0':
                   1222:            delaying = 0;
                   1223:            break;
                   1224:        default:
                   1225:            if (delaying) {
                   1226:                s = skip_delay(s);
                   1227:                if (*s == '>')
                   1228:                    ++s;
                   1229:                delaying = 0;
                   1230:            }
                   1231:            break;
                   1232:        }
                   1233:        if (delaying)
                   1234:            ++s;
                   1235:     } while (delaying);
                   1236:     return s;
                   1237: }
                   1238:
1.21      millert  1239: /*
1.17      millert  1240:  * An sgr string may contain several settings other than the one we're
                   1241:  * interested in, essentially sgr0 + rmacs + whatever.  As long as the
                   1242:  * "whatever" is contained in the sgr string, that is close enough for our
                   1243:  * sanity check.
                   1244:  */
                   1245: static bool
1.30      nicm     1246: similar_sgr(int num, char *a, char *b)
1.17      millert  1247: {
1.30      nicm     1248:     static const char *names[] =
                   1249:     {
                   1250:        "none"
                   1251:        ,"standout"
                   1252:        ,"underline"
                   1253:        ,"reverse"
                   1254:        ,"blink"
                   1255:        ,"dim"
                   1256:        ,"bold"
                   1257:        ,"invis"
                   1258:        ,"protect"
                   1259:        ,"altcharset"
                   1260:     };
                   1261:     char *base_a = a;
                   1262:     char *base_b = b;
                   1263:     int delaying = 0;
                   1264:
1.17      millert  1265:     while (*b != 0) {
                   1266:        while (*a != *b) {
1.30      nicm     1267:            if (*a == 0) {
                   1268:                if (b[0] == '$'
                   1269:                    && b[1] == '<') {
                   1270:                    _nc_warning("Did not find delay %s", _nc_visbuf(b));
                   1271:                } else {
                   1272:                    _nc_warning("checking sgr(%s) %s\n\tcompare to %s\n\tunmatched %s",
                   1273:                                names[num], _nc_visbuf2(1, base_a),
                   1274:                                _nc_visbuf2(2, base_b),
                   1275:                                _nc_visbuf2(3, b));
                   1276:                }
1.17      millert  1277:                return FALSE;
1.30      nicm     1278:            } else if (delaying) {
                   1279:                a = skip_delay(a);
                   1280:                b = skip_delay(b);
                   1281:            } else {
                   1282:                a++;
                   1283:            }
                   1284:        }
                   1285:        switch (*a) {
                   1286:        case '$':
                   1287:            if (delaying == 0)
                   1288:                delaying = 1;
                   1289:            break;
                   1290:        case '<':
                   1291:            if (delaying == 1)
                   1292:                delaying = 2;
                   1293:            break;
                   1294:        default:
                   1295:            delaying = 0;
                   1296:            break;
1.17      millert  1297:        }
                   1298:        a++;
                   1299:        b++;
                   1300:     }
1.30      nicm     1301:     /* ignore delays on the end of the string */
                   1302:     a = ignore_delays(a);
                   1303:     return ((num != 0) || (*a == 0));
1.17      millert  1304: }
                   1305:
1.30      nicm     1306: static char *
                   1307: check_sgr(TERMTYPE *tp, char *zero, int num, char *cap, const char *name)
1.17      millert  1308: {
1.30      nicm     1309:     char *test;
                   1310:
                   1311:     _nc_tparm_err = 0;
                   1312:     test = TPARM_9(set_attributes,
                   1313:                   num == 1,
                   1314:                   num == 2,
                   1315:                   num == 3,
                   1316:                   num == 4,
                   1317:                   num == 5,
                   1318:                   num == 6,
                   1319:                   num == 7,
                   1320:                   num == 8,
                   1321:                   num == 9);
1.17      millert  1322:     if (test != 0) {
                   1323:        if (PRESENT(cap)) {
1.30      nicm     1324:            if (!similar_sgr(num, test, cap)) {
                   1325:                _nc_warning("%s differs from sgr(%d)\n\t%s=%s\n\tsgr(%d)=%s",
                   1326:                            name, num,
                   1327:                            name, _nc_visbuf2(1, cap),
                   1328:                            num, _nc_visbuf2(2, test));
1.17      millert  1329:            }
1.30      nicm     1330:        } else if (_nc_capcmp(test, zero)) {
1.17      millert  1331:            _nc_warning("sgr(%d) present, but not %s", num, name);
                   1332:        }
                   1333:     } else if (PRESENT(cap)) {
                   1334:        _nc_warning("sgr(%d) missing, but %s present", num, name);
                   1335:     }
1.30      nicm     1336:     if (_nc_tparm_err)
                   1337:        _nc_warning("stack error in sgr(%d) string", num);
                   1338:     return test;
1.17      millert  1339: }
                   1340:
                   1341: #define CHECK_SGR(num,name) check_sgr(tp, zero, num, name, #name)
                   1342:
1.30      nicm     1343: #ifdef TRACE
                   1344: /*
                   1345:  * If tic is compiled with TRACE, we'll be able to see the output from the
                   1346:  * DEBUG() macro.  But since it doesn't use traceon(), it always goes to
                   1347:  * the standard error.  Use this function to make it simpler to follow the
                   1348:  * resulting debug traces.
                   1349:  */
                   1350: static void
                   1351: show_where(unsigned level)
                   1352: {
                   1353:     if (_nc_tracing >= DEBUG_LEVEL(level)) {
                   1354:        char my_name[256];
                   1355:        _nc_get_type(my_name);
                   1356:        fprintf(stderr, "\"%s\", line %d, '%s' ",
                   1357:                _nc_get_source(),
                   1358:                _nc_curr_line, my_name);
                   1359:     }
                   1360: }
                   1361:
                   1362: #else
                   1363: #define show_where(level)      /* nothing */
                   1364: #endif
                   1365:
1.6       millert  1366: /* other sanity-checks (things that we don't want in the normal
                   1367:  * logic that reads a terminfo entry)
                   1368:  */
1.14      millert  1369: static void
1.30      nicm     1370: check_termtype(TERMTYPE *tp, bool literal)
1.6       millert  1371: {
1.14      millert  1372:     bool conflict = FALSE;
                   1373:     unsigned j, k;
                   1374:     char fkeys[STRCOUNT];
                   1375:
                   1376:     /*
                   1377:      * A terminal entry may contain more than one keycode assigned to
                   1378:      * a given string (e.g., KEY_END and KEY_LL).  But curses will only
                   1379:      * return one (the last one assigned).
                   1380:      */
1.30      nicm     1381:     if (!(_nc_syntax == SYN_TERMCAP && capdump)) {
                   1382:        memset(fkeys, 0, sizeof(fkeys));
                   1383:        for (j = 0; _nc_tinfo_fkeys[j].code; j++) {
                   1384:            char *a = tp->Strings[_nc_tinfo_fkeys[j].offset];
                   1385:            bool first = TRUE;
                   1386:            if (!VALID_STRING(a))
1.6       millert  1387:                continue;
1.30      nicm     1388:            for (k = j + 1; _nc_tinfo_fkeys[k].code; k++) {
                   1389:                char *b = tp->Strings[_nc_tinfo_fkeys[k].offset];
                   1390:                if (!VALID_STRING(b)
                   1391:                    || fkeys[k])
                   1392:                    continue;
                   1393:                if (!_nc_capcmp(a, b)) {
                   1394:                    fkeys[j] = 1;
                   1395:                    fkeys[k] = 1;
                   1396:                    if (first) {
                   1397:                        if (!conflict) {
                   1398:                            _nc_warning("Conflicting key definitions (using the last)");
                   1399:                            conflict = TRUE;
                   1400:                        }
                   1401:                        fprintf(stderr, "... %s is the same as %s",
                   1402:                                keyname((int) _nc_tinfo_fkeys[j].code),
                   1403:                                keyname((int) _nc_tinfo_fkeys[k].code));
                   1404:                        first = FALSE;
                   1405:                    } else {
                   1406:                        fprintf(stderr, ", %s",
                   1407:                                keyname((int) _nc_tinfo_fkeys[k].code));
1.6       millert  1408:                    }
                   1409:                }
                   1410:            }
1.30      nicm     1411:            if (!first)
                   1412:                fprintf(stderr, "\n");
1.6       millert  1413:        }
1.21      millert  1414:     }
                   1415:
                   1416:     for (j = 0; j < NUM_STRINGS(tp); j++) {
                   1417:        char *a = tp->Strings[j];
                   1418:        if (VALID_STRING(a))
                   1419:            check_params(tp, ExtStrname(tp, j, strnames), a);
1.14      millert  1420:     }
                   1421:
1.30      nicm     1422:     check_acs(tp);
                   1423:     check_colors(tp);
                   1424:     check_keypad(tp);
1.6       millert  1425:
1.14      millert  1426:     /*
                   1427:      * These may be mismatched because the terminal description relies on
                   1428:      * restoring the cursor visibility by resetting it.
                   1429:      */
1.17      millert  1430:     ANDMISSING(cursor_invisible, cursor_normal);
                   1431:     ANDMISSING(cursor_visible, cursor_normal);
                   1432:
                   1433:     if (PRESENT(cursor_visible) && PRESENT(cursor_normal)
1.30      nicm     1434:        && !_nc_capcmp(cursor_visible, cursor_normal))
1.17      millert  1435:        _nc_warning("cursor_visible is same as cursor_normal");
1.14      millert  1436:
                   1437:     /*
                   1438:      * From XSI & O'Reilly, we gather that sc/rc are required if csr is
                   1439:      * given, because the cursor position after the scrolling operation is
                   1440:      * performed is undefined.
                   1441:      */
1.17      millert  1442:     ANDMISSING(change_scroll_region, save_cursor);
                   1443:     ANDMISSING(change_scroll_region, restore_cursor);
                   1444:
                   1445:     if (PRESENT(set_attributes)) {
1.30      nicm     1446:        char *zero = 0;
1.17      millert  1447:
1.30      nicm     1448:        _nc_tparm_err = 0;
                   1449:        if (PRESENT(exit_attribute_mode)) {
                   1450:            zero = strdup(CHECK_SGR(0, exit_attribute_mode));
                   1451:        } else {
                   1452:            zero = strdup(TPARM_9(set_attributes, 0, 0, 0, 0, 0, 0, 0, 0, 0));
                   1453:        }
                   1454:        if (_nc_tparm_err)
                   1455:            _nc_warning("stack error in sgr(0) string");
                   1456:
                   1457:        if (zero != 0) {
                   1458:            CHECK_SGR(1, enter_standout_mode);
                   1459:            CHECK_SGR(2, enter_underline_mode);
                   1460:            CHECK_SGR(3, enter_reverse_mode);
                   1461:            CHECK_SGR(4, enter_blink_mode);
                   1462:            CHECK_SGR(5, enter_dim_mode);
                   1463:            CHECK_SGR(6, enter_bold_mode);
                   1464:            CHECK_SGR(7, enter_secure_mode);
                   1465:            CHECK_SGR(8, enter_protected_mode);
                   1466:            CHECK_SGR(9, enter_alt_charset_mode);
                   1467:            free(zero);
                   1468:        } else {
                   1469:            _nc_warning("sgr(0) did not return a value");
                   1470:        }
                   1471:     } else if (PRESENT(exit_attribute_mode) &&
                   1472:               set_attributes != CANCELLED_STRING) {
                   1473:        if (_nc_syntax == SYN_TERMINFO)
                   1474:            _nc_warning("missing sgr string");
1.17      millert  1475:     }
1.14      millert  1476:
1.30      nicm     1477:     if (PRESENT(exit_attribute_mode)) {
                   1478:        char *check_sgr0 = _nc_trim_sgr0(tp);
                   1479:
                   1480:        if (check_sgr0 == 0 || *check_sgr0 == '\0') {
                   1481:            _nc_warning("trimmed sgr0 is empty");
                   1482:        } else {
                   1483:            show_where(2);
                   1484:            if (check_sgr0 != exit_attribute_mode) {
                   1485:                DEBUG(2,
                   1486:                      ("will trim sgr0\n\toriginal sgr0=%s\n\ttrimmed  sgr0=%s",
                   1487:                       _nc_visbuf2(1, exit_attribute_mode),
                   1488:                       _nc_visbuf2(2, check_sgr0)));
                   1489:                free(check_sgr0);
                   1490:            } else {
                   1491:                DEBUG(2,
                   1492:                      ("will not trim sgr0\n\toriginal sgr0=%s",
                   1493:                       _nc_visbuf(exit_attribute_mode)));
                   1494:            }
                   1495:        }
                   1496:     }
                   1497: #ifdef TRACE
                   1498:     show_where(2);
                   1499:     if (!auto_right_margin) {
                   1500:        DEBUG(2,
                   1501:              ("can write to lower-right directly"));
                   1502:     } else if (PRESENT(enter_am_mode) && PRESENT(exit_am_mode)) {
                   1503:        DEBUG(2,
                   1504:              ("can write to lower-right by suppressing automargin"));
                   1505:     } else if ((PRESENT(enter_insert_mode) && PRESENT(exit_insert_mode))
                   1506:               || PRESENT(insert_character) || PRESENT(parm_ich)) {
                   1507:        DEBUG(2,
                   1508:              ("can write to lower-right by using inserts"));
                   1509:     } else {
                   1510:        DEBUG(2,
                   1511:              ("cannot write to lower-right"));
                   1512:     }
                   1513: #endif
                   1514:
1.14      millert  1515:     /*
                   1516:      * Some standard applications (e.g., vi) and some non-curses
1.30      nicm     1517:      * applications (e.g., jove) get confused if we have both ich1 and
1.14      millert  1518:      * smir/rmir.  Let's be nice and warn about that, too, even though
                   1519:      * ncurses handles it.
                   1520:      */
1.17      millert  1521:     if ((PRESENT(enter_insert_mode) || PRESENT(exit_insert_mode))
1.30      nicm     1522:        && PRESENT(parm_ich)) {
                   1523:        _nc_warning("non-curses applications may be confused by ich1 with smir/rmir");
1.14      millert  1524:     }
1.6       millert  1525:
1.14      millert  1526:     /*
                   1527:      * Finally, do the non-verbose checks
                   1528:      */
                   1529:     if (save_check_termtype != 0)
1.30      nicm     1530:        save_check_termtype(tp, literal);
1.1       millert  1531: }