Implemented -i|--in-tee and -o|--out-tee options
[slsnif.git] / src / slsnif.c
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 :