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.43 2007/02/03 19:10:39 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();
69 make_db_name(char *dst, const char *src, unsigned limit)
71 static const char suffix[] = DBM_SUFFIX;
74 unsigned lens = sizeof(suffix) - 1;
75 unsigned size = strlen(src);
76 unsigned need = lens + size;
80 && !strcmp(src + size - lens, suffix))
81 (void) strcpy(dst, src);
83 (void) sprintf(dst, "%s%s", src, suffix);
91 is_database(const char *path)
95 if (_nc_is_dir_path(path) && access(path, R_OK | X_OK) == 0) {
100 if (_nc_is_file_path(path) && access(path, R_OK) == 0) {
106 char filename[PATH_MAX];
107 if (_nc_is_file_path(path) && access(path, R_OK) == 0) {
109 } else if (make_db_name(filename, path, sizeof(filename))) {
110 if (_nc_is_file_path(filename) && access(filename, R_OK) == 0) {
120 deschook(const char *cn, TERMTYPE *tp)
121 /* display a description for the type */
125 if ((desc = strrchr(tp->term_names, '|')) == 0 || *++desc == '\0')
126 desc = "(No description)";
128 (void) printf("%-10s\t%s\n", cn, desc);
133 show_termcap(char *buffer,
134 void (*hook) (const char *, TERMTYPE *tp))
137 char *next = strchr(buffer, ':');
144 last = strrchr(buffer, '|');
148 data.term_names = strdup(buffer);
149 while ((next = strtok(list, "|")) != 0) {
154 free(data.term_names);
159 typelist(int eargc, char *eargv[],
161 void (*hook) (const char *, TERMTYPE *tp))
162 /* apply a function to each entry in given terminfo directories */
166 for (i = 0; i < eargc; i++) {
168 if (_nc_is_dir_path(eargv[i])) {
172 if ((termdir = opendir(eargv[i])) == 0) {
173 (void) fflush(stdout);
174 (void) fprintf(stderr,
175 "%s: can't open terminfo directory %s\n",
176 _nc_progname, eargv[i]);
177 return (EXIT_FAILURE);
178 } else if (verbosity)
179 (void) printf("#\n#%s:\n#\n", eargv[i]);
181 while ((subdir = readdir(termdir)) != 0) {
182 size_t len = NAMLEN(subdir);
184 char name_1[PATH_MAX];
188 strncpy(name_1, subdir->d_name, len)[len] = '\0';
189 if (isDotname(name_1))
192 (void) sprintf(buf, "%s/%s/", eargv[i], name_1);
196 entrydir = opendir(".");
197 while ((entry = readdir(entrydir)) != 0) {
198 char name_2[PATH_MAX];
204 strncpy(name_2, entry->d_name, len)[len] = '\0';
205 if (isDotname(name_2) || !_nc_is_file_path(name_2))
208 status = _nc_read_file_entry(name_2, <erm);
210 (void) fflush(stdout);
211 (void) fprintf(stderr,
212 "%s: couldn't open terminfo file %s.\n",
213 _nc_progname, name_2);
214 return (EXIT_FAILURE);
217 /* only visit things once, by primary name */
218 cn = _nc_first_name(lterm.term_names);
219 if (!strcmp(cn, name_2)) {
220 /* apply the selected hook function */
221 (*hook) (cn, <erm);
223 _nc_free_termtype(<erm);
232 char filename[PATH_MAX];
234 if (make_db_name(filename, eargv[i], sizeof(filename))) {
235 if ((capdbp = _nc_db_open(filename, FALSE)) != 0) {
239 code = _nc_db_first(capdbp, &key, &data);
246 if (_nc_db_have_data(&key, &data, &have, &used)) {
247 if (_nc_read_termtype(<erm, have, used) > 0) {
248 /* only visit things once, by primary name */
249 cn = _nc_first_name(lterm.term_names);
250 /* apply the selected hook function */
251 (*hook) (cn, <erm);
252 _nc_free_termtype(<erm);
255 code = _nc_db_next(capdbp, &key, &data);
258 _nc_db_close(capdbp);
270 (void) printf("#\n#%s:\n#\n", eargv[i]);
272 db_array[0] = eargv[i];
275 if (cgetfirst(&buffer, db_array)) {
276 show_termcap(buffer, hook);
278 while (cgetnext(&buffer, db_array)) {
279 show_termcap(buffer, hook);
285 /* scan termcap text-file only */
286 if (_nc_is_file_path(eargv[i])) {
290 if ((fp = fopen(eargv[i], "r")) != 0) {
291 while (fgets(buffer, sizeof(buffer), fp) != 0) {
294 if (isspace(*buffer))
296 show_termcap(buffer, hook);
305 return (EXIT_SUCCESS);
311 (void) fprintf(stderr, "usage: %s [-ahuUV] [-v n] [file...]\n", _nc_progname);
312 ExitProgram(EXIT_FAILURE);
316 main(int argc, char *argv[])
318 bool all_dirs = FALSE;
319 bool direct_dependencies = FALSE;
320 bool invert_dependencies = FALSE;
324 int this_opt, last_opt = '?';
327 _nc_progname = _nc_rootname(argv[0]);
329 while ((this_opt = getopt(argc, argv, "0123456789ahuvUV")) != EOF) {
330 /* handle optional parameter */
331 if (isdigit(this_opt)) {
334 v_opt = (this_opt - '0');
337 if (isdigit(last_opt))
341 v_opt += (this_opt - '0');
354 direct_dependencies = TRUE;
360 invert_dependencies = TRUE;
363 puts(curses_version());
364 ExitProgram(EXIT_SUCCESS);
369 set_trace_level(v_opt);
371 if (direct_dependencies || invert_dependencies) {
372 if (freopen(argv[optind], "r", stdin) == 0) {
373 (void) fflush(stdout);
374 fprintf(stderr, "%s: can't open %s\n", _nc_progname, argv[optind]);
375 ExitProgram(EXIT_FAILURE);
378 /* parse entries out of the source file */
379 _nc_set_source(argv[optind]);
380 _nc_read_entry_source(stdin, 0, FALSE, FALSE, NULLHOOK);
383 /* maybe we want a direct-dependency listing? */
384 if (direct_dependencies) {
391 (void) printf("%s:", _nc_first_name(qp->tterm.term_names));
392 for (j = 0; j < qp->nuses; j++)
393 (void) printf(" %s", qp->uses[j].name);
398 ExitProgram(EXIT_SUCCESS);
401 /* maybe we want a reverse-dependency listing? */
402 if (invert_dependencies) {
412 for (i = 0; i < rp->nuses; i++)
413 if (_nc_name_match(qp->tterm.term_names,
414 rp->uses[i].name, "|")) {
415 if (matchcount++ == 0)
417 _nc_first_name(qp->tterm.term_names));
419 _nc_first_name(rp->tterm.term_names));
426 ExitProgram(EXIT_SUCCESS);
430 * If we get this far, user wants a simple terminal type listing.
433 code = typelist(argc - optind, argv + optind, header, deschook);
434 } else if (all_dirs) {
442 for (pass = 0; pass < 2; ++pass) {
445 _nc_first_db(&state, &offset);
446 while ((path = _nc_next_db(&state, &offset)) != 0) {
447 if (!is_database(path)) {
449 } else if (eargv != 0) {
453 /* eliminate duplicates */
454 for (n = 0; n < count; ++n) {
455 if (!strcmp(path, eargv[n])) {
461 eargv[count] = strdup(path);
469 eargv = typeCalloc(char *, count + 1);
471 code = typelist((int) count, eargv, header, deschook);
484 _nc_first_db(&state, &offset);
485 while ((path = _nc_next_db(&state, &offset)) != 0) {
486 if (is_database(path)) {
487 eargv[count++] = strdup(path);
493 code = typelist(count, eargv, header, deschook);