libncurses: updated to 6.0
[tomato.git] / release / src / router / libncurses / ncurses / tinfo / read_entry.c
blob81fdc46c93b729c88c3926b16c3273610706bd86
1 /****************************************************************************
2 * Copyright (c) 1998-2014,2015 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 ****************************************************************************/
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 ****************************************************************************/
36 * read_entry.c -- Routine for reading in a compiled terminfo file
39 #include <curses.priv.h>
40 #include <hashed_db.h>
42 #include <tic.h>
44 MODULE_ID("$Id: read_entry.c,v 1.129 2015/06/27 16:16:40 tom Exp $")
46 #define TYPE_CALLOC(type,elts) typeCalloc(type, (unsigned)(elts))
48 #define MyNumber(n) (short) LOW_MSB(n)
50 #if NCURSES_USE_DATABASE
51 static void
52 convert_shorts(char *buf, short *Numbers, int count)
54 int i;
55 for (i = 0; i < count; i++) {
56 if (IS_NEG1(buf + 2 * i))
57 Numbers[i] = ABSENT_NUMERIC;
58 else if (IS_NEG2(buf + 2 * i))
59 Numbers[i] = CANCELLED_NUMERIC;
60 else
61 Numbers[i] = MyNumber(buf + 2 * i);
62 TR(TRACE_DATABASE, ("get Numbers[%d]=%d", i, Numbers[i]));
66 static void
67 convert_strings(char *buf, char **Strings, int count, int size, char *table)
69 int i;
70 char *p;
72 for (i = 0; i < count; i++) {
73 if (IS_NEG1(buf + 2 * i)) {
74 Strings[i] = ABSENT_STRING;
75 } else if (IS_NEG2(buf + 2 * i)) {
76 Strings[i] = CANCELLED_STRING;
77 } else if (MyNumber(buf + 2 * i) > size) {
78 Strings[i] = ABSENT_STRING;
79 } else {
80 Strings[i] = (MyNumber(buf + 2 * i) + table);
81 TR(TRACE_DATABASE, ("Strings[%d] = %s", i, _nc_visbuf(Strings[i])));
84 /* make sure all strings are NUL terminated */
85 if (VALID_STRING(Strings[i])) {
86 for (p = Strings[i]; p <= table + size; p++)
87 if (*p == '\0')
88 break;
89 /* if there is no NUL, ignore the string */
90 if (p > table + size)
91 Strings[i] = ABSENT_STRING;
96 static int
97 fake_read(char *src, int *offset, int limit, char *dst, unsigned want)
99 int have = (limit - *offset);
101 if (have > 0) {
102 if ((int) want > have)
103 want = (unsigned) have;
104 memcpy(dst, src + *offset, (size_t) want);
105 *offset += (int) want;
106 } else {
107 want = 0;
109 return (int) want;
112 #define Read(buf, count) fake_read(buffer, &offset, limit, (char *) buf, (unsigned) count)
114 #define read_shorts(buf, count) \
115 (Read(buf, (count)*2) == (int) (count)*2)
117 #define even_boundary(value) \
118 if ((value) % 2 != 0) Read(buf, 1)
119 #endif
121 NCURSES_EXPORT(void)
122 _nc_init_termtype(TERMTYPE *const tp)
124 unsigned i;
126 #if NCURSES_XNAMES
127 tp->num_Booleans = BOOLCOUNT;
128 tp->num_Numbers = NUMCOUNT;
129 tp->num_Strings = STRCOUNT;
130 tp->ext_Booleans = 0;
131 tp->ext_Numbers = 0;
132 tp->ext_Strings = 0;
133 #endif
134 if (tp->Booleans == 0)
135 TYPE_MALLOC(NCURSES_SBOOL, BOOLCOUNT, tp->Booleans);
136 if (tp->Numbers == 0)
137 TYPE_MALLOC(short, NUMCOUNT, tp->Numbers);
138 if (tp->Strings == 0)
139 TYPE_MALLOC(char *, STRCOUNT, tp->Strings);
141 for_each_boolean(i, tp)
142 tp->Booleans[i] = FALSE;
144 for_each_number(i, tp)
145 tp->Numbers[i] = ABSENT_NUMERIC;
147 for_each_string(i, tp)
148 tp->Strings[i] = ABSENT_STRING;
151 #if NCURSES_USE_DATABASE
152 #if NCURSES_XNAMES
153 static bool
154 valid_shorts(char *buffer, int limit)
156 bool result = FALSE;
157 int n;
158 for (n = 0; n < limit; ++n) {
159 if (MyNumber(buffer + (n * 2)) > 0) {
160 result = TRUE;
161 break;
164 return result;
166 #endif
169 * Return TGETENT_YES if read, TGETENT_NO if not found or garbled.
171 NCURSES_EXPORT(int)
172 _nc_read_termtype(TERMTYPE *ptr, char *buffer, int limit)
174 int offset = 0;
175 int name_size, bool_count, num_count, str_count, str_size;
176 int i;
177 char buf[MAX_ENTRY_SIZE + 2];
178 char *string_table;
179 unsigned want, have;
181 TR(TRACE_DATABASE, ("READ termtype header @%d", offset));
183 memset(ptr, 0, sizeof(*ptr));
185 /* grab the header */
186 if (!read_shorts(buf, 6)
187 || !IS_TIC_MAGIC(buf)) {
188 return (TGETENT_NO);
191 name_size = MyNumber(buf + 2);
192 bool_count = MyNumber(buf + 4);
193 num_count = MyNumber(buf + 6);
194 str_count = MyNumber(buf + 8);
195 str_size = MyNumber(buf + 10);
197 TR(TRACE_DATABASE,
198 ("TERMTYPE name_size=%d, bool=%d/%d, num=%d/%d str=%d/%d(%d)",
199 name_size, bool_count, BOOLCOUNT, num_count, NUMCOUNT,
200 str_count, STRCOUNT, str_size));
201 if (name_size < 0
202 || bool_count < 0
203 || num_count < 0
204 || str_count < 0
205 || str_size < 0) {
206 return (TGETENT_NO);
209 want = (unsigned) (str_size + name_size + 1);
210 if (str_size) {
211 /* try to allocate space for the string table */
212 if (str_count * 2 >= MAX_ENTRY_SIZE
213 || (string_table = typeMalloc(char, want)) == 0) {
214 return (TGETENT_NO);
216 } else {
217 str_count = 0;
218 if ((string_table = typeMalloc(char, want)) == 0) {
219 return (TGETENT_NO);
223 /* grab the name (a null-terminated string) */
224 want = min(MAX_NAME_SIZE, (unsigned) name_size);
225 ptr->str_table = string_table;
226 ptr->term_names = string_table;
227 if ((have = (unsigned) Read(ptr->term_names, want)) != want) {
228 memset(ptr->term_names + have, 0, (size_t) (want - have));
230 ptr->term_names[want] = '\0';
231 string_table += (want + 1);
233 if (have > MAX_NAME_SIZE)
234 offset = (int) (have - MAX_NAME_SIZE);
236 /* grab the booleans */
237 if ((ptr->Booleans = TYPE_CALLOC(NCURSES_SBOOL,
238 max(BOOLCOUNT, bool_count))) == 0
239 || Read(ptr->Booleans, (unsigned) bool_count) < bool_count) {
240 return (TGETENT_NO);
244 * If booleans end on an odd byte, skip it. The machine they
245 * originally wrote terminfo on must have been a 16-bit
246 * word-oriented machine that would trap out if you tried a
247 * word access off a 2-byte boundary.
249 even_boundary(name_size + bool_count);
251 /* grab the numbers */
252 if ((ptr->Numbers = TYPE_CALLOC(short, max(NUMCOUNT, num_count))) == 0
253 || !read_shorts(buf, num_count)) {
254 return (TGETENT_NO);
256 convert_shorts(buf, ptr->Numbers, num_count);
258 if ((ptr->Strings = TYPE_CALLOC(char *, max(STRCOUNT, str_count))) == 0) {
259 return (TGETENT_NO);
262 if (str_count) {
263 /* grab the string offsets */
264 if (!read_shorts(buf, str_count)) {
265 return (TGETENT_NO);
267 /* finally, grab the string table itself */
268 if (Read(string_table, (unsigned) str_size) != str_size) {
269 return (TGETENT_NO);
271 convert_strings(buf, ptr->Strings, str_count, str_size, string_table);
273 #if NCURSES_XNAMES
275 ptr->num_Booleans = BOOLCOUNT;
276 ptr->num_Numbers = NUMCOUNT;
277 ptr->num_Strings = STRCOUNT;
280 * Read extended entries, if any, after the normal end of terminfo data.
282 even_boundary(str_size);
283 TR(TRACE_DATABASE, ("READ extended_header @%d", offset));
284 if (_nc_user_definable && read_shorts(buf, 5) && valid_shorts(buf, 5)) {
285 int ext_bool_count = MyNumber(buf + 0);
286 int ext_num_count = MyNumber(buf + 2);
287 int ext_str_count = MyNumber(buf + 4);
288 int ext_str_size = MyNumber(buf + 6);
289 int ext_str_limit = MyNumber(buf + 8);
290 unsigned need = (unsigned) (ext_bool_count + ext_num_count + ext_str_count);
291 int base = 0;
293 if (need >= (MAX_ENTRY_SIZE / 2)
294 || ext_str_size >= MAX_ENTRY_SIZE
295 || ext_str_limit >= MAX_ENTRY_SIZE
296 || ext_bool_count < 0
297 || ext_num_count < 0
298 || ext_str_count < 0
299 || ext_str_size < 0
300 || ext_str_limit < 0) {
301 return (TGETENT_NO);
304 ptr->num_Booleans = UShort(BOOLCOUNT + ext_bool_count);
305 ptr->num_Numbers = UShort(NUMCOUNT + ext_num_count);
306 ptr->num_Strings = UShort(STRCOUNT + ext_str_count);
308 TYPE_REALLOC(NCURSES_SBOOL, ptr->num_Booleans, ptr->Booleans);
309 TYPE_REALLOC(short, ptr->num_Numbers, ptr->Numbers);
310 TYPE_REALLOC(char *, ptr->num_Strings, ptr->Strings);
312 TR(TRACE_DATABASE, ("extended header is %d/%d/%d(%d:%d)",
313 ext_bool_count, ext_num_count, ext_str_count,
314 ext_str_size, ext_str_limit));
316 TR(TRACE_DATABASE, ("READ %d extended-booleans @%d",
317 ext_bool_count, offset));
318 if ((ptr->ext_Booleans = UShort(ext_bool_count)) != 0) {
319 if (Read(ptr->Booleans + BOOLCOUNT, (unsigned)
320 ext_bool_count) != ext_bool_count) {
321 return (TGETENT_NO);
324 even_boundary(ext_bool_count);
326 TR(TRACE_DATABASE, ("READ %d extended-numbers @%d",
327 ext_num_count, offset));
328 if ((ptr->ext_Numbers = UShort(ext_num_count)) != 0) {
329 if (!read_shorts(buf, ext_num_count)) {
330 return (TGETENT_NO);
332 TR(TRACE_DATABASE, ("Before converting extended-numbers"));
333 convert_shorts(buf, ptr->Numbers + NUMCOUNT, ext_num_count);
336 TR(TRACE_DATABASE, ("READ extended-offsets @%d", offset));
337 if ((unsigned) (ext_str_count + (int) need) >= (MAX_ENTRY_SIZE / 2)) {
338 return (TGETENT_NO);
340 if ((ext_str_count || need)
341 && !read_shorts(buf, ext_str_count + (int) need)) {
342 return (TGETENT_NO);
345 TR(TRACE_DATABASE, ("READ %d bytes of extended-strings @%d",
346 ext_str_limit, offset));
348 if (ext_str_limit) {
349 ptr->ext_str_table = typeMalloc(char, (size_t) ext_str_limit);
350 if (ptr->ext_str_table == 0) {
351 return (TGETENT_NO);
353 if (Read(ptr->ext_str_table, (unsigned) ext_str_limit) != ext_str_limit) {
354 return (TGETENT_NO);
356 TR(TRACE_DATABASE, ("first extended-string is %s", _nc_visbuf(ptr->ext_str_table)));
359 if ((ptr->ext_Strings = UShort(ext_str_count)) != 0) {
360 TR(TRACE_DATABASE,
361 ("Before computing extended-string capabilities str_count=%d, ext_str_count=%d",
362 str_count, ext_str_count));
363 convert_strings(buf, ptr->Strings + str_count, ext_str_count,
364 ext_str_limit, ptr->ext_str_table);
365 for (i = ext_str_count - 1; i >= 0; i--) {
366 TR(TRACE_DATABASE, ("MOVE from [%d:%d] %s",
367 i, i + str_count,
368 _nc_visbuf(ptr->Strings[i + str_count])));
369 ptr->Strings[i + STRCOUNT] = ptr->Strings[i + str_count];
370 if (VALID_STRING(ptr->Strings[i + STRCOUNT]))
371 base += (int) (strlen(ptr->Strings[i + STRCOUNT]) + 1);
372 TR(TRACE_DATABASE, ("... to [%d] %s",
373 i + STRCOUNT,
374 _nc_visbuf(ptr->Strings[i + STRCOUNT])));
378 if (need) {
379 if (ext_str_count >= (MAX_ENTRY_SIZE / 2)) {
380 return (TGETENT_NO);
382 if ((ptr->ext_Names = TYPE_CALLOC(char *, need)) == 0) {
383 return (TGETENT_NO);
385 TR(TRACE_DATABASE,
386 ("ext_NAMES starting @%d in extended_strings, first = %s",
387 base, _nc_visbuf(ptr->ext_str_table + base)));
388 convert_strings(buf + (2 * ext_str_count),
389 ptr->ext_Names,
390 (int) need,
391 ext_str_limit, ptr->ext_str_table + base);
394 TR(TRACE_DATABASE,
395 ("...done reading terminfo bool %d(%d) num %d(%d) str %d(%d)",
396 ptr->num_Booleans, ptr->ext_Booleans,
397 ptr->num_Numbers, ptr->ext_Numbers,
398 ptr->num_Strings, ptr->ext_Strings));
400 TR(TRACE_DATABASE, ("extend: num_Booleans:%d", ptr->num_Booleans));
401 } else
402 #endif /* NCURSES_XNAMES */
404 TR(TRACE_DATABASE, ("...done reading terminfo bool %d num %d str %d",
405 bool_count, num_count, str_count));
406 #if NCURSES_XNAMES
407 TR(TRACE_DATABASE, ("normal: num_Booleans:%d", ptr->num_Booleans));
408 #endif
411 for (i = bool_count; i < BOOLCOUNT; i++)
412 ptr->Booleans[i] = FALSE;
413 for (i = num_count; i < NUMCOUNT; i++)
414 ptr->Numbers[i] = ABSENT_NUMERIC;
415 for (i = str_count; i < STRCOUNT; i++)
416 ptr->Strings[i] = ABSENT_STRING;
418 return (TGETENT_YES);
422 * int
423 * _nc_read_file_entry(filename, ptr)
425 * Read the compiled terminfo entry in the given file into the
426 * structure pointed to by ptr, allocating space for the string
427 * table.
429 NCURSES_EXPORT(int)
430 _nc_read_file_entry(const char *const filename, TERMTYPE *ptr)
431 /* return 1 if read, 0 if not found or garbled */
433 FILE *fp = 0;
434 int code;
435 int limit;
436 char buffer[MAX_ENTRY_SIZE + 1];
438 if (_nc_access(filename, R_OK) < 0
439 || (fp = fopen(filename, "rb")) == 0) {
440 TR(TRACE_DATABASE, ("cannot open terminfo %s (errno=%d)", filename, errno));
441 code = TGETENT_NO;
442 } else {
443 if ((limit = (int) fread(buffer, sizeof(char), sizeof(buffer), fp))
444 > 0) {
446 TR(TRACE_DATABASE, ("read terminfo %s", filename));
447 if ((code = _nc_read_termtype(ptr, buffer, limit)) == TGETENT_NO) {
448 _nc_free_termtype(ptr);
450 } else {
451 code = TGETENT_NO;
453 fclose(fp);
456 return (code);
459 #if USE_HASHED_DB
461 * Return if if we can build the filename of a ".db" file.
463 static bool
464 make_db_filename(char *filename, unsigned limit, const char *const path)
466 static const char suffix[] = DBM_SUFFIX;
468 size_t lens = sizeof(suffix) - 1;
469 size_t size = strlen(path);
470 size_t test = lens + size;
471 bool result = FALSE;
473 if (test < limit) {
474 if (size >= lens
475 && !strcmp(path + size - lens, suffix))
476 _nc_STRCPY(filename, path, limit);
477 else
478 _nc_SPRINTF(filename, _nc_SLIMIT(limit) "%s%s", path, suffix);
479 result = TRUE;
481 return result;
483 #endif
486 * Return true if we can build the name of a filesystem entry.
488 static bool
489 make_dir_filename(char *filename,
490 unsigned limit,
491 const char *const path,
492 const char *name)
494 bool result = FALSE;
496 #if NCURSES_USE_TERMCAP
497 if (_nc_is_dir_path(path))
498 #endif
500 unsigned need = (unsigned) (LEAF_LEN + 3 + strlen(path) + strlen(name));
502 if (need <= limit) {
503 _nc_SPRINTF(filename, _nc_SLIMIT(limit)
504 "%s/" LEAF_FMT "/%s", path, *name, name);
505 result = TRUE;
508 return result;
512 * Build a terminfo pathname and try to read the data. Returns TGETENT_YES on
513 * success, TGETENT_NO on failure.
515 static int
516 _nc_read_tic_entry(char *filename,
517 unsigned limit,
518 const char *const path,
519 const char *name,
520 TERMTYPE *const tp)
522 int code = TGETENT_NO;
524 #if USE_HASHED_DB
525 DB *capdbp;
527 if (make_db_filename(filename, limit, path)
528 && (capdbp = _nc_db_open(filename, FALSE)) != 0) {
530 DBT key, data;
531 int reccnt = 0;
532 char *save = strdup(name);
534 memset(&key, 0, sizeof(key));
535 key.data = save;
536 key.size = strlen(save);
539 * This lookup could return termcap data, which we do not want. We are
540 * looking for compiled (binary) terminfo data.
542 * cgetent uses a two-level lookup. On the first it uses the given
543 * name to return a record containing only the aliases for an entry.
544 * On the second (using that list of aliases as a key), it returns the
545 * content of the terminal description. We expect second lookup to
546 * return data beginning with the same set of aliases.
548 * For compiled terminfo, the list of aliases in the second case will
549 * be null-terminated. A termcap entry will not be, and will run on
550 * into the description. So we can easily distinguish between the two
551 * (source/binary) by checking the lengths.
553 while (_nc_db_get(capdbp, &key, &data) == 0) {
554 int used = (int) data.size - 1;
555 char *have = (char *) data.data;
557 if (*have++ == 0) {
558 if (data.size > key.size
559 && IS_TIC_MAGIC(have)) {
560 code = _nc_read_termtype(tp, have, used);
561 if (code == TGETENT_NO) {
562 _nc_free_termtype(tp);
565 break;
569 * Just in case we have a corrupt database, do not waste time with
570 * it.
572 if (++reccnt >= 3)
573 break;
576 * Prepare for the second level.
578 key.data = have;
579 key.size = used;
582 free(save);
583 } else /* may be either filesystem or flat file */
584 #endif
585 if (make_dir_filename(filename, limit, path, name)) {
586 code = _nc_read_file_entry(filename, tp);
588 #if NCURSES_USE_TERMCAP
589 else if (code != TGETENT_YES) {
590 code = _nc_read_termcap_entry(name, tp);
591 _nc_SPRINTF(filename, _nc_SLIMIT(PATH_MAX)
592 "%.*s", PATH_MAX - 1, _nc_get_source());
594 #endif
595 return code;
597 #endif /* NCURSES_USE_DATABASE */
600 * _nc_read_entry(char *name, char *filename, TERMTYPE *tp)
602 * Find and read the compiled entry for a given terminal type,
603 * if it exists. We take pains here to make sure no combination
604 * of environment variables and terminal type name can be used to
605 * overrun the file buffer.
608 NCURSES_EXPORT(int)
609 _nc_read_entry(const char *const name, char *const filename, TERMTYPE *const tp)
611 int code = TGETENT_NO;
613 _nc_SPRINTF(filename, _nc_SLIMIT(PATH_MAX)
614 "%.*s", PATH_MAX - 1, name);
616 if (strlen(name) == 0
617 || strcmp(name, ".") == 0
618 || strcmp(name, "..") == 0
619 || _nc_pathlast(name) != 0
620 || strchr(name, NCURSES_PATHSEP) != 0) {
621 TR(TRACE_DATABASE, ("illegal or missing entry name '%s'", name));
622 } else {
623 #if NCURSES_USE_DATABASE
624 DBDIRS state;
625 int offset;
626 const char *path;
628 _nc_first_db(&state, &offset);
629 code = TGETENT_ERR;
630 while ((path = _nc_next_db(&state, &offset)) != 0) {
631 TR(TRACE_DATABASE, ("_nc_read_tic_entry path=%s, name=%s", path, name));
632 code = _nc_read_tic_entry(filename, PATH_MAX, path, name, tp);
633 if (code == TGETENT_YES) {
634 _nc_last_db();
635 break;
638 #elif NCURSES_USE_TERMCAP
639 if (code != TGETENT_YES) {
640 code = _nc_read_termcap_entry(name, tp);
641 _nc_SPRINTF(filename, _nc_SLIMIT(PATH_MAX)
642 "%.*s", PATH_MAX - 1, _nc_get_source());
644 #endif
646 return code;