1 /****************************************************************************
2 * Copyright (c) 1998-2006,2007 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 * and: Thomas E. Dickey 1996-on *
33 ****************************************************************************/
36 * toe.c --- table of entries report generator
39 #include <progs.priv.h>
43 #include <dump_entry.h>
46 #include <hashed_db.h>
49 MODULE_ID("$Id: toe.c,v 1.42 2007/01/21 01:09:07 tom Exp $")
51 #define isDotname(name) (!strcmp(name, ".") || !strcmp(name, ".."))
53 const char *_nc_progname;
57 static void ExitProgram(int code) GCC_NORETURN;
61 _nc_free_entries(_nc_head);
62 _nc_leaks_dump_entry();
64 _nc_free_and_exit(code);
70 make_db_name(char *dst, const char *src, unsigned limit)
72 static const char suffix[] = DBM_SUFFIX;
75 unsigned lens = sizeof(suffix) - 1;
76 unsigned size = strlen(src);
77 unsigned need = lens + size;
81 && !strcmp(src + size - lens, suffix))
82 (void) strcpy(dst, src);
84 (void) sprintf(dst, "%s%s", src, suffix);
92 is_database(const char *path)
96 if (_nc_is_dir_path(path) && access(path, R_OK | X_OK) == 0) {
101 if (_nc_is_file_path(path) && access(path, R_OK) == 0) {
107 char filename[PATH_MAX];
108 if (_nc_is_file_path(path) && access(path, R_OK) == 0) {
110 } else if (make_db_name(filename, path, sizeof(filename))) {
111 if (_nc_is_file_path(filename) && access(filename, R_OK) == 0) {
121 deschook(const char *cn, TERMTYPE *tp)
122 /* display a description for the type */
126 if ((desc = strrchr(tp->term_names, '|')) == 0 || *++desc == '\0')
127 desc = "(No description)";
129 (void) printf("%-10s\t%s\n", cn, desc);
134 show_termcap(char *buffer,
135 void (*hook) (const char *, TERMTYPE *tp))
138 char *next = strchr(buffer, ':');
145 last = strrchr(buffer, '|');
149 data.term_names = strdup(buffer);
150 while ((next = strtok(list, "|")) != 0) {
155 free(data.term_names);
160 typelist(int eargc, char *eargv[],
162 void (*hook) (const char *, TERMTYPE *tp))
163 /* apply a function to each entry in given terminfo directories */
167 for (i = 0; i < eargc; i++) {
169 if (_nc_is_dir_path(eargv[i])) {
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]);
182 while ((subdir = readdir(termdir)) != 0) {
183 size_t len = NAMLEN(subdir);
185 char name_1[PATH_MAX];
189 strncpy(name_1, subdir->d_name, len)[len] = '\0';
190 if (isDotname(name_1))
193 (void) sprintf(buf, "%s/%s/", eargv[i], name_1);
197 entrydir = opendir(".");
198 while ((entry = readdir(entrydir)) != 0) {
199 char name_2[PATH_MAX];
205 strncpy(name_2, entry->d_name, len)[len] = '\0';
206 if (isDotname(name_2) || !_nc_is_file_path(name_2))
209 status = _nc_read_file_entry(name_2, <erm);
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);
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, <erm);
224 _nc_free_termtype(<erm);
233 char filename[PATH_MAX];
235 if (make_db_name(filename, eargv[i], sizeof(filename))) {
236 if ((capdbp = _nc_db_open(filename, FALSE)) != 0) {
240 code = _nc_db_first(capdbp, &key, &data);
247 if (_nc_db_have_data(&key, &data, &have, &used)) {
248 if (_nc_read_termtype(<erm, 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, <erm);
253 _nc_free_termtype(<erm);
256 code = _nc_db_next(capdbp, &key, &data);
259 _nc_db_close(capdbp);
271 (void) printf("#\n#%s:\n#\n", eargv[i]);
273 db_array[0] = eargv[i];
276 if (cgetfirst(&buffer, db_array)) {
277 show_termcap(buffer, hook);
279 while (cgetnext(&buffer, db_array)) {
280 show_termcap(buffer, hook);
286 /* scan termcap text-file only */
287 if (_nc_is_file_path(eargv[i])) {
291 if ((fp = fopen(eargv[i], "r")) != 0) {
292 while (fgets(buffer, sizeof(buffer), fp) != 0) {
295 if (isspace(*buffer))
297 show_termcap(buffer, hook);
306 return (EXIT_SUCCESS);
312 (void) fprintf(stderr, "usage: %s [-ahuUV] [-v n] [file...]\n", _nc_progname);
313 ExitProgram(EXIT_FAILURE);
317 main(int argc, char *argv[])
319 bool all_dirs = FALSE;
320 bool direct_dependencies = FALSE;
321 bool invert_dependencies = FALSE;
325 int this_opt, last_opt = '?';
328 _nc_progname = _nc_rootname(argv[0]);
330 while ((this_opt = getopt(argc, argv, "0123456789ahuvUV")) != EOF) {
331 /* handle optional parameter */
332 if (isdigit(this_opt)) {
335 v_opt = (this_opt - '0');
338 if (isdigit(last_opt))
342 v_opt += (this_opt - '0');
355 direct_dependencies = TRUE;
361 invert_dependencies = TRUE;
364 puts(curses_version());
365 ExitProgram(EXIT_SUCCESS);
370 set_trace_level(v_opt);
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);
379 /* parse entries out of the source file */
380 _nc_set_source(argv[optind]);
381 _nc_read_entry_source(stdin, 0, FALSE, FALSE, NULLHOOK);
384 /* maybe we want a direct-dependency listing? */
385 if (direct_dependencies) {
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);
399 ExitProgram(EXIT_SUCCESS);
402 /* maybe we want a reverse-dependency listing? */
403 if (invert_dependencies) {
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)
418 _nc_first_name(qp->tterm.term_names));
420 _nc_first_name(rp->tterm.term_names));
427 ExitProgram(EXIT_SUCCESS);
431 * If we get this far, user wants a simple terminal type listing.
434 code = typelist(argc - optind, argv + optind, header, deschook);
435 } else if (all_dirs) {
443 for (pass = 0; pass < 2; ++pass) {
446 _nc_first_db(&state, &offset);
447 while ((path = _nc_next_db(&state, &offset)) != 0) {
448 if (!is_database(path)) {
450 } else if (eargv != 0) {
454 /* eliminate duplicates */
455 for (n = 0; n < count; ++n) {
456 if (!strcmp(path, eargv[n])) {
462 eargv[count] = strdup(path);
470 eargv = typeCalloc(char *, count + 1);
472 code = typelist((int) count, eargv, header, deschook);
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);
494 code = typelist(count, eargv, header, deschook);