diff options
Diffstat (limited to 'redes')
| -rw-r--r-- | redes/exec.c | 80 | ||||
| -rw-r--r-- | redes/lexparse.c | 2 | ||||
| -rw-r--r-- | redes/syntax.h | 42 |
3 files changed, 105 insertions, 19 deletions
diff --git a/redes/exec.c b/redes/exec.c new file mode 100644 index 0000000..18ea59e --- /dev/null +++ b/redes/exec.c @@ -0,0 +1,80 @@ +#include <unistd.h> +#include "syntax.h" + + +void exec_cmd(ExecutableCommand cmd, int* pgid) { + + pid_t pid = fork(); + + if (pid == 0) { + + if (*pgid == -1) { + setpgid(0, 0); + } else { + setpgid(0, *pgid); + } + + dup2(cmd.stdin_fd, STDIN_FILENO); + dup2(cmd.stdout_fd, STDOUT_FILENO); + + for (int i = 0; i < cmd.open_fds.count; i++) { + FdRedirect fd_rd = *get_from_FdRedirectVec(&cmd.redirects, i); + dup2(fd_rd.to, fd_rd.redirected); + } + + signal(SIGINT, SIG_DFL); + signal(SIGQUIT, SIG_DFL); + signal(SIGTSTP, SIG_DFL); + signal(SIGTTIN, SIG_DFL); + signal(SIGTTOU, SIG_DFL); + + execvp(cmd.args[0], cmd.args); + exit(67); + } + + if (*pgid == -1) { + setpgid(pid, pid); + *pgid = pid; + } else { + setpgid(pid, *pgid); + } +} + + +ExecutableCommand process_pl_command(PlCommand pl_cmd) { + StrVec arg_vec = make_StrVec(256); + 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; + default: + exit(67); + } + } + + FdRedirectVec fd_redirs = make_FdRedirectVec(256); + for (int j = 0; j < pl_cmd.redirects.count, j++) { + Redirect redir = *get_from_RedirectVec(&pl_cmd.redirects, j); + FdRedirect fd_redir; + int fd; + switch (redir.kind) { + case REDIR_FILE_IN: + fd = + } + } +} + + +void execute_command_pipeline(PlCommand* pipeline) { + int trunk_fd = STDIN_FILENO; + int pgid = -1; + + size_t i = 0; + Connector last_conn; + do { + PlCommand cmd = pipeline[i]; + + } while (last_conn != CONN_EOL); +} diff --git a/redes/lexparse.c b/redes/lexparse.c index 0ea86df..d23268c 100644 --- a/redes/lexparse.c +++ b/redes/lexparse.c @@ -1,6 +1,6 @@ #include <ctype.h> #include "syntax.h" -#include "utils.h" +#include "../src/utils.h" typedef struct { Word word_germ; diff --git a/redes/syntax.h b/redes/syntax.h index 8404d15..1c43e49 100644 --- a/redes/syntax.h +++ b/redes/syntax.h @@ -1,6 +1,6 @@ # pragma once -#include "utils.h" +#include "../src/utils.h" // Stage 1 @@ -106,8 +106,9 @@ typedef struct { typedef enum { - REDIR_FILE_IN, - REDIR_FILE_OUT, + REDIR_WRITE_TRUNC, + REDIR_WRITE_APPEND, + REDIR_READ, REDIR_FDS, } RedirectKind; @@ -121,13 +122,23 @@ typedef struct { DECLARE_VEC(Redirect, RedirectVec) +typedef struct PlCommand PlCommand; + +typedef enum { + ARG_PS_IN, + ARG_PS_OUT, + ARG_LIT, +} ArgKind; + typedef struct { - char** args; - RedirectVec redirects; -} SimpleCommand; + ArgKind kind; + union { + char* lit_str; + PlCommand* sub_tree; + } as; +} Arg; -DECLARE_VEC(SimpleCommand, SimpleCommandVec) -DECLARE_VEC(SimpleCommand*, SimpleCommandPtrVec) +DECLARE_VEC(Arg, ArgVec) typedef enum { @@ -135,18 +146,13 @@ typedef enum { CONN_EOL, } Connector; -typedef struct { - SimpleCommand* cmd; - SimpleCommandPtrVec deps; +struct PlCommand { + ArgVec args; + RedirectVec redirects; Connector conn; -} PlCommand; - -DECLARE_VEC(PlCommand, PlCommandVec) +}; -typedef struct { - SimpleCommandVec commands; - PlCommandVec pipeline; -} ExecTree; +DECLARE_VEC(PlCommand, Pipeline) |
