diff options
author | dzwdz | 2022-07-27 21:09:31 +0200 |
---|---|---|
committer | dzwdz | 2022-07-27 21:09:31 +0200 |
commit | e8aedb0ef4f73961a98a1505527dc9ec8780a0d5 (patch) | |
tree | 50cdabeab0f66c66bfeb00259da0caccb2dee65e /src/user/app/shell/shell.c | |
parent | ba7aecffc3b52b39c27558b693ed4026298b58c9 (diff) |
user/shell: basic shell script support
Diffstat (limited to 'src/user/app/shell/shell.c')
-rw-r--r-- | src/user/app/shell/shell.c | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/src/user/app/shell/shell.c b/src/user/app/shell/shell.c index e1514d9..995ce53 100644 --- a/src/user/app/shell/shell.c +++ b/src/user/app/shell/shell.c @@ -10,11 +10,10 @@ int main(); -// TODO fgets -static int readline(char *buf, size_t max) { +static int readline(char *buf, size_t max, FILE *f) { char c = '\0'; size_t pos = 0; - while (pos < (max-1) && c != '\n' && fread(&c, 1, 1, stdin)) + while (pos < (max-1) && c != '\n' && fread(&c, 1, 1, f)) buf[pos++] = c; buf[pos++] = '\0'; return pos; @@ -111,12 +110,23 @@ static void run(char *cmd) { } -int main(void) { +int main(int argc, char **argv) { static char buf[256]; + FILE *f = stdin; + + if (argc > 1) { + f = fopen(argv[1], "r"); + if (!f) { + printf("sh: couldn't open %s\n", argv[1]); + return 1; + } + } + for (;;) { - printf("$ "); - readline(buf, 256); - if (feof(stdin)) return 0; + if (f == stdin) + printf("$ "); + readline(buf, 256, f); + if (feof(f)) return 0; run(buf); } } |