diff options
Diffstat (limited to 'src/driver/sfrt_driver_ctx.c')
-rw-r--r-- | src/driver/sfrt_driver_ctx.c | 70 |
1 files changed, 63 insertions, 7 deletions
diff --git a/src/driver/sfrt_driver_ctx.c b/src/driver/sfrt_driver_ctx.c index fc3ea3a..44cfe82 100644 --- a/src/driver/sfrt_driver_ctx.c +++ b/src/driver/sfrt_driver_ctx.c @@ -41,6 +41,7 @@ static uint32_t sfrt_argv_flags(uint32_t flags) } static int sfrt_driver_usage( + int fdout, const char * program, const char * arg, const struct argv_option ** optv, @@ -52,7 +53,7 @@ static int sfrt_driver_usage( "Usage: %s [options] <file>...\n" "Options:\n", program); - argv_usage(STDOUT_FILENO,header,optv,arg); + argv_usage(fdout,header,optv,arg); argv_free(meta); return SFRT_USAGE; @@ -60,6 +61,7 @@ static int sfrt_driver_usage( static struct sfrt_driver_ctx_impl * sfrt_driver_ctx_alloc( struct argv_meta * meta, + const struct sfrt_fd_ctx * fdctx, const struct sfrt_common_ctx * cctx, size_t nunits) { @@ -75,8 +77,8 @@ static struct sfrt_driver_ctx_impl * sfrt_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) @@ -103,6 +105,7 @@ int sfrt_get_driver_ctx( char ** argv, char ** envp, uint32_t flags, + const struct sfrt_fd_ctx * fdctx, struct sfrt_driver_ctx ** pctx) { struct sfrt_driver_ctx_impl * ctx; @@ -115,12 +118,23 @@ int sfrt_get_driver_ctx( (void)envp; + if (!fdctx) { + fdctx = &(const struct sfrt_fd_ctx) { + .fdin = STDIN_FILENO, + .fdout = STDOUT_FILENO, + .fderr = STDERR_FILENO, + .fdlog = (-1), + .fdcwd = AT_FDCWD, + .fddst = AT_FDCWD, + }; + } + argv_optv_init(sfrt_default_options,optv); if (!(meta = argv_get( argv,optv, sfrt_argv_flags(flags), - STDERR_FILENO))) + fdctx->fderr))) return -1; nunits = 0; @@ -129,7 +143,10 @@ int sfrt_get_driver_ctx( cctx.drvflags = flags; if (!argv[1] && (flags & SFRT_DRIVER_VERBOSITY_USAGE)) - return sfrt_driver_usage(program,0,optv,meta); + return sfrt_driver_usage( + fdctx->fderr, + program,0, + optv,meta); /* get options, count units */ for (entry=meta->entries; entry->fopt || entry->arg; entry++) { @@ -137,7 +154,10 @@ int sfrt_get_driver_ctx( switch (entry->tag) { case TAG_HELP: if (flags & SFRT_DRIVER_VERBOSITY_USAGE) - return sfrt_driver_usage(program,entry->arg,optv,meta); + return sfrt_driver_usage( + fdctx->fderr, + program,entry->arg, + optv,meta); case TAG_VERSION: cctx.drvflags |= SFRT_DRIVER_VERSION; @@ -158,7 +178,7 @@ int sfrt_get_driver_ctx( nunits++; } - if (!(ctx = sfrt_driver_ctx_alloc(meta,&cctx,nunits))) + if (!(ctx = sfrt_driver_ctx_alloc(meta,fdctx,&cctx,nunits))) return sfrt_get_driver_ctx_fail(meta); ctx->ctx.program = program; @@ -191,3 +211,39 @@ const struct sfrt_source_version * sfrt_source_version(void) { return &sfrt_src_version; } + +int sfrt_get_driver_fdctx( + const struct sfrt_driver_ctx * dctx, + struct sfrt_fd_ctx * fdctx) +{ + struct sfrt_driver_ctx_impl * ictx; + + ictx = sfrt_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 sfrt_set_driver_fdctx( + struct sfrt_driver_ctx * dctx, + const struct sfrt_fd_ctx * fdctx) +{ + struct sfrt_driver_ctx_impl * ictx; + + ictx = sfrt_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; +} |