]> rtime.felk.cvut.cz Git - sojka/sterm.git/blob - sterm.c
default.nix: include completion
[sojka/sterm.git] / sterm.c
1 /*
2  * Simple serial terminal
3  *
4  * Copyright 2014, 2015, 2016, 2017, 2019, 2020, 2021 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 <stdint.h>
46 #include <string.h>
47 #include <signal.h>
48 #ifdef HAVE_LOCKDEV
49 #include <lockdev.h>
50 #endif
51 #include <sys/file.h>
52 #include <time.h>
53 #include <errno.h>
54
55 #define STRINGIFY(val) #val
56 #define TOSTRING(val) STRINGIFY(val)
57 #define CHECK(cmd) ({ int ret = (cmd); if (ret == -1) { perror(#cmd " line " TOSTRING(__LINE__)); exit(1); }; ret; })
58 #define CHECKPTR(cmd) ({ void *ptr = (cmd); if (ptr == (void*)-1) { perror(#cmd " line " TOSTRING(__LINE__)); exit(1); }; ptr; })
59 #define CHECKNULL(cmd) ({ void *ptr = (cmd); if (ptr == NULL) { perror(#cmd " line " TOSTRING(__LINE__)); exit(1); }; ptr; })
60
61 #define VERBOSE(format, ...) do { if (verbose) fprintf(stderr, "sterm: " format, ##__VA_ARGS__); } while (0)
62
63 #define MAX(X, Y) (((X) > (Y)) ? (X) : (Y))
64
65 bool verbose = false;
66 bool exit_on_escape = true;
67
68 struct termios stdin_tio_backup;
69 char *dev = NULL;
70
71 void rm_file(int status, void *arg)
72 {
73         char *fn = arg;
74         if (fn[0])
75                 unlink(fn);
76         fn[0] = 0;
77 }
78
79 void restore_stdin_term()
80 {
81         tcsetattr(0, TCSANOW, &stdin_tio_backup);
82 }
83
84 #ifdef HAVE_LOCKDEV
85 void unlock()
86 {
87         dev_unlock(dev, getpid());
88 }
89 #endif
90
91 void sighandler(int arg)
92 {
93         exit(0); /* Invoke exit handlers */
94 }
95
96 int dtr_rts_arg(const char option, const char *optarg)
97 {
98         int val = -1;
99
100         if (optarg) {
101                 char *end;
102                 val = strtol(optarg, &end, 10);
103                 if (end == optarg) {
104                         /* Not a number */
105                         switch (optarg[0]) {
106                         case '+': val = +1; break;
107                         case '-': val = -1; break;
108                         default:
109                                 fprintf(stderr, "Unknown -%c argument: %s\n", option, optarg);
110                                 exit(1);
111                         }
112                 }
113         }
114         return val;
115 }
116
117 // See DSR and CPR at
118 // https://en.wikipedia.org/wiki/ANSI_escape_code#Terminal_output_sequences
119 bool is_cpr_control_seq(char c)
120 {
121         static enum state { CSI_ESC, CSI_BRACKET, PAR_N, PAR_M, FIN_R } state;
122
123         switch (state) {
124         case CSI_ESC:      state = (c == 0x1b) ? CSI_BRACKET : CSI_ESC; break;
125         case CSI_BRACKET:  state = (c == '[')  ? PAR_N : CSI_ESC; break;
126         case PAR_N:        state = (c == ';')  ? PAR_M : (c >= '0' && c <= '9' ? PAR_N : CSI_ESC); break;
127         case PAR_M:        state = (c == 'R')  ? FIN_R : (c >= '0' && c <= '9' ? PAR_M : CSI_ESC); break;
128         case FIN_R: break;
129         }
130         if (state == FIN_R) {
131                 state = CSI_ESC;
132                 return true;
133
134         } else
135                 return state != CSI_ESC;
136 }
137
138 void exit_on_escapeseq(const char *buf, int len)
139 {
140         static const char escseq[] = "\r~.";
141         static const char *state = escseq+1;
142         int i;
143         for (i = 0; i < len; i++) {
144                 if (is_cpr_control_seq(buf[i]))
145                         continue;
146                 if (buf[i] == *state) {
147                         state++;
148                         if (*state == 0)
149                                 exit(0);
150                 } else {
151                         state = escseq;
152                         if (buf[i] == *state)
153                                 state++;
154                 }
155         }
156 }
157
158 void print_usage_and_exit(const char* argv0, int exit_code)
159 {
160         fprintf(exit_code == 0 ? stdout : stderr, "Usage: %s [options] <device>\n", argv0);
161         fprintf(exit_code == 0 ? stdout : stderr,
162                 "Options:\n"
163                 "  -b <duration> send break signal\n"
164                 "  -c        enter command mode\n"
165                 "  -d[PULSE] make pulse on DTR\n"
166                 "  -e        ignore '~.' escape sequence\n"
167                 "  -h, --help  print help and exit\n"
168                 "  -n        do not switch stdin TTY to raw mode\n"
169                 "  -r[PULSE] make pulse on RTS\n"
170                 "  -s <baudrate>\n"
171                 "  -t <ms>   minimum delay between two transmitted characters\n"
172                 "  -v        verbose mode\n"
173                 "  -V, --version  print version and exit\n"
174                 "\n"
175                 "PULSE is a number specifying the pulse. Absolute value defines the\n"
176                 "length of the pulse in milliseconds, sign determines the polarity of\n"
177                 "the pulse. Alternatively, PULSE can be either '+' or '-', which\n"
178                 "corresponds to '+1' or '-1'.\n"
179                 );
180         exit(exit_code);
181 }
182
183 void pulse(int fd, int dtr, int rts)
184 {
185         int status, ms = 0;
186         /* tio.c_cflag &= ~HUPCL; */ /* Don't lower DTR/RTS on close */
187
188         CHECK(ioctl(fd, TIOCMGET, &status));
189         if (dtr > 0) { status &= ~TIOCM_DTR; ms = +dtr; }
190         if (dtr < 0) { status |=  TIOCM_DTR; ms = -dtr; }
191         if (rts > 0) { status &= ~TIOCM_RTS; ms = +rts; }
192         if (rts < 0) { status |=  TIOCM_RTS; ms = -rts; }
193         CHECK(ioctl(fd, TIOCMSET, &status));
194
195         usleep(ms*1000);
196
197         if (dtr < 0) { status &= ~TIOCM_DTR; }
198         if (dtr > 0) { status |=  TIOCM_DTR; }
199         if (rts < 0) { status &= ~TIOCM_RTS; }
200         if (rts > 0) { status |=  TIOCM_RTS; }
201         CHECK(ioctl(fd, TIOCMSET, &status));
202 }
203
204 void handle_commands(int fd)
205 {
206         char command[100];
207         bool go = false;
208
209         while (!go) {
210                 char *p1 = NULL;
211                 int num;
212                 if (fgets(command, sizeof(command), stdin) == NULL) {
213                         if (!feof(stdin))
214                             perror("Command read");
215                         exit(1);
216                 }
217                 if (sscanf(command, "dtr %ms", &p1) == 1)
218                         pulse(fd, dtr_rts_arg('d', p1), 0);
219                 else if (sscanf(command, "rts %ms", &p1) == 1)
220                         pulse(fd, 0, dtr_rts_arg('r', p1));
221                 else if (sscanf(command, "break %d", &num) == 1)
222                         CHECK(tcsendbreak(fd, num));
223                 else if (strcmp(command, "go\n") == 0)
224                         break;
225                 else if (strcmp(command, "exit\n") == 0)
226                         exit(0);
227                 else {
228                         fprintf(stderr, "Unknown command: %s\n", command);
229                         exit(1);
230                 }
231
232                 free(p1);
233         }
234 }
235
236 static int64_t now_us()
237 {
238         struct timespec ts;
239         CHECK(clock_gettime(CLOCK_MONOTONIC, &ts));
240         return ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
241 }
242
243 int main(int argc, char *argv[])
244 {
245         int fd;
246         int opt;
247         speed_t speed = 0;
248         int dtr = 0, rts = 0;
249         struct termios tio;
250         bool stdin_tty;
251         bool raw = true;
252         bool cmd = false;
253         int break_dur = -1;
254         int tx_delay_ms = 0;
255
256         if ((stdin_tty = isatty(0))) {
257                 CHECK(tcgetattr(0, &stdin_tio_backup));
258                 atexit(restore_stdin_term);
259         }
260
261         static struct option long_options[] = {
262                 {"help",    no_argument,       0,  'h' },
263                 {"version", no_argument,       0,  'V' },
264                 {0,         0,                 0,  0   }
265         };
266
267         while ((opt = getopt_long(argc, argv, "b:cnd::er::s:vVt:h",
268                                   long_options, NULL)) != -1) {
269                 switch (opt) {
270                 case 'b': break_dur = atoi(optarg); break;
271                 case 'c': cmd = true; break;
272                 case 'd': dtr = dtr_rts_arg(opt, optarg); break;
273                 case 'e': exit_on_escape = false; break;
274                 case 'n': raw = false; break;
275                 case 'r': rts = dtr_rts_arg(opt, optarg); break;
276                 case 's': {
277                         int s = atoi(optarg);
278                         switch (s) {
279 #define S(s) case s: speed = B##s; break;
280                                 S(0);
281                                 S(50);
282                                 S(75);
283                                 S(110);
284                                 S(134);
285                                 S(150);
286                                 S(200);
287                                 S(300);
288                                 S(600);
289                                 S(1200);
290                                 S(1800);
291                                 S(2400);
292                                 S(4800);
293                                 S(9600);
294                                 S(19200);
295                                 S(38400);
296                                 S(57600);
297                                 S(115200);
298                                 S(230400);
299                                 S(460800);
300                                 S(500000);
301                                 S(576000);
302                                 S(921600);
303                                 S(1000000);
304                                 S(1152000);
305                                 S(1500000);
306                                 S(2000000);
307                                 S(2500000);
308                                 S(3000000);
309                                 S(3500000);
310                                 S(4000000);
311 #undef S
312                         default:
313                                 fprintf(stderr, "Unknown baud rate %d\n", s);
314                                 exit(1);
315                         }
316                         break;
317                 }
318                 case 't':
319                         tx_delay_ms = atoi(optarg);
320                         break;
321                 case 'v':
322                         verbose = true;
323                         break;
324                 case 'V':
325                         printf("sterm version %s\n", strlen(STERM_VERSION) > 0 ? STERM_VERSION : "unknown");
326                         return 0;
327                 case 'h':
328                         /* fallthrough */
329                 default: /* '?' */
330                         print_usage_and_exit(argv[0], opt == 'h' ? 0 : 1);
331                 }
332         }
333
334         if (optind < argc)
335                 dev = argv[optind];
336
337         if (!dev) {
338                 fprintf(stderr, "No device specified\n");
339                 print_usage_and_exit(argv[0], 1);
340         }
341
342         signal(SIGINT, sighandler);
343         signal(SIGTERM, sighandler);
344         signal(SIGHUP, sighandler);
345
346 #ifdef HAVE_LOCKDEV
347         pid_t pid = dev_lock(dev);
348         if (pid > 0) {
349                 fprintf(stderr, "%s is used by PID %d\n", dev, pid);
350                 exit(1);
351         } else if (pid < 0) {
352                 char *msg;
353                 asprintf(&msg, "dev_lock('%s')", dev); /* No free() because we exit() immediately */
354                 if (errno)
355                         perror(msg);
356                 else
357                         fprintf(stderr, "%s: Error\n", msg);
358                 exit(1);
359         }
360         atexit(unlock);
361 #endif
362
363         /* O_NONBLOCK is needed to not wait for the CDC signal. See tty_ioctl(4). */
364         if ((fd = open(dev, O_RDWR|O_NOCTTY|O_NONBLOCK)) < 0) {
365                 perror(dev);
366                 exit(1);
367         }
368         /* Cancel the efect of O_NONBLOCK flag. */
369         int n = fcntl(fd, F_GETFL, 0);
370         fcntl(fd, F_SETFL, n & ~O_NDELAY);
371
372         flock(fd, LOCK_EX);
373
374         if (isatty(fd)) {
375                 CHECK(ioctl(fd, TIOCEXCL, NULL));
376
377                 CHECK(tcgetattr(fd, &tio));
378
379                 cfmakeraw(&tio);
380
381                 if (speed) {
382                         CHECK(cfsetospeed(&tio, speed));
383                         CHECK(cfsetispeed(&tio, speed));
384                 }
385
386                 if (dtr || rts)
387                         pulse(fd, dtr, rts);
388
389                 if (break_dur != -1)
390                         CHECK(tcsendbreak(fd, break_dur));
391
392                  /* Disable flow control */
393                 tio.c_cflag &= ~(CRTSCTS);
394                 tio.c_iflag &= ~(IXON|IXOFF);
395
396                 CHECK(tcsetattr(fd, TCSANOW, &tio));
397         } else if (speed || dtr || rts) {
398                 fprintf(stderr, "Cannot set speed, DTR or RTS on non-terminal %s\n", dev);
399                 exit(1);
400         }
401
402         VERBOSE("Connected.\r\n");
403
404         if (cmd)
405                 handle_commands(fd);
406
407
408         enum { STDIN, DEV };
409         struct pollfd fds[2] = {
410                 [STDIN] = { .fd = STDIN_FILENO,  .events = POLLIN },
411                 [DEV]   = { .fd = fd,            .events = POLLIN },
412         };
413
414         if (stdin_tty) {
415                 tio = stdin_tio_backup;
416                 if (raw)
417                         cfmakeraw(&tio);
418                 CHECK(tcsetattr(0, TCSANOW, &tio));
419         }
420
421         if (exit_on_escape)
422                 VERBOSE("Use '<Enter>~.' sequence to exit.\r\n");
423
424         char buf2dev[4096];
425         int buf_len = 0, buf_idx = 0;
426         int64_t last_tx_us = 0;
427         while (1) {
428                 int timeout = (tx_delay_ms == 0 || buf_len == 0)
429                         ? -1
430                         : MAX(0, tx_delay_ms - (now_us() - last_tx_us) / 1000);
431
432                 CHECK(poll(fds, 2, timeout));
433                 if (fds[STDIN].revents & POLLIN && buf_len == 0) {
434                         buf_len = CHECK(read(STDIN_FILENO, buf2dev, sizeof(buf2dev)));
435                         if (buf_len == 0) {
436                                 VERBOSE("EOF on stdin\r\n");
437                                 break;
438                         }
439                         buf_idx = 0;
440                         if (exit_on_escape)
441                                 exit_on_escapeseq(buf2dev, buf_len);
442                 }
443                 if (buf_len > 0) {
444                         int wlen = 0;
445                         bool short_write = false;
446                         if (tx_delay_ms == 0) {
447                                 wlen = CHECK(write(fd, buf2dev, buf_len));
448                                 short_write = wlen != buf_len;
449
450                         } else {
451                                 int64_t now = now_us();
452                                 if (now - last_tx_us >= tx_delay_ms * 1000) {
453                                         wlen = CHECK(write(fd, &buf2dev[buf_idx], 1));
454                                         short_write = wlen != 1;
455                                         last_tx_us = now;
456                                 }
457                         }
458                         if (short_write) {
459                                 fprintf(stderr, "Not all data written to %s (%d/%d)\n", dev, buf_len, wlen);
460                                 exit(1);
461                         }
462                         buf_idx += wlen;
463                         if (buf_idx >= buf_len)
464                                 buf_len = 0;
465                 }
466                 if (fds[STDIN].revents & POLLHUP) {
467                         VERBOSE("HUP on stdin\r\n");
468                         break;
469                 }
470                 if (fds[DEV].revents & POLLIN) {
471                         char buf[1024];
472                         int rlen, wlen;
473                         rlen = CHECK(read(fd, buf, sizeof(buf)));
474                         if (rlen == 0) {
475                                 VERBOSE("EOF on %s\r\n", dev);
476                                 break;
477                         }
478                         wlen = CHECK(write(STDOUT_FILENO, buf, rlen));
479                         if (rlen != wlen) {
480                                 fprintf(stderr, "Not all data written to stdout (%d/%d)\n", wlen, rlen);
481                                 exit(1);
482                         }
483                 }
484         }
485         return 0;
486 }