summaryrefslogtreecommitdiffhomepage
path: root/src/driver
diff options
context:
space:
mode:
authormidipix <writeonce@midipix.org>2019-01-01 05:30:34 -0500
committermidipix <writeonce@midipix.org>2019-01-01 05:37:59 -0500
commitb6f827d6939397e99d72b1dc8480c72364d8905c (patch)
tree74c198de06f27fa0e7fd8649838924ead5a879a6 /src/driver
parentb95a5b04a4a3b6bf0b86080abce1436f0b7aee1c (diff)
downloadsofort-b6f827d6939397e99d72b1dc8480c72364d8905c.tar.bz2
sofort-b6f827d6939397e99d72b1dc8480c72364d8905c.tar.xz
sofort: split-up: keep build-system elements, move extras to sofortex.
Diffstat (limited to 'src/driver')
-rw-r--r--src/driver/sfrt_amain.c97
-rw-r--r--src/driver/sfrt_driver_ctx.c249
-rw-r--r--src/driver/sfrt_unit_ctx.c61
3 files changed, 0 insertions, 407 deletions
diff --git a/src/driver/sfrt_amain.c b/src/driver/sfrt_amain.c
deleted file mode 100644
index 075d9f8..0000000
--- a/src/driver/sfrt_amain.c
+++ /dev/null
@@ -1,97 +0,0 @@
-#include <stdio.h>
-#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 \
- | SFRT_DRIVER_VERBOSITY_USAGE
-#endif
-
-static const char vermsg[] = "%s%s%s (git://midipix.org/sofort): "
- "version %s%d.%d.%d%s.\n"
- "[commit reference: %s%s%s]\n";
-
-static const char * const sfrt_ver_color[6] = {
- "\x1b[1m\x1b[35m","\x1b[0m",
- "\x1b[1m\x1b[32m","\x1b[0m",
- "\x1b[1m\x1b[34m","\x1b[0m"
-};
-
-static const char * const sfrt_ver_plain[6] = {
- "","",
- "","",
- "",""
-};
-
-static ssize_t sfrt_version(struct sfrt_driver_ctx * dctx, int fdout)
-{
- const struct sfrt_source_version * verinfo;
- const char * const * verclr;
-
- verinfo = sfrt_source_version();
- verclr = isatty(fdout) ? sfrt_ver_color : sfrt_ver_plain;
-
- return sfrt_dprintf(
- fdout,vermsg,
- verclr[0],dctx->program,verclr[1],
- verclr[2],verinfo->major,verinfo->minor,
- verinfo->revision,verclr[3],
- verclr[4],verinfo->commit,verclr[5]);
-}
-
-static void sfrt_perform_unit_actions(
- const struct sfrt_driver_ctx * dctx,
- struct sfrt_unit_ctx * uctx)
-{
- uint64_t flags = dctx->cctx->actflags; /* dummy */
- /* dummy */
- if (flags & SFRT_OUTPUT_NAME) /* dummy */
- sfrt_output_name(dctx,uctx); /* dummy */
- /* dummy */
- if (flags & SFRT_OUTPUT_ADDRESS) /* dummy */
- sfrt_output_address(dctx,uctx); /* dummy */
-}
-
-static int sfrt_exit(struct sfrt_driver_ctx * dctx, int ret)
-{
- sfrt_output_error_vector(dctx);
- sfrt_free_driver_ctx(dctx);
- return ret;
-}
-
-int sfrt_main(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;
-
- 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)
- ? !argv || !argv[0] || !argv[1]
- : SFRT_ERROR;
-
- if (dctx->cctx->drvflags & SFRT_DRIVER_VERSION)
- if ((sfrt_version(dctx,fdout)) < 0)
- return sfrt_exit(dctx,SFRT_ERROR);
-
- if (dctx->cctx->anystring) /* dummy */
- if ((sfrt_output_dummy(dctx)) < 0) /* dummy */
- return sfrt_exit(dctx,2); /* dummy */
- /* dummy */
- for (unit=dctx->units; *unit && !dctx->errv[0]; unit++) {
- if (!(sfrt_get_unit_ctx(dctx,*unit,&uctx))) {
- sfrt_perform_unit_actions(dctx,uctx);
- sfrt_free_unit_ctx(uctx);
- }
- }
-
- return sfrt_exit(dctx,dctx->errv[0] ? SFRT_ERROR : SFRT_OK);
-}
diff --git a/src/driver/sfrt_driver_ctx.c b/src/driver/sfrt_driver_ctx.c
deleted file mode 100644
index 643de8d..0000000
--- a/src/driver/sfrt_driver_ctx.c
+++ /dev/null
@@ -1,249 +0,0 @@
-#include <stdint.h>
-#include <unistd.h>
-#include <fcntl.h>
-
-#define ARGV_DRIVER
-
-#include <sofort/sofort.h>
-#include "sofort_version.h"
-#include "sofort_driver_impl.h"
-#include "argv/argv.h"
-
-/* package info */
-static const struct sfrt_source_version sfrt_src_version = {
- SFRT_TAG_VER_MAJOR,
- SFRT_TAG_VER_MINOR,
- SFRT_TAG_VER_PATCH,
- SOFORT_GIT_VERSION
-};
-
-struct sfrt_driver_ctx_alloc {
- struct argv_meta * meta;
- struct sfrt_driver_ctx_impl ctx;
- uint64_t guard;
- const char * units[];
-};
-
-static uint32_t sfrt_argv_flags(uint32_t flags)
-{
- uint32_t ret = ARGV_CLONE_VECTOR;
-
- if (flags & SFRT_DRIVER_VERBOSITY_NONE)
- ret |= ARGV_VERBOSITY_NONE;
-
- if (flags & SFRT_DRIVER_VERBOSITY_ERRORS)
- ret |= ARGV_VERBOSITY_ERRORS;
-
- if (flags & SFRT_DRIVER_VERBOSITY_STATUS)
- ret |= ARGV_VERBOSITY_STATUS;
-
- return ret;
-}
-
-static int sfrt_driver_usage(
- int fdout,
- const char * program,
- const char * arg,
- const struct argv_option ** optv,
- struct argv_meta * meta)
-{
- char header[512];
-
- snprintf(header,sizeof(header),
- "Usage: %s [options] <file>...\n" "Options:\n",
- program);
-
- argv_usage(fdout,header,optv,arg);
- argv_free(meta);
-
- return SFRT_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)
-{
- struct sfrt_driver_ctx_alloc * ictx;
- size_t size;
- struct argv_entry * entry;
- const char ** units;
- int elements;
-
- size = sizeof(struct sfrt_driver_ctx_alloc);
- size += (nunits+1)*sizeof(const char *);
-
- if (!(ictx = calloc(1,size)))
- return 0;
-
- 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)
- *units++ = entry->arg;
-
- elements = sizeof(ictx->ctx.erribuf) / sizeof(*ictx->ctx.erribuf);
-
- ictx->ctx.errinfp = &ictx->ctx.erriptr[0];
- ictx->ctx.erricap = &ictx->ctx.erriptr[--elements];
-
- ictx->meta = meta;
- ictx->ctx.ctx.units = ictx->units;
- ictx->ctx.ctx.errv = ictx->ctx.errinfp;
- return &ictx->ctx;
-}
-
-static int sfrt_get_driver_ctx_fail(struct argv_meta * meta)
-{
- argv_free(meta);
- return -1;
-}
-
-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;
- struct sfrt_common_ctx cctx;
- const struct argv_option * optv[SFRT_OPTV_ELEMENTS];
- struct argv_meta * meta;
- struct argv_entry * entry;
- size_t nunits;
- const char * program;
-
- (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),
- fdctx->fderr)))
- return -1;
-
- nunits = 0;
- program = argv_program_name(argv[0]);
- memset(&cctx,0,sizeof(cctx));
- cctx.drvflags = flags;
-
- if (!argv[1] && (flags & SFRT_DRIVER_VERBOSITY_USAGE))
- return sfrt_driver_usage(
- fdctx->fderr,
- program,0,
- optv,meta);
-
- /* get options, count units */
- for (entry=meta->entries; entry->fopt || entry->arg; entry++) {
- if (entry->fopt) {
- switch (entry->tag) {
- case TAG_HELP:
- if (flags & SFRT_DRIVER_VERBOSITY_USAGE)
- return sfrt_driver_usage(
- fdctx->fderr,
- program,entry->arg,
- optv,meta);
-
- case TAG_VERSION:
- cctx.drvflags |= SFRT_DRIVER_VERSION;
- break;
-/* dummy */
-/* dummy */ case TAG_OUTPUT_DUMMY:
-/* dummy */ cctx.anystring = entry->arg;
-/* dummy */ break;
-/* dummy */
-/* dummy */ case TAG_OUTPUT_PROPERTY:
-/* dummy */ if (!(strcmp(entry->arg,"name")))
-/* dummy */ cctx.actflags |= SFRT_OUTPUT_NAME;
-/* dummy */ else if (!(strcmp(entry->arg,"address")))
-/* dummy */ cctx.actflags |= SFRT_OUTPUT_ADDRESS;
-/* dummy */ break;
- }
- } else
- nunits++;
- }
-
- if (!(ctx = sfrt_driver_ctx_alloc(meta,fdctx,&cctx,nunits)))
- return sfrt_get_driver_ctx_fail(meta);
-
- ctx->ctx.program = program;
- ctx->ctx.cctx = &ctx->cctx;
-
- *pctx = &ctx->ctx;
- return SFRT_OK;
-}
-
-static void sfrt_free_driver_ctx_impl(struct sfrt_driver_ctx_alloc * ictx)
-{
- argv_free(ictx->meta);
- free(ictx);
-}
-
-void sfrt_free_driver_ctx(struct sfrt_driver_ctx * ctx)
-{
- struct sfrt_driver_ctx_alloc * ictx;
- uintptr_t addr;
-
- if (ctx) {
- addr = (uintptr_t)ctx - offsetof(struct sfrt_driver_ctx_impl,ctx);
- addr = addr - offsetof(struct sfrt_driver_ctx_alloc,ctx);
- ictx = (struct sfrt_driver_ctx_alloc *)addr;
- sfrt_free_driver_ctx_impl(ictx);
- }
-}
-
-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/driver/sfrt_unit_ctx.c b/src/driver/sfrt_unit_ctx.c
deleted file mode 100644
index 630fdba..0000000
--- a/src/driver/sfrt_unit_ctx.c
+++ /dev/null
@@ -1,61 +0,0 @@
-#include <stdint.h>
-#include <stddef.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/mman.h>
-
-#include <sofort/sofort.h>
-#include "sofort_driver_impl.h"
-#include "sofort_errinfo_impl.h"
-
-static int sfrt_free_unit_ctx_impl(struct sfrt_unit_ctx_impl * ctx, int ret)
-{
- if (ctx) {
- sfrt_unmap_input(&ctx->map);
- free(ctx);
- }
-
- return ret;
-}
-
-int sfrt_get_unit_ctx(
- const struct sfrt_driver_ctx * dctx,
- const char * path,
- struct sfrt_unit_ctx ** pctx)
-{
- struct sfrt_unit_ctx_impl * ctx;
-
- if (!dctx)
- return SFRT_CUSTOM_ERROR(
- dctx,SFRT_ERR_NULL_CONTEXT);
-
- else if (!(ctx = calloc(1,sizeof(*ctx))))
- return SFRT_BUFFER_ERROR(dctx);
-
- sfrt_driver_set_ectx(
- dctx,0,path);
-
- if (sfrt_map_input(dctx,-1,path,PROT_READ,&ctx->map))
- return sfrt_free_unit_ctx_impl(ctx,
- SFRT_NESTED_ERROR(dctx));
-
- ctx->path = path;
-
- ctx->uctx.path = &ctx->path;
- ctx->uctx.map = &ctx->map;
-
- *pctx = &ctx->uctx;
- return 0;
-}
-
-void sfrt_free_unit_ctx(struct sfrt_unit_ctx * ctx)
-{
- struct sfrt_unit_ctx_impl * ictx;
- uintptr_t addr;
-
- if (ctx) {
- addr = (uintptr_t)ctx - offsetof(struct sfrt_unit_ctx_impl,uctx);
- ictx = (struct sfrt_unit_ctx_impl *)addr;
- sfrt_free_unit_ctx_impl(ictx,0);
- }
-}