1105756Srwatson/*	$NetBSD: fparseln.c,v 1.7 2007/03/08 19:57:53 drochner Exp $	*/
2105756Srwatson
3105756Srwatson/*
4105756Srwatson * Copyright (c) 1997 Christos Zoulas.  All rights reserved.
5105756Srwatson *
6105756Srwatson * Redistribution and use in source and binary forms, with or without
7105756Srwatson * modification, are permitted provided that the following conditions
8105756Srwatson * are met:
9105756Srwatson * 1. Redistributions of source code must retain the above copyright
10105756Srwatson *    notice, this list of conditions and the following disclaimer.
11105756Srwatson * 2. Redistributions in binary form must reproduce the above copyright
12105756Srwatson *    notice, this list of conditions and the following disclaimer in the
13105756Srwatson *    documentation and/or other materials provided with the distribution.
14105756Srwatson * 3. All advertising materials mentioning features or use of this software
15105756Srwatson *    must display the following acknowledgement:
16105756Srwatson *	This product includes software developed by Christos Zoulas.
17105756Srwatson * 4. The name of the author may not be used to endorse or promote products
18105756Srwatson *    derived from this software without specific prior written permission.
19105756Srwatson *
20105756Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21105756Srwatson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22105756Srwatson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23105756Srwatson * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24105756Srwatson * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25105756Srwatson * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26105756Srwatson * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27105756Srwatson * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28105756Srwatson * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29105756Srwatson * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30105756Srwatson */
31105756Srwatson
32105756Srwatson#include <sys/cdefs.h>
33105756Srwatson__FBSDID("$FreeBSD: releng/10.3/lib/libutil/fparseln.c 270031 2014-08-16 00:54:56Z pfg $");
34105756Srwatson
35105756Srwatson#include <sys/types.h>
36105756Srwatson#include <assert.h>
37105756Srwatson#include <errno.h>
38105756Srwatson#include <stdio.h>
39105756Srwatson#include <string.h>
40105756Srwatson#include <stdlib.h>
41105756Srwatson#include <libutil.h>
42105756Srwatson
43105756Srwatsonstatic int isescaped(const char *, const char *, int);
44105756Srwatson
45105756Srwatson/* isescaped():
46105756Srwatson *	Return true if the character in *p that belongs to a string
47105756Srwatson *	that starts in *sp, is escaped by the escape character esc.
48105756Srwatson */
49105756Srwatsonstatic int
50105756Srwatsonisescaped(const char *sp, const char *p, int esc)
51105756Srwatson{
52105756Srwatson	const char     *cp;
53105756Srwatson	size_t		ne;
54105756Srwatson
55105756Srwatson#if 0
56105756Srwatson	_DIAGASSERT(sp != NULL);
57105756Srwatson	_DIAGASSERT(p != NULL);
58105756Srwatson#endif
59105756Srwatson
60105756Srwatson	/* No escape character */
61105756Srwatson	if (esc == '\0')
62105756Srwatson		return 0;
63105756Srwatson
64105756Srwatson	/* Count the number of escape characters that precede ours */
65105756Srwatson	for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
66105756Srwatson		continue;
67105756Srwatson
68105756Srwatson	/* Return true if odd number of escape characters */
69105756Srwatson	return (ne & 1) != 0;
70105756Srwatson}
71105756Srwatson
72105756Srwatson
73105756Srwatson/* fparseln():
74105756Srwatson *	Read a line from a file parsing continuations ending in \
75105756Srwatson *	and eliminating trailing newlines, or comments starting with
76105756Srwatson *	the comment char.
77105756Srwatson */
78105756Srwatsonchar *
79105756Srwatsonfparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
80105756Srwatson{
81105756Srwatson	static const char dstr[3] = { '\\', '\\', '#' };
82105756Srwatson
83105756Srwatson	size_t	s, len;
84105756Srwatson	char   *buf;
85105756Srwatson	char   *ptr, *cp;
86105756Srwatson	int	cnt;
87105756Srwatson	char	esc, con, nl, com;
88105756Srwatson
89105756Srwatson#if 0
90105756Srwatson	_DIAGASSERT(fp != NULL);
91105756Srwatson#endif
92105756Srwatson
93105756Srwatson	len = 0;
94105756Srwatson	buf = NULL;
95105756Srwatson	cnt = 1;
96105756Srwatson
97105756Srwatson	if (str == NULL)
98105756Srwatson		str = dstr;
99105756Srwatson
100105756Srwatson	esc = str[0];
101105756Srwatson	con = str[1];
102105756Srwatson	com = str[2];
103105756Srwatson	/*
104	 * XXX: it would be cool to be able to specify the newline character,
105	 * but unfortunately, fgetln does not let us
106	 */
107	nl  = '\n';
108
109	while (cnt) {
110		cnt = 0;
111
112		if (lineno)
113			(*lineno)++;
114
115		if ((ptr = fgetln(fp, &s)) == NULL)
116			break;
117
118		if (s && com) {		/* Check and eliminate comments */
119			for (cp = ptr; cp < ptr + s; cp++)
120				if (*cp == com && !isescaped(ptr, cp, esc)) {
121					s = cp - ptr;
122					cnt = s == 0 && buf == NULL;
123					break;
124				}
125		}
126
127		if (s && nl) { 		/* Check and eliminate newlines */
128			cp = &ptr[s - 1];
129
130			if (*cp == nl)
131				s--;	/* forget newline */
132		}
133
134		if (s && con) {		/* Check and eliminate continuations */
135			cp = &ptr[s - 1];
136
137			if (*cp == con && !isescaped(ptr, cp, esc)) {
138				s--;	/* forget continuation char */
139				cnt = 1;
140			}
141		}
142
143		if (s == 0) {
144			/*
145			 * nothing to add, skip realloc except in case
146			 * we need a minimal buf to return an empty line
147			 */
148			if (cnt || buf != NULL)
149				continue;
150		}
151
152		if ((cp = realloc(buf, len + s + 1)) == NULL) {
153			free(buf);
154			return NULL;
155		}
156		buf = cp;
157
158		(void) memcpy(buf + len, ptr, s);
159		len += s;
160		buf[len] = '\0';
161	}
162
163	if ((flags & FPARSELN_UNESCALL) != 0 && esc && buf != NULL &&
164	    strchr(buf, esc) != NULL) {
165		ptr = cp = buf;
166		while (cp[0] != '\0') {
167			int skipesc;
168
169			while (cp[0] != '\0' && cp[0] != esc)
170				*ptr++ = *cp++;
171			if (cp[0] == '\0' || cp[1] == '\0')
172				break;
173
174			skipesc = 0;
175			if (cp[1] == com)
176				skipesc += (flags & FPARSELN_UNESCCOMM);
177			if (cp[1] == con)
178				skipesc += (flags & FPARSELN_UNESCCONT);
179			if (cp[1] == esc)
180				skipesc += (flags & FPARSELN_UNESCESC);
181			if (cp[1] != com && cp[1] != con && cp[1] != esc)
182				skipesc = (flags & FPARSELN_UNESCREST);
183
184			if (skipesc)
185				cp++;
186			else
187				*ptr++ = *cp++;
188			*ptr++ = *cp++;
189		}
190		*ptr = '\0';
191		len = strlen(buf);
192	}
193
194	if (size)
195		*size = len;
196	return buf;
197}
198
199#ifdef TEST
200
201int
202main(int argc, char *argv[])
203{
204	char   *ptr;
205	size_t	size, line;
206
207	line = 0;
208	while ((ptr = fparseln(stdin, &size, &line, NULL,
209	    FPARSELN_UNESCALL)) != NULL)
210		printf("line %d (%d) |%s|\n", line, size, ptr);
211	return 0;
212}
213
214/*
215
216# This is a test
217line 1
218line 2 \
219line 3 # Comment
220line 4 \# Not comment \\\\
221
222# And a comment \
223line 5 \\\
224line 6
225
226*/
227
228#endif /* TEST */
229