]>
git.saurik.com Git - apple/libc.git/blob - util/fparseln.c
1 /* $NetBSD: fparseln.c,v 1.9 1999/09/20 04:48:06 lukem 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.
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.
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.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD: src/lib/libutil/fparseln.c,v 1.5 2002/03/21 23:52:49 obrien Exp $");
35 #include <sys/types.h>
43 static int isescaped(const char *, const char *, int);
46 * Return true if the character in *p that belongs to a string
47 * that starts in *sp, is escaped by the escape character esc.
58 _DIAGASSERT(sp
!= NULL
);
59 _DIAGASSERT(p
!= NULL
);
62 /* No escape character */
66 /* Count the number of escape characters that precede ours */
67 for (ne
= 0, cp
= p
; --cp
>= sp
&& *cp
== esc
; ne
++)
70 /* Return true if odd number of escape characters */
76 * Read a line from a file parsing continuations ending in \
77 * and eliminating trailing newlines, or comments starting with
81 fparseln(fp
, size
, lineno
, str
, flags
)
88 static const char dstr
[3] = { '\\', '\\', '#' };
94 char esc
, con
, nl
, com
;
97 _DIAGASSERT(fp
!= NULL
);
111 * XXX: it would be cool to be able to specify the newline character,
112 * but unfortunately, fgetln does not let us
122 if ((ptr
= fgetln(fp
, &s
)) == NULL
)
125 if (s
&& com
) { /* Check and eliminate comments */
126 for (cp
= ptr
; cp
< ptr
+ s
; cp
++)
127 if (*cp
== com
&& !isescaped(ptr
, cp
, esc
)) {
129 cnt
= s
== 0 && buf
== NULL
;
134 if (s
&& nl
) { /* Check and eliminate newlines */
138 s
--; /* forget newline */
141 if (s
&& con
) { /* Check and eliminate continuations */
144 if (*cp
== con
&& !isescaped(ptr
, cp
, esc
)) {
145 s
--; /* forget escape */
150 if (s
== 0 && buf
!= NULL
)
153 if ((cp
= realloc(buf
, len
+ s
+ 1)) == NULL
) {
159 (void) memcpy(buf
+ len
, ptr
, s
);
164 if ((flags
& FPARSELN_UNESCALL
) != 0 && esc
&& buf
!= NULL
&&
165 strchr(buf
, esc
) != NULL
) {
167 while (cp
[0] != '\0') {
170 while (cp
[0] != '\0' && cp
[0] != esc
)
172 if (cp
[0] == '\0' || cp
[1] == '\0')
177 skipesc
+= (flags
& FPARSELN_UNESCCOMM
);
179 skipesc
+= (flags
& FPARSELN_UNESCCONT
);
181 skipesc
+= (flags
& FPARSELN_UNESCESC
);
182 if (cp
[1] != com
&& cp
[1] != con
&& cp
[1] != esc
)
183 skipesc
= (flags
& FPARSELN_UNESCREST
);
202 int main(int, char **);
213 while ((ptr
= fparseln(stdin
, &size
, &line
, NULL
,
214 FPARSELN_UNESCALL
)) != NULL
)
215 printf("line %d (%d) |%s|\n", line
, size
, ptr
);
225 line 4 \# Not comment \\\\