1 /****************************************************************************
2 * Copyright 2018-2022,2023 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1996-on *
34 ****************************************************************************/
37 * read_entry.c -- Routine for reading in a compiled terminfo file
40 #include <curses.priv.h>
41 #include <hashed_db.h>
45 MODULE_ID("$Id: read_entry.c,v 1.171 2023/09/16 16:30:34 tom Exp $")
47 #define MyNumber(n) (short) LOW_MSB(n)
49 #define SIZEOF_32BITS 4
51 #if NCURSES_USE_DATABASE
52 #if NCURSES_EXT_NUMBERS
54 convert_16bits(char *buf, NCURSES_INT2 *Numbers, int count)
58 size_t size = SIZEOF_SHORT;
59 for (i = 0; i < count; i++) {
63 for (j = 0; j < size; ++j) {
65 Numbers[i] |= (ch << (8 * j));
70 Numbers[i] |= (int) mask;
74 TR(TRACE_DATABASE, ("get Numbers[%d]=%d", i, Numbers[i]));
80 convert_32bits(char *buf, NCURSES_INT2 *Numbers, int count)
84 size_t size = SIZEOF_INT2;
87 assert(sizeof(NCURSES_INT2) == size);
88 for (i = 0; i < count; i++) {
90 for (j = 0; j < size; ++j) {
92 Numbers[i] |= (ch << (8 * j));
94 /* "unsigned" and NCURSES_INT2 are the same size - no sign-extension */
95 TR(TRACE_DATABASE, ("get Numbers[%d]=%d", i, Numbers[i]));
101 convert_32bits(char *buf, NCURSES_INT2 *Numbers, int count)
105 for (i = 0; i < count; i++) {
107 for (j = 0; j < SIZEOF_32BITS; ++j) {
109 value |= (ch << (8 * j));
112 Numbers[i] = ABSENT_NUMERIC;
113 else if (value == -2)
114 Numbers[i] = CANCELLED_NUMERIC;
115 else if (value > MAX_OF_TYPE(NCURSES_INT2))
116 Numbers[i] = MAX_OF_TYPE(NCURSES_INT2);
118 Numbers[i] = (short) value;
119 TR(TRACE_DATABASE, ("get Numbers[%d]=%d", i, Numbers[i]));
125 convert_16bits(char *buf, NCURSES_INT2 *Numbers, int count)
128 for (i = 0; i < count; i++) {
129 if (IS_NEG1(buf + 2 * i))
130 Numbers[i] = ABSENT_NUMERIC;
131 else if (IS_NEG2(buf + 2 * i))
132 Numbers[i] = CANCELLED_NUMERIC;
134 Numbers[i] = MyNumber(buf + 2 * i);
135 TR(TRACE_DATABASE, ("get Numbers[%d]=%d", i, Numbers[i]));
142 convert_strings(char *buf, char **Strings, int count, int size,
143 char *table, bool always)
149 for (i = 0; i < count; i++) {
150 if (IS_NEG1(buf + 2 * i)) {
151 Strings[i] = ABSENT_STRING;
152 } else if (IS_NEG2(buf + 2 * i)) {
153 Strings[i] = CANCELLED_STRING;
154 } else if (MyNumber(buf + 2 * i) > size) {
155 Strings[i] = ABSENT_STRING;
157 int nn = MyNumber(buf + 2 * i);
158 if (nn >= 0 && nn < size) {
159 Strings[i] = (nn + table);
160 TR(TRACE_DATABASE, ("Strings[%d] = %s", i,
161 _nc_visbuf(Strings[i])));
164 ("found out-of-range index %d to Strings[%d]", nn, i));
170 /* make sure all strings are NUL terminated */
171 if (VALID_STRING(Strings[i])) {
172 for (p = Strings[i]; p < table + size; p++)
175 /* if there is no NUL, ignore the string */
176 if (p >= table + size) {
177 Strings[i] = ABSENT_STRING;
178 } else if (p == Strings[i] && always) {
180 ("found empty but required Strings[%d]", i));
184 } else if (always) { /* names are always needed */
186 ("found invalid but required Strings[%d]", i));
192 _nc_warning("corrupt data found in convert_strings");
198 fake_read(char *src, int *offset, int limit, char *dst, unsigned want)
200 int have = (limit - *offset);
203 if ((int) want > have)
204 want = (unsigned) have;
205 memcpy(dst, src + *offset, (size_t) want);
206 *offset += (int) want;
213 #define Read(buf, count) fake_read(buffer, &offset, limit, (char *) buf, (unsigned) count)
215 #define read_shorts(buf, count) \
216 (Read(buf, (count)*SIZEOF_SHORT) == (int) (count)*SIZEOF_SHORT)
218 #define read_numbers(buf, count) \
219 (Read(buf, (count)*(unsigned)size_of_numbers) == (int) (count)*size_of_numbers)
221 #define even_boundary(value) \
222 if ((value) % 2 != 0) Read(buf, 1)
226 _nc_init_termtype(TERMTYPE2 *const tp)
230 DEBUG(2, (T_CALLED("_nc_init_termtype(tp=%p)"), (void *) tp));
233 tp->num_Booleans = BOOLCOUNT;
234 tp->num_Numbers = NUMCOUNT;
235 tp->num_Strings = STRCOUNT;
236 tp->ext_Booleans = 0;
240 if (tp->Booleans == 0)
241 TYPE_MALLOC(NCURSES_SBOOL, BOOLCOUNT, tp->Booleans);
242 if (tp->Numbers == 0)
243 TYPE_MALLOC(NCURSES_INT2, NUMCOUNT, tp->Numbers);
244 if (tp->Strings == 0)
245 TYPE_MALLOC(char *, STRCOUNT, tp->Strings);
247 for_each_boolean(i, tp)
248 tp->Booleans[i] = FALSE;
250 for_each_number(i, tp)
251 tp->Numbers[i] = ABSENT_NUMERIC;
253 for_each_string(i, tp)
254 tp->Strings[i] = ABSENT_STRING;
256 DEBUG(2, (T_RETURN("")));
259 #if NCURSES_USE_DATABASE
262 valid_shorts(char *buffer, int limit)
266 for (n = 0; n < limit; ++n) {
267 if (MyNumber(buffer + (n * 2)) > 0) {
277 * Return TGETENT_YES if read, TGETENT_NO if not found or garbled.
280 _nc_read_termtype(TERMTYPE2 *ptr, char *buffer, int limit)
283 int name_size, bool_count, num_count, str_count, str_size;
285 char buf[MAX_ENTRY_SIZE + 2];
288 size_t (*convert_numbers) (char *, NCURSES_INT2 *, int);
290 int max_entry_size = MAX_ENTRY_SIZE;
293 (T_CALLED("_nc_read_termtype(ptr=%p, buffer=%p, limit=%d)"),
294 (void *) ptr, buffer, limit));
296 TR(TRACE_DATABASE, ("READ termtype header @%d", offset));
298 memset(ptr, 0, sizeof(*ptr));
300 /* grab the header */
301 if (!read_shorts(buf, 6)
302 || !IS_TIC_MAGIC(buf)) {
303 returnDB(TGETENT_NO);
305 #if NCURSES_EXT_NUMBERS
306 if (LOW_MSB(buf) == MAGIC2) {
307 convert_numbers = convert_32bits;
308 size_of_numbers = SIZEOF_INT2;
310 max_entry_size = MAX_ENTRY_SIZE1;
311 convert_numbers = convert_16bits;
312 size_of_numbers = SIZEOF_SHORT;
315 if (LOW_MSB(buf) == MAGIC2) {
316 convert_numbers = convert_32bits;
317 size_of_numbers = SIZEOF_32BITS;
319 convert_numbers = convert_16bits;
320 size_of_numbers = SIZEOF_INT2;
325 name_size = MyNumber(buf + 2);
326 bool_count = MyNumber(buf + 4);
327 num_count = MyNumber(buf + 6);
328 str_count = MyNumber(buf + 8);
329 str_size = MyNumber(buf + 10);
332 ("TERMTYPE name_size=%d, bool=%d/%d, num=%d/%d str=%d/%d(%d)",
333 name_size, bool_count, BOOLCOUNT, num_count, NUMCOUNT,
334 str_count, STRCOUNT, str_size));
339 || bool_count > BOOLCOUNT
340 || num_count > NUMCOUNT
341 || str_count > STRCOUNT
343 returnDB(TGETENT_NO);
346 want = (unsigned) (str_size + name_size + 1);
347 /* try to allocate space for the string table */
348 if (str_count * SIZEOF_SHORT >= max_entry_size
349 || (string_table = typeMalloc(char, want)) == 0) {
350 returnDB(TGETENT_NO);
353 /* grab the name (a null-terminated string) */
354 want = Min(MAX_NAME_SIZE, (unsigned) name_size);
355 ptr->str_table = string_table;
356 ptr->term_names = string_table;
357 if ((have = (unsigned) Read(ptr->term_names, want)) != want) {
358 memset(ptr->term_names + have, 0, (size_t) (want - have));
360 ptr->term_names[want] = '\0';
361 string_table += (want + 1);
363 if (have > MAX_NAME_SIZE)
364 offset = (int) (have - MAX_NAME_SIZE);
366 /* grab the booleans */
367 TYPE_CALLOC(NCURSES_SBOOL, Max(BOOLCOUNT, bool_count), ptr->Booleans);
368 if (Read(ptr->Booleans, (unsigned) bool_count) < bool_count) {
369 returnDB(TGETENT_NO);
373 * If booleans end on an odd byte, skip it. The machine they
374 * originally wrote terminfo on must have been a 16-bit
375 * word-oriented machine that would trap out if you tried a
376 * word access off a 2-byte boundary.
378 even_boundary(name_size + bool_count);
380 /* grab the numbers */
381 TYPE_CALLOC(NCURSES_INT2, Max(NUMCOUNT, num_count), ptr->Numbers);
382 if (!read_numbers(buf, num_count)) {
383 returnDB(TGETENT_NO);
385 convert_numbers(buf, ptr->Numbers, num_count);
387 TYPE_CALLOC(char *, Max(STRCOUNT, str_count), ptr->Strings);
390 /* grab the string offsets */
391 if (!read_shorts(buf, str_count)) {
392 returnDB(TGETENT_NO);
394 /* finally, grab the string table itself */
395 if (Read(string_table, (unsigned) str_size) != str_size) {
396 returnDB(TGETENT_NO);
398 if (!convert_strings(buf, ptr->Strings, str_count, str_size,
399 string_table, FALSE)) {
400 returnDB(TGETENT_NO);
405 ptr->num_Booleans = BOOLCOUNT;
406 ptr->num_Numbers = NUMCOUNT;
407 ptr->num_Strings = STRCOUNT;
410 * Read extended entries, if any, after the normal end of terminfo data.
412 even_boundary(str_size);
413 TR(TRACE_DATABASE, ("READ extended_header @%d", offset));
414 if (_nc_user_definable && read_shorts(buf, 5) && valid_shorts(buf, 5)) {
415 int ext_bool_count = MyNumber(buf + 0);
416 int ext_num_count = MyNumber(buf + 2);
417 int ext_str_count = MyNumber(buf + 4);
418 int ext_str_usage = MyNumber(buf + 6);
419 int ext_str_limit = MyNumber(buf + 8);
420 unsigned need = (unsigned) (ext_bool_count + ext_num_count + ext_str_count);
423 if ((int) need >= (max_entry_size / 2)
424 || ext_str_usage >= max_entry_size
425 || ext_str_limit >= max_entry_size
426 || ext_bool_count < 0
430 || ext_str_limit < 0) {
431 returnDB(TGETENT_NO);
434 ptr->num_Booleans = UShort(BOOLCOUNT + ext_bool_count);
435 ptr->num_Numbers = UShort(NUMCOUNT + ext_num_count);
436 ptr->num_Strings = UShort(STRCOUNT + ext_str_count);
438 TYPE_REALLOC(NCURSES_SBOOL, ptr->num_Booleans, ptr->Booleans);
439 TYPE_REALLOC(NCURSES_INT2, ptr->num_Numbers, ptr->Numbers);
440 TYPE_REALLOC(char *, ptr->num_Strings, ptr->Strings);
442 TR(TRACE_DATABASE, ("extended header: "
452 TR(TRACE_DATABASE, ("READ %d extended-booleans @%d",
453 ext_bool_count, offset));
454 if ((ptr->ext_Booleans = UShort(ext_bool_count)) != 0) {
455 if (Read(ptr->Booleans + BOOLCOUNT, (unsigned)
456 ext_bool_count) != ext_bool_count) {
457 returnDB(TGETENT_NO);
460 even_boundary(ext_bool_count);
462 TR(TRACE_DATABASE, ("READ %d extended-numbers @%d",
463 ext_num_count, offset));
464 if ((ptr->ext_Numbers = UShort(ext_num_count)) != 0) {
465 if (!read_numbers(buf, ext_num_count)) {
466 returnDB(TGETENT_NO);
468 TR(TRACE_DATABASE, ("Before converting extended-numbers"));
469 convert_numbers(buf, ptr->Numbers + NUMCOUNT, ext_num_count);
472 TR(TRACE_DATABASE, ("READ extended-offsets @%d", offset));
473 if ((ext_str_count + (int) need) >= (max_entry_size / 2)) {
474 returnDB(TGETENT_NO);
476 if ((ext_str_count || need)
477 && !read_shorts(buf, ext_str_count + (int) need)) {
478 returnDB(TGETENT_NO);
481 TR(TRACE_DATABASE, ("READ %d bytes of extended-strings @%d",
482 ext_str_limit, offset));
485 ptr->ext_str_table = typeMalloc(char, (size_t) ext_str_limit);
486 if (ptr->ext_str_table == 0) {
487 returnDB(TGETENT_NO);
489 if (Read(ptr->ext_str_table, (unsigned) ext_str_limit) != ext_str_limit) {
490 returnDB(TGETENT_NO);
492 TR(TRACE_DATABASE, ("first extended-string is %s", _nc_visbuf(ptr->ext_str_table)));
495 if ((ptr->ext_Strings = UShort(ext_str_count)) != 0) {
496 int check = (ext_bool_count + ext_num_count + ext_str_count);
499 ("Before computing extended-string capabilities "
500 "str_count=%d, ext_str_count=%d",
501 str_count, ext_str_count));
502 if (!convert_strings(buf, ptr->Strings + str_count, ext_str_count,
503 ext_str_limit, ptr->ext_str_table, FALSE)) {
504 returnDB(TGETENT_NO);
506 for (i = ext_str_count - 1; i >= 0; i--) {
507 TR(TRACE_DATABASE, ("MOVE from [%d:%d] %s",
509 _nc_visbuf(ptr->Strings[i + str_count])));
510 ptr->Strings[i + STRCOUNT] = ptr->Strings[i + str_count];
511 if (VALID_STRING(ptr->Strings[i + STRCOUNT])) {
512 base += (int) (strlen(ptr->Strings[i + STRCOUNT]) + 1);
515 TR(TRACE_DATABASE, ("... to [%d] %s",
517 _nc_visbuf(ptr->Strings[i + STRCOUNT])));
519 TR(TRACE_DATABASE, ("Check table-size: %d/%d", check, ext_str_usage));
522 * Phasing in a proper check will be done "later".
524 if (check != ext_str_usage)
525 returnDB(TGETENT_NO);
530 if (ext_str_count >= (max_entry_size / 2)) {
531 returnDB(TGETENT_NO);
533 TYPE_CALLOC(char *, need, ptr->ext_Names);
535 ("ext_NAMES starting @%d in extended_strings, first = %s",
536 base, _nc_visbuf(ptr->ext_str_table + base)));
537 if (!convert_strings(buf + (2 * ext_str_count),
540 ext_str_limit, ptr->ext_str_table + base,
542 returnDB(TGETENT_NO);
547 ("...done reading terminfo bool %d(%d) num %d(%d) str %d(%d)",
548 ptr->num_Booleans, ptr->ext_Booleans,
549 ptr->num_Numbers, ptr->ext_Numbers,
550 ptr->num_Strings, ptr->ext_Strings));
552 TR(TRACE_DATABASE, ("extend: num_Booleans:%d", ptr->num_Booleans));
554 #endif /* NCURSES_XNAMES */
556 TR(TRACE_DATABASE, ("...done reading terminfo bool %d num %d str %d",
557 bool_count, num_count, str_count));
559 TR(TRACE_DATABASE, ("normal: num_Booleans:%d", ptr->num_Booleans));
563 for (i = bool_count; i < BOOLCOUNT; i++)
564 ptr->Booleans[i] = FALSE;
565 for (i = num_count; i < NUMCOUNT; i++)
566 ptr->Numbers[i] = ABSENT_NUMERIC;
567 for (i = str_count; i < STRCOUNT; i++)
568 ptr->Strings[i] = ABSENT_STRING;
570 returnDB(TGETENT_YES);
575 * _nc_read_file_entry(filename, ptr)
577 * Read the compiled terminfo entry in the given file into the
578 * structure pointed to by ptr, allocating space for the string
582 _nc_read_file_entry(const char *const filename, TERMTYPE2 *ptr)
583 /* return 1 if read, 0 if not found or garbled */
588 if (_nc_access(filename, R_OK) < 0
589 || (fp = safe_fopen(filename, BIN_R)) == 0) {
590 TR(TRACE_DATABASE, ("cannot open terminfo %s (errno=%d)", filename, errno));
594 char buffer[MAX_ENTRY_SIZE + 1];
596 limit = (int) fread(buffer, sizeof(char), sizeof(buffer), fp);
598 const char *old_source = _nc_get_source();
600 TR(TRACE_DATABASE, ("read terminfo %s", filename));
601 if (old_source == NULL)
602 _nc_set_source(filename);
603 if ((code = _nc_read_termtype(ptr, buffer, limit)) == TGETENT_NO) {
604 _nc_free_termtype2(ptr);
606 _nc_set_source(old_source);
618 * Return if if we can build the filename of a ".db" file.
621 make_db_filename(char *filename, unsigned limit, const char *const path)
623 static const char suffix[] = DBM_SUFFIX;
625 size_t lens = sizeof(suffix) - 1;
626 size_t size = strlen(path);
627 size_t test = lens + size;
632 && !strcmp(path + size - lens, suffix))
633 _nc_STRCPY(filename, path, limit);
635 _nc_SPRINTF(filename, _nc_SLIMIT(limit) "%s%s", path, suffix);
643 * Return true if we can build the name of a filesystem entry.
646 make_dir_filename(char *filename,
648 const char *const path,
653 #if NCURSES_USE_TERMCAP
654 if (_nc_is_dir_path(path))
657 unsigned need = (unsigned) (LEAF_LEN + 3 + strlen(path) + strlen(name));
660 _nc_SPRINTF(filename, _nc_SLIMIT(limit)
661 "%s/" LEAF_FMT "/%s", path, *name, name);
669 lookup_b64(int *target, const char **source)
674 * ncurses' quickdump writes only RFC 4648 "url/filename-safe" encoding,
675 * but accepts RFC-3548
677 for (j = 0; j < 4; ++j) {
678 int ch = UChar(**source);
680 if (ch >= 'A' && ch <= 'Z') {
681 target[j] = (ch - 'A');
682 } else if (ch >= 'a' && ch <= 'z') {
683 target[j] = 26 + (ch - 'a');
684 } else if (ch >= '0' && ch <= '9') {
685 target[j] = 52 + (ch - '0');
686 } else if (ch == '-' || ch == '+') {
688 } else if (ch == '_' || ch == '/') {
690 } else if (ch == '=') {
702 decode_hex(const char **source)
707 for (nibble = 0; nibble < 2; ++nibble) {
708 int ch = UChar(**source);
711 if (ch >= '0' && ch <= '9') {
713 } else if (ch >= 'A' && ch <= 'F') {
716 } else if (ch >= 'a' && ch <= 'f') {
729 decode_quickdump(char *target, const char *source)
734 if (!strncmp(source, "b64:", (size_t) 4)) {
736 while (*source != '\0') {
738 int ch = lookup_b64(bits, &source);
739 if (ch < 0 || (ch + target - base) >= MAX_ENTRY_SIZE) {
744 *target++ = (char) ((bits[0] << 2) | (bits[1] >> 4));
746 *target++ = (char) ((bits[1] << 4) | (bits[2] >> 2));
748 *target++ = (char) ((bits[2] << 6) | bits[3]);
752 } else if (!strncmp(source, "hex:", (size_t) 4)) {
754 while (*source != '\0') {
755 int ch = decode_hex(&source);
756 if (ch < 0 || (target - base) >= MAX_ENTRY_SIZE) {
760 *target++ = (char) ch;
768 * Build a terminfo pathname and try to read the data. Returns TGETENT_YES on
769 * success, TGETENT_NO on failure.
772 _nc_read_tic_entry(char *filename,
774 const char *const path,
778 int code = TGETENT_NO;
782 char buffer[MAX_ENTRY_SIZE + 1];
786 (T_CALLED("_nc_read_tic_entry(file=%p, path=%s, name=%s)"),
787 filename, path, name));
789 assert(TGETENT_YES == TRUE); /* simplify call for _nc_name_match */
791 if ((used = decode_quickdump(buffer, path)) != 0
792 && (code = _nc_read_termtype(tp, buffer, used)) == TGETENT_YES
793 && (code = _nc_name_match(tp->term_names, name, "|")) == TGETENT_YES) {
794 TR(TRACE_DATABASE, ("loaded quick-dump for %s", name));
795 /* shorten name shown by infocmp */
796 _nc_STRCPY(filename, "$TERMINFO", limit);
799 if (make_db_filename(filename, limit, path)
800 && (capdbp = _nc_db_open(filename, FALSE)) != 0) {
804 char *save = strdup(name);
809 memset(&key, 0, sizeof(key));
811 key.size = strlen(save);
814 * This lookup could return termcap data, which we do not want. We are
815 * looking for compiled (binary) terminfo data.
817 * cgetent uses a two-level lookup. On the first it uses the given
818 * name to return a record containing only the aliases for an entry.
819 * On the second (using that list of aliases as a key), it returns the
820 * content of the terminal description. We expect second lookup to
821 * return data beginning with the same set of aliases.
823 * For compiled terminfo, the list of aliases in the second case will
824 * be null-terminated. A termcap entry will not be, and will run on
825 * into the description. So we can easily distinguish between the two
826 * (source/binary) by checking the lengths.
828 while (_nc_db_get(capdbp, &key, &data) == 0) {
829 char *have = (char *) data.data;
830 used = (int) data.size - 1;
833 if (data.size > key.size
834 && IS_TIC_MAGIC(have)) {
835 code = _nc_read_termtype(tp, have, used);
836 if (code == TGETENT_NO) {
837 _nc_free_termtype2(tp);
844 * Just in case we have a corrupt database, do not waste time with
851 * Prepare for the second level.
858 } else /* may be either filesystem or flat file */
860 if (make_dir_filename(filename, limit, path, name)) {
861 code = _nc_read_file_entry(filename, tp);
863 #if NCURSES_USE_TERMCAP
864 if (code != TGETENT_YES) {
865 code = _nc_read_termcap_entry(name, tp);
866 _nc_SPRINTF(filename, _nc_SLIMIT(PATH_MAX)
867 "%.*s", PATH_MAX - 1, _nc_get_source());
872 #endif /* NCURSES_USE_DATABASE */
875 * Find and read the compiled entry for a given terminal type, if it exists.
876 * We take pains here to make sure no combination of environment variables and
877 * terminal type name can be used to overrun the file buffer.
880 _nc_read_entry2(const char *const name, char *const filename, TERMTYPE2 *const tp)
882 int code = TGETENT_NO;
885 return _nc_read_entry2("", filename, tp);
887 _nc_SPRINTF(filename, _nc_SLIMIT(PATH_MAX)
888 "%.*s", PATH_MAX - 1, name);
890 if (strlen(name) == 0
891 || strcmp(name, ".") == 0
892 || strcmp(name, "..") == 0
893 || _nc_pathlast(name) != 0
894 || strchr(name, NCURSES_PATHSEP) != 0) {
895 TR(TRACE_DATABASE, ("illegal or missing entry name '%s'", name));
897 #if NCURSES_USE_DATABASE
902 _nc_first_db(&state, &offset);
904 while ((path = _nc_next_db(&state, &offset)) != 0) {
905 code = _nc_read_tic_entry(filename, PATH_MAX, path, name, tp);
906 if (code == TGETENT_YES) {
911 #elif NCURSES_USE_TERMCAP
912 if (code != TGETENT_YES) {
913 code = _nc_read_termcap_entry(name, tp);
914 _nc_SPRINTF(filename, _nc_SLIMIT(PATH_MAX)
915 "%.*s", PATH_MAX - 1, _nc_get_source());
922 #if NCURSES_EXT_NUMBERS
924 _nc_read_entry(const char *const name, char *const filename, TERMTYPE *const tp)
928 rc = _nc_read_entry2(name, filename, &dummy);
929 if (rc == TGETENT_YES)
930 _nc_export_termtype2(tp, &dummy);