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

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