1 /****************************************************************************
2 * Copyright (c) 1998-2007,2008 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.51 2008/08/16 21:53:25 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);
66 make_db_name(char *dst, const char *src, unsigned limit)
68 static const char suffix[] = DBM_SUFFIX;
71 unsigned lens = sizeof(suffix) - 1;
72 unsigned size = strlen(src);
73 unsigned need = lens + size;
77 && !strcmp(src + size - lens, suffix))
78 (void) strcpy(dst, src);
80 (void) sprintf(dst, "%s%s", src, suffix);
88 is_database(const char *path)
92 if (_nc_is_dir_path(path) && access(path, R_OK | X_OK) == 0) {
97 if (_nc_is_file_path(path) && access(path, R_OK) == 0) {
103 char filename[PATH_MAX];
104 if (_nc_is_file_path(path) && access(path, R_OK) == 0) {
106 } else if (make_db_name(filename, path, sizeof(filename))) {
107 if (_nc_is_file_path(filename) && access(filename, R_OK) == 0) {
117 deschook(const char *cn, TERMTYPE *tp)
118 /* display a description for the type */
122 if ((desc = strrchr(tp->term_names, '|')) == 0 || *++desc == '\0')
123 desc = "(No description)";
125 (void) printf("%-10s\t%s\n", cn, desc);
130 show_termcap(char *buffer,
131 void (*hook) (const char *, TERMTYPE *tp))
134 char *next = strchr(buffer, ':');
141 last = strrchr(buffer, '|');
145 data.term_names = strdup(buffer);
146 while ((next = strtok(list, "|")) != 0) {
151 free(data.term_names);
156 typelist(int eargc, char *eargv[],
158 void (*hook) (const char *, TERMTYPE *tp))
159 /* apply a function to each entry in given terminfo directories */
163 for (i = 0; i < eargc; i++) {
165 if (_nc_is_dir_path(eargv[i])) {
170 if ((termdir = opendir(eargv[i])) == 0) {
171 (void) fflush(stdout);
172 (void) fprintf(stderr,
173 "%s: can't open terminfo directory %s\n",
174 _nc_progname, eargv[i]);
175 return (EXIT_FAILURE);
176 } else if (verbosity)
177 (void) printf("#\n#%s:\n#\n", eargv[i]);
179 while ((subdir = readdir(termdir)) != 0) {
180 size_t len = NAMLEN(subdir);
181 size_t cwd_len = len + strlen(eargv[i]) + 3;
182 char name_1[PATH_MAX];
186 cwd_buf = typeRealloc(char, cwd_len, cwd_buf);
188 perror("realloc cwd_buf");
192 strncpy(name_1, subdir->d_name, len)[len] = '\0';
193 if (isDotname(name_1))
196 (void) sprintf(cwd_buf, "%s/%.*s/", eargv[i], (int) len, name_1);
197 if (chdir(cwd_buf) != 0)
200 entrydir = opendir(".");
205 while ((entry = readdir(entrydir)) != 0) {
206 char name_2[PATH_MAX];
212 strncpy(name_2, entry->d_name, len)[len] = '\0';
213 if (isDotname(name_2) || !_nc_is_file_path(name_2))
216 status = _nc_read_file_entry(name_2, <erm);
218 (void) fflush(stdout);
219 (void) fprintf(stderr,
220 "%s: couldn't open terminfo file %s.\n",
221 _nc_progname, name_2);
222 return (EXIT_FAILURE);
225 /* only visit things once, by primary name */
226 cn = _nc_first_name(lterm.term_names);
227 if (!strcmp(cn, name_2)) {
228 /* apply the selected hook function */
229 (*hook) (cn, <erm);
231 _nc_free_termtype(<erm);
242 char filename[PATH_MAX];
244 if (make_db_name(filename, eargv[i], sizeof(filename))) {
245 if ((capdbp = _nc_db_open(filename, FALSE)) != 0) {
249 code = _nc_db_first(capdbp, &key, &data);
256 if (_nc_db_have_data(&key, &data, &have, &used)) {
257 if (_nc_read_termtype(<erm, have, used) > 0) {
258 /* only visit things once, by primary name */
259 cn = _nc_first_name(lterm.term_names);
260 /* apply the selected hook function */
261 (*hook) (cn, <erm);
262 _nc_free_termtype(<erm);
265 code = _nc_db_next(capdbp, &key, &data);
268 _nc_db_close(capdbp);
280 (void) printf("#\n#%s:\n#\n", eargv[i]);
282 db_array[0] = eargv[i];
285 if (cgetfirst(&buffer, db_array)) {
286 show_termcap(buffer, hook);
288 while (cgetnext(&buffer, db_array)) {
289 show_termcap(buffer, hook);
295 /* scan termcap text-file only */
296 if (_nc_is_file_path(eargv[i])) {
300 if ((fp = fopen(eargv[i], "r")) != 0) {
301 while (fgets(buffer, sizeof(buffer), fp) != 0) {
304 if (isspace(*buffer))
306 show_termcap(buffer, hook);
315 return (EXIT_SUCCESS);
321 (void) fprintf(stderr, "usage: %s [-ahuUV] [-v n] [file...]\n", _nc_progname);
322 ExitProgram(EXIT_FAILURE);
326 main(int argc, char *argv[])
328 bool all_dirs = FALSE;
329 bool direct_dependencies = FALSE;
330 bool invert_dependencies = FALSE;
332 char *report_file = 0;
335 int this_opt, last_opt = '?';
338 _nc_progname = _nc_rootname(argv[0]);
340 while ((this_opt = getopt(argc, argv, "0123456789ahu:vU:V")) != -1) {
341 /* handle optional parameter */
342 if (isdigit(this_opt)) {
345 v_opt = (this_opt - '0');
348 if (isdigit(last_opt))
352 v_opt += (this_opt - '0');
365 direct_dependencies = TRUE;
366 report_file = optarg;
372 invert_dependencies = TRUE;
373 report_file = optarg;
376 puts(curses_version());
377 ExitProgram(EXIT_SUCCESS);
382 set_trace_level(v_opt);
384 if (report_file != 0) {
385 if (freopen(report_file, "r", stdin) == 0) {
386 (void) fflush(stdout);
387 fprintf(stderr, "%s: can't open %s\n", _nc_progname, report_file);
388 ExitProgram(EXIT_FAILURE);
391 /* parse entries out of the source file */
392 _nc_set_source(report_file);
393 _nc_read_entry_source(stdin, 0, FALSE, FALSE, NULLHOOK);
396 /* maybe we want a direct-dependency listing? */
397 if (direct_dependencies) {
404 (void) printf("%s:", _nc_first_name(qp->tterm.term_names));
405 for (j = 0; j < qp->nuses; j++)
406 (void) printf(" %s", qp->uses[j].name);
411 ExitProgram(EXIT_SUCCESS);
414 /* maybe we want a reverse-dependency listing? */
415 if (invert_dependencies) {
425 for (i = 0; i < rp->nuses; i++)
426 if (_nc_name_match(qp->tterm.term_names,
427 rp->uses[i].name, "|")) {
428 if (matchcount++ == 0)
430 _nc_first_name(qp->tterm.term_names));
432 _nc_first_name(rp->tterm.term_names));
439 ExitProgram(EXIT_SUCCESS);
443 * If we get this far, user wants a simple terminal type listing.
446 code = typelist(argc - optind, argv + optind, header, deschook);
447 } else if (all_dirs) {
455 for (pass = 0; pass < 2; ++pass) {
458 _nc_first_db(&state, &offset);
459 while ((path = _nc_next_db(&state, &offset)) != 0) {
460 if (!is_database(path)) {
462 } else if (eargv != 0) {
466 /* eliminate duplicates */
467 for (n = 0; n < count; ++n) {
468 if (!strcmp(path, eargv[n])) {
474 eargv[count] = strdup(path);
482 eargv = typeCalloc(char *, count + 1);
484 code = typelist((int) count, eargv, header, deschook);
497 _nc_first_db(&state, &offset);
498 while ((path = _nc_next_db(&state, &offset)) != 0) {
499 if (is_database(path)) {
500 eargv[count++] = strdup(path);
506 code = typelist(count, eargv, header, deschook);