summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2024-09-09 16:07:34 +0200
committerAnton Kling <anton@kling.gg>2024-09-12 00:53:49 +0200
commit785684b9fe760ba5aa591032dbdef1ca2b9aaa45 (patch)
treea27238bce76dae18756b2a0e452059c4653322d6
parent1da6c154e32c171b4fbd9190344ba4c5c71c0d88 (diff)
sh: Load script from file
-rw-r--r--userland/minibox/utilities/sh/sh.c26
1 files changed, 24 insertions, 2 deletions
diff --git a/userland/minibox/utilities/sh/sh.c b/userland/minibox/utilities/sh/sh.c
index 433c2e1..1505de3 100644
--- a/userland/minibox/utilities/sh/sh.c
+++ b/userland/minibox/utilities/sh/sh.c
@@ -145,8 +145,30 @@ void get_line(struct sb *s) {
}
int sh_main(int argc, char **argv) {
- (void)argc;
- (void)argv;
+ if (argc > 1) {
+ int fd = open(argv[1], O_RDONLY, 0);
+ char buffer[8192];
+ struct sb file;
+ sb_init(&file);
+
+ for (;;) {
+ int rc = read(fd, buffer, 8192);
+ if (0 == rc) {
+ break;
+ }
+ sb_append_buffer(&file, buffer, rc);
+ }
+
+ close(fd);
+ struct TOKEN *h = lex(SB_TO_SV(file));
+ struct AST *ast_h = generate_ast(h);
+ execute_ast(ast_h);
+ free_tokens(h);
+ free_ast(ast_h);
+ sb_free(&file);
+ return 0;
+ }
+
for (;;) {
char buffer[256];
printf("%s : ", getcwd(buffer, 256));