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