From 4513d5e1d3ae75ecb61051cdabbd12542f013880 Mon Sep 17 00:00:00 2001 From: id101010 Date: Tue, 15 Nov 2016 03:53:42 +0100 Subject: [PATCH] Added optparsing, updated readme --- README.md | 40 +++++++++++++++++++++++++++++ bfckr.c | 75 ++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 90 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 33c9de0..9ecff14 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,46 @@ A little brainfuck interpreter written in C. * Create your own brainfuck software * ./bfckr "$yourstuff" +# Usage +Usage: ./bfckr [OPTION] [FILE] +-h Show this help. +-d Enable debugger. +-f Execute brainfuck code given as file. +-e Execute brainfuck code given as argument. + +## examples: + +./bfckr -f examples/pi.bf +~~~~ +3.14070455282885 +~~~~ + +./bfckr -d -f examples/pi.bf +~~~~ +[ENTER]: single step [c]: continue +Source viewer: +------------------------------------------------------------ + >+++++++++++++++[<+>>>>>>>>+++ + ^ + ip=0 +------------------------------------------------------------ + +Memory viewer: +------------------------------------------------------------ +000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 + + ^ + mp=1 +000 000 000 000 000 000 001 002 003 004 005 006 007 008 009 +------------------------------------------------------------ +~~~~ + + + +# Breakpoints + +\# in the brainfuck source will be interpreted as breakpoints. You can use them if you want to stop the program flow at a specific point and start the debugger from there. If you are in debugger mode pressing c will get you back to executing the program. + # Licence ![WTFPL](http://www.wtfpl.net/wp-content/uploads/2012/12/logo-220x1601.png) diff --git a/bfckr.c b/bfckr.c index f4ea3ba..e2060d0 100644 --- a/bfckr.c +++ b/bfckr.c @@ -10,9 +10,9 @@ #include #include #include -#include #include #include +#include // Defines #define MEMORY_SIZE 10000 @@ -35,6 +35,9 @@ void print_sourceviewer(bf_code_t *bf); void print_memoryviewer(bf_code_t *bf); void init_bf_object(bf_code_t *bf); bool is_brainfuck(char c); +int getopt(int argc, char * const argv[], const char *optstring); +extern char *optarg; +extern int optind, opterr, optopt; /* initialize bf object */ void init_bf_object(bf_code_t *bf) @@ -54,7 +57,7 @@ void init_bf_object(bf_code_t *bf) bool is_brainfuck(char c) { - return (c=='+') || (c=='-') || (c=='>') || (c=='<') || (c=='.') || (c==',') || (c=='[') || (c==']'); + return (c=='+') || (c=='-') || (c=='>') || (c=='<') || (c=='.') || (c==',') || (c=='[') || (c==']' || (c=='#')); } /* Error handler */ @@ -82,8 +85,7 @@ void print_sourceviewer(bf_code_t *bf) putchar((i<0 || i>MAX_INPUT_SIZE) ? ' ' : bf->code[i]); } - printf("\n"); - printf(" ^ \n"); + printf("\n ^ \n"); printf(" ip=%d \n", ip); printf("------------------------------------------------------------\n"); } @@ -121,19 +123,19 @@ void bfuck_debugger(bf_code_t *bf) //char *bf_source_input, int instructionpoint { clear(); // clear terminal - printf("[s]: single step [c]: continue\n"); + printf("[ENTER]: single step [c]: continue\n"); print_sourceviewer(bf); print_memoryviewer(bf); switch(getchar()) { - case 's': - // single step - break; case 'c': // continue bf->debug = false; break; + default: + // single step + break; } } @@ -224,40 +226,63 @@ void bfuck_execute(bf_code_t *bf) break; } - bfuck_debugger(bf); + if(bf->debug) { + bfuck_debugger(bf); + } } } int main(int argc, char* argv[]) { + // vars FILE *fp; int i = 0; - int c; + char c, option; bf_code_t bf; + // initialize bf object + init_bf_object(&bf); + // check arguments if(argc < 2) { die("Need more arguments."); } - // initialize bf object - init_bf_object(&bf); - - // try to open file - if((fp = fopen(argv[1], "rt")) == NULL) { - die("Couldn't open file."); - } - - // read the file and store it in the input buffer - while((c = getc(fp)) != EOF) { - if(is_brainfuck(c)) { - bf.code[i++] = c; + // optparsing ahead + while((option = getopt(argc, argv, "hdef:")) >= 0) { + switch(option) { + case 'h': // show help + printf("Usage: %s [OPTION] [FILE]\n", argv[0]); + printf("-h\t Show this help.\n"); + printf("-d\t Enable debugger.\n"); + printf("-f\t Execute brainfuck code given as file.\n"); + printf("-e\t Execute brainfuck code given as string.\n"); + exit(EXIT_SUCCESS); + break; + case 'e': // read input string + //printf ("Input code: \"%s\"\n", argv[2]); + sscanf(bf.code, "%s", argv[2]); + break; + case 'f': // file input + // try to open file + if((fp = fopen(optarg, "r")) == NULL) { + die("Couldn't open file."); + } + // read the file and store it in the input buffer + while((c = getc(fp)) != EOF) { + if(is_brainfuck(c)) { + bf.code[i++] = c; + } + } + // close file after reading + fclose(fp); + break; + case 'd': // set to use debugger + bf.debug = true; + break; } } - // close file after reading - fclose(fp); - // try to interpret it bfuck_execute(&bf);