X-Git-Url: http://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=progs%2Ftoe.c;h=afdf379fb2871a114e4856f7ceb2837dbdcbf60c;hp=438d59de0c9d4dbd5bf567052fbbdc0027a9f198;hb=b1f61d9f3aa244512045a6b02e759825d7049d34;hpb=3a9b6a3bf0269231bef7de74757a910dedd04e0c diff --git a/progs/toe.c b/progs/toe.c index 438d59de..afdf379f 100644 --- a/progs/toe.c +++ b/progs/toe.c @@ -1,23 +1,35 @@ - -/*************************************************************************** -* COPYRIGHT NOTICE * -**************************************************************************** -* ncurses is copyright (C) 1992-1995 * -* Zeyd M. Ben-Halim * -* zmbenhal@netcom.com * -* Eric S. Raymond * -* esr@snark.thyrsus.com * -* * -* Permission is hereby granted to reproduce and distribute ncurses * -* by any means and for any fee, whether alone or as part of a * -* larger distribution, in source or in binary form, PROVIDED * -* this notice is included with any such distribution, and is not * -* removed from any of its header files. Mention of ncurses in any * -* applications linked with it is highly appreciated. * -* * -* ncurses comes AS IS with no warranty, implied or expressed. * -* * -***************************************************************************/ +/**************************************************************************** + * Copyright (c) 1998-2000 Free Software Foundation, Inc. * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the * + * "Software"), to deal in the Software without restriction, including * + * without limitation the rights to use, copy, modify, merge, publish, * + * distribute, distribute with modifications, sublicense, and/or sell * + * copies of the Software, and to permit persons to whom the Software is * + * furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included * + * in all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * + * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * + * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * + * * + * Except as contained in this notice, the name(s) of the above copyright * + * holders shall not be used in advertising or otherwise to promote the * + * sale, use or other dealings in this Software without prior written * + * authorization. * + ****************************************************************************/ + +/**************************************************************************** + * Author: Zeyd M. Ben-Halim 1992,1995 * + * and: Eric S. Raymond * + ****************************************************************************/ /* * toe.c --- table of entries report generator @@ -26,44 +38,62 @@ #include +#include + #include #include -MODULE_ID("$Id: toe.c,v 0.15 1997/02/15 18:54:47 tom Exp $") +MODULE_ID("$Id: toe.c,v 1.22 2000/03/11 21:47:35 tom Exp $") + +#define isDotname(name) (!strcmp(name, ".") || !strcmp(name, "..")) const char *_nc_progname; static int typelist(int eargc, char *eargv[], bool, - void (*)(const char *, TERMTYPE *)); + void (*)(const char *, TERMTYPE *)); static void deschook(const char *, TERMTYPE *); #if NO_LEAKS #undef ExitProgram -static void ExitProgram(int code) GCC_NORETURN; -static void ExitProgram(int code) +static void +ExitProgram(int code) GCC_NORETURN; + static void ExitProgram(int code) { - _nc_free_entries(_nc_head); - _nc_leaks_dump_entry(); - _nc_free_and_exit(code); + _nc_free_entries(_nc_head); + _nc_leaks_dump_entry(); + _nc_free_and_exit(code); } #endif -int main (int argc, char *argv[]) +static char * +get_directory(char *path) +{ + if (path != 0) { + struct stat sb; + if (stat(path, &sb) != 0 + || (sb.st_mode & S_IFMT) != S_IFDIR + || access(path, R_OK | X_OK) != 0) + path = 0; + } + return path; +} + +int +main(int argc, char *argv[]) { - bool direct_dependencies = FALSE; - bool invert_dependencies = FALSE; - bool header = FALSE; - int i, c, debug_level = 0; - int code; + bool direct_dependencies = FALSE; + bool invert_dependencies = FALSE; + bool header = FALSE; + int i, c; + int code; - if ((_nc_progname = strrchr(argv[0], '/')) == NULL) + if ((_nc_progname = strrchr(argv[0], '/')) == 0) _nc_progname = argv[0]; else _nc_progname++; while ((c = getopt(argc, argv, "huv:UV")) != EOF) - switch (c) - { + switch (c) { case 'h': header = TRUE; break; @@ -71,8 +101,7 @@ int main (int argc, char *argv[]) direct_dependencies = TRUE; break; case 'v': - debug_level = atoi(optarg); - _nc_tracing = (1 << debug_level) - 1; + set_trace_level(atoi(optarg)); break; case 'U': invert_dependencies = TRUE; @@ -82,66 +111,58 @@ int main (int argc, char *argv[]) putchar('\n'); ExitProgram(EXIT_SUCCESS); default: - (void) fprintf (stderr, "usage: toe [-huUV] [-v n] [file...]\n"); + (void) fprintf(stderr, "usage: toe [-huUV] [-v n] [file...]\n"); ExitProgram(EXIT_FAILURE); } - if (direct_dependencies || invert_dependencies) - { - if (freopen(argv[optind], "r", stdin) == NULL) - { + if (direct_dependencies || invert_dependencies) { + if (freopen(argv[optind], "r", stdin) == 0) { + (void) fflush(stdout); fprintf(stderr, "%s: can't open %s\n", _nc_progname, argv[optind]); ExitProgram(EXIT_FAILURE); } /* parse entries out of the source file */ _nc_set_source(argv[optind]); - _nc_read_entry_source(stdin, (char *)NULL, - FALSE, FALSE, - NULLHOOK); + _nc_read_entry_source(stdin, 0, FALSE, FALSE, NULLHOOK); } /* maybe we want a direct-dependency listing? */ - if (direct_dependencies) - { - ENTRY *qp; + if (direct_dependencies) { + ENTRY *qp; for_entry_list(qp) - if (qp->nuses) - { - int j; + if (qp->nuses) { + int j; - (void) printf("%s:", _nc_first_name(qp->tterm.term_names)); - for (j = 0; j < qp->nuses; j++) - (void) printf(" %s", (char *)(qp->uses[j].parent)); - putchar('\n'); - } + (void) printf("%s:", _nc_first_name(qp->tterm.term_names)); + for (j = 0; j < qp->nuses; j++) + (void) printf(" %s", qp->uses[j].name); + putchar('\n'); + } ExitProgram(EXIT_SUCCESS); } /* maybe we want a reverse-dependency listing? */ - if (invert_dependencies) - { - ENTRY *qp, *rp; - int matchcount; + if (invert_dependencies) { + ENTRY *qp, *rp; + int matchcount; - for_entry_list(qp) - { + for_entry_list(qp) { matchcount = 0; - for_entry_list(rp) - { + for_entry_list(rp) { if (rp->nuses == 0) continue; for (i = 0; i < rp->nuses; i++) - if (_nc_name_match(qp->tterm.term_names,(char*)rp->uses[i].parent, "|")) - { + if (_nc_name_match(qp->tterm.term_names, + rp->uses[i].name, "|")) { if (matchcount++ == 0) (void) printf("%s:", - _nc_first_name(qp->tterm.term_names)); + _nc_first_name(qp->tterm.term_names)); (void) printf(" %s", - _nc_first_name(rp->tterm.term_names)); + _nc_first_name(rp->tterm.term_names)); } } if (matchcount) @@ -155,27 +176,25 @@ int main (int argc, char *argv[]) * If we get this far, user wants a simple terminal type listing. */ if (optind < argc) { - code = typelist(argc-optind, argv+optind, header, deschook); + code = typelist(argc - optind, argv + optind, header, deschook); } else { - char *explicit, *home, *eargv[3]; - int j; + char *home, *eargv[3]; + char personal[PATH_MAX]; + int j; j = 0; - if ((explicit = getenv("TERMINFO")) != (char *)NULL) - eargv[j++] = explicit; - else - { - if ((home = getenv("HOME")) != (char *)NULL) - { - char personal[PATH_MAX]; - + if ((eargv[j] = get_directory(getenv("TERMINFO"))) != 0) { + j++; + } else { + if ((home = getenv("HOME")) != 0) { (void) sprintf(personal, PRIVATE_INFO, home); - if (access(personal, F_OK) == 0) - eargv[j++] = personal; + if ((eargv[j] = get_directory(personal)) != 0) + j++; } - eargv[j++] = TERMINFO; + if ((eargv[j] = get_directory(TERMINFO)) != 0) + j++; } - eargv[j] = (char *)NULL; + eargv[j] = 0; code = typelist(j, eargv, header, deschook); } @@ -183,12 +202,13 @@ int main (int argc, char *argv[]) ExitProgram(code); } -static void deschook(const char *cn, TERMTYPE *tp) +static void +deschook(const char *cn, TERMTYPE * tp) /* display a description for the type */ { const char *desc; - if ((desc = strrchr(tp->term_names, '|')) == (char *)NULL) + if ((desc = strrchr(tp->term_names, '|')) == 0) desc = "(No description)"; else ++desc; @@ -196,83 +216,74 @@ static void deschook(const char *cn, TERMTYPE *tp) (void) printf("%-10s\t%s\n", cn, desc); } -static int typelist(int eargc, char *eargv[], - bool verbosity, - void (*hook)(const char *, TERMTYPE *tp)) +static int +typelist(int eargc, char *eargv[], + bool verbosity, + void (*hook) (const char *, TERMTYPE * tp)) /* apply a function to each entry in given terminfo directories */ { - int i; + int i; - for (i = 0; i < eargc; i++) - { - DIR *termdir; + for (i = 0; i < eargc; i++) { + DIR *termdir; struct dirent *subdir; - if ((termdir = opendir(eargv[i])) == (DIR *)NULL) - { + if ((termdir = opendir(eargv[i])) == 0) { + (void) fflush(stdout); (void) fprintf(stderr, - "%s: can't open terminfo directory %s\n", - _nc_progname, eargv[i]); - return(EXIT_FAILURE); - } - else if (verbosity) + "%s: can't open terminfo directory %s\n", + _nc_progname, eargv[i]); + return (EXIT_FAILURE); + } else if (verbosity) (void) printf("#\n#%s:\n#\n", eargv[i]); - while ((subdir = readdir(termdir)) != NULL) - { - size_t len = NAMLEN(subdir); - char buf[PATH_MAX]; - char name_1[PATH_MAX]; - DIR *entrydir; + while ((subdir = readdir(termdir)) != 0) { + size_t len = NAMLEN(subdir); + char buf[PATH_MAX]; + char name_1[PATH_MAX]; + DIR *entrydir; struct dirent *entry; strncpy(name_1, subdir->d_name, len)[len] = '\0'; - if (!strcmp(name_1, ".") - || !strcmp(name_1, "..")) + if (isDotname(name_1)) continue; - (void) strcpy(buf, eargv[i]); - (void) strcat(buf, "/"); - (void) strcat(buf, name_1); - (void) strcat(buf, "/"); + (void) sprintf(buf, "%s/%s/", eargv[i], name_1); chdir(buf); entrydir = opendir("."); - while ((entry = readdir(entrydir)) != NULL) - { - char name_2[PATH_MAX]; - TERMTYPE lterm; - char *cn; - int status; + while ((entry = readdir(entrydir)) != 0) { + char name_2[PATH_MAX]; + TERMTYPE lterm; + char *cn; + int status; len = NAMLEN(entry); strncpy(name_2, entry->d_name, len)[len] = '\0'; - if (!strcmp(name_2, ".") - || !strcmp(name_2, "..")) + if (isDotname(name_2)) continue; status = _nc_read_file_entry(name_2, <erm); - if (status <= 0) - { + if (status <= 0) { + (void) fflush(stdout); (void) fprintf(stderr, - "toe: couldn't open terminfo file %s.\n", - name_2); - return(EXIT_FAILURE); + "toe: couldn't open terminfo file %s.\n", + name_2); + return (EXIT_FAILURE); } /* only visit things once, by primary name */ cn = _nc_first_name(lterm.term_names); - if (!strcmp(cn, name_2)) - { + if (!strcmp(cn, name_2)) { /* apply the selected hook function */ - (*hook)(cn, <erm); + (*hook) (cn, <erm); } if (lterm.term_names) { free(lterm.term_names); - lterm.term_names = NULL; + lterm.term_names = 0; } if (lterm.str_table) { free(lterm.str_table); - lterm.str_table = NULL; + lterm.str_table = 0; } } closedir(entrydir); @@ -280,5 +291,5 @@ static int typelist(int eargc, char *eargv[], closedir(termdir); } - return(EXIT_SUCCESS); + return (EXIT_SUCCESS); }