diff options
| author | Ákos Kőrösi <korakos99@gmail.com> | 2026-05-22 11:16:46 +0200 |
|---|---|---|
| committer | Ákos Kőrösi <korakos99@gmail.com> | 2026-05-22 11:16:46 +0200 |
| commit | 75c3a166b98aa5c89fa3a8c82c70ef9f9fb54416 (patch) | |
| tree | ec27b7d5f5baf5c0995c59eadb0a5d00b202f243 /src | |
| parent | 77dfc38c19b9602c08777686f626975c54cb9183 (diff) | |
add sourcing
Diffstat (limited to 'src')
| -rw-r--r-- | src/exec.c | 23 |
1 files changed, 23 insertions, 0 deletions
@@ -8,6 +8,7 @@ #include "syntax.h" #include "utils.h" +#include "stages.h" void execute_command_pipeline(Pipeline pipeline, int stdin_fd, int stdout_fd, bool wait_here, int* pgid, ShellState* shstate); @@ -25,6 +26,24 @@ MaybeStrPair parse_assignment(char* token) { } +void source_script(char* fname, ShellState* shstate) { + // TODO: add stdin/out (to other builtins too, actually) + FILE* fp = fopen(fname, "r"); // TODO: checks + if (fp == 0) exit(1); + + char* line = NULL; + size_t len = 0; + + while (getline(&line, &len, fp) != -1) { + line[strcspn(line, "\r\n")] = '\0'; + execute_line(line, shstate); + } + free(line); + fclose(fp); + +} + + bool handle_builtin(ExecutableCommand cmd, ShellState* shstate) { MaybeStrPair m_assignment; assert(cmd.args[0] != NULL); @@ -48,6 +67,10 @@ bool handle_builtin(ExecutableCommand cmd, ShellState* shstate) { assert(m_assignment.some); set_map_pair(&shstate->aliases, m_assignment.value); return true; + } else if (strcmp(cmd.args[0], "source") == 0) { + assert(cmd.args[1] != NULL); + source_script(cmd.args[1], shstate); + return true; } else if (strcmp(cmd.args[0], "export") == 0) { m_assignment = parse_assignment(cmd.args[1]); assert(m_assignment.some); |
