11 #include <sys/ioctl.h>
14 #include <asm/termbits.h>
15 #include <asm/ioctls.h>
27 #if defined(CCDTR_IFLOW) && defined(CDSR_OFLOW)
28 #define CDTRDSR (CDTR_IFLOW | CDSR_OFLOW)
30 #define IXONXOFF (IXON | IXOFF)
32 static int gettermattr(int dv
, void *strct
);
33 static int settermattr(int dv
, void *strct
);
34 static void settermspd(unsigned int lispeed
, unsigned int lospeed
, void *strct
);
35 static int openport();
36 static unsigned int strtoui(const char *str
, const char *msg
, const unsigned int min
);
37 static int troptions(crlfopt
*options
, const char *str
);
38 static int setroptions();
39 static inline int termchck(const void *term
);
40 static void interchck();
42 static void sighandl(const int signo
);
43 static void die(const int code
, const char *msg
, ...);
44 static void *writeport(void *unused
);
45 static void *readport(void *unused
);
46 static inline void replacechar(char *str
, ssize_t size
, const char find
, const char repl
);
47 static inline ssize_t
addchar(char *str
, int *scratch
, ssize_t size
, const size_t bsize
, const char find
, const char inp
, const int offset
);
48 static inline ssize_t
rmchar(char *str
, int *scratch
, ssize_t size
, const char find
);
49 static void getcmd(int escape
);
52 static struct termios2 cntrl
, origterm
= {0}, newterm
= {0};
54 static struct termios cntrl
, origterm
= {0}, newterm
= {0};
57 static const unsigned int uintmax
= ~(unsigned int)0;
58 static struct timespec wts
;
59 static char *writebuff
= NULL
;
60 static char *readbuff
= NULL
;
61 static int *scratchr
= NULL
;
62 static int *scratchw
= NULL
;
63 static char backspc
= DEL
,tbackspc
= DEL
;
65 static int interactive
= 0;
68 main(int argc
, char **argv
)
70 for (int i
= 1; i
< argc
; i
++ ) {
71 if (argv
[i
][0] == '-' && argv
[i
][1] >= '0' && argv
[i
][1] <= '9') {
73 /* glibc's `asprintf()` won't set the string to NULL on failure automatically */
74 asprintf(&t
, "-s%s", argv
[i
] + 1);
76 fprintf(stderr
, "cannot convert -# to -s#\n");
84 static struct option longopt
[] = {
85 {"line", required_argument
, NULL
, 'l'},
86 {"speed", required_argument
, NULL
, 's'},
87 {"rx-speed", required_argument
, NULL
, 'i'},
88 {"echo", no_argument
, NULL
, 'h'},
89 {"canonical", no_argument
, NULL
, 'c'},
90 {"odd", no_argument
, NULL
, 'o'},
91 {"even", no_argument
, NULL
, 'e'},
92 {"hardware-rts-cts", no_argument
, NULL
, 'R'},
93 {"hardware-dtr-dsr", no_argument
, NULL
, 'r'},
94 {"software", no_argument
, NULL
, 'X'},
95 {"data", required_argument
, NULL
, 'D'},
96 {"delay", required_argument
, NULL
, 'd'},
97 {"min-chars", required_argument
, NULL
, 'm'},
98 {"stop-bits", no_argument
, NULL
, 'S'},
99 {"backspace", no_argument
, NULL
, 'b'},
100 {"translation", required_argument
, NULL
, 't'},
101 {"input-translation", required_argument
, NULL
, 'T'},
102 {"verbose", no_argument
, NULL
, 'v'},
107 int oind
= 0, rxspdset
= 0, devset
= 0, c
;
108 while ((c
= getopt_long(argc
, argv
, "bcd:D:ehi:l:m:oRrs:t:T:vX", longopt
, &oind
)) != -1) {
113 backspace
^= 1; break;
115 canonical
^= 1; break;
125 t
= strtol(optarg
, &endptr
, 10);
126 if (errno
!= 0 || *endptr
!= '\0' || t
< 0) {
127 fprintf(stderr
, "invalid character delay\n");
134 if (strlen(optarg
) != 1 || !(optarg
[0] >= '5' && optarg
[0] <= '8')) {
135 fprintf(stderr
, "invalid number of data bits: %s\n", optarg
);
148 tui
= strtoui(optarg
, "invalid rx speed: %s\n", 1);
155 tui
= strtoui(optarg
, "invalid speed: %s\n", 1);
164 fprintf(stderr
, "cannot specify multiple devices\n");
167 if (strlen(optarg
) > 10) {
168 fprintf(stderr
, "device name too long\n");
171 if (strchr(optarg
, '/')) {
172 strcpy(line
, optarg
);
175 sprintf(line
, "/dev/%s", optarg
);
180 tui
= strtoui(optarg
, "invalid number of characters: %s\n", 1);
186 if (troptions(&tropts
, optarg
))
190 if (troptions(&itropts
, optarg
))
197 die(2, "usage: %s [--line|-l line] [--speed|-s #|-#] [--rx-speed|-i #]\n"
198 " [--data-bits|-D #] [--stop-bits|-S]"
199 " [--even|-e] [--odd|-o]\n"
200 " [--hardware-rts-cts|-R]"
201 " [--hardware-dtr-dsr|-r] [--software|-X]\n"
202 " [--delay|-d #] [--min-chars|-m #]"
203 " [--canonical|-c] [--echo|-h]\n"
204 " [--translation|-t tropt]"
205 " [--input-translation|-T tropt]\n"
206 " [--verbose|-v] [--backspace|-b]\n",
217 if (isatty(STDIN_FILENO
) || isatty(STDOUT_FILENO
))
220 signal(SIGHUP
, sighandl
);
221 signal(SIGINT
, sighandl
);
222 signal(SIGQUIT
, sighandl
);
223 signal(SIGTERM
, sighandl
);
227 pthread_t readthread
, writethread
;
228 pthread_create(&writethread
, NULL
, writeport
, NULL
);
229 pthread_create(&readthread
, NULL
, readport
, NULL
);
233 gettermattr(int dv
, void *strct
)
236 struct termios2
*optst
= (struct termios2
*)strct
;
237 return ioctl(dv
, TCGETS2
, optst
);
239 struct termios
*optst
= (struct termios
*)strct
;
240 return tcgetattr(dv
, optst
);
245 settermattr(int dv
, void *strct
)
248 struct termios2
*optst
= (struct termios2
*)strct
;
249 return ioctl(dv
, TCSETS2
, optst
);
251 struct termios
*optst
= (struct termios
*)strct
;
252 return tcsetattr(dv
, TCSANOW
, optst
);
257 settermspd(unsigned int lispeed
, unsigned int lospeed
, void *strct
)
260 struct termios2
*optst
= (struct termios2
*)strct
;
262 optst
->c_cflag
&= ~CBAUD
;
263 optst
->c_cflag
|= BOTHER
;
264 optst
->c_ispeed
= ispeed
;
265 optst
->c_ospeed
= ospeed
;
267 struct termios
*optst
= (struct termios
*)strct
;
268 cfsetispeed(optst
, ispeed
);
269 cfsetospeed(optst
, ospeed
);
276 if (verbose
) fprintf(stderr
, "opening \"%s\"\n", line
);
278 fd
= open(line
, O_RDWR
| O_NOCTTY
| O_NDELAY
);
280 perror("error opening device");
284 if (verbose
) fprintf(stderr
, "checking if \"%s\" is a TTY\n", line
);
287 die(2, "device \"%s\" is not a TTY\n", line
);
290 flags
= fcntl(fd
, F_GETFL
, 0);
291 /* opened to check with non-blocking mode, now set to blocking */
292 flags
&= ~(O_NONBLOCK
| O_NDELAY
);
293 if (fcntl(fd
, F_SETFL
, flags
) == -1)
294 die(1, "failed to set the device to blocking mode\n");
296 if (gettermattr(fd
, &cntrl
) == -1)
297 die(1, "failed to get device attributes\n");
299 if (verbose
) fprintf(stderr
, "setting baudrate [RX:%u | TX:%u]\n", ispeed
, ospeed
);
301 settermspd(ispeed
, ospeed
, &cntrl
);
302 if (settermattr(fd
, &cntrl
) == -1)
303 die(2,"failed to set baudrate [RX:%u | TX:%u]\n", ispeed
, ospeed
);
306 cntrl
.c_iflag
&= ~(ISTRIP
| BRKINT
);
307 cntrl
.c_cflag
&= ~(PARENB
| PARODD
);
309 if (verbose
) fprintf(stderr
, "setting parity [even: %d, odd: %d]\n", parity
& 1, (parity
& 2) >> 1);
312 cntrl
.c_cflag
|= PARENB
;
314 cntrl
.c_cflag
|= PARENB
| PARODD
;
316 if (settermattr(fd
, &cntrl
) == -1)
317 die(2, "failed to set parity [even: %d, odd: %d]\n", parity
& 1, (parity
& 2) >> 1);
320 /* it's so ugly and beautiful at the same time */
321 int t
= (((hard
& 1) << 1) & (hard
& 2)) >> 1;
322 fprintf(stderr
, "setting flow control [XON/XOFF: %d, RTS/CTS: %d, DTR/DSR: %d, DCD: %d]\n",\
323 soft
, t
^ ((hard
& 1) << 1) >> 1, t
^ (hard
& 2) >> 1, t
);
326 cntrl
.c_cflag
|= CLOCAL
;
327 cntrl
.c_iflag
&= ~IXONXOFF
;
330 cntrl
.c_iflag
|= IXONXOFF
;
333 cntrl
.c_cflag
|= CRTSCTS
;
334 } else if (hard
== 2) {
336 fprintf(stderr
, "DTR/DSR flow control is not supported on this platform\n"
337 "enabling this option does nothing!\n");
339 cntrl
.c_lflag
|= CDTRDSR
;
341 } else if (hard
== 3) {
342 cntrl
.c_cflag
&= ~CLOCAL
;
344 cntrl
.c_lflag
|= CCAR_OFLOW
;
348 if (settermattr(fd
, &cntrl
) == -1)
349 die(2, "failed to set flow control\n");
352 die(2, "failed to set cr-lf translation options\n");
354 if (verbose
) fprintf(stderr
, "setting data bits [%c]\n", datab
);
367 cntrl
.c_cflag
&= ~CSIZE
;
370 if (settermattr(fd
, &cntrl
) == -1)
371 die(2, "failed to set data bits [%c]\n", datab
);
373 if (verbose
) fprintf(stderr
, "setting stop bits [%d]\n", stopb
+1);
376 cntrl
.c_cflag
|= CSTOPB
;
378 cntrl
.c_cflag
&= ~CSTOPB
;
380 if (settermattr(fd
, &cntrl
) == -1)
381 die(1, "failed to set stop bits [%d]\n", stopb
+1);
384 ioctl(fd
, TCFLSH
, TCIOFLUSH
);
386 tcflush(fd
, TCIOFLUSH
);
392 strtoui(const char *str
, const char *msg
, const unsigned int min
)
397 t
= strtol(str
, &endptr
, 10);
398 if (errno
!= 0 || *endptr
!= '\0' || t
< min
) {
399 fprintf(stderr
, msg
, str
);
403 * conversion like this results in 'undefined behavior' according to C spec,
404 * but almost all compilers will just truncate the value so it's OK
406 return (unsigned int)t
;
407 /* termios's `speed_t` is a typedef for `unsigned int` */
411 troptions(crlfopt
*options
, const char *str
)
415 if (strcmp(str
, "lf-in-cr") == 0) {
416 options
->lfincr
= !options
->lfincr
; return 0;
417 } else if (strcmp(str
, "lf-to-cr") == 0) {
418 options
->lftocr
= !options
->lftocr
; return 0;
424 if (strcmp(str
, "cr-in-lf") == 0) {
425 options
->crinlf
= !options
->crinlf
; return 0;
426 } else if (strcmp(str
, "cr-to-lf") == 0) {
427 options
->crtolf
= !options
->crtolf
; return 0;
433 if (strcmp(str
, "no-lf") == 0) {
434 options
->nolf
= !options
->nolf
; return 0;
435 } else if (strcmp(str
, "no-cr") == 0) {
436 options
->nocr
= !options
->nocr
; return 0;
443 fprintf(stderr
, "invalid translation option: %s\n", str
);
452 cntrl
.c_iflag
|= ICRNL
;
454 cntrl
.c_iflag
&= ~ICRNL
;
457 cntrl
.c_iflag
|= INLCR
;
459 cntrl
.c_iflag
&= ~INLCR
;
462 cntrl
.c_oflag
|= OCRNL
;
464 cntrl
.c_oflag
&= ~OCRNL
;
467 cntrl
.c_oflag
|= ONLRET
;
469 cntrl
.c_oflag
&= ~ONLRET
;
472 cntrl
.c_oflag
|= ONLCR
;
474 cntrl
.c_oflag
&= ~ONLCR
;
476 return settermattr(fd
, &cntrl
);
480 termchck(const void *term
)
483 struct termios2
*iterm
= (struct termios2
*)term
;
484 #define TERMIOS_STRUCT termios2
486 struct termios
*iterm
= (struct termios
*)term
;
487 #define TERMIOS_STRUCT termios
489 for (size_t i
= 0; i
< sizeof(struct TERMIOS_STRUCT
); i
++)
490 if (((char*)iterm
)[i
] != 0)
497 writeport(void *unused
)
500 ts
.tv_sec
= swritedelay
;
501 ts
.tv_nsec
= nswritedelay
;
503 wts
.tv_nsec
= chardelay
;
504 writebuff
= malloc(wbuffsz
* sizeof(char));
505 scratchw
= malloc(scratchwsz
* sizeof(int));
507 if (writebuff
== NULL
)
508 die(1, "buffer allocation failed\n");
509 if (scratchw
== NULL
)
510 die(1, "scratch buffer allocation failed\n");
515 ssize_t inln
= read(STDIN_FILENO
, writebuff
, wbuffsz
- 1);
521 if (writebuff
[0] == escapechar
) {
528 if (backspc
!= tbackspc
)
529 replacechar(writebuff
, inln
, backspc
, tbackspc
);
532 inln
= rmchar(writebuff
, scratchw
, inln
, CR
);
534 inln
= rmchar(writebuff
, scratchw
, inln
, LF
);
536 inln
= addchar(writebuff
, scratchw
, inln
, wbuffsz
, CR
, LF
, 1);
539 for (int i
= 0; i
<= inln
; i
++) {
540 write(fd
, &writebuff
[i
], 1);
541 nanosleep(&wts
, NULL
);
544 write(fd
, writebuff
, 1);
547 nanosleep(&ts
, NULL
);
553 readport(void *unused
)
556 ts
.tv_sec
= sreaddelay
;
557 ts
.tv_nsec
= nsreaddelay
;
558 readbuff
= malloc(rbuffsz
* sizeof(char));
559 scratchr
= malloc(scratchrsz
* sizeof(int));
561 if (readbuff
== NULL
)
562 die(1, "buffer allocation failed\n");
563 if (scratchr
== NULL
)
564 die(1, "scratch buffer allocation failed\n");
568 /* disable stdout buffering */
569 setvbuf(stdout
, NULL
, _IONBF
, 0);
572 ssize_t outln
= read(fd
, readbuff
, rbuffsz
- 1);
575 outln
= rmchar(readbuff
, scratchw
, outln
, CR
);
577 outln
= rmchar(readbuff
, scratchw
, outln
, LF
);
579 outln
= addchar(readbuff
, scratchw
, outln
, rbuffsz
, LF
, CR
, -1);
581 outln
= addchar(readbuff
, scratchw
, outln
, rbuffsz
, CR
, LF
, 1);
583 write(STDOUT_FILENO
, readbuff
, outln
);
585 nanosleep(&ts
, NULL
);
587 if (isatty(STDIN_FILENO
))
588 settermattr(STDIN_FILENO
, &origterm
);
592 inline void __attribute__((hot
))
593 replacechar(char *str
, ssize_t size
, const char find
, const char repl
)
595 for (int i
= 0; i
< size
; i
++)
600 /* TODO: optimize the function and allow for offsets greater than 1 */
601 inline ssize_t
__attribute__((hot
))
602 addchar(char *str
, int *scratch
, ssize_t size
, const size_t bsize
,const char find
, const char inp
, const int offset
)
605 for (int i
= 0; i
< size
; i
++) {
606 if (str
[i
] == find
) {
613 if ((size
+ c
) > bsize
)
616 if (scratch
[0] == 0 && offset
< 0) {
617 memmove(&str
[0]+1, &str
[0], size
);
620 for (int i
= c
; i
> 0; i
--) {
621 for (int s
= size
; s
>= scratch
[i
]; s
--)
622 str
[s
+ offset
] = str
[s
];
623 str
[scratch
[i
]] = inp
;
628 inline ssize_t
__attribute__((hot
))
629 rmchar(char *str
, int *scratch
, ssize_t size
, const char find
)
632 for (int i
= 0; i
< size
; i
++) {
633 if (str
[i
] == find
) {
641 for (int i
= c
; i
> 0; i
--)
642 for (int s
= size
; s
>= scratch
[i
]; s
--)
652 if (gettermattr(STDIN_FILENO
, &origterm
) < 0 )
653 die(1, "failed to get terminal attributes\n");
658 newterm
.c_lflag
&= ~ECHO
;
660 newterm
.c_lflag
&= ~ICANON
;
663 newterm
.c_iflag
|= IXONXOFF
;
665 newterm
.c_iflag
|= INLCR
;
666 newterm
.c_cc
[VMIN
] = minchars
;
667 newterm
.c_cc
[VINTR
] = _POSIX_VDISABLE
;
668 newterm
.c_cc
[VSUSP
] = _POSIX_VDISABLE
;
669 newterm
.c_cc
[VQUIT
] = _POSIX_VDISABLE
;
676 backspc
= origterm
.c_cc
[VERASE
];
678 if (settermattr(STDOUT_FILENO
, &newterm
) < 0)
679 die(1, "failed to set terminal attributes\n");
687 if ((!half
|| !canonical
) && interactive
) {
688 newterm
.c_lflag
|= ECHO
;
689 newterm
.c_lflag
|= ICANON
;
690 if (settermattr(STDIN_FILENO
, &newterm
) < 0)
691 fprintf(stderr
, "failed to enable echo and/or canonical mode\n");
703 if (isatty(STDIN_FILENO
) || isatty(STDOUT_FILENO
))
709 cmdchar
= writebuff
[0];
711 cmdchar
= writebuff
[1];
722 backspace
= !backspace
;
726 newterm
.c_lflag
&= ~ECHO
;
728 newterm
.c_lflag
|= ECHO
;
730 if (settermattr(STDOUT_FILENO
, &newterm
) < 0)
731 die(1, "failed to set terminal attributes\n");
735 newterm
.c_lflag
&= ~ICANON
;
737 newterm
.c_lflag
|= ICANON
;
738 canonical
= !canonical
;
739 if (settermattr(STDOUT_FILENO
, &newterm
) < 0)
740 die(1, "failed to set terminal attributes\n");
744 if (fgets(ttr
, sizeof(ttr
), stdin
) != NULL
) {
745 replacechar(ttr
, 63, LF
, '\0');
747 int wfd
= open(ttr
, O_RDONLY
);
749 perror("error opening file");
753 while ((frln
= read(wfd
, writebuff
, wbuffsz
- 1)) > 0) {
754 for (int i
= 0; i
<= frln
; i
++) {
755 write(fd
, &writebuff
[i
], 1);
756 nanosleep(&wts
, NULL
);
763 if (fgets(ttr
, sizeof(ttr
), stdin
) != NULL
) {
764 replacechar(ttr
, 63, LF
, '\0');
765 tspd
= strtoui(ttr
, "invalid speed\n", 0);
766 if (tspd
!= uintmax
) {
767 ospeed
= ispeed
= tspd
;
768 settermspd(ispeed
, ospeed
, &cntrl
);
769 if (settermattr(fd
, &cntrl
) != 0) {
770 fprintf(stderr
, "failed to set baudrate [RX:%u | TX:%u]\n", ispeed
, ospeed
);
777 if (fgets(ttr
, sizeof(ttr
), stdin
) != NULL
) {
778 replacechar(ttr
, 63, LF
, '\0');
779 chardelay
= strtoui(ttr
, "invalid delay\n", 0);
780 if (chardelay
!= uintmax
) {
782 wts
.tv_nsec
= chardelay
;
788 fprintf(stderr
, "additional output translation option: ");
789 if (fgets(ttr
, sizeof(ttr
), stdin
) != NULL
) {
790 replacechar(ttr
, 63, LF
, '\0');
791 if(troptions(&tropts
, ttr
))
794 fprintf(stderr
, "could not set new options\n");
800 printf("additional input translation option: ");
801 if (fgets(ttr
, sizeof(ttr
), stdin
) != NULL
) {
802 replacechar(ttr
, 63, LF
, '\0');
803 if (troptions(&itropts
, ttr
))
806 fprintf(stderr
, "could not set new options\n");
809 if (!half
&& interactive
)
810 newterm
.c_lflag
&= ~ECHO
;
811 if (!canonical
&& interactive
)
812 newterm
.c_lflag
&= ~ICANON
;
813 settermattr(STDIN_FILENO
, &newterm
);
818 ts
.tv_sec
= spulsedelay
;
819 ts
.tv_nsec
= nspulsedelay
;
821 if (ioctl(fd
, TIOCMGET
, &st
) != 0) {
822 fprintf(stderr
, "failed to get port status\n");
826 if (ioctl(fd
, TIOCMSET
, &st
) != 0) {
827 fprintf(stderr
, "failed to set DTR [assertion]\n");
831 nanosleep(&ts
, NULL
);
834 if (ioctl(fd
, TIOCMSET
, &st
) != 0)
835 fprintf(stderr
, "failed to set DTR [negation]\n");
842 fprintf(stderr
, "not a valid command [%c]\n", cmdchar
);
849 sighandl(const int signo
)
851 die(128 + signo
, "\nrecieved signal [%d], exiting\n", signo
);
855 die(const int code
, const char *msg
, ...)
870 if (termchck(&newterm
))
871 settermattr(STDIN_FILENO
, &origterm
);
874 vfprintf(stderr
, msg
, fpa
);