summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authormidipix <writeonce@midipix.org>2018-08-04 03:56:54 -0400
committermidipix <writeonce@midipix.org>2018-08-04 05:21:02 -0400
commitd4f88d42725956c3aa6d9c6e83e70729c1a938e8 (patch)
treec5b3569b5e4f4872376e924e19e9027e7b4d01a7 /src
parente203594f6b01576630ef72a6ac6872e52f97c4c2 (diff)
downloadsofort-d4f88d42725956c3aa6d9c6e83e70729c1a938e8.tar.bz2
sofort-d4f88d42725956c3aa6d9c6e83e70729c1a938e8.tar.xz
driver, library interfaces: support alternate fd's for input/output/error/log.
Diffstat (limited to 'src')
-rw-r--r--src/driver/sfrt_amain.c19
-rw-r--r--src/driver/sfrt_driver_ctx.c70
-rw-r--r--src/internal/sofort_driver_impl.h43
-rw-r--r--src/sofort.c2
4 files changed, 120 insertions, 14 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;
+}
diff --git a/src/internal/sofort_driver_impl.h b/src/internal/sofort_driver_impl.h
index 640aa2d..a7615ad 100644
--- a/src/internal/sofort_driver_impl.h
+++ b/src/internal/sofort_driver_impl.h
@@ -23,6 +23,7 @@ enum app_tags {
struct sfrt_driver_ctx_impl {
struct sfrt_common_ctx cctx;
struct sfrt_driver_ctx ctx;
+ struct sfrt_fd_ctx fdctx;
const struct sfrt_unit_ctx * euctx;
const char * eunit;
struct sfrt_error_info ** errinfp;
@@ -62,4 +63,46 @@ static inline void sfrt_driver_set_ectx(
ictx->eunit = unit;
}
+static inline int sfrt_driver_fdin(const struct sfrt_driver_ctx * dctx)
+{
+ struct sfrt_fd_ctx fdctx;
+ sfrt_get_driver_fdctx(dctx,&fdctx);
+ return fdctx.fdin;
+}
+
+static inline int sfrt_driver_fdout(const struct sfrt_driver_ctx * dctx)
+{
+ struct sfrt_fd_ctx fdctx;
+ sfrt_get_driver_fdctx(dctx,&fdctx);
+ return fdctx.fdout;
+}
+
+static inline int sfrt_driver_fderr(const struct sfrt_driver_ctx * dctx)
+{
+ struct sfrt_fd_ctx fdctx;
+ sfrt_get_driver_fdctx(dctx,&fdctx);
+ return fdctx.fderr;
+}
+
+static inline int sfrt_driver_fdlog(const struct sfrt_driver_ctx * dctx)
+{
+ struct sfrt_fd_ctx fdctx;
+ sfrt_get_driver_fdctx(dctx,&fdctx);
+ return fdctx.fdlog;
+}
+
+static inline int sfrt_driver_fdcwd(const struct sfrt_driver_ctx * dctx)
+{
+ struct sfrt_fd_ctx fdctx;
+ sfrt_get_driver_fdctx(dctx,&fdctx);
+ return fdctx.fdcwd;
+}
+
+static inline int sfrt_driver_fddst(const struct sfrt_driver_ctx * dctx)
+{
+ struct sfrt_fd_ctx fdctx;
+ sfrt_get_driver_fdctx(dctx,&fdctx);
+ return fdctx.fddst;
+}
+
#endif
diff --git a/src/sofort.c b/src/sofort.c
index 112823d..d52059e 100644
--- a/src/sofort.c
+++ b/src/sofort.c
@@ -2,5 +2,5 @@
int main(int argc, char ** argv, char ** envp)
{
- return sfrt_main(argc,argv,envp);
+ return sfrt_main(argc,argv,envp,0);
}