]> ncurses.scripts.mit.edu Git - ncurses.git/blob - progs/toe.c
ncurses 5.2
[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.24 2000/09/09 19:52: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     _nc_progname = _nc_basename(argv[0]);
91
92     while ((c = getopt(argc, argv, "huv:UV")) != EOF)
93         switch (c) {
94         case 'h':
95             header = TRUE;
96             break;
97         case 'u':
98             direct_dependencies = TRUE;
99             break;
100         case 'v':
101             set_trace_level(atoi(optarg));
102             break;
103         case 'U':
104             invert_dependencies = TRUE;
105             break;
106         case 'V':
107             puts(curses_version());
108             ExitProgram(EXIT_SUCCESS);
109         default:
110             (void) fprintf(stderr, "usage: toe [-huUV] [-v n] [file...]\n");
111             ExitProgram(EXIT_FAILURE);
112         }
113
114     if (direct_dependencies || invert_dependencies) {
115         if (freopen(argv[optind], "r", stdin) == 0) {
116             (void) fflush(stdout);
117             fprintf(stderr, "%s: can't open %s\n", _nc_progname, argv[optind]);
118             ExitProgram(EXIT_FAILURE);
119         }
120
121         /* parse entries out of the source file */
122         _nc_set_source(argv[optind]);
123         _nc_read_entry_source(stdin, 0, FALSE, FALSE, NULLHOOK);
124     }
125
126     /* maybe we want a direct-dependency listing? */
127     if (direct_dependencies) {
128         ENTRY *qp;
129
130         for_entry_list(qp)
131             if (qp->nuses) {
132             int j;
133
134             (void) printf("%s:", _nc_first_name(qp->tterm.term_names));
135             for (j = 0; j < qp->nuses; j++)
136                 (void) printf(" %s", qp->uses[j].name);
137             putchar('\n');
138         }
139
140         ExitProgram(EXIT_SUCCESS);
141     }
142
143     /* maybe we want a reverse-dependency listing? */
144     if (invert_dependencies) {
145         ENTRY *qp, *rp;
146         int matchcount;
147
148         for_entry_list(qp) {
149             matchcount = 0;
150             for_entry_list(rp) {
151                 if (rp->nuses == 0)
152                     continue;
153
154                 for (i = 0; i < rp->nuses; i++)
155                     if (_nc_name_match(qp->tterm.term_names,
156                             rp->uses[i].name, "|")) {
157                         if (matchcount++ == 0)
158                             (void) printf("%s:",
159                                 _nc_first_name(qp->tterm.term_names));
160                         (void) printf(" %s",
161                             _nc_first_name(rp->tterm.term_names));
162                     }
163             }
164             if (matchcount)
165                 putchar('\n');
166         }
167
168         ExitProgram(EXIT_SUCCESS);
169     }
170
171     /*
172      * If we get this far, user wants a simple terminal type listing.
173      */
174     if (optind < argc) {
175         code = typelist(argc - optind, argv + optind, header, deschook);
176     } else {
177         char *home, *eargv[3];
178         char personal[PATH_MAX];
179         int j;
180
181         j = 0;
182         if ((eargv[j] = get_directory(getenv("TERMINFO"))) != 0) {
183             j++;
184         } else {
185             if ((home = getenv("HOME")) != 0) {
186                 (void) sprintf(personal, PRIVATE_INFO, home);
187                 if ((eargv[j] = get_directory(personal)) != 0)
188                     j++;
189             }
190             if ((eargv[j] = get_directory(strcpy(personal, TERMINFO))) != 0)
191                 j++;
192         }
193         eargv[j] = 0;
194
195         code = typelist(j, eargv, header, deschook);
196     }
197
198     ExitProgram(code);
199 }
200
201 static void
202 deschook(const char *cn, TERMTYPE * tp)
203 /* display a description for the type */
204 {
205     const char *desc;
206
207     if ((desc = strrchr(tp->term_names, '|')) == 0)
208         desc = "(No description)";
209     else
210         ++desc;
211
212     (void) printf("%-10s\t%s\n", cn, desc);
213 }
214
215 static int
216 typelist(int eargc, char *eargv[],
217     bool verbosity,
218     void (*hook) (const char *, TERMTYPE * tp))
219 /* apply a function to each entry in given terminfo directories */
220 {
221     int i;
222
223     for (i = 0; i < eargc; i++) {
224         DIR *termdir;
225         struct dirent *subdir;
226
227         if ((termdir = opendir(eargv[i])) == 0) {
228             (void) fflush(stdout);
229             (void) fprintf(stderr,
230                 "%s: can't open terminfo directory %s\n",
231                 _nc_progname, eargv[i]);
232             return (EXIT_FAILURE);
233         } else if (verbosity)
234             (void) printf("#\n#%s:\n#\n", eargv[i]);
235
236         while ((subdir = readdir(termdir)) != 0) {
237             size_t len = NAMLEN(subdir);
238             char buf[PATH_MAX];
239             char name_1[PATH_MAX];
240             DIR *entrydir;
241             struct dirent *entry;
242
243             strncpy(name_1, subdir->d_name, len)[len] = '\0';
244             if (isDotname(name_1))
245                 continue;
246
247             (void) sprintf(buf, "%s/%s/", eargv[i], name_1);
248             chdir(buf);
249             entrydir = opendir(".");
250             while ((entry = readdir(entrydir)) != 0) {
251                 char name_2[PATH_MAX];
252                 TERMTYPE lterm;
253                 char *cn;
254                 int status;
255
256                 len = NAMLEN(entry);
257                 strncpy(name_2, entry->d_name, len)[len] = '\0';
258                 if (isDotname(name_2))
259                     continue;
260
261                 status = _nc_read_file_entry(name_2, &lterm);
262                 if (status <= 0) {
263                     (void) fflush(stdout);
264                     (void) fprintf(stderr,
265                         "toe: couldn't open terminfo file %s.\n",
266                         name_2);
267                     return (EXIT_FAILURE);
268                 }
269
270                 /* only visit things once, by primary name */
271                 cn = _nc_first_name(lterm.term_names);
272                 if (!strcmp(cn, name_2)) {
273                     /* apply the selected hook function */
274                     (*hook) (cn, &lterm);
275                 }
276                 if (lterm.term_names) {
277                     free(lterm.term_names);
278                     lterm.term_names = 0;
279                 }
280                 if (lterm.str_table) {
281                     free(lterm.str_table);
282                     lterm.str_table = 0;
283                 }
284             }
285             closedir(entrydir);
286         }
287         closedir(termdir);
288     }
289
290     return (EXIT_SUCCESS);
291 }