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