]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Stdio/fparseln.c
1 /* $NetBSD: fparseln.c,v 1.5 2004/06/20 22:20:15 jmc 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.
31 #include <LibConfig.h>
32 #include <sys/EfiCdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 __RCSID("$NetBSD: fparseln.c,v 1.5 2004/06/20 22:20:15 jmc Exp $");
35 #endif /* LIBC_SCCS and not lint */
37 #include "namespace.h"
46 __weak_alias(fparseln
,_fparseln
)
51 #ifndef HAVE_NBTOOL_CONFIG_H
52 #include "reentrant.h"
56 #define FUNLOCKFILE(fp)
59 #if defined(_REENTRANT) && !HAVE_NBTOOL_CONFIG_H
60 #define __fgetln(f, l) __fgetstr(f, l, '\n')
62 #define __fgetln(f, l) fgetln(f, l)
65 static int isescaped(const char *, const char *, int);
68 * Return true if the character in *p that belongs to a string
69 * that starts in *sp, is escaped by the escape character esc.
72 isescaped(const char *sp
, const char *p
, int esc
)
77 _DIAGASSERT(sp
!= NULL
);
78 _DIAGASSERT(p
!= NULL
);
80 /* No escape character */
84 /* Count the number of escape characters that precede ours */
85 for (ne
= 0, cp
= p
; --cp
>= sp
&& *cp
== esc
; ne
++)
88 /* Return true if odd number of escape characters */
94 * Read a line from a file parsing continuations ending in \
95 * and eliminating trailing newlines, or comments starting with
99 fparseln(FILE *fp
, size_t *size
, size_t *lineno
, const char str
[3], int flags
)
101 static const char dstr
[3] = { '\\', '\\', '#' };
107 char esc
, con
, nl
, com
;
109 _DIAGASSERT(fp
!= NULL
);
122 * XXX: it would be cool to be able to specify the newline character,
123 * but unfortunately, fgetln does not let us
135 if ((ptr
= __fgetln(fp
, &s
)) == NULL
)
138 if (s
&& com
) { /* Check and eliminate comments */
139 for (cp
= ptr
; cp
< ptr
+ s
; cp
++)
140 if (*cp
== com
&& !isescaped(ptr
, cp
, esc
)) {
142 cnt
= s
== 0 && buf
== NULL
;
147 if (s
&& nl
) { /* Check and eliminate newlines */
151 s
--; /* forget newline */
154 if (s
&& con
) { /* Check and eliminate continuations */
157 if (*cp
== con
&& !isescaped(ptr
, cp
, esc
)) {
158 s
--; /* forget escape */
163 if (s
== 0 && buf
!= NULL
)
166 if ((cp
= realloc(buf
, len
+ s
+ 1)) == NULL
) {
173 (void) memcpy(buf
+ len
, ptr
, s
);
180 if ((flags
& FPARSELN_UNESCALL
) != 0 && esc
&& buf
!= NULL
&&
181 strchr(buf
, esc
) != NULL
) {
183 while (cp
[0] != '\0') {
186 while (cp
[0] != '\0' && cp
[0] != esc
)
188 if (cp
[0] == '\0' || cp
[1] == '\0')
193 skipesc
+= (flags
& FPARSELN_UNESCCOMM
);
195 skipesc
+= (flags
& FPARSELN_UNESCCONT
);
197 skipesc
+= (flags
& FPARSELN_UNESCESC
);
198 if (cp
[1] != com
&& cp
[1] != con
&& cp
[1] != esc
)
199 skipesc
= (flags
& FPARSELN_UNESCREST
);
218 int main(int, char **);
221 main(int argc
, char **argv
)
227 while ((ptr
= fparseln(stdin
, &size
, &line
, NULL
,
228 FPARSELN_UNESCALL
)) != NULL
)
229 printf("line %d (%d) |%s|\n", line
, size
, ptr
);
239 line 4 \# Not comment \\\\
248 #endif /* ! HAVE_FPARSELN */