__aeabi_ldivmod: fix sign logic
[minix.git] / lib / libc / stdio / fparseln.c
blob902f6feb4d9026ff183363c6cfdfa21c28e576b9
1 /* $NetBSD: fparseln.c,v 1.10 2009/10/21 01:07:45 snj Exp $ */
3 /*
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
8 * are met:
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/cdefs.h>
28 #if defined(LIBC_SCCS) && !defined(lint)
29 __RCSID("$NetBSD: fparseln.c,v 1.10 2009/10/21 01:07:45 snj Exp $");
30 #endif /* LIBC_SCCS and not lint */
32 #include "namespace.h"
34 #include <assert.h>
35 #include <errno.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
40 #ifdef __weak_alias
41 __weak_alias(fparseln,_fparseln)
42 #endif
44 #if ! HAVE_FPARSELN || BROKEN_FPARSELN
46 #ifndef HAVE_NBTOOL_CONFIG_H
47 #include "reentrant.h"
48 #include "local.h"
49 #else
50 #define FLOCKFILE(fp)
51 #define FUNLOCKFILE(fp)
52 #endif
54 #if defined(_REENTRANT) && !HAVE_NBTOOL_CONFIG_H
55 #define __fgetln(f, l) __fgetstr(f, l, '\n')
56 #else
57 #define __fgetln(f, l) fgetln(f, l)
58 #endif
60 static int isescaped(const char *, const char *, int);
62 /* isescaped():
63 * Return true if the character in *p that belongs to a string
64 * that starts in *sp, is escaped by the escape character esc.
66 static int
67 isescaped(const char *sp, const char *p, int esc)
69 const char *cp;
70 size_t ne;
72 _DIAGASSERT(sp != NULL);
73 _DIAGASSERT(p != NULL);
75 /* No escape character */
76 if (esc == '\0')
77 return 0;
79 /* Count the number of escape characters that precede ours */
80 for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
81 continue;
83 /* Return true if odd number of escape characters */
84 return (ne & 1) != 0;
88 /* fparseln():
89 * Read a line from a file parsing continuations ending in \
90 * and eliminating trailing newlines, or comments starting with
91 * the comment char.
93 char *
94 fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
96 static const char dstr[3] = { '\\', '\\', '#' };
98 size_t s, len;
99 char *buf;
100 char *ptr, *cp;
101 int cnt;
102 char esc, con, nl, com;
104 _DIAGASSERT(fp != NULL);
106 len = 0;
107 buf = NULL;
108 cnt = 1;
110 if (str == NULL)
111 str = dstr;
113 esc = str[0];
114 con = str[1];
115 com = str[2];
117 * XXX: it would be cool to be able to specify the newline character,
118 * but unfortunately, fgetln does not let us
120 nl = '\n';
122 FLOCKFILE(fp);
124 while (cnt) {
125 cnt = 0;
127 if (lineno)
128 (*lineno)++;
130 if ((ptr = __fgetln(fp, &s)) == NULL)
131 break;
133 if (s && com) { /* Check and eliminate comments */
134 for (cp = ptr; cp < ptr + s; cp++)
135 if (*cp == com && !isescaped(ptr, cp, esc)) {
136 s = cp - ptr;
137 cnt = s == 0 && buf == NULL;
138 break;
142 if (s && nl) { /* Check and eliminate newlines */
143 cp = &ptr[s - 1];
145 if (*cp == nl)
146 s--; /* forget newline */
149 if (s && con) { /* Check and eliminate continuations */
150 cp = &ptr[s - 1];
152 if (*cp == con && !isescaped(ptr, cp, esc)) {
153 s--; /* forget continuation char */
154 cnt = 1;
158 if (s == 0) {
160 * nothing to add, skip realloc except in case
161 * we need a minimal buf to return an empty line
163 if (cnt || buf != NULL)
164 continue;
167 if ((cp = realloc(buf, len + s + 1)) == NULL) {
168 FUNLOCKFILE(fp);
169 free(buf);
170 return NULL;
172 buf = cp;
174 (void) memcpy(buf + len, ptr, s);
175 len += s;
176 buf[len] = '\0';
179 FUNLOCKFILE(fp);
181 if ((flags & FPARSELN_UNESCALL) != 0 && esc && buf != NULL &&
182 strchr(buf, esc) != NULL) {
183 ptr = cp = buf;
184 while (cp[0] != '\0') {
185 int skipesc;
187 while (cp[0] != '\0' && cp[0] != esc)
188 *ptr++ = *cp++;
189 if (cp[0] == '\0' || cp[1] == '\0')
190 break;
192 skipesc = 0;
193 if (cp[1] == com)
194 skipesc += (flags & FPARSELN_UNESCCOMM);
195 if (cp[1] == con)
196 skipesc += (flags & FPARSELN_UNESCCONT);
197 if (cp[1] == esc)
198 skipesc += (flags & FPARSELN_UNESCESC);
199 if (cp[1] != com && cp[1] != con && cp[1] != esc)
200 skipesc = (flags & FPARSELN_UNESCREST);
202 if (skipesc)
203 cp++;
204 else
205 *ptr++ = *cp++;
206 *ptr++ = *cp++;
208 *ptr = '\0';
209 len = strlen(buf);
212 if (size)
213 *size = len;
214 return buf;
217 #ifdef TEST
219 int main(int, char **);
222 main(int argc, char **argv)
224 char *ptr;
225 size_t size, line;
227 line = 0;
228 while ((ptr = fparseln(stdin, &size, &line, NULL,
229 FPARSELN_UNESCALL)) != NULL)
230 printf("line %d (%d) |%s|\n", line, size, ptr);
231 return 0;
236 # This is a test
237 line 1
238 line 2 \
239 line 3 # Comment
240 line 4 \# Not comment \\\\
242 # And a comment \
243 line 5 \\\
244 line 6
248 #endif /* TEST */
249 #endif /* ! HAVE_FPARSELN || BROKEN_FPARSELN */