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)
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();
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
, ...);
59 static struct termios2 cntrl
, origterm
= {0}, newterm
= {0};
61 static struct termios cntrl
, origterm
= {0}, newterm
= {0};
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
;
72 static int interactive
= 0;
75 gettermattr(int dv
, void *strct
)
78 struct termios2
*optst
= (struct termios2
*)strct
;
79 return ioctl(dv
, TCGETS2
, optst
);
81 struct termios
*optst
= (struct termios
*)strct
;
82 return tcgetattr(dv
, optst
);
87 settermattr(int dv
, const void *strct
)
90 struct termios2
*optst
= (struct termios2
*)strct
;
91 return ioctl(dv
, TCSETS2
, optst
);
93 struct termios
*optst
= (struct termios
*)strct
;
94 return tcsetattr(dv
, TCSANOW
, optst
);
99 settermspd(unsigned int lispeed
, unsigned int lospeed
, const void *strct
)
102 struct termios2
*optst
= (struct termios2
*)strct
;
104 optst
->c_cflag
&= ~CBAUD
;
105 optst
->c_cflag
|= BOTHER
;
106 optst
->c_ispeed
= ispeed
;
107 optst
->c_ospeed
= ospeed
;
109 struct termios
*optst
= (struct termios
*)strct
;
110 cfsetispeed(optst
, ispeed
);
111 cfsetospeed(optst
, ospeed
);
118 if (verbose
) fprintf(stderr
, "opening \"%s\"\n", line
);
120 fd
= open(line
, O_RDWR
| O_NOCTTY
| O_NDELAY
);
122 perror("error opening device");
126 if (verbose
) fprintf(stderr
, "checking if \"%s\" is a TTY\n", line
);
129 die(2, "device \"%s\" is not a TTY\n", line
);
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");
138 if (gettermattr(fd
, &cntrl
) == -1)
139 die(1, "failed to get device attributes\n");
141 if (verbose
) fprintf(stderr
, "setting baudrate [RX:%u | TX:%u]\n", ispeed
, ospeed
);
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
);
148 cntrl
.c_iflag
&= ~(ISTRIP
| BRKINT
);
149 cntrl
.c_cflag
&= ~(PARENB
| PARODD
);
151 if (verbose
) fprintf(stderr
, "setting parity [even: %d, odd: %d]\n", parity
& 1, (parity
& 2) >> 1);
154 cntrl
.c_cflag
|= PARENB
;
156 cntrl
.c_cflag
|= PARENB
| PARODD
;
158 if (settermattr(fd
, &cntrl
) == -1)
159 die(2, "failed to set parity [even: %d, odd: %d]\n", parity
& 1, (parity
& 2) >> 1);
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
);
168 cntrl
.c_cflag
|= CLOCAL
;
169 cntrl
.c_iflag
&= ~IXONXOFF
;
172 cntrl
.c_iflag
|= IXONXOFF
;
175 cntrl
.c_cflag
|= CRTSCTS
;
176 } else if (hard
== 2) {
178 fprintf(stderr
, "DTR/DSR flow control is not supported on this platform\n"
179 "enabling this option does nothing!\n");
181 cntrl
.c_lflag
|= CDTRDSR
;
183 } else if (hard
== 3) {
184 cntrl
.c_cflag
&= ~CLOCAL
;
186 cntrl
.c_lflag
|= CCAR_OFLOW
;
190 if (settermattr(fd
, &cntrl
) == -1)
191 die(2, "failed to set flow control\n");
194 die(2, "failed to set cr-lf translation options\n");
196 if (verbose
) fprintf(stderr
, "setting data bits [%c]\n", datab
);
209 cntrl
.c_cflag
&= ~CSIZE
;
212 if (settermattr(fd
, &cntrl
) == -1)
213 die(2, "failed to set data bits [%c]\n", datab
);
215 if (verbose
) fprintf(stderr
, "setting stop bits [%d]\n", stopb
+1);
218 cntrl
.c_cflag
|= CSTOPB
;
220 cntrl
.c_cflag
&= ~CSTOPB
;
222 if (settermattr(fd
, &cntrl
) == -1)
223 die(1, "failed to set stop bits [%d]\n", stopb
+1);
226 ioctl(fd
, TCFLSH
, TCIOFLUSH
);
228 tcflush(fd
, TCIOFLUSH
);
234 strtoui(const char *str
, const char *msg
, unsigned int min
)
239 t
= strtol(str
, &endptr
, 10);
240 if (errno
!= 0 || *endptr
!= '\0' || t
< min
) {
241 fprintf(stderr
, msg
, str
);
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
248 return (unsigned int)t
;
249 /* termios's `speed_t` is a typedef for `unsigned int` */
253 troptions(CRLFOpt
*options
, const char *str
)
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;
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;
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;
285 fprintf(stderr
, "invalid translation option: %s\n", str
);
294 cntrl
.c_iflag
|= ICRNL
;
296 cntrl
.c_iflag
&= ~ICRNL
;
299 cntrl
.c_iflag
|= INLCR
;
301 cntrl
.c_iflag
&= ~INLCR
;
304 cntrl
.c_oflag
|= OCRNL
;
306 cntrl
.c_oflag
&= ~OCRNL
;
309 cntrl
.c_oflag
|= ONLRET
;
311 cntrl
.c_oflag
&= ~ONLRET
;
314 cntrl
.c_oflag
|= ONLCR
;
316 cntrl
.c_oflag
&= ~ONLCR
;
318 return settermattr(fd
, &cntrl
);
322 termchck(const void *term
)
325 struct termios2
*iterm
= (struct termios2
*)term
;
326 #define TERMIOS_STRUCT termios2
328 struct termios
*iterm
= (struct termios
*)term
;
329 #define TERMIOS_STRUCT termios
331 for (size_t i
= 0; i
< sizeof(struct TERMIOS_STRUCT
); i
++)
332 if (((char*)iterm
)[i
] != 0)
339 writeport(void *unused
)
342 ts
.tv_sec
= swritedelay
;
343 ts
.tv_nsec
= nswritedelay
;
345 wts
.tv_nsec
= chardelay
;
346 writebuff
= malloc(wbuffsz
* sizeof(char));
347 scratchw
= malloc(scratchwsz
* sizeof(int));
349 if (writebuff
== NULL
)
350 die(1, "buffer allocation failed\n");
351 if (scratchw
== NULL
)
352 die(1, "scratch buffer allocation failed\n");
357 Args bsargs
, nocrargs
, nolfargs
, lfincrargs
;
358 bsargs
.sizem
= wbuffsz
;
359 nocrargs
= nolfargs
= lfincrargs
= bsargs
;
361 bsargs
.find
=backspc
; bsargs
.input
=tbackspc
;
366 lfincrargs
.find
= CR
; lfincrargs
.input
= LF
; lfincrargs
.offset
= 1;
369 ssize_t inln
= read(STDIN_FILENO
, writebuff
, wbuffsz
- 1);
375 if (writebuff
[0] == escapechar
) {
382 if (backspc
!= tbackspc
)
383 replacechar(writebuff
, inln
, &bsargs
);
386 inln
= rmchar(writebuff
, inln
, scratchw
, &nocrargs
);
388 inln
= rmchar(writebuff
, inln
, scratchw
, &nolfargs
);
390 inln
= addchar(writebuff
, inln
, scratchw
, &lfincrargs
);
393 for (int i
= 0; i
<= inln
; i
++) {
394 write(fd
, &writebuff
[i
], 1);
395 nanosleep(&wts
, NULL
);
398 write(fd
, writebuff
, 1);
401 nanosleep(&ts
, NULL
);
407 readport(void *unused
)
410 ts
.tv_sec
= sreaddelay
;
411 ts
.tv_nsec
= nsreaddelay
;
412 readbuff
= malloc(rbuffsz
* sizeof(char));
413 scratchr
= malloc(scratchrsz
* sizeof(int));
415 if (readbuff
== NULL
)
416 die(1, "buffer allocation failed\n");
417 if (scratchr
== NULL
)
418 die(1, "scratch buffer allocation failed\n");
422 Args nocrargs
, nolfargs
, crinlfargs
, lfincrargs
;
423 nocrargs
.sizem
= rbuffsz
;
424 nolfargs
= crinlfargs
= lfincrargs
= nocrargs
;
429 lfincrargs
.find
= CR
; lfincrargs
.input
= LF
; lfincrargs
.offset
= 1;
430 crinlfargs
.find
= LF
; crinlfargs
.input
= CR
; crinlfargs
.offset
= -1;
433 /* disable stdout buffering */
434 setvbuf(stdout
, NULL
, _IONBF
, 0);
437 ssize_t outln
= read(fd
, readbuff
, rbuffsz
- 1);
440 outln
= rmchar(readbuff
, outln
, scratchr
, &nocrargs
);
442 outln
= rmchar(readbuff
, outln
, scratchr
, &nolfargs
);
444 outln
= addchar(readbuff
, outln
, scratchr
, &crinlfargs
);
446 outln
= addchar(readbuff
, outln
, scratchr
, &lfincrargs
);
448 write(STDOUT_FILENO
, readbuff
, outln
);
450 nanosleep(&ts
, NULL
);
452 if (isatty(STDIN_FILENO
))
453 settermattr(STDIN_FILENO
, &origterm
);
457 inline void __attribute__((hot
))
458 replacechar(char *buff
, ssize_t size
, const Args
*args
)
460 for (int i
= 0; i
< size
; i
++)
461 if (buff
[i
] == args
->find
)
462 buff
[i
] = args
->input
;
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
)
471 for (int i
= 0; i
< size
; i
++) {
472 if (buff
[i
] == args
->find
) {
479 if ((size
+ c
) > args
->sizem
)
480 c
= args
->sizem
- size
;
482 if (scratch
[0] == 0 && args
->offset
< 0) {
483 memmove(&buff
[0]+1, &buff
[0], size
);
486 for (int i
= c
; i
> 0; i
--) {
488 for (int s
= size
; s
>= t
;s
--)
489 buff
[s
+ args
->offset
] = buff
[s
];
490 buff
[t
] = args
->input
;
495 inline ssize_t
__attribute__((hot
))
496 rmchar(char *buff
, ssize_t size
, int *scratch
, const Args
*args
)
500 for (int i
= 0; i
< size
; i
++) {
501 if (buff
[i
] == args
->find
) {
509 for (int i
= c
; i
> 0; i
--)
510 for (int s
= size
; s
>= scratch
[i
]; s
--)
511 buff
[s
] = buff
[s
+ 1];
520 if (gettermattr(STDIN_FILENO
, &origterm
) < 0 )
521 die(1, "failed to get terminal attributes\n");
526 newterm
.c_lflag
&= ~ECHO
;
528 newterm
.c_lflag
&= ~ICANON
;
531 newterm
.c_iflag
|= IXONXOFF
;
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
;
544 backspc
= origterm
.c_cc
[VERASE
];
546 if (settermattr(STDOUT_FILENO
, &newterm
) < 0)
547 die(1, "failed to set terminal attributes\n");
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");
572 rmlf
.find
= LF
; rmlf
.input
= '\0';
574 if (isatty(STDIN_FILENO
) || isatty(STDOUT_FILENO
))
580 cmdchar
= writebuff
[0];
582 cmdchar
= writebuff
[1];
593 backspace
= !backspace
;
597 newterm
.c_lflag
&= ~ECHO
;
599 newterm
.c_lflag
|= ECHO
;
601 if (settermattr(STDOUT_FILENO
, &newterm
) < 0)
602 die(1, "failed to set terminal attributes\n");
606 newterm
.c_lflag
&= ~ICANON
;
608 newterm
.c_lflag
|= ICANON
;
609 canonical
= !canonical
;
610 if (settermattr(STDOUT_FILENO
, &newterm
) < 0)
611 die(1, "failed to set terminal attributes\n");
615 if (fgets(ttr
, sizeof(ttr
), stdin
) != NULL
) {
616 replacechar(ttr
, 63, &rmlf
);
618 int wfd
= open(ttr
, O_RDONLY
);
620 perror("error opening file");
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
);
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
);
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
) {
652 wts
.tv_nsec
= chardelay
;
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
))
664 fprintf(stderr
, "could not set new options\n");
669 printf("additional input translation option: ");
670 if (fgets(ttr
, sizeof(ttr
), stdin
) != NULL
) {
671 replacechar(ttr
, 63, &rmlf
);
672 if (troptions(&itropts
, ttr
))
675 fprintf(stderr
, "could not set new options\n");
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");
688 ts
.tv_sec
= spulsedelay
;
689 ts
.tv_nsec
= nspulsedelay
;
691 if (ioctl(fd
, TIOCMGET
, &st
) != 0) {
692 fprintf(stderr
, "failed to get port status\n");
696 if (ioctl(fd
, TIOCMSET
, &st
) != 0) {
697 fprintf(stderr
, "failed to set DTR [assertion]\n");
700 nanosleep(&ts
, NULL
);
703 if (ioctl(fd
, TIOCMSET
, &st
) != 0)
704 fprintf(stderr
, "failed to set DTR [negation]\n");
706 case BS
: /* FALLTHROUGH */
711 fprintf(stderr
, "not a valid command [%c]\n", cmdchar
);
720 die(128 + signo
, "\nrecieved signal [%d], exiting\n", signo
);
724 die(int code
, const char *msg
, ...)
739 if (termchck(&newterm
))
740 settermattr(STDIN_FILENO
, &origterm
);
743 vfprintf(stderr
, msg
, fpa
);
750 main(int argc
, char **argv
)
752 for (int i
= 1; i
< argc
; i
++ ) {
753 if (argv
[i
][0] == '-' && argv
[i
][1] >= '0' && argv
[i
][1] <= '9') {
755 /* glibc's `asprintf()` won't set the string to NULL on failure automatically */
756 asprintf(&t
, "-s%s", argv
[i
] + 1);
758 fprintf(stderr
, "cannot convert -# to -s#\n");
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'},
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) {
796 backspace
^= 1; break;
798 canonical
^= 1; break;
808 t
= strtol(optarg
, &endptr
, 10);
809 if (errno
!= 0 || *endptr
!= '\0' || t
< 0) {
810 fprintf(stderr
, "invalid character delay\n");
817 if (strlen(optarg
) != 1 || !(optarg
[0] >= '5' && optarg
[0] <= '8')) {
818 fprintf(stderr
, "invalid number of data bits: %s\n", optarg
);
831 tui
= strtoui(optarg
, "invalid rx speed: %s\n", 1);
838 tui
= strtoui(optarg
, "invalid speed: %s\n", 1);
847 fprintf(stderr
, "cannot specify multiple devices\n");
850 if (strlen(optarg
) > 10) {
851 fprintf(stderr
, "device name too long\n");
854 if (strchr(optarg
, '/')) {
855 strcpy(line
, optarg
);
858 sprintf(line
, "/dev/%s", optarg
);
863 tui
= strtoui(optarg
, "invalid number of characters: %s\n", 1);
869 if (troptions(&tropts
, optarg
))
873 if (troptions(&itropts
, optarg
))
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",
900 if (isatty(STDIN_FILENO
) || isatty(STDOUT_FILENO
))
903 signal(SIGHUP
, sighandl
);
904 signal(SIGINT
, sighandl
);
905 signal(SIGQUIT
, sighandl
);
906 signal(SIGTERM
, sighandl
);
910 pthread_t readthread
, writethread
;
911 pthread_create(&writethread
, NULL
, writeport
, NULL
);
912 pthread_create(&readthread
, NULL
, readport
, NULL
);