home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
TELECOM
/
stg_v4.lzh
/
trm.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-11-13
|
9KB
|
446 lines
/*
* termcap routines
* Copyright (c) 1990 by Scott Griepentrog
*
* 90/??/?? StG initial version, incompatible calls
* 91/04/08 StG updated for compat w/orig termlib & msdos
* 91/07/18 StG re-installed into new DB9 library
* 91/07/28 StG cleaned up variable names, coding
*
* Functions defined:
* _TrmName(pcBuf,pcName) - internal function to search for terminal name
* _Trm(pcName) - call to load termcap info (ERR=not found)
* char * _TrmString(pcCode) - returns string for given code (0=not found)
* int _TrmNumber(pcCode) - returns numeric value for given code (0=none)
* char * _TrmConvert(pcFix) - returns string converted from contol sequences
* char * _TrmGoXY(iX,iY) - returns string to do cursor motion to position
* _TrmWrite(hPath,pcBuf) - converts and writes string to path
* _TrmWrCode(hPath,pcCode) - sends converted code to path
* _TrmWrXY(hPath,iX,iY) - sends cursor motion to path
*
*/
#define ERR (-1)
#include "db9.h"
#include "trm.h"
#include "stglib.h" /* PrJ - 94/11/13 */
extern int errno;
#define TERMFILE "termcap"
#ifdef OSK
#define TERMDIR1 "/dd/sys/"
#define TERMDIR2 "/h0/sys/"
#define TERMDIR3 "/d0/sys/"
#define OPENOPTS 1
#endif
#ifdef __MSDOS__
#define TERMDIR1 "C:\\SYS\\"
#define OPENOPTS O_RDONLY
#else
char *malloc();
#endif
#ifdef _UNIX
#include <fcntl.h>
#define TERMDIR1 "/etc/"
#define OPENOPTS O_RDONLY
#define readln fastln
#endif
#ifndef OPENOPTS
#define OPENOPTS 1
#endif
#define MAXTRMBUF 256
char _TrmBuf[MAXTRMBUF];
int _TrmLen=0;
struct sTrmCap
{
struct sTrmCap *psNext;
short wCode;
short nNumber;
char *pcString;
};
struct sTrmCap *_psTermRoot=0;
char *_Trm_CM=0;
int _Trm_LI=0;
int _Trm_CO=0;
/* internal function to find terminal name in entry */
int
_TrmName(pcBuf,pcName)
char *pcBuf,*pcName;
{
char *p;
nxtstr:
p=pcName;
while (*pcBuf && *pcBuf!='|' && *pcBuf==*p)
{
pcBuf++;
p++;
}
if (*pcBuf=='|' && !*p) return(0);
if (*pcBuf==':' && !*p) return(0);
while (*pcBuf && *pcBuf!='|') pcBuf++;
if (!*pcBuf)
return(ERR);
pcBuf++;
goto nxtstr;
}
/* locate and load terminal definition */
_Trm(pcName)
char *pcName;
{
int hFile;
int iTemp;
char *pcTemp;
struct sTrmCap *_psTerm;
if (!pcName)
{
errno=DE_NOTERM;
return(ERR);
}
while (_psTermRoot)
{
_psTerm=_psTermRoot;
free(_psTermRoot);
_psTermRoot=_psTerm->psNext;
}
_Trm_CM=0;
_Trm_LI=_Trm_CO=0;
strcpy(_TrmBuf,TERMFILE);
hFile=open(_TrmBuf,OPENOPTS);
if (hFile!=ERR)
goto trmagn;
#ifdef TERMDIR1
strcpy(_TrmBuf,TERMDIR1);
strcat(_TrmBuf,TERMFILE);
hFile=open(_TrmBuf,OPENOPTS);
if (hFile!=ERR)
goto trmagn;
#ifdef TERMDIR2
strcpy(_TrmBuf,TERMDIR2);
strcat(_TrmBuf,TERMFILE);
hFile=open(_TrmBuf,OPENOPTS);
if (hFile!=ERR)
goto trmagn;
#ifdef TERMDIR3
strcpy(_TrmBuf,TERMDIR3);
strcat(_TrmBuf,TERMFILE);
hFile=open(_TrmBuf,OPENOPTS);
if (hFile!=ERR)
goto trmagn;
#endif
#endif
#endif
/*
writeln(2,"ERROR: cant open termcap file: ",31);
#ifdef TERMDIR1
writeln(2,TERMDIR1,strlen(TERMDIR1));
#endif
writeln(2,TERMFILE,strlen(TERMFILE));
writeln(2,"\n",1);
exit(errno);
*/
return(ERR);
trmagn:
while ((iTemp=readln(hFile,_TrmBuf,MAXTRMBUF))>0)
{
*(_TrmBuf+iTemp-1)=0;
if (!*_TrmBuf || *_TrmBuf=='#') continue;
if (!_TrmName(_TrmBuf,pcName)) goto trmfnd;
}
trmerr:
close(hFile);
errno=DE_TERMNF;
return(ERR);
trmred:
iTemp=readln(hFile,_TrmBuf,MAXTRMBUF);
if (iTemp<=0) goto trmerr;
*(_TrmBuf+iTemp-1)=0;
trmfnd:
pcTemp=_TrmBuf;
while (*pcTemp && *pcTemp!=':') pcTemp++;
if (*pcTemp) pcTemp++;
trmnxt:
if (!*pcTemp) goto trmok;
if (*pcTemp=='\\') goto trmred;
iTemp=0;
while (*pcTemp && *pcTemp!=':')
{
iTemp++;
pcTemp++;
}
if (iTemp<2) goto trmerr;
pcTemp-=iTemp;
_psTerm=(struct sTrmCap *)malloc(sizeof(*_psTerm));
if (!_psTerm) goto trmerr;
_psTerm->nNumber=0;
_psTerm->pcString=0;
/* _psTerm->wCode=*(short *)pcTemp; - causes addr trap on move.w (a0),4(a1) ?!? */
_psTerm->wCode=(*pcTemp<<8)|*(pcTemp+1);
pcTemp+=2;
iTemp-=2;
if (!iTemp) _psTerm->nNumber=1;
else if (*pcTemp=='#') _psTerm->nNumber=atoi(pcTemp+1);
else if (*pcTemp!='=') goto trmerr;
else
{
if (_psTerm->wCode==0x7463)
{
*(pcTemp+iTemp)=0;
pcName=malloc(strlen(pcTemp));
strcpy(pcName,pcTemp+1);
free(_psTerm);
lseek(hFile,0L,0);
goto trmagn;
}
_psTerm->pcString=malloc(iTemp);
if (!_psTerm->pcString) goto trmerr;
strncpy(_psTerm->pcString,pcTemp+1,iTemp-1);
*(_psTerm->pcString+iTemp-1)=0;
}
if (_psTerm->wCode==0x636d) _Trm_CM=_psTerm->pcString;
if (_psTerm->wCode==0x6c69) _Trm_LI=_psTerm->nNumber;
if (_psTerm->wCode==0x636f) _Trm_CO=_psTerm->nNumber;
pcTemp+=iTemp;
if (*pcTemp) pcTemp++;
_psTerm->psNext=_psTermRoot;
_psTermRoot=_psTerm;
goto trmnxt;
trmok:
close(hFile);
return(0);
}
/* return pointer to string for code */
char *
_TrmString(pcCode)
char *pcCode;
{
struct sTrmCap *_psTerm;
_psTerm=_psTermRoot;
while (_psTerm)
{
/* if (*(short *)pcCode==_psTerm->wCode) break; arg!! */
if ( (((*pcCode)<<8)|*(pcCode+1))==_psTerm->wCode) break;
_psTerm=_psTerm->psNext;
}
if (_psTerm) return(_psTerm->pcString);
return(0);
}
/* return integer value for code */
int
_TrmNumber(pcCode)
char *pcCode;
{
struct sTrmCap *_psTerm;
_psTerm=_psTermRoot;
while (_psTerm)
{
/* if (*(short *)pcCode==_psTerm->wCode) break; */
if ( (((*pcCode)<<8)|*(pcCode+1))==_psTerm->wCode) break;
_psTerm=_psTerm->psNext;
}
if (_psTerm) return(_psTerm->nNumber);
return(0);
}
/* process coded string to real escape sequences */
char *
_TrmConvert(pcFix)
char *pcFix;
{
char *pcTemp;
if (!pcFix) return(0);
while (*pcFix && *pcFix>='0' && *pcFix<='9') pcFix++;
if (*pcFix=='.') pcFix++;
while (*pcFix && *pcFix>='0' && *pcFix<='9') pcFix++;
if (*pcFix=='*') pcFix++;
pcTemp=_TrmBuf;
while (*pcFix)
{
if (*pcFix=='\\')
{
switch (*++pcFix)
{
case 'E': *pcTemp++=0x1b; break;
case 'n': *pcTemp++=0x0a; break;
case 'r': *pcTemp++=0x0d; break;
case 't': *pcTemp++=0x09; break;
case 'b': *pcTemp++=0x08; break;
case 'f': *pcTemp++=0x0c; break;
case '0':
case '1':
case '2':
case '3':
*pcTemp=*pcFix-'0';
if (*(pcFix+1)>='0' && *(pcFix+1)<='7')
{
(*pcTemp)<<=3;
*pcTemp+=(*++pcFix)-'0';
if (*(pcFix+1)>='0' && *(pcFix+1)<='7')
{
(*(unsigned char *)pcTemp)<<=3;
(*(unsigned char *)pcTemp)+=(*++pcFix)-'0';
}
}
pcTemp++;
break;
default: *pcTemp++=*pcFix;
}
}
else
{
if (*pcFix=='^' && pcFix[1])
*pcTemp++=(*++pcFix)-'@';
else
*pcTemp++=*pcFix;
}
pcFix++;
}
*pcTemp=0;
_TrmLen=pcTemp-_TrmBuf;
return(_TrmBuf);
}
char *
_TrmGoXY(iX,iY)
int iX,iY;
{
char *pcCM,*pcTemp;
int iTemp;
if (iX<0) iX+=(_Trm_CO?_Trm_CO:80);
if (iY<0) iY+=(_Trm_LI?_Trm_LI:24);
pcCM=_Trm_CM;
if (!pcCM)
return(0);
pcTemp=_TrmBuf;
while (*pcCM)
{
if (*pcCM=='%')
{
switch (*++pcCM)
{
case 'i': iX+=1; iY+=1; break;
case '.': *pcTemp++=iY; iTemp=iX; iX=iY; iY=iTemp; break;
case '+': *pcTemp++=iY+(*++pcCM); iTemp=iX; iX=iY; iY=iTemp; break;
case '3':
if (iY<100) *pcTemp++='0';
case '2':
if (iY<10) *pcTemp++='0';
case 'd':
if (iY>99) *pcTemp++='0'+iY/100;
if (iY>9) *pcTemp++='0'+(iY/10)%10;
*pcTemp++='0'+iY%10;
case 'r': iTemp=iX; iX=iY; iY=iTemp; break;
default: *pcTemp++=*pcCM;
}
}
else
if (*pcCM=='\\')
{
switch (*++pcCM)
{
case 'E': *pcTemp++=0x1b; break;
case 'n': *pcTemp++=0x0a; break;
case 'r': *pcTemp++=0x0d; break;
case 't': *pcTemp++=0x09; break;
case 'b': *pcTemp++=0x08; break;
case 'f': *pcTemp++=0x0c; break;
case '0': *pcTemp=0;
case '1': *pcTemp=0x40;
case '2': *pcTemp=0x00;
case '3': *pcTemp=0x40;
*pcTemp|=((*++pcCM)&7)<<3;
*pcTemp|=(*++pcCM)&7;
break;
default: *pcTemp++=*pcCM;
}
}
else
if (*pcCM=='^' && pcCM[1])
*pcTemp++=(*++pcCM)-'@';
else
*pcTemp++=*pcCM;
pcCM++;
}
*pcTemp=0;
_TrmLen=pcTemp-_TrmBuf;
return(_TrmBuf);
}
/* write unconverted string */
_TrmWrite(hPath,pcBuf)
int hPath;
char *pcBuf;
{
_TrmConvert(pcBuf);
write(hPath,_TrmBuf,_TrmLen);
return(0);
}
_TrmWrCode(hPath,pcCode)
int hPath;
char *pcCode;
{
_TrmConvert(_TrmString(pcCode));
write(hPath,_TrmBuf,_TrmLen);
return(0);
}
/* write sequence to goto x,y */
_TrmWrXY(hPath,iX,iY)
int hPath,iX,iY;
{
_TrmConvert(_TrmGoXY(iX,iY));
write(hPath,_TrmBuf,_TrmLen);
return(0);
}