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