annotate files.c @ 203:3a25180d3a5c

Abort on line numbering or column numbering overflow. Line numbers are limited to values that fit in "unsigned int". Also reject input lines longer than 2^32-1 characters. It seems reasonable to presume that any input that violates these constraints is someone screwing around and not a serious attempt to compile or preprocess anything useful. Done in response to n2129, but without getting into any of the silliness found there.
author David A. Holland
date Tue, 01 Aug 2017 14:51:04 -0400
parents 1d2bad7151f9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
1 /*-
99
60184aa42604 add 2013 to copyrights where it seems warranted
David A. Holland
parents: 81
diff changeset
2 * Copyright (c) 2010, 2013 The NetBSD Foundation, Inc.
30
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
3 * All rights reserved.
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
4 *
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
5 * This code is derived from software contributed to The NetBSD Foundation
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
6 * by David A. Holland.
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
7 *
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
8 * Redistribution and use in source and binary forms, with or without
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
9 * modification, are permitted provided that the following conditions
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
10 * are met:
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
11 * 1. Redistributions of source code must retain the above copyright
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
12 * notice, this list of conditions and the following disclaimer.
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
13 * 2. Redistributions in binary form must reproduce the above copyright
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
14 * notice, this list of conditions and the following disclaimer in the
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
15 * documentation and/or other materials provided with the distribution.
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
16 *
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
27 * POSSIBILITY OF SUCH DAMAGE.
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
28 */
76c114899f63 copyrights
David A. Holland
parents: 28
diff changeset
29
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
30 #include <stdio.h>
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
31 #include <stdlib.h>
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
32 #include <string.h>
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
33 #include <unistd.h>
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
34 #include <fcntl.h>
113
1e7144176a42 Print a warning if we get an unexpected error trying to open a file.
David A. Holland
parents: 112
diff changeset
35 #include <errno.h>
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
36
183
4c3375895c6e Don't use <stdbool.h> unless __STDC__ is large enough.
David A. Holland
parents: 175
diff changeset
37 #include "bool.h"
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
38 #include "array.h"
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
39 #include "mode.h"
8
97243badae69 split place stuff to its own file
David A. Holland
parents: 7
diff changeset
40 #include "place.h"
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
41 #include "files.h"
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
42 #include "directive.h"
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
43
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
44 struct incdir {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
45 const char *name;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
46 bool issystem;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
47 };
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
48
107
33954a07d013 __unused -> UNUSED
David A. Holland
parents: 104
diff changeset
49 DECLARRAY(incdir, static UNUSED);
47
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
50 DEFARRAY(incdir, static);
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
51
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
52 static struct incdirarray quotepath, bracketpath;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
53
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
54 ////////////////////////////////////////////////////////////
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
55 // management
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
56
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
57 static
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
58 struct incdir *
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
59 incdir_create(const char *name, bool issystem)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
60 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
61 struct incdir *id;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
62
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
63 id = domalloc(sizeof(*id));
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
64 id->name = name;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
65 id->issystem = issystem;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
66 return id;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
67 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
68
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
69 static
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
70 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
71 incdir_destroy(struct incdir *id)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
72 {
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
73 dofree(id, sizeof(*id));
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
74 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
75
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
76 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
77 files_init(void)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
78 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
79 incdirarray_init(&quotepath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
80 incdirarray_init(&bracketpath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
81 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
82
9
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 8
diff changeset
83 DESTROYALL_ARRAY(incdir, );
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
84
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
85 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
86 files_cleanup(void)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
87 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
88 incdirarray_destroyall(&quotepath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
89 incdirarray_cleanup(&quotepath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
90 incdirarray_destroyall(&bracketpath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
91 incdirarray_cleanup(&bracketpath);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
92 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
93
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
94 ////////////////////////////////////////////////////////////
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
95 // path setup
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
96
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
97 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
98 files_addquotepath(const char *dir, bool issystem)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
99 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
100 struct incdir *id;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
101
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
102 id = incdir_create(dir, issystem);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
103 incdirarray_add(&quotepath, id, NULL);
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 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
107 files_addbracketpath(const char *dir, bool issystem)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
108 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
109 struct incdir *id;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
110
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
111 id = incdir_create(dir, issystem);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
112 incdirarray_add(&bracketpath, id, NULL);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
113 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
114
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
115 ////////////////////////////////////////////////////////////
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
116 // parsing
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
117
75
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
118 /*
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
119 * Find the end of the logical line. End of line characters that are
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
120 * commented out do not count.
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
121 */
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
122 static
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
123 size_t
75
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
124 findeol(const char *buf, size_t start, size_t limit)
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
125 {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
126 size_t i;
75
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
127 int incomment = 0;
81
27c9aafcaca1 Don't recognize comments within double-quote strings.
David A. Holland
parents: 75
diff changeset
128 bool inquote = false;
128
1cda505ddc78 Don't expand macros within character constants.
David A. Holland
parents: 113
diff changeset
129 char quote = '\0';
75
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
130
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
131 for (i=start; i<limit; i++) {
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
132 if (incomment) {
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
133 if (i+1 < limit && buf[i] == '*' && buf[i+1] == '/') {
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
134 i++;
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
135 incomment = 0;
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
136 }
81
27c9aafcaca1 Don't recognize comments within double-quote strings.
David A. Holland
parents: 75
diff changeset
137 } else if (!inquote && i+1 < limit &&
27c9aafcaca1 Don't recognize comments within double-quote strings.
David A. Holland
parents: 75
diff changeset
138 buf[i] == '/' && buf[i+1] == '*') {
27c9aafcaca1 Don't recognize comments within double-quote strings.
David A. Holland
parents: 75
diff changeset
139 i++;
27c9aafcaca1 Don't recognize comments within double-quote strings.
David A. Holland
parents: 75
diff changeset
140 incomment = 1;
27c9aafcaca1 Don't recognize comments within double-quote strings.
David A. Holland
parents: 75
diff changeset
141 } else if (i+1 < limit &&
128
1cda505ddc78 Don't expand macros within character constants.
David A. Holland
parents: 113
diff changeset
142 buf[i] == '\\' && buf[i+1] != '\n') {
81
27c9aafcaca1 Don't recognize comments within double-quote strings.
David A. Holland
parents: 75
diff changeset
143 i++;
128
1cda505ddc78 Don't expand macros within character constants.
David A. Holland
parents: 113
diff changeset
144 } else if (!inquote && (buf[i] == '"' || buf[i] == '\'')) {
1cda505ddc78 Don't expand macros within character constants.
David A. Holland
parents: 113
diff changeset
145 inquote = true;
1cda505ddc78 Don't expand macros within character constants.
David A. Holland
parents: 113
diff changeset
146 quote = buf[i];
1cda505ddc78 Don't expand macros within character constants.
David A. Holland
parents: 113
diff changeset
147 } else if (inquote && buf[i] == quote) {
1cda505ddc78 Don't expand macros within character constants.
David A. Holland
parents: 113
diff changeset
148 inquote = false;
81
27c9aafcaca1 Don't recognize comments within double-quote strings.
David A. Holland
parents: 75
diff changeset
149 } else if (buf[i] == '\n') {
27c9aafcaca1 Don't recognize comments within double-quote strings.
David A. Holland
parents: 75
diff changeset
150 return i;
75
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
151 }
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
152 }
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
153 return limit;
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
154 }
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
155
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
156 static
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
157 unsigned
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
158 countnls(const char *buf, size_t start, size_t limit)
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
159 {
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
160 size_t i;
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
161 unsigned count = 0;
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
162
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
163 for (i=start; i<limit; i++) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
164 if (buf[i] == '\n') {
75
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
165 count++;
203
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
166 if (count == 0) {
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
167 /* just return the max and error downstream */
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
168 return count - 1;
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
169 }
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
170 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
171 }
75
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
172 return count;
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
173 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
174
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
175 static
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
176 void
28
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 24
diff changeset
177 file_read(const struct placefile *pf, int fd, const char *name, bool toplevel)
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
178 {
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 143
diff changeset
179 struct lineplace places;
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 143
diff changeset
180 struct place ptmp;
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
181 size_t bufend, bufmax, linestart, lineend, nextlinestart, tmp;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
182 ssize_t result;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
183 bool ateof = false;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
184 char *buf;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
185
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 143
diff changeset
186 place_setfilestart(&places.current, pf);
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 143
diff changeset
187 places.nextline = places.current;
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
188
199
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 187
diff changeset
189 if (name) {
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 187
diff changeset
190 debuglog(&places.current, "Reading file %s", name);
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 187
diff changeset
191 } else {
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 187
diff changeset
192 debuglog(&places.current, "Reading standard input");
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 187
diff changeset
193 }
1d2bad7151f9 Add a -debuglog option to send an execution trace to a file.
David A. Holland
parents: 187
diff changeset
194
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
195 bufmax = 128;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
196 bufend = 0;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
197 linestart = 0;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
198 lineend = 0;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
199 buf = domalloc(bufmax);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
200
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
201 while (1) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
202 if (lineend >= bufend) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
203 /* do not have a whole line in the buffer; read more */
75
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
204 assert(bufend >= linestart);
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
205 if (linestart > 0 && bufend > linestart) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
206 /* slide to beginning of buffer */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
207 memmove(buf, buf+linestart, bufend-linestart);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
208 bufend -= linestart;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
209 lineend -= linestart;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
210 linestart = 0;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
211 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
212 if (bufend >= bufmax) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
213 /* need bigger buffer */
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
214 buf = dorealloc(buf, bufmax, bufmax*2);
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
215 bufmax = bufmax*2;
203
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
216 /* just in case someone's screwing around */
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
217 if (bufmax > 0xffffffff) {
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
218 complain(&places.current,
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
219 "Input line too long");
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
220 die();
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
221 }
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
222 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
223
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
224 if (ateof) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
225 /* don't read again, in case it's a socket */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
226 result = 0;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
227 } else {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
228 result = read(fd, buf+bufend, bufmax - bufend);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
229 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
230
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
231 if (result == -1) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
232 /* read error */
143
ed45f2d8d3bc Don't use the <err.h> functions.
David A. Holland
parents: 128
diff changeset
233 complain(NULL, "%s: %s",
ed45f2d8d3bc Don't use the <err.h> functions.
David A. Holland
parents: 128
diff changeset
234 name, strerror(errno));
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
235 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
236 } else if (result == 0 && bufend == linestart) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
237 /* eof */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
238 ateof = true;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
239 break;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
240 } else if (result == 0) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
241 /* eof in middle of line */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
242 ateof = true;
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 143
diff changeset
243 ptmp = places.current;
203
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
244 place_addcolumns(&ptmp, bufend - linestart);
186
9637bf434f8e Don't report unclosed comments as "no newline at end of file".
David A. Holland
parents: 183
diff changeset
245 if (buf[bufend - 1] == '\n') {
9637bf434f8e Don't report unclosed comments as "no newline at end of file".
David A. Holland
parents: 183
diff changeset
246 complain(&ptmp, "Unclosed comment");
187
8c7e508da6cc more of previous
David A. Holland
parents: 186
diff changeset
247 complain_fail();
186
9637bf434f8e Don't report unclosed comments as "no newline at end of file".
David A. Holland
parents: 183
diff changeset
248 } else {
9637bf434f8e Don't report unclosed comments as "no newline at end of file".
David A. Holland
parents: 183
diff changeset
249 complain(&ptmp,
9637bf434f8e Don't report unclosed comments as "no newline at end of file".
David A. Holland
parents: 183
diff changeset
250 "No newline at end of file");
9637bf434f8e Don't report unclosed comments as "no newline at end of file".
David A. Holland
parents: 183
diff changeset
251 }
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
252 if (mode.werror) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
253 complain_fail();
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
254 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
255 assert(bufend < bufmax);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
256 lineend = bufend++;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
257 buf[lineend] = '\n';
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
258 } else {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
259 bufend += (size_t)result;
81
27c9aafcaca1 Don't recognize comments within double-quote strings.
David A. Holland
parents: 75
diff changeset
260 lineend = findeol(buf, linestart, bufend);
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
261 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
262 /* loop in case we still don't have a whole line */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
263 continue;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
264 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
265
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
266 /* have a line */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
267 assert(buf[lineend] == '\n');
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
268 buf[lineend] = '\0';
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
269 nextlinestart = lineend+1;
203
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
270 place_addlines(&places.nextline, 1);
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
271
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
272 /* check for CR/NL */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
273 if (lineend > 0 && buf[lineend-1] == '\r') {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
274 buf[lineend-1] = '\0';
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
275 lineend--;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
276 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
277
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
278 /* check for continuation line */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
279 if (lineend > 0 && buf[lineend-1]=='\\') {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
280 lineend--;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
281 tmp = nextlinestart - lineend;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
282 if (bufend > nextlinestart) {
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
283 memmove(buf+lineend, buf+nextlinestart,
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
284 bufend - nextlinestart);
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
285 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
286 bufend -= tmp;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
287 nextlinestart -= tmp;
81
27c9aafcaca1 Don't recognize comments within double-quote strings.
David A. Holland
parents: 75
diff changeset
288 lineend = findeol(buf, linestart, bufend);
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
289 /* might not have a whole line, so loop */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
290 continue;
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
291 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
292
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
293 /* line now goes from linestart to lineend */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
294 assert(buf[lineend] == '\0');
75
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
295
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
296 /* count how many commented-out newlines we swallowed */
203
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
297 place_addlines(&places.nextline,
3a25180d3a5c Abort on line numbering or column numbering overflow.
David A. Holland
parents: 199
diff changeset
298 countnls(buf, linestart, lineend));
75
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
299
175
ffdb0b73856f Suppress blank lines later.
David A. Holland
parents: 164
diff changeset
300 /* process the line (even if it's empty) */
ffdb0b73856f Suppress blank lines later.
David A. Holland
parents: 164
diff changeset
301 directive_gotline(&places, buf+linestart, lineend-linestart);
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
302
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
303 linestart = nextlinestart;
75
980ed7cb620a More multiline comment fixes.
David A. Holland
parents: 47
diff changeset
304 lineend = findeol(buf, linestart, bufend);
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 143
diff changeset
305 places.current = places.nextline;
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
306 }
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
307
28
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 24
diff changeset
308 if (toplevel) {
154
a2c2fe8dbea3 Wrap up the current and next line position when invoking directives.
David A. Holland
parents: 143
diff changeset
309 directive_goteof(&places.current);
28
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 24
diff changeset
310 }
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
311 dofree(buf, bufmax);
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
312 }
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
313
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
314 ////////////////////////////////////////////////////////////
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
315 // path search
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
316
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
317 static
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
318 char *
112
2b0b61fd1a36 Fix handling of relative includes.
David A. Holland
parents: 107
diff changeset
319 mkfilename(struct place *place, const char *dir, const char *file)
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
320 {
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
321 size_t dlen, flen, rlen;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
322 char *ret;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
323 bool needslash = false;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
324
112
2b0b61fd1a36 Fix handling of relative includes.
David A. Holland
parents: 107
diff changeset
325 if (dir == NULL) {
2b0b61fd1a36 Fix handling of relative includes.
David A. Holland
parents: 107
diff changeset
326 dir = place_getparsedir(place);
2b0b61fd1a36 Fix handling of relative includes.
David A. Holland
parents: 107
diff changeset
327 }
2b0b61fd1a36 Fix handling of relative includes.
David A. Holland
parents: 107
diff changeset
328
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
329 dlen = strlen(dir);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
330 flen = strlen(file);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
331 if (dlen > 0 && dir[dlen-1] != '/') {
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
332 needslash = true;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
333 }
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
334
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
335 rlen = dlen + (needslash ? 1 : 0) + flen;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
336 ret = domalloc(rlen + 1);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
337 strcpy(ret, dir);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
338 if (needslash) {
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
339 strcat(ret, "/");
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
340 }
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
341 strcat(ret, file);
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
342 return ret;
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
343 }
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
344
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
345 static
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
346 int
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
347 file_tryopen(const char *file)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
348 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
349 int fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
350
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
351 /* XXX check for non-regular files */
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
352
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
353 fd = open(file, O_RDONLY);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
354 if (fd < 0) {
113
1e7144176a42 Print a warning if we get an unexpected error trying to open a file.
David A. Holland
parents: 112
diff changeset
355 if (errno != ENOENT && errno != ENOTDIR) {
143
ed45f2d8d3bc Don't use the <err.h> functions.
David A. Holland
parents: 128
diff changeset
356 complain(NULL, "%s: %s", file, strerror(errno));
113
1e7144176a42 Print a warning if we get an unexpected error trying to open a file.
David A. Holland
parents: 112
diff changeset
357 }
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
358 return -1;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
359 }
15
f6177d3ed5c2 handle directives
David A. Holland
parents: 13
diff changeset
360
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
361 return fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
362 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
363
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
364 static
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
365 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
366 file_search(struct place *place, struct incdirarray *path, const char *name)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
367 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
368 unsigned i, num;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
369 struct incdir *id;
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
370 const struct placefile *pf;
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
371 char *file;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
372 int fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
373
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
374 assert(place != NULL);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
375
104
91f600e6647b Allow absolute paths in include files.
David A. Holland
parents: 99
diff changeset
376 if (name[0] == '/') {
91f600e6647b Allow absolute paths in include files.
David A. Holland
parents: 99
diff changeset
377 fd = file_tryopen(name);
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
378 if (fd >= 0) {
104
91f600e6647b Allow absolute paths in include files.
David A. Holland
parents: 99
diff changeset
379 pf = place_addfile(place, name, true);
91f600e6647b Allow absolute paths in include files.
David A. Holland
parents: 99
diff changeset
380 file_read(pf, fd, name, false);
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
381 close(fd);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
382 return;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
383 }
104
91f600e6647b Allow absolute paths in include files.
David A. Holland
parents: 99
diff changeset
384 } else {
91f600e6647b Allow absolute paths in include files.
David A. Holland
parents: 99
diff changeset
385 num = incdirarray_num(path);
91f600e6647b Allow absolute paths in include files.
David A. Holland
parents: 99
diff changeset
386 for (i=0; i<num; i++) {
91f600e6647b Allow absolute paths in include files.
David A. Holland
parents: 99
diff changeset
387 id = incdirarray_get(path, i);
112
2b0b61fd1a36 Fix handling of relative includes.
David A. Holland
parents: 107
diff changeset
388 file = mkfilename(place, id->name, name);
104
91f600e6647b Allow absolute paths in include files.
David A. Holland
parents: 99
diff changeset
389 fd = file_tryopen(file);
91f600e6647b Allow absolute paths in include files.
David A. Holland
parents: 99
diff changeset
390 if (fd >= 0) {
91f600e6647b Allow absolute paths in include files.
David A. Holland
parents: 99
diff changeset
391 pf = place_addfile(place, file, id->issystem);
91f600e6647b Allow absolute paths in include files.
David A. Holland
parents: 99
diff changeset
392 file_read(pf, fd, file, false);
91f600e6647b Allow absolute paths in include files.
David A. Holland
parents: 99
diff changeset
393 dostrfree(file);
91f600e6647b Allow absolute paths in include files.
David A. Holland
parents: 99
diff changeset
394 close(fd);
91f600e6647b Allow absolute paths in include files.
David A. Holland
parents: 99
diff changeset
395 return;
91f600e6647b Allow absolute paths in include files.
David A. Holland
parents: 99
diff changeset
396 }
91f600e6647b Allow absolute paths in include files.
David A. Holland
parents: 99
diff changeset
397 dostrfree(file);
91f600e6647b Allow absolute paths in include files.
David A. Holland
parents: 99
diff changeset
398 }
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
399 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
400 complain(place, "Include file %s not found", name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
401 complain_fail();
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
402 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
403
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
404 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
405 file_readquote(struct place *place, const char *name)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
406 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
407 file_search(place, &quotepath, name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
408 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
409
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
410 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
411 file_readbracket(struct place *place, const char *name)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
412 {
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
413 file_search(place, &bracketpath, name);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
414 }
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
415
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
416 void
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
417 file_readabsolute(struct place *place, const char *name)
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
418 {
13
120629a5d6bf seenfile -> placefile (clearer)
David A. Holland
parents: 10
diff changeset
419 const struct placefile *pf;
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
420 int fd;
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
421
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
422 assert(place != NULL);
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
423
24
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
424 if (name == NULL) {
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
425 fd = STDIN_FILENO;
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
426 pf = place_addfile(place, "<standard-input>", false);
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
427 } else {
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
428 fd = file_tryopen(name);
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
429 if (fd < 0) {
143
ed45f2d8d3bc Don't use the <err.h> functions.
David A. Holland
parents: 128
diff changeset
430 complain(NULL, "%s: %s", name, strerror(errno));
24
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
431 die();
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
432 }
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
433 pf = place_addfile(place, name, false);
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
434 }
24
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
435
28
8a955e3dda2c two more tests, more fixes
David A. Holland
parents: 24
diff changeset
436 file_read(pf, fd, name, true);
24
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
437
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
438 if (name != NULL) {
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
439 close(fd);
daa801fe719e fix some bugs
David A. Holland
parents: 15
diff changeset
440 }
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents:
diff changeset
441 }