microcom: split -d (delay) option away from -t

This commit is contained in:
Denis Vlasenko 2008-02-09 11:37:21 +00:00
parent 937b10f35d
commit d56e3ccf96
2 changed files with 28 additions and 20 deletions

View File

@ -2235,9 +2235,15 @@ USE_FEATURE_BRCTL_FANCY("\n" \
" n Disallow write access to your terminal" " n Disallow write access to your terminal"
#define microcom_trivial_usage \ #define microcom_trivial_usage \
"[-s speed] tty-name" "[-d DELAY] [-t TIMEOUT] [-s SPEED] [-X] TTY"
#define microcom_full_usage \ #define microcom_full_usage \
"" "Copy bytes for stdin to TTY and from TTY to stdout" \
"\n\nOptions:" \
"\n -d Wait up to DELAY ms for TTY output before sending every" \
"\n next byte to it" \
"\n -t Exit if both stdin and TTY are silent for TIMEOUT ms" \
"\n -s Set serial line to SPEED"
"\n -X Disable special meaning of NUL and Ctrl-X from stdin"
#define mkdir_trivial_usage \ #define mkdir_trivial_usage \
"[OPTION] DIRECTORY..." "[OPTION] DIRECTORY..."

View File

@ -51,22 +51,28 @@ int microcom_main(int argc, char **argv)
enum { enum {
OPT_X = 1 << 0, // do not respect Ctrl-X, Ctrl-@ OPT_X = 1 << 0, // do not respect Ctrl-X, Ctrl-@
OPT_s = 1 << 1, // baudrate OPT_s = 1 << 1, // baudrate
OPT_t = 1 << 2 // wait for device response, msecs OPT_d = 1 << 2 // wait for device response, msecs
OPT_t = 1 << 3 // timeout, ms
}; };
speed_t speed = 9600; speed_t speed = 9600;
int timeout = 100; // 0.1 sec timeout int delay = -1;
int timeout = -1;
// fetch options // fetch options
char *opt_s; char *opt_s;
char *opt_t; char *opt_t;
unsigned opts; unsigned opts;
opt_complementary = "=1"; /* exactly one arg should be there */ opt_complementary = "=1"; /* exactly one arg should be there */
opts = getopt32(argv, "Xs:t:", &opt_s, &opt_t); opts = getopt32(argv, "Xs:d:t:", &opt_s, &opt_d, &opt_t);
// apply options // apply options
if (opts & OPT_s) if (opts & OPT_s)
speed = xatoi_u(opt_s); speed = xatoi_u(opt_s);
if (opts & OPT_d)
delay = xatoi_u(opt_d);
if (opts & OPT_t) if (opts & OPT_t)
timeout = xatoi_u(opt_t); timeout = xatoi_u(opt_t);
// argc -= optind; // argc -= optind;
argv += optind; argv += optind;
@ -106,13 +112,12 @@ int microcom_main(int argc, char **argv)
goto done; goto done;
fcntl(sfd, F_SETFL, O_RDWR | O_NOCTTY); fcntl(sfd, F_SETFL, O_RDWR | O_NOCTTY);
/* put stdin to "raw mode" (if stdin is a TTY), // put stdin to "raw mode" (if stdin is a TTY),
handle one character at a time */ // handle one character at a time
if (isatty(STDIN_FILENO)) { if (isatty(STDIN_FILENO)) {
xget1(STDIN_FILENO, &tio, &tio0); xget1(STDIN_FILENO, &tio, &tio0);
if (xset1(STDIN_FILENO, &tio, "stdin")) if (xset1(STDIN_FILENO, &tio, "stdin"))
goto done; goto done;
timeout = -1; // tty input? -> set infinite timeout for poll()
} }
// same thing for modem // same thing for modem
@ -136,16 +141,18 @@ int microcom_main(int argc, char **argv)
nfd = 2; nfd = 2;
while (!signalled && safe_poll(pfd, nfd, timeout) > 0) { while (!signalled && safe_poll(pfd, nfd, timeout) > 0) {
char c; char c;
if (pfd[0].revents & POLLIN) { if (pfd[0].revents) {
serial_ready:
// read from device -> write to stdout // read from device -> write to stdout
if (safe_read(sfd, &c, 1) > 0) if (safe_read(sfd, &c, 1) > 0)
write(STDOUT_FILENO, &c, 1); write(STDOUT_FILENO, &c, 1);
// else { EOF/error - what to do? }
} }
if (pfd[1].revents & POLLIN) { if (pfd[1].revents) {
pfd[1].revents = 0;
// read from stdin -> write to device // read from stdin -> write to device
if (safe_read(STDIN_FILENO, &c, 1) < 1) { if (safe_read(STDIN_FILENO, &c, 1) < 1) {
// skip polling stdin if we got EOF/error // don't poll stdin anymore if we got EOF/error
pfd[1].revents = 0;
nfd--; nfd--;
continue; continue;
} }
@ -161,20 +168,15 @@ int microcom_main(int argc, char **argv)
break; break;
} }
write(sfd, &c, 1); write(sfd, &c, 1);
//// vda: this is suspicious! if (delay >= 0 && safe_poll(pfd, 1, delay) > 0)
// without this we never get POLLIN on sfd goto serial_ready;
// until piped stdin is drained
if (-1 != timeout)
safe_poll(pfd, 1, 1 /* 1 ms */);
} }
} }
/* usleep(10000); - let last chars leave serial line */
tcsetattr(sfd, TCSAFLUSH, &tiosfd); tcsetattr(sfd, TCSAFLUSH, &tiosfd);
restore0_and_done: restore0_and_done:
// timeout == -1 -- stdin is a tty if (isatty(STDIN_FILENO))
if (-1 == timeout)
tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0); tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0);
done: done: