Changed the way buffer processing functions take arguments
[ust.git] / ust.c
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <getopt.h>
4 #include <signal.h>
5 #include <stdarg.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <pthread.h>
11 #include <sys/ioctl.h>
12
13 #ifdef __linux__
14 #include <asm/termbits.h>
15 #include <asm/ioctls.h>
16 #else
17 #include <termios.h>
18 #endif
19
20 #include "config.h"
21
22 #define CR '\r'
23 #define LF '\n'
24 #define BS '\b'
25 #define DEL '\x7f'
26 #define ESC '\x1b'
27 #if defined(CCDTR_IFLOW) && defined(CDSR_OFLOW)
28 #define CDTRDSR (CDTR_IFLOW | CDSR_OFLOW)
29 #endif
30 #define IXONXOFF (IXON | IXOFF)
31
32 typedef struct {
33 ssize_t sizem;
34 char find;
35 char input;
36 int offset;
37 } Args;
38
39 static int gettermattr(int dv, void *strct);
40 static int settermattr(int dv, const void *strct);
41 static void settermspd(unsigned int lispeed, unsigned int lospeed, const void *strct);
42 static int openport();
43 static unsigned int strtoui(const char *str, const char *msg, unsigned int min);
44 static int troptions(CRLFOpt *options, const char *str);
45 static int setroptions();
46 static inline int termchck(const void *term);
47 static void interchck();
48 static void cechck();
49 static void *writeport(void *unused);
50 static void *readport(void *unused);
51 static void getcmd(int escape);
52 static inline void replacechar(char *buff, ssize_t sread, const Args *args);
53 static inline ssize_t addchar(char *buff, ssize_t sread, int *scratch, const Args *args);
54 static inline ssize_t rmchar(char *buff, ssize_t sread, int *scratch, const Args *args);
55 static void sighandl(int signo);
56 static void die(int code, const char *msg, ...);
57
58 #ifdef __linux__
59 static struct termios2 cntrl, origterm = {0}, newterm = {0};
60 #else
61 static struct termios cntrl, origterm = {0}, newterm = {0};
62 #endif
63
64 static const unsigned int uintmax = ~(unsigned int)0;
65 static struct timespec wts;
66 static char *writebuff = NULL;
67 static char *readbuff = NULL;
68 static int *scratchr = NULL;
69 static int *scratchw = NULL;
70 static char backspc = DEL,tbackspc = DEL;
71 static int fd = -1;
72 static int interactive = 0;
73
74 int
75 gettermattr(int dv, void *strct)
76 {
77 #ifdef __linux__
78 struct termios2 *optst = (struct termios2*)strct;
79 return ioctl(dv, TCGETS2, optst);
80 #else
81 struct termios *optst = (struct termios*)strct;
82 return tcgetattr(dv, optst);
83 #endif
84 }
85
86 int
87 settermattr(int dv, const void *strct)
88 {
89 #ifdef __linux__
90 struct termios2 *optst = (struct termios2*)strct;
91 return ioctl(dv, TCSETS2, optst);
92 #else
93 struct termios *optst = (struct termios*)strct;
94 return tcsetattr(dv, TCSANOW, optst);
95 #endif
96 }
97
98 void
99 settermspd(unsigned int lispeed, unsigned int lospeed, const void *strct)
100 {
101 #ifdef __linux__
102 struct termios2 *optst = (struct termios2*)strct;
103
104 optst->c_cflag &= ~CBAUD;
105 optst->c_cflag |= BOTHER;
106 optst->c_ispeed = ispeed;
107 optst->c_ospeed = ospeed;
108 #else
109 struct termios *optst = (struct termios*)strct;
110 cfsetispeed(optst, ispeed);
111 cfsetospeed(optst, ospeed);
112 #endif
113 }
114
115 int
116 openport()
117 {
118 if (verbose) fprintf(stderr, "opening \"%s\"\n", line);
119
120 fd = open(line, O_RDWR | O_NOCTTY | O_NDELAY);
121 if (fd == -1) {
122 perror("error opening device");
123 exit(1);
124 }
125
126 if (verbose) fprintf(stderr, "checking if \"%s\" is a TTY\n", line);
127
128 if (!isatty(fd))
129 die(2, "device \"%s\" is not a TTY\n", line);
130
131 int flags;
132 flags = fcntl(fd, F_GETFL, 0);
133 /* opened to check with non-blocking mode, now set to blocking */
134 flags &= ~(O_NONBLOCK | O_NDELAY);
135 if (fcntl(fd, F_SETFL, flags) == -1)
136 die(1, "failed to set the device to blocking mode\n");
137
138 if (gettermattr(fd, &cntrl) == -1)
139 die(1, "failed to get device attributes\n");
140
141 if (verbose) fprintf(stderr, "setting baudrate [RX:%u | TX:%u]\n", ispeed, ospeed);
142
143 settermspd(ispeed, ospeed, &cntrl);
144 if (settermattr(fd, &cntrl) == -1)
145 die(2,"failed to set baudrate [RX:%u | TX:%u]\n", ispeed, ospeed);
146
147 cntrl.c_lflag = 0;
148 cntrl.c_iflag &= ~(ISTRIP | BRKINT);
149 cntrl.c_cflag &= ~(PARENB | PARODD);
150
151 if (verbose) fprintf(stderr, "setting parity [even: %d, odd: %d]\n", parity & 1, (parity & 2) >> 1);
152
153 if (parity == 1)
154 cntrl.c_cflag |= PARENB;
155 if (parity == 2)
156 cntrl.c_cflag |= PARENB | PARODD;
157
158 if (settermattr(fd, &cntrl) == -1)
159 die(2, "failed to set parity [even: %d, odd: %d]\n", parity & 1, (parity & 2) >> 1);
160
161 if (verbose) {
162 /* it's so ugly and beautiful at the same time */
163 int t = (((hard & 1) << 1) & (hard & 2)) >> 1;
164 fprintf(stderr, "setting flow control [XON/XOFF: %d, RTS/CTS: %d, DTR/DSR: %d, DCD: %d]\n",\
165 soft, t ^ ((hard & 1) << 1) >> 1, t ^ (hard & 2) >> 1, t);
166 }
167
168 cntrl.c_cflag |= CLOCAL;
169 cntrl.c_iflag &= ~IXONXOFF;
170
171 if (soft)
172 cntrl.c_iflag |= IXONXOFF;
173
174 if (hard == 1) {
175 cntrl.c_cflag |= CRTSCTS;
176 } else if (hard == 2) {
177 #ifndef CDTRDSR
178 fprintf(stderr, "DTR/DSR flow control is not supported on this platform\n"
179 "enabling this option does nothing!\n");
180 #else
181 cntrl.c_lflag |= CDTRDSR;
182 #endif
183 } else if (hard == 3) {
184 cntrl.c_cflag &= ~CLOCAL;
185 #ifdef CCAR_OFLOW
186 cntrl.c_lflag |= CCAR_OFLOW;
187 #endif
188 }
189
190 if (settermattr(fd, &cntrl) == -1)
191 die(2, "failed to set flow control\n");
192
193 if (setroptions())
194 die(2, "failed to set cr-lf translation options\n");
195
196 if (verbose) fprintf(stderr, "setting data bits [%c]\n", datab);
197
198 tcflag_t db = CS8;
199 switch (datab) {
200 case '5':
201 db = CS5; break;
202 case '6':
203 db = CS6; break;
204 case '7':
205 db = CS7; break;
206 default:
207 break;
208 }
209 cntrl.c_cflag &= ~CSIZE;
210 cntrl.c_cflag |= db;
211
212 if (settermattr(fd, &cntrl) == -1)
213 die(2, "failed to set data bits [%c]\n", datab);
214
215 if (verbose) fprintf(stderr, "setting stop bits [%d]\n", stopb+1);
216
217 if (stopb)
218 cntrl.c_cflag |= CSTOPB;
219 else
220 cntrl.c_cflag &= ~CSTOPB;
221
222 if (settermattr(fd, &cntrl) == -1)
223 die(1, "failed to set stop bits [%d]\n", stopb+1);
224
225 #ifdef __linux__
226 ioctl(fd, TCFLSH, TCIOFLUSH);
227 #else
228 tcflush(fd, TCIOFLUSH);
229 #endif
230 return(fd);
231 }
232
233 unsigned int
234 strtoui(const char *str, const char *msg, unsigned int min)
235 {
236 long t;
237 char *endptr;
238
239 t = strtol(str, &endptr, 10);
240 if (errno != 0 || *endptr != '\0' || t < min) {
241 fprintf(stderr, msg, str);
242 return uintmax;
243 }
244 /*
245 * conversion like this results in 'undefined behavior' according to C spec,
246 * but almost all compilers will just truncate the value so it's OK
247 */
248 return (unsigned int)t;
249 /* termios's `speed_t` is a typedef for `unsigned int` */
250 }
251
252 int
253 troptions(CRLFOpt *options, const char *str)
254 {
255 switch (str[0]) {
256 case 'l':
257 if (strcmp(str, "lf-in-cr") == 0) {
258 options->lfincr = !options->lfincr; return 0;
259 } else if (strcmp(str, "lf-to-cr") == 0) {
260 options->lftocr = !options->lftocr; return 0;
261 } else {
262 goto trerr;
263 }
264 break;
265 case 'c':
266 if (strcmp(str, "cr-in-lf") == 0) {
267 options->crinlf = !options->crinlf; return 0;
268 } else if (strcmp(str, "cr-to-lf") == 0) {
269 options->crtolf = !options->crtolf; return 0;
270 } else {
271 goto trerr;
272 }
273 break;
274 case 'n':
275 if (strcmp(str, "no-lf") == 0) {
276 options->nolf = !options->nolf; return 0;
277 } else if (strcmp(str, "no-cr") == 0) {
278 options->nocr = !options->nocr; return 0;
279 } else {
280 goto trerr;
281 }
282 break;
283 default:
284 trerr:
285 fprintf(stderr, "invalid translation option: %s\n", str);
286 return 1;
287 }
288 }
289
290 int
291 setroptions()
292 {
293 if (itropts.crtolf)
294 cntrl.c_iflag |= ICRNL;
295 else
296 cntrl.c_iflag &= ~ICRNL;
297
298 if (itropts.lftocr)
299 cntrl.c_iflag |= INLCR;
300 else
301 cntrl.c_iflag &= ~INLCR;
302
303 if (tropts.crtolf)
304 cntrl.c_oflag |= OCRNL;
305 else
306 cntrl.c_oflag &= ~OCRNL;
307
308 if (tropts.lftocr)
309 cntrl.c_oflag |= ONLRET;
310 else
311 cntrl.c_oflag &= ~ONLRET;
312
313 if (tropts.crinlf)
314 cntrl.c_oflag |= ONLCR;
315 else
316 cntrl.c_oflag &= ~ONLCR;
317
318 return settermattr(fd, &cntrl);
319 }
320
321 inline int
322 termchck(const void *term)
323 {
324 #ifdef __linux__
325 struct termios2 *iterm = (struct termios2*)term;
326 #define TERMIOS_STRUCT termios2
327 #else
328 struct termios *iterm = (struct termios*)term;
329 #define TERMIOS_STRUCT termios
330 #endif
331 for (size_t i = 0; i < sizeof(struct TERMIOS_STRUCT); i++)
332 if (((char*)iterm)[i] != 0)
333 return 1;
334
335 return 0;
336 }
337
338 void *
339 writeport(void *unused)
340 {
341 struct timespec ts;
342 ts.tv_sec = swritedelay;
343 ts.tv_nsec = nswritedelay;
344 wts.tv_sec = 0;
345 wts.tv_nsec = chardelay;
346 writebuff = malloc(wbuffsz * sizeof(char));
347 scratchw = malloc(scratchwsz * sizeof(int));
348
349 if (writebuff == NULL)
350 die(1, "buffer allocation failed\n");
351 if (scratchw == NULL)
352 die(1, "scratch buffer allocation failed\n");
353
354 int escape = 0;
355
356
357 Args bsargs, nocrargs, nolfargs, lfincrargs;
358 bsargs.sizem = wbuffsz;
359 nocrargs = nolfargs = lfincrargs = bsargs;
360
361 bsargs.find=backspc; bsargs.input=tbackspc;
362
363 nocrargs.find = CR;
364 nolfargs.find = LF;
365
366 lfincrargs.find = CR; lfincrargs.input = LF; lfincrargs.offset = 1;
367
368 for (;;) {
369 ssize_t inln = read(STDIN_FILENO, writebuff, wbuffsz - 1);
370 if (inln > 0) {
371 if (escape) {
372 getcmd(escape);
373 escape = 0;
374 }
375 if (writebuff[0] == escapechar) {
376 if (inln > 1)
377 getcmd(escape);
378 else
379 escape = 1;
380 }
381
382 if (backspc != tbackspc)
383 replacechar(writebuff, inln, &bsargs);
384
385 if (tropts.nocr)
386 inln = rmchar(writebuff, inln, scratchw, &nocrargs);
387 if (tropts.nolf)
388 inln = rmchar(writebuff, inln, scratchw, &nolfargs);
389 if (tropts.lfincr)
390 inln = addchar(writebuff, inln, scratchw, &lfincrargs);
391
392 if (inln > 1) {
393 for (int i = 0; i <= inln; i++) {
394 write(fd, &writebuff[i], 1);
395 nanosleep(&wts, NULL);
396 }
397 } else {
398 write(fd, writebuff, 1);
399 }
400 }
401 nanosleep(&ts, NULL);
402 }
403 return NULL;
404 }
405
406 void *
407 readport(void *unused)
408 {
409 struct timespec ts;
410 ts.tv_sec = sreaddelay;
411 ts.tv_nsec = nsreaddelay;
412 readbuff = malloc(rbuffsz * sizeof(char));
413 scratchr = malloc(scratchrsz * sizeof(int));
414
415 if (readbuff == NULL)
416 die(1, "buffer allocation failed\n");
417 if (scratchr == NULL)
418 die(1, "scratch buffer allocation failed\n");
419
420 interchck();
421
422 Args nocrargs, nolfargs, crinlfargs, lfincrargs;
423 nocrargs.sizem = rbuffsz;
424 nolfargs = crinlfargs = lfincrargs = nocrargs;
425
426 nocrargs.find = CR;
427 nolfargs.find = LF;
428
429 lfincrargs.find = CR; lfincrargs.input = LF; lfincrargs.offset = 1;
430 crinlfargs.find = LF; crinlfargs.input = CR; crinlfargs.offset = -1;
431
432
433 /* disable stdout buffering */
434 setvbuf(stdout, NULL, _IONBF, 0);
435
436 for (;;) {
437 ssize_t outln = read(fd, readbuff, rbuffsz - 1);
438 if (outln > 0) {
439 if (itropts.nocr)
440 outln = rmchar(readbuff, outln, scratchr, &nocrargs);
441 if (itropts.nolf)
442 outln = rmchar(readbuff, outln, scratchr, &nolfargs);
443 if (itropts.crinlf)
444 outln = addchar(readbuff, outln, scratchr, &crinlfargs);
445 if (itropts.lfincr)
446 outln = addchar(readbuff, outln, scratchr, &lfincrargs);
447
448 write(STDOUT_FILENO, readbuff, outln);
449 }
450 nanosleep(&ts, NULL);
451 }
452 if (isatty(STDIN_FILENO))
453 settermattr(STDIN_FILENO, &origterm);
454 return NULL;
455 }
456
457 inline void __attribute__((hot))
458 replacechar(char *buff, ssize_t size, const Args *args)
459 {
460 for (int i = 0; i < size; i++)
461 if (buff[i] == args->find)
462 buff[i] = args->input;
463 }
464
465 /* TODO: optimize the function and allow for offsets greater than 1 */
466 inline ssize_t __attribute__((hot))
467 addchar(char *buff, ssize_t size, int *scratch, const Args *args)
468 {
469 int c = 0;
470
471 for (int i = 0; i < size; i++) {
472 if (buff[i] == args->find) {
473 scratch[c] = i;
474 c++;
475 }
476 }
477 if (!c)
478 return(size);
479 if ((size + c) > args->sizem)
480 c = args->sizem - size;
481
482 if (scratch[0] == 0 && args->offset < 0) {
483 memmove(&buff[0]+1, &buff[0], size);
484 scratch[0] = 1;
485 }
486 for (int i = c; i > 0; i--) {
487 int t = scratch[i];
488 for (int s = size; s >= t;s--)
489 buff[s + args->offset] = buff[s];
490 buff[t] = args->input;
491 }
492 return (size + c);
493 }
494
495 inline ssize_t __attribute__((hot))
496 rmchar(char *buff, ssize_t size, int *scratch, const Args *args)
497 {
498 int c = 0;
499
500 for (int i = 0; i < size; i++) {
501 if (buff[i] == args->find) {
502 scratch[c] = i;
503 c++;
504 }
505 }
506 if (!c)
507 return(size);
508
509 for (int i = c; i > 0; i--)
510 for (int s = size; s >= scratch[i]; s--)
511 buff[s] = buff[s + 1];
512
513 return(size - c);
514 }
515
516 void
517 interchck()
518 {
519 if (interactive) {
520 if (gettermattr(STDIN_FILENO, &origterm) < 0 )
521 die(1, "failed to get terminal attributes\n");
522
523 newterm = origterm;
524
525 if (!canonical)
526 newterm.c_lflag &= ~ECHO;
527 if (!half)
528 newterm.c_lflag &= ~ICANON;
529
530 if (soft)
531 newterm.c_iflag |= IXONXOFF;
532
533 newterm.c_iflag |= INLCR;
534 newterm.c_cc[VMIN] = minchars;
535 newterm.c_cc[VINTR] = _POSIX_VDISABLE;
536 newterm.c_cc[VSUSP] = _POSIX_VDISABLE;
537 newterm.c_cc[VQUIT] = _POSIX_VDISABLE;
538
539 if (backspace)
540 tbackspc = BS;
541 else
542 tbackspc = DEL;
543
544 backspc = origterm.c_cc[VERASE];
545
546 if (settermattr(STDOUT_FILENO, &newterm) < 0)
547 die(1, "failed to set terminal attributes\n");
548 }
549 return;
550 }
551
552 void
553 cechck()
554 {
555 if ((!half || !canonical) && interactive) {
556 newterm.c_lflag |= ECHO;
557 newterm.c_lflag |= ICANON;
558 if (settermattr(STDIN_FILENO, &newterm) < 0)
559 fprintf(stderr, "failed to enable echo and/or canonical mode\n");
560 }
561 return;
562 }
563
564 void
565 getcmd(int escape)
566 {
567 Args rmlf;
568 char cmdchar;
569 char ttr[64];
570 unsigned int tspd;
571
572 rmlf.find = LF; rmlf.input = '\0';
573
574 if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO))
575 interactive = 1;
576
577 interchck();
578
579 if (escape)
580 cmdchar = writebuff[0];
581 else
582 cmdchar = writebuff[1];
583
584 switch (cmdchar) {
585 case '.':
586 die(0,"\n[EOT]\n");
587 break;
588 case 'b':
589 if (backspace)
590 tbackspc = DEL;
591 else
592 tbackspc = BS;
593 backspace = !backspace;
594 break;
595 case 'h':
596 if (half)
597 newterm.c_lflag &= ~ECHO;
598 else
599 newterm.c_lflag |= ECHO;
600 half = !half;
601 if (settermattr(STDOUT_FILENO, &newterm) < 0)
602 die(1, "failed to set terminal attributes\n");
603 break;
604 case 'c':
605 if (canonical)
606 newterm.c_lflag &= ~ICANON;
607 else
608 newterm.c_lflag |= ICANON;
609 canonical = !canonical;
610 if (settermattr(STDOUT_FILENO, &newterm) < 0)
611 die(1, "failed to set terminal attributes\n");
612 break;
613 case 'w':
614 cechck();
615 if (fgets(ttr, sizeof(ttr), stdin) != NULL) {
616 replacechar(ttr, 63, &rmlf);
617 ssize_t frln;
618 int wfd = open(ttr, O_RDONLY);
619 if (fd == -1) {
620 perror("error opening file");
621 break;
622 }
623 while ((frln = read(wfd, writebuff, wbuffsz - 1)) > 0) {
624 for (int i = 0; i <= frln; i++) {
625 write(fd, &writebuff[i], 1);
626 nanosleep(&wts, NULL);
627 }
628 }
629 }
630 goto finish;
631 case 's':
632 cechck();
633 if (fgets(ttr, sizeof(ttr), stdin) != NULL) {
634 replacechar(ttr, 63, &rmlf);
635 tspd = strtoui(ttr, "invalid speed\n", 0);
636 if (tspd != uintmax) {
637 ospeed = ispeed = tspd;
638 settermspd(ispeed, ospeed, &cntrl);
639 if (settermattr(fd, &cntrl) != 0)
640 fprintf(stderr, "failed to set baudrate "
641 "[RX:%u | TX:%u]\n", ispeed, ospeed);
642 }
643 }
644 goto finish;
645 case 'd':
646 cechck();
647 if (fgets(ttr, sizeof(ttr), stdin) != NULL) {
648 replacechar(ttr, 63, &rmlf);
649 chardelay = strtoui(ttr, "invalid delay\n", 0);
650 if (chardelay != uintmax) {
651 wts.tv_sec = 0;
652 wts.tv_nsec = chardelay;
653 }
654 }
655 goto finish;
656 case 't':
657 cechck();
658 fprintf(stderr, "additional output translation option: ");
659 if (fgets(ttr, sizeof(ttr), stdin) != NULL) {
660 replacechar(ttr, 63, &rmlf);
661 if(troptions(&tropts, ttr))
662 goto finish;
663 if (setroptions())
664 fprintf(stderr, "could not set new options\n");
665 }
666 goto finish;
667 case 'T':
668 cechck();
669 printf("additional input translation option: ");
670 if (fgets(ttr, sizeof(ttr), stdin) != NULL) {
671 replacechar(ttr, 63, &rmlf);
672 if (troptions(&itropts, ttr))
673 goto finish;
674 if (setroptions())
675 fprintf(stderr, "could not set new options\n");
676 }
677 finish:
678 if (!half && interactive)
679 newterm.c_lflag &= ~ECHO;
680 if (!canonical && interactive)
681 newterm.c_lflag &= ~ICANON;
682 if (settermattr(STDIN_FILENO, &newterm) == -1)
683 fprintf(stderr, "failed to restore echo and/or canonical mode\n");
684 break;
685 case 'p':;
686 int st;
687 struct timespec ts;
688 ts.tv_sec = spulsedelay;
689 ts.tv_nsec = nspulsedelay;
690
691 if (ioctl(fd, TIOCMGET, &st) != 0) {
692 fprintf(stderr, "failed to get port status\n");
693 break;
694 }
695 st ^= TIOCM_DTR;
696 if (ioctl(fd, TIOCMSET, &st) != 0) {
697 fprintf(stderr, "failed to set DTR [assertion]\n");
698 break;
699 }
700 nanosleep(&ts, NULL);
701
702 st ^= TIOCM_DTR;
703 if (ioctl(fd, TIOCMSET, &st) != 0)
704 fprintf(stderr, "failed to set DTR [negation]\n");
705 break;
706 case BS: /* FALLTHROUGH */
707 case DEL:
708 case ESC:
709 break;
710 default:
711 fprintf(stderr, "not a valid command [%c]\n", cmdchar);
712 break;
713 }
714 return;
715 }
716
717 void
718 sighandl(int signo)
719 {
720 die(128 + signo, "\nrecieved signal [%d], exiting\n", signo);
721 }
722
723 void
724 die(int code, const char *msg, ...)
725 {
726 va_list fpa;
727 if (fd != -1)
728 close(fd);
729
730 if (writebuff)
731 free(writebuff);
732 if (readbuff)
733 free(readbuff);
734 if (scratchw)
735 free(scratchw);
736 if (scratchr)
737 free(scratchr);
738
739 if (termchck(&newterm))
740 settermattr(STDIN_FILENO, &origterm);
741
742 va_start(fpa, msg);
743 vfprintf(stderr, msg, fpa);
744 va_end(fpa);
745
746 exit(code);
747 }
748
749 int
750 main(int argc, char **argv)
751 {
752 for (int i = 1; i < argc; i++ ) {
753 if (argv[i][0] == '-' && argv[i][1] >= '0' && argv[i][1] <= '9') {
754 char *t = NULL;
755 /* glibc's `asprintf()` won't set the string to NULL on failure automatically */
756 asprintf(&t, "-s%s", argv[i] + 1);
757 if (!t) {
758 fprintf(stderr, "cannot convert -# to -s#\n");
759 break;
760 }
761 argv[i] = t;
762 free(t);
763 }
764 }
765
766 static struct option longopt[] = {
767 {"line", required_argument, NULL, 'l'},
768 {"speed", required_argument, NULL, 's'},
769 {"rx-speed", required_argument, NULL, 'i'},
770 {"echo", no_argument, NULL, 'h'},
771 {"canonical", no_argument, NULL, 'c'},
772 {"odd", no_argument, NULL, 'o'},
773 {"even", no_argument, NULL, 'e'},
774 {"hardware-rts-cts", no_argument, NULL, 'R'},
775 {"hardware-dtr-dsr", no_argument, NULL, 'r'},
776 {"software", no_argument, NULL, 'X'},
777 {"data", required_argument, NULL, 'D'},
778 {"delay", required_argument, NULL, 'd'},
779 {"min-chars", required_argument, NULL, 'm'},
780 {"stop-bits", no_argument, NULL, 'S'},
781 {"backspace", no_argument, NULL, 'b'},
782 {"translation", required_argument, NULL, 't'},
783 {"input-translation", required_argument, NULL, 'T'},
784 {"verbose", no_argument, NULL, 'v'},
785 {NULL, 0, NULL, 0}
786 };
787
788 unsigned int tui;
789 int oind, rxspdset, devset, c;
790 oind = rxspdset = devset = 0;
791 while ((c = getopt_long(argc, argv, "bcd:D:ehi:l:m:oRrs:t:T:vX", longopt, &oind)) != -1) {
792 switch (c) {
793 case 0:
794 break;
795 case 'b':
796 backspace ^= 1; break;
797 case 'c':
798 canonical ^= 1; break;
799 case 'R':
800 hard ^= 1; break;
801 case 'r':
802 hard ^= 2; break;
803 case 'X':
804 soft ^= 1; break;
805 case 'd':;
806 char* endptr;
807 long t;
808 t = strtol(optarg, &endptr, 10);
809 if (errno != 0 || *endptr != '\0' || t < 0) {
810 fprintf(stderr, "invalid character delay\n");
811 goto ustusage;
812 } else {
813 chardelay = t;
814 }
815 break;
816 case 'D':
817 if (strlen(optarg) != 1 || !(optarg[0] >= '5' && optarg[0] <= '8')) {
818 fprintf(stderr, "invalid number of data bits: %s\n", optarg);
819 goto ustusage;
820 } else {
821 datab = optarg[0];
822 }
823 break;
824 case 'e':
825 parity ^= 1; break;
826 case 'o':
827 parity ^= 2; break;
828 case 'h':
829 half ^= 1; break;
830 case 'i':
831 tui = strtoui(optarg, "invalid rx speed: %s\n", 1);
832 if (tui == uintmax)
833 goto ustusage;
834 ispeed = tui;
835 rxspdset = 1;
836 break;
837 case 's':
838 tui = strtoui(optarg, "invalid speed: %s\n", 1);
839 if (tui == uintmax)
840 goto ustusage;
841 ospeed = tui;
842 break;
843 case 'S':
844 stopb ^= 1; break;
845 case 'l':
846 if (devset) {
847 fprintf(stderr, "cannot specify multiple devices\n");
848 goto ustusage;
849 }
850 if (strlen(optarg) > 10) {
851 fprintf(stderr, "device name too long\n");
852 goto ustusage;
853 }
854 if (strchr(optarg, '/')) {
855 strcpy(line, optarg);
856 devset = 1;
857 } else {
858 sprintf(line, "/dev/%s", optarg);
859 devset = 1;
860 }
861 break;
862 case 'm':
863 tui = strtoui(optarg, "invalid number of characters: %s\n", 1);
864 if (tui == uintmax)
865 goto ustusage;
866 minchars = tui;
867 break;
868 case 't':
869 if (troptions(&tropts, optarg))
870 goto ustusage;
871 break;
872 case 'T':
873 if (troptions(&itropts, optarg))
874 goto ustusage;
875 break;
876 case 'v':
877 verbose ^= 1; break;
878 default:
879 ustusage:
880 die(2, "usage: %s [--line|-l line] [--speed|-s #|-#] [--rx-speed|-i #]\n"
881 " [--data-bits|-D #] [--stop-bits|-S]"
882 " [--even|-e] [--odd|-o]\n"
883 " [--hardware-rts-cts|-R]"
884 " [--hardware-dtr-dsr|-r] [--software|-X]\n"
885 " [--delay|-d #] [--min-chars|-m #]"
886 " [--canonical|-c] [--echo|-h]\n"
887 " [--translation|-t tropt]"
888 " [--input-translation|-T tropt]\n"
889 " [--verbose|-v] [--backspace|-b]\n",
890 argv[0]);
891 break;
892 }
893
894
895 }
896
897 if (!rxspdset)
898 ispeed = ospeed;
899
900 if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO))
901 interactive = 1;
902
903 signal(SIGHUP, sighandl);
904 signal(SIGINT, sighandl);
905 signal(SIGQUIT, sighandl);
906 signal(SIGTERM, sighandl);
907
908 fd = openport();
909
910 pthread_t readthread, writethread;
911 pthread_create(&writethread, NULL, writeport, NULL);
912 pthread_create(&readthread, NULL, readport, NULL);
913 }