1 /****************************************************************************
2 * Copyright (c) 1998-2010,2011 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>
44 #include <hashed_db.h>
47 MODULE_ID("$Id: toe.c,v 1.58 2011/08/07 18:36:31 tom Exp $")
49 #define isDotname(name) (!strcmp(name, ".") || !strcmp(name, ".."))
51 const char *_nc_progname;
55 static void ExitProgram(int code) GCC_NORETURN;
59 _nc_free_entries(_nc_head);
64 static void failed(const char *) GCC_NORETURN;
67 failed(const char *msg)
70 ExitProgram(EXIT_FAILURE);
75 make_db_name(char *dst, const char *src, unsigned limit)
77 static const char suffix[] = DBM_SUFFIX;
80 unsigned lens = sizeof(suffix) - 1;
81 unsigned size = strlen(src);
82 unsigned need = lens + size;
86 && !strcmp(src + size - lens, suffix))
87 (void) strcpy(dst, src);
89 (void) sprintf(dst, "%s%s", src, suffix);
97 is_database(const char *path)
101 if (_nc_is_dir_path(path) && access(path, R_OK | X_OK) == 0) {
106 if (_nc_is_file_path(path) && access(path, R_OK) == 0) {
112 char filename[PATH_MAX];
113 if (_nc_is_file_path(path) && access(path, R_OK) == 0) {
115 } else if (make_db_name(filename, path, sizeof(filename))) {
116 if (_nc_is_file_path(filename) && access(filename, R_OK) == 0) {
126 deschook(const char *cn, TERMTYPE *tp)
127 /* display a description for the type */
131 if ((desc = strrchr(tp->term_names, '|')) == 0 || *++desc == '\0')
132 desc = "(No description)";
134 (void) printf("%-10s\t%s\n", cn, desc);
139 show_termcap(char *buffer,
140 void (*hook) (const char *, TERMTYPE *tp))
143 char *next = strchr(buffer, ':');
150 last = strrchr(buffer, '|');
154 data.term_names = strdup(buffer);
155 while ((next = strtok(list, "|")) != 0) {
160 free(data.term_names);
165 typelist(int eargc, char *eargv[],
167 void (*hook) (const char *, TERMTYPE *tp))
168 /* apply a function to each entry in given terminfo directories */
172 for (i = 0; i < eargc; i++) {
174 if (_nc_is_dir_path(eargv[i])) {
179 if ((termdir = opendir(eargv[i])) == 0) {
180 (void) fflush(stdout);
181 (void) fprintf(stderr,
182 "%s: can't open terminfo directory %s\n",
183 _nc_progname, eargv[i]);
188 (void) printf("#\n#%s:\n#\n", eargv[i]);
190 while ((subdir = readdir(termdir)) != 0) {
191 size_t len = NAMLEN(subdir);
192 size_t cwd_len = len + strlen(eargv[i]) + 3;
193 char name_1[PATH_MAX];
197 cwd_buf = typeRealloc(char, cwd_len, cwd_buf);
199 failed("realloc cwd_buf");
201 assert(cwd_buf != 0);
203 strncpy(name_1, subdir->d_name, len)[len] = '\0';
204 if (isDotname(name_1))
207 (void) sprintf(cwd_buf, "%s/%.*s/", eargv[i], (int) len, name_1);
208 if (chdir(cwd_buf) != 0)
211 entrydir = opendir(".");
216 while ((entry = readdir(entrydir)) != 0) {
217 char name_2[PATH_MAX];
223 strncpy(name_2, entry->d_name, len)[len] = '\0';
224 if (isDotname(name_2) || !_nc_is_file_path(name_2))
227 status = _nc_read_file_entry(name_2, <erm);
229 (void) fflush(stdout);
230 (void) fprintf(stderr,
231 "%s: couldn't open terminfo file %s.\n",
232 _nc_progname, name_2);
233 return (EXIT_FAILURE);
236 /* only visit things once, by primary name */
237 cn = _nc_first_name(lterm.term_names);
238 if (!strcmp(cn, name_2)) {
239 /* apply the selected hook function */
240 (*hook) (cn, <erm);
242 _nc_free_termtype(<erm);
254 char filename[PATH_MAX];
257 (void) printf("#\n#%s:\n#\n", eargv[i]);
259 if (make_db_name(filename, eargv[i], sizeof(filename))) {
260 if ((capdbp = _nc_db_open(filename, FALSE)) != 0) {
264 code = _nc_db_first(capdbp, &key, &data);
271 if (_nc_db_have_data(&key, &data, &have, &used)) {
272 if (_nc_read_termtype(<erm, have, used) > 0) {
273 /* only visit things once, by primary name */
274 cn = _nc_first_name(lterm.term_names);
275 /* apply the selected hook function */
276 (*hook) (cn, <erm);
277 _nc_free_termtype(<erm);
280 code = _nc_db_next(capdbp, &key, &data);
283 _nc_db_close(capdbp);
293 CGETENT_CONST char *db_array[2];
297 (void) printf("#\n#%s:\n#\n", eargv[i]);
299 db_array[0] = eargv[i];
302 if (cgetfirst(&buffer, db_array) > 0) {
303 show_termcap(buffer, hook);
305 while (cgetnext(&buffer, db_array) > 0) {
306 show_termcap(buffer, hook);
314 /* scan termcap text-file only */
315 if (_nc_is_file_path(eargv[i])) {
320 (void) printf("#\n#%s:\n#\n", eargv[i]);
322 if ((fp = fopen(eargv[i], "r")) != 0) {
323 while (fgets(buffer, sizeof(buffer), fp) != 0) {
326 if (isspace(*buffer))
328 show_termcap(buffer, hook);
337 return (EXIT_SUCCESS);
343 (void) fprintf(stderr, "usage: %s [-ahuUV] [-v n] [file...]\n", _nc_progname);
344 ExitProgram(EXIT_FAILURE);
348 main(int argc, char *argv[])
350 bool all_dirs = FALSE;
351 bool direct_dependencies = FALSE;
352 bool invert_dependencies = FALSE;
354 char *report_file = 0;
357 int this_opt, last_opt = '?';
360 _nc_progname = _nc_rootname(argv[0]);
362 while ((this_opt = getopt(argc, argv, "0123456789ahu:vU:V")) != -1) {
363 /* handle optional parameter */
364 if (isdigit(this_opt)) {
367 v_opt = (unsigned) (this_opt - '0');
370 if (isdigit(last_opt))
374 v_opt += (unsigned) (this_opt - '0');
387 direct_dependencies = TRUE;
388 report_file = optarg;
394 invert_dependencies = TRUE;
395 report_file = optarg;
398 puts(curses_version());
399 ExitProgram(EXIT_SUCCESS);
404 set_trace_level(v_opt);
406 if (report_file != 0) {
407 if (freopen(report_file, "r", stdin) == 0) {
408 (void) fflush(stdout);
409 fprintf(stderr, "%s: can't open %s\n", _nc_progname, report_file);
410 ExitProgram(EXIT_FAILURE);
413 /* parse entries out of the source file */
414 _nc_set_source(report_file);
415 _nc_read_entry_source(stdin, 0, FALSE, FALSE, NULLHOOK);
418 /* maybe we want a direct-dependency listing? */
419 if (direct_dependencies) {
426 (void) printf("%s:", _nc_first_name(qp->tterm.term_names));
427 for (j = 0; j < qp->nuses; j++)
428 (void) printf(" %s", qp->uses[j].name);
433 ExitProgram(EXIT_SUCCESS);
436 /* maybe we want a reverse-dependency listing? */
437 if (invert_dependencies) {
447 for (i = 0; i < rp->nuses; i++)
448 if (_nc_name_match(qp->tterm.term_names,
449 rp->uses[i].name, "|")) {
450 if (matchcount++ == 0)
452 _nc_first_name(qp->tterm.term_names));
454 _nc_first_name(rp->tterm.term_names));
461 ExitProgram(EXIT_SUCCESS);
465 * If we get this far, user wants a simple terminal type listing.
468 code = typelist(argc - optind, argv + optind, header, deschook);
469 } else if (all_dirs) {
477 for (pass = 0; pass < 2; ++pass) {
480 _nc_first_db(&state, &offset);
481 while ((path = _nc_next_db(&state, &offset)) != 0) {
482 if (!is_database(path)) {
484 } else if (eargv != 0) {
488 /* eliminate duplicates */
489 for (n = 0; n < count; ++n) {
490 if (!strcmp(path, eargv[n])) {
496 eargv[count] = strdup(path);
504 eargv = typeCalloc(char *, count + 1);
506 failed("realloc eargv");
510 code = typelist((int) count, eargv, header, deschook);
523 _nc_first_db(&state, &offset);
524 while ((path = _nc_next_db(&state, &offset)) != 0) {
525 if (is_database(path)) {
526 eargv[count++] = strdup(path);
532 code = typelist(count, eargv, header, deschook);