home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
TELECOM
/
tterm_src.lzh
/
sterm.c
< prev
next >
Wrap
Text File
|
1996-03-10
|
18KB
|
739 lines
/*********************************************************************
sterm.c - main module for tterm
*********************************************************************/
#include "common.h"
#include "sterm.h"
#ifdef GWINDOWS
# include <window.h>
# define WFM_PRIORITY 47000
#endif
extern void
*_glob_data; /* _os_intercept needs this (undocumented feature?) */
extern void
trap(int), /* signal trap module */
dskout(int),
opendsk(),
FileTransfer(),
closdsk();
extern int
max_err;
int (*DoCleanup)(),
(*DoProcess)();
char *XYProg = "xyt"; /* XY transfer program */
u_char filterbyte=0x7F; /* filter for high bit */
/*********************************************************************
MAIN
*********************************************************************/
main(argc, argv)
int argc;
char **argv;
{
int option;
u_int32 c;
int lastchar;
int cistate = CIDLE;
char *p;
u_int32 size=0; /* number of characters in screen buffer */
u_int32
snooze,
writehere; /* write out screen buffer here */
signal_code
wakeup;
to_floppy = TRUE; /* keep this so we don;t mess with the B Plus stuff */
if(ParseParms(argc,argv) == -1)
exit(0);
if(mport == 0) /* if no port defined */
{
mport=getenv("MODEM");
if(mport == NULL)
{
fprintf(stderr,"No MODEM port defined!\n");
exit(0);
}
}
_os_intercept(trap,_glob_data); /* set up interrupt handler */
g_tickrate=CLOCKS_PER_SEC; /* system heartbeat */
if(modsetup(mport) != 0) /* open and set up modem path */
{
cleanup();
exit(errno);
}
if(termsetup() != 0) /* configure stdin for terminal action */
{
cleanup();
exit(errno);
}
T_PutC = ToScreen;
CopyRight();
SetDialerFile();
SetPriority();
if(LoadTransfer(XYProg) == 0)
SetLogFile();
DoProcess = NullProcess;
DoCleanup = NullProcess;
t_puts("\012\n\012***** TTERM is Ready *****\012\n\012");
/*
* Loop forever reading the modem port and perfoming functions as needed
*/
while(1)
{
if(ModemReady()) /* if data waiting in the buffer */
{
c=(m_getc() & filterbyte);
switch (c)
{
case ENQ : /* Starting B+ transfer */
lastchar = c; /* save last rcvd char */
bp_ENQ();
break;
case DLE :
if (lastchar == ENQ) /* MUST follow an ENQ */
{
c = bp_DLE(to_floppy);
cistate = CIDLE; /* Return to normal */
}
else
{
ToScreen(lastchar);
ToScreen(c);
}
break;
case 0:
break;
default:
T_PutC(c); /* send to buffer for output */
break;
}
} /* endif modem ready */
else if(TermReady()) /* if there is something there */
{
g_sigkb = 0;
c = inkey(); /* go get it */
option = scankey(c);
if (option) /* if command key */
internal(option); /* check for command */
else
{
m_putc(c); /* send to modem */
if (echoflg) /* if half-duplex echo to screen */
ToScreen(c);
}
} /* endif (TermReady()) */
else
{
if(nextphase == TRUE)
{
nextphase = FALSE;
DoCleanup();
DoCleanup = NullProcess;
DoProcess = NullProcess;
T_PutC = ToScreen;
}
DoProcess();
WaitSerial(1);
}
} /* End of Loop Forever */
} /* End of Main */
/*********************************************************************
NULL functions
*********************************************************************/
NullProcess() { }
/**********************************************************************
SCAN the KEYcodes
returns offset to key pressed
0 if not in keycodes[];
**********************************************************************/
int scankey(kp)
int kp;
{
int cntr;
if(kp==0)
return 0;
for(cntr=0;cntr<MAXCODES;cntr++)
{
if(keycodes[cntr] == kp)
return cntr+1;
}
return 0;
}
/*********************************************************************
INTERNAL commnad handler
*********************************************************************/
internal(c)
int c;
{
extern char *KU,*KD,*KL,*KR;
switch (c)
{
case CAPTURE :
if (capflg)
closdsk();
else
opendsk();
break;
case DIAL:
Dialer();
break;
case ECHO :
toggleduplex();
break;
case SHELL:
shell();
break;
case TRANSFER:
FileTransfer(XYProg,mport);
break;
case TRANSMIT :
transmit();
break;
case DIR :
cwd();
break;
case HELP :
tprint(CL);
help();
break;
case FILTER:
if(filterbyte == 0xFF)
{
filterbyte=0x7F;
tprint("High bit filter on!\012\n");
}
else
{
filterbyte=0xFF;
tprint("High bit filter off!\012\n");
}
break;
case TRANSLST:
SetFileList();
break;
case TRANSFIL:
SetTransferFile();
break;
case QUIT:
if (getans("\012\nQUIT - are you sure? ") == TRUE)
{
t_puts(CL);
cleanup();
exit(0);
}
break;
case UPARR:
m_puts(KU);
break;
case DNARR:
m_puts(KD);
break;
case LFARR:
m_puts(KL);
break;
case RTARR:
m_puts(KR);
break;
default:
m_putc(c); /* and then send the next char */
}
}
/*********************************************************************
HELP message
*********************************************************************/
help()
{
static char *hlpmsg[] = {
F1_M,F7_M,
F2_M,F8_M,
F3_M,F9_M,
F4_M,F0_M,
F5_M,FA_M,
F6_M,FB_M,
BLNK,BLNK,
0 };
int
cntr;
char
*margin = " ", /* nine spaces */
buff[80],
**hscan=hlpmsg;
buff[0]=glyph[5];
for(cntr=1; cntr < LASTGLYPH; cntr++)
buff[cntr]=glyph[6];
buff[LASTGLYPH]=glyph[2];
buff[LASTGLYPH + 1]=0;
tprint(margin);tprint(buff); tprint("\012\n");
while(**hscan)
{
sprintf(buff,"%c%s%s%c\012\n",glyph[1],*hscan,*(hscan+1),glyph[1]);
tprint(margin);tprint(buff);
hscan += 2;
}
sprintf(buff,"%c Connected to port %-5s Baud Rate: %-14s %c\012\n",
glyph[1],mport,GetBaud(),glyph[1]);
tprint(margin);tprint(buff);
buff[0]=glyph[4];
for(cntr=1; cntr < LASTGLYPH; cntr++)
buff[cntr]=glyph[6];
buff[LASTGLYPH]=glyph[3];
buff[LASTGLYPH + 1]=0;
tprint(margin);tprint(buff); tprint("\012\n");
}
/*********************************************************************
display the COPYRIGHT message
*********************************************************************/
CopyRight()
{
static char *copyrite[]={
"Tterm - a simple terminal program for OS9 and OS9000\012\n",
"Modified from Mark Griffith's Original Source Code\012\n",
"by Ed Gresick and Stephen Carville\012\n"
"Version ",_VERSION," by Stephen Carville\012\n",
"High G Software P.O. Box 822 Glendora, CA 91740\012\n",
"\012\n",
0};
char **s;
s=copyrite;
tprint(CL);
while(**s != 0)
tprint(*s++);
help();
}
/*********************************************************************
program USAGE
*********************************************************************/
usage()
{
fprintf(stderr, "\nTterm Version %s\n\n",_VERSION);
fprintf(stderr, "Usage: tterm <options>\n");
fprintf(stderr, " -? Print this usage message\n");
fprintf(stderr, " -l[=]p Link to modem port 'p'\n");
fprintf(stderr, " defaults to MODEM\n");
}
/*********************************************************************
CLOSE the capture FILE
*********************************************************************/
void close_file()
{
if (cp_pn != 0x00) /* bloop */
{
dskout(-1);
fclose(cp_pn); /* and close it */
}
}
/*********************************************************************
DiSK OUTput - pass c as -1 to force a flush of buffer
*********************************************************************/
void dskout(c)
int c;
{
char khar;
int stopped=0;
static int inbuff=0;
static char *dbip=diskbuff;
if(c == -1) /* if a forced flush */
{
fwrite(diskbuff,1,inbuff,cp_pn);
fflush(cp_pn); /* And write out the buffer */
dbip=diskbuff; /* reset */
inbuff=0;
return;
}
khar=c;
if(khar >= 0x20)
*dbip++=khar;
else if(khar == BACKSP || khar == CR || khar == TAB)
*dbip++=khar;
else
return;
inbuff++;
}
/*********************************************************************
CLOSe the capture to DiSK - asks if file to be closed
*********************************************************************/
void closdsk()
{
if (cp_pn == 0x00) /* bloop */
{
tprint(" FILE NOT OPENED\012\n");
capflg = FALSE;
}
else
{
dskout(-1);
capflg = FALSE;
if (getans("Close File? ") == TRUE)
{
fclose(cp_pn);
cp_pn = 0;
tprint("\012\nCapture Off - File Closed\012\n");
}
else
tprint("\012\nCapture Off - File Remains Open\012\n");
}
}
/*********************************************************************
OPEN capture to DiSK
*********************************************************************/
void opendsk()
{
int ansok;
char cz;
char *mode="w"; /* fiel access mode */
char buff[80];
path_id path;
if(cp_pn != 0) /* file already opened */
{
t_puts("\012\nCapture ON - File already opened\012\n");
capflg = TRUE;
return;
}
else
{
t_puts("Filename: ");
t_gets(filename,sizeof(filename));
if(filename[0] == 0)
{
t_puts("No filename given\012\n");
return;
}
}
if(_os_open(filename,0,&path) == 0) /* existing file? */
{
_os_close(path);
t_puts("\012\nFILE EXISTS - [O]verwrite, [A]ppend, or [RETURN]: ");
ansok = FALSE;
while(ansok == FALSE)
{
cz = GetOneChar();
switch (tolower(cz))
{
case 0x0d:
t_puts("\012\nABORTED\012\n");
return;
case 'o':
t_puts("\012\nOverwriting...\012\n");
ansok = TRUE;
break;
case 'a':
t_puts("\012\nAppending ...\012\n");
mode="a";
ansok = TRUE;
}
}
}
cp_pn=fopen(filename,mode);
if(cp_pn == 0)
{
sprintf(buff,"Can't open '%s' - error %d %c!\012\n",filename, errno, 7);
tprint(buff);
cp_pn = 0;
capflg = FALSE;
}
else
{
tprint("\nCapture ON - File Opened\012\n");
capflg = TRUE;
setvbuf(cp_pn,0,_IOFBF,VBUFFSIZ);
}
}
/*********************************************************************
TRANSMIT a file
*********************************************************************/
void transmit()
{
extern int TransmitLine(void),TransmitCleanup(void);
char buff[80];
t_puts("\012\nTransmit Filename: ");
t_gets(filename,sizeof(filename));
if(filename[0] == '\0')
{
t_puts("ABORTED - No Filename given\012\n");
return;
}
xmf=fopen(filename, "r");
if(xmf == 0)
{
sprintf(buff,"CAN'T OPEN %s %c\012\n",filename,BELL);
t_puts(buff);
return;
}
DoProcess = TransmitLine;
DoCleanup = TransmitCleanup;
t_puts(CL);
}
/*********************************************************************
TRANSMIT a LINE
*********************************************************************/
TransmitLine()
{
if(fgets(sbuf, MAXLIN, xmf) == NULL) /* Get line to xmit */
nextphase = TRUE;
else
m_puts(sbuf); /* and send it out */
}
/*********************************************************************
TRANSMIT CLEANUP function
*********************************************************************/
TransmitCleanup()
{
fclose(xmf);
t_puts("\012\nTransmit File Closed\012\n");
}
/*********************************************************************
GET a single character ANSwer from the keyboard
this had better not be called during any kind of a transfer!
*********************************************************************/
getans(s)
char *s; /* prompt */
{
char c;
t_puts(s);
c = (char)GetOneChar();
ToScreen(c);
t_puts("\012\n");
return (c == 'y');
}
/*********************************************************************
GET ONE CHARacter
*********************************************************************/
GetOneChar()
{
u_int32 snooze;
signal_code wakeup;
while (g_sigkb == 0)
{
snooze=10;
TSleep(snooze,wakeup);
}
return inkey();
}
/*********************************************************************
GET FILE name
*********************************************************************/
int getfile()
{
t_gets(filename,sizeof(filename));
return (filename[0] != '\0');
}
/*********************************************************************
Change the Working Directory
*********************************************************************/
cwd()
{
t_puts("\012\nNew Directory ? ");
t_gets(filename,sizeof(filename));
if (filename[0] == '\0')
t_puts("ABORTED\012\n");
else
{
if(_os_chdir(filename,FAM_READ | FAM_WRITE) != 0)
t_puts("Can't CHD\012\n");
else
t_puts("OK!\012\n");
}
}
/*********************************************************************
execute a SHELL command
*********************************************************************/
shell()
{
t_puts("OS9: ");
t_gets(sbuf,sizeof(sbuf));
cook_io();
system(sbuf);
raw_io();
t_puts("\012\n**** TTERM back online ****\012\n");
FlushSerial();
}
/*********************************************************************
TOGGLE echo mode (DUPLEX)
*********************************************************************/
toggleduplex()
{
if (echoflg)
{
t_puts("\012\nFull duplex\012\n");
echoflg = FALSE;
}
else
{
t_puts("\012\nHalf duplex\012\n");
echoflg = TRUE;
}
}
/*********************************************************************
ALARM RESET - resets an existing alarm returns new alarm_id
*********************************************************************/
AlarmReset(alarm_id alrm_id,signal_code signal,u_int32 time)
{
_os_alarm_delete(alrm_id);
_os_alarm_set(&alrm_id,signal,time);
return alrm_id;
}
/*********************************************************************
get the SYStem DEVice from init module - this is identical in function
to the G-Windows function sysdev() but since some poeple don't
have wmlib.l we'll just write one
*********************************************************************/
char *SysDev()
{
char *rtn,*modname = "init";
u_int16
type_lang = 0,
attr_rev = 0;
mod_config
*mod; /* it is a config module */
mh_com *mod_head; /* points to start of module */
void *mod_exec;
if(_os_link(&modname,&mod_head,&mod_exec,&type_lang,&attr_rev) != 0)
rtn = "/dd";
else
{
mod = (mod_config*)mod_head;
rtn = (char*)mod_exec + mod->_msysdrive;
}
_os_unlink(mod_head);
return rtn;
}
/*********************************************************************
SET the program PRIORITY - to make this work under G-windows, we have
to push the priority to match wfm
*********************************************************************/
SetPriority()
{
#ifdef GWINDOWS
process_id pid;
u_int16
oldpriority,
age,
group,
user;
int32 schedule;
CRT_DEFINITION crtdef;
char devnam[32];
/* first verify we are in the Gwindows environment */
if(Window_Get(2,0,
W_CRT_Definition,crtdef,sizeof(CRT_DEFINITION),
0) != -1)
{
# ifdef _MPF386
_os_id(&pid,&oldpriority,&age,&schedule,&group,&user);
# endif
# ifdef _MPF68K
_os9_id(&pid,&oldpriority,&group,&user);
# endif
_os_setpr(pid,WFM_PRIORITY);
}
#endif
}
/*********************************************************************
PARSE the PARaMeterS - returns -1 on error
*********************************************************************/
ParseParms(argc,argv)
int argc;
char *argv[];
{
char *p;
while(--argc > 0)
{
p=argv[argc];
if (*p == '-' ) /* if a parameter */
{
p++;
while(*p) /* until end of string */
{
switch(*p)
{
case 'l': /* force terminal port */
case 'L':
p++;
if(*p == '=') /* if an equal sign */
p++;
mport=p;
p+=strlen(p)-1; /* point to NULL */
break;
case '?':
case 'h':
case 'H':
usage();
return -1;
default:
fprintf(stderr,"Unknown Parameter: %c\n",*p);
return -1;
}
p++;
}
}
else
{
fprintf(stderr,"Unknown Parameter: %c\n",*p);
return -1;
}
}
return 0;
}