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.54 2011/05/21 18:32:13 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]);
184 return (EXIT_FAILURE);
185 } else if (verbosity)
186 (void) printf("#\n#%s:\n#\n", eargv[i]);
188 while ((subdir = readdir(termdir)) != 0) {
189 size_t len = NAMLEN(subdir);
190 size_t cwd_len = len + strlen(eargv[i]) + 3;
191 char name_1[PATH_MAX];
195 cwd_buf = typeRealloc(char, cwd_len, cwd_buf);
197 failed("realloc cwd_buf");
199 assert(cwd_buf != 0);
201 strncpy(name_1, subdir->d_name, len)[len] = '\0';
202 if (isDotname(name_1))
205 (void) sprintf(cwd_buf, "%s/%.*s/", eargv[i], (int) len, name_1);
206 if (chdir(cwd_buf) != 0)
209 entrydir = opendir(".");
214 while ((entry = readdir(entrydir)) != 0) {
215 char name_2[PATH_MAX];
221 strncpy(name_2, entry->d_name, len)[len] = '\0';
222 if (isDotname(name_2) || !_nc_is_file_path(name_2))
225 status = _nc_read_file_entry(name_2, <erm);
227 (void) fflush(stdout);
228 (void) fprintf(stderr,
229 "%s: couldn't open terminfo file %s.\n",
230 _nc_progname, name_2);
231 return (EXIT_FAILURE);
234 /* only visit things once, by primary name */
235 cn = _nc_first_name(lterm.term_names);
236 if (!strcmp(cn, name_2)) {
237 /* apply the selected hook function */
238 (*hook) (cn, <erm);
240 _nc_free_termtype(<erm);
251 char filename[PATH_MAX];
253 if (make_db_name(filename, eargv[i], sizeof(filename))) {
254 if ((capdbp = _nc_db_open(filename, FALSE)) != 0) {
258 code = _nc_db_first(capdbp, &key, &data);
265 if (_nc_db_have_data(&key, &data, &have, &used)) {
266 if (_nc_read_termtype(<erm, have, used) > 0) {
267 /* only visit things once, by primary name */
268 cn = _nc_first_name(lterm.term_names);
269 /* apply the selected hook function */
270 (*hook) (cn, <erm);
271 _nc_free_termtype(<erm);
274 code = _nc_db_next(capdbp, &key, &data);
277 _nc_db_close(capdbp);
289 (void) printf("#\n#%s:\n#\n", eargv[i]);
291 db_array[0] = eargv[i];
294 if (cgetfirst(&buffer, db_array)) {
295 show_termcap(buffer, hook);
297 while (cgetnext(&buffer, db_array)) {
298 show_termcap(buffer, hook);
304 /* scan termcap text-file only */
305 if (_nc_is_file_path(eargv[i])) {
309 if ((fp = fopen(eargv[i], "r")) != 0) {
310 while (fgets(buffer, sizeof(buffer), fp) != 0) {
313 if (isspace(*buffer))
315 show_termcap(buffer, hook);
324 return (EXIT_SUCCESS);
330 (void) fprintf(stderr, "usage: %s [-ahuUV] [-v n] [file...]\n", _nc_progname);
331 ExitProgram(EXIT_FAILURE);
335 main(int argc, char *argv[])
337 bool all_dirs = FALSE;
338 bool direct_dependencies = FALSE;
339 bool invert_dependencies = FALSE;
341 char *report_file = 0;
344 int this_opt, last_opt = '?';
347 _nc_progname = _nc_rootname(argv[0]);
349 while ((this_opt = getopt(argc, argv, "0123456789ahu:vU:V")) != -1) {
350 /* handle optional parameter */
351 if (isdigit(this_opt)) {
354 v_opt = (unsigned) (this_opt - '0');
357 if (isdigit(last_opt))
361 v_opt += (unsigned) (this_opt - '0');
374 direct_dependencies = TRUE;
375 report_file = optarg;
381 invert_dependencies = TRUE;
382 report_file = optarg;
385 puts(curses_version());
386 ExitProgram(EXIT_SUCCESS);
391 set_trace_level(v_opt);
393 if (report_file != 0) {
394 if (freopen(report_file, "r", stdin) == 0) {
395 (void) fflush(stdout);
396 fprintf(stderr, "%s: can't open %s\n", _nc_progname, report_file);
397 ExitProgram(EXIT_FAILURE);
400 /* parse entries out of the source file */
401 _nc_set_source(report_file);
402 _nc_read_entry_source(stdin, 0, FALSE, FALSE, NULLHOOK);
405 /* maybe we want a direct-dependency listing? */
406 if (direct_dependencies) {
413 (void) printf("%s:", _nc_first_name(qp->tterm.term_names));
414 for (j = 0; j < qp->nuses; j++)
415 (void) printf(" %s", qp->uses[j].name);
420 ExitProgram(EXIT_SUCCESS);
423 /* maybe we want a reverse-dependency listing? */
424 if (invert_dependencies) {
434 for (i = 0; i < rp->nuses; i++)
435 if (_nc_name_match(qp->tterm.term_names,
436 rp->uses[i].name, "|")) {
437 if (matchcount++ == 0)
439 _nc_first_name(qp->tterm.term_names));
441 _nc_first_name(rp->tterm.term_names));
448 ExitProgram(EXIT_SUCCESS);
452 * If we get this far, user wants a simple terminal type listing.
455 code = typelist(argc - optind, argv + optind, header, deschook);
456 } else if (all_dirs) {
464 for (pass = 0; pass < 2; ++pass) {
467 _nc_first_db(&state, &offset);
468 while ((path = _nc_next_db(&state, &offset)) != 0) {
469 if (!is_database(path)) {
471 } else if (eargv != 0) {
475 /* eliminate duplicates */
476 for (n = 0; n < count; ++n) {
477 if (!strcmp(path, eargv[n])) {
483 eargv[count] = strdup(path);
491 eargv = typeCalloc(char *, count + 1);
493 failed("realloc eargv");
497 code = typelist((int) count, eargv, header, deschook);
510 _nc_first_db(&state, &offset);
511 while ((path = _nc_next_db(&state, &offset)) != 0) {
512 if (is_database(path)) {
513 eargv[count++] = strdup(path);
519 code = typelist(count, eargv, header, deschook);