fparseln.c revision 54820
1/*	$NetBSD: fparseln.c,v 1.9 1999/09/20 04:48:06 lukem Exp $	*/
2
3/*
4 * Copyright (c) 1997 Christos Zoulas.  All rights reserved.
5 *
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.
14 * 3. All advertising materials mentioning features or use of this software
15 *    must display the following acknowledgement:
16 *	This product includes software developed by Christos Zoulas.
17 * 4. The name of the author may not be used to endorse or promote products
18 *    derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33#if defined(LIBC_SCCS) && !defined(lint)
34__RCSID("$NetBSD: fparseln.c,v 1.9 1999/09/20 04:48:06 lukem Exp $");
35#endif
36
37#include <assert.h>
38#include <errno.h>
39#include <stdio.h>
40#include <string.h>
41#include <stdlib.h>
42#include <util.h>
43
44static int isescaped __P((const char *, const char *, int));
45
46/* isescaped():
47 *	Return true if the character in *p that belongs to a string
48 *	that starts in *sp, is escaped by the escape character esc.
49 */
50static int
51isescaped(sp, p, esc)
52	const char *sp, *p;
53	int esc;
54{
55	const char     *cp;
56	size_t		ne;
57
58	_DIAGASSERT(sp != NULL);
59	_DIAGASSERT(p != NULL);
60
61	/* No escape character */
62	if (esc == '\0')
63		return 1;
64
65	/* Count the number of escape characters that precede ours */
66	for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
67		continue;
68
69	/* Return true if odd number of escape characters */
70	return (ne & 1) != 0;
71}
72
73
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.
78 */
79char *
80fparseln(fp, size, lineno, str, flags)
81	FILE		*fp;
82	size_t		*size;
83	size_t		*lineno;
84	const char	 str[3];
85	int		 flags;
86{
87	static const char dstr[3] = { '\\', '\\', '#' };
88
89	size_t	s, len;
90	char   *buf;
91	char   *ptr, *cp;
92	int	cnt;
93	char	esc, con, nl, com;
94
95	_DIAGASSERT(fp != NULL);
96
97	len = 0;
98	buf = NULL;
99	cnt = 1;
100
101	if (str == NULL)
102		str = dstr;
103
104	esc = str[0];
105	con = str[1];
106	com = str[2];
107	/*
108	 * XXX: it would be cool to be able to specify the newline character,
109	 * but unfortunately, fgetln does not let us
110	 */
111	nl  = '\n';
112
113	while (cnt) {
114		cnt = 0;
115
116		if (lineno)
117			(*lineno)++;
118
119		if ((ptr = fgetln(fp, &s)) == NULL)
120			break;
121
122		if (s && com) {		/* Check and eliminate comments */
123			for (cp = ptr; cp < ptr + s; cp++)
124				if (*cp == com && !isescaped(ptr, cp, esc)) {
125					s = cp - ptr;
126					cnt = s == 0 && buf == NULL;
127					break;
128				}
129		}
130
131		if (s && nl) { 		/* Check and eliminate newlines */
132			cp = &ptr[s - 1];
133
134			if (*cp == nl)
135				s--;	/* forget newline */
136		}
137
138		if (s && con) {		/* Check and eliminate continuations */
139			cp = &ptr[s - 1];
140
141			if (*cp == con && !isescaped(ptr, cp, esc)) {
142				s--;	/* forget escape */
143				cnt = 1;
144			}
145		}
146
147		if (s == 0 && buf != NULL)
148			continue;
149
150		if ((cp = realloc(buf, len + s + 1)) == NULL) {
151			free(buf);
152			return NULL;
153		}
154		buf = cp;
155
156		(void) memcpy(buf + len, ptr, s);
157		len += s;
158		buf[len] = '\0';
159	}
160
161	if ((flags & FPARSELN_UNESCALL) != 0 && esc && buf != NULL &&
162	    strchr(buf, esc) != NULL) {
163		ptr = cp = buf;
164		while (cp[0] != '\0') {
165			int skipesc;
166
167			while (cp[0] != '\0' && cp[0] != esc)
168				*ptr++ = *cp++;
169			if (cp[0] == '\0' || cp[1] == '\0')
170				break;
171
172			skipesc = 0;
173			if (cp[1] == com)
174				skipesc += (flags & FPARSELN_UNESCCOMM);
175			if (cp[1] == con)
176				skipesc += (flags & FPARSELN_UNESCCONT);
177			if (cp[1] == esc)
178				skipesc += (flags & FPARSELN_UNESCESC);
179			if (cp[1] != com && cp[1] != con && cp[1] != esc)
180				skipesc = (flags & FPARSELN_UNESCREST);
181
182			if (skipesc)
183				cp++;
184			else
185				*ptr++ = *cp++;
186			*ptr++ = *cp++;
187		}
188		*ptr = '\0';
189		len = strlen(buf);
190	}
191
192	if (size)
193		*size = len;
194	return buf;
195}
196
197#ifdef TEST
198
199int main __P((int, char **));
200
201int
202main(argc, argv)
203	int argc;
204	char **argv;
205{
206	char   *ptr;
207	size_t	size, line;
208
209	line = 0;
210	while ((ptr = fparseln(stdin, &size, &line, NULL,
211	    FPARSELN_UNESCALL)) != NULL)
212		printf("line %d (%d) |%s|\n", line, size, ptr);
213	return 0;
214}
215
216/*
217
218# This is a test
219line 1
220line 2 \
221line 3 # Comment
222line 4 \# Not comment \\\\
223
224# And a comment \
225line 5 \\\
226line 6
227
228*/
229
230#endif /* TEST */
231