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

View file

@ -38,8 +38,7 @@ static int match_name(const char *buf, const char *name);
* 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.
*/
int
tgetent(char *bp, const char *name)
int tgetent(char* bp, const char* name)
{
FILE* fp;
char* file;
@ -47,23 +46,29 @@ tgetent(char *bp, const char *name)
char buf[1024];
capab = bp;
if ((file = getenv("TERMCAP")) != (char *) NULL) {
if (*file != '/' &&
(cp = getenv("TERM")) != NULL && strcmp(name, cp) == 0) {
if ((file = getenv("TERMCAP")) != (char*)NULL)
{
if (*file != '/' && (cp = getenv("TERM")) != NULL && strcmp(name, cp) == 0)
{
(void)strcpy(bp, file);
return (1);
}
else file = "/etc/termcap";
} else
else
file = "/etc/termcap";
}
else
file = "/etc/termcap";
if ((fp = fopen(file, "r")) == (FILE*)NULL)
return (-1);
while (fgets(buf, 1024, fp) != NULL) {
if (buf[0] == '#') continue;
while (fgets(buf, 1024, fp) != NULL)
{
if (buf[0] == '#')
continue;
while (*(cp = &buf[strlen(buf) - 2]) == '\\')
if (fgets(cp, 1024, fp) == NULL)
return (0);
if (match_name(buf, name)) {
if (match_name(buf, name))
{
strcpy(bp, buf);
fclose(fp);
return (check_for_tc());
@ -83,12 +88,17 @@ match_name(const char *buf, const char *name)
register const char* tp = buf;
register const char* np;
for (;;) {
for (np = name; *np && *tp == *np; np++, tp++) { }
for (;;)
{
for (np = name; *np && *tp == *np; np++, tp++)
{
}
if (*np == 0 && (*tp == '|' || *tp == ':' || *tp == 0))
return (1);
while (*tp != 0 && *tp != '|' && *tp != ':') tp++;
if (*tp++ != '|') return (0);
while (*tp != 0 && *tp != '|' && *tp != ':')
tp++;
if (*tp++ != '|')
return (0);
}
}
@ -109,18 +119,23 @@ check_for_tc(void)
return (0); /* no : in termcap entry */
if (p[1] != 't' || p[2] != 'c')
return (1);
if (count > 16) return(0); /* recursion in tc= definitions */
if (count > 16)
return (0); /* recursion in tc= definitions */
count++;
strcpy(terminalname, &p[4]);
q = terminalname;
while (*q && *q != ':') q++;
while (*q && *q != ':')
q++;
*q = 0;
if (tgetent(buf, terminalname) != 1) {
if (tgetent(buf, terminalname) != 1)
{
--count;
return (0);
}
--count;
for (q = buf; *q && *q != ':'; q++) { }
for (q = buf; *q && *q != ':'; q++)
{
}
strcpy(p, q);
capab = savcapab;
return (1);
@ -130,8 +145,7 @@ check_for_tc(void)
* tgetnum - get the numeric terminal capability corresponding
* to id. Returns the value, -1 if invalid.
*/
int
tgetnum(const char *id)
int tgetnum(const char* id)
{
const char* cp;
int ret;
@ -140,11 +154,13 @@ tgetnum(const char *id)
return (-1);
while (*++cp != ':')
;
while (*cp) {
while (*cp)
{
cp++;
while (ISSPACE(*cp))
cp++;
if (strncmp(cp, id, CAPABLEN) == 0) {
if (strncmp(cp, id, CAPABLEN) == 0)
{
while (*cp && *cp != ':' && *cp != '#')
cp++;
if (*cp != '#')
@ -164,8 +180,7 @@ tgetnum(const char *id)
* if invalid, 0 if the flag is not in termcap entry, or 1 if it is
* present.
*/
int
tgetflag(const char *id)
int tgetflag(const char* id)
{
const char* cp;
@ -173,7 +188,8 @@ tgetflag(const char *id)
return (-1);
while (*++cp != ':')
;
while (*cp) {
while (*cp)
{
cp++;
while (ISSPACE(*cp))
cp++;
@ -190,8 +206,7 @@ tgetflag(const char *id)
* it in area (advancing area at same time). Expand escape sequences
* etc. Returns the string, or NULL if it can't do it.
*/
char *
tgetstr(const char *id, char ** const area)
char* tgetstr(const char* id, char** const area)
{
const char* cp;
char* ret;
@ -201,22 +216,26 @@ tgetstr(const char *id, char ** const area)
return (NULL);
while (*++cp != ':')
;
while (*cp) {
while (*cp)
{
cp++;
while (ISSPACE(*cp))
cp++;
if (strncmp(cp, id, CAPABLEN) == 0) {
if (strncmp(cp, id, CAPABLEN) == 0)
{
while (*cp && *cp != ':' && *cp != '=')
cp++;
if (*cp != '=')
return (NULL);
for (ret = *area, cp++; *cp && *cp != ':'; (*area)++, cp++)
switch(*cp) {
switch (*cp)
{
case '^':
**area = *++cp - 'A' + 1;
break;
case '\\':
switch(*++cp) {
switch (*++cp)
{
case 'E':
**area = '\033';
break;
@ -267,8 +286,7 @@ tgetstr(const char *id, char ** const area)
* for the cursor to go to (destcol, destline), and return the string.
* Returns "OOPS" if something's gone wrong, or the string otherwise.
*/
char *
tgoto(const char *cm, int destcol, int destline)
char* tgoto(const char* cm, int destcol, int destline)
{
register char* rp;
static char ret[24];
@ -278,14 +296,18 @@ tgoto(const char *cm, int destcol, int destline)
int swapped = 0;
added[0] = 0;
for (rp = ret ; *cm ; cm++) {
if (*cm == '%') {
switch(*++cm) {
for (rp = ret; *cm; cm++)
{
if (*cm == '%')
{
switch (*++cm)
{
case '>':
if (dp == NULL)
return ("OOPS");
cm++;
if (*dp > *cm++) {
if (*dp > *cm++)
{
*dp += *cm;
}
break;
@ -293,18 +315,20 @@ tgoto(const char *cm, int destcol, int destline)
case '.':
if (dp == NULL)
return ("OOPS");
if (*cm == '+') *dp = *dp + *++cm;
for (;;) {
switch(*dp) {
if (*cm == '+')
*dp = *dp + *++cm;
for (;;)
{
switch (*dp)
{
case 0:
case 04:
case '\t':
case '\n':
/* filter these out */
if (dp == &destcol || swapped || UP) {
strcat(added, dp == &destcol || swapped ?
(BC ? BC : "\b") :
UP);
if (dp == &destcol || swapped || UP)
{
strcat(added, dp == &destcol || swapped ? (BC ? BC : "\b") : UP);
(*dp)++;
continue;
}
@ -315,7 +339,8 @@ tgoto(const char *cm, int destcol, int destline)
dp = (dp == &destline) ? &destcol : NULL;
break;
case 'r' : {
case 'r':
{
int tmp = destline;
destline = destcol;
@ -356,16 +381,20 @@ tgoto(const char *cm, int destcol, int destline)
return ("OOPS");
numval = *dp;
dp = (dp == &destline) ? &destcol : NULL;
if (numval >= 100) {
if (numval >= 100)
{
*rp++ = '0' + numval / 100;
}
else if (*cm == '3') {
else if (*cm == '3')
{
*rp++ = ' ';
}
if (numval >= 10) {
if (numval >= 10)
{
*rp++ = '0' + ((numval % 100) / 10);
}
else if (*cm == '3' || *cm == '2') {
else if (*cm == '3' || *cm == '2')
{
*rp++ = ' ';
}
*rp++ = '0' + (numval % 10);
@ -374,7 +403,8 @@ tgoto(const char *cm, int destcol, int destline)
return ("OOPS");
}
}
else *rp++ = *cm;
else
*rp++ = *cm;
}
*rp = '\0';
strcpy(rp, added);
@ -389,35 +419,38 @@ static int tens_of_ms_p_char[] = { /* index as returned by gtty */
* tputs - put the string cp out onto the terminal, using the function
* outc. Also handle padding.
*/
int
tputs(register const char *cp, int affcnt, int (*outc)(int))
int tputs(register const char* cp, int affcnt, int (*outc)(int))
{
int delay = 0;
if (cp == NULL)
return (1);
while (ISDIGIT(*cp)) {
while (ISDIGIT(*cp))
{
delay = delay * 10 + (*cp++ - '0');
}
delay *= 10;
if (*cp == '.') {
if (*cp == '.')
{
cp++;
if (ISDIGIT(*cp)) {
if (ISDIGIT(*cp))
{
delay += *cp++ - '0';
}
while (ISDIGIT(*cp)) cp++;
while (ISDIGIT(*cp))
cp++;
}
if (*cp == '*') {
if (*cp == '*')
{
delay *= affcnt;
cp++;
}
while (*cp)
(*outc)(*cp++);
if (delay != 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];
while (delay--) (*outc)(PC);
if (delay != 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];
while (delay--)
(*outc)(PC);
}
return (1);
}