1 /*	$OpenBSD: fparseln.c,v 1.5 2004/05/28 07:03:47 deraadt Exp $	*/
2 /*	$NetBSD: fparseln.c,v 1.7 1999/07/02 15:49:12 simonb Exp $	*/
3 
4 /*
5  * Copyright (c) 2009, 2010 Thorsten Glaser
6  * Copyright (c) 1997 Christos Zoulas.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 
35 #ifdef DEBIAN
36 #define NEED_FPARSELN_DECL
37 #include "mbsdtree.h"
38 #else
39 #include "util.h"
40 #endif
41 
42 __RCSID("$MirOS: src/lib/libutil/fparseln.c,v 1.4 2010/03/16 22:26:11 tg Exp $");
43 
44 static int isescaped(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
isescaped(const char * sp,const char * p,int esc)51 isescaped(const char *sp, const char *p, int esc)
52 {
53 	const char     *cp;
54 	size_t		ne;
55 
56 	/* No escape character */
57 	if (esc == '\0')
58 		return 1;
59 
60 	/* Count the number of escape characters that precede ours */
61 	for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
62 		continue;
63 
64 	/* Return true if odd number of escape characters */
65 	return (ne & 1) != 0;
66 }
67 
68 
69 /* fparseln():
70  *	Read a line from a file parsing continuations ending in \
71  *	and eliminating trailing newlines, or comments starting with
72  *	the comment char.
73  */
74 char *
fparseln(FILE * fp,size_t * size,size_t * lineno,const char str[3],int flags)75 fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3],
76     int flags)
77 {
78 	static const char dstr[3] = { '\\', '\\', '#' };
79 	char	*buf = NULL, *ptr, *cp, esc, con, nl, com;
80 	size_t	s, len = 0;
81 	int	cnt = 1;
82 
83 	if (str == NULL)
84 		str = dstr;
85 
86 	esc = str[0];
87 	con = str[1];
88 	com = str[2];
89 
90 	/*
91 	 * XXX: it would be cool to be able to specify the newline character,
92 	 * but unfortunately, fgetln does not let us
93 	 */
94 	nl  = '\n';
95 
96 	while (cnt) {
97 		cnt = 0;
98 
99 		if (lineno)
100 			(*lineno)++;
101 
102 		if ((ptr = fgetln(fp, &s)) == NULL)
103 			break;
104 
105 		if (s && com) {		/* Check and eliminate comments */
106 			for (cp = ptr; cp < ptr + s; cp++)
107 				if (*cp == com && !isescaped(ptr, cp, esc)) {
108 					s = cp - ptr;
109 					cnt = s == 0 && buf == NULL;
110 					break;
111 				}
112 		}
113 
114 		if (s && nl) {		/* Check and eliminate newlines */
115 			cp = &ptr[s - 1];
116 
117 			if (*cp == nl)
118 				s--;	/* forget newline */
119 		}
120 
121 		if (s && con) {		/* Check and eliminate continuations */
122 			cp = &ptr[s - 1];
123 
124 			if (*cp == con && !isescaped(ptr, cp, esc)) {
125 				s--;	/* forget escape */
126 				cnt = 1;
127 			}
128 		}
129 
130 		if (s == 0 && buf != NULL)
131 			continue;
132 
133 		if ((cp = realloc(buf, len + s + 1)) == NULL) {
134 			free(buf);
135 			return NULL;
136 		}
137 		buf = cp;
138 
139 		(void) memcpy(buf + len, ptr, s);
140 		len += s;
141 		buf[len] = '\0';
142 	}
143 
144 	if ((flags & FPARSELN_UNESCALL) != 0 && esc && buf != NULL &&
145 	    strchr(buf, esc) != NULL) {
146 		ptr = cp = buf;
147 		while (cp[0] != '\0') {
148 			int skipesc;
149 
150 			while (cp[0] != '\0' && cp[0] != esc)
151 				*ptr++ = *cp++;
152 			if (cp[0] == '\0' || cp[1] == '\0')
153 				break;
154 
155 			skipesc = 0;
156 			if (cp[1] == com)
157 				skipesc += (flags & FPARSELN_UNESCCOMM);
158 			if (cp[1] == con)
159 				skipesc += (flags & FPARSELN_UNESCCONT);
160 			if (cp[1] == esc)
161 				skipesc += (flags & FPARSELN_UNESCESC);
162 			if (cp[1] != com && cp[1] != con && cp[1] != esc)
163 				skipesc = (flags & FPARSELN_UNESCREST);
164 
165 			if (skipesc)
166 				cp++;
167 			else
168 				*ptr++ = *cp++;
169 			*ptr++ = *cp++;
170 		}
171 		*ptr = '\0';
172 		len = strlen(buf);
173 	}
174 
175 	if (size)
176 		*size = len;
177 	return buf;
178 }
179 
180 #ifdef TEST
181 
182 int main(int, char **);
183 
184 int
main(argc,argv)185 main(argc, argv)
186 	int argc;
187 	char **argv;
188 {
189 	char   *ptr;
190 	size_t	size, line;
191 
192 	line = 0;
193 	while ((ptr = fparseln(stdin, &size, &line, NULL,
194 	    FPARSELN_UNESCALL)) != NULL)
195 		printf("line %d (%d) |%s|\n", line, size, ptr);
196 	return 0;
197 }
198 
199 /*
200 
201 # This is a test
202 line 1
203 line 2 \
204 line 3 # Comment
205 line 4 \# Not comment \\\\
206 
207 # And a comment \
208 line 5 \\\
209 line 6
210 
211 */
212 
213 #endif /* TEST */
214