36
36
* read_entry.c -- Routine for reading in a compiled terminfo file
40
39
#include <curses.priv.h>
40
#include <hashed_db.h>
43
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() */
45
MODULE_ID("$Id: read_entry.c,v 1.99 2006/08/19 15:58:50 tom Exp $")
51
47
#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
51
convert_shorts(char *buf, short *Numbers, int count)
151
#define read_shorts(fd, buf, count) \
152
(read(fd, buf, (unsigned) (count)*2) == (int) (count)*2)
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)
154
116
#define even_boundary(value) \
155
if ((value) % 2 != 0) read(fd, buf, 1)
117
if ((value) % 2 != 0) Read(buf, 1)
158
read_termtype(int fd, TERMTYPE *ptr)
120
_nc_read_termtype(TERMTYPE *ptr, char *buffer, int limit)
159
121
/* return 1 if read, 0 if not found or garbled */
161
124
int name_size, bool_count, num_count, str_count, str_size;
163
126
char buf[MAX_ENTRY_SIZE + 1];
164
128
unsigned want, have;
166
TR(TRACE_DATABASE, ("READ termtype header @%ld", (long) tell(fd)));
130
TR(TRACE_DATABASE, ("READ termtype header @%d", offset));
168
132
memset(ptr, 0, sizeof(*ptr));
170
134
/* grab the header */
171
if (!read_shorts(fd, buf, 6)
172
|| LOW_MSB(buf) != MAGIC) {
135
if (!read_shorts(buf, 6)
136
|| !IS_TIC_MAGIC(buf)) {
176
140
name_size = LOW_MSB(buf + 2);
190
154
|| str_size < 0) {
158
want = str_size + name_size + 1;
195
160
/* try to allocate space for the string table */
196
161
if (str_count * 2 >= (int) sizeof(buf)
197
|| (ptr->str_table = typeMalloc(char, (unsigned) str_size)) == 0) {
162
|| (string_table = typeMalloc(char, want)) == 0) {
167
if ((string_table = typeMalloc(char, want)) == 0) {
204
172
/* grab the name (a null-terminated string) */
205
173
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);
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);
215
182
if (have > MAX_NAME_SIZE)
216
lseek(fd, (off_t) (have - MAX_NAME_SIZE), 1);
183
offset = (have - MAX_NAME_SIZE);
218
185
/* 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) {
186
if ((ptr->Booleans = TYPE_CALLOC(NCURSES_SBOOL,
187
max(BOOLCOUNT, bool_count))) == 0
188
|| Read(ptr->Booleans, (unsigned) bool_count) < bool_count) {
232
200
/* grab the numbers */
233
201
if ((ptr->Numbers = TYPE_CALLOC(short, max(NUMCOUNT, num_count))) == 0
234
|| !read_shorts(fd, buf, num_count)) {
202
|| !read_shorts(buf, num_count)) {
237
205
convert_shorts(buf, ptr->Numbers, num_count);
239
207
if ((ptr->Strings = TYPE_CALLOC(char *, max(STRCOUNT, str_count))) == 0)
243
211
/* grab the string offsets */
244
if (!read_shorts(fd, buf, str_count)) {
212
if (!read_shorts(buf, str_count)) {
247
215
/* 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);
216
if (Read(string_table, (unsigned) str_size) != str_size)
218
convert_strings(buf, ptr->Strings, str_count, str_size, string_table);
252
220
#if NCURSES_XNAMES
291
259
ext_bool_count, ext_num_count, ext_str_count,
292
260
ext_str_size, ext_str_limit));
294
TR(TRACE_DATABASE, ("READ %d extended-booleans @%ld",
295
ext_bool_count, (long) tell(fd)));
262
TR(TRACE_DATABASE, ("READ %d extended-booleans @%d",
263
ext_bool_count, offset));
296
264
if ((ptr->ext_Booleans = ext_bool_count) != 0) {
297
if (read(fd, ptr->Booleans + BOOLCOUNT, (unsigned)
265
if (Read(ptr->Booleans + BOOLCOUNT, (unsigned)
298
266
ext_bool_count) != ext_bool_count)
301
269
even_boundary(ext_bool_count);
303
TR(TRACE_DATABASE, ("READ %d extended-numbers @%ld",
304
ext_num_count, (long) tell(fd)));
271
TR(TRACE_DATABASE, ("READ %d extended-numbers @%d",
272
ext_num_count, offset));
305
273
if ((ptr->ext_Numbers = ext_num_count) != 0) {
306
if (!read_shorts(fd, buf, ext_num_count))
274
if (!read_shorts(buf, ext_num_count))
308
276
TR(TRACE_DATABASE, ("Before converting extended-numbers"));
309
277
convert_shorts(buf, ptr->Numbers + NUMCOUNT, ext_num_count);
312
TR(TRACE_DATABASE, ("READ extended-offsets @%ld", (long) tell(fd)));
280
TR(TRACE_DATABASE, ("READ extended-offsets @%d", offset));
313
281
if ((ext_str_count || need)
314
&& !read_shorts(fd, buf, ext_str_count + need))
282
&& !read_shorts(buf, ext_str_count + need))
317
TR(TRACE_DATABASE, ("READ %d bytes of extended-strings @%ld",
318
ext_str_limit, (long) tell(fd)));
285
TR(TRACE_DATABASE, ("READ %d bytes of extended-strings @%d",
286
ext_str_limit, offset));
320
288
if (ext_str_limit) {
321
289
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)
291
if (Read(ptr->ext_str_table, (unsigned) ext_str_limit) != ext_str_limit)
325
293
TR(TRACE_DATABASE, ("first extended-string is %s", _nc_visbuf(ptr->ext_str_table)));
379
347
for (i = str_count; i < STRCOUNT; i++)
380
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
385
361
NCURSES_EXPORT(int)
386
362
_nc_read_file_entry(const char *const filename, TERMTYPE *ptr)
387
363
/* return 1 if read, 0 if not found or garbled */
389
365
int code, fd = -1;
367
char buffer[MAX_ENTRY_SIZE + 1];
391
369
if (_nc_access(filename, R_OK) < 0
392
370
|| (fd = open(filename, O_RDONLY | O_BINARY)) < 0) {
393
371
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);
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);
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) {
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/%c/%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 */
460
* _nc_read_entry(char *tn, char *filename, TERMTYPE *tp)
503
* _nc_read_entry(char *name, char *filename, TERMTYPE *tp)
462
505
* Find and read the compiled entry for a given terminal type,
463
506
* if it exists. We take pains here to make sure no combination
468
511
NCURSES_EXPORT(int)
469
_nc_read_entry(const char *const tn, char *const filename, TERMTYPE *const tp)
512
_nc_read_entry(const char *const name, 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) {
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) {
504
/* this is an ncurses extension */
505
if ((envp = getenv("TERMINFO_DIRS")) != 0)
506
return _nc_read_terminfo_dirs(envp, filename, ttn, tp);
537
if (code != TGETENT_YES) {
538
code = _nc_read_termcap_entry(name, tp);
539
sprintf(filename, "%.*s", PATH_MAX - 1, _nc_get_source());
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);