xref: /freebsd/contrib/blocklist/port/fparseln.c (revision df21a004be237a1dccd03c7b47254625eea62fa9)
1 /*	$NetBSD: fparseln.c,v 1.2 2025/02/11 17:48:30 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 #ifdef HAVE_SYS_CDEFS_H
31 #include <sys/cdefs.h>
32 #endif
33 #if defined(LIBC_SCCS) && !defined(lint)
34 __RCSID("$NetBSD: fparseln.c,v 1.2 2025/02/11 17:48:30 christos Exp $");
35 #endif /* LIBC_SCCS and not lint */
36 
37 #include <assert.h>
38 #include <errno.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <stdlib.h>
42 
43 #if ! HAVE_FPARSELN || BROKEN_FPARSELN
44 
45 #define FLOCKFILE(fp)
46 #define FUNLOCKFILE(fp)
47 
48 #if defined(_REENTRANT) && !HAVE_NBTOOL_CONFIG_H
49 #define __fgetln(f, l) __fgetstr(f, l, '\n')
50 #else
51 #define __fgetln(f, l) fgetln(f, l)
52 #endif
53 
54 static int isescaped(const char *, const char *, int);
55 
56 /* isescaped():
57  *	Return true if the character in *p that belongs to a string
58  *	that starts in *sp, is escaped by the escape character esc.
59  */
60 static int
61 isescaped(const char *sp, const char *p, int esc)
62 {
63 	const char     *cp;
64 	size_t		ne;
65 
66 	/* No escape character */
67 	if (esc == '\0')
68 		return 0;
69 
70 	/* Count the number of escape characters that precede ours */
71 	for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
72 		continue;
73 
74 	/* Return true if odd number of escape characters */
75 	return (ne & 1) != 0;
76 }
77 
78 
79 /* fparseln():
80  *	Read a line from a file parsing continuations ending in \
81  *	and eliminating trailing newlines, or comments starting with
82  *	the comment char.
83  */
84 char *
85 fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], 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 	len = 0;
96 	buf = NULL;
97 	cnt = 1;
98 
99 	if (str == NULL)
100 		str = dstr;
101 
102 	esc = str[0];
103 	con = str[1];
104 	com = str[2];
105 	/*
106 	 * XXX: it would be cool to be able to specify the newline character,
107 	 * but unfortunately, fgetln does not let us
108 	 */
109 	nl  = '\n';
110 
111 	FLOCKFILE(fp);
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 continuation char */
143 				cnt = 1;
144 			}
145 		}
146 
147 		if (s == 0) {
148 			/*
149 			 * nothing to add, skip realloc except in case
150 			 * we need a minimal buf to return an empty line
151 			 */
152 			if (cnt || buf != NULL)
153 				continue;
154 		}
155 
156 		if ((cp = realloc(buf, len + s + 1)) == NULL) {
157 			FUNLOCKFILE(fp);
158 			free(buf);
159 			return NULL;
160 		}
161 		buf = cp;
162 
163 		(void) memcpy(buf + len, ptr, s);
164 		len += s;
165 		buf[len] = '\0';
166 	}
167 
168 	FUNLOCKFILE(fp);
169 
170 	if ((flags & FPARSELN_UNESCALL) != 0 && esc && buf != NULL &&
171 	    strchr(buf, esc) != NULL) {
172 		ptr = cp = buf;
173 		while (cp[0] != '\0') {
174 			int skipesc;
175 
176 			while (cp[0] != '\0' && cp[0] != esc)
177 				*ptr++ = *cp++;
178 			if (cp[0] == '\0' || cp[1] == '\0')
179 				break;
180 
181 			skipesc = 0;
182 			if (cp[1] == com)
183 				skipesc += (flags & FPARSELN_UNESCCOMM);
184 			if (cp[1] == con)
185 				skipesc += (flags & FPARSELN_UNESCCONT);
186 			if (cp[1] == esc)
187 				skipesc += (flags & FPARSELN_UNESCESC);
188 			if (cp[1] != com && cp[1] != con && cp[1] != esc)
189 				skipesc = (flags & FPARSELN_UNESCREST);
190 
191 			if (skipesc)
192 				cp++;
193 			else
194 				*ptr++ = *cp++;
195 			*ptr++ = *cp++;
196 		}
197 		*ptr = '\0';
198 		len = strlen(buf);
199 	}
200 
201 	if (size)
202 		*size = len;
203 	return buf;
204 }
205 
206 #ifdef TEST
207 
208 int main(int, char **);
209 
210 int
211 main(int argc, char **argv)
212 {
213 	char   *ptr;
214 	size_t	size, line;
215 
216 	line = 0;
217 	while ((ptr = fparseln(stdin, &size, &line, NULL,
218 	    FPARSELN_UNESCALL)) != NULL)
219 		printf("line %d (%d) |%s|\n", line, size, ptr);
220 	return 0;
221 }
222 
223 /*
224 
225 # This is a test
226 line 1
227 line 2 \
228 line 3 # Comment
229 line 4 \# Not comment \\\\
230 
231 # And a comment \
232 line 5 \\\
233 line 6
234 
235 */
236 
237 #endif /* TEST */
238 #endif	/* ! HAVE_FPARSELN || BROKEN_FPARSELN */
239