]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/read_entry.c
ncurses 6.0 - patch 20170617
[ncurses.git] / ncurses / tinfo / read_entry.c
1 /****************************************************************************
2  * Copyright (c) 1998-2016,2017 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  *     and: Thomas E. Dickey                        1996-on                 *
33  ****************************************************************************/
34
35 /*
36  *      read_entry.c -- Routine for reading in a compiled terminfo file
37  */
38
39 #include <curses.priv.h>
40 #include <hashed_db.h>
41
42 #include <tic.h>
43
44 MODULE_ID("$Id: read_entry.c,v 1.143 2017/04/06 22:19:06 tom Exp $")
45
46 #define TYPE_CALLOC(type,elts) typeCalloc(type, (unsigned)(elts))
47
48 #define MyNumber(n) (short) LOW_MSB(n)
49
50 #if NCURSES_USE_DATABASE
51 static void
52 convert_numbers(char *buf, NCURSES_INT2 *Numbers, int count)
53 {
54     int i;
55     for (i = 0; i < count; i++) {
56         if (IS_NEG1(buf + 2 * i))
57             Numbers[i] = ABSENT_NUMERIC;
58         else if (IS_NEG2(buf + 2 * i))
59             Numbers[i] = CANCELLED_NUMERIC;
60         else
61             Numbers[i] = MyNumber(buf + 2 * i);
62         TR(TRACE_DATABASE, ("get Numbers[%d]=%d", i, Numbers[i]));
63     }
64 }
65
66 static void
67 convert_strings(char *buf, char **Strings, int count, int size, char *table)
68 {
69     int i;
70     char *p;
71
72     for (i = 0; i < count; i++) {
73         if (IS_NEG1(buf + 2 * i)) {
74             Strings[i] = ABSENT_STRING;
75         } else if (IS_NEG2(buf + 2 * i)) {
76             Strings[i] = CANCELLED_STRING;
77         } else if (MyNumber(buf + 2 * i) > size) {
78             Strings[i] = ABSENT_STRING;
79         } else {
80             Strings[i] = (MyNumber(buf + 2 * i) + table);
81             TR(TRACE_DATABASE, ("Strings[%d] = %s", i, _nc_visbuf(Strings[i])));
82         }
83
84         /* make sure all strings are NUL terminated */
85         if (VALID_STRING(Strings[i])) {
86             for (p = Strings[i]; p <= table + size; p++)
87                 if (*p == '\0')
88                     break;
89             /* if there is no NUL, ignore the string */
90             if (p > table + size)
91                 Strings[i] = ABSENT_STRING;
92         }
93     }
94 }
95
96 static int
97 fake_read(char *src, int *offset, int limit, char *dst, unsigned want)
98 {
99     int have = (limit - *offset);
100
101     if (have > 0) {
102         if ((int) want > have)
103             want = (unsigned) have;
104         memcpy(dst, src + *offset, (size_t) want);
105         *offset += (int) want;
106     } else {
107         want = 0;
108     }
109     return (int) want;
110 }
111
112 #define Read(buf, count) fake_read(buffer, &offset, limit, (char *) buf, (unsigned) count)
113
114 #define read_shorts(buf, count) \
115         (Read(buf, (count)*2) == (int) (count)*2)
116
117 #define even_boundary(value) \
118     if ((value) % 2 != 0) Read(buf, 1)
119 #endif
120
121 NCURSES_EXPORT(void)
122 _nc_init_termtype(TERMTYPE2 *const tp)
123 {
124     unsigned i;
125
126 #if NCURSES_XNAMES
127     tp->num_Booleans = BOOLCOUNT;
128     tp->num_Numbers = NUMCOUNT;
129     tp->num_Strings = STRCOUNT;
130     tp->ext_Booleans = 0;
131     tp->ext_Numbers = 0;
132     tp->ext_Strings = 0;
133 #endif
134     if (tp->Booleans == 0)
135         TYPE_MALLOC(NCURSES_SBOOL, BOOLCOUNT, tp->Booleans);
136     if (tp->Numbers == 0)
137         TYPE_MALLOC(NCURSES_INT2, NUMCOUNT, tp->Numbers);
138     if (tp->Strings == 0)
139         TYPE_MALLOC(char *, STRCOUNT, tp->Strings);
140
141     for_each_boolean(i, tp)
142         tp->Booleans[i] = FALSE;
143
144     for_each_number(i, tp)
145         tp->Numbers[i] = ABSENT_NUMERIC;
146
147     for_each_string(i, tp)
148         tp->Strings[i] = ABSENT_STRING;
149 }
150
151 #if NCURSES_USE_DATABASE
152 #if NCURSES_XNAMES
153 static bool
154 valid_shorts(char *buffer, int limit)
155 {
156     bool result = FALSE;
157     int n;
158     for (n = 0; n < limit; ++n) {
159         if (MyNumber(buffer + (n * 2)) > 0) {
160             result = TRUE;
161             break;
162         }
163     }
164     return result;
165 }
166 #endif
167
168 /*
169  * Return TGETENT_YES if read, TGETENT_NO if not found or garbled.
170  */
171 NCURSES_EXPORT(int)
172 _nc_read_termtype(TERMTYPE2 *ptr, char *buffer, int limit)
173 {
174     int offset = 0;
175     int name_size, bool_count, num_count, str_count, str_size;
176     int i;
177     char buf[MAX_ENTRY_SIZE + 2];
178     char *string_table;
179     unsigned want, have;
180
181     TR(TRACE_DATABASE,
182        (T_CALLED("_nc_read_termtype(ptr=%p, buffer=%p, limit=%d)"),
183         (void *) ptr, buffer, limit));
184
185     TR(TRACE_DATABASE, ("READ termtype header @%d", offset));
186
187     memset(ptr, 0, sizeof(*ptr));
188
189     /* grab the header */
190     if (!read_shorts(buf, 6)
191         || !IS_TIC_MAGIC(buf)) {
192         returnDB(TGETENT_NO);
193     }
194
195     /* *INDENT-EQLS* */
196     name_size  = MyNumber(buf + 2);
197     bool_count = MyNumber(buf + 4);
198     num_count  = MyNumber(buf + 6);
199     str_count  = MyNumber(buf + 8);
200     str_size   = MyNumber(buf + 10);
201
202     TR(TRACE_DATABASE,
203        ("TERMTYPE name_size=%d, bool=%d/%d, num=%d/%d str=%d/%d(%d)",
204         name_size, bool_count, BOOLCOUNT, num_count, NUMCOUNT,
205         str_count, STRCOUNT, str_size));
206     if (name_size < 0
207         || bool_count < 0
208         || num_count < 0
209         || str_count < 0
210         || str_size < 0) {
211         returnDB(TGETENT_NO);
212     }
213
214     want = (unsigned) (str_size + name_size + 1);
215     /* try to allocate space for the string table */
216     if (str_count * 2 >= MAX_ENTRY_SIZE
217         || (string_table = typeMalloc(char, want)) == 0) {
218         returnDB(TGETENT_NO);
219     }
220
221     /* grab the name (a null-terminated string) */
222     want = min(MAX_NAME_SIZE, (unsigned) name_size);
223     ptr->str_table = string_table;
224     ptr->term_names = string_table;
225     if ((have = (unsigned) Read(ptr->term_names, want)) != want) {
226         memset(ptr->term_names + have, 0, (size_t) (want - have));
227     }
228     ptr->term_names[want] = '\0';
229     string_table += (want + 1);
230
231     if (have > MAX_NAME_SIZE)
232         offset = (int) (have - MAX_NAME_SIZE);
233
234     /* grab the booleans */
235     if ((ptr->Booleans = TYPE_CALLOC(NCURSES_SBOOL,
236                                      max(BOOLCOUNT, bool_count))) == 0
237         || Read(ptr->Booleans, (unsigned) bool_count) < bool_count) {
238         returnDB(TGETENT_NO);
239     }
240
241     /*
242      * If booleans end on an odd byte, skip it.  The machine they
243      * originally wrote terminfo on must have been a 16-bit
244      * word-oriented machine that would trap out if you tried a
245      * word access off a 2-byte boundary.
246      */
247     even_boundary(name_size + bool_count);
248
249     /* grab the numbers */
250     if (!(ptr->Numbers = TYPE_CALLOC(NCURSES_INT2, max(NUMCOUNT, num_count)))
251         || !read_shorts(buf, num_count)) {
252         returnDB(TGETENT_NO);
253     }
254     convert_numbers(buf, ptr->Numbers, num_count);
255
256     if ((ptr->Strings = TYPE_CALLOC(char *, max(STRCOUNT, str_count))) == 0) {
257         returnDB(TGETENT_NO);
258     }
259
260     if (str_count) {
261         /* grab the string offsets */
262         if (!read_shorts(buf, str_count)) {
263             returnDB(TGETENT_NO);
264         }
265         /* finally, grab the string table itself */
266         if (Read(string_table, (unsigned) str_size) != str_size) {
267             returnDB(TGETENT_NO);
268         }
269         convert_strings(buf, ptr->Strings, str_count, str_size, string_table);
270     }
271 #if NCURSES_XNAMES
272
273     ptr->num_Booleans = BOOLCOUNT;
274     ptr->num_Numbers = NUMCOUNT;
275     ptr->num_Strings = STRCOUNT;
276
277     /*
278      * Read extended entries, if any, after the normal end of terminfo data.
279      */
280     even_boundary(str_size);
281     TR(TRACE_DATABASE, ("READ extended_header @%d", offset));
282     if (_nc_user_definable && read_shorts(buf, 5) && valid_shorts(buf, 5)) {
283         int ext_bool_count = MyNumber(buf + 0);
284         int ext_num_count = MyNumber(buf + 2);
285         int ext_str_count = MyNumber(buf + 4);
286         int ext_str_size = MyNumber(buf + 6);
287         int ext_str_limit = MyNumber(buf + 8);
288         unsigned need = (unsigned) (ext_bool_count + ext_num_count + ext_str_count);
289         int base = 0;
290
291         if (need >= (MAX_ENTRY_SIZE / 2)
292             || ext_str_size >= MAX_ENTRY_SIZE
293             || ext_str_limit >= MAX_ENTRY_SIZE
294             || ext_bool_count < 0
295             || ext_num_count < 0
296             || ext_str_count < 0
297             || ext_str_size < 0
298             || ext_str_limit < 0) {
299             returnDB(TGETENT_NO);
300         }
301
302         ptr->num_Booleans = UShort(BOOLCOUNT + ext_bool_count);
303         ptr->num_Numbers = UShort(NUMCOUNT + ext_num_count);
304         ptr->num_Strings = UShort(STRCOUNT + ext_str_count);
305
306         TYPE_REALLOC(NCURSES_SBOOL, ptr->num_Booleans, ptr->Booleans);
307         TYPE_REALLOC(NCURSES_INT2, ptr->num_Numbers, ptr->Numbers);
308         TYPE_REALLOC(char *, ptr->num_Strings, ptr->Strings);
309
310         TR(TRACE_DATABASE, ("extended header is %d/%d/%d(%d:%d)",
311                             ext_bool_count, ext_num_count, ext_str_count,
312                             ext_str_size, ext_str_limit));
313
314         TR(TRACE_DATABASE, ("READ %d extended-booleans @%d",
315                             ext_bool_count, offset));
316         if ((ptr->ext_Booleans = UShort(ext_bool_count)) != 0) {
317             if (Read(ptr->Booleans + BOOLCOUNT, (unsigned)
318                      ext_bool_count) != ext_bool_count) {
319                 returnDB(TGETENT_NO);
320             }
321         }
322         even_boundary(ext_bool_count);
323
324         TR(TRACE_DATABASE, ("READ %d extended-numbers @%d",
325                             ext_num_count, offset));
326         if ((ptr->ext_Numbers = UShort(ext_num_count)) != 0) {
327             if (!read_shorts(buf, ext_num_count)) {
328                 returnDB(TGETENT_NO);
329             }
330             TR(TRACE_DATABASE, ("Before converting extended-numbers"));
331             convert_numbers(buf, ptr->Numbers + NUMCOUNT, ext_num_count);
332         }
333
334         TR(TRACE_DATABASE, ("READ extended-offsets @%d", offset));
335         if ((unsigned) (ext_str_count + (int) need) >= (MAX_ENTRY_SIZE / 2)) {
336             returnDB(TGETENT_NO);
337         }
338         if ((ext_str_count || need)
339             && !read_shorts(buf, ext_str_count + (int) need)) {
340             returnDB(TGETENT_NO);
341         }
342
343         TR(TRACE_DATABASE, ("READ %d bytes of extended-strings @%d",
344                             ext_str_limit, offset));
345
346         if (ext_str_limit) {
347             ptr->ext_str_table = typeMalloc(char, (size_t) ext_str_limit);
348             if (ptr->ext_str_table == 0) {
349                 returnDB(TGETENT_NO);
350             }
351             if (Read(ptr->ext_str_table, (unsigned) ext_str_limit) != ext_str_limit) {
352                 returnDB(TGETENT_NO);
353             }
354             TR(TRACE_DATABASE, ("first extended-string is %s", _nc_visbuf(ptr->ext_str_table)));
355         }
356
357         if ((ptr->ext_Strings = UShort(ext_str_count)) != 0) {
358             TR(TRACE_DATABASE,
359                ("Before computing extended-string capabilities str_count=%d, ext_str_count=%d",
360                 str_count, ext_str_count));
361             convert_strings(buf, ptr->Strings + str_count, ext_str_count,
362                             ext_str_limit, ptr->ext_str_table);
363             for (i = ext_str_count - 1; i >= 0; i--) {
364                 TR(TRACE_DATABASE, ("MOVE from [%d:%d] %s",
365                                     i, i + str_count,
366                                     _nc_visbuf(ptr->Strings[i + str_count])));
367                 ptr->Strings[i + STRCOUNT] = ptr->Strings[i + str_count];
368                 if (VALID_STRING(ptr->Strings[i + STRCOUNT]))
369                     base += (int) (strlen(ptr->Strings[i + STRCOUNT]) + 1);
370                 TR(TRACE_DATABASE, ("... to    [%d] %s",
371                                     i + STRCOUNT,
372                                     _nc_visbuf(ptr->Strings[i + STRCOUNT])));
373             }
374         }
375
376         if (need) {
377             if (ext_str_count >= (MAX_ENTRY_SIZE / 2)) {
378                 returnDB(TGETENT_NO);
379             }
380             if ((ptr->ext_Names = TYPE_CALLOC(char *, need)) == 0) {
381                 returnDB(TGETENT_NO);
382             }
383             TR(TRACE_DATABASE,
384                ("ext_NAMES starting @%d in extended_strings, first = %s",
385                 base, _nc_visbuf(ptr->ext_str_table + base)));
386             convert_strings(buf + (2 * ext_str_count),
387                             ptr->ext_Names,
388                             (int) need,
389                             ext_str_limit, ptr->ext_str_table + base);
390         }
391
392         TR(TRACE_DATABASE,
393            ("...done reading terminfo bool %d(%d) num %d(%d) str %d(%d)",
394             ptr->num_Booleans, ptr->ext_Booleans,
395             ptr->num_Numbers, ptr->ext_Numbers,
396             ptr->num_Strings, ptr->ext_Strings));
397
398         TR(TRACE_DATABASE, ("extend: num_Booleans:%d", ptr->num_Booleans));
399     } else
400 #endif /* NCURSES_XNAMES */
401     {
402         TR(TRACE_DATABASE, ("...done reading terminfo bool %d num %d str %d",
403                             bool_count, num_count, str_count));
404 #if NCURSES_XNAMES
405         TR(TRACE_DATABASE, ("normal: num_Booleans:%d", ptr->num_Booleans));
406 #endif
407     }
408
409     for (i = bool_count; i < BOOLCOUNT; i++)
410         ptr->Booleans[i] = FALSE;
411     for (i = num_count; i < NUMCOUNT; i++)
412         ptr->Numbers[i] = ABSENT_NUMERIC;
413     for (i = str_count; i < STRCOUNT; i++)
414         ptr->Strings[i] = ABSENT_STRING;
415
416     returnDB(TGETENT_YES);
417 }
418
419 /*
420  *      int
421  *      _nc_read_file_entry(filename, ptr)
422  *
423  *      Read the compiled terminfo entry in the given file into the
424  *      structure pointed to by ptr, allocating space for the string
425  *      table.
426  */
427 NCURSES_EXPORT(int)
428 _nc_read_file_entry(const char *const filename, TERMTYPE2 *ptr)
429 /* return 1 if read, 0 if not found or garbled */
430 {
431     FILE *fp = 0;
432     int code;
433
434     if (_nc_access(filename, R_OK) < 0
435         || (fp = fopen(filename, "rb")) == 0) {
436         TR(TRACE_DATABASE, ("cannot open terminfo %s (errno=%d)", filename, errno));
437         code = TGETENT_NO;
438     } else {
439         int limit;
440         char buffer[MAX_ENTRY_SIZE + 1];
441
442         if ((limit = (int) fread(buffer, sizeof(char), sizeof(buffer), fp))
443             > 0) {
444
445             TR(TRACE_DATABASE, ("read terminfo %s", filename));
446             if ((code = _nc_read_termtype(ptr, buffer, limit)) == TGETENT_NO) {
447                 _nc_free_termtype2(ptr);
448             }
449         } else {
450             code = TGETENT_NO;
451         }
452         fclose(fp);
453     }
454
455     return (code);
456 }
457
458 #if USE_HASHED_DB
459 /*
460  * Return if if we can build the filename of a ".db" file.
461  */
462 static bool
463 make_db_filename(char *filename, unsigned limit, const char *const path)
464 {
465     static const char suffix[] = DBM_SUFFIX;
466
467     size_t lens = sizeof(suffix) - 1;
468     size_t size = strlen(path);
469     size_t test = lens + size;
470     bool result = FALSE;
471
472     if (test < limit) {
473         if (size >= lens
474             && !strcmp(path + size - lens, suffix))
475             _nc_STRCPY(filename, path, limit);
476         else
477             _nc_SPRINTF(filename, _nc_SLIMIT(limit) "%s%s", path, suffix);
478         result = TRUE;
479     }
480     return result;
481 }
482 #endif
483
484 /*
485  * Return true if we can build the name of a filesystem entry.
486  */
487 static bool
488 make_dir_filename(char *filename,
489                   unsigned limit,
490                   const char *const path,
491                   const char *name)
492 {
493     bool result = FALSE;
494
495 #if NCURSES_USE_TERMCAP
496     if (_nc_is_dir_path(path))
497 #endif
498     {
499         unsigned need = (unsigned) (LEAF_LEN + 3 + strlen(path) + strlen(name));
500
501         if (need <= limit) {
502             _nc_SPRINTF(filename, _nc_SLIMIT(limit)
503                         "%s/" LEAF_FMT "/%s", path, *name, name);
504             result = TRUE;
505         }
506     }
507     return result;
508 }
509
510 static int
511 lookup_b64(int *target, const char **source)
512 {
513     int result = 3;
514     int j;
515     /*
516      * ncurses' quickdump writes only RFC 4648 "url/filename-safe" encoding,
517      * but accepts RFC-3548
518      */
519     for (j = 0; j < 4; ++j) {
520         int ch = UChar(**source);
521         *source += 1;
522         if (ch >= 'A' && ch <= 'Z') {
523             target[j] = (ch - 'A');
524         } else if (ch >= 'a' && ch <= 'z') {
525             target[j] = 26 + (ch - 'a');
526         } else if (ch >= '0' && ch <= '9') {
527             target[j] = 52 + (ch - '0');
528         } else if (ch == '-' || ch == '+') {
529             target[j] = 62;
530         } else if (ch == '_' || ch == '/') {
531             target[j] = 63;
532         } else if (ch == '=') {
533             target[j] = 64;
534             result--;
535         } else {
536             result = -1;
537             break;
538         }
539     }
540     return result;
541 }
542
543 static int
544 decode_hex(const char **source)
545 {
546     int result = 0;
547     int nibble;
548     int ch;
549
550     for (nibble = 0; nibble < 2; ++nibble) {
551         result <<= 4;
552         ch = UChar(**source);
553         *source += 1;
554         if (ch >= '0' && ch <= '9') {
555             ch -= '0';
556         } else if (ch >= 'A' && ch <= 'F') {
557             ch -= 'A';
558             ch += 10;
559         } else if (ch >= 'a' && ch <= 'f') {
560             ch -= 'a';
561             ch += 10;
562         } else {
563             result = -1;
564             break;
565         }
566         result |= ch;
567     }
568     return result;
569 }
570
571 static int
572 decode_quickdump(char *target, const char *source)
573 {
574     char *base = target;
575     int result = 0;
576
577     if (!strncmp(source, "b64:", 4)) {
578         source += 4;
579         while (*source != '\0') {
580             int bits[4];
581             int ch = lookup_b64(bits, &source);
582             if (ch < 0 || (ch + target - base) >= MAX_ENTRY_SIZE) {
583                 result = 0;
584                 break;
585             }
586             result += ch;
587             *target++ = (char) ((bits[0] << 2) | (bits[1] >> 4));
588             if (bits[2] < 64) {
589                 *target++ = (char) ((bits[1] << 4) | (bits[2] >> 2));
590                 if (bits[3] < 64) {
591                     *target++ = (char) ((bits[2] << 6) | bits[3]);
592                 }
593             }
594         }
595     } else if (!strncmp(source, "hex:", 4)) {
596         source += 4;
597         while (*source != '\0') {
598             int ch = decode_hex(&source);
599             if (ch < 0 || (target - base) >= MAX_ENTRY_SIZE) {
600                 result = 0;
601                 break;
602             }
603             *target++ = (char) ch;
604             ++result;
605         }
606     }
607     return result;
608 }
609
610 /*
611  * Build a terminfo pathname and try to read the data.  Returns TGETENT_YES on
612  * success, TGETENT_NO on failure.
613  */
614 static int
615 _nc_read_tic_entry(char *filename,
616                    unsigned limit,
617                    const char *const path,
618                    const char *name,
619                    TERMTYPE2 *const tp)
620 {
621     int code = TGETENT_NO;
622 #if USE_HASHED_DB
623     DB *capdbp;
624 #endif
625     char buffer[MAX_ENTRY_SIZE + 1];
626     int used;
627
628     TR(TRACE_DATABASE,
629        (T_CALLED("_nc_read_tic_entry(file=%p, path=%s, name=%s)"),
630         filename, path, name));
631
632     if ((used = decode_quickdump(buffer, path)) != 0
633         && (code = _nc_read_termtype(tp, buffer, used)) == TGETENT_YES
634         && _nc_name_match(tp->term_names, name, "|")) {
635         TR(TRACE_DATABASE, ("loaded quick-dump for %s", name));
636     } else
637 #if USE_HASHED_DB
638         if (make_db_filename(filename, limit, path)
639             && (capdbp = _nc_db_open(filename, FALSE)) != 0) {
640
641         DBT key, data;
642         int reccnt = 0;
643         char *save = strdup(name);
644
645         memset(&key, 0, sizeof(key));
646         key.data = save;
647         key.size = strlen(save);
648
649         /*
650          * This lookup could return termcap data, which we do not want.  We are
651          * looking for compiled (binary) terminfo data.
652          *
653          * cgetent uses a two-level lookup.  On the first it uses the given
654          * name to return a record containing only the aliases for an entry. 
655          * On the second (using that list of aliases as a key), it returns the
656          * content of the terminal description.  We expect second lookup to
657          * return data beginning with the same set of aliases.
658          *
659          * For compiled terminfo, the list of aliases in the second case will
660          * be null-terminated.  A termcap entry will not be, and will run on
661          * into the description.  So we can easily distinguish between the two
662          * (source/binary) by checking the lengths.
663          */
664         while (_nc_db_get(capdbp, &key, &data) == 0) {
665             char *have = (char *) data.data;
666             used = (int) data.size - 1;
667
668             if (*have++ == 0) {
669                 if (data.size > key.size
670                     && IS_TIC_MAGIC(have)) {
671                     code = _nc_read_termtype(tp, have, used);
672                     if (code == TGETENT_NO) {
673                         _nc_free_termtype(tp);
674                     }
675                 }
676                 break;
677             }
678
679             /*
680              * Just in case we have a corrupt database, do not waste time with
681              * it.
682              */
683             if (++reccnt >= 3)
684                 break;
685
686             /*
687              * Prepare for the second level.
688              */
689             key.data = have;
690             key.size = used;
691         }
692
693         free(save);
694     } else                      /* may be either filesystem or flat file */
695 #endif
696     if (make_dir_filename(filename, limit, path, name)) {
697         code = _nc_read_file_entry(filename, tp);
698     }
699 #if NCURSES_USE_TERMCAP
700     else if (code != TGETENT_YES) {
701         code = _nc_read_termcap_entry(name, tp);
702         _nc_SPRINTF(filename, _nc_SLIMIT(PATH_MAX)
703                     "%.*s", PATH_MAX - 1, _nc_get_source());
704     }
705 #endif
706     returnDB(code);
707 }
708 #endif /* NCURSES_USE_DATABASE */
709
710 /*
711  * Find and read the compiled entry for a given terminal type, if it exists. 
712  * We take pains here to make sure no combination of environment variables and
713  * terminal type name can be used to overrun the file buffer.
714  */
715 NCURSES_EXPORT(int)
716 _nc_read_entry2(const char *const name, char *const filename, TERMTYPE2 *const tp)
717 {
718     int code = TGETENT_NO;
719
720     _nc_SPRINTF(filename, _nc_SLIMIT(PATH_MAX)
721                 "%.*s", PATH_MAX - 1, name);
722
723     if (strlen(name) == 0
724         || strcmp(name, ".") == 0
725         || strcmp(name, "..") == 0
726         || _nc_pathlast(name) != 0
727         || strchr(name, NCURSES_PATHSEP) != 0) {
728         TR(TRACE_DATABASE, ("illegal or missing entry name '%s'", name));
729     } else {
730 #if NCURSES_USE_DATABASE
731         DBDIRS state;
732         int offset;
733         const char *path;
734
735         _nc_first_db(&state, &offset);
736         code = TGETENT_ERR;
737         while ((path = _nc_next_db(&state, &offset)) != 0) {
738             code = _nc_read_tic_entry(filename, PATH_MAX, path, name, tp);
739             if (code == TGETENT_YES) {
740                 _nc_last_db();
741                 break;
742             }
743         }
744 #elif NCURSES_USE_TERMCAP
745         if (code != TGETENT_YES) {
746             code = _nc_read_termcap_entry(name, tp);
747             _nc_SPRINTF(filename, _nc_SLIMIT(PATH_MAX)
748                         "%.*s", PATH_MAX - 1, _nc_get_source());
749         }
750 #endif
751     }
752     return code;
753 }
754
755 #if NCURSES_EXT_NUMBERS
756 /*
757  * This entrypoint is used by tack.
758  */
759 NCURSES_EXPORT(int)
760 _nc_read_entry(const char *const name, char *const filename, TERMTYPE *const tp)
761 {
762     TERMTYPE2 dummy;
763     int rc;
764     rc = _nc_read_entry2(name, filename, &dummy);
765     if (rc == TGETENT_YES)
766         _nc_export_termtype2(tp, &dummy);
767     return rc;
768 }
769 #endif