1 /****************************************************************************
2 * Copyright (c) 1998-2006,2007 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
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>
43 #include <term_entry.h>
45 MODULE_ID("$Id: read_entry.c,v 1.100 2007/11/17 23:56:50 tom Exp $")
47 #define TYPE_CALLOC(type,elts) typeCalloc(type, (unsigned)(elts))
51 convert_shorts(char *buf, short *Numbers, int count)
54 for (i = 0; i < count; i++) {
55 if (IS_NEG1(buf + 2 * i))
56 Numbers[i] = ABSENT_NUMERIC;
57 else if (IS_NEG2(buf + 2 * i))
58 Numbers[i] = CANCELLED_NUMERIC;
60 Numbers[i] = LOW_MSB(buf + 2 * i);
61 TR(TRACE_DATABASE, ("get Numbers[%d]=%d", i, Numbers[i]));
66 convert_strings(char *buf, char **Strings, int count, int size, char *table)
71 for (i = 0; i < count; i++) {
72 if (IS_NEG1(buf + 2 * i)) {
73 Strings[i] = ABSENT_STRING;
74 } else if (IS_NEG2(buf + 2 * i)) {
75 Strings[i] = CANCELLED_STRING;
76 } else if (LOW_MSB(buf + 2 * i) > size) {
77 Strings[i] = ABSENT_STRING;
79 Strings[i] = (LOW_MSB(buf + 2 * i) + table);
80 TR(TRACE_DATABASE, ("Strings[%d] = %s", i, _nc_visbuf(Strings[i])));
83 /* make sure all strings are NUL terminated */
84 if (VALID_STRING(Strings[i])) {
85 for (p = Strings[i]; p <= table + size; p++)
88 /* if there is no NUL, ignore the string */
90 Strings[i] = ABSENT_STRING;
96 fake_read(char *src, int *offset, int limit, char *dst, unsigned want)
98 int have = (limit - *offset);
101 if ((int) want > have)
103 memcpy(dst, src + *offset, want);
111 #define Read(buf, count) fake_read(buffer, &offset, limit, buf, count)
113 #define read_shorts(buf, count) \
114 (Read(buf, (unsigned) (count)*2) == (int) (count)*2)
116 #define even_boundary(value) \
117 if ((value) % 2 != 0) Read(buf, 1)
120 _nc_read_termtype(TERMTYPE *ptr, char *buffer, int limit)
121 /* return 1 if read, 0 if not found or garbled */
124 int name_size, bool_count, num_count, str_count, str_size;
126 char buf[MAX_ENTRY_SIZE + 1];
130 TR(TRACE_DATABASE, ("READ termtype header @%d", offset));
132 memset(ptr, 0, sizeof(*ptr));
134 /* grab the header */
135 if (!read_shorts(buf, 6)
136 || !IS_TIC_MAGIC(buf)) {
140 name_size = LOW_MSB(buf + 2);
141 bool_count = LOW_MSB(buf + 4);
142 num_count = LOW_MSB(buf + 6);
143 str_count = LOW_MSB(buf + 8);
144 str_size = LOW_MSB(buf + 10);
147 ("TERMTYPE name_size=%d, bool=%d/%d, num=%d/%d str=%d/%d(%d)",
148 name_size, bool_count, BOOLCOUNT, num_count, NUMCOUNT,
149 str_count, STRCOUNT, str_size));
158 want = str_size + name_size + 1;
160 /* try to allocate space for the string table */
161 if (str_count * 2 >= (int) sizeof(buf)
162 || (string_table = typeMalloc(char, want)) == 0) {
167 if ((string_table = typeMalloc(char, want)) == 0) {
172 /* grab the name (a null-terminated string) */
173 want = min(MAX_NAME_SIZE, (unsigned) name_size);
174 ptr->str_table = string_table;
175 ptr->term_names = string_table;
176 if ((have = Read(ptr->term_names, want)) != want) {
177 memset(ptr->term_names + have, 0, want - have);
179 ptr->term_names[want] = '\0';
180 string_table += (want + 1);
182 if (have > MAX_NAME_SIZE)
183 offset = (have - MAX_NAME_SIZE);
185 /* grab the booleans */
186 if ((ptr->Booleans = TYPE_CALLOC(NCURSES_SBOOL,
187 max(BOOLCOUNT, bool_count))) == 0
188 || Read(ptr->Booleans, (unsigned) bool_count) < bool_count) {
193 * If booleans end on an odd byte, skip it. The machine they
194 * originally wrote terminfo on must have been a 16-bit
195 * word-oriented machine that would trap out if you tried a
196 * word access off a 2-byte boundary.
198 even_boundary(name_size + bool_count);
200 /* grab the numbers */
201 if ((ptr->Numbers = TYPE_CALLOC(short, max(NUMCOUNT, num_count))) == 0
202 || !read_shorts(buf, num_count)) {
205 convert_shorts(buf, ptr->Numbers, num_count);
207 if ((ptr->Strings = TYPE_CALLOC(char *, max(STRCOUNT, str_count))) == 0)
211 /* grab the string offsets */
212 if (!read_shorts(buf, str_count)) {
215 /* finally, grab the string table itself */
216 if (Read(string_table, (unsigned) str_size) != str_size)
218 convert_strings(buf, ptr->Strings, str_count, str_size, string_table);
222 ptr->num_Booleans = BOOLCOUNT;
223 ptr->num_Numbers = NUMCOUNT;
224 ptr->num_Strings = STRCOUNT;
227 * Read extended entries, if any, after the normal end of terminfo data.
229 even_boundary(str_size);
230 TR(TRACE_DATABASE, ("READ extended_header @%d", offset));
231 if (_nc_user_definable && read_shorts(buf, 5)) {
232 int ext_bool_count = LOW_MSB(buf + 0);
233 int ext_num_count = LOW_MSB(buf + 2);
234 int ext_str_count = LOW_MSB(buf + 4);
235 int ext_str_size = LOW_MSB(buf + 6);
236 int ext_str_limit = LOW_MSB(buf + 8);
237 unsigned need = (ext_bool_count + ext_num_count + ext_str_count);
240 if (need >= sizeof(buf)
241 || ext_str_size >= (int) sizeof(buf)
242 || ext_str_limit >= (int) sizeof(buf)
243 || ext_bool_count < 0
247 || ext_str_limit < 0)
250 ptr->num_Booleans = BOOLCOUNT + ext_bool_count;
251 ptr->num_Numbers = NUMCOUNT + ext_num_count;
252 ptr->num_Strings = STRCOUNT + ext_str_count;
254 ptr->Booleans = typeRealloc(NCURSES_SBOOL, ptr->num_Booleans, ptr->Booleans);
255 ptr->Numbers = typeRealloc(short, ptr->num_Numbers, ptr->Numbers);
256 ptr->Strings = typeRealloc(char *, ptr->num_Strings, ptr->Strings);
258 TR(TRACE_DATABASE, ("extended header is %d/%d/%d(%d:%d)",
259 ext_bool_count, ext_num_count, ext_str_count,
260 ext_str_size, ext_str_limit));
262 TR(TRACE_DATABASE, ("READ %d extended-booleans @%d",
263 ext_bool_count, offset));
264 if ((ptr->ext_Booleans = ext_bool_count) != 0) {
265 if (Read(ptr->Booleans + BOOLCOUNT, (unsigned)
266 ext_bool_count) != ext_bool_count)
269 even_boundary(ext_bool_count);
271 TR(TRACE_DATABASE, ("READ %d extended-numbers @%d",
272 ext_num_count, offset));
273 if ((ptr->ext_Numbers = ext_num_count) != 0) {
274 if (!read_shorts(buf, ext_num_count))
276 TR(TRACE_DATABASE, ("Before converting extended-numbers"));
277 convert_shorts(buf, ptr->Numbers + NUMCOUNT, ext_num_count);
280 TR(TRACE_DATABASE, ("READ extended-offsets @%d", offset));
281 if ((ext_str_count || need)
282 && !read_shorts(buf, ext_str_count + need))
285 TR(TRACE_DATABASE, ("READ %d bytes of extended-strings @%d",
286 ext_str_limit, offset));
289 if ((ptr->ext_str_table = typeMalloc(char, ext_str_limit)) == 0)
291 if (Read(ptr->ext_str_table, (unsigned) ext_str_limit) != ext_str_limit)
293 TR(TRACE_DATABASE, ("first extended-string is %s", _nc_visbuf(ptr->ext_str_table)));
296 if ((ptr->ext_Strings = ext_str_count) != 0) {
298 ("Before computing extended-string capabilities str_count=%d, ext_str_count=%d",
299 str_count, ext_str_count));
300 convert_strings(buf, ptr->Strings + str_count, ext_str_count,
301 ext_str_limit, ptr->ext_str_table);
302 for (i = ext_str_count - 1; i >= 0; i--) {
303 TR(TRACE_DATABASE, ("MOVE from [%d:%d] %s",
305 _nc_visbuf(ptr->Strings[i + str_count])));
306 ptr->Strings[i + STRCOUNT] = ptr->Strings[i + str_count];
307 if (VALID_STRING(ptr->Strings[i + STRCOUNT]))
308 base += (strlen(ptr->Strings[i + STRCOUNT]) + 1);
309 TR(TRACE_DATABASE, ("... to [%d] %s",
311 _nc_visbuf(ptr->Strings[i + STRCOUNT])));
316 if ((ptr->ext_Names = TYPE_CALLOC(char *, need)) == 0)
319 ("ext_NAMES starting @%d in extended_strings, first = %s",
320 base, _nc_visbuf(ptr->ext_str_table + base)));
321 convert_strings(buf + (2 * ext_str_count),
324 ext_str_limit, ptr->ext_str_table + base);
327 T(("...done reading terminfo bool %d(%d) num %d(%d) str %d(%d)",
328 ptr->num_Booleans, ptr->ext_Booleans,
329 ptr->num_Numbers, ptr->ext_Numbers,
330 ptr->num_Strings, ptr->ext_Strings));
332 TR(TRACE_DATABASE, ("extend: num_Booleans:%d", ptr->num_Booleans));
334 #endif /* NCURSES_XNAMES */
336 T(("...done reading terminfo bool %d num %d str %d",
337 bool_count, num_count, str_count));
339 TR(TRACE_DATABASE, ("normal: num_Booleans:%d", ptr->num_Booleans));
343 for (i = bool_count; i < BOOLCOUNT; i++)
344 ptr->Booleans[i] = FALSE;
345 for (i = num_count; i < NUMCOUNT; i++)
346 ptr->Numbers[i] = ABSENT_NUMERIC;
347 for (i = str_count; i < STRCOUNT; i++)
348 ptr->Strings[i] = ABSENT_STRING;
350 return (TGETENT_YES);
355 * _nc_read_file_entry(filename, ptr)
357 * Read the compiled terminfo entry in the given file into the
358 * structure pointed to by ptr, allocating space for the string
362 _nc_read_file_entry(const char *const filename, TERMTYPE *ptr)
363 /* return 1 if read, 0 if not found or garbled */
367 char buffer[MAX_ENTRY_SIZE + 1];
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));
374 if ((limit = read(fd, buffer, sizeof(buffer))) > 0) {
376 T(("read terminfo %s", filename));
377 if ((code = _nc_read_termtype(ptr, buffer, limit)) == TGETENT_NO) {
378 _nc_free_termtype(ptr);
390 * Build a terminfo pathname and try to read the data. Returns TGETENT_YES on
391 * success, TGETENT_NO on failure.
394 _nc_read_tic_entry(char *filename,
396 const char *const path,
400 int result = TGETENT_NO;
403 * If we are looking in a directory, assume the entry is a file under that,
404 * according to the normal rules.
406 * FIXME - add caseless-filename fixup.
408 if (_nc_is_dir_path(path)) {
409 unsigned need = 4 + strlen(path) + strlen(name);
412 (void) sprintf(filename, "%s/" LEAF_FMT "/%s", path, *name, name);
413 result = _nc_read_file_entry(filename, tp);
418 static const char suffix[] = DBM_SUFFIX;
420 unsigned lens = sizeof(suffix) - 1;
421 unsigned size = strlen(path);
422 unsigned need = lens + size;
426 && !strcmp(path + size - lens, suffix))
427 (void) strcpy(filename, path);
429 (void) sprintf(filename, "%s%s", path, suffix);
432 * It would be nice to optimize the dbopen/close activity, as
433 * done in the cgetent implementation for tc= clauses. However,
434 * since we support multiple database locations, we cannot do
437 if ((capdbp = _nc_db_open(filename, FALSE)) != 0) {
440 char *save = strdup(name);
442 memset(&key, 0, sizeof(key));
444 key.size = strlen(save);
447 * This lookup could return termcap data, which we do not want.
448 * We are looking for compiled (binary) terminfo data.
450 * cgetent uses a two-level lookup. On the first it uses the
451 * given name to return a record containing only the aliases
452 * for an entry. On the second (using that list of aliases as
453 * a key), it returns the content of the terminal description.
454 * We expect second lookup to return data beginning with the
455 * same set of aliases.
457 * For compiled terminfo, the list of aliases in the second
458 * case will be null-terminated. A termcap entry will not be,
459 * and will run on into the description. So we can easily
460 * distinguish between the two (source/binary) by checking the
463 while (_nc_db_get(capdbp, &key, &data) == 0) {
464 int used = data.size - 1;
465 char *have = (char *) data.data;
468 if (data.size > key.size
469 && IS_TIC_MAGIC(have)) {
470 result = _nc_read_termtype(tp, have, used);
471 if (result == TGETENT_NO) {
472 _nc_free_termtype(tp);
479 * Just in case we have a corrupt database, do not waste
486 * Prepare for the second level.
492 _nc_db_close(capdbp);
500 #endif /* USE_DATABASE */
503 * _nc_read_entry(char *name, char *filename, TERMTYPE *tp)
505 * Find and read the compiled entry for a given terminal type,
506 * if it exists. We take pains here to make sure no combination
507 * of environment variables and terminal type name can be used to
508 * overrun the file buffer.
512 _nc_read_entry(const char *const name, char *const filename, TERMTYPE *const tp)
514 int code = TGETENT_NO;
516 if (strlen(name) == 0
517 || strcmp(name, ".") == 0
518 || strcmp(name, "..") == 0
519 || _nc_pathlast(name) != 0
520 || strchr(name, NCURSES_PATHSEP) != 0) {
521 T(("illegal or missing entry name '%s'", name));
524 DBDIRS state = dbdTIC;
528 while ((path = _nc_next_db(&state, &offset)) != 0) {
529 code = _nc_read_tic_entry(filename, PATH_MAX, path, name, tp);
530 if (code == TGETENT_YES) {
537 if (code != TGETENT_YES) {
538 code = _nc_read_termcap_entry(name, tp);
539 sprintf(filename, "%.*s", PATH_MAX - 1, _nc_get_source());