1 /****************************************************************************
2 * Copyright (c) 1998,1999,2000 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>
43 #include <dump_entry.h>
44 #include <term_entry.h>
45 #include <transform.h>
47 MODULE_ID("$Id: tic.c,v 1.82 2000/10/01 02:11:39 tom Exp $")
49 const char *_nc_progname = "tic";
53 static bool showsummary = FALSE;
54 static const char *to_remove;
56 static void (*save_check_termtype) (TERMTYPE *);
57 static void check_termtype(TERMTYPE * tt);
59 static const char usage_string[] = "[-V] [-v[n]] [-e names] [-CILNRTcfrswx1] source-file\n";
76 failed(const char *msg)
86 static const char *const tbl[] =
89 " -1 format translation output one capability per line",
90 " -C translate entries to termcap source form",
91 " -I translate entries to terminfo source form",
92 " -L translate entries to full terminfo source form",
93 " -N disable smart defaults for source translation",
94 " -R restrict translation to given terminfo/termcap version",
95 " -T remove size-restrictions on compiled description",
98 " -a retain commented-out capabilities (sets -x also)",
100 " -c check only, validate input without compiling or translating",
101 " -f format complex strings for readability",
102 " -G format %{number} to %'char'",
103 " -g format %'char' to %{number}",
104 " -e<names> translate/compile only entries named by comma-separated list",
105 " -o<dir> set output directory for compiled entry writes",
106 " -r force resolution of all use entries in source translation",
107 " -s print summary statistics",
108 " -v[n] set verbosity level",
109 " -w[n] set format width for translation output",
111 " -x treat unknown capabilities as user-defined",
115 " <file> file to translate or compile"
119 fprintf(stderr, "Usage: %s %s\n", _nc_progname, usage_string);
120 for (j = 0; j < SIZEOF(tbl); j++) {
121 fputs(tbl[j], stderr);
129 #define S_QUOTE '\'';
137 char result[MAX_ENTRY_SIZE];
140 * Look for strings that contain %{number}, convert them to %'char',
141 * which is shorter and runs a little faster.
143 for (n = 0; n < STRCOUNT; n++) {
144 s = ep->tterm.Strings[n];
146 && strchr(s, L_BRACE) != 0) {
149 while ((ch = *t++) != 0) {
153 } else if ((ch == '%')
154 && (*t == L_BRACE)) {
156 long value = strtol(t + 1, &v, 0);
160 && value != '\\' /* FIXME */
162 && isprint((int) value)) {
171 if (strlen(result) < strlen(s))
176 _nc_set_type(_nc_first_name(ep->tterm.term_names));
177 _nc_curr_line = ep->startline;
178 _nc_write_entry(&ep->tterm);
182 immedhook(ENTRY * ep GCC_UNUSED)
183 /* write out entries with no use capabilities immediately to save storage */
187 * This is strictly a core-economy kluge. The really clean way to handle
188 * compilation is to slurp the whole file into core and then do all the
189 * name-collision checks and entry writes in one swell foop. But the
190 * terminfo master file is large enough that some core-poor systems swap
191 * like crazy when you compile it this way...there have been reports of
192 * this process taking *three hours*, rather than the twenty seconds or
193 * less typical on my development box.
195 * So. This hook *immediately* writes out the referenced entry if it
196 * has no use capabilities. The compiler main loop refrains from
197 * adding the entry to the in-core list when this hook fires. If some
198 * other entry later needs to reference an entry that got written
199 * immediately, that's OK; the resolution code will fetch it off disk
200 * when it can't find it in core.
202 * Name collisions will still be detected, just not as cleanly. The
203 * write_entry() code complains before overwriting an entry that
204 * postdates the time of tic's first call to write_entry(). Thus
205 * it will complain about overwriting entries newly made during the
206 * tic run, but not about overwriting ones that predate it.
208 * The reason this is a hook, and not in line with the rest of the
209 * compiler code, is that the support for termcap fallback cannot assume
210 * it has anywhere to spool out these entries!
212 * The _nc_set_type() call here requires a compensating one in
215 * If you define HAVE_BIG_CORE, you'll disable this kluge. This will
216 * make tic a bit faster (because the resolution code won't have to do
217 * disk I/O nearly as often).
219 if (ep->nuses == 0) {
220 int oldline = _nc_curr_line;
223 _nc_curr_line = oldline;
224 free(ep->tterm.str_table);
227 #endif /* HAVE_BIG_CORE */
233 /* emit a comment char, translating terminfo names to termcap names */
235 static bool in_name = FALSE;
236 static size_t have, used;
237 static char *namebuf, *suffix;
240 if (used + 1 >= have) {
242 namebuf = typeRealloc(char, have, namebuf);
243 suffix = typeRealloc(char, have, suffix);
245 if (c == '\n' || c == '@') {
246 namebuf[used++] = '\0';
248 (void) fputs(namebuf, stdout);
251 } else if (c != '>') {
253 } else { /* ah! candidate name! */
255 NCURSES_CONST char *tp;
257 namebuf[used++] = '\0';
261 if ((up = strchr(namebuf, '#')) != 0
262 || (up = strchr(namebuf, '=')) != 0
263 || ((up = strchr(namebuf, '@')) != 0 && up[1] == '>')) {
264 (void) strcpy(suffix, up);
268 if ((tp = nametrans(namebuf)) != 0) {
270 (void) fputs(tp, stdout);
271 (void) fputs(suffix, stdout);
274 /* couldn't find a translation, just dump the name */
276 (void) fputs(namebuf, stdout);
277 (void) fputs(suffix, stdout);
291 /* Returns a string, stripped of leading/trailing whitespace */
295 while (isspace(*src))
298 char *dst = strcpy(malloc(strlen(src) + 1), src);
299 size_t len = strlen(dst);
300 while (--len != 0 && isspace(dst[len]))
308 open_input(const char *filename)
310 FILE *fp = fopen(filename, "r");
314 fprintf(stderr, "%s: Can't open %s\n", _nc_progname, filename);
317 if (fstat(fileno(fp), &sb) < 0
318 || (sb.st_mode & S_IFMT) != S_IFREG) {
319 fprintf(stderr, "%s: %s is not a file\n", _nc_progname, filename);
325 /* Parse the "-e" option-value into a list of names */
327 make_namelist(char *src)
329 const char **dst = 0;
332 unsigned pass, n, nn;
337 } else if (strchr(src, '/') != 0) { /* a filename */
338 FILE *fp = open_input(src);
340 for (pass = 1; pass <= 2; pass++) {
342 while (fgets(buffer, sizeof(buffer), fp) != 0) {
343 if ((s = stripped(buffer)) != 0) {
350 dst = typeCalloc(const char *, nn + 1);
355 } else { /* literal list of names */
356 for (pass = 1; pass <= 2; pass++) {
357 for (n = nn = 0, base = src;; n++) {
359 if (mark == ',' || mark == '\0') {
364 if ((s = stripped(base)) != 0)
373 dst = typeCalloc(const char *, nn + 1);
377 fprintf(log_fp, "Entries that will be compiled:\n");
378 for (n = 0; dst[n] != 0; n++)
379 fprintf(log_fp, "%d:%s\n", n + 1, dst[n]);
385 matches(const char **needle, const char *haystack)
386 /* does entry in needle list match |-separated field in haystack? */
392 for (n = 0; needle[n] != 0; n++) {
393 if (_nc_name_match(haystack, needle[n], "|")) {
404 open_tempfile(char *name)
408 int fd = mkstemp(name);
410 result = fdopen(fd, "w");
412 if (tmpnam(name) != 0)
413 result = fopen(name, "w");
419 main(int argc, char *argv[])
421 char my_tmpname[PATH_MAX];
422 int v_opt = -1, debug_level;
423 int smart_defaults = TRUE;
427 int this_opt, last_opt = '?';
429 int outform = F_TERMINFO; /* output format */
430 int sortmode = S_TERMINFO; /* sort_mode */
433 bool formatted = FALSE; /* reformat complex strings? */
434 int numbers = 0; /* format "%'char'" to/from "%{number}" */
435 bool infodump = FALSE; /* running as captoinfo? */
436 bool capdump = FALSE; /* running as infotocap? */
437 bool forceresolve = FALSE; /* force resolution */
439 char *tversion = (char *) NULL;
440 const char *source_file = "terminfo";
441 const char **namelst = 0;
442 char *outdir = (char *) NULL;
443 bool check_only = FALSE;
447 _nc_progname = _nc_basename(argv[0]);
449 if ((infodump = (strcmp(_nc_progname, PROG_CAPTOINFO) == 0)) != FALSE) {
450 outform = F_TERMINFO;
451 sortmode = S_TERMINFO;
453 if ((capdump = (strcmp(_nc_progname, PROG_INFOTOCAP) == 0)) != FALSE) {
455 sortmode = S_TERMCAP;
458 use_extended_names(FALSE);
462 * Processing arguments is a little complicated, since someone made a
463 * design decision to allow the numeric values for -w, -v options to
466 while ((this_opt = getopt(argc, argv,
467 "0123456789CILNR:TVace:fGgo:rsvwx")) != EOF) {
468 if (isdigit(this_opt)) {
471 v_opt = (v_opt * 10) + (this_opt - '0');
474 width = (width * 10) + (this_opt - '0');
488 sortmode = S_TERMCAP;
492 outform = F_TERMINFO;
493 sortmode = S_TERMINFO;
497 outform = F_VARIABLE;
498 sortmode = S_VARIABLE;
501 smart_defaults = FALSE;
510 puts(curses_version());
516 namelst = make_namelist(optarg);
544 _nc_disable_period = TRUE;
547 use_extended_names(TRUE);
556 debug_level = (v_opt > 0) ? v_opt : (v_opt == 0);
557 set_trace_level(debug_level);
560 save_check_termtype = _nc_check_termtype;
561 _nc_check_termtype = check_termtype;
565 * Aaargh! immedhook seriously hoses us!
567 * One problem with immedhook is it means we can't do -e. Problem
568 * is that we can't guarantee that for each terminal listed, all the
569 * terminals it depends on will have been kept in core for reference
570 * resolution -- in fact it's certain the primitive types at the end
571 * of reference chains *won't* be in core unless they were explicitly
572 * in the select list themselves.
574 if (namelst && (!infodump && !capdump)) {
575 (void) fprintf(stderr,
576 "Sorry, -e can't be used without -I or -C\n");
580 #endif /* HAVE_BIG_CORE */
583 source_file = argv[optind++];
586 "%s: Too many file names. Usage:\n\t%s %s",
593 if (infodump == TRUE) {
594 /* captoinfo's no-argument case */
595 source_file = "/etc/termcap";
596 if ((termcap = getenv("TERMCAP")) != 0
597 && (namelst = make_namelist(getenv("TERM"))) != 0) {
598 if (access(termcap, F_OK) == 0) {
600 source_file = termcap;
601 } else if ((tmp_fp = open_tempfile(strcpy(my_tmpname,
604 source_file = my_tmpname;
605 fprintf(tmp_fp, "%s\n", termcap);
607 tmp_fp = open_input(source_file);
608 to_remove = source_file;
616 "%s: File name needed. Usage:\n\t%s %s",
626 tmp_fp = open_input(source_file);
633 sortmode, width, debug_level, formatted);
637 sortmode, width, debug_level, FALSE);
639 /* parse entries out of the source file */
640 _nc_set_source(source_file);
642 if (!(check_only || infodump || capdump))
643 _nc_set_writedir(outdir);
644 #endif /* HAVE_BIG_CORE */
645 _nc_read_entry_source(tmp_fp, (char *) NULL,
646 !smart_defaults, FALSE,
647 (check_only || infodump || capdump) ? NULLHOOK : immedhook);
649 /* do use resolution */
650 if (check_only || (!infodump && !capdump) || forceresolve) {
651 if (!_nc_resolve_uses(TRUE) && !check_only) {
658 if (check_only && (capdump || infodump)) {
660 if (matches(namelst, qp->tterm.term_names)) {
661 int len = fmt_entry(&qp->tterm, NULL, TRUE, infodump, numbers);
663 if (len > (infodump ? MAX_TERMINFO_LENGTH : MAX_TERMCAP_LENGTH))
664 (void) fprintf(stderr,
665 "warning: resolved %s entry is %d bytes long\n",
666 _nc_first_name(qp->tterm.term_names),
672 /* write or dump all entries */
674 if (!infodump && !capdump) {
675 _nc_set_writedir(outdir);
677 if (matches(namelst, qp->tterm.term_names))
681 /* this is in case infotocap() generates warnings */
682 _nc_curr_col = _nc_curr_line = -1;
685 if (matches(namelst, qp->tterm.term_names)) {
686 int j = qp->cend - qp->cstart;
689 /* this is in case infotocap() generates warnings */
690 _nc_set_type(_nc_first_name(qp->tterm.term_names));
692 (void) fseek(tmp_fp, qp->cstart, SEEK_SET);
695 (void) putchar(fgetc(tmp_fp));
697 put_translate(fgetc(tmp_fp));
700 len = dump_entry(&qp->tterm, limited, numbers, NULL);
701 for (j = 0; j < qp->nuses; j++)
702 len += dump_uses(qp->uses[j].name, !capdump);
703 (void) putchar('\n');
704 if (debug_level != 0 && !limited)
705 printf("# length=%d\n", len);
710 bool in_comment = FALSE;
711 bool trailing_comment = FALSE;
713 (void) fseek(tmp_fp, _nc_tail->cend, SEEK_SET);
714 while ((c = fgetc(tmp_fp)) != EOF) {
717 trailing_comment = TRUE;
724 && (in_comment || (oldc == '\n' && c == '\n')))
732 /* Show the directory into which entries were written, and the total
736 && (!(check_only || infodump || capdump))) {
737 int total = _nc_tic_written();
739 fprintf(log_fp, "%d entries written to %s\n",
741 _nc_tic_dir((char *) 0));
743 fprintf(log_fp, "No entries written\n");
746 return (EXIT_SUCCESS);
750 * This bit of legerdemain turns all the terminfo variable names into
751 * references to locations in the arrays Booleans, Numbers, and Strings ---
752 * precisely what's needed (see comp_parse.c).
755 TERMINAL *cur_term; /* tweak to avoid linking lib_cur_term.c */
761 * Returns the expected number of parameters for the given capability.
764 expected_params(char *name)
767 static const struct {
835 int result = 0; /* function-keys, etc., use none */
837 for (n = 0; n < SIZEOF(table); n++) {
838 if (!strcmp(name, table[n].name)) {
839 result = table[n].count;
848 * Make a quick sanity check for the parameters which are used in the given
849 * strings. If there are no "%p" tokens, then there should be no other "%"
853 check_params(TERMTYPE * tp, char *name, char *value)
855 int expected = expected_params(name);
861 for (n = 0; n < 10; n++)
867 _nc_warning("expected character after %% in %s", name);
869 } else if (*s == 'p') {
870 if (*++s == '\0' || !isdigit((int) *s)) {
871 _nc_warning("expected digit after %%p in %s", name);
885 _nc_warning("%s refers to parameter 0 (%%p0), which is not allowed", name);
887 if (value == set_attributes || expected < 0) {
889 } else if (expected != actual) {
890 _nc_warning("%s uses %d parameters, expected %d", name,
892 for (n = 1; n < actual; n++) {
894 _nc_warning("%s omits parameter %d", name, n);
900 * An sgr string may contain several settings other than the one we're
901 * interested in, essentially sgr0 + rmacs + whatever. As long as the
902 * "whatever" is contained in the sgr string, that is close enough for our
906 similar_sgr(char *a, char *b)
921 check_sgr(TERMTYPE * tp, char *zero, int num, char *cap, const char *name)
923 char *test = tparm(set_attributes,
935 if (!similar_sgr(test, cap)) {
936 _nc_warning("%s differs from sgr(%d): %s", name, num,
939 } else if (strcmp(test, zero)) {
940 _nc_warning("sgr(%d) present, but not %s", num, name);
942 } else if (PRESENT(cap)) {
943 _nc_warning("sgr(%d) missing, but %s present", num, name);
947 #define CHECK_SGR(num,name) check_sgr(tp, zero, num, name, #name)
949 /* other sanity-checks (things that we don't want in the normal
950 * logic that reads a terminfo entry)
953 check_termtype(TERMTYPE * tp)
955 bool conflict = FALSE;
957 char fkeys[STRCOUNT];
960 * A terminal entry may contain more than one keycode assigned to
961 * a given string (e.g., KEY_END and KEY_LL). But curses will only
962 * return one (the last one assigned).
964 memset(fkeys, 0, sizeof(fkeys));
965 for (j = 0; _nc_tinfo_fkeys[j].code; j++) {
966 char *a = tp->Strings[_nc_tinfo_fkeys[j].offset];
968 if (!VALID_STRING(a))
970 for (k = j + 1; _nc_tinfo_fkeys[k].code; k++) {
971 char *b = tp->Strings[_nc_tinfo_fkeys[k].offset];
980 _nc_warning("Conflicting key definitions (using the last)");
983 fprintf(stderr, "... %s is the same as %s",
984 keyname(_nc_tinfo_fkeys[j].code),
985 keyname(_nc_tinfo_fkeys[k].code));
988 fprintf(stderr, ", %s",
989 keyname(_nc_tinfo_fkeys[k].code));
994 fprintf(stderr, "\n");
997 for (j = 0; j < NUM_STRINGS(tp); j++) {
998 char *a = tp->Strings[j];
1000 check_params(tp, ExtStrname(tp, j, strnames), a);
1004 * Quick check for color. We could also check if the ANSI versus
1005 * non-ANSI strings are misused.
1007 if ((max_colors > 0) != (max_pairs > 0)
1008 || (max_colors > max_pairs))
1009 _nc_warning("inconsistent values for max_colors and max_pairs");
1011 PAIRED(set_foreground, set_background);
1012 PAIRED(set_a_foreground, set_a_background);
1015 * These may be mismatched because the terminal description relies on
1016 * restoring the cursor visibility by resetting it.
1018 ANDMISSING(cursor_invisible, cursor_normal);
1019 ANDMISSING(cursor_visible, cursor_normal);
1021 if (PRESENT(cursor_visible) && PRESENT(cursor_normal)
1022 && !strcmp(cursor_visible, cursor_normal))
1023 _nc_warning("cursor_visible is same as cursor_normal");
1026 * From XSI & O'Reilly, we gather that sc/rc are required if csr is
1027 * given, because the cursor position after the scrolling operation is
1028 * performed is undefined.
1030 ANDMISSING(change_scroll_region, save_cursor);
1031 ANDMISSING(change_scroll_region, restore_cursor);
1033 if (PRESENT(set_attributes)) {
1034 char *zero = tparm(set_attributes, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1036 zero = strdup(zero);
1037 CHECK_SGR(1, enter_standout_mode);
1038 CHECK_SGR(2, enter_underline_mode);
1039 CHECK_SGR(3, enter_reverse_mode);
1040 CHECK_SGR(4, enter_blink_mode);
1041 CHECK_SGR(5, enter_dim_mode);
1042 CHECK_SGR(6, enter_bold_mode);
1043 CHECK_SGR(7, enter_secure_mode);
1044 CHECK_SGR(8, enter_protected_mode);
1045 CHECK_SGR(9, enter_alt_charset_mode);
1050 * Some standard applications (e.g., vi) and some non-curses
1051 * applications (e.g., jove) get confused if we have both ich/ich1 and
1052 * smir/rmir. Let's be nice and warn about that, too, even though
1053 * ncurses handles it.
1055 if ((PRESENT(enter_insert_mode) || PRESENT(exit_insert_mode))
1056 && (PRESENT(insert_character) || PRESENT(parm_ich))) {
1057 _nc_warning("non-curses applications may be confused by ich/ich1 with smir/rmir");
1061 * Finally, do the non-verbose checks
1063 if (save_check_termtype != 0)
1064 save_check_termtype(tp);