6
|
1 #include <stdbool.h>
|
|
2 #include <stdio.h>
|
|
3 #include <stdlib.h>
|
|
4 #include <unistd.h>
|
|
5 #include <fcntl.h>
|
|
6 #include <err.h>
|
|
7
|
|
8 #include "array.h"
|
8
|
9 #include "place.h"
|
6
|
10 #include "files.h"
|
|
11
|
|
12 struct incdir {
|
|
13 const char *name;
|
|
14 bool issystem;
|
|
15 };
|
|
16
|
|
17 struct seenfile {
|
|
18 struct place includedfrom;
|
|
19 char *name;
|
|
20 bool fromsystemdir;
|
|
21 };
|
|
22
|
|
23 DECLARRAY(incdir);
|
|
24 DECLARRAY(seenfile);
|
|
25 DEFARRAY(incdir, );
|
|
26 DEFARRAY(seenfile, );
|
|
27
|
|
28 static struct incdirarray quotepath, bracketpath;
|
|
29 static struct seenfilearray seenfiles;
|
|
30
|
|
31 ////////////////////////////////////////////////////////////
|
|
32 // management
|
|
33
|
|
34 static
|
|
35 struct incdir *
|
|
36 incdir_create(const char *name, bool issystem)
|
|
37 {
|
|
38 struct incdir *id;
|
|
39
|
|
40 id = domalloc(sizeof(*id));
|
|
41 id->name = name;
|
|
42 id->issystem = issystem;
|
|
43 return id;
|
|
44 }
|
|
45
|
|
46 static
|
|
47 void
|
|
48 incdir_destroy(struct incdir *id)
|
|
49 {
|
|
50 free(id);
|
|
51 }
|
|
52
|
|
53 static
|
|
54 struct seenfile *
|
|
55 seenfile_create(const struct place *from, char *name, bool fromsystemdir)
|
|
56 {
|
|
57 struct seenfile *sf;
|
|
58
|
|
59 sf = domalloc(sizeof(*sf));
|
|
60 sf->includedfrom = *from;
|
|
61 sf->name = name;
|
|
62 sf->fromsystemdir = fromsystemdir;
|
|
63 return sf;
|
|
64 }
|
|
65
|
|
66 static
|
|
67 void
|
|
68 seenfile_destroy(struct seenfile *sf)
|
|
69 {
|
|
70 free(sf->name);
|
|
71 free(sf);
|
|
72 }
|
|
73
|
|
74 void
|
|
75 files_init(void)
|
|
76 {
|
|
77 incdirarray_init("epath);
|
|
78 incdirarray_init(&bracketpath);
|
|
79 }
|
|
80
|
9
|
81 DESTROYALL_ARRAY(incdir, );
|
|
82 DESTROYALL_ARRAY(seenfile, );
|
6
|
83
|
|
84 void
|
|
85 files_cleanup(void)
|
|
86 {
|
|
87 seenfilearray_destroyall(&seenfiles);
|
|
88 seenfilearray_cleanup(&seenfiles);
|
|
89
|
|
90 incdirarray_destroyall("epath);
|
|
91 incdirarray_cleanup("epath);
|
|
92 incdirarray_destroyall(&bracketpath);
|
|
93 incdirarray_cleanup(&bracketpath);
|
|
94 }
|
|
95
|
|
96 ////////////////////////////////////////////////////////////
|
|
97 // path setup
|
|
98
|
|
99 void
|
|
100 files_addquotepath(const char *dir, bool issystem)
|
|
101 {
|
|
102 struct incdir *id;
|
|
103
|
|
104 id = incdir_create(dir, issystem);
|
|
105 incdirarray_add("epath, id, NULL);
|
|
106 }
|
|
107
|
|
108 void
|
|
109 files_addbracketpath(const char *dir, bool issystem)
|
|
110 {
|
|
111 struct incdir *id;
|
|
112
|
|
113 id = incdir_create(dir, issystem);
|
|
114 incdirarray_add(&bracketpath, id, NULL);
|
|
115 }
|
|
116
|
|
117 ////////////////////////////////////////////////////////////
|
8
|
118 // seenfile functions exposed for places.c
|
6
|
119
|
8
|
120 const char *
|
|
121 seenfile_getname(const struct seenfile *file)
|
6
|
122 {
|
8
|
123 return file->name;
|
7
|
124 }
|
|
125
|
8
|
126 const struct place *
|
|
127 seenfile_getincludeplace(const struct seenfile *file)
|
6
|
128 {
|
8
|
129 return &file->includedfrom;
|
6
|
130 }
|
|
131
|
|
132 ////////////////////////////////////////////////////////////
|
|
133 // parsing
|
|
134
|
|
135 void
|
|
136 file_read(struct seenfile *sf, int fd);
|
|
137
|
|
138 ////////////////////////////////////////////////////////////
|
|
139 // path search
|
|
140
|
|
141 static
|
|
142 int
|
|
143 file_tryopen(const char *file)
|
|
144 {
|
|
145 int fd;
|
|
146
|
|
147 fd = open(file, O_RDONLY);
|
|
148 if (fd < 0) {
|
|
149 return -1;
|
|
150 }
|
|
151 /* XXX: do we need to do anything here or is this function pointless?*/
|
|
152 return fd;
|
|
153 }
|
|
154
|
|
155 static
|
|
156 void
|
|
157 file_search(struct place *place, struct incdirarray *path, const char *name)
|
|
158 {
|
|
159 unsigned i, num;
|
|
160 struct incdir *id;
|
|
161 struct seenfile *sf;
|
|
162 char *file;
|
|
163 int fd;
|
|
164
|
|
165 assert(place != NULL);
|
|
166
|
|
167 num = incdirarray_num(path);
|
|
168 for (i=0; i<num; i++) {
|
|
169 id = incdirarray_get(path, i);
|
|
170 file = dostrdup3(id->name, "/", name);
|
|
171 fd = file_tryopen(file);
|
|
172 if (fd >= 0) {
|
|
173 sf = seenfile_create(place, file, id->issystem);
|
|
174 seenfilearray_add(&seenfiles, sf, NULL);
|
|
175 file_read(sf, fd);
|
|
176 close(fd);
|
|
177 return;
|
|
178 }
|
|
179 free(file);
|
|
180 }
|
|
181 complain(place, "Include file %s not found", name);
|
|
182 complain_fail();
|
|
183 }
|
|
184
|
|
185 void
|
|
186 file_readquote(struct place *place, const char *name)
|
|
187 {
|
|
188 file_search(place, "epath, name);
|
|
189 }
|
|
190
|
|
191 void
|
|
192 file_readbracket(struct place *place, const char *name)
|
|
193 {
|
|
194 file_search(place, &bracketpath, name);
|
|
195 }
|
|
196
|
|
197 void
|
|
198 file_readabsolute(struct place *place, const char *name)
|
|
199 {
|
|
200 struct seenfile *sf;
|
|
201 int fd;
|
|
202
|
|
203 assert(place != NULL);
|
|
204
|
|
205 fd = file_tryopen(name);
|
|
206 if (fd < 0) {
|
|
207 warn("%s", name);
|
|
208 die();
|
|
209 }
|
|
210 sf = seenfile_create(place, dostrdup(name), false);
|
|
211 seenfilearray_add(&seenfiles, sf, NULL);
|
|
212 file_read(sf, fd);
|
|
213 close(fd);
|
|
214 }
|