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