1
0

technically, it builds

This commit is contained in:
dtb 2023-12-12 20:59:09 -07:00
parent 09a04a2e88
commit c40c3079dd
3 changed files with 17 additions and 17 deletions

2
wip/nutshell/Makefile Normal file
View File

@ -0,0 +1,2 @@
nutshell: nutshell.c nutshell.h nutshell_builtins.c
$(CC) $(CFLAGS) -o nutshell nutshell.c

View File

@ -6,14 +6,12 @@
#include <sys/wait.h> /* wait(2) */
#include <time.h> /* time(3) */
#include <unistd.h> /* 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;
}

View File

@ -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;
}