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>
41 #include <dump_entry.h>
42 #include <term_entry.h>
44 MODULE_ID("$Id: toe.c,v 0.18 1998/02/11 12:14:02 tom Exp $")
46 const char *_nc_progname;
48 static int typelist(int eargc, char *eargv[], bool,
49 void (*)(const char *, TERMTYPE *));
50 static void deschook(const char *, TERMTYPE *);
54 static void ExitProgram(int code) GCC_NORETURN;
55 static void ExitProgram(int code)
57 _nc_free_entries(_nc_head);
58 _nc_leaks_dump_entry();
59 _nc_free_and_exit(code);
63 int main (int argc, char *argv[])
65 bool direct_dependencies = FALSE;
66 bool invert_dependencies = FALSE;
68 int i, c, debug_level = 0;
71 if ((_nc_progname = strrchr(argv[0], '/')) == NULL)
72 _nc_progname = argv[0];
76 while ((c = getopt(argc, argv, "huv:UV")) != EOF)
83 direct_dependencies = TRUE;
86 debug_level = atoi(optarg);
87 _nc_tracing = (1 << debug_level) - 1;
90 invert_dependencies = TRUE;
93 (void) fputs(NCURSES_VERSION, stdout);
95 ExitProgram(EXIT_SUCCESS);
97 (void) fprintf (stderr, "usage: toe [-huUV] [-v n] [file...]\n");
98 ExitProgram(EXIT_FAILURE);
101 if (direct_dependencies || invert_dependencies)
103 if (freopen(argv[optind], "r", stdin) == NULL)
105 (void) fflush(stdout);
106 fprintf(stderr, "%s: can't open %s\n", _nc_progname, argv[optind]);
107 ExitProgram(EXIT_FAILURE);
110 /* parse entries out of the source file */
111 _nc_set_source(argv[optind]);
112 _nc_read_entry_source(stdin, (char *)NULL,
117 /* maybe we want a direct-dependency listing? */
118 if (direct_dependencies)
127 (void) printf("%s:", _nc_first_name(qp->tterm.term_names));
128 for (j = 0; j < qp->nuses; j++)
129 (void) printf(" %s", (char *)(qp->uses[j].parent));
133 ExitProgram(EXIT_SUCCESS);
136 /* maybe we want a reverse-dependency listing? */
137 if (invert_dependencies)
150 for (i = 0; i < rp->nuses; i++)
151 if (_nc_name_match(qp->tterm.term_names,(char*)rp->uses[i].parent, "|"))
153 if (matchcount++ == 0)
155 _nc_first_name(qp->tterm.term_names));
157 _nc_first_name(rp->tterm.term_names));
164 ExitProgram(EXIT_SUCCESS);
168 * If we get this far, user wants a simple terminal type listing.
171 code = typelist(argc-optind, argv+optind, header, deschook);
173 char *by_env, *home, *eargv[3];
177 if ((by_env = getenv("TERMINFO")) != (char *)NULL)
181 if ((home = getenv("HOME")) != (char *)NULL)
183 char personal[PATH_MAX];
185 (void) sprintf(personal, PRIVATE_INFO, home);
186 if (access(personal, F_OK) == 0)
187 eargv[j++] = personal;
189 eargv[j++] = TERMINFO;
191 eargv[j] = (char *)NULL;
193 code = typelist(j, eargv, header, deschook);
199 static void deschook(const char *cn, TERMTYPE *tp)
200 /* display a description for the type */
204 if ((desc = strrchr(tp->term_names, '|')) == (char *)NULL)
205 desc = "(No description)";
209 (void) printf("%-10s\t%s\n", cn, desc);
212 static int typelist(int eargc, char *eargv[],
214 void (*hook)(const char *, TERMTYPE *tp))
215 /* apply a function to each entry in given terminfo directories */
219 for (i = 0; i < eargc; i++)
222 struct dirent *subdir;
224 if ((termdir = opendir(eargv[i])) == (DIR *)NULL)
226 (void) fflush(stdout);
227 (void) fprintf(stderr,
228 "%s: can't open terminfo directory %s\n",
229 _nc_progname, eargv[i]);
230 return(EXIT_FAILURE);
233 (void) printf("#\n#%s:\n#\n", eargv[i]);
235 while ((subdir = readdir(termdir)) != NULL)
237 size_t len = NAMLEN(subdir);
239 char name_1[PATH_MAX];
241 struct dirent *entry;
243 strncpy(name_1, subdir->d_name, len)[len] = '\0';
244 if (!strcmp(name_1, ".")
245 || !strcmp(name_1, ".."))
248 (void) strcpy(buf, eargv[i]);
249 (void) strcat(buf, "/");
250 (void) strcat(buf, name_1);
251 (void) strcat(buf, "/");
253 entrydir = opendir(".");
254 while ((entry = readdir(entrydir)) != NULL)
256 char name_2[PATH_MAX];
262 strncpy(name_2, entry->d_name, len)[len] = '\0';
263 if (!strcmp(name_2, ".")
264 || !strcmp(name_2, ".."))
267 status = _nc_read_file_entry(name_2, <erm);
270 (void) fflush(stdout);
271 (void) fprintf(stderr,
272 "toe: couldn't open terminfo file %s.\n",
274 return(EXIT_FAILURE);
277 /* only visit things once, by primary name */
278 cn = _nc_first_name(lterm.term_names);
279 if (!strcmp(cn, name_2))
281 /* apply the selected hook function */
284 if (lterm.term_names) {
285 free(lterm.term_names);
286 lterm.term_names = NULL;
288 if (lterm.str_table) {
289 free(lterm.str_table);
290 lterm.str_table = NULL;
298 return(EXIT_SUCCESS);