rpc.statd(8): Fix 2 cases where syslog() was passed too many args.
[dragonfly.git] / lib / libutil / fparseln.c
blob238c08f341b9460901a31ffef9830aaf780b7679
1 /* $NetBSD: fparseln.c,v 1.9 1999/09/20 04:48:06 lukem Exp $ */
2 /* $FreeBSD: src/lib/libutil/fparseln.c,v 1.2 1999/12/29 17:50:33 peter Exp $ */
3 /* $DragonFly: src/lib/libutil/fparseln.c,v 1.6 2005/03/16 06:35:48 cpressey Exp $ */
5 /*
6 * Copyright (c) 1997 Christos Zoulas. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Christos Zoulas.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * $FreeBSD: src/lib/libutil/fparseln.c,v 1.2 1999/12/29 17:50:33 peter Exp $
36 #include <sys/types.h>
38 #include <assert.h>
39 #include <errno.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
44 #include "libutil.h"
46 static int isescaped (const char *, const char *, int);
48 /* isescaped():
49 * Return true if the character in *p that belongs to a string
50 * that starts in *sp, is escaped by the escape character esc.
52 static int
53 isescaped(const char *sp, const char *p, int esc)
55 const char *cp;
56 size_t ne;
58 _DIAGASSERT(sp != NULL);
59 _DIAGASSERT(p != NULL);
61 /* No escape character */
62 if (esc == '\0')
63 return 1;
65 /* Count the number of escape characters that precede ours */
66 for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
67 continue;
69 /* Return true if odd number of escape characters */
70 return (ne & 1) != 0;
74 /* fparseln():
75 * Read a line from a file parsing continuations ending in \
76 * and eliminating trailing newlines, or comments starting with
77 * the comment char.
79 char *
80 fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
82 static const char dstr[3] = { '\\', '\\', '#' };
84 size_t s, len;
85 char *buf;
86 char *ptr, *cp;
87 int cnt;
88 char esc, con, nl, com;
90 _DIAGASSERT(fp != NULL);
92 len = 0;
93 buf = NULL;
94 cnt = 1;
96 if (str == NULL)
97 str = dstr;
99 esc = str[0];
100 con = str[1];
101 com = str[2];
103 * XXX: it would be cool to be able to specify the newline character,
104 * but unfortunately, fgetln does not let us
106 nl = '\n';
108 while (cnt) {
109 cnt = 0;
111 if (lineno)
112 (*lineno)++;
114 if ((ptr = fgetln(fp, &s)) == NULL)
115 break;
117 if (s && com) { /* Check and eliminate comments */
118 for (cp = ptr; cp < ptr + s; cp++)
119 if (*cp == com && !isescaped(ptr, cp, esc)) {
120 s = cp - ptr;
121 cnt = s == 0 && buf == NULL;
122 break;
126 if (s && nl) { /* Check and eliminate newlines */
127 cp = &ptr[s - 1];
129 if (*cp == nl)
130 s--; /* forget newline */
133 if (s && con) { /* Check and eliminate continuations */
134 cp = &ptr[s - 1];
136 if (*cp == con && !isescaped(ptr, cp, esc)) {
137 s--; /* forget escape */
138 cnt = 1;
142 if (s == 0 && buf != NULL)
143 continue;
145 if ((cp = realloc(buf, len + s + 1)) == NULL) {
146 free(buf);
147 return NULL;
149 buf = cp;
151 (void) memcpy(buf + len, ptr, s);
152 len += s;
153 buf[len] = '\0';
156 if ((flags & FPARSELN_UNESCALL) != 0 && esc && buf != NULL &&
157 strchr(buf, esc) != NULL) {
158 ptr = cp = buf;
159 while (cp[0] != '\0') {
160 int skipesc;
162 while (cp[0] != '\0' && cp[0] != esc)
163 *ptr++ = *cp++;
164 if (cp[0] == '\0' || cp[1] == '\0')
165 break;
167 skipesc = 0;
168 if (cp[1] == com)
169 skipesc += (flags & FPARSELN_UNESCCOMM);
170 if (cp[1] == con)
171 skipesc += (flags & FPARSELN_UNESCCONT);
172 if (cp[1] == esc)
173 skipesc += (flags & FPARSELN_UNESCESC);
174 if (cp[1] != com && cp[1] != con && cp[1] != esc)
175 skipesc = (flags & FPARSELN_UNESCREST);
177 if (skipesc)
178 cp++;
179 else
180 *ptr++ = *cp++;
181 *ptr++ = *cp++;
183 *ptr = '\0';
184 len = strlen(buf);
187 if (size)
188 *size = len;
189 return buf;
192 #ifdef TEST
195 main(int argc, char **argv)
197 char *ptr;
198 size_t size, line;
200 line = 0;
201 while ((ptr = fparseln(stdin, &size, &line, NULL,
202 FPARSELN_UNESCALL)) != NULL)
203 printf("line %d (%d) |%s|\n", line, size, ptr);
204 return 0;
209 # This is a test
210 line 1
211 line 2 \
212 line 3 # Comment
213 line 4 \# Not comment \\\\
215 # And a comment \
216 line 5 \\\
217 line 6
221 #endif /* TEST */