Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 1 | /**************************************************************************** |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 2 | * Copyright (c) 1998-2012,2013 Free Software Foundation, Inc. * |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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 | * and: Thomas E. Dickey 1996-on * |
| 33 | ****************************************************************************/ |
| 34 | |
| 35 | /* |
| 36 | * toe.c --- table of entries report generator |
| 37 | */ |
| 38 | |
| 39 | #include <progs.priv.h> |
| 40 | |
| 41 | #include <sys/stat.h> |
| 42 | |
| 43 | #if USE_HASHED_DB |
| 44 | #include <hashed_db.h> |
| 45 | #endif |
| 46 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 47 | MODULE_ID("$Id: toe.c,v 1.74 2013/12/15 01:08:28 tom Exp $") |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 48 | |
| 49 | #define isDotname(name) (!strcmp(name, ".") || !strcmp(name, "..")) |
| 50 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 51 | typedef struct { |
| 52 | int db_index; |
| 53 | unsigned long checksum; |
| 54 | char *term_name; |
| 55 | char *description; |
| 56 | } TERMDATA; |
| 57 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 58 | const char *_nc_progname; |
| 59 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 60 | static TERMDATA *ptr_termdata; /* array of terminal data */ |
| 61 | static size_t use_termdata; /* actual usage in ptr_termdata[] */ |
| 62 | static size_t len_termdata; /* allocated size of ptr_termdata[] */ |
| 63 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 64 | #if NO_LEAKS |
| 65 | #undef ExitProgram |
| 66 | static void ExitProgram(int code) GCC_NORETURN; |
| 67 | static void |
| 68 | ExitProgram(int code) |
| 69 | { |
| 70 | _nc_free_entries(_nc_head); |
| 71 | _nc_free_tic(code); |
| 72 | } |
| 73 | #endif |
| 74 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 75 | static void failed(const char *) GCC_NORETURN; |
| 76 | |
| 77 | static void |
| 78 | failed(const char *msg) |
| 79 | { |
| 80 | perror(msg); |
| 81 | ExitProgram(EXIT_FAILURE); |
| 82 | } |
| 83 | |
| 84 | static char * |
| 85 | strmalloc(const char *value) |
| 86 | { |
| 87 | char *result = strdup(value); |
| 88 | if (result == 0) { |
| 89 | failed("strmalloc"); |
| 90 | } |
| 91 | return result; |
| 92 | } |
| 93 | |
| 94 | static TERMDATA * |
| 95 | new_termdata(void) |
| 96 | { |
| 97 | size_t want = use_termdata + 1; |
| 98 | |
| 99 | if (want >= len_termdata) { |
| 100 | len_termdata = (2 * want) + 10; |
| 101 | ptr_termdata = typeRealloc(TERMDATA, len_termdata, ptr_termdata); |
| 102 | if (ptr_termdata == 0) |
| 103 | failed("ptr_termdata"); |
| 104 | } |
| 105 | |
| 106 | return ptr_termdata + use_termdata++; |
| 107 | } |
| 108 | |
| 109 | static int |
| 110 | compare_termdata(const void *a, const void *b) |
| 111 | { |
| 112 | const TERMDATA *p = (const TERMDATA *) a; |
| 113 | const TERMDATA *q = (const TERMDATA *) b; |
| 114 | int result = strcmp(p->term_name, q->term_name); |
| 115 | |
| 116 | if (result == 0) { |
| 117 | result = (p->db_index - q->db_index); |
| 118 | } |
| 119 | return result; |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * Sort the array of TERMDATA and print it. If more than one database is being |
| 124 | * reported, add a column to show which database has a given entry. |
| 125 | */ |
| 126 | static void |
| 127 | show_termdata(int eargc, char **eargv) |
| 128 | { |
| 129 | int j, k; |
| 130 | size_t n; |
| 131 | |
| 132 | if (use_termdata) { |
| 133 | if (eargc > 1) { |
| 134 | for (j = 0; j < eargc; ++j) { |
| 135 | for (k = 0; k <= j; ++k) { |
| 136 | printf("--"); |
| 137 | } |
| 138 | printf("> "); |
| 139 | printf("%s\n", eargv[j]); |
| 140 | } |
| 141 | } |
| 142 | if (use_termdata > 1) |
| 143 | qsort(ptr_termdata, use_termdata, sizeof(TERMDATA), compare_termdata); |
| 144 | for (n = 0; n < use_termdata; ++n) { |
| 145 | |
| 146 | /* |
| 147 | * If there is more than one database, show how they differ. |
| 148 | */ |
| 149 | if (eargc > 1) { |
| 150 | unsigned long check = 0; |
| 151 | k = 0; |
| 152 | for (;;) { |
| 153 | for (; k < ptr_termdata[n].db_index; ++k) { |
| 154 | printf("--"); |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * If this is the first entry, or its checksum differs |
| 159 | * from the first entry's checksum, print "*". Otherwise |
| 160 | * it looks enough like a duplicate to print "+". |
| 161 | */ |
| 162 | printf("%c-", ((check == 0 |
| 163 | || (check != ptr_termdata[n].checksum)) |
| 164 | ? '*' |
| 165 | : '+')); |
| 166 | check = ptr_termdata[n].checksum; |
| 167 | |
| 168 | ++k; |
| 169 | if ((n + 1) >= use_termdata |
| 170 | || strcmp(ptr_termdata[n].term_name, |
| 171 | ptr_termdata[n + 1].term_name)) { |
| 172 | break; |
| 173 | } |
| 174 | ++n; |
| 175 | } |
| 176 | for (; k < eargc; ++k) { |
| 177 | printf("--"); |
| 178 | } |
| 179 | printf(":\t"); |
| 180 | } |
| 181 | |
| 182 | (void) printf("%-10s\t%s\n", |
| 183 | ptr_termdata[n].term_name, |
| 184 | ptr_termdata[n].description); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | static void |
| 190 | free_termdata(void) |
| 191 | { |
| 192 | if (ptr_termdata != 0) { |
| 193 | while (use_termdata != 0) { |
| 194 | --use_termdata; |
| 195 | free(ptr_termdata[use_termdata].term_name); |
| 196 | free(ptr_termdata[use_termdata].description); |
| 197 | } |
| 198 | free(ptr_termdata); |
| 199 | ptr_termdata = 0; |
| 200 | } |
| 201 | use_termdata = 0; |
| 202 | len_termdata = 0; |
| 203 | } |
| 204 | |
| 205 | static char ** |
| 206 | allocArgv(size_t count) |
| 207 | { |
| 208 | char **result = typeCalloc(char *, count + 1); |
| 209 | if (result == 0) |
| 210 | failed("realloc eargv"); |
| 211 | |
| 212 | assert(result != 0); |
| 213 | return result; |
| 214 | } |
| 215 | |
| 216 | static void |
| 217 | freeArgv(char **argv) |
| 218 | { |
| 219 | if (argv) { |
| 220 | int count = 0; |
| 221 | while (argv[count]) { |
| 222 | free(argv[count++]); |
| 223 | } |
| 224 | free(argv); |
| 225 | } |
| 226 | } |
| 227 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 228 | #if USE_HASHED_DB |
| 229 | static bool |
| 230 | make_db_name(char *dst, const char *src, unsigned limit) |
| 231 | { |
| 232 | static const char suffix[] = DBM_SUFFIX; |
| 233 | |
| 234 | bool result = FALSE; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 235 | size_t lens = sizeof(suffix) - 1; |
| 236 | size_t size = strlen(src); |
| 237 | size_t need = lens + size; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 238 | |
| 239 | if (need <= limit) { |
| 240 | if (size >= lens |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 241 | && !strcmp(src + size - lens, suffix)) { |
| 242 | _nc_STRCPY(dst, src, PATH_MAX); |
| 243 | } else { |
| 244 | _nc_SPRINTF(dst, _nc_SLIMIT(PATH_MAX) "%s%s", src, suffix); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 245 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 246 | result = TRUE; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 247 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 248 | return result; |
| 249 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 250 | #endif |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 251 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 252 | typedef void (DescHook) (int /* db_index */ , |
| 253 | int /* db_limit */ , |
| 254 | const char * /* term_name */ , |
| 255 | TERMTYPE * /* term */ ); |
| 256 | |
| 257 | static const char * |
| 258 | term_description(TERMTYPE *tp) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 259 | { |
| 260 | const char *desc; |
| 261 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 262 | if (tp->term_names == 0 |
| 263 | || (desc = strrchr(tp->term_names, '|')) == 0 |
| 264 | || (*++desc == '\0')) { |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 265 | desc = "(No description)"; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 266 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 267 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 268 | return desc; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 269 | } |
| 270 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 271 | /* display a description for the type */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 272 | static void |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 273 | deschook(int db_index, int db_limit, const char *term_name, TERMTYPE *tp) |
| 274 | { |
| 275 | (void) db_index; |
| 276 | (void) db_limit; |
| 277 | (void) printf("%-10s\t%s\n", term_name, term_description(tp)); |
| 278 | } |
| 279 | |
| 280 | static unsigned long |
| 281 | string_sum(const char *value) |
| 282 | { |
| 283 | unsigned long result = 0; |
| 284 | |
| 285 | if ((intptr_t) value == (intptr_t) (-1)) { |
| 286 | result = ~result; |
| 287 | } else if (value) { |
| 288 | while (*value) { |
| 289 | result += UChar(*value); |
| 290 | ++value; |
| 291 | } |
| 292 | } |
| 293 | return result; |
| 294 | } |
| 295 | |
| 296 | static unsigned long |
| 297 | checksum_of(TERMTYPE *tp) |
| 298 | { |
| 299 | unsigned long result = string_sum(tp->term_names); |
| 300 | unsigned i; |
| 301 | |
| 302 | for (i = 0; i < NUM_BOOLEANS(tp); i++) { |
| 303 | result += (unsigned long) (tp->Booleans[i]); |
| 304 | } |
| 305 | for (i = 0; i < NUM_NUMBERS(tp); i++) { |
| 306 | result += (unsigned long) (tp->Numbers[i]); |
| 307 | } |
| 308 | for (i = 0; i < NUM_STRINGS(tp); i++) { |
| 309 | result += string_sum(tp->Strings[i]); |
| 310 | } |
| 311 | return result; |
| 312 | } |
| 313 | |
| 314 | /* collect data, to sort before display */ |
| 315 | static void |
| 316 | sorthook(int db_index, int db_limit, const char *term_name, TERMTYPE *tp) |
| 317 | { |
| 318 | TERMDATA *data = new_termdata(); |
| 319 | |
| 320 | data->db_index = db_index; |
| 321 | data->checksum = ((db_limit > 1) ? checksum_of(tp) : 0); |
| 322 | data->term_name = strmalloc(term_name); |
| 323 | data->description = strmalloc(term_description(tp)); |
| 324 | } |
| 325 | |
| 326 | #if NCURSES_USE_TERMCAP |
| 327 | static void |
| 328 | show_termcap(int db_index, int db_limit, char *buffer, DescHook hook) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 329 | { |
| 330 | TERMTYPE data; |
| 331 | char *next = strchr(buffer, ':'); |
| 332 | char *last; |
| 333 | char *list = buffer; |
| 334 | |
| 335 | if (next) |
| 336 | *next = '\0'; |
| 337 | |
| 338 | last = strrchr(buffer, '|'); |
| 339 | if (last) |
| 340 | ++last; |
| 341 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 342 | memset(&data, 0, sizeof(data)); |
| 343 | data.term_names = strmalloc(buffer); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 344 | while ((next = strtok(list, "|")) != 0) { |
| 345 | if (next != last) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 346 | hook(db_index, db_limit, next, &data); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 347 | list = 0; |
| 348 | } |
| 349 | free(data.term_names); |
| 350 | } |
| 351 | #endif |
| 352 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 353 | #if NCURSES_USE_DATABASE |
| 354 | static char * |
| 355 | copy_entryname(DIRENT * src) |
| 356 | { |
| 357 | size_t len = NAMLEN(src); |
| 358 | char *result = malloc(len + 1); |
| 359 | if (result == 0) |
| 360 | failed("copy entryname"); |
| 361 | memcpy(result, src->d_name, len); |
| 362 | result[len] = '\0'; |
| 363 | |
| 364 | return result; |
| 365 | } |
| 366 | #endif |
| 367 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 368 | static int |
| 369 | typelist(int eargc, char *eargv[], |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 370 | int verbosity, |
| 371 | DescHook hook) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 372 | /* apply a function to each entry in given terminfo directories */ |
| 373 | { |
| 374 | int i; |
| 375 | |
| 376 | for (i = 0; i < eargc; i++) { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 377 | #if NCURSES_USE_DATABASE |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 378 | if (_nc_is_dir_path(eargv[i])) { |
| 379 | char *cwd_buf = 0; |
| 380 | DIR *termdir; |
| 381 | DIRENT *subdir; |
| 382 | |
| 383 | if ((termdir = opendir(eargv[i])) == 0) { |
| 384 | (void) fflush(stdout); |
| 385 | (void) fprintf(stderr, |
| 386 | "%s: can't open terminfo directory %s\n", |
| 387 | _nc_progname, eargv[i]); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 388 | continue; |
| 389 | } |
| 390 | |
| 391 | if (verbosity) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 392 | (void) printf("#\n#%s:\n#\n", eargv[i]); |
| 393 | |
| 394 | while ((subdir = readdir(termdir)) != 0) { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 395 | size_t cwd_len; |
| 396 | char *name_1; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 397 | DIR *entrydir; |
| 398 | DIRENT *entry; |
| 399 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 400 | name_1 = copy_entryname(subdir); |
| 401 | if (isDotname(name_1)) { |
| 402 | free(name_1); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 403 | continue; |
| 404 | } |
| 405 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 406 | cwd_len = NAMLEN(subdir) + strlen(eargv[i]) + 3; |
| 407 | cwd_buf = typeRealloc(char, cwd_len, cwd_buf); |
| 408 | if (cwd_buf == 0) |
| 409 | failed("realloc cwd_buf"); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 410 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 411 | assert(cwd_buf != 0); |
| 412 | |
| 413 | _nc_SPRINTF(cwd_buf, _nc_SLIMIT(cwd_len) |
| 414 | "%s/%s/", eargv[i], name_1); |
| 415 | free(name_1); |
| 416 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 417 | if (chdir(cwd_buf) != 0) |
| 418 | continue; |
| 419 | |
| 420 | entrydir = opendir("."); |
| 421 | if (entrydir == 0) { |
| 422 | perror(cwd_buf); |
| 423 | continue; |
| 424 | } |
| 425 | while ((entry = readdir(entrydir)) != 0) { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 426 | char *name_2; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 427 | TERMTYPE lterm; |
| 428 | char *cn; |
| 429 | int status; |
| 430 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 431 | name_2 = copy_entryname(entry); |
| 432 | if (isDotname(name_2) || !_nc_is_file_path(name_2)) { |
| 433 | free(name_2); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 434 | continue; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 435 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 436 | |
| 437 | status = _nc_read_file_entry(name_2, <erm); |
| 438 | if (status <= 0) { |
| 439 | (void) fflush(stdout); |
| 440 | (void) fprintf(stderr, |
| 441 | "%s: couldn't open terminfo file %s.\n", |
| 442 | _nc_progname, name_2); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 443 | free(cwd_buf); |
| 444 | free(name_2); |
| 445 | closedir(entrydir); |
| 446 | closedir(termdir); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 447 | return (EXIT_FAILURE); |
| 448 | } |
| 449 | |
| 450 | /* only visit things once, by primary name */ |
| 451 | cn = _nc_first_name(lterm.term_names); |
| 452 | if (!strcmp(cn, name_2)) { |
| 453 | /* apply the selected hook function */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 454 | hook(i, eargc, cn, <erm); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 455 | } |
| 456 | _nc_free_termtype(<erm); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 457 | free(name_2); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 458 | } |
| 459 | closedir(entrydir); |
| 460 | } |
| 461 | closedir(termdir); |
| 462 | if (cwd_buf != 0) |
| 463 | free(cwd_buf); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 464 | continue; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 465 | } |
| 466 | #if USE_HASHED_DB |
| 467 | else { |
| 468 | DB *capdbp; |
| 469 | char filename[PATH_MAX]; |
| 470 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 471 | if (verbosity) |
| 472 | (void) printf("#\n#%s:\n#\n", eargv[i]); |
| 473 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 474 | if (make_db_name(filename, eargv[i], sizeof(filename))) { |
| 475 | if ((capdbp = _nc_db_open(filename, FALSE)) != 0) { |
| 476 | DBT key, data; |
| 477 | int code; |
| 478 | |
| 479 | code = _nc_db_first(capdbp, &key, &data); |
| 480 | while (code == 0) { |
| 481 | TERMTYPE lterm; |
| 482 | int used; |
| 483 | char *have; |
| 484 | char *cn; |
| 485 | |
| 486 | if (_nc_db_have_data(&key, &data, &have, &used)) { |
| 487 | if (_nc_read_termtype(<erm, have, used) > 0) { |
| 488 | /* only visit things once, by primary name */ |
| 489 | cn = _nc_first_name(lterm.term_names); |
| 490 | /* apply the selected hook function */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 491 | hook(i, eargc, cn, <erm); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 492 | _nc_free_termtype(<erm); |
| 493 | } |
| 494 | } |
| 495 | code = _nc_db_next(capdbp, &key, &data); |
| 496 | } |
| 497 | |
| 498 | _nc_db_close(capdbp); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 499 | continue; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | } |
| 503 | #endif |
| 504 | #endif |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 505 | #if NCURSES_USE_TERMCAP |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 506 | #if HAVE_BSD_CGETENT |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 507 | { |
| 508 | CGETENT_CONST char *db_array[2]; |
| 509 | char *buffer = 0; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 510 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 511 | if (verbosity) |
| 512 | (void) printf("#\n#%s:\n#\n", eargv[i]); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 513 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 514 | db_array[0] = eargv[i]; |
| 515 | db_array[1] = 0; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 516 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 517 | if (cgetfirst(&buffer, db_array) > 0) { |
| 518 | show_termcap(i, eargc, buffer, hook); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 519 | free(buffer); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 520 | while (cgetnext(&buffer, db_array) > 0) { |
| 521 | show_termcap(i, eargc, buffer, hook); |
| 522 | free(buffer); |
| 523 | } |
| 524 | cgetclose(); |
| 525 | continue; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 526 | } |
| 527 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 528 | #else |
| 529 | /* scan termcap text-file only */ |
| 530 | if (_nc_is_file_path(eargv[i])) { |
| 531 | char buffer[2048]; |
| 532 | FILE *fp; |
| 533 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 534 | if (verbosity) |
| 535 | (void) printf("#\n#%s:\n#\n", eargv[i]); |
| 536 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 537 | if ((fp = fopen(eargv[i], "r")) != 0) { |
| 538 | while (fgets(buffer, sizeof(buffer), fp) != 0) { |
| 539 | if (*buffer == '#') |
| 540 | continue; |
| 541 | if (isspace(*buffer)) |
| 542 | continue; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 543 | show_termcap(i, eargc, buffer, hook); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 544 | } |
| 545 | fclose(fp); |
| 546 | } |
| 547 | } |
| 548 | #endif |
| 549 | #endif |
| 550 | } |
| 551 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 552 | if (hook == sorthook) { |
| 553 | show_termdata(eargc, eargv); |
| 554 | free_termdata(); |
| 555 | } |
| 556 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 557 | return (EXIT_SUCCESS); |
| 558 | } |
| 559 | |
| 560 | static void |
| 561 | usage(void) |
| 562 | { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 563 | (void) fprintf(stderr, "usage: %s [-ahsuUV] [-v n] [file...]\n", _nc_progname); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 564 | ExitProgram(EXIT_FAILURE); |
| 565 | } |
| 566 | |
| 567 | int |
| 568 | main(int argc, char *argv[]) |
| 569 | { |
| 570 | bool all_dirs = FALSE; |
| 571 | bool direct_dependencies = FALSE; |
| 572 | bool invert_dependencies = FALSE; |
| 573 | bool header = FALSE; |
| 574 | char *report_file = 0; |
| 575 | unsigned i; |
| 576 | int code; |
| 577 | int this_opt, last_opt = '?'; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 578 | unsigned v_opt = 0; |
| 579 | DescHook *hook = deschook; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 580 | |
| 581 | _nc_progname = _nc_rootname(argv[0]); |
| 582 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 583 | while ((this_opt = getopt(argc, argv, "0123456789ahsu:vU:V")) != -1) { |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 584 | /* handle optional parameter */ |
| 585 | if (isdigit(this_opt)) { |
| 586 | switch (last_opt) { |
| 587 | case 'v': |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 588 | v_opt = (unsigned) (this_opt - '0'); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 589 | break; |
| 590 | default: |
| 591 | if (isdigit(last_opt)) |
| 592 | v_opt *= 10; |
| 593 | else |
| 594 | v_opt = 0; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 595 | v_opt += (unsigned) (this_opt - '0'); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 596 | last_opt = this_opt; |
| 597 | } |
| 598 | continue; |
| 599 | } |
| 600 | switch (this_opt) { |
| 601 | case 'a': |
| 602 | all_dirs = TRUE; |
| 603 | break; |
| 604 | case 'h': |
| 605 | header = TRUE; |
| 606 | break; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 607 | case 's': |
| 608 | hook = sorthook; |
| 609 | break; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 610 | case 'u': |
| 611 | direct_dependencies = TRUE; |
| 612 | report_file = optarg; |
| 613 | break; |
| 614 | case 'v': |
| 615 | v_opt = 1; |
| 616 | break; |
| 617 | case 'U': |
| 618 | invert_dependencies = TRUE; |
| 619 | report_file = optarg; |
| 620 | break; |
| 621 | case 'V': |
| 622 | puts(curses_version()); |
| 623 | ExitProgram(EXIT_SUCCESS); |
| 624 | default: |
| 625 | usage(); |
| 626 | } |
| 627 | } |
| 628 | set_trace_level(v_opt); |
| 629 | |
| 630 | if (report_file != 0) { |
| 631 | if (freopen(report_file, "r", stdin) == 0) { |
| 632 | (void) fflush(stdout); |
| 633 | fprintf(stderr, "%s: can't open %s\n", _nc_progname, report_file); |
| 634 | ExitProgram(EXIT_FAILURE); |
| 635 | } |
| 636 | |
| 637 | /* parse entries out of the source file */ |
| 638 | _nc_set_source(report_file); |
| 639 | _nc_read_entry_source(stdin, 0, FALSE, FALSE, NULLHOOK); |
| 640 | } |
| 641 | |
| 642 | /* maybe we want a direct-dependency listing? */ |
| 643 | if (direct_dependencies) { |
| 644 | ENTRY *qp; |
| 645 | |
| 646 | for_entry_list(qp) { |
| 647 | if (qp->nuses) { |
| 648 | unsigned j; |
| 649 | |
| 650 | (void) printf("%s:", _nc_first_name(qp->tterm.term_names)); |
| 651 | for (j = 0; j < qp->nuses; j++) |
| 652 | (void) printf(" %s", qp->uses[j].name); |
| 653 | putchar('\n'); |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | ExitProgram(EXIT_SUCCESS); |
| 658 | } |
| 659 | |
| 660 | /* maybe we want a reverse-dependency listing? */ |
| 661 | if (invert_dependencies) { |
| 662 | ENTRY *qp, *rp; |
| 663 | int matchcount; |
| 664 | |
| 665 | for_entry_list(qp) { |
| 666 | matchcount = 0; |
| 667 | for_entry_list(rp) { |
| 668 | if (rp->nuses == 0) |
| 669 | continue; |
| 670 | |
| 671 | for (i = 0; i < rp->nuses; i++) |
| 672 | if (_nc_name_match(qp->tterm.term_names, |
| 673 | rp->uses[i].name, "|")) { |
| 674 | if (matchcount++ == 0) |
| 675 | (void) printf("%s:", |
| 676 | _nc_first_name(qp->tterm.term_names)); |
| 677 | (void) printf(" %s", |
| 678 | _nc_first_name(rp->tterm.term_names)); |
| 679 | } |
| 680 | } |
| 681 | if (matchcount) |
| 682 | putchar('\n'); |
| 683 | } |
| 684 | |
| 685 | ExitProgram(EXIT_SUCCESS); |
| 686 | } |
| 687 | |
| 688 | /* |
| 689 | * If we get this far, user wants a simple terminal type listing. |
| 690 | */ |
| 691 | if (optind < argc) { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 692 | code = typelist(argc - optind, argv + optind, header, hook); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 693 | } else if (all_dirs) { |
| 694 | DBDIRS state; |
| 695 | int offset; |
| 696 | int pass; |
| 697 | const char *path; |
| 698 | char **eargv = 0; |
| 699 | |
| 700 | code = EXIT_FAILURE; |
| 701 | for (pass = 0; pass < 2; ++pass) { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 702 | size_t count = 0; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 703 | |
| 704 | _nc_first_db(&state, &offset); |
| 705 | while ((path = _nc_next_db(&state, &offset)) != 0) { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 706 | if (pass) { |
| 707 | eargv[count] = strmalloc(path); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 708 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 709 | ++count; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 710 | } |
| 711 | if (!pass) { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 712 | eargv = allocArgv(count); |
| 713 | if (eargv == 0) |
| 714 | failed("eargv"); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 715 | } else { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 716 | code = typelist((int) count, eargv, header, hook); |
| 717 | freeArgv(eargv); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | } else { |
| 721 | DBDIRS state; |
| 722 | int offset; |
| 723 | const char *path; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 724 | char **eargv = allocArgv((size_t) 2); |
| 725 | size_t count = 0; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 726 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 727 | if (eargv == 0) |
| 728 | failed("eargv"); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 729 | _nc_first_db(&state, &offset); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 730 | if ((path = _nc_next_db(&state, &offset)) != 0) { |
| 731 | eargv[count++] = strmalloc(path); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 732 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 733 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 734 | code = typelist((int) count, eargv, header, hook); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 735 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 736 | freeArgv(eargv); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 737 | } |
| 738 | _nc_last_db(); |
| 739 | |
| 740 | ExitProgram(code); |
| 741 | } |