From d31d9ce64c3085bea1a094e649f62176b1a896a6 Mon Sep 17 00:00:00 2001 From: Ákos Kőrösi Date: Mon, 18 May 2026 12:29:53 +0200 Subject: put line processing in exec --- exec.c | 18 ++++++++++++++++++ exec.h | 2 +- main.c | 15 +-------------- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/exec.c b/exec.c index 7147bfd..45c97b8 100644 --- a/exec.c +++ b/exec.c @@ -196,3 +196,21 @@ void run_pipeline(Pipeline pl, ShellState* shstate) { while (waitpid(-pgid, NULL, 0) > 0); tcsetpgrp(STDIN_FILENO, getpgrp()); } + + +void process_input_line(char* line, ShellState* shstate) { + PreprocArena arena = { + .tokens = make_TokenVec(256), + .chunks = make_ChunkVec(256), + .regions = make_RegionVec(256), + }; + TokenPtrVec line_tok_ptrs = lex(line, &arena); + Token* line_toks = malloc(sizeof(Token) * line_tok_ptrs.count); + for (int i = 0; i < line_tok_ptrs.count; i++) { + line_toks[i] = **get_from_TokenPtrVec(&line_tok_ptrs, i); + } + Pipeline pl = parse_tokstream(line_toks); + run_pipeline(pl, shstate); + free(line_toks); +} + diff --git a/exec.h b/exec.h index 1e23cdc..2424801 100644 --- a/exec.h +++ b/exec.h @@ -9,4 +9,4 @@ typedef struct { KeyValMap shell_vars; } ShellState; -void run_pipeline(Pipeline pl, ShellState* shstate); +void process_input_line(char* line, ShellState* shstate); diff --git a/main.c b/main.c index 3fe10bf..52e1691 100644 --- a/main.c +++ b/main.c @@ -3,7 +3,6 @@ #include #include -#include "preproc.h" #include "exec.h" @@ -14,19 +13,7 @@ int main() { line = readline("$ "); if (line[0] == '\0') continue; if (strcmp(line, "exit") == 0) exit(0); - PreprocArena arena = { - .tokens = make_TokenVec(256), - .chunks = make_ChunkVec(256), - .regions = make_RegionVec(256), - }; - TokenPtrVec line_tok_ptrs = lex(line, &arena); - Token* tokstream = malloc(sizeof(Token) * line_tok_ptrs.count); - for (int i = 0; i < line_tok_ptrs.count; i++) { - tokstream[i] = **get_from_TokenPtrVec(&line_tok_ptrs, i); - } - Pipeline pl = parse_tokstream(tokstream); - run_pipeline(pl, &shstate); - free(tokstream); + process_input_line(line, &shstate); } free(line); } -- cgit v1.2.3