11 #include <sys/ioctl.h>
14 #include <asm/termbits.h>
15 #include <asm/ioctls.h>
26 #if defined(CCDTR_IFLOW) && defined(CDSR_OFLOW)
27 #define CDTRDSR (CDTR_IFLOW | CDSR_OFLOW)
29 #define IXONXOFF (IXON | IXOFF)
31 static int gettermattr(int dv
, void *strct
);
32 static int settermattr(int dv
, void *strct
);
33 static void settermspd(unsigned int lispeed
, unsigned int lospeed
, void *strct
);
34 static int openport();
35 static unsigned int strtoui(const char *str
, const char *msg
, const unsigned int min
);
36 static int troptions(crlfopt
*options
, const char *str
);
37 static int setroptions();
38 static inline int termchck(const void *term
);
39 static void interchck();
41 static void sighandl(const int signo
);
42 static void die(const int code
, const char *msg
, ...);
43 static void *writeport(void *unused
);
44 static void *readport(void *unused
);
45 static inline void replacechar(char *str
, ssize_t size
, const char find
, const char repl
);
46 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
);
47 static inline ssize_t
rmchar(char *str
, int *scratch
, ssize_t size
, const char find
);
48 static void getcmd(int escape
);
51 static struct termios2 cntrl
, origterm
= {0}, newterm
= {0};
53 static struct termios cntrl
, origterm
= {0}, newterm
= {0};
56 static const unsigned int uintmax
= ~(unsigned int)0;
57 static struct timespec wts
;
58 static char *writebuff
= NULL
;
59 static char *readbuff
= NULL
;
60 static int *scratchr
= NULL
;
61 static int *scratchw
= NULL
;
62 static char backspc
= DEL
,tbackspc
= DEL
;
64 static int interactive
= 0;
67 main(int argc
, char **argv
)
69 for (int i
= 1; i
< argc
; i
++ ) {
70 if (argv
[i
][0] == '-' && argv
[i
][1] >= '0' && argv
[i
][1] <= '9') {
72 /* glibc's `asprintf()` won't set the string to NULL on failure automatically */
73 asprintf(&t
, "-s%s", argv
[i
] + 1);
75 fprintf(stderr
, "cannot convert -# to -s#\n");
83 static struct option longopt
[] = {
84 {"line", required_argument
, NULL
, 'l'},
85 {"speed", required_argument
, NULL
, 's'},
86 {"rx-speed", required_argument
, NULL
, 'i'},
87 {"echo", no_argument
, NULL
, 'h'},
88 {"canonical", no_argument
, NULL
, 'c'},
89 {"odd", no_argument
, NULL
, 'o'},
90 {"even", no_argument
, NULL
, 'e'},
91 {"hardware-rtscts", no_argument
, NULL
, 'R'},
92 {"hardware-dsrdtr", no_argument
, NULL
, 'r'},
93 {"software", no_argument
, NULL
, 'X'},
94 {"data", required_argument
, NULL
, 'D'},
95 {"delay", required_argument
, NULL
, 'd'},
96 {"min-chars", required_argument
, NULL
, 'm'},
97 {"stop-bits", no_argument
, NULL
, 'S'},
98 {"backspace", no_argument
, NULL
, 'b'},
99 {"translation", required_argument
, NULL
, 't'},
100 {"input-translation", required_argument
, NULL
, 'T'},
101 {"verbose", no_argument
, NULL
, 'v'},
106 int oind
= 0, rxspdset
= 0, devset
= 0, c
;
107 while ((c
= getopt_long(argc
, argv
, "bcd:D:ehi:l:m:oRrs:t:T:vX", longopt
, &oind
)) != -1) {
112 backspace
^= 1; break;
114 canonical
^= 1; break;
123 long t
= strtol(optarg
, &endptr
, 10);
124 if (errno
!= 0 || *endptr
!= '\0' || t
< 0) {
125 fprintf(stderr
, "invalid character delay\n");
132 if (strlen(optarg
) != 1 || !(optarg
[0] >= '5' && optarg
[0] <= '8')) {
133 fprintf(stderr
, "invalid number of data bits: %s\n", optarg
);
146 tui
= strtoui(optarg
, "invalid rx speed: %s\n", 1);
153 tui
= strtoui(optarg
, "invalid speed: %s\n", 1);
162 fprintf(stderr
, "cannot specify multiple devices\n");
165 if (strlen(optarg
) > 10) {
166 fprintf(stderr
, "device name too long\n");
169 if (strchr(optarg
, '/')) {
170 strcpy(line
, optarg
);
173 sprintf(line
, "/dev/%s", optarg
);
178 tui
= strtoui(optarg
, "invalid number of characters: %s\n", 1);
184 if (troptions(&tropts
, optarg
))
188 if (troptions(&itropts
, optarg
))
195 die(2, "usage: %s [--line|-l line] [--speed|-s #|-#] [--rx-speed|-i #]\n"
196 " [--data-bits|-D #] [--stop-bits|-S]"
197 " [--even|-e] [--odd|-o]\n"
198 " [--hardware-rtscts|-R]"
199 " [--hardware-dsrdtr|-r] [--software|-X]\n"
200 " [--delay|-d #] [--min-chars|-m #]"
201 " [--canonical|-c] [--echo|-h]\n"
202 " [--translation|-t tropt]"
203 " [--input-translation|-T tropt]\n"
204 " [--verbose|-v] [--backspace|-b]\n",
215 if (isatty(STDIN_FILENO
) || isatty(STDOUT_FILENO
))
218 signal(SIGHUP
, sighandl
);
219 signal(SIGINT
, sighandl
);
220 signal(SIGQUIT
, sighandl
);
221 signal(SIGTERM
, sighandl
);
225 pthread_t readthread
, writethread
;
226 pthread_create(&writethread
, NULL
, writeport
, NULL
);
227 pthread_create(&readthread
, NULL
, readport
, NULL
);
231 gettermattr(int dv
, void *strct
)
234 struct termios2
*optst
= (struct termios2
*)strct
;
235 return ioctl(dv
, TCGETS2
, optst
);
237 struct termios
*optst
= (struct termios
*)strct
;
238 return tcgetattr(dv
, optst
);
243 settermattr(int dv
, void *strct
)
246 struct termios2
*optst
= (struct termios2
*)strct
;
247 return ioctl(dv
, TCSETS2
, optst
);
249 struct termios
*optst
= (struct termios
*)strct
;
250 return tcsetattr(dv
, TCSANOW
, optst
);
255 settermspd(unsigned int lispeed
, unsigned int lospeed
, void *strct
)
258 struct termios2
*optst
= (struct termios2
*)strct
;
260 optst
->c_cflag
&= ~CBAUD
;
261 optst
->c_cflag
|= BOTHER
;
262 optst
->c_ispeed
= ispeed
;
263 optst
->c_ospeed
= ospeed
;
265 struct termios
*optst
= (struct termios
*)strct
;
266 cfsetispeed(optst
, ispeed
);
267 cfsetospeed(optst
, ospeed
);
274 if (verbose
) fprintf(stderr
, "opening \"%s\"\n", line
);
276 fd
= open(line
, O_RDWR
| O_NOCTTY
| O_NDELAY
);
278 perror("error opening device");
282 if (verbose
) fprintf(stderr
, "checking if \"%s\" is a TTY\n", line
);
285 die(2, "device \"%s\" is not a TTY\n", line
);
287 int flags
= fcntl(fd
, F_GETFL
, 0);
288 flags
&= ~(O_NONBLOCK
| O_NDELAY
); /* opened to check with non-blocking mode, now set to blocking */
289 if (fcntl(fd
, F_SETFL
, flags
) == -1)
290 die(1, "failed to set the device to blocking mode\n");
292 if (gettermattr(fd
, &cntrl
) == -1)
293 die(1, "failed to get device attributes\n");
295 if (verbose
) fprintf(stderr
, "setting baudrate [RX:%u | TX:%u]\n", ispeed
, ospeed
);
297 settermspd(ispeed
, ospeed
, &cntrl
);
298 if (settermattr(fd
, &cntrl
) == -1)
299 die(2,"failed to set baudrate [RX:%u | TX:%u]\n", ispeed
, ospeed
);
302 cntrl
.c_iflag
&= ~(ISTRIP
| BRKINT
);
303 cntrl
.c_cflag
&= ~(PARENB
| PARODD
);
305 if (verbose
) fprintf(stderr
, "setting parity [even: %d, odd: %d]\n", parity
& 1, (parity
& 2) >> 1);
308 cntrl
.c_cflag
|= PARENB
;
310 cntrl
.c_cflag
|= PARENB
| PARODD
;
312 if (settermattr(fd
, &cntrl
) == -1)
313 die(2, "failed to set parity [even: %d, odd: %d]\n", parity
& 1, (parity
& 2) >> 1);
316 /* it's so ugly and beautiful at the same time */
317 int t
= (((hard
& 1) << 1) & (hard
& 2)) >> 1;
318 fprintf(stderr
, "setting flow control [XON/XOFF: %d, RTS/CTS: %d, DTR/DSR: %d, DCD: %d]\n",\
319 soft
, t
^ ((hard
& 1) << 1) >> 1, t
^ (hard
& 2) >> 1, t
);
322 cntrl
.c_cflag
|= CLOCAL
;
323 cntrl
.c_iflag
&= ~IXONXOFF
;
326 cntrl
.c_iflag
|= IXONXOFF
;
329 cntrl
.c_cflag
|= CRTSCTS
;
330 } else if (hard
== 2) {
332 fprintf(stderr
, "DTR/DSR flow control is not supported on this platform\n"
333 "enabling this option does nothing!\n");
335 cntrl
.c_lflag
|= CDTRDSR
;
337 } else if (hard
== 3) {
338 cntrl
.c_cflag
&= ~CLOCAL
;
340 cntrl
.c_lflag
|= CCAR_OFLOW
;
344 if (settermattr(fd
, &cntrl
) == -1)
345 die(2, "failed to set flow control\n");
348 die(2, "failed to set cr-lf translation options\n");
350 if (verbose
) fprintf(stderr
, "setting data bits [%c]\n", datab
);
363 cntrl
.c_cflag
&= ~CSIZE
;
366 if (settermattr(fd
, &cntrl
) == -1)
367 die(2, "failed to set data bits [%c]\n", datab
);
369 if (verbose
) fprintf(stderr
, "setting stop bits [%d]\n", stopb
+1);
372 cntrl
.c_cflag
|= CSTOPB
;
374 cntrl
.c_cflag
&= ~CSTOPB
;
376 if (settermattr(fd
, &cntrl
) == -1)
377 die(1, "failed to set stop bits [%d]\n", stopb
+1);
380 ioctl(fd
, TCFLSH
, TCIOFLUSH
);
382 tcflush(fd
, TCIOFLUSH
);
388 strtoui(const char *str
, const char *msg
, const unsigned int min
)
393 t
= strtol(str
, &endptr
, 10);
394 if (errno
!= 0 || *endptr
!= '\0' || t
< min
) {
395 fprintf(stderr
, msg
, str
);
399 * conversion like this results in 'undefined behavior' according to C spec,
400 * but almost all compilers will just truncate the value so it's OK
402 return (unsigned int)t
;
403 /* termios's `speed_t` is a typedef for `unsigned int` */
407 troptions(crlfopt
*options
, const char *str
)
411 if (strcmp(str
, "lf-in-cr") == 0) {
412 options
->lfincr
= !options
->lfincr
; return 0;
413 } else if (strcmp(str
, "lf-to-cr") == 0) {
414 options
->lftocr
= !options
->lftocr
; return 0;
419 if (strcmp(str
, "cr-in-lf") == 0) {
420 options
->crinlf
= !options
->crinlf
; return 0;
421 } else if (strcmp(str
, "cr-to-lf") == 0) {
422 options
->crtolf
= !options
->crtolf
; return 0;
427 if (strcmp(str
, "no-lf") == 0) {
428 options
->nolf
= !options
->nolf
; return 0;
429 } else if (strcmp(str
, "no-cr") == 0) {
430 options
->nocr
= !options
->nocr
; return 0;
436 fprintf(stderr
, "invalid translation option: %s\n", str
);
445 cntrl
.c_iflag
|= ICRNL
;
447 cntrl
.c_iflag
&= ~ICRNL
;
450 cntrl
.c_iflag
|= INLCR
;
452 cntrl
.c_iflag
&= ~INLCR
;
455 cntrl
.c_oflag
|= OCRNL
;
457 cntrl
.c_oflag
&= ~OCRNL
;
460 cntrl
.c_oflag
|= ONLRET
;
462 cntrl
.c_oflag
&= ~ONLRET
;
465 cntrl
.c_oflag
|= ONLCR
;
467 cntrl
.c_oflag
&= ~ONLCR
;
469 return settermattr(fd
, &cntrl
);
473 termchck(const void *term
)
476 struct termios2
*iterm
= (struct termios2
*)term
;
477 #define TERMIOS_STRUCT termios2
479 struct termios
*iterm
= (struct termios
*)term
;
480 #define TERMIOS_STRUCT termios
482 for (size_t i
= 0; i
< sizeof(struct TERMIOS_STRUCT
); i
++) {
483 if (((char*)iterm
)[i
] != 0)
490 writeport(void *unused
)
493 ts
.tv_sec
= swritedelay
;
494 ts
.tv_nsec
= nswritedelay
;
496 wts
.tv_nsec
= chardelay
;
497 writebuff
= malloc(wbuffsz
* sizeof(char));
498 scratchw
= malloc(scratchwsz
* sizeof(int));
500 if (writebuff
== NULL
)
501 die(1, "buffer allocation failed\n");
502 if (scratchw
== NULL
)
503 die(1, "scratch buffer allocation failed\n");
508 ssize_t inln
= read(STDIN_FILENO
, writebuff
, wbuffsz
- 1);
514 if (writebuff
[0] == escapechar
) {
521 if (backspc
!= tbackspc
)
522 replacechar(writebuff
, inln
, backspc
, tbackspc
);
525 inln
= rmchar(writebuff
, scratchw
, inln
, CR
);
527 inln
= rmchar(writebuff
, scratchw
, inln
, LF
);
529 inln
= addchar(writebuff
, scratchw
, inln
, wbuffsz
, CR
, LF
, 1);
532 for (int i
= 0; i
<= inln
; i
++) {
533 write(fd
, &writebuff
[i
], 1);
534 nanosleep(&wts
, NULL
);
538 write(fd
, writebuff
, 1);
541 nanosleep(&ts
, NULL
);
547 readport(void *unused
)
550 ts
.tv_sec
= sreaddelay
;
551 ts
.tv_nsec
= nsreaddelay
;
552 readbuff
= malloc(rbuffsz
* sizeof(char));
553 scratchr
= malloc(scratchrsz
* sizeof(int));
555 if (readbuff
== NULL
)
556 die(1, "buffer allocation failed\n");
557 if (scratchr
== NULL
)
558 die(1, "scratch buffer allocation failed\n");
562 /* disable stdout buffering */
563 setvbuf(stdout
, NULL
, _IONBF
, 0);
566 ssize_t outln
= read(fd
, readbuff
, rbuffsz
- 1);
569 outln
= rmchar(readbuff
, scratchw
, outln
, CR
);
571 outln
= rmchar(readbuff
, scratchw
, outln
, LF
);
573 outln
= addchar(readbuff
, scratchw
, outln
, rbuffsz
, LF
, CR
, -1);
575 outln
= addchar(readbuff
, scratchw
, outln
, rbuffsz
, CR
, LF
, 1);
577 write(STDOUT_FILENO
, readbuff
, outln
);
579 nanosleep(&ts
, NULL
);
581 if (isatty(STDIN_FILENO
))
582 settermattr(STDIN_FILENO
, &origterm
);
586 inline void __attribute__((hot
))
587 replacechar(char *str
, ssize_t size
, const char find
, const char repl
)
589 for (int i
= 0; i
< size
; i
++) {
594 /* TODO: optimize the function and allow for offsets greater than 1 */
595 inline ssize_t
__attribute__((hot
))
596 addchar(char *str
, int *scratch
, ssize_t size
, const size_t bsize
,const char find
, const char inp
, const int offset
)
599 for (int i
= 0; i
< size
; i
++) {
600 if (str
[i
] == find
) {
607 if ((size
+ c
) > bsize
)
610 if (scratch
[0] == 0 && offset
< 0) {
611 memmove(&str
[0]+1, &str
[0], size
);
614 for (int i
= c
; i
> 0; i
--) {
615 for (int s
= size
; s
>= scratch
[i
]; s
--)
616 str
[s
+ offset
] = str
[s
];
617 str
[scratch
[i
]] = inp
;
622 inline ssize_t
__attribute__((hot
))
623 rmchar(char *str
, int *scratch
, ssize_t size
, const char find
)
626 for (int i
= 0; i
< size
; i
++) {
627 if (str
[i
] == find
) {
634 for (int i
= c
; i
> 0; i
--) {
635 for (int s
= size
; s
>= scratch
[i
]; s
--)
645 if (gettermattr(STDIN_FILENO
, &origterm
) < 0 )
646 die(1, "failed to get terminal attributes\n");
651 newterm
.c_lflag
&= ~ECHO
;
653 newterm
.c_lflag
&= ~ICANON
;
656 newterm
.c_iflag
|= IXONXOFF
;
658 newterm
.c_iflag
|= INLCR
;
659 newterm
.c_cc
[VMIN
] = minchars
;
660 newterm
.c_cc
[VINTR
] = _POSIX_VDISABLE
;
661 newterm
.c_cc
[VSUSP
] = _POSIX_VDISABLE
;
662 newterm
.c_cc
[VQUIT
] = _POSIX_VDISABLE
;
669 backspc
= origterm
.c_cc
[VERASE
];
671 if (settermattr(STDOUT_FILENO
, &newterm
) < 0)
672 die(1, "failed to set terminal attributes\n");
680 if ((!half
|| !canonical
) && interactive
) {
681 newterm
.c_lflag
|= ECHO
;
682 newterm
.c_lflag
|= ICANON
;
683 if (settermattr(STDIN_FILENO
, &newterm
) < 0)
684 fprintf(stderr
, "failed to enable echo and/or canonical mode\n");
696 if (isatty(STDIN_FILENO
) || isatty(STDOUT_FILENO
))
702 cmdchar
= writebuff
[0];
704 cmdchar
= writebuff
[1];
715 backspace
= !backspace
;
719 newterm
.c_lflag
&= ~ECHO
;
721 newterm
.c_lflag
|= ECHO
;
723 if (settermattr(STDOUT_FILENO
, &newterm
) < 0)
724 die(1, "failed to set terminal attributes\n");
728 newterm
.c_lflag
&= ~ICANON
;
730 newterm
.c_lflag
|= ICANON
;
731 canonical
= !canonical
;
732 if (settermattr(STDOUT_FILENO
, &newterm
) < 0)
733 die(1, "failed to set terminal attributes\n");
736 if (!half
|| !canonical
) {
737 newterm
.c_lflag
|= ECHO
;
738 newterm
.c_lflag
|= ICANON
;
739 settermattr(STDIN_FILENO
, &newterm
);
741 if (fgets(ttr
, sizeof(ttr
), stdin
) != NULL
) {
742 replacechar(ttr
, 63, LF
, '\0');
744 int wfd
= open(ttr
, O_RDONLY
);
746 perror("error opening file");
750 while ((frln
= read(wfd
, writebuff
, wbuffsz
- 1)) > 0) {
751 for (int i
= 0; i
<= frln
; i
++) {
752 write(fd
, &writebuff
[i
], 1);
753 nanosleep(&wts
, NULL
);
760 if (fgets(ttr
, sizeof(ttr
), stdin
) != NULL
) {
761 replacechar(ttr
, 63, LF
, '\0');
762 tspd
= strtoui(ttr
, "invalid speed\n", 0);
763 if (tspd
!= uintmax
) {
764 ospeed
= ispeed
= tspd
;
765 settermspd(ispeed
, ospeed
, &cntrl
);
766 if (settermattr(fd
, &cntrl
) != 0) {
767 fprintf(stderr
, "failed to set baudrate [RX:%u | TX:%u]\n", ispeed
, ospeed
);
774 if (fgets(ttr
, sizeof(ttr
), stdin
) != NULL
) {
775 replacechar(ttr
, 63, LF
, '\0');
776 chardelay
= strtoui(ttr
, "invalid delay\n", 0);
777 if (chardelay
!= uintmax
) {
779 wts
.tv_nsec
= chardelay
;
785 fprintf(stderr
, "additional output translation option: ");
786 if (fgets(ttr
, sizeof(ttr
), stdin
) != NULL
) {
787 replacechar(ttr
, 63, LF
, '\0');
788 if(troptions(&tropts
, ttr
))
791 fprintf(stderr
, "could not set new options\n");
797 printf("additional input translation option: ");
798 if (fgets(ttr
, sizeof(ttr
), stdin
) != NULL
) {
799 replacechar(ttr
, 63, LF
, '\0');
800 if (troptions(&itropts
, ttr
))
803 fprintf(stderr
, "could not set new options\n");
806 if (!half
&& interactive
)
807 newterm
.c_lflag
&= ~ECHO
;
808 if (!canonical
&& interactive
)
809 newterm
.c_lflag
&= ~ICANON
;
810 settermattr(STDIN_FILENO
, &newterm
);
815 ts
.tv_sec
= spulsedelay
;
816 ts
.tv_nsec
= nspulsedelay
;
818 if (ioctl(fd
, TIOCMGET
, &st
) != 0) {
819 fprintf(stderr
, "failed to get port status\n");
823 if (ioctl(fd
, TIOCMSET
, &st
) != 0) {
824 fprintf(stderr
, "failed to set DTR [assertion]\n");
827 nanosleep(&ts
, NULL
);
830 if (ioctl(fd
, TIOCMSET
, &st
) != 0) {
831 fprintf(stderr
, "failed to set DTR [negation]\n");
839 fprintf(stderr
, "not a valid command [%c]\n", cmdchar
);
846 sighandl(const int signo
)
848 die(128 + signo
, "\nrecieved signal [%d], exiting\n", signo
);
852 die(const int code
, const char *msg
, ...)
867 if (termchck(&newterm
))
868 settermattr(STDIN_FILENO
, &origterm
);
871 vfprintf(stderr
, msg
, fpa
);