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