diff options
| author | Anton Kling <anton@kling.gg> | 2024-12-15 03:35:50 +0100 | 
|---|---|---|
| committer | Anton Kling <anton@kling.gg> | 2024-12-15 03:35:50 +0100 | 
| commit | 2f13c181e4cd760b793ff338f16253d3cd29a277 (patch) | |
| tree | 3cf348b47a8e1f81e748c0f25da9277b8190f98e | |
| parent | b26125c7c9163410bd11193909f04106f0582dc9 (diff) | |
sh: source /profile file
| -rw-r--r-- | userland/minibox/utilities/sh/sh.c | 51 | 
1 files changed, 31 insertions, 20 deletions
diff --git a/userland/minibox/utilities/sh/sh.c b/userland/minibox/utilities/sh/sh.c index e4a701c..e30e3f5 100644 --- a/userland/minibox/utilities/sh/sh.c +++ b/userland/minibox/utilities/sh/sh.c @@ -193,29 +193,40 @@ void get_line(struct sb *s) {    }  } -int sh_main(int argc, char **argv) { -  if (argc > 1) { -    int fd = open(argv[1], O_RDONLY, 0); -    char buffer[8192]; -    struct sb file; -    sb_init(&file); +int source_file(const char *path) { +  int fd = open(path, O_RDONLY, 0); +  if (-1 == fd) { +    return 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); +  for (;;) { +    int rc = read(fd, buffer, 8192); +    if (-1 == rc) { +      break;      } +    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; +  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 1; +} + +int sh_main(int argc, char **argv) { +  (void)source_file("/profile"); +  if (argc > 1) { +    return source_file(argv[1]) ? 0 : 1;    }    for (;;) {  |