fparseln.c revision 84225
1183593Sjkoshy/*	$NetBSD: fparseln.c,v 1.9 1999/09/20 04:48:06 lukem Exp $	*/
2183593Sjkoshy
3183593Sjkoshy/*
4183593Sjkoshy * Copyright (c) 1997 Christos Zoulas.  All rights reserved.
5183593Sjkoshy *
6183593Sjkoshy * Redistribution and use in source and binary forms, with or without
7183593Sjkoshy * modification, are permitted provided that the following conditions
8183593Sjkoshy * are met:
9183593Sjkoshy * 1. Redistributions of source code must retain the above copyright
10183593Sjkoshy *    notice, this list of conditions and the following disclaimer.
11183593Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright
12231871Sbrueffer *    notice, this list of conditions and the following disclaimer in the
13231871Sbrueffer *    documentation and/or other materials provided with the distribution.
14231871Sbrueffer * 3. All advertising materials mentioning features or use of this software
15231871Sbrueffer *    must display the following acknowledgement:
16231871Sbrueffer *	This product includes software developed by Christos Zoulas.
17231871Sbrueffer * 4. The name of the author may not be used to endorse or promote products
18231871Sbrueffer *    derived from this software without specific prior written permission.
19231871Sbrueffer *
20231871Sbrueffer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21231871Sbrueffer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22231871Sbrueffer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23183593Sjkoshy * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24183593Sjkoshy * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25183593Sjkoshy * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26184995Sjkoshy * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27206622Suqs * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28183593Sjkoshy * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29183593Sjkoshy * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30183593Sjkoshy */
31183593Sjkoshy
32183593Sjkoshy#include <sys/cdefs.h>
33183593Sjkoshy__FBSDID("$FreeBSD: head/lib/libutil/fparseln.c 84225 2001-09-30 22:35:07Z dillon $");
34183593Sjkoshy
35183593Sjkoshy#include <sys/types.h>
36183593Sjkoshy#include <assert.h>
37183593Sjkoshy#include <errno.h>
38183593Sjkoshy#include <stdio.h>
39183593Sjkoshy#include <string.h>
40183593Sjkoshy#include <stdlib.h>
41183593Sjkoshy#include <libutil.h>
42183593Sjkoshy
43183593Sjkoshystatic int isescaped __P((const char *, const char *, int));
44183593Sjkoshy
45183593Sjkoshy/* isescaped():
46183593Sjkoshy *	Return true if the character in *p that belongs to a string
47183593Sjkoshy *	that starts in *sp, is escaped by the escape character esc.
48183593Sjkoshy */
49183593Sjkoshystatic int
50183593Sjkoshyisescaped(sp, p, esc)
51183593Sjkoshy	const char *sp, *p;
52183593Sjkoshy	int esc;
53183593Sjkoshy{
54183593Sjkoshy	const char     *cp;
55183593Sjkoshy	size_t		ne;
56183593Sjkoshy
57183593Sjkoshy#if 0
58183593Sjkoshy	_DIAGASSERT(sp != NULL);
59183593Sjkoshy	_DIAGASSERT(p != NULL);
60183593Sjkoshy#endif
61183593Sjkoshy
62183593Sjkoshy	/* No escape character */
63183593Sjkoshy	if (esc == '\0')
64183593Sjkoshy		return 1;
65183593Sjkoshy
66183593Sjkoshy	/* Count the number of escape characters that precede ours */
67183593Sjkoshy	for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
68183593Sjkoshy		continue;
69183593Sjkoshy
70183593Sjkoshy	/* Return true if odd number of escape characters */
71183593Sjkoshy	return (ne & 1) != 0;
72183593Sjkoshy}
73183593Sjkoshy
74183593Sjkoshy
75184995Sjkoshy/* fparseln():
76196449Sjkoshy *	Read a line from a file parsing continuations ending in \
77184995Sjkoshy *	and eliminating trailing newlines, or comments starting with
78183593Sjkoshy *	the comment char.
79183593Sjkoshy */
80183593Sjkoshychar *
81183593Sjkoshyfparseln(fp, size, lineno, str, flags)
82183593Sjkoshy	FILE		*fp;
83183593Sjkoshy	size_t		*size;
84183593Sjkoshy	size_t		*lineno;
85183593Sjkoshy	const char	 str[3];
86184995Sjkoshy	int		 flags;
87184995Sjkoshy{
88184995Sjkoshy	static const char dstr[3] = { '\\', '\\', '#' };
89184995Sjkoshy
90184995Sjkoshy	size_t	s, len;
91183593Sjkoshy	char   *buf;
92183593Sjkoshy	char   *ptr, *cp;
93183593Sjkoshy	int	cnt;
94183593Sjkoshy	char	esc, con, nl, com;
95183593Sjkoshy
96183593Sjkoshy#if 0
97183593Sjkoshy	_DIAGASSERT(fp != NULL);
98183593Sjkoshy#endif
99183593Sjkoshy
100183593Sjkoshy	len = 0;
101183593Sjkoshy	buf = NULL;
102184891Sjkoshy	cnt = 1;
103183593Sjkoshy
104183593Sjkoshy	if (str == NULL)
105184891Sjkoshy		str = dstr;
106183593Sjkoshy
107183593Sjkoshy	esc = str[0];
108184891Sjkoshy	con = str[1];
109183593Sjkoshy	com = str[2];
110183593Sjkoshy	/*
111183593Sjkoshy	 * XXX: it would be cool to be able to specify the newline character,
112184995Sjkoshy	 * but unfortunately, fgetln does not let us
113184995Sjkoshy	 */
114184995Sjkoshy	nl  = '\n';
115184995Sjkoshy
116184995Sjkoshy	while (cnt) {
117184995Sjkoshy		cnt = 0;
118184995Sjkoshy
119184995Sjkoshy		if (lineno)
120184995Sjkoshy			(*lineno)++;
121184995Sjkoshy
122184995Sjkoshy		if ((ptr = fgetln(fp, &s)) == NULL)
123184995Sjkoshy			break;
124184995Sjkoshy
125183593Sjkoshy		if (s && com) {		/* Check and eliminate comments */
126183593Sjkoshy			for (cp = ptr; cp < ptr + s; cp++)
127183593Sjkoshy				if (*cp == com && !isescaped(ptr, cp, esc)) {
128183593Sjkoshy					s = cp - ptr;
129183593Sjkoshy					cnt = s == 0 && buf == NULL;
130183593Sjkoshy					break;
131183593Sjkoshy				}
132183593Sjkoshy		}
133183593Sjkoshy
134183593Sjkoshy		if (s && nl) { 		/* Check and eliminate newlines */
135233628Sfabient			cp = &ptr[s - 1];
136183593Sjkoshy
137183593Sjkoshy			if (*cp == nl)
138183593Sjkoshy				s--;	/* forget newline */
139183593Sjkoshy		}
140183593Sjkoshy
141183593Sjkoshy		if (s && con) {		/* Check and eliminate continuations */
142183593Sjkoshy			cp = &ptr[s - 1];
143183593Sjkoshy
144183593Sjkoshy			if (*cp == con && !isescaped(ptr, cp, esc)) {
145183593Sjkoshy				s--;	/* forget escape */
146183593Sjkoshy				cnt = 1;
147183593Sjkoshy			}
148183593Sjkoshy		}
149183593Sjkoshy
150183593Sjkoshy		if (s == 0 && buf != NULL)
151			continue;
152
153		if ((cp = realloc(buf, len + s + 1)) == NULL) {
154			free(buf);
155			return NULL;
156		}
157		buf = cp;
158
159		(void) memcpy(buf + len, ptr, s);
160		len += s;
161		buf[len] = '\0';
162	}
163
164	if ((flags & FPARSELN_UNESCALL) != 0 && esc && buf != NULL &&
165	    strchr(buf, esc) != NULL) {
166		ptr = cp = buf;
167		while (cp[0] != '\0') {
168			int skipesc;
169
170			while (cp[0] != '\0' && cp[0] != esc)
171				*ptr++ = *cp++;
172			if (cp[0] == '\0' || cp[1] == '\0')
173				break;
174
175			skipesc = 0;
176			if (cp[1] == com)
177				skipesc += (flags & FPARSELN_UNESCCOMM);
178			if (cp[1] == con)
179				skipesc += (flags & FPARSELN_UNESCCONT);
180			if (cp[1] == esc)
181				skipesc += (flags & FPARSELN_UNESCESC);
182			if (cp[1] != com && cp[1] != con && cp[1] != esc)
183				skipesc = (flags & FPARSELN_UNESCREST);
184
185			if (skipesc)
186				cp++;
187			else
188				*ptr++ = *cp++;
189			*ptr++ = *cp++;
190		}
191		*ptr = '\0';
192		len = strlen(buf);
193	}
194
195	if (size)
196		*size = len;
197	return buf;
198}
199
200#ifdef TEST
201
202int main __P((int, char **));
203
204int
205main(argc, argv)
206	int argc;
207	char **argv;
208{
209	char   *ptr;
210	size_t	size, line;
211
212	line = 0;
213	while ((ptr = fparseln(stdin, &size, &line, NULL,
214	    FPARSELN_UNESCALL)) != NULL)
215		printf("line %d (%d) |%s|\n", line, size, ptr);
216	return 0;
217}
218
219/*
220
221# This is a test
222line 1
223line 2 \
224line 3 # Comment
225line 4 \# Not comment \\\\
226
227# And a comment \
228line 5 \\\
229line 6
230
231*/
232
233#endif /* TEST */
234