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>
42 #include <dump_entry.h>
43 #include <term_entry.h>
45 MODULE_ID("$Id: tic.c,v 1.69 2000/04/08 23:53:49 tom Exp $")
47 const char *_nc_progname = "tic";
51 static bool showsummary = FALSE;
52 static const char *to_remove;
54 static void (*save_check_termtype) (TERMTYPE *);
55 static void check_termtype(TERMTYPE * tt);
57 static const char usage_string[] = "[-h] [-v[n]] [-e names] [-CILNRTcfrswx1] source-file\n";
74 failed(const char *msg)
84 static const char *const tbl[] =
87 " -1 format translation output one capability per line",
88 " -C translate entries to termcap source form",
89 " -I translate entries to terminfo source form",
90 " -L translate entries to full terminfo source form",
91 " -N disable smart defaults for source translation",
92 " -R restrict translation to given terminfo/termcap version",
93 " -T remove size-restrictions on compiled description",
95 " -a retain commented-out capabilities (sets -x also)",
97 " -c check only, validate input without compiling or translating",
98 " -f format complex strings for readability",
99 " -G format %{number} to %'char'",
100 " -g format %'char' to %{number}",
101 " -e<names> translate/compile only entries named by comma-separated list",
102 " -o<dir> set output directory for compiled entry writes",
103 " -r force resolution of all use entries in source translation",
104 " -s print summary statistics",
105 " -v[n] set verbosity level",
106 " -w[n] set format width for translation output",
108 " -x treat unknown capabilities as user-defined",
112 " <file> file to translate or compile"
116 fprintf(stderr, "Usage: %s %s\n", _nc_progname, usage_string);
117 for (j = 0; j < sizeof(tbl) / sizeof(tbl[0]); j++) {
118 fputs(tbl[j], stderr);
126 #define S_QUOTE '\'';
134 char result[MAX_ENTRY_SIZE];
137 * Look for strings that contain %{number}, convert them to %'char',
138 * which is shorter and runs a little faster.
140 for (n = 0; n < STRCOUNT; n++) {
141 s = ep->tterm.Strings[n];
143 && strchr(s, L_BRACE) != 0) {
146 while ((ch = *t++) != 0) {
150 } else if ((ch == '%')
151 && (*t == L_BRACE)) {
153 long value = strtol(t + 1, &v, 0);
157 && value != '\\' /* FIXME */
159 && isprint((int) value)) {
168 if (strlen(result) < strlen(s))
173 _nc_set_type(_nc_first_name(ep->tterm.term_names));
174 _nc_curr_line = ep->startline;
175 _nc_write_entry(&ep->tterm);
179 immedhook(ENTRY * ep GCC_UNUSED)
180 /* write out entries with no use capabilities immediately to save storage */
182 #ifndef HAVE_BIG_CORE
184 * This is strictly a core-economy kluge. The really clean way to handle
185 * compilation is to slurp the whole file into core and then do all the
186 * name-collision checks and entry writes in one swell foop. But the
187 * terminfo master file is large enough that some core-poor systems swap
188 * like crazy when you compile it this way...there have been reports of
189 * this process taking *three hours*, rather than the twenty seconds or
190 * less typical on my development box.
192 * So. This hook *immediately* writes out the referenced entry if it
193 * has no use capabilities. The compiler main loop refrains from
194 * adding the entry to the in-core list when this hook fires. If some
195 * other entry later needs to reference an entry that got written
196 * immediately, that's OK; the resolution code will fetch it off disk
197 * when it can't find it in core.
199 * Name collisions will still be detected, just not as cleanly. The
200 * write_entry() code complains before overwriting an entry that
201 * postdates the time of tic's first call to write_entry(). Thus
202 * it will complain about overwriting entries newly made during the
203 * tic run, but not about overwriting ones that predate it.
205 * The reason this is a hook, and not in line with the rest of the
206 * compiler code, is that the support for termcap fallback cannot assume
207 * it has anywhere to spool out these entries!
209 * The _nc_set_type() call here requires a compensating one in
212 * If you define HAVE_BIG_CORE, you'll disable this kluge. This will
213 * make tic a bit faster (because the resolution code won't have to do
214 * disk I/O nearly as often).
216 if (ep->nuses == 0) {
217 int oldline = _nc_curr_line;
220 _nc_curr_line = oldline;
221 free(ep->tterm.str_table);
224 #endif /* HAVE_BIG_CORE */
230 /* emit a comment char, translating terminfo names to termcap names */
232 static bool in_name = FALSE;
233 static size_t have, used;
234 static char *namebuf, *suffix;
237 if (used + 1 >= have) {
239 namebuf = typeRealloc(char, have, namebuf);
240 suffix = typeRealloc(char, have, suffix);
242 if (c == '\n' || c == '@') {
243 namebuf[used++] = '\0';
245 (void) fputs(namebuf, stdout);
248 } else if (c != '>') {
250 } else { /* ah! candidate name! */
252 NCURSES_CONST char *tp;
254 namebuf[used++] = '\0';
258 if ((up = strchr(namebuf, '#')) != 0
259 || (up = strchr(namebuf, '=')) != 0
260 || ((up = strchr(namebuf, '@')) != 0 && up[1] == '>')) {
261 (void) strcpy(suffix, up);
265 if ((tp = nametrans(namebuf)) != 0) {
267 (void) fputs(tp, stdout);
268 (void) fputs(suffix, stdout);
271 /* couldn't find a translation, just dump the name */
273 (void) fputs(namebuf, stdout);
274 (void) fputs(suffix, stdout);
288 /* Returns a string, stripped of leading/trailing whitespace */
292 while (isspace(*src))
295 char *dst = strcpy(malloc(strlen(src) + 1), src);
296 size_t len = strlen(dst);
297 while (--len != 0 && isspace(dst[len]))
304 /* Parse the "-e" option-value into a list of names */
306 make_namelist(char *src)
308 const char **dst = 0;
311 unsigned pass, n, nn;
316 } else if (strchr(src, '/') != 0) { /* a filename */
317 FILE *fp = fopen(src, "r");
321 for (pass = 1; pass <= 2; pass++) {
323 while (fgets(buffer, sizeof(buffer), fp) != 0) {
324 if ((s = stripped(buffer)) != 0) {
331 dst = typeCalloc(const char *, nn + 1);
336 } else { /* literal list of names */
337 for (pass = 1; pass <= 2; pass++) {
338 for (n = nn = 0, base = src;; n++) {
340 if (mark == ',' || mark == '\0') {
345 if ((s = stripped(base)) != 0)
354 dst = typeCalloc(const char *, nn + 1);
358 fprintf(log_fp, "Entries that will be compiled:\n");
359 for (n = 0; dst[n] != 0; n++)
360 fprintf(log_fp, "%d:%s\n", n + 1, dst[n]);
366 matches(const char **needle, const char *haystack)
367 /* does entry in needle list match |-separated field in haystack? */
373 for (n = 0; needle[n] != 0; n++) {
374 if (_nc_name_match(haystack, needle[n], "|")) {
385 open_tempfile(char *name)
389 int fd = mkstemp(name);
391 result = fdopen(fd, "w");
393 if (tmpnam(name) != 0)
394 result = fopen(name, "w");
400 main(int argc, char *argv[])
402 char my_tmpname[PATH_MAX];
403 int v_opt = -1, debug_level;
404 int smart_defaults = TRUE;
408 int this_opt, last_opt = '?';
410 int outform = F_TERMINFO; /* output format */
411 int sortmode = S_TERMINFO; /* sort_mode */
414 bool formatted = FALSE; /* reformat complex strings? */
415 int numbers = 0; /* format "%'char'" to/from "%{number}" */
416 bool infodump = FALSE; /* running as captoinfo? */
417 bool capdump = FALSE; /* running as infotocap? */
418 bool forceresolve = FALSE; /* force resolution */
420 char *tversion = (char *) NULL;
421 const char *source_file = "terminfo";
422 const char **namelst = 0;
423 char *outdir = (char *) NULL;
424 bool check_only = FALSE;
428 if ((_nc_progname = strrchr(argv[0], '/')) == NULL)
429 _nc_progname = argv[0];
433 if ((infodump = (strcmp(_nc_progname, "captoinfo") == 0)) != FALSE) {
434 outform = F_TERMINFO;
435 sortmode = S_TERMINFO;
437 if ((capdump = (strcmp(_nc_progname, "infotocap") == 0)) != FALSE) {
439 sortmode = S_TERMCAP;
442 use_extended_names(FALSE);
446 * Processing arguments is a little complicated, since someone made a
447 * design decision to allow the numeric values for -w, -v options to
450 while ((this_opt = getopt(argc, argv,
451 "0123456789CILNR:TVace:fGgo:rsvwx")) != EOF) {
452 if (isdigit(this_opt)) {
455 v_opt = (v_opt * 10) + (this_opt - '0');
458 width = (width * 10) + (this_opt - '0');
472 sortmode = S_TERMCAP;
476 outform = F_TERMINFO;
477 sortmode = S_TERMINFO;
481 outform = F_VARIABLE;
482 sortmode = S_VARIABLE;
485 smart_defaults = FALSE;
494 puts(NCURSES_VERSION);
500 namelst = make_namelist(optarg);
528 _nc_disable_period = TRUE;
531 use_extended_names(TRUE);
540 debug_level = (v_opt > 0) ? v_opt : (v_opt == 0);
541 set_trace_level(debug_level);
544 save_check_termtype = _nc_check_termtype;
545 _nc_check_termtype = check_termtype;
547 #ifndef HAVE_BIG_CORE
549 * Aaargh! immedhook seriously hoses us!
551 * One problem with immedhook is it means we can't do -e. Problem
552 * is that we can't guarantee that for each terminal listed, all the
553 * terminals it depends on will have been kept in core for reference
554 * resolution -- in fact it's certain the primitive types at the end
555 * of reference chains *won't* be in core unless they were explicitly
556 * in the select list themselves.
558 if (namelst && (!infodump && !capdump)) {
559 (void) fprintf(stderr,
560 "Sorry, -e can't be used without -I or -C\n");
564 #endif /* HAVE_BIG_CORE */
567 source_file = argv[optind++];
570 "%s: Too many file names. Usage:\n\t%s %s",
577 if (infodump == TRUE) {
578 /* captoinfo's no-argument case */
579 source_file = "/etc/termcap";
580 if ((termcap = getenv("TERMCAP")) != 0
581 && (namelst = make_namelist(getenv("TERM"))) != 0) {
582 if (access(termcap, F_OK) == 0) {
584 source_file = termcap;
585 } else if ((tmp_fp = open_tempfile(my_tmpname)) != 0) {
586 source_file = my_tmpname;
587 fprintf(tmp_fp, "%s\n", termcap);
589 tmp_fp = fopen(source_file, "r");
590 to_remove = source_file;
598 "%s: File name needed. Usage:\n\t%s %s",
608 && (tmp_fp = fopen(source_file, "r")) == 0) {
609 fprintf(stderr, "%s: Can't open %s\n", _nc_progname, source_file);
618 sortmode, width, debug_level, formatted);
622 sortmode, width, debug_level, FALSE);
624 /* parse entries out of the source file */
625 _nc_set_source(source_file);
626 #ifndef HAVE_BIG_CORE
627 if (!(check_only || infodump || capdump))
628 _nc_set_writedir(outdir);
629 #endif /* HAVE_BIG_CORE */
630 _nc_read_entry_source(tmp_fp, (char *) NULL,
631 !smart_defaults, FALSE,
632 (check_only || infodump || capdump) ? NULLHOOK : immedhook);
634 /* do use resolution */
635 if (check_only || (!infodump && !capdump) || forceresolve) {
636 if (!_nc_resolve_uses(TRUE) && !check_only) {
643 if (check_only && (capdump || infodump)) {
645 if (matches(namelst, qp->tterm.term_names)) {
646 int len = fmt_entry(&qp->tterm, NULL, TRUE, infodump, numbers);
648 if (len > (infodump ? MAX_TERMINFO_LENGTH : MAX_TERMCAP_LENGTH))
649 (void) fprintf(stderr,
650 "warning: resolved %s entry is %d bytes long\n",
651 _nc_first_name(qp->tterm.term_names),
657 /* write or dump all entries */
659 if (!infodump && !capdump) {
660 _nc_set_writedir(outdir);
662 if (matches(namelst, qp->tterm.term_names))
666 /* this is in case infotocap() generates warnings */
667 _nc_curr_col = _nc_curr_line = -1;
670 if (matches(namelst, qp->tterm.term_names)) {
671 int j = qp->cend - qp->cstart;
674 /* this is in case infotocap() generates warnings */
675 _nc_set_type(_nc_first_name(qp->tterm.term_names));
677 (void) fseek(tmp_fp, qp->cstart, SEEK_SET);
680 (void) putchar(fgetc(tmp_fp));
682 put_translate(fgetc(tmp_fp));
685 len = dump_entry(&qp->tterm, limited, numbers, NULL);
686 for (j = 0; j < qp->nuses; j++)
687 len += dump_uses(qp->uses[j].name, !capdump);
688 (void) putchar('\n');
689 if (debug_level != 0 && !limited)
690 printf("# length=%d\n", len);
695 bool in_comment = FALSE;
696 bool trailing_comment = FALSE;
698 (void) fseek(tmp_fp, _nc_tail->cend, SEEK_SET);
699 while ((c = fgetc(tmp_fp)) != EOF) {
702 trailing_comment = TRUE;
709 && (in_comment || (oldc == '\n' && c == '\n')))
717 /* Show the directory into which entries were written, and the total
721 && (!(check_only || infodump || capdump))) {
722 int total = _nc_tic_written();
724 fprintf(log_fp, "%d entries written to %s\n",
726 _nc_tic_dir((char *) 0));
728 fprintf(log_fp, "No entries written\n");
731 return (EXIT_SUCCESS);
735 * This bit of legerdemain turns all the terminfo variable names into
736 * references to locations in the arrays Booleans, Numbers, and Strings ---
737 * precisely what's needed (see comp_parse.c).
740 TERMINAL *cur_term; /* tweak to avoid linking lib_cur_term.c */
746 * An sgr string may contain several settings other than the one we're
747 * interested in, essentially sgr0 + rmacs + whatever. As long as the
748 * "whatever" is contained in the sgr string, that is close enough for our
752 similar_sgr(char *a, char *b)
767 check_sgr(TERMTYPE * tp, char *zero, int num, char *cap, const char *name)
769 char *test = tparm(set_attributes,
781 if (!similar_sgr(test, cap)) {
782 _nc_warning("%s differs from sgr(%d): %s", name, num,
785 } else if (strcmp(test, zero)) {
786 _nc_warning("sgr(%d) present, but not %s", num, name);
788 } else if (PRESENT(cap)) {
789 _nc_warning("sgr(%d) missing, but %s present", num, name);
793 #define CHECK_SGR(num,name) check_sgr(tp, zero, num, name, #name)
795 /* other sanity-checks (things that we don't want in the normal
796 * logic that reads a terminfo entry)
799 check_termtype(TERMTYPE * tp)
801 bool conflict = FALSE;
803 char fkeys[STRCOUNT];
806 * A terminal entry may contain more than one keycode assigned to
807 * a given string (e.g., KEY_END and KEY_LL). But curses will only
808 * return one (the last one assigned).
810 memset(fkeys, 0, sizeof(fkeys));
811 for (j = 0; _nc_tinfo_fkeys[j].code; j++) {
812 char *a = tp->Strings[_nc_tinfo_fkeys[j].offset];
814 if (!VALID_STRING(a))
816 for (k = j + 1; _nc_tinfo_fkeys[k].code; k++) {
817 char *b = tp->Strings[_nc_tinfo_fkeys[k].offset];
826 _nc_warning("Conflicting key definitions (using the last)");
829 fprintf(stderr, "... %s is the same as %s",
830 keyname(_nc_tinfo_fkeys[j].code),
831 keyname(_nc_tinfo_fkeys[k].code));
834 fprintf(stderr, ", %s",
835 keyname(_nc_tinfo_fkeys[k].code));
840 fprintf(stderr, "\n");
844 * Quick check for color. We could also check if the ANSI versus
845 * non-ANSI strings are misused.
847 if ((max_colors > 0) != (max_pairs > 0)
848 || (max_colors > max_pairs))
849 _nc_warning("inconsistent values for max_colors and max_pairs");
851 PAIRED(set_foreground, set_background);
852 PAIRED(set_a_foreground, set_a_background);
855 * These may be mismatched because the terminal description relies on
856 * restoring the cursor visibility by resetting it.
858 ANDMISSING(cursor_invisible, cursor_normal);
859 ANDMISSING(cursor_visible, cursor_normal);
861 if (PRESENT(cursor_visible) && PRESENT(cursor_normal)
862 && !strcmp(cursor_visible, cursor_normal))
863 _nc_warning("cursor_visible is same as cursor_normal");
866 * From XSI & O'Reilly, we gather that sc/rc are required if csr is
867 * given, because the cursor position after the scrolling operation is
868 * performed is undefined.
870 ANDMISSING(change_scroll_region, save_cursor);
871 ANDMISSING(change_scroll_region, restore_cursor);
873 if (PRESENT(set_attributes)) {
874 char *zero = tparm(set_attributes, 0, 0, 0, 0, 0, 0, 0, 0, 0);
877 CHECK_SGR(1, enter_standout_mode);
878 CHECK_SGR(2, enter_underline_mode);
879 CHECK_SGR(3, enter_reverse_mode);
880 CHECK_SGR(4, enter_blink_mode);
881 CHECK_SGR(5, enter_dim_mode);
882 CHECK_SGR(6, enter_bold_mode);
883 CHECK_SGR(7, enter_secure_mode);
884 CHECK_SGR(8, enter_protected_mode);
885 CHECK_SGR(9, enter_alt_charset_mode);
890 * Some standard applications (e.g., vi) and some non-curses
891 * applications (e.g., jove) get confused if we have both ich/ich1 and
892 * smir/rmir. Let's be nice and warn about that, too, even though
893 * ncurses handles it.
895 if ((PRESENT(enter_insert_mode) || PRESENT(exit_insert_mode))
896 && (PRESENT(insert_character) || PRESENT(parm_ich))) {
897 _nc_warning("non-curses applications may be confused by ich/ich1 with smir/rmir");
901 * Finally, do the non-verbose checks
903 if (save_check_termtype != 0)
904 save_check_termtype(tp);