Implemented -i|--in-tee and -o|--out-tee options
authorshort <>
Wed, 6 Mar 2002 04:26:55 +0000 (04:26 +0000)
committershort <>
Wed, 6 Mar 2002 04:26:55 +0000 (04:26 +0000)
src/slsnif.c
src/slsnif.h

index ca797b6..09f532d 100644 (file)
@@ -43,6 +43,8 @@ void usage() {
     printf("  -l (--log) <logfile>    - file to store output in, defaults to stdout.\n");
     printf("  -s (--speed) <speed>    - baudrate to use, defaults to 9600 baud.\n");
     printf("  -p (--port2) <port2>    - serial port to use instead of pty.\n");
+    printf("  -i (--in-tee)  <file>   - write all input  raw data also to this file(s).\n");
+    printf("  -o (--out-tee) <file>   - write all output raw data also to this file(s).\n");
     printf("  --color      <color>    - color to use for normal output.\n");
     printf("  --timecolor  <color>    - color to use for timestamp.\n");
     printf("  --bytescolor <color>    - color to use for number of bytes transmitted.\n\n");
@@ -147,6 +149,8 @@ void writeData(int in, int out, int aux, int mode) {
                 write(out, buffer, n);
                 write(aux, buffer, n);                    
             } else {
+                struct tee_entry *entry;
+
                 /* print timestamp if necessary */
                 if (tty_data.tstamp) {
                     if (out == STDOUT_FILENO) setColor(out, tty_data.tclr);
@@ -183,6 +187,9 @@ void writeData(int in, int out, int aux, int mode) {
                     if (out == STDOUT_FILENO) setColor(out, tty_data.bclr);
                     write (out, buffer, strlen(buffer));
                 }
+                for (entry = (aux ? tee_inputs : tee_outputs); entry; entry=entry->next)
+                    if (n!=write(entry->fd, buffer, n))
+                        fatalError(TEEWRTFAIL);
             }
         }
     }
@@ -296,6 +303,8 @@ int main(int argc, char *argv[]) {
         {"color",      1, NULL, 'x'},
         {"timecolor",  1, NULL, 'y'},
         {"bytescolor", 1, NULL, 'z'},
+        {"in-tee",     1, NULL, 'i'},
+        {"out-tee",    1, NULL, 'o'},
         {NULL,         0, NULL,   0}
     };
 #endif
@@ -372,6 +381,20 @@ int main(int argc, char *argv[]) {
                     strcat(tty_data.bclr, ptr1);
                 }
                 break;
+            case 'i':
+            case 'o': {
+                int fd=open(optarg, O_WRONLY|O_NOCTTY|O_CREAT|O_TRUNC, 0644);
+                struct tee_entry *entry=malloc(sizeof(*entry));
+                struct tee_entry **entryp=(optret=='i' ? &tee_inputs : &tee_outputs);
+
+                if (fd == -1)
+                    fatalError(TEEFAIL);
+                if (!entry)
+                    fatalError(MEMFAIL);
+                entry->fd = fd;
+                entry->next = *entryp;
+                *entryp = entry;
+                } break;
             case 'h':
             case '?':
             default :
index 7baca70..f586617 100644 (file)
@@ -38,7 +38,7 @@
 #endif
 #include "common.h"
 
-#define OPTSTR      "bnhtp:l:s:" /* list of short options for getopt_long() */
+#define OPTSTR      "bnhtp:l:s:i:o:" /* list of short options for getopt_long() */
 #define PORT_OUT    "\nHost   --> "
 #define PORT_IN     "\nDevice --> "
 #define DEFPTRNAME  "/dev/pty??"
@@ -47,6 +47,8 @@
 #define PTYFAIL     "Failed to open a pty"
 #define PORTFAIL    "Failed to open port"
 #define LOGFAIL     "Failed to open log file, defaulting to stdout"
+#define TEEFAIL     "Failed to create tee file"
+#define TEEWRTFAIL  "Failed to write to tee file"
 #define SELFAIL     "`Select` failed"
 #define RPORTFAIL   "Error reading from port"
 #define RPTYFAIL    "Error reading from pty"
 static tty_struct   tty_data;
 static pid_t        pid = -1;
 
+static struct tee_entry {
+    struct tee_entry *next;
+    int fd;
+    } *tee_inputs,*tee_outputs;
+
 /* baudrates */
 typedef struct _spd_struct {
     speed_t speed;