summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÁkos Kőrösi <korakos99@gmail.com>2026-05-20 16:22:26 +0200
committerÁkos Kőrösi <korakos99@gmail.com>2026-05-20 16:22:26 +0200
commit334b39474648df09f96a1cdd2a5f49e4b9494b66 (patch)
tree392f1d15dffc3184ec6b69315a29efcf8672965f
parent77e7fb03e8ed61fa4df227188788958c13d27fdf (diff)
save
-rw-r--r--redes/exec.c51
-rw-r--r--redes/lexparse.c81
-rw-r--r--redes/preproc.c11
-rw-r--r--redes/syntax.h9
-rw-r--r--src/utils.h4
5 files changed, 105 insertions, 51 deletions
diff --git a/redes/exec.c b/redes/exec.c
index 18ea59e..f7928af 100644
--- a/redes/exec.c
+++ b/redes/exec.c
@@ -2,7 +2,7 @@
#include "syntax.h"
-void exec_cmd(ExecutableCommand cmd, int* pgid) {
+void execute_cmd(ExecutableCommand cmd, int* pgid) {
pid_t pid = fork();
@@ -55,15 +55,40 @@ ExecutableCommand process_pl_command(PlCommand pl_cmd) {
}
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;
int fd;
switch (redir.kind) {
case REDIR_FILE_IN:
- fd =
+ fd = open(redir.as.filename, O_RDONLY, 0644);
+ push_to_IntVec(&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);
+ 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);
+ fd_redir = (FdRedirect){.to = fd, .redirected = STDOUT_FILENO};
+ break;
+ default:
+ exit(67);
}
+ push_to_FdRedirectVec(&fd_redirs, fd_redir);
+ }
+
+ char** args = malloc(sizeof(char*) * (arg_vec.count + 1));
+ for (int i = 0; i < arg_vec.count; i++) {
+ args[i] = *get_from_StrVec(&arg_vec, i);
}
+ args[arg_vec.count] = NULL;
+
+ return (ExecutableCommand){.args = args, .redirects = fd_redirs, .open_fds = open_fds};
}
@@ -75,6 +100,28 @@ void execute_command_pipeline(PlCommand* pipeline) {
Connector last_conn;
do {
PlCommand cmd = pipeline[i];
+ ExecutableCommand exec_cmd = process_pl_command(cmd);
+ exec_cmd.stdin_fd = trunk_fd;
+ exec_cmd.stdout_fd = STDOUT_FILENO;
+
+ 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]);
+ 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.count, j);
+ close(fd);
+ }
} while (last_conn != CONN_EOL);
+
+ tcsetpgrp(STDIN_FILENO, pgid);
+ while (waitpid(-pgid, NULL, 0) > 0);
+ tcsetpgrp(STDIN_FILENO, getpgrp());
}
diff --git a/redes/lexparse.c b/redes/lexparse.c
index d23268c..adf9d1d 100644
--- a/redes/lexparse.c
+++ b/redes/lexparse.c
@@ -12,7 +12,9 @@ typedef struct {
TokenPtrVec* top_tokens;
} PseudoAst;
-static void consolidate_buffer(LexingBuffer* buf, PseudoAst ps_ast) {
+DECLARE_MAYBE(Token, MaybeToken)
+
+MaybeToken consolidate_buffer(LexingBuffer* buf) {
if (buf->lit_germ.count > 0) {
push_to_Word(&buf->word_germ, (Chunk){
.qmode = UN_Q,
@@ -22,12 +24,15 @@ static void consolidate_buffer(LexingBuffer* buf, PseudoAst ps_ast) {
buf->lit_germ = make_String(256);
}
if (buf->word_germ.count > 0) {
- Token* tok_ptr = push_to_TokenVec(ps_ast.token_pool, (Token){
- .kind = TOK_WORD,
- .as.word = buf->word_germ
- });
- push_to_TokenPtrVec(ps_ast.top_tokens, tok_ptr);
+ return (MaybeToken){
+ .some = true,
+ .value = (Token){
+ .kind = TOK_WORD,
+ .as.word = buf->word_germ
+ }
+ };
}
+ return (MaybeToken){.some = false};
}
@@ -117,21 +122,21 @@ static MaybeSpecialToken recognize_spec_token(char* line, size_t* pos) {
}
}
-static void lex_level(char* line, size_t* pos, char until, PseudoAst ps_ast) {
+TokenVec* lex_level(char* line, size_t* pos, char until, TokenVecVec* tok_seq_pool) {
+ TokenVec tokens = make_TokenVec(256);
LexingBuffer buf = {.word_germ = make_Word(256), .lit_germ = make_String(256)};
while (line[*pos] != until) {
if (line[*pos] == ' ') {
- consolidate_buffer(&buf, ps_ast);
+ MaybeToken m_tok = consolidate_buffer(&buf);
+ if (m_tok.some) {
+ push_to_TokenVec(&tokens, m_tok.value);
+ }
while (line[*pos] == ' ') (*pos)++;
continue;
}
MaybeSpecialToken m_spec = recognize_spec_token(line, pos);
if (m_spec.some) {
- Token* tok_ptr = push_to_TokenVec(ps_ast.token_pool, (Token){
- .kind = TOK_SPEC,
- .as.spec = m_spec.value
- });
- push_to_TokenPtrVec(ps_ast.top_tokens, tok_ptr);
+ push_to_TokenVec(&tokens, (Token){.kind = TOK_SPEC, .as.spec = m_spec.value});
continue;
}
@@ -143,43 +148,35 @@ static void lex_level(char* line, size_t* pos, char until, PseudoAst ps_ast) {
push_to_Word(&buf.word_germ, chunk);
} else if (line[*pos] == '<' && line[(*pos)+1] == '(') {
*pos += 2;
- consolidate_buffer(&buf, ps_ast);
- TokenPtrVec procsub_toks = make_TokenPtrVec(256);
- lex_level(line, pos, ')', (PseudoAst){
- .token_pool = ps_ast.token_pool,
- .top_tokens = &procsub_toks
- });
- (*pos)++; // consume ')'
- Token* tok_ptr = push_to_TokenVec(ps_ast.token_pool, (Token){
- .kind = TOK_SUB_IN,
- .as.procsub_toks = procsub_toks,
+ MaybeToken m_tok = consolidate_buffer(&buf);
+ if (m_tok.some) {
+ push_to_TokenVec(&tokens, m_tok.value);
+ }
+ TokenVec* sub_toks = lex_level(line, pos, ')', tok_seq_pool);
+ (*pos)++;
+ push_to_TokenVec(&tokens, (Token){
+ .kind = TOK_SUB_IN,
+ .as.procsub_token_vec = sub_toks
});
- push_to_TokenPtrVec(ps_ast.top_tokens, tok_ptr);
} else if (line[*pos] == '>' && line[(*pos)+1] == '(') {
*pos += 2;
- consolidate_buffer(&buf, ps_ast);
- TokenPtrVec procsub_toks = make_TokenPtrVec(256);
- lex_level(line, pos, ')', (PseudoAst){
- .token_pool = ps_ast.token_pool,
- .top_tokens = &procsub_toks
- });
- (*pos)++; // consume ')'
- Token* tok_ptr = push_to_TokenVec(ps_ast.token_pool, (Token){
- .kind = TOK_SUB_OUT,
- .as.procsub_toks = procsub_toks,
+ MaybeToken m_tok = consolidate_buffer(&buf);
+ if (m_tok.some) {
+ push_to_TokenVec(&tokens, m_tok.value);
+ }
+ TokenVec* sub_toks = lex_level(line, pos, ')', tok_seq_pool);
+ (*pos)++;
+ push_to_TokenVec(&tokens, (Token){
+ .kind = TOK_SUB_OUT,
+ .as.procsub_token_vec = sub_toks
});
- push_to_TokenPtrVec(ps_ast.top_tokens, tok_ptr);
}
}
}
Ast lex(char* input) {
size_t line_pos = 0;
- TokenVec token_pool = make_TokenVec(256);
- TokenPtrVec top_tokens = make_TokenPtrVec(256);
- lex_level(input, &line_pos, '\0', (PseudoAst){
- .token_pool = &token_pool,
- .top_tokens = &top_tokens
- });
- return (Ast){.token_pool = token_pool, .top_tokens = top_tokens};
+ TokenVecVec token_seqs = make_TokenVecVec(256);
+ TokenVec* root_ptr = lex_level(input, &line_pos, '\0', &token_seqs);
+ return (Ast){.region_pool = token_seqs, .root = root_ptr};
}
diff --git a/redes/preproc.c b/redes/preproc.c
new file mode 100644
index 0000000..f29b058
--- /dev/null
+++ b/redes/preproc.c
@@ -0,0 +1,11 @@
+#include "syntax.h"
+
+
+Pipeline process_token_sequence(TokenVec* token_pool, TokenPtrVec ptr_seq) {
+
+}
+
+
+Pipeline process_into_pipeline(Ast ast) {
+ exit(1);
+}
diff --git a/redes/syntax.h b/redes/syntax.h
index 1c43e49..70cfe22 100644
--- a/redes/syntax.h
+++ b/redes/syntax.h
@@ -89,15 +89,16 @@ struct Token {
union {
Word word;
SpecialToken spec;
- TokenPtrVec procsub_toks;
+ void* procsub_token_vec;
} as;
};
DECLARE_VEC(Token, TokenVec)
+DECLARE_VEC(TokenVec, TokenVecVec)
typedef struct {
- TokenVec token_pool;
- TokenPtrVec top_tokens;
+ TokenVecVec region_pool;
+ TokenVec* root;
} Ast;
@@ -134,7 +135,7 @@ typedef struct {
ArgKind kind;
union {
char* lit_str;
- PlCommand* sub_tree;
+ void* sub_pipeline;
} as;
} Arg;
diff --git a/src/utils.h b/src/utils.h
index a213c71..cbfaadd 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -69,11 +69,9 @@
return &vec->blocks \
[index / vec->block_size] \
[index % vec->block_size]; \
- }
-
+ } \
DECLARE_VEC(int, IntVec)
-
DECLARE_VEC(char, String);
DECLARE_VEC(char*, StrVec)