diff options
author | midipix <writeonce@midipix.org> | 2018-08-05 04:29:18 -0400 |
---|---|---|
committer | midipix <writeonce@midipix.org> | 2018-08-05 04:52:35 -0400 |
commit | ec000532ee50c64e66a76114b745f6804901bab9 (patch) | |
tree | 4c70470fd701720ae84b0bbfe269b7699e28a729 /src/driver | |
parent | 30077e892fcb1a6b6ec01e74af060fc11e7a067f (diff) | |
download | ntux-ec000532ee50c64e66a76114b745f6804901bab9.tar.bz2 ntux-ec000532ee50c64e66a76114b745f6804901bab9.tar.xz |
driver, library interfaces: support alternate fd's for input/output/error/log.
Diffstat (limited to 'src/driver')
-rw-r--r-- | src/driver/ntux_amain.c | 18 | ||||
-rw-r--r-- | src/driver/ntux_driver_ctx.c | 94 |
2 files changed, 94 insertions, 18 deletions
diff --git a/src/driver/ntux_amain.c b/src/driver/ntux_amain.c index b6719ff..22a1403 100644 --- a/src/driver/ntux_amain.c +++ b/src/driver/ntux_amain.c @@ -8,6 +8,7 @@ #include <unistd.h> #include <ntux/ntux.h> +#include "ntux_dprintf_impl.h" #include "ntux_driver_impl.h" #include "ntux_nolibc_impl.h" @@ -32,16 +33,16 @@ static const char * const ntux_ver_plain[6] = { "","" }; -static ssize_t ntux_version(struct ntux_driver_ctx * dctx) +static ssize_t ntux_version(struct ntux_driver_ctx * dctx, int fdout) { const struct ntux_source_version * verinfo; const char * const * verclr; verinfo = ntux_source_version(); - verclr = isatty(STDOUT_FILENO) ? ntux_ver_color : ntux_ver_plain; + verclr = isatty(fdout) ? ntux_ver_color : ntux_ver_plain; return ntux_dprintf( - STDOUT_FILENO,vermsg, + fdout,vermsg, verclr[0],dctx->program,verclr[1], verclr[2],verinfo->major,verinfo->minor, verinfo->revision,verclr[3], @@ -63,19 +64,24 @@ static int ntux_exit(struct ntux_driver_ctx * dctx, int ret) return ret; } -int ntux_main(int argc, char ** argv, char ** envp) +int ntux_main(int argc, char ** argv, char ** envp, const struct ntux_fd_ctx * fdctx) { int ret; + int fdout; + uint64_t flags; struct ntux_driver_ctx * dctx; const char ** unit; - if ((ret = ntux_get_driver_ctx(argv,envp,NTUX_DRIVER_FLAGS,&dctx))) + flags = NTUX_DRIVER_FLAGS; + fdout = fdctx ? fdctx->fdout : STDOUT_FILENO; + + if ((ret = ntux_get_driver_ctx(argv,envp,flags,fdctx,&dctx))) return (ret == NTUX_USAGE) ? !--argc : NTUX_ERROR; if (dctx->cctx->drvflags & NTUX_DRIVER_VERSION) - if ((ntux_version(dctx)) < 0) + if ((ntux_version(dctx,fdout)) < 0) return ntux_exit(dctx,NTUX_ERROR); if (dctx->cctx->cmd == NTUX_CMD_SPAWN) diff --git a/src/driver/ntux_driver_ctx.c b/src/driver/ntux_driver_ctx.c index 954b2db..243a729 100644 --- a/src/driver/ntux_driver_ctx.c +++ b/src/driver/ntux_driver_ctx.c @@ -6,6 +6,7 @@ #include <ntapi/ntapi.h> #include <psxscl/psxscl.h> +#include <psxabi/sys_fcntl.h> #include <stdint.h> #include <ntux/ntux.h> @@ -76,6 +77,7 @@ static uint32_t ntux_argv_flags(uint32_t flags) } static int ntux_driver_usage( + int fdout, const char * program, const char * arg, const struct argv_option ** optv, @@ -107,7 +109,7 @@ static int ntux_driver_usage( "Options%s%s%s:\n", program,program,cmdarg[0],cmdname,cmdarg[1]); - argv_usage(STDOUT_FILENO,header,optv,arg); + argv_usage(fdout,header,optv,arg); argv_free(meta); return NTUX_USAGE; @@ -115,6 +117,7 @@ static int ntux_driver_usage( static struct ntux_driver_ctx_impl * ntux_driver_ctx_alloc( struct argv_meta * meta, + const struct ntux_fd_ctx * fdctx, const struct ntux_common_ctx * cctx, size_t nunits) { @@ -137,8 +140,8 @@ static struct ntux_driver_ctx_impl * ntux_driver_ctx_alloc( ictx->ctx.errinfp = &ictx->ctx.erriptr[0]; ictx->ctx.erricap = &ictx->ctx.erriptr[--elements]; - 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) @@ -155,6 +158,7 @@ static int ntux_cctx_update( const struct argv_option ** optv, struct argv_meta * meta, uint32_t flags, + const struct ntux_fd_ctx * fdctx, struct ntux_common_ctx * cctx, size_t * nunits) { @@ -167,6 +171,7 @@ static int ntux_cctx_update( case TAG_HELP: if (flags & NTUX_DRIVER_VERBOSITY_USAGE) return ntux_driver_usage( + fdctx->fdout, program,entry->arg, optv,0,cctx->cmd); @@ -177,6 +182,7 @@ static int ntux_cctx_update( case TAG_CMD: if (*nunits) return ntux_driver_usage( + fdctx->fderr, program,entry->arg, optv,0,cctx->cmd); @@ -243,6 +249,7 @@ int ntux_get_driver_ctx( char ** argv, char ** envp, uint32_t flags, + const struct ntux_fd_ctx * fdctx, struct ntux_driver_ctx ** pctx) { struct ntux_driver_ctx_impl * ictx; @@ -264,6 +271,19 @@ int ntux_get_driver_ctx( if (ntux_init()) return -1; + /* fdctx */ + if (!fdctx) { + fdctx = &(const struct ntux_fd_ctx) { + .fdin = STDIN_FILENO, + .fdout = STDOUT_FILENO, + .fderr = STDERR_FILENO, + .fdlog = (-1), + .fdcwd = AT_FDCWD, + .fddst = AT_FDCWD, + }; + } + + /* cctx */ memset(&cctx,0,sizeof(cctx)); nunits = 0; @@ -276,7 +296,10 @@ int ntux_get_driver_ctx( argv_optv_init(ntux_cmd_options[cctx.cmd],optv); if (!argv[1] && (flags & NTUX_DRIVER_VERBOSITY_USAGE)) - return ntux_driver_usage(program,0,optv,0,cctx.cmd); + return ntux_driver_usage( + fdctx->fderr, + program,0, + optv,0,cctx.cmd); /* initial argv scan: ... --cmd=xxx ... */ for (parg=argv, cmdargv=0; *parg && !cmdargv; parg++) { @@ -296,10 +319,12 @@ int ntux_get_driver_ctx( if (!(meta = argv_get( argv,optv, ntux_argv_flags(flags), - STDERR_FILENO))) + fdctx->fderr))) return -1; - if (ntux_cctx_update(program,optv,meta,flags,&cctx,&nunits)) { + if (ntux_cctx_update( + program,optv,meta,flags, + fdctx,&cctx,&nunits)) { argv_free(meta); return -1; } @@ -325,12 +350,15 @@ int ntux_get_driver_ctx( argv_get( argv,optv, ntux_argv_flags(flags), - STDERR_FILENO); + fdctx->fderr); return -1; } if (!ctx.unitidx && (flags & NTUX_DRIVER_VERBOSITY_USAGE)) - ntux_driver_usage(program,0,optv,0,cctx.cmd); + ntux_driver_usage( + fdctx->fderr, + program,0, + optv,0,cctx.cmd); if (!ctx.unitidx) return NTUX_ERROR; @@ -344,10 +372,12 @@ int ntux_get_driver_ctx( if (!(meta = argv_get( argv,optv, ntux_argv_flags(flags), - STDERR_FILENO))) + fdctx->fderr))) return -1; - if (ntux_cctx_update(program,optv,meta,flags,&cctx,&nunits)) { + if (ntux_cctx_update( + program,optv,meta,flags, + fdctx,&cctx,&nunits)) { argv_free(meta); return -1; } @@ -362,9 +392,13 @@ int ntux_get_driver_ctx( /* finalize */ if (nunits && !cctx.cmd) - return ntux_driver_usage(program,0,optv,meta,NTUX_CMD_DEFAULT); + return ntux_driver_usage( + fdctx->fderr, + program,0, + optv,meta, + NTUX_CMD_DEFAULT); - if (!(ictx = ntux_driver_ctx_alloc(meta,&cctx,nunits))) + if (!(ictx = ntux_driver_ctx_alloc(meta,fdctx,&cctx,nunits))) return ntux_get_driver_ctx_fail(meta); ictx->ctx.program = program; @@ -397,3 +431,39 @@ const struct ntux_source_version * ntux_source_version(void) { return &ntux_src_version; } + +int ntux_get_driver_fdctx( + const struct ntux_driver_ctx * dctx, + struct ntux_fd_ctx * fdctx) +{ + struct ntux_driver_ctx_impl * ictx; + + ictx = ntux_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 ntux_set_driver_fdctx( + struct ntux_driver_ctx * dctx, + const struct ntux_fd_ctx * fdctx) +{ + struct ntux_driver_ctx_impl * ictx; + + ictx = ntux_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; +} |