1 /****************************************************************************
2 * Copyright (c) 1998-2011,2012 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.68 2012/07/21 22:55:59 tom Exp $")
49 #define isDotname(name) (!strcmp(name, ".") || !strcmp(name, ".."))
53 unsigned long checksum;
58 const char *_nc_progname;
60 static TERMDATA *ptr_termdata; /* array of terminal data */
61 static size_t use_termdata; /* actual usage in ptr_termdata[] */
62 static size_t len_termdata; /* allocated size of ptr_termdata[] */
66 static void ExitProgram(int code) GCC_NORETURN;
70 _nc_free_entries(_nc_head);
75 static void failed(const char *) GCC_NORETURN;
78 failed(const char *msg)
81 ExitProgram(EXIT_FAILURE);
85 strmalloc(const char *value)
87 char *result = strdup(value);
97 size_t want = use_termdata + 1;
99 if (want >= len_termdata) {
100 len_termdata = (2 * want) + 10;
101 ptr_termdata = typeRealloc(TERMDATA, len_termdata, ptr_termdata);
104 return ptr_termdata + use_termdata++;
108 compare_termdata(const void *a, const void *b)
110 const TERMDATA *p = (const TERMDATA *) a;
111 const TERMDATA *q = (const TERMDATA *) b;
112 int result = strcmp(p->term_name, q->term_name);
115 result = (p->db_index - q->db_index);
121 * Sort the array of TERMDATA and print it. If more than one database is being
122 * reported, add a column to show which database has a given entry.
125 show_termdata(int eargc, char **eargv)
132 for (j = 0; j < eargc; ++j) {
133 for (k = 0; k <= j; ++k) {
137 printf("%s\n", eargv[j]);
140 if (use_termdata > 1)
141 qsort(ptr_termdata, use_termdata, sizeof(TERMDATA), compare_termdata);
142 for (n = 0; n < use_termdata; ++n) {
145 * If there is more than one database, show how they differ.
148 unsigned long check = 0;
151 for (; k < ptr_termdata[n].db_index; ++k) {
156 * If this is the first entry, or its checksum differs
157 * from the first entry's checksum, print "*". Otherwise
158 * it looks enough like a duplicate to print "+".
160 printf("%c-", ((check == 0
161 || (check != ptr_termdata[n].checksum))
164 check = ptr_termdata[n].checksum;
167 if ((n + 1) >= use_termdata
168 || strcmp(ptr_termdata[n].term_name,
169 ptr_termdata[n + 1].term_name)) {
174 for (; k < eargc; ++k) {
180 (void) printf("%-10s\t%s\n",
181 ptr_termdata[n].term_name,
182 ptr_termdata[n].description);
190 if (ptr_termdata != 0) {
191 while (use_termdata != 0) {
193 free(ptr_termdata[use_termdata].term_name);
194 free(ptr_termdata[use_termdata].description);
204 allocArgv(size_t count)
206 char **result = typeCalloc(char *, count + 1);
208 failed("realloc eargv");
215 freeArgv(char **argv)
219 while (argv[count]) {
228 make_db_name(char *dst, const char *src, unsigned limit)
230 static const char suffix[] = DBM_SUFFIX;
233 unsigned lens = sizeof(suffix) - 1;
234 unsigned size = strlen(src);
235 unsigned need = lens + size;
239 && !strcmp(src + size - lens, suffix)) {
240 _nc_STRCPY(dst, src, PATH_MAX);
242 _nc_SPRINTF(dst, _nc_SLIMIT(PATH_MAX) "%s%s", src, suffix);
250 typedef void (DescHook) (int /* db_index */ ,
252 const char * /* term_name */ ,
253 TERMTYPE * /* term */ );
256 term_description(TERMTYPE *tp)
260 if ((desc = strrchr(tp->term_names, '|')) == 0 || *++desc == '\0')
261 desc = "(No description)";
266 /* display a description for the type */
268 deschook(int db_index, int db_limit, const char *term_name, TERMTYPE *tp)
272 (void) printf("%-10s\t%s\n", term_name, term_description(tp));
276 string_sum(const char *value)
278 unsigned long result = 0;
280 if ((intptr_t) value == (intptr_t) (-1)) {
284 result += UChar(*value);
292 checksum_of(TERMTYPE *tp)
294 unsigned long result = string_sum(tp->term_names);
297 for (i = 0; i < NUM_BOOLEANS(tp); i++) {
298 result += (unsigned long) (tp->Booleans[i]);
300 for (i = 0; i < NUM_NUMBERS(tp); i++) {
301 result += (unsigned long) (tp->Numbers[i]);
303 for (i = 0; i < NUM_STRINGS(tp); i++) {
304 result += string_sum(tp->Strings[i]);
309 /* collect data, to sort before display */
311 sorthook(int db_index, int db_limit, const char *term_name, TERMTYPE *tp)
313 TERMDATA *data = new_termdata();
315 data->db_index = db_index;
316 data->checksum = ((db_limit > 1) ? checksum_of(tp) : 0);
317 data->term_name = strmalloc(term_name);
318 data->description = strmalloc(term_description(tp));
323 show_termcap(int db_index, int db_limit, char *buffer, DescHook hook)
326 char *next = strchr(buffer, ':');
333 last = strrchr(buffer, '|');
337 memset(&data, 0, sizeof(data));
338 data.term_names = strmalloc(buffer);
339 while ((next = strtok(list, "|")) != 0) {
341 hook(db_index, db_limit, next, &data);
344 free(data.term_names);
350 copy_entryname(DIRENT * src)
352 size_t len = NAMLEN(src);
353 char *result = malloc(len + 1);
355 failed("copy entryname");
356 memcpy(result, src->d_name, len);
364 typelist(int eargc, char *eargv[],
367 /* apply a function to each entry in given terminfo directories */
371 for (i = 0; i < eargc; i++) {
373 if (_nc_is_dir_path(eargv[i])) {
378 if ((termdir = opendir(eargv[i])) == 0) {
379 (void) fflush(stdout);
380 (void) fprintf(stderr,
381 "%s: can't open terminfo directory %s\n",
382 _nc_progname, eargv[i]);
387 (void) printf("#\n#%s:\n#\n", eargv[i]);
389 while ((subdir = readdir(termdir)) != 0) {
395 name_1 = copy_entryname(subdir);
396 if (isDotname(name_1)) {
401 cwd_len = NAMLEN(subdir) + strlen(eargv[i]) + 3;
402 cwd_buf = typeRealloc(char, cwd_len, cwd_buf);
404 failed("realloc cwd_buf");
406 assert(cwd_buf != 0);
408 _nc_SPRINTF(cwd_buf, _nc_SLIMIT(cwd_len)
409 "%s/%s/", eargv[i], name_1);
412 if (chdir(cwd_buf) != 0)
415 entrydir = opendir(".");
420 while ((entry = readdir(entrydir)) != 0) {
426 name_2 = copy_entryname(entry);
427 if (isDotname(name_2) || !_nc_is_file_path(name_2)) {
432 status = _nc_read_file_entry(name_2, <erm);
434 (void) fflush(stdout);
435 (void) fprintf(stderr,
436 "%s: couldn't open terminfo file %s.\n",
437 _nc_progname, name_2);
439 return (EXIT_FAILURE);
442 /* only visit things once, by primary name */
443 cn = _nc_first_name(lterm.term_names);
444 if (!strcmp(cn, name_2)) {
445 /* apply the selected hook function */
446 hook(i, eargc, cn, <erm);
448 _nc_free_termtype(<erm);
461 char filename[PATH_MAX];
464 (void) printf("#\n#%s:\n#\n", eargv[i]);
466 if (make_db_name(filename, eargv[i], sizeof(filename))) {
467 if ((capdbp = _nc_db_open(filename, FALSE)) != 0) {
471 code = _nc_db_first(capdbp, &key, &data);
478 if (_nc_db_have_data(&key, &data, &have, &used)) {
479 if (_nc_read_termtype(<erm, have, used) > 0) {
480 /* only visit things once, by primary name */
481 cn = _nc_first_name(lterm.term_names);
482 /* apply the selected hook function */
483 hook(i, eargc, cn, <erm);
484 _nc_free_termtype(<erm);
487 code = _nc_db_next(capdbp, &key, &data);
490 _nc_db_close(capdbp);
500 CGETENT_CONST char *db_array[2];
504 (void) printf("#\n#%s:\n#\n", eargv[i]);
506 db_array[0] = eargv[i];
509 if (cgetfirst(&buffer, db_array) > 0) {
510 show_termcap(i, eargc, buffer, hook);
512 while (cgetnext(&buffer, db_array) > 0) {
513 show_termcap(i, eargc, buffer, hook);
521 /* scan termcap text-file only */
522 if (_nc_is_file_path(eargv[i])) {
527 (void) printf("#\n#%s:\n#\n", eargv[i]);
529 if ((fp = fopen(eargv[i], "r")) != 0) {
530 while (fgets(buffer, sizeof(buffer), fp) != 0) {
533 if (isspace(*buffer))
535 show_termcap(i, eargc, buffer, hook);
544 if (hook == sorthook) {
545 show_termdata(eargc, eargv);
549 return (EXIT_SUCCESS);
555 (void) fprintf(stderr, "usage: %s [-ahsuUV] [-v n] [file...]\n", _nc_progname);
556 ExitProgram(EXIT_FAILURE);
560 main(int argc, char *argv[])
562 bool all_dirs = FALSE;
563 bool direct_dependencies = FALSE;
564 bool invert_dependencies = FALSE;
566 char *report_file = 0;
569 int this_opt, last_opt = '?';
571 DescHook *hook = deschook;
573 _nc_progname = _nc_rootname(argv[0]);
575 while ((this_opt = getopt(argc, argv, "0123456789ahsu:vU:V")) != -1) {
576 /* handle optional parameter */
577 if (isdigit(this_opt)) {
580 v_opt = (unsigned) (this_opt - '0');
583 if (isdigit(last_opt))
587 v_opt += (unsigned) (this_opt - '0');
603 direct_dependencies = TRUE;
604 report_file = optarg;
610 invert_dependencies = TRUE;
611 report_file = optarg;
614 puts(curses_version());
615 ExitProgram(EXIT_SUCCESS);
620 set_trace_level(v_opt);
622 if (report_file != 0) {
623 if (freopen(report_file, "r", stdin) == 0) {
624 (void) fflush(stdout);
625 fprintf(stderr, "%s: can't open %s\n", _nc_progname, report_file);
626 ExitProgram(EXIT_FAILURE);
629 /* parse entries out of the source file */
630 _nc_set_source(report_file);
631 _nc_read_entry_source(stdin, 0, FALSE, FALSE, NULLHOOK);
634 /* maybe we want a direct-dependency listing? */
635 if (direct_dependencies) {
642 (void) printf("%s:", _nc_first_name(qp->tterm.term_names));
643 for (j = 0; j < qp->nuses; j++)
644 (void) printf(" %s", qp->uses[j].name);
649 ExitProgram(EXIT_SUCCESS);
652 /* maybe we want a reverse-dependency listing? */
653 if (invert_dependencies) {
663 for (i = 0; i < rp->nuses; i++)
664 if (_nc_name_match(qp->tterm.term_names,
665 rp->uses[i].name, "|")) {
666 if (matchcount++ == 0)
668 _nc_first_name(qp->tterm.term_names));
670 _nc_first_name(rp->tterm.term_names));
677 ExitProgram(EXIT_SUCCESS);
681 * If we get this far, user wants a simple terminal type listing.
684 code = typelist(argc - optind, argv + optind, header, hook);
685 } else if (all_dirs) {
693 for (pass = 0; pass < 2; ++pass) {
696 _nc_first_db(&state, &offset);
697 while ((path = _nc_next_db(&state, &offset)) != 0) {
699 eargv[count] = strmalloc(path);
704 eargv = allocArgv(count);
706 code = typelist((int) count, eargv, header, hook);
714 char **eargv = allocArgv(2);
717 _nc_first_db(&state, &offset);
718 if ((path = _nc_next_db(&state, &offset)) != 0) {
719 eargv[count++] = strmalloc(path);
722 code = typelist((int) count, eargv, header, hook);