]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/read_entry.c
ncurses 5.9 - patch 20121222
[ncurses.git] / ncurses / tinfo / read_entry.c
1 /****************************************************************************
2  * Copyright (c) 1998-2011,2012 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.121 2012/11/18 01:18:47 tom Exp $")
45
46 #define TYPE_CALLOC(type,elts) typeCalloc(type, (unsigned)(elts))
47
48 #if USE_DATABASE
49 static void
50 convert_shorts(char *buf, short *Numbers, int count)
51 {
52     int i;
53     for (i = 0; i < count; i++) {
54         if (IS_NEG1(buf + 2 * i))
55             Numbers[i] = ABSENT_NUMERIC;
56         else if (IS_NEG2(buf + 2 * i))
57             Numbers[i] = CANCELLED_NUMERIC;
58         else
59             Numbers[i] = (short) LOW_MSB(buf + 2 * i);
60         TR(TRACE_DATABASE, ("get Numbers[%d]=%d", i, Numbers[i]));
61     }
62 }
63
64 static void
65 convert_strings(char *buf, char **Strings, int count, int size, char *table)
66 {
67     int i;
68     char *p;
69
70     for (i = 0; i < count; i++) {
71         if (IS_NEG1(buf + 2 * i)) {
72             Strings[i] = ABSENT_STRING;
73         } else if (IS_NEG2(buf + 2 * i)) {
74             Strings[i] = CANCELLED_STRING;
75         } else if ((int) LOW_MSB(buf + 2 * i) > size) {
76             Strings[i] = ABSENT_STRING;
77         } else {
78             Strings[i] = (LOW_MSB(buf + 2 * i) + table);
79             TR(TRACE_DATABASE, ("Strings[%d] = %s", i, _nc_visbuf(Strings[i])));
80         }
81
82         /* make sure all strings are NUL terminated */
83         if (VALID_STRING(Strings[i])) {
84             for (p = Strings[i]; p <= table + size; p++)
85                 if (*p == '\0')
86                     break;
87             /* if there is no NUL, ignore the string */
88             if (p > table + size)
89                 Strings[i] = ABSENT_STRING;
90         }
91     }
92 }
93
94 static int
95 fake_read(char *src, int *offset, int limit, char *dst, unsigned want)
96 {
97     int have = (limit - *offset);
98
99     if (have > 0) {
100         if ((int) want > have)
101             want = (unsigned) have;
102         memcpy(dst, src + *offset, (size_t) want);
103         *offset += (int) want;
104     } else {
105         want = 0;
106     }
107     return (int) want;
108 }
109
110 #define Read(buf, count) fake_read(buffer, &offset, limit, buf, (unsigned) count)
111
112 #define read_shorts(buf, count) \
113         (Read(buf, (count)*2) == (int) (count)*2)
114
115 #define even_boundary(value) \
116     if ((value) % 2 != 0) Read(buf, 1)
117
118 /*
119  * Return TGETENT_YES if read, TGETENT_NO if not found or garbled.
120  */
121 NCURSES_EXPORT(int)
122 _nc_read_termtype(TERMTYPE *ptr, char *buffer, int limit)
123 {
124     int offset = 0;
125     int name_size, bool_count, num_count, str_count, str_size;
126     int i;
127     char buf[MAX_ENTRY_SIZE + 1];
128     char *string_table;
129     unsigned want, have;
130
131     TR(TRACE_DATABASE, ("READ termtype header @%d", offset));
132
133     memset(ptr, 0, sizeof(*ptr));
134
135     /* grab the header */
136     if (!read_shorts(buf, 6)
137         || !IS_TIC_MAGIC(buf)) {
138         return (TGETENT_NO);
139     }
140
141     name_size = LOW_MSB(buf + 2);
142     bool_count = LOW_MSB(buf + 4);
143     num_count = LOW_MSB(buf + 6);
144     str_count = LOW_MSB(buf + 8);
145     str_size = LOW_MSB(buf + 10);
146
147     TR(TRACE_DATABASE,
148        ("TERMTYPE name_size=%d, bool=%d/%d, num=%d/%d str=%d/%d(%d)",
149         name_size, bool_count, BOOLCOUNT, num_count, NUMCOUNT,
150         str_count, STRCOUNT, str_size));
151     if (name_size < 0
152         || bool_count < 0
153         || num_count < 0
154         || str_count < 0
155         || str_size < 0) {
156         return (TGETENT_NO);
157     }
158
159     want = (unsigned) (str_size + name_size + 1);
160     if (str_size) {
161         /* try to allocate space for the string table */
162         if (str_count * 2 >= (int) sizeof(buf)
163             || (string_table = typeMalloc(char, want)) == 0) {
164             return (TGETENT_NO);
165         }
166     } else {
167         str_count = 0;
168         if ((string_table = typeMalloc(char, want)) == 0) {
169             return (TGETENT_NO);
170         }
171     }
172
173     /* grab the name (a null-terminated string) */
174     want = min(MAX_NAME_SIZE, (unsigned) name_size);
175     ptr->str_table = string_table;
176     ptr->term_names = string_table;
177     if ((have = (unsigned) Read(ptr->term_names, want)) != want) {
178         memset(ptr->term_names + have, 0, (size_t) (want - have));
179     }
180     ptr->term_names[want] = '\0';
181     string_table += (want + 1);
182
183     if (have > MAX_NAME_SIZE)
184         offset = (int) (have - MAX_NAME_SIZE);
185
186     /* grab the booleans */
187     if ((ptr->Booleans = TYPE_CALLOC(NCURSES_SBOOL,
188                                      max(BOOLCOUNT, bool_count))) == 0
189         || Read(ptr->Booleans, (unsigned) bool_count) < bool_count) {
190         return (TGETENT_NO);
191     }
192
193     /*
194      * If booleans end on an odd byte, skip it.  The machine they
195      * originally wrote terminfo on must have been a 16-bit
196      * word-oriented machine that would trap out if you tried a
197      * word access off a 2-byte boundary.
198      */
199     even_boundary(name_size + bool_count);
200
201     /* grab the numbers */
202     if ((ptr->Numbers = TYPE_CALLOC(short, max(NUMCOUNT, num_count))) == 0
203         || !read_shorts(buf, num_count)) {
204         return (TGETENT_NO);
205     }
206     convert_shorts(buf, ptr->Numbers, num_count);
207
208     if ((ptr->Strings = TYPE_CALLOC(char *, max(STRCOUNT, str_count))) == 0)
209           return (TGETENT_NO);
210
211     if (str_count) {
212         /* grab the string offsets */
213         if (!read_shorts(buf, str_count)) {
214             return (TGETENT_NO);
215         }
216         /* finally, grab the string table itself */
217         if (Read(string_table, (unsigned) str_size) != str_size)
218             return (TGETENT_NO);
219         convert_strings(buf, ptr->Strings, str_count, str_size, string_table);
220     }
221 #if NCURSES_XNAMES
222
223     ptr->num_Booleans = BOOLCOUNT;
224     ptr->num_Numbers = NUMCOUNT;
225     ptr->num_Strings = STRCOUNT;
226
227     /*
228      * Read extended entries, if any, after the normal end of terminfo data.
229      */
230     even_boundary(str_size);
231     TR(TRACE_DATABASE, ("READ extended_header @%d", offset));
232     if (_nc_user_definable && read_shorts(buf, 5)) {
233         int ext_bool_count = LOW_MSB(buf + 0);
234         int ext_num_count = LOW_MSB(buf + 2);
235         int ext_str_count = LOW_MSB(buf + 4);
236         int ext_str_size = LOW_MSB(buf + 6);
237         int ext_str_limit = LOW_MSB(buf + 8);
238         unsigned need = (unsigned) (ext_bool_count + ext_num_count + ext_str_count);
239         int base = 0;
240
241         if (need >= sizeof(buf)
242             || ext_str_size >= (int) sizeof(buf)
243             || ext_str_limit >= (int) sizeof(buf)
244             || ext_bool_count < 0
245             || ext_num_count < 0
246             || ext_str_count < 0
247             || ext_str_size < 0
248             || ext_str_limit < 0)
249             return (TGETENT_NO);
250
251         ptr->num_Booleans = UShort(BOOLCOUNT + ext_bool_count);
252         ptr->num_Numbers = UShort(NUMCOUNT + ext_num_count);
253         ptr->num_Strings = UShort(STRCOUNT + ext_str_count);
254
255         TYPE_REALLOC(NCURSES_SBOOL, ptr->num_Booleans, ptr->Booleans);
256         TYPE_REALLOC(short, ptr->num_Numbers, ptr->Numbers);
257         TYPE_REALLOC(char *, ptr->num_Strings, ptr->Strings);
258
259         TR(TRACE_DATABASE, ("extended header is %d/%d/%d(%d:%d)",
260                             ext_bool_count, ext_num_count, ext_str_count,
261                             ext_str_size, ext_str_limit));
262
263         TR(TRACE_DATABASE, ("READ %d extended-booleans @%d",
264                             ext_bool_count, offset));
265         if ((ptr->ext_Booleans = UShort(ext_bool_count)) != 0) {
266             if (Read(ptr->Booleans + BOOLCOUNT, (unsigned)
267                      ext_bool_count) != ext_bool_count)
268                 return (TGETENT_NO);
269         }
270         even_boundary(ext_bool_count);
271
272         TR(TRACE_DATABASE, ("READ %d extended-numbers @%d",
273                             ext_num_count, offset));
274         if ((ptr->ext_Numbers = UShort(ext_num_count)) != 0) {
275             if (!read_shorts(buf, ext_num_count))
276                 return (TGETENT_NO);
277             TR(TRACE_DATABASE, ("Before converting extended-numbers"));
278             convert_shorts(buf, ptr->Numbers + NUMCOUNT, ext_num_count);
279         }
280
281         TR(TRACE_DATABASE, ("READ extended-offsets @%d", offset));
282         if ((unsigned) (ext_str_count + (int) need) >= (MAX_ENTRY_SIZE / 2))
283             return (TGETENT_NO);
284         if ((ext_str_count || need)
285             && !read_shorts(buf, ext_str_count + (int) need))
286             return (TGETENT_NO);
287
288         TR(TRACE_DATABASE, ("READ %d bytes of extended-strings @%d",
289                             ext_str_limit, offset));
290
291         if (ext_str_limit) {
292             ptr->ext_str_table = typeMalloc(char, (size_t) ext_str_limit);
293             if (ptr->ext_str_table == 0)
294                 return (TGETENT_NO);
295             if (Read(ptr->ext_str_table, (unsigned) ext_str_limit) != ext_str_limit)
296                 return (TGETENT_NO);
297             TR(TRACE_DATABASE, ("first extended-string is %s", _nc_visbuf(ptr->ext_str_table)));
298         }
299
300         if ((ptr->ext_Strings = UShort(ext_str_count)) != 0) {
301             TR(TRACE_DATABASE,
302                ("Before computing extended-string capabilities str_count=%d, ext_str_count=%d",
303                 str_count, ext_str_count));
304             convert_strings(buf, ptr->Strings + str_count, ext_str_count,
305                             ext_str_limit, ptr->ext_str_table);
306             for (i = ext_str_count - 1; i >= 0; i--) {
307                 TR(TRACE_DATABASE, ("MOVE from [%d:%d] %s",
308                                     i, i + str_count,
309                                     _nc_visbuf(ptr->Strings[i + str_count])));
310                 ptr->Strings[i + STRCOUNT] = ptr->Strings[i + str_count];
311                 if (VALID_STRING(ptr->Strings[i + STRCOUNT]))
312                     base += (int) (strlen(ptr->Strings[i + STRCOUNT]) + 1);
313                 TR(TRACE_DATABASE, ("... to    [%d] %s",
314                                     i + STRCOUNT,
315                                     _nc_visbuf(ptr->Strings[i + STRCOUNT])));
316             }
317         }
318
319         if (need) {
320             if (ext_str_count >= (MAX_ENTRY_SIZE / 2))
321                 return (TGETENT_NO);
322             if ((ptr->ext_Names = TYPE_CALLOC(char *, need)) == 0)
323                   return (TGETENT_NO);
324             TR(TRACE_DATABASE,
325                ("ext_NAMES starting @%d in extended_strings, first = %s",
326                 base, _nc_visbuf(ptr->ext_str_table + base)));
327             convert_strings(buf + (2 * ext_str_count),
328                             ptr->ext_Names,
329                             (int) need,
330                             ext_str_limit, ptr->ext_str_table + base);
331         }
332
333         TR(TRACE_DATABASE,
334            ("...done reading terminfo bool %d(%d) num %d(%d) str %d(%d)",
335             ptr->num_Booleans, ptr->ext_Booleans,
336             ptr->num_Numbers, ptr->ext_Numbers,
337             ptr->num_Strings, ptr->ext_Strings));
338
339         TR(TRACE_DATABASE, ("extend: num_Booleans:%d", ptr->num_Booleans));
340     } else
341 #endif /* NCURSES_XNAMES */
342     {
343         TR(TRACE_DATABASE, ("...done reading terminfo bool %d num %d str %d",
344                             bool_count, num_count, str_count));
345 #if NCURSES_XNAMES
346         TR(TRACE_DATABASE, ("normal: num_Booleans:%d", ptr->num_Booleans));
347 #endif
348     }
349
350     for (i = bool_count; i < BOOLCOUNT; i++)
351         ptr->Booleans[i] = FALSE;
352     for (i = num_count; i < NUMCOUNT; i++)
353         ptr->Numbers[i] = ABSENT_NUMERIC;
354     for (i = str_count; i < STRCOUNT; i++)
355         ptr->Strings[i] = ABSENT_STRING;
356
357     return (TGETENT_YES);
358 }
359
360 /*
361  *      int
362  *      _nc_read_file_entry(filename, ptr)
363  *
364  *      Read the compiled terminfo entry in the given file into the
365  *      structure pointed to by ptr, allocating space for the string
366  *      table.
367  */
368 NCURSES_EXPORT(int)
369 _nc_read_file_entry(const char *const filename, TERMTYPE *ptr)
370 /* return 1 if read, 0 if not found or garbled */
371 {
372     FILE *fp = 0;
373     int code;
374     int limit;
375     char buffer[MAX_ENTRY_SIZE + 1];
376
377     if (_nc_access(filename, R_OK) < 0
378         || (fp = fopen(filename, "rb")) == 0) {
379         TR(TRACE_DATABASE, ("cannot open terminfo %s (errno=%d)", filename, errno));
380         code = TGETENT_NO;
381     } else {
382         if ((limit = (int) fread(buffer, sizeof(char), sizeof(buffer), fp))
383             > 0) {
384
385             TR(TRACE_DATABASE, ("read terminfo %s", filename));
386             if ((code = _nc_read_termtype(ptr, buffer, limit)) == TGETENT_NO) {
387                 _nc_free_termtype(ptr);
388             }
389         } else {
390             code = TGETENT_NO;
391         }
392         fclose(fp);
393     }
394
395     return (code);
396 }
397
398 #if USE_HASHED_DB
399 /*
400  * Return if if we can build the filename of a ".db" file.
401  */
402 static bool
403 make_db_filename(char *filename, unsigned limit, const char *const path)
404 {
405     static const char suffix[] = DBM_SUFFIX;
406
407     unsigned lens = sizeof(suffix) - 1;
408     unsigned size = strlen(path);
409     unsigned test = lens + size;
410     bool result = FALSE;
411
412     if (test < limit) {
413         if (size >= lens
414             && !strcmp(path + size - lens, suffix))
415             _nc_STRCPY(filename, path, limit);
416         else
417             _nc_SPRINTF(filename, _nc_SLIMIT(limit) "%s%s", path, suffix);
418         result = TRUE;
419     }
420     return result;
421 }
422 #endif
423
424 /*
425  * Return true if we can build the name of a filesystem entry.
426  */
427 static bool
428 make_dir_filename(char *filename,
429                   unsigned limit,
430                   const char *const path,
431                   const char *name)
432 {
433     bool result = FALSE;
434
435 #if USE_TERMCAP
436     if (_nc_is_dir_path(path))
437 #endif
438     {
439         unsigned need = (unsigned) (LEAF_LEN + 3 + strlen(path) + strlen(name));
440
441         if (need <= limit) {
442             _nc_SPRINTF(filename, _nc_SLIMIT(limit)
443                         "%s/" LEAF_FMT "/%s", path, *name, name);
444             result = TRUE;
445         }
446     }
447     return result;
448 }
449
450 /*
451  * Build a terminfo pathname and try to read the data.  Returns TGETENT_YES on
452  * success, TGETENT_NO on failure.
453  */
454 static int
455 _nc_read_tic_entry(char *filename,
456                    unsigned limit,
457                    const char *const path,
458                    const char *name,
459                    TERMTYPE *const tp)
460 {
461     int code = TGETENT_NO;
462
463 #if USE_HASHED_DB
464     DB *capdbp;
465
466     if (make_db_filename(filename, limit, path)
467         && (capdbp = _nc_db_open(filename, FALSE)) != 0) {
468
469         DBT key, data;
470         int reccnt = 0;
471         char *save = strdup(name);
472
473         memset(&key, 0, sizeof(key));
474         key.data = save;
475         key.size = strlen(save);
476
477         /*
478          * This lookup could return termcap data, which we do not want.  We are
479          * looking for compiled (binary) terminfo data.
480          *
481          * cgetent uses a two-level lookup.  On the first it uses the given
482          * name to return a record containing only the aliases for an entry. 
483          * On the second (using that list of aliases as a key), it returns the
484          * content of the terminal description.  We expect second lookup to
485          * return data beginning with the same set of aliases.
486          *
487          * For compiled terminfo, the list of aliases in the second case will
488          * be null-terminated.  A termcap entry will not be, and will run on
489          * into the description.  So we can easily distinguish between the two
490          * (source/binary) by checking the lengths.
491          */
492         while (_nc_db_get(capdbp, &key, &data) == 0) {
493             int used = data.size - 1;
494             char *have = (char *) data.data;
495
496             if (*have++ == 0) {
497                 if (data.size > key.size
498                     && IS_TIC_MAGIC(have)) {
499                     code = _nc_read_termtype(tp, have, used);
500                     if (code == TGETENT_NO) {
501                         _nc_free_termtype(tp);
502                     }
503                 }
504                 break;
505             }
506
507             /*
508              * Just in case we have a corrupt database, do not waste time with
509              * it.
510              */
511             if (++reccnt >= 3)
512                 break;
513
514             /*
515              * Prepare for the second level.
516              */
517             key.data = have;
518             key.size = used;
519         }
520
521         free(save);
522     } else                      /* may be either filesystem or flat file */
523 #endif
524     if (make_dir_filename(filename, limit, path, name)) {
525         code = _nc_read_file_entry(filename, tp);
526     }
527 #if USE_TERMCAP
528     else if (code != TGETENT_YES) {
529         code = _nc_read_termcap_entry(name, tp);
530         _nc_SPRINTF(filename, _nc_SLIMIT(PATH_MAX)
531                     "%.*s", PATH_MAX - 1, _nc_get_source());
532     }
533 #endif
534     return code;
535 }
536 #endif /* USE_DATABASE */
537
538 /*
539  *      _nc_read_entry(char *name, char *filename, TERMTYPE *tp)
540  *
541  *      Find and read the compiled entry for a given terminal type,
542  *      if it exists.  We take pains here to make sure no combination
543  *      of environment variables and terminal type name can be used to
544  *      overrun the file buffer.
545  */
546
547 NCURSES_EXPORT(int)
548 _nc_read_entry(const char *const name, char *const filename, TERMTYPE *const tp)
549 {
550     int code = TGETENT_NO;
551
552     _nc_SPRINTF(filename, _nc_SLIMIT(PATH_MAX)
553                 "%.*s", PATH_MAX - 1, name);
554
555     if (strlen(name) == 0
556         || strcmp(name, ".") == 0
557         || strcmp(name, "..") == 0
558         || _nc_pathlast(name) != 0
559         || strchr(name, NCURSES_PATHSEP) != 0) {
560         TR(TRACE_DATABASE, ("illegal or missing entry name '%s'", name));
561     } else {
562 #if USE_DATABASE
563         DBDIRS state;
564         int offset;
565         const char *path;
566
567         _nc_first_db(&state, &offset);
568         while ((path = _nc_next_db(&state, &offset)) != 0) {
569             TR(TRACE_DATABASE, ("_nc_read_tic_entry path=%s, name=%s", path, name));
570             code = _nc_read_tic_entry(filename, PATH_MAX, path, name, tp);
571             if (code == TGETENT_YES) {
572                 _nc_last_db();
573                 break;
574             }
575         }
576 #elif USE_TERMCAP
577         if (code != TGETENT_YES) {
578             code = _nc_read_termcap_entry(name, tp);
579             _nc_SPRINTF(filename, _nc_SLIMIT(PATH_MAX)
580                         "%.*s", PATH_MAX - 1, _nc_get_source());
581         }
582 #endif
583     }
584     return code;
585 }