1 /****************************************************************************
2 * Copyright 2018-2020,2021 Thomas E. Dickey *
3 * Copyright 1998-2013,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1996-on *
34 ****************************************************************************/
37 * toe.c --- table of entries report generator
40 #include <progs.priv.h>
45 #include <hashed_db.h>
48 MODULE_ID("$Id: toe.c,v 1.83 2021/09/21 20:07:51 tom Exp $")
50 #define isDotname(name) (!strcmp(name, ".") || !strcmp(name, ".."))
54 unsigned long checksum;
59 const char *_nc_progname;
61 static TERMDATA *ptr_termdata; /* array of terminal data */
62 static size_t use_termdata; /* actual usage in ptr_termdata[] */
63 static size_t len_termdata; /* allocated size of ptr_termdata[] */
67 static GCC_NORETURN void ExitProgram(int code);
71 _nc_free_entries(_nc_head);
76 static GCC_NORETURN void failed(const char *);
79 failed(const char *msg)
82 ExitProgram(EXIT_FAILURE);
86 strmalloc(const char *value)
88 char *result = strdup(value);
98 size_t want = use_termdata + 1;
100 if (want >= len_termdata) {
101 len_termdata = (2 * want) + 10;
102 ptr_termdata = typeRealloc(TERMDATA, len_termdata, ptr_termdata);
103 if (ptr_termdata == 0)
104 failed("ptr_termdata");
107 return ptr_termdata + use_termdata++;
111 compare_termdata(const void *a, const void *b)
113 const TERMDATA *p = (const TERMDATA *) a;
114 const TERMDATA *q = (const TERMDATA *) b;
115 int result = strcmp(p->term_name, q->term_name);
118 result = (p->db_index - q->db_index);
124 * Sort the array of TERMDATA and print it. If more than one database is being
125 * reported, add a column to show which database has a given entry.
128 show_termdata(int eargc, char **eargv)
136 for (j = 0; j < eargc; ++j) {
139 for (k = 0; k <= j; ++k) {
143 printf("%s\n", eargv[j]);
146 if (use_termdata > 1)
147 qsort(ptr_termdata, use_termdata, sizeof(TERMDATA), compare_termdata);
148 for (n = 0; n < use_termdata; ++n) {
151 * If there is more than one database, show how they differ.
154 unsigned long check = 0;
157 for (; k < ptr_termdata[n].db_index; ++k) {
162 * If this is the first entry, or its checksum differs
163 * from the first entry's checksum, print "*". Otherwise
164 * it looks enough like a duplicate to print "+".
166 printf("%c-", ((check == 0
167 || (check != ptr_termdata[n].checksum))
170 check = ptr_termdata[n].checksum;
173 if ((n + 1) >= use_termdata
174 || strcmp(ptr_termdata[n].term_name,
175 ptr_termdata[n + 1].term_name)) {
180 for (; k < eargc; ++k) {
186 (void) printf("%-10s\t%s\n",
187 ptr_termdata[n].term_name,
188 ptr_termdata[n].description);
196 if (ptr_termdata != 0) {
197 while (use_termdata != 0) {
199 free(ptr_termdata[use_termdata].term_name);
200 free(ptr_termdata[use_termdata].description);
210 allocArgv(size_t count)
212 char **result = typeCalloc(char *, count + 1);
214 failed("realloc eargv");
221 freeArgv(char **argv)
225 while (argv[count]) {
234 make_db_name(char *dst, const char *src, unsigned limit)
236 static const char suffix[] = DBM_SUFFIX;
239 size_t lens = sizeof(suffix) - 1;
240 size_t size = strlen(src);
241 size_t need = lens + size;
245 && !strcmp(src + size - lens, suffix)) {
246 _nc_STRCPY(dst, src, PATH_MAX);
248 _nc_SPRINTF(dst, _nc_SLIMIT(PATH_MAX) "%.*s%s",
249 (int) (PATH_MAX - sizeof(suffix)),
258 typedef void (DescHook) (int /* db_index */ ,
260 const char * /* term_name */ ,
261 TERMTYPE2 * /* term */ );
264 term_description(TERMTYPE2 *tp)
268 if (tp->term_names == 0
269 || (desc = strrchr(tp->term_names, '|')) == 0
270 || (*++desc == '\0')) {
271 desc = "(No description)";
277 /* display a description for the type */
279 deschook(int db_index, int db_limit, const char *term_name, TERMTYPE2 *tp)
283 (void) printf("%-10s\t%s\n", term_name, term_description(tp));
287 string_sum(const char *value)
289 unsigned long result = 0;
291 if ((intptr_t) value == (intptr_t) (-1)) {
295 result += UChar(*value);
303 checksum_of(TERMTYPE2 *tp)
305 unsigned long result = string_sum(tp->term_names);
308 for (i = 0; i < NUM_BOOLEANS(tp); i++) {
309 result += (unsigned long) (tp->Booleans[i]);
311 for (i = 0; i < NUM_NUMBERS(tp); i++) {
312 result += (unsigned long) (tp->Numbers[i]);
314 for (i = 0; i < NUM_STRINGS(tp); i++) {
315 result += string_sum(tp->Strings[i]);
320 /* collect data, to sort before display */
322 sorthook(int db_index, int db_limit, const char *term_name, TERMTYPE2 *tp)
324 TERMDATA *data = new_termdata();
326 data->db_index = db_index;
327 data->checksum = ((db_limit > 1) ? checksum_of(tp) : 0);
328 data->term_name = strmalloc(term_name);
329 data->description = strmalloc(term_description(tp));
332 #if NCURSES_USE_TERMCAP
334 show_termcap(int db_index, int db_limit, char *buffer, DescHook hook)
337 char *next = strchr(buffer, ':');
344 last = strrchr(buffer, '|');
348 memset(&data, 0, sizeof(data));
349 data.term_names = strmalloc(buffer);
350 while ((next = strtok(list, "|")) != 0) {
352 hook(db_index, db_limit, next, &data);
355 free(data.term_names);
359 #if NCURSES_USE_DATABASE
361 copy_entryname(DIRENT * src)
363 size_t len = NAMLEN(src);
364 char *result = malloc(len + 1);
366 failed("copy entryname");
367 memcpy(result, src->d_name, len);
375 typelist(int eargc, char *eargv[],
378 /* apply a function to each entry in given terminfo directories */
382 for (i = 0; i < eargc; i++) {
383 #if NCURSES_USE_DATABASE
384 if (_nc_is_dir_path(eargv[i])) {
389 if ((termdir = opendir(eargv[i])) == 0) {
390 (void) fflush(stdout);
391 (void) fprintf(stderr,
392 "%s: can't open terminfo directory %s\n",
393 _nc_progname, eargv[i]);
398 (void) printf("#\n#%s:\n#\n", eargv[i]);
400 while ((subdir = readdir(termdir)) != 0) {
406 name_1 = copy_entryname(subdir);
407 if (isDotname(name_1)) {
412 cwd_len = NAMLEN(subdir) + strlen(eargv[i]) + 3;
413 cwd_buf = typeRealloc(char, cwd_len, cwd_buf);
415 failed("realloc cwd_buf");
417 assert(cwd_buf != 0);
419 _nc_SPRINTF(cwd_buf, _nc_SLIMIT(cwd_len)
420 "%s/%s/", eargv[i], name_1);
423 if (chdir(cwd_buf) != 0)
426 entrydir = opendir(".");
431 while ((entry = readdir(entrydir)) != 0) {
437 name_2 = copy_entryname(entry);
438 if (isDotname(name_2) || !_nc_is_file_path(name_2)) {
443 status = _nc_read_file_entry(name_2, <erm);
445 (void) fflush(stdout);
446 (void) fprintf(stderr,
447 "%s: couldn't open terminfo file %s.\n",
448 _nc_progname, name_2);
453 /* only visit things once, by primary name */
454 cn = _nc_first_name(lterm.term_names);
455 if (!strcmp(cn, name_2)) {
456 /* apply the selected hook function */
457 hook(i, eargc, cn, <erm);
459 _nc_free_termtype2(<erm);
472 char filename[PATH_MAX];
475 (void) printf("#\n#%s:\n#\n", eargv[i]);
477 if (make_db_name(filename, eargv[i], sizeof(filename))) {
478 if ((capdbp = _nc_db_open(filename, FALSE)) != 0) {
482 code = _nc_db_first(capdbp, &key, &data);
489 if (_nc_db_have_data(&key, &data, &have, &used)) {
490 if (_nc_read_termtype(<erm, have, used) > 0) {
491 /* only visit things once, by primary name */
492 cn = _nc_first_name(lterm.term_names);
493 /* apply the selected hook function */
494 hook(i, eargc, cn, <erm);
495 _nc_free_termtype2(<erm);
498 code = _nc_db_next(capdbp, &key, &data);
501 _nc_db_close(capdbp);
506 #endif /* USE_HASHED_DB */
507 #endif /* NCURSES_USE_DATABASE */
508 #if NCURSES_USE_TERMCAP
511 CGETENT_CONST char *db_array[2];
515 (void) printf("#\n#%s:\n#\n", eargv[i]);
517 db_array[0] = eargv[i];
520 if (cgetfirst(&buffer, db_array) > 0) {
521 show_termcap(i, eargc, buffer, hook);
523 while (cgetnext(&buffer, db_array) > 0) {
524 show_termcap(i, eargc, buffer, hook);
532 /* scan termcap text-file only */
533 if (_nc_is_file_path(eargv[i])) {
538 (void) printf("#\n#%s:\n#\n", eargv[i]);
540 if ((fp = safe_fopen(eargv[i], "r")) != 0) {
541 while (fgets(buffer, sizeof(buffer), fp) != 0) {
544 if (isspace(*buffer))
546 show_termcap(i, eargc, buffer, hook);
555 if (hook == sorthook) {
556 show_termdata(eargc, eargv);
560 return (EXIT_SUCCESS);
566 (void) fprintf(stderr, "usage: %s [-ahsuUV] [-v n] [file...]\n", _nc_progname);
567 ExitProgram(EXIT_FAILURE);
571 main(int argc, char *argv[])
573 bool all_dirs = FALSE;
574 bool direct_dependencies = FALSE;
575 bool invert_dependencies = FALSE;
577 char *report_file = 0;
579 int this_opt, last_opt = '?';
581 DescHook *hook = deschook;
583 _nc_progname = _nc_rootname(argv[0]);
585 while ((this_opt = getopt(argc, argv, "0123456789ahsu:vU:V")) != -1) {
586 /* handle optional parameter */
587 if (isdigit(this_opt)) {
590 v_opt = (unsigned) (this_opt - '0');
593 if (isdigit(last_opt))
597 v_opt += (unsigned) (this_opt - '0');
613 direct_dependencies = TRUE;
614 report_file = optarg;
620 invert_dependencies = TRUE;
621 report_file = optarg;
624 puts(curses_version());
625 ExitProgram(EXIT_SUCCESS);
630 set_trace_level(v_opt);
632 if (report_file != 0) {
633 if (freopen(report_file, "r", stdin) == 0) {
634 (void) fflush(stdout);
635 fprintf(stderr, "%s: can't open %s\n", _nc_progname, report_file);
636 ExitProgram(EXIT_FAILURE);
639 /* parse entries out of the source file */
640 _nc_set_source(report_file);
641 _nc_read_entry_source(stdin, 0, FALSE, FALSE, NULLHOOK);
644 /* maybe we want a direct-dependency listing? */
645 if (direct_dependencies) {
652 (void) printf("%s:", _nc_first_name(qp->tterm.term_names));
653 for (j = 0; j < qp->nuses; j++)
654 (void) printf(" %s", qp->uses[j].name);
659 ExitProgram(EXIT_SUCCESS);
662 /* maybe we want a reverse-dependency listing? */
663 if (invert_dependencies) {
675 for (i = 0; i < rp->nuses; i++)
676 if (_nc_name_match(qp->tterm.term_names,
677 rp->uses[i].name, "|")) {
678 if (matchcount++ == 0)
680 _nc_first_name(qp->tterm.term_names));
682 _nc_first_name(rp->tterm.term_names));
689 ExitProgram(EXIT_SUCCESS);
693 * If we get this far, user wants a simple terminal type listing.
696 code = typelist(argc - optind, argv + optind, header, hook);
697 } else if (all_dirs) {
704 for (pass = 0; pass < 2; ++pass) {
708 _nc_first_db(&state, &offset);
709 while ((path = _nc_next_db(&state, &offset)) != 0) {
710 if (quick_prefix(path))
713 eargv[count] = strmalloc(path);
718 eargv = allocArgv(count);
722 code = typelist((int) count, eargv, header, hook);
730 char **eargv = allocArgv((size_t) 2);
735 _nc_first_db(&state, &offset);
736 if ((path = _nc_next_db(&state, &offset)) != 0) {
737 if (!quick_prefix(path))
738 eargv[count++] = strmalloc(path);
741 code = typelist((int) count, eargv, header, hook);