whitespace fixes
[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 tarball for details.
9  */
10 #include "libbb.h"
11
12 /* All known arches use small ints for signals */
13 static volatile smallint signalled;
14
15 static void signal_handler(int signo)
16 {
17         signalled = signo;
18 }
19
20 // set raw tty mode
21 static void xget1(int fd, struct termios *t, struct termios *oldt)
22 {
23         tcgetattr(fd, oldt);
24         *t = *oldt;
25         cfmakeraw(t);
26 //      t->c_lflag &= ~(ISIG|ICANON|ECHO|IEXTEN);
27 //      t->c_iflag &= ~(BRKINT|IXON|ICRNL);
28 //      t->c_oflag &= ~(ONLCR);
29 //      t->c_cc[VMIN]  = 1;
30 //      t->c_cc[VTIME] = 0;
31 }
32
33 static int xset1(int fd, struct termios *tio, const char *device)
34 {
35         int ret = tcsetattr(fd, TCSAFLUSH, tio);
36
37         if (ret) {
38                 bb_perror_msg("can't tcsetattr for %s", device);
39         }
40         return ret;
41 }
42
43 int microcom_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
44 int microcom_main(int argc, char **argv)
45 {
46         int sfd;
47         int nfd;
48         struct pollfd pfd[2];
49         struct termios tio0, tiosfd, tio;
50         char *device_lock_file;
51         enum {
52                 OPT_X = 1 << 0, // do not respect Ctrl-X, Ctrl-@
53                 OPT_s = 1 << 1, // baudrate
54                 OPT_d = 1 << 2, // wait for device response, msecs
55                 OPT_t = 1 << 3, // timeout, ms
56         };
57         speed_t speed = 9600;
58         int delay = -1;
59         int timeout = -1;
60
61         // fetch options
62         unsigned opts;
63         opt_complementary = "=1:s+:d+:t+"; // exactly one arg should be there
64         opts = getopt32(argv, "Xs:d:t:", &speed, &delay, &timeout);
65
66 //      argc -= optind;
67         argv += optind;
68
69         // try to create lock file in /var/lock
70         device_lock_file = (char *)bb_basename(argv[0]);
71         device_lock_file = xasprintf("/var/lock/LCK..%s", device_lock_file);
72         sfd = open(device_lock_file, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644);
73         if (sfd < 0) {
74                 if (errno == EEXIST)
75                         bb_perror_msg_and_die("can't create %s", device_lock_file);
76                 if (ENABLE_FEATURE_CLEAN_UP)
77                         free(device_lock_file);
78                 device_lock_file = NULL;
79         }
80         if (sfd > 0) {
81                 // %4d to make mgetty happy. It treats 4-bytes lock files as binary,
82                 // not text, PID. Making 5+ char file. Brrr...
83                 char *s = xasprintf("%4d\n", getpid());
84                 write(sfd, s, strlen(s));
85                 if (ENABLE_FEATURE_CLEAN_UP)
86                         free(s);
87                 close(sfd);
88         }
89
90         // setup signals
91         bb_signals(0
92                 + (1 << SIGHUP)
93                 + (1 << SIGINT)
94                 + (1 << SIGTERM)
95                 + (1 << SIGPIPE)
96                 , signal_handler);
97
98         // error exit code if we fail to open the device
99         signalled = 1;
100
101         // put stdin to "raw mode" (if stdin is a TTY),
102         // handle one character at a time
103         if (isatty(STDIN_FILENO)) {
104                 xget1(STDIN_FILENO, &tio, &tio0);
105                 if (xset1(STDIN_FILENO, &tio, "stdin"))
106                         goto done;
107         }
108
109         // open device
110         sfd = open_or_warn(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK);
111         if (sfd < 0)
112                 goto done;
113         fcntl(sfd, F_SETFL, 0);
114
115         // put device to "raw mode"
116         xget1(sfd, &tio, &tiosfd);
117 //      tio.c_cflag |= (CREAD|HUPCL); // we just bail out on any device error
118         // set device speed
119         cfsetspeed(&tio, tty_value_to_baud(speed));
120         if (xset1(sfd, &tio, argv[0]))
121                 goto restore0_and_done;
122
123         // main loop: check with poll(), then read/write bytes across
124         pfd[0].fd = sfd;
125         pfd[0].events = POLLIN;
126         pfd[1].fd = STDIN_FILENO;
127         pfd[1].events = POLLIN;
128
129         signalled = 0;
130         nfd = 2;
131         while (!signalled && safe_poll(pfd, nfd, timeout) > 0) {
132                 if (nfd > 1 && pfd[1].revents) {
133                         char c;
134                         // read from stdin -> write to device
135                         if (safe_read(STDIN_FILENO, &c, 1) < 1) {
136                                 // don't poll stdin anymore if we got EOF/error
137                                 nfd--;
138                                 goto skip_write;
139                         }
140                         // do we need special processing?
141                         if (!(opts & OPT_X)) {
142                                 // ^@ sends Break
143                                 if (VINTR == c) {
144                                         tcsendbreak(sfd, 0);
145                                         goto skip_write;
146                                 }
147                                 // ^X exits
148                                 if (24 == c)
149                                         break;
150                         }
151                         write(sfd, &c, 1);
152                         if (delay >= 0)
153                                 safe_poll(pfd, 1, delay);
154 skip_write: ;
155                 }
156                 if (pfd[0].revents) {
157 #define iobuf bb_common_bufsiz1
158                         ssize_t len;
159                         // read from device -> write to stdout
160                         len = safe_read(sfd, iobuf, sizeof(iobuf));
161                         if (len > 0)
162                                 full_write(STDOUT_FILENO, iobuf, len);
163                         else {
164                                 // EOF/error -> bail out
165                                 signalled = SIGHUP;
166                                 break;
167                         }
168                 }
169         }
170
171         // restore device mode
172         tcsetattr(sfd, TCSAFLUSH, &tiosfd);
173
174 restore0_and_done:
175         if (isatty(STDIN_FILENO))
176                 tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0);
177
178 done:
179         if (device_lock_file)
180                 unlink(device_lock_file);
181
182         return signalled;
183 }