summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormidipix <writeonce@midipix.org>2018-08-04 12:37:44 -0400
committermidipix <writeonce@midipix.org>2018-08-05 03:22:20 -0400
commitabf92311681c10ed589ac05f0f2b451e01fbc63e (patch)
tree12be9fc344e500330f4b2876406b91d858a94433 /src
parent0dfdcbb2bd0295ccfc2da2ce2e8efe6997b45a2c (diff)
downloadapimagic-abf92311681c10ed589ac05f0f2b451e01fbc63e.tar.bz2
apimagic-abf92311681c10ed589ac05f0f2b451e01fbc63e.tar.xz
driver, library interfaces: support alternate fd's for input/output/error/log.
Diffstat (limited to 'src')
-rw-r--r--src/apimagic.c2
-rw-r--r--src/driver/amgc_amain.c19
-rw-r--r--src/driver/amgc_driver_ctx.c70
-rw-r--r--src/internal/apimagic_driver_impl.h43
4 files changed, 120 insertions, 14 deletions
diff --git a/src/apimagic.c b/src/apimagic.c
index 16b5a1e..32e8e96 100644
--- a/src/apimagic.c
+++ b/src/apimagic.c
@@ -8,5 +8,5 @@
int main(int argc, char ** argv, char ** envp)
{
- return amgc_main(argc,argv,envp);
+ return amgc_main(argc,argv,envp,0);
}
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;
+}
diff --git a/src/internal/apimagic_driver_impl.h b/src/internal/apimagic_driver_impl.h
index 44268e0..a50a2ec 100644
--- a/src/internal/apimagic_driver_impl.h
+++ b/src/internal/apimagic_driver_impl.h
@@ -31,6 +31,7 @@ enum app_tags {
struct amgc_driver_ctx_impl {
struct amgc_common_ctx cctx;
struct amgc_driver_ctx ctx;
+ struct amgc_fd_ctx fdctx;
struct amgc_action * actions;
struct compilation_env_t ccenv;
int fdtmpin;
@@ -80,4 +81,46 @@ static inline void amgc_driver_set_ectx(
ictx->eunit = unit;
}
+static inline int amgc_driver_fdin(const struct amgc_driver_ctx * dctx)
+{
+ struct amgc_fd_ctx fdctx;
+ amgc_get_driver_fdctx(dctx,&fdctx);
+ return fdctx.fdin;
+}
+
+static inline int amgc_driver_fdout(const struct amgc_driver_ctx * dctx)
+{
+ struct amgc_fd_ctx fdctx;
+ amgc_get_driver_fdctx(dctx,&fdctx);
+ return fdctx.fdout;
+}
+
+static inline int amgc_driver_fderr(const struct amgc_driver_ctx * dctx)
+{
+ struct amgc_fd_ctx fdctx;
+ amgc_get_driver_fdctx(dctx,&fdctx);
+ return fdctx.fderr;
+}
+
+static inline int amgc_driver_fdlog(const struct amgc_driver_ctx * dctx)
+{
+ struct amgc_fd_ctx fdctx;
+ amgc_get_driver_fdctx(dctx,&fdctx);
+ return fdctx.fdlog;
+}
+
+static inline int amgc_driver_fdcwd(const struct amgc_driver_ctx * dctx)
+{
+ struct amgc_fd_ctx fdctx;
+ amgc_get_driver_fdctx(dctx,&fdctx);
+ return fdctx.fdcwd;
+}
+
+static inline int amgc_driver_fddst(const struct amgc_driver_ctx * dctx)
+{
+ struct amgc_fd_ctx fdctx;
+ amgc_get_driver_fdctx(dctx,&fdctx);
+ return fdctx.fddst;
+}
+
#endif