ESH

Executive Shell
git clone http://git.omkov.net/ESH
Log | Tree | Refs | README | Download

AuthorJamozed <[email protected]>
Date2021-11-26 13:01:05
Commit56462a08763239e31826972127049709e7549618
Parent557f683de8b02a5c484e4c981c10efb377a6a546

Fix redirects and pipes not working together

Diffstat

M src/esh.h | 2 +-
M src/eval.c | 2 +-
M src/exec.c | 48 +++++++++++++++++++++++++-----------------------

3 files changed, 27 insertions, 25 deletions

diff --git a/src/esh.h b/src/esh.h
index c6d9db9..34d2578 100644
--- a/src/esh.h
+++ b/src/esh.h
@@ -39,7 +39,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
 extern bool Lflag, Pflag;
 extern int _loop, _ret;
 
-extern int execute(ast a);
+extern int execute(ast a, int fdi, int fd[2]);
 
 extern void eval(char *src, UINT len);
 extern void eval_file(const char *file);
diff --git a/src/eval.c b/src/eval.c
index 98778d9..605cce0 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -50,7 +50,7 @@ extern void eval(char *src, UINT len) {
 		a = parse(&l); if (!a) { return; }
 		if (Pflag) { ast_debug(a); continue; }
 
-		_ret = execute(a);
+		_ret = execute(a, STDIN_FILENO, NULL);
 	}
 }
 
diff --git a/src/exec.c b/src/exec.c
index daa35b6..f211b7f 100644
--- a/src/exec.c
+++ b/src/exec.c
@@ -46,20 +46,21 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
 #include <string.h>
 
 static int execute_comm(ast a, int fdi, int fd[2]);
-static int execute_pipe(ast a, int fdi);
+static int execute_pipe(ast a, int fdi, int fd[2]);
 
 /* Execute a program. */
-int execute(ast a) {
+int execute(ast a, int fdi, int fd[2]) {
 	switch (a->k) {
-	case AK_COMM: {} return execute_comm(a, STDIN_FILENO, NULL);
-	case AK_PIPE: {} return execute_pipe(a, STDIN_FILENO);
+	case AK_COMM: {} return execute_comm(a, fdi, fd);
+	case AK_PIPE: {} return execute_pipe(a, fdi, fd);
 	default: { warn("%s: Unimplemented AST", ast_ks[a->k]); } return -1;
 	}
 }
 
-#define CLOSEIF(fd) if (fd >= 0) { close(fd); }
+#define CLOSE(fd) if (fd >= 0) { close(fd); }
 
 /* Execute a command statement. */
+/* TODO improve this code, its ugly and weird */
 static int execute_comm(ast a, int fdi, int fd[2]) {
 	/* If the command is null, then do nothing */
 	if (!a->s) { return _ret; }
@@ -70,30 +71,27 @@ static int execute_comm(ast a, int fdi, int fd[2]) {
 		}
 	}
 
-	pid_t pid; int status, ri = -1, ro = -1, re = -1; char **av = a->c.a;
+	pid_t pid; int status, ri = -1, ro = -1, re = -1;
 
-	/* Attempt to open redirect files */
+	/* Open redirect files. */
 	if (a->ri.s) {
-		ri = open(a->ri.s, O_RDONLY);
-		if (ri == -1) {
-			warn("%s: %s", a->ri.s, serr()); errno = 0; return 1;
+		if ((ri = open(a->ri.s, O_RDONLY)) == -1) {
+			ALERT_RE(1, "%s: %s", a->ri.s, SERR);
 		}
 	}
 	if (a->ro.s) {
-		ro = open(a->ro.s, O_WRONLY | O_CREAT | O_TRUNC, 0666);
-		if (ro == -1) {
-			warn("%s: %s", a->ri.s, serr()); errno = 0;
-			CLOSEIF(ri); return 1;
+		if ((ro = open(a->ro.s, O_WRONLY | O_CREAT | O_TRUNC, 0666)) == -1) {
+			CLOSE(ri); ALERT_RE(1, "%s: %s", a->ro.s, SERR);
 		}
 	}
 	if (a->re.s) {
-		re = open(a->re.s, O_WRONLY | O_CREAT | O_TRUNC, 0666);
-		if (re == -1) {
-			warn("%s: %s", a->re.s, serr()); errno = 0;
-			CLOSEIF(ri); CLOSEIF(ro); return 1;
+		if ((re = open(a->ro.s, O_WRONLY | O_CREAT | O_TRUNC, 0666)) == -1) {
+			CLOSE(ri); CLOSE(ro); ALERT_RE(1, "%s: %s", a->re.s, SERR);
 		}
 	}
 
+	char **av = a->c.a;
+	
 	/* Fork and exec the child process */
 	if ((pid = fork()) == 0) {
 		signal(SIGINT, SIG_DFL);
@@ -102,7 +100,8 @@ static int execute_comm(ast a, int fdi, int fd[2]) {
 		else { dup2(fdi, STDIN_FILENO); }
 
 		if (ro != -1) { dup2(ro, STDOUT_FILENO); }
-		else if (fd) { if (fd[0]) { close(fd[0]); } dup2(fd[1], fileno(stdout)); }
+		else if (fd) { dup2(fd[1], fileno(stdout)); }
+		if (fd && fd[0]) { close(fd[0]); }
 
 		if (re != -1) { dup2(re, STDERR_FILENO); }
 
@@ -118,16 +117,19 @@ static int execute_comm(ast a, int fdi, int fd[2]) {
 			if (fdi != STDIN_FILENO) { close(fdi); } close(fd[1]);
 		}
 
-		else { waitpid(pid, &status, 0); }
+		else {
+			if (fd) { close(fd[1]); }
+			waitpid(pid, &status, 0);
+		}
 	}
 
-	CLOSEIF(ri); CLOSEIF(ro); CLOSEIF(re);
+	CLOSE(ri); CLOSE(ro); CLOSE(re);
 
 	return WEXITSTATUS(status);
 }
 
 /* Execute a pipe statement. */
-static int execute_pipe(ast a, int fdi) {
+static int execute_pipe(ast a, int fdi, int fd[2]) {
 	int pd[2]; if (pipe(pd) == -1) { warn("PIPE: %s", serr()); return -1; }
-	execute_comm(a->lc, fdi, pd); return execute_comm(a->rc, pd[0], NULL);
+	execute(a->lc, fdi, pd); return execute(a->rc, pd[0], fd);
 }