fparseln.c revision 1.2.2.2 1 1.2.2.2 tron /* $NetBSD: fparseln.c,v 1.2.2.2 2004/06/22 07:04:44 tron Exp $ */
2 1.1 lukem
3 1.1 lukem /*
4 1.1 lukem * Copyright (c) 1997 Christos Zoulas. All rights reserved.
5 1.1 lukem *
6 1.1 lukem * Redistribution and use in source and binary forms, with or without
7 1.1 lukem * modification, are permitted provided that the following conditions
8 1.1 lukem * are met:
9 1.1 lukem * 1. Redistributions of source code must retain the above copyright
10 1.1 lukem * notice, this list of conditions and the following disclaimer.
11 1.1 lukem * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 lukem * notice, this list of conditions and the following disclaimer in the
13 1.1 lukem * documentation and/or other materials provided with the distribution.
14 1.1 lukem * 3. All advertising materials mentioning features or use of this software
15 1.1 lukem * must display the following acknowledgement:
16 1.1 lukem * This product includes software developed by Christos Zoulas.
17 1.1 lukem * 4. The name of the author may not be used to endorse or promote products
18 1.1 lukem * derived from this software without specific prior written permission.
19 1.1 lukem *
20 1.1 lukem * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.1 lukem * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.1 lukem * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.1 lukem * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1 lukem * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.1 lukem * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.1 lukem * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.1 lukem * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.1 lukem * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.1 lukem * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1 lukem */
31 1.1 lukem
32 1.1 lukem #include <sys/cdefs.h>
33 1.1 lukem #if defined(LIBC_SCCS) && !defined(lint)
34 1.2.2.2 tron __RCSID("$NetBSD: fparseln.c,v 1.2.2.2 2004/06/22 07:04:44 tron Exp $");
35 1.1 lukem #endif /* LIBC_SCCS and not lint */
36 1.1 lukem
37 1.1 lukem #include "namespace.h"
38 1.1 lukem
39 1.1 lukem #include <assert.h>
40 1.1 lukem #include <errno.h>
41 1.1 lukem #include <stdio.h>
42 1.1 lukem #include <string.h>
43 1.1 lukem #include <stdlib.h>
44 1.1 lukem
45 1.1 lukem #ifdef __weak_alias
46 1.1 lukem __weak_alias(fparseln,_fparseln)
47 1.1 lukem #endif
48 1.1 lukem
49 1.1 lukem #if ! HAVE_FPARSELN
50 1.1 lukem
51 1.2.2.2 tron #ifndef HAVE_NBTOOL_CONFIG_H
52 1.2.2.1 tron #include "reentrant.h"
53 1.2.2.1 tron #include "local.h"
54 1.2.2.2 tron #else
55 1.2.2.2 tron #define FLOCKFILE(fp)
56 1.2.2.2 tron #define FUNLOCKFILE(fp)
57 1.2.2.2 tron #endif
58 1.2.2.1 tron
59 1.2.2.1 tron #ifdef _REENTRANT
60 1.2.2.1 tron #define __fgetln(f, l) __fgetstr(f, l, '\n')
61 1.2.2.1 tron #else
62 1.2.2.1 tron #define __fgetln(f, l) fgetln(f, l)
63 1.2.2.1 tron #endif
64 1.2.2.1 tron
65 1.1 lukem static int isescaped(const char *, const char *, int);
66 1.1 lukem
67 1.1 lukem /* isescaped():
68 1.1 lukem * Return true if the character in *p that belongs to a string
69 1.1 lukem * that starts in *sp, is escaped by the escape character esc.
70 1.1 lukem */
71 1.1 lukem static int
72 1.1 lukem isescaped(const char *sp, const char *p, int esc)
73 1.1 lukem {
74 1.1 lukem const char *cp;
75 1.1 lukem size_t ne;
76 1.1 lukem
77 1.1 lukem _DIAGASSERT(sp != NULL);
78 1.1 lukem _DIAGASSERT(p != NULL);
79 1.1 lukem
80 1.1 lukem /* No escape character */
81 1.1 lukem if (esc == '\0')
82 1.1 lukem return 1;
83 1.1 lukem
84 1.1 lukem /* Count the number of escape characters that precede ours */
85 1.1 lukem for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
86 1.1 lukem continue;
87 1.1 lukem
88 1.1 lukem /* Return true if odd number of escape characters */
89 1.1 lukem return (ne & 1) != 0;
90 1.1 lukem }
91 1.1 lukem
92 1.1 lukem
93 1.1 lukem /* fparseln():
94 1.1 lukem * Read a line from a file parsing continuations ending in \
95 1.1 lukem * and eliminating trailing newlines, or comments starting with
96 1.1 lukem * the comment char.
97 1.1 lukem */
98 1.1 lukem char *
99 1.1 lukem fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
100 1.1 lukem {
101 1.1 lukem static const char dstr[3] = { '\\', '\\', '#' };
102 1.1 lukem
103 1.1 lukem size_t s, len;
104 1.1 lukem char *buf;
105 1.1 lukem char *ptr, *cp;
106 1.1 lukem int cnt;
107 1.1 lukem char esc, con, nl, com;
108 1.1 lukem
109 1.1 lukem _DIAGASSERT(fp != NULL);
110 1.1 lukem
111 1.1 lukem len = 0;
112 1.1 lukem buf = NULL;
113 1.1 lukem cnt = 1;
114 1.1 lukem
115 1.1 lukem if (str == NULL)
116 1.1 lukem str = dstr;
117 1.1 lukem
118 1.1 lukem esc = str[0];
119 1.1 lukem con = str[1];
120 1.1 lukem com = str[2];
121 1.1 lukem /*
122 1.1 lukem * XXX: it would be cool to be able to specify the newline character,
123 1.1 lukem * but unfortunately, fgetln does not let us
124 1.1 lukem */
125 1.1 lukem nl = '\n';
126 1.1 lukem
127 1.2.2.1 tron FLOCKFILE(fp);
128 1.2.2.1 tron
129 1.1 lukem while (cnt) {
130 1.1 lukem cnt = 0;
131 1.1 lukem
132 1.1 lukem if (lineno)
133 1.1 lukem (*lineno)++;
134 1.1 lukem
135 1.2.2.1 tron if ((ptr = __fgetln(fp, &s)) == NULL)
136 1.1 lukem break;
137 1.1 lukem
138 1.1 lukem if (s && com) { /* Check and eliminate comments */
139 1.1 lukem for (cp = ptr; cp < ptr + s; cp++)
140 1.1 lukem if (*cp == com && !isescaped(ptr, cp, esc)) {
141 1.1 lukem s = cp - ptr;
142 1.1 lukem cnt = s == 0 && buf == NULL;
143 1.1 lukem break;
144 1.1 lukem }
145 1.1 lukem }
146 1.1 lukem
147 1.1 lukem if (s && nl) { /* Check and eliminate newlines */
148 1.1 lukem cp = &ptr[s - 1];
149 1.1 lukem
150 1.1 lukem if (*cp == nl)
151 1.1 lukem s--; /* forget newline */
152 1.1 lukem }
153 1.1 lukem
154 1.1 lukem if (s && con) { /* Check and eliminate continuations */
155 1.1 lukem cp = &ptr[s - 1];
156 1.1 lukem
157 1.1 lukem if (*cp == con && !isescaped(ptr, cp, esc)) {
158 1.1 lukem s--; /* forget escape */
159 1.1 lukem cnt = 1;
160 1.1 lukem }
161 1.1 lukem }
162 1.1 lukem
163 1.1 lukem if (s == 0 && buf != NULL)
164 1.1 lukem continue;
165 1.1 lukem
166 1.1 lukem if ((cp = realloc(buf, len + s + 1)) == NULL) {
167 1.2.2.1 tron FUNLOCKFILE(fp);
168 1.1 lukem free(buf);
169 1.1 lukem return NULL;
170 1.1 lukem }
171 1.1 lukem buf = cp;
172 1.1 lukem
173 1.1 lukem (void) memcpy(buf + len, ptr, s);
174 1.1 lukem len += s;
175 1.1 lukem buf[len] = '\0';
176 1.1 lukem }
177 1.1 lukem
178 1.2.2.1 tron FUNLOCKFILE(fp);
179 1.2.2.1 tron
180 1.1 lukem if ((flags & FPARSELN_UNESCALL) != 0 && esc && buf != NULL &&
181 1.1 lukem strchr(buf, esc) != NULL) {
182 1.1 lukem ptr = cp = buf;
183 1.1 lukem while (cp[0] != '\0') {
184 1.1 lukem int skipesc;
185 1.1 lukem
186 1.1 lukem while (cp[0] != '\0' && cp[0] != esc)
187 1.1 lukem *ptr++ = *cp++;
188 1.1 lukem if (cp[0] == '\0' || cp[1] == '\0')
189 1.1 lukem break;
190 1.1 lukem
191 1.1 lukem skipesc = 0;
192 1.1 lukem if (cp[1] == com)
193 1.1 lukem skipesc += (flags & FPARSELN_UNESCCOMM);
194 1.1 lukem if (cp[1] == con)
195 1.1 lukem skipesc += (flags & FPARSELN_UNESCCONT);
196 1.1 lukem if (cp[1] == esc)
197 1.1 lukem skipesc += (flags & FPARSELN_UNESCESC);
198 1.1 lukem if (cp[1] != com && cp[1] != con && cp[1] != esc)
199 1.1 lukem skipesc = (flags & FPARSELN_UNESCREST);
200 1.1 lukem
201 1.1 lukem if (skipesc)
202 1.1 lukem cp++;
203 1.1 lukem else
204 1.1 lukem *ptr++ = *cp++;
205 1.1 lukem *ptr++ = *cp++;
206 1.1 lukem }
207 1.1 lukem *ptr = '\0';
208 1.1 lukem len = strlen(buf);
209 1.1 lukem }
210 1.1 lukem
211 1.1 lukem if (size)
212 1.1 lukem *size = len;
213 1.1 lukem return buf;
214 1.1 lukem }
215 1.1 lukem
216 1.1 lukem #ifdef TEST
217 1.1 lukem
218 1.1 lukem int main(int, char **);
219 1.1 lukem
220 1.1 lukem int
221 1.1 lukem main(int argc, char **argv)
222 1.1 lukem {
223 1.1 lukem char *ptr;
224 1.1 lukem size_t size, line;
225 1.1 lukem
226 1.1 lukem line = 0;
227 1.1 lukem while ((ptr = fparseln(stdin, &size, &line, NULL,
228 1.1 lukem FPARSELN_UNESCALL)) != NULL)
229 1.1 lukem printf("line %d (%d) |%s|\n", line, size, ptr);
230 1.1 lukem return 0;
231 1.1 lukem }
232 1.1 lukem
233 1.1 lukem /*
234 1.1 lukem
235 1.1 lukem # This is a test
236 1.1 lukem line 1
237 1.1 lukem line 2 \
238 1.1 lukem line 3 # Comment
239 1.1 lukem line 4 \# Not comment \\\\
240 1.1 lukem
241 1.1 lukem # And a comment \
242 1.1 lukem line 5 \\\
243 1.1 lukem line 6
244 1.1 lukem
245 1.1 lukem */
246 1.1 lukem
247 1.1 lukem #endif /* TEST */
248 1.1 lukem #endif /* ! HAVE_FPARSELN */
249