fparseln.c revision 1.2.2.4 1 1.2.2.4 tron /* $NetBSD: fparseln.c,v 1.2.2.4 2004/06/22 21:42:28 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.2.2.4 tron #if HAVE_NBTOOL_CONFIG_H
33 1.2.2.4 tron #include "nbtool_config.h"
34 1.2.2.4 tron #endif
35 1.2.2.4 tron
36 1.1 lukem #include <sys/cdefs.h>
37 1.1 lukem #if defined(LIBC_SCCS) && !defined(lint)
38 1.2.2.4 tron __RCSID("$NetBSD: fparseln.c,v 1.2.2.4 2004/06/22 21:42:28 tron Exp $");
39 1.1 lukem #endif /* LIBC_SCCS and not lint */
40 1.1 lukem
41 1.1 lukem #include "namespace.h"
42 1.1 lukem
43 1.1 lukem #include <assert.h>
44 1.1 lukem #include <errno.h>
45 1.1 lukem #include <stdio.h>
46 1.1 lukem #include <string.h>
47 1.1 lukem #include <stdlib.h>
48 1.1 lukem
49 1.1 lukem #ifdef __weak_alias
50 1.1 lukem __weak_alias(fparseln,_fparseln)
51 1.1 lukem #endif
52 1.1 lukem
53 1.1 lukem #if ! HAVE_FPARSELN
54 1.1 lukem
55 1.1 lukem static int isescaped(const char *, const char *, int);
56 1.1 lukem
57 1.1 lukem /* isescaped():
58 1.1 lukem * Return true if the character in *p that belongs to a string
59 1.1 lukem * that starts in *sp, is escaped by the escape character esc.
60 1.1 lukem */
61 1.1 lukem static int
62 1.1 lukem isescaped(const char *sp, const char *p, int esc)
63 1.1 lukem {
64 1.1 lukem const char *cp;
65 1.1 lukem size_t ne;
66 1.1 lukem
67 1.1 lukem _DIAGASSERT(sp != NULL);
68 1.1 lukem _DIAGASSERT(p != NULL);
69 1.1 lukem
70 1.1 lukem /* No escape character */
71 1.1 lukem if (esc == '\0')
72 1.1 lukem return 1;
73 1.1 lukem
74 1.1 lukem /* Count the number of escape characters that precede ours */
75 1.1 lukem for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
76 1.1 lukem continue;
77 1.1 lukem
78 1.1 lukem /* Return true if odd number of escape characters */
79 1.1 lukem return (ne & 1) != 0;
80 1.1 lukem }
81 1.1 lukem
82 1.1 lukem
83 1.1 lukem /* fparseln():
84 1.1 lukem * Read a line from a file parsing continuations ending in \
85 1.1 lukem * and eliminating trailing newlines, or comments starting with
86 1.1 lukem * the comment char.
87 1.1 lukem */
88 1.1 lukem char *
89 1.1 lukem fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
90 1.1 lukem {
91 1.1 lukem static const char dstr[3] = { '\\', '\\', '#' };
92 1.1 lukem
93 1.1 lukem size_t s, len;
94 1.1 lukem char *buf;
95 1.1 lukem char *ptr, *cp;
96 1.1 lukem int cnt;
97 1.1 lukem char esc, con, nl, com;
98 1.1 lukem
99 1.1 lukem _DIAGASSERT(fp != NULL);
100 1.1 lukem
101 1.1 lukem len = 0;
102 1.1 lukem buf = NULL;
103 1.1 lukem cnt = 1;
104 1.1 lukem
105 1.1 lukem if (str == NULL)
106 1.1 lukem str = dstr;
107 1.1 lukem
108 1.1 lukem esc = str[0];
109 1.1 lukem con = str[1];
110 1.1 lukem com = str[2];
111 1.1 lukem /*
112 1.1 lukem * XXX: it would be cool to be able to specify the newline character,
113 1.1 lukem * but unfortunately, fgetln does not let us
114 1.1 lukem */
115 1.1 lukem nl = '\n';
116 1.1 lukem
117 1.1 lukem while (cnt) {
118 1.1 lukem cnt = 0;
119 1.1 lukem
120 1.1 lukem if (lineno)
121 1.1 lukem (*lineno)++;
122 1.1 lukem
123 1.2.2.4 tron if ((ptr = fgetln(fp, &s)) == NULL)
124 1.1 lukem break;
125 1.1 lukem
126 1.1 lukem if (s && com) { /* Check and eliminate comments */
127 1.1 lukem for (cp = ptr; cp < ptr + s; cp++)
128 1.1 lukem if (*cp == com && !isescaped(ptr, cp, esc)) {
129 1.1 lukem s = cp - ptr;
130 1.1 lukem cnt = s == 0 && buf == NULL;
131 1.1 lukem break;
132 1.1 lukem }
133 1.1 lukem }
134 1.1 lukem
135 1.1 lukem if (s && nl) { /* Check and eliminate newlines */
136 1.1 lukem cp = &ptr[s - 1];
137 1.1 lukem
138 1.1 lukem if (*cp == nl)
139 1.1 lukem s--; /* forget newline */
140 1.1 lukem }
141 1.1 lukem
142 1.1 lukem if (s && con) { /* Check and eliminate continuations */
143 1.1 lukem cp = &ptr[s - 1];
144 1.1 lukem
145 1.1 lukem if (*cp == con && !isescaped(ptr, cp, esc)) {
146 1.1 lukem s--; /* forget escape */
147 1.1 lukem cnt = 1;
148 1.1 lukem }
149 1.1 lukem }
150 1.1 lukem
151 1.1 lukem if (s == 0 && buf != NULL)
152 1.1 lukem continue;
153 1.1 lukem
154 1.1 lukem if ((cp = realloc(buf, len + s + 1)) == NULL) {
155 1.1 lukem free(buf);
156 1.1 lukem return NULL;
157 1.1 lukem }
158 1.1 lukem buf = cp;
159 1.1 lukem
160 1.1 lukem (void) memcpy(buf + len, ptr, s);
161 1.1 lukem len += s;
162 1.1 lukem buf[len] = '\0';
163 1.1 lukem }
164 1.1 lukem
165 1.1 lukem if ((flags & FPARSELN_UNESCALL) != 0 && esc && buf != NULL &&
166 1.1 lukem strchr(buf, esc) != NULL) {
167 1.1 lukem ptr = cp = buf;
168 1.1 lukem while (cp[0] != '\0') {
169 1.1 lukem int skipesc;
170 1.1 lukem
171 1.1 lukem while (cp[0] != '\0' && cp[0] != esc)
172 1.1 lukem *ptr++ = *cp++;
173 1.1 lukem if (cp[0] == '\0' || cp[1] == '\0')
174 1.1 lukem break;
175 1.1 lukem
176 1.1 lukem skipesc = 0;
177 1.1 lukem if (cp[1] == com)
178 1.1 lukem skipesc += (flags & FPARSELN_UNESCCOMM);
179 1.1 lukem if (cp[1] == con)
180 1.1 lukem skipesc += (flags & FPARSELN_UNESCCONT);
181 1.1 lukem if (cp[1] == esc)
182 1.1 lukem skipesc += (flags & FPARSELN_UNESCESC);
183 1.1 lukem if (cp[1] != com && cp[1] != con && cp[1] != esc)
184 1.1 lukem skipesc = (flags & FPARSELN_UNESCREST);
185 1.1 lukem
186 1.1 lukem if (skipesc)
187 1.1 lukem cp++;
188 1.1 lukem else
189 1.1 lukem *ptr++ = *cp++;
190 1.1 lukem *ptr++ = *cp++;
191 1.1 lukem }
192 1.1 lukem *ptr = '\0';
193 1.1 lukem len = strlen(buf);
194 1.1 lukem }
195 1.1 lukem
196 1.1 lukem if (size)
197 1.1 lukem *size = len;
198 1.1 lukem return buf;
199 1.1 lukem }
200 1.1 lukem
201 1.1 lukem #ifdef TEST
202 1.1 lukem
203 1.1 lukem int main(int, char **);
204 1.1 lukem
205 1.1 lukem int
206 1.1 lukem main(int argc, char **argv)
207 1.1 lukem {
208 1.1 lukem char *ptr;
209 1.1 lukem size_t size, line;
210 1.1 lukem
211 1.1 lukem line = 0;
212 1.1 lukem while ((ptr = fparseln(stdin, &size, &line, NULL,
213 1.1 lukem FPARSELN_UNESCALL)) != NULL)
214 1.1 lukem printf("line %d (%d) |%s|\n", line, size, ptr);
215 1.1 lukem return 0;
216 1.1 lukem }
217 1.1 lukem
218 1.1 lukem /*
219 1.1 lukem
220 1.1 lukem # This is a test
221 1.1 lukem line 1
222 1.1 lukem line 2 \
223 1.1 lukem line 3 # Comment
224 1.1 lukem line 4 \# Not comment \\\\
225 1.1 lukem
226 1.1 lukem # And a comment \
227 1.1 lukem line 5 \\\
228 1.1 lukem line 6
229 1.1 lukem
230 1.1 lukem */
231 1.1 lukem
232 1.1 lukem #endif /* TEST */
233 1.1 lukem #endif /* ! HAVE_FPARSELN */
234