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