Mercurial > ~dholland > hg > tradcpp > index.cgi
view files.c @ 65:161edab0d361
test undef
author | David A. Holland |
---|---|
date | Sun, 31 Mar 2013 02:18:24 -0400 |
parents | 2e25e55dba6b |
children | 980ed7cb620a |
line wrap: on
line source
/*- * Copyright (c) 2010 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation * by David A. Holland. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <err.h> #include "array.h" #include "mode.h" #include "place.h" #include "files.h" #include "directive.h" struct incdir { const char *name; bool issystem; }; DECLARRAY(incdir, static __unused); DEFARRAY(incdir, static); static struct incdirarray quotepath, bracketpath; //////////////////////////////////////////////////////////// // management static struct incdir * incdir_create(const char *name, bool issystem) { struct incdir *id; id = domalloc(sizeof(*id)); id->name = name; id->issystem = issystem; return id; } static void incdir_destroy(struct incdir *id) { dofree(id, sizeof(*id)); } void files_init(void) { incdirarray_init("epath); incdirarray_init(&bracketpath); } DESTROYALL_ARRAY(incdir, ); void files_cleanup(void) { incdirarray_destroyall("epath); incdirarray_cleanup("epath); incdirarray_destroyall(&bracketpath); incdirarray_cleanup(&bracketpath); } //////////////////////////////////////////////////////////// // path setup void files_addquotepath(const char *dir, bool issystem) { struct incdir *id; id = incdir_create(dir, issystem); incdirarray_add("epath, id, NULL); } void files_addbracketpath(const char *dir, bool issystem) { struct incdir *id; id = incdir_create(dir, issystem); incdirarray_add(&bracketpath, id, NULL); } //////////////////////////////////////////////////////////// // parsing static size_t findnl(const char *buf, size_t start, size_t limit) { size_t i; for (i=start; i<limit; i++) { if (buf[i] == '\n') { return i; } } return limit; } static void file_read(const struct placefile *pf, int fd, const char *name, bool toplevel) { struct place linestartplace, nextlinestartplace, ptmp; size_t bufend, bufmax, linestart, lineend, nextlinestart, tmp; ssize_t result; bool ateof = false; char *buf; place_setfilestart(&linestartplace, pf); nextlinestartplace = linestartplace; bufmax = 128; bufend = 0; linestart = 0; lineend = 0; buf = domalloc(bufmax); while (1) { if (lineend >= bufend) { /* do not have a whole line in the buffer; read more */ if (linestart > 0 && bufend > linestart) { /* slide to beginning of buffer */ memmove(buf, buf+linestart, bufend-linestart); bufend -= linestart; lineend -= linestart; linestart = 0; } if (bufend >= bufmax) { /* need bigger buffer */ buf = dorealloc(buf, bufmax, bufmax*2); bufmax = bufmax*2; } if (ateof) { /* don't read again, in case it's a socket */ result = 0; } else { result = read(fd, buf+bufend, bufmax - bufend); } if (result == -1) { /* read error */ warn("%s", name); complain_fail(); } else if (result == 0 && bufend == linestart) { /* eof */ ateof = true; break; } else if (result == 0) { /* eof in middle of line */ ateof = true; ptmp = linestartplace; ptmp.column += bufend - linestart; complain(&ptmp, "No newline at end of file"); if (mode.werror) { complain_fail(); } assert(bufend < bufmax); lineend = bufend++; buf[lineend] = '\n'; } else { tmp = bufend; bufend += (size_t)result; lineend = findnl(buf, tmp, bufend); } /* loop in case we still don't have a whole line */ continue; } /* have a line */ assert(buf[lineend] == '\n'); buf[lineend] = '\0'; nextlinestart = lineend+1; nextlinestartplace.line++; /* check for CR/NL */ if (lineend > 0 && buf[lineend-1] == '\r') { buf[lineend-1] = '\0'; lineend--; } /* check for continuation line */ if (lineend > 0 && buf[lineend-1]=='\\') { lineend--; tmp = nextlinestart - lineend; if (bufend > nextlinestart) { memmove(buf+lineend, buf+nextlinestart, bufend - nextlinestart); } bufend -= tmp; nextlinestart -= tmp; lineend = findnl(buf, lineend, bufend); /* might not have a whole line, so loop */ continue; } /* line now goes from linestart to lineend */ assert(buf[lineend] == '\0'); if (lineend > linestart) { directive_gotline(&linestartplace, buf+linestart, lineend-linestart); } linestart = nextlinestart; lineend = findnl(buf, linestart, bufend); linestartplace = nextlinestartplace; } if (toplevel) { directive_goteof(&linestartplace); } dofree(buf, bufmax); } //////////////////////////////////////////////////////////// // path search static char * mkfilename(const char *dir, const char *file) { size_t dlen, flen, rlen; char *ret; bool needslash = false; dlen = strlen(dir); flen = strlen(file); if (dlen > 0 && dir[dlen-1] != '/') { needslash = true; } rlen = dlen + (needslash ? 1 : 0) + flen; ret = domalloc(rlen + 1); strcpy(ret, dir); if (needslash) { strcat(ret, "/"); } strcat(ret, file); return ret; } static int file_tryopen(const char *file) { int fd; /* XXX check for non-regular files */ fd = open(file, O_RDONLY); if (fd < 0) { return -1; } return fd; } static void file_search(struct place *place, struct incdirarray *path, const char *name) { unsigned i, num; struct incdir *id; const struct placefile *pf; char *file; int fd; assert(place != NULL); num = incdirarray_num(path); for (i=0; i<num; i++) { id = incdirarray_get(path, i); file = mkfilename(id->name, name); fd = file_tryopen(file); if (fd >= 0) { pf = place_addfile(place, file, id->issystem); file_read(pf, fd, file, false); dostrfree(file); close(fd); return; } dostrfree(file); } complain(place, "Include file %s not found", name); complain_fail(); } void file_readquote(struct place *place, const char *name) { file_search(place, "epath, name); } void file_readbracket(struct place *place, const char *name) { file_search(place, &bracketpath, name); } void file_readabsolute(struct place *place, const char *name) { const struct placefile *pf; int fd; assert(place != NULL); if (name == NULL) { fd = STDIN_FILENO; pf = place_addfile(place, "<standard-input>", false); } else { fd = file_tryopen(name); if (fd < 0) { warn("%s", name); die(); } pf = place_addfile(place, name, false); } file_read(pf, fd, name, true); if (name != NULL) { close(fd); } }