Permalink
Browse files

created beginning of a user space shell

  • Loading branch information...
1 parent bb241a7 commit d352764c7b07e69d67a267f0fe74055f3c7d6261 @jezze committed Jul 13, 2011
Showing with 181 additions and 3 deletions.
  1. +175 −0 bin/shell.c
  2. +5 −2 build/Makefile
  3. +1 −1 kernel/shell.c
View
@@ -0,0 +1,175 @@
+#include <call.h>
+#include <file.h>
+#include <memory.h>
+#include <string.h>
+
+#define SHELL_BUFFER_SIZE 256
+
+static char shellBuffer[SHELL_BUFFER_SIZE];
+static unsigned int shellBufferHead;
+
+static void shell_stack_push(char c)
+{
+
+ if (shellBufferHead < SHELL_BUFFER_SIZE)
+ {
+
+ shellBuffer[shellBufferHead] = c;
+ shellBufferHead++;
+
+ }
+
+}
+
+static char shell_stack_pop()
+{
+
+ return (shellBufferHead > 0) ? shellBuffer[--shellBufferHead] : 0;
+
+}
+
+static void shell_stack_clear()
+{
+
+ shellBufferHead = 0;
+
+}
+
+static void shell_clear()
+{
+
+ file_write_string(FILE_STDOUT, "fudge:/$ ");
+ shell_stack_clear();
+
+}
+
+static void shell_call(int file, int argc, char *argv[])
+{
+
+ void *buffer = (void *)0x00500000;
+ file_read(file, 0x100000, buffer);
+
+ unsigned int address = call_map((unsigned int)buffer);
+
+ void (*func)(int argc, char **argv) = (void (*)(int argc, char **argv))address;
+
+ func(argc, argv);
+
+}
+
+static void shell_interpret(char *command)
+{
+
+ char *argv[32];
+ unsigned int argc = string_split(argv, command, ' ');
+
+ if (!argc)
+ return;
+
+ char path[256];
+
+ string_copy(path, "/initrd/");
+ string_concat(path, argv[0]);
+
+ int file = file_open(path);
+
+ if (file == -1)
+ {
+
+ file_write_string(FILE_STDOUT, argv[0]);
+ file_write_string(FILE_STDOUT, ": Command not found\n");
+
+ return;
+
+ }
+
+ shell_call(file, argc, argv);
+ file_close(file);
+
+}
+
+static void shell_handle_input(char c)
+{
+
+ switch (c)
+ {
+
+ case '\t':
+
+ break;
+
+ case '\b':
+
+ if (shell_stack_pop())
+ {
+
+ file_write_byte(FILE_STDOUT, '\b');
+ file_write_byte(FILE_STDOUT, ' ');
+ file_write_byte(FILE_STDOUT, '\b');
+
+ }
+
+ break;
+
+ case '\n':
+
+ shell_stack_push('\0');
+ file_write_byte(FILE_STDOUT, c);
+ shell_interpret(shellBuffer);
+ shell_clear();
+
+ break;
+
+ default:
+
+ shell_stack_push(c);
+ file_write_byte(FILE_STDOUT, c);
+
+ break;
+
+ }
+
+}
+
+static void shell_poll()
+{
+
+ char c;
+
+ for (;;)
+ {
+
+ while (!file_read(FILE_STDIN, 1, &c));
+
+ shell_handle_input(c);
+
+ }
+
+}
+
+void main()
+{
+
+ shellBufferHead = 0;
+
+ int sin = file_open("/dev/kbd");
+ int sout = file_open("/dev/tty");
+ int serr = file_open("/dev/serial");
+
+ file_write_string(FILE_STDOUT, "Fudge\n\n");
+ file_write_string(FILE_STDOUT, "Copyright (c) 2009 Jens Nyberg\n");
+ file_write_string(FILE_STDOUT, "Type 'cat help.txt' to read the help section.\n\n");
+
+ file_write_string(FILE_STDERR, "Fudge\n\n");
+ file_write_string(FILE_STDERR, "Copyright (c) 2009 Jens Nyberg\n");
+ file_write_string(FILE_STDERR, "Debug window.\n\n");
+
+ shell_clear();
+ shell_poll();
+
+ file_close(sin);
+ file_close(sout);
+ file_close(serr);
+
+}
+
View
@@ -134,6 +134,7 @@ ramdisk:
@${GCC} ${GCCFLAGS_RAMDISK} ${DIR_SOURCE_BIN}/hello.c -o ${DIR_SOURCE_BIN}/hello.o
@${GCC} ${GCCFLAGS_RAMDISK} ${DIR_SOURCE_BIN}/ls.c -o ${DIR_SOURCE_BIN}/ls.o
@${GCC} ${GCCFLAGS_RAMDISK} ${DIR_SOURCE_BIN}/reboot.c -o ${DIR_SOURCE_BIN}/reboot.o
+ @${GCC} ${GCCFLAGS_RAMDISK} ${DIR_SOURCE_BIN}/shell.c -o ${DIR_SOURCE_BIN}/shell.o
@${GCC} ${GCCFLAGS_RAMDISK} ${DIR_SOURCE_BIN}/tar.c -o ${DIR_SOURCE_BIN}/tar.o
@${GCC} ${GCCFLAGS_RAMDISK} ${DIR_SOURCE_BIN}/timer.c -o ${DIR_SOURCE_BIN}/timer.o
@${GCC} ${GCCFLAGS_RAMDISK} ${DIR_SOURCE_BIN}/vga.c -o ${DIR_SOURCE_BIN}/vga.o
@@ -147,6 +148,7 @@ ramdisk:
@${LD} ${LDFLAGS_RAMDISK} ${DIR_SOURCE_BIN}/hello.o ${DIR_SOURCE_LIB}/memory.o ${DIR_SOURCE_LIB}/string.o ${DIR_SOURCE_LIB}/file.o ${DIR_SOURCE_ARCH_LIB}/calls.o -o ${DIR_IMAGE_BIN}/hello
@${LD} ${LDFLAGS_RAMDISK} ${DIR_SOURCE_BIN}/ls.o ${DIR_SOURCE_LIB}/memory.o ${DIR_SOURCE_LIB}/string.o ${DIR_SOURCE_LIB}/file.o ${DIR_SOURCE_ARCH_LIB}/calls.o -o ${DIR_IMAGE_BIN}/ls
@${LD} ${LDFLAGS_RAMDISK} ${DIR_SOURCE_BIN}/reboot.o ${DIR_SOURCE_LIB}/memory.o ${DIR_SOURCE_LIB}/string.o ${DIR_SOURCE_LIB}/file.o ${DIR_SOURCE_ARCH_LIB}/calls.o -o ${DIR_IMAGE_BIN}/reboot
+ @${LD} ${LDFLAGS_RAMDISK} ${DIR_SOURCE_BIN}/shell.o ${DIR_SOURCE_LIB}/memory.o ${DIR_SOURCE_LIB}/string.o ${DIR_SOURCE_LIB}/file.o ${DIR_SOURCE_ARCH_LIB}/calls.o -o ${DIR_IMAGE_BIN}/shell
@${LD} ${LDFLAGS_RAMDISK} ${DIR_SOURCE_BIN}/tar.o ${DIR_SOURCE_LIB}/memory.o ${DIR_SOURCE_LIB}/string.o ${DIR_SOURCE_LIB}/file.o ${DIR_SOURCE_ARCH_LIB}/calls.o -o ${DIR_IMAGE_BIN}/tar
@${LD} ${LDFLAGS_RAMDISK} ${DIR_SOURCE_BIN}/timer.o ${DIR_SOURCE_LIB}/memory.o ${DIR_SOURCE_LIB}/string.o ${DIR_SOURCE_LIB}/file.o ${DIR_SOURCE_ARCH_LIB}/calls.o -o ${DIR_IMAGE_BIN}/timer
@${LD} ${LDFLAGS_RAMDISK} ${DIR_SOURCE_BIN}/vga.o ${DIR_SOURCE_LIB}/memory.o ${DIR_SOURCE_LIB}/string.o ${DIR_SOURCE_LIB}/file.o ${DIR_SOURCE_ARCH_LIB}/calls.o -o ${DIR_IMAGE_BIN}/vga
@@ -179,14 +181,15 @@ clean:
@rm -f root/bin/hello
@rm -f root/bin/ls
@rm -f root/bin/reboot
+ @rm -f root/bin/shell
@rm -f root/bin/tar
@rm -f root/bin/timer
@rm -f root/bin/vga
@rm -f root/boot/kernel
@rm -f root/boot/initrd
@rm -f root/boot/initrd.tar
- @rm -f ../arch/a rm/kernel/*.o
- @rm -f ../arch/a rm/lib/*.o
+ @rm -f ../arch/arm/kernel/*.o
+ @rm -f ../arch/arm/lib/*.o
@rm -f ../arch/x86/kernel/*.o
@rm -f ../arch/x86/lib/*.o
@rm -f ../arch/x86/modules/ata/*.o
View
@@ -45,7 +45,7 @@ static void shell_clear()
static void shell_call(int file, int argc, char *argv[])
{
- void *buffer = (void *)0x00420000;
+ void *buffer = (void *)0x00400000;
file_read(file, 0x100000, buffer);
unsigned int address = call_map((unsigned int)buffer);

0 comments on commit d352764

Please sign in to comment.