287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov/* $NetBSD: fparseln.c,v 1.9 1999/09/20 04:48:06 lukem Exp $ */
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * Copyright (c) 1997 Christos Zoulas. All rights reserved.
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * Redistribution and use in source and binary forms, with or without
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * modification, are permitted provided that the following conditions
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * 1. Redistributions of source code must retain the above copyright
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * notice, this list of conditions and the following disclaimer.
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * 2. Redistributions in binary form must reproduce the above copyright
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * notice, this list of conditions and the following disclaimer in the
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * documentation and/or other materials provided with the distribution.
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * 3. All advertising materials mentioning features or use of this software
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * must display the following acknowledgement:
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * This product includes software developed by Christos Zoulas.
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * 4. The name of the author may not be used to endorse or promote products
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * derived from this software without specific prior written permission.
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalovstatic int isescaped(const char *, const char *, int);
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * isescaped():
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * Return true if the character in *p that belongs to a string
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * that starts in *sp, is escaped by the escape character esc.
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalovisescaped(const char *sp, const char *p, int esc)
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov /* No escape character */
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov /* Count the number of escape characters that precede ours */
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov for (ne = 0, cp = p; --cp >= sp && *cp == esc; ne++)
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov /* Return true if odd number of escape characters */
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * Read a line from a file parsing continuations ending in \
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * and eliminating trailing newlines, or comments starting with
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * the comment char.
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalovfparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov static const char dstr[3] = { '\\', '\\', '#' };
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * XXX: it would be cool to be able to specify the newline character,
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * but unfortunately, fgetln does not let us
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov if (s && com) { /* Check and eliminate comments */
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov if (*cp == com && !isescaped(ptr, cp, esc)) {
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov if (s && nl) { /* Check and eliminate newlines */
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov s--; /* forget newline */
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov if (s && con) { /* Check and eliminate continuations */
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov if (*cp == con && !isescaped(ptr, cp, esc)) {
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov s--; /* forget escape */
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov if ((cp = realloc(buf, len + s + 1)) == NULL) {
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov if ((flags & FPARSELN_UNESCALL) != 0 && esc && buf != NULL &&
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov if (cp[1] != com && cp[1] != con && cp[1] != esc)
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov while ((ptr = fparseln(stdin, &size, &line, NULL,
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov printf("line %d (%d) |%s|\n", line, size, ptr);
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * # This is a test
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * line 3 # Comment
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * line 4 \# Not comment \\\\
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov * # And a comment \
287247a826fa2ab8d01f6c8f276d405eb08420f8Alexander Pyhalov#endif /* TEST */