📄 termcap.c
字号:
/* Work-alike for termcap, plus extra features.
Copyright (C) 1985, 86, 93, 94, 95 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to the
Free Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
/* Emacs config.h may rename various library functions such as malloc. */
#ifdef HAVE_CONFIG_H
#include <config.h>
/* Get the O_* definitions for open et al. */
#ifndef _MINIX
#include <sys/file.h>
#endif
#include <fcntl.h>
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#else
extern char *getenv ();
extern char *malloc ();
extern char *realloc ();
#endif
#else /* not HAVE_CONFIG_H */
#ifdef STDC_HEADERS
#include <stdlib.h>
#include <string.h>
#else
char *getenv ();
char *malloc ();
char *realloc ();
#endif
/* Do this after the include, in case string.h prototypes bcopy. */
#if (defined(HAVE_STRING_H) || defined(STDC_HEADERS)) && !defined(bcopy)
#define bcopy(s, d, n) memcpy ((d), (s), (n))
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef _POSIX_VERSION
#include <fcntl.h>
#endif
#endif /* not HAVE_CONFIG_H */
#ifndef NULL
#define NULL (char *) 0
#endif
#ifndef O_RDONLY
#define O_RDONLY 0
#endif
/* BUFSIZE is the initial size allocated for the buffer
for reading the termcap file.
It is not a limit.
Make it large normally for speed.
Make it variable when debugging, so can exercise
increasing the space dynamically. */
#ifndef BUFSIZE
#ifdef DEBUG
#define BUFSIZE bufsize
int bufsize = 128;
#else
#define BUFSIZE 2048
#endif
#endif
#include "ltcap.h"
#ifndef TERMCAP_FILE
#define TERMCAP_FILE "/etc/termcap"
#endif
#ifndef emacs
static void
memory_out ()
{
write (2, "virtual memory exhausted\n", 25);
exit (1);
}
static char *
xmalloc (size)
unsigned size;
{
register char *tem = malloc (size);
if (!tem)
memory_out ();
return tem;
}
static char *
xrealloc (ptr, size)
char *ptr;
unsigned size;
{
register char *tem = realloc (ptr, size);
if (!tem)
memory_out ();
return tem;
}
#endif /* not emacs */
/* Looking up capabilities in the entry already found. */
/* The pointer to the data made by tgetent is left here
for tgetnum, tgetflag and tgetstr to find. */
static char *term_entry;
static char *tgetst1 ();
/* Search entry BP for capability CAP.
Return a pointer to the capability (in BP) if found,
0 if not found. */
static char *
find_capability (bp, cap)
register char *bp, *cap;
{
for (; *bp; bp++)
if (bp[0] == ':'
&& bp[1] == cap[0]
&& bp[2] == cap[1])
return &bp[4];
return NULL;
}
__private_extern__
int
tgetnum (cap)
char *cap;
{
register char *ptr = find_capability (term_entry, cap);
if (!ptr || ptr[-1] != '#')
return -1;
return atoi (ptr);
}
__private_extern__
int
tgetflag (cap)
char *cap;
{
register char *ptr = find_capability (term_entry, cap);
return ptr && ptr[-1] == ':';
}
/* Look up a string-valued capability CAP.
If AREA is non-null, it points to a pointer to a block in which
to store the string. That pointer is advanced over the space used.
If AREA is null, space is allocated with `malloc'. */
__private_extern__
char *
tgetstr (cap, area)
char *cap;
char **area;
{
register char *ptr = find_capability (term_entry, cap);
if (!ptr || (ptr[-1] != '=' && ptr[-1] != '~'))
return NULL;
return tgetst1 (ptr, area);
}
/* Table, indexed by a character in range 0100 to 0140 with 0100 subtracted,
gives meaning of character following \, or a space if no special meaning.
Eight characters per line within the string. */
static char esctab[]
= " \007\010 \033\014 \
\012 \
\015 \011 \013 \
";
/* PTR points to a string value inside a termcap entry.
Copy that value, processing \ and ^ abbreviations,
into the block that *AREA points to,
or to newly allocated storage if AREA is NULL.
Return the address to which we copied the value,
or NULL if PTR is NULL. */
static char *
tgetst1 (ptr, area)
char *ptr;
char **area;
{
register char *p, *r;
register int c;
register int size;
char *ret;
register int c1;
if (!ptr)
return NULL;
/* `ret' gets address of where to store the string. */
if (!area)
{
/* Compute size of block needed (may overestimate). */
p = ptr;
while ((c = *p++) && c != ':' && c != '\n')
;
ret = (char *) xmalloc (p - ptr + 1);
}
else
ret = *area;
/* Copy the string value, stopping at null or colon.
Also process ^ and \ abbreviations. */
p = ptr;
r = ret;
while ((c = *p++) && c != ':' && c != '\n')
{
if (c == '^')
{
c = *p++;
if (c == '?')
c = 0177;
else
c &= 037;
}
else if (c == '\\')
{
c = *p++;
if (c >= '0' && c <= '7')
{
c -= '0';
size = 0;
while (++size < 3 && (c1 = *p) >= '0' && c1 <= '7')
{
c *= 8;
c += c1 - '0';
p++;
}
}
else if (c >= 0100 && c < 0200)
{
c1 = esctab[(c & ~040) - 0100];
if (c1 != ' ')
c = c1;
}
}
*r++ = c;
}
*r = '\0';
/* Update *AREA. */
if (area)
*area = r + 1;
return ret;
}
/* Outputting a string with padding. */
short ospeed;
/* If OSPEED is 0, we use this as the actual baud rate. */
int tputs_baud_rate;
__private_extern__ char PC = '\0';
/* Actual baud rate if positive;
- baud rate / 100 if negative. */
static int speeds[] =
{
#ifdef VMS
0, 50, 75, 110, 134, 150, -3, -6, -12, -18,
-20, -24, -36, -48, -72, -96, -192
#else /* not VMS */
0, 50, 75, 110, 135, 150, -2, -3, -6, -12,
-18, -24, -48, -96, -192, -288, -384, -576, -1152
#endif /* not VMS */
};
__private_extern__
void
tputs (str, nlines, outfun)
register char *str;
int nlines;
register int (*outfun) ();
{
register int padcount = 0;
register int speed;
#ifdef emacs
extern baud_rate;
speed = baud_rate;
/* For quite high speeds, convert to the smaller
units to avoid overflow. */
if (speed > 10000)
speed = - speed / 100;
#else
if (ospeed == 0)
speed = tputs_baud_rate;
else if (ospeed > 0 && ospeed < (sizeof speeds / sizeof speeds[0]))
speed = speeds[ospeed];
else
speed = 0;
#endif
if (!str)
return;
while (*str >= '0' && *str <= '9')
{
padcount += *str++ - '0';
padcount *= 10;
}
if (*str == '.')
{
str++;
padcount += *str++ - '0';
}
if (*str == '*')
{
str++;
padcount *= nlines;
}
while (*str)
(*outfun) (*str++);
/* PADCOUNT is now in units of tenths of msec.
SPEED is measured in characters per 10 seconds
or in characters per .1 seconds (if negative).
We use the smaller units for larger speeds to avoid overflow. */
padcount *= speed;
padcount += 500;
padcount /= 1000;
if (speed < 0)
padcount = -padcount;
else
{
padcount += 50;
padcount /= 100;
}
while (padcount-- > 0)
(*outfun) (PC);
}
/* Finding the termcap entry in the termcap data base. */
struct buffer
{
char *beg;
int size;
char *ptr;
int ateof;
int full;
};
/* Forward declarations of static functions. */
static int scan_file ();
static char *gobble_line ();
static int compare_contin ();
static int name_match ();
#ifdef VMS
#include <rmsdef.h>
#include <fab.h>
#include <nam.h>
static int
valid_filename_p (fn)
char *fn;
{
struct FAB fab = cc$rms_fab;
struct NAM nam = cc$rms_nam;
char esa[NAM$C_MAXRSS];
fab.fab$l_fna = fn;
fab.fab$b_fns = strlen(fn);
fab.fab$l_nam = &nam;
fab.fab$l_fop = FAB$M_NAM;
nam.nam$l_esa = esa;
nam.nam$b_ess = sizeof esa;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -