From 8f84db8936c1c9da355b79f3a2d1b21d5f0847df Mon Sep 17 00:00:00 2001
From: Andre Noll <maan@systemlinux.org>
Date: Sat, 4 Jan 2014 06:48:36 +0000
Subject: [PATCH] gui: Move static variables of cmd_post_select() into struct
 cmd_task.

For the same reason stated in the commit message of the previous patch,
these variables belong to the context structure of the command task,
so move them there.

The COMMAND_BUF_SIZE define had to be moved above the definition of
struct cmd_task since COMMAND_BUF_SIZE uses it.
---
 gui.c | 39 ++++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 19 deletions(-)

diff --git a/gui.c b/gui.c
index c9ee17f3..c817bf31 100644
--- a/gui.c
+++ b/gui.c
@@ -131,8 +131,13 @@ struct status_task {
 	int fd;
 };
 
+#define COMMAND_BUF_SIZE 32768
+
 struct cmd_task {
 	struct task task;
+	char command_buf[2][COMMAND_BUF_SIZE]; /* stdout/stderr of command */
+	int cbo[2]; /* command buf offsets */
+	unsigned flags[2]; /* passed to for_each_line() */
 };
 
 static int find_cmd_byname(char *name)
@@ -913,8 +918,6 @@ static int signal_post_select(struct sched *s, __a_unused struct task *t)
 	return 1;
 }
 
-#define COMMAND_BUF_SIZE 32768
-
 static enum cmd_status cmd_status(void)
 {
 	if (command_fds[0] >= 0 || command_fds[1] >= 0)
@@ -936,12 +939,10 @@ static void command_pre_select(struct sched *s, __a_unused struct task *t)
 		para_fd_set(command_fds[1], &s->rfds, &s->max_fileno);
 }
 
-static int command_post_select(struct sched *s, __a_unused struct task *t)
+static int command_post_select(struct sched *s, struct task *t)
 {
+	struct cmd_task *ct = container_of(t, struct cmd_task, task);
 	int i, ret;
-	static char command_buf[2][COMMAND_BUF_SIZE];
-	static int cbo[2]; /* command buf offsets */
-	static unsigned flags[2]; /* for for_each_line() */
 	enum cmd_status cmds = cmd_status();
 
 	if (cmds != CMDS_DCMD)
@@ -951,15 +952,15 @@ static int command_post_select(struct sched *s, __a_unused struct task *t)
 		if (command_fds[i] < 0)
 			continue;
 		ret = read_nonblock(command_fds[i],
-			command_buf[i] + cbo[i],
-			COMMAND_BUF_SIZE - 1 - cbo[i], &s->rfds, &sz);
-		cbo[i] += sz;
-		sz = cbo[i];
-		cbo[i] = for_each_line(flags[i], command_buf[i], cbo[i],
-			add_output_line, &i);
-		if (sz != cbo[i]) { /* at least one line found */
+			ct->command_buf[i] + ct->cbo[i],
+			COMMAND_BUF_SIZE - 1 - ct->cbo[i], &s->rfds, &sz);
+		ct->cbo[i] += sz;
+		sz = ct->cbo[i];
+		ct->cbo[i] = for_each_line(ct->flags[i], ct->command_buf[i],
+			ct->cbo[i], add_output_line, &i);
+		if (sz != ct->cbo[i]) { /* at least one line found */
 			wrefresh(bot.win);
-			flags[i] = 0;
+			ct->flags[i] = 0;
 		}
 		if (ret < 0 || cmd_pid == 0) {
 			if (ret < 0)
@@ -967,15 +968,15 @@ static int command_post_select(struct sched *s, __a_unused struct task *t)
 					i, para_strerror(-ret));
 			close(command_fds[i]);
 			command_fds[i] = -1;
-			flags[i] = 0;
-			cbo[i] = 0;
+			ct->flags[i] = 0;
+			ct->cbo[i] = 0;
 			if (command_fds[!i] < 0) /* both fds closed */
 				return 1;
 		}
-		if (cbo[i] == COMMAND_BUF_SIZE - 1) {
+		if (ct->cbo[i] == COMMAND_BUF_SIZE - 1) {
 			PARA_NOTICE_LOG("discarding overlong line");
-			cbo[i] = 0;
-			flags[i] = FELF_DISCARD_FIRST;
+			ct->cbo[i] = 0;
+			ct->flags[i] = FELF_DISCARD_FIRST;
 		}
 	}
 	return 0;
-- 
2.39.5