1 /* $OpenBSD: read_entry.c,v 1.15 2008/01/29 13:02:31 krw Exp $ */ 2 3 /**************************************************************************** 4 * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc. * 5 * * 6 * Permission is hereby granted, free of charge, to any person obtaining a * 7 * copy of this software and associated documentation files (the * 8 * "Software"), to deal in the Software without restriction, including * 9 * without limitation the rights to use, copy, modify, merge, publish, * 10 * distribute, distribute with modifications, sublicense, and/or sell * 11 * copies of the Software, and to permit persons to whom the Software is * 12 * furnished to do so, subject to the following conditions: * 13 * * 14 * The above copyright notice and this permission notice shall be included * 15 * in all copies or substantial portions of the Software. * 16 * * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 20 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 23 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 24 * * 25 * Except as contained in this notice, the name(s) of the above copyright * 26 * holders shall not be used in advertising or otherwise to promote the * 27 * sale, use or other dealings in this Software without prior written * 28 * authorization. * 29 ****************************************************************************/ 30 31 /**************************************************************************** 32 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 33 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 34 ****************************************************************************/ 35 36 /* 37 * read_entry.c -- Routine for reading in a compiled terminfo file 38 * 39 */ 40 41 #include <curses.priv.h> 42 43 #include <tic.h> 44 #include <term_entry.h> 45 46 MODULE_ID("$From: read_entry.c,v 1.72 2000/12/10 02:55:08 tom Exp $") 47 48 #if !HAVE_TELL 49 #define tell(fd) 0 /* lseek() is POSIX, but not tell() - odd... */ 50 #endif 51 52 /* 53 * int 54 * _nc_read_file_entry(filename, ptr) 55 * 56 * Read the compiled terminfo entry in the given file into the 57 * structure pointed to by ptr, allocating space for the string 58 * table. 59 */ 60 61 #undef BYTE 62 #define BYTE(p,n) (unsigned char)((p)[n]) 63 64 #define IS_NEG1(p) ((BYTE(p,0) == 0377) && (BYTE(p,1) == 0377)) 65 #define IS_NEG2(p) ((BYTE(p,0) == 0376) && (BYTE(p,1) == 0377)) 66 #define LOW_MSB(p) (BYTE(p,0) + 256*BYTE(p,1)) 67 68 static bool have_tic_directory = FALSE; 69 static bool keep_tic_directory = FALSE; 70 71 /* 72 * Record the "official" location of the terminfo directory, according to 73 * the place where we're writing to, or the normal default, if not. 74 */ 75 NCURSES_EXPORT(const char *) 76 _nc_tic_dir(const char *path) 77 { 78 static const char *result = TERMINFO; 79 80 if (!keep_tic_directory) { 81 if (path != 0) { 82 result = path; 83 have_tic_directory = TRUE; 84 } else if (!have_tic_directory && use_terminfo_vars()) { 85 char *envp; 86 if ((envp = getenv("TERMINFO")) != 0) 87 return _nc_tic_dir(envp); 88 } 89 } 90 return result; 91 } 92 93 /* 94 * Special fix to prevent the terminfo directory from being moved after tic 95 * has chdir'd to it. If we let it be changed, then if $TERMINFO has a 96 * relative path, we'll lose track of the actual directory. 97 */ 98 NCURSES_EXPORT(void) 99 _nc_keep_tic_dir(const char *path) 100 { 101 _nc_tic_dir(path); 102 keep_tic_directory = TRUE; 103 } 104 105 static void 106 convert_shorts(char *buf, short *Numbers, int count) 107 { 108 int i; 109 for (i = 0; i < count; i++) { 110 if (IS_NEG1(buf + 2 * i)) 111 Numbers[i] = ABSENT_NUMERIC; 112 else if (IS_NEG2(buf + 2 * i)) 113 Numbers[i] = CANCELLED_NUMERIC; 114 else 115 Numbers[i] = LOW_MSB(buf + 2 * i); 116 TR(TRACE_DATABASE, ("get Numbers[%d]=%d", i, Numbers[i])); 117 } 118 } 119 120 static void 121 convert_strings(char *buf, char **Strings, int count, int size, char *table) 122 { 123 int i; 124 char *p; 125 126 for (i = 0; i < count; i++) { 127 if (IS_NEG1(buf + 2 * i)) { 128 Strings[i] = ABSENT_STRING; 129 } else if (IS_NEG2(buf + 2 * i)) { 130 Strings[i] = CANCELLED_STRING; 131 } else if (LOW_MSB(buf + 2 * i) > size) { 132 Strings[i] = ABSENT_STRING; 133 } else { 134 Strings[i] = (LOW_MSB(buf + 2 * i) + table); 135 TR(TRACE_DATABASE, ("Strings[%d] = %s", i, _nc_visbuf(Strings[i]))); 136 } 137 138 /* make sure all strings are NUL terminated */ 139 if (VALID_STRING(Strings[i])) { 140 for (p = Strings[i]; p <= table + size; p++) 141 if (*p == '\0') 142 break; 143 /* if there is no NUL, ignore the string */ 144 if (p > table + size) 145 Strings[i] = ABSENT_STRING; 146 } 147 } 148 } 149 150 #define read_shorts(fd, buf, count) (read(fd, buf, (count)*2) == (count)*2) 151 152 #define even_boundary(value) \ 153 if ((value) % 2 != 0) read(fd, buf, 1) 154 155 static int 156 read_termtype(int fd, TERMTYPE * ptr) 157 /* return 1 if read, 0 if not found or garbled */ 158 { 159 int name_size, bool_count, num_count, str_count, str_size; 160 int i; 161 size_t bsize; 162 char buf[MAX_ENTRY_SIZE]; 163 164 TR(TRACE_DATABASE, ("READ termtype header @%d", tell(fd))); 165 166 memset(ptr, 0, sizeof(*ptr)); 167 168 /* grab the header */ 169 if (!read_shorts(fd, buf, 6) 170 || LOW_MSB(buf) != MAGIC) { 171 return (0); 172 } 173 174 _nc_free_termtype(ptr); 175 name_size = LOW_MSB(buf + 2); 176 bool_count = LOW_MSB(buf + 4); 177 num_count = LOW_MSB(buf + 6); 178 str_count = LOW_MSB(buf + 8); 179 str_size = LOW_MSB(buf + 10); 180 181 TR(TRACE_DATABASE, 182 ("TERMTYPE name_size=%d, bool=%d/%d, num=%d/%d str=%d/%d(%d)", 183 name_size, bool_count, BOOLCOUNT, num_count, NUMCOUNT, 184 str_count, STRCOUNT, str_size)); 185 if (name_size < 0 186 || bool_count < 0 187 || num_count < 0 188 || str_count < 0 189 || str_size < 0) { 190 return (0); 191 } 192 193 if (str_size) { 194 /* try to allocate space for the string table */ 195 if (str_count * 2 >= (int) sizeof(buf) 196 || (ptr->str_table = typeMalloc(char, (unsigned) str_size)) == 0) { 197 return (0); 198 } 199 } else { 200 str_count = 0; 201 } 202 203 /* grab the name (a null-terminate string) */ 204 read(fd, buf, min(MAX_NAME_SIZE, (unsigned) name_size)); 205 buf[MAX_NAME_SIZE] = '\0'; 206 bsize = strlen(buf) + 1; 207 ptr->term_names = typeCalloc(char, bsize); 208 if (ptr->term_names == NULL) { 209 return (0); 210 } 211 (void) strlcpy(ptr->term_names, buf, bsize); 212 if (name_size > MAX_NAME_SIZE) 213 lseek(fd, (off_t) (name_size - MAX_NAME_SIZE), 1); 214 215 /* grab the booleans */ 216 if ((ptr->Booleans = typeCalloc(char, max(BOOLCOUNT, bool_count))) == 0 217 || read(fd, ptr->Booleans, (unsigned) bool_count) != bool_count) { 218 return (0); 219 } 220 221 /* 222 * If booleans end on an odd byte, skip it. The machine they 223 * originally wrote terminfo on must have been a 16-bit 224 * word-oriented machine that would trap out if you tried a 225 * word access off a 2-byte boundary. 226 */ 227 even_boundary(name_size + bool_count); 228 229 /* grab the numbers */ 230 if ((ptr->Numbers = typeCalloc(short, max(NUMCOUNT, num_count))) == 0 231 || !read_shorts(fd, buf, num_count)) { 232 return (0); 233 } 234 convert_shorts(buf, ptr->Numbers, num_count); 235 236 if ((ptr->Strings = typeCalloc(char *, max(STRCOUNT, str_count))) == 0) 237 return (0); 238 239 if (str_count) { 240 /* grab the string offsets */ 241 if (!read_shorts(fd, buf, str_count)) { 242 return (0); 243 } 244 /* finally, grab the string table itself */ 245 if (read(fd, ptr->str_table, (unsigned) str_size) != str_size) 246 return (0); 247 convert_strings(buf, ptr->Strings, str_count, str_size, ptr->str_table); 248 } 249 #if NCURSES_XNAMES 250 251 ptr->num_Booleans = BOOLCOUNT; 252 ptr->num_Numbers = NUMCOUNT; 253 ptr->num_Strings = STRCOUNT; 254 255 /* 256 * Read extended entries, if any, after the normal end of terminfo data. 257 */ 258 even_boundary(str_size); 259 TR(TRACE_DATABASE, ("READ extended_header @%d", tell(fd))); 260 if (_nc_user_definable && read_shorts(fd, buf, 5)) { 261 int ext_bool_count = LOW_MSB(buf + 0); 262 int ext_num_count = LOW_MSB(buf + 2); 263 int ext_str_count = LOW_MSB(buf + 4); 264 int ext_str_size = LOW_MSB(buf + 6); 265 int ext_str_limit = LOW_MSB(buf + 8); 266 int need = (ext_bool_count + ext_num_count + ext_str_count); 267 int base = 0; 268 269 if (need >= (int) sizeof(buf) 270 || ext_str_size >= (int) sizeof(buf) 271 || ext_str_limit >= (int) sizeof(buf) 272 || ext_bool_count < 0 273 || ext_num_count < 0 274 || ext_str_count < 0 275 || ext_str_size < 0 276 || ext_str_limit < 0) 277 return (0); 278 279 ptr->num_Booleans = BOOLCOUNT + ext_bool_count; 280 ptr->num_Numbers = NUMCOUNT + ext_num_count; 281 ptr->num_Strings = STRCOUNT + ext_str_count; 282 283 ptr->Booleans = typeRealloc(char, ptr->num_Booleans, ptr->Booleans); 284 ptr->Numbers = typeRealloc(short, ptr->num_Numbers, ptr->Numbers); 285 ptr->Strings = typeRealloc(char *, ptr->num_Strings, ptr->Strings); 286 287 TR(TRACE_DATABASE, ("extended header is %d/%d/%d(%d:%d)", 288 ext_bool_count, ext_num_count, ext_str_count, 289 ext_str_size, ext_str_limit)); 290 291 TR(TRACE_DATABASE, ("READ %d extended-booleans @%d", 292 ext_bool_count, tell(fd))); 293 if ((ptr->ext_Booleans = ext_bool_count) != 0) { 294 if (read(fd, ptr->Booleans + BOOLCOUNT, (unsigned) 295 ext_bool_count) != ext_bool_count) 296 return (0); 297 } 298 even_boundary(ext_bool_count); 299 300 TR(TRACE_DATABASE, ("READ %d extended-numbers @%d", 301 ext_num_count, tell(fd))); 302 if ((ptr->ext_Numbers = ext_num_count) != 0) { 303 if (!read_shorts(fd, buf, ext_num_count)) 304 return (0); 305 TR(TRACE_DATABASE, ("Before converting extended-numbers")); 306 convert_shorts(buf, ptr->Numbers + NUMCOUNT, ext_num_count); 307 } 308 309 TR(TRACE_DATABASE, ("READ extended-offsets @%d", tell(fd))); 310 if ((ext_str_count || need) 311 && !read_shorts(fd, buf, ext_str_count + need)) 312 return (0); 313 314 TR(TRACE_DATABASE, ("READ %d bytes of extended-strings @%d", 315 ext_str_limit, tell(fd))); 316 317 if (ext_str_limit) { 318 if ((ptr->ext_str_table = typeMalloc(char, ext_str_limit)) == 0) 319 return (0); 320 if (read(fd, ptr->ext_str_table, ext_str_limit) != ext_str_limit) 321 return (0); 322 TR(TRACE_DATABASE, ("first extended-string is %s", _nc_visbuf(ptr->ext_str_table))); 323 } 324 325 if ((ptr->ext_Strings = ext_str_count) != 0) { 326 TR(TRACE_DATABASE, 327 ("Before computing extended-string capabilities str_count=%d, ext_str_count=%d", 328 str_count, ext_str_count)); 329 convert_strings(buf, ptr->Strings + str_count, ext_str_count, 330 ext_str_limit, ptr->ext_str_table); 331 for (i = ext_str_count - 1; i >= 0; i--) { 332 TR(TRACE_DATABASE, ("MOVE from [%d:%d] %s", 333 i, i + str_count, 334 _nc_visbuf(ptr->Strings[i + str_count]))); 335 ptr->Strings[i + STRCOUNT] = ptr->Strings[i + str_count]; 336 if (VALID_STRING(ptr->Strings[i + STRCOUNT])) 337 base += (strlen(ptr->Strings[i + STRCOUNT]) + 1); 338 TR(TRACE_DATABASE, ("... to [%d] %s", 339 i + STRCOUNT, 340 _nc_visbuf(ptr->Strings[i + STRCOUNT]))); 341 } 342 } 343 344 if (need) { 345 if ((ptr->ext_Names = typeCalloc(char *, need)) == 0) 346 return (0); 347 TR(TRACE_DATABASE, 348 ("ext_NAMES starting @%d in extended_strings, first = %s", 349 base, _nc_visbuf(ptr->ext_str_table + base))); 350 convert_strings(buf + (2 * ext_str_count), ptr->ext_Names, need, 351 ext_str_limit, ptr->ext_str_table + base); 352 } 353 354 T(("...done reading terminfo bool %d(%d) num %d(%d) str %d(%d)", 355 ptr->num_Booleans, ptr->ext_Booleans, 356 ptr->num_Numbers, ptr->ext_Numbers, 357 ptr->num_Strings, ptr->ext_Strings)); 358 359 TR(TRACE_DATABASE, ("extend: num_Booleans:%d", ptr->num_Booleans)); 360 } else 361 #endif /* NCURSES_XNAMES */ 362 { 363 T(("...done reading terminfo bool %d num %d str %d", 364 bool_count, num_count, str_count)); 365 #if NCURSES_XNAMES 366 TR(TRACE_DATABASE, ("normal: num_Booleans:%d", ptr->num_Booleans)); 367 #endif 368 } 369 370 for (i = bool_count; i < BOOLCOUNT; i++) 371 ptr->Booleans[i] = FALSE; 372 for (i = num_count; i < NUMCOUNT; i++) 373 ptr->Numbers[i] = ABSENT_NUMERIC; 374 for (i = str_count; i < STRCOUNT; i++) 375 ptr->Strings[i] = ABSENT_STRING; 376 377 return (1); 378 } 379 380 NCURSES_EXPORT(int) 381 _nc_read_file_entry 382 (const char *const filename, TERMTYPE * ptr) 383 /* return 1 if read, 0 if not found or garbled */ 384 { 385 int code, fd = -1; 386 387 #ifdef __OpenBSD__ 388 if (_nc_read_bsd_terminfo_file(filename, ptr) == 1) 389 return (1); 390 #endif /* __OpenBSD__ */ 391 392 if (_nc_access(filename, R_OK) < 0 393 || (fd = open(filename, O_RDONLY | O_BINARY)) < 0) { 394 T(("cannot open terminfo %s (errno=%d)", filename, errno)); 395 return (0); 396 } 397 398 T(("read terminfo %s", filename)); 399 if ((code = read_termtype(fd, ptr)) == 0) 400 _nc_free_termtype(ptr); 401 close(fd); 402 403 return (code); 404 } 405 406 /* 407 * Build a terminfo pathname and try to read the data. Returns 1 on success, 408 * 0 on failure. 409 */ 410 static int 411 _nc_read_tic_entry(char *const filename, 412 const char *const dir, const char *ttn, TERMTYPE * const tp) 413 { 414 /* maximum safe length of terminfo root directory name */ 415 #define MAX_TPATH (PATH_MAX - MAX_ALIAS - 6) 416 417 if (strlen(dir) > MAX_TPATH) 418 return 0; 419 (void) snprintf(filename, MAX_TPATH + 1, "%s/%s", dir, ttn); 420 return _nc_read_file_entry(filename, tp); 421 } 422 423 /* 424 * Process the list of :-separated directories, looking for the terminal type. 425 * We don't use strtok because it does not show us empty tokens. 426 */ 427 static int 428 _nc_read_terminfo_dirs(const char *dirs, char *const filename, const char *const 429 ttn, TERMTYPE * const tp) 430 { 431 char *list, *a; 432 const char *b; 433 int code = 0; 434 435 /* we'll modify the argument, so we must copy */ 436 if ((b = a = list = strdup(dirs)) == NULL) 437 return (0); 438 439 for (;;) { 440 int c = *a; 441 if (c == 0 || c == NCURSES_PATHSEP) { 442 *a = 0; 443 if ((b + 1) >= a) 444 b = TERMINFO; 445 if (_nc_read_tic_entry(filename, b, ttn, tp) == 1) { 446 code = 1; 447 break; 448 } 449 b = a + 1; 450 if (c == 0) 451 break; 452 } 453 a++; 454 } 455 456 free(list); 457 return (code); 458 } 459 460 /* 461 * _nc_read_entry(char *tn, char *filename, TERMTYPE *tp) 462 * 463 * Find and read the compiled entry for a given terminal type, 464 * if it exists. We take pains here to make sure no combination 465 * of environment variables and terminal type name can be used to 466 * overrun the file buffer. 467 */ 468 469 NCURSES_EXPORT(int) 470 _nc_read_entry 471 (const char *const tn, char *const filename, TERMTYPE * const tp) 472 { 473 char *envp; 474 char ttn[MAX_ALIAS + 3]; 475 476 #ifdef __OpenBSD__ 477 /* First check the BSD terminfo.db file */ 478 if (_nc_read_bsd_terminfo_entry(tn, filename, tp) == 1) 479 return (1); 480 #endif /* __OpenBSD__ */ 481 482 /* truncate the terminal name to prevent dangerous buffer airline */ 483 (void) snprintf(ttn, sizeof(ttn), "%c/%.*s", *tn, MAX_ALIAS, tn); 484 485 /* This is System V behavior, in conjunction with our requirements for 486 * writing terminfo entries. 487 */ 488 if (have_tic_directory 489 && _nc_read_tic_entry(filename, _nc_tic_dir(0), ttn, tp) == 1) 490 return 1; 491 492 if (use_terminfo_vars()) { 493 if ((envp = getenv("TERMINFO")) != 0 494 && _nc_read_tic_entry(filename, _nc_tic_dir(envp), ttn, tp) == 1) 495 return 1; 496 497 /* this is an ncurses extension */ 498 if ((envp = _nc_home_terminfo()) != 0) { 499 if (_nc_read_tic_entry(filename, envp, ttn, tp) == 1) { 500 return (1); 501 } 502 } 503 504 /* this is an ncurses extension */ 505 if ((envp = getenv("TERMINFO_DIRS")) != 0) 506 return _nc_read_terminfo_dirs(envp, filename, ttn, tp); 507 } 508 509 /* Try the system directory. Note that the TERMINFO_DIRS value, if 510 * defined by the configure script, begins with a ":", which will be 511 * interpreted as TERMINFO. 512 */ 513 #ifdef TERMINFO_DIRS 514 return _nc_read_terminfo_dirs(TERMINFO_DIRS, filename, ttn, tp); 515 #else 516 return _nc_read_tic_entry(filename, TERMINFO, ttn, tp); 517 #endif 518 } 519