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