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