1 /****************************************************************************
2 * Copyright (c) 1998-2013,2017 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.77 2017/12/23 19:23:40 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);
102 if (ptr_termdata == 0)
103 failed("ptr_termdata");
106 return ptr_termdata + use_termdata++;
110 compare_termdata(const void *a, const void *b)
112 const TERMDATA *p = (const TERMDATA *) a;
113 const TERMDATA *q = (const TERMDATA *) b;
114 int result = strcmp(p->term_name, q->term_name);
117 result = (p->db_index - q->db_index);
123 * Sort the array of TERMDATA and print it. If more than one database is being
124 * reported, add a column to show which database has a given entry.
127 show_termdata(int eargc, char **eargv)
134 for (j = 0; j < eargc; ++j) {
135 for (k = 0; k <= j; ++k) {
139 printf("%s\n", eargv[j]);
142 if (use_termdata > 1)
143 qsort(ptr_termdata, use_termdata, sizeof(TERMDATA), compare_termdata);
144 for (n = 0; n < use_termdata; ++n) {
147 * If there is more than one database, show how they differ.
150 unsigned long check = 0;
153 for (; k < ptr_termdata[n].db_index; ++k) {
158 * If this is the first entry, or its checksum differs
159 * from the first entry's checksum, print "*". Otherwise
160 * it looks enough like a duplicate to print "+".
162 printf("%c-", ((check == 0
163 || (check != ptr_termdata[n].checksum))
166 check = ptr_termdata[n].checksum;
169 if ((n + 1) >= use_termdata
170 || strcmp(ptr_termdata[n].term_name,
171 ptr_termdata[n + 1].term_name)) {
176 for (; k < eargc; ++k) {
182 (void) printf("%-10s\t%s\n",
183 ptr_termdata[n].term_name,
184 ptr_termdata[n].description);
192 if (ptr_termdata != 0) {
193 while (use_termdata != 0) {
195 free(ptr_termdata[use_termdata].term_name);
196 free(ptr_termdata[use_termdata].description);
206 allocArgv(size_t count)
208 char **result = typeCalloc(char *, count + 1);
210 failed("realloc eargv");
217 freeArgv(char **argv)
221 while (argv[count]) {
230 make_db_name(char *dst, const char *src, unsigned limit)
232 static const char suffix[] = DBM_SUFFIX;
235 size_t lens = sizeof(suffix) - 1;
236 size_t size = strlen(src);
237 size_t need = lens + size;
241 && !strcmp(src + size - lens, suffix)) {
242 _nc_STRCPY(dst, src, PATH_MAX);
244 _nc_SPRINTF(dst, _nc_SLIMIT(PATH_MAX) "%s%s", src, suffix);
252 typedef void (DescHook) (int /* db_index */ ,
254 const char * /* term_name */ ,
255 TERMTYPE2 * /* term */ );
258 term_description(TERMTYPE2 *tp)
262 if (tp->term_names == 0
263 || (desc = strrchr(tp->term_names, '|')) == 0
264 || (*++desc == '\0')) {
265 desc = "(No description)";
271 /* display a description for the type */
273 deschook(int db_index, int db_limit, const char *term_name, TERMTYPE2 *tp)
277 (void) printf("%-10s\t%s\n", term_name, term_description(tp));
281 string_sum(const char *value)
283 unsigned long result = 0;
285 if ((intptr_t) value == (intptr_t) (-1)) {
289 result += UChar(*value);
297 checksum_of(TERMTYPE2 *tp)
299 unsigned long result = string_sum(tp->term_names);
302 for (i = 0; i < NUM_BOOLEANS(tp); i++) {
303 result += (unsigned long) (tp->Booleans[i]);
305 for (i = 0; i < NUM_NUMBERS(tp); i++) {
306 result += (unsigned long) (tp->Numbers[i]);
308 for (i = 0; i < NUM_STRINGS(tp); i++) {
309 result += string_sum(tp->Strings[i]);
314 /* collect data, to sort before display */
316 sorthook(int db_index, int db_limit, const char *term_name, TERMTYPE2 *tp)
318 TERMDATA *data = new_termdata();
320 data->db_index = db_index;
321 data->checksum = ((db_limit > 1) ? checksum_of(tp) : 0);
322 data->term_name = strmalloc(term_name);
323 data->description = strmalloc(term_description(tp));
326 #if NCURSES_USE_TERMCAP
328 show_termcap(int db_index, int db_limit, char *buffer, DescHook hook)
331 char *next = strchr(buffer, ':');
338 last = strrchr(buffer, '|');
342 memset(&data, 0, sizeof(data));
343 data.term_names = strmalloc(buffer);
344 while ((next = strtok(list, "|")) != 0) {
346 hook(db_index, db_limit, next, &data);
349 free(data.term_names);
353 #if NCURSES_USE_DATABASE
355 copy_entryname(DIRENT * src)
357 size_t len = NAMLEN(src);
358 char *result = malloc(len + 1);
360 failed("copy entryname");
361 memcpy(result, src->d_name, len);
369 typelist(int eargc, char *eargv[],
372 /* apply a function to each entry in given terminfo directories */
376 for (i = 0; i < eargc; i++) {
377 #if NCURSES_USE_DATABASE
378 if (_nc_is_dir_path(eargv[i])) {
383 if ((termdir = opendir(eargv[i])) == 0) {
384 (void) fflush(stdout);
385 (void) fprintf(stderr,
386 "%s: can't open terminfo directory %s\n",
387 _nc_progname, eargv[i]);
392 (void) printf("#\n#%s:\n#\n", eargv[i]);
394 while ((subdir = readdir(termdir)) != 0) {
400 name_1 = copy_entryname(subdir);
401 if (isDotname(name_1)) {
406 cwd_len = NAMLEN(subdir) + strlen(eargv[i]) + 3;
407 cwd_buf = typeRealloc(char, cwd_len, cwd_buf);
409 failed("realloc cwd_buf");
411 assert(cwd_buf != 0);
413 _nc_SPRINTF(cwd_buf, _nc_SLIMIT(cwd_len)
414 "%s/%s/", eargv[i], name_1);
417 if (chdir(cwd_buf) != 0)
420 entrydir = opendir(".");
425 while ((entry = readdir(entrydir)) != 0) {
431 name_2 = copy_entryname(entry);
432 if (isDotname(name_2) || !_nc_is_file_path(name_2)) {
437 status = _nc_read_file_entry(name_2, <erm);
439 (void) fflush(stdout);
440 (void) fprintf(stderr,
441 "%s: couldn't open terminfo file %s.\n",
442 _nc_progname, name_2);
447 /* only visit things once, by primary name */
448 cn = _nc_first_name(lterm.term_names);
449 if (!strcmp(cn, name_2)) {
450 /* apply the selected hook function */
451 hook(i, eargc, cn, <erm);
453 _nc_free_termtype2(<erm);
466 char filename[PATH_MAX];
469 (void) printf("#\n#%s:\n#\n", eargv[i]);
471 if (make_db_name(filename, eargv[i], sizeof(filename))) {
472 if ((capdbp = _nc_db_open(filename, FALSE)) != 0) {
476 code = _nc_db_first(capdbp, &key, &data);
483 if (_nc_db_have_data(&key, &data, &have, &used)) {
484 if (_nc_read_termtype(<erm, have, used) > 0) {
485 /* only visit things once, by primary name */
486 cn = _nc_first_name(lterm.term_names);
487 /* apply the selected hook function */
488 hook(i, eargc, cn, <erm);
489 _nc_free_termtype2(<erm);
492 code = _nc_db_next(capdbp, &key, &data);
495 _nc_db_close(capdbp);
502 #if NCURSES_USE_TERMCAP
505 CGETENT_CONST char *db_array[2];
509 (void) printf("#\n#%s:\n#\n", eargv[i]);
511 db_array[0] = eargv[i];
514 if (cgetfirst(&buffer, db_array) > 0) {
515 show_termcap(i, eargc, buffer, hook);
517 while (cgetnext(&buffer, db_array) > 0) {
518 show_termcap(i, eargc, buffer, hook);
526 /* scan termcap text-file only */
527 if (_nc_is_file_path(eargv[i])) {
532 (void) printf("#\n#%s:\n#\n", eargv[i]);
534 if ((fp = fopen(eargv[i], "r")) != 0) {
535 while (fgets(buffer, sizeof(buffer), fp) != 0) {
538 if (isspace(*buffer))
540 show_termcap(i, eargc, buffer, hook);
549 if (hook == sorthook) {
550 show_termdata(eargc, eargv);
554 return (EXIT_SUCCESS);
560 (void) fprintf(stderr, "usage: %s [-ahsuUV] [-v n] [file...]\n", _nc_progname);
561 ExitProgram(EXIT_FAILURE);
565 main(int argc, char *argv[])
567 bool all_dirs = FALSE;
568 bool direct_dependencies = FALSE;
569 bool invert_dependencies = FALSE;
571 char *report_file = 0;
574 int this_opt, last_opt = '?';
576 DescHook *hook = deschook;
578 _nc_progname = _nc_rootname(argv[0]);
580 while ((this_opt = getopt(argc, argv, "0123456789ahsu:vU:V")) != -1) {
581 /* handle optional parameter */
582 if (isdigit(this_opt)) {
585 v_opt = (unsigned) (this_opt - '0');
588 if (isdigit(last_opt))
592 v_opt += (unsigned) (this_opt - '0');
608 direct_dependencies = TRUE;
609 report_file = optarg;
615 invert_dependencies = TRUE;
616 report_file = optarg;
619 puts(curses_version());
620 ExitProgram(EXIT_SUCCESS);
625 set_trace_level(v_opt);
627 if (report_file != 0) {
628 if (freopen(report_file, "r", stdin) == 0) {
629 (void) fflush(stdout);
630 fprintf(stderr, "%s: can't open %s\n", _nc_progname, report_file);
631 ExitProgram(EXIT_FAILURE);
634 /* parse entries out of the source file */
635 _nc_set_source(report_file);
636 _nc_read_entry_source(stdin, 0, FALSE, FALSE, NULLHOOK);
639 /* maybe we want a direct-dependency listing? */
640 if (direct_dependencies) {
647 (void) printf("%s:", _nc_first_name(qp->tterm.term_names));
648 for (j = 0; j < qp->nuses; j++)
649 (void) printf(" %s", qp->uses[j].name);
654 ExitProgram(EXIT_SUCCESS);
657 /* maybe we want a reverse-dependency listing? */
658 if (invert_dependencies) {
668 for (i = 0; i < rp->nuses; i++)
669 if (_nc_name_match(qp->tterm.term_names,
670 rp->uses[i].name, "|")) {
671 if (matchcount++ == 0)
673 _nc_first_name(qp->tterm.term_names));
675 _nc_first_name(rp->tterm.term_names));
682 ExitProgram(EXIT_SUCCESS);
686 * If we get this far, user wants a simple terminal type listing.
689 code = typelist(argc - optind, argv + optind, header, hook);
690 } else if (all_dirs) {
698 for (pass = 0; pass < 2; ++pass) {
701 _nc_first_db(&state, &offset);
702 while ((path = _nc_next_db(&state, &offset)) != 0) {
704 eargv[count] = strmalloc(path);
709 eargv = allocArgv(count);
713 code = typelist((int) count, eargv, header, hook);
721 char **eargv = allocArgv((size_t) 2);
726 _nc_first_db(&state, &offset);
727 if ((path = _nc_next_db(&state, &offset)) != 0) {
728 eargv[count++] = strmalloc(path);
731 code = typelist((int) count, eargv, header, hook);