diff --git a/wip/nutshell/Makefile b/wip/nutshell/Makefile new file mode 100644 index 0000000..bd15bbb --- /dev/null +++ b/wip/nutshell/Makefile @@ -0,0 +1,2 @@ +nutshell: nutshell.c nutshell.h nutshell_builtins.c + $(CC) $(CFLAGS) -o nutshell nutshell.c diff --git a/wip/nutshell/nutshell.c b/wip/nutshell/nutshell.c index 09fc63d..5e1f357 100644 --- a/wip/nutshell/nutshell.c +++ b/wip/nutshell/nutshell.c @@ -6,14 +6,12 @@ #include /* wait(2) */ #include /* time(3) */ #include /* fork(2), write(2) */ -#include "libio.h" -#include "usefulmacros.h" #include "nutshell.h" #include "nutshell_builtins.c" static int -parse_argv(char *buffer, char **argv, int argc_max){ +parse_argv(int *buffer, char **argv, int argc_max){ int i; int j; int s; @@ -30,8 +28,8 @@ parse_argv(char *buffer, char **argv, int argc_max){ return j; } -static char * -read_buffer(char *buf, size_t buf_s){ +static int * +read_buffer(int *buf, size_t buf_s){ int i; for(i = 0; ; ++i){ @@ -58,19 +56,19 @@ read_buffer(char *buf, size_t buf_s){ } static int -run(char *buf){ +run(int *buf){ int argc; static char *argv[ARGV_MAX]; int i; /* get argv and argc */ - for(i = 0; i < ARRAYLEN(argv); ++i){ + for(i = 0; i < (sizeof argv) / (sizeof *argv); ++i){ if(argv[i] == NULL) break; argv[i] = NULL; } - argc = parse_argv(buf, argv, ARRAYLEN(argv)); + argc = parse_argv(buf, argv, (sizeof argv) / (sizeof *argv)); /* builtins get priority */ if((i = isbuiltin(argv[0])) != 0) @@ -85,15 +83,15 @@ runerr: } int main(int argc, char *argv[]){ - static char buf[BUF_MAX]; + static int buf[BUF_MAX]; static char prompt[] = DEFAULT_PROMPT; builtin_init(0, empty_argv); for(;;){ - write(1, prompt, ARRAYLEN(prompt)); - if(read_buffer(buf, ARRAYLEN(buf)) == NULL){ - write(1, "EOF\n", 4); + fprintf(stdout, "%s", prompt); + if(read_buffer(buf, (sizeof buf)/(sizeof *buf)) == NULL){ + fprintf(stdout, "EOF\n"); state.status = 0; break; } diff --git a/wip/nutshell/nutshell_builtins.c b/wip/nutshell/nutshell_builtins.c index 18f3b63..db69051 100644 --- a/wip/nutshell/nutshell_builtins.c +++ b/wip/nutshell/nutshell_builtins.c @@ -27,7 +27,7 @@ int isbuiltin(char *s){ int i; - for(i = 0; i < ARRAYLEN(builtins); ++i) + for(i = 0; i < (sizeof builtins) / (sizeof *builtins); ++i) if(strcmp(builtins[i].name, s) == 0) return i + 1; return 0; @@ -38,7 +38,7 @@ builtin_builtin(int argc, char **argv){ int i; if(argc < 2){ - fdputs(2, "%s: needs argument(s)"); + fprintf(stderr, "needs argument(s)\n"); return 1; } @@ -55,7 +55,7 @@ builtin_escape(int argc, char **argv){ char *s; if((s = getenv("SHELL")) == NULL){ - fdputs(1, "$SHELL seems to be empty. This is an OS error.\n"); + fprintf(stdout, "$SHELL seems to be empty. This is an OS error.\n"); }else if(fork() == 0) execl(s, s, NULL); else @@ -74,7 +74,7 @@ int builtin_jail(int argc, char **argv){ printf("%s\n", argv[1]); if(argv[1] == NULL) - fdputd(1, state.jailed); + fprintf(stdout, "%d", state.jailed); else if(strcmp(argv[1], "break") == 0 || strcmp(argv[1], "build") == 0) state.jailed = (argv[1][1] - 'r') / ('u' - 'r'); /* cheap */ else @@ -99,6 +99,6 @@ builtin_nethack(int argc, char **argv){ int builtin_status(int argc, char **argv){ - fdputd(1, state.status); + fprintf(stdout, "%d", state.status); return 0; }