annotate files.c @ 10:800f3a560a3b

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