1 /****************************************************************************
2 * Copyright (c) 1998 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 ****************************************************************************/
35 * toe.c --- table of entries report generator
39 #include <progs.priv.h>
43 #include <dump_entry.h>
44 #include <term_entry.h>
46 MODULE_ID("$Id: toe.c,v 0.19 1998/03/08 01:02:46 tom Exp $")
48 const char *_nc_progname;
50 static int typelist(int eargc, char *eargv[], bool,
51 void (*)(const char *, TERMTYPE *));
52 static void deschook(const char *, TERMTYPE *);
56 static void ExitProgram(int code) GCC_NORETURN;
57 static void ExitProgram(int code)
59 _nc_free_entries(_nc_head);
60 _nc_leaks_dump_entry();
61 _nc_free_and_exit(code);
65 int main (int argc, char *argv[])
67 bool direct_dependencies = FALSE;
68 bool invert_dependencies = FALSE;
70 int i, c, debug_level = 0;
73 if ((_nc_progname = strrchr(argv[0], '/')) == NULL)
74 _nc_progname = argv[0];
78 while ((c = getopt(argc, argv, "huv:UV")) != EOF)
85 direct_dependencies = TRUE;
88 debug_level = atoi(optarg);
89 _nc_tracing = (1 << debug_level) - 1;
92 invert_dependencies = TRUE;
95 (void) fputs(NCURSES_VERSION, stdout);
97 ExitProgram(EXIT_SUCCESS);
99 (void) fprintf (stderr, "usage: toe [-huUV] [-v n] [file...]\n");
100 ExitProgram(EXIT_FAILURE);
103 if (direct_dependencies || invert_dependencies)
105 if (freopen(argv[optind], "r", stdin) == NULL)
107 (void) fflush(stdout);
108 fprintf(stderr, "%s: can't open %s\n", _nc_progname, argv[optind]);
109 ExitProgram(EXIT_FAILURE);
112 /* parse entries out of the source file */
113 _nc_set_source(argv[optind]);
114 _nc_read_entry_source(stdin, (char *)NULL,
119 /* maybe we want a direct-dependency listing? */
120 if (direct_dependencies)
129 (void) printf("%s:", _nc_first_name(qp->tterm.term_names));
130 for (j = 0; j < qp->nuses; j++)
131 (void) printf(" %s", (char *)(qp->uses[j].parent));
135 ExitProgram(EXIT_SUCCESS);
138 /* maybe we want a reverse-dependency listing? */
139 if (invert_dependencies)
152 for (i = 0; i < rp->nuses; i++)
153 if (_nc_name_match(qp->tterm.term_names,(char*)rp->uses[i].parent, "|"))
155 if (matchcount++ == 0)
157 _nc_first_name(qp->tterm.term_names));
159 _nc_first_name(rp->tterm.term_names));
166 ExitProgram(EXIT_SUCCESS);
170 * If we get this far, user wants a simple terminal type listing.
173 code = typelist(argc-optind, argv+optind, header, deschook);
175 char *by_env, *home, *eargv[3];
179 if ((by_env = getenv("TERMINFO")) != (char *)NULL)
183 if ((home = getenv("HOME")) != (char *)NULL)
185 char personal[PATH_MAX];
188 (void) sprintf(personal, PRIVATE_INFO, home);
189 if (stat(personal, &sb) == 0
190 && (sb.st_mode & S_IFMT) == S_IFDIR)
191 eargv[j++] = personal;
193 eargv[j++] = TERMINFO;
195 eargv[j] = (char *)NULL;
197 code = typelist(j, eargv, header, deschook);
203 static void deschook(const char *cn, TERMTYPE *tp)
204 /* display a description for the type */
208 if ((desc = strrchr(tp->term_names, '|')) == (char *)NULL)
209 desc = "(No description)";
213 (void) printf("%-10s\t%s\n", cn, desc);
216 static int typelist(int eargc, char *eargv[],
218 void (*hook)(const char *, TERMTYPE *tp))
219 /* apply a function to each entry in given terminfo directories */
223 for (i = 0; i < eargc; i++)
226 struct dirent *subdir;
228 if ((termdir = opendir(eargv[i])) == (DIR *)NULL)
230 (void) fflush(stdout);
231 (void) fprintf(stderr,
232 "%s: can't open terminfo directory %s\n",
233 _nc_progname, eargv[i]);
234 return(EXIT_FAILURE);
237 (void) printf("#\n#%s:\n#\n", eargv[i]);
239 while ((subdir = readdir(termdir)) != NULL)
241 size_t len = NAMLEN(subdir);
243 char name_1[PATH_MAX];
245 struct dirent *entry;
247 strncpy(name_1, subdir->d_name, len)[len] = '\0';
248 if (!strcmp(name_1, ".")
249 || !strcmp(name_1, ".."))
252 (void) strcpy(buf, eargv[i]);
253 (void) strcat(buf, "/");
254 (void) strcat(buf, name_1);
255 (void) strcat(buf, "/");
257 entrydir = opendir(".");
258 while ((entry = readdir(entrydir)) != NULL)
260 char name_2[PATH_MAX];
266 strncpy(name_2, entry->d_name, len)[len] = '\0';
267 if (!strcmp(name_2, ".")
268 || !strcmp(name_2, ".."))
271 status = _nc_read_file_entry(name_2, <erm);
274 (void) fflush(stdout);
275 (void) fprintf(stderr,
276 "toe: couldn't open terminfo file %s.\n",
278 return(EXIT_FAILURE);
281 /* only visit things once, by primary name */
282 cn = _nc_first_name(lterm.term_names);
283 if (!strcmp(cn, name_2))
285 /* apply the selected hook function */
288 if (lterm.term_names) {
289 free(lterm.term_names);
290 lterm.term_names = NULL;
292 if (lterm.str_table) {
293 free(lterm.str_table);
294 lterm.str_table = NULL;
302 return(EXIT_SUCCESS);