2 * Simple serial terminal
4 * Copyright 2014 Michal Sojka <sojkam1@fel.cvut.cz>
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
22 * This is a minimalist terminal program like minicom or cu. The only
23 * thing it does is creating a bidirectional connection between
24 * stdin/stdout and a device (e.g. serial terminal). It can also set
25 * serial line baudrate and manipulate DTR/RTS modem lines.
27 * The -d and -r option create short pulse on DTR/RTS. The lines are
28 * always raised when the device is opened and those options lower the
29 * lines immediately after opening.
33 #include <sys/ioctl.h>
34 #include <sys/types.h>
47 #define STRINGIFY(val) #val
48 #define TOSTRING(val) STRINGIFY(val)
49 #define CHECK(cmd) ({ int ret = (cmd); if (ret == -1) { perror(#cmd " line " TOSTRING(__LINE__)); exit(1); }; ret; })
50 #define CHECKPTR(cmd) ({ void *ptr = (cmd); if (ptr == (void*)-1) { perror(#cmd " line " TOSTRING(__LINE__)); exit(1); }; ptr; })
52 #define VERBOSE(format, ...) do { if (verbose) fprintf(stderr, "sterm: " format, ##__VA_ARGS__); } while (0)
55 bool exit_on_escape = true;
57 struct termios stdin_tio_backup;
60 void rm_file(int status, void *arg)
68 void restore_stdin_term()
70 tcsetattr(0, TCSANOW, &stdin_tio_backup);
75 dev_unlock(dev, getpid());
78 void sighandler(int arg)
80 exit(0); /* Invoke exit handlers */
83 int dtr_rts_arg(const char option)
89 case '+': val = +1; break;
90 case '-': val = -1; break;
92 fprintf(stderr, "Unknown -%c argument: %s", option, optarg);
99 void exit_on_escapeseq(const char *buf, int len)
101 static const char escseq[] = "\r~.";
102 static const char *state = escseq+1;
105 for (i = 0; i < len; i++) {
106 if (buf[i] == *state) {
115 void usage(const char* argv0)
117 fprintf(stderr, "Usage: %s [options] <device>\n", argv0);
118 fprintf(stderr, "Options:\n");
119 fprintf(stderr, " -d [+|-] create short positive/negative pulse on DTR\n");
120 fprintf(stderr, " -e ignore '~.' escape sequence\n");
121 fprintf(stderr, " -n do not switch the device to raw mode\n");
122 fprintf(stderr, " -r [+|-] create short positive/negative pulse on RTS\n");
123 fprintf(stderr, " -s <baudrate>\n");
124 fprintf(stderr, " -v verbose\n");
127 int main(int argc, char *argv[])
132 int dtr = 0, rts = 0;
137 if ((stdin_tty = isatty(0))) {
138 CHECK(tcgetattr(0, &stdin_tio_backup));
139 atexit(restore_stdin_term);
142 while ((opt = getopt(argc, argv, "nd::er::s:v")) != -1) {
144 case 'd': dtr = dtr_rts_arg(opt); break;
145 case 'e': exit_on_escape = false; break;
146 case 'n': raw = false; break;
147 case 'r': rts = dtr_rts_arg(opt); break;
149 int s = atoi(optarg);
151 #define S(s) case s: speed = B##s; break;
173 fprintf(stderr, "Unknown baud rate %d\n", s);
191 fprintf(stderr, "No device specified\n");
196 signal(SIGINT, sighandler);
197 signal(SIGTERM, sighandler);
198 signal(SIGHUP, sighandler);
200 pid_t pid = dev_lock(dev);
202 fprintf(stderr, "%s is used by PID %d\n", dev, pid);
204 } else if (pid < 0) {
205 perror("dev_lock()");
210 if ((fd = open(dev, O_RDWR)) < 0) {
216 CHECK(ioctl(fd, TIOCEXCL, NULL));
218 CHECK(tcgetattr(fd, &tio));
223 CHECK(cfsetospeed(&tio, speed));
224 CHECK(cfsetispeed(&tio, speed));
229 /* tio.c_cflag &= ~HUPCL; */ /* Don't lower DTR/RTS on close */
231 CHECK(ioctl(fd, TIOCMGET, &status));
232 if (dtr == -1) status &= ~TIOCM_DTR;
233 if (dtr == +1) status |= TIOCM_DTR;
234 if (rts == -1) status &= ~TIOCM_RTS;
235 if (rts == +1) status |= TIOCM_RTS;
236 CHECK(ioctl(fd, TIOCMSET, &status));
239 CHECK(tcsetattr(fd, TCSANOW, &tio));
240 } else if (speed || dtr || rts) {
241 fprintf(stderr, "Cannot set speed, DTR or RTS on non-terminal %s\n", dev);
245 struct pollfd fds[2] = {
246 { .fd = 0, .events = POLLIN },
247 { .fd = fd, .events = POLLIN },
252 tio = stdin_tio_backup;
255 CHECK(tcsetattr(0, TCSANOW, &tio));
258 VERBOSE("Connected.\r\n");
260 VERBOSE("Use '<Enter>~.' sequence to exit.\r\n");
264 CHECK(poll(fds, 2, -1));
265 if (fds[0].revents & POLLIN) {
266 r1 = CHECK(read(0, buf, sizeof(buf)));
268 VERBOSE("EOF on stdin\r\n");
272 exit_on_escapeseq(buf, r1);
273 r2 = CHECK(write(fd, buf, r1));
275 fprintf(stderr, "Not all data written to %s (%d/%d)\n", dev, r1, r2);
279 if (fds[1].revents & POLLIN) {
280 r1 = CHECK(read(fd, buf, sizeof(buf)));
282 VERBOSE("EOF on %s\r\n", dev);
285 r2 = CHECK(write(1, buf, r1));
287 fprintf(stderr, "Not all data written to stdout (%d/%d)\n", r1, r2);