annotate files.c @ 9:1fbcbd58742e

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