1 /****************************************************************************
2 * Copyright (c) 1998-2004,2005 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
40 #include <curses.priv.h>
43 #include <term_entry.h>
45 MODULE_ID("$Id: read_entry.c,v 1.81 2005/06/02 22:04:32 tom Exp $")
48 #define tell(fd) lseek(fd, 0, SEEK_CUR) /* lseek() is POSIX, but not tell() */
51 #define TYPE_CALLOC(type,elts) typeCalloc(type, (unsigned)(elts))
55 * _nc_read_file_entry(filename, ptr)
57 * Read the compiled terminfo entry in the given file into the
58 * structure pointed to by ptr, allocating space for the string
63 #define BYTE(p,n) (unsigned char)((p)[n])
65 #define IS_NEG1(p) ((BYTE(p,0) == 0377) && (BYTE(p,1) == 0377))
66 #define IS_NEG2(p) ((BYTE(p,0) == 0376) && (BYTE(p,1) == 0377))
67 #define LOW_MSB(p) (BYTE(p,0) + 256*BYTE(p,1))
69 static bool have_tic_directory = FALSE;
70 static bool keep_tic_directory = FALSE;
73 * Record the "official" location of the terminfo directory, according to
74 * the place where we're writing to, or the normal default, if not.
76 NCURSES_EXPORT(const char *)
77 _nc_tic_dir(const char *path)
79 static const char *result = TERMINFO;
81 if (!keep_tic_directory) {
84 have_tic_directory = TRUE;
85 } else if (!have_tic_directory && use_terminfo_vars()) {
87 if ((envp = getenv("TERMINFO")) != 0)
88 return _nc_tic_dir(envp);
95 * Special fix to prevent the terminfo directory from being moved after tic
96 * has chdir'd to it. If we let it be changed, then if $TERMINFO has a
97 * relative path, we'll lose track of the actual directory.
100 _nc_keep_tic_dir(const char *path)
103 keep_tic_directory = TRUE;
107 convert_shorts(char *buf, short *Numbers, int count)
110 for (i = 0; i < count; i++) {
111 if (IS_NEG1(buf + 2 * i))
112 Numbers[i] = ABSENT_NUMERIC;
113 else if (IS_NEG2(buf + 2 * i))
114 Numbers[i] = CANCELLED_NUMERIC;
116 Numbers[i] = LOW_MSB(buf + 2 * i);
117 TR(TRACE_DATABASE, ("get Numbers[%d]=%d", i, Numbers[i]));
122 convert_strings(char *buf, char **Strings, int count, int size, char *table)
127 for (i = 0; i < count; i++) {
128 if (IS_NEG1(buf + 2 * i)) {
129 Strings[i] = ABSENT_STRING;
130 } else if (IS_NEG2(buf + 2 * i)) {
131 Strings[i] = CANCELLED_STRING;
132 } else if (LOW_MSB(buf + 2 * i) > size) {
133 Strings[i] = ABSENT_STRING;
135 Strings[i] = (LOW_MSB(buf + 2 * i) + table);
136 TR(TRACE_DATABASE, ("Strings[%d] = %s", i, _nc_visbuf(Strings[i])));
139 /* make sure all strings are NUL terminated */
140 if (VALID_STRING(Strings[i])) {
141 for (p = Strings[i]; p <= table + size; p++)
144 /* if there is no NUL, ignore the string */
145 if (p > table + size)
146 Strings[i] = ABSENT_STRING;
151 #define read_shorts(fd, buf, count) \
152 (read(fd, buf, (unsigned) (count)*2) == (int) (count)*2)
154 #define even_boundary(value) \
155 if ((value) % 2 != 0) read(fd, buf, 1)
158 read_termtype(int fd, TERMTYPE *ptr)
159 /* return 1 if read, 0 if not found or garbled */
161 int name_size, bool_count, num_count, str_count, str_size;
163 char buf[MAX_ENTRY_SIZE + 1];
166 TR(TRACE_DATABASE, ("READ termtype header @%ld", (long) tell(fd)));
168 memset(ptr, 0, sizeof(*ptr));
170 /* grab the header */
171 if (!read_shorts(fd, buf, 6)
172 || LOW_MSB(buf) != MAGIC) {
176 name_size = LOW_MSB(buf + 2);
177 bool_count = LOW_MSB(buf + 4);
178 num_count = LOW_MSB(buf + 6);
179 str_count = LOW_MSB(buf + 8);
180 str_size = LOW_MSB(buf + 10);
183 ("TERMTYPE name_size=%d, bool=%d/%d, num=%d/%d str=%d/%d(%d)",
184 name_size, bool_count, BOOLCOUNT, num_count, NUMCOUNT,
185 str_count, STRCOUNT, str_size));
195 /* try to allocate space for the string table */
196 if (str_count * 2 >= (int) sizeof(buf)
197 || (ptr->str_table = typeMalloc(char, (unsigned) str_size)) == 0) {
204 /* grab the name (a null-terminated string) */
205 want = min(MAX_NAME_SIZE, (unsigned) name_size);
206 if ((have = read(fd, buf, want)) != want) {
207 memset(buf + have, 0, want - have);
210 ptr->term_names = TYPE_CALLOC(char, strlen(buf) + 1);
211 if (ptr->term_names == NULL) {
214 (void) strcpy(ptr->term_names, buf);
215 if (have > MAX_NAME_SIZE)
216 lseek(fd, (off_t) (have - MAX_NAME_SIZE), 1);
218 /* grab the booleans */
219 if ((ptr->Booleans = TYPE_CALLOC(char, max(BOOLCOUNT, bool_count))) == 0
220 || read(fd, ptr->Booleans, (unsigned) bool_count) < bool_count) {
225 * If booleans end on an odd byte, skip it. The machine they
226 * originally wrote terminfo on must have been a 16-bit
227 * word-oriented machine that would trap out if you tried a
228 * word access off a 2-byte boundary.
230 even_boundary(name_size + bool_count);
232 /* grab the numbers */
233 if ((ptr->Numbers = TYPE_CALLOC(short, max(NUMCOUNT, num_count))) == 0
234 || !read_shorts(fd, buf, num_count)) {
237 convert_shorts(buf, ptr->Numbers, num_count);
239 if ((ptr->Strings = TYPE_CALLOC(char *, max(STRCOUNT, str_count))) == 0)
243 /* grab the string offsets */
244 if (!read_shorts(fd, buf, str_count)) {
247 /* finally, grab the string table itself */
248 if (read(fd, ptr->str_table, (unsigned) str_size) != str_size)
250 convert_strings(buf, ptr->Strings, str_count, str_size, ptr->str_table);
254 ptr->num_Booleans = BOOLCOUNT;
255 ptr->num_Numbers = NUMCOUNT;
256 ptr->num_Strings = STRCOUNT;
259 * Read extended entries, if any, after the normal end of terminfo data.
261 even_boundary(str_size);
262 TR(TRACE_DATABASE, ("READ extended_header @%ld", (long) tell(fd)));
263 if (_nc_user_definable && read_shorts(fd, buf, 5)) {
264 int ext_bool_count = LOW_MSB(buf + 0);
265 int ext_num_count = LOW_MSB(buf + 2);
266 int ext_str_count = LOW_MSB(buf + 4);
267 int ext_str_size = LOW_MSB(buf + 6);
268 int ext_str_limit = LOW_MSB(buf + 8);
269 unsigned need = (ext_bool_count + ext_num_count + ext_str_count);
272 if (need >= sizeof(buf)
273 || ext_str_size >= (int) sizeof(buf)
274 || ext_str_limit >= (int) sizeof(buf)
275 || ext_bool_count < 0
279 || ext_str_limit < 0)
282 ptr->num_Booleans = BOOLCOUNT + ext_bool_count;
283 ptr->num_Numbers = NUMCOUNT + ext_num_count;
284 ptr->num_Strings = STRCOUNT + ext_str_count;
286 ptr->Booleans = typeRealloc(char, ptr->num_Booleans, ptr->Booleans);
287 ptr->Numbers = typeRealloc(short, ptr->num_Numbers, ptr->Numbers);
288 ptr->Strings = typeRealloc(char *, ptr->num_Strings, ptr->Strings);
290 TR(TRACE_DATABASE, ("extended header is %d/%d/%d(%d:%d)",
291 ext_bool_count, ext_num_count, ext_str_count,
292 ext_str_size, ext_str_limit));
294 TR(TRACE_DATABASE, ("READ %d extended-booleans @%ld",
295 ext_bool_count, (long) tell(fd)));
296 if ((ptr->ext_Booleans = ext_bool_count) != 0) {
297 if (read(fd, ptr->Booleans + BOOLCOUNT, (unsigned)
298 ext_bool_count) != ext_bool_count)
301 even_boundary(ext_bool_count);
303 TR(TRACE_DATABASE, ("READ %d extended-numbers @%ld",
304 ext_num_count, (long) tell(fd)));
305 if ((ptr->ext_Numbers = ext_num_count) != 0) {
306 if (!read_shorts(fd, buf, ext_num_count))
308 TR(TRACE_DATABASE, ("Before converting extended-numbers"));
309 convert_shorts(buf, ptr->Numbers + NUMCOUNT, ext_num_count);
312 TR(TRACE_DATABASE, ("READ extended-offsets @%ld", (long) tell(fd)));
313 if ((ext_str_count || need)
314 && !read_shorts(fd, buf, ext_str_count + need))
317 TR(TRACE_DATABASE, ("READ %d bytes of extended-strings @%ld",
318 ext_str_limit, (long) tell(fd)));
321 if ((ptr->ext_str_table = typeMalloc(char, ext_str_limit)) == 0)
323 if (read(fd, ptr->ext_str_table, (unsigned) ext_str_limit) != ext_str_limit)
325 TR(TRACE_DATABASE, ("first extended-string is %s", _nc_visbuf(ptr->ext_str_table)));
328 if ((ptr->ext_Strings = ext_str_count) != 0) {
330 ("Before computing extended-string capabilities str_count=%d, ext_str_count=%d",
331 str_count, ext_str_count));
332 convert_strings(buf, ptr->Strings + str_count, ext_str_count,
333 ext_str_limit, ptr->ext_str_table);
334 for (i = ext_str_count - 1; i >= 0; i--) {
335 TR(TRACE_DATABASE, ("MOVE from [%d:%d] %s",
337 _nc_visbuf(ptr->Strings[i + str_count])));
338 ptr->Strings[i + STRCOUNT] = ptr->Strings[i + str_count];
339 if (VALID_STRING(ptr->Strings[i + STRCOUNT]))
340 base += (strlen(ptr->Strings[i + STRCOUNT]) + 1);
341 TR(TRACE_DATABASE, ("... to [%d] %s",
343 _nc_visbuf(ptr->Strings[i + STRCOUNT])));
348 if ((ptr->ext_Names = TYPE_CALLOC(char *, need)) == 0)
351 ("ext_NAMES starting @%d in extended_strings, first = %s",
352 base, _nc_visbuf(ptr->ext_str_table + base)));
353 convert_strings(buf + (2 * ext_str_count),
356 ext_str_limit, ptr->ext_str_table + base);
359 T(("...done reading terminfo bool %d(%d) num %d(%d) str %d(%d)",
360 ptr->num_Booleans, ptr->ext_Booleans,
361 ptr->num_Numbers, ptr->ext_Numbers,
362 ptr->num_Strings, ptr->ext_Strings));
364 TR(TRACE_DATABASE, ("extend: num_Booleans:%d", ptr->num_Booleans));
366 #endif /* NCURSES_XNAMES */
368 T(("...done reading terminfo bool %d num %d str %d",
369 bool_count, num_count, str_count));
371 TR(TRACE_DATABASE, ("normal: num_Booleans:%d", ptr->num_Booleans));
375 for (i = bool_count; i < BOOLCOUNT; i++)
376 ptr->Booleans[i] = FALSE;
377 for (i = num_count; i < NUMCOUNT; i++)
378 ptr->Numbers[i] = ABSENT_NUMERIC;
379 for (i = str_count; i < STRCOUNT; i++)
380 ptr->Strings[i] = ABSENT_STRING;
386 _nc_read_file_entry(const char *const filename, TERMTYPE *ptr)
387 /* return 1 if read, 0 if not found or garbled */
391 if (_nc_access(filename, R_OK) < 0
392 || (fd = open(filename, O_RDONLY | O_BINARY)) < 0) {
393 T(("cannot open terminfo %s (errno=%d)", filename, errno));
396 T(("read terminfo %s", filename));
397 if ((code = read_termtype(fd, ptr)) == 0) {
398 _nc_free_termtype(ptr);
407 * Build a terminfo pathname and try to read the data. Returns 1 on success,
411 _nc_read_tic_entry(char *const filename,
412 const char *const dir, const char *ttn, TERMTYPE *const tp)
414 int need = 2 + strlen(dir) + strlen(ttn);
418 (void) sprintf(filename, "%s/%s", dir, ttn);
419 return _nc_read_file_entry(filename, tp);
423 * Process the list of :-separated directories, looking for the terminal type.
424 * We don't use strtok because it does not show us empty tokens.
427 _nc_read_terminfo_dirs(const char *dirs, char *const filename, const char *const
428 ttn, TERMTYPE *const tp)
434 /* we'll modify the argument, so we must copy */
435 if ((b = a = list = strdup(dirs)) == NULL)
440 if (c == 0 || c == NCURSES_PATHSEP) {
444 if (_nc_read_tic_entry(filename, b, ttn, tp) == 1) {
460 * _nc_read_entry(char *tn, char *filename, TERMTYPE *tp)
462 * Find and read the compiled entry for a given terminal type,
463 * if it exists. We take pains here to make sure no combination
464 * of environment variables and terminal type name can be used to
465 * overrun the file buffer.
469 _nc_read_entry(const char *const tn, char *const filename, TERMTYPE *const tp)
475 || strcmp(tn, ".") == 0
476 || strcmp(tn, "..") == 0
477 || _nc_pathlast(tn) != 0) {
478 T(("illegal or missing entry name '%s'", tn));
482 /* truncate the terminal name to prevent buffer overflow */
483 (void) sprintf(ttn, "%c/%.*s", *tn, (int) sizeof(ttn) - 3, tn);
485 /* This is System V behavior, in conjunction with our requirements for
486 * writing terminfo entries.
488 if (have_tic_directory
489 && _nc_read_tic_entry(filename, _nc_tic_dir(0), ttn, tp) == 1)
492 if (use_terminfo_vars()) {
493 if ((envp = getenv("TERMINFO")) != 0
494 && _nc_read_tic_entry(filename, _nc_tic_dir(envp), ttn, tp) == 1)
497 /* this is an ncurses extension */
498 if ((envp = _nc_home_terminfo()) != 0) {
499 if (_nc_read_tic_entry(filename, envp, ttn, tp) == 1) {
504 /* this is an ncurses extension */
505 if ((envp = getenv("TERMINFO_DIRS")) != 0)
506 return _nc_read_terminfo_dirs(envp, filename, ttn, tp);
509 /* Try the system directory. Note that the TERMINFO_DIRS value, if
510 * defined by the configure script, begins with a ":", which will be
511 * interpreted as TERMINFO.
514 return _nc_read_terminfo_dirs(TERMINFO_DIRS, filename, ttn, tp);
516 return _nc_read_tic_entry(filename, TERMINFO, ttn, tp);