xref: /netbsd/lib/libc/stdio/fparseln.c (revision c4a72b64)
1 /*	$NetBSD: fparseln.c,v 1.1 2002/11/30 03:04:45 lukem 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  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Christos Zoulas.
17  * 4. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 __RCSID("$NetBSD: fparseln.c,v 1.1 2002/11/30 03:04:45 lukem Exp $");
35 #endif /* LIBC_SCCS and not lint */
36 
37 #include "namespace.h"
38 
39 #include <assert.h>
40 #include <errno.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <stdlib.h>
44 
45 #ifdef __weak_alias
46 __weak_alias(fparseln,_fparseln)
47 #endif
48 
49 #if ! HAVE_FPARSELN
50 
51 static int isescaped(const char *, const char *, int);
52 
53 /* isescaped():
54  *	Return true if the character in *p that belongs to a string
55  *	that starts in *sp, is escaped by the escape character esc.
56  */
57 static int
58 isescaped(const char *sp, const char *p, int esc)
59 {
60 	const char     *cp;
61 	size_t		ne;
62 
63 	_DIAGASSERT(sp != NULL);
64 	_DIAGASSERT(p != NULL);
65 
66 	/* No escape character */
67 	if (esc == '\0')
68 		return 1;
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 	_DIAGASSERT(fp != NULL);
96 
97 	len = 0;
98 	buf = NULL;
99 	cnt = 1;
100 
101 	if (str == NULL)
102 		str = dstr;
103 
104 	esc = str[0];
105 	con = str[1];
106 	com = str[2];
107 	/*
108 	 * XXX: it would be cool to be able to specify the newline character,
109 	 * but unfortunately, fgetln does not let us
110 	 */
111 	nl  = '\n';
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 escape */
143 				cnt = 1;
144 			}
145 		}
146 
147 		if (s == 0 && buf != NULL)
148 			continue;
149 
150 		if ((cp = realloc(buf, len + s + 1)) == NULL) {
151 			free(buf);
152 			return NULL;
153 		}
154 		buf = cp;
155 
156 		(void) memcpy(buf + len, ptr, s);
157 		len += s;
158 		buf[len] = '\0';
159 	}
160 
161 	if ((flags & FPARSELN_UNESCALL) != 0 && esc && buf != NULL &&
162 	    strchr(buf, esc) != NULL) {
163 		ptr = cp = buf;
164 		while (cp[0] != '\0') {
165 			int skipesc;
166 
167 			while (cp[0] != '\0' && cp[0] != esc)
168 				*ptr++ = *cp++;
169 			if (cp[0] == '\0' || cp[1] == '\0')
170 				break;
171 
172 			skipesc = 0;
173 			if (cp[1] == com)
174 				skipesc += (flags & FPARSELN_UNESCCOMM);
175 			if (cp[1] == con)
176 				skipesc += (flags & FPARSELN_UNESCCONT);
177 			if (cp[1] == esc)
178 				skipesc += (flags & FPARSELN_UNESCESC);
179 			if (cp[1] != com && cp[1] != con && cp[1] != esc)
180 				skipesc = (flags & FPARSELN_UNESCREST);
181 
182 			if (skipesc)
183 				cp++;
184 			else
185 				*ptr++ = *cp++;
186 			*ptr++ = *cp++;
187 		}
188 		*ptr = '\0';
189 		len = strlen(buf);
190 	}
191 
192 	if (size)
193 		*size = len;
194 	return buf;
195 }
196 
197 #ifdef TEST
198 
199 int main(int, char **);
200 
201 int
202 main(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
217 line 1
218 line 2 \
219 line 3 # Comment
220 line 4 \# Not comment \\\\
221 
222 # And a comment \
223 line 5 \\\
224 line 6
225 
226 */
227 
228 #endif /* TEST */
229 #endif	/* ! HAVE_FPARSELN */
230