summaryrefslogtreecommitdiff
path: root/userspace/shell.c
diff options
context:
space:
mode:
Diffstat (limited to 'userspace/shell.c')
-rw-r--r--userspace/shell.c147
1 files changed, 147 insertions, 0 deletions
diff --git a/userspace/shell.c b/userspace/shell.c
new file mode 100644
index 0000000..36912c8
--- /dev/null
+++ b/userspace/shell.c
@@ -0,0 +1,147 @@
+#include <stdio.h>
+#include <assert.h>
+#include <string.h>
+#include "syscalls.c"
+#include "../fs/fs.h"
+
+void hello() {
+ puts(
+ "Welcome to FoolShell v0.1"
+ );
+}
+
+void prompt() {
+ printf(
+ "$ "
+ );
+}
+
+int main(int argc, char **argv)
+{
+ hello();
+
+ FILE *input;
+ input=fopen("input.txt","r");
+ char *buf=malloc(256);
+
+ while(1)
+ {
+ prompt();
+ fgets(buf,255,input);
+ buf[strlen(buf)-1]=0; // remove \n
+ process(buf);
+ }
+
+ return 0;
+}
+
+char **tokenize(char *buf)
+{
+
+ char **token;
+ token=malloc(10*sizeof(char*));
+
+ int l=strlen(buf);
+
+ int i;
+ int c=0;
+
+ for(i=0;i<l;i++)
+ {
+ // init space for next token
+ token[c]=malloc(256);
+
+ //skip all the whitespace
+ while(buf[i]==' '&&i<l)i++;
+ if(i==l)break;
+
+ //get token
+ int t=0;
+
+ while(buf[i]!=' '&&i<l)
+ {
+ token[c][t]=buf[i];
+ t++;
+ i++;
+ }
+ token[c][t]=0;
+
+
+// printf("token %i : <%s>\n",c, token[c]);
+ c++;
+
+ }
+
+ return token;
+
+}
+
+int process(char *buf)
+{
+
+ char **token=tokenize(buf);
+ char *command=token[0];
+ // puts(command);
+ // copied from trottelshell
+
+ if(!strcmp(command,"help"))
+ {
+ puts("foolshell: supported built-in commands: 'help', 'echo [string]', 'ls [inode_nr]', exec [inode_nr],'malloc [bytes]', 'free [address]', 'getenv [var]', 'putenv [var] [val]'");
+ }
+ else if(!strcmp(command,"ls"))
+ {
+ fs_dirent *dirs=malloc(sizeof(fs_dirent)*25);
+
+ int ls=readdir(atoi(token[1]),dirs,25);
+
+ int i;
+ for(i=0;i<ls;i++)
+ {
+ printf("foolshell: %i %s%c\n",dirs[i].inode, dirs[i].name, ((dirs[i].type==FS_FILE_TYPE_DIR)?'/':' '));
+ }
+
+ }
+ else if(!strcmp(command,"exec"))
+ {
+ execve(atoi(token[1]),0,0);
+
+ }
+ else if(!strcmp(command,"echo"))
+ {
+ printf("foolshell: \"%s\"\n",token[1]);
+
+ }
+ else if(!strcmp(command,"malloc"))
+ {
+ uint8_t *mall=malloc(atoi(token[1]));
+ printf("foolshell: allocated %d bytes at 0x%08X (%i).\n",atoi(token[1]),mall,mall);
+ }
+ else if(!strcmp(command,"free"))
+ {
+ free(atoi(token[1]));
+ printf("foolshell: called free(0x%08X).\n",atoi(token[1]));
+ }
+ else if(!strcmp(command,"getenv"))
+ {
+ printf("foolshell: %s = %s \n",token[1],getenv(token[1]));
+ }
+ else if(!strcmp(command,"putenv"))
+ {
+ char buf[256];
+ sprintf(buf,"%s=%s",token[1],token[2]);
+ putenv(buf);
+ printf("foolshell: %s = %s \n",token[1],getenv(token[1]));
+ }
+ else
+ {
+ puts("foolshell: command not found");
+ }
+
+
+ //
+}
+
+
+
+
+