]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Stdio/fparseln.c
2 Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
3 This program and the accompanying materials are licensed and made available
4 under the terms and conditions of the BSD License that accompanies this
5 distribution. The full text of the license may be found at
6 http://opensource.org/licenses/bsd-license.
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11 * Copyright (c) 1997 Christos Zoulas. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by Christos Zoulas.
24 * 4. The name of the author may not be used to endorse or promote products
25 * derived from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 NetBSD: fparseln.c,v 1.5 2004/06/20 22:20:15 jmc Exp
40 #include <LibConfig.h>
41 #include <sys/EfiCdefs.h>
43 #include "namespace.h"
52 __weak_alias(fparseln
,_fparseln
)
57 #ifndef HAVE_NBTOOL_CONFIG_H
58 #include "reentrant.h"
62 #define FUNLOCKFILE(fp)
65 #if defined(_REENTRANT) && !HAVE_NBTOOL_CONFIG_H
66 #define __fgetln(f, l) __fgetstr(f, l, '\n')
68 #define __fgetln(f, l) fgetln(f, l)
71 static int isescaped(const char *, const char *, int);
74 * Return true if the character in *p that belongs to a string
75 * that starts in *sp, is escaped by the escape character esc.
78 isescaped(const char *sp
, const char *p
, int esc
)
83 _DIAGASSERT(sp
!= NULL
);
84 _DIAGASSERT(p
!= NULL
);
86 /* No escape character */
90 /* Count the number of escape characters that precede ours */
91 for (ne
= 0, cp
= p
; --cp
>= sp
&& *cp
== esc
; ne
++)
94 /* Return true if odd number of escape characters */
100 * Read a line from a file parsing continuations ending in \
101 * and eliminating trailing newlines, or comments starting with
105 fparseln(FILE *fp
, size_t *size
, size_t *lineno
, const char str
[3], int flags
)
107 static const char dstr
[3] = { '\\', '\\', '#' };
113 char esc
, con
, nl
, com
;
115 _DIAGASSERT(fp
!= NULL
);
132 * XXX: it would be cool to be able to specify the newline character,
133 * but unfortunately, fgetln does not let us
145 if ((ptr
= __fgetln(fp
, &s
)) == NULL
)
148 if (s
&& com
) { /* Check and eliminate comments */
149 for (cp
= ptr
; cp
< ptr
+ s
; cp
++)
150 if (*cp
== com
&& !isescaped(ptr
, cp
, esc
)) {
152 cnt
= s
== 0 && buf
== NULL
;
157 if (s
&& nl
) { /* Check and eliminate newlines */
161 s
--; /* forget newline */
164 if (s
&& con
) { /* Check and eliminate continuations */
167 if (*cp
== con
&& !isescaped(ptr
, cp
, esc
)) {
168 s
--; /* forget escape */
173 if (s
== 0 && buf
!= NULL
)
176 if ((cp
= realloc(buf
, len
+ s
+ 1)) == NULL
) {
183 (void) memcpy(buf
+ len
, ptr
, s
);
190 if ((flags
& FPARSELN_UNESCALL
) != 0 && esc
&& buf
!= NULL
&&
191 strchr(buf
, esc
) != NULL
) {
193 while (cp
[0] != '\0') {
196 while (cp
[0] != '\0' && cp
[0] != esc
)
198 if (cp
[0] == '\0' || cp
[1] == '\0')
203 skipesc
+= (flags
& FPARSELN_UNESCCOMM
);
205 skipesc
+= (flags
& FPARSELN_UNESCCONT
);
207 skipesc
+= (flags
& FPARSELN_UNESCESC
);
208 if (cp
[1] != com
&& cp
[1] != con
&& cp
[1] != esc
)
209 skipesc
= (flags
& FPARSELN_UNESCREST
);
228 int main(int, char **);
231 main(int argc
, char **argv
)
237 while ((ptr
= fparseln(stdin
, &size
, &line
, NULL
,
238 FPARSELN_UNESCALL
)) != NULL
)
239 printf("line %d (%d) |%s|\n", line
, size
, ptr
);
249 line 4 \# Not comment \\\\
258 #endif /* ! HAVE_FPARSELN */