diff options
Diffstat (limited to 'src/driver/amgc_driver_ctx.c')
-rw-r--r-- | src/driver/amgc_driver_ctx.c | 70 |
1 files changed, 63 insertions, 7 deletions
diff --git a/src/driver/amgc_driver_ctx.c b/src/driver/amgc_driver_ctx.c index e22d566..7546168 100644 --- a/src/driver/amgc_driver_ctx.c +++ b/src/driver/amgc_driver_ctx.c @@ -59,6 +59,7 @@ static uint32_t amgc_argv_flags(uint32_t flags) } static int amgc_driver_usage( + int fdout, const char * program, const char * arg, const struct argv_option ** optv, @@ -70,7 +71,7 @@ static int amgc_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 AMGC_USAGE; @@ -78,6 +79,7 @@ static int amgc_driver_usage( static struct amgc_driver_ctx_impl * amgc_driver_ctx_alloc( struct argv_meta * meta, + const struct amgc_fd_ctx * fdctx, const struct amgc_common_ctx * cctx, size_t nunits, size_t nactions) @@ -100,8 +102,8 @@ static struct amgc_driver_ctx_impl * amgc_driver_ctx_alloc( 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,aunits=ictx->units; entry->fopt || entry->arg; entry++) if (!entry->fopt) @@ -147,6 +149,7 @@ int amgc_get_driver_ctx( char ** argv, char ** envp, uint32_t flags, + const struct amgc_fd_ctx * fdctx, struct amgc_driver_ctx ** pctx) { struct amgc_driver_ctx_impl * ctx; @@ -160,12 +163,23 @@ int amgc_get_driver_ctx( (void)envp; + if (!fdctx) { + fdctx = &(const struct amgc_fd_ctx) { + .fdin = STDIN_FILENO, + .fdout = STDOUT_FILENO, + .fderr = STDERR_FILENO, + .fdlog = (-1), + .fdcwd = AT_FDCWD, + .fddst = AT_FDCWD, + }; + } + argv_optv_init(amgc_default_options,optv); if (!(meta = argv_get( argv,optv, amgc_argv_flags(flags), - STDERR_FILENO))) + fdctx->fderr))) return -1; nunits = 0; @@ -175,7 +189,10 @@ int amgc_get_driver_ctx( cctx.drvflags = flags; if (!argv[1] && (flags & AMGC_DRIVER_VERBOSITY_USAGE)) - return amgc_driver_usage(program,0,optv,meta); + return amgc_driver_usage( + fdctx->fderr, + program,0, + optv,meta); /* compiler defaults */ cctx.std = STANDARD_C99; @@ -186,7 +203,10 @@ int amgc_get_driver_ctx( switch (entry->tag) { case TAG_HELP: if (flags & AMGC_DRIVER_VERBOSITY_USAGE) - return amgc_driver_usage(program,entry->arg,optv,meta); + return amgc_driver_usage( + fdctx->fderr, + program,entry->arg, + optv,meta); case TAG_VERSION: cctx.drvflags |= AMGC_DRIVER_VERSION; @@ -207,7 +227,7 @@ int amgc_get_driver_ctx( if (amgc_init_cparser()) return amgc_get_driver_ctx_fail(meta); - if (!(ctx = amgc_driver_ctx_alloc(meta,&cctx,nunits,nactions))) + if (!(ctx = amgc_driver_ctx_alloc(meta,fdctx,&cctx,nunits,nactions))) return amgc_get_driver_ctx_fail(meta); /* create action vector */ @@ -289,3 +309,39 @@ const struct amgc_source_version * amgc_source_version(void) { return &amgc_src_version; } + +int amgc_get_driver_fdctx( + const struct amgc_driver_ctx * dctx, + struct amgc_fd_ctx * fdctx) +{ + struct amgc_driver_ctx_impl * ictx; + + ictx = amgc_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 amgc_set_driver_fdctx( + struct amgc_driver_ctx * dctx, + const struct amgc_fd_ctx * fdctx) +{ + struct amgc_driver_ctx_impl * ictx; + + ictx = amgc_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; +} |