]> ncurses.scripts.mit.edu Git - ncurses.git/blob - progs/toe.c
ncurses 5.6 - patch 20070127
[ncurses.git] / progs / toe.c
1 /****************************************************************************
2  * Copyright (c) 1998-2006,2007 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  *     and: Thomas E. Dickey                        1996-on                 *
33  ****************************************************************************/
34
35 /*
36  *      toe.c --- table of entries report generator
37  */
38
39 #include <progs.priv.h>
40
41 #include <sys/stat.h>
42
43 #include <dump_entry.h>
44
45 #if USE_HASHED_DB
46 #include <hashed_db.h>
47 #endif
48
49 MODULE_ID("$Id: toe.c,v 1.42 2007/01/21 01:09:07 tom Exp $")
50
51 #define isDotname(name) (!strcmp(name, ".") || !strcmp(name, ".."))
52
53 const char *_nc_progname;
54
55 #if NO_LEAKS
56 #undef ExitProgram
57 static void ExitProgram(int code) GCC_NORETURN;
58 static void
59 ExitProgram(int code)
60 {
61     _nc_free_entries(_nc_head);
62     _nc_leaks_dump_entry();
63     _nc_leaks_tic();
64     _nc_free_and_exit(code);
65 }
66 #endif
67
68 #if USE_HASHED_DB
69 static bool
70 make_db_name(char *dst, const char *src, unsigned limit)
71 {
72     static const char suffix[] = DBM_SUFFIX;
73
74     bool result = FALSE;
75     unsigned lens = sizeof(suffix) - 1;
76     unsigned size = strlen(src);
77     unsigned need = lens + size;
78
79     if (need <= limit) {
80         if (size >= lens
81             && !strcmp(src + size - lens, suffix))
82             (void) strcpy(dst, src);
83         else
84             (void) sprintf(dst, "%s%s", src, suffix);
85         result = TRUE;
86     }
87     return result;
88 }
89 #endif
90
91 static bool
92 is_database(const char *path)
93 {
94     bool result = FALSE;
95 #if USE_DATABASE
96     if (_nc_is_dir_path(path) && access(path, R_OK | X_OK) == 0) {
97         result = TRUE;
98     }
99 #endif
100 #if USE_TERMCAP
101     if (_nc_is_file_path(path) && access(path, R_OK) == 0) {
102         result = TRUE;
103     }
104 #endif
105 #if USE_HASHED_DB
106     if (!result) {
107         char filename[PATH_MAX];
108         if (_nc_is_file_path(path) && access(path, R_OK) == 0) {
109             result = TRUE;
110         } else if (make_db_name(filename, path, sizeof(filename))) {
111             if (_nc_is_file_path(filename) && access(filename, R_OK) == 0) {
112                 result = TRUE;
113             }
114         }
115     }
116 #endif
117     return result;
118 }
119
120 static void
121 deschook(const char *cn, TERMTYPE *tp)
122 /* display a description for the type */
123 {
124     const char *desc;
125
126     if ((desc = strrchr(tp->term_names, '|')) == 0 || *++desc == '\0')
127         desc = "(No description)";
128
129     (void) printf("%-10s\t%s\n", cn, desc);
130 }
131
132 #if USE_TERMCAP
133 static void
134 show_termcap(char *buffer,
135              void (*hook) (const char *, TERMTYPE *tp))
136 {
137     TERMTYPE data;
138     char *next = strchr(buffer, ':');
139     char *last;
140     char *list = buffer;
141
142     if (next)
143         *next = '\0';
144
145     last = strrchr(buffer, '|');
146     if (last)
147         ++last;
148
149     data.term_names = strdup(buffer);
150     while ((next = strtok(list, "|")) != 0) {
151         if (next != last)
152             hook(next, &data);
153         list = 0;
154     }
155     free(data.term_names);
156 }
157 #endif
158
159 static int
160 typelist(int eargc, char *eargv[],
161          bool verbosity,
162          void (*hook) (const char *, TERMTYPE *tp))
163 /* apply a function to each entry in given terminfo directories */
164 {
165     int i;
166
167     for (i = 0; i < eargc; i++) {
168 #if USE_DATABASE
169         if (_nc_is_dir_path(eargv[i])) {
170             DIR *termdir;
171             DIRENT *subdir;
172
173             if ((termdir = opendir(eargv[i])) == 0) {
174                 (void) fflush(stdout);
175                 (void) fprintf(stderr,
176                                "%s: can't open terminfo directory %s\n",
177                                _nc_progname, eargv[i]);
178                 return (EXIT_FAILURE);
179             } else if (verbosity)
180                 (void) printf("#\n#%s:\n#\n", eargv[i]);
181
182             while ((subdir = readdir(termdir)) != 0) {
183                 size_t len = NAMLEN(subdir);
184                 char buf[PATH_MAX];
185                 char name_1[PATH_MAX];
186                 DIR *entrydir;
187                 DIRENT *entry;
188
189                 strncpy(name_1, subdir->d_name, len)[len] = '\0';
190                 if (isDotname(name_1))
191                     continue;
192
193                 (void) sprintf(buf, "%s/%s/", eargv[i], name_1);
194                 if (chdir(buf) != 0)
195                     continue;
196
197                 entrydir = opendir(".");
198                 while ((entry = readdir(entrydir)) != 0) {
199                     char name_2[PATH_MAX];
200                     TERMTYPE lterm;
201                     char *cn;
202                     int status;
203
204                     len = NAMLEN(entry);
205                     strncpy(name_2, entry->d_name, len)[len] = '\0';
206                     if (isDotname(name_2) || !_nc_is_file_path(name_2))
207                         continue;
208
209                     status = _nc_read_file_entry(name_2, &lterm);
210                     if (status <= 0) {
211                         (void) fflush(stdout);
212                         (void) fprintf(stderr,
213                                        "%s: couldn't open terminfo file %s.\n",
214                                        _nc_progname, name_2);
215                         return (EXIT_FAILURE);
216                     }
217
218                     /* only visit things once, by primary name */
219                     cn = _nc_first_name(lterm.term_names);
220                     if (!strcmp(cn, name_2)) {
221                         /* apply the selected hook function */
222                         (*hook) (cn, &lterm);
223                     }
224                     _nc_free_termtype(&lterm);
225                 }
226                 closedir(entrydir);
227             }
228             closedir(termdir);
229         }
230 #if USE_HASHED_DB
231         else {
232             DB *capdbp;
233             char filename[PATH_MAX];
234
235             if (make_db_name(filename, eargv[i], sizeof(filename))) {
236                 if ((capdbp = _nc_db_open(filename, FALSE)) != 0) {
237                     DBT key, data;
238                     int code;
239
240                     code = _nc_db_first(capdbp, &key, &data);
241                     while (code == 0) {
242                         TERMTYPE lterm;
243                         int used;
244                         char *have;
245                         char *cn;
246
247                         if (_nc_db_have_data(&key, &data, &have, &used)) {
248                             if (_nc_read_termtype(&lterm, have, used) > 0) {
249                                 /* only visit things once, by primary name */
250                                 cn = _nc_first_name(lterm.term_names);
251                                 /* apply the selected hook function */
252                                 (*hook) (cn, &lterm);
253                                 _nc_free_termtype(&lterm);
254                             }
255                         }
256                         code = _nc_db_next(capdbp, &key, &data);
257                     }
258
259                     _nc_db_close(capdbp);
260                 }
261             }
262         }
263 #endif
264 #endif
265 #if USE_TERMCAP
266 #if HAVE_BSD_CGETENT
267         char *db_array[2];
268         char *buffer = 0;
269
270         if (verbosity)
271             (void) printf("#\n#%s:\n#\n", eargv[i]);
272
273         db_array[0] = eargv[i];
274         db_array[1] = 0;
275
276         if (cgetfirst(&buffer, db_array)) {
277             show_termcap(buffer, hook);
278             free(buffer);
279             while (cgetnext(&buffer, db_array)) {
280                 show_termcap(buffer, hook);
281                 free(buffer);
282             }
283         }
284         cgetclose();
285 #else
286         /* scan termcap text-file only */
287         if (_nc_is_file_path(eargv[i])) {
288             char buffer[2048];
289             FILE *fp;
290
291             if ((fp = fopen(eargv[i], "r")) != 0) {
292                 while (fgets(buffer, sizeof(buffer), fp) != 0) {
293                     if (*buffer == '#')
294                         continue;
295                     if (isspace(*buffer))
296                         continue;
297                     show_termcap(buffer, hook);
298                 }
299                 fclose(fp);
300             }
301         }
302 #endif
303 #endif
304     }
305
306     return (EXIT_SUCCESS);
307 }
308
309 static void
310 usage(void)
311 {
312     (void) fprintf(stderr, "usage: %s [-ahuUV] [-v n] [file...]\n", _nc_progname);
313     ExitProgram(EXIT_FAILURE);
314 }
315
316 int
317 main(int argc, char *argv[])
318 {
319     bool all_dirs = FALSE;
320     bool direct_dependencies = FALSE;
321     bool invert_dependencies = FALSE;
322     bool header = FALSE;
323     int i;
324     int code;
325     int this_opt, last_opt = '?';
326     int v_opt = 0;
327
328     _nc_progname = _nc_rootname(argv[0]);
329
330     while ((this_opt = getopt(argc, argv, "0123456789ahuvUV")) != EOF) {
331         /* handle optional parameter */
332         if (isdigit(this_opt)) {
333             switch (last_opt) {
334             case 'v':
335                 v_opt = (this_opt - '0');
336                 break;
337             default:
338                 if (isdigit(last_opt))
339                     v_opt *= 10;
340                 else
341                     v_opt = 0;
342                 v_opt += (this_opt - '0');
343                 last_opt = this_opt;
344             }
345             continue;
346         }
347         switch (this_opt) {
348         case 'a':
349             all_dirs = TRUE;
350             break;
351         case 'h':
352             header = TRUE;
353             break;
354         case 'u':
355             direct_dependencies = TRUE;
356             break;
357         case 'v':
358             v_opt = 1;
359             break;
360         case 'U':
361             invert_dependencies = TRUE;
362             break;
363         case 'V':
364             puts(curses_version());
365             ExitProgram(EXIT_SUCCESS);
366         default:
367             usage();
368         }
369     }
370     set_trace_level(v_opt);
371
372     if (direct_dependencies || invert_dependencies) {
373         if (freopen(argv[optind], "r", stdin) == 0) {
374             (void) fflush(stdout);
375             fprintf(stderr, "%s: can't open %s\n", _nc_progname, argv[optind]);
376             ExitProgram(EXIT_FAILURE);
377         }
378
379         /* parse entries out of the source file */
380         _nc_set_source(argv[optind]);
381         _nc_read_entry_source(stdin, 0, FALSE, FALSE, NULLHOOK);
382     }
383
384     /* maybe we want a direct-dependency listing? */
385     if (direct_dependencies) {
386         ENTRY *qp;
387
388         for_entry_list(qp) {
389             if (qp->nuses) {
390                 int j;
391
392                 (void) printf("%s:", _nc_first_name(qp->tterm.term_names));
393                 for (j = 0; j < qp->nuses; j++)
394                     (void) printf(" %s", qp->uses[j].name);
395                 putchar('\n');
396             }
397         }
398
399         ExitProgram(EXIT_SUCCESS);
400     }
401
402     /* maybe we want a reverse-dependency listing? */
403     if (invert_dependencies) {
404         ENTRY *qp, *rp;
405         int matchcount;
406
407         for_entry_list(qp) {
408             matchcount = 0;
409             for_entry_list(rp) {
410                 if (rp->nuses == 0)
411                     continue;
412
413                 for (i = 0; i < rp->nuses; i++)
414                     if (_nc_name_match(qp->tterm.term_names,
415                                        rp->uses[i].name, "|")) {
416                         if (matchcount++ == 0)
417                             (void) printf("%s:",
418                                           _nc_first_name(qp->tterm.term_names));
419                         (void) printf(" %s",
420                                       _nc_first_name(rp->tterm.term_names));
421                     }
422             }
423             if (matchcount)
424                 putchar('\n');
425         }
426
427         ExitProgram(EXIT_SUCCESS);
428     }
429
430     /*
431      * If we get this far, user wants a simple terminal type listing.
432      */
433     if (optind < argc) {
434         code = typelist(argc - optind, argv + optind, header, deschook);
435     } else if (all_dirs) {
436         DBDIRS state;
437         int offset;
438         int pass;
439         const char *path;
440         char **eargv = 0;
441
442         code = EXIT_FAILURE;
443         for (pass = 0; pass < 2; ++pass) {
444             unsigned count = 0;
445
446             _nc_first_db(&state, &offset);
447             while ((path = _nc_next_db(&state, &offset)) != 0) {
448                 if (!is_database(path)) {
449                     ;
450                 } else if (eargv != 0) {
451                     unsigned n;
452                     int found = FALSE;
453
454                     /* eliminate duplicates */
455                     for (n = 0; n < count; ++n) {
456                         if (!strcmp(path, eargv[n])) {
457                             found = TRUE;
458                             break;
459                         }
460                     }
461                     if (!found) {
462                         eargv[count] = strdup(path);
463                         ++count;
464                     }
465                 } else {
466                     ++count;
467                 }
468             }
469             if (!pass) {
470                 eargv = typeCalloc(char *, count + 1);
471             } else {
472                 code = typelist((int) count, eargv, header, deschook);
473                 while (count-- > 0)
474                     free(eargv[count]);
475                 free(eargv);
476             }
477         }
478     } else {
479         DBDIRS state;
480         int offset;
481         const char *path;
482         char *eargv[3];
483         int count = 0;
484
485         _nc_first_db(&state, &offset);
486         while ((path = _nc_next_db(&state, &offset)) != 0) {
487             if (is_database(path)) {
488                 eargv[count++] = strdup(path);
489                 break;
490             }
491         }
492         eargv[count] = 0;
493
494         code = typelist(count, eargv, header, deschook);
495
496         while (count-- > 0)
497             free(eargv[count]);
498     }
499     _nc_last_db();
500
501     ExitProgram(code);
502 }