home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Overload
/
ShartewareOverload.cdr
/
progm
/
cpgms.zip
/
DATASCOP.C
< prev
next >
Wrap
C/C++ Source or Header
|
1985-11-17
|
19KB
|
811 lines
#define DOTS 50 /* sector counting dots per line */
#define SPS 9500 /* loops per second */
#define SECSIZ 0x80
#define DATMSK 0x7f
#define BUFSEC 128 /* number of filel sectors to buffer */
#define BUFSIZ 0x7f80 /* large text buffer (32k - 1 sector) */
#define ERRMAX 10 /* max errors before abort */
#define RETMAX 5 /* max retrys before abort */
#define ERROR 0 /* bad open return */
#define ERR -1 /* general char for error */
#define FALSE 0 /* universal evil */
#define TRUE 1 /* universal good */
#define NULL '\0' /* end of string, etc */
#define EOF -1 /* end of file on i/o */
/***********************************************************************/
/* */
/* The following are specific for the IBM PC. */
/* */
/***********************************************************************/
#define REVVID 0x70 /* reverse video attribute */
#define NORM 0x07 /* normal video attribute */
/***********************************************************************/
/* */
/* Special characters used in the file transfer-protocol */
/* */
/***********************************************************************/
#define TIMOUT -1 /* timeout character */
#define SOH 1 /* start of sector character */
#define EOT 4 /* end of tranmission char */
#define ACK 6 /* Acknowledge sector transmission */
#define NAK 21 /* error in transmission detected */
/***********************************************************************/
/* */
/* Miscellaneous ASCII characters */
/* */
/***********************************************************************/
#define ESC 0x1b /* escape code */
#define ENQ 0x05 /* enqueue. when received, send Hereis string */
#define TAB 9
#define LF 10
#define CR 13
#define CTRLZ 26 /* eof char */
/***********************************************************************/
/* */
/* These defines determine which keys will be interpreted */
/* as command characters */
/* */
/***********************************************************************/
#define CTRLA 01
#define CTRLB 02
#define CTRLC 03
#define CTRLE 05
#define CTRLI 08
#define CTRLK 11
#define CTRLL 12
#define CTRLO 15
#define CTRLP 16
#define CTRLQ 17
#define CTRLR 18
#define CTRLS 19
#define CTRLV 22
#define CTRLX 24
#define CTRLY 25
#define CTRLRS 30
/* commands */
#define CMND CTRLC
#define BAUD 'B' /* change the baud rate */
#define CAPTUR 'C' /* toggle text capture */
#define ECHO CTRLE /* local echo of keyboard data */
#define FILEIN 'F' /* input from a file */
#define HEREIS 'H' /* value to return as "hereis" after ENQ */
#define ISCOPE 'I' /* toggle input data scope */
#define KEEP 'K' /* keep text buffer */
#define LITERL CTRLL /* send literal character */
#define OSCOPE 'O' /* toggle output data scope */
#define PRINT CTRLP /* echo to printer */
#define QUIT 'Q' /* quit */
#define STAT 'S' /* status display on screen */
#define TERMNL 'T' /* enter terminal mode */
#define CTRL CTRLRS /* cntl char name escape */
#define SIMCR '!' /* Simulate CR */
#define SIMCTRL '^' /* Simulate control char */
char ViewMd, BFlag, KbData, ModDat, Echofl, Showst, TrmMd;
char AscFlg, ShowTr, ShowRc, View, Prntfl;
char Commok, IScope, OScope, Herflg;
char *Bufr, *Sbufr; /* capture buffer pointers */
char FileNm[15];
char strbuf[20];
char CmdBuf[60];
char Hereisc[21];
char Shstat[13] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',0};
int Fd, PFd, IFd, CRCflg, Batflg;
int _rax, _rdx;
int TxtPtr;
int brate = 1200;
char Infile[15];
main(argc,argv)
int argc;
char *argv[];
{
char ctlnam[4],c,getctl();
int i, j, datain(), getchar();
AscFlg = ShowRc = ShowTr = BFlag = View = Echofl = TrmMd = FALSE;
Prntfl = Commok = OScope = IScope = Herflg = Showst = FALSE;
Fd = PFd = IFd = -1; /* show file not open */
TxtPtr = 0;
ViewMd = KbData = CmdBuf[0] = Infile[0] = Hereisc[0] = NULL;
dismod(); /* set display mode */
if ((Sbufr = Bufr = malloc(BUFSIZ)) == ERROR) {
conout("Unable to acquire buffer\n",NORM);
getchar();
return;
}
CmdBuf[0] = NULL;
/* get the command line parameters, if any */
i = 0; j = 1;
while ((argc > j) && (i < 60)) {
strncpy(&CmdBuf[i], argv[j], 60-i);
i += strlen(argv[j]) + 1;
j++;
CmdBuf[i-1] = ' ';
}
if (argc > 1) {
CmdBuf[i-1] = NULL; /* set end of string */
for (i=0; CmdBuf[i] != '\0'; i++) /* cmd is upper */
CmdBuf[i] = toupper(CmdBuf[i]);
}
evalcd(CmdBuf);
initmd();
/* the main loop */
/* while ((dcdrdy() || !Commok) && (CmdBuf[0] != QUIT)) { */
while (CmdBuf[0] != QUIT) {
if (!Commok && dcdrdy()) /* we have a connection */
Commok = TRUE;
if (KbData = datain()) /* get any char at kbd or file */
switch(KbData) { /* eval term mode cmds */
case CMND:
TrmMd = FALSE;
CmdBuf[0] = NULL;
evalcd(CmdBuf); /* eval cmd mode entry */
break;
case LITERL:
while (!(KbData = datain()));
mcharo(KbData);
break;
case PRINT:
Prntfl = ~Prntfl;
if (Prntfl & (PFd == -1))
PFd = fopen("LST:", "w");
if (Prntfl)
conout("++ Printer ON ++\n",NORM);
else
conout("++ Printer OFF ++\n",NORM);
break;
case ECHO:
Echofl = ~Echofl;
if (Echofl)
conout("++ Echo ON ++\n",NORM);
else
conout("++ Echo OFF ++\n",NORM);
break;
case CTRL:
for (i=0; i<3; i++) {
while (!(KbData = datain())); /* wait input */
ctlnam[i] = toupper(KbData);
}
ctlnam[3] = NULL;
if ((c = getctl(ctlnam)) == ERR)
conout("Invalid Ctrl Name\n",NORM);
else {
mcharo(c); /* output value */
}
break;
default:
if ((IFd != -1) && (KbData == SIMCR))
mcharo(CR);
else
mcharo(KbData);
break;
}
if (minrdy()) {
ModDat = mchari();
if (Showst) { /* display stat in upper right corner */
scr_rowcol(0,66); /* position */
conout(Shstat,REVVID);
scr_rowcol(23,0);
}
if (BFlag && (TxtPtr < BUFSIZ))
*(Bufr+(TxtPtr++)) = ModDat;
else if (BFlag) {
conout("Capture Bufr overflow\n",NORM);
getchar();
}
if (IScope) {
showch(ModDat);
if (ModDat == CR)
conout("\n",NORM);
}
if (Herflg && ModDat == ENQ)
for (i=0; Hereisc[i]; i++) {
if (Hereisc[i] == '!')
mcharo(CR);
else
mcharo(Hereisc[i]);
}
else if (ModDat == CR)
conout("\n",NORM);
else
if ((ModDat > 0x1f) & (ModDat < 0x7f)) {
strbuf[0] = ModDat;
strbuf[1] = NULL;
conout(strbuf,NORM);
}
if (ModDat == CR)
ModDat = '\n';
if (Prntfl)
putc(ModDat, PFd);
}
}
}
evalcd(cbuf)
char *cbuf;
{
char Cmdchr, option[30], *savcbf;
int i;
savcbf = cbuf; /* save start of original buffer */
Cmdchr = NULL;
while ((Cmdchr != QUIT) && (Cmdchr != TERMNL)) {
if (*cbuf == NULL) { /* need new cmd input */
cbuf = savcbf;
cmdmen();
getstr(60,cbuf);
for (i=0; cbuf[i] != '\0'; i++) /* cmd is upper */
cbuf[i] = toupper(cbuf[i]);
}
Cmdchr = *cbuf;
/* skip command in buffer */
cbuf = skptok(cbuf);
switch(Cmdchr) { /* eval cmd mode cmds */
case BAUD:
brate = atoi(cbuf);
if (brate = baudst(brate))
initmd();
else {
conout("Invalid baud rate -- Command terminated.\n",NORM);
getchar();
}
break;
case CAPTUR:
BFlag = ~BFlag;
if (BFlag)
conout("Capture initiated, ",NORM);
else
conout("Capture terminated, ",NORM);
itoa(BUFSIZ - TxtPtr,strbuf);
conout(strbuf,NORM);
conout(" bytes free\n",NORM);
if (IFd != -1)
getchar();
break;
case KEEP:
if (!TxtPtr) {
conout("Nothing to save\n",NORM);
if (IFd != -1)
getchar();
}
else {
if (*cbuf == ' ') { /* user entered filename */
cbuf++;
mkstr(15,cbuf,FileNm);
}
else {
conout("Save as what file? \n",NORM);
getstr(15,FileNm);
}
*(Bufr+(TxtPtr)) = CTRLZ;
Fd = fopen(FileNm, "w");
if (Fd == ERROR) {
conout("Cannot create ",NORM);
conout(FileNm,NORM);
getchar();
}
else {
write(Fd, Bufr,
1+TxtPtr);
fclose(Fd);
BFlag = FALSE;
TxtPtr = 0;
}
}
break;
case FILEIN:
if (*cbuf == ' ') { /* user entered filename */
cbuf++;
mkstr(15,cbuf,Infile);
}
else {
if (IFd != -1) {
fclose(IFd);
IFd = -1;
}
}
IFd = fopen(Infile, "r");
if (IFd == ERROR) {
conout("Cannot open ",NORM);
conout(Infile,NORM);
getchar();
IFd = -1;
}
break;
case QUIT:
hangup();
break;
case ISCOPE:
IScope = ~IScope;
break;
case OSCOPE:
OScope = ~OScope;
break;
case HEREIS:
Herflg = ~Herflg;
if (*cbuf == ' ') { /* user entered string */
cbuf++;
mkstr(21,cbuf,Hereisc);
}
else {
conout("\nEnter the 'Here-is' string ('!' for CR) ",NORM);
getstr(21,Hereisc);
}
break;
case STAT:
Showst = ~Showst;
break;
case TERMNL:
TrmMd = TRUE;
break;
default:
conout("Invalid Command, try again\n",NORM);
getchar();
}
for(;(*cbuf != ';') && (*cbuf != '\n') && (*cbuf != '\0') && (*cbuf != CR)
;cbuf++)
; /* skip this command */
}
clrscr();
return;
}
int datain()
{
int nc, c, kbdin();
unsigned i;
if (IFd != -1) {
if ((c = kbdin()) == CMND) {
conout("\nFile Abort from Console \n",NORM);
fclose(IFd);
IFd = -1;
getchar();
return(c);
}
if ((c = getc(IFd)) == EOF) {
conout("\nEnd of File on ",NORM);
conout(Infile,NORM);
fclose(IFd);
IFd = -1;
return(kbdin());
}
else {
if (TrmMd) {
if (c == SIMCTRL) {
if ((nc = getc(IFd)) == EOF) {
conout("\nPremature End of File on ",NORM);
conout(Infile,NORM);
fclose(IFd);
IFd = -1;
return(kbdin());
}
if (nc != c)
c = 0xbf & nc; /* convert to control character */
}
for (i=0; i <10000; i++) ;
}
return(c);
}
}
else
return(kbdin());
}
int kbdin()
{
char scr_csts();
return((int)scr_csts());
}
skptok(cbuf)
char *cbuf;
{
/* skip token in buffer */
for(;(*cbuf != ' ') && (*cbuf != ';') && (*cbuf != '\0');cbuf++)
;
return (cbuf);
}
cmdmen()
{
char OnOff[4];
char Filenam[16];
int i;
clrscr(); /* clear screen */
conout(" C Modem\n",NORM);
conout("\nCommand Mode Commands\n",NORM);
conout("Baud [rate] (",NORM);
itoa(brate,strbuf);
conout(strbuf,NORM);
conout(") Set baud rate\n",NORM);
if (BFlag)
strcpy(OnOff,"On ");
else
strcpy(OnOff,"Off");
conout("Capture (",NORM);
conout(OnOff,NORM);
conout(") Capture received data to buffer\n",NORM);
if (IFd != -1)
strcpy(Filenam,Infile);
else
strcpy(Filenam,"Kybd ");
conout("From [filename]: (",NORM);
conout(Filenam,NORM);
conout(") Input Source\n",NORM);
if (IScope)
strcpy(OnOff,"On ");
else
strcpy(OnOff,"Off");
conout("Inp Scope (",NORM);
conout(OnOff,NORM);
conout(") Input Datascope Toggle\n",NORM);
conout("Keep [drive/filename] Save captured data\n",NORM);
if (OScope)
strcpy(OnOff,"On ");
else
strcpy(OnOff,"Off");
conout("Out Scope (",NORM);
conout(OnOff,NORM);
conout(") Output Datascope Toggle\n",NORM);
if (Showst)
strcpy(OnOff,"On ");
else
strcpy(OnOff,"Off");
conout("Status Display (",NORM);
conout(OnOff,NORM);
conout(") Line and Modem Status\n",NORM);
conout("Here-is string (",NORM);
conout(Hereisc,NORM);
conout(")",NORM);
for (i=0; i < (21 - strlen(Hereisc)); i++)
conout(" ",NORM);
conout("Output Datascope Toggle\n",NORM);
conout("Quit Exit program\n",NORM);
conout("Terminal Enter terminal mode\n",NORM);
conout("\nTerminal Mode Commands\n",NORM);
conout(" ^C Enter command mode\n",NORM);
if (Echofl)
strcpy(OnOff,"On ");
else
strcpy(OnOff,"Off");
conout(" ^E (",NORM);
conout(OnOff,NORM);
conout(") Echo On/Off\n",NORM);
conout(" ^L Send next char as-is\n",NORM);
if (Prntfl)
strcpy(OnOff,"On ");
else
strcpy(OnOff,"Off");
conout(" ^P (",NORM);
conout(OnOff,NORM);
conout(") Printer On/Off\n",NORM);
conout("\n Command: ",NORM);
return;
}
getstr(maxlen, str)
int maxlen;
char *str;
{
int c;
int datain();
while ((--maxlen) > 0) {
c = datain();
while ((c == NULL) || (c == '\n'))
c = datain();
if ((c > 0x1f) & (c < 0x7f)) {
strbuf[0] = c;
strbuf[1] = NULL;
conout(strbuf,NORM);
*str++ = c;
}
else if (c == '\b') { /* if backspace, then */
*str-- = ' '; /* get rid of unwanted char */
maxlen++; /* and fool loop counter */
putchar(c);
}
else
*str++ = c;
if ((c == '\n') || (c == CR)) {
str--;
break;
}
if (c == SIMCR) /* handle simulated CR */
if (str[-2] == CR) { /* is it a real exclamation point? */
*str-- = ' ';
str[-1] = SIMCR;
}
else
str[-1] = CR;
}
*str = '\0';
}
/*************************************************************************/
/* */
/* The following functions may be hardware dependent. These are */
/* set up for the Hayes Smartmodem 1200 using the IBM serial */
/* card. */
/* */
/*************************************************************************/
/* port definitions */
#define mstat 0x3fe /* modem status port */
#define mcntl 0x3fc /* modem data port */
#define lnesta 0x3fd /* line status port */
#define rxbuf 0x3f8 /* receive data port */
#define txbuf 0x3f8 /* transmit data port */
/* status bit masks */
#define rxrdy 0x01 /* receive char ready */
#define txrdy 0x20 /* transmit buffer empty */
#define mdcd 0x80 /* modem carrier detect */
#define mcts 0x10 /* modem clear to send */
#define errpar 0x04 /* parity error detected */
#define errovr 0x02 /* overrun error detected */
#define errfrm 0x08 /* framing error detected */
#define mdtr 0x01 /* data terminal ready */
#define mrts 0x02 /* request to send */
int baudrt = 04; /* index for 1200 baud (baudtb[4]=1200) */
baudst(baudin)
int baudin;
{
static baudtb[] = {110,150,300,600,1200,
2400,4800,9600};
static int indx;
static char baudst[5];
while (baudin == 0) {
conout("\n\t 110,150,300,600,1200",NORM);
conout("\n\t 2400,4800,9600",NORM);
conout("\nEnter the required baud rate ",NORM);
getstr(5,baudst);
baudin = atoi(baudst);
}
for (indx=0; indx<(sizeof(baudtb)/sizeof(baudtb[1])); indx++)
if (baudin == baudtb[indx]) {
baudrt = indx;
return baudin;
}
return FALSE;
}
dismod()
{
scr_setup();
return;
}
clrscr()
{
scr_clr();
}
conout(s,attr)
char *s;
int attr;
{
scr_aputs(s,attr);
}
initmd()
{
sioint(baudrt);
prglne();
}
prglne()
{
while (minrdy()) /* while there are characters... */
mchari(); /* gobble them */
}
mchari()
{
while (!(lstatus() & rxrdy));
return inp(rxbuf);
}
mcharo(c)
char c;
{
outp(mcntl,(cstatus() | mrts)); /* turn on rts */
while(!(lstatus() & txrdy));
outp(txbuf,c);
outp(mcntl,(cstatus() & (0xff - mrts))); /* turn off rts */
if (Echofl)
kbecho(c);
}
motrdy()
{
return ((lstatus() & txrdy) > 0);
}
lstatus()
{
char lstat;
lstat = inp(lnesta);
if (Showst) {
if (lstat & rxrdy)
Shstat[0] = 'R';
else
Shstat[0] = '.';
if (lstat & txrdy)
Shstat[1] = 'T';
else
Shstat[1] = '.';
if (lstat & errpar)
Shstat[6] = 'P';
else
Shstat[6] = '.';
if (lstat & errovr)
Shstat[7] = 'O';
else
Shstat[7] = '.';
if (lstat & errfrm)
Shstat[8] = 'F';
else
Shstat[8] = '.';
}
return (lstat);
}
mstatus()
{
char msta;
msta = inp(mstat);
if (Showst) {
if (msta & mdcd)
Shstat[3] = 'C';
else
Shstat[3] = '.';
if (msta & mcts)
Shstat[4] = 'C';
else
Shstat[4] = '.';
}
return (msta);
}
cstatus()
{
char mctl;
mctl = inp(mcntl);
if (Showst) {
if (mctl & mdtr)
Shstat[10] = 'D';
else
Shstat[10] = '.';
if (mctl & mrts)
Shstat[11] = 'R';
else
Shstat[11] = '.';
}
return (mctl);
}
minrdy()
{
return ((lstatus() & rxrdy) > 0);
}
dcdrdy()
{
return ((mstatus() & mdcd) > 0);
}
hangup()
{
outp(mcntl,(cstatus() & (0xff - mdtr)));
/* clear data term ready to make
the smartmodem hang up */
}
inp(port)
int port;
{
return(_inb(port));
}
outp(port, val)
int port, val;
{
_outb(val,port);
return;
}
sioint(baud)
int baud;
{
int intparm;
/* set given baud rate, no parity, 1 sto bit, 8 bits/char */
intparm = baud << 5; /* correct pos for interrupt */
intparm = intparm | 2; /* 7 bits/char (7-5=2) */
intparm = intparm | 24; /* even parity */
_rdx = 0; /* select com1 port */
_rax=intparm;
_doint(0x14);
outp(mcntl,(cstatus() | mdtr)); /* turn on dtr */
return;
}