]> rtime.felk.cvut.cz Git - sojka/sterm.git/blob - sterm.c
Fix usage text about the -n option
[sojka/sterm.git] / sterm.c
1 /*
2  * Simple serial terminal
3  *
4  * Copyright 2014, 2015, 2016, 2017, 2019 Michal Sojka <sojkam1@fel.cvut.cz>
5  *
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.
10  *
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.
15  *
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/>.
19  */
20
21 /*
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.
26  *
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.
30  */
31
32 #define _BSD_SOURCE
33 #define _DEFAULT_SOURCE
34 #define _GNU_SOURCE
35 #include <sys/ioctl.h>
36 #include <sys/types.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <termios.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <getopt.h>
43 #include <poll.h>
44 #include <stdbool.h>
45 #include <string.h>
46 #include <signal.h>
47 #ifdef HAVE_LOCKDEV
48 #include <lockdev.h>
49 #endif
50 #include <sys/file.h>
51 #include <errno.h>
52
53 #define STRINGIFY(val) #val
54 #define TOSTRING(val) STRINGIFY(val)
55 #define CHECK(cmd) ({ int ret = (cmd); if (ret == -1) { perror(#cmd " line " TOSTRING(__LINE__)); exit(1); }; ret; })
56 #define CHECKPTR(cmd) ({ void *ptr = (cmd); if (ptr == (void*)-1) { perror(#cmd " line " TOSTRING(__LINE__)); exit(1); }; ptr; })
57 #define CHECKNULL(cmd) ({ void *ptr = (cmd); if (ptr == NULL) { perror(#cmd " line " TOSTRING(__LINE__)); exit(1); }; ptr; })
58
59 #define VERBOSE(format, ...) do { if (verbose) fprintf(stderr, "sterm: " format, ##__VA_ARGS__); } while (0)
60
61 bool verbose = false;
62 bool exit_on_escape = true;
63
64 struct termios stdin_tio_backup;
65 char *dev = NULL;
66
67 void rm_file(int status, void *arg)
68 {
69         char *fn = arg;
70         if (fn[0])
71                 unlink(fn);
72         fn[0] = 0;
73 }
74
75 void restore_stdin_term()
76 {
77         tcsetattr(0, TCSANOW, &stdin_tio_backup);
78 }
79
80 #ifdef HAVE_LOCKDEV
81 void unlock()
82 {
83         dev_unlock(dev, getpid());
84 }
85 #endif
86
87 void sighandler(int arg)
88 {
89         exit(0); /* Invoke exit handlers */
90 }
91
92 int dtr_rts_arg(const char option, const char *optarg)
93 {
94         int val = -1;
95
96         if (optarg) {
97                 char *end;
98                 val = strtol(optarg, &end, 10);
99                 if (end == optarg) {
100                         /* Not a number */
101                         switch (optarg[0]) {
102                         case '+': val = +1; break;
103                         case '-': val = -1; break;
104                         default:
105                                 fprintf(stderr, "Unknown -%c argument: %s\n", option, optarg);
106                                 exit(1);
107                         }
108                 }
109         }
110         return val;
111 }
112
113 void exit_on_escapeseq(const char *buf, int len)
114 {
115         static const char escseq[] = "\r~.";
116         static const char *state = escseq+1;
117         int i;
118         for (i = 0; i < len; i++) {
119                 if (buf[i] == *state) {
120                         state++;
121                         if (*state == 0)
122                                 exit(0);
123                 } else {
124                         state = escseq;
125                         if (buf[i] == *state)
126                                 state++;
127                 }
128         }
129 }
130
131 void usage(const char* argv0)
132 {
133         fprintf(stderr, "Usage: %s [options] <device>\n", argv0);
134         fprintf(stderr,
135                 "Options:\n"
136                 "  -b <duration> send break signal\n"
137                 "  -c        enter command mode\n"
138                 "  -d[PULSE] make pulse on DTR\n"
139                 "  -e        ignore '~.' escape sequence\n"
140                 "  -n        do not switch stdin TTY to raw mode\n"
141                 "  -r[PULSE] make pulse on RTS\n"
142                 "  -s <baudrate>\n"
143                 "  -v        verbose mode\n"
144                 "\n"
145                 "PULSE is a number specifying the pulse. Absolute value defines the\n"
146                 "length of the pulse in milliseconds, sign determines the polarity of\n"
147                 "the pulse. Alternatively, PULSE can be either '+' or '-', which\n"
148                 "corresponds to +1 or -1.\n"
149                 );
150 }
151
152 void pulse(int fd, int dtr, int rts)
153 {
154         int status, ms = 0;
155         /* tio.c_cflag &= ~HUPCL; */ /* Don't lower DTR/RTS on close */
156
157         CHECK(ioctl(fd, TIOCMGET, &status));
158         if (dtr > 0) { status &= ~TIOCM_DTR; ms = +dtr; }
159         if (dtr < 0) { status |=  TIOCM_DTR; ms = -dtr; }
160         if (rts > 0) { status &= ~TIOCM_RTS; ms = +rts; }
161         if (rts < 0) { status |=  TIOCM_RTS; ms = -rts; }
162         CHECK(ioctl(fd, TIOCMSET, &status));
163
164         usleep(ms*1000);
165
166         if (dtr < 0) { status &= ~TIOCM_DTR; }
167         if (dtr > 0) { status |=  TIOCM_DTR; }
168         if (rts < 0) { status &= ~TIOCM_RTS; }
169         if (rts > 0) { status |=  TIOCM_RTS; }
170         CHECK(ioctl(fd, TIOCMSET, &status));
171 }
172
173 void handle_commands(int fd)
174 {
175         char command[100];
176         bool go = false;
177
178         while (!go) {
179                 char *p1 = NULL;
180                 int num;
181                 if (fgets(command, sizeof(command), stdin) == NULL) {
182                         if (!feof(stdin))
183                             perror("Command read");
184                         exit(1);
185                 }
186                 if (sscanf(command, "dtr %ms", &p1) == 1)
187                         pulse(fd, dtr_rts_arg('d', p1), 0);
188                 else if (sscanf(command, "rts %ms", &p1) == 1)
189                         pulse(fd, 0, dtr_rts_arg('r', p1));
190                 else if (sscanf(command, "break %d", &num) == 1)
191                         CHECK(tcsendbreak(fd, num));
192                 else if (strcmp(command, "go\n") == 0)
193                         break;
194                 else if (strcmp(command, "exit\n") == 0)
195                         exit(0);
196                 else {
197                         fprintf(stderr, "Unknown command: %s\n", command);
198                         exit(1);
199                 }
200
201                 free(p1);
202         }
203 }
204
205 int main(int argc, char *argv[])
206 {
207         int fd;
208         int opt;
209         speed_t speed = 0;
210         int dtr = 0, rts = 0;
211         struct termios tio;
212         bool stdin_tty;
213         bool raw = true;
214         bool cmd = false;
215         int break_dur = -1;
216
217         if ((stdin_tty = isatty(0))) {
218                 CHECK(tcgetattr(0, &stdin_tio_backup));
219                 atexit(restore_stdin_term);
220         }
221
222         while ((opt = getopt(argc, argv, "b:cnd::er::s:v")) != -1) {
223                 switch (opt) {
224                 case 'b': break_dur = atoi(optarg); break;
225                 case 'c': cmd = true; break;
226                 case 'd': dtr = dtr_rts_arg(opt, optarg); break;
227                 case 'e': exit_on_escape = false; break;
228                 case 'n': raw = false; break;
229                 case 'r': rts = dtr_rts_arg(opt, optarg); break;
230                 case 's': {
231                         int s = atoi(optarg);
232                         switch (s) {
233 #define S(s) case s: speed = B##s; break;
234                                 S(0);
235                                 S(50);
236                                 S(75);
237                                 S(110);
238                                 S(134);
239                                 S(150);
240                                 S(200);
241                                 S(300);
242                                 S(600);
243                                 S(1200);
244                                 S(1800);
245                                 S(2400);
246                                 S(4800);
247                                 S(9600);
248                                 S(19200);
249                                 S(38400);
250                                 S(57600);
251                                 S(115200);
252                                 S(230400);
253                                 S(460800);
254                                 S(500000);
255                                 S(576000);
256                                 S(921600);
257                                 S(1000000);
258                                 S(1152000);
259                                 S(1500000);
260                                 S(2000000);
261                                 S(2500000);
262                                 S(3000000);
263                                 S(3500000);
264                                 S(4000000);
265 #undef S
266                         default:
267                                 fprintf(stderr, "Unknown baud rate %d\n", s);
268                                 exit(1);
269                         }
270                         break;
271                 }
272                 case 'v':
273                         verbose = true;
274                         break;
275                 default: /* '?' */
276                         usage(argv[0]);
277                         exit(1);
278                 }
279         }
280
281         if (optind < argc)
282                 dev = argv[optind];
283
284         if (!dev) {
285                 fprintf(stderr, "No device specified\n");
286                 usage(argv[0]);
287                 exit(1);
288         }
289
290         signal(SIGINT, sighandler);
291         signal(SIGTERM, sighandler);
292         signal(SIGHUP, sighandler);
293
294 #ifdef HAVE_LOCKDEV
295         pid_t pid = dev_lock(dev);
296         if (pid > 0) {
297                 fprintf(stderr, "%s is used by PID %d\n", dev, pid);
298                 exit(1);
299         } else if (pid < 0) {
300                 char *msg;
301                 asprintf(&msg, "dev_lock('%s')", dev); /* No free() because we exit() immediately */
302                 if (errno)
303                         perror(msg);
304                 else
305                         fprintf(stderr, "%s: Error\n", msg);
306                 exit(1);
307         }
308         atexit(unlock);
309 #endif
310
311         /* O_NONBLOCK is needed to not wait for the CDC signal. See tty_ioctl(4). */
312         if ((fd = open(dev, O_RDWR|O_NOCTTY|O_NONBLOCK)) < 0) {
313                 perror(dev);
314                 exit(1);
315         }
316         /* Cancel the efect of O_NONBLOCK flag. */
317         int n = fcntl(fd, F_GETFL, 0);
318         fcntl(fd, F_SETFL, n & ~O_NDELAY);
319
320         flock(fd, LOCK_EX);
321
322         if (isatty(fd)) {
323                 CHECK(ioctl(fd, TIOCEXCL, NULL));
324
325                 CHECK(tcgetattr(fd, &tio));
326
327                 cfmakeraw(&tio);
328
329                 if (speed) {
330                         CHECK(cfsetospeed(&tio, speed));
331                         CHECK(cfsetispeed(&tio, speed));
332                 }
333
334                 if (dtr || rts)
335                         pulse(fd, dtr, rts);
336
337                 if (break_dur != -1)
338                         CHECK(tcsendbreak(fd, break_dur));
339
340                  /* Disable flow control */
341                 tio.c_cflag &= ~(CRTSCTS);
342                 tio.c_iflag &= ~(IXON|IXOFF);
343
344                 CHECK(tcsetattr(fd, TCSANOW, &tio));
345         } else if (speed || dtr || rts) {
346                 fprintf(stderr, "Cannot set speed, DTR or RTS on non-terminal %s\n", dev);
347                 exit(1);
348         }
349
350         VERBOSE("Connected.\r\n");
351
352         if (cmd)
353                 handle_commands(fd);
354
355         struct pollfd fds[2] = {
356                 { .fd = 0,  .events = POLLIN },
357                 { .fd = fd, .events = POLLIN },
358         };
359         char buf[4096];
360
361         if (stdin_tty) {
362                 tio = stdin_tio_backup;
363                 if (raw)
364                         cfmakeraw(&tio);
365                 CHECK(tcsetattr(0, TCSANOW, &tio));
366         }
367
368         if (exit_on_escape)
369                 VERBOSE("Use '<Enter>~.' sequence to exit.\r\n");
370
371         while (1) {
372                 int r1, r2;
373                 CHECK(poll(fds, 2, -1));
374                 if (fds[0].revents & POLLIN) {
375                         r1 = CHECK(read(0, buf, sizeof(buf)));
376                         if (r1 == 0) {
377                                 VERBOSE("EOF on stdin\r\n");
378                                 break;
379                         }
380                         if (exit_on_escape)
381                                 exit_on_escapeseq(buf, r1);
382                         r2 = CHECK(write(fd, buf, r1));
383                         if (r1 != r2) {
384                                 fprintf(stderr, "Not all data written to %s (%d/%d)\n", dev, r1, r2);
385                                 exit(1);
386                         }
387                 }
388                 if (fds[0].revents & POLLHUP) {
389                         VERBOSE("HUP on stdin\r\n");
390                         break;
391                 }
392                 if (fds[1].revents & POLLIN) {
393                         r1 = CHECK(read(fd, buf, sizeof(buf)));
394                         if (r1 == 0) {
395                                 VERBOSE("EOF on %s\r\n", dev);
396                                 break;
397                         }
398                         r2 = CHECK(write(1, buf, r1));
399                         if (r1 != r2) {
400                                 fprintf(stderr, "Not all data written to stdout (%d/%d)\n", r1, r2);
401                                 exit(1);
402                         }
403                 }
404         }
405         return 0;
406 }