diff options
Diffstat (limited to 'userspace')
| -rw-r--r-- | userspace/Makefile | 7 | ||||
| -rw-r--r-- | userspace/foolshell.c | 279 | ||||
| -rw-r--r-- | userspace/init.c | 2 |
3 files changed, 138 insertions, 150 deletions
diff --git a/userspace/Makefile b/userspace/Makefile index dbc2d0f..d39f15f 100644 --- a/userspace/Makefile +++ b/userspace/Makefile @@ -2,6 +2,8 @@ IMAGESIZE=10000 #ext2.img size in Kb ####################### +GIT_REVISION=$(shell git rev-parse HEAD) + CC=i686-foolos-gcc CC=i686-elf-gcc AS=i686-elf-as @@ -9,11 +11,13 @@ CC = @echo "Compiling (i686-elf-gcc) $<..."; i686-elf-gcc AS = @echo "Assembling (i686-elf-as) $<..."; i686-elf-as CFLAGS= -CFLAGS=-w +CFLAGS+=-DGIT_REVISION=\"$(GIT_REVISION)\" +CFLAGS+=-w CFLAGS+=-I.. CFLAGS+=-I/home/miguel/temp/foolos/usr/i686-foolos/include CFLAGS+=-O0 CFLAGS+=-g +CFLAGS+= -Werror=implicit-function-declaration LDFLAGS=-L/home/miguel/temp/foolos/usr/i686-foolos/lib/ LDLIBS=-lc -lm -lg -lnosys @@ -40,6 +44,7 @@ ext2.img: $(PROGS) @echo "Welcome to FoolOs\nWe hope you will enjoy your stay." > mnt/home/miguel/hello.txt @mkdir -p mnt/bin @mkdir -p mnt/doc/test + @mkdir -p mnt/sys # mountpoint for sysfs @cp test.txt mnt/doc/test/ @cp $(PROGS) mnt/bin @cp fonts/binfont.bin mnt/ diff --git a/userspace/foolshell.c b/userspace/foolshell.c index 0012592..e8b384e 100644 --- a/userspace/foolshell.c +++ b/userspace/foolshell.c @@ -1,47 +1,45 @@ +/** + * @file + * + * Fool's Shell + * ========= + * + * A minimalsitic and naive shell developed along the Fool OS kernel. + * TODO: Free tokenizer / dynamic size! + */ + #include <stdio.h> #include <stdint.h> #include <stdbool.h> #include <string.h> - -#include <reent.h> +#include <errno.h> +#include <string.h> extern char **environ; -extern struct _reent *_impure_ptr; -uint8_t buf_test[1024*300]; +bool process(char *buf); +bool cd(char *path); -// -void hello() +void version() +{ + puts("Fool's Shell version git-commit:" GIT_REVISION "\ncompiled on " __DATE__ " at " __TIME__); +} +void help() { - // ascci art: http://patorjk.com/software/taag/#p=testall&f=Cards&t=Fool%20OS - puts( - - "\033c" -// "\033[36m" - -// " ______ __ ____ _____ \n" -// " / ____/___ ____ / / / __ \\/ ___/ \n" -// " / /_ / __ \\/ __ \\/ / / / / /\\__ \\ \n" -// " / __/ / /_/ / /_/ / / / /_/ /___/ / \n" -// " /_/ \\____/\\____/_/ \\____//____/ \n" -// " \n" - -// "\033[37;44m" -// " \n" - - " Welcome to FoolShell v0.12 (Compiled on " __DATE__ " at " __TIME__")\n" - " ------------------------------------------------------------------\n\n" - " Please type 'help' anytime, to show shell \"built-ins\". You can execute \n" - " user programms that are in your $PATH directory by simply typing \n" - " their filenames. Get additional information for many, of the available\n" - " commands by invoking them with --help or -h. (e.g. ls --help) \n" - " \n" - - "\033[37;40m" - ); - - printf(" Your $PATH is currently set to: %s\n",getenv("PATH")); - printf(" Type 'ls %s' to list programms on your $PATH.\n\n",getenv("PATH")); + puts( "\nfoolshell: supported built-in commands/functions:\n\n" + + "'env' - show all environment variables\n" + "'getenv [var]' - show environment variable\n" + "'setenv [var] [val]' - set environemnet variable\n" + + "'cd [dir]' - change directory (set $PWD)\n" + "'[binary] [params...]' - run a binary\n" + "'[binary] [params...] &' - run a binary in background\n" + " TODO - pipes\n" + + "'help' - show this message\n" + "'exit' - exit running foolshell\n\n"); + } void prompt() @@ -49,23 +47,32 @@ void prompt() printf("\033[36mfool\033[37m@\033[32mhill\033[33m:%s%s\033[37m",getenv("PWD"),getenv("PS1")); } - int main(int argc, char **argv) { - bool silent=false; for(int i=0;i<argc;i++) { - if(!strcmp(argv[i],"--silent"))silent=true; + if(!strcmp(argv[i],"--version")) + { + version(); + return 0; + } + + if(!strcmp(argv[i],"--help")) + { + help(); + return 0; + } } - - if(!silent)hello(); char *buf=calloc(sizeof(char),256); + // input and output without buffering setvbuf(stdin,NULL,_IONBF,0); setvbuf(stdout,NULL,_IONBF,0); - while(1) + printf("\033c"); // clear screen + + while(1) // process commands until exit { prompt(); int bl=0; @@ -89,19 +96,18 @@ int main(int argc, char **argv) } } - //fgets(buf,255,stdin); - //buf[strlen(buf)-1]=0; // remove \n - //printf("[%s]",buf); - process(buf); + if(!process(buf))break; // process input and return if exit } return 0; } -char **tokenize(char *buf) +// break the input in tokens on spaces +char **tokenize(char *buf,char chr) { char **token; token=malloc(10*sizeof(char*)); + token=realloc(token,20*sizeof(char*)); int l=strlen(buf); @@ -114,13 +120,13 @@ char **tokenize(char *buf) token[c]=malloc(256); //skip all the whitespace - while(buf[i]==' '&&i<l)i++; + while(buf[i]==chr&&i<l)i++; if(i==l)break; //get token int t=0; - while(buf[i]!=' '&&i<l) + while(buf[i]!=chr&&i<l) { token[c][t]=buf[i]; t++; @@ -136,129 +142,106 @@ char **tokenize(char *buf) return token; } -int process(char *buf) +bool process(char *buf) { - char **token=tokenize(buf); + char **token=tokenize(buf,' '); char *command=token[0]; - // puts(command); - // copied from trottelshell - - if(!strcmp(command,"help")) - { - puts( "\nfoolshell: supported built-in commands:\n\n" - "'help' - show this message\n" - "'echo [string]' - print given string to stdout\n" - "'getenv [var]' - show environment variable\n" - "'putenv/setenv [var] [val]' - set environemnet variable\n" - "'env' - show all environment variables\n" - "'cd [dir]' - change directory (set $PWD)\n" - "'exit' - exit running foolshell\n\n"); - } - else if(!strcmp(command,"cd")) + if(!strcmp(command,"help"))help(); + else if(!strcmp(command,"cd"))cd(token[1]); + else if(!strcmp(command,"exit")) return false; + //else if(!strcmp(command,"getenv"))printf("(0x%08X) get: '%s' = '%s'(0x%08X) \n",environ,token[1],getenv(token[1]),getenv(token[1])); + else if(!strcmp(command,"getenv"))printf("%s\n",getenv(token[1])); + else if(!strcmp(command,"setenv")) { - char *dir=getenv("PWD"); - - char buf[256]; - - if(token[1][0]!='/') - { - sprintf(buf,"%s%s%s",dir,dir[0]=='/'&&dir[1]==0?"":"/",token[1]); - } - else - { - sprintf(buf,"%s",token[1]); - } - - char token[10]; - - //adjust pwd (resolve '..' and '.') -//// printf("adjusting pwd: '%s'\n",buf); - int left=1; - int right=0; - - do{ - - int t=0; - do - { - right++; - token[t]=buf[right]; - - t++; - } - while(buf[right]!=0&&buf[right]!='/'); - token[t-1]=0; - -// printf("path token: '%s'\n",token); - - if(!strcmp(token,"..")) - { - left--; - while(buf[left]!='/')left--; - } - else if(!strcmp(token,".")) - { - } - else - { - int i=0; - if(left!=1)buf[left++]='/'; - do{buf[left++]=token[i++];}while(token[i]!=0); - } - - }while(buf[right]!=0); - buf[left]=0; -// printf("adjusted: '%s'\n",buf); - if(buf[0]==0)setenv("PWD","/",1); - else setenv("PWD",buf,1); - } - else if(!strcmp(command,"exit")) - { - _exit(1); - } - else if(!strcmp(command,"echo")) - { - printf("\"%s\"\n",token[1]); - - } - else if(!strcmp(command,"getenv")) - { - printf("(0x%08X) get: '%s' = '%s'(0x%08X) \n",environ,token[1],getenv(token[1]),getenv(token[1])); - } - else if(!strcmp(command,"putenv")||!strcmp(command,"setenv")) - { - char buf[256]; sprintf(buf,"%s=%s",token[1],token[2]); putenv(buf); - printf("(0x%08X) set: '%s' = '%s' \n",environ,token[1],getenv(token[1])); + //printf("(0x%08X) set: '%s' = '%s' \n",environ,token[1],getenv(token[1])); } else if(!strcmp(command,"env")) { int i=0; - printf("env: 0x%08X\n",environ); +// printf("env: 0x%08X\n",environ); while(environ[i]!=NULL) { - printf("envvar %s (0x%08X)\n" ,environ[i],environ[i]); +// printf("envvar %s (0x%08X)\n" ,environ[i],environ[i]); + printf("%s\n" ,environ[i]); i++; } } - else + else // otherwise treat command as exectutable and send to execve { int pid=_fork(); if(pid==0) { - sprintf(buf,"%s",token[0]); - _execve(buf,token,environ); - sprintf(buf,"%s/%s",getenv("PATH"),token[0]); + if(token[0][0]=='/')sprintf(buf,"%s",token[0]); + else sprintf(buf,"%s/%s",getenv("PATH"),token[0]); _execve(buf,token,environ); - puts("foolshell: command not found"); - exit(0); + printf("foolshell: %s (errno: %d)\n",strerror(errno),errno); + return false; } - if(token[1]==NULL||strcmp(token[1],"branch"))_wait(pid); + if(token[1]==NULL||strcmp(token[1],"&"))_wait(pid); } - return 0; + return true; +} + +bool setpwd(char *path) +{ + if(!strcmp(path,"/")) + { + putenv("PWD=/"); + return true; + } + char buf[255]; + buf[0]=0; + strcat(buf,"PWD="); + + char **t=tokenize(path,'/'); + + char *p[100]; + int pp=0; + + while(*t!=NULL) + { + if(!strcmp(*t,"..")){ + pp--; + t++; + if(pp<0)pp=0; + continue; + } + if(!strcmp(*t,".")){t++; continue;} + if(**t!=0) + { + p[pp]=*t; + //printf("> %s\n",p[pp]); + pp++; + } + t++; + } + + if(pp==0) + { + strcat(buf,"/"); + } + + for(int i=0;i<pp;i++) + { + strcat(buf,"/"); + strcat(buf,p[i]); + } + + putenv(buf); + return true; // TODO check if dir exists at all. +} + +bool cd(char *path) +{ + char buf[256]; + if(path==NULL)return setpwd(getenv("HOME")); + if(path[0]=='/')return setpwd(path); + sprintf(buf,"%s/%s",getenv("PWD"),path); + return setpwd(buf); } diff --git a/userspace/init.c b/userspace/init.c index 3384edb..8530368 100644 --- a/userspace/init.c +++ b/userspace/init.c @@ -4,7 +4,7 @@ int main(int argc, char **argv) { char *argv1[]={"/bin/foolshell",0}; - char *env1[]={"PS1=\033[34m$\033[37m","PWD=/home/miguel","PATH=/bin","TERM=fool-term",0}; + char *env1[]={"HOME=/home/miguel","PS1=\033[34m$\033[37m","PWD=/home/miguel","PATH=/bin","TERM=fool-term",0}; time_t ltime; time(<ime); |
