1 /****************************************************************************
2 * Copyright (c) 1999 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 ****************************************************************************/
37 * read_entry.c -- Routine for reading in a compiled terminfo file
41 #include <curses.priv.h>
48 #include <term_entry.h>
50 MODULE_ID("$Id: read_entry.c,v 1.61 1999/07/24 20:07:20 tom Exp $")
57 #define TRACE_IN(p) DEBUG(2, p)
59 #define TRACE_IN(p) /*nothing*/
64 * _nc_read_file_entry(filename, ptr)
66 * Read the compiled terminfo entry in the given file into the
67 * structure pointed to by ptr, allocating space for the string
72 #define BYTE(p,n) (unsigned char)((p)[n])
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))
78 static bool have_tic_directory = FALSE;
79 static bool keep_tic_directory = FALSE;
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.
85 const char *_nc_tic_dir(const char *path)
87 static const char *result = TERMINFO;
89 if (!keep_tic_directory) {
92 have_tic_directory = TRUE;
93 } else if (!have_tic_directory) {
95 if ((envp = getenv("TERMINFO")) != 0)
96 return _nc_tic_dir(envp);
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.
107 void _nc_keep_tic_dir(const char *path)
110 keep_tic_directory = TRUE;
113 static void convert_shorts(char *buf, short *Numbers, int count)
116 for (i = 0; i < count; i++)
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;
123 Numbers[i] = LOW_MSB(buf + 2*i);
124 TRACE_IN(("get Numbers[%d]=%d", i, Numbers[i]));
128 static void convert_strings(char *buf, char **Strings, int count, int size, char *table)
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;
141 Strings[i] = (LOW_MSB(buf+2*i) + table);
142 TRACE_IN(("Strings[%d] = %s", i, _nc_visbuf(Strings[i])));
145 /* make sure all strings are NUL terminated */
146 if (VALID_STRING(Strings[i])) {
147 for (p = Strings[i]; p <= table + size; p++)
150 /* if there is no NUL, ignore the string */
151 if (p > table + size)
152 Strings[i] = ABSENT_STRING;
157 #define read_shorts(fd, buf, count) (read(fd, buf, (count)*2) == (count)*2)
159 #define even_boundary(value) \
160 if ((value) % 2 != 0) read(fd, buf, 1)
162 static int read_termtype(int fd, TERMTYPE *ptr)
163 /* return 1 if read, 0 if not found or garbled */
165 int name_size, bool_count, num_count, str_count, str_size;
167 char buf[MAX_ENTRY_SIZE];
169 TRACE_IN(("READ termtype header @%d", tell(fd)));
171 /* grab the header */
172 if (!read_shorts(fd, buf, 6)
173 || LOW_MSB(buf) != MAGIC) {
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);
184 TRACE_IN(("header is %d/%d/%d/%d(%d)", name_size, bool_count, num_count, str_count, 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) {
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) {
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);
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) {
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.
226 even_boundary(name_size + bool_count);
228 /* grab the numbers */
229 if ((ptr->Numbers = typeCalloc(short, max(NUMCOUNT, num_count))) == 0
230 || !read_shorts(fd, buf, num_count)) {
233 convert_shorts(buf, ptr->Numbers, num_count);
235 if ((ptr->Strings = typeCalloc(char *, max(STRCOUNT, str_count))) == 0)
240 /* grab the string offsets */
241 if (!read_shorts(fd, buf, str_count)) {
244 /* finally, grab the string table itself */
245 if (read(fd, ptr->str_table, (unsigned)str_size) != str_size)
247 convert_strings(buf, ptr->Strings, str_count, str_size, ptr->str_table);
252 ptr->num_Booleans = BOOLCOUNT;
253 ptr->num_Numbers = NUMCOUNT;
254 ptr->num_Strings = STRCOUNT;
257 * Read extended entries, if any, after the normal end of terminfo data.
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);
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
277 || ext_str_limit < 0)
280 ptr->num_Booleans = BOOLCOUNT + ext_bool_count;
281 ptr->num_Numbers = NUMCOUNT + ext_num_count;
282 ptr->num_Strings = STRCOUNT + ext_str_count;
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);
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));
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)
295 even_boundary(ext_bool_count);
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))
301 TRACE_IN(("Before converting extended-numbers"));
302 convert_shorts(buf, ptr->Numbers + NUMCOUNT, ext_num_count);
305 TRACE_IN(("READ extended-offsets @%d", tell(fd)));
306 if ((ext_str_count || need)
307 && !read_shorts(fd, buf, ext_str_count+need))
310 TRACE_IN(("READ %d bytes of extended-strings @%d", ext_str_limit, tell(fd)));
312 if ((ptr->ext_str_table = typeMalloc(char, ext_str_limit)) == 0)
314 if (read(fd, ptr->ext_str_table, ext_str_limit) != ext_str_limit)
316 TRACE_IN(("first extended-string is %s", _nc_visbuf(ptr->ext_str_table)));
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])));
332 if ((ptr->ext_Names = typeCalloc(char *, need)) == 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);
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));
343 TRACE_IN(("extend: num_Booleans:%d", ptr->num_Booleans));
345 #endif /* NCURSES_XNAMES */
347 T(("...done reading terminfo bool %d num %d str %d",
351 TRACE_IN(("normal: num_Booleans:%d", ptr->num_Booleans));
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;
364 int _nc_read_file_entry(const char *const filename, TERMTYPE *ptr)
365 /* return 1 if read, 0 if not found or garbled */
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));
375 T(("read terminfo %s", filename));
376 if ((code = read_termtype(fd, ptr)) == 0)
377 _nc_free_termtype(ptr);
384 * Build a terminfo pathname and try to read the data. Returns 1 on success,
387 static int _nc_read_tic_entry(char *const filename,
388 const char *const dir, const char *ttn, TERMTYPE *const tp)
390 /* maximum safe length of terminfo root directory name */
391 #define MAX_TPATH (PATH_MAX - MAX_ALIAS - 6)
393 if (strlen(dir) > MAX_TPATH)
395 (void) sprintf(filename, "%s/%s", dir, ttn);
396 return _nc_read_file_entry(filename, tp);
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.
403 static int _nc_read_terminfo_dirs(const char *dirs, char *const filename, const char *const ttn, TERMTYPE *const tp)
409 /* we'll modify the argument, so we must copy */
410 if ((b = a = list = strdup(dirs)) == NULL)
415 if (c == 0 || c == ':') {
419 if (_nc_read_tic_entry(filename, b, ttn, tp) == 1) {
435 * _nc_read_entry(char *tn, char *filename, TERMTYPE *tp)
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.
443 int _nc_read_entry(const char *const tn, char *const filename, TERMTYPE *const tp)
446 char ttn[MAX_ALIAS + 3];
448 /* truncate the terminal name to prevent dangerous buffer airline */
449 (void) sprintf(ttn, "%c/%.*s", *tn, MAX_ALIAS, tn);
451 /* This is System V behavior, in conjunction with our requirements for
452 * writing terminfo entries.
454 if (have_tic_directory
455 && _nc_read_tic_entry(filename, _nc_tic_dir(0), ttn, tp) == 1)
458 if ((envp = getenv("TERMINFO")) != 0
459 && _nc_read_tic_entry(filename, _nc_tic_dir(envp), ttn, tp) == 1)
462 if ((envp = _nc_home_terminfo()) != 0) {
463 if (_nc_read_tic_entry(filename, envp, ttn, tp) == 1) {
468 /* this is an ncurses extension */
469 if ((envp = getenv("TERMINFO_DIRS")) != 0)
470 return _nc_read_terminfo_dirs(envp, filename, ttn, tp);
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.
477 return _nc_read_terminfo_dirs(TERMINFO_DIRS, filename, ttn, tp);
479 return _nc_read_tic_entry(filename, TERMINFO, ttn, tp);