X-Git-Url: https://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Fmain.c;h=754d90aa7f216863d6f6337c0c3d41bd3def2afc;hb=98752831dc061590acffee7e68a075f8c18cb132;hp=8029be718d08bc4823e6723657a66441b68fe651;hpb=879793eaf1d6378593f78a192f2961670f686530;p=dsw-obn.git diff --git a/src/main.c b/src/main.c index 8029be7..754d90a 100644 --- a/src/main.c +++ b/src/main.c @@ -5,22 +5,48 @@ #include "../include/oberon.h" +struct string_stack +{ + char * string; + struct string_stack * next; +}; + static oberon_context_t * ctx; static oberon_module_t * mod; +static const char * module; +static const char * out_path; +static struct string_stack * path_list; + +static void +add_to_stack(struct string_stack ** stack, char * string) +{ + struct string_stack * p = malloc(sizeof *p); + p -> string = string; + p -> next = *stack; + *stack = p; +} + +static FILE * +open_file(struct string_stack * path, const char * ext, const char * name, const char * mode) +{ + FILE * fp = NULL; + while(fp == NULL && path != NULL) + { + char fname[256]; + snprintf(fname, 256, "%s/%s.%s", path -> string, name, ext); + fp = fopen(fname, mode); + path = path -> next; + } + return fp; +} static const char * import_module(const char * name) { - assert(name); - FILE * fp; - char fname[256]; - snprintf(fname, 256, "%s.obn", name); - fp = fopen(fname, "r"); + fp = open_file(path_list, "obn", name, "r"); if(fp == NULL) { - printf("can't open file %s\n", fname); - exit(1); return NULL; } @@ -37,23 +63,62 @@ import_module(const char * name) return source; } +static void +init(int argc, char ** argv) +{ + int i = 1; + add_to_stack(&path_list, "."); + out_path = "."; + + while(argc > 2) + { + if(argc > 2 && strcmp(argv[i], "-d") == 0) + { + out_path = argv[i + 1]; + argc -= 2; + i += 2; + } + else if(argc > 2 && strcmp(argv[i], "-I") == 0) + { + add_to_stack(&path_list, argv[i + 1]); + argc -= 2; + i += 2; + } + else + { + break; + } + } + + if(argc != 2 || argv[i][0] == '-') + { + printf("usage: obn [-d out_dir] module\n"); + exit(1); + } + + module = argv[i]; +} + int main(int argc, char ** argv) { const char * code; - if(argc != 2) + init(argc, argv); + + code = import_module(module); + if(code == NULL) { - printf("use: %s \n", argv[0]); - return 1; + printf("can't open module source %s\n", module); + return 0; } - code = import_module(argv[1]); - ctx = oberon_create_context(import_module); + oberon_set_out_directory(ctx, out_path); + mod = oberon_compile_module(ctx, code); - oberon_generate_code(ctx); oberon_destroy_context(ctx); + return 0; }