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