comparison 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
comparison
equal deleted inserted replaced
12:8e34daa80f64 13:bf660b45bba9
12 #include <string.h> 12 #include <string.h>
13 13
14 #include "baum.h" 14 #include "baum.h"
15 #include "actions.h" 15 #include "actions.h"
16 16
17 #define VERSION "0.1"
18
19 int option_check = 0;
20 int option_verbose = 0;
17 21
18 struct Node* root = 0; 22 struct Node* root = 0;
19 struct Stackitem* stack = NULL; 23 struct Stackitem* stack = NULL;
20 24
21 25
22 void logit(char* text) { 26 void logit(char* text) {
23 fprintf(stderr, "[%s]\n", text); 27 if (option_verbose) {
28 fprintf(stderr, "[%s]\n", text);
29 }
24 } 30 }
25 31
26 32
27 /* new */ 33 /* new */
28 struct Node* newNode(char* name, unsigned char value) { 34 struct Node* newNode(char* name, unsigned char value) {
162 } 168 }
163 } 169 }
164 170
165 if (c == '\n') { /* end of line: create node */ 171 if (c == '\n') { /* end of line: create node */
166 if (strlen(name) > 0) { 172 if (strlen(name) > 0) {
167 fprintf(stderr, " %d - %s - %d\n", indent, name, value); 173 if (option_verbose) {
174 fprintf(stderr, " %d - %s - %d\n", indent, name, value);
175 }
168 /* create node */ 176 /* create node */
169 node = newNode((char*) name, value); 177 node = newNode((char*) name, value);
170 if (indent > last_indent) { /* down */ 178 if (indent > last_indent) { /* down */
171 last_node->down = node; 179 last_node->down = node;
172 push(last_node); 180 push(last_node);
210 218
211 /* main */ 219 /* main */
212 int main(int argc, char* argv[]) { 220 int main(int argc, char* argv[]) {
213 unsigned char shell_return = 0; 221 unsigned char shell_return = 0;
214 222
215 read_input("./input_sum3input"); 223 while (--argc > 0 && (*++argv)[0] == '-') {
216 printTree(root); 224 if (strcmp(argv[0], "--version") == 0) {
225 printf("baum %s\n\
226 an esoteric programming language\n\
227 by markus schnalke and julian forster\n\
228 http://prog.marmaro.de/baum\n", VERSION);
229 exit(0);
230 } else if (strcmp(argv[0], "--help") == 0) {
231 printf("\
232 baum --version print version information and exit\n\
233 baum --help print this output\n\
234 baum [-v] -c <file> (verbosly) check file and return 1 if invalid\n\
235 baum [-v] <file> (verbosly) run file\n\
236 ");
237 exit(0);
238 } else if (strcmp(argv[0], "-c") == 0) {
239 option_check = 1;
240 } else if (strcmp(argv[0], "-v") == 0) {
241 option_verbose = 1;
242 /*
243 } else if (strcmp(argv[0], "-W") == 0) {
244 / * TODO: catch if no value given * /
245 iDWarn = atoi((++argv)[0]);
246 argc--;
247 */
248 } else {
249 fprintf(stderr, "unknown option: %s\n", argv[0]);
250 exit(127);
251 }
252 }
253
254 if (argc != 1) {
255 fprintf(stderr, "%d source files given, please specify one.\n", argc);
256 exit(3);
257 }
258
259 read_input(argv[0]);
260
261 if (option_verbose) {
262 printTree(root);
263 }
217 264
218 shell_return = action(root); 265 shell_return = action(root);
219 266
220 printTree(root); 267 if (option_verbose) {
268 printTree(root);
269 }
270
221 delete(root); 271 delete(root);
222
223 exit(shell_return); 272 exit(shell_return);
224 } 273 }