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