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