annotate files.c @ 13:120629a5d6bf

seenfile -> placefile (clearer)
author David A. Holland
date Sun, 19 Dec 2010 19:49:43 -0500
parents 800f3a560a3b
children f6177d3ed5c2
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>
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
4 #include <string.h>
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
5 #include <unistd.h>
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
6 #include <fcntl.h>
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
7 #include <err.h>
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
8
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
9 #include "array.h"
8
97243badae69 split place stuff to its own file
David A. Holland
parents: 7
diff changeset
10 #include "place.h"
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
11 #include "files.h"
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
12
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
13 struct incdir {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
14 const char *name;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
15 bool issystem;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
16 };
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
17
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
18 DECLARRAY(incdir);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
19 DEFARRAY(incdir, );
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
20
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
21 static struct incdirarray quotepath, bracketpath;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
22
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
23 ////////////////////////////////////////////////////////////
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
24 // management
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
25
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
26 static
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
27 struct incdir *
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
28 incdir_create(const char *name, bool issystem)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
29 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
30 struct incdir *id;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
31
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
32 id = domalloc(sizeof(*id));
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
33 id->name = name;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
34 id->issystem = issystem;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
35 return id;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
36 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
37
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
38 static
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
39 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
40 incdir_destroy(struct incdir *id)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
41 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
42 free(id);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
43 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
44
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
45 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
46 files_init(void)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
47 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
48 incdirarray_init(&quotepath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
49 incdirarray_init(&bracketpath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
50 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
51
9
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 8
diff changeset
52 DESTROYALL_ARRAY(incdir, );
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
53
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
54 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
55 files_cleanup(void)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
56 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
57 incdirarray_destroyall(&quotepath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
58 incdirarray_cleanup(&quotepath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
59 incdirarray_destroyall(&bracketpath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
60 incdirarray_cleanup(&bracketpath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
61 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
62
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
63 ////////////////////////////////////////////////////////////
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
64 // path setup
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
65
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
66 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
67 files_addquotepath(const char *dir, bool issystem)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
68 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
69 struct incdir *id;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
70
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
71 id = incdir_create(dir, issystem);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
72 incdirarray_add(&quotepath, id, NULL);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
73 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
74
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
75 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
76 files_addbracketpath(const char *dir, bool issystem)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
77 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
78 struct incdir *id;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
79
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
80 id = incdir_create(dir, issystem);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
81 incdirarray_add(&bracketpath, id, NULL);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
82 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
83
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
84 ////////////////////////////////////////////////////////////
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
85 // parsing
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
86
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
87 void
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
88 file_read(const struct placefile *pf, int fd);
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
89
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
90 ////////////////////////////////////////////////////////////
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
91 // path search
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
92
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
93 static
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
94 char *
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
95 mkfilename(const char *dir, const char *file)
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
96 {
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
97 size_t dlen, flen, rlen;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
98 char *ret;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
99 bool needslash = false;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
100
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
101 dlen = strlen(dir);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
102 flen = strlen(file);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
103 if (dlen > 0 && dir[dlen-1] != '/') {
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
104 needslash = true;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
105 }
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
106
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
107 rlen = dlen + (needslash ? 1 : 0) + flen;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
108 ret = domalloc(rlen + 1);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
109 strcpy(ret, dir);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
110 if (needslash) {
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
111 strcat(ret, "/");
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
112 }
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
113 strcat(ret, file);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
114 return ret;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
115 }
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
116
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
117 static
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
118 int
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
119 file_tryopen(const char *file)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
120 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
121 int fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
122
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
123 fd = open(file, O_RDONLY);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
124 if (fd < 0) {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
125 return -1;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
126 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
127 /* XXX: do we need to do anything here or is this function pointless?*/
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
128 return fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
129 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
130
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
131 static
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
132 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
133 file_search(struct place *place, struct incdirarray *path, const char *name)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
134 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
135 unsigned i, num;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
136 struct incdir *id;
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
137 const struct placefile *pf;
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
138 char *file;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
139 int fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
140
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
141 assert(place != NULL);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
142
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
143 num = incdirarray_num(path);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
144 for (i=0; i<num; i++) {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
145 id = incdirarray_get(path, i);
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
146 file = mkfilename(id->name, name);
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
147 fd = file_tryopen(file);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
148 if (fd >= 0) {
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
149 pf = place_addfile(place, file, id->issystem);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
150 free(file);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
151 file_read(pf, fd);
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
152 close(fd);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
153 return;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
154 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
155 free(file);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
156 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
157 complain(place, "Include file %s not found", name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
158 complain_fail();
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
159 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
160
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
161 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
162 file_readquote(struct place *place, const char *name)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
163 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
164 file_search(place, &quotepath, name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
165 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
166
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
167 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
168 file_readbracket(struct place *place, const char *name)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
169 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
170 file_search(place, &bracketpath, name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
171 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
172
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
173 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
174 file_readabsolute(struct place *place, const char *name)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
175 {
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
176 const struct placefile *pf;
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
177 int fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
178
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
179 assert(place != NULL);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
180
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
181 fd = file_tryopen(name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
182 if (fd < 0) {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
183 warn("%s", name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
184 die();
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
185 }
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
186 pf = place_addfile(place, name, false);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
187 file_read(pf, fd);
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
188 close(fd);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
189 }