summaryrefslogtreecommitdiffhomepage
path: root/src/driver
diff options
context:
space:
mode:
Diffstat (limited to 'src/driver')
-rw-r--r--src/driver/amgc_amain.c19
-rw-r--r--src/driver/amgc_driver_ctx.c70
2 files changed, 76 insertions, 13 deletions
diff --git a/src/driver/amgc_amain.c b/src/driver/amgc_amain.c
index d86cc96..096b960 100644
--- a/src/driver/amgc_amain.c
+++ b/src/driver/amgc_amain.c
@@ -8,6 +8,7 @@
#include <unistd.h>
#include <apimagic/apimagic.h>
#include "apimagic_driver_impl.h"
+#include "apimagic_dprintf_impl.h"
#ifndef AMGC_DRIVER_FLAGS
#define AMGC_DRIVER_FLAGS AMGC_DRIVER_VERBOSITY_ERRORS \
@@ -30,15 +31,16 @@ static const char * const amgc_ver_plain[6] = {
"",""
};
-static ssize_t amgc_version(struct amgc_driver_ctx * dctx)
+static ssize_t amgc_version(struct amgc_driver_ctx * dctx, int fdout)
{
const struct amgc_source_version * verinfo;
const char * const * verclr;
verinfo = amgc_source_version();
- verclr = isatty(STDOUT_FILENO) ? amgc_ver_color : amgc_ver_plain;
+ verclr = isatty(fdout) ? amgc_ver_color : amgc_ver_plain;
- return fprintf(stdout,vermsg,
+ return amgc_dprintf(
+ fdout,vermsg,
verclr[0],dctx->program,verclr[1],
verclr[2],verinfo->major,verinfo->minor,
verinfo->revision,verclr[3],
@@ -60,20 +62,25 @@ static int amgc_exit(struct amgc_driver_ctx * dctx, int ret)
return ret;
}
-int amgc_main(int argc, char ** argv, char ** envp)
+int amgc_main(int argc, char ** argv, char ** envp, const struct amgc_fd_ctx * fdctx)
{
int ret;
+ int fdout;
+ uint64_t flags;
struct amgc_driver_ctx * dctx;
struct amgc_unit_ctx * uctx;
const char ** unit;
- if ((ret = amgc_get_driver_ctx(argv,envp,AMGC_DRIVER_FLAGS,&dctx)))
+ flags = AMGC_DRIVER_FLAGS;
+ fdout = fdctx ? fdctx->fdout : STDOUT_FILENO;
+
+ if ((ret = amgc_get_driver_ctx(argv,envp,flags,fdctx,&dctx)))
return (ret == AMGC_USAGE)
? !--argc
: AMGC_ERROR;
if (dctx->cctx->drvflags & AMGC_DRIVER_VERSION)
- if ((amgc_version(dctx)) < 0)
+ if ((amgc_version(dctx,fdout)) < 0)
return amgc_exit(dctx,AMGC_ERROR);
for (unit=dctx->units; *unit && !dctx->errv[0]; unit++) {
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;
+}