6163e32961c39900dc60aa1c815d76c044ef6169
[ncurses.git] / progs / tic.c
1 /****************************************************************************
2  * Copyright (c) 1998 Free Software Foundation, Inc.                        *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  ****************************************************************************/
33
34 /*
35  *      tic.c --- Main program for terminfo compiler
36  *                      by Eric S. Raymond
37  *
38  */
39
40 #include <progs.priv.h>
41
42 #include <dump_entry.h>
43 #include <term_entry.h>
44
45 MODULE_ID("$Id: tic.c,v 1.28 1998/02/11 12:14:02 tom Exp $")
46
47 const char *_nc_progname = "tic";
48
49 static  FILE    *log_fp;
50 static  bool    showsummary = FALSE;
51
52 static  const   char usage_string[] = "[-hc] [-v[n]] [-e names] [-CILNRTrsw1] source-file\n";
53
54 static void usage(void)
55 {
56         static const char *const tbl[] = {
57         "Options:",
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",
72         "",
73         "Parameters:",
74         "  <file>     file to translate or compile"
75         };
76         size_t j;
77
78         printf("Usage: %s %s\n", _nc_progname, usage_string);
79         for (j = 0; j < sizeof(tbl)/sizeof(tbl[0]); j++)
80                 puts(tbl[j]);
81         exit(EXIT_FAILURE);
82 }
83
84 static bool immedhook(ENTRY *ep)
85 /* write out entries with no use capabilities immediately to save storage */
86 {
87 #ifndef HAVE_BIG_CORE
88     /*
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.
96      *
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.
103      *
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.
109      *
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!
113      *
114      * The _nc_set_type() call here requires a compensating one in
115      * _nc_parse_entry().
116      *
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).
120      */
121     if (ep->nuses == 0)
122     {
123         int     oldline = _nc_curr_line;
124
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);
130         return(TRUE);
131     }
132     else
133 #endif /* HAVE_BIG_CORE */
134         return(FALSE);
135 }
136
137 static void put_translate(int c)
138 /* emit a comment char, translating terminfo names to termcap names */
139 {
140     static bool in_name = FALSE;
141     static char namebuf[132], suffix[132], *sp;
142
143     if (!in_name)
144     {
145         if (c == '<')
146         {
147             in_name = TRUE;
148             sp = namebuf;
149         }
150         else
151             putchar(c);
152     }
153     else if (c == '\n' || c == '@')
154     {
155         *sp++ = '\0';
156         (void) putchar('<');
157         (void) fputs(namebuf, stdout);
158         putchar(c);
159         in_name = FALSE;
160     }
161     else if (c != '>')
162         *sp++ = c;
163     else                /* ah! candidate name! */
164     {
165         char    *up;
166         NCURSES_CONST char *tp;
167
168         *sp++ = '\0';
169         in_name = FALSE;
170
171         suffix[0] = '\0';
172         if ((up = strchr(namebuf, '#')) != 0
173          || (up = strchr(namebuf, '=')) != 0
174          || ((up = strchr(namebuf, '@')) != 0 && up[1] == '>'))
175         {
176             (void) strcpy(suffix, up);
177             *up = '\0';
178         }
179
180         if ((tp = nametrans(namebuf)) != 0)
181         {
182             (void) putchar(':');
183             (void) fputs(tp, stdout);
184             (void) fputs(suffix, stdout);
185             (void) putchar(':');
186         }
187         else
188         {
189             /* couldn't find a translation, just dump the name */
190             (void) putchar('<');
191             (void) fputs(namebuf, stdout);
192             (void) fputs(suffix, stdout);
193             (void) putchar('>');
194         }
195
196     }
197 }
198
199 /* Returns a string, stripped of leading/trailing whitespace */
200 static char *stripped(char *src)
201 {
202         while (isspace(*src))
203                 src++;
204         if (*src != '\0') {
205                 char *dst = strcpy(malloc(strlen(src)+1), src);
206                 size_t len = strlen(dst);
207                 while (--len != 0 && isspace(dst[len]))
208                         dst[len] = '\0';
209                 return dst;
210         }
211         return 0;
212 }
213
214 /* Parse the "-e" option-value into a list of names */
215 static const char **make_namelist(char *src)
216 {
217         const char **dst = 0;
218
219         char *s, *base;
220         size_t pass, n, nn;
221         char buffer[BUFSIZ];
222
223         if (strchr(src, '/') != 0) {    /* a filename */
224                 FILE *fp = fopen(src, "r");
225                 if (fp == 0) {
226                         perror(src);
227                         exit(EXIT_FAILURE);
228                 }
229                 for (pass = 1; pass <= 2; pass++) {
230                         nn = 0;
231                         while (fgets(buffer, sizeof(buffer), fp) != 0) {
232                                 if ((s = stripped(buffer)) != 0) {
233                                         if (dst != 0)
234                                                 dst[nn] = s;
235                                         nn++;
236                                 }
237                         }
238                         if (pass == 1) {
239                                 dst = (const char **)calloc(nn+1, sizeof(*dst));
240                                 rewind(fp);
241                         }
242                 }
243                 fclose(fp);
244         } else {                        /* literal list of names */
245                 for (pass = 1; pass <= 2; pass++) {
246                         for (n = nn = 0, base = src; ; n++) {
247                                 int mark = src[n];
248                                 if (mark == ',' || mark == '\0') {
249                                         if (pass == 1) {
250                                                 nn++;
251                                         } else {
252                                                 src[n] = '\0';
253                                                 if ((s = stripped(base)) != 0)
254                                                         dst[nn++] = s;
255                                                 base = &src[n+1];
256                                         }
257                                 }
258                                 if (mark == '\0')
259                                         break;
260                         }
261                         if (pass == 1)
262                                 dst = (const char **)calloc(nn+1, sizeof(*dst));
263                 }
264         }
265         if (showsummary) {
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]);
269         }
270         return dst;
271 }
272
273 static bool matches(const char **needle, const char *haystack)
274 /* does entry in needle list match |-separated field in haystack? */
275 {
276         bool code = FALSE;
277         size_t n;
278
279         if (needle != 0)
280         {
281                 for (n = 0; needle[n] != 0; n++)
282                 {
283                         if (_nc_name_match(haystack, needle[n], "|"))
284                         {
285                                 code = TRUE;
286                                 break;
287                         }
288                 }
289         }
290         else
291                 code = TRUE;
292         return(code);
293 }
294
295 int main (int argc, char *argv[])
296 {
297 int     v_opt = -1, debug_level;
298 int     smart_defaults = TRUE;
299 char    *termcap;
300 ENTRY   *qp;
301
302 int     this_opt, last_opt = '?';
303
304 int     outform = F_TERMINFO;   /* output format */
305 int     sortmode = S_TERMINFO;  /* sort_mode */
306
307 int     width = 60;
308 bool    infodump = FALSE;       /* running as captoinfo? */
309 bool    capdump = FALSE;        /* running as infotocap? */
310 bool    forceresolve = FALSE;   /* force resolution */
311 bool    limited = TRUE;
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;
317
318         log_fp = stderr;
319
320         if ((_nc_progname = strrchr(argv[0], '/')) == NULL)
321                 _nc_progname = argv[0];
322         else
323                 _nc_progname++;
324
325         infodump = (strcmp(_nc_progname, "captoinfo") == 0);
326         capdump = (strcmp(_nc_progname, "infotocap") == 0);
327
328         /*
329          * Processing arguments is a little complicated, since someone made a
330          * design decision to allow the numeric values for -w, -v options to
331          * be optional.
332          */
333         while ((this_opt = getopt(argc, argv, "0123456789CILNR:TVce:o:rsvw")) != EOF) {
334                 if (isdigit(this_opt)) {
335                         switch (last_opt) {
336                         case 'v':
337                                 v_opt = (v_opt * 10) + (this_opt - '0');
338                                 break;
339                         case 'w':
340                                 width = (width * 10) + (this_opt - '0');
341                                 break;
342                         default:
343                                 if (this_opt != '1')
344                                         usage();
345                                 last_opt = this_opt;
346                                 width = 0;
347                         }
348                         continue;
349                 }
350                 switch (this_opt) {
351                 case 'C':
352                         capdump  = TRUE;
353                         outform  = F_TERMCAP;
354                         sortmode = S_TERMCAP;
355                         break;
356                 case 'I':
357                         infodump = TRUE;
358                         outform  = F_TERMINFO;
359                         sortmode = S_TERMINFO;
360                         break;
361                 case 'L':
362                         infodump = TRUE;
363                         outform  = F_VARIABLE;
364                         sortmode = S_VARIABLE;
365                         break;
366                 case 'N':
367                         smart_defaults = FALSE;
368                         break;
369                 case 'R':
370                         tversion = optarg;
371                         break;
372                 case 'T':
373                         limited = FALSE;
374                         break;
375                 case 'V':
376                         puts(NCURSES_VERSION);
377                         return EXIT_SUCCESS;
378                 case 'c':
379                         check_only = TRUE;
380                         break;
381                 case 'e':
382                         namelst = make_namelist(optarg);
383                         break;
384                 case 'o':
385                         outdir = optarg;
386                         break;
387                 case 'r':
388                         forceresolve = TRUE;
389                         break;
390                 case 's':
391                         showsummary = TRUE;
392                         break;
393                 case 'v':
394                         v_opt = 0;
395                         break;
396                 case 'w':
397                         width = 0;
398                         break;
399                 default:
400                         usage();
401                 }
402                 last_opt = this_opt;
403         }
404
405         debug_level = (v_opt > 0) ? v_opt : (v_opt == 0);
406         _nc_tracing = (1 << debug_level) - 1;
407
408         if (optind < argc) {
409                 source_file = argv[optind++];
410                 if (optind < argc) {
411                         fprintf (stderr,
412                                 "%s: Too many file names.  Usage:\n\t%s %s",
413                                 _nc_progname,
414                                 _nc_progname,
415                                 usage_string);
416                         return EXIT_FAILURE;
417                 }
418         } else {
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) {
424                                         /* file exists */
425                                         source_file = termcap;
426                                 }
427                         }
428                 } else {
429                 /* tic */
430                         fprintf (stderr,
431                                 "%s: File name needed.  Usage:\n\t%s %s",
432                                 _nc_progname,
433                                 _nc_progname,
434                                 usage_string);
435                         return EXIT_FAILURE;
436                 }
437         }
438
439         if (freopen(source_file, "r", stdin) == NULL) {
440                 fprintf (stderr, "%s: Can't open %s\n", _nc_progname, source_file);
441                 return EXIT_FAILURE;
442         }
443
444         if (infodump)
445                 dump_init(tversion,
446                           smart_defaults
447                                 ? outform
448                                 : F_LITERAL,
449                           sortmode, width, debug_level);
450         else if (capdump)
451                 dump_init(tversion,
452                           outform,
453                           sortmode, width, debug_level);
454
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);
464
465         /* do use resolution */
466         if (check_only || (!infodump && !capdump) || forceresolve)
467             if (!_nc_resolve_uses() && !check_only)
468                 return EXIT_FAILURE;
469
470 #ifndef HAVE_BIG_CORE
471         /*
472          * Aaargh! immedhook seriously hoses us!
473          *
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.
480          */
481         if (namelst && (!infodump && !capdump))
482         {
483             (void) fprintf(stderr,
484                            "Sorry, -e can't be used without -I or -C\n");
485             return EXIT_FAILURE;
486         }
487 #endif /* HAVE_BIG_CORE */
488
489         /* length check */
490         if (check_only && (capdump || infodump))
491         {
492             for_entry_list(qp)
493             {
494                 if (matches(namelst, qp->tterm.term_names))
495                 {
496                     int len = fmt_entry(&qp->tterm, NULL, TRUE, infodump);
497
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),
502                            len);
503                 }
504             }
505         }
506
507         /* write or dump all entries */
508         if (!check_only)
509         {
510             if (!infodump && !capdump)
511             {
512                 _nc_set_writedir(outdir);
513                 for_entry_list(qp)
514                     if (matches(namelst, qp->tterm.term_names))
515                     {
516                         _nc_set_type(_nc_first_name(qp->tterm.term_names));
517                         _nc_curr_line = qp->startline;
518                         _nc_write_entry(&qp->tterm);
519                     }
520             }
521             else
522             {
523                 /* this is in case infotocap() generates warnings */
524                 _nc_curr_col = _nc_curr_line = -1;
525
526                 for_entry_list(qp)
527                     if (matches(namelst, qp->tterm.term_names))
528                     {
529                         int     j = qp->cend - qp->cstart;
530                         int     len = 0;
531
532                         /* this is in case infotocap() generates warnings */
533                         _nc_set_type(_nc_first_name(qp->tterm.term_names));
534
535                         (void) fseek(stdin, qp->cstart, SEEK_SET);
536                         while (j-- )
537                             if (infodump)
538                                 (void) putchar(getchar());
539                             else
540                                 put_translate(getchar());
541
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);
548                     }
549                 if (!namelst)
550                 {
551                     int  c, oldc = '\0';
552                     bool in_comment = FALSE;
553                     bool trailing_comment = FALSE;
554
555                     (void) fseek(stdin, _nc_tail->cend, SEEK_SET);
556                     while ((c = getchar()) != EOF)
557                     {
558                         if (oldc == '\n') {
559                             if (c == '#') {
560                                 trailing_comment = TRUE;
561                                 in_comment = TRUE;
562                             } else {
563                                 in_comment = FALSE;
564                             }
565                         }
566                         if (trailing_comment
567                          && (in_comment || (oldc == '\n' && c == '\n')))
568                             putchar(c);
569                         oldc = c;
570                     }
571                 }
572             }
573         }
574
575         /* Show the directory into which entries were written, and the total
576          * number of entries
577          */
578         if (showsummary
579          && (!(check_only || infodump || capdump))) {
580                 int total = _nc_tic_written();
581                 if (total != 0)
582                         fprintf(log_fp, "%d entries written to %s\n",
583                                 total,
584                                 _nc_tic_dir((char *)0));
585                 else
586                         fprintf(log_fp, "No entries written\n");
587         }
588         return(EXIT_SUCCESS);
589 }