diff options
| author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-02-09 11:37:21 +0000 |
|---|---|---|
| committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-02-09 11:37:21 +0000 |
| commit | d56e3ccf960a6773ea7939811728f1942cc493ac (patch) | |
| tree | cca31e184ef73cada7b1786c5b47f8aacf6b672c /miscutils | |
| parent | 937b10f35dc9fb74220ffa8d3f8e98f07f732f79 (diff) | |
| download | busybox-w32-d56e3ccf960a6773ea7939811728f1942cc493ac.tar.gz busybox-w32-d56e3ccf960a6773ea7939811728f1942cc493ac.tar.bz2 busybox-w32-d56e3ccf960a6773ea7939811728f1942cc493ac.zip | |
microcom: split -d (delay) option away from -t
Diffstat (limited to 'miscutils')
| -rw-r--r-- | miscutils/microcom.c | 38 |
1 files changed, 20 insertions, 18 deletions
diff --git a/miscutils/microcom.c b/miscutils/microcom.c index fb5e9c868..52baaca51 100644 --- a/miscutils/microcom.c +++ b/miscutils/microcom.c | |||
| @@ -51,22 +51,28 @@ int microcom_main(int argc, char **argv) | |||
| 51 | enum { | 51 | enum { |
| 52 | OPT_X = 1 << 0, // do not respect Ctrl-X, Ctrl-@ | 52 | OPT_X = 1 << 0, // do not respect Ctrl-X, Ctrl-@ |
| 53 | OPT_s = 1 << 1, // baudrate | 53 | OPT_s = 1 << 1, // baudrate |
| 54 | OPT_t = 1 << 2 // wait for device response, msecs | 54 | OPT_d = 1 << 2 // wait for device response, msecs |
| 55 | OPT_t = 1 << 3 // timeout, ms | ||
| 55 | }; | 56 | }; |
| 56 | speed_t speed = 9600; | 57 | speed_t speed = 9600; |
| 57 | int timeout = 100; // 0.1 sec timeout | 58 | int delay = -1; |
| 59 | int timeout = -1; | ||
| 58 | 60 | ||
| 59 | // fetch options | 61 | // fetch options |
| 60 | char *opt_s; | 62 | char *opt_s; |
| 61 | char *opt_t; | 63 | char *opt_t; |
| 62 | unsigned opts; | 64 | unsigned opts; |
| 63 | opt_complementary = "=1"; /* exactly one arg should be there */ | 65 | opt_complementary = "=1"; /* exactly one arg should be there */ |
| 64 | opts = getopt32(argv, "Xs:t:", &opt_s, &opt_t); | 66 | opts = getopt32(argv, "Xs:d:t:", &opt_s, &opt_d, &opt_t); |
| 67 | |||
| 65 | // apply options | 68 | // apply options |
| 66 | if (opts & OPT_s) | 69 | if (opts & OPT_s) |
| 67 | speed = xatoi_u(opt_s); | 70 | speed = xatoi_u(opt_s); |
| 71 | if (opts & OPT_d) | ||
| 72 | delay = xatoi_u(opt_d); | ||
| 68 | if (opts & OPT_t) | 73 | if (opts & OPT_t) |
| 69 | timeout = xatoi_u(opt_t); | 74 | timeout = xatoi_u(opt_t); |
| 75 | |||
| 70 | // argc -= optind; | 76 | // argc -= optind; |
| 71 | argv += optind; | 77 | argv += optind; |
| 72 | 78 | ||
| @@ -106,13 +112,12 @@ int microcom_main(int argc, char **argv) | |||
| 106 | goto done; | 112 | goto done; |
| 107 | fcntl(sfd, F_SETFL, O_RDWR | O_NOCTTY); | 113 | fcntl(sfd, F_SETFL, O_RDWR | O_NOCTTY); |
| 108 | 114 | ||
| 109 | /* put stdin to "raw mode" (if stdin is a TTY), | 115 | // put stdin to "raw mode" (if stdin is a TTY), |
| 110 | handle one character at a time */ | 116 | // handle one character at a time |
| 111 | if (isatty(STDIN_FILENO)) { | 117 | if (isatty(STDIN_FILENO)) { |
| 112 | xget1(STDIN_FILENO, &tio, &tio0); | 118 | xget1(STDIN_FILENO, &tio, &tio0); |
| 113 | if (xset1(STDIN_FILENO, &tio, "stdin")) | 119 | if (xset1(STDIN_FILENO, &tio, "stdin")) |
| 114 | goto done; | 120 | goto done; |
| 115 | timeout = -1; // tty input? -> set infinite timeout for poll() | ||
| 116 | } | 121 | } |
| 117 | 122 | ||
| 118 | // same thing for modem | 123 | // same thing for modem |
| @@ -136,16 +141,18 @@ int microcom_main(int argc, char **argv) | |||
| 136 | nfd = 2; | 141 | nfd = 2; |
| 137 | while (!signalled && safe_poll(pfd, nfd, timeout) > 0) { | 142 | while (!signalled && safe_poll(pfd, nfd, timeout) > 0) { |
| 138 | char c; | 143 | char c; |
| 139 | if (pfd[0].revents & POLLIN) { | 144 | if (pfd[0].revents) { |
| 145 | serial_ready: | ||
| 140 | // read from device -> write to stdout | 146 | // read from device -> write to stdout |
| 141 | if (safe_read(sfd, &c, 1) > 0) | 147 | if (safe_read(sfd, &c, 1) > 0) |
| 142 | write(STDOUT_FILENO, &c, 1); | 148 | write(STDOUT_FILENO, &c, 1); |
| 149 | // else { EOF/error - what to do? } | ||
| 143 | } | 150 | } |
| 144 | if (pfd[1].revents & POLLIN) { | 151 | if (pfd[1].revents) { |
| 152 | pfd[1].revents = 0; | ||
| 145 | // read from stdin -> write to device | 153 | // read from stdin -> write to device |
| 146 | if (safe_read(STDIN_FILENO, &c, 1) < 1) { | 154 | if (safe_read(STDIN_FILENO, &c, 1) < 1) { |
| 147 | // skip polling stdin if we got EOF/error | 155 | // don't poll stdin anymore if we got EOF/error |
| 148 | pfd[1].revents = 0; | ||
| 149 | nfd--; | 156 | nfd--; |
| 150 | continue; | 157 | continue; |
| 151 | } | 158 | } |
| @@ -161,20 +168,15 @@ int microcom_main(int argc, char **argv) | |||
| 161 | break; | 168 | break; |
| 162 | } | 169 | } |
| 163 | write(sfd, &c, 1); | 170 | write(sfd, &c, 1); |
| 164 | //// vda: this is suspicious! | 171 | if (delay >= 0 && safe_poll(pfd, 1, delay) > 0) |
| 165 | // without this we never get POLLIN on sfd | 172 | goto serial_ready; |
| 166 | // until piped stdin is drained | ||
| 167 | if (-1 != timeout) | ||
| 168 | safe_poll(pfd, 1, 1 /* 1 ms */); | ||
| 169 | } | 173 | } |
| 170 | } | 174 | } |
| 171 | /* usleep(10000); - let last chars leave serial line */ | ||
| 172 | 175 | ||
| 173 | tcsetattr(sfd, TCSAFLUSH, &tiosfd); | 176 | tcsetattr(sfd, TCSAFLUSH, &tiosfd); |
| 174 | 177 | ||
| 175 | restore0_and_done: | 178 | restore0_and_done: |
| 176 | // timeout == -1 -- stdin is a tty | 179 | if (isatty(STDIN_FILENO)) |
| 177 | if (-1 == timeout) | ||
| 178 | tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0); | 180 | tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0); |
| 179 | 181 | ||
| 180 | done: | 182 | done: |
