From 0b010d22dbf845ad030e2e7320f9c5935b2604a4 Mon Sep 17 00:00:00 2001 From: Miguel Date: Sat, 15 Sep 2018 12:25:13 +0200 Subject: brainfuck interpreter works! --- fs/fd.c | 2 +- kernel/syscalls.c | 22 +++++++++++-- userspace/brain.brain | 1 + userspace/brainfuck.c | 89 +++++++++++++++++++-------------------------------- userspace/foolshell.c | 20 ++++++------ userspace/piper.c | 31 +++++++++++++++--- 6 files changed, 91 insertions(+), 74 deletions(-) create mode 100644 userspace/brain.brain diff --git a/fs/fd.c b/fs/fd.c index 0ec134b..ed8a3b1 100644 --- a/fs/fd.c +++ b/fs/fd.c @@ -42,7 +42,7 @@ void* ext2_init(char *path) { uint32_t *data=kballoc(1); uint32_t inode= ext2_filename_to_inode(VMEM_EXT2_RAMIMAGE,path); - data[0]=inode;// pos + data[0]=inode;// inode data[1]=0; //pos return data; diff --git a/kernel/syscalls.c b/kernel/syscalls.c index 57ebc54..5205bf1 100644 --- a/kernel/syscalls.c +++ b/kernel/syscalls.c @@ -109,7 +109,25 @@ int syscall_gettimeofday(struct timeval *tv, struct timezone *tz) int syscall_lseek(int file,int ptr,int dir) { - kpanic("unhandled syscall: lseek"); + +#ifndef SEEK_SET +#define SEEK_SET 0 /* set file offset to offset */ +#endif +#ifndef SEEK_CUR +#define SEEK_CUR 1 /* set file offset to current plus offset */ +#endif +#ifndef SEEK_END +#define SEEK_END 2 /* set file offset to EOF plus offset */ +#endif + + if(dir==SEEK_CUR) + { + uint32_t *dat=fds[file].data; + dat[1]+=ptr; + return dat[1]; + } + + kpanic("not fully handled lseek! dir=%d",dir); return 0; } @@ -312,8 +330,8 @@ int syscall_open(char *name, int flags, int mode) } else { - fds[next_fd]=fd_from_path(name); + if(*(uint32_t *)fds[next_fd].data==0)return -1; } next_fd++; diff --git a/userspace/brain.brain b/userspace/brain.brain new file mode 100644 index 0000000..cf7d005 --- /dev/null +++ b/userspace/brain.brain @@ -0,0 +1 @@ +++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.X diff --git a/userspace/brainfuck.c b/userspace/brainfuck.c index a1d5d53..eda0556 100644 --- a/userspace/brainfuck.c +++ b/userspace/brainfuck.c @@ -1,12 +1,4 @@ -// taken from: http://it-ride.blogspot.com/2009/11/brainfuck-interpreter.html - -/* comment - * TODO - * You have a bug. You don't check for EOF while looking for square brackets. - */ - /* - A brainfuck intepreter written in C, complete with error checking so you don't hurt yourself while, uh, brainfucking. Nothing really special about the implementation and it is probably very poor performance-wise. @@ -14,77 +6,64 @@ Author: Felix Oghină License: (brain)fuck licenses! + */ +// taken from: http://it-ride.blogspot.com/2009/11/brainfuck-interpreter.html +// and adapted for FoolOs by Miguel +// #include #include #include // by brainfuck standards (doesn't that sound funny?), the data pointer has // 3,000 bytes at its disposal, but I hate hard-coding such stuff. + // FOOLOS: decreased to 3000 (from 30.000) #define DATA_SIZE 3000 -void usage() { - puts( - "Usage: brainfuck FILE\n" - "If FILE is ommited or is '-', standard input is read" - ); +void usage(char *progname) +{ + printf("Usage: %s [FILE]\n",progname); } int main(int argc, char **argv) { + puts("\n\nbrainfuck: Welcome to the BRAINFUCK INTERPRETER by Felix Oghina"); + puts("brainfuck: Licensed under the (brain)fuck licenses!"); + puts("brainfuck: Adapted & Compiled for FoolOS by Miguel"); // used by the bf program unsigned char *dataptr = malloc(sizeof(char) * DATA_SIZE); // position of the data pointer unsigned int datapos = 0; + // input file FILE *input; + // level - deepness of brackets // i - uh, you need explanation for this one? unsigned int level, i; + // we will read chars from the input into r unsigned char r; - - /* uncommented by FOOLOS - // determine input - if (argc == 2) { - if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) { - usage(); - return EXIT_SUCCESS; - } - else { - input = fopen(argv[1], "r"); - if (input == NULL) { - puts("Error opening input file"); - return EXIT_FAILURE; - } - } - } - else { - usage(); + // check argument count + if(argc!=2) + { + usage(argv[0]); return EXIT_FAILURE; } - */ - - // added by FOOLOS - input=fopen("input.txt","r"); - char *buf=malloc(256); - puts("\n\nbrainfuck: Welcome to the BRAINFUCK INTERPRETER by Felix Oghina"); - puts("brainfuck: Licensed under the (brain)fuck licenses!"); - puts("brainfuck: Adapted & Compiled for FoolOS"); -// printf("brainfuck: dataptr: 0x%08X\n",dataptr); -// printf("brainfuck: dataptr2: 0x%08X\n",buf); - rewind(input); - fgets(buf,255,input); -// printf("\nbrainfuck: loaded programm: %s\n\n",buf); - fseek(input, 0, SEEK_SET); - + // open brainfuck source + input = fopen(argv[1], "r"); + if (input == NULL) + { + puts("Error opening input file"); + return EXIT_FAILURE; + } // zero the data pointer for (i=0; i < DATA_SIZE; i++) { @@ -92,17 +71,18 @@ int main(int argc, char **argv) } // start interpreting + while (1) { - while (!feof(input)) { r = (unsigned char) fgetc(input); -// printf("%c",r); + switch(r) { + case 'X': + puts("END!"); + return 0; case '>': if (datapos < DATA_SIZE - 1) datapos++; else { puts("brainfuck error: pointer overflow"); - // replace with foolshell. - _execve(15,0,0); return EXIT_FAILURE; } break; @@ -110,8 +90,6 @@ int main(int argc, char **argv) if (datapos > 0) datapos--; else { puts("brainfuck error: pointer underflow"); - // replace with foolshell. - _execve(15,0,0); return EXIT_FAILURE; } break; @@ -141,7 +119,8 @@ int main(int argc, char **argv) if (dataptr[datapos] != 0) { level = 1; while (level != 0) { - fseek(input, -2, SEEK_CUR); + //fseek(input, -2, SEEK_CUR); + _lseek(3,-2,SEEK_CUR); r = (unsigned char) fgetc(input); if (r == ']') level ++; else if (r == '[') level --; @@ -150,8 +129,6 @@ int main(int argc, char **argv) break; } } - // replace with foolshell. - _execve(15,0,0); - return EXIT_SUCCESS; + return EXIT_SUCCESS; } diff --git a/userspace/foolshell.c b/userspace/foolshell.c index 6877adb..0012592 100644 --- a/userspace/foolshell.c +++ b/userspace/foolshell.c @@ -17,17 +17,17 @@ void hello() puts( "\033c" - "\033[36m" +// "\033[36m" - " ______ __ ____ _____ \n" - " / ____/___ ____ / / / __ \\/ ___/ \n" - " / /_ / __ \\/ __ \\/ / / / / /\\__ \\ \n" - " / __/ / /_/ / /_/ / / / /_/ /___/ / \n" - " /_/ \\____/\\____/_/ \\____//____/ \n" - " \n" - - "\033[37;44m" - " \n" +// " ______ __ ____ _____ \n" +// " / ____/___ ____ / / / __ \\/ ___/ \n" +// " / /_ / __ \\/ __ \\/ / / / / /\\__ \\ \n" +// " / __/ / /_/ / /_/ / / / /_/ /___/ / \n" +// " /_/ \\____/\\____/_/ \\____//____/ \n" +// " \n" + +// "\033[37;44m" +// " \n" " Welcome to FoolShell v0.12 (Compiled on " __DATE__ " at " __TIME__")\n" " ------------------------------------------------------------------\n\n" diff --git a/userspace/piper.c b/userspace/piper.c index 2272f96..73b9516 100644 --- a/userspace/piper.c +++ b/userspace/piper.c @@ -1,21 +1,42 @@ #include int main() { - int pid=_fork(); + setvbuf(stdout,NULL,_IONBF,0); + /* FILE *f=fopen("~testpipe","rw"); - setvbuf(f,NULL,_IONBF,0); + int pid=_fork(); if(pid==0) { char buf[2]; - fread(f,buf,1,1); + fread(buf,1,1,f); printf("[%c]\n",buf[0]); + while(1); + } + + else + { + char buf[]="666"; + fwrite(buf,1,1,f); + printf("written\n"); + while(1); } + */ + int f=_open("~testpipe","RW"); + int pid=_fork(); + if(pid==0) + { + char buf[2]; + while(_read(f,buf,1))printf("%c",buf[0]); + } + else { - char buf="666"; - fwrite(f,buf,1,1); + char buf[]="666 the number of the beast"; + _write(f,buf,27); printf("written\n"); } + + } -- cgit v1.2.3