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
/
monitor.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-07-11
|
4KB
|
233 lines
#include <ctype.h>
#include <errno.h>
#include <signal.h>
#include <setjmp.h>
int tnc_in, tnc_out, ctl_in, ctl_out;
int signalman();
jmp_buf interrupt;
main (argc, argv)
char *argv[];
{
char filter[100], str[100];
int i, mon_path;
if (argc < 2) {
printf ("monitor <port> [<filter>]\n");
exit (0);
}
intercept (signalman);
upper (argv[1]);
strcpy (str, argv[2]);
if (argc > 2)
for (i = 3; i < argc; i++) {
strcat (str, " ");
strcat (str, argv[i]);
}
if (argc < 3)
/* No filter argument, monitor everything */
if (strcmp (argv[1], "T7") == 0 || strcmp (argv[1], "T8") == 0)
strcpy (filter, "USICRT+"); /* This is for a TNC-1 */
else
strcpy (filter, "USIC+"); /* This is for a TNC-2 */
else if (*str == '+' || *str == '-')
/* Short-hand for monitor everything to/from this station */
if (strcmp (argv[1], "T7") == 0)
sprintf (filter, "USICRT%s", str);
else
sprintf (filter, "USIC%s", str);
else
strcpy (filter, str);
moncmd (argv[1], filter);
sprintf (str, "/pipe/monitor.%s", argv[1]);
if ((mon_path = open (str, 1)) < 0)
exit (errno, printf ("Error opening %s\n", str));
if (setjmp (interrupt)) {
printf ("Cleaning up...\n");
moncmd (argv[1], "N");
drain (mon_path);
exit (0);
}
pfilter (mon_path);
}
moncmd (port, filter)
char *port, *filter;
{
char str[80], w[40];
int i, abort = 0;
if (!open_channel (port))
exit (errno, printf ("Error opening channel to %s\n", port));
sprintf (str, "M%s\n", filter);
write_path (ctl_out, str);
while ((i = _gs_rdy (ctl_out)) > 0) {
sleep (1);
}
write_path (ctl_out, "M\n");
read_path (ctl_in, str, 80);
printf ("%s\n", str);
scanword (str, w, 40);
if (strcmp (w, "INVALID") == 0) {
read_path (ctl_in, str, 80);
abort++;
}
if (!close_channel (port))
exit (errno, printf ("Error closing channel to %s\n", port));
if (abort) exit (0);
}
pfilter (path)
{
int len;
char line[300], *p;
char *str = "^ ";
int time, oldtime, date, day, tick;
while ((len = readln (path, line, 300)) > 0) {
_sysdate (0, &time, &date, &day, &tick);
if ((time & 0xffff00) != (oldtime & 0xffff00)) {
char str2[10];
oldtime = time;
sprintf (str2, "%02d:%02d\n", (time >> 16) & 0xff, (time >> 8) & 0xff);
writeln (1, str2, strlen (str2));
}
for (p = line; p - line < len; p++) {
if ((*p & 0x7f) < ' ' && *p != '\n' && *p != '\t') {
str[1] = (*p & 0x7f) + '@';
writeln (1, str, 2);
}
else
writeln (1, p, 1);
}
}
}
signalman (signal)
{
longjmp (interrupt);
}
open_channel (port)
char *port;
{
int hostcmd;
int i, pid;
char str[40];
sprintf (str, "/pipe/command.%s", port);
if ((hostcmd = open (str, 2)) < 0)
return (0);
pid = getpid ();
sprintf (str, "o%d\n", pid);
write_path (hostcmd, str);
close (hostcmd);
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)
return (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);
return (1);
}
close_channel (port)
char *port;
{
int hostcmd;
int i, j, pid;
char str[40];
sprintf (str, "/pipe/command.%s", port);
if ((hostcmd = open (str, 2)) < 0)
return (0);
pid = getpid ();
sprintf (str, "c%d\n", pid);
write_path (hostcmd, str);
close (hostcmd);
sleep (5);
drain (tnc_in);
drain (tnc_out);
drain (ctl_in);
drain (ctl_out);
close (tnc_in);
close (tnc_out);
close (ctl_in);
close (ctl_out);
sprintf (str, "/pipe/data_in.%d", pid);
for (i = 0; i < 10; i++) {
if (access (str, 0) == -1) break;
sleep (1);
}
if (i == 10) return 0;
return (1);
}
drain (path)
{
int i, j;
char c;
while ((i = _gs_rdy (path)) > 0) {
for (j = 0; j < i; j++)
read (path, &c, 1);
sleep (1);
}
}
write_path (path, str)
char *str;
{
write (path, str, strlen (str));
}
read_path (path, str, len)
char *str;
{
int i;
i = readln (path, str, len);
if (i)
str[i-1] = '\0';
return i;
}
scanword (s, d, len)
char *s, *d;
{
char *p;
int num = 0;
while (*s && isspace (*s)) {
s++;
num++;
}
p = d;
while (*s && !isspace (*s)) {
if (p - d < len - 1) *p++ = *s;
s++;
num++;
}
*p = '\0';
return (num);
}
upper (s)
char *s;
{
while (*s) {
*s = toupper (*s);
s++;
}
}