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, format, ##__VA_ARGS__); } while (0)
54 char template[] = "/var/lock/TMPXXXXXX";
56 struct termios stdin_tio_backup;
58 void rm_file(int status, void *arg)
66 void restore_stdin_term()
68 tcsetattr(0, TCSANOW, &stdin_tio_backup);
71 void sighandler(int arg)
73 exit(0); /* Invoke exit handlers */
76 int dtr_rts_arg(const char option)
82 case '+': val = +1; break;
83 case '-': val = -1; break;
85 fprintf(stderr, "Unknown -%c argument: %s", option, optarg);
93 int main(int argc, char *argv[])
104 if ((stdin_tty = isatty(0))) {
105 CHECK(tcgetattr(0, &stdin_tio_backup));
106 atexit(restore_stdin_term);
109 while ((opt = getopt(argc, argv, "nd::r::s:v")) != -1) {
111 case 'd': dtr = dtr_rts_arg(opt); break;
112 case 'n': raw = false; break;
113 case 'r': rts = dtr_rts_arg(opt); break;
115 int s = atoi(optarg);
117 #define S(s) case s: speed = B##s; break;
139 fprintf(stderr, "Unknown baud rate %d\n", s);
148 fprintf(stderr, "Usage: %s [-s baudrate] [-v] <device>\n", argv[0]);
157 fprintf(stderr, "No device specified\n");
161 signal(SIGINT, sighandler);
162 signal(SIGTERM, sighandler);
163 signal(SIGHUP, sighandler);
165 if (strncmp(dev, "/dev/", 5) == 0 &&
166 strrchr(dev, '/') == dev + 4 &&
168 { /* Create lock file (to be inter-operable with other programs) */
169 /* This is racy, but what we can do - see also comments in uucp / cu */
170 int tmp = CHECK(mkstemp(template));
171 on_exit(rm_file, template);
173 snprintf(pid, sizeof(pid), "%u", getpid());
174 CHECK(write(tmp, pid, strlen(pid)));
176 snprintf(lockfile, sizeof(lockfile), "/var/lock/LCK..%s", dev + 5);
178 if (link(template, lockfile) == -1) {
179 tmp = CHECK(open(lockfile, O_RDONLY));
180 CHECK(read(tmp, pid, sizeof(pid)));
184 snprintf(proc, sizeof(proc), "/proc/%d", p);
185 if (access(proc, F_OK) == 0) {
186 fprintf(stderr, "%s is used by PID %d\n", dev, p);
189 fprintf(stderr, "Stale lock file %s (PID %d) - removing it!\n", lockfile, p);
190 CHECK(unlink(lockfile));
193 rm_file(0, template);
194 on_exit(rm_file, lockfile);
197 if ((fd = open(dev, O_RDWR)) < 0) {
203 CHECK(ioctl(fd, TIOCEXCL, NULL));
205 CHECK(tcgetattr(fd, &tio));
210 CHECK(cfsetospeed(&tio, speed));
211 CHECK(cfsetispeed(&tio, speed));
216 /* tio.c_cflag &= ~HUPCL; */ /* Don't lower DTR/RTS on close */
218 CHECK(ioctl(fd, TIOCMGET, &status));
219 if (dtr == -1) status &= ~TIOCM_DTR;
220 if (dtr == +1) status |= TIOCM_DTR;
221 if (rts == -1) status &= ~TIOCM_RTS;
222 if (rts == +1) status |= TIOCM_RTS;
223 CHECK(ioctl(fd, TIOCMSET, &status));
226 CHECK(tcsetattr(fd, TCSANOW, &tio));
227 } else if (speed || dtr || rts) {
228 fprintf(stderr, "Cannot set speed, DTR or RTS on non-terminal %s\n", dev);
232 struct pollfd fds[2] = {
233 { .fd = 0, .events = POLLIN },
234 { .fd = fd, .events = POLLIN },
239 tio = stdin_tio_backup;
242 CHECK(tcsetattr(0, TCSANOW, &tio));
245 VERBOSE("Connected.\n");
248 CHECK(poll(fds, 2, -1));
249 if (fds[0].revents & POLLIN) {
250 r1 = CHECK(read(0, buf, sizeof(buf)));
252 VERBOSE("EOF on stdin\n");
255 r2 = CHECK(write(fd, buf, r1));
257 fprintf(stderr, "Not all data written to %s (%d/%d)\n", dev, r1, r2);
261 if (fds[1].revents & POLLIN) {
262 r1 = CHECK(read(fd, buf, sizeof(buf)));
264 VERBOSE("EOF on %s\n", dev);
267 r2 = CHECK(write(1, buf, r1));
269 fprintf(stderr, "Not all data written to stdout (%d/%d)\n", r1, r2);