make nutshell build
This commit is contained in:
parent
b3b27ceff1
commit
29b9cb9bd5
7
Makefile
7
Makefile
@ -84,8 +84,11 @@ lowercase: sysexits src/lowercase.c
|
||||
nonzero: src/nonzero.c
|
||||
$(CC) $(CFLAGS) -o bin/nonzero src/nonzero.c
|
||||
|
||||
nutshell: src/nutshell.c src/nutshell.h src/nutshell_builtins.c
|
||||
$(CC) $(CFLAGS) -o bin/nutshell src/nutshell.c
|
||||
nutshell.o: libio usefulmacros src/nutshell.c src/nutshell.h src/nutshell_builtins.c
|
||||
$(CC) $(CFLAGS) -c -o build/nutshell.o src/nutshell.c
|
||||
|
||||
nutshell: libio nutshell.o
|
||||
$(CC) $(CFLAGS) -o bin/nutshell build/libio.o build/nutshell.o
|
||||
|
||||
roll.o: lib/libio.h src/roll.c sysexits
|
||||
$(CC) $(CFLAGS) -c -o build/roll.o src/roll.c
|
||||
|
@ -1,26 +1,17 @@
|
||||
#include <ctype.h> /* isspace(3) */
|
||||
#include <stddef.h> /* NULL */
|
||||
#include <stdio.h> /* getc(3) */
|
||||
#include <stdlib.h> /* getenv(3), rand(3), srand(3) */
|
||||
#include <string.h> /* strcmp(3) */
|
||||
#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"
|
||||
|
||||
int
|
||||
fdputd(int fd, int d){
|
||||
if(d < 0){
|
||||
putc('-', stdout);
|
||||
d *= -1;
|
||||
}
|
||||
return printf("%d\n", d);
|
||||
}
|
||||
|
||||
/* Probably not buggy. */
|
||||
int
|
||||
fdputs(int fd, char *s){
|
||||
int r;
|
||||
|
||||
r = write(1, s, strlen(s));
|
||||
r += write(1, "\n", 1);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
static int
|
||||
parse_argv(char *buffer, char **argv, int argc_max){
|
||||
int i;
|
||||
@ -82,9 +73,8 @@ run(char *buf){
|
||||
argc = parse_argv(buf, argv, ARRAYLEN(argv));
|
||||
|
||||
/* builtins get priority */
|
||||
for(i = 0; i < ARRAYLEN(builtins); ++i)
|
||||
if(strcmp(builtins[i].name, argv[0]) == 0)
|
||||
return builtins[i].f(argc, argv);
|
||||
if((i = isbuiltin(argv[0])) != 0)
|
||||
return builtins[i - 1].f(argc, argv);
|
||||
|
||||
if(state.jailed)
|
||||
goto runerr;
|
||||
@ -96,8 +86,8 @@ runerr:
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
static char buf[BUF_MAX];
|
||||
static char prompt[] = DEFAULT_PROMPT;
|
||||
|
||||
srand(time(NULL));
|
||||
builtin_init(0, empty_argv);
|
||||
|
||||
for(;;){
|
||||
|
@ -1,41 +1,10 @@
|
||||
#include <ctype.h> /*
|
||||
isspace(3)
|
||||
*/
|
||||
#include <stddef.h> /*
|
||||
NULL
|
||||
*/
|
||||
#include <stdio.h> /*
|
||||
getc(3)
|
||||
*/
|
||||
#include <stdlib.h> /*
|
||||
getenv(3)
|
||||
rand(3)
|
||||
srand(3)
|
||||
*/
|
||||
#include <string.h> /*
|
||||
strcmp(3)
|
||||
strlen(3)
|
||||
*/
|
||||
#include <sys/wait.h> /*
|
||||
wait(2)
|
||||
*/
|
||||
#include <time.h> /*
|
||||
time(3)
|
||||
*/
|
||||
#include <unistd.h> /*
|
||||
fork(2)
|
||||
write(2)
|
||||
*/
|
||||
|
||||
#define ARRAYLEN(a) sizeof(a) / sizeof(*(a))
|
||||
|
||||
/* conservative defaults */
|
||||
#define ARGV_MAX 10
|
||||
#define BUF_MAX 100
|
||||
|
||||
#define DEFAULT_PROMPT "^_^?: "
|
||||
|
||||
char *empty_argv[1] = { NULL };
|
||||
char *nethack_name = "nethack";
|
||||
char prompt[] = "^_^?: ";
|
||||
|
||||
struct {
|
||||
int jailed;
|
||||
@ -44,6 +13,3 @@ struct {
|
||||
0,
|
||||
0
|
||||
};
|
||||
|
||||
int fdputd(int fd, int d);
|
||||
int fdputs(int fd, char *s);
|
||||
|
@ -1,3 +1,6 @@
|
||||
#define NETHACK_NAME "nethack"
|
||||
|
||||
int builtin_builtin (int, char **);
|
||||
int builtin_escape (int, char **);
|
||||
int builtin_init (int, char **);
|
||||
int builtin_jail (int, char **);
|
||||
@ -9,13 +12,44 @@ struct {
|
||||
char *name;
|
||||
int (*f)(int, char **);
|
||||
}builtins[] = {
|
||||
{"escape", builtin_escape},
|
||||
/* guaranteed to exist */
|
||||
{"builtin", builtin_builtin},
|
||||
{"init", builtin_init},
|
||||
|
||||
/* optional additions */
|
||||
{"escape", builtin_escape},
|
||||
{"jail", builtin_jail},
|
||||
{"nethack", builtin_nethack},
|
||||
{"status", builtin_status}
|
||||
};
|
||||
|
||||
int
|
||||
isbuiltin(char *s){
|
||||
int i;
|
||||
|
||||
for(i = 0; i < ARRAYLEN(builtins); ++i)
|
||||
if(strcmp(builtins[i].name, s) == 0)
|
||||
return i + 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
builtin_builtin(int argc, char **argv){
|
||||
int i;
|
||||
|
||||
if(argc < 2){
|
||||
fdputs(2, "%s: needs argument(s)");
|
||||
return 1;
|
||||
}
|
||||
|
||||
++argv;
|
||||
|
||||
while(*argv != NULL)
|
||||
if(!isbuiltin(*argv))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
builtin_escape(int argc, char **argv){
|
||||
char *s;
|
||||
@ -32,11 +66,7 @@ builtin_escape(int argc, char **argv){
|
||||
|
||||
int
|
||||
builtin_init(int argc, char **argv){
|
||||
fdputs(1,"\
|
||||
\
|
||||
A voice whispers to you. \"Play NetHack...\" (\"nethack\" [enter]).\n\
|
||||
What would you like to do?"
|
||||
);
|
||||
srand(time(NULL));
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -54,7 +84,9 @@ builtin_jail(int argc, char **argv){
|
||||
|
||||
int
|
||||
builtin_nethack(int argc, char **argv){
|
||||
static char *nethack_name = NETHACK_NAME;
|
||||
char *a[2];
|
||||
|
||||
if(fork() == 0){
|
||||
a[0] = nethack_name;
|
||||
a[1] = NULL;
|
||||
|
Loading…
Reference in New Issue
Block a user