DEADSOFTWARE

Можно задавать директории для поиска модулей
[dsw-obn.git] / src / main.c
index 994ae806747dc34788ee8c76e9b247e27dd8202b..754d90aa7f216863d6f6337c0c3d41bd3def2afc 100644 (file)
@@ -5,24 +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;
        }
 
@@ -43,13 +67,27 @@ static void
 init(int argc, char ** argv)
 {
        int i = 1;
+       add_to_stack(&path_list, ".");
        out_path = ".";
 
-       if(argc > 2 && strcmp(argv[i], "-d") == 0)
+       while(argc > 2)
        {
-               out_path = argv[i + 1];
-               argc -= 2;
-               i += 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] == '-')
@@ -69,6 +107,11 @@ main(int argc, char ** argv)
        init(argc, argv);
 
        code = import_module(module);
+       if(code == NULL)
+       {
+               printf("can't open module source %s\n", module);
+               return 0;
+       }
 
        ctx = oberon_create_context(import_module);
        oberon_set_out_directory(ctx, out_path);