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