diff options
| author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-02-16 23:40:47 +0000 |
|---|---|---|
| committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-02-16 23:40:47 +0000 |
| commit | ffae845cfd0a0b9872827d806984841d4cfee104 (patch) | |
| tree | 214784b0d10f50a9fa63a12993a1678dbcc88874 | |
| parent | b1d8e7db0fced98ba2cd049019f8820fd91a0482 (diff) | |
| download | busybox-w32-ffae845cfd0a0b9872827d806984841d4cfee104.tar.gz busybox-w32-ffae845cfd0a0b9872827d806984841d4cfee104.tar.bz2 busybox-w32-ffae845cfd0a0b9872827d806984841d4cfee104.zip | |
microcom: maintainer update
| -rw-r--r-- | miscutils/microcom.c | 69 |
1 files changed, 30 insertions, 39 deletions
diff --git a/miscutils/microcom.c b/miscutils/microcom.c index b9ed9e401..2857a1b1b 100644 --- a/miscutils/microcom.c +++ b/miscutils/microcom.c | |||
| @@ -3,7 +3,7 @@ | |||
| 3 | * bare bones 'talk to modem' program - similar to 'cu -l $device' | 3 | * bare bones 'talk to modem' program - similar to 'cu -l $device' |
| 4 | * inspired by mgetty's microcom | 4 | * inspired by mgetty's microcom |
| 5 | * | 5 | * |
| 6 | * Copyright (C) 2007 by Vladimir Dronnikov <dronnikov@gmail.ru> | 6 | * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com> |
| 7 | * | 7 | * |
| 8 | * Licensed under GPLv2, see file LICENSE in this tarball for details. | 8 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
| 9 | */ | 9 | */ |
| @@ -59,20 +59,9 @@ int microcom_main(int argc, char **argv) | |||
| 59 | int timeout = -1; | 59 | int timeout = -1; |
| 60 | 60 | ||
| 61 | // fetch options | 61 | // fetch options |
| 62 | char *opt_s; | ||
| 63 | char *opt_d; | ||
| 64 | char *opt_t; | ||
| 65 | unsigned opts; | 62 | unsigned opts; |
| 66 | opt_complementary = "=1"; // exactly one arg should be there | 63 | opt_complementary = "=1:s+:d+:t+"; // exactly one arg should be there |
| 67 | opts = getopt32(argv, "Xs:d:t:", &opt_s, &opt_d, &opt_t); | 64 | opts = getopt32(argv, "Xs:d:t:", &speed, &delay, &timeout); |
| 68 | |||
| 69 | // apply options | ||
| 70 | if (opts & OPT_s) | ||
| 71 | speed = xatoi_u(opt_s); | ||
| 72 | if (opts & OPT_d) | ||
| 73 | delay = xatoi_u(opt_d); | ||
| 74 | if (opts & OPT_t) | ||
| 75 | timeout = xatoi_u(opt_t); | ||
| 76 | 65 | ||
| 77 | // argc -= optind; | 66 | // argc -= optind; |
| 78 | argv += optind; | 67 | argv += optind; |
| @@ -99,22 +88,16 @@ int microcom_main(int argc, char **argv) | |||
| 99 | } | 88 | } |
| 100 | 89 | ||
| 101 | // setup signals | 90 | // setup signals |
| 102 | bb_signals_recursive(0 | 91 | bb_signals(0 |
| 103 | + (1 << SIGHUP) | 92 | + (1 << SIGHUP) |
| 104 | + (1 << SIGINT) | 93 | + (1 << SIGINT) |
| 105 | + (1 << SIGTERM) | 94 | + (1 << SIGTERM) |
| 106 | + (1 << SIGPIPE) | 95 | + (1 << SIGPIPE) |
| 107 | , signal_handler); | 96 | , signal_handler); |
| 108 | 97 | ||
| 109 | // error exit code if we fail to open the device | 98 | // error exit code if we fail to open the device |
| 110 | signalled = 1; | 99 | signalled = 1; |
| 111 | 100 | ||
| 112 | // open device | ||
| 113 | sfd = open_or_warn(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK); | ||
| 114 | if (sfd < 0) | ||
| 115 | goto done; | ||
| 116 | fcntl(sfd, F_SETFL, O_RDWR | O_NOCTTY); | ||
| 117 | |||
| 118 | // put stdin to "raw mode" (if stdin is a TTY), | 101 | // put stdin to "raw mode" (if stdin is a TTY), |
| 119 | // handle one character at a time | 102 | // handle one character at a time |
| 120 | if (isatty(STDIN_FILENO)) { | 103 | if (isatty(STDIN_FILENO)) { |
| @@ -123,12 +106,15 @@ int microcom_main(int argc, char **argv) | |||
| 123 | goto done; | 106 | goto done; |
| 124 | } | 107 | } |
| 125 | 108 | ||
| 126 | // same thing for modem | 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" | ||
| 127 | xget1(sfd, &tio, &tiosfd); | 116 | xget1(sfd, &tio, &tiosfd); |
| 128 | // order device to hang up at exit | 117 | // tio.c_cflag |= (CREAD|HUPCL); // we just bail out on any device error |
| 129 | tio.c_cflag |= (CREAD|HUPCL); | ||
| 130 | // if (!istty) | ||
| 131 | // tio.c_iflag |= (IGNCR); | ||
| 132 | // set device speed | 118 | // set device speed |
| 133 | cfsetspeed(&tio, tty_value_to_baud(speed)); | 119 | cfsetspeed(&tio, tty_value_to_baud(speed)); |
| 134 | if (xset1(sfd, &tio, argv[0])) | 120 | if (xset1(sfd, &tio, argv[0])) |
| @@ -143,21 +129,20 @@ int microcom_main(int argc, char **argv) | |||
| 143 | signalled = 0; | 129 | signalled = 0; |
| 144 | nfd = 2; | 130 | nfd = 2; |
| 145 | while (!signalled && safe_poll(pfd, nfd, timeout) > 0) { | 131 | while (!signalled && safe_poll(pfd, nfd, timeout) > 0) { |
| 146 | if (pfd[1].revents) { | 132 | if (nfd > 1 && pfd[1].revents) { |
| 147 | char c; | 133 | char c; |
| 148 | // read from stdin -> write to device | 134 | // read from stdin -> write to device |
| 149 | if (safe_read(STDIN_FILENO, &c, 1) < 1) { | 135 | if (safe_read(STDIN_FILENO, &c, 1) < 1) { |
| 150 | // don't poll stdin anymore if we got EOF/error | 136 | // don't poll stdin anymore if we got EOF/error |
| 151 | pfd[1].revents = 0; | ||
| 152 | nfd--; | 137 | nfd--; |
| 153 | goto check_stdin; | 138 | goto skip_write; |
| 154 | } | 139 | } |
| 155 | // do we need special processing? | 140 | // do we need special processing? |
| 156 | if (!(opts & OPT_X)) { | 141 | if (!(opts & OPT_X)) { |
| 157 | // ^@ sends Break | 142 | // ^@ sends Break |
| 158 | if (VINTR == c) { | 143 | if (VINTR == c) { |
| 159 | tcsendbreak(sfd, 0); | 144 | tcsendbreak(sfd, 0); |
| 160 | goto check_stdin; | 145 | goto skip_write; |
| 161 | } | 146 | } |
| 162 | // ^X exits | 147 | // ^X exits |
| 163 | if (24 == c) | 148 | if (24 == c) |
| @@ -166,18 +151,24 @@ int microcom_main(int argc, char **argv) | |||
| 166 | write(sfd, &c, 1); | 151 | write(sfd, &c, 1); |
| 167 | if (delay >= 0) | 152 | if (delay >= 0) |
| 168 | safe_poll(pfd, 1, delay); | 153 | safe_poll(pfd, 1, delay); |
| 154 | skip_write: ; | ||
| 169 | } | 155 | } |
| 170 | check_stdin: | ||
| 171 | if (pfd[0].revents) { | 156 | if (pfd[0].revents) { |
| 157 | #define iobuf bb_common_bufsiz1 | ||
| 172 | ssize_t len; | 158 | ssize_t len; |
| 173 | // read from device -> write to stdout | 159 | // read from device -> write to stdout |
| 174 | len = safe_read(sfd, bb_common_bufsiz1, sizeof(bb_common_bufsiz1)); | 160 | len = safe_read(sfd, iobuf, sizeof(iobuf)); |
| 175 | if (len > 0) | 161 | if (len > 0) |
| 176 | full_write(STDOUT_FILENO, bb_common_bufsiz1, len); | 162 | full_write(STDOUT_FILENO, iobuf, len); |
| 177 | // else { EOF/error - what to do? } | 163 | else { |
| 164 | // EOF/error -> bail out | ||
| 165 | signalled = SIGHUP; | ||
| 166 | break; | ||
| 167 | } | ||
| 178 | } | 168 | } |
| 179 | } | 169 | } |
| 180 | 170 | ||
| 171 | // restore device mode | ||
| 181 | tcsetattr(sfd, TCSAFLUSH, &tiosfd); | 172 | tcsetattr(sfd, TCSAFLUSH, &tiosfd); |
| 182 | 173 | ||
| 183 | restore0_and_done: | 174 | restore0_and_done: |
