123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- #include <stdlib.h>
- #include <limits.h>
- #include <memory.h>
- #include <signal.h>
- #include <termios.h>
- #include <unistd.h>
- #include "../tools/getoptv.h"
- #include "../tools/putoptv.h"
- #include "../tools/number.h"
- #include "../tools/error.h"
- #include "../tools/files.h"
- #include "../serial/serial.h"
- #ifndef MAKEFILE
- #include "../tools/getoptv.c"
- #include "../tools/putoptv.c"
- #include "../tools/version.c"
- #include "../tools/efreopen.c"
- #include "../tools/uintspec.c"
- #include "../tools/todigit.c"
- #include "../tools/error.c"
- #endif
- #if defined (WIN32)
- #error "This program does not support Windows platforms"
- #endif
- static signed copy (signed ifd, signed ofd, void * memory, signed extent)
- {
- while ((extent = read (ifd, memory, extent)) > 0)
- {
- if (write (ofd, memory, extent) < extent)
- {
- return (-1);
- }
- }
- return (0);
- }
- int main (int argc, char const * argv [])
- {
- static char const * optv [] =
- {
- "s:",
- PUTOPTV_S_FUNNEL,
- "copy one or more files to a serial device",
- "s n\tline speed is (n) [115200]",
- (char const *)(0)
- };
- struct termios restore;
- struct termios current;
- speed_t speed = B115200;
- byte buffer [512];
- signed c;
- while ((c = getoptv (argc, argv, optv)) != -1)
- {
- switch (c)
- {
- case 's':
- if (baudrate (uintspec (optarg, 0, UINT_MAX), &speed))
- {
- error (1, 0, "could not set baud rate");
- }
- break;
- default:
- break;
- }
- }
- argc -= optind;
- argv += optind;
- if (!isatty (STDOUT_FILENO))
- {
- error (1, ENOTSUP, "stdout must be a serial line device");
- }
- tcflush (STDOUT_FILENO, TCIFLUSH);
- tcgetattr (STDOUT_FILENO, &restore);
- memset (¤t, 0, sizeof (current));
- current.c_cflag = speed | CS8 | CLOCAL | CREAD;
- current.c_iflag = IGNPAR;
- current.c_oflag = 0;
- current.c_lflag = 0;
- current.c_cc [VTIME] = 0;
- current.c_cc [VMIN] = 5;
- tcsetattr (STDOUT_FILENO, TCSANOW, ¤t);
- if (!argc)
- {
- copy (STDIN_FILENO, STDOUT_FILENO, buffer, sizeof (buffer));
- }
- while ((argc) && (* argv))
- {
- if (efreopen (* argv, "rb", stdin))
- {
- copy (STDIN_FILENO, STDOUT_FILENO, buffer, sizeof (buffer));
- }
- argc--;
- argv++;
- }
- tcsetattr (STDOUT_FILENO, TCSANOW, &restore);
- exit (0);
- }
|