ack/lang/cem/libcc.ansi/core/misc/termcap.c

463 lines
8.6 KiB
C
Raw Normal View History

1989-12-18 14:40:54 +00:00
/*
* termcap.c 1.1 20/7/87 agc Joypace Ltd
*
* Copyright Joypace Ltd, London, UK, 1987. All rights reserved.
* This file may be freely distributed provided that this notice
* remains attached.
*
* A public domain implementation of the termcap(3) routines.
*
* Made fully functional by Ceriel J.H. Jacobs.
*
* BUGS:
* - does not check termcap entry sizes
* - not fully tested
*/
1994-06-24 14:02:31 +00:00
/* $Id$ */
1989-12-18 14:40:54 +00:00
2018-06-02 18:57:43 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
1989-12-18 14:40:54 +00:00
2018-06-02 18:57:43 +00:00
#define CAPABLEN 2
1989-12-18 14:40:54 +00:00
2018-06-02 18:57:43 +00:00
#define ISSPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\r' || (c) == '\n')
#define ISDIGIT(x) ((x) >= '0' && (x) <= '9')
1989-12-18 14:40:54 +00:00
2018-06-02 18:57:43 +00:00
short ospeed; /* output speed */
char PC; /* padding character */
char* BC; /* back cursor movement */
char* UP; /* up cursor movement */
1989-12-18 14:40:54 +00:00
2018-06-02 18:57:43 +00:00
static const char* capab; /* the capability itself */
static int check_for_tc(void);
static int match_name(const char* buf, const char* name);
1989-12-18 14:40:54 +00:00
/*
* tgetent - get the termcap entry for terminal name, and put it
* 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.
*/
2018-06-02 18:57:43 +00:00
int tgetent(char* bp, const char* name)
1989-12-18 14:40:54 +00:00
{
2018-06-02 18:57:43 +00:00
FILE* fp;
char* file;
char* cp;
char buf[1024];
1989-12-18 14:40:54 +00:00
capab = bp;
2018-06-02 18:57:43 +00:00
if ((file = getenv("TERMCAP")) != (char*)NULL)
{
if (*file != '/' && (cp = getenv("TERM")) != NULL && strcmp(name, cp) == 0)
{
(void)strcpy(bp, file);
return (1);
1989-12-18 14:40:54 +00:00
}
2018-06-02 18:57:43 +00:00
else
file = "/etc/termcap";
}
else
1989-12-18 14:40:54 +00:00
file = "/etc/termcap";
2018-06-02 18:59:11 +00:00
2018-06-02 18:57:43 +00:00
if ((fp = fopen(file, "r")) == (FILE*)NULL)
return (-1);
while (fgets(buf, 1024, fp) != NULL)
{
if (buf[0] == '#')
continue;
1989-12-18 14:40:54 +00:00
while (*(cp = &buf[strlen(buf) - 2]) == '\\')
if (fgets(cp, 1024, fp) == NULL)
2018-06-02 18:59:11 +00:00
goto exit;
2018-06-02 18:57:43 +00:00
if (match_name(buf, name))
{
1989-12-18 14:40:54 +00:00
strcpy(bp, buf);
fclose(fp);
2018-06-02 18:57:43 +00:00
return (check_for_tc());
1989-12-18 14:40:54 +00:00
}
}
2018-06-02 18:59:11 +00:00
exit:
1989-12-18 14:40:54 +00:00
fclose(fp);
2018-06-02 18:57:43 +00:00
return (0);
1989-12-18 14:40:54 +00:00
}
/*
* Compare the terminal name with each termcap entry name; Return 1 if a
* match is found.
*/
static int
2018-06-02 18:57:43 +00:00
match_name(const char* buf, const char* name)
1989-12-18 14:40:54 +00:00
{
2018-06-02 18:57:43 +00:00
register const char* tp = buf;
register const char* np;
1989-12-18 14:40:54 +00:00
2018-06-02 18:57:43 +00:00
for (;;)
{
for (np = name; *np && *tp == *np; np++, tp++)
{
}
1989-12-18 14:40:54 +00:00
if (*np == 0 && (*tp == '|' || *tp == ':' || *tp == 0))
2018-06-02 18:57:43 +00:00
return (1);
while (*tp != 0 && *tp != '|' && *tp != ':')
tp++;
if (*tp++ != '|')
return (0);
1989-12-18 14:40:54 +00:00
}
}
/*
* Handle tc= definitions recursively.
*/
static int
check_for_tc(void)
{
2018-06-02 18:57:43 +00:00
static int count = 0;
const char* savcapab = capab;
char buf[1024];
char terminalname[128];
register char *p = (char*)capab + strlen(capab) - 2, *q;
1989-12-18 14:40:54 +00:00
while (*p != ':')
2018-06-02 18:57:43 +00:00
if (--p < (char*)capab)
return (0); /* no : in termcap entry */
1989-12-18 14:40:54 +00:00
if (p[1] != 't' || p[2] != 'c')
2018-06-02 18:57:43 +00:00
return (1);
if (count > 16)
return (0); /* recursion in tc= definitions */
1989-12-18 14:40:54 +00:00
count++;
strcpy(terminalname, &p[4]);
q = terminalname;
2018-06-02 18:57:43 +00:00
while (*q && *q != ':')
q++;
1989-12-18 14:40:54 +00:00
*q = 0;
2018-06-02 18:57:43 +00:00
if (tgetent(buf, terminalname) != 1)
{
1989-12-18 14:40:54 +00:00
--count;
2018-06-02 18:57:43 +00:00
return (0);
1989-12-18 14:40:54 +00:00
}
--count;
2018-06-02 18:57:43 +00:00
for (q = buf; *q && *q != ':'; q++)
{
}
1989-12-18 14:40:54 +00:00
strcpy(p, q);
capab = savcapab;
2018-06-02 18:57:43 +00:00
return (1);
1989-12-18 14:40:54 +00:00
}
/*
* tgetnum - get the numeric terminal capability corresponding
* to id. Returns the value, -1 if invalid.
*/
2018-06-02 18:57:43 +00:00
int tgetnum(const char* id)
1989-12-18 14:40:54 +00:00
{
2018-06-02 18:57:43 +00:00
const char* cp;
int ret;
1989-12-18 14:40:54 +00:00
if ((cp = capab) == NULL || id == NULL)
2018-06-02 18:57:43 +00:00
return (-1);
1989-12-18 14:40:54 +00:00
while (*++cp != ':')
;
2018-06-02 18:57:43 +00:00
while (*cp)
{
1989-12-18 14:40:54 +00:00
cp++;
while (ISSPACE(*cp))
cp++;
2018-06-02 18:57:43 +00:00
if (strncmp(cp, id, CAPABLEN) == 0)
{
1989-12-18 14:40:54 +00:00
while (*cp && *cp != ':' && *cp != '#')
cp++;
if (*cp != '#')
2018-06-02 18:57:43 +00:00
return (-1);
for (ret = 0, cp++; *cp && ISDIGIT(*cp); cp++)
1989-12-18 14:40:54 +00:00
ret = ret * 10 + *cp - '0';
2018-06-02 18:57:43 +00:00
return (ret);
1989-12-18 14:40:54 +00:00
}
while (*cp && *cp != ':')
cp++;
}
2018-06-02 18:57:43 +00:00
return (-1);
1989-12-18 14:40:54 +00:00
}
/*
* tgetflag - get the boolean flag corresponding to id. Returns -1
* if invalid, 0 if the flag is not in termcap entry, or 1 if it is
* present.
*/
2018-06-02 18:57:43 +00:00
int tgetflag(const char* id)
1989-12-18 14:40:54 +00:00
{
2018-06-02 18:57:43 +00:00
const char* cp;
1989-12-18 14:40:54 +00:00
if ((cp = capab) == NULL || id == NULL)
2018-06-02 18:57:43 +00:00
return (-1);
1989-12-18 14:40:54 +00:00
while (*++cp != ':')
;
2018-06-02 18:57:43 +00:00
while (*cp)
{
1989-12-18 14:40:54 +00:00
cp++;
while (ISSPACE(*cp))
cp++;
if (strncmp(cp, id, CAPABLEN) == 0)
2018-06-02 18:57:43 +00:00
return (1);
1989-12-18 14:40:54 +00:00
while (*cp && *cp != ':')
cp++;
}
2018-06-02 18:57:43 +00:00
return (0);
1989-12-18 14:40:54 +00:00
}
/*
* tgetstr - get the string capability corresponding to id and place
* it in area (advancing area at same time). Expand escape sequences
* etc. Returns the string, or NULL if it can't do it.
*/
2018-06-02 18:57:43 +00:00
char* tgetstr(const char* id, char** const area)
1989-12-18 14:40:54 +00:00
{
2018-06-02 18:57:43 +00:00
const char* cp;
char* ret;
int i;
1989-12-18 14:40:54 +00:00
if ((cp = capab) == NULL || id == NULL)
2018-06-02 18:57:43 +00:00
return (NULL);
1989-12-18 14:40:54 +00:00
while (*++cp != ':')
;
2018-06-02 18:57:43 +00:00
while (*cp)
{
1989-12-18 14:40:54 +00:00
cp++;
while (ISSPACE(*cp))
cp++;
2018-06-02 18:57:43 +00:00
if (strncmp(cp, id, CAPABLEN) == 0)
{
1989-12-18 14:40:54 +00:00
while (*cp && *cp != ':' && *cp != '=')
cp++;
if (*cp != '=')
2018-06-02 18:57:43 +00:00
return (NULL);
for (ret = *area, cp++; *cp && *cp != ':'; (*area)++, cp++)
switch (*cp)
{
case '^':
**area = *++cp - 'A' + 1;
1989-12-18 14:40:54 +00:00
break;
2018-06-02 18:57:43 +00:00
case '\\':
switch (*++cp)
{
case 'E':
**area = '\033';
break;
case 'n':
**area = '\n';
break;
case 'r':
**area = '\r';
break;
case 't':
**area = '\t';
break;
case 'b':
**area = '\b';
break;
case 'f':
**area = '\f';
break;
case '0':
case '1':
case '2':
case '3':
for (i = 0; *cp && ISDIGIT(*cp); cp++)
i = i * 8 + *cp - '0';
**area = i;
cp--;
break;
case '^':
case '\\':
**area = *cp;
break;
}
1989-12-18 14:40:54 +00:00
break;
2018-06-02 18:57:43 +00:00
default:
1989-12-18 14:40:54 +00:00
**area = *cp;
}
*(*area)++ = '\0';
2018-06-02 18:57:43 +00:00
return (ret);
1989-12-18 14:40:54 +00:00
}
while (*cp && *cp != ':')
cp++;
}
2018-06-02 18:57:43 +00:00
return (NULL);
1989-12-18 14:40:54 +00:00
}
/*
* tgoto - given the cursor motion string cm, make up 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.
*/
2018-06-02 18:57:43 +00:00
char* tgoto(const char* cm, int destcol, int destline)
1989-12-18 14:40:54 +00:00
{
2018-06-02 18:57:43 +00:00
register char* rp;
static char ret[24];
char added[16];
int* dp = &destline;
int numval;
int swapped = 0;
1989-12-18 14:40:54 +00:00
added[0] = 0;
2018-06-02 18:57:43 +00:00
for (rp = ret; *cm; cm++)
{
if (*cm == '%')
{
switch (*++cm)
{
case '>':
if (dp == NULL)
return ("OOPS");
cm++;
if (*dp > *cm++)
{
*dp += *cm;
1989-12-18 14:40:54 +00:00
}
2018-06-02 18:57:43 +00:00
break;
case '+':
case '.':
if (dp == NULL)
return ("OOPS");
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);
(*dp)++;
continue;
}
}
break;
}
*rp++ = *dp;
dp = (dp == &destline) ? &destcol : NULL;
break;
1989-12-18 14:40:54 +00:00
2018-06-02 18:57:43 +00:00
case 'r':
{
int tmp = destline;
1989-12-18 14:40:54 +00:00
2018-06-02 18:57:43 +00:00
destline = destcol;
destcol = tmp;
swapped = 1 - swapped;
break;
}
case 'n':
destcol ^= 0140;
destline ^= 0140;
break;
1989-12-18 14:40:54 +00:00
2018-06-02 18:57:43 +00:00
case '%':
*rp++ = '%';
break;
1989-12-18 14:40:54 +00:00
2018-06-02 18:57:43 +00:00
case 'i':
destcol++;
destline++;
break;
1989-12-18 14:40:54 +00:00
2018-06-02 18:57:43 +00:00
case 'B':
if (dp == NULL)
return ("OOPS");
*dp = 16 * (*dp / 10) + *dp % 10;
break;
1989-12-18 14:40:54 +00:00
2018-06-02 18:57:43 +00:00
case 'D':
if (dp == NULL)
return ("OOPS");
*dp = *dp - 2 * (*dp % 16);
break;
1989-12-18 14:40:54 +00:00
2018-06-02 18:57:43 +00:00
case 'd':
case '2':
case '3':
if (dp == NULL)
return ("OOPS");
numval = *dp;
dp = (dp == &destline) ? &destcol : NULL;
if (numval >= 100)
{
*rp++ = '0' + numval / 100;
}
else if (*cm == '3')
{
*rp++ = ' ';
}
if (numval >= 10)
{
*rp++ = '0' + ((numval % 100) / 10);
}
else if (*cm == '3' || *cm == '2')
{
*rp++ = ' ';
}
*rp++ = '0' + (numval % 10);
break;
default:
return ("OOPS");
1989-12-18 14:40:54 +00:00
}
}
2018-06-02 18:57:43 +00:00
else
*rp++ = *cm;
1989-12-18 14:40:54 +00:00
}
*rp = '\0';
strcpy(rp, added);
2018-06-02 18:57:43 +00:00
return (ret);
1989-12-18 14:40:54 +00:00
}
2018-06-02 18:57:43 +00:00
static int tens_of_ms_p_char[] = { /* index as returned by gtty */
/* assume 10 bits per char */
1989-12-18 14:40:54 +00:00
0, 2000, 1333, 909, 743, 666, 500, 333, 166, 83, 55, 41, 20, 10, 5, 2
};
/*
* tputs - put the string cp out onto the terminal, using the function
* outc. Also handle padding.
*/
2018-06-02 18:57:43 +00:00
int tputs(register const char* cp, int affcnt, int (*outc)(int))
1989-12-18 14:40:54 +00:00
{
int delay = 0;
if (cp == NULL)
2018-06-02 18:57:43 +00:00
return (1);
while (ISDIGIT(*cp))
{
1989-12-18 14:40:54 +00:00
delay = delay * 10 + (*cp++ - '0');
}
delay *= 10;
2018-06-02 18:57:43 +00:00
if (*cp == '.')
{
1989-12-18 14:40:54 +00:00
cp++;
2018-06-02 18:57:43 +00:00
if (ISDIGIT(*cp))
{
1989-12-18 14:40:54 +00:00
delay += *cp++ - '0';
}
2018-06-02 18:57:43 +00:00
while (ISDIGIT(*cp))
cp++;
1989-12-18 14:40:54 +00:00
}
2018-06-02 18:57:43 +00:00
if (*cp == '*')
{
1989-12-18 14:40:54 +00:00
delay *= affcnt;
cp++;
}
while (*cp)
(*outc)(*cp++);
2018-06-02 18:57:43 +00:00
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);
1989-12-18 14:40:54 +00:00
}
2018-06-02 18:57:43 +00:00
return (1);
1989-12-18 14:40:54 +00:00
}
/*
* That's all, folks...
*/