1 /****************************************************************************
2 * Copyright 2018,2020 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.79 2020/02/02 23:34:34 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 void ExitProgram(int code) GCC_NORETURN;
71 _nc_free_entries(_nc_head);
76 static void failed(const char *) GCC_NORETURN;
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)
135 for (j = 0; j < eargc; ++j) {
136 for (k = 0; k <= j; ++k) {
140 printf("%s\n", eargv[j]);
143 if (use_termdata > 1)
144 qsort(ptr_termdata, use_termdata, sizeof(TERMDATA), compare_termdata);
145 for (n = 0; n < use_termdata; ++n) {
148 * If there is more than one database, show how they differ.
151 unsigned long check = 0;
154 for (; k < ptr_termdata[n].db_index; ++k) {
159 * If this is the first entry, or its checksum differs
160 * from the first entry's checksum, print "*". Otherwise
161 * it looks enough like a duplicate to print "+".
163 printf("%c-", ((check == 0
164 || (check != ptr_termdata[n].checksum))
167 check = ptr_termdata[n].checksum;
170 if ((n + 1) >= use_termdata
171 || strcmp(ptr_termdata[n].term_name,
172 ptr_termdata[n + 1].term_name)) {
177 for (; k < eargc; ++k) {
183 (void) printf("%-10s\t%s\n",
184 ptr_termdata[n].term_name,
185 ptr_termdata[n].description);
193 if (ptr_termdata != 0) {
194 while (use_termdata != 0) {
196 free(ptr_termdata[use_termdata].term_name);
197 free(ptr_termdata[use_termdata].description);
207 allocArgv(size_t count)
209 char **result = typeCalloc(char *, count + 1);
211 failed("realloc eargv");
218 freeArgv(char **argv)
222 while (argv[count]) {
231 make_db_name(char *dst, const char *src, unsigned limit)
233 static const char suffix[] = DBM_SUFFIX;
236 size_t lens = sizeof(suffix) - 1;
237 size_t size = strlen(src);
238 size_t need = lens + size;
242 && !strcmp(src + size - lens, suffix)) {
243 _nc_STRCPY(dst, src, PATH_MAX);
245 _nc_SPRINTF(dst, _nc_SLIMIT(PATH_MAX) "%s%s", src, suffix);
253 typedef void (DescHook) (int /* db_index */ ,
255 const char * /* term_name */ ,
256 TERMTYPE2 * /* term */ );
259 term_description(TERMTYPE2 *tp)
263 if (tp->term_names == 0
264 || (desc = strrchr(tp->term_names, '|')) == 0
265 || (*++desc == '\0')) {
266 desc = "(No description)";
272 /* display a description for the type */
274 deschook(int db_index, int db_limit, const char *term_name, TERMTYPE2 *tp)
278 (void) printf("%-10s\t%s\n", term_name, term_description(tp));
282 string_sum(const char *value)
284 unsigned long result = 0;
286 if ((intptr_t) value == (intptr_t) (-1)) {
290 result += UChar(*value);
298 checksum_of(TERMTYPE2 *tp)
300 unsigned long result = string_sum(tp->term_names);
303 for (i = 0; i < NUM_BOOLEANS(tp); i++) {
304 result += (unsigned long) (tp->Booleans[i]);
306 for (i = 0; i < NUM_NUMBERS(tp); i++) {
307 result += (unsigned long) (tp->Numbers[i]);
309 for (i = 0; i < NUM_STRINGS(tp); i++) {
310 result += string_sum(tp->Strings[i]);
315 /* collect data, to sort before display */
317 sorthook(int db_index, int db_limit, const char *term_name, TERMTYPE2 *tp)
319 TERMDATA *data = new_termdata();
321 data->db_index = db_index;
322 data->checksum = ((db_limit > 1) ? checksum_of(tp) : 0);
323 data->term_name = strmalloc(term_name);
324 data->description = strmalloc(term_description(tp));
327 #if NCURSES_USE_TERMCAP
329 show_termcap(int db_index, int db_limit, char *buffer, DescHook hook)
332 char *next = strchr(buffer, ':');
339 last = strrchr(buffer, '|');
343 memset(&data, 0, sizeof(data));
344 data.term_names = strmalloc(buffer);
345 while ((next = strtok(list, "|")) != 0) {
347 hook(db_index, db_limit, next, &data);
350 free(data.term_names);
354 #if NCURSES_USE_DATABASE
356 copy_entryname(DIRENT * src)
358 size_t len = NAMLEN(src);
359 char *result = malloc(len + 1);
361 failed("copy entryname");
362 memcpy(result, src->d_name, len);
370 typelist(int eargc, char *eargv[],
373 /* apply a function to each entry in given terminfo directories */
377 for (i = 0; i < eargc; i++) {
378 #if NCURSES_USE_DATABASE
379 if (_nc_is_dir_path(eargv[i])) {
384 if ((termdir = opendir(eargv[i])) == 0) {
385 (void) fflush(stdout);
386 (void) fprintf(stderr,
387 "%s: can't open terminfo directory %s\n",
388 _nc_progname, eargv[i]);
393 (void) printf("#\n#%s:\n#\n", eargv[i]);
395 while ((subdir = readdir(termdir)) != 0) {
401 name_1 = copy_entryname(subdir);
402 if (isDotname(name_1)) {
407 cwd_len = NAMLEN(subdir) + strlen(eargv[i]) + 3;
408 cwd_buf = typeRealloc(char, cwd_len, cwd_buf);
410 failed("realloc cwd_buf");
412 assert(cwd_buf != 0);
414 _nc_SPRINTF(cwd_buf, _nc_SLIMIT(cwd_len)
415 "%s/%s/", eargv[i], name_1);
418 if (chdir(cwd_buf) != 0)
421 entrydir = opendir(".");
426 while ((entry = readdir(entrydir)) != 0) {
432 name_2 = copy_entryname(entry);
433 if (isDotname(name_2) || !_nc_is_file_path(name_2)) {
438 status = _nc_read_file_entry(name_2, <erm);
440 (void) fflush(stdout);
441 (void) fprintf(stderr,
442 "%s: couldn't open terminfo file %s.\n",
443 _nc_progname, name_2);
448 /* only visit things once, by primary name */
449 cn = _nc_first_name(lterm.term_names);
450 if (!strcmp(cn, name_2)) {
451 /* apply the selected hook function */
452 hook(i, eargc, cn, <erm);
454 _nc_free_termtype2(<erm);
467 char filename[PATH_MAX];
470 (void) printf("#\n#%s:\n#\n", eargv[i]);
472 if (make_db_name(filename, eargv[i], sizeof(filename))) {
473 if ((capdbp = _nc_db_open(filename, FALSE)) != 0) {
477 code = _nc_db_first(capdbp, &key, &data);
484 if (_nc_db_have_data(&key, &data, &have, &used)) {
485 if (_nc_read_termtype(<erm, have, used) > 0) {
486 /* only visit things once, by primary name */
487 cn = _nc_first_name(lterm.term_names);
488 /* apply the selected hook function */
489 hook(i, eargc, cn, <erm);
490 _nc_free_termtype2(<erm);
493 code = _nc_db_next(capdbp, &key, &data);
496 _nc_db_close(capdbp);
501 #endif /* USE_HASHED_DB */
502 #endif /* NCURSES_USE_DATABASE */
503 #if NCURSES_USE_TERMCAP
506 CGETENT_CONST char *db_array[2];
510 (void) printf("#\n#%s:\n#\n", eargv[i]);
512 db_array[0] = eargv[i];
515 if (cgetfirst(&buffer, db_array) > 0) {
516 show_termcap(i, eargc, buffer, hook);
518 while (cgetnext(&buffer, db_array) > 0) {
519 show_termcap(i, eargc, buffer, hook);
527 /* scan termcap text-file only */
528 if (_nc_is_file_path(eargv[i])) {
533 (void) printf("#\n#%s:\n#\n", eargv[i]);
535 if ((fp = fopen(eargv[i], "r")) != 0) {
536 while (fgets(buffer, sizeof(buffer), fp) != 0) {
539 if (isspace(*buffer))
541 show_termcap(i, eargc, buffer, hook);
550 if (hook == sorthook) {
551 show_termdata(eargc, eargv);
555 return (EXIT_SUCCESS);
561 (void) fprintf(stderr, "usage: %s [-ahsuUV] [-v n] [file...]\n", _nc_progname);
562 ExitProgram(EXIT_FAILURE);
566 main(int argc, char *argv[])
568 bool all_dirs = FALSE;
569 bool direct_dependencies = FALSE;
570 bool invert_dependencies = FALSE;
572 char *report_file = 0;
575 int this_opt, last_opt = '?';
577 DescHook *hook = deschook;
579 _nc_progname = _nc_rootname(argv[0]);
581 while ((this_opt = getopt(argc, argv, "0123456789ahsu:vU:V")) != -1) {
582 /* handle optional parameter */
583 if (isdigit(this_opt)) {
586 v_opt = (unsigned) (this_opt - '0');
589 if (isdigit(last_opt))
593 v_opt += (unsigned) (this_opt - '0');
609 direct_dependencies = TRUE;
610 report_file = optarg;
616 invert_dependencies = TRUE;
617 report_file = optarg;
620 puts(curses_version());
621 ExitProgram(EXIT_SUCCESS);
626 set_trace_level(v_opt);
628 if (report_file != 0) {
629 if (freopen(report_file, "r", stdin) == 0) {
630 (void) fflush(stdout);
631 fprintf(stderr, "%s: can't open %s\n", _nc_progname, report_file);
632 ExitProgram(EXIT_FAILURE);
635 /* parse entries out of the source file */
636 _nc_set_source(report_file);
637 _nc_read_entry_source(stdin, 0, FALSE, FALSE, NULLHOOK);
640 /* maybe we want a direct-dependency listing? */
641 if (direct_dependencies) {
648 (void) printf("%s:", _nc_first_name(qp->tterm.term_names));
649 for (j = 0; j < qp->nuses; j++)
650 (void) printf(" %s", qp->uses[j].name);
655 ExitProgram(EXIT_SUCCESS);
658 /* maybe we want a reverse-dependency listing? */
659 if (invert_dependencies) {
669 for (i = 0; i < rp->nuses; i++)
670 if (_nc_name_match(qp->tterm.term_names,
671 rp->uses[i].name, "|")) {
672 if (matchcount++ == 0)
674 _nc_first_name(qp->tterm.term_names));
676 _nc_first_name(rp->tterm.term_names));
683 ExitProgram(EXIT_SUCCESS);
687 * If we get this far, user wants a simple terminal type listing.
690 code = typelist(argc - optind, argv + optind, header, hook);
691 } else if (all_dirs) {
699 for (pass = 0; pass < 2; ++pass) {
702 _nc_first_db(&state, &offset);
703 while ((path = _nc_next_db(&state, &offset)) != 0) {
704 if (quick_prefix(path))
707 eargv[count] = strmalloc(path);
712 eargv = allocArgv(count);
716 code = typelist((int) count, eargv, header, hook);
724 char **eargv = allocArgv((size_t) 2);
729 _nc_first_db(&state, &offset);
730 if ((path = _nc_next_db(&state, &offset)) != 0) {
731 if (!quick_prefix(path))
732 eargv[count++] = strmalloc(path);
735 code = typelist((int) count, eargv, header, hook);