/* * Copyright (c) 1998, 1999, 2000, 2001, 2002, 2005, 2010, 2011, * 2014, 2015 * Tama Communications Corporation * * This file is part of GNU GLOBAL. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #ifdef HAVE_CONFIG_H #include #endif #include #include #ifdef STDC_HEADERS #include #endif #ifdef HAVE_STRING_H #include #else #include #endif #if defined(_WIN32) && !defined(__CYGWIN__) #define WIN32_LEAN_AND_MEAN #include #endif #include "gparam.h" #include "char.h" #include "checkalloc.h" #include "conf.h" #include "die.h" #include "env.h" #include "langmap.h" #include "locatestring.h" #include "makepath.h" #include "path.h" #include "strbuf.h" #include "strlimcpy.h" #include "strmake.h" #include "test.h" #include "usable.h" static FILE *fp; static STRBUF *ib; static char *confline; static char *config_path; static char *config_label; /** * 32 level nested tc= or include= is allowed. */ static int allowed_nest_level = 32; static int opened; static void trim(char *); static const char *readrecord(FILE *, const char *); static void includelabel(FILE *, STRBUF *, const char *, int); #ifndef isblank #define isblank(c) ((c) == ' ' || (c) == '\t') #endif /** * trim: trim string. * * : var1=a b : * | * v * :var1=a b : */ static void trim(char *l) { char *f, *b; int colon = 0; /* * delete blanks. */ for (f = b = l; *f; f++) { if (colon && isblank(*f)) continue; colon = 0; if ((*b++ = *f) == ':') colon = 1; } *b = 0; /* * delete duplicate semi colons. */ for (f = b = l; *f;) { if ((*b++ = *f++) == ':') { while (*f == ':') f++; } } *b = 0; } /** * readrecord: read recoed indexed by label. * * @param[in] label label in config file * @return record or NULL * * Jobs: * - skip comment. * - append following line. * - format check. */ static const char * readrecord(FILE *fp, const char *label) { char *p; int flag = STRBUF_NOCRLF|STRBUF_SHARPSKIP; int count = 0; rewind(fp); while ((p = strbuf_fgets(ib, fp, flag)) != NULL) { count++; /* * ignore \. */ flag &= ~STRBUF_APPEND; if (*p == '\0') continue; if (strbuf_unputc(ib, '\\')) { flag |= STRBUF_APPEND; continue; } trim(p); for (;;) { const char *candidate; /* * pick up candidate. */ if ((candidate = strmake(p, "|:")) == NULL) die("invalid config file format (%s line: %d).", config_path, count); if (!strcmp(label, candidate)) { if (!(p = locatestring(p, ":", MATCH_FIRST))) die("invalid config file format (%s line: %d).", config_path, count); return check_strdup(p); } /* * locate next candidate. */ p += strlen(candidate); if (*p == ':') break; else if (*p == '|') p++; else die("invalid config file format (%s line: %d).", config_path, count); } } /* * config line not found. */ return NULL; } /** * includelabel: procedure for tc= (or include=) * * @param[in] fp file pointer * @param[out] sb string buffer * @param[in] label record label * @param[in] level nest level for check * * This function may call itself (recursive) */ static void includelabel(FILE *fp, STRBUF *sb, const char *label, int level) { const char *savep, *p, *q; char *file; if (++level > allowed_nest_level) die("nested include= (or tc=) over flow in '%s'.", config_path); /* * Label can include a '@' and a following path name. * Label: