xref: /openbsd-src/lib/libutil/fparseln.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: fparseln.c,v 1.6 2005/08/02 21:46:23 espie 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 #include <sys/cdefs.h>
34 
35 #include <stdio.h>
36 #include <string.h>
37 #include <stdlib.h>
38 
39 #include "util.h"
40 
41 static int isescaped(const char *, const char *, int);
42 
43 /* isescaped():
44  *	Return true if the character in *p that belongs to a string
45  *	that starts in *sp, is escaped by the escape character esc.
46  */
47 static int
48 isescaped(const char *sp, const char *p, int esc)
49 {
50 	const char     *cp;
51 	size_t		ne;
52 
53 	/* No escape character */
54 	if (esc == '\0')
55 		return 1;
56 
57 	/* Count the number of escape characters that precede ours */
58 	for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
59 		continue;
60 
61 	/* Return true if odd number of escape characters */
62 	return (ne & 1) != 0;
63 }
64 
65 
66 /* fparseln():
67  *	Read a line from a file parsing continuations ending in \
68  *	and eliminating trailing newlines, or comments starting with
69  *	the comment char.
70  */
71 char *
72 fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3],
73     int flags)
74 {
75 	static const char dstr[3] = { '\\', '\\', '#' };
76 	char	*buf = NULL, *ptr, *cp, esc, con, nl, com;
77 	size_t	s, len = 0;
78 	int	cnt = 1;
79 
80 	if (str == NULL)
81 		str = dstr;
82 
83 	esc = str[0];
84 	con = str[1];
85 	com = str[2];
86 
87 	/*
88 	 * XXX: it would be cool to be able to specify the newline character,
89 	 * but unfortunately, fgetln does not let us
90 	 */
91 	nl  = '\n';
92 
93 	while (cnt) {
94 		cnt = 0;
95 
96 		if (lineno)
97 			(*lineno)++;
98 
99 		if ((ptr = fgetln(fp, &s)) == NULL)
100 			break;
101 
102 		if (s && com) {		/* Check and eliminate comments */
103 			for (cp = ptr; cp < ptr + s; cp++)
104 				if (*cp == com && !isescaped(ptr, cp, esc)) {
105 					s = cp - ptr;
106 					cnt = s == 0 && buf == NULL;
107 					break;
108 				}
109 		}
110 
111 		if (s && nl) {		/* Check and eliminate newlines */
112 			cp = &ptr[s - 1];
113 
114 			if (*cp == nl)
115 				s--;	/* forget newline */
116 		}
117 
118 		if (s && con) {		/* Check and eliminate continuations */
119 			cp = &ptr[s - 1];
120 
121 			if (*cp == con && !isescaped(ptr, cp, esc)) {
122 				s--;	/* forget escape */
123 				cnt = 1;
124 			}
125 		}
126 
127 		if (s == 0 && buf != NULL)
128 			continue;
129 
130 		if ((cp = realloc(buf, len + s + 1)) == NULL) {
131 			free(buf);
132 			return NULL;
133 		}
134 		buf = cp;
135 
136 		(void) memcpy(buf + len, ptr, s);
137 		len += s;
138 		buf[len] = '\0';
139 	}
140 
141 	if ((flags & FPARSELN_UNESCALL) != 0 && esc && buf != NULL &&
142 	    strchr(buf, esc) != NULL) {
143 		ptr = cp = buf;
144 		while (cp[0] != '\0') {
145 			int skipesc;
146 
147 			while (cp[0] != '\0' && cp[0] != esc)
148 				*ptr++ = *cp++;
149 			if (cp[0] == '\0' || cp[1] == '\0')
150 				break;
151 
152 			skipesc = 0;
153 			if (cp[1] == com)
154 				skipesc += (flags & FPARSELN_UNESCCOMM);
155 			if (cp[1] == con)
156 				skipesc += (flags & FPARSELN_UNESCCONT);
157 			if (cp[1] == esc)
158 				skipesc += (flags & FPARSELN_UNESCESC);
159 			if (cp[1] != com && cp[1] != con && cp[1] != esc)
160 				skipesc = (flags & FPARSELN_UNESCREST);
161 
162 			if (skipesc)
163 				cp++;
164 			else
165 				*ptr++ = *cp++;
166 			*ptr++ = *cp++;
167 		}
168 		*ptr = '\0';
169 		len = strlen(buf);
170 	}
171 
172 	if (size)
173 		*size = len;
174 	return buf;
175 }
176 
177 #ifdef TEST
178 
179 int main(int, char **);
180 
181 int
182 main(argc, argv)
183 	int argc;
184 	char **argv;
185 {
186 	char   *ptr;
187 	size_t	size, line;
188 
189 	line = 0;
190 	while ((ptr = fparseln(stdin, &size, &line, NULL,
191 	    FPARSELN_UNESCALL)) != NULL)
192 		printf("line %d (%d) |%s|\n", line, size, ptr);
193 	return 0;
194 }
195 
196 /*
197 
198 # This is a test
199 line 1
200 line 2 \
201 line 3 # Comment
202 line 4 \# Not comment \\\\
203 
204 # And a comment \
205 line 5 \\\
206 line 6
207 
208 */
209 
210 #endif /* TEST */
211