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.48 2008/01/05 20:41:26 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])) {
169 if ((termdir = opendir(eargv[i])) == 0) {
170 (void) fflush(stdout);
171 (void) fprintf(stderr,
172 "%s: can't open terminfo directory %s\n",
173 _nc_progname, eargv[i]);
174 return (EXIT_FAILURE);
175 } else if (verbosity)
176 (void) printf("#\n#%s:\n#\n", eargv[i]);
178 while ((subdir = readdir(termdir)) != 0) {
179 size_t len = NAMLEN(subdir);
181 char name_1[PATH_MAX];
185 strncpy(name_1, subdir->d_name, len)[len] = '\0';
186 if (isDotname(name_1))
189 (void) sprintf(buf, "%s/%s/", eargv[i], name_1);
193 entrydir = opendir(".");
194 while ((entry = readdir(entrydir)) != 0) {
195 char name_2[PATH_MAX];
201 strncpy(name_2, entry->d_name, len)[len] = '\0';
202 if (isDotname(name_2) || !_nc_is_file_path(name_2))
205 status = _nc_read_file_entry(name_2, <erm);
207 (void) fflush(stdout);
208 (void) fprintf(stderr,
209 "%s: couldn't open terminfo file %s.\n",
210 _nc_progname, name_2);
211 return (EXIT_FAILURE);
214 /* only visit things once, by primary name */
215 cn = _nc_first_name(lterm.term_names);
216 if (!strcmp(cn, name_2)) {
217 /* apply the selected hook function */
218 (*hook) (cn, <erm);
220 _nc_free_termtype(<erm);
229 char filename[PATH_MAX];
231 if (make_db_name(filename, eargv[i], sizeof(filename))) {
232 if ((capdbp = _nc_db_open(filename, FALSE)) != 0) {
236 code = _nc_db_first(capdbp, &key, &data);
243 if (_nc_db_have_data(&key, &data, &have, &used)) {
244 if (_nc_read_termtype(<erm, have, used) > 0) {
245 /* only visit things once, by primary name */
246 cn = _nc_first_name(lterm.term_names);
247 /* apply the selected hook function */
248 (*hook) (cn, <erm);
249 _nc_free_termtype(<erm);
252 code = _nc_db_next(capdbp, &key, &data);
255 _nc_db_close(capdbp);
267 (void) printf("#\n#%s:\n#\n", eargv[i]);
269 db_array[0] = eargv[i];
272 if (cgetfirst(&buffer, db_array)) {
273 show_termcap(buffer, hook);
275 while (cgetnext(&buffer, db_array)) {
276 show_termcap(buffer, hook);
282 /* scan termcap text-file only */
283 if (_nc_is_file_path(eargv[i])) {
287 if ((fp = fopen(eargv[i], "r")) != 0) {
288 while (fgets(buffer, sizeof(buffer), fp) != 0) {
291 if (isspace(*buffer))
293 show_termcap(buffer, hook);
302 return (EXIT_SUCCESS);
308 (void) fprintf(stderr, "usage: %s [-ahuUV] [-v n] [file...]\n", _nc_progname);
309 ExitProgram(EXIT_FAILURE);
313 main(int argc, char *argv[])
315 bool all_dirs = FALSE;
316 bool direct_dependencies = FALSE;
317 bool invert_dependencies = FALSE;
319 char *report_file = 0;
322 int this_opt, last_opt = '?';
325 _nc_progname = _nc_rootname(argv[0]);
327 while ((this_opt = getopt(argc, argv, "0123456789ahu:vU:V")) != -1) {
328 /* handle optional parameter */
329 if (isdigit(this_opt)) {
332 v_opt = (this_opt - '0');
335 if (isdigit(last_opt))
339 v_opt += (this_opt - '0');
352 direct_dependencies = TRUE;
353 report_file = optarg;
359 invert_dependencies = TRUE;
360 report_file = optarg;
363 puts(curses_version());
364 ExitProgram(EXIT_SUCCESS);
369 set_trace_level(v_opt);
371 if (report_file != 0) {
372 if (freopen(report_file, "r", stdin) == 0) {
373 (void) fflush(stdout);
374 fprintf(stderr, "%s: can't open %s\n", _nc_progname, report_file);
375 ExitProgram(EXIT_FAILURE);
378 /* parse entries out of the source file */
379 _nc_set_source(report_file);
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);