ossp-pkg/cfg/cfg_syn.c
1.1
/*
** OSSP cfg - Configuration Parsing
** Copyright (c) 1999-2002 Ralf S. Engelschall <rse@engelschall.com>
** 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 "cfg.h"
#include "cfg_grid.h"
#include "cfg_node.h"
#include "cfg_fmt.h"
#include "cfg_syn.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(cfg_t *cfg, cfg_node_t **node, char *err_buf, size_t err_len, const char *input)
{
cfg_syn_ctx_t ctx;
void *yyscan;
/* 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;
}