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