1 /****************************************************************************
2 * Copyright (c) 1998 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 ****************************************************************************/
35 * tic.c --- Main program for terminfo compiler
40 #include <progs.priv.h>
42 #include <dump_entry.h>
43 #include <term_entry.h>
45 MODULE_ID("$Id: tic.c,v 1.28 1998/02/11 12:14:02 tom Exp $")
47 const char *_nc_progname = "tic";
50 static bool showsummary = FALSE;
52 static const char usage_string[] = "[-hc] [-v[n]] [-e names] [-CILNRTrsw1] source-file\n";
54 static void usage(void)
56 static const char *const tbl[] = {
58 " -1 format translation output one capability per line",
59 " -C translate entries to termcap source form",
60 " -I translate entries to terminfo source form",
61 " -L translate entries to full terminfo source form",
62 " -N disable smart defaults for source translation",
63 " -R restrict translation to given terminfo/termcap version",
64 " -T remove size-restrictions on compiled description",
65 " -c check only, validate input without compiling or translating",
66 " -e<names> translate/compile only entries named by comma-separated list",
67 " -o<dir> set output directory for compiled entry writes",
68 " -r force resolution of all use entries in source translation",
69 " -s print summary statistics",
70 " -v[n] set verbosity level",
71 " -w[n] set format width for translation output",
74 " <file> file to translate or compile"
78 printf("Usage: %s %s\n", _nc_progname, usage_string);
79 for (j = 0; j < sizeof(tbl)/sizeof(tbl[0]); j++)
84 static bool immedhook(ENTRY *ep)
85 /* write out entries with no use capabilities immediately to save storage */
89 * This is strictly a core-economy kluge. The really clean way to handle
90 * compilation is to slurp the whole file into core and then do all the
91 * name-collision checks and entry writes in one swell foop. But the
92 * terminfo master file is large enough that some core-poor systems swap
93 * like crazy when you compile it this way...there have been reports of
94 * this process taking *three hours*, rather than the twenty seconds or
95 * less typical on my development box.
97 * So. This hook *immediately* writes out the referenced entry if it
98 * has no use capabilities. The compiler main loop refrains from
99 * adding the entry to the in-core list when this hook fires. If some
100 * other entry later needs to reference an entry that got written
101 * immediately, that's OK; the resolution code will fetch it off disk
102 * when it can't find it in core.
104 * Name collisions will still be detected, just not as cleanly. The
105 * write_entry() code complains before overwriting an entry that
106 * postdates the time of tic's first call to write_entry(). Thus
107 * it will complain about overwriting entries newly made during the
108 * tic run, but not about overwriting ones that predate it.
110 * The reason this is a hook, and not in line with the rest of the
111 * compiler code, is that the support for termcap fallback cannot assume
112 * it has anywhere to spool out these entries!
114 * The _nc_set_type() call here requires a compensating one in
117 * If you define HAVE_BIG_CORE, you'll disable this kluge. This will
118 * make tic a bit faster (because the resolution code won't have to do
119 * disk I/O nearly as often).
123 int oldline = _nc_curr_line;
125 _nc_set_type(_nc_first_name(ep->tterm.term_names));
126 _nc_curr_line = ep->startline;
127 _nc_write_entry(&ep->tterm);
128 _nc_curr_line = oldline;
129 free(ep->tterm.str_table);
133 #endif /* HAVE_BIG_CORE */
137 static void put_translate(int c)
138 /* emit a comment char, translating terminfo names to termcap names */
140 static bool in_name = FALSE;
141 static char namebuf[132], suffix[132], *sp;
153 else if (c == '\n' || c == '@')
157 (void) fputs(namebuf, stdout);
163 else /* ah! candidate name! */
166 NCURSES_CONST char *tp;
172 if ((up = strchr(namebuf, '#')) != 0
173 || (up = strchr(namebuf, '=')) != 0
174 || ((up = strchr(namebuf, '@')) != 0 && up[1] == '>'))
176 (void) strcpy(suffix, up);
180 if ((tp = nametrans(namebuf)) != 0)
183 (void) fputs(tp, stdout);
184 (void) fputs(suffix, stdout);
189 /* couldn't find a translation, just dump the name */
191 (void) fputs(namebuf, stdout);
192 (void) fputs(suffix, stdout);
199 /* Returns a string, stripped of leading/trailing whitespace */
200 static char *stripped(char *src)
202 while (isspace(*src))
205 char *dst = strcpy(malloc(strlen(src)+1), src);
206 size_t len = strlen(dst);
207 while (--len != 0 && isspace(dst[len]))
214 /* Parse the "-e" option-value into a list of names */
215 static const char **make_namelist(char *src)
217 const char **dst = 0;
223 if (strchr(src, '/') != 0) { /* a filename */
224 FILE *fp = fopen(src, "r");
229 for (pass = 1; pass <= 2; pass++) {
231 while (fgets(buffer, sizeof(buffer), fp) != 0) {
232 if ((s = stripped(buffer)) != 0) {
239 dst = (const char **)calloc(nn+1, sizeof(*dst));
244 } else { /* literal list of names */
245 for (pass = 1; pass <= 2; pass++) {
246 for (n = nn = 0, base = src; ; n++) {
248 if (mark == ',' || mark == '\0') {
253 if ((s = stripped(base)) != 0)
262 dst = (const char **)calloc(nn+1, sizeof(*dst));
266 fprintf(log_fp, "Entries that will be compiled:\n");
267 for (n = 0; dst[n] != 0; n++)
268 fprintf(log_fp, "%d:%s\n", n+1, dst[n]);
273 static bool matches(const char **needle, const char *haystack)
274 /* does entry in needle list match |-separated field in haystack? */
281 for (n = 0; needle[n] != 0; n++)
283 if (_nc_name_match(haystack, needle[n], "|"))
295 int main (int argc, char *argv[])
297 int v_opt = -1, debug_level;
298 int smart_defaults = TRUE;
302 int this_opt, last_opt = '?';
304 int outform = F_TERMINFO; /* output format */
305 int sortmode = S_TERMINFO; /* sort_mode */
308 bool infodump = FALSE; /* running as captoinfo? */
309 bool capdump = FALSE; /* running as infotocap? */
310 bool forceresolve = FALSE; /* force resolution */
312 char *tversion = (char *)NULL;
313 const char *source_file = "terminfo";
314 const char **namelst = 0;
315 char *outdir = (char *)NULL;
316 bool check_only = FALSE;
320 if ((_nc_progname = strrchr(argv[0], '/')) == NULL)
321 _nc_progname = argv[0];
325 infodump = (strcmp(_nc_progname, "captoinfo") == 0);
326 capdump = (strcmp(_nc_progname, "infotocap") == 0);
329 * Processing arguments is a little complicated, since someone made a
330 * design decision to allow the numeric values for -w, -v options to
333 while ((this_opt = getopt(argc, argv, "0123456789CILNR:TVce:o:rsvw")) != EOF) {
334 if (isdigit(this_opt)) {
337 v_opt = (v_opt * 10) + (this_opt - '0');
340 width = (width * 10) + (this_opt - '0');
354 sortmode = S_TERMCAP;
358 outform = F_TERMINFO;
359 sortmode = S_TERMINFO;
363 outform = F_VARIABLE;
364 sortmode = S_VARIABLE;
367 smart_defaults = FALSE;
376 puts(NCURSES_VERSION);
382 namelst = make_namelist(optarg);
405 debug_level = (v_opt > 0) ? v_opt : (v_opt == 0);
406 _nc_tracing = (1 << debug_level) - 1;
409 source_file = argv[optind++];
412 "%s: Too many file names. Usage:\n\t%s %s",
419 if (infodump == TRUE) {
420 /* captoinfo's no-argument case */
421 source_file = "/etc/termcap";
422 if ((termcap = getenv("TERMCAP")) != NULL) {
423 if (access(termcap, F_OK) == 0) {
425 source_file = termcap;
431 "%s: File name needed. Usage:\n\t%s %s",
439 if (freopen(source_file, "r", stdin) == NULL) {
440 fprintf (stderr, "%s: Can't open %s\n", _nc_progname, source_file);
449 sortmode, width, debug_level);
453 sortmode, width, debug_level);
455 /* parse entries out of the source file */
456 _nc_set_source(source_file);
457 #ifndef HAVE_BIG_CORE
458 if (!(check_only || infodump || capdump))
459 _nc_set_writedir(outdir);
460 #endif /* HAVE_BIG_CORE */
461 _nc_read_entry_source(stdin, (char *)NULL,
462 !smart_defaults, FALSE,
463 (check_only || infodump || capdump) ? NULLHOOK : immedhook);
465 /* do use resolution */
466 if (check_only || (!infodump && !capdump) || forceresolve)
467 if (!_nc_resolve_uses() && !check_only)
470 #ifndef HAVE_BIG_CORE
472 * Aaargh! immedhook seriously hoses us!
474 * One problem with immedhook is it means we can't do -e. Problem
475 * is that we can't guarantee that for each terminal listed, all the
476 * terminals it depends on will have been kept in core for reference
477 * resolution -- in fact it's certain the primitive types at the end
478 * of reference chains *won't* be in core unless they were explicitly
479 * in the select list themselves.
481 if (namelst && (!infodump && !capdump))
483 (void) fprintf(stderr,
484 "Sorry, -e can't be used without -I or -C\n");
487 #endif /* HAVE_BIG_CORE */
490 if (check_only && (capdump || infodump))
494 if (matches(namelst, qp->tterm.term_names))
496 int len = fmt_entry(&qp->tterm, NULL, TRUE, infodump);
498 if (len>(infodump?MAX_TERMINFO_LENGTH:MAX_TERMCAP_LENGTH))
499 (void) fprintf(stderr,
500 "warning: resolved %s entry is %d bytes long\n",
501 _nc_first_name(qp->tterm.term_names),
507 /* write or dump all entries */
510 if (!infodump && !capdump)
512 _nc_set_writedir(outdir);
514 if (matches(namelst, qp->tterm.term_names))
516 _nc_set_type(_nc_first_name(qp->tterm.term_names));
517 _nc_curr_line = qp->startline;
518 _nc_write_entry(&qp->tterm);
523 /* this is in case infotocap() generates warnings */
524 _nc_curr_col = _nc_curr_line = -1;
527 if (matches(namelst, qp->tterm.term_names))
529 int j = qp->cend - qp->cstart;
532 /* this is in case infotocap() generates warnings */
533 _nc_set_type(_nc_first_name(qp->tterm.term_names));
535 (void) fseek(stdin, qp->cstart, SEEK_SET);
538 (void) putchar(getchar());
540 put_translate(getchar());
542 len = dump_entry(&qp->tterm, limited, NULL);
543 for (j = 0; j < qp->nuses; j++)
544 len += dump_uses((char *)(qp->uses[j].parent), infodump);
545 (void) putchar('\n');
546 if (debug_level != 0 && !limited)
547 printf("# length=%d\n", len);
552 bool in_comment = FALSE;
553 bool trailing_comment = FALSE;
555 (void) fseek(stdin, _nc_tail->cend, SEEK_SET);
556 while ((c = getchar()) != EOF)
560 trailing_comment = TRUE;
567 && (in_comment || (oldc == '\n' && c == '\n')))
575 /* Show the directory into which entries were written, and the total
579 && (!(check_only || infodump || capdump))) {
580 int total = _nc_tic_written();
582 fprintf(log_fp, "%d entries written to %s\n",
584 _nc_tic_dir((char *)0));
586 fprintf(log_fp, "No entries written\n");
588 return(EXIT_SUCCESS);