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