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