summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/exec.c50
-rw-r--r--src/preproc.c14
-rw-r--r--src/syntax.h7
3 files changed, 48 insertions, 23 deletions
diff --git a/src/exec.c b/src/exec.c
index afdf140..1da1f25 100644
--- a/src/exec.c
+++ b/src/exec.c
@@ -6,6 +6,8 @@
#include "syntax.h"
+void execute_command_pipeline(Pipeline pipeline, int stdin_fd, int stdout_fd);
+
void execute_cmd(ExecutableCommand cmd, int* pgid) {
@@ -27,8 +29,8 @@ void execute_cmd(ExecutableCommand cmd, int* pgid) {
dup2(fd_rd.to, fd_rd.redirected);
}
- for (int j = 0; j < cmd.open_fds.count; j++) {
- int fd = *get_from_IntVec(&cmd.open_fds, j);
+ for (int j = 0; j < cmd.child_open_fds.count; j++) {
+ int fd = *get_from_IntVec(&cmd.child_open_fds, j);
close(fd);
}
@@ -53,19 +55,30 @@ void execute_cmd(ExecutableCommand cmd, int* pgid) {
ExecutableCommand process_pl_command(PlCommand pl_cmd) {
StrVec arg_vec = make_StrVec(256);
+ FdRedirectVec fd_redirs = make_FdRedirectVec(256);
+ IntVec parent_open_fds = make_IntVec(256);
+ IntVec child_open_fds = make_IntVec(256);
+
+ int ps_pipe_fds[2];
+
for (int i = 0; i < pl_cmd.args.count; i++) {
Arg arg = *get_from_ArgVec(&pl_cmd.args, i);
switch (arg.kind) {
case ARG_LIT:
push_to_StrVec(&arg_vec, arg.as.lit_str);
break;
+ case ARG_PS_IN:
+ pipe(ps_pipe_fds);
+ execute_command_pipeline(*(Pipeline*)arg.as.sub_pipeline, STDIN_FILENO, ps_pipe_fds[1]);
+ close(ps_pipe_fds[1]);
+ char pname[64];
+ snprintf(pname, sizeof(pname), "/dev/fd/%d", ps_pipe_fds[0]);
+ break;
default:
exit(67);
}
}
- FdRedirectVec fd_redirs = make_FdRedirectVec(256);
- IntVec open_fds = make_IntVec(256);
for (int j = 0; j < pl_cmd.redirects.count; j++) {
Redirect redir = *get_from_RedirectVec(&pl_cmd.redirects, j);
FdRedirect fd_redir;
@@ -73,17 +86,20 @@ ExecutableCommand process_pl_command(PlCommand pl_cmd) {
switch (redir.kind) {
case REDIR_READ:
fd = open(redir.as.filename, O_RDONLY, 0644);
- push_to_IntVec(&open_fds, fd);
+ push_to_IntVec(&parent_open_fds, fd);
+ push_to_IntVec(&child_open_fds, fd);
fd_redir = (FdRedirect){.to = fd, .redirected = STDIN_FILENO};
break;
case REDIR_WRITE_APPEND:
fd = open(redir.as.filename, O_WRONLY | O_CREAT | O_APPEND, 0644);
- push_to_IntVec(&open_fds, fd);
+ push_to_IntVec(&parent_open_fds, fd);
+ push_to_IntVec(&child_open_fds, fd);
fd_redir = (FdRedirect){.to = fd, .redirected = STDOUT_FILENO};
break;
case REDIR_WRITE_TRUNC:
fd = open(redir.as.filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
- push_to_IntVec(&open_fds, fd);
+ push_to_IntVec(&parent_open_fds, fd);
+ push_to_IntVec(&child_open_fds, fd);
fd_redir = (FdRedirect){.to = fd, .redirected = STDOUT_FILENO};
break;
default:
@@ -98,12 +114,12 @@ ExecutableCommand process_pl_command(PlCommand pl_cmd) {
}
args[arg_vec.count] = NULL;
- return (ExecutableCommand){.args = args, .redirects = fd_redirs, .open_fds = open_fds};
+ return (ExecutableCommand){.args = args, .redirects = fd_redirs, .parent_open_fds = parent_open_fds, .child_open_fds = child_open_fds};
}
-void execute_command_pipeline(Pipeline pipeline) {
- int trunk_fd = STDIN_FILENO;
+void execute_command_pipeline(Pipeline pipeline, int stdin_fd, int stdout_fd) {
+ int trunk_fd = stdin_fd;
int pgid = -1;
@@ -116,23 +132,25 @@ void execute_command_pipeline(Pipeline pipeline) {
exec_cmd.stdin_fd = trunk_fd;
if (i > 0) {
- push_to_IntVec(&exec_cmd.open_fds, trunk_fd);
+ push_to_IntVec(&exec_cmd.parent_open_fds, trunk_fd);
+ push_to_IntVec(&exec_cmd.child_open_fds, trunk_fd);
}
- exec_cmd.stdout_fd = STDOUT_FILENO;
+ exec_cmd.stdout_fd = stdout_fd;
if (cmd.conn == CONN_PIPE) {
int pipe_fds[2];
pipe(pipe_fds);
exec_cmd.stdout_fd = pipe_fds[1];
- push_to_IntVec(&exec_cmd.open_fds, pipe_fds[1]);
+ push_to_IntVec(&exec_cmd.parent_open_fds, pipe_fds[1]);
+ push_to_IntVec(&exec_cmd.child_open_fds, pipe_fds[1]);
trunk_fd = pipe_fds[0];
}
execute_cmd(exec_cmd, &pgid);
- for (int j = 0; j < exec_cmd.open_fds.count; j++) {
- int fd = *get_from_IntVec(&exec_cmd.open_fds, j);
+ for (int j = 0; j < exec_cmd.parent_open_fds.count; j++) {
+ int fd = *get_from_IntVec(&exec_cmd.parent_open_fds, j);
close(fd);
}
}
@@ -143,5 +161,5 @@ void execute_command_pipeline(Pipeline pipeline) {
}
void execute_pipeline_tree(PipelineTree pltree) {
- execute_command_pipeline(*pltree.root);
+ execute_command_pipeline(*pltree.root, STDIN_FILENO, STDOUT_FILENO);
}
diff --git a/src/preproc.c b/src/preproc.c
index 63e618f..d64f4c3 100644
--- a/src/preproc.c
+++ b/src/preproc.c
@@ -80,6 +80,7 @@ Pipeline* process_token_sequence(TokenVec tokens, PipelineVec* pl_pool) {
ExpandedToken curr_tok = *get_from_ExpandedTokenVec(&exp_tokens, i);
ExpandedToken next_tok;
Redirect rd;
+ Pipeline* sub_pl;
switch (curr_tok.kind) {
case TOK_WORD:
push_to_ArgVec(&curr_cmd.args, (Arg){.kind = ARG_LIT, .as.lit_str = curr_tok.as.word});
@@ -118,8 +119,14 @@ Pipeline* process_token_sequence(TokenVec tokens, PipelineVec* pl_pool) {
exit(63);
}
break;
- default:
- exit(62);
+ case SPEC_SUB_IN:
+ sub_pl = process_token_sequence(*(TokenVec*)curr_tok.as.spec.as.proc_sub_region, pl_pool);
+ push_to_ArgVec(&curr_cmd.args, (Arg){.kind = ARG_PS_IN, .as.sub_pipeline = sub_pl});
+ break;
+ case SPEC_SUB_OUT:
+ sub_pl = process_token_sequence(*(TokenVec*)curr_tok.as.spec.as.proc_sub_region, pl_pool);
+ push_to_ArgVec(&curr_cmd.args, (Arg){.kind = ARG_PS_OUT, .as.sub_pipeline = sub_pl});
+ break;
}
}
}
@@ -129,7 +136,6 @@ Pipeline* process_token_sequence(TokenVec tokens, PipelineVec* pl_pool) {
PipelineTree process_ast(Ast ast) {
PipelineVec pls = make_PipelineVec(256);
- TokenVec root_toks = *ast.root;
- Pipeline* root_pl = process_token_sequence(root_toks, &pls);
+ Pipeline* root_pl = process_token_sequence(*ast.root, &pls);
return (PipelineTree){.pl_pool = pls, root_pl};
}
diff --git a/src/syntax.h b/src/syntax.h
index 0cdb76c..a0ccfac 100644
--- a/src/syntax.h
+++ b/src/syntax.h
@@ -1,6 +1,6 @@
# pragma once
-#include "../src/utils.h"
+#include "utils.h"
// Stage 1
@@ -90,7 +90,7 @@ typedef struct {
SpecialTokenKind kind;
union {
OperatorToken op;
- void* proc_sub_region;
+ void* proc_sub_region; // (TokenVec)
} as;
} SpecialToken;
@@ -190,5 +190,6 @@ typedef struct {
char** args;
FdRedirectVec redirects;
int stdin_fd, stdout_fd;
- IntVec open_fds;
+ IntVec parent_open_fds;
+ IntVec child_open_fds;
} ExecutableCommand;