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