xref: /minix3/lib/libc/stdio/fparseln.c (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2fe8fb19SBen Gras /*	$NetBSD: fparseln.c,v 1.10 2009/10/21 01:07:45 snj Exp $	*/
2*2fe8fb19SBen Gras 
3*2fe8fb19SBen Gras /*
4*2fe8fb19SBen Gras  * Copyright (c) 1997 Christos Zoulas.  All rights reserved.
5*2fe8fb19SBen Gras  *
6*2fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
7*2fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
8*2fe8fb19SBen Gras  * are met:
9*2fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
10*2fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
11*2fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
12*2fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
13*2fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
14*2fe8fb19SBen Gras  *
15*2fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*2fe8fb19SBen Gras  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*2fe8fb19SBen Gras  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*2fe8fb19SBen Gras  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*2fe8fb19SBen Gras  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*2fe8fb19SBen Gras  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*2fe8fb19SBen Gras  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*2fe8fb19SBen Gras  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*2fe8fb19SBen Gras  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*2fe8fb19SBen Gras  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*2fe8fb19SBen Gras  */
26*2fe8fb19SBen Gras 
27*2fe8fb19SBen Gras #include <sys/cdefs.h>
28*2fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
29*2fe8fb19SBen Gras __RCSID("$NetBSD: fparseln.c,v 1.10 2009/10/21 01:07:45 snj Exp $");
30*2fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
31*2fe8fb19SBen Gras 
32*2fe8fb19SBen Gras #include "namespace.h"
33*2fe8fb19SBen Gras 
34*2fe8fb19SBen Gras #include <assert.h>
35*2fe8fb19SBen Gras #include <errno.h>
36*2fe8fb19SBen Gras #include <stdio.h>
37*2fe8fb19SBen Gras #include <string.h>
38*2fe8fb19SBen Gras #include <stdlib.h>
39*2fe8fb19SBen Gras 
40*2fe8fb19SBen Gras #ifdef __weak_alias
41*2fe8fb19SBen Gras __weak_alias(fparseln,_fparseln)
42*2fe8fb19SBen Gras #endif
43*2fe8fb19SBen Gras 
44*2fe8fb19SBen Gras #if ! HAVE_FPARSELN || BROKEN_FPARSELN
45*2fe8fb19SBen Gras 
46*2fe8fb19SBen Gras #ifndef HAVE_NBTOOL_CONFIG_H
47*2fe8fb19SBen Gras #include "reentrant.h"
48*2fe8fb19SBen Gras #include "local.h"
49*2fe8fb19SBen Gras #else
50*2fe8fb19SBen Gras #define FLOCKFILE(fp)
51*2fe8fb19SBen Gras #define FUNLOCKFILE(fp)
52*2fe8fb19SBen Gras #endif
53*2fe8fb19SBen Gras 
54*2fe8fb19SBen Gras #if defined(_REENTRANT) && !HAVE_NBTOOL_CONFIG_H
55*2fe8fb19SBen Gras #define __fgetln(f, l) __fgetstr(f, l, '\n')
56*2fe8fb19SBen Gras #else
57*2fe8fb19SBen Gras #define __fgetln(f, l) fgetln(f, l)
58*2fe8fb19SBen Gras #endif
59*2fe8fb19SBen Gras 
60*2fe8fb19SBen Gras static int isescaped(const char *, const char *, int);
61*2fe8fb19SBen Gras 
62*2fe8fb19SBen Gras /* isescaped():
63*2fe8fb19SBen Gras  *	Return true if the character in *p that belongs to a string
64*2fe8fb19SBen Gras  *	that starts in *sp, is escaped by the escape character esc.
65*2fe8fb19SBen Gras  */
66*2fe8fb19SBen Gras static int
isescaped(const char * sp,const char * p,int esc)67*2fe8fb19SBen Gras isescaped(const char *sp, const char *p, int esc)
68*2fe8fb19SBen Gras {
69*2fe8fb19SBen Gras 	const char     *cp;
70*2fe8fb19SBen Gras 	size_t		ne;
71*2fe8fb19SBen Gras 
72*2fe8fb19SBen Gras 	_DIAGASSERT(sp != NULL);
73*2fe8fb19SBen Gras 	_DIAGASSERT(p != NULL);
74*2fe8fb19SBen Gras 
75*2fe8fb19SBen Gras 	/* No escape character */
76*2fe8fb19SBen Gras 	if (esc == '\0')
77*2fe8fb19SBen Gras 		return 0;
78*2fe8fb19SBen Gras 
79*2fe8fb19SBen Gras 	/* Count the number of escape characters that precede ours */
80*2fe8fb19SBen Gras 	for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
81*2fe8fb19SBen Gras 		continue;
82*2fe8fb19SBen Gras 
83*2fe8fb19SBen Gras 	/* Return true if odd number of escape characters */
84*2fe8fb19SBen Gras 	return (ne & 1) != 0;
85*2fe8fb19SBen Gras }
86*2fe8fb19SBen Gras 
87*2fe8fb19SBen Gras 
88*2fe8fb19SBen Gras /* fparseln():
89*2fe8fb19SBen Gras  *	Read a line from a file parsing continuations ending in \
90*2fe8fb19SBen Gras  *	and eliminating trailing newlines, or comments starting with
91*2fe8fb19SBen Gras  *	the comment char.
92*2fe8fb19SBen Gras  */
93*2fe8fb19SBen Gras char *
fparseln(FILE * fp,size_t * size,size_t * lineno,const char str[3],int flags)94*2fe8fb19SBen Gras fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
95*2fe8fb19SBen Gras {
96*2fe8fb19SBen Gras 	static const char dstr[3] = { '\\', '\\', '#' };
97*2fe8fb19SBen Gras 
98*2fe8fb19SBen Gras 	size_t	s, len;
99*2fe8fb19SBen Gras 	char   *buf;
100*2fe8fb19SBen Gras 	char   *ptr, *cp;
101*2fe8fb19SBen Gras 	int	cnt;
102*2fe8fb19SBen Gras 	char	esc, con, nl, com;
103*2fe8fb19SBen Gras 
104*2fe8fb19SBen Gras 	_DIAGASSERT(fp != NULL);
105*2fe8fb19SBen Gras 
106*2fe8fb19SBen Gras 	len = 0;
107*2fe8fb19SBen Gras 	buf = NULL;
108*2fe8fb19SBen Gras 	cnt = 1;
109*2fe8fb19SBen Gras 
110*2fe8fb19SBen Gras 	if (str == NULL)
111*2fe8fb19SBen Gras 		str = dstr;
112*2fe8fb19SBen Gras 
113*2fe8fb19SBen Gras 	esc = str[0];
114*2fe8fb19SBen Gras 	con = str[1];
115*2fe8fb19SBen Gras 	com = str[2];
116*2fe8fb19SBen Gras 	/*
117*2fe8fb19SBen Gras 	 * XXX: it would be cool to be able to specify the newline character,
118*2fe8fb19SBen Gras 	 * but unfortunately, fgetln does not let us
119*2fe8fb19SBen Gras 	 */
120*2fe8fb19SBen Gras 	nl  = '\n';
121*2fe8fb19SBen Gras 
122*2fe8fb19SBen Gras 	FLOCKFILE(fp);
123*2fe8fb19SBen Gras 
124*2fe8fb19SBen Gras 	while (cnt) {
125*2fe8fb19SBen Gras 		cnt = 0;
126*2fe8fb19SBen Gras 
127*2fe8fb19SBen Gras 		if (lineno)
128*2fe8fb19SBen Gras 			(*lineno)++;
129*2fe8fb19SBen Gras 
130*2fe8fb19SBen Gras 		if ((ptr = __fgetln(fp, &s)) == NULL)
131*2fe8fb19SBen Gras 			break;
132*2fe8fb19SBen Gras 
133*2fe8fb19SBen Gras 		if (s && com) {		/* Check and eliminate comments */
134*2fe8fb19SBen Gras 			for (cp = ptr; cp < ptr + s; cp++)
135*2fe8fb19SBen Gras 				if (*cp == com && !isescaped(ptr, cp, esc)) {
136*2fe8fb19SBen Gras 					s = cp - ptr;
137*2fe8fb19SBen Gras 					cnt = s == 0 && buf == NULL;
138*2fe8fb19SBen Gras 					break;
139*2fe8fb19SBen Gras 				}
140*2fe8fb19SBen Gras 		}
141*2fe8fb19SBen Gras 
142*2fe8fb19SBen Gras 		if (s && nl) { 		/* Check and eliminate newlines */
143*2fe8fb19SBen Gras 			cp = &ptr[s - 1];
144*2fe8fb19SBen Gras 
145*2fe8fb19SBen Gras 			if (*cp == nl)
146*2fe8fb19SBen Gras 				s--;	/* forget newline */
147*2fe8fb19SBen Gras 		}
148*2fe8fb19SBen Gras 
149*2fe8fb19SBen Gras 		if (s && con) {		/* Check and eliminate continuations */
150*2fe8fb19SBen Gras 			cp = &ptr[s - 1];
151*2fe8fb19SBen Gras 
152*2fe8fb19SBen Gras 			if (*cp == con && !isescaped(ptr, cp, esc)) {
153*2fe8fb19SBen Gras 				s--;	/* forget continuation char */
154*2fe8fb19SBen Gras 				cnt = 1;
155*2fe8fb19SBen Gras 			}
156*2fe8fb19SBen Gras 		}
157*2fe8fb19SBen Gras 
158*2fe8fb19SBen Gras 		if (s == 0) {
159*2fe8fb19SBen Gras 			/*
160*2fe8fb19SBen Gras 			 * nothing to add, skip realloc except in case
161*2fe8fb19SBen Gras 			 * we need a minimal buf to return an empty line
162*2fe8fb19SBen Gras 			 */
163*2fe8fb19SBen Gras 			if (cnt || buf != NULL)
164*2fe8fb19SBen Gras 				continue;
165*2fe8fb19SBen Gras 		}
166*2fe8fb19SBen Gras 
167*2fe8fb19SBen Gras 		if ((cp = realloc(buf, len + s + 1)) == NULL) {
168*2fe8fb19SBen Gras 			FUNLOCKFILE(fp);
169*2fe8fb19SBen Gras 			free(buf);
170*2fe8fb19SBen Gras 			return NULL;
171*2fe8fb19SBen Gras 		}
172*2fe8fb19SBen Gras 		buf = cp;
173*2fe8fb19SBen Gras 
174*2fe8fb19SBen Gras 		(void) memcpy(buf + len, ptr, s);
175*2fe8fb19SBen Gras 		len += s;
176*2fe8fb19SBen Gras 		buf[len] = '\0';
177*2fe8fb19SBen Gras 	}
178*2fe8fb19SBen Gras 
179*2fe8fb19SBen Gras 	FUNLOCKFILE(fp);
180*2fe8fb19SBen Gras 
181*2fe8fb19SBen Gras 	if ((flags & FPARSELN_UNESCALL) != 0 && esc && buf != NULL &&
182*2fe8fb19SBen Gras 	    strchr(buf, esc) != NULL) {
183*2fe8fb19SBen Gras 		ptr = cp = buf;
184*2fe8fb19SBen Gras 		while (cp[0] != '\0') {
185*2fe8fb19SBen Gras 			int skipesc;
186*2fe8fb19SBen Gras 
187*2fe8fb19SBen Gras 			while (cp[0] != '\0' && cp[0] != esc)
188*2fe8fb19SBen Gras 				*ptr++ = *cp++;
189*2fe8fb19SBen Gras 			if (cp[0] == '\0' || cp[1] == '\0')
190*2fe8fb19SBen Gras 				break;
191*2fe8fb19SBen Gras 
192*2fe8fb19SBen Gras 			skipesc = 0;
193*2fe8fb19SBen Gras 			if (cp[1] == com)
194*2fe8fb19SBen Gras 				skipesc += (flags & FPARSELN_UNESCCOMM);
195*2fe8fb19SBen Gras 			if (cp[1] == con)
196*2fe8fb19SBen Gras 				skipesc += (flags & FPARSELN_UNESCCONT);
197*2fe8fb19SBen Gras 			if (cp[1] == esc)
198*2fe8fb19SBen Gras 				skipesc += (flags & FPARSELN_UNESCESC);
199*2fe8fb19SBen Gras 			if (cp[1] != com && cp[1] != con && cp[1] != esc)
200*2fe8fb19SBen Gras 				skipesc = (flags & FPARSELN_UNESCREST);
201*2fe8fb19SBen Gras 
202*2fe8fb19SBen Gras 			if (skipesc)
203*2fe8fb19SBen Gras 				cp++;
204*2fe8fb19SBen Gras 			else
205*2fe8fb19SBen Gras 				*ptr++ = *cp++;
206*2fe8fb19SBen Gras 			*ptr++ = *cp++;
207*2fe8fb19SBen Gras 		}
208*2fe8fb19SBen Gras 		*ptr = '\0';
209*2fe8fb19SBen Gras 		len = strlen(buf);
210*2fe8fb19SBen Gras 	}
211*2fe8fb19SBen Gras 
212*2fe8fb19SBen Gras 	if (size)
213*2fe8fb19SBen Gras 		*size = len;
214*2fe8fb19SBen Gras 	return buf;
215*2fe8fb19SBen Gras }
216*2fe8fb19SBen Gras 
217*2fe8fb19SBen Gras #ifdef TEST
218*2fe8fb19SBen Gras 
219*2fe8fb19SBen Gras int main(int, char **);
220*2fe8fb19SBen Gras 
221*2fe8fb19SBen Gras int
main(int argc,char ** argv)222*2fe8fb19SBen Gras main(int argc, char **argv)
223*2fe8fb19SBen Gras {
224*2fe8fb19SBen Gras 	char   *ptr;
225*2fe8fb19SBen Gras 	size_t	size, line;
226*2fe8fb19SBen Gras 
227*2fe8fb19SBen Gras 	line = 0;
228*2fe8fb19SBen Gras 	while ((ptr = fparseln(stdin, &size, &line, NULL,
229*2fe8fb19SBen Gras 	    FPARSELN_UNESCALL)) != NULL)
230*2fe8fb19SBen Gras 		printf("line %d (%d) |%s|\n", line, size, ptr);
231*2fe8fb19SBen Gras 	return 0;
232*2fe8fb19SBen Gras }
233*2fe8fb19SBen Gras 
234*2fe8fb19SBen Gras /*
235*2fe8fb19SBen Gras 
236*2fe8fb19SBen Gras # This is a test
237*2fe8fb19SBen Gras line 1
238*2fe8fb19SBen Gras line 2 \
239*2fe8fb19SBen Gras line 3 # Comment
240*2fe8fb19SBen Gras line 4 \# Not comment \\\\
241*2fe8fb19SBen Gras 
242*2fe8fb19SBen Gras # And a comment \
243*2fe8fb19SBen Gras line 5 \\\
244*2fe8fb19SBen Gras line 6
245*2fe8fb19SBen Gras 
246*2fe8fb19SBen Gras */
247*2fe8fb19SBen Gras 
248*2fe8fb19SBen Gras #endif /* TEST */
249*2fe8fb19SBen Gras #endif	/* ! HAVE_FPARSELN || BROKEN_FPARSELN */
250