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>
45 #define STRINGIFY(val) #val
46 #define TOSTRING(val) STRINGIFY(val)
47 #define CHECK(cmd) ({ int ret = (cmd); if (ret == -1) { perror(#cmd " line " TOSTRING(__LINE__)); exit(1); }; ret; })
48 #define CHECKPTR(cmd) ({ void *ptr = (cmd); if (ptr == (void*)-1) { perror(#cmd " line " TOSTRING(__LINE__)); exit(1); }; ptr; })
50 #define VERBOSE(format, ...) do { if (verbose) fprintf(stderr, "sterm: " format, ##__VA_ARGS__); } while (0)
53 bool exit_on_escape = true;
55 char template[] = "/var/lock/TMPXXXXXX";
57 struct termios stdin_tio_backup;
59 void rm_file(int status, void *arg)
67 void restore_stdin_term()
69 tcsetattr(0, TCSANOW, &stdin_tio_backup);
72 void sighandler(int arg)
74 exit(0); /* Invoke exit handlers */
77 int dtr_rts_arg(const char option)
83 case '+': val = +1; break;
84 case '-': val = -1; break;
86 fprintf(stderr, "Unknown -%c argument: %s", option, optarg);
93 void exit_on_escapeseq(const char *buf, int len)
95 static const char escseq[] = "\r~.";
96 static const char *state = escseq+1;
99 for (i = 0; i < len; i++) {
100 if (buf[i] == *state) {
109 void usage(const char* argv0)
111 fprintf(stderr, "Usage: %s [options] <device>\n", argv0);
112 fprintf(stderr, "Options:\n");
113 fprintf(stderr, " -d [+|-] create short positive/negative pulse on DTR\n");
114 fprintf(stderr, " -e ignore '~.' escape sequence\n");
115 fprintf(stderr, " -n do not switch the device to raw mode\n");
116 fprintf(stderr, " -r [+|-] create short positive/negative pulse on RTS\n");
117 fprintf(stderr, " -s <baudrate>\n");
118 fprintf(stderr, " -v verbose\n");
121 int main(int argc, char *argv[])
127 int dtr = 0, rts = 0;
132 if ((stdin_tty = isatty(0))) {
133 CHECK(tcgetattr(0, &stdin_tio_backup));
134 atexit(restore_stdin_term);
137 while ((opt = getopt(argc, argv, "nd::er::s:v")) != -1) {
139 case 'd': dtr = dtr_rts_arg(opt); break;
140 case 'e': exit_on_escape = false; break;
141 case 'n': raw = false; break;
142 case 'r': rts = dtr_rts_arg(opt); break;
144 int s = atoi(optarg);
146 #define S(s) case s: speed = B##s; break;
168 fprintf(stderr, "Unknown baud rate %d\n", s);
186 fprintf(stderr, "No device specified\n");
191 signal(SIGINT, sighandler);
192 signal(SIGTERM, sighandler);
193 signal(SIGHUP, sighandler);
195 if (strncmp(dev, "/dev/", 5) == 0 &&
196 strrchr(dev, '/') == dev + 4 &&
198 { /* Create lock file (to be inter-operable with other programs) */
199 /* This is racy, but what we can do - see also comments in uucp / cu */
200 int tmp = CHECK(mkstemp(template));
201 on_exit(rm_file, template);
203 snprintf(pid, sizeof(pid), "%u", getpid());
204 CHECK(write(tmp, pid, strlen(pid)));
206 snprintf(lockfile, sizeof(lockfile), "/var/lock/LCK..%s", dev + 5);
208 if (link(template, lockfile) == -1) {
209 tmp = CHECK(open(lockfile, O_RDONLY));
210 CHECK(read(tmp, pid, sizeof(pid)));
214 snprintf(proc, sizeof(proc), "/proc/%d", p);
215 if (access(proc, F_OK) == 0) {
216 fprintf(stderr, "%s is used by PID %d\n", dev, p);
219 fprintf(stderr, "Stale lock file %s (PID %d) - removing it!\n", lockfile, p);
220 CHECK(unlink(lockfile));
223 rm_file(0, template);
224 on_exit(rm_file, lockfile);
227 if ((fd = open(dev, O_RDWR)) < 0) {
233 CHECK(ioctl(fd, TIOCEXCL, NULL));
235 CHECK(tcgetattr(fd, &tio));
240 CHECK(cfsetospeed(&tio, speed));
241 CHECK(cfsetispeed(&tio, speed));
246 /* tio.c_cflag &= ~HUPCL; */ /* Don't lower DTR/RTS on close */
248 CHECK(ioctl(fd, TIOCMGET, &status));
249 if (dtr == -1) status &= ~TIOCM_DTR;
250 if (dtr == +1) status |= TIOCM_DTR;
251 if (rts == -1) status &= ~TIOCM_RTS;
252 if (rts == +1) status |= TIOCM_RTS;
253 CHECK(ioctl(fd, TIOCMSET, &status));
256 CHECK(tcsetattr(fd, TCSANOW, &tio));
257 } else if (speed || dtr || rts) {
258 fprintf(stderr, "Cannot set speed, DTR or RTS on non-terminal %s\n", dev);
262 struct pollfd fds[2] = {
263 { .fd = 0, .events = POLLIN },
264 { .fd = fd, .events = POLLIN },
269 tio = stdin_tio_backup;
272 CHECK(tcsetattr(0, TCSANOW, &tio));
275 VERBOSE("Connected.\r\n");
277 VERBOSE("Use '<Enter>~.' sequence to exit.\r\n");
281 CHECK(poll(fds, 2, -1));
282 if (fds[0].revents & POLLIN) {
283 r1 = CHECK(read(0, buf, sizeof(buf)));
285 VERBOSE("EOF on stdin\r\n");
289 exit_on_escapeseq(buf, r1);
290 r2 = CHECK(write(fd, buf, r1));
292 fprintf(stderr, "Not all data written to %s (%d/%d)\n", dev, r1, r2);
296 if (fds[1].revents & POLLIN) {
297 r1 = CHECK(read(fd, buf, sizeof(buf)));
299 VERBOSE("EOF on %s\r\n", dev);
302 r2 = CHECK(write(1, buf, r1));
304 fprintf(stderr, "Not all data written to stdout (%d/%d)\n", r1, r2);