baum

diff baum.c @ 13:bf660b45bba9

added commandline options (-v, -c, file); logging only if -v is set
author meillo@marmaro.de
date Wed, 13 Feb 2008 09:13:02 +0100
parents 8e34daa80f64
children e2048e569891
line diff
     1.1 --- a/baum.c	Tue Feb 12 15:53:08 2008 +0100
     1.2 +++ b/baum.c	Wed Feb 13 09:13:02 2008 +0100
     1.3 @@ -14,13 +14,19 @@
     1.4  #include "baum.h"
     1.5  #include "actions.h"
     1.6  
     1.7 +#define VERSION "0.1"
     1.8 +
     1.9 +int option_check = 0;
    1.10 +int option_verbose = 0;
    1.11  
    1.12  struct Node* root = 0;
    1.13  struct Stackitem* stack = NULL;
    1.14  
    1.15  
    1.16  void logit(char* text) {
    1.17 -	fprintf(stderr, "[%s]\n", text);
    1.18 +	if (option_verbose) {
    1.19 +		fprintf(stderr, "[%s]\n", text);
    1.20 +	}
    1.21  }
    1.22  
    1.23  
    1.24 @@ -164,7 +170,9 @@
    1.25  
    1.26  		if (c == '\n') {  /* end of line: create node */
    1.27  			if (strlen(name) > 0) {
    1.28 -				fprintf(stderr, " %d - %s - %d\n", indent, name, value);
    1.29 +				if (option_verbose) {
    1.30 +					fprintf(stderr, " %d - %s - %d\n", indent, name, value);
    1.31 +				}
    1.32  				/* create node */
    1.33  				node = newNode((char*) name, value);
    1.34  				if (indent > last_indent) { /* down */
    1.35 @@ -212,13 +220,54 @@
    1.36  int main(int argc, char* argv[]) {
    1.37  	unsigned char shell_return = 0;
    1.38  	
    1.39 -	read_input("./input_sum3input");
    1.40 -	printTree(root);
    1.41 +	while (--argc > 0 && (*++argv)[0] == '-') {
    1.42 +		if (strcmp(argv[0], "--version") == 0) {
    1.43 +			printf("baum %s\n\
    1.44 +an esoteric programming language\n\
    1.45 +by markus schnalke and julian forster\n\
    1.46 +http://prog.marmaro.de/baum\n", VERSION);
    1.47 +			exit(0);
    1.48 +		} else if (strcmp(argv[0], "--help") == 0) {
    1.49 +			printf("\
    1.50 +baum --version        print version information and exit\n\
    1.51 +baum --help           print this output\n\
    1.52 +baum [-v] -c <file>   (verbosly) check file and return 1 if invalid\n\
    1.53 +baum [-v] <file>      (verbosly) run file\n\
    1.54 +					");
    1.55 +			exit(0);
    1.56 +		} else if (strcmp(argv[0], "-c") == 0) {
    1.57 +			option_check = 1;
    1.58 +		} else if (strcmp(argv[0], "-v") == 0) {
    1.59 +			option_verbose = 1;
    1.60 +			/*
    1.61 +		} else if (strcmp(argv[0], "-W") == 0) {
    1.62 +			/ * TODO: catch if no value given * /
    1.63 +			iDWarn = atoi((++argv)[0]);
    1.64 +			argc--;
    1.65 +			*/
    1.66 +		} else {
    1.67 +			fprintf(stderr, "unknown option: %s\n", argv[0]);
    1.68 +			exit(127);
    1.69 +		}
    1.70 +	}
    1.71 +
    1.72 +	if (argc != 1) {
    1.73 +		fprintf(stderr, "%d source files given, please specify one.\n", argc);
    1.74 +		exit(3);
    1.75 +	}
    1.76 +
    1.77 +	read_input(argv[0]);
    1.78 +
    1.79 +	if (option_verbose) {
    1.80 +		printTree(root);
    1.81 +	}
    1.82  
    1.83  	shell_return = action(root);
    1.84  
    1.85 -	printTree(root);
    1.86 +	if (option_verbose) {
    1.87 +		printTree(root);
    1.88 +	}
    1.89 +
    1.90  	delete(root);
    1.91 -
    1.92  	exit(shell_return);
    1.93  }