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

1.1       millert     1: /****************************************************************************
1.17    ! millert     2:  * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc.              *
1.1       millert     3:  *                                                                          *
                      4:  * Permission is hereby granted, free of charge, to any person obtaining a  *
                      5:  * copy of this software and associated documentation files (the            *
                      6:  * "Software"), to deal in the Software without restriction, including      *
                      7:  * without limitation the rights to use, copy, modify, merge, publish,      *
                      8:  * distribute, distribute with modifications, sublicense, and/or sell       *
                      9:  * copies of the Software, and to permit persons to whom the Software is    *
                     10:  * furnished to do so, subject to the following conditions:                 *
                     11:  *                                                                          *
                     12:  * The above copyright notice and this permission notice shall be included  *
                     13:  * in all copies or substantial portions of the Software.                   *
                     14:  *                                                                          *
                     15:  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
                     16:  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
                     17:  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
                     18:  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
                     19:  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
                     20:  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
                     21:  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
                     22:  *                                                                          *
                     23:  * Except as contained in this notice, the name(s) of the above copyright   *
                     24:  * holders shall not be used in advertising or otherwise to promote the     *
                     25:  * sale, use or other dealings in this Software without prior written       *
                     26:  * authorization.                                                           *
                     27:  ****************************************************************************/
                     28:
                     29: /****************************************************************************
                     30:  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
                     31:  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
                     32:  ****************************************************************************/
                     33:
                     34: /*
                     35:  *     tic.c --- Main program for terminfo compiler
                     36:  *                     by Eric S. Raymond
                     37:  *
                     38:  */
                     39:
                     40: #include <progs.priv.h>
                     41:
                     42: #include <dump_entry.h>
                     43: #include <term_entry.h>
                     44:
1.17    ! millert    45: MODULE_ID("$From: tic.c,v 1.63 2000/03/05 04:33:15 tom Exp $")
1.1       millert    46:
                     47: const char *_nc_progname = "tic";
                     48:
1.14      millert    49: static FILE *log_fp;
                     50: static FILE *tmp_fp;
                     51: static bool showsummary = FALSE;
                     52: static const char *to_remove;
                     53:
                     54: static void (*save_check_termtype) (TERMTYPE *);
                     55: static void check_termtype(TERMTYPE * tt);
                     56:
                     57: static const char usage_string[] = "[-h] [-v[n]] [-e names] [-CILNRTcfrswx1] source-file\n";
                     58:
                     59: static void
                     60: cleanup(void)
                     61: {
                     62:     if (tmp_fp != 0)
                     63:        fclose(tmp_fp);
                     64:     if (to_remove != 0) {
1.11      millert    65: #if HAVE_REMOVE
1.14      millert    66:        remove(to_remove);
1.11      millert    67: #else
1.14      millert    68:        unlink(to_remove);
1.11      millert    69: #endif
1.14      millert    70:     }
1.2       millert    71: }
                     72:
1.14      millert    73: static void
                     74: failed(const char *msg)
1.2       millert    75: {
1.14      millert    76:     perror(msg);
                     77:     cleanup();
                     78:     exit(EXIT_FAILURE);
1.2       millert    79: }
                     80:
1.14      millert    81: static void
                     82: usage(void)
1.1       millert    83: {
1.14      millert    84:     static const char *const tbl[] =
                     85:     {
1.1       millert    86:        "Options:",
                     87:        "  -1         format translation output one capability per line",
                     88:        "  -C         translate entries to termcap source form",
                     89:        "  -I         translate entries to terminfo source form",
                     90:        "  -L         translate entries to full terminfo source form",
                     91:        "  -N         disable smart defaults for source translation",
                     92:        "  -R         restrict translation to given terminfo/termcap version",
                     93:        "  -T         remove size-restrictions on compiled description",
                     94:        "  -c         check only, validate input without compiling or translating",
                     95:        "  -f         format complex strings for readability",
1.8       millert    96:        "  -G         format %{number} to %'char'",
1.2       millert    97:        "  -g         format %'char' to %{number}",
1.1       millert    98:        "  -e<names>  translate/compile only entries named by comma-separated list",
                     99:        "  -o<dir>    set output directory for compiled entry writes",
                    100:        "  -r         force resolution of all use entries in source translation",
                    101:        "  -s         print summary statistics",
                    102:        "  -v[n]      set verbosity level",
                    103:        "  -w[n]      set format width for translation output",
1.7       millert   104: #if NCURSES_XNAMES
                    105:        "  -x         treat unknown capabilities as user-defined",
                    106: #endif
1.1       millert   107:        "",
                    108:        "Parameters:",
                    109:        "  <file>     file to translate or compile"
1.14      millert   110:     };
                    111:     size_t j;
1.1       millert   112:
1.15      millert   113:     fprintf(stderr, "Usage: %s %s\n", _nc_progname, usage_string);
                    114:     for (j = 0; j < sizeof(tbl) / sizeof(tbl[0]); j++) {
                    115:        fputs(tbl[j], stderr);
                    116:        putc('\n', stderr);
                    117:     }
1.14      millert   118:     exit(EXIT_FAILURE);
1.1       millert   119: }
                    120:
1.2       millert   121: #define L_BRACE '{'
                    122: #define R_BRACE '}'
                    123: #define S_QUOTE '\'';
                    124:
1.14      millert   125: static void
                    126: write_it(ENTRY * ep)
1.2       millert   127: {
1.14      millert   128:     unsigned n;
                    129:     int ch;
                    130:     char *s, *d, *t;
                    131:     char result[MAX_ENTRY_SIZE];
                    132:
                    133:     /*
                    134:      * Look for strings that contain %{number}, convert them to %'char',
                    135:      * which is shorter and runs a little faster.
                    136:      */
                    137:     for (n = 0; n < STRCOUNT; n++) {
                    138:        s = ep->tterm.Strings[n];
                    139:        if (VALID_STRING(s)
                    140:            && strchr(s, L_BRACE) != 0) {
                    141:            d = result;
                    142:            t = s;
                    143:            while ((ch = *t++) != 0) {
                    144:                *d++ = ch;
                    145:                if (ch == '\\') {
                    146:                    *d++ = *t++;
                    147:                } else if ((ch == '%')
                    148:                    && (*t == L_BRACE)) {
                    149:                    char *v = 0;
                    150:                    long value = strtol(t + 1, &v, 0);
                    151:                    if (v != 0
                    152:                        && *v == R_BRACE
                    153:                        && value > 0
                    154:                        && value != '\\'        /* FIXME */
                    155:                        && value < 127
                    156:                        && isprint((int) value)) {
                    157:                        *d++ = S_QUOTE;
                    158:                        *d++ = (int) value;
                    159:                        *d++ = S_QUOTE;
                    160:                        t = (v + 1);
                    161:                    }
1.2       millert   162:                }
1.14      millert   163:            }
                    164:            *d = 0;
                    165:            if (strlen(result) < strlen(s))
                    166:                strcpy(s, result);
1.2       millert   167:        }
1.14      millert   168:     }
1.2       millert   169:
1.14      millert   170:     _nc_set_type(_nc_first_name(ep->tterm.term_names));
                    171:     _nc_curr_line = ep->startline;
                    172:     _nc_write_entry(&ep->tterm);
1.2       millert   173: }
                    174:
1.14      millert   175: static bool
                    176: immedhook(ENTRY * ep GCC_UNUSED)
1.1       millert   177: /* write out entries with no use capabilities immediately to save storage */
                    178: {
                    179: #ifndef HAVE_BIG_CORE
                    180:     /*
                    181:      * This is strictly a core-economy kluge.  The really clean way to handle
                    182:      * compilation is to slurp the whole file into core and then do all the
                    183:      * name-collision checks and entry writes in one swell foop.  But the
                    184:      * terminfo master file is large enough that some core-poor systems swap
                    185:      * like crazy when you compile it this way...there have been reports of
                    186:      * this process taking *three hours*, rather than the twenty seconds or
                    187:      * less typical on my development box.
                    188:      *
                    189:      * So.  This hook *immediately* writes out the referenced entry if it
                    190:      * has no use capabilities.  The compiler main loop refrains from
                    191:      * adding the entry to the in-core list when this hook fires.  If some
                    192:      * other entry later needs to reference an entry that got written
                    193:      * immediately, that's OK; the resolution code will fetch it off disk
                    194:      * when it can't find it in core.
                    195:      *
                    196:      * Name collisions will still be detected, just not as cleanly.  The
                    197:      * write_entry() code complains before overwriting an entry that
                    198:      * postdates the time of tic's first call to write_entry().  Thus
                    199:      * it will complain about overwriting entries newly made during the
                    200:      * tic run, but not about overwriting ones that predate it.
                    201:      *
                    202:      * The reason this is a hook, and not in line with the rest of the
                    203:      * compiler code, is that the support for termcap fallback cannot assume
                    204:      * it has anywhere to spool out these entries!
                    205:      *
                    206:      * The _nc_set_type() call here requires a compensating one in
                    207:      * _nc_parse_entry().
                    208:      *
                    209:      * If you define HAVE_BIG_CORE, you'll disable this kluge.  This will
                    210:      * make tic a bit faster (because the resolution code won't have to do
                    211:      * disk I/O nearly as often).
                    212:      */
1.14      millert   213:     if (ep->nuses == 0) {
                    214:        int oldline = _nc_curr_line;
1.1       millert   215:
1.2       millert   216:        write_it(ep);
1.1       millert   217:        _nc_curr_line = oldline;
                    218:        free(ep->tterm.str_table);
1.14      millert   219:        return (TRUE);
1.1       millert   220:     }
                    221: #endif /* HAVE_BIG_CORE */
1.14      millert   222:     return (FALSE);
1.1       millert   223: }
                    224:
1.14      millert   225: static void
                    226: put_translate(int c)
1.1       millert   227: /* emit a comment char, translating terminfo names to termcap names */
                    228: {
                    229:     static bool in_name = FALSE;
1.14      millert   230:     static size_t have, used;
                    231:     static char *namebuf, *suffix;
1.1       millert   232:
1.14      millert   233:     if (in_name) {
                    234:        if (used + 1 >= have) {
                    235:            have += 132;
                    236:            namebuf = (namebuf != 0) ? realloc(namebuf, have) : malloc(have);
                    237:            suffix = (suffix != 0) ? realloc(suffix, have) : malloc(have);
1.1       millert   238:        }
1.14      millert   239:        if (c == '\n' || c == '@') {
                    240:            namebuf[used++] = '\0';
                    241:            (void) putchar('<');
                    242:            (void) fputs(namebuf, stdout);
1.1       millert   243:            putchar(c);
1.14      millert   244:            in_name = FALSE;
                    245:        } else if (c != '>') {
                    246:            namebuf[used++] = c;
                    247:        } else {                /* ah! candidate name! */
                    248:            char *up;
                    249:            NCURSES_CONST char *tp;
                    250:
                    251:            namebuf[used++] = '\0';
                    252:            in_name = FALSE;
                    253:
                    254:            suffix[0] = '\0';
                    255:            if ((up = strchr(namebuf, '#')) != 0
                    256:                || (up = strchr(namebuf, '=')) != 0
                    257:                || ((up = strchr(namebuf, '@')) != 0 && up[1] == '>')) {
                    258:                (void) strcpy(suffix, up);
                    259:                *up = '\0';
                    260:            }
1.1       millert   261:
1.14      millert   262:            if ((tp = nametrans(namebuf)) != 0) {
                    263:                (void) putchar(':');
                    264:                (void) fputs(tp, stdout);
                    265:                (void) fputs(suffix, stdout);
                    266:                (void) putchar(':');
                    267:            } else {
                    268:                /* couldn't find a translation, just dump the name */
                    269:                (void) putchar('<');
                    270:                (void) fputs(namebuf, stdout);
                    271:                (void) fputs(suffix, stdout);
                    272:                (void) putchar('>');
                    273:            }
1.1       millert   274:        }
1.14      millert   275:     } else {
                    276:        used = 0;
                    277:        if (c == '<') {
                    278:            in_name = TRUE;
                    279:        } else {
                    280:            putchar(c);
1.1       millert   281:        }
                    282:     }
                    283: }
                    284:
                    285: /* Returns a string, stripped of leading/trailing whitespace */
1.14      millert   286: static char *
                    287: stripped(char *src)
1.1       millert   288: {
1.14      millert   289:     while (isspace(*src))
                    290:        src++;
                    291:     if (*src != '\0') {
                    292:        char *dst = strcpy(malloc(strlen(src) + 1), src);
                    293:        size_t len = strlen(dst);
                    294:        while (--len != 0 && isspace(dst[len]))
                    295:            dst[len] = '\0';
                    296:        return dst;
                    297:     }
                    298:     return 0;
1.1       millert   299: }
                    300:
                    301: /* Parse the "-e" option-value into a list of names */
1.14      millert   302: static const char **
                    303: make_namelist(char *src)
1.1       millert   304: {
1.14      millert   305:     const char **dst = 0;
1.1       millert   306:
1.14      millert   307:     char *s, *base;
                    308:     unsigned pass, n, nn;
                    309:     char buffer[BUFSIZ];
                    310:
                    311:     if (src == 0) {
                    312:        /* EMPTY */ ;
                    313:     } else if (strchr(src, '/') != 0) {                /* a filename */
                    314:        FILE *fp = fopen(src, "r");
                    315:        if (fp == 0)
                    316:            failed(src);
                    317:
                    318:        for (pass = 1; pass <= 2; pass++) {
                    319:            nn = 0;
                    320:            while (fgets(buffer, sizeof(buffer), fp) != 0) {
                    321:                if ((s = stripped(buffer)) != 0) {
                    322:                    if (dst != 0)
                    323:                        dst[nn] = s;
                    324:                    nn++;
1.1       millert   325:                }
1.14      millert   326:            }
                    327:            if (pass == 1) {
                    328:                dst = (const char **) calloc(nn + 1, sizeof(*dst));
                    329:                rewind(fp);
                    330:            }
                    331:        }
                    332:        fclose(fp);
                    333:     } else {                   /* literal list of names */
                    334:        for (pass = 1; pass <= 2; pass++) {
                    335:            for (n = nn = 0, base = src;; n++) {
                    336:                int mark = src[n];
                    337:                if (mark == ',' || mark == '\0') {
                    338:                    if (pass == 1) {
                    339:                        nn++;
                    340:                    } else {
                    341:                        src[n] = '\0';
                    342:                        if ((s = stripped(base)) != 0)
                    343:                            dst[nn++] = s;
                    344:                        base = &src[n + 1];
                    345:                    }
1.1       millert   346:                }
1.14      millert   347:                if (mark == '\0')
                    348:                    break;
                    349:            }
                    350:            if (pass == 1)
                    351:                dst = (const char **) calloc(nn + 1, sizeof(*dst));
1.1       millert   352:        }
1.14      millert   353:     }
                    354:     if (showsummary) {
                    355:        fprintf(log_fp, "Entries that will be compiled:\n");
                    356:        for (n = 0; dst[n] != 0; n++)
                    357:            fprintf(log_fp, "%d:%s\n", n + 1, dst[n]);
                    358:     }
                    359:     return dst;
1.1       millert   360: }
                    361:
1.14      millert   362: static bool
                    363: matches(const char **needle, const char *haystack)
1.1       millert   364: /* does entry in needle list match |-separated field in haystack? */
                    365: {
1.14      millert   366:     bool code = FALSE;
                    367:     size_t n;
1.1       millert   368:
1.14      millert   369:     if (needle != 0) {
                    370:        for (n = 0; needle[n] != 0; n++) {
                    371:            if (_nc_name_match(haystack, needle[n], "|")) {
                    372:                code = TRUE;
                    373:                break;
                    374:            }
1.1       millert   375:        }
1.14      millert   376:     } else
                    377:        code = TRUE;
                    378:     return (code);
1.1       millert   379: }
                    380:
1.17    ! millert   381: static FILE *
        !           382: open_tempfile(char *name)
        !           383: {
        !           384:     FILE *result = 0;
        !           385: #if HAVE_MKSTEMP
        !           386:     int fd = mkstemp(name);
        !           387:     if (fd >= 0)
        !           388:        result = fdopen(fd, "w");
        !           389: #else
        !           390:     if (tmpnam(name) != 0)
        !           391:        result = fopen(name, "w");
        !           392: #endif
        !           393:     return result;
        !           394: }
        !           395:
1.14      millert   396: int
                    397: main(int argc, char *argv[])
1.1       millert   398: {
1.14      millert   399:     char my_tmpname[PATH_MAX];
                    400:     int v_opt = -1, debug_level;
                    401:     int smart_defaults = TRUE;
                    402:     char *termcap;
                    403:     ENTRY *qp;
                    404:
                    405:     int this_opt, last_opt = '?';
                    406:
                    407:     int outform = F_TERMINFO;  /* output format */
                    408:     int sortmode = S_TERMINFO; /* sort_mode */
                    409:
                    410:     int width = 60;
                    411:     bool formatted = FALSE;    /* reformat complex strings? */
                    412:     int numbers = 0;           /* format "%'char'" to/from "%{number}" */
                    413:     bool infodump = FALSE;     /* running as captoinfo? */
                    414:     bool capdump = FALSE;      /* running as infotocap? */
                    415:     bool forceresolve = FALSE; /* force resolution */
                    416:     bool limited = TRUE;
                    417:     char *tversion = (char *) NULL;
                    418:     const char *source_file = "terminfo";
                    419:     const char **namelst = 0;
                    420:     char *outdir = (char *) NULL;
                    421:     bool check_only = FALSE;
                    422:
                    423:     log_fp = stderr;
                    424:
                    425:     if ((_nc_progname = strrchr(argv[0], '/')) == NULL)
                    426:        _nc_progname = argv[0];
                    427:     else
                    428:        _nc_progname++;
                    429:
                    430:     if ((infodump = (strcmp(_nc_progname, "captoinfo") == 0)) != FALSE) {
                    431:        outform = F_TERMINFO;
                    432:        sortmode = S_TERMINFO;
                    433:     }
                    434:     if ((capdump = (strcmp(_nc_progname, "infotocap") == 0)) != FALSE) {
                    435:        outform = F_TERMCAP;
                    436:        sortmode = S_TERMCAP;
                    437:     }
1.7       millert   438: #if NCURSES_XNAMES
1.14      millert   439:     use_extended_names(FALSE);
1.7       millert   440: #endif
1.1       millert   441:
1.14      millert   442:     /*
                    443:      * Processing arguments is a little complicated, since someone made a
                    444:      * design decision to allow the numeric values for -w, -v options to
                    445:      * be optional.
                    446:      */
                    447:     while ((this_opt = getopt(argc, argv,
                    448:                "0123456789CILNR:TVce:fGgo:rsvwx")) != -1) {
                    449:        if (isdigit(this_opt)) {
                    450:            switch (last_opt) {
                    451:            case 'v':
                    452:                v_opt = (v_opt * 10) + (this_opt - '0');
                    453:                break;
                    454:            case 'w':
                    455:                width = (width * 10) + (this_opt - '0');
                    456:                break;
                    457:            default:
                    458:                if (this_opt != '1')
                    459:                    usage();
                    460:                last_opt = this_opt;
                    461:                width = 0;
                    462:            }
                    463:            continue;
                    464:        }
                    465:        switch (this_opt) {
                    466:        case 'C':
                    467:            capdump = TRUE;
                    468:            outform = F_TERMCAP;
                    469:            sortmode = S_TERMCAP;
                    470:            break;
                    471:        case 'I':
                    472:            infodump = TRUE;
                    473:            outform = F_TERMINFO;
                    474:            sortmode = S_TERMINFO;
                    475:            break;
                    476:        case 'L':
                    477:            infodump = TRUE;
                    478:            outform = F_VARIABLE;
                    479:            sortmode = S_VARIABLE;
                    480:            break;
                    481:        case 'N':
                    482:            smart_defaults = FALSE;
                    483:            break;
                    484:        case 'R':
                    485:            tversion = optarg;
                    486:            break;
                    487:        case 'T':
                    488:            limited = FALSE;
                    489:            break;
                    490:        case 'V':
                    491:            puts(NCURSES_VERSION);
                    492:            return EXIT_SUCCESS;
                    493:        case 'c':
                    494:            check_only = TRUE;
                    495:            break;
                    496:        case 'e':
                    497:            namelst = make_namelist(optarg);
                    498:            break;
                    499:        case 'f':
                    500:            formatted = TRUE;
                    501:            break;
                    502:        case 'G':
                    503:            numbers = 1;
                    504:            break;
                    505:        case 'g':
                    506:            numbers = -1;
                    507:            break;
                    508:        case 'o':
                    509:            outdir = optarg;
                    510:            break;
                    511:        case 'r':
                    512:            forceresolve = TRUE;
                    513:            break;
                    514:        case 's':
                    515:            showsummary = TRUE;
                    516:            break;
                    517:        case 'v':
                    518:            v_opt = 0;
                    519:            break;
                    520:        case 'w':
                    521:            width = 0;
                    522:            break;
1.7       millert   523: #if NCURSES_XNAMES
1.14      millert   524:        case 'x':
                    525:            use_extended_names(TRUE);
                    526:            break;
1.7       millert   527: #endif
1.14      millert   528:        default:
                    529:            usage();
1.1       millert   530:        }
1.14      millert   531:        last_opt = this_opt;
                    532:     }
1.1       millert   533:
1.14      millert   534:     debug_level = (v_opt > 0) ? v_opt : (v_opt == 0);
1.16      millert   535:     set_trace_level(debug_level);
1.6       millert   536:
1.14      millert   537:     if (_nc_tracing) {
                    538:        save_check_termtype = _nc_check_termtype;
                    539:        _nc_check_termtype = check_termtype;
                    540:     }
1.4       millert   541: #ifndef HAVE_BIG_CORE
1.14      millert   542:     /*
                    543:      * Aaargh! immedhook seriously hoses us!
                    544:      *
                    545:      * One problem with immedhook is it means we can't do -e.  Problem
                    546:      * is that we can't guarantee that for each terminal listed, all the
                    547:      * terminals it depends on will have been kept in core for reference
                    548:      * resolution -- in fact it's certain the primitive types at the end
                    549:      * of reference chains *won't* be in core unless they were explicitly
                    550:      * in the select list themselves.
                    551:      */
                    552:     if (namelst && (!infodump && !capdump)) {
                    553:        (void) fprintf(stderr,
                    554:            "Sorry, -e can't be used without -I or -C\n");
                    555:        cleanup();
                    556:        return EXIT_FAILURE;
                    557:     }
1.4       millert   558: #endif /* HAVE_BIG_CORE */
                    559:
1.14      millert   560:     if (optind < argc) {
                    561:        source_file = argv[optind++];
1.1       millert   562:        if (optind < argc) {
1.14      millert   563:            fprintf(stderr,
                    564:                "%s: Too many file names.  Usage:\n\t%s %s",
                    565:                _nc_progname,
                    566:                _nc_progname,
                    567:                usage_string);
                    568:            return EXIT_FAILURE;
                    569:        }
                    570:     } else {
                    571:        if (infodump == TRUE) {
                    572:            /* captoinfo's no-argument case */
                    573:            source_file = "/usr/share/misc/termcap";
                    574:            if ((termcap = getenv("TERMCAP")) != 0
                    575:                && (namelst = make_namelist(getenv("TERM"))) != 0) {
                    576:                if (access(termcap, F_OK) == 0) {
                    577:                    /* file exists */
                    578:                    source_file = termcap;
1.17    ! millert   579:                } else if ((tmp_fp = open_tempfile(my_tmpname)) != 0) {
        !           580:                    source_file = my_tmpname;
1.14      millert   581:                    fprintf(tmp_fp, "%s\n", termcap);
                    582:                    fclose(tmp_fp);
                    583:                    tmp_fp = fopen(source_file, "r");
                    584:                    to_remove = source_file;
                    585:                } else {
                    586:                    failed("mkstemp");
1.1       millert   587:                }
1.14      millert   588:            }
1.1       millert   589:        } else {
1.14      millert   590:            /* tic */
                    591:            fprintf(stderr,
                    592:                "%s: File name needed.  Usage:\n\t%s %s",
                    593:                _nc_progname,
                    594:                _nc_progname,
                    595:                usage_string);
                    596:            cleanup();
                    597:            return EXIT_FAILURE;
1.1       millert   598:        }
1.14      millert   599:     }
                    600:
                    601:     if (tmp_fp == 0
                    602:        && (tmp_fp = fopen(source_file, "r")) == 0) {
                    603:        fprintf(stderr, "%s: Can't open %s\n", _nc_progname, source_file);
                    604:        return EXIT_FAILURE;
                    605:     }
1.1       millert   606:
1.14      millert   607:     if (infodump)
                    608:        dump_init(tversion,
                    609:            smart_defaults
                    610:            ? outform
                    611:            : F_LITERAL,
                    612:            sortmode, width, debug_level, formatted);
                    613:     else if (capdump)
                    614:        dump_init(tversion,
                    615:            outform,
                    616:            sortmode, width, debug_level, FALSE);
1.1       millert   617:
1.14      millert   618:     /* parse entries out of the source file */
                    619:     _nc_set_source(source_file);
1.1       millert   620: #ifndef HAVE_BIG_CORE
1.14      millert   621:     if (!(check_only || infodump || capdump))
                    622:        _nc_set_writedir(outdir);
1.1       millert   623: #endif /* HAVE_BIG_CORE */
1.14      millert   624:     _nc_read_entry_source(tmp_fp, (char *) NULL,
                    625:        !smart_defaults, FALSE,
                    626:        (check_only || infodump || capdump) ? NULLHOOK : immedhook);
                    627:
                    628:     /* do use resolution */
                    629:     if (check_only || (!infodump && !capdump) || forceresolve) {
                    630:        if (!_nc_resolve_uses() && !check_only) {
                    631:            cleanup();
                    632:            return EXIT_FAILURE;
                    633:        }
                    634:     }
1.1       millert   635:
1.14      millert   636:     /* length check */
                    637:     if (check_only && (capdump || infodump)) {
                    638:        for_entry_list(qp) {
                    639:            if (matches(namelst, qp->tterm.term_names)) {
                    640:                int len = fmt_entry(&qp->tterm, NULL, TRUE, infodump, numbers);
                    641:
                    642:                if (len > (infodump ? MAX_TERMINFO_LENGTH : MAX_TERMCAP_LENGTH))
                    643:                    (void) fprintf(stderr,
                    644:                        "warning: resolved %s entry is %d bytes long\n",
                    645:                        _nc_first_name(qp->tterm.term_names),
                    646:                        len);
1.2       millert   647:            }
                    648:        }
1.14      millert   649:     }
1.1       millert   650:
1.14      millert   651:     /* write or dump all entries */
                    652:     if (!check_only) {
                    653:        if (!infodump && !capdump) {
                    654:            _nc_set_writedir(outdir);
1.1       millert   655:            for_entry_list(qp)
                    656:                if (matches(namelst, qp->tterm.term_names))
1.14      millert   657:                write_it(qp);
                    658:        } else {
                    659:            /* this is in case infotocap() generates warnings */
                    660:            _nc_curr_col = _nc_curr_line = -1;
1.1       millert   661:
1.14      millert   662:            for_entry_list(qp) {
                    663:                if (matches(namelst, qp->tterm.term_names)) {
                    664:                    int j = qp->cend - qp->cstart;
                    665:                    int len = 0;
                    666:
                    667:                    /* this is in case infotocap() generates warnings */
                    668:                    _nc_set_type(_nc_first_name(qp->tterm.term_names));
                    669:
                    670:                    (void) fseek(tmp_fp, qp->cstart, SEEK_SET);
                    671:                    while (j--) {
                    672:                        if (infodump)
                    673:                            (void) putchar(fgetc(tmp_fp));
                    674:                        else
                    675:                            put_translate(fgetc(tmp_fp));
                    676:                    }
                    677:
                    678:                    len = dump_entry(&qp->tterm, limited, numbers, NULL);
                    679:                    for (j = 0; j < qp->nuses; j++)
                    680:                        len += dump_uses((char *) (qp->uses[j].parent), !capdump);
                    681:                    (void) putchar('\n');
                    682:                    if (debug_level != 0 && !limited)
                    683:                        printf("# length=%d\n", len);
1.1       millert   684:                }
                    685:            }
1.14      millert   686:            if (!namelst) {
                    687:                int c, oldc = '\0';
                    688:                bool in_comment = FALSE;
                    689:                bool trailing_comment = FALSE;
                    690:
                    691:                (void) fseek(tmp_fp, _nc_tail->cend, SEEK_SET);
                    692:                while ((c = fgetc(tmp_fp)) != EOF) {
                    693:                    if (oldc == '\n') {
                    694:                        if (c == '#') {
                    695:                            trailing_comment = TRUE;
                    696:                            in_comment = TRUE;
                    697:                        } else {
                    698:                            in_comment = FALSE;
1.1       millert   699:                        }
                    700:                    }
1.14      millert   701:                    if (trailing_comment
                    702:                        && (in_comment || (oldc == '\n' && c == '\n')))
                    703:                        putchar(c);
                    704:                    oldc = c;
1.1       millert   705:                }
                    706:            }
                    707:        }
1.14      millert   708:     }
1.1       millert   709:
1.14      millert   710:     /* Show the directory into which entries were written, and the total
                    711:      * number of entries
                    712:      */
                    713:     if (showsummary
                    714:        && (!(check_only || infodump || capdump))) {
                    715:        int total = _nc_tic_written();
                    716:        if (total != 0)
                    717:            fprintf(log_fp, "%d entries written to %s\n",
                    718:                total,
                    719:                _nc_tic_dir((char *) 0));
                    720:        else
                    721:            fprintf(log_fp, "No entries written\n");
                    722:     }
                    723:     cleanup();
                    724:     return (EXIT_SUCCESS);
1.6       millert   725: }
                    726:
                    727: /*
                    728:  * This bit of legerdemain turns all the terminfo variable names into
                    729:  * references to locations in the arrays Booleans, Numbers, and Strings ---
                    730:  * precisely what's needed (see comp_parse.c).
                    731:  */
1.9       millert   732:
1.14      millert   733: TERMINAL *cur_term;            /* tweak to avoid linking lib_cur_term.c */
1.6       millert   734:
                    735: #undef CUR
                    736: #define CUR tp->
                    737:
1.17    ! millert   738: /*
        !           739:  * An sgr string may contain several settings other than the one we're
        !           740:  * interested in, essentially sgr0 + rmacs + whatever.  As long as the
        !           741:  * "whatever" is contained in the sgr string, that is close enough for our
        !           742:  * sanity check.
        !           743:  */
        !           744: static bool
        !           745: similar_sgr(char *a, char *b)
        !           746: {
        !           747:     while (*b != 0) {
        !           748:        while (*a != *b) {
        !           749:            if (*a == 0)
        !           750:                return FALSE;
        !           751:            a++;
        !           752:        }
        !           753:        a++;
        !           754:        b++;
        !           755:     }
        !           756:     return TRUE;
        !           757: }
        !           758:
        !           759: static void
        !           760: check_sgr(TERMTYPE * tp, char *zero, int num, char *cap, char *name)
        !           761: {
        !           762:     char *test = tparm(set_attributes,
        !           763:        num == 1,
        !           764:        num == 2,
        !           765:        num == 3,
        !           766:        num == 4,
        !           767:        num == 5,
        !           768:        num == 6,
        !           769:        num == 7,
        !           770:        num == 8,
        !           771:        num == 9);
        !           772:     if (test != 0) {
        !           773:        if (PRESENT(cap)) {
        !           774:            if (!similar_sgr(test, cap)) {
        !           775:                _nc_warning("%s differs from sgr(%d): %s", name, num,
        !           776:                    _nc_visbuf(test));
        !           777:            }
        !           778:        } else if (strcmp(test, zero)) {
        !           779:            _nc_warning("sgr(%d) present, but not %s", num, name);
        !           780:        }
        !           781:     } else if (PRESENT(cap)) {
        !           782:        _nc_warning("sgr(%d) missing, but %s present", num, name);
        !           783:     }
        !           784: }
        !           785:
        !           786: #define CHECK_SGR(num,name) check_sgr(tp, zero, num, name, #name)
        !           787:
1.6       millert   788: /* other sanity-checks (things that we don't want in the normal
                    789:  * logic that reads a terminfo entry)
                    790:  */
1.14      millert   791: static void
                    792: check_termtype(TERMTYPE * tp)
1.6       millert   793: {
1.14      millert   794:     bool conflict = FALSE;
                    795:     unsigned j, k;
                    796:     char fkeys[STRCOUNT];
                    797:
                    798:     /*
                    799:      * A terminal entry may contain more than one keycode assigned to
                    800:      * a given string (e.g., KEY_END and KEY_LL).  But curses will only
                    801:      * return one (the last one assigned).
                    802:      */
                    803:     memset(fkeys, 0, sizeof(fkeys));
                    804:     for (j = 0; _nc_tinfo_fkeys[j].code; j++) {
                    805:        char *a = tp->Strings[_nc_tinfo_fkeys[j].offset];
                    806:        bool first = TRUE;
                    807:        if (!VALID_STRING(a))
                    808:            continue;
                    809:        for (k = j + 1; _nc_tinfo_fkeys[k].code; k++) {
                    810:            char *b = tp->Strings[_nc_tinfo_fkeys[k].offset];
                    811:            if (!VALID_STRING(b)
                    812:                || fkeys[k])
1.6       millert   813:                continue;
1.14      millert   814:            if (!strcmp(a, b)) {
                    815:                fkeys[j] = 1;
                    816:                fkeys[k] = 1;
                    817:                if (first) {
                    818:                    if (!conflict) {
                    819:                        _nc_warning("Conflicting key definitions (using the last)");
                    820:                        conflict = TRUE;
1.6       millert   821:                    }
1.14      millert   822:                    fprintf(stderr, "... %s is the same as %s",
                    823:                        keyname(_nc_tinfo_fkeys[j].code),
                    824:                        keyname(_nc_tinfo_fkeys[k].code));
                    825:                    first = FALSE;
                    826:                } else {
                    827:                    fprintf(stderr, ", %s",
                    828:                        keyname(_nc_tinfo_fkeys[k].code));
1.6       millert   829:                }
                    830:            }
                    831:        }
1.14      millert   832:        if (!first)
                    833:            fprintf(stderr, "\n");
                    834:     }
                    835:
                    836:     /*
                    837:      * Quick check for color.  We could also check if the ANSI versus
                    838:      * non-ANSI strings are misused.
                    839:      */
                    840:     if ((max_colors > 0) != (max_pairs > 0)
                    841:        || (max_colors > max_pairs))
                    842:        _nc_warning("inconsistent values for max_colors and max_pairs");
                    843:
1.17    ! millert   844:     PAIRED(set_foreground, set_background);
        !           845:     PAIRED(set_a_foreground, set_a_background);
1.6       millert   846:
1.14      millert   847:     /*
                    848:      * These may be mismatched because the terminal description relies on
                    849:      * restoring the cursor visibility by resetting it.
                    850:      */
1.17    ! millert   851:     ANDMISSING(cursor_invisible, cursor_normal);
        !           852:     ANDMISSING(cursor_visible, cursor_normal);
        !           853:
        !           854:     if (PRESENT(cursor_visible) && PRESENT(cursor_normal)
        !           855:        && !strcmp(cursor_visible, cursor_normal))
        !           856:        _nc_warning("cursor_visible is same as cursor_normal");
1.14      millert   857:
                    858:     /*
                    859:      * From XSI & O'Reilly, we gather that sc/rc are required if csr is
                    860:      * given, because the cursor position after the scrolling operation is
                    861:      * performed is undefined.
                    862:      */
1.17    ! millert   863:     ANDMISSING(change_scroll_region, save_cursor);
        !           864:     ANDMISSING(change_scroll_region, restore_cursor);
        !           865:
        !           866:     if (PRESENT(set_attributes)) {
        !           867:        char *zero = tparm(set_attributes, 0, 0, 0, 0, 0, 0, 0, 0, 0);
        !           868:
        !           869:        zero = strdup(zero);
        !           870:        CHECK_SGR(1, enter_standout_mode);
        !           871:        CHECK_SGR(2, enter_underline_mode);
        !           872:        CHECK_SGR(3, enter_reverse_mode);
        !           873:        CHECK_SGR(4, enter_blink_mode);
        !           874:        CHECK_SGR(5, enter_dim_mode);
        !           875:        CHECK_SGR(6, enter_bold_mode);
        !           876:        CHECK_SGR(7, enter_secure_mode);
        !           877:        CHECK_SGR(8, enter_protected_mode);
        !           878:        CHECK_SGR(9, enter_alt_charset_mode);
        !           879:        free(zero);
        !           880:     }
1.14      millert   881:
                    882:     /*
                    883:      * Some standard applications (e.g., vi) and some non-curses
                    884:      * applications (e.g., jove) get confused if we have both ich/ich1 and
                    885:      * smir/rmir.  Let's be nice and warn about that, too, even though
                    886:      * ncurses handles it.
                    887:      */
1.17    ! millert   888:     if ((PRESENT(enter_insert_mode) || PRESENT(exit_insert_mode))
1.14      millert   889:        && (PRESENT(insert_character) || PRESENT(parm_ich))) {
                    890:        _nc_warning("non-curses applications may be confused by ich/ich1 with smir/rmir");
                    891:     }
1.6       millert   892:
1.14      millert   893:     /*
                    894:      * Finally, do the non-verbose checks
                    895:      */
                    896:     if (save_check_termtype != 0)
                    897:        save_check_termtype(tp);
1.1       millert   898: }