microcom: add copyright/license info
[oweals/busybox.git] / util-linux / microcom.c
1 /* 
2  * bare bones 'talk to modem' program - similar to 'cu -l $device'
3  * inspired by mgetty's microcom
4  *
5  * Copyright (C) 2007 by Vladimir Dronnikov <dronnikov@gmail.ru>
6  *
7  * Licensed under GPLv2, see file LICENSE in this tarball for details.
8  */
9 #include "busybox.h"
10
11 int microcom_main(int argc, char **argv);
12 int microcom_main(int argc, char **argv)
13 {
14         struct pollfd pfd[2];
15 #define sfd (pfd[1].fd)
16         char *device_lock_file = NULL;
17         const char *s;
18         const char *opt_s = "9600";
19         unsigned speed;
20         int len;
21         int exitcode = 1;
22         struct termios tio0, tiosfd, tio;
23
24         getopt32(argv, "s:", &opt_s);
25         argc -= optind;
26         argv += optind;
27         if (!argv[0])
28                 bb_show_usage();
29         speed = xatou(opt_s);
30
31         // try to create lock file in /var/lock
32         s = bb_basename(argv[0]);
33         if (!s[0]) {
34                 errno = ENODEV;
35                 bb_perror_msg_and_die("can't lock device");
36         }
37         device_lock_file = xasprintf("/var/lock/LCK..%s", s);
38         sfd = open(device_lock_file, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644);
39         if (sfd < 0) {
40                 if (ENABLE_FEATURE_CLEAN_UP)
41                         free(device_lock_file);
42                 device_lock_file = NULL;
43                 if (errno == EEXIST)
44                         bb_perror_msg_and_die("can't lock device");
45                 // We don't abort on other errors: /var/lock can be
46                 // non-writable or non-existent
47         } else {
48                 // %4d to make mgetty happy. It treats 4-bytes lock files as binary,
49                 // not text, PID. Making 5+ char file. Brrr...
50                 s = xasprintf("%4d\n", getpid());
51                 write(sfd, s, strlen(s));
52                 if (ENABLE_FEATURE_CLEAN_UP)
53                         free((char*)s);
54                 close(sfd);
55         }
56
57         // open device
58         sfd = open(argv[0], O_RDWR | O_NDELAY);
59         if (sfd < 0) {
60                 bb_perror_msg("can't open device");
61                 goto unlock_and_exit;
62         }
63         fcntl(sfd, F_SETFL, O_RDWR); // why?
64
65         // put stdin to "raw mode", handle one character at a time
66         tcgetattr(STDIN_FILENO, &tio0);
67         tio = tio0;
68         tio.c_lflag &= ~(ICANON|ECHO);
69         tio.c_iflag &= ~(IXON|ICRNL);
70         tio.c_oflag &= ~(ONLCR);
71         tio.c_cc[VMIN] = 1;
72         tio.c_cc[VTIME] = 0;
73         if (tcsetattr(STDIN_FILENO, TCSANOW, &tio)) {
74                 bb_perror_msg("can't tcsetattr for %s", "stdin");
75                 goto unlock_and_exit;
76         }
77
78         /* same thing for modem (plus: set baud rate) - TODO: make CLI option */
79         tcgetattr(sfd, &tiosfd);
80         tio = tiosfd;
81         tio.c_lflag &= ~(ICANON|ECHO);
82         tio.c_iflag &= ~(IXON|ICRNL);
83         tio.c_oflag &= ~(ONLCR);
84         tio.c_cc[VMIN] = 1;
85         tio.c_cc[VTIME] = 0;
86         cfsetispeed(&tio, tty_value_to_baud(speed));
87         cfsetospeed(&tio, tty_value_to_baud(speed));
88         if (tcsetattr(sfd, TCSANOW, &tio)) {
89                 bb_perror_msg("can't tcsetattr for %s", "device");
90                 goto unlock_and_exit;
91         }
92
93         // disable SIGINT
94         signal(SIGINT, SIG_IGN);
95
96         // drain stdin
97         tcflush(STDIN_FILENO, TCIFLUSH);
98         printf("connected to '%s' (%d bps), exit with ctrl-X...\r\n", argv[0], speed);
99
100         // main loop: check with poll(), then read/write bytes across
101         pfd[0].fd = STDIN_FILENO;
102         pfd[0].events = POLLIN;
103         /*pfd[1].fd = sfd;*/
104         pfd[1].events = POLLIN;
105         while (1) {
106                 int i;
107                 while (-1 == poll(pfd, 2, -1) && EINTR == errno)
108                         continue;
109                 for (i = 0; i < 2; ++i) {
110                         if (pfd[i].revents & POLLIN) { 
111                                 len = read(pfd[i].fd, bb_common_bufsiz1, COMMON_BUFSIZE);
112                                 if (len > 0) {
113                                         if (!i && 24 == bb_common_bufsiz1[0])
114                                                 goto done; // ^X exits
115                                         write(pfd[1-i].fd, bb_common_bufsiz1, len);
116                                 }
117                         }
118                 }
119         }
120  done:
121         tcsetattr(sfd, TCSANOW, &tiosfd);
122         tcsetattr(STDIN_FILENO, TCSANOW, &tio0);
123         tcflush(STDIN_FILENO, TCIFLUSH);
124
125         if (ENABLE_FEATURE_CLEAN_UP)
126                 close(sfd);
127         exitcode = 0;
128
129  unlock_and_exit:
130         // delete lock file
131         if (device_lock_file) {
132                 unlink(device_lock_file);
133                 if (ENABLE_FEATURE_CLEAN_UP)
134                         free(device_lock_file);
135         }
136         return exitcode;
137 }