]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/read_entry.c
f60a4863bdf85e8d985a8c5e3957c64fb357d7c0
[ncurses.git] / ncurses / tinfo / read_entry.c
1 /****************************************************************************
2  * Copyright (c) 1999 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  ****************************************************************************/
33
34
35
36 /*
37  *      read_entry.c -- Routine for reading in a compiled terminfo file
38  *
39  */
40
41 #include <curses.priv.h>
42
43 #if HAVE_FCNTL_H
44 #include <fcntl.h>
45 #endif
46
47 #include <tic.h>
48 #include <term_entry.h>
49
50 MODULE_ID("$Id: read_entry.c,v 1.61 1999/07/24 20:07:20 tom Exp $")
51
52 #ifndef O_BINARY
53 #define O_BINARY 0
54 #endif
55
56 #if 0
57 #define TRACE_IN(p) DEBUG(2, p)
58 #else
59 #define TRACE_IN(p) /*nothing*/
60 #endif
61
62 /*
63  *      int
64  *      _nc_read_file_entry(filename, ptr)
65  *
66  *      Read the compiled terminfo entry in the given file into the
67  *      structure pointed to by ptr, allocating space for the string
68  *      table.
69  */
70
71 #undef  BYTE
72 #define BYTE(p,n)       (unsigned char)((p)[n])
73
74 #define IS_NEG1(p)      ((BYTE(p,0) == 0377) && (BYTE(p,1) == 0377))
75 #define IS_NEG2(p)      ((BYTE(p,0) == 0376) && (BYTE(p,1) == 0377))
76 #define LOW_MSB(p)      (BYTE(p,0) + 256*BYTE(p,1))
77
78 static bool have_tic_directory = FALSE;
79 static bool keep_tic_directory = FALSE;
80
81 /*
82  * Record the "official" location of the terminfo directory, according to
83  * the place where we're writing to, or the normal default, if not.
84  */
85 const char *_nc_tic_dir(const char *path)
86 {
87     static const char *result = TERMINFO;
88
89     if (!keep_tic_directory) {
90         if (path != 0) {
91             result = path;
92             have_tic_directory = TRUE;
93         } else if (!have_tic_directory) {
94             char *envp;
95             if ((envp = getenv("TERMINFO")) != 0)
96                 return _nc_tic_dir(envp);
97         }
98     }
99     return result;
100 }
101
102 /*
103  * Special fix to prevent the terminfo directory from being moved after tic
104  * has chdir'd to it.  If we let it be changed, then if $TERMINFO has a
105  * relative path, we'll lose track of the actual directory.
106  */
107 void _nc_keep_tic_dir(const char *path)
108 {
109     _nc_tic_dir(path);
110     keep_tic_directory = TRUE;
111 }
112
113 static void convert_shorts(char *buf, short *Numbers, int count)
114 {
115     int i;
116     for (i = 0; i < count; i++)
117     {
118         if (IS_NEG1(buf + 2*i))
119             Numbers[i] = ABSENT_NUMERIC;
120         else if (IS_NEG2(buf + 2*i))
121             Numbers[i] = CANCELLED_NUMERIC;
122         else
123             Numbers[i] = LOW_MSB(buf + 2*i);
124         TRACE_IN(("get Numbers[%d]=%d", i, Numbers[i]));
125     }
126 }
127
128 static void convert_strings(char *buf, char **Strings, int count, int size, char *table)
129 {
130     int i;
131     char *p;
132
133     for (i = 0; i < count; i++) {
134         if (IS_NEG1(buf + 2*i)) {
135             Strings[i] = ABSENT_STRING;
136         } else if (IS_NEG2(buf + 2*i)) {
137             Strings[i] = CANCELLED_STRING;
138         } else if (LOW_MSB(buf + 2*i) > size) {
139             Strings[i] = ABSENT_STRING;
140         } else {
141             Strings[i] = (LOW_MSB(buf+2*i) + table);
142             TRACE_IN(("Strings[%d] = %s", i, _nc_visbuf(Strings[i])));
143         }
144
145         /* make sure all strings are NUL terminated */
146         if (VALID_STRING(Strings[i])) {
147             for (p = Strings[i]; p <= table + size; p++)
148                 if (*p == '\0')
149                     break;
150             /* if there is no NUL, ignore the string */
151             if (p > table + size)
152                 Strings[i] = ABSENT_STRING;
153         }
154     }
155 }
156
157 #define read_shorts(fd, buf, count) (read(fd, buf, (count)*2) == (count)*2)
158
159 #define even_boundary(value) \
160     if ((value) % 2 != 0) read(fd, buf, 1)
161
162 static int read_termtype(int fd, TERMTYPE *ptr)
163 /* return 1 if read, 0 if not found or garbled */
164 {
165     int         name_size, bool_count, num_count, str_count, str_size;
166     int         i;
167     char        buf[MAX_ENTRY_SIZE];
168
169     TRACE_IN(("READ termtype header @%d", tell(fd)));
170
171     /* grab the header */
172     if (!read_shorts(fd, buf, 6)
173      || LOW_MSB(buf) != MAGIC) {
174         return(0);
175     }
176
177     _nc_free_termtype(ptr);
178     name_size  = LOW_MSB(buf + 2);
179     bool_count = LOW_MSB(buf + 4);
180     num_count  = LOW_MSB(buf + 6);
181     str_count  = LOW_MSB(buf + 8);
182     str_size   = LOW_MSB(buf + 10);
183
184     TRACE_IN(("header is %d/%d/%d/%d(%d)", name_size, bool_count, num_count, str_count, str_size));
185     if (name_size  < 0
186      || bool_count < 0
187      || num_count  < 0
188      || str_count  < 0
189      || str_size   < 0) {
190         return(0);
191     }
192
193     if (str_size) {
194         /* try to allocate space for the string table */
195         if (str_count*2 >= (int) sizeof(buf)
196          || (ptr->str_table = typeMalloc(char, (unsigned)str_size)) == 0) {
197             return(0);
198         }
199     } else {
200         str_count = 0;
201     }
202
203     /* grab the name (a null-terminate string) */
204     read(fd, buf, min(MAX_NAME_SIZE, (unsigned)name_size));
205     buf[MAX_NAME_SIZE] = '\0';
206     ptr->term_names = typeCalloc(char, strlen(buf) + 1);
207     if (ptr->term_names == NULL) {
208         return(0);
209     }
210     (void) strcpy(ptr->term_names, buf);
211     if (name_size > MAX_NAME_SIZE)
212         lseek(fd, (off_t) (name_size - MAX_NAME_SIZE), 1);
213
214     /* grab the booleans */
215     if ((ptr->Booleans = typeCalloc(char, max(BOOLCOUNT, bool_count))) == 0
216      || read(fd, ptr->Booleans, (unsigned)bool_count) < bool_count) {
217         return(0);
218     }
219
220     /*
221      * If booleans end on an odd byte, skip it.  The machine they
222      * originally wrote terminfo on must have been a 16-bit
223      * word-oriented machine that would trap out if you tried a
224      * word access off a 2-byte boundary.
225      */
226     even_boundary(name_size + bool_count);
227
228     /* grab the numbers */
229     if ((ptr->Numbers = typeCalloc(short, max(NUMCOUNT, num_count))) == 0
230      || !read_shorts(fd, buf, num_count)) {
231         return(0);
232     }
233     convert_shorts(buf, ptr->Numbers, num_count);
234
235     if ((ptr->Strings = typeCalloc(char *, max(STRCOUNT, str_count))) == 0)
236         return(0);
237
238     if (str_count)
239     {
240         /* grab the string offsets */
241         if (!read_shorts(fd, buf, str_count)) {
242             return(0);
243         }
244         /* finally, grab the string table itself */
245         if (read(fd, ptr->str_table, (unsigned)str_size) != str_size)
246             return(0);
247         convert_strings(buf, ptr->Strings, str_count, str_size, ptr->str_table);
248     }
249
250 #if NCURSES_XNAMES
251
252     ptr->num_Booleans = BOOLCOUNT;
253     ptr->num_Numbers  = NUMCOUNT;
254     ptr->num_Strings  = STRCOUNT;
255
256     /*
257      * Read extended entries, if any, after the normal end of terminfo data.
258      */
259     even_boundary(str_size);
260     TRACE_IN(("READ extended_header @%d", tell(fd)));
261     if (_nc_user_definable && read_shorts(fd, buf, 5)) {
262         int ext_bool_count = LOW_MSB(buf + 0);
263         int ext_num_count  = LOW_MSB(buf + 2);
264         int ext_str_count  = LOW_MSB(buf + 4);
265         int ext_str_size   = LOW_MSB(buf + 6);
266         int ext_str_limit  = LOW_MSB(buf + 8);
267         int need = (ext_bool_count + ext_num_count + ext_str_count);
268         int base = 0;
269
270         if (need >= (int) sizeof(buf)
271          || ext_str_size >= (int) sizeof(buf)
272          || ext_str_limit >= (int) sizeof(buf)
273          || ext_bool_count < 0
274          || ext_num_count  < 0
275          || ext_str_count  < 0
276          || ext_str_size   < 0
277          || ext_str_limit  < 0)
278             return(0);
279
280         ptr->num_Booleans = BOOLCOUNT + ext_bool_count;
281         ptr->num_Numbers  = NUMCOUNT + ext_num_count;
282         ptr->num_Strings  = STRCOUNT + ext_str_count;
283
284         ptr->Booleans = typeRealloc(char, ptr->num_Booleans,ptr->Booleans);
285         ptr->Numbers = typeRealloc(short, ptr->num_Numbers, ptr->Numbers);
286         ptr->Strings = typeRealloc(char*, ptr->num_Strings, ptr->Strings);
287
288         TRACE_IN(("extended header is %d/%d/%d(%d:%d)", ext_bool_count, ext_num_count, ext_str_count, ext_str_size, ext_str_limit));
289
290         TRACE_IN(("READ %d extended-booleans @%d", ext_bool_count, tell(fd)));
291         if ((ptr->ext_Booleans = ext_bool_count) != 0) {
292             if (read(fd, ptr->Booleans + BOOLCOUNT, (unsigned)ext_bool_count) != ext_bool_count)
293                 return(0);
294         }
295         even_boundary(ext_bool_count);
296
297         TRACE_IN(("READ %d extended-numbers @%d", ext_num_count, tell(fd)));
298         if ((ptr->ext_Numbers = ext_num_count) != 0) {
299             if (!read_shorts(fd, buf, ext_num_count))
300                 return(0);
301             TRACE_IN(("Before converting extended-numbers"));
302             convert_shorts(buf, ptr->Numbers + NUMCOUNT, ext_num_count);
303         }
304
305         TRACE_IN(("READ extended-offsets @%d", tell(fd)));
306         if ((ext_str_count || need)
307          && !read_shorts(fd, buf, ext_str_count+need))
308             return(0);
309
310         TRACE_IN(("READ %d bytes of extended-strings @%d", ext_str_limit, tell(fd)));
311         if (ext_str_limit) {
312             if ((ptr->ext_str_table = typeMalloc(char, ext_str_limit)) == 0)
313                 return(0);
314             if (read(fd, ptr->ext_str_table, ext_str_limit) != ext_str_limit)
315                 return(0);
316             TRACE_IN(("first extended-string is %s", _nc_visbuf(ptr->ext_str_table)));
317         }
318
319         if ((ptr->ext_Strings = ext_str_count) != 0) {
320             TRACE_IN(("Before computing extended-string capabilities str_count=%d, ext_str_count=%d", str_count, ext_str_count));
321             convert_strings(buf, ptr->Strings + str_count, ext_str_count, ext_str_limit, ptr->ext_str_table);
322             for (i = ext_str_count-1; i >= 0; i--) {
323                 TRACE_IN(("MOVE from [%d:%d] %s", i, i+str_count, _nc_visbuf(ptr->Strings[i+str_count])));
324                 ptr->Strings[i+STRCOUNT] = ptr->Strings[i+str_count];
325                 if (VALID_STRING(ptr->Strings[i+STRCOUNT])) 
326                     base += (strlen(ptr->Strings[i+STRCOUNT]) + 1);
327                 TRACE_IN(("... to    [%d] %s", i+STRCOUNT, _nc_visbuf(ptr->Strings[i+STRCOUNT])));
328             }
329         }
330
331         if (need) {
332             if ((ptr->ext_Names = typeCalloc(char *, need)) == 0)
333                 return(0);
334             TRACE_IN(("ext_NAMES starting @%d in extended_strings, first = %s", base, _nc_visbuf(ptr->ext_str_table+base)));
335             convert_strings(buf + (2 * ext_str_count), ptr->ext_Names, need, ext_str_limit, ptr->ext_str_table + base);
336         }
337
338         T(("...done reading terminfo bool %d(%d) num %d(%d) str %d(%d)",
339             ptr->num_Booleans, ptr->ext_Booleans,
340             ptr->num_Numbers,  ptr->ext_Numbers,
341             ptr->num_Strings,  ptr->ext_Strings));
342
343         TRACE_IN(("extend: num_Booleans:%d", ptr->num_Booleans));
344     } else
345 #endif /* NCURSES_XNAMES */
346     {
347         T(("...done reading terminfo bool %d num %d str %d",
348             bool_count,
349             num_count,
350             str_count));
351         TRACE_IN(("normal: num_Booleans:%d", ptr->num_Booleans));
352     }
353
354     for (i = bool_count; i < BOOLCOUNT; i++)
355         ptr->Booleans[i] = FALSE;
356     for (i = num_count; i < NUMCOUNT; i++)
357         ptr->Numbers[i] = ABSENT_NUMERIC;
358     for (i = str_count; i < STRCOUNT; i++)
359         ptr->Strings[i] = ABSENT_STRING;
360
361     return(1);
362 }
363
364 int _nc_read_file_entry(const char *const filename, TERMTYPE *ptr)
365 /* return 1 if read, 0 if not found or garbled */
366 {
367     int code, fd = -1;
368
369     if (_nc_access(filename, R_OK) < 0
370      || (fd = open(filename, O_RDONLY|O_BINARY)) < 0) {
371         T(("cannot open terminfo %s (errno=%d)", filename, errno));
372         return(0);
373     }
374
375     T(("read terminfo %s", filename));
376     if ((code = read_termtype(fd, ptr)) == 0)
377         _nc_free_termtype(ptr);
378     close(fd);
379
380     return (code);
381 }
382
383 /*
384  * Build a terminfo pathname and try to read the data.  Returns 1 on success,
385  * 0 on failure.
386  */
387 static int _nc_read_tic_entry(char *const filename,
388         const char *const dir, const char *ttn, TERMTYPE *const tp)
389 {
390 /* maximum safe length of terminfo root directory name */
391 #define MAX_TPATH       (PATH_MAX - MAX_ALIAS - 6)
392
393         if (strlen(dir) > MAX_TPATH)
394                 return 0;
395         (void) sprintf(filename, "%s/%s", dir, ttn);
396         return _nc_read_file_entry(filename, tp);
397 }
398
399 /*
400  * Process the list of :-separated directories, looking for the terminal type.
401  * We don't use strtok because it does not show us empty tokens.
402  */
403 static int _nc_read_terminfo_dirs(const char *dirs, char *const filename, const char *const ttn, TERMTYPE *const tp)
404 {
405         char *list, *a;
406         const char *b;
407         int code = 0;
408
409         /* we'll modify the argument, so we must copy */
410         if ((b = a = list = strdup(dirs)) == NULL)
411                 return(0);
412
413         for (;;) {
414                 int c = *a;
415                 if (c == 0 || c == ':') {
416                         *a = 0;
417                         if ((b + 1) >= a)
418                                 b = TERMINFO;
419                         if (_nc_read_tic_entry(filename, b, ttn, tp) == 1) {
420                                 code = 1;
421                                 break;
422                         }
423                         b = a + 1;
424                         if (c == 0)
425                                 break;
426                 }
427                 a++;
428         }
429
430         free(list);
431         return(code);
432 }
433
434 /*
435  *      _nc_read_entry(char *tn, char *filename, TERMTYPE *tp)
436  *
437  *      Find and read the compiled entry for a given terminal type,
438  *      if it exists.  We take pains here to make sure no combination
439  *      of environment variables and terminal type name can be used to
440  *      overrun the file buffer.
441  */
442
443 int _nc_read_entry(const char *const tn, char *const filename, TERMTYPE *const tp)
444 {
445 char            *envp;
446 char            ttn[MAX_ALIAS + 3];
447
448         /* truncate the terminal name to prevent dangerous buffer airline */
449         (void) sprintf(ttn, "%c/%.*s", *tn, MAX_ALIAS, tn);
450
451         /* This is System V behavior, in conjunction with our requirements for
452          * writing terminfo entries.
453          */
454         if (have_tic_directory
455          && _nc_read_tic_entry(filename, _nc_tic_dir(0), ttn, tp) == 1)
456                 return 1;
457
458         if ((envp = getenv("TERMINFO")) != 0
459          && _nc_read_tic_entry(filename, _nc_tic_dir(envp), ttn, tp) == 1)
460                 return 1;
461
462         if ((envp = _nc_home_terminfo()) != 0) {
463                 if (_nc_read_tic_entry(filename, envp, ttn, tp) == 1) {
464                         return(1);
465                 }
466         }
467
468         /* this is an ncurses extension */
469         if ((envp = getenv("TERMINFO_DIRS")) != 0)
470                 return _nc_read_terminfo_dirs(envp, filename, ttn, tp);
471
472         /* Try the system directory.  Note that the TERMINFO_DIRS value, if
473          * defined by the configure script, begins with a ":", which will be
474          * interpreted as TERMINFO.
475          */
476 #ifdef TERMINFO_DIRS
477         return _nc_read_terminfo_dirs(TERMINFO_DIRS, filename, ttn, tp);
478 #else
479         return _nc_read_tic_entry(filename, TERMINFO, ttn, tp);
480 #endif
481 }
482