2 /***************************************************************************
4 ****************************************************************************
5 * ncurses is copyright (C) 1992-1995 *
7 * zmbenhal@netcom.com *
9 * esr@snark.thyrsus.com *
11 * Permission is hereby granted to reproduce and distribute ncurses *
12 * by any means and for any fee, whether alone or as part of a *
13 * larger distribution, in source or in binary form, PROVIDED *
14 * this notice is included with any such distribution, and is not *
15 * removed from any of its header files. Mention of ncurses in any *
16 * applications linked with it is highly appreciated. *
18 * ncurses comes AS IS with no warranty, implied or expressed. *
20 ***************************************************************************/
23 * toe.c --- table of entries report generator
27 #include <progs.priv.h>
29 #include <dump_entry.h>
30 #include <term_entry.h>
32 MODULE_ID("$Id: toe.c,v 0.15 1997/02/15 18:54:47 tom Exp $")
34 const char *_nc_progname;
36 static int typelist(int eargc, char *eargv[], bool,
37 void (*)(const char *, TERMTYPE *));
38 static void deschook(const char *, TERMTYPE *);
42 static void ExitProgram(int code) GCC_NORETURN;
43 static void ExitProgram(int code)
45 _nc_free_entries(_nc_head);
46 _nc_leaks_dump_entry();
47 _nc_free_and_exit(code);
51 int main (int argc, char *argv[])
53 bool direct_dependencies = FALSE;
54 bool invert_dependencies = FALSE;
56 int i, c, debug_level = 0;
59 if ((_nc_progname = strrchr(argv[0], '/')) == NULL)
60 _nc_progname = argv[0];
64 while ((c = getopt(argc, argv, "huv:UV")) != EOF)
71 direct_dependencies = TRUE;
74 debug_level = atoi(optarg);
75 _nc_tracing = (1 << debug_level) - 1;
78 invert_dependencies = TRUE;
81 (void) fputs(NCURSES_VERSION, stdout);
83 ExitProgram(EXIT_SUCCESS);
85 (void) fprintf (stderr, "usage: toe [-huUV] [-v n] [file...]\n");
86 ExitProgram(EXIT_FAILURE);
89 if (direct_dependencies || invert_dependencies)
91 if (freopen(argv[optind], "r", stdin) == NULL)
93 fprintf(stderr, "%s: can't open %s\n", _nc_progname, argv[optind]);
94 ExitProgram(EXIT_FAILURE);
97 /* parse entries out of the source file */
98 _nc_set_source(argv[optind]);
99 _nc_read_entry_source(stdin, (char *)NULL,
104 /* maybe we want a direct-dependency listing? */
105 if (direct_dependencies)
114 (void) printf("%s:", _nc_first_name(qp->tterm.term_names));
115 for (j = 0; j < qp->nuses; j++)
116 (void) printf(" %s", (char *)(qp->uses[j].parent));
120 ExitProgram(EXIT_SUCCESS);
123 /* maybe we want a reverse-dependency listing? */
124 if (invert_dependencies)
137 for (i = 0; i < rp->nuses; i++)
138 if (_nc_name_match(qp->tterm.term_names,(char*)rp->uses[i].parent, "|"))
140 if (matchcount++ == 0)
142 _nc_first_name(qp->tterm.term_names));
144 _nc_first_name(rp->tterm.term_names));
151 ExitProgram(EXIT_SUCCESS);
155 * If we get this far, user wants a simple terminal type listing.
158 code = typelist(argc-optind, argv+optind, header, deschook);
160 char *explicit, *home, *eargv[3];
164 if ((explicit = getenv("TERMINFO")) != (char *)NULL)
165 eargv[j++] = explicit;
168 if ((home = getenv("HOME")) != (char *)NULL)
170 char personal[PATH_MAX];
172 (void) sprintf(personal, PRIVATE_INFO, home);
173 if (access(personal, F_OK) == 0)
174 eargv[j++] = personal;
176 eargv[j++] = TERMINFO;
178 eargv[j] = (char *)NULL;
180 code = typelist(j, eargv, header, deschook);
186 static void deschook(const char *cn, TERMTYPE *tp)
187 /* display a description for the type */
191 if ((desc = strrchr(tp->term_names, '|')) == (char *)NULL)
192 desc = "(No description)";
196 (void) printf("%-10s\t%s\n", cn, desc);
199 static int typelist(int eargc, char *eargv[],
201 void (*hook)(const char *, TERMTYPE *tp))
202 /* apply a function to each entry in given terminfo directories */
206 for (i = 0; i < eargc; i++)
209 struct dirent *subdir;
211 if ((termdir = opendir(eargv[i])) == (DIR *)NULL)
213 (void) fprintf(stderr,
214 "%s: can't open terminfo directory %s\n",
215 _nc_progname, eargv[i]);
216 return(EXIT_FAILURE);
219 (void) printf("#\n#%s:\n#\n", eargv[i]);
221 while ((subdir = readdir(termdir)) != NULL)
223 size_t len = NAMLEN(subdir);
225 char name_1[PATH_MAX];
227 struct dirent *entry;
229 strncpy(name_1, subdir->d_name, len)[len] = '\0';
230 if (!strcmp(name_1, ".")
231 || !strcmp(name_1, ".."))
234 (void) strcpy(buf, eargv[i]);
235 (void) strcat(buf, "/");
236 (void) strcat(buf, name_1);
237 (void) strcat(buf, "/");
239 entrydir = opendir(".");
240 while ((entry = readdir(entrydir)) != NULL)
242 char name_2[PATH_MAX];
248 strncpy(name_2, entry->d_name, len)[len] = '\0';
249 if (!strcmp(name_2, ".")
250 || !strcmp(name_2, ".."))
253 status = _nc_read_file_entry(name_2, <erm);
256 (void) fprintf(stderr,
257 "toe: couldn't open terminfo file %s.\n",
259 return(EXIT_FAILURE);
262 /* only visit things once, by primary name */
263 cn = _nc_first_name(lterm.term_names);
264 if (!strcmp(cn, name_2))
266 /* apply the selected hook function */
269 if (lterm.term_names) {
270 free(lterm.term_names);
271 lterm.term_names = NULL;
273 if (lterm.str_table) {
274 free(lterm.str_table);
275 lterm.str_table = NULL;
283 return(EXIT_SUCCESS);