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 DECLARRAY(incdir);
|
|
18 DEFARRAY(incdir, );
|
|
19
|
|
20 static struct incdirarray quotepath, bracketpath;
|
|
21
|
|
22 ////////////////////////////////////////////////////////////
|
|
23 // management
|
|
24
|
|
25 static
|
|
26 struct incdir *
|
|
27 incdir_create(const char *name, bool issystem)
|
|
28 {
|
|
29 struct incdir *id;
|
|
30
|
|
31 id = domalloc(sizeof(*id));
|
|
32 id->name = name;
|
|
33 id->issystem = issystem;
|
|
34 return id;
|
|
35 }
|
|
36
|
|
37 static
|
|
38 void
|
|
39 incdir_destroy(struct incdir *id)
|
|
40 {
|
|
41 free(id);
|
|
42 }
|
|
43
|
|
44 void
|
|
45 files_init(void)
|
|
46 {
|
|
47 incdirarray_init("epath);
|
|
48 incdirarray_init(&bracketpath);
|
|
49 }
|
|
50
|
9
|
51 DESTROYALL_ARRAY(incdir, );
|
6
|
52
|
|
53 void
|
|
54 files_cleanup(void)
|
|
55 {
|
|
56 incdirarray_destroyall("epath);
|
|
57 incdirarray_cleanup("epath);
|
|
58 incdirarray_destroyall(&bracketpath);
|
|
59 incdirarray_cleanup(&bracketpath);
|
|
60 }
|
|
61
|
|
62 ////////////////////////////////////////////////////////////
|
|
63 // path setup
|
|
64
|
|
65 void
|
|
66 files_addquotepath(const char *dir, bool issystem)
|
|
67 {
|
|
68 struct incdir *id;
|
|
69
|
|
70 id = incdir_create(dir, issystem);
|
|
71 incdirarray_add("epath, id, NULL);
|
|
72 }
|
|
73
|
|
74 void
|
|
75 files_addbracketpath(const char *dir, bool issystem)
|
|
76 {
|
|
77 struct incdir *id;
|
|
78
|
|
79 id = incdir_create(dir, issystem);
|
|
80 incdirarray_add(&bracketpath, id, NULL);
|
|
81 }
|
|
82
|
|
83 ////////////////////////////////////////////////////////////
|
|
84 // parsing
|
|
85
|
|
86 void
|
|
87 file_read(struct seenfile *sf, int fd);
|
|
88
|
|
89 ////////////////////////////////////////////////////////////
|
|
90 // path search
|
|
91
|
|
92 static
|
|
93 int
|
|
94 file_tryopen(const char *file)
|
|
95 {
|
|
96 int fd;
|
|
97
|
|
98 fd = open(file, O_RDONLY);
|
|
99 if (fd < 0) {
|
|
100 return -1;
|
|
101 }
|
|
102 /* XXX: do we need to do anything here or is this function pointless?*/
|
|
103 return fd;
|
|
104 }
|
|
105
|
|
106 static
|
|
107 void
|
|
108 file_search(struct place *place, struct incdirarray *path, const char *name)
|
|
109 {
|
|
110 unsigned i, num;
|
|
111 struct incdir *id;
|
|
112 struct seenfile *sf;
|
|
113 char *file;
|
|
114 int fd;
|
|
115
|
|
116 assert(place != NULL);
|
|
117
|
|
118 num = incdirarray_num(path);
|
|
119 for (i=0; i<num; i++) {
|
|
120 id = incdirarray_get(path, i);
|
|
121 file = dostrdup3(id->name, "/", name);
|
|
122 fd = file_tryopen(file);
|
|
123 if (fd >= 0) {
|
10
|
124 sf = place_seen_file(place, file, id->issystem);
|
6
|
125 file_read(sf, fd);
|
|
126 close(fd);
|
|
127 return;
|
|
128 }
|
|
129 free(file);
|
|
130 }
|
|
131 complain(place, "Include file %s not found", name);
|
|
132 complain_fail();
|
|
133 }
|
|
134
|
|
135 void
|
|
136 file_readquote(struct place *place, const char *name)
|
|
137 {
|
|
138 file_search(place, "epath, name);
|
|
139 }
|
|
140
|
|
141 void
|
|
142 file_readbracket(struct place *place, const char *name)
|
|
143 {
|
|
144 file_search(place, &bracketpath, name);
|
|
145 }
|
|
146
|
|
147 void
|
|
148 file_readabsolute(struct place *place, const char *name)
|
|
149 {
|
|
150 struct seenfile *sf;
|
|
151 int fd;
|
|
152
|
|
153 assert(place != NULL);
|
|
154
|
|
155 fd = file_tryopen(name);
|
|
156 if (fd < 0) {
|
|
157 warn("%s", name);
|
|
158 die();
|
|
159 }
|
10
|
160 sf = place_seen_file(place, dostrdup(name), false);
|
6
|
161 file_read(sf, fd);
|
|
162 close(fd);
|
|
163 }
|