1 /* $NetBSD: fparseln.c,v 1.9 1999/09/20 04:48:06 lukem Exp $ */
2 /* $FreeBSD: src/lib/libutil/fparseln.c,v 1.2 1999/12/29 17:50:33 peter Exp $ */
3 /* $DragonFly: src/lib/libutil/fparseln.c,v 1.3 2003/11/12 20:21:31 eirikn Exp $ */
6 * Copyright (c) 1997 Christos Zoulas. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Christos Zoulas.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * $FreeBSD: src/lib/libutil/fparseln.c,v 1.2 1999/12/29 17:50:33 peter Exp $
36 #include <sys/cdefs.h>
38 #include <sys/types.h>
46 static int isescaped (const char *, const char *, int);
49 * Return true if the character in *p that belongs to a string
50 * that starts in *sp, is escaped by the escape character esc.
61 _DIAGASSERT(sp != NULL);
62 _DIAGASSERT(p != NULL);
65 /* No escape character */
69 /* Count the number of escape characters that precede ours */
70 for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
73 /* Return true if odd number of escape characters */
79 * Read a line from a file parsing continuations ending in \
80 * and eliminating trailing newlines, or comments starting with
84 fparseln(fp, size, lineno, str, flags)
91 static const char dstr[3] = { '\\', '\\', '#' };
97 char esc, con, nl, com;
100 _DIAGASSERT(fp != NULL);
114 * XXX: it would be cool to be able to specify the newline character,
115 * but unfortunately, fgetln does not let us
125 if ((ptr = fgetln(fp, &s)) == NULL)
128 if (s && com) { /* Check and eliminate comments */
129 for (cp = ptr; cp < ptr + s; cp++)
130 if (*cp == com && !isescaped(ptr, cp, esc)) {
132 cnt = s == 0 && buf == NULL;
137 if (s && nl) { /* Check and eliminate newlines */
141 s--; /* forget newline */
144 if (s && con) { /* Check and eliminate continuations */
147 if (*cp == con && !isescaped(ptr, cp, esc)) {
148 s--; /* forget escape */
153 if (s == 0 && buf != NULL)
156 if ((cp = realloc(buf, len + s + 1)) == NULL) {
162 (void) memcpy(buf + len, ptr, s);
167 if ((flags & FPARSELN_UNESCALL) != 0 && esc && buf != NULL &&
168 strchr(buf, esc) != NULL) {
170 while (cp[0] != '\0') {
173 while (cp[0] != '\0' && cp[0] != esc)
175 if (cp[0] == '\0' || cp[1] == '\0')
180 skipesc += (flags & FPARSELN_UNESCCOMM);
182 skipesc += (flags & FPARSELN_UNESCCONT);
184 skipesc += (flags & FPARSELN_UNESCESC);
185 if (cp[1] != com && cp[1] != con && cp[1] != esc)
186 skipesc = (flags & FPARSELN_UNESCREST);
205 int main (int, char **);
216 while ((ptr = fparseln(stdin, &size, &line, NULL,
217 FPARSELN_UNESCALL)) != NULL)
218 printf("line %d (%d) |%s|\n", line, size, ptr);
228 line 4 \# Not comment \\\\