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 >
C/C++ Source or Header  |  1992-07-11  |  4KB  |  233 lines

  1. #include <ctype.h>
  2. #include <errno.h>
  3. #include <signal.h>
  4. #include <setjmp.h>
  5.  
  6. int tnc_in, tnc_out, ctl_in, ctl_out;
  7. int signalman();
  8. jmp_buf interrupt;
  9.  
  10. main (argc, argv)
  11. char *argv[];
  12. {
  13.     char filter[100], str[100];
  14.     int i, mon_path;
  15.  
  16.     if (argc < 2) {
  17.         printf ("monitor <port> [<filter>]\n");
  18.         exit (0);
  19.         }
  20.     intercept (signalman);
  21.     upper (argv[1]);
  22.     strcpy (str, argv[2]);
  23.     if (argc > 2)
  24.         for (i = 3; i < argc; i++) {
  25.             strcat (str, " ");
  26.             strcat (str, argv[i]);
  27.             }
  28.     if (argc < 3)
  29.         /* No filter argument, monitor everything */
  30.         if (strcmp (argv[1], "T7") == 0 || strcmp (argv[1], "T8") == 0)
  31.             strcpy (filter, "USICRT+");        /* This is for a TNC-1 */
  32.         else
  33.             strcpy (filter, "USIC+");        /* This is for a TNC-2 */
  34.     else if (*str == '+' || *str == '-')
  35.         /* Short-hand for monitor everything to/from this station */
  36.         if (strcmp (argv[1], "T7") == 0)
  37.             sprintf (filter, "USICRT%s", str);
  38.         else
  39.             sprintf (filter, "USIC%s", str);
  40.     else
  41.         strcpy (filter, str);
  42.     moncmd (argv[1], filter);
  43.     sprintf (str, "/pipe/monitor.%s", argv[1]);
  44.     if ((mon_path = open (str, 1)) < 0)
  45.         exit (errno, printf ("Error opening %s\n", str));
  46.     if (setjmp (interrupt)) {
  47.         printf ("Cleaning up...\n");
  48.         moncmd (argv[1], "N");
  49.         drain (mon_path);
  50.         exit (0);
  51.         }
  52.     pfilter (mon_path);
  53.     }
  54.  
  55. moncmd (port, filter)
  56. char *port, *filter;
  57. {
  58.     char str[80], w[40];
  59.     int i, abort = 0;
  60.  
  61.     if (!open_channel (port))
  62.         exit (errno, printf ("Error opening channel to %s\n", port));
  63.     sprintf (str, "M%s\n", filter);
  64.     write_path (ctl_out, str);
  65.     while ((i = _gs_rdy (ctl_out)) > 0) {
  66.         sleep (1);
  67.         }
  68.     write_path (ctl_out, "M\n");
  69.     read_path (ctl_in, str, 80);
  70. printf ("%s\n", str);
  71.     scanword (str, w, 40);
  72.     if (strcmp (w, "INVALID") == 0) {
  73.         read_path (ctl_in, str, 80);
  74.         abort++;
  75.         }
  76.     if (!close_channel (port))
  77.         exit (errno, printf ("Error closing channel to %s\n", port));
  78.     if (abort) exit (0);
  79.     }
  80.  
  81. pfilter (path)
  82. {
  83.     int len;
  84.     char line[300], *p;
  85.     char *str = "^ ";
  86.     int time, oldtime, date, day, tick;
  87.     
  88.     while ((len = readln (path, line, 300)) > 0) {
  89.         _sysdate (0, &time, &date, &day, &tick);
  90.         if ((time & 0xffff00) != (oldtime & 0xffff00)) {
  91.             char str2[10];
  92.             
  93.             oldtime = time;
  94.             sprintf (str2, "%02d:%02d\n", (time >> 16) & 0xff, (time >> 8) & 0xff);
  95.             writeln (1, str2, strlen (str2));
  96.             }
  97.         for (p = line; p - line < len; p++) {
  98.             if ((*p & 0x7f) < ' ' && *p != '\n' && *p != '\t') {
  99.                 str[1] = (*p & 0x7f) + '@';
  100.                 writeln (1, str, 2);
  101.                 }
  102.             else
  103.                 writeln (1, p, 1);
  104.             }
  105.         }
  106.     }
  107.  
  108. signalman (signal)
  109. {
  110.     longjmp (interrupt);
  111.     }
  112.  
  113. open_channel (port)
  114. char *port;
  115. {
  116.     int hostcmd;
  117.     int i, pid;
  118.     char str[40];
  119.  
  120.     sprintf (str, "/pipe/command.%s", port);
  121.     if ((hostcmd = open (str, 2)) < 0)
  122.         return (0);
  123.     pid = getpid ();
  124.     sprintf (str, "o%d\n", pid);
  125.     write_path (hostcmd, str);
  126.     close (hostcmd);
  127.     sprintf (str, "/pipe/data_in.%d", pid);
  128.     for (i = 0; i < 10; i++) {
  129.         if ((tnc_in = open (str, 3)) != -1) break;
  130.         sleep (1);
  131.         }
  132.     if (tnc_in == -1)
  133.         return (0);
  134.     sprintf (str, "/pipe/data_out.%d", pid);
  135.     tnc_out = open (str, 3);
  136.     sprintf (str, "/pipe/ctl_in.%d", pid);
  137.     ctl_in = open (str, 3);
  138.     sprintf (str, "/pipe/ctl_out.%d", pid);
  139.     ctl_out = open (str, 3);
  140.     return (1);
  141.     }
  142.  
  143. close_channel (port)
  144. char *port;
  145. {
  146.     int hostcmd;
  147.     int i, j, pid;
  148.     char str[40];
  149.  
  150.     sprintf (str, "/pipe/command.%s", port);
  151.     if ((hostcmd = open (str, 2)) < 0)
  152.         return (0);
  153.     pid = getpid ();
  154.     sprintf (str, "c%d\n", pid);
  155.     write_path (hostcmd, str);
  156.     close (hostcmd);
  157.     sleep (5);
  158.     drain (tnc_in);
  159.     drain (tnc_out);
  160.     drain (ctl_in);
  161.     drain (ctl_out);
  162.     close (tnc_in);
  163.     close (tnc_out);
  164.     close (ctl_in);
  165.     close (ctl_out);
  166.     sprintf (str, "/pipe/data_in.%d", pid);
  167.     for (i = 0; i < 10; i++) {
  168.         if (access (str, 0) == -1) break;
  169.         sleep (1);
  170.         }
  171.     if (i == 10) return 0;
  172.     return (1);
  173.     }
  174.  
  175. drain (path)
  176. {
  177.     int i, j;
  178.     char c;
  179.  
  180.     while ((i = _gs_rdy (path)) > 0) {
  181.         for (j = 0; j < i; j++)
  182.             read (path, &c, 1);
  183.         sleep (1);
  184.         }
  185.     }
  186.  
  187. write_path (path, str)
  188. char *str;
  189. {
  190.     write (path, str, strlen (str));
  191.     }
  192.  
  193. read_path (path, str, len)
  194. char *str;
  195. {
  196.     int i;
  197.     i = readln (path, str, len);
  198.     if (i)
  199.         str[i-1] = '\0';
  200.     return i;
  201.     }
  202.  
  203. scanword (s, d, len)
  204. char *s, *d;
  205. {
  206.     char *p;
  207.     int num = 0;
  208.  
  209.     while (*s && isspace (*s)) {
  210.         s++;
  211.         num++;
  212.         }
  213.     p = d;
  214.     while (*s && !isspace (*s)) {
  215.         if (p - d < len - 1) *p++ = *s;
  216.         s++;
  217.         num++;
  218.         }
  219.     *p = '\0';
  220.     return (num);
  221.     }
  222.  
  223. upper (s)
  224. char *s;
  225. {
  226.     while (*s) {
  227.         *s = toupper (*s);
  228.         s++;
  229.         }
  230.     }
  231.  
  232.  
  233.