diff options
Diffstat (limited to 'src/driver')
-rw-r--r-- | src/driver/sfrt_amain.c | 19 | ||||
-rw-r--r-- | src/driver/sfrt_driver_ctx.c | 70 |
2 files changed, 76 insertions, 13 deletions
diff --git a/src/driver/sfrt_amain.c b/src/driver/sfrt_amain.c index 0b18010..c37f77d 100644 --- a/src/driver/sfrt_amain.c +++ b/src/driver/sfrt_amain.c @@ -2,6 +2,7 @@ #include <unistd.h> #include <sofort/sofort.h> #include "sofort_driver_impl.h" +#include "sofort_dprintf_impl.h" #ifndef SFRT_DRIVER_FLAGS #define SFRT_DRIVER_FLAGS SFRT_DRIVER_VERBOSITY_ERRORS \ @@ -24,15 +25,16 @@ static const char * const sfrt_ver_plain[6] = { "","" }; -static ssize_t sfrt_version(struct sfrt_driver_ctx * dctx) +static ssize_t sfrt_version(int fdout, struct sfrt_driver_ctx * dctx) { const struct sfrt_source_version * verinfo; const char * const * verclr; verinfo = sfrt_source_version(); - verclr = isatty(STDOUT_FILENO) ? sfrt_ver_color : sfrt_ver_plain; + verclr = isatty(fdout) ? sfrt_ver_color : sfrt_ver_plain; - return fprintf(stdout,vermsg, + return sfrt_dprintf( + fdout,vermsg, verclr[0],dctx->program,verclr[1], verclr[2],verinfo->major,verinfo->minor, verinfo->revision,verclr[3], @@ -59,20 +61,25 @@ static int sfrt_exit(struct sfrt_driver_ctx * dctx, int ret) return ret; } -int sfrt_main(int argc, char ** argv, char ** envp) +int sfrt_main(int argc, char ** argv, char ** envp, const struct sfrt_fd_ctx * fdctx) { int ret; + int fdout; + uint64_t flags; struct sfrt_driver_ctx * dctx; struct sfrt_unit_ctx * uctx; const char ** unit; - if ((ret = sfrt_get_driver_ctx(argv,envp,SFRT_DRIVER_FLAGS,&dctx))) + flags = SFRT_DRIVER_FLAGS; + fdout = fdctx ? fdctx->fdout : STDOUT_FILENO; + + if ((ret = sfrt_get_driver_ctx(argv,envp,flags,fdctx,&dctx))) return (ret == SFRT_USAGE) ? !--argc : SFRT_ERROR; if (dctx->cctx->drvflags & SFRT_DRIVER_VERSION) - if ((sfrt_version(dctx)) < 0) + if ((sfrt_version(fdout,dctx)) < 0) return sfrt_exit(dctx,SFRT_ERROR); if (dctx->cctx->anystring) /* dummy */ 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; +} |