1 /****************************************************************************
2 * Copyright (c) 1998-2008,2010 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 * and: Thomas E. Dickey 1996-on *
33 ****************************************************************************/
36 * toe.c --- table of entries report generator
39 #include <progs.priv.h>
44 #include <hashed_db.h>
47 MODULE_ID("$Id: toe.c,v 1.52 2010/05/01 22:04:08 tom Exp $")
49 #define isDotname(name) (!strcmp(name, ".") || !strcmp(name, ".."))
51 const char *_nc_progname
;
55 static void ExitProgram(int code
) GCC_NORETURN
;
59 _nc_free_entries(_nc_head
);
65 failed(const char *msg
)
68 ExitProgram(EXIT_FAILURE
);
73 make_db_name(char *dst
, const char *src
, unsigned limit
)
75 static const char suffix
[] = DBM_SUFFIX
;
78 unsigned lens
= sizeof(suffix
) - 1;
79 unsigned size
= strlen(src
);
80 unsigned need
= lens
+ size
;
84 && !strcmp(src
+ size
- lens
, suffix
))
85 (void) strcpy(dst
, src
);
87 (void) sprintf(dst
, "%s%s", src
, suffix
);
95 is_database(const char *path
)
99 if (_nc_is_dir_path(path
) && access(path
, R_OK
| X_OK
) == 0) {
104 if (_nc_is_file_path(path
) && access(path
, R_OK
) == 0) {
110 char filename
[PATH_MAX
];
111 if (_nc_is_file_path(path
) && access(path
, R_OK
) == 0) {
113 } else if (make_db_name(filename
, path
, sizeof(filename
))) {
114 if (_nc_is_file_path(filename
) && access(filename
, R_OK
) == 0) {
124 deschook(const char *cn
, TERMTYPE
*tp
)
125 /* display a description for the type */
129 if ((desc
= strrchr(tp
->term_names
, '|')) == 0 || *++desc
== '\0')
130 desc
= "(No description)";
132 (void) printf("%-10s\t%s\n", cn
, desc
);
137 show_termcap(char *buffer
,
138 void (*hook
) (const char *, TERMTYPE
*tp
))
141 char *next
= strchr(buffer
, ':');
148 last
= strrchr(buffer
, '|');
152 data
.term_names
= strdup(buffer
);
153 while ((next
= strtok(list
, "|")) != 0) {
158 free(data
.term_names
);
163 typelist(int eargc
, char *eargv
[],
165 void (*hook
) (const char *, TERMTYPE
*tp
))
166 /* apply a function to each entry in given terminfo directories */
170 for (i
= 0; i
< eargc
; i
++) {
172 if (_nc_is_dir_path(eargv
[i
])) {
177 if ((termdir
= opendir(eargv
[i
])) == 0) {
178 (void) fflush(stdout
);
179 (void) fprintf(stderr
,
180 "%s: can't open terminfo directory %s\n",
181 _nc_progname
, eargv
[i
]);
182 return (EXIT_FAILURE
);
183 } else if (verbosity
)
184 (void) printf("#\n#%s:\n#\n", eargv
[i
]);
186 while ((subdir
= readdir(termdir
)) != 0) {
187 size_t len
= NAMLEN(subdir
);
188 size_t cwd_len
= len
+ strlen(eargv
[i
]) + 3;
189 char name_1
[PATH_MAX
];
193 cwd_buf
= typeRealloc(char, cwd_len
, cwd_buf
);
195 failed("realloc cwd_buf");
197 assert(cwd_buf
!= 0);
199 strncpy(name_1
, subdir
->d_name
, len
)[len
] = '\0';
200 if (isDotname(name_1
))
203 (void) sprintf(cwd_buf
, "%s/%.*s/", eargv
[i
], (int) len
, name_1
);
204 if (chdir(cwd_buf
) != 0)
207 entrydir
= opendir(".");
212 while ((entry
= readdir(entrydir
)) != 0) {
213 char name_2
[PATH_MAX
];
219 strncpy(name_2
, entry
->d_name
, len
)[len
] = '\0';
220 if (isDotname(name_2
) || !_nc_is_file_path(name_2
))
223 status
= _nc_read_file_entry(name_2
, <erm
);
225 (void) fflush(stdout
);
226 (void) fprintf(stderr
,
227 "%s: couldn't open terminfo file %s.\n",
228 _nc_progname
, name_2
);
229 return (EXIT_FAILURE
);
232 /* only visit things once, by primary name */
233 cn
= _nc_first_name(lterm
.term_names
);
234 if (!strcmp(cn
, name_2
)) {
235 /* apply the selected hook function */
236 (*hook
) (cn
, <erm
);
238 _nc_free_termtype(<erm
);
249 char filename
[PATH_MAX
];
251 if (make_db_name(filename
, eargv
[i
], sizeof(filename
))) {
252 if ((capdbp
= _nc_db_open(filename
, FALSE
)) != 0) {
256 code
= _nc_db_first(capdbp
, &key
, &data
);
263 if (_nc_db_have_data(&key
, &data
, &have
, &used
)) {
264 if (_nc_read_termtype(<erm
, have
, used
) > 0) {
265 /* only visit things once, by primary name */
266 cn
= _nc_first_name(lterm
.term_names
);
267 /* apply the selected hook function */
268 (*hook
) (cn
, <erm
);
269 _nc_free_termtype(<erm
);
272 code
= _nc_db_next(capdbp
, &key
, &data
);
275 _nc_db_close(capdbp
);
282 #if defined(HAVE_BSD_CGETENT)
287 (void) printf("#\n#%s:\n#\n", eargv
[i
]);
289 db_array
[0] = eargv
[i
];
292 if (cgetfirst(&buffer
, db_array
)) {
293 show_termcap(buffer
, hook
);
295 while (cgetnext(&buffer
, db_array
)) {
296 show_termcap(buffer
, hook
);
302 /* scan termcap text-file only */
303 if (_nc_is_file_path(eargv
[i
])) {
307 if ((fp
= fopen(eargv
[i
], "r")) != 0) {
308 while (fgets(buffer
, sizeof(buffer
), fp
) != 0) {
311 if (isspace(*buffer
))
313 show_termcap(buffer
, hook
);
322 return (EXIT_SUCCESS
);
328 (void) fprintf(stderr
, "usage: %s [-ahuUV] [-v n] [file...]\n", _nc_progname
);
329 ExitProgram(EXIT_FAILURE
);
333 main(int argc
, char *argv
[])
335 bool all_dirs
= FALSE
;
336 bool direct_dependencies
= FALSE
;
337 bool invert_dependencies
= FALSE
;
339 char *report_file
= 0;
342 int this_opt
, last_opt
= '?';
345 _nc_progname
= _nc_rootname(argv
[0]);
347 while ((this_opt
= getopt(argc
, argv
, "0123456789ahu:vU:V")) != -1) {
348 /* handle optional parameter */
349 if (isdigit(this_opt
)) {
352 v_opt
= (this_opt
- '0');
355 if (isdigit(last_opt
))
359 v_opt
+= (this_opt
- '0');
372 direct_dependencies
= TRUE
;
373 report_file
= optarg
;
379 invert_dependencies
= TRUE
;
380 report_file
= optarg
;
383 puts(curses_version());
384 ExitProgram(EXIT_SUCCESS
);
389 set_trace_level(v_opt
);
391 if (report_file
!= 0) {
392 if (freopen(report_file
, "r", stdin
) == 0) {
393 (void) fflush(stdout
);
394 fprintf(stderr
, "%s: can't open %s\n", _nc_progname
, report_file
);
395 ExitProgram(EXIT_FAILURE
);
398 /* parse entries out of the source file */
399 _nc_set_source(report_file
);
400 _nc_read_entry_source(stdin
, 0, FALSE
, FALSE
, NULLHOOK
);
403 /* maybe we want a direct-dependency listing? */
404 if (direct_dependencies
) {
411 (void) printf("%s:", _nc_first_name(qp
->tterm
.term_names
));
412 for (j
= 0; j
< qp
->nuses
; j
++)
413 (void) printf(" %s", qp
->uses
[j
].name
);
418 ExitProgram(EXIT_SUCCESS
);
421 /* maybe we want a reverse-dependency listing? */
422 if (invert_dependencies
) {
432 for (i
= 0; i
< rp
->nuses
; i
++)
433 if (_nc_name_match(qp
->tterm
.term_names
,
434 rp
->uses
[i
].name
, "|")) {
435 if (matchcount
++ == 0)
437 _nc_first_name(qp
->tterm
.term_names
));
439 _nc_first_name(rp
->tterm
.term_names
));
446 ExitProgram(EXIT_SUCCESS
);
450 * If we get this far, user wants a simple terminal type listing.
453 code
= typelist(argc
- optind
, argv
+ optind
, header
, deschook
);
454 } else if (all_dirs
) {
462 for (pass
= 0; pass
< 2; ++pass
) {
465 _nc_first_db(&state
, &offset
);
466 while ((path
= _nc_next_db(&state
, &offset
)) != 0) {
467 if (!is_database(path
)) {
469 } else if (eargv
!= 0) {
473 /* eliminate duplicates */
474 for (n
= 0; n
< count
; ++n
) {
475 if (!strcmp(path
, eargv
[n
])) {
481 eargv
[count
] = strdup(path
);
489 eargv
= typeCalloc(char *, count
+ 1);
491 failed("realloc eargv");
495 code
= typelist((int) count
, eargv
, header
, deschook
);
508 _nc_first_db(&state
, &offset
);
509 while ((path
= _nc_next_db(&state
, &offset
)) != 0) {
510 if (is_database(path
)) {
511 eargv
[count
++] = strdup(path
);
517 code
= typelist(count
, eargv
, header
, deschook
);