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