From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: Sami Kerola <kerolasa@iki.fi>
Subject: [PATCH 08/12] script: move do_io() content to small functions
Date: Sun, 22 Feb 2015 14:42:45 +0000 [thread overview]
Message-ID: <1424616169-693-9-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1424616169-693-1-git-send-email-kerolasa@iki.fi>
The do_io() got to be a bit long with relatively deep indentation.
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
term-utils/script.c | 108 ++++++++++++++++++++++++++++------------------------
1 file changed, 58 insertions(+), 50 deletions(-)
diff --git a/term-utils/script.c b/term-utils/script.c
index a7242e7..08bd922 100644
--- a/term-utils/script.c
+++ b/term-utils/script.c
@@ -223,14 +223,63 @@ static void write_output(struct script_control *ctl, char *obuf,
}
}
-static void do_io(struct script_control *ctl)
+static void handle_io(struct script_control *ctl, int fd, double *oldtime, int i)
{
char buf[BUFSIZ];
+ ssize_t bytes;
+
+ bytes = read(fd, buf, sizeof(buf));
+ if (bytes < 0) {
+ if (errno == EAGAIN)
+ return;
+ fail(ctl);
+ }
+ if (i == 0) {
+ if (write_all(ctl->master, buf, bytes)) {
+ warn(_("write failed"));
+ fail(ctl);
+ }
+ /* without sync write_output() will write both input &
+ * shell output that looks like double echoing */
+ fdatasync(ctl->master);
+ if (!ctl->isterm && !feof(stdin)) {
+ int c = DEF_EOF;
+ write_all(ctl->master, &c, 1);
+ }
+ } else
+ write_output(ctl, buf, bytes, oldtime);
+}
+
+static void handle_signal(struct script_control *ctl, int fd)
+{
+ struct signalfd_siginfo info;
+ ssize_t bytes;
+
+ bytes = read(fd, &info, sizeof(info));
+ assert(bytes == sizeof(info));
+ switch (info.ssi_signo) {
+ case SIGCHLD:
+ finish(ctl, 0);
+ ctl->poll_timeout = 10;
+ return;
+ case SIGWINCH:
+ if (ctl->isterm) {
+ ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&ctl->win);
+ ioctl(ctl->slave, TIOCSWINSZ, (char *)&ctl->win);
+ }
+ break;
+ default:
+ abort();
+ }
+}
+
+static void do_io(struct script_control *ctl)
+{
struct pollfd pfd[POLLFDS];
int ret, i;
- ssize_t bytes;
double oldtime = time(NULL);
time_t tvec = time((time_t *)NULL);
+ char buf[128];
if (ctl->tflg && !ctl->timingfp)
ctl->timingfp = fdopen(STDERR_FILENO, "w");
@@ -260,57 +309,16 @@ static void do_io(struct script_control *ctl)
if (pfd[i].revents == 0)
continue;
if (i < 2) {
- bytes = read(pfd[i].fd, buf, BUFSIZ);
- if (bytes < 0) {
- if (errno == EAGAIN)
- continue;
- fail(ctl);
- }
- if (i == 0) {
- if (write_all(ctl->master, buf, bytes)) {
- warn(_("write failed"));
- fail(ctl);
- } else
-
- /* without sync write_output()
- * will write both input &
- * shell output that looks like
- * double echoing */
- fdatasync(ctl->master);
- if (!ctl->isterm && !feof(stdin)) {
- int c = DEF_EOF;
- write_all(ctl->master, &c, 1);
- }
- } else
- write_output(ctl, buf, bytes, &oldtime);
+ handle_io(ctl, pfd[i].fd, &oldtime, i);
continue;
}
if (i == 2) {
- struct signalfd_siginfo info;
- ssize_t bytes;
-
- bytes = read(pfd[i].fd, &info, sizeof(info));
- assert(bytes == sizeof(info));
- switch (info.ssi_signo) {
- case SIGCHLD:
- finish(ctl, 0);
- ctl->poll_timeout = 10;
- if (!ctl->isterm)
- /* In situation such as 'date' in
- * $ echo date | ./script
- * ignore input when shell has
- * exited. */
- pfd[0].fd = -1;
- break;
- case SIGWINCH:
- if (ctl->isterm) {
- ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&ctl->win);
- ioctl(ctl->slave, TIOCSWINSZ, (char *)&ctl->win);
- }
- break;
- default:
- abort();
- }
+ handle_signal(ctl, pfd[i].fd);
+ if (!ctl->isterm && -1 < ctl->poll_timeout)
+ /* In situation such as 'date' in
+ * $ echo date | ./script
+ * ignore input when shell has exited. */
+ pfd[0].fd = -1;
}
}
}
--
2.3.0
next prev parent reply other threads:[~2015-02-22 14:43 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-02-22 14:42 [PATCH 00/12] pull: script(1) changes Sami Kerola
2015-02-22 14:42 ` [PATCH 01/12] script: remove function prototypes Sami Kerola
2015-02-22 14:42 ` [PATCH 02/12] script: add struct script_control and remove global variables Sami Kerola
2015-03-03 11:56 ` Karel Zak
2015-02-22 14:42 ` [PATCH 03/12] script: use signalfd() to catch signals Sami Kerola
2015-02-22 14:42 ` [PATCH 04/12] script: use poll() rather than select() Sami Kerola
2015-02-22 14:42 ` [PATCH 05/12] script: merge doinput() and output() functions to do_io() Sami Kerola
2015-02-22 14:42 ` [PATCH 06/12] script: remove io vs signal race Sami Kerola
2015-02-22 14:42 ` [PATCH 07/12] script: add 'Script started' line always to capture file Sami Kerola
2015-02-22 14:42 ` Sami Kerola [this message]
2015-02-22 14:42 ` [PATCH 09/12] script: replace strftime() workaround with CFLAGS = -Wno-format-y2k Sami Kerola
2015-02-22 14:42 ` [PATCH 10/12] script: use correct input type, move comment, and so on Sami Kerola
2015-02-22 14:42 ` [PATCH 11/12] script: use gettime_monotonic() to get timing file timestamps Sami Kerola
2015-02-22 14:42 ` [PATCH 12/12] script: add noreturn function attributes Sami Kerola
2015-03-03 12:05 ` [PATCH 00/12] pull: script(1) changes Karel Zak
2015-03-04 15:49 ` Karel Zak
2015-05-21 11:13 ` Karel Zak
2015-05-24 19:07 ` Sami Kerola
2015-06-02 12:05 ` Karel Zak
2015-06-07 20:25 ` Sami Kerola
2015-06-08 9:15 ` Sami Kerola
2015-06-08 21:01 ` Sami Kerola
2015-06-25 10:00 ` Karel Zak
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1424616169-693-9-git-send-email-kerolasa@iki.fi \
--to=kerolasa@iki.fi \
--cc=util-linux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox