This commit is contained in:
David Given 2018-06-02 20:57:43 +02:00
parent d1cbb9cf60
commit d6e65833fb

View file

@ -26,51 +26,56 @@
short ospeed; /* output speed */ short ospeed; /* output speed */
char PC; /* padding character */ char PC; /* padding character */
char *BC; /* back cursor movement */ char* BC; /* back cursor movement */
char *UP; /* up cursor movement */ char* UP; /* up cursor movement */
static const char *capab; /* the capability itself */ static const char* capab; /* the capability itself */
static int check_for_tc(void); static int check_for_tc(void);
static int match_name(const char *buf, const char *name); static int match_name(const char* buf, const char* name);
/* /*
* tgetent - get the termcap entry for terminal name, and put it * tgetent - get the termcap entry for terminal name, and put it
* in bp (which must be an array of 1024 chars). Returns 1 if * in bp (which must be an array of 1024 chars). Returns 1 if
* termcap entry found, 0 if not found, and -1 if file not found. * termcap entry found, 0 if not found, and -1 if file not found.
*/ */
int int tgetent(char* bp, const char* name)
tgetent(char *bp, const char *name)
{ {
FILE *fp; FILE* fp;
char *file; char* file;
char *cp; char* cp;
char buf[1024]; char buf[1024];
capab = bp; capab = bp;
if ((file = getenv("TERMCAP")) != (char *) NULL) { if ((file = getenv("TERMCAP")) != (char*)NULL)
if (*file != '/' && {
(cp = getenv("TERM")) != NULL && strcmp(name, cp) == 0) { if (*file != '/' && (cp = getenv("TERM")) != NULL && strcmp(name, cp) == 0)
(void) strcpy(bp, file); {
return(1); (void)strcpy(bp, file);
return (1);
} }
else file = "/etc/termcap"; else
} else
file = "/etc/termcap"; file = "/etc/termcap";
if ((fp = fopen(file, "r")) == (FILE *) NULL) }
return(-1); else
while (fgets(buf, 1024, fp) != NULL) { file = "/etc/termcap";
if (buf[0] == '#') continue; if ((fp = fopen(file, "r")) == (FILE*)NULL)
return (-1);
while (fgets(buf, 1024, fp) != NULL)
{
if (buf[0] == '#')
continue;
while (*(cp = &buf[strlen(buf) - 2]) == '\\') while (*(cp = &buf[strlen(buf) - 2]) == '\\')
if (fgets(cp, 1024, fp) == NULL) if (fgets(cp, 1024, fp) == NULL)
return (0); return (0);
if (match_name(buf, name)) { if (match_name(buf, name))
{
strcpy(bp, buf); strcpy(bp, buf);
fclose(fp); fclose(fp);
return(check_for_tc()); return (check_for_tc());
} }
} }
fclose(fp); fclose(fp);
return(0); return (0);
} }
/* /*
@ -78,17 +83,22 @@ tgetent(char *bp, const char *name)
* match is found. * match is found.
*/ */
static int static int
match_name(const char *buf, const char *name) match_name(const char* buf, const char* name)
{ {
register const char *tp = buf; register const char* tp = buf;
register const char *np; register const char* np;
for (;;) { for (;;)
for (np = name; *np && *tp == *np; np++, tp++) { } {
for (np = name; *np && *tp == *np; np++, tp++)
{
}
if (*np == 0 && (*tp == '|' || *tp == ':' || *tp == 0)) if (*np == 0 && (*tp == '|' || *tp == ':' || *tp == 0))
return(1); return (1);
while (*tp != 0 && *tp != '|' && *tp != ':') tp++; while (*tp != 0 && *tp != '|' && *tp != ':')
if (*tp++ != '|') return (0); tp++;
if (*tp++ != '|')
return (0);
} }
} }
@ -99,64 +109,70 @@ static int
check_for_tc(void) check_for_tc(void)
{ {
static int count = 0; static int count = 0;
const char *savcapab = capab; const char* savcapab = capab;
char buf[1024]; char buf[1024];
char terminalname[128]; char terminalname[128];
register char *p = (char *)capab + strlen(capab) - 2, *q; register char *p = (char*)capab + strlen(capab) - 2, *q;
while (*p != ':') while (*p != ':')
if (--p < (char *)capab) if (--p < (char*)capab)
return(0); /* no : in termcap entry */ return (0); /* no : in termcap entry */
if (p[1] != 't' || p[2] != 'c') if (p[1] != 't' || p[2] != 'c')
return(1); return (1);
if (count > 16) return(0); /* recursion in tc= definitions */ if (count > 16)
return (0); /* recursion in tc= definitions */
count++; count++;
strcpy(terminalname, &p[4]); strcpy(terminalname, &p[4]);
q = terminalname; q = terminalname;
while (*q && *q != ':') q++; while (*q && *q != ':')
q++;
*q = 0; *q = 0;
if (tgetent(buf, terminalname) != 1) { if (tgetent(buf, terminalname) != 1)
{
--count; --count;
return(0); return (0);
} }
--count; --count;
for (q = buf; *q && *q != ':'; q++) { } for (q = buf; *q && *q != ':'; q++)
{
}
strcpy(p, q); strcpy(p, q);
capab = savcapab; capab = savcapab;
return(1); return (1);
} }
/* /*
* tgetnum - get the numeric terminal capability corresponding * tgetnum - get the numeric terminal capability corresponding
* to id. Returns the value, -1 if invalid. * to id. Returns the value, -1 if invalid.
*/ */
int int tgetnum(const char* id)
tgetnum(const char *id)
{ {
const char *cp; const char* cp;
int ret; int ret;
if ((cp = capab) == NULL || id == NULL) if ((cp = capab) == NULL || id == NULL)
return(-1); return (-1);
while (*++cp != ':') while (*++cp != ':')
; ;
while (*cp) { while (*cp)
{
cp++; cp++;
while (ISSPACE(*cp)) while (ISSPACE(*cp))
cp++; cp++;
if (strncmp(cp, id, CAPABLEN) == 0) { if (strncmp(cp, id, CAPABLEN) == 0)
{
while (*cp && *cp != ':' && *cp != '#') while (*cp && *cp != ':' && *cp != '#')
cp++; cp++;
if (*cp != '#') if (*cp != '#')
return(-1); return (-1);
for (ret = 0, cp++ ; *cp && ISDIGIT(*cp) ; cp++) for (ret = 0, cp++; *cp && ISDIGIT(*cp); cp++)
ret = ret * 10 + *cp - '0'; ret = ret * 10 + *cp - '0';
return(ret); return (ret);
} }
while (*cp && *cp != ':') while (*cp && *cp != ':')
cp++; cp++;
} }
return(-1); return (-1);
} }
/* /*
@ -164,25 +180,25 @@ tgetnum(const char *id)
* if invalid, 0 if the flag is not in termcap entry, or 1 if it is * if invalid, 0 if the flag is not in termcap entry, or 1 if it is
* present. * present.
*/ */
int int tgetflag(const char* id)
tgetflag(const char *id)
{ {
const char *cp; const char* cp;
if ((cp = capab) == NULL || id == NULL) if ((cp = capab) == NULL || id == NULL)
return(-1); return (-1);
while (*++cp != ':') while (*++cp != ':')
; ;
while (*cp) { while (*cp)
{
cp++; cp++;
while (ISSPACE(*cp)) while (ISSPACE(*cp))
cp++; cp++;
if (strncmp(cp, id, CAPABLEN) == 0) if (strncmp(cp, id, CAPABLEN) == 0)
return(1); return (1);
while (*cp && *cp != ':') while (*cp && *cp != ':')
cp++; cp++;
} }
return(0); return (0);
} }
/* /*
@ -190,76 +206,79 @@ tgetflag(const char *id)
* it in area (advancing area at same time). Expand escape sequences * it in area (advancing area at same time). Expand escape sequences
* etc. Returns the string, or NULL if it can't do it. * etc. Returns the string, or NULL if it can't do it.
*/ */
char * char* tgetstr(const char* id, char** const area)
tgetstr(const char *id, char ** const area)
{ {
const char *cp; const char* cp;
char *ret; char* ret;
int i; int i;
if ((cp = capab) == NULL || id == NULL) if ((cp = capab) == NULL || id == NULL)
return(NULL); return (NULL);
while (*++cp != ':') while (*++cp != ':')
; ;
while (*cp) { while (*cp)
{
cp++; cp++;
while (ISSPACE(*cp)) while (ISSPACE(*cp))
cp++; cp++;
if (strncmp(cp, id, CAPABLEN) == 0) { if (strncmp(cp, id, CAPABLEN) == 0)
{
while (*cp && *cp != ':' && *cp != '=') while (*cp && *cp != ':' && *cp != '=')
cp++; cp++;
if (*cp != '=') if (*cp != '=')
return(NULL); return (NULL);
for (ret = *area, cp++; *cp && *cp != ':' ; (*area)++, cp++) for (ret = *area, cp++; *cp && *cp != ':'; (*area)++, cp++)
switch(*cp) { switch (*cp)
case '^' : {
case '^':
**area = *++cp - 'A' + 1; **area = *++cp - 'A' + 1;
break; break;
case '\\' : case '\\':
switch(*++cp) { switch (*++cp)
case 'E' : {
case 'E':
**area = '\033'; **area = '\033';
break; break;
case 'n' : case 'n':
**area = '\n'; **area = '\n';
break; break;
case 'r' : case 'r':
**area = '\r'; **area = '\r';
break; break;
case 't' : case 't':
**area = '\t'; **area = '\t';
break; break;
case 'b' : case 'b':
**area = '\b'; **area = '\b';
break; break;
case 'f' : case 'f':
**area = '\f'; **area = '\f';
break; break;
case '0' : case '0':
case '1' : case '1':
case '2' : case '2':
case '3' : case '3':
for (i=0 ; *cp && ISDIGIT(*cp) ; cp++) for (i = 0; *cp && ISDIGIT(*cp); cp++)
i = i * 8 + *cp - '0'; i = i * 8 + *cp - '0';
**area = i; **area = i;
cp--; cp--;
break; break;
case '^' : case '^':
case '\\' : case '\\':
**area = *cp; **area = *cp;
break; break;
} }
break; break;
default : default:
**area = *cp; **area = *cp;
} }
*(*area)++ = '\0'; *(*area)++ = '\0';
return(ret); return (ret);
} }
while (*cp && *cp != ':') while (*cp && *cp != ':')
cp++; cp++;
} }
return(NULL); return (NULL);
} }
/* /*
@ -267,44 +286,49 @@ tgetstr(const char *id, char ** const area)
* for the cursor to go to (destcol, destline), and return the string. * for the cursor to go to (destcol, destline), and return the string.
* Returns "OOPS" if something's gone wrong, or the string otherwise. * Returns "OOPS" if something's gone wrong, or the string otherwise.
*/ */
char * char* tgoto(const char* cm, int destcol, int destline)
tgoto(const char *cm, int destcol, int destline)
{ {
register char *rp; register char* rp;
static char ret[24]; static char ret[24];
char added[16]; char added[16];
int *dp = &destline; int* dp = &destline;
int numval; int numval;
int swapped = 0; int swapped = 0;
added[0] = 0; added[0] = 0;
for (rp = ret ; *cm ; cm++) { for (rp = ret; *cm; cm++)
if (*cm == '%') { {
switch(*++cm) { if (*cm == '%')
case '>' : {
switch (*++cm)
{
case '>':
if (dp == NULL) if (dp == NULL)
return("OOPS"); return ("OOPS");
cm++; cm++;
if (*dp > *cm++) { if (*dp > *cm++)
{
*dp += *cm; *dp += *cm;
} }
break; break;
case '+' : case '+':
case '.' : case '.':
if (dp == NULL) if (dp == NULL)
return("OOPS"); return ("OOPS");
if (*cm == '+') *dp = *dp + *++cm; if (*cm == '+')
for (;;) { *dp = *dp + *++cm;
switch(*dp) { for (;;)
{
switch (*dp)
{
case 0: case 0:
case 04: case 04:
case '\t': case '\t':
case '\n': case '\n':
/* filter these out */ /* filter these out */
if (dp == &destcol || swapped || UP) { if (dp == &destcol || swapped || UP)
strcat(added, dp == &destcol || swapped ? {
(BC ? BC : "\b") : strcat(added, dp == &destcol || swapped ? (BC ? BC : "\b") : UP);
UP);
(*dp)++; (*dp)++;
continue; continue;
} }
@ -315,7 +339,8 @@ tgoto(const char *cm, int destcol, int destline)
dp = (dp == &destline) ? &destcol : NULL; dp = (dp == &destline) ? &destcol : NULL;
break; break;
case 'r' : { case 'r':
{
int tmp = destline; int tmp = destline;
destline = destcol; destline = destcol;
@ -323,62 +348,67 @@ tgoto(const char *cm, int destcol, int destline)
swapped = 1 - swapped; swapped = 1 - swapped;
break; break;
} }
case 'n' : case 'n':
destcol ^= 0140; destcol ^= 0140;
destline ^= 0140; destline ^= 0140;
break; break;
case '%' : case '%':
*rp++ = '%'; *rp++ = '%';
break; break;
case 'i' : case 'i':
destcol++; destcol++;
destline++; destline++;
break; break;
case 'B' : case 'B':
if (dp == NULL) if (dp == NULL)
return("OOPS"); return ("OOPS");
*dp = 16 * (*dp / 10) + *dp % 10; *dp = 16 * (*dp / 10) + *dp % 10;
break; break;
case 'D' : case 'D':
if (dp == NULL) if (dp == NULL)
return("OOPS"); return ("OOPS");
*dp = *dp - 2 * (*dp % 16); *dp = *dp - 2 * (*dp % 16);
break; break;
case 'd' : case 'd':
case '2' : case '2':
case '3' : case '3':
if (dp == NULL) if (dp == NULL)
return("OOPS"); return ("OOPS");
numval = *dp; numval = *dp;
dp = (dp == &destline) ? &destcol : NULL; dp = (dp == &destline) ? &destcol : NULL;
if (numval >= 100) { if (numval >= 100)
{
*rp++ = '0' + numval / 100; *rp++ = '0' + numval / 100;
} }
else if (*cm == '3') { else if (*cm == '3')
{
*rp++ = ' '; *rp++ = ' ';
} }
if (numval >= 10) { if (numval >= 10)
*rp++ = '0' + ((numval%100)/10); {
*rp++ = '0' + ((numval % 100) / 10);
} }
else if (*cm == '3' || *cm == '2') { else if (*cm == '3' || *cm == '2')
{
*rp++ = ' '; *rp++ = ' ';
} }
*rp++ = '0' + (numval%10); *rp++ = '0' + (numval % 10);
break; break;
default : default:
return("OOPS"); return ("OOPS");
} }
} }
else *rp++ = *cm; else
*rp++ = *cm;
} }
*rp = '\0'; *rp = '\0';
strcpy(rp, added); strcpy(rp, added);
return(ret); return (ret);
} }
static int tens_of_ms_p_char[] = { /* index as returned by gtty */ static int tens_of_ms_p_char[] = { /* index as returned by gtty */
@ -389,37 +419,40 @@ static int tens_of_ms_p_char[] = { /* index as returned by gtty */
* tputs - put the string cp out onto the terminal, using the function * tputs - put the string cp out onto the terminal, using the function
* outc. Also handle padding. * outc. Also handle padding.
*/ */
int int tputs(register const char* cp, int affcnt, int (*outc)(int))
tputs(register const char *cp, int affcnt, int (*outc)(int))
{ {
int delay = 0; int delay = 0;
if (cp == NULL) if (cp == NULL)
return(1); return (1);
while (ISDIGIT(*cp)) { while (ISDIGIT(*cp))
{
delay = delay * 10 + (*cp++ - '0'); delay = delay * 10 + (*cp++ - '0');
} }
delay *= 10; delay *= 10;
if (*cp == '.') { if (*cp == '.')
{
cp++; cp++;
if (ISDIGIT(*cp)) { if (ISDIGIT(*cp))
{
delay += *cp++ - '0'; delay += *cp++ - '0';
} }
while (ISDIGIT(*cp)) cp++; while (ISDIGIT(*cp))
cp++;
} }
if (*cp == '*') { if (*cp == '*')
{
delay *= affcnt; delay *= affcnt;
cp++; cp++;
} }
while (*cp) while (*cp)
(*outc)(*cp++); (*outc)(*cp++);
if (delay != 0 && if (delay != 0 && ospeed > 0 && ospeed < (sizeof tens_of_ms_p_char / sizeof tens_of_ms_p_char[0]))
ospeed > 0 && {
ospeed < (sizeof tens_of_ms_p_char / sizeof tens_of_ms_p_char[0])) { delay = (delay + tens_of_ms_p_char[ospeed] - 1) / tens_of_ms_p_char[ospeed];
delay = (delay + tens_of_ms_p_char[ospeed] - 1) / while (delay--)
tens_of_ms_p_char[ospeed]; (*outc)(PC);
while (delay--) (*outc)(PC);
} }
return(1); return (1);
} }
/* /*