summaryrefslogtreecommitdiff
path: root/src/driver
diff options
context:
space:
mode:
authormidipix <writeonce@midipix.org>2015-12-18 03:14:18 -0500
committermidipix <writeonce@midipix.org>2016-11-10 23:35:33 -0500
commit99763fff233dab9455ad317fe7e2813d40cd2871 (patch)
treea4c726251ed881aa6e5925d791847b65e3282c1c /src/driver
parent03eeb04dfb135fcbcea8fb4ee32199d1ec97fe2a (diff)
downloadperk-99763fff233dab9455ad317fe7e2813d40cd2871.tar.bz2
perk-99763fff233dab9455ad317fe7e2813d40cd2871.tar.xz
driver: improve common context initialization logic.
Diffstat (limited to 'src/driver')
-rw-r--r--src/driver/pe_driver_ctx.c22
1 files changed, 13 insertions, 9 deletions
diff --git a/src/driver/pe_driver_ctx.c b/src/driver/pe_driver_ctx.c
index 82379ce..1508262 100644
--- a/src/driver/pe_driver_ctx.c
+++ b/src/driver/pe_driver_ctx.c
@@ -52,7 +52,10 @@ static int pe_driver_usage(
return PERK_USAGE;
}
-static struct pe_driver_ctx_impl * pe_driver_ctx_alloc(struct argv_meta * meta, size_t nunits)
+static struct pe_driver_ctx_impl * pe_driver_ctx_alloc(
+ struct argv_meta * meta,
+ const struct pe_common_ctx * cctx,
+ size_t nunits)
{
struct pe_driver_ctx_alloc * ictx;
size_t size;
@@ -65,6 +68,9 @@ static struct pe_driver_ctx_impl * pe_driver_ctx_alloc(struct argv_meta * meta,
if (!(ictx = calloc(size,1)))
return 0;
+ if (cctx)
+ memcpy(&ictx->ctx.cctx,cctx,sizeof(*cctx));
+
ictx->meta = meta;
ictx->ctx.cctx.symctx = &ictx->ctx.symctx;
ictx->ctx.cctx.outctx = &ictx->ctx.outctx;
@@ -157,18 +163,19 @@ int pe_get_driver_ctx(
S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0))
return pe_get_driver_ctx_fail(meta);
- if (!(ctx = pe_driver_ctx_alloc(meta,nunits)) && cctx.output)
+ if (pretty && !strcmp(pretty,"yaml"))
+ cctx.fmtflags |= PERK_PRETTY_YAML;
+
+ if (!(ctx = pe_driver_ctx_alloc(meta,&cctx,nunits)) && cctx.output)
close(fdout);
if (!ctx)
return pe_get_driver_ctx_fail(meta);
- if (pretty && !strcmp(pretty,"yaml"))
- cctx.fmtflags |= PERK_PRETTY_YAML;
-
ctx->ctx.program = program;
- ctx->ioctx.fdout = cctx.output ? fdout : -1;
+ ctx->ctx.cctx = &ctx->cctx;
+ ctx->ioctx.fdout = cctx.output ? fdout : -1;
ctx->ioctx.fdin = -1;
ctx->ioctx.fderr = -1;
ctx->ioctx.fdlog = -1;
@@ -176,9 +183,6 @@ int pe_get_driver_ctx(
ctx->ioctx.fddst = AT_FDCWD;
ctx->ioctx.fdtmp = AT_FDCWD;
- ctx->ctx.cctx = &ctx->cctx;
-
- memcpy(&ctx->cctx,&cctx,sizeof(cctx));
*pctx = &ctx->ctx;
return PERK_OK;
}