libite 2.6.1
fparseln.c
Go to the documentation of this file.
1/* $NetBSD: fparseln.c,v 1.10 2009/10/21 01:07:45 snj 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 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
33
34#include <assert.h>
35#include <errno.h>
36#include <stdio.h>
37#include <string.h>
38#include <stdlib.h>
39#include "lite.h"
40
41#ifndef _DIAGASSERT
42#define _DIAGASSERT(t)
43#endif
44
45static int isescaped(const char *, const char *, int);
46
47/* isescaped():
48 * Return true if the character in *p that belongs to a string
49 * that starts in *sp, is escaped by the escape character esc.
50 */
51static int
52isescaped(const char *sp, const char *p, int esc)
53{
54 const char *cp;
55 size_t ne;
56
57 _DIAGASSERT(sp != NULL);
58 _DIAGASSERT(p != NULL);
59
60 /* No escape character */
61 if (esc == '\0')
62 return 0;
63
64 /* Count the number of escape characters that precede ours */
65 for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
66 continue;
67
68 /* Return true if odd number of escape characters */
69 return (ne & 1) != 0;
70}
71
72
93char *
94fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
95{
96 static const char dstr[3] = { '\\', '\\', '#' };
97
98 ssize_t s;
99 size_t len, ptrlen;
100 char *buf;
101 char *ptr, *cp;
102 int cnt;
103 char esc, con, nl, com;
104
105 _DIAGASSERT(fp != NULL);
106
107 len = 0;
108 buf = NULL;
109 ptrlen = 0;
110 ptr = NULL;
111 cnt = 1;
112
113 if (str == NULL)
114 str = dstr;
115
116 esc = str[0];
117 con = str[1];
118 com = str[2];
119 /*
120 * XXX: it would be cool to be able to specify the newline character,
121 * getdelim(3) does let us, but supporting it would diverge from BSDs.
122 */
123 nl = '\n';
124
125 flockfile(fp);
126
127 while (cnt) {
128 cnt = 0;
129
130 if (lineno)
131 (*lineno)++;
132
133 s = getline(&ptr, &ptrlen, fp);
134 if (s < 0)
135 break;
136
137 if (s && com) { /* Check and eliminate comments */
138 for (cp = ptr; cp < ptr + s; cp++)
139 if (*cp == com && !isescaped(ptr, cp, esc)) {
140 s = cp - ptr;
141 cnt = s == 0 && buf == NULL;
142 break;
143 }
144 }
145
146 if (s && nl) { /* Check and eliminate newlines */
147 cp = &ptr[s - 1];
148
149 if (*cp == nl)
150 s--; /* forget newline */
151 }
152
153 if (s && con) { /* Check and eliminate continuations */
154 cp = &ptr[s - 1];
155
156 if (*cp == con && !isescaped(ptr, cp, esc)) {
157 s--; /* forget continuation char */
158 cnt = 1;
159 }
160 }
161
162 if (s == 0) {
163 /*
164 * nothing to add, skip realloc except in case
165 * we need a minimal buf to return an empty line
166 */
167 if (cnt || buf != NULL)
168 continue;
169 }
170
171 if ((cp = realloc(buf, len + s + 1)) == NULL) {
172 funlockfile(fp);
173 free(buf);
174 free(ptr);
175 return NULL;
176 }
177 buf = cp;
178
179 (void) memcpy(buf + len, ptr, s);
180 len += s;
181 buf[len] = '\0';
182 }
183
184 funlockfile(fp);
185 free(ptr);
186
187 if ((flags & FPARSELN_UNESCALL) != 0 && esc && buf != NULL &&
188 strchr(buf, esc) != NULL) {
189 ptr = cp = buf;
190 while (cp[0] != '\0') {
191 int skipesc;
192
193 while (cp[0] != '\0' && cp[0] != esc)
194 *ptr++ = *cp++;
195 if (cp[0] == '\0' || cp[1] == '\0')
196 break;
197
198 skipesc = 0;
199 if (cp[1] == com)
200 skipesc += (flags & FPARSELN_UNESCCOMM);
201 if (cp[1] == con)
202 skipesc += (flags & FPARSELN_UNESCCONT);
203 if (cp[1] == esc)
204 skipesc += (flags & FPARSELN_UNESCESC);
205 if (cp[1] != com && cp[1] != con && cp[1] != esc)
206 skipesc = (flags & FPARSELN_UNESCREST);
207
208 if (skipesc)
209 cp++;
210 else
211 *ptr++ = *cp++;
212 *ptr++ = *cp++;
213 }
214 *ptr = '\0';
215 len = strlen(buf);
216 }
217
218 if (size)
219 *size = len;
220 return buf;
221}
char * fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
Definition fparseln.c:94
#define FPARSELN_UNESCALL
Definition lite.h:62
#define FPARSELN_UNESCCOMM
Definition lite.h:60
#define FPARSELN_UNESCCONT
Definition lite.h:59
#define FPARSELN_UNESCESC
Definition lite.h:58
#define FPARSELN_UNESCREST
Definition lite.h:61