Home | History | Annotate | Line # | Download | only in stdio
fparseln.c revision 1.9
      1  1.9  christos /*	$NetBSD: fparseln.c,v 1.9 2009/01/11 02:46:29 christos 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.9  christos __RCSID("$NetBSD: fparseln.c,v 1.9 2009/01/11 02:46:29 christos 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.8   tnozaki #if ! HAVE_FPARSELN || BROKEN_FPARSELN
     50  1.1     lukem 
     51  1.4  drochner #ifndef HAVE_NBTOOL_CONFIG_H
     52  1.3  drochner #include "reentrant.h"
     53  1.3  drochner #include "local.h"
     54  1.4  drochner #else
     55  1.4  drochner #define FLOCKFILE(fp)
     56  1.4  drochner #define FUNLOCKFILE(fp)
     57  1.4  drochner #endif
     58  1.3  drochner 
     59  1.5       jmc #if defined(_REENTRANT) && !HAVE_NBTOOL_CONFIG_H
     60  1.3  drochner #define __fgetln(f, l) __fgetstr(f, l, '\n')
     61  1.3  drochner #else
     62  1.3  drochner #define __fgetln(f, l) fgetln(f, l)
     63  1.3  drochner #endif
     64  1.3  drochner 
     65  1.1     lukem static int isescaped(const char *, const char *, int);
     66  1.1     lukem 
     67  1.1     lukem /* isescaped():
     68  1.1     lukem  *	Return true if the character in *p that belongs to a string
     69  1.1     lukem  *	that starts in *sp, is escaped by the escape character esc.
     70  1.1     lukem  */
     71  1.1     lukem static int
     72  1.1     lukem isescaped(const char *sp, const char *p, int esc)
     73  1.1     lukem {
     74  1.1     lukem 	const char     *cp;
     75  1.1     lukem 	size_t		ne;
     76  1.1     lukem 
     77  1.1     lukem 	_DIAGASSERT(sp != NULL);
     78  1.1     lukem 	_DIAGASSERT(p != NULL);
     79  1.1     lukem 
     80  1.1     lukem 	/* No escape character */
     81  1.1     lukem 	if (esc == '\0')
     82  1.6  drochner 		return 0;
     83  1.1     lukem 
     84  1.1     lukem 	/* Count the number of escape characters that precede ours */
     85  1.1     lukem 	for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
     86  1.1     lukem 		continue;
     87  1.1     lukem 
     88  1.1     lukem 	/* Return true if odd number of escape characters */
     89  1.1     lukem 	return (ne & 1) != 0;
     90  1.1     lukem }
     91  1.1     lukem 
     92  1.1     lukem 
     93  1.1     lukem /* fparseln():
     94  1.1     lukem  *	Read a line from a file parsing continuations ending in \
     95  1.1     lukem  *	and eliminating trailing newlines, or comments starting with
     96  1.1     lukem  *	the comment char.
     97  1.1     lukem  */
     98  1.1     lukem char *
     99  1.1     lukem fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
    100  1.1     lukem {
    101  1.1     lukem 	static const char dstr[3] = { '\\', '\\', '#' };
    102  1.1     lukem 
    103  1.1     lukem 	size_t	s, len;
    104  1.1     lukem 	char   *buf;
    105  1.1     lukem 	char   *ptr, *cp;
    106  1.1     lukem 	int	cnt;
    107  1.1     lukem 	char	esc, con, nl, com;
    108  1.1     lukem 
    109  1.1     lukem 	_DIAGASSERT(fp != NULL);
    110  1.1     lukem 
    111  1.1     lukem 	len = 0;
    112  1.1     lukem 	buf = NULL;
    113  1.1     lukem 	cnt = 1;
    114  1.1     lukem 
    115  1.1     lukem 	if (str == NULL)
    116  1.1     lukem 		str = dstr;
    117  1.1     lukem 
    118  1.1     lukem 	esc = str[0];
    119  1.1     lukem 	con = str[1];
    120  1.1     lukem 	com = str[2];
    121  1.1     lukem 	/*
    122  1.1     lukem 	 * XXX: it would be cool to be able to specify the newline character,
    123  1.1     lukem 	 * but unfortunately, fgetln does not let us
    124  1.1     lukem 	 */
    125  1.1     lukem 	nl  = '\n';
    126  1.1     lukem 
    127  1.3  drochner 	FLOCKFILE(fp);
    128  1.3  drochner 
    129  1.1     lukem 	while (cnt) {
    130  1.1     lukem 		cnt = 0;
    131  1.1     lukem 
    132  1.1     lukem 		if (lineno)
    133  1.1     lukem 			(*lineno)++;
    134  1.1     lukem 
    135  1.3  drochner 		if ((ptr = __fgetln(fp, &s)) == NULL)
    136  1.1     lukem 			break;
    137  1.1     lukem 
    138  1.1     lukem 		if (s && com) {		/* Check and eliminate comments */
    139  1.1     lukem 			for (cp = ptr; cp < ptr + s; cp++)
    140  1.1     lukem 				if (*cp == com && !isescaped(ptr, cp, esc)) {
    141  1.1     lukem 					s = cp - ptr;
    142  1.1     lukem 					cnt = s == 0 && buf == NULL;
    143  1.1     lukem 					break;
    144  1.1     lukem 				}
    145  1.1     lukem 		}
    146  1.1     lukem 
    147  1.1     lukem 		if (s && nl) { 		/* Check and eliminate newlines */
    148  1.1     lukem 			cp = &ptr[s - 1];
    149  1.1     lukem 
    150  1.1     lukem 			if (*cp == nl)
    151  1.1     lukem 				s--;	/* forget newline */
    152  1.1     lukem 		}
    153  1.1     lukem 
    154  1.1     lukem 		if (s && con) {		/* Check and eliminate continuations */
    155  1.1     lukem 			cp = &ptr[s - 1];
    156  1.1     lukem 
    157  1.1     lukem 			if (*cp == con && !isescaped(ptr, cp, esc)) {
    158  1.7  drochner 				s--;	/* forget continuation char */
    159  1.1     lukem 				cnt = 1;
    160  1.1     lukem 			}
    161  1.1     lukem 		}
    162  1.1     lukem 
    163  1.7  drochner 		if (s == 0) {
    164  1.7  drochner 			/*
    165  1.7  drochner 			 * nothing to add, skip realloc except in case
    166  1.7  drochner 			 * we need a minimal buf to return an empty line
    167  1.7  drochner 			 */
    168  1.7  drochner 			if (cnt || buf != NULL)
    169  1.7  drochner 				continue;
    170  1.7  drochner 		}
    171  1.1     lukem 
    172  1.1     lukem 		if ((cp = realloc(buf, len + s + 1)) == NULL) {
    173  1.3  drochner 			FUNLOCKFILE(fp);
    174  1.1     lukem 			free(buf);
    175  1.1     lukem 			return NULL;
    176  1.1     lukem 		}
    177  1.1     lukem 		buf = cp;
    178  1.1     lukem 
    179  1.1     lukem 		(void) memcpy(buf + len, ptr, s);
    180  1.1     lukem 		len += s;
    181  1.1     lukem 		buf[len] = '\0';
    182  1.1     lukem 	}
    183  1.1     lukem 
    184  1.3  drochner 	FUNLOCKFILE(fp);
    185  1.3  drochner 
    186  1.1     lukem 	if ((flags & FPARSELN_UNESCALL) != 0 && esc && buf != NULL &&
    187  1.1     lukem 	    strchr(buf, esc) != NULL) {
    188  1.1     lukem 		ptr = cp = buf;
    189  1.1     lukem 		while (cp[0] != '\0') {
    190  1.1     lukem 			int skipesc;
    191  1.1     lukem 
    192  1.1     lukem 			while (cp[0] != '\0' && cp[0] != esc)
    193  1.1     lukem 				*ptr++ = *cp++;
    194  1.1     lukem 			if (cp[0] == '\0' || cp[1] == '\0')
    195  1.1     lukem 				break;
    196  1.1     lukem 
    197  1.1     lukem 			skipesc = 0;
    198  1.1     lukem 			if (cp[1] == com)
    199  1.1     lukem 				skipesc += (flags & FPARSELN_UNESCCOMM);
    200  1.1     lukem 			if (cp[1] == con)
    201  1.1     lukem 				skipesc += (flags & FPARSELN_UNESCCONT);
    202  1.1     lukem 			if (cp[1] == esc)
    203  1.1     lukem 				skipesc += (flags & FPARSELN_UNESCESC);
    204  1.1     lukem 			if (cp[1] != com && cp[1] != con && cp[1] != esc)
    205  1.1     lukem 				skipesc = (flags & FPARSELN_UNESCREST);
    206  1.1     lukem 
    207  1.1     lukem 			if (skipesc)
    208  1.1     lukem 				cp++;
    209  1.1     lukem 			else
    210  1.1     lukem 				*ptr++ = *cp++;
    211  1.1     lukem 			*ptr++ = *cp++;
    212  1.1     lukem 		}
    213  1.1     lukem 		*ptr = '\0';
    214  1.1     lukem 		len = strlen(buf);
    215  1.1     lukem 	}
    216  1.1     lukem 
    217  1.1     lukem 	if (size)
    218  1.1     lukem 		*size = len;
    219  1.1     lukem 	return buf;
    220  1.1     lukem }
    221  1.1     lukem 
    222  1.1     lukem #ifdef TEST
    223  1.1     lukem 
    224  1.1     lukem int main(int, char **);
    225  1.1     lukem 
    226  1.1     lukem int
    227  1.1     lukem main(int argc, char **argv)
    228  1.1     lukem {
    229  1.1     lukem 	char   *ptr;
    230  1.1     lukem 	size_t	size, line;
    231  1.1     lukem 
    232  1.1     lukem 	line = 0;
    233  1.1     lukem 	while ((ptr = fparseln(stdin, &size, &line, NULL,
    234  1.1     lukem 	    FPARSELN_UNESCALL)) != NULL)
    235  1.1     lukem 		printf("line %d (%d) |%s|\n", line, size, ptr);
    236  1.1     lukem 	return 0;
    237  1.1     lukem }
    238  1.1     lukem 
    239  1.1     lukem /*
    240  1.1     lukem 
    241  1.1     lukem # This is a test
    242  1.1     lukem line 1
    243  1.1     lukem line 2 \
    244  1.1     lukem line 3 # Comment
    245  1.1     lukem line 4 \# Not comment \\\\
    246  1.1     lukem 
    247  1.1     lukem # And a comment \
    248  1.1     lukem line 5 \\\
    249  1.1     lukem line 6
    250  1.1     lukem 
    251  1.1     lukem */
    252  1.1     lukem 
    253  1.1     lukem #endif /* TEST */
    254  1.8   tnozaki #endif	/* ! HAVE_FPARSELN || BROKEN_FPARSELN */
    255