1 /* see LICENSE for license details */
18 #include <asm/termbits.h>
19 #include <asm/ioctls.h>
20 #else /* including for termios2, which is needed for non-standard bauds */
31 #define IXONXOFF (IXON | IXOFF)
33 #if defined(CCDTR_IFLOW) && defined(CDSR_OFLOW)
34 #define CDTRDSR (CDTR_IFLOW | CDSR_OFLOW)
38 typedef struct termios2 TermStruct
;
40 typedef struct termios TermStruct
;
44 int offset
; /* can only be a 1 or a -1 */
63 /* including here to access CRLFOpt */
66 static int gettermattr(int dv
, TermStruct
*optst
);
67 static int settermattr(int dv
, const TermStruct
*optst
);
68 static void settermspd(unsigned int lispeed
, unsigned int lospeed
,
70 static int openport(void);
71 static int openparent(void);
72 static unsigned int strtoui(const char *str
, unsigned int min
);
73 static int chcktropts(CRLFOpt
*options
, const char *str
);
74 static int settropts(void);
75 static inline unsigned int lsbmask(unsigned int n
);
76 static inline int termvalid(const TermStruct
*term
);
77 static void eninter(void);
78 static void enusrecho(void);
79 static void *writeport(void *);
80 static void *readport(void *);
81 static void getcmd(int escape
);
82 static inline void replacechar(char *buff
, ssize_t size
, const Args
*args
);
83 static inline ssize_t
addchar(char *buff
, ssize_t size
, int *scratch
,
84 const Sizes
*msizes
, const Args
*args
);
85 static inline ssize_t
rmchar(char *buff
, ssize_t sread
, int *scratch
,
87 static void sighandl(int signo
);
88 static void die(int code
, const char *msg
, ...);
90 static TermStruct cntrl
, /* device struct */
91 /* controlling terminal structs */
95 static Args bsargs
= {0, DEL
, DEL
};
96 static const Args nocrargs
= {0, CR
, 0},
97 /* null-terminator is for use inside `getcmd()` */
98 nolfargs
= {0, LF
, '\0'},
99 lfincrargs
= {1, CR
, LF
},
100 crinlfargs
= {0, LF
, CR
};
102 static struct timespec wts
;
103 /* unsigned -1 used to return an error */
104 static const unsigned int uintmax
= ~(unsigned int)0;
105 static int interactive
= 0;
106 static char *writebuff
= NULL
;
107 static char *readbuff
= NULL
;
108 static int *scratchr
= NULL
;
109 static int *scratchw
= NULL
;
115 gettermattr(int dv
, TermStruct
*optst
)
118 return ioctl(dv
, TCGETS2
, optst
);
120 return tcgetattr(dv
, optst
);
125 settermattr(int dv
, const TermStruct
*optst
)
128 return ioctl(dv
, TCSETS2
, optst
);
130 return tcsetattr(dv
, TCSANOW
, optst
);
135 settermspd(unsigned int lispeed
, unsigned int lospeed
, TermStruct
*optst
)
138 optst
->c_cflag
&= ~CBAUD
;
139 optst
->c_cflag
|= BOTHER
;
140 optst
->c_ispeed
= lispeed
;
141 optst
->c_ospeed
= lospeed
;
143 (void)cfsetispeed(optst
, lispeed
);
144 (void)cfsetospeed(optst
, lospeed
);
152 struct flock fl
= { 0 };
154 if (verbose
) fprintf(stderr
, "opening \"%s\"\n", line
);
156 fd
= open(line
, (O_RDWR
| O_NOCTTY
| O_NONBLOCK
| O_NDELAY
));
158 perror("error opening device");
162 if (verbose
) fprintf(stderr
, "checking if \"%s\" is a TTY\n", line
);
165 die(2, "device \"%s\" is not a TTY\n", line
);
168 flags
= fcntl(fd
, F_GETFL
, 0);
169 flags
&= ~(O_NONBLOCK
| O_NDELAY
);
171 if (fcntl(fd
, F_SETFL
, flags
) < 0) {
173 die(1, "exiting now\n");
177 if (gettermattr(fd
, &cntrl
) < 0)
178 die(1, "failed to get device attributes\n");
182 fl
.l_whence
= SEEK_SET
;
183 if (fcntl(fd
, F_SETLK
, &fl
) < 0) {
185 die(1, "failed to lock the device, exiting now\n");
192 if (verbose
) fprintf(stderr
, "setting baudrate [RX:%u | TX:%u]\n",
195 settermspd(ispeed
, ospeed
, &cntrl
);
196 if (settermattr(fd
, &cntrl
) < 0)
197 die(2,"failed to set baudrate [RX:%u | TX:%u]\n",
204 cntrl
.c_iflag
&= ~(ISTRIP
| BRKINT
);
205 cntrl
.c_cflag
&= ~(PARENB
| PARODD
);
207 if (verbose
) fprintf(stderr
, "setting parity [even: %d, odd: %d]\n",
208 parity
& 1, (parity
& 2) >> 1);
211 cntrl
.c_cflag
|= PARENB
;
213 cntrl
.c_cflag
|= PARENB
| PARODD
;
215 if (settermattr(fd
, &cntrl
) < 0)
216 die(2, "failed to set parity [even: %d, odd: %d]\n",
217 parity
& 1, (parity
& 2) >> 1);
223 /* it's so ugly and beautiful at the same time */
224 int t
= (((hard
& 1) << 1) & (hard
& 2)) >> 1;
225 fprintf(stderr
, "setting flow control "
226 "[XON/XOFF: %d, RTS/CTS: %d, DTR/DSR: %d, DCD: %d]\n",
227 soft
, t
^ ((hard
& 1) << 1) >> 1,
228 t
^ (hard
& 2) >> 1, t
);
231 cntrl
.c_cflag
|= CLOCAL
;
232 cntrl
.c_iflag
&= ~IXONXOFF
;
235 cntrl
.c_iflag
|= IXONXOFF
;
238 cntrl
.c_cflag
|= CRTSCTS
;
239 } else if (hard
== 2) {
241 fprintf(stderr
, "DTR/DSR "
242 "flow control is not supported on this platform\n"
243 "enabling this option does nothing!\n");
245 cntrl
.c_lflag
|= CDTRDSR
;
247 } else if (hard
== 3) {
248 cntrl
.c_cflag
&= ~CLOCAL
;
250 cntrl
.c_lflag
|= CCAR_OFLOW
;
254 if (settermattr(fd
, &cntrl
) < 0)
255 die(2, "failed to set flow control\n");
260 cntrl
.c_iflag
&= ~(ICRNL
| INLCR
);
261 cntrl
.c_oflag
&= ~(OCRNL
| ONLRET
| ONLCR
);
264 die(2, "failed to set cr-lf translation options\n");
269 if (verbose
) fprintf(stderr
, "setting data bits [%c]\n", datab
);
282 cntrl
.c_cflag
&= ~CSIZE
;
285 if (settermattr(fd
, &cntrl
) < 0)
286 die(2, "failed to set data bits [%c]\n", datab
);
291 if (verbose
) fprintf(stderr
, "setting stop bits [%d]\n", stopb
+1);
294 cntrl
.c_cflag
|= CSTOPB
;
296 cntrl
.c_cflag
&= ~CSTOPB
;
298 if (settermattr(fd
, &cntrl
) < 0)
299 die(1, "failed to set stop bits [%d]\n", stopb
+1);
301 cntrl
.c_cc
[VMIN
] = 0;
302 cntrl
.c_cc
[VTIME
] = 0;
304 /* flush both hardware buffers */
306 ioctl(fd
, TCFLSH
, TCIOFLUSH
);
308 tcflush(fd
, TCIOFLUSH
);
310 /* returning only for readability */
317 if (verbose
) fprintf(stderr
, "opening parent terminal\n");
319 pfd
= open("/dev/tty", O_RDWR
);
321 perror("error opening parent terminal fd");
322 die(1, "exiting now\n");
325 if (gettermattr(pfd
, &origterm
) < 0 )
326 die(1, "failed to get parent terminal attributes\n");
330 /* returning only for readability */
335 strtoui(const char *str
, unsigned int min
)
340 t
= strtol(str
, &endptr
, 10);
341 if (errno
!= 0 || *endptr
!= '\0' || t
< min
|| t
> uintmax
) {
345 return (unsigned int)t
;
349 chcktropts(CRLFOpt
*options
, const char *str
)
353 if (!strcmp(str
, "lf-in-cr")) {
354 options
->lfincr
^= 1; return 0;
355 } else if (!strcmp(str
, "lf-to-cr")) {
356 options
->lftocr
^= 1; return 0;
361 if (!strcmp(str
, "cr-in-lf")) {
362 options
->crinlf
^= 1; return 0;
363 } else if (!strcmp(str
, "cr-to-lf")) {
364 options
->crtolf
^= 1; return 0;
369 if (!strcmp(str
, "no-lf")) {
370 options
->nolf
^= 1; return 0;
371 } else if (!strcmp(str
, "no-cr")) {
372 options
->nocr
^= 1; return 0;
384 cntrl
.c_iflag
^= (ICRNL
& lsbmask(itropts
.crtolf
));
385 cntrl
.c_iflag
^= (INLCR
& lsbmask(itropts
.lftocr
));
386 cntrl
.c_oflag
^= (OCRNL
& lsbmask(tropts
.crtolf
));
387 cntrl
.c_oflag
^= (ONLRET
& lsbmask(tropts
.lftocr
));
388 cntrl
.c_oflag
^= (ONLCR
& lsbmask(tropts
.crinlf
));
390 return settermattr(fd
, &cntrl
);
394 lsbmask(unsigned int n
)
396 /* if n == 1, n fills with all 1s, 0 stays the same */
397 for (int i
= (sizeof(int) * CHAR_BIT
); i
> 0; i
--)
403 termvalid(const TermStruct
*term
)
405 for (size_t i
= 0; i
< sizeof(TermStruct
); i
++)
406 if (((char*)term
)[i
] != 0)
413 writeport(void *unused
)
419 ts
.tv_sec
= swritedelay
;
420 ts
.tv_nsec
= nswritedelay
;
422 wts
.tv_nsec
= chardelay
;
423 msizes
.sizesm
= scratchwsz
;
424 msizes
.sizebm
= wbuffsz
;
426 writebuff
= malloc(wbuffsz
* sizeof(char));
427 scratchw
= malloc(scratchwsz
* sizeof(int));
429 if (writebuff
== NULL
)
430 die(1, "buffer allocation failed\n");
431 if (scratchw
== NULL
)
432 die(1, "scratch buffer allocation failed\n");
435 ssize_t inln
= read(STDIN_FILENO
, writebuff
, wbuffsz
- 1);
442 if (writebuff
[0] == escapechar
) {
450 if (bsargs
.find
!= bsargs
.input
)
451 replacechar(writebuff
, inln
, &bsargs
);
453 inln
= rmchar(writebuff
, inln
, scratchw
, &nocrargs
);
455 inln
= rmchar(writebuff
, inln
, scratchw
, &nolfargs
);
457 inln
= addchar(writebuff
, inln
, scratchw
, &msizes
,
461 for (int i
= 0; i
<= inln
; i
++) {
462 rwc
-= write(fd
, &writebuff
[i
], 1);
463 nanosleep(&wts
, NULL
);
467 rwc
-= write(fd
, writebuff
, inln
);
470 } else if (inln
== 0 && !interactive
) {
473 nanosleep(&ts
, NULL
);
483 readport(void *unused
)
488 ts
.tv_sec
= sreaddelay
;
489 ts
.tv_nsec
= nsreaddelay
;
490 msizes
.sizesm
= scratchrsz
;
491 msizes
.sizebm
= rbuffsz
;
493 readbuff
= malloc(rbuffsz
* sizeof(char));
494 scratchr
= malloc(scratchrsz
* sizeof(int));
496 if (readbuff
== NULL
)
497 die(1, "buffer allocation failed\n");
498 if (scratchr
== NULL
)
499 die(1, "scratch buffer allocation failed\n");
503 /* disable stdout buffering */
504 setvbuf(stdout
, NULL
, _IONBF
, 0);
507 ssize_t outln
= read(fd
, readbuff
, rbuffsz
- 1);
510 outln
= rmchar(readbuff
, outln
, scratchr
, &nocrargs
);
512 outln
= rmchar(readbuff
, outln
, scratchr
, &nolfargs
);
514 outln
= addchar(readbuff
, outln
, scratchr
, &msizes
,
517 outln
= addchar(readbuff
, outln
, scratchr
, &msizes
,
520 rwc
+= write(STDOUT_FILENO
, readbuff
, outln
);
522 nanosleep(&ts
, NULL
);
530 replacechar(char *buff
, ssize_t size
, const Args
*args
)
532 for (int i
= 0; i
< size
; i
++)
533 if (buff
[i
] == args
->find
)
534 buff
[i
] = args
->input
;
538 addchar(char *buff
, ssize_t size
, int *scratch
, const Sizes
*sizes
,
543 /* turns negative numbers into 1, positive into 0 */
544 to
= ((args
->offset
>> ((sizeof(int) * CHAR_BIT
) - 1)) & 1);
546 for (int i
= 0; i
< size
; i
++) {
547 if (buff
[i
] == args
->find
) {
551 if (c
>= sizes
->sizesm
)
556 if (size
== 1 && c
== 1) {
557 buff
[to
^ 1] = args
->input
;
558 buff
[to
] = args
->find
;
561 if ((size
+ c
) > sizes
->sizebm
)
562 c
= sizes
->sizebm
- size
;
564 for (int i
= c
- 1; i
>= 0; i
--) {
566 memmove(&buff
[t
] + 1, &buff
[t
], (size
+ c
) - t
);
567 buff
[t
+ (to
^ 1)] = args
->input
;
573 rmchar(char *buff
, ssize_t size
, int *scratch
, const Args
*args
)
576 for (int i
= 0; i
< size
; i
++) {
577 if (buff
[i
] == args
->find
) {
584 if (size
== 1 && c
== 1)
587 for (int i
= c
; i
>= 0; i
--) {
589 memmove(&buff
[t
], &buff
[t
] + 1, (size
+ c
) - t
);
599 newterm
.c_lflag
&= ~ECHO
;
601 newterm
.c_lflag
&= ~ICANON
;
604 newterm
.c_iflag
|= IXONXOFF
;
606 newterm
.c_iflag
|= INLCR
;
607 newterm
.c_cc
[VMIN
] = minchars
;
608 newterm
.c_cc
[VTIME
] = 0;
609 newterm
.c_cc
[VINTR
] = newterm
.c_cc
[VSUSP
]
610 = newterm
.c_cc
[VQUIT
] = newterm
.c_cc
[VLNEXT
]
611 = newterm
.c_cc
[VDISCARD
] = _POSIX_VDISABLE
;
614 newterm
.c_cc
[VDSUSP
] = _POSIX_VDISABLE
;
622 bsargs
.find
= origterm
.c_cc
[VERASE
];
624 if (settermattr(pfd
, &newterm
) < 0)
625 die(1, "failed to set terminal attributes\n");
633 if ((!half
|| !canonical
) && interactive
) {
634 newterm
.c_lflag
|= ECHO
;
635 newterm
.c_lflag
|= ICANON
;
636 if (settermattr(pfd
, &newterm
) < 0)
638 "failed to enable local echo for user input\n");
652 msizes
.sizesm
= scratchwsz
;
653 msizes
.sizebm
= wbuffsz
;
656 cmdchar
= writebuff
[0];
658 cmdchar
= writebuff
[1];
661 case EOT
: /* FALLTHROUGH */
673 newterm
.c_lflag
&= ~ECHO
;
675 newterm
.c_lflag
|= ECHO
;
676 if (settermattr(pfd
, &newterm
) < 0)
677 die(1, "failed to set terminal attributes\n");
683 newterm
.c_lflag
&= ~ICANON
;
685 newterm
.c_lflag
|= ICANON
;
686 if (settermattr(pfd
, &newterm
) < 0)
687 die(1, "failed to set terminal attributes\n");
693 if (fgets(ttr
, sizeof(ttr
), stdin
) != NULL
) {
695 int wfd
= open(ttr
, O_RDONLY
);
696 replacechar(ttr
, 63, &nolfargs
);
698 perror("error opening file");
701 while ((frln
= read(wfd
, writebuff
, wbuffsz
- 1)) > 0) {
703 for (int i
= 0; i
<= frln
; i
++) {
705 frln
= rmchar(writebuff
, frln
, scratchw
,
708 frln
= rmchar(writebuff
, frln
, scratchw
,
711 frln
= addchar(writebuff
, frln
, scratchw
,
712 &msizes
, &lfincrargs
);
714 rwc
-= write(fd
, &writebuff
[i
], 1);
715 nanosleep(&wts
, NULL
);
722 if (fgets(ttr
, sizeof(ttr
), stdin
) != NULL
) {
723 replacechar(ttr
, 63, &nolfargs
);
724 tspd
= strtoui(ttr
, 0);
725 if (tspd
!= uintmax
) {
726 ospeed
= ispeed
= tspd
;
727 settermspd(ispeed
, ospeed
, &cntrl
);
728 if (settermattr(fd
, &cntrl
) < 0)
729 fprintf(stderr
, "failed to set baudrate "
730 "[RX:%u | TX:%u]\n", ispeed
, ospeed
);
732 fprintf(stderr
, "invalid speed: %s\n", ttr
);
738 if (fgets(ttr
, sizeof(ttr
), stdin
) != NULL
) {
739 replacechar(ttr
, 63, &nolfargs
);
740 chardelay
= strtoui(ttr
, 0);
741 if (chardelay
!= uintmax
) {
743 wts
.tv_nsec
= chardelay
;
745 fprintf(stderr
, "invalid delay: %s\n", ttr
);
749 case 't': /* FALLTHROUGH */
752 toptptr
= (cmdchar
== 't') ? &tropts
: &itropts
;
753 fprintf(stderr
, "additional translation option: ");
754 if (fgets(ttr
, sizeof(ttr
), stdin
) != NULL
) {
755 replacechar(ttr
, 63, &nolfargs
);
756 if (chcktropts(toptptr
, ttr
)) {
758 "invalid translation option: %s\n",
764 fprintf(stderr
, "could not set new options\n");
767 if (!half
&& interactive
)
768 newterm
.c_lflag
&= ~ECHO
;
769 if (!canonical
&& interactive
)
770 newterm
.c_lflag
&= ~ICANON
;
771 if (settermattr(pfd
, &newterm
) < 0)
773 "failed to restore echo and/or canonical mode\n");
778 ts
.tv_sec
= spulsedelay
;
779 ts
.tv_nsec
= nspulsedelay
;
781 if (ioctl(fd
, TIOCMGET
, &st
) < 0) {
782 fprintf(stderr
, "failed to get port status\n");
786 if (ioctl(fd
, TIOCMSET
, &st
) < 0) {
787 fprintf(stderr
, "failed to set DTR [assertion]\n");
790 nanosleep(&ts
, NULL
);
793 if (ioctl(fd
, TIOCMSET
, &st
) < 0)
794 fprintf(stderr
, "failed to set DTR [negation]\n");
796 case BS
: /* FALLTHROUGH */
801 fprintf(stderr
, "not a valid command [%c]\n", cmdchar
);
810 die(128 + signo
, "\nrecieved signal [%d], exiting\n", signo
);
814 die(int code
, const char *msg
, ...)
818 if (termvalid(&origterm
))
819 settermattr(pfd
, &origterm
);
823 nanosleep(&wts
, NULL
);
842 vfprintf(stderr
, msg
, fpa
);
849 main(int argc
, char *argv
[])
855 int oind
, rxspdset
, devset
, c
;
856 oind
= rxspdset
= devset
= 0;
858 for (int i
= 1; i
< argc
; i
++) {
859 if (argv
[i
][0] == '-' && argv
[i
][1] >= '0' &&
861 if (asprintf(&t
, "-s%s", argv
[i
] + 1) == -1) {
863 "%s: cannot convert -# to -s#\n",
873 static struct option longopt
[] = {
874 {"line", required_argument
, NULL
, 'l'},
875 {"speed", required_argument
, NULL
, 's'},
876 {"rx-speed", required_argument
, NULL
, 'i'},
877 {"echo", no_argument
, NULL
, 'h'},
878 {"canonical", no_argument
, NULL
, 'c'},
879 {"odd", no_argument
, NULL
, 'o'},
880 {"even", no_argument
, NULL
, 'e'},
881 {"hardware-rts-cts", no_argument
, NULL
, 'R'},
882 {"hardware-dtr-dsr", no_argument
, NULL
, 'r'},
883 {"software", no_argument
, NULL
, 'X'},
884 {"data", required_argument
, NULL
, 'D'},
885 {"delay", required_argument
, NULL
, 'd'},
886 {"min-chars", required_argument
, NULL
, 'm'},
887 {"stop-bits", no_argument
, NULL
, 'S'},
888 {"backspace", no_argument
, NULL
, 'b'},
889 {"translation", required_argument
, NULL
, 't'},
890 {"input-translation", required_argument
, NULL
, 'T'},
891 {"verbose", no_argument
, NULL
, 'v'},
895 while ((c
= getopt_long(argc
, argv
,
896 "D:T:d:i:l:m:s:t:RXbcehorv", longopt
, &oind
)) >= 0) {
901 backspace
^= 1; break;
903 canonical
^= 1; break;
911 tl
= strtol(optarg
, &endptr
, 10);
912 if (errno
!= 0 || *endptr
!= '\0' || tl
< 0) {
914 "%s: invalid character delay: %s\n",
923 if (optarg
[1] != '\0' ||
924 !(optarg
[0] >= '5' && optarg
[0] <= '8')) {
926 "%s: invalid number of data bits: %s\n",
940 case 'i': /* FALLTHROUGH */
942 tui
= strtoui(optarg
, 1);
943 if (tui
== uintmax
) {
945 "%s: invalid speed: %s\n",
951 ispeed
= tui
; rxspdset
= 1;
954 ispeed
= rxspdset
? ispeed
: ospeed
;
962 "%s: cannot specify multiple devices\n",
967 if (strlen(optarg
) > 58) {
968 fprintf(stderr
, "%s: device name too long\n", argv
[0]);
971 if (strchr(optarg
, '/')) {
972 strcpy(line
, optarg
);
975 sprintf(line
, "/dev/%s", optarg
);
980 tui
= strtoui(optarg
, 1);
981 if (tui
== uintmax
) {
983 "%s: invalid number of characters: %s\n",
991 if (chcktropts(&tropts
, optarg
)) {
993 "%s: invalid output translation option: %s\n",
1000 if (chcktropts(&itropts
, optarg
)) {
1002 "%s: invalid input translation option: %s\n",
1009 verbose
^= 1; break;
1012 die(2, "usage: %s [--line | -l line] "
1013 "[--speed | -s # | -#] [--rx-speed | -i #]\n"
1014 " [--data-bits | -D #] "
1015 "[--stop-bits | -S] [--even | -e] [--odd | -o]\n"
1016 " [--hardware-rts-cts | -R] "
1017 "[--hardware-dtr-dsr | -r] [--software | -X]\n"
1018 " [--min-chars | -m #] [--echo | -h] "
1019 "[--canonical | -c] [--delay | -d #]\n"
1020 " [--translation | -t option] "
1021 "[--input-translation | -T option]\n"
1022 " [--verbose | -v] "
1023 "[--backspace | -b]\n", argv
[0]);
1031 if (isatty(STDIN_FILENO
))
1034 signal(SIGHUP
, sighandl
);
1035 signal(SIGINT
, sighandl
);
1036 signal(SIGQUIT
, sighandl
);
1037 signal(SIGTERM
, sighandl
);
1040 if (interactive
) pfd
= openparent();
1043 if (pledge("stdio rpath tty", NULL
) < 0) {
1045 die(1, "pledge failed, exiting now\n");
1049 pthread_t readthread
, writethread
;
1050 pthread_create(&writethread
, NULL
, writeport
, NULL
);
1051 pthread_create(&readthread
, NULL
, readport
, NULL
);
1052 pthread_join(writethread
, NULL
);
1053 pthread_join(readthread
, NULL
);