xref: /openbsd/lib/libutil/fparseln.c (revision db3296cf)
1 /*	$OpenBSD: fparseln.c,v 1.4 2002/06/09 22:18:43 fgsch Exp $	*/
2 /*	$NetBSD: fparseln.c,v 1.7 1999/07/02 15:49:12 simonb Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Christos Zoulas.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Christos Zoulas.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static const char rcsid[] = "$OpenBSD: fparseln.c,v 1.4 2002/06/09 22:18:43 fgsch Exp $";
35 #endif /* LIBC_SCCS and not lint */
36 
37 #include <sys/cdefs.h>
38 
39 #include <stdio.h>
40 #include <string.h>
41 #include <stdlib.h>
42 
43 #include "util.h"
44 
45 static int isescaped(const char *, const char *, int);
46 
47 /* isescaped():
48  *	Return true if the character in *p that belongs to a string
49  *	that starts in *sp, is escaped by the escape character esc.
50  */
51 static int
52 isescaped(sp, p, esc)
53 	const char *sp, *p;
54 	int esc;
55 {
56 	const char     *cp;
57 	size_t		ne;
58 
59 	/* No escape character */
60 	if (esc == '\0')
61 		return 1;
62 
63 	/* Count the number of escape characters that precede ours */
64 	for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
65 		continue;
66 
67 	/* Return true if odd number of escape characters */
68 	return (ne & 1) != 0;
69 }
70 
71 
72 /* fparseln():
73  *	Read a line from a file parsing continuations ending in \
74  *	and eliminating trailing newlines, or comments starting with
75  *	the comment char.
76  */
77 char *
78 fparseln(fp, size, lineno, str, flags)
79 	FILE		*fp;
80 	size_t		*size;
81 	size_t		*lineno;
82 	const char	 str[3];
83 	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 	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 escape */
139 				cnt = 1;
140 			}
141 		}
142 
143 		if (s == 0 && buf != NULL)
144 			continue;
145 
146 		if ((cp = realloc(buf, len + s + 1)) == NULL) {
147 			free(buf);
148 			return NULL;
149 		}
150 		buf = cp;
151 
152 		(void) memcpy(buf + len, ptr, s);
153 		len += s;
154 		buf[len] = '\0';
155 	}
156 
157 	if ((flags & FPARSELN_UNESCALL) != 0 && esc && buf != NULL &&
158 	    strchr(buf, esc) != NULL) {
159 		ptr = cp = buf;
160 		while (cp[0] != '\0') {
161 			int skipesc;
162 
163 			while (cp[0] != '\0' && cp[0] != esc)
164 				*ptr++ = *cp++;
165 			if (cp[0] == '\0' || cp[1] == '\0')
166 				break;
167 
168 			skipesc = 0;
169 			if (cp[1] == com)
170 				skipesc += (flags & FPARSELN_UNESCCOMM);
171 			if (cp[1] == con)
172 				skipesc += (flags & FPARSELN_UNESCCONT);
173 			if (cp[1] == esc)
174 				skipesc += (flags & FPARSELN_UNESCESC);
175 			if (cp[1] != com && cp[1] != con && cp[1] != esc)
176 				skipesc = (flags & FPARSELN_UNESCREST);
177 
178 			if (skipesc)
179 				cp++;
180 			else
181 				*ptr++ = *cp++;
182 			*ptr++ = *cp++;
183 		}
184 		*ptr = '\0';
185 		len = strlen(buf);
186 	}
187 
188 	if (size)
189 		*size = len;
190 	return buf;
191 }
192 
193 #ifdef TEST
194 
195 int main(int, char **);
196 
197 int
198 main(argc, argv)
199 	int argc;
200 	char **argv;
201 {
202 	char   *ptr;
203 	size_t	size, line;
204 
205 	line = 0;
206 	while ((ptr = fparseln(stdin, &size, &line, NULL,
207 	    FPARSELN_UNESCALL)) != NULL)
208 		printf("line %d (%d) |%s|\n", line, size, ptr);
209 	return 0;
210 }
211 
212 /*
213 
214 # This is a test
215 line 1
216 line 2 \
217 line 3 # Comment
218 line 4 \# Not comment \\\\
219 
220 # And a comment \
221 line 5 \\\
222 line 6
223 
224 */
225 
226 #endif /* TEST */
227