2 /***************************************************************************
4 ****************************************************************************
5 * ncurses is copyright (C) 1992-1995 *
7 * zmbenhal@netcom.com *
9 * esr@snark.thyrsus.com *
11 * Permission is hereby granted to reproduce and distribute ncurses *
12 * by any means and for any fee, whether alone or as part of a *
13 * larger distribution, in source or in binary form, PROVIDED *
14 * this notice is included with any such distribution, and is not *
15 * removed from any of its header files. Mention of ncurses in any *
16 * applications linked with it is highly appreciated. *
18 * ncurses comes AS IS with no warranty, implied or expressed. *
20 ***************************************************************************/
23 * tic.c --- Main program for terminfo compiler
28 #include <progs.priv.h>
32 #include <dump_entry.h>
33 #include <term_entry.h>
35 MODULE_ID("$Id: tic.c,v 1.21 1996/12/30 02:24:15 tom Exp $")
37 const char *_nc_progname = "tic";
40 static bool showsummary = FALSE;
42 static const char usage_string[] = "[-hc] [-v[n]] [-e names] [-CILNRTrsw1] source-file\n";
44 static void usage(void)
46 static const char *const tbl[] = {
48 " -1 format translation output one capability per line",
49 " -C translate entries to termcap source form",
50 " -I translate entries to terminfo source form",
51 " -L translate entries to full terminfo source form",
52 " -N disable smart defaults for source translation",
53 " -R restrict translation to given terminfo/termcap version",
54 " -T remove size-restrictions on compiled description",
55 " -c check only, validate input without compiling or translating",
56 " -e<names> translate/compile only entries named by comma-separated list",
57 " -o<dir> set output directory for compiled entry writes",
58 " -r force resolution of all use entries in source translation",
59 " -s print summary statistics",
60 " -v[n] set verbosity level",
61 " -w[n] set format width for translation output",
64 " <file> file to translate or compile"
68 printf("Usage: %s %s\n", _nc_progname, usage_string);
69 for (j = 0; j < sizeof(tbl)/sizeof(tbl[0]); j++)
74 static bool immedhook(ENTRY *ep)
75 /* write out entries with no use capabilities immediately to save storage */
79 * This is strictly a core-economy kluge. The really clean way to handle
80 * compilation is to slurp the whole file into core and then do all the
81 * name-collision checks and entry writes in one swell foop. But the
82 * terminfo master file is large enough that some core-poor systems swap
83 * like crazy when you compile it this way...there have been reports of
84 * this process taking *three hours*, rather than the twenty seconds or
85 * less typical on my development box.
87 * So. This hook *immediately* writes out the referenced entry if it
88 * has no use capabilities. The compiler main loop refrains from
89 * adding the entry to the in-core list when this hook fires. If some
90 * other entry later needs to reference an entry that got written
91 * immediately, that's OK; the resolution code will fetch it off disk
92 * when it can't find it in core.
94 * Name collisions will still be detected, just not as cleanly. The
95 * write_entry() code complains before overwriting an entry that
96 * postdates the time of tic's first call to write_entry(). Thus
97 * it will complain about overwriting entries newly made during the
98 * tic run, but not about overwriting ones that predate it.
100 * The reason this is a hook, and not in line with the rest of the
101 * compiler code, is that the support for termcap fallback cannot assume
102 * it has anywhere to spool out these entries!
104 * The _nc_set_type() call here requires a compensating one in
107 * If you define HAVE_BIG_CORE, you'll disable this kluge. This will
108 * make tic a bit faster (because the resolution code won't have to do
109 * disk I/O nearly as often).
113 int oldline = _nc_curr_line;
115 _nc_set_type(_nc_first_name(ep->tterm.term_names));
116 _nc_curr_line = ep->startline;
117 _nc_write_entry(&ep->tterm);
118 _nc_curr_line = oldline;
119 free(ep->tterm.str_table);
123 #endif /* HAVE_BIG_CORE */
127 static void put_translate(int c)
128 /* emit a comment char, translating terminfo names to termcap names */
130 static bool in_name = FALSE;
131 static char namebuf[132], suffix[132], *sp;
143 else if (c == '\n' || c == '@')
147 (void) fputs(namebuf, stdout);
153 else /* ah! candidate name! */
161 if ((up = strchr(namebuf, '#')) != 0
162 || (up = strchr(namebuf, '=')) != 0
163 || ((up = strchr(namebuf, '@')) != 0 && up[1] == '>'))
165 (void) strcpy(suffix, up);
169 if ((tp = nametrans(namebuf)) != (char *)NULL)
172 (void) fputs(tp, stdout);
173 (void) fputs(suffix, stdout);
178 /* couldn't find a translation, just dump the name */
180 (void) fputs(namebuf, stdout);
181 (void) fputs(suffix, stdout);
188 /* Returns a string, stripped of leading/trailing whitespace */
189 static char *stripped(char *src)
191 while (isspace(*src))
194 char *dst = strcpy(malloc(strlen(src)+1), src);
195 size_t len = strlen(dst);
196 while (--len != 0 && isspace(dst[len]))
203 /* Parse the "-e" option-value into a list of names */
204 static const char **make_namelist(char *src)
206 const char **dst = 0;
212 if (strchr(src, '/') != 0) { /* a filename */
213 FILE *fp = fopen(src, "r");
218 for (pass = 1; pass <= 2; pass++) {
220 while (fgets(buffer, sizeof(buffer), fp) != 0) {
221 if ((s = stripped(buffer)) != 0) {
228 dst = (const char **)calloc(nn+1, sizeof(*dst));
233 } else { /* literal list of names */
234 for (pass = 1; pass <= 2; pass++) {
235 for (n = nn = 0, base = src; ; n++) {
237 if (mark == ',' || mark == '\0') {
242 if ((s = stripped(base)) != 0)
251 dst = (const char **)calloc(nn+1, sizeof(*dst));
255 fprintf(log_fp, "Entries that will be compiled:\n");
256 for (n = 0; dst[n] != 0; n++)
257 fprintf(log_fp, "%d:%s\n", n+1, dst[n]);
262 static bool matches(const char **needle, const char *haystack)
263 /* does entry in needle list match |-separated field in haystack? */
270 for (n = 0; needle[n] != 0; n++)
272 if (_nc_name_match(haystack, needle[n], "|"))
284 int main (int argc, char *argv[])
286 int v_opt = -1, debug_level;
287 int smart_defaults = TRUE;
291 int this_opt, last_opt = '?';
293 int outform = F_TERMINFO; /* output format */
294 int sortmode = S_TERMINFO; /* sort_mode */
297 bool infodump = FALSE; /* running as captoinfo? */
298 bool capdump = FALSE; /* running as infotocap? */
299 bool forceresolve = FALSE; /* force resolution */
301 char *tversion = (char *)NULL;
302 const char *source_file = "terminfo";
303 const char **namelst = 0;
304 char *outdir = (char *)NULL;
305 bool check_only = FALSE;
309 if ((_nc_progname = strrchr(argv[0], '/')) == NULL)
310 _nc_progname = argv[0];
314 infodump = (strcmp(_nc_progname, "captoinfo") == 0);
315 capdump = (strcmp(_nc_progname, "infotocap") == 0);
318 * Processing arguments is a little complicated, since someone made a
319 * design decision to allow the numeric values for -w, -v options to
322 while ((this_opt = getopt(argc, argv, "0123456789CILNR:TVce:orsvw")) != EOF) {
323 if (isdigit(this_opt)) {
326 v_opt = (v_opt * 10) + (this_opt - '0');
329 width = (width * 10) + (this_opt - '0');
343 sortmode = S_TERMCAP;
347 outform = F_TERMINFO;
348 sortmode = S_TERMINFO;
352 outform = F_VARIABLE;
353 sortmode = S_VARIABLE;
356 smart_defaults = FALSE;
365 puts(NCURSES_VERSION);
371 namelst = make_namelist(optarg);
394 debug_level = (v_opt > 0) ? v_opt : (v_opt == 0);
395 _nc_tracing = (1 << debug_level) - 1;
398 source_file = argv[optind++];
401 "%s: Too many file names. Usage:\n\t%s %s",
408 if (infodump == TRUE) {
409 /* captoinfo's no-argument case */
410 source_file = "/etc/termcap";
411 if ((termcap = getenv("TERMCAP")) != NULL) {
412 if (access(termcap, F_OK) == 0) {
414 source_file = termcap;
420 "%s: File name needed. Usage:\n\t%s %s",
428 if (freopen(source_file, "r", stdin) == NULL) {
429 fprintf (stderr, "%s: Can't open %s\n", _nc_progname, source_file);
438 sortmode, width, debug_level);
442 sortmode, width, debug_level);
444 /* parse entries out of the source file */
445 _nc_set_source(source_file);
446 #ifndef HAVE_BIG_CORE
447 if (!(check_only || infodump || capdump))
448 _nc_set_writedir(outdir);
449 #endif /* HAVE_BIG_CORE */
450 _nc_read_entry_source(stdin, (char *)NULL,
451 !smart_defaults, FALSE,
452 (check_only || infodump || capdump) ? NULLHOOK : immedhook);
454 /* do use resolution */
455 if (check_only || (!infodump && !capdump) || forceresolve)
456 if (!_nc_resolve_uses() && !check_only)
459 #ifndef HAVE_BIG_CORE
461 * Aaargh! immedhook seriously hoses us!
463 * One problem with immedhook is it means we can't do -e. Problem
464 * is that we can't guarantee that for each terminal listed, all the
465 * terminals it depends on will have been kept in core for reference
466 * resolution -- in fact it's certain the primitive types at the end
467 * of reference chains *won't* be in core unless they were explicitly
468 * in the select list themselves.
470 if (namelst && (!infodump && !capdump))
472 (void) fprintf(stderr,
473 "Sorry, -e can't be used without -I or -C\n");
476 #endif /* HAVE_BIG_CORE */
479 if (check_only && (capdump || infodump))
483 if (matches(namelst, qp->tterm.term_names))
485 int len = fmt_entry(&qp->tterm, NULL, TRUE, infodump);
487 if (len>(infodump?MAX_TERMINFO_LENGTH:MAX_TERMCAP_LENGTH))
488 (void) fprintf(stderr,
489 "warning: resolved %s entry is %d bytes long\n",
490 _nc_first_name(qp->tterm.term_names),
496 /* write or dump all entries */
499 if (!infodump && !capdump)
501 _nc_set_writedir(outdir);
503 if (matches(namelst, qp->tterm.term_names))
505 _nc_set_type(_nc_first_name(qp->tterm.term_names));
506 _nc_curr_line = qp->startline;
507 _nc_write_entry(&qp->tterm);
512 bool trailing_comment = FALSE;
515 /* this is in case infotocap() generates warnings */
516 _nc_curr_col = _nc_curr_line = -1;
519 if (matches(namelst, qp->tterm.term_names))
521 int j = qp->cend - qp->cstart;
524 /* this is in case infotocap() generates warnings */
525 _nc_set_type(_nc_first_name(qp->tterm.term_names));
527 (void) fseek(stdin, qp->cstart, SEEK_SET);
530 (void) putchar(getchar());
532 put_translate(getchar());
534 len = dump_entry(&qp->tterm, limited, NULL);
535 for (j = 0; j < qp->nuses; j++)
536 len += dump_uses((char *)(qp->uses[j].parent), infodump);
537 (void) putchar('\n');
538 if (debug_level != 0 && !limited)
539 printf("# length=%d\n", len);
543 (void) fseek(stdin, _nc_tail->cend, SEEK_SET);
544 while ((c = getchar()) != EOF)
546 if (oldc == '\n' && c == '#')
547 trailing_comment = TRUE;
548 if (trailing_comment)
556 /* Show the directory into which entries were written, and the total
560 && (!(check_only || infodump || capdump))) {
561 int total = _nc_tic_written();
563 fprintf(log_fp, "%d entries written to %s\n",
565 _nc_tic_dir((char *)0));
567 fprintf(log_fp, "No entries written\n");
569 return(EXIT_SUCCESS);