aboutsummaryrefslogtreecommitdiffstats
path: root/tools/libxl/libxlu_cfg.c
diff options
context:
space:
mode:
authorIan Jackson <Ian.Jackson@eu.citrix.com>2011-01-11 19:29:05 +0000
committerIan Jackson <Ian.Jackson@eu.citrix.com>2011-01-11 19:29:05 +0000
commit6615a0a173a5e37b190be8f64c316b3ea1db8ed9 (patch)
treeb1d510109cc8d59e65a9c112bf1774606a0d7110 /tools/libxl/libxlu_cfg.c
parent1d80237d14c9bf7be01325765c35b5809ce0030e (diff)
downloadxen-6615a0a173a5e37b190be8f64c316b3ea1db8ed9.tar.gz
xen-6615a0a173a5e37b190be8f64c316b3ea1db8ed9.tar.bz2
xen-6615a0a173a5e37b190be8f64c316b3ea1db8ed9.zip
libxl: config parser: make CfgParseContext initialisation common
xlu_cfg_readfile and xlu_cfg_readdata had some somewhat-boilerplate code for initialisation, parsing, and cleanup. Make that common. No functional change. Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
Diffstat (limited to 'tools/libxl/libxlu_cfg.c')
-rw-r--r--tools/libxl/libxlu_cfg.c82
1 files changed, 45 insertions, 37 deletions
diff --git a/tools/libxl/libxlu_cfg.c b/tools/libxl/libxlu_cfg.c
index 1ade7eacda..821b8c04c4 100644
--- a/tools/libxl/libxlu_cfg.c
+++ b/tools/libxl/libxlu_cfg.c
@@ -18,59 +18,68 @@ XLU_Config *xlu_cfg_init(FILE *report, const char *report_filename) {
return cfg;
}
+static int ctx_prep(CfgParseContext *ctx, XLU_Config *cfg) {
+ int e;
+
+ ctx->cfg= cfg;
+ ctx->err= 0;
+ ctx->lexerrlineno= -1;
+ ctx->scanner= 0;
+
+ e= xlu__cfg_yylex_init_extra(ctx, &ctx->scanner);
+ if (e) {
+ fprintf(cfg->report,"%s: unable to create scanner: %s\n",
+ cfg->filename, strerror(e));
+ return e;
+ }
+ return 0;
+}
+
+static void ctx_dispose(CfgParseContext *ctx) {
+ if (ctx->scanner) xlu__cfg_yylex_destroy(ctx->scanner);
+}
+
+static void parse(CfgParseContext *ctx) {
+ /* On return, ctx.err will be updated with the error status. */
+ int r;
+ r= xlu__cfg_yyparse(ctx);
+ if (r) assert(ctx->err);
+}
+
int xlu_cfg_readfile(XLU_Config *cfg, const char *real_filename) {
+ FILE *f = 0;
+ int e;
+
CfgParseContext ctx;
- FILE *f;
- int e, r;
+ e = ctx_prep(&ctx, cfg);
+ if (e) { ctx.err= e; goto xe; }
- ctx.cfg= cfg;
- ctx.err= 0;
- ctx.lexerrlineno= -1;
-
f= fopen(real_filename, "r");
if (!f) {
- e= errno;
+ ctx.err = errno;
fprintf(cfg->report,"%s: unable to open configuration file: %s\n",
real_filename, strerror(e));
- return e;
- }
-
- e= xlu__cfg_yylex_init_extra(&ctx, &ctx.scanner);
- if (e) {
- fprintf(cfg->report,"%s: unable to create scanner: %s\n",
- cfg->filename, strerror(e));
- return e;
+ goto xe;
}
xlu__cfg_yyrestart(f, ctx.scanner);
- r= xlu__cfg_yyparse(&ctx);
- if (r) assert(ctx.err);
+ parse(&ctx);
- xlu__cfg_yylex_destroy(ctx.scanner);
- fclose(f);
+ xe:
+ ctx_dispose(&ctx);
+ if (f) fclose(f);
return ctx.err;
}
int xlu_cfg_readdata(XLU_Config *cfg, const char *data, int length) {
- CfgParseContext ctx;
- int e, r;
+ int e;
YY_BUFFER_STATE buf= 0;
- ctx.scanner= 0;
- ctx.cfg= cfg;
- ctx.err= 0;
- ctx.lexerrlineno= -1;
-
- e= xlu__cfg_yylex_init_extra(&ctx, &ctx.scanner);
- if (e) {
- fprintf(cfg->report,"%s: unable to create scanner: %s\n",
- cfg->filename, strerror(e));
- ctx.err= e;
- ctx.scanner= 0;
- goto xe;
- }
+ CfgParseContext ctx;
+ e= ctx_prep(&ctx, cfg);
+ if (e) { ctx.err= e; goto xe; }
buf = xlu__cfg_yy_scan_bytes(data, length, ctx.scanner);
if (!buf) {
@@ -80,12 +89,11 @@ int xlu_cfg_readdata(XLU_Config *cfg, const char *data, int length) {
goto xe;
}
- r= xlu__cfg_yyparse(&ctx);
- if (r) assert(ctx.err);
+ parse(&ctx);
xe:
if (buf) xlu__cfg_yy_delete_buffer(buf, ctx.scanner);
- if (ctx.scanner) xlu__cfg_yylex_destroy(ctx.scanner);
+ ctx_dispose(&ctx);
return ctx.err;
}