diff --git a/src/main.c b/src/main.c
index 994ae806747dc34788ee8c76e9b247e27dd8202b..754d90aa7f216863d6f6337c0c3d41bd3def2afc 100644 (file)
--- a/src/main.c
+++ b/src/main.c
#include "../include/oberon.h"
#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 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)
{
static const char *
import_module(const char * name)
{
- assert(name);
-
FILE * fp;
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)
{
if(fp == NULL)
{
- printf("can't open file %s\n", fname);
- exit(1);
return NULL;
}
return NULL;
}
init(int argc, char ** argv)
{
int i = 1;
init(int argc, char ** argv)
{
int i = 1;
+ add_to_stack(&path_list, ".");
out_path = ".";
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] == '-')
}
if(argc != 2 || argv[i][0] == '-')
init(argc, argv);
code = import_module(module);
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);
ctx = oberon_create_context(import_module);
oberon_set_out_directory(ctx, out_path);