DEADSOFTWARE

Можно задавать директории для поиска модулей
[dsw-obn.git] / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
6 #include "../include/oberon.h"
8 struct string_stack
9 {
10 char * string;
11 struct string_stack * next;
12 };
14 static oberon_context_t * ctx;
15 static oberon_module_t * mod;
16 static const char * module;
17 static const char * out_path;
18 static struct string_stack * path_list;
20 static void
21 add_to_stack(struct string_stack ** stack, char * string)
22 {
23 struct string_stack * p = malloc(sizeof *p);
24 p -> string = string;
25 p -> next = *stack;
26 *stack = p;
27 }
29 static FILE *
30 open_file(struct string_stack * path, const char * ext, const char * name, const char * mode)
31 {
32 FILE * fp = NULL;
33 while(fp == NULL && path != NULL)
34 {
35 char fname[256];
36 snprintf(fname, 256, "%s/%s.%s", path -> string, name, ext);
37 fp = fopen(fname, mode);
38 path = path -> next;
39 }
40 return fp;
41 }
43 static const char *
44 import_module(const char * name)
45 {
46 FILE * fp;
47 fp = open_file(path_list, "obn", name, "r");
48 if(fp == NULL)
49 {
50 return NULL;
51 }
53 char * source;
54 size_t len;
56 fseek(fp, 0, SEEK_END);
57 len = ftell(fp);
58 fseek(fp, 0, SEEK_SET);
60 source = calloc(1, len + 1);
61 fread(source, len, 1, fp);
63 return source;
64 }
66 static void
67 init(int argc, char ** argv)
68 {
69 int i = 1;
70 add_to_stack(&path_list, ".");
71 out_path = ".";
73 while(argc > 2)
74 {
75 if(argc > 2 && strcmp(argv[i], "-d") == 0)
76 {
77 out_path = argv[i + 1];
78 argc -= 2;
79 i += 2;
80 }
81 else if(argc > 2 && strcmp(argv[i], "-I") == 0)
82 {
83 add_to_stack(&path_list, argv[i + 1]);
84 argc -= 2;
85 i += 2;
86 }
87 else
88 {
89 break;
90 }
91 }
93 if(argc != 2 || argv[i][0] == '-')
94 {
95 printf("usage: obn [-d out_dir] module\n");
96 exit(1);
97 }
99 module = argv[i];
102 int
103 main(int argc, char ** argv)
105 const char * code;
107 init(argc, argv);
109 code = import_module(module);
110 if(code == NULL)
112 printf("can't open module source %s\n", module);
113 return 0;
116 ctx = oberon_create_context(import_module);
117 oberon_set_out_directory(ctx, out_path);
119 mod = oberon_compile_module(ctx, code);
121 oberon_destroy_context(ctx);
123 return 0;