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