+ album-3.10, getmail-4.3.14, zile-2.2.9.
[pkgsrc.git] / pkgtools / libnbcompat / files / fparseln.c
1 /*      $NetBSD: fparseln.c,v 1.2 2003/09/06 23:03:02 grant Exp $       */
2
3 /*
4  * Copyright (c) 1997 Christos Zoulas.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Christos Zoulas.
17  * 4. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include <nbcompat.h>
33 #include <nbcompat/cdefs.h>
34 #if defined(LIBC_SCCS) && !defined(lint)
35 __RCSID("$NetBSD: fparseln.c,v 1.5 2004/06/20 22:20:15 jmc Exp $");
36 #endif /* LIBC_SCCS and not lint */
37
38 #if 0
39 #include "namespace.h"
40 #endif
41
42 #include <nbcompat/assert.h>
43 #if HAVE_ERRNO_H
44 #include <errno.h>
45 #endif
46 #include <nbcompat/stdio.h>
47 #include <nbcompat/string.h>
48 #include <nbcompat/stdlib.h>
49
50 #if 0
51 #ifdef __weak_alias
52 __weak_alias(fparseln,_fparseln)
53 #endif
54 #endif
55
56 #if ! HAVE_FPARSELN
57
58 #if 0
59 #ifndef HAVE_NBTOOL_CONFIG_H
60 #include "reentrant.h"
61 #include "local.h"
62 #else
63 #define FLOCKFILE(fp)
64 #define FUNLOCKFILE(fp)
65 #endif
66 #endif
67
68 #if 0
69 #if defined(_REENTRANT) && !HAVE_NBTOOL_CONFIG_H
70 #define __fgetln(f, l) __fgetstr(f, l, '\n')
71 #else
72 #define __fgetln(f, l) fgetln(f, l)
73 #endif
74 #endif
75
76 static int isescaped(const char *, const char *, int);
77
78 /* isescaped():
79  *      Return true if the character in *p that belongs to a string
80  *      that starts in *sp, is escaped by the escape character esc.
81  */
82 static int
83 isescaped(const char *sp, const char *p, int esc)
84 {
85         const char     *cp;
86         size_t          ne;
87
88         /* No escape character */
89         if (esc == '\0')
90                 return 1;
91
92         /* Count the number of escape characters that precede ours */
93         for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
94                 continue;
95
96         /* Return true if odd number of escape characters */
97         return (ne & 1) != 0;
98 }
99
100
101 /* fparseln():
102  *      Read a line from a file parsing continuations ending in \
103  *      and eliminating trailing newlines, or comments starting with
104  *      the comment char.
105  */
106 char *
107 fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
108 {
109         static const char dstr[3] = { '\\', '\\', '#' };
110
111         size_t  s, len;
112         char   *buf;
113         char   *ptr, *cp;
114         int     cnt;
115         char    esc, con, nl, com;
116
117         len = 0;
118         buf = NULL;
119         cnt = 1;
120
121         if (str == NULL)
122                 str = dstr;
123
124         esc = str[0];
125         con = str[1];
126         com = str[2];
127         /*
128          * XXX: it would be cool to be able to specify the newline character,
129          * but unfortunately, fgetln does not let us
130          */
131         nl  = '\n';
132
133         while (cnt) {
134                 cnt = 0;
135
136                 if (lineno)
137                         (*lineno)++;
138
139                 if ((ptr = fgetln(fp, &s)) == NULL)
140                         break;
141
142                 if (s && com) {         /* Check and eliminate comments */
143                         for (cp = ptr; cp < ptr + s; cp++)
144                                 if (*cp == com && !isescaped(ptr, cp, esc)) {
145                                         s = cp - ptr;
146                                         cnt = s == 0 && buf == NULL;
147                                         break;
148                                 }
149                 }
150
151                 if (s && nl) {          /* Check and eliminate newlines */
152                         cp = &ptr[s - 1];
153
154                         if (*cp == nl)
155                                 s--;    /* forget newline */
156                 }
157
158                 if (s && con) {         /* Check and eliminate continuations */
159                         cp = &ptr[s - 1];
160
161                         if (*cp == con && !isescaped(ptr, cp, esc)) {
162                                 s--;    /* forget escape */
163                                 cnt = 1;
164                         }
165                 }
166
167                 if (s == 0 && buf != NULL)
168                         continue;
169
170                 if ((cp = realloc(buf, len + s + 1)) == NULL) {
171                         free(buf);
172                         return NULL;
173                 }
174                 buf = cp;
175
176                 (void) memcpy(buf + len, ptr, s);
177                 len += s;
178                 buf[len] = '\0';
179         }
180
181         if ((flags & FPARSELN_UNESCALL) != 0 && esc && buf != NULL &&
182             strchr(buf, esc) != NULL) {
183                 ptr = cp = buf;
184                 while (cp[0] != '\0') {
185                         int skipesc;
186
187                         while (cp[0] != '\0' && cp[0] != esc)
188                                 *ptr++ = *cp++;
189                         if (cp[0] == '\0' || cp[1] == '\0')
190                                 break;
191
192                         skipesc = 0;
193                         if (cp[1] == com)
194                                 skipesc += (flags & FPARSELN_UNESCCOMM);
195                         if (cp[1] == con)
196                                 skipesc += (flags & FPARSELN_UNESCCONT);
197                         if (cp[1] == esc)
198                                 skipesc += (flags & FPARSELN_UNESCESC);
199                         if (cp[1] != com && cp[1] != con && cp[1] != esc)
200                                 skipesc = (flags & FPARSELN_UNESCREST);
201
202                         if (skipesc)
203                                 cp++;
204                         else
205                                 *ptr++ = *cp++;
206                         *ptr++ = *cp++;
207                 }
208                 *ptr = '\0';
209                 len = strlen(buf);
210         }
211
212         if (size)
213                 *size = len;
214         return buf;
215 }
216
217 #ifdef TEST
218
219 int main(int, char *[]);
220
221 int
222 main(int argc, char *argv[])
223 {
224         char   *ptr;
225         size_t  size, line;
226
227         line = 0;
228         while ((ptr = fparseln(stdin, &size, &line, NULL,
229             FPARSELN_UNESCALL)) != NULL)
230                 printf("line %d (%d) |%s|\n", line, size, ptr);
231         return 0;
232 }
233
234 /*
235
236 # This is a test
237 line 1
238 line 2 \
239 line 3 # Comment
240 line 4 \# Not comment \\\\
241
242 # And a comment \
243 line 5 \\\
244 line 6
245
246 */
247
248 #endif /* TEST */
249 #endif  /* ! HAVE_FPARSELN */