DEADSOFTWARE

Можно задавать директории для поиска модулей
[dsw-obn.git] / src / main.c
index a97f2d7d71606da039bf0ded4764654dd8b64005..754d90aa7f216863d6f6337c0c3d41bd3def2afc 100644 (file)
@@ -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,22 +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 <module>\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_destroy_context(ctx);
+
        return 0;
 }