home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
TELECOM
/
OSKBox.lzh
/
MAILBOX
/
CC
/
hostube.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-03-17
|
7KB
|
258 lines
#include <stdio.h>
#include <sgstat.h>
#include <signal.h>
#include <errno.h>
#define ERROR -1
#define ESC 27
#define CMD '\\'
int tnc_in, tnc_out, ctl_in, ctl_out, hostcmd;
int pid;
struct sgbuf term_opt, oldt_opt;
#define RXSIZE 2048
char rxbuff[RXSIZE], *rxptr = rxbuff;
int rxfile;
main (argc, argv)
char *argv[];
{
int i;
char str[40];
if (argc < 2) {
printf ("No port supplied.\n");
exit (0);
}
strcpy (str, "/pipe/command.");
strcat (str, argv[1]);
if ((hostcmd = open (str, 2)) < 0) {
printf ("Can't find command channel\n");
exit (0);
}
pid = getpid ();
sprintf (str, "o%d\n", pid);
write (hostcmd, str, strlen (str));
sprintf (str, "/pipe/data_in.%d", pid);
for (i = 0; i < 10; i++) {
if ((tnc_in = open (str, 3)) != -1) break;
sleep (1);
}
if (tnc_in == -1) {
printf ("No response from host\n");
exit (0);
}
sprintf (str, "/pipe/data_out.%d", pid);
tnc_out = open (str, 3);
sprintf (str, "/pipe/ctl_in.%d", pid);
ctl_in = open (str, 3);
sprintf (str, "/pipe/ctl_out.%d", pid);
ctl_out = open (str, 3);
printf ("Connection established...\n\n");
_gs_opt (0, &oldt_opt);
movmem (&oldt_opt, &term_opt, sizeof (term_opt));
term_opt.sg_kbich = 0;
term_opt.sg_kbach = 0;
term_opt.sg_eofch = 0;
term_opt.sg_pause = 0;
_ss_opt (0, &term_opt);
if (tube ()) exit (errno);
}
tube ()
{
int tx = 0;
int rx = 0;
int txfile;
char fname[80], buffer[256], *p;
int len;
char out[256], *p2;
int i, j;
errno = 0;
while (1) {
if (tx)
if (read (txfile, buffer, 1) == 0) {
close (txfile);
tx = 0;
printf ("\nTransmission completed\n");
} else {
write (tnc_out, buffer, 1);
writeln (1, buffer, 1);
}
if ((len = _gs_rdy (0)) != ERROR) {
len = readln (0, buffer, 256);
if (*buffer == CMD) {
switch (*(buffer+1)) {
case 't':
case 'T':
if (tx) {
close (txfile);
tx = 0;
printf ("Transmission aborted\n");
} else {
printf ("File to transmit? ");
if (gets (fname, sizeof fname) != 0)
if ((txfile = open (fname, 1)) > 0)
tx = 1;
else
printf ("Error opening file\n");
}
break;
case 'r':
case 'R':
if (rx) {
if (rxptr-rxbuff) {
write (rxfile, rxbuff, rxptr-rxbuff);
rxptr = rxbuff;
}
write (rxfile, "\n", 1);
close (rxfile);
rx = 0;
printf ("Receiving terminated\n");
} else {
printf ("File to receive? ");
if (gets (fname, sizeof fname) != 0) {
if ((rxfile = creat (fname, 3)) < 0)
printf ("Error creating file\n");
else {
rx = 1;
rxptr = rxbuff;
}
}
}
break;
case 's':
case 'S':
_ss_opt (0, &oldt_opt);
system ("");
_ss_opt (0, &term_opt);
break;
case 'c':
case 'C':
printf ("\nCHD to? ");
if (gets (fname, sizeof fname) != 0) {
if (chdir (fname) == -1)
printf ("error during CHD\n");
}
break;
case 'q':
case 'Q':
goto quit;
}
} else if (*buffer == ESC)
write (ctl_out, buffer+1, len-1);
else if (write (tnc_out, buffer, len) == ERROR) {
goto quit;
}
} else
if (errno == 246)
errno = 0;
else {
goto quit;
}
if (_gs_rdy (ctl_in) != -1) {
len = readln (ctl_in, buffer, 256);
writeln (1, buffer, len);
}
if ((len = _gs_rdy (tnc_in)) != -1) {
if ((len = read (tnc_in, buffer, min (len, 256))) == ERROR)
if (errno == 244) {
errno = 0;
write (1, "\007", 1);
} else {
goto quit;
}
for (p = buffer, p2 = out; p-buffer < len; p++, p2++)
*p2 = (*p == '\t') ? oldt_opt.sg_tabcr : *p;
for (p = out; p - out < len; )
p += writeln (1, p, len - (p - out));
if (rx)
for (p = buffer; p-buffer < len; p++) {
*p &= 0x7f;
writerx (*p);
}
}
if (!tx && _gs_rdy (0) == ERROR &&
_gs_rdy (tnc_in) == ERROR &&
_gs_rdy (ctl_in) == ERROR) {
_ss_ssig (0, SIGWAKE);
/* _ss_ssig (tnc_in, SIGWAKE);
_ss_ssig (ctl_in, SIGWAKE); */
tsleep (6);
_ss_rel (0);
/* _ss_rel (tnc_in);
_ss_rel (ctl_in); */
errno = 0;
}
}
quit:_ss_opt (0, &oldt_opt);
sprintf (buffer, "c%d\n", pid);
write (hostcmd, buffer, strlen (buffer));
close (hostcmd);
close (tnc_in);
close (tnc_out);
close (ctl_in);
close (ctl_out);
j = errno;
sprintf (fname, "/pipe/data_in.%d", pid);
for (i = 0; i < 10; i++) {
if (access (fname, 0) == -1) break;
sleep (1);
}
if (i == 10)
printf ("Host will not close.\n");
errno = j;
if (rx) {
if (rxptr-rxbuff) {
write (rxfile, rxbuff, rxptr-rxbuff);
rxptr = rxbuff;
}
write (rxfile, "\n", 1);
close (rxfile);
}
return (errno);
}
writerx (c)
{
if (c == '\l')
return;
else if (c < ' ' && c != '\t' && c != '\n')
return;
*rxptr++ = c;
if (rxptr-rxbuff == RXSIZE) {
write (rxfile, rxbuff, RXSIZE);
rxptr = rxbuff;
}
}
min (a, b)
{
return ((a < b) ? a : b);
}
movmem (from, to, count)
register char *from, *to;
register int count;
{
if (from > to)
while (count--)
*to++ = *from++;
else {
from += count;
to += count;
while (count--)
*--to = *--from;
}
}