read_ent.c
来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 504 行 · 第 1/2 页
C
504 行
/****************************************************************************
* Copyright (c) 1998,1999,2000 Free Software Foundation, Inc. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the *
* "Software"), to deal in the Software without restriction, including *
* without limitation the rights to use, copy, modify, merge, publish, *
* distribute, distribute with modifications, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included *
* in all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
* IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
* THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
* *
* Except as contained in this notice, the name(s) of the above copyright *
* holders shall not be used in advertising or otherwise to promote the *
* sale, use or other dealings in this Software without prior written *
* authorization. *
****************************************************************************/
/****************************************************************************
* Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
* and: Eric S. Raymond <esr@snark.thyrsus.com> *
****************************************************************************/
/*
* read_entry.c -- Routine for reading in a compiled terminfo file
*
*/
#include <curses_p.h>
#include <tic.h>
#include <term_ent.h>
MODULE_ID("$Id: read_entry.c,v 1.72 2000/12/10 02:55:08 tom Exp $")
#if !HAVE_TELL
#define tell(fd) 0 /* lseek() is POSIX, but not tell() - odd... */
#endif
/*
* int
* _nc_read_file_entry(filename, ptr)
*
* Read the compiled terminfo entry in the given file into the
* structure pointed to by ptr, allocating space for the string
* table.
*/
#undef BYTE
#define BYTE(p,n) (unsigned char)((p)[n])
#define IS_NEG1(p) ((BYTE(p,0) == 0377) && (BYTE(p,1) == 0377))
#define IS_NEG2(p) ((BYTE(p,0) == 0376) && (BYTE(p,1) == 0377))
#define LOW_MSB(p) (BYTE(p,0) + 256*BYTE(p,1))
static bool have_tic_directory = FALSE;
static bool keep_tic_directory = FALSE;
/*
* Record the "official" location of the terminfo directory, according to
* the place where we're writing to, or the normal default, if not.
*/
NCURSES_EXPORT(const char *)
_nc_tic_dir(const char *path)
{
static const char *result = TERMINFO;
if (!keep_tic_directory) {
if (path != 0) {
result = path;
have_tic_directory = TRUE;
} else if (!have_tic_directory && use_terminfo_vars()) {
char *envp;
if ((envp = getenv("TERMINFO")) != 0)
return _nc_tic_dir(envp);
}
}
return result;
}
/*
* Special fix to prevent the terminfo directory from being moved after tic
* has chdir'd to it. If we let it be changed, then if $TERMINFO has a
* relative path, we'll lose track of the actual directory.
*/
NCURSES_EXPORT(void)
_nc_keep_tic_dir(const char *path)
{
_nc_tic_dir(path);
keep_tic_directory = TRUE;
}
static void
convert_shorts(char *buf, short *Numbers, int count)
{
int i;
for (i = 0; i < count; i++) {
if (IS_NEG1(buf + 2 * i))
Numbers[i] = ABSENT_NUMERIC;
else if (IS_NEG2(buf + 2 * i))
Numbers[i] = CANCELLED_NUMERIC;
else
Numbers[i] = LOW_MSB(buf + 2 * i);
TR(TRACE_DATABASE, ("get Numbers[%d]=%d", i, Numbers[i]));
}
}
static void
convert_strings(char *buf, char **Strings, int count, int size, char *table)
{
int i;
char *p;
for (i = 0; i < count; i++) {
if (IS_NEG1(buf + 2 * i)) {
Strings[i] = ABSENT_STRING;
} else if (IS_NEG2(buf + 2 * i)) {
Strings[i] = CANCELLED_STRING;
} else if (LOW_MSB(buf + 2 * i) > size) {
Strings[i] = ABSENT_STRING;
} else {
Strings[i] = (LOW_MSB(buf + 2 * i) + table);
TR(TRACE_DATABASE, ("Strings[%d] = %s", i, _nc_visbuf(Strings[i])));
}
/* make sure all strings are NUL terminated */
if (VALID_STRING(Strings[i])) {
for (p = Strings[i]; p <= table + size; p++)
if (*p == '\0')
break;
/* if there is no NUL, ignore the string */
if (p > table + size)
Strings[i] = ABSENT_STRING;
}
}
}
#define read_shorts(fd, buf, count) (read(fd, buf, (count)*2) == (count)*2)
#define even_boundary(value) \
if ((value) % 2 != 0) read(fd, buf, 1)
static int
read_termtype(int fd, TERMTYPE * ptr)
/* return 1 if read, 0 if not found or garbled */
{
int name_size, bool_count, num_count, str_count, str_size;
int i;
char buf[MAX_ENTRY_SIZE];
TR(TRACE_DATABASE, ("READ termtype header @%d", tell(fd)));
memset(ptr, 0, sizeof(*ptr));
/* grab the header */
if (!read_shorts(fd, buf, 6)
|| LOW_MSB(buf) != MAGIC) {
return (0);
}
_nc_free_termtype(ptr);
name_size = LOW_MSB(buf + 2);
bool_count = LOW_MSB(buf + 4);
num_count = LOW_MSB(buf + 6);
str_count = LOW_MSB(buf + 8);
str_size = LOW_MSB(buf + 10);
TR(TRACE_DATABASE,
("TERMTYPE name_size=%d, bool=%d/%d, num=%d/%d str=%d/%d(%d)",
name_size, bool_count, BOOLCOUNT, num_count, NUMCOUNT,
str_count, STRCOUNT, str_size));
if (name_size < 0
|| bool_count < 0
|| num_count < 0
|| str_count < 0
|| str_size < 0) {
return (0);
}
if (str_size) {
/* try to allocate space for the string table */
if (str_count * 2 >= (int) sizeof(buf)
|| (ptr->str_table = typeMalloc(char, (unsigned) str_size)) == 0) {
return (0);
}
} else {
str_count = 0;
}
/* grab the name (a null-terminate string) */
read(fd, buf, min(MAX_NAME_SIZE, (unsigned) name_size));
buf[MAX_NAME_SIZE] = '\0';
ptr->term_names = typeCalloc(char, strlen(buf) + 1);
if (ptr->term_names == NULL) {
return (0);
}
(void) strcpy(ptr->term_names, buf);
if (name_size > MAX_NAME_SIZE)
lseek(fd, (off_t) (name_size - MAX_NAME_SIZE), 1);
/* grab the booleans */
if ((ptr->Booleans = typeCalloc(char, max(BOOLCOUNT, bool_count))) == 0
|| read(fd, ptr->Booleans, (unsigned) bool_count) < bool_count) {
return (0);
}
/*
* If booleans end on an odd byte, skip it. The machine they
* originally wrote terminfo on must have been a 16-bit
* word-oriented machine that would trap out if you tried a
* word access off a 2-byte boundary.
*/
even_boundary(name_size + bool_count);
/* grab the numbers */
if ((ptr->Numbers = typeCalloc(short, max(NUMCOUNT, num_count))) == 0
|| !read_shorts(fd, buf, num_count)) {
return (0);
}
convert_shorts(buf, ptr->Numbers, num_count);
if ((ptr->Strings = typeCalloc(char *, max(STRCOUNT, str_count))) == 0)
return (0);
if (str_count) {
/* grab the string offsets */
if (!read_shorts(fd, buf, str_count)) {
return (0);
}
/* finally, grab the string table itself */
if (read(fd, ptr->str_table, (unsigned) str_size) != str_size)
return (0);
convert_strings(buf, ptr->Strings, str_count, str_size, ptr->str_table);
}
#if NCURSES_XNAMES
ptr->num_Booleans = BOOLCOUNT;
ptr->num_Numbers = NUMCOUNT;
ptr->num_Strings = STRCOUNT;
/*
* Read extended entries, if any, after the normal end of terminfo data.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?