main
module.c
1#define _XOPEN_SOURCE 700
2
3#include <stdlib.h>
4#include <string.h>
5#include <errno.h>
6
7#include "util.h"
8#include "kav.h"
9
10static struct module *seen;
11static struct module *head;
12static struct module **end = &head;
13
14static char *
15kavpath(struct token name) {
16 const char *dir = getenv("KAVPATH");
17 const char *colon;
18 char *canonical;
19 stralloc sa = {0};
20 usize len;
21
22 if(!dir)
23 dir = "/usr/lib/kav";
24 if(name.str[1] == '/')
25 errorat(&name.loc, "system module paths must be relative");
26 for(;;) {
27 colon = strchr(dir, ':');
28 len = colon ? (usize)(colon - dir) : strlen(dir);
29 if(len) {
30 sa.len = 0;
31 sacatb(&sa, dir, len);
32 sacatl(&sa, "/");
33 sacatb(&sa, name.str + 1, name.length - 2);
34 sa0(&sa);
35 canonical = realpath(sa.s, nil);
36 if(canonical) {
37 free(canonical);
38 return sa.s;
39 }
40 if(errno != ENOENT && errno != ENOTDIR)
41 errorat(&name.loc, "unable to resolve ", sa.s, ": ", strerror(errno));
42 }
43 if(!colon)
44 break;
45 dir = colon + 1;
46 }
47 free(sa.s);
48 errorat(&name.loc, "module ", tokdup(name), " not found");
49}
50
51static char *
52usepath(const char *from, struct token name) {
53 stralloc sa = {0};
54 const char *slash;
55
56 if(name.kind == TUSEPATH)
57 return kavpath(name);
58 if(name.str[1] != '/' && (slash = strrchr(from, '/')))
59 sacatb(&sa, from, slash - from + 1);
60 sacatb(&sa, name.str + 1, name.length - 2);
61 sa0(&sa);
62 return sa.s;
63}
64
65/* a module's logical name, we need it later for qbe */
66static char *
67usename(struct module *from, struct token use) {
68 stralloc sa = {0};
69 const char *s = use.str + 1;
70 const char *slash;
71 usize len = use.length - 2;
72
73 if(use.kind != TUSEPATH && (slash = strrchr(from->name, '/')))
74 sacatb(&sa, from->name, slash - from->name + 1);
75 if(len > 4 && memcmp(s + len - 4, ".kav", 4) == 0)
76 len -= 4;
77 sacatb(&sa, s, len);
78 sa0(&sa);
79 return sa.s;
80}
81
82static char *
83rootname(const char *path) {
84 stralloc sa = {0};
85 const char *s;
86 usize len;
87
88 s = strrchr(path, '/');
89 s = s ? s + 1 : path;
90 len = strlen(s);
91 if(len > 4 && strcmp(s + len - 4, ".kav") == 0)
92 len -= 4;
93 sacatb(&sa, s, len);
94 sa0(&sa);
95 return sa.s;
96}
97
98static struct module *
99load(const char *path, const char *file, char *name, int sys, const struct location *at) {
100 struct module *m;
101 struct module *s;
102 struct decl *d;
103 char *canonical;
104 char *importpath;
105 char *importfile;
106 char *sourcepath;
107
108 canonical = realpath(path, nil);
109 if(!canonical) {
110 if(!at)
111 diesys("unable to resolve ", file);
112 errorat(at, "unable to resolve ", file, ": ", strerror(errno));
113 }
114 for(s = seen; s; s = s->nextseen) {
115 if(strcmp(s->canonical, canonical) == 0) {
116 if(s->state == 1)
117 die("import cycle through ", file);
118 free(canonical);
119 free(name);
120 return s;
121 }
122 }
123 m = zalloc(sizeof(*m));
124 m->state = 1;
125 m->name = name;
126 m->sys = sys;
127 m->nextseen = seen;
128 seen = m;
129 sourcepath = alloc(strlen(file) + 1);
130 strcpy(sourcepath, file);
131 m->path = sourcepath;
132 m->canonical = canonical;
133 if(!openreadclose(canonical, &m->src)) {
134 if(!at)
135 diesys("unable to read ", file);
136 errorat(at, "unable to read ", file, ": ", strerror(errno));
137 }
138 sa0(&m->src);
139 m->decls = parse(m->path, m->src.s);
140 for(d = m->decls; d; d = d->next)
141 if(d->kind == DUSE) {
142 importpath = usepath(canonical, d->name);
143 importfile = d->name.kind == TUSEPATH ? importpath : usepath(m->path, d->name);
144 /* system modules and anything they import get k.sys. names */
145 d->mod = load(importpath, importfile, usename(m, d->name),
146 d->name.kind == TUSEPATH || m->sys, &d->name.loc);
147 if(importfile != importpath)
148 free(importfile);
149 free(importpath);
150 }
151 m->state = 2;
152 *end = m;
153 end = &m->next;
154 return m;
155}
156
157struct module *
158loadall(const char *path) {
159 seen = nil;
160 head = nil;
161 end = &head;
162 load(path, path, rootname(path), 0, nil);
163 return head;
164}