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