From f1dbc60ab6cc4bc2422441c9028c7edc56684b82 Mon Sep 17 00:00:00 2001 From: midipix Date: Mon, 30 Jul 2018 11:12:23 +0000 Subject: driver, library interfaces: support alternate fd's for input/output/error/log. --- src/driver/mdso_amain.c | 20 ++++++--- src/driver/mdso_driver_ctx.c | 101 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 101 insertions(+), 20 deletions(-) (limited to 'src/driver') diff --git a/src/driver/mdso_amain.c b/src/driver/mdso_amain.c index 70fb44a..02daefb 100644 --- a/src/driver/mdso_amain.c +++ b/src/driver/mdso_amain.c @@ -6,9 +6,11 @@ #include #include + #include #include #include "mdso_driver_impl.h" +#include "mdso_dprintf_impl.h" #ifndef MDSO_DRIVER_FLAGS #define MDSO_DRIVER_FLAGS MDSO_DRIVER_VERBOSITY_ERRORS \ @@ -31,15 +33,16 @@ static const char * const mdso_ver_plain[6] = { "","" }; -static ssize_t mdso_version(struct mdso_driver_ctx * dctx) +static ssize_t mdso_version(int fdout, struct mdso_driver_ctx * dctx) { const struct mdso_source_version * verinfo; const char * const * verclr; verinfo = mdso_source_version(); - verclr = isatty(STDOUT_FILENO) ? mdso_ver_color : mdso_ver_plain; + verclr = isatty(fdout) ? mdso_ver_color : mdso_ver_plain; - return fprintf(stdout,vermsg, + return mdso_dprintf( + fdout,vermsg, verclr[0],dctx->program,verclr[1], verclr[2],verinfo->major,verinfo->minor, verinfo->revision,verclr[3], @@ -62,20 +65,25 @@ static int mdso_exit(struct mdso_driver_ctx * dctx, int ret) return ret; } -int mdso_main(int argc, char ** argv, char ** envp) +int mdso_main(int argc, char ** argv, char ** envp, const struct mdso_fd_ctx * fdctx) { int ret; + int fdout; + uint64_t flags; struct mdso_driver_ctx * dctx; struct mdso_unit_ctx * uctx; const char ** unit; - if ((ret = mdso_get_driver_ctx(argv,envp,MDSO_DRIVER_FLAGS,&dctx))) + flags = MDSO_DRIVER_FLAGS; + fdout = fdctx ? fdctx->fdout : STDOUT_FILENO; + + if ((ret = mdso_get_driver_ctx(argv,envp,flags,fdctx,&dctx))) return (ret == MDSO_USAGE) ? !--argc : MDSO_ERROR; if (dctx->cctx->drvflags & MDSO_DRIVER_VERSION) - if ((mdso_version(dctx)) < 0) + if ((mdso_version(fdout,dctx)) < 0) return mdso_exit(dctx,MDSO_ERROR); if (dctx->cctx->implib) diff --git a/src/driver/mdso_driver_ctx.c b/src/driver/mdso_driver_ctx.c index 6362d32..4b79c1e 100644 --- a/src/driver/mdso_driver_ctx.c +++ b/src/driver/mdso_driver_ctx.c @@ -57,6 +57,7 @@ static uint32_t mdso_argv_flags(uint32_t flags) } static int mdso_driver_usage( + int fdout, const char * program, const char * arg, const struct argv_option ** optv, @@ -68,7 +69,7 @@ static int mdso_driver_usage( "Usage: %s [options] ...\n" "Options:\n", program); - argv_usage(STDOUT_FILENO,header,optv,arg); + argv_usage(fdout,header,optv,arg); argv_free(meta); return MDSO_USAGE; @@ -76,6 +77,7 @@ static int mdso_driver_usage( static struct mdso_driver_ctx_impl * mdso_driver_ctx_alloc( struct argv_meta * meta, + const struct mdso_fd_ctx * fdctx, const struct mdso_common_ctx * cctx, size_t nunits) { @@ -91,8 +93,8 @@ static struct mdso_driver_ctx_impl * mdso_driver_ctx_alloc( if (!(ictx = calloc(1,size))) return 0; - if (cctx) - memcpy(&ictx->ctx.cctx,cctx,sizeof(*cctx)); + memcpy(&ictx->ctx.fdctx,fdctx,sizeof(*fdctx)); + memcpy(&ictx->ctx.cctx,cctx,sizeof(*cctx)); for (entry=meta->entries,units=ictx->units; entry->fopt || entry->arg; entry++) if (!entry->fopt) @@ -154,10 +156,11 @@ static int mdso_get_driver_ctx_fail( } int mdso_get_driver_ctx( - char ** argv, - char ** envp, - uint32_t flags, - struct mdso_driver_ctx ** pctx) + char ** argv, + char ** envp, + uint32_t flags, + const struct mdso_fd_ctx * fdctx, + struct mdso_driver_ctx ** pctx) { struct mdso_driver_ctx_impl * ctx; struct mdso_common_ctx cctx; @@ -175,12 +178,23 @@ int mdso_get_driver_ctx( (void)envp; + if (!fdctx) { + fdctx = &(const struct mdso_fd_ctx) { + .fdin = STDIN_FILENO, + .fdout = STDOUT_FILENO, + .fderr = STDERR_FILENO, + .fdlog = (-1), + .fdcwd = AT_FDCWD, + .fddst = AT_FDCWD, + }; + } + argv_optv_init(mdso_default_options,optv); if (!(meta = argv_get( argv,optv, mdso_argv_flags(flags), - STDERR_FILENO))) + fdctx->fderr))) return -1; /* cctx init, option defaults */ @@ -198,7 +212,10 @@ int mdso_get_driver_ctx( cctx.dsoflags = MDSO_FLAG_LOADER_PATH; if (!argv[1] && (flags & MDSO_DRIVER_VERBOSITY_USAGE)) - return mdso_driver_usage(program,0,optv,meta); + return mdso_driver_usage( + fdctx->fderr, + program,0, + optv,meta); /* get options, count units */ for (entry=meta->entries; entry->fopt || entry->arg; entry++) { @@ -206,7 +223,11 @@ int mdso_get_driver_ctx( switch (entry->tag) { case TAG_HELP: if (flags & MDSO_DRIVER_VERBOSITY_USAGE) - return mdso_driver_usage(program,entry->arg,optv,meta); + return mdso_driver_usage( + fdctx->fdout, + program, + entry->arg, + optv,meta); case TAG_VERSION: cctx.drvflags |= MDSO_DRIVER_VERSION; @@ -281,7 +302,10 @@ int mdso_get_driver_ctx( if (!nunits && !(cctx.drvflags & MDSO_DRIVER_VERSION)) - return mdso_driver_usage(program,0,optv,meta); + return mdso_driver_usage( + fdctx->fderr, + program,0, + optv,meta); if (pretty && !strcmp(pretty,"yaml")) cctx.fmtflags |= MDSO_PRETTY_YAML; @@ -301,7 +325,7 @@ int mdso_get_driver_ctx( if (cctx.dstdir && (fddst = mdso_dstdir_open(&cctx,asmbase)) < 0) return mdso_get_driver_ctx_fail(meta,implib,asmbase,fddst); - if (!(ctx = mdso_driver_ctx_alloc(meta,&cctx,nunits))) + if (!(ctx = mdso_driver_ctx_alloc(meta,fdctx,&cctx,nunits))) return mdso_get_driver_ctx_fail(meta,implib,asmbase,fddst); ctx->implib = implib; @@ -318,6 +342,7 @@ int mdso_get_driver_ctx( int mdso_create_driver_ctx( const struct mdso_common_ctx * cctx, + const struct mdso_fd_ctx * fdctx, struct mdso_driver_ctx ** pctx) { const struct argv_option * optv[MDSO_OPTV_ELEMENTS]; @@ -326,15 +351,26 @@ int mdso_create_driver_ctx( int fddst = -1; char * argv[] = {"mdso_driver",0}; + if (!fdctx) { + fdctx = &(const struct mdso_fd_ctx) { + .fdin = STDIN_FILENO, + .fdout = STDOUT_FILENO, + .fderr = STDERR_FILENO, + .fdlog = (-1), + .fdcwd = AT_FDCWD, + .fddst = AT_FDCWD, + }; + } + argv_optv_init(mdso_default_options,optv); - if (!(meta = argv_get(argv,optv,0,STDERR_FILENO))) + if (!(meta = argv_get(argv,optv,0,fdctx->fderr))) return -1; if (cctx->dstdir && (fddst = mdso_dstdir_open(cctx,cctx->asmbase)) < 0) return mdso_get_driver_ctx_fail(meta,0,0,fddst); - if (!(ctx = mdso_driver_ctx_alloc(meta,cctx,0))) + if (!(ctx = mdso_driver_ctx_alloc(meta,fdctx,cctx,0))) return mdso_get_driver_ctx_fail(meta,0,0,fddst); if (!ctx->cctx.dsoflags) @@ -380,3 +416,40 @@ const struct mdso_source_version * mdso_source_version(void) { return &mdso_src_version; } + + +int mdso_get_driver_fdctx( + const struct mdso_driver_ctx * dctx, + struct mdso_fd_ctx * fdctx) +{ + struct mdso_driver_ctx_impl * ictx; + + ictx = mdso_get_driver_ictx(dctx); + + fdctx->fdin = ictx->fdctx.fdin; + fdctx->fdout = ictx->fdctx.fdout; + fdctx->fderr = ictx->fdctx.fderr; + fdctx->fdlog = ictx->fdctx.fdlog; + fdctx->fdcwd = ictx->fdctx.fdcwd; + fdctx->fddst = ictx->fdctx.fddst; + + return 0; +} + +int mdso_set_driver_fdctx( + struct mdso_driver_ctx * dctx, + const struct mdso_fd_ctx * fdctx) +{ + struct mdso_driver_ctx_impl * ictx; + + ictx = mdso_get_driver_ictx(dctx); + + ictx->fdctx.fdin = fdctx->fdin; + ictx->fdctx.fdout = fdctx->fdout; + ictx->fdctx.fderr = fdctx->fderr; + ictx->fdctx.fdlog = fdctx->fdlog; + ictx->fdctx.fdcwd = fdctx->fdcwd; + ictx->fdctx.fddst = fdctx->fddst; + + return 0; +} -- cgit v1.2.3