ubi_tools: a bit smaller applet resolution code
[oweals/busybox.git] / miscutils / microcom.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * bare bones 'talk to modem' program - similar to 'cu -l $device'
4  * inspired by mgetty's microcom
5  *
6  * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
7  *
8  * Licensed under GPLv2, see file LICENSE in this source tree.
9  */
10 //config:config MICROCOM
11 //config:       bool "microcom (5.6 kb)"
12 //config:       default y
13 //config:       help
14 //config:       The poor man's minicom utility for chatting with serial port devices.
15
16 //applet:IF_MICROCOM(APPLET(microcom, BB_DIR_USR_BIN, BB_SUID_DROP))
17
18 //kbuild:lib-$(CONFIG_MICROCOM) += microcom.o
19
20 //usage:#define microcom_trivial_usage
21 //usage:       "[-d DELAY] [-t TIMEOUT] [-s SPEED] [-X] TTY"
22 //usage:#define microcom_full_usage "\n\n"
23 //usage:       "Copy bytes for stdin to TTY and from TTY to stdout\n"
24 //usage:     "\n        -d      Wait up to DELAY ms for TTY output before sending every"
25 //usage:     "\n                next byte to it"
26 //usage:     "\n        -t      Exit if both stdin and TTY are silent for TIMEOUT ms"
27 //usage:     "\n        -s      Set serial line to SPEED"
28 //usage:     "\n        -X      Disable special meaning of NUL and Ctrl-X from stdin"
29
30 #include "libbb.h"
31 #include "common_bufsiz.h"
32
33 // set raw tty mode
34 static void xget1(int fd, struct termios *t, struct termios *oldt)
35 {
36 //TODO: use set_termios_to_raw()
37         tcgetattr(fd, oldt);
38         *t = *oldt;
39         cfmakeraw(t);
40 //      t->c_lflag &= ~(ISIG|ICANON|ECHO|IEXTEN);
41 //      t->c_iflag &= ~(BRKINT|IXON|ICRNL);
42 //      t->c_oflag &= ~(ONLCR);
43 //      t->c_cc[VMIN]  = 1;
44 //      t->c_cc[VTIME] = 0;
45 }
46
47 static int xset1(int fd, struct termios *tio, const char *device)
48 {
49         int ret = tcsetattr(fd, TCSAFLUSH, tio);
50
51         if (ret) {
52                 bb_perror_msg("can't tcsetattr for %s", device);
53         }
54         return ret;
55 }
56
57 int microcom_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
58 int microcom_main(int argc UNUSED_PARAM, char **argv)
59 {
60         int sfd;
61         int nfd;
62         struct pollfd pfd[2];
63         struct termios tio0, tiosfd, tio;
64         char *device_lock_file;
65         enum {
66                 OPT_X = 1 << 0, // do not respect Ctrl-X, Ctrl-@
67                 OPT_s = 1 << 1, // baudrate
68                 OPT_d = 1 << 2, // wait for device response, ms
69                 OPT_t = 1 << 3, // timeout, ms
70         };
71         speed_t speed = 9600;
72         int delay = -1;
73         int timeout = -1;
74         unsigned opts;
75
76         // fetch options
77         opt_complementary = "=1";
78         opts = getopt32(argv, "Xs:+d:+t:+", &speed, &delay, &timeout);
79 //      argc -= optind;
80         argv += optind;
81
82         // try to create lock file in /var/lock
83         device_lock_file = (char *)bb_basename(argv[0]);
84         device_lock_file = xasprintf("/var/lock/LCK..%s", device_lock_file);
85         sfd = open(device_lock_file, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644);
86         if (sfd < 0) {
87                 // device already locked -> bail out
88                 if (errno == EEXIST)
89                         bb_perror_msg_and_die("can't create '%s'", device_lock_file);
90                 // can't create lock -> don't care
91                 if (ENABLE_FEATURE_CLEAN_UP)
92                         free(device_lock_file);
93                 device_lock_file = NULL;
94         } else {
95                 // %4d to make concurrent mgetty (if any) happy.
96                 // Mgetty treats 4-bytes lock files as binary,
97                 // not text, PID. Making 5+ char file. Brrr...
98                 fdprintf(sfd, "%4d\n", getpid());
99                 close(sfd);
100         }
101
102         // setup signals
103         bb_signals(0
104                 + (1 << SIGHUP)
105                 + (1 << SIGINT)
106                 + (1 << SIGTERM)
107                 + (1 << SIGPIPE)
108                 , record_signo);
109
110         // error exit code if we fail to open the device
111         bb_got_signal = 1;
112
113         // open device
114         sfd = open_or_warn(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK);
115         if (sfd < 0)
116                 goto done;
117         fcntl(sfd, F_SETFL, O_RDWR);
118
119         // put device to "raw mode"
120         xget1(sfd, &tio, &tiosfd);
121         // set device speed
122         cfsetspeed(&tio, tty_value_to_baud(speed));
123         if (xset1(sfd, &tio, argv[0]))
124                 goto done;
125
126         // put stdin to "raw mode" (if stdin is a TTY),
127         // handle one character at a time
128         if (isatty(STDIN_FILENO)) {
129                 xget1(STDIN_FILENO, &tio, &tio0);
130                 if (xset1(STDIN_FILENO, &tio, "stdin"))
131                         goto done;
132         }
133
134         // main loop: check with poll(), then read/write bytes across
135         pfd[0].fd = sfd;
136         pfd[0].events = POLLIN;
137         pfd[1].fd = STDIN_FILENO;
138         pfd[1].events = POLLIN;
139
140         bb_got_signal = 0;
141         nfd = 2;
142         // Not safe_poll: we want to exit on signal
143         while (!bb_got_signal && poll(pfd, nfd, timeout) > 0) {
144                 if (nfd > 1 && pfd[1].revents) {
145                         char c;
146                         // read from stdin -> write to device
147                         if (safe_read(STDIN_FILENO, &c, 1) < 1) {
148                                 // don't poll stdin anymore if we got EOF/error
149                                 nfd--;
150                                 goto skip_write;
151                         }
152                         // do we need special processing?
153                         if (!(opts & OPT_X)) {
154                                 // ^@ sends Break
155                                 if (VINTR == c) {
156                                         tcsendbreak(sfd, 0);
157                                         goto skip_write;
158                                 }
159                                 // ^X exits
160                                 if (24 == c)
161                                         break;
162                         }
163                         write(sfd, &c, 1);
164                         if (delay >= 0)
165                                 safe_poll(pfd, 1, delay);
166 skip_write: ;
167                 }
168                 if (pfd[0].revents) {
169                         ssize_t len;
170 #define iobuf bb_common_bufsiz1
171                         setup_common_bufsiz();
172                         // read from device -> write to stdout
173                         len = safe_read(sfd, iobuf, COMMON_BUFSIZE);
174                         if (len > 0)
175                                 full_write(STDOUT_FILENO, iobuf, len);
176                         else {
177                                 // EOF/error -> bail out
178                                 bb_got_signal = SIGHUP;
179                                 break;
180                         }
181                 }
182         }
183
184         // restore device mode
185         tcsetattr(sfd, TCSAFLUSH, &tiosfd);
186
187         if (isatty(STDIN_FILENO))
188                 tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0);
189
190 done:
191         if (device_lock_file)
192                 unlink(device_lock_file);
193
194         return bb_got_signal;
195 }