/* ** OSSP cfg - Configuration Parsing ** Copyright (c) 1999-2002 Ralf S. Engelschall ** Copyright (c) 1999-2002 The OSSP Project (http://www.ossp.org/) ** Copyright (c) 2001-2002 Cable & Wireless Deutschland (http://www.cw.com/de/) ** ** This file is part of OSSP cfg, a configuration parsing ** library which can be found at http://www.ossp.org/pkg/lib/cfg/. ** ** Permission to use, copy, modify, and distribute this software for ** any purpose with or without fee is hereby granted, provided that ** the above copyright notice and this permission notice appear in all ** copies. ** ** THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 AUTHORS AND COPYRIGHT HOLDERS AND THEIR ** 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. ** ** cfg_syn.c: syntax parsing */ #include #include "cfg.h" #include "cfg_grid.h" #include "cfg_node.h" #include "cfg_fmt.h" #include "cfg_syn.h" #include "cfg_buf.h" /* prototypes for Flex-generated scanner */ extern int cfg_syn_lex_init(void *); extern int cfg_syn_lex_destroy(void *); extern void cfg_syn_set_extra(void *, void *); /* prototypes for Bison-generated parser */ extern int cfg_syn_parse(void *); /* build a node tree according to a textual specification */ cfg_rc_t cfg_syn_import( cfg_t *cfg, cfg_node_t **node, const char *input, char *err_buf, size_t err_len) { cfg_syn_ctx_t ctx; void *yyscan; /* argument sanity checking */ if (node == NULL || input == NULL) return CFG_ERR_ARG; /* initialize scanner */ cfg_syn_lex_init(&yyscan); cfg_syn_set_extra(&ctx, yyscan); /* establish our own context which is passed through the parser and scanner */ ctx.inputptr = input; ctx.inputbuf = input; ctx.inputlen = strlen(input); ctx.cfg = cfg; ctx.node = NULL; ctx.rv = CFG_OK; ctx.err_buf = err_buf; ctx.err_len = err_len; ctx.yyscan = yyscan; /* start the parser loop */ if (cfg_syn_parse(&ctx)) ctx.rv = (ctx.rv == CFG_OK ? CFG_ERR_INT : ctx.rv); /* destroy scanner */ cfg_syn_lex_destroy(yyscan); /* provide root/top-level node as result */ *node = ctx.node; return ctx.rv; } /* remember a parsing error (used internally) */ void cfg_syn_error(cfg_syn_ctx_t *ctx, cfg_rc_t rv, YYLTYPE *loc, const char *fmt, ...) { va_list ap; const char *cpF, *cpL; const char *cpP, *cpE; int line, column; char *cpBuf; char *cp; size_t n; /* remember error code */ ctx->rv = rv; /* short circuit processing if no error buffer exists */ if (ctx->err_buf == NULL || ctx->err_len <= 0) return; /* determine first and last positions of token */ cpF = ctx->inputbuf+loc->first; cpL = ctx->inputbuf+loc->last; /* determine epilog and prolog of token */ cpP = cpF-4; if (cpP < ctx->inputbuf) cpP = ctx->inputbuf; cpE = cpL+4; if (cpE > ctx->inputbuf+ctx->inputlen) cpE = ctx->inputbuf+ctx->inputlen; /* calculate line and column of token */ line = 1; column = 1; for (cp = (char *)ctx->inputbuf; cp < (ctx->inputbuf+ctx->inputlen) && cp != cpF; cp++) { column++; if (*cp == '\n') { column = 1; line++; } } /* extract token context with mark token borders */ if ((cpBuf = malloc((size_t)((cpE-cpP)+2+1))) == NULL) return; cp = cpBuf; n = cpF-cpP; memcpy(cp, cpP, n); cp += n; *cp++ = '<'; n = cpL-cpF; memcpy(cp, cpF, n); cp += n; *cp++ = '>'; n = cpE-cpL; memcpy(cp, cpL, n); cp += n; *cp++ = '\0'; /* remember parsing error: part I (context part) */ cfg_fmt_sprintf(ctx->err_buf, sizeof(ctx->err_len), "line %d, column %d: `%s'; %s", line, column, cpBuf); free(cpBuf); /* remember parsing error: part II (parsing part) */ n = strlen(ctx->err_buf); va_start(ap, fmt); cfg_fmt_vsprintf(ctx->err_buf+n, sizeof(ctx->err_len)-n, fmt, ap); va_end(ap); return; } typedef struct { cfg_buf_t *buf; int indent; } export_t; static void export_format(export_t *ctx, char *fmt, ...) { int i; char *cp; va_list ap; char *str; va_start(ap, fmt); if ((str = cfg_fmt_vasprintf(fmt, ap)) == NULL) return; while ((cp = strchr(str, '\n')) != NULL) { cfg_buf_format(ctx->buf, "%.*s", cp-str+1, str); for (i = 0; i < ctx->indent; i++) cfg_buf_format(ctx->buf, " "); str = cp+1; } if (str[0] != '\0') cfg_buf_format(ctx->buf, "%s", str); free(str); va_end(ap); return; } static void export_indent(export_t *ctx, int n) { if (n > 0) { while (n > 0) { n--; ctx->indent++; cfg_buf_format(ctx->buf, " "); } } else { while (n < 0 && ctx->indent > 0) { n++; ctx->indent--; cfg_buf_resize(ctx->buf, -4); } } } static void export_token(export_t *ctx, const char *token) { if (strcspn(token, " {};\\") != strlen(token)) export_format(ctx, "\"%s\"", token); else export_format(ctx, "%s", token); return; } static void export_node(export_t *ctx, cfg_node_t *node) { cfg_node_type_t type; cfg_node_t *node2; cfg_rc_t rc; char *token; rc = cfg_node_get(node, CFG_NODE_ATTR_TYPE, &type); if (type == CFG_NODE_TYPE_SEQ) { export_format(ctx, "{\n"); export_indent(ctx, 1); cfg_node_goto(node, CFG_NODE_GOTO_CHILD1, &node2); while (node2 != NULL) { export_node(ctx, node2); cfg_node_goto(node2, CFG_NODE_GOTO_RBROTH, &node2); } export_indent(ctx, -1); export_format(ctx, "}"); } else if (type == CFG_NODE_TYPE_DIR) { cfg_node_goto(node, CFG_NODE_GOTO_CHILD1, &node2); while (node2 != NULL) { export_node(ctx, node2); cfg_node_goto(node2, CFG_NODE_GOTO_RBROTH, &node2); if (node2 != NULL) export_format(ctx, " "); } export_format(ctx, ";\n"); } else if (type == CFG_NODE_TYPE_TOK) { cfg_node_get(node, CFG_NODE_ATTR_TOKEN, &token); if (token != NULL) export_token(ctx, token); else export_format(ctx, ""); } return; } cfg_rc_t cfg_syn_export( cfg_t *cfg, cfg_node_t *node, char **output) { cfg_buf_t *buf; cfg_rc_t rc; export_t ctx; if (node == NULL || output == NULL) return CFG_ERR_ARG; if ((rc = cfg_buf_create(&buf)) != CFG_OK) return rc; ctx.buf = buf; ctx.indent = 0; export_node(&ctx, node); cfg_buf_content(buf, output, NULL, NULL); cfg_buf_destroy(buf); return CFG_OK; }