ncurses 4.1
[ncurses.git] / progs / toe.c
1
2 /***************************************************************************
3 *                            COPYRIGHT NOTICE                              *
4 ****************************************************************************
5 *                ncurses is copyright (C) 1992-1995                        *
6 *                          Zeyd M. Ben-Halim                               *
7 *                          zmbenhal@netcom.com                             *
8 *                          Eric S. Raymond                                 *
9 *                          esr@snark.thyrsus.com                           *
10 *                                                                          *
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.                *
17 *                                                                          *
18 *        ncurses comes AS IS with no warranty, implied or expressed.       *
19 *                                                                          *
20 ***************************************************************************/
21
22 /*
23  *      toe.c --- table of entries report generator
24  *
25  */
26
27 #include <progs.priv.h>
28
29 #include <dump_entry.h>
30 #include <term_entry.h>
31
32 MODULE_ID("$Id: toe.c,v 0.15 1997/02/15 18:54:47 tom Exp $")
33
34 const char *_nc_progname;
35
36 static int typelist(int eargc, char *eargv[], bool,
37                      void (*)(const char *, TERMTYPE *));
38 static void deschook(const char *, TERMTYPE *);
39
40 #if NO_LEAKS
41 #undef ExitProgram
42 static void ExitProgram(int code) GCC_NORETURN;
43 static void ExitProgram(int code)
44 {
45         _nc_free_entries(_nc_head);
46         _nc_leaks_dump_entry();
47         _nc_free_and_exit(code);
48 }
49 #endif
50
51 int main (int argc, char *argv[])
52 {
53     bool        direct_dependencies = FALSE;
54     bool        invert_dependencies = FALSE;
55     bool        header = FALSE;
56     int         i, c, debug_level = 0;
57     int         code;
58
59     if ((_nc_progname = strrchr(argv[0], '/')) == NULL)
60         _nc_progname = argv[0];
61     else
62         _nc_progname++;
63
64     while ((c = getopt(argc, argv, "huv:UV")) != EOF)
65         switch (c)
66         {
67         case 'h':
68             header = TRUE;
69             break;
70         case 'u':
71             direct_dependencies = TRUE;
72             break;
73         case 'v':
74             debug_level = atoi(optarg);
75             _nc_tracing = (1 << debug_level) - 1;
76             break;
77         case 'U':
78             invert_dependencies = TRUE;
79             break;
80         case 'V':
81             (void) fputs(NCURSES_VERSION, stdout);
82             putchar('\n');
83             ExitProgram(EXIT_SUCCESS);
84         default:
85             (void) fprintf (stderr, "usage: toe [-huUV] [-v n] [file...]\n");
86             ExitProgram(EXIT_FAILURE);
87         }
88
89     if (direct_dependencies || invert_dependencies)
90     {
91         if (freopen(argv[optind], "r", stdin) == NULL)
92         {
93             fprintf(stderr, "%s: can't open %s\n", _nc_progname, argv[optind]);
94             ExitProgram(EXIT_FAILURE);
95         }
96
97         /* parse entries out of the source file */
98         _nc_set_source(argv[optind]);
99         _nc_read_entry_source(stdin, (char *)NULL,
100                               FALSE, FALSE,
101                               NULLHOOK);
102     }
103
104     /* maybe we want a direct-dependency listing? */
105     if (direct_dependencies)
106     {
107         ENTRY   *qp;
108
109         for_entry_list(qp)
110             if (qp->nuses)
111             {
112                 int             j;
113
114                 (void) printf("%s:", _nc_first_name(qp->tterm.term_names));
115                 for (j = 0; j < qp->nuses; j++)
116                     (void) printf(" %s", (char *)(qp->uses[j].parent));
117                 putchar('\n');
118             }
119
120         ExitProgram(EXIT_SUCCESS);
121     }
122
123     /* maybe we want a reverse-dependency listing? */
124     if (invert_dependencies)
125     {
126         ENTRY   *qp, *rp;
127         int             matchcount;
128
129         for_entry_list(qp)
130         {
131             matchcount = 0;
132             for_entry_list(rp)
133             {
134                 if (rp->nuses == 0)
135                     continue;
136
137                 for (i = 0; i < rp->nuses; i++)
138                     if (_nc_name_match(qp->tterm.term_names,(char*)rp->uses[i].parent, "|"))
139                     {
140                         if (matchcount++ == 0)
141                             (void) printf("%s:",
142                                           _nc_first_name(qp->tterm.term_names));
143                         (void) printf(" %s",
144                                       _nc_first_name(rp->tterm.term_names));
145                     }
146             }
147             if (matchcount)
148                 putchar('\n');
149         }
150
151         ExitProgram(EXIT_SUCCESS);
152     }
153
154     /*
155      * If we get this far, user wants a simple terminal type listing.
156      */
157     if (optind < argc) {
158         code = typelist(argc-optind, argv+optind, header, deschook);
159     } else {
160         char    *explicit, *home, *eargv[3];
161         int     j;
162
163         j = 0;
164         if ((explicit = getenv("TERMINFO")) != (char *)NULL)
165             eargv[j++] = explicit;
166         else
167         {
168             if ((home = getenv("HOME")) != (char *)NULL)
169             {
170                 char    personal[PATH_MAX];
171
172                 (void) sprintf(personal, PRIVATE_INFO, home);
173                 if (access(personal, F_OK) == 0)
174                     eargv[j++] = personal;
175             }
176             eargv[j++] = TERMINFO;
177         }
178         eargv[j] = (char *)NULL;
179
180         code = typelist(j, eargv, header, deschook);
181     }
182
183     ExitProgram(code);
184 }
185
186 static void deschook(const char *cn, TERMTYPE *tp)
187 /* display a description for the type */
188 {
189     const char *desc;
190
191     if ((desc = strrchr(tp->term_names, '|')) == (char *)NULL)
192         desc = "(No description)";
193     else
194         ++desc;
195
196     (void) printf("%-10s\t%s\n", cn, desc);
197 }
198
199 static int typelist(int eargc, char *eargv[],
200                      bool verbosity,
201                      void  (*hook)(const char *, TERMTYPE *tp))
202 /* apply a function to each entry in given terminfo directories */
203 {
204     int i;
205
206     for (i = 0; i < eargc; i++)
207     {
208         DIR     *termdir;
209         struct dirent *subdir;
210
211         if ((termdir = opendir(eargv[i])) == (DIR *)NULL)
212         {
213             (void) fprintf(stderr,
214                            "%s: can't open terminfo directory %s\n",
215                            _nc_progname, eargv[i]);
216             return(EXIT_FAILURE);
217         }
218         else if (verbosity)
219             (void) printf("#\n#%s:\n#\n", eargv[i]);
220
221         while ((subdir = readdir(termdir)) != NULL)
222         {
223             size_t      len = NAMLEN(subdir);
224             char        buf[PATH_MAX];
225             char        name_1[PATH_MAX];
226             DIR *entrydir;
227             struct dirent *entry;
228
229             strncpy(name_1, subdir->d_name, len)[len] = '\0';
230             if (!strcmp(name_1, ".")
231                 || !strcmp(name_1, ".."))
232                 continue;
233
234             (void) strcpy(buf, eargv[i]);
235             (void) strcat(buf, "/");
236             (void) strcat(buf, name_1);
237             (void) strcat(buf, "/");
238             chdir(buf);
239             entrydir = opendir(".");
240             while ((entry = readdir(entrydir)) != NULL)
241             {
242                 char            name_2[PATH_MAX];
243                 TERMTYPE        lterm;
244                 char            *cn;
245                 int             status;
246
247                 len = NAMLEN(entry);
248                 strncpy(name_2, entry->d_name, len)[len] = '\0';
249                 if (!strcmp(name_2, ".")
250                     || !strcmp(name_2, ".."))
251                     continue;
252
253                 status = _nc_read_file_entry(name_2, &lterm);
254                 if (status <= 0)
255                 {
256                     (void) fprintf(stderr,
257                                    "toe: couldn't open terminfo file %s.\n",
258                                    name_2);
259                     return(EXIT_FAILURE);
260                 }
261
262                 /* only visit things once, by primary name */
263                 cn = _nc_first_name(lterm.term_names);
264                 if (!strcmp(cn, name_2))
265                 {
266                     /* apply the selected hook function */
267                     (*hook)(cn, &lterm);
268                 }
269                 if (lterm.term_names) {
270                     free(lterm.term_names);
271                     lterm.term_names = NULL;
272                 }
273                 if (lterm.str_table) {
274                     free(lterm.str_table);
275                     lterm.str_table = NULL;
276                 }
277             }
278             closedir(entrydir);
279         }
280         closedir(termdir);
281     }
282
283     return(EXIT_SUCCESS);
284 }