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

1.1       millert     1: /****************************************************************************
1.7       millert     2:  * Copyright (c) 1998,1999 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.14    ! millert    45: MODULE_ID("$From: tic.c,v 1.55 1999/12/11 20:20:54 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.14    ! millert   113:     printf("Usage: %s %s\n", _nc_progname, usage_string);
        !           114:     for (j = 0; j < sizeof(tbl) / sizeof(tbl[0]); j++)
        !           115:        puts(tbl[j]);
        !           116:     exit(EXIT_FAILURE);
1.1       millert   117: }
                    118:
1.2       millert   119: #define L_BRACE '{'
                    120: #define R_BRACE '}'
                    121: #define S_QUOTE '\'';
                    122:
1.14    ! millert   123: static void
        !           124: write_it(ENTRY * ep)
1.2       millert   125: {
1.14    ! millert   126:     unsigned n;
        !           127:     int ch;
        !           128:     char *s, *d, *t;
        !           129:     char result[MAX_ENTRY_SIZE];
        !           130:
        !           131:     /*
        !           132:      * Look for strings that contain %{number}, convert them to %'char',
        !           133:      * which is shorter and runs a little faster.
        !           134:      */
        !           135:     for (n = 0; n < STRCOUNT; n++) {
        !           136:        s = ep->tterm.Strings[n];
        !           137:        if (VALID_STRING(s)
        !           138:            && strchr(s, L_BRACE) != 0) {
        !           139:            d = result;
        !           140:            t = s;
        !           141:            while ((ch = *t++) != 0) {
        !           142:                *d++ = ch;
        !           143:                if (ch == '\\') {
        !           144:                    *d++ = *t++;
        !           145:                } else if ((ch == '%')
        !           146:                    && (*t == L_BRACE)) {
        !           147:                    char *v = 0;
        !           148:                    long value = strtol(t + 1, &v, 0);
        !           149:                    if (v != 0
        !           150:                        && *v == R_BRACE
        !           151:                        && value > 0
        !           152:                        && value != '\\'        /* FIXME */
        !           153:                        && value < 127
        !           154:                        && isprint((int) value)) {
        !           155:                        *d++ = S_QUOTE;
        !           156:                        *d++ = (int) value;
        !           157:                        *d++ = S_QUOTE;
        !           158:                        t = (v + 1);
        !           159:                    }
1.2       millert   160:                }
1.14    ! millert   161:            }
        !           162:            *d = 0;
        !           163:            if (strlen(result) < strlen(s))
        !           164:                strcpy(s, result);
1.2       millert   165:        }
1.14    ! millert   166:     }
1.2       millert   167:
1.14    ! millert   168:     _nc_set_type(_nc_first_name(ep->tterm.term_names));
        !           169:     _nc_curr_line = ep->startline;
        !           170:     _nc_write_entry(&ep->tterm);
1.2       millert   171: }
                    172:
1.14    ! millert   173: static bool
        !           174: immedhook(ENTRY * ep GCC_UNUSED)
1.1       millert   175: /* write out entries with no use capabilities immediately to save storage */
                    176: {
                    177: #ifndef HAVE_BIG_CORE
                    178:     /*
                    179:      * This is strictly a core-economy kluge.  The really clean way to handle
                    180:      * compilation is to slurp the whole file into core and then do all the
                    181:      * name-collision checks and entry writes in one swell foop.  But the
                    182:      * terminfo master file is large enough that some core-poor systems swap
                    183:      * like crazy when you compile it this way...there have been reports of
                    184:      * this process taking *three hours*, rather than the twenty seconds or
                    185:      * less typical on my development box.
                    186:      *
                    187:      * So.  This hook *immediately* writes out the referenced entry if it
                    188:      * has no use capabilities.  The compiler main loop refrains from
                    189:      * adding the entry to the in-core list when this hook fires.  If some
                    190:      * other entry later needs to reference an entry that got written
                    191:      * immediately, that's OK; the resolution code will fetch it off disk
                    192:      * when it can't find it in core.
                    193:      *
                    194:      * Name collisions will still be detected, just not as cleanly.  The
                    195:      * write_entry() code complains before overwriting an entry that
                    196:      * postdates the time of tic's first call to write_entry().  Thus
                    197:      * it will complain about overwriting entries newly made during the
                    198:      * tic run, but not about overwriting ones that predate it.
                    199:      *
                    200:      * The reason this is a hook, and not in line with the rest of the
                    201:      * compiler code, is that the support for termcap fallback cannot assume
                    202:      * it has anywhere to spool out these entries!
                    203:      *
                    204:      * The _nc_set_type() call here requires a compensating one in
                    205:      * _nc_parse_entry().
                    206:      *
                    207:      * If you define HAVE_BIG_CORE, you'll disable this kluge.  This will
                    208:      * make tic a bit faster (because the resolution code won't have to do
                    209:      * disk I/O nearly as often).
                    210:      */
1.14    ! millert   211:     if (ep->nuses == 0) {
        !           212:        int oldline = _nc_curr_line;
1.1       millert   213:
1.2       millert   214:        write_it(ep);
1.1       millert   215:        _nc_curr_line = oldline;
                    216:        free(ep->tterm.str_table);
1.14    ! millert   217:        return (TRUE);
1.1       millert   218:     }
                    219: #endif /* HAVE_BIG_CORE */
1.14    ! millert   220:     return (FALSE);
1.1       millert   221: }
                    222:
1.14    ! millert   223: static void
        !           224: put_translate(int c)
1.1       millert   225: /* emit a comment char, translating terminfo names to termcap names */
                    226: {
                    227:     static bool in_name = FALSE;
1.14    ! millert   228:     static size_t have, used;
        !           229:     static char *namebuf, *suffix;
1.1       millert   230:
1.14    ! millert   231:     if (in_name) {
        !           232:        if (used + 1 >= have) {
        !           233:            have += 132;
        !           234:            namebuf = (namebuf != 0) ? realloc(namebuf, have) : malloc(have);
        !           235:            suffix = (suffix != 0) ? realloc(suffix, have) : malloc(have);
1.1       millert   236:        }
1.14    ! millert   237:        if (c == '\n' || c == '@') {
        !           238:            namebuf[used++] = '\0';
        !           239:            (void) putchar('<');
        !           240:            (void) fputs(namebuf, stdout);
1.1       millert   241:            putchar(c);
1.14    ! millert   242:            in_name = FALSE;
        !           243:        } else if (c != '>') {
        !           244:            namebuf[used++] = c;
        !           245:        } else {                /* ah! candidate name! */
        !           246:            char *up;
        !           247:            NCURSES_CONST char *tp;
        !           248:
        !           249:            namebuf[used++] = '\0';
        !           250:            in_name = FALSE;
        !           251:
        !           252:            suffix[0] = '\0';
        !           253:            if ((up = strchr(namebuf, '#')) != 0
        !           254:                || (up = strchr(namebuf, '=')) != 0
        !           255:                || ((up = strchr(namebuf, '@')) != 0 && up[1] == '>')) {
        !           256:                (void) strcpy(suffix, up);
        !           257:                *up = '\0';
        !           258:            }
1.1       millert   259:
1.14    ! millert   260:            if ((tp = nametrans(namebuf)) != 0) {
        !           261:                (void) putchar(':');
        !           262:                (void) fputs(tp, stdout);
        !           263:                (void) fputs(suffix, stdout);
        !           264:                (void) putchar(':');
        !           265:            } else {
        !           266:                /* couldn't find a translation, just dump the name */
        !           267:                (void) putchar('<');
        !           268:                (void) fputs(namebuf, stdout);
        !           269:                (void) fputs(suffix, stdout);
        !           270:                (void) putchar('>');
        !           271:            }
1.1       millert   272:        }
1.14    ! millert   273:     } else {
        !           274:        used = 0;
        !           275:        if (c == '<') {
        !           276:            in_name = TRUE;
        !           277:        } else {
        !           278:            putchar(c);
1.1       millert   279:        }
                    280:     }
                    281: }
                    282:
                    283: /* Returns a string, stripped of leading/trailing whitespace */
1.14    ! millert   284: static char *
        !           285: stripped(char *src)
1.1       millert   286: {
1.14    ! millert   287:     while (isspace(*src))
        !           288:        src++;
        !           289:     if (*src != '\0') {
        !           290:        char *dst = strcpy(malloc(strlen(src) + 1), src);
        !           291:        size_t len = strlen(dst);
        !           292:        while (--len != 0 && isspace(dst[len]))
        !           293:            dst[len] = '\0';
        !           294:        return dst;
        !           295:     }
        !           296:     return 0;
1.1       millert   297: }
                    298:
                    299: /* Parse the "-e" option-value into a list of names */
1.14    ! millert   300: static const char **
        !           301: make_namelist(char *src)
1.1       millert   302: {
1.14    ! millert   303:     const char **dst = 0;
1.1       millert   304:
1.14    ! millert   305:     char *s, *base;
        !           306:     unsigned pass, n, nn;
        !           307:     char buffer[BUFSIZ];
        !           308:
        !           309:     if (src == 0) {
        !           310:        /* EMPTY */ ;
        !           311:     } else if (strchr(src, '/') != 0) {                /* a filename */
        !           312:        FILE *fp = fopen(src, "r");
        !           313:        if (fp == 0)
        !           314:            failed(src);
        !           315:
        !           316:        for (pass = 1; pass <= 2; pass++) {
        !           317:            nn = 0;
        !           318:            while (fgets(buffer, sizeof(buffer), fp) != 0) {
        !           319:                if ((s = stripped(buffer)) != 0) {
        !           320:                    if (dst != 0)
        !           321:                        dst[nn] = s;
        !           322:                    nn++;
1.1       millert   323:                }
1.14    ! millert   324:            }
        !           325:            if (pass == 1) {
        !           326:                dst = (const char **) calloc(nn + 1, sizeof(*dst));
        !           327:                rewind(fp);
        !           328:            }
        !           329:        }
        !           330:        fclose(fp);
        !           331:     } else {                   /* literal list of names */
        !           332:        for (pass = 1; pass <= 2; pass++) {
        !           333:            for (n = nn = 0, base = src;; n++) {
        !           334:                int mark = src[n];
        !           335:                if (mark == ',' || mark == '\0') {
        !           336:                    if (pass == 1) {
        !           337:                        nn++;
        !           338:                    } else {
        !           339:                        src[n] = '\0';
        !           340:                        if ((s = stripped(base)) != 0)
        !           341:                            dst[nn++] = s;
        !           342:                        base = &src[n + 1];
        !           343:                    }
1.1       millert   344:                }
1.14    ! millert   345:                if (mark == '\0')
        !           346:                    break;
        !           347:            }
        !           348:            if (pass == 1)
        !           349:                dst = (const char **) calloc(nn + 1, sizeof(*dst));
1.1       millert   350:        }
1.14    ! millert   351:     }
        !           352:     if (showsummary) {
        !           353:        fprintf(log_fp, "Entries that will be compiled:\n");
        !           354:        for (n = 0; dst[n] != 0; n++)
        !           355:            fprintf(log_fp, "%d:%s\n", n + 1, dst[n]);
        !           356:     }
        !           357:     return dst;
1.1       millert   358: }
                    359:
1.14    ! millert   360: static bool
        !           361: matches(const char **needle, const char *haystack)
1.1       millert   362: /* does entry in needle list match |-separated field in haystack? */
                    363: {
1.14    ! millert   364:     bool code = FALSE;
        !           365:     size_t n;
1.1       millert   366:
1.14    ! millert   367:     if (needle != 0) {
        !           368:        for (n = 0; needle[n] != 0; n++) {
        !           369:            if (_nc_name_match(haystack, needle[n], "|")) {
        !           370:                code = TRUE;
        !           371:                break;
        !           372:            }
1.1       millert   373:        }
1.14    ! millert   374:     } else
        !           375:        code = TRUE;
        !           376:     return (code);
1.1       millert   377: }
                    378:
1.14    ! millert   379: int
        !           380: main(int argc, char *argv[])
1.1       millert   381: {
1.14    ! millert   382:     char my_tmpname[PATH_MAX];
        !           383:     int v_opt = -1, debug_level;
        !           384:     int smart_defaults = TRUE;
        !           385:     char *termcap;
        !           386:     ENTRY *qp;
        !           387:
        !           388:     int this_opt, last_opt = '?';
        !           389:
        !           390:     int outform = F_TERMINFO;  /* output format */
        !           391:     int sortmode = S_TERMINFO; /* sort_mode */
        !           392:
        !           393:     int fd;
        !           394:     int width = 60;
        !           395:     bool formatted = FALSE;    /* reformat complex strings? */
        !           396:     int numbers = 0;           /* format "%'char'" to/from "%{number}" */
        !           397:     bool infodump = FALSE;     /* running as captoinfo? */
        !           398:     bool capdump = FALSE;      /* running as infotocap? */
        !           399:     bool forceresolve = FALSE; /* force resolution */
        !           400:     bool limited = TRUE;
        !           401:     char *tversion = (char *) NULL;
        !           402:     const char *source_file = "terminfo";
        !           403:     const char **namelst = 0;
        !           404:     char *outdir = (char *) NULL;
        !           405:     bool check_only = FALSE;
        !           406:
        !           407:     log_fp = stderr;
        !           408:
        !           409:     if ((_nc_progname = strrchr(argv[0], '/')) == NULL)
        !           410:        _nc_progname = argv[0];
        !           411:     else
        !           412:        _nc_progname++;
        !           413:
        !           414:     if ((infodump = (strcmp(_nc_progname, "captoinfo") == 0)) != FALSE) {
        !           415:        outform = F_TERMINFO;
        !           416:        sortmode = S_TERMINFO;
        !           417:     }
        !           418:     if ((capdump = (strcmp(_nc_progname, "infotocap") == 0)) != FALSE) {
        !           419:        outform = F_TERMCAP;
        !           420:        sortmode = S_TERMCAP;
        !           421:     }
1.7       millert   422: #if NCURSES_XNAMES
1.14    ! millert   423:     use_extended_names(FALSE);
1.7       millert   424: #endif
1.1       millert   425:
1.14    ! millert   426:     /*
        !           427:      * Processing arguments is a little complicated, since someone made a
        !           428:      * design decision to allow the numeric values for -w, -v options to
        !           429:      * be optional.
        !           430:      */
        !           431:     while ((this_opt = getopt(argc, argv,
        !           432:                "0123456789CILNR:TVce:fGgo:rsvwx")) != -1) {
        !           433:        if (isdigit(this_opt)) {
        !           434:            switch (last_opt) {
        !           435:            case 'v':
        !           436:                v_opt = (v_opt * 10) + (this_opt - '0');
        !           437:                break;
        !           438:            case 'w':
        !           439:                width = (width * 10) + (this_opt - '0');
        !           440:                break;
        !           441:            default:
        !           442:                if (this_opt != '1')
        !           443:                    usage();
        !           444:                last_opt = this_opt;
        !           445:                width = 0;
        !           446:            }
        !           447:            continue;
        !           448:        }
        !           449:        switch (this_opt) {
        !           450:        case 'C':
        !           451:            capdump = TRUE;
        !           452:            outform = F_TERMCAP;
        !           453:            sortmode = S_TERMCAP;
        !           454:            break;
        !           455:        case 'I':
        !           456:            infodump = TRUE;
        !           457:            outform = F_TERMINFO;
        !           458:            sortmode = S_TERMINFO;
        !           459:            break;
        !           460:        case 'L':
        !           461:            infodump = TRUE;
        !           462:            outform = F_VARIABLE;
        !           463:            sortmode = S_VARIABLE;
        !           464:            break;
        !           465:        case 'N':
        !           466:            smart_defaults = FALSE;
        !           467:            break;
        !           468:        case 'R':
        !           469:            tversion = optarg;
        !           470:            break;
        !           471:        case 'T':
        !           472:            limited = FALSE;
        !           473:            break;
        !           474:        case 'V':
        !           475:            puts(NCURSES_VERSION);
        !           476:            return EXIT_SUCCESS;
        !           477:        case 'c':
        !           478:            check_only = TRUE;
        !           479:            break;
        !           480:        case 'e':
        !           481:            namelst = make_namelist(optarg);
        !           482:            break;
        !           483:        case 'f':
        !           484:            formatted = TRUE;
        !           485:            break;
        !           486:        case 'G':
        !           487:            numbers = 1;
        !           488:            break;
        !           489:        case 'g':
        !           490:            numbers = -1;
        !           491:            break;
        !           492:        case 'o':
        !           493:            outdir = optarg;
        !           494:            break;
        !           495:        case 'r':
        !           496:            forceresolve = TRUE;
        !           497:            break;
        !           498:        case 's':
        !           499:            showsummary = TRUE;
        !           500:            break;
        !           501:        case 'v':
        !           502:            v_opt = 0;
        !           503:            break;
        !           504:        case 'w':
        !           505:            width = 0;
        !           506:            break;
1.7       millert   507: #if NCURSES_XNAMES
1.14    ! millert   508:        case 'x':
        !           509:            use_extended_names(TRUE);
        !           510:            break;
1.7       millert   511: #endif
1.14    ! millert   512:        default:
        !           513:            usage();
1.1       millert   514:        }
1.14    ! millert   515:        last_opt = this_opt;
        !           516:     }
1.1       millert   517:
1.14    ! millert   518:     debug_level = (v_opt > 0) ? v_opt : (v_opt == 0);
        !           519:     _nc_tracing = (1 << debug_level) - 1;
1.6       millert   520:
1.14    ! millert   521:     if (_nc_tracing) {
        !           522:        save_check_termtype = _nc_check_termtype;
        !           523:        _nc_check_termtype = check_termtype;
        !           524:     }
1.4       millert   525: #ifndef HAVE_BIG_CORE
1.14    ! millert   526:     /*
        !           527:      * Aaargh! immedhook seriously hoses us!
        !           528:      *
        !           529:      * One problem with immedhook is it means we can't do -e.  Problem
        !           530:      * is that we can't guarantee that for each terminal listed, all the
        !           531:      * terminals it depends on will have been kept in core for reference
        !           532:      * resolution -- in fact it's certain the primitive types at the end
        !           533:      * of reference chains *won't* be in core unless they were explicitly
        !           534:      * in the select list themselves.
        !           535:      */
        !           536:     if (namelst && (!infodump && !capdump)) {
        !           537:        (void) fprintf(stderr,
        !           538:            "Sorry, -e can't be used without -I or -C\n");
        !           539:        cleanup();
        !           540:        return EXIT_FAILURE;
        !           541:     }
1.4       millert   542: #endif /* HAVE_BIG_CORE */
                    543:
1.14    ! millert   544:     if (optind < argc) {
        !           545:        source_file = argv[optind++];
1.1       millert   546:        if (optind < argc) {
1.14    ! millert   547:            fprintf(stderr,
        !           548:                "%s: Too many file names.  Usage:\n\t%s %s",
        !           549:                _nc_progname,
        !           550:                _nc_progname,
        !           551:                usage_string);
        !           552:            return EXIT_FAILURE;
        !           553:        }
        !           554:     } else {
        !           555:        if (infodump == TRUE) {
        !           556:            /* captoinfo's no-argument case */
        !           557:            source_file = "/usr/share/misc/termcap";
        !           558:            if ((termcap = getenv("TERMCAP")) != 0
        !           559:                && (namelst = make_namelist(getenv("TERM"))) != 0) {
        !           560:                if (access(termcap, F_OK) == 0) {
        !           561:                    /* file exists */
        !           562:                    source_file = termcap;
        !           563:                } else if (strcpy(my_tmpname, "/tmp/tic.XXXXXXXX")
        !           564:                    && (fd = mkstemp(my_tmpname)) != -1
        !           565:                    && (tmp_fp = fdopen(fd, "w")) != 0) {
        !           566:                    fprintf(tmp_fp, "%s\n", termcap);
        !           567:                    fclose(tmp_fp);
        !           568:                    tmp_fp = fopen(source_file, "r");
        !           569:                    to_remove = source_file;
        !           570:                } else {
        !           571:                    failed("mkstemp");
1.1       millert   572:                }
1.14    ! millert   573:            }
1.1       millert   574:        } else {
1.14    ! millert   575:            /* tic */
        !           576:            fprintf(stderr,
        !           577:                "%s: File name needed.  Usage:\n\t%s %s",
        !           578:                _nc_progname,
        !           579:                _nc_progname,
        !           580:                usage_string);
        !           581:            cleanup();
        !           582:            return EXIT_FAILURE;
1.1       millert   583:        }
1.14    ! millert   584:     }
        !           585:
        !           586:     if (tmp_fp == 0
        !           587:        && (tmp_fp = fopen(source_file, "r")) == 0) {
        !           588:        fprintf(stderr, "%s: Can't open %s\n", _nc_progname, source_file);
        !           589:        return EXIT_FAILURE;
        !           590:     }
1.1       millert   591:
1.14    ! millert   592:     if (infodump)
        !           593:        dump_init(tversion,
        !           594:            smart_defaults
        !           595:            ? outform
        !           596:            : F_LITERAL,
        !           597:            sortmode, width, debug_level, formatted);
        !           598:     else if (capdump)
        !           599:        dump_init(tversion,
        !           600:            outform,
        !           601:            sortmode, width, debug_level, FALSE);
1.1       millert   602:
1.14    ! millert   603:     /* parse entries out of the source file */
        !           604:     _nc_set_source(source_file);
1.1       millert   605: #ifndef HAVE_BIG_CORE
1.14    ! millert   606:     if (!(check_only || infodump || capdump))
        !           607:        _nc_set_writedir(outdir);
1.1       millert   608: #endif /* HAVE_BIG_CORE */
1.14    ! millert   609:     _nc_read_entry_source(tmp_fp, (char *) NULL,
        !           610:        !smart_defaults, FALSE,
        !           611:        (check_only || infodump || capdump) ? NULLHOOK : immedhook);
        !           612:
        !           613:     /* do use resolution */
        !           614:     if (check_only || (!infodump && !capdump) || forceresolve) {
        !           615:        if (!_nc_resolve_uses() && !check_only) {
        !           616:            cleanup();
        !           617:            return EXIT_FAILURE;
        !           618:        }
        !           619:     }
1.1       millert   620:
1.14    ! millert   621:     /* length check */
        !           622:     if (check_only && (capdump || infodump)) {
        !           623:        for_entry_list(qp) {
        !           624:            if (matches(namelst, qp->tterm.term_names)) {
        !           625:                int len = fmt_entry(&qp->tterm, NULL, TRUE, infodump, numbers);
        !           626:
        !           627:                if (len > (infodump ? MAX_TERMINFO_LENGTH : MAX_TERMCAP_LENGTH))
        !           628:                    (void) fprintf(stderr,
        !           629:                        "warning: resolved %s entry is %d bytes long\n",
        !           630:                        _nc_first_name(qp->tterm.term_names),
        !           631:                        len);
1.2       millert   632:            }
                    633:        }
1.14    ! millert   634:     }
1.1       millert   635:
1.14    ! millert   636:     /* write or dump all entries */
        !           637:     if (!check_only) {
        !           638:        if (!infodump && !capdump) {
        !           639:            _nc_set_writedir(outdir);
1.1       millert   640:            for_entry_list(qp)
                    641:                if (matches(namelst, qp->tterm.term_names))
1.14    ! millert   642:                write_it(qp);
        !           643:        } else {
        !           644:            /* this is in case infotocap() generates warnings */
        !           645:            _nc_curr_col = _nc_curr_line = -1;
1.1       millert   646:
1.14    ! millert   647:            for_entry_list(qp) {
        !           648:                if (matches(namelst, qp->tterm.term_names)) {
        !           649:                    int j = qp->cend - qp->cstart;
        !           650:                    int len = 0;
        !           651:
        !           652:                    /* this is in case infotocap() generates warnings */
        !           653:                    _nc_set_type(_nc_first_name(qp->tterm.term_names));
        !           654:
        !           655:                    (void) fseek(tmp_fp, qp->cstart, SEEK_SET);
        !           656:                    while (j--) {
        !           657:                        if (infodump)
        !           658:                            (void) putchar(fgetc(tmp_fp));
        !           659:                        else
        !           660:                            put_translate(fgetc(tmp_fp));
        !           661:                    }
        !           662:
        !           663:                    len = dump_entry(&qp->tterm, limited, numbers, NULL);
        !           664:                    for (j = 0; j < qp->nuses; j++)
        !           665:                        len += dump_uses((char *) (qp->uses[j].parent), !capdump);
        !           666:                    (void) putchar('\n');
        !           667:                    if (debug_level != 0 && !limited)
        !           668:                        printf("# length=%d\n", len);
1.1       millert   669:                }
                    670:            }
1.14    ! millert   671:            if (!namelst) {
        !           672:                int c, oldc = '\0';
        !           673:                bool in_comment = FALSE;
        !           674:                bool trailing_comment = FALSE;
        !           675:
        !           676:                (void) fseek(tmp_fp, _nc_tail->cend, SEEK_SET);
        !           677:                while ((c = fgetc(tmp_fp)) != EOF) {
        !           678:                    if (oldc == '\n') {
        !           679:                        if (c == '#') {
        !           680:                            trailing_comment = TRUE;
        !           681:                            in_comment = TRUE;
        !           682:                        } else {
        !           683:                            in_comment = FALSE;
1.1       millert   684:                        }
                    685:                    }
1.14    ! millert   686:                    if (trailing_comment
        !           687:                        && (in_comment || (oldc == '\n' && c == '\n')))
        !           688:                        putchar(c);
        !           689:                    oldc = c;
1.1       millert   690:                }
                    691:            }
                    692:        }
1.14    ! millert   693:     }
1.1       millert   694:
1.14    ! millert   695:     /* Show the directory into which entries were written, and the total
        !           696:      * number of entries
        !           697:      */
        !           698:     if (showsummary
        !           699:        && (!(check_only || infodump || capdump))) {
        !           700:        int total = _nc_tic_written();
        !           701:        if (total != 0)
        !           702:            fprintf(log_fp, "%d entries written to %s\n",
        !           703:                total,
        !           704:                _nc_tic_dir((char *) 0));
        !           705:        else
        !           706:            fprintf(log_fp, "No entries written\n");
        !           707:     }
        !           708:     cleanup();
        !           709:     return (EXIT_SUCCESS);
1.6       millert   710: }
                    711:
                    712: /*
                    713:  * This bit of legerdemain turns all the terminfo variable names into
                    714:  * references to locations in the arrays Booleans, Numbers, and Strings ---
                    715:  * precisely what's needed (see comp_parse.c).
                    716:  */
1.9       millert   717:
1.14    ! millert   718: TERMINAL *cur_term;            /* tweak to avoid linking lib_cur_term.c */
1.6       millert   719:
                    720: #undef CUR
                    721: #define CUR tp->
                    722:
                    723: /* other sanity-checks (things that we don't want in the normal
                    724:  * logic that reads a terminfo entry)
                    725:  */
1.14    ! millert   726: static void
        !           727: check_termtype(TERMTYPE * tp)
1.6       millert   728: {
1.14    ! millert   729:     bool conflict = FALSE;
        !           730:     unsigned j, k;
        !           731:     char fkeys[STRCOUNT];
        !           732:
        !           733:     /*
        !           734:      * A terminal entry may contain more than one keycode assigned to
        !           735:      * a given string (e.g., KEY_END and KEY_LL).  But curses will only
        !           736:      * return one (the last one assigned).
        !           737:      */
        !           738:     memset(fkeys, 0, sizeof(fkeys));
        !           739:     for (j = 0; _nc_tinfo_fkeys[j].code; j++) {
        !           740:        char *a = tp->Strings[_nc_tinfo_fkeys[j].offset];
        !           741:        bool first = TRUE;
        !           742:        if (!VALID_STRING(a))
        !           743:            continue;
        !           744:        for (k = j + 1; _nc_tinfo_fkeys[k].code; k++) {
        !           745:            char *b = tp->Strings[_nc_tinfo_fkeys[k].offset];
        !           746:            if (!VALID_STRING(b)
        !           747:                || fkeys[k])
1.6       millert   748:                continue;
1.14    ! millert   749:            if (!strcmp(a, b)) {
        !           750:                fkeys[j] = 1;
        !           751:                fkeys[k] = 1;
        !           752:                if (first) {
        !           753:                    if (!conflict) {
        !           754:                        _nc_warning("Conflicting key definitions (using the last)");
        !           755:                        conflict = TRUE;
1.6       millert   756:                    }
1.14    ! millert   757:                    fprintf(stderr, "... %s is the same as %s",
        !           758:                        keyname(_nc_tinfo_fkeys[j].code),
        !           759:                        keyname(_nc_tinfo_fkeys[k].code));
        !           760:                    first = FALSE;
        !           761:                } else {
        !           762:                    fprintf(stderr, ", %s",
        !           763:                        keyname(_nc_tinfo_fkeys[k].code));
1.6       millert   764:                }
                    765:            }
                    766:        }
1.14    ! millert   767:        if (!first)
        !           768:            fprintf(stderr, "\n");
        !           769:     }
        !           770:
        !           771:     /*
        !           772:      * Quick check for color.  We could also check if the ANSI versus
        !           773:      * non-ANSI strings are misused.
        !           774:      */
        !           775:     if ((max_colors > 0) != (max_pairs > 0)
        !           776:        || (max_colors > max_pairs))
        !           777:        _nc_warning("inconsistent values for max_colors and max_pairs");
        !           778:
        !           779:     PAIRED(set_foreground, set_background)
        !           780:        PAIRED(set_a_foreground, set_a_background)
1.6       millert   781:
1.14    ! millert   782:     /*
        !           783:      * These may be mismatched because the terminal description relies on
        !           784:      * restoring the cursor visibility by resetting it.
        !           785:      */
        !           786:        ANDMISSING(cursor_invisible, cursor_normal)
        !           787:        ANDMISSING(cursor_visible, cursor_normal)
        !           788:
        !           789:     /*
        !           790:      * From XSI & O'Reilly, we gather that sc/rc are required if csr is
        !           791:      * given, because the cursor position after the scrolling operation is
        !           792:      * performed is undefined.
        !           793:      */
        !           794:        ANDMISSING(change_scroll_region, save_cursor)
        !           795:        ANDMISSING(change_scroll_region, restore_cursor)
        !           796:
        !           797:     /*
        !           798:      * Some standard applications (e.g., vi) and some non-curses
        !           799:      * applications (e.g., jove) get confused if we have both ich/ich1 and
        !           800:      * smir/rmir.  Let's be nice and warn about that, too, even though
        !           801:      * ncurses handles it.
        !           802:      */
1.6       millert   803:        if ((PRESENT(enter_insert_mode) || PRESENT(exit_insert_mode))
1.14    ! millert   804:        && (PRESENT(insert_character) || PRESENT(parm_ich))) {
        !           805:        _nc_warning("non-curses applications may be confused by ich/ich1 with smir/rmir");
        !           806:     }
1.6       millert   807:
1.14    ! millert   808:     /*
        !           809:      * Finally, do the non-verbose checks
        !           810:      */
        !           811:     if (save_check_termtype != 0)
        !           812:        save_check_termtype(tp);
1.1       millert   813: }