/*
*  Output text slowly, simulating a physical serial terminal
*/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include "tsc_usleep.h"

#define DEFBAUD 9600

int baud = DEFBAUD;
int delay;


static void output(int fd)
{
   unsigned char c;

   while (read(fd, &c, 1) > 0) {
       tsc_usleep(delay);
       write(1, &c, 1);
   }
}

static void usage(void)
{
   fprintf(stderr, "usage: scat [-b baud] [file ...]\n");
   exit(1);
}

int main(int argc, char **argv)
{
   int c;

   while ((c = getopt(argc, argv, ":b:")) != -1) {
       switch (c) {
           case 'b':
               baud = atoi(optarg);
               break;
           case '?':
           case ':':
           default:
               usage();
       }
   }

   delay = 1000000 / ((double)baud / 10.);

   if (optind < argc) {
       int i;
       for (i = optind; i < argc; i++) {
           int fd;
           if ((fd = open(argv[i], O_RDONLY)) < 0) {
               perror("open");
               continue;
           }
           output(fd);
           close(fd);
       }
   } else
       output(0);

   return 0;
}