debuggers.hg

changeset 22779:0b04aea87667

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>
author Ian Jackson <Ian.Jackson@eu.citrix.com>
date Tue Jan 11 19:29:05 2011 +0000 (2011-01-11)
parents b80c530dac8c
children 1434b8515233
files tools/libxl/libxlu_cfg.c
line diff
     1.1 --- a/tools/libxl/libxlu_cfg.c	Tue Jan 11 19:28:22 2011 +0000
     1.2 +++ b/tools/libxl/libxlu_cfg.c	Tue Jan 11 19:29:05 2011 +0000
     1.3 @@ -18,59 +18,68 @@ XLU_Config *xlu_cfg_init(FILE *report, c
     1.4      return cfg;
     1.5  }
     1.6  
     1.7 -int xlu_cfg_readfile(XLU_Config *cfg, const char *real_filename) {
     1.8 -    CfgParseContext ctx;
     1.9 -    FILE *f;
    1.10 -    int e, r;
    1.11 +static int ctx_prep(CfgParseContext *ctx, XLU_Config *cfg) {
    1.12 +    int e;
    1.13  
    1.14 -    ctx.cfg= cfg;
    1.15 -    ctx.err= 0;
    1.16 -    ctx.lexerrlineno= -1;
    1.17 +    ctx->cfg= cfg;
    1.18 +    ctx->err= 0;
    1.19 +    ctx->lexerrlineno= -1;
    1.20 +    ctx->scanner= 0;
    1.21      
    1.22 -    f= fopen(real_filename, "r");
    1.23 -    if (!f) {
    1.24 -        e= errno;
    1.25 -        fprintf(cfg->report,"%s: unable to open configuration file: %s\n",
    1.26 -                real_filename, strerror(e));
    1.27 -        return e;
    1.28 -    }
    1.29 -
    1.30 -    e= xlu__cfg_yylex_init_extra(&ctx, &ctx.scanner);
    1.31 +    e= xlu__cfg_yylex_init_extra(ctx, &ctx->scanner);
    1.32      if (e) {
    1.33          fprintf(cfg->report,"%s: unable to create scanner: %s\n",
    1.34                  cfg->filename, strerror(e));
    1.35          return e;
    1.36      }
    1.37 +    return 0;
    1.38 +}
    1.39 +
    1.40 +static void ctx_dispose(CfgParseContext *ctx) {
    1.41 +    if (ctx->scanner) xlu__cfg_yylex_destroy(ctx->scanner);
    1.42 +}
    1.43 +
    1.44 +static void parse(CfgParseContext *ctx) {
    1.45 +    /* On return, ctx.err will be updated with the error status. */
    1.46 +    int r;
    1.47 +    r= xlu__cfg_yyparse(ctx);
    1.48 +    if (r) assert(ctx->err);
    1.49 +}
    1.50 +
    1.51 +int xlu_cfg_readfile(XLU_Config *cfg, const char *real_filename) {
    1.52 +    FILE *f = 0;
    1.53 +    int e;
    1.54 +
    1.55 +    CfgParseContext ctx;
    1.56 +    e = ctx_prep(&ctx, cfg);
    1.57 +    if (e) { ctx.err= e; goto xe; }
    1.58 +
    1.59 +    f= fopen(real_filename, "r");
    1.60 +    if (!f) {
    1.61 +        ctx.err = errno;
    1.62 +        fprintf(cfg->report,"%s: unable to open configuration file: %s\n",
    1.63 +                real_filename, strerror(e));
    1.64 +        goto xe;
    1.65 +    }
    1.66  
    1.67      xlu__cfg_yyrestart(f, ctx.scanner);
    1.68  
    1.69 -    r= xlu__cfg_yyparse(&ctx);
    1.70 -    if (r) assert(ctx.err);
    1.71 +    parse(&ctx);
    1.72  
    1.73 -    xlu__cfg_yylex_destroy(ctx.scanner);
    1.74 -    fclose(f);
    1.75 + xe:
    1.76 +    ctx_dispose(&ctx);
    1.77 +    if (f) fclose(f);
    1.78  
    1.79      return ctx.err;
    1.80  }
    1.81  
    1.82  int xlu_cfg_readdata(XLU_Config *cfg, const char *data, int length) {
    1.83 -    CfgParseContext ctx;
    1.84 -    int e, r;
    1.85 +    int e;
    1.86      YY_BUFFER_STATE buf= 0;
    1.87  
    1.88 -    ctx.scanner= 0;
    1.89 -    ctx.cfg= cfg;
    1.90 -    ctx.err= 0;
    1.91 -    ctx.lexerrlineno= -1;
    1.92 -
    1.93 -    e= xlu__cfg_yylex_init_extra(&ctx, &ctx.scanner);
    1.94 -    if (e) {
    1.95 -        fprintf(cfg->report,"%s: unable to create scanner: %s\n",
    1.96 -                cfg->filename, strerror(e));
    1.97 -        ctx.err= e;
    1.98 -        ctx.scanner= 0;
    1.99 -        goto xe;
   1.100 -    }
   1.101 +    CfgParseContext ctx;
   1.102 +    e= ctx_prep(&ctx, cfg);
   1.103 +    if (e) { ctx.err= e; goto xe; }
   1.104  
   1.105      buf = xlu__cfg_yy_scan_bytes(data, length, ctx.scanner);
   1.106      if (!buf) {
   1.107 @@ -80,12 +89,11 @@ int xlu_cfg_readdata(XLU_Config *cfg, co
   1.108          goto xe;
   1.109      }
   1.110  
   1.111 -    r= xlu__cfg_yyparse(&ctx);
   1.112 -    if (r) assert(ctx.err);
   1.113 +    parse(&ctx);
   1.114  
   1.115   xe:
   1.116      if (buf) xlu__cfg_yy_delete_buffer(buf, ctx.scanner);
   1.117 -    if (ctx.scanner) xlu__cfg_yylex_destroy(ctx.scanner);
   1.118 +    ctx_dispose(&ctx);
   1.119  
   1.120      return ctx.err;
   1.121  }