]> ncurses.scripts.mit.edu Git - ncurses.git/blob - progs/toe.c
ncurses 4.2
[ncurses.git] / progs / toe.c
1 /****************************************************************************
2  * Copyright (c) 1998 Free Software Foundation, Inc.                        *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  ****************************************************************************/
33
34 /*
35  *      toe.c --- table of entries report generator
36  *
37  */
38
39 #include <progs.priv.h>
40
41 #include <dump_entry.h>
42 #include <term_entry.h>
43
44 MODULE_ID("$Id: toe.c,v 0.18 1998/02/11 12:14:02 tom Exp $")
45
46 const char *_nc_progname;
47
48 static int typelist(int eargc, char *eargv[], bool,
49                      void (*)(const char *, TERMTYPE *));
50 static void deschook(const char *, TERMTYPE *);
51
52 #if NO_LEAKS
53 #undef ExitProgram
54 static void ExitProgram(int code) GCC_NORETURN;
55 static void ExitProgram(int code)
56 {
57         _nc_free_entries(_nc_head);
58         _nc_leaks_dump_entry();
59         _nc_free_and_exit(code);
60 }
61 #endif
62
63 int main (int argc, char *argv[])
64 {
65     bool        direct_dependencies = FALSE;
66     bool        invert_dependencies = FALSE;
67     bool        header = FALSE;
68     int         i, c, debug_level = 0;
69     int         code;
70
71     if ((_nc_progname = strrchr(argv[0], '/')) == NULL)
72         _nc_progname = argv[0];
73     else
74         _nc_progname++;
75
76     while ((c = getopt(argc, argv, "huv:UV")) != EOF)
77         switch (c)
78         {
79         case 'h':
80             header = TRUE;
81             break;
82         case 'u':
83             direct_dependencies = TRUE;
84             break;
85         case 'v':
86             debug_level = atoi(optarg);
87             _nc_tracing = (1 << debug_level) - 1;
88             break;
89         case 'U':
90             invert_dependencies = TRUE;
91             break;
92         case 'V':
93             (void) fputs(NCURSES_VERSION, stdout);
94             putchar('\n');
95             ExitProgram(EXIT_SUCCESS);
96         default:
97             (void) fprintf (stderr, "usage: toe [-huUV] [-v n] [file...]\n");
98             ExitProgram(EXIT_FAILURE);
99         }
100
101     if (direct_dependencies || invert_dependencies)
102     {
103         if (freopen(argv[optind], "r", stdin) == NULL)
104         {
105             (void) fflush(stdout);
106             fprintf(stderr, "%s: can't open %s\n", _nc_progname, argv[optind]);
107             ExitProgram(EXIT_FAILURE);
108         }
109
110         /* parse entries out of the source file */
111         _nc_set_source(argv[optind]);
112         _nc_read_entry_source(stdin, (char *)NULL,
113                               FALSE, FALSE,
114                               NULLHOOK);
115     }
116
117     /* maybe we want a direct-dependency listing? */
118     if (direct_dependencies)
119     {
120         ENTRY   *qp;
121
122         for_entry_list(qp)
123             if (qp->nuses)
124             {
125                 int             j;
126
127                 (void) printf("%s:", _nc_first_name(qp->tterm.term_names));
128                 for (j = 0; j < qp->nuses; j++)
129                     (void) printf(" %s", (char *)(qp->uses[j].parent));
130                 putchar('\n');
131             }
132
133         ExitProgram(EXIT_SUCCESS);
134     }
135
136     /* maybe we want a reverse-dependency listing? */
137     if (invert_dependencies)
138     {
139         ENTRY   *qp, *rp;
140         int             matchcount;
141
142         for_entry_list(qp)
143         {
144             matchcount = 0;
145             for_entry_list(rp)
146             {
147                 if (rp->nuses == 0)
148                     continue;
149
150                 for (i = 0; i < rp->nuses; i++)
151                     if (_nc_name_match(qp->tterm.term_names,(char*)rp->uses[i].parent, "|"))
152                     {
153                         if (matchcount++ == 0)
154                             (void) printf("%s:",
155                                           _nc_first_name(qp->tterm.term_names));
156                         (void) printf(" %s",
157                                       _nc_first_name(rp->tterm.term_names));
158                     }
159             }
160             if (matchcount)
161                 putchar('\n');
162         }
163
164         ExitProgram(EXIT_SUCCESS);
165     }
166
167     /*
168      * If we get this far, user wants a simple terminal type listing.
169      */
170     if (optind < argc) {
171         code = typelist(argc-optind, argv+optind, header, deschook);
172     } else {
173         char    *by_env, *home, *eargv[3];
174         int     j;
175
176         j = 0;
177         if ((by_env = getenv("TERMINFO")) != (char *)NULL)
178             eargv[j++] = by_env;
179         else
180         {
181             if ((home = getenv("HOME")) != (char *)NULL)
182             {
183                 char    personal[PATH_MAX];
184
185                 (void) sprintf(personal, PRIVATE_INFO, home);
186                 if (access(personal, F_OK) == 0)
187                     eargv[j++] = personal;
188             }
189             eargv[j++] = TERMINFO;
190         }
191         eargv[j] = (char *)NULL;
192
193         code = typelist(j, eargv, header, deschook);
194     }
195
196     ExitProgram(code);
197 }
198
199 static void deschook(const char *cn, TERMTYPE *tp)
200 /* display a description for the type */
201 {
202     const char *desc;
203
204     if ((desc = strrchr(tp->term_names, '|')) == (char *)NULL)
205         desc = "(No description)";
206     else
207         ++desc;
208
209     (void) printf("%-10s\t%s\n", cn, desc);
210 }
211
212 static int typelist(int eargc, char *eargv[],
213                      bool verbosity,
214                      void  (*hook)(const char *, TERMTYPE *tp))
215 /* apply a function to each entry in given terminfo directories */
216 {
217     int i;
218
219     for (i = 0; i < eargc; i++)
220     {
221         DIR     *termdir;
222         struct dirent *subdir;
223
224         if ((termdir = opendir(eargv[i])) == (DIR *)NULL)
225         {
226             (void) fflush(stdout);
227             (void) fprintf(stderr,
228                            "%s: can't open terminfo directory %s\n",
229                            _nc_progname, eargv[i]);
230             return(EXIT_FAILURE);
231         }
232         else if (verbosity)
233             (void) printf("#\n#%s:\n#\n", eargv[i]);
234
235         while ((subdir = readdir(termdir)) != NULL)
236         {
237             size_t      len = NAMLEN(subdir);
238             char        buf[PATH_MAX];
239             char        name_1[PATH_MAX];
240             DIR *entrydir;
241             struct dirent *entry;
242
243             strncpy(name_1, subdir->d_name, len)[len] = '\0';
244             if (!strcmp(name_1, ".")
245                 || !strcmp(name_1, ".."))
246                 continue;
247
248             (void) strcpy(buf, eargv[i]);
249             (void) strcat(buf, "/");
250             (void) strcat(buf, name_1);
251             (void) strcat(buf, "/");
252             chdir(buf);
253             entrydir = opendir(".");
254             while ((entry = readdir(entrydir)) != NULL)
255             {
256                 char            name_2[PATH_MAX];
257                 TERMTYPE        lterm;
258                 char            *cn;
259                 int             status;
260
261                 len = NAMLEN(entry);
262                 strncpy(name_2, entry->d_name, len)[len] = '\0';
263                 if (!strcmp(name_2, ".")
264                     || !strcmp(name_2, ".."))
265                     continue;
266
267                 status = _nc_read_file_entry(name_2, &lterm);
268                 if (status <= 0)
269                 {
270                     (void) fflush(stdout);
271                     (void) fprintf(stderr,
272                                    "toe: couldn't open terminfo file %s.\n",
273                                    name_2);
274                     return(EXIT_FAILURE);
275                 }
276
277                 /* only visit things once, by primary name */
278                 cn = _nc_first_name(lterm.term_names);
279                 if (!strcmp(cn, name_2))
280                 {
281                     /* apply the selected hook function */
282                     (*hook)(cn, &lterm);
283                 }
284                 if (lterm.term_names) {
285                     free(lterm.term_names);
286                     lterm.term_names = NULL;
287                 }
288                 if (lterm.str_table) {
289                     free(lterm.str_table);
290                     lterm.str_table = NULL;
291                 }
292             }
293             closedir(entrydir);
294         }
295         closedir(termdir);
296     }
297
298     return(EXIT_SUCCESS);
299 }