1 /* $NetBSD: fparseln.c,v 1.10 2009/10/21 01:07:45 snj Exp $ */
4 * Copyright (c) 1997 Christos Zoulas. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/types.h>
35 static int isescaped(const char *, const char *, int);
38 * Return true if the character in *p that belongs to a string
39 * that starts in *sp, is escaped by the escape character esc.
42 isescaped(const char *sp, const char *p, int esc)
48 _DIAGASSERT(sp != NULL);
49 _DIAGASSERT(p != NULL);
52 /* No escape character */
56 /* Count the number of escape characters that precede ours */
57 for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
60 /* Return true if odd number of escape characters */
66 * Read a line from a file parsing continuations ending in \
67 * and eliminating trailing newlines, or comments starting with
71 fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
73 static const char dstr[3] = { '\\', '\\', '#' };
79 char esc, con, nl, com;
82 _DIAGASSERT(fp != NULL);
96 * XXX: it would be cool to be able to specify the newline character,
97 * but unfortunately, fgetln does not let us
107 if ((ptr = fgetln(fp, &s)) == NULL)
110 if (s && com) { /* Check and eliminate comments */
111 for (cp = ptr; cp < ptr + s; cp++)
112 if (*cp == com && !isescaped(ptr, cp, esc)) {
114 cnt = s == 0 && buf == NULL;
119 if (s && nl) { /* Check and eliminate newlines */
123 s--; /* forget newline */
126 if (s && con) { /* Check and eliminate continuations */
129 if (*cp == con && !isescaped(ptr, cp, esc)) {
130 s--; /* forget continuation char */
137 * nothing to add, skip realloc except in case
138 * we need a minimal buf to return an empty line
140 if (cnt || buf != NULL)
144 if ((cp = realloc(buf, len + s + 1)) == NULL) {
150 (void) memcpy(buf + len, ptr, s);
155 if ((flags & FPARSELN_UNESCALL) != 0 && esc && buf != NULL &&
156 strchr(buf, esc) != NULL) {
158 while (cp[0] != '\0') {
161 while (cp[0] != '\0' && cp[0] != esc)
163 if (cp[0] == '\0' || cp[1] == '\0')
168 skipesc += (flags & FPARSELN_UNESCCOMM);
170 skipesc += (flags & FPARSELN_UNESCCONT);
172 skipesc += (flags & FPARSELN_UNESCESC);
173 if (cp[1] != com && cp[1] != con && cp[1] != esc)
174 skipesc = (flags & FPARSELN_UNESCREST);
194 main(int argc, char *argv[])
200 while ((ptr = fparseln(stdin, &size, &line, NULL,
201 FPARSELN_UNESCALL)) != NULL)
202 printf("line %d (%d) |%s|\n", line, size, ptr);
212 line 4 \# Not comment \\\\