summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/exec.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/exec.c b/src/exec.c
index e3ea28f..86175a9 100644
--- a/src/exec.c
+++ b/src/exec.c
@@ -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);