From b6f827d6939397e99d72b1dc8480c72364d8905c Mon Sep 17 00:00:00 2001 From: midipix Date: Jan 01 2019 10:37:59 +0000 Subject: sofort: split-up: keep build-system elements, move extras to sofortex. --- diff --git a/HOWTO b/HOWTO deleted file mode 100644 index 4e06731..0000000 --- a/HOWTO +++ /dev/null @@ -1,113 +0,0 @@ -The purpose of this how-to is to help you transform the sofort -project template into your own work-in-progress repository. - -So that all steps are concrete and fully defined, let us -pretend that you would like to create a library and front-end -utility named 'wonderer', and have accordingly chosen 'wndr' -as your namespace prefix. - - -OUTLINE -------- -step 1: clone. -step 2: make this your own. -step 3: create your first interface. - - -step 1: clone -------------- -# ./sofort.sh -d $HOME/wonderer -p wonderer -n wndr -# cd $HOME/wonderer -# git init - - -step 2: make this your own --------------------------- -* update the README file with a preliminary description - of your project. - -* update COPYING.WONDERER with your license of choice. - -* include/wonderer/wonderer.h: fix whitespace as needed. - -* src/wonderer.c: vermsg[]: replace git://localhost - with your own git host. - -* verify that all went well: - -# mkdir ../build && cd ../build -# ../wonderer/configure && make - -* enjoy your skeleton by running bin/wonderer. - -* commit your changes (i.e. 'created skeleton'). - - -step 3: create your first interface ------------------------------------ -At the outset, adding a command-line interface involves -the addition of an argv option record in conjunction -with an option tag and a driver switch case. - -Unless a no-op, the above interface will correspond to -either a flag in your common context structure (i.e. -struct wndr_common_ctx), or an attribute in that structure, -or both. - -Now let us practice all that while also looking at the two -dummy interfaces which are part of sofort (note that these -interfaces are *not* present in the generated project): - -* src/internal/wonderer_driver_impl.h: add a tag (see the - DUMMY and PROPERTY tags in the corresponding sofort file). - -* src/skin/wonderer_skin_default.c: add an argv option record; - if your option is associated with a finite set of values - (i.e. --fruit=apple|grape|orange) then you should take - advantage of the 'paradigm' member in struct argv_option - (see src/internal/argv/argv.h for the complete struct - definition). - -* src/driver/wndr_driver_ctx.c: add a switch case; depending - on the interface that you are adding, you will be either - setting a bit in one of the flag members of wndr_common_ctx - (in which case you should define a macro for that flag), - or assign the option value to an attribute in the above - context (in which case you should add an appropriate member - to the context structure). For the latter case, see the - handling of TAG_OUTPUT_DUMMY in the respective sofort file; - for the former, see the handling of TAG_OUTPUT_PROPERTY. - -* make use of the newly added flag/attribute in one or more - of your functions; remember to refer to your new source file - in project/common.src, and, assuming you would like to expose - it, add the function declaration to wonderer.h. - -* front-end utility (src/wonderer.c): if applicable, extend - wonderer_perform_unit_actions() so that it calls the above - (library) function as needed. - - -what about attribution? ------------------------ -As far as I (Z. Gilboa) am concerned, no attribution -beyond the copyright clause in argv.h is necessary. -Should you wish to include a detailed attribution note -with your project, however, you may find the following -text useful: - -The starting point for this repository was a skeleton -generated by 'sofort: portable software project template' -(git://midipix.org/sofort, MIT licensed, see COPYING.SOFORT). - -The generated skeleton included the following elements: -+ the argv parser and usage screen generator (argv.h). -+ the build system (configure, config.usage, Makefile.in, - sofort/config.vars, sofort/defs.mk, sofort/flavor.mk, - sofort/pkgconf.mk, sofort/pkgconf.sh, and - sofort/version.mk). -+ the initial driver logic: - --> src/driver/namespace_driver_ctx.c - --> src/driver/namespace_unit_ctx.c -+ and initial input mapping logic: - --> src/logic/namespace_map_input.c diff --git a/README b/README deleted file mode 100644 index adc486a..0000000 --- a/README +++ /dev/null @@ -1,50 +0,0 @@ -sofort: portable software project template ------------------------------------------- - -motivation ----------- -* take care of the elements that are common to all - (of my) cross-platform C projects, and which, for - the most part, do not (or at least should not) - contain any project-specific bits: - --> the build system; - --> the argv parser and usage screen generator; - --> the initial program driver; - --> and initial front-end utility. -* zero external dependencies: the new project is - generated from the template project using basic - shell utilities, specifically cp, grep, and sed. - -build system ------------- -* the project is conceived as a library with - an accompanying front-end utility. -* the configure script is fast and skinny, - yet comprehensive. -* unified logic for native and cross builds. -* unified logic for in-tree and out-of-tree builds. - -driver ------- -* the provided argv parser and usage screen generator - is powerful, flexible, and thread-safe; moreover, - it allows for a program driver that is entirely - independent of the chosen skin. - -skins ------ -* one benefit of the above design is that it allows the - front-end utility to have several distinct skins at - virtually no effort. - -modularity ----------- -* the distinct driver and unit context, in combination - with the thread-safe argv parser, render the front-end - utility's inclusion in a multi-call binary trivial. - -extras ------- -* the template provides the skeleton of an application - that may accept one or more files for its input, then - operate on each input file individually. diff --git a/config.project b/config.project index 96914b5..4c5efa6 100644 --- a/config.project +++ b/config.project @@ -67,9 +67,9 @@ mb_default_elf_config_defs= # config mb_all_static= mb_all_shared= -mb_disable_frontend= -mb_disable_static= -mb_disable_shared= +mb_disable_frontend=yes +mb_disable_static=yes +mb_disable_shared=yes mb_use_custom_cfgdefs=no mb_use_custom_usrdefs=no diff --git a/include/empty.dir b/include/empty.dir new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/include/empty.dir diff --git a/include/sofort/sofort.h b/include/sofort/sofort.h deleted file mode 100644 index 5495b94..0000000 --- a/include/sofort/sofort.h +++ /dev/null @@ -1,156 +0,0 @@ -#ifndef SOFORT_H -#define SOFORT_H - -#include -#include - -#include "sofort_api.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/* pre-alpha */ -#ifndef SFRT_APP -#ifndef SFRT_PRE_ALPHA -#error libsofort: pre-alpha: ABI is not final! -#error to use the library, please pass -DSFRT_PRE_ALPHA to the compiler. -#endif -#endif - -/* status codes */ -#define SFRT_OK 0x00 -#define SFRT_USAGE 0x01 -#define SFRT_ERROR 0x02 - -/* driver flags */ -#define SFRT_DRIVER_VERBOSITY_NONE 0x0000 -#define SFRT_DRIVER_VERBOSITY_ERRORS 0x0001 -#define SFRT_DRIVER_VERBOSITY_STATUS 0x0002 -#define SFRT_DRIVER_VERBOSITY_USAGE 0x0004 -#define SFRT_DRIVER_CLONE_VECTOR 0x0008 - -#define SFRT_DRIVER_VERSION 0x0010 -#define SFRT_DRIVER_DRY_RUN 0x0020 - -#define SFRT_DRIVER_ANNOTATE_ALWAYS 0x1000 -#define SFRT_DRIVER_ANNOTATE_NEVER 0x2000 -#define SFRT_DRIVER_ANNOTATE_FULL 0x4000 - -/* unit action flags */ -#define SFRT_OUTPUT_NAME 0x0001 /* dummy */ -#define SFRT_OUTPUT_ADDRESS 0x0002 /* dummy */ - -/* error flags */ -#define SFRT_ERROR_TOP_LEVEL 0x0001 -#define SFRT_ERROR_NESTED 0x0002 -#define SFRT_ERROR_CHILD 0x0004 -#define SFRT_ERROR_CUSTOM 0x0008 - -enum sfrt_custom_error { - SFRT_ERR_FLOW_ERROR, - SFRT_ERR_FLEE_ERROR, - SFRT_ERR_NULL_CONTEXT, - SFRT_ERR_NULL_SOURCE, - SFRT_ERR_INVALID_CONTEXT, - SFRT_ERR_INVALID_SOURCE, - SFRT_ERR_SOURCE_SIZE_ZERO, - SFRT_ERR_CAP, -}; - -struct sfrt_input { - void * addr; - size_t size; -}; - -struct sfrt_source_version { - int major; - int minor; - int revision; - const char * commit; -}; - -struct sfrt_fd_ctx { - int fdin; - int fdout; - int fderr; - int fdlog; - int fdcwd; - int fddst; -}; - -struct sfrt_error_info { - const struct sfrt_driver_ctx * edctx; - const struct sfrt_unit_ctx * euctx; - const char * eunit; - int esyscode; - int elibcode; - const char * efunction; - int eline; - unsigned eflags; - void * eany; -}; - -struct sfrt_common_ctx { - uint64_t drvflags; - uint64_t actflags; - uint64_t fmtflags; - const char * anystring; /* dummy */ -}; - -struct sfrt_driver_ctx { - const char ** units; - const char * program; - const char * module; - const struct sfrt_common_ctx * cctx; - struct sfrt_error_info ** errv; - void * any; -}; - -struct sfrt_unit_ctx { - const char * const * path; - const struct sfrt_input * map; - void * any; -}; - -/* driver api */ -sfrt_api int sfrt_get_driver_ctx (char **, char **, uint32_t, - const struct sfrt_fd_ctx *, - struct sfrt_driver_ctx **); - -sfrt_api void sfrt_free_driver_ctx (struct sfrt_driver_ctx *); - -sfrt_api int sfrt_get_unit_ctx (const struct sfrt_driver_ctx *, const char *, - struct sfrt_unit_ctx **); - -sfrt_api void sfrt_free_unit_ctx (struct sfrt_unit_ctx *); - -sfrt_api int sfrt_get_driver_fdctx (const struct sfrt_driver_ctx *, struct sfrt_fd_ctx *); -sfrt_api int sfrt_set_driver_fdctx (struct sfrt_driver_ctx *, const struct sfrt_fd_ctx *); - -/* utility api */ -sfrt_api int sfrt_main (char **, char **, const struct sfrt_fd_ctx *); - -sfrt_api int sfrt_output_dummy (const struct sfrt_driver_ctx *); /* dummy */ -sfrt_api int sfrt_output_name (const struct sfrt_driver_ctx *, const struct sfrt_unit_ctx *); /* dummy */ -sfrt_api int sfrt_output_address (const struct sfrt_driver_ctx *, const struct sfrt_unit_ctx *); /* dummy */ - -/* error trace api */ -sfrt_api int sfrt_output_error_record (const struct sfrt_driver_ctx *, const struct sfrt_error_info *); -sfrt_api int sfrt_output_error_vector (const struct sfrt_driver_ctx *); - -/* raw input api */ -sfrt_api int sfrt_map_input (const struct sfrt_driver_ctx *, - int, const char *, int, - struct sfrt_input *); - -sfrt_api int sfrt_unmap_input (struct sfrt_input *); - -/* package info */ -sfrt_api const struct sfrt_source_version * sfrt_source_version(void); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/include/sofort/sofort_api.h b/include/sofort/sofort_api.h deleted file mode 100644 index 1303d3c..0000000 --- a/include/sofort/sofort_api.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef SOFORT_API_H -#define SOFORT_API_H - -#include - -/* sfrt_export */ -#if defined(__dllexport) -#define sfrt_export __dllexport -#else -#define sfrt_export -#endif - -/* sfrt_import */ -#if defined(__dllimport) -#define sfrt_import __dllimport -#else -#define sfrt_import -#endif - -/* sfrt_api */ -#ifndef SFRT_APP -#if defined (SFRT_EXPORT) -#define sfrt_api sfrt_export -#elif defined (SFRT_IMPORT) -#define sfrt_api sfrt_import -#elif defined (SFRT_STATIC) -#define sfrt_api -#else -#define sfrt_api -#endif -#else -#define sfrt_api -#endif - -#endif diff --git a/project/common.mk b/project/common.mk index 8850308..4b72987 100644 --- a/project/common.mk +++ b/project/common.mk @@ -1,19 +1,7 @@ API_SRCS = \ - src/driver/sfrt_amain.c \ - src/driver/sfrt_driver_ctx.c \ - src/driver/sfrt_unit_ctx.c \ - src/logic/sfrt_map_input.c \ - src/output/sfrt_output_address.c \ - src/output/sfrt_output_dummy.c \ - src/output/sfrt_output_error.c \ - src/output/sfrt_output_name.c \ - src/skin/sfrt_skin_default.c \ INTERNAL_SRCS = \ - src/internal/$(PACKAGE)_dprintf_impl.c \ - src/internal/$(PACKAGE)_errinfo_impl.c \ APP_SRCS = \ - src/sofort.c COMMON_SRCS = $(API_SRCS) $(INTERNAL_SRCS) diff --git a/project/extras.mk b/project/extras.mk index 4d79ec5..e69de29 100644 --- a/project/extras.mk +++ b/project/extras.mk @@ -1,6 +0,0 @@ -CFLAGS_SHARED_ATTR += -DSFRT_PRE_ALPHA -DSFRT_EXPORT -CFLAGS_STATIC_ATTR += -DSFRT_PRE_ALPHA -DSFRT_STATIC -CFLAGS_APP_ATTR += -DSFRT_APP - -src/driver/sfrt_driver_ctx.o: version.tag -src/driver/sfrt_driver_ctx.lo: version.tag diff --git a/project/headers.mk b/project/headers.mk index 2f2512a..9d50ee3 100644 --- a/project/headers.mk +++ b/project/headers.mk @@ -1,11 +1,5 @@ API_HEADERS = \ - $(PROJECT_DIR)/include/$(PACKAGE)/sofort.h \ - $(PROJECT_DIR)/include/$(PACKAGE)/sofort_api.h \ INTERNAL_HEADERS = \ - $(PROJECT_DIR)/src/internal/argv/argv.h \ - $(PROJECT_DIR)/src/internal/$(PACKAGE)_dprintf_impl.h \ - $(PROJECT_DIR)/src/internal/$(PACKAGE)_driver_impl.h \ - $(PROJECT_DIR)/src/internal/$(PACKAGE)_errinfo_impl.h \ ALL_HEADERS = $(API_HEADERS) $(INTERNAL_HEADERS) diff --git a/project/tagver.mk b/project/tagver.mk index eee2041..6a5d8d6 100644 --- a/project/tagver.mk +++ b/project/tagver.mk @@ -1,4 +1,4 @@ -VER_NAMESPACE = SFRT +VER_NAMESPACE = VER_MAJOR = 0 VER_MINOR = 0 diff --git a/project/tree.mk b/project/tree.mk index d91f6a2..20f0861 100644 --- a/project/tree.mk +++ b/project/tree.mk @@ -1,8 +1,3 @@ tree.tag: mkdir -p src - mkdir -p src/driver - mkdir -p src/internal - mkdir -p src/logic - mkdir -p src/output - mkdir -p src/skin touch tree.tag diff --git a/recipiff b/recipiff deleted file mode 100755 index 50f3b77..0000000 --- a/recipiff +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/sh - -src=$1 -dst=$2 - -if [ -z "$dst" ]; then - echo 'usage: $0 src dst' - exit 1 -fi - -diff -ru $src/configure $dst/configure || exit 1 -diff -ru $src/config.usage $dst/config.usage || exit 1 -diff -ru $src/Makefile.in $dst/Makefile.in || exit 1 - -diff -ru $src/sofort $dst/sofort || exit 1 -diff -ru $src/sysinfo $dst/sysinfo || exit 1 diff --git a/sofort.sh b/sofort.sh deleted file mode 100755 index f2d038d..0000000 --- a/sofort.sh +++ /dev/null @@ -1,178 +0,0 @@ -#!/bin/sh - -usage() -{ -cat << EOF >&2 - -Usage: - -h show this HELP message - -d DSTDIR set destination directory - -p PROJECT set project name (i.e. sofort) - -n PREFIX set namespace prefix (i.e. sfrt) - -EOF -exit 1 -} - -error_dstdir_exists() -{ - echo "the destination directory '$dstdir' already exists!" >&2 - exit 2 -} - -# one: args -dstdir= -project= -namespace= - -srcdir=`dirname $0` || exit 2 -cd "$srcdir" || exit 2 -srcdir=`pwd` || exit 2 - -while getopts "hd:p:n:" opt; do - case $opt in - h) - usage - ;; - d) - dstdir="$OPTARG" - ;; - p) - project="$OPTARG" - ;; - n) - namespace="$OPTARG" - ;; - \?) - printf "Invalid option: -%s" "$OPTARG" >&2 - usage - ;; - esac -done - -# two: clone -if [ -z "$dstdir" ] || [ -z "$project" ] || [ -z "$namespace" ]; then - usage -fi - -stat "$dstdir" >/dev/null 2>/dev/null && error_dstdir_exists -mkdir -p "$(dirname $dstdir)" || exit 2 -cp -r "$srcdir" "$dstdir" || exit 2 -rm "$dstdir"/sofort.sh || exit 2 -rm "$dstdir"/recipiff || exit 2 -rm -rf "$dstdir"/.git || exit 2 - -# three: content -cd "$dstdir" || exit 2 -files=$(find . -type f) -lowerspace=`echo "$namespace" | tr '[:upper:]' '[:lower:]'`_ -upperspace=`echo "$namespace" | tr '[:lower:]' '[:upper:]'`_ - -for f in $files; do - sed -e s/sofort/$project/g "$f" > "$f.tmp" || exit 2 - mv "$f.tmp" "$f" || exit 2 - - sed -e s/sfrt_/$lowerspace/g "$f" > "$f.tmp" || exit 2 - mv "$f.tmp" "$f" || exit 2 - - sed -e s/SFRT_/$upperspace/g "$f" > "$f.tmp" || exit 2 - mv "$f.tmp" "$f" || exit 2 -done - -# and also the driver and public headers -files="$dstdir/src/driver/sfrt_driver_ctx.c" -files="$files $dstdir/src/internal/sofort_driver_impl.h" -files="$files $dstdir/include/sofort/sofort.h" -files="$files $dstdir/include/sofort/sofort_api.h" - -upperspace=`echo "$project" | tr '[:lower:]' '[:upper:]'` - -for f in $files; do - sed -e s/SOFORT/$upperspace/g "$f" > "$f.tmp" || exit 2 - mv "$f.tmp" "$f" || exit 2 -done - -# and also project/tagver.mk, which has SFRT, not SFRT_ -f=project/tagver.mk -upperspace=`echo "$namespace" | tr '[:lower:]' '[:upper:]'` -sed -e s/SFRT/$upperspace/g "$f" > "$f.tmp" || exit 2 -mv "$f.tmp" "$f" || exit 2 - -# four: directory names -mv include/sofort include/$project || exit 2 -dirs=$(find . -type d) - -for d in $dirs; do - case $d in - ./sofort | ./sofort/* ) - name=$d - ;; - * ) - name=`echo "$d" | sed -e s/sfrt_/$lowerspace/g \ - -e s/sofort/$project/g` - ;; - esac - - if [ "$d" != "$name" ]; then - mv "$d" "$name" || exit 2 - fi -done - -# five: file names -files=$(find . -type f) - -for f in $files; do - case $(dirname $f) in - ./sofort | ./sofort/* ) - name=$f - ;; - - * ) - name=`echo "$f" | sed -e s/sfrt_/$lowerspace/g \ - -e s/sofort/$project/g` - ;; - esac - - if [ "$f" != "$name" ]; then - mv "$f" "$name" || exit 2 - fi -done - -# six: references -cp "$srcdir"/COPYING.SOFORT "$dstdir" || exit 2 -cp "$srcdir"/src/internal/argv/argv.h "$dstdir"/src/internal/argv || exit 2 - -# seven: remove howto text and dummy interfaces -rm "$dstdir"/HOWTO || exit 2 -rm "$dstdir"/src/output/* || exit 2 - -recipe="$dstdir"/project/common.mk -files=$(find . -type f) - -grep -v 'src/output' "$recipe" > "$recipe".tmp || exit 2 -mv "$recipe".tmp "$recipe" || exit 2 - -for f in $files; do - grep -v 'dummy' "$f" > "$f".tmp - mv "$f".tmp "$f" || exit 2 -done - -# seven: sofort -cp -p "$srcdir/configure" "$dstdir" -cp -p "$srcdir/Makefile.in" "$dstdir" - -# eight: finalize -uppername=`echo "$project" | tr '[:lower:]' '[:upper:]'` -utilcsrc=src/$project.c - -sed -e s/SOFORT/$uppername/g $utilcsrc> $utilcsrc.tmp || exit 2 -mv $utilcsrc.tmp $utilcsrc || exit 2 - -touch COPYING.$uppername || exit 2 -echo "$project: project description" > README || exit 2 - -chmod +x sysinfo/version.sh || exit 2 -chmod +x ./configure || exit 2 - -# all done -exit 0 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 -#include -#include -#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 -#include -#include - -#define ARGV_DRIVER - -#include -#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] ...\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 -#include -#include -#include -#include - -#include -#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); - } -} diff --git a/src/empty.dir b/src/empty.dir new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/empty.dir diff --git a/src/internal/argv/argv.h b/src/internal/argv/argv.h deleted file mode 100644 index a730e76..0000000 --- a/src/internal/argv/argv.h +++ /dev/null @@ -1,942 +0,0 @@ -/****************************************************************************/ -/* argv.h: a thread-safe argument vector parser and usage screen generator */ -/* Copyright (C) 2015--2018 Z. Gilboa */ -/* Released under the Standard MIT License; see COPYING.SOFORT. */ -/* This file is (also) part of sofort: portable software project template. */ -/****************************************************************************/ - -#ifndef ARGV_H -#define ARGV_H - -#include -#include -#include -#include -#include -#include -#include - -#define ARGV_VERBOSITY_NONE 0x00 -#define ARGV_VERBOSITY_ERRORS 0x01 -#define ARGV_VERBOSITY_STATUS 0x02 -#define ARGV_CLONE_VECTOR 0x80 - -#ifndef ARGV_TAB_WIDTH -#define ARGV_TAB_WIDTH 8 -#endif - -/*******************************************/ -/* */ -/* support of hybrid options */ -/* ------------------------- */ -/* hybrid options are very similar to */ -/* long options, yet are prefixed by */ -/* a single dash rather than two */ -/* (i.e. -std, -isystem). */ -/* hybrid options are supported by this */ -/* driver for compatibility with legacy */ -/* tools; note, however, that the use */ -/* of hybrid options should be strongly */ -/* discouraged due to the limitations */ -/* they impose on short options (for */ -/* example, a driver implementing -std */ -/* may not provide -s as a short option */ -/* that takes an arbitrary value). */ -/* */ -/* SPACE: -hybrid VALUE (i.e. -MF file) */ -/* EQUAL: -hybrid=VALUE (i.e. -std=c99) */ -/* COMMA: -hybrid,VALUE (i.e. -Wl,) */ -/* ONLY: -opt accepted, --opt rejected */ -/* JOINED: -optVALUE */ -/* */ -/*******************************************/ - - -#define ARGV_OPTION_HYBRID_NONE 0x00 -#define ARGV_OPTION_HYBRID_ONLY 0x01 -#define ARGV_OPTION_HYBRID_SPACE 0x02 -#define ARGV_OPTION_HYBRID_EQUAL 0x04 -#define ARGV_OPTION_HYBRID_COMMA 0x08 -#define ARGV_OPTION_HYBRID_JOINED 0x10 -#define ARGV_OPTION_HYBRID_CIRCUS (ARGV_OPTION_HYBRID_SPACE \ - | ARGV_OPTION_HYBRID_JOINED) -#define ARGV_OPTION_HYBRID_DUAL (ARGV_OPTION_HYBRID_SPACE \ - | ARGV_OPTION_HYBRID_EQUAL) -#define ARGV_OPTION_HYBRID_SWITCH (ARGV_OPTION_HYBRID_ONLY \ - | ARGV_OPTION_HYBRID_SPACE \ - | ARGV_OPTION_HYBRID_EQUAL \ - | ARGV_OPTION_HYBRID_COMMA \ - | ARGV_OPTION_HYBRID_JOINED) - -enum argv_optarg { - ARGV_OPTARG_NONE, - ARGV_OPTARG_REQUIRED, - ARGV_OPTARG_OPTIONAL, -}; - -enum argv_mode { - ARGV_MODE_SCAN, - ARGV_MODE_COPY, -}; - -enum argv_error { - ARGV_ERROR_OK, - ARGV_ERROR_INTERNAL, - ARGV_ERROR_SHORT_OPTION, - ARGV_ERROR_LONG_OPTION, - ARGV_ERROR_OPTARG_NONE, - ARGV_ERROR_OPTARG_REQUIRED, - ARGV_ERROR_OPTARG_PARADIGM, - ARGV_ERROR_HYBRID_NONE, - ARGV_ERROR_HYBRID_ONLY, - ARGV_ERROR_HYBRID_SPACE, - ARGV_ERROR_HYBRID_EQUAL, - ARGV_ERROR_HYBRID_COMMA, -}; - -struct argv_option { - const char * long_name; - const char short_name; - int tag; - enum argv_optarg optarg; - int flags; - const char * paradigm; - const char * argname; - const char * description; -}; - -struct argv_entry { - const char * arg; - int tag; - bool fopt; - bool fval; - bool fnoscan; - enum argv_error errcode; -}; - -struct argv_meta { - char ** argv; - struct argv_entry * entries; -}; - -struct argv_ctx { - int flags; - int mode; - int nentries; - intptr_t unitidx; - intptr_t erridx; - enum argv_error errcode; - const char * errch; - const struct argv_option * erropt; - const char * program; -}; - -#ifdef ARGV_DRIVER - -struct argv_meta_impl { - char ** argv; - char * strbuf; - struct argv_meta meta; -}; - -static int argv_optv_init( - const struct argv_option[], - const struct argv_option **); - -static const char * argv_program_name(const char *); - -static void argv_usage( - int fd, - const char * header, - const struct argv_option **, - const char * mode); - -static struct argv_meta * argv_get( - char **, - const struct argv_option **, - int flags, - int fd); - -static void argv_free(struct argv_meta *); - -#ifndef argv_dprintf -#define argv_dprintf dprintf -#endif - - - -/*------------------------------------*/ -/* implementation of static functions */ -/*------------------------------------*/ - -static int argv_optv_init( - const struct argv_option options[], - const struct argv_option ** optv) -{ - const struct argv_option * option; - int i; - - for (option=options,i=0; option->long_name || option->short_name; option++) - optv[i++] = option; - - optv[i] = 0; - return i; -} - -static const struct argv_option * argv_short_option( - const char * ch, - const struct argv_option ** optv, - struct argv_entry * entry) -{ - const struct argv_option * option; - - for (; *optv; optv++) { - option = *optv; - - if (option->short_name == *ch) { - entry->tag = option->tag; - entry->fopt = true; - return option; - } - } - - return 0; -} - -static const struct argv_option * argv_long_option( - const char * ch, - const struct argv_option ** optv, - struct argv_entry * entry) -{ - const struct argv_option * option; - const char * arg; - size_t len; - - for (; *optv; optv++) { - option = *optv; - len = option->long_name ? strlen(option->long_name) : 0; - - if (len && !(strncmp(option->long_name,ch,len))) { - arg = ch + len; - - if (!*arg - || (*arg == '=') - || (option->flags & ARGV_OPTION_HYBRID_JOINED) - || ((option->flags & ARGV_OPTION_HYBRID_COMMA) - && (*arg == ','))) { - entry->tag = option->tag; - entry->fopt = true; - return option; - } - } - } - - return 0; -} - -static inline bool is_short_option(const char * arg) -{ - return (arg[0]=='-') && arg[1] && (arg[1]!='-'); -} - -static inline bool is_long_option(const char * arg) -{ - return (arg[0]=='-') && (arg[1]=='-') && arg[2]; -} - -static inline bool is_last_option(const char * arg) -{ - return (arg[0]=='-') && (arg[1]=='-') && !arg[2]; -} - -static inline bool is_hybrid_option( - const char * arg, - const struct argv_option ** optv) -{ - const struct argv_option * option; - struct argv_entry entry; - - if (!is_short_option(arg)) - return false; - - if (!(option = argv_long_option(++arg,optv,&entry))) - return false; - - if (!(option->flags & ARGV_OPTION_HYBRID_SWITCH)) - if (argv_short_option(arg,optv,&entry)) - return false; - - return true; -} - -static inline bool is_arg_in_paradigm(const char * arg, const char * paradigm) -{ - size_t len; - const char * ch; - - for (ch=paradigm,len=strlen(arg); ch; ) { - if (!strncmp(arg,ch,len)) { - if (!*(ch += len)) - return true; - else if (*ch == '|') - return true; - } - - if ((ch = strchr(ch,'|'))) - ch++; - } - - return false; -} - -static inline const struct argv_option * option_from_tag( - const struct argv_option ** optv, - int tag) -{ - for (; *optv; optv++) - if (optv[0]->tag == tag) - return optv[0]; - return 0; -} - -static void argv_scan( - char ** argv, - const struct argv_option ** optv, - struct argv_ctx * ctx, - struct argv_meta * meta) -{ - char ** parg; - const char * ch; - const char * val; - const struct argv_option * option; - struct argv_entry entry; - struct argv_entry * mentry; - enum argv_error ferr; - bool fval; - bool fnext; - bool fshort; - bool fhybrid; - bool fnoscan; - - parg = &argv[1]; - ch = *parg; - ferr = ARGV_ERROR_OK; - fshort = false; - fnoscan = false; - fval = false; - mentry = meta ? meta->entries : 0; - - ctx->unitidx = 0; - ctx->erridx = 0; - - while (ch && (ferr == ARGV_ERROR_OK)) { - option = 0; - fhybrid = false; - - if (fnoscan) - fval = true; - - else if (is_last_option(ch)) - fnoscan = true; - - else if (!fshort && is_hybrid_option(ch,optv)) - fhybrid = true; - - if (!fnoscan && !fhybrid && (fshort || is_short_option(ch))) { - if (!fshort) - ch++; - - if ((option = argv_short_option(ch,optv,&entry))) { - if (ch[1]) { - ch++; - fnext = false; - fshort = (option->optarg == ARGV_OPTARG_NONE); - } else { - parg++; - ch = *parg; - fnext = true; - fshort = false; - } - - if (option->optarg == ARGV_OPTARG_NONE) { - if (!fnext && ch && (*ch == '-')) { - ferr = ARGV_ERROR_OPTARG_NONE; - } else { - fval = false; - } - - } else if (!fnext) { - fval = true; - - } else if (option->optarg == ARGV_OPTARG_REQUIRED) { - if (ch && is_short_option(ch)) - ferr = ARGV_ERROR_OPTARG_REQUIRED; - else if (ch && is_long_option(ch)) - ferr = ARGV_ERROR_OPTARG_REQUIRED; - else if (ch && is_last_option(ch)) - ferr = ARGV_ERROR_OPTARG_REQUIRED; - else if (ch) - fval = true; - else - ferr = ARGV_ERROR_OPTARG_REQUIRED; - } else { - /* ARGV_OPTARG_OPTIONAL */ - if (ch && is_short_option(ch)) - fval = false; - else if (ch && is_long_option(ch)) - fval = false; - else if (ch && is_last_option(ch)) - fval = false; - else if (fnext) - fval = false; - else - fval = ch; - } - } else { - ferr = ARGV_ERROR_SHORT_OPTION; - } - - } else if (!fnoscan && (fhybrid || is_long_option(ch))) { - ch += (fhybrid ? 1 : 2); - - if ((option = argv_long_option(ch,optv,&entry))) { - val = ch + strlen(option->long_name); - - /* val[0] is either '=' (or ',') or '\0' */ - if (!val[0]) { - parg++; - ch = *parg; - } - - if (fhybrid && !(option->flags & ARGV_OPTION_HYBRID_SWITCH)) - ferr = ARGV_ERROR_HYBRID_NONE; - else if (!fhybrid && (option->flags & ARGV_OPTION_HYBRID_ONLY)) - ferr = ARGV_ERROR_HYBRID_ONLY; - else if (option->optarg == ARGV_OPTARG_NONE) { - if (val[0]) { - ferr = ARGV_ERROR_OPTARG_NONE; - ctx->errch = val + 1; - } else - fval = false; - } else if (val[0] && (option->flags & ARGV_OPTION_HYBRID_JOINED)) { - fval = true; - ch = val; - } else if (fhybrid && !val[0] && !(option->flags & ARGV_OPTION_HYBRID_SPACE)) - ferr = ARGV_ERROR_HYBRID_SPACE; - else if (fhybrid && (val[0]=='=') && !(option->flags & ARGV_OPTION_HYBRID_EQUAL)) - ferr = ARGV_ERROR_HYBRID_EQUAL; - else if (fhybrid && (val[0]==',') && !(option->flags & ARGV_OPTION_HYBRID_COMMA)) - ferr = ARGV_ERROR_HYBRID_COMMA; - else if (!fhybrid && (val[0]==',')) - ferr = ARGV_ERROR_HYBRID_COMMA; - else if (val[0] && !val[1]) - ferr = ARGV_ERROR_OPTARG_REQUIRED; - else if (val[0] && val[1]) { - fval = true; - ch = ++val; - } else if (option->optarg == ARGV_OPTARG_REQUIRED) { - if (!val[0] && !*parg) - ferr = ARGV_ERROR_OPTARG_REQUIRED; - else if (*parg && is_short_option(*parg)) - ferr = ARGV_ERROR_OPTARG_REQUIRED; - else if (*parg && is_long_option(*parg)) - ferr = ARGV_ERROR_OPTARG_REQUIRED; - else if (*parg && is_last_option(*parg)) - ferr = ARGV_ERROR_OPTARG_REQUIRED; - else - fval = true; - } else { - /* ARGV_OPTARG_OPTIONAL */ - fval = val[0]; - } - } else - ferr = ARGV_ERROR_LONG_OPTION; - } - - if (ferr == ARGV_ERROR_OK) - if (option && fval && option->paradigm) - if (!is_arg_in_paradigm(ch,option->paradigm)) - ferr = ARGV_ERROR_OPTARG_PARADIGM; - - if (ferr == ARGV_ERROR_OK) - if (!option && !ctx->unitidx) - ctx->unitidx = parg - argv; - - if (ferr != ARGV_ERROR_OK) { - ctx->errcode = ferr; - ctx->errch = ctx->errch ? ctx->errch : ch; - ctx->erropt = option; - ctx->erridx = parg - argv; - return; - } else if (ctx->mode == ARGV_MODE_SCAN) { - if (!fnoscan) - ctx->nentries++; - else if (fval) - ctx->nentries++; - - if (fval || !option) { - parg++; - ch = *parg; - } - } else if (ctx->mode == ARGV_MODE_COPY) { - if (fnoscan) { - if (fval) { - mentry->arg = ch; - mentry->fnoscan = true; - mentry++; - } - - parg++; - ch = *parg; - } else if (option) { - mentry->arg = fval ? ch : 0; - mentry->tag = option->tag; - mentry->fopt = true; - mentry->fval = fval; - mentry++; - - if (fval) { - parg++; - ch = *parg; - } - } else { - mentry->arg = ch; - mentry++; - parg++; - ch = *parg; - } - } - } -} - -static const char * argv_program_name(const char * program_path) -{ - const char * ch; - - if (program_path) { - if ((ch = strrchr(program_path,'/'))) - return *(++ch) ? ch : 0; - - if ((ch = strrchr(program_path,'\\'))) - return *(++ch) ? ch : 0; - } - - return program_path; -} - -static void argv_show_error(int fd, struct argv_ctx * ctx) -{ - char opt_short_name[2] = {0,0}; - - if (ctx->erropt && ctx->erropt->short_name) - opt_short_name[0] = ctx->erropt->short_name; - - argv_dprintf(fd,"%s: error: ",ctx->program); - - switch (ctx->errcode) { - case ARGV_ERROR_SHORT_OPTION: - argv_dprintf(fd,"'-%c' is not a valid short option\n",*ctx->errch); - break; - - case ARGV_ERROR_LONG_OPTION: - argv_dprintf(fd,"'--%s' is not a valid long option\n",ctx->errch); - break; - - case ARGV_ERROR_OPTARG_NONE: - argv_dprintf(fd,"'%s' is not a valid option value for [%s%s%s%s%s] " - "(option values may not be specified)\n", - ctx->errch, - opt_short_name[0] ? "-" : "", - opt_short_name, - opt_short_name[0] ? "," : "", - ctx->erropt->long_name ? "--" : "", - ctx->erropt->long_name); - break; - - case ARGV_ERROR_OPTARG_REQUIRED: - argv_dprintf(fd,"option [%s%s%s%s%s] requires %s %s%s%s\n", - opt_short_name[0] ? "-" : "", - opt_short_name, - opt_short_name[0] ? "," : "", - ctx->erropt->long_name ? "--" : "", - ctx->erropt->long_name, - ctx->erropt->paradigm ? "one of the following values:" : "a value", - ctx->erropt->paradigm ? "{" : "", - ctx->erropt->paradigm ? ctx->erropt->paradigm : "", - ctx->erropt->paradigm ? "}" : ""); - break; - - case ARGV_ERROR_OPTARG_PARADIGM: - argv_dprintf(fd,"'%s' is not a valid option value for [%s%s%s%s%s]={%s}\n", - ctx->errch, - opt_short_name[0] ? "-" : "", - opt_short_name, - opt_short_name[0] ? "," : "", - ctx->erropt->long_name ? "--" : "", - ctx->erropt->long_name, - ctx->erropt->paradigm); - break; - - case ARGV_ERROR_HYBRID_NONE: - argv_dprintf(fd,"-%s is not a synonym for --%s\n", - ctx->erropt->long_name, - ctx->erropt->long_name); - break; - - case ARGV_ERROR_HYBRID_ONLY: - argv_dprintf(fd,"--%s is not a synonym for -%s\n", - ctx->erropt->long_name, - ctx->erropt->long_name); - break; - - case ARGV_ERROR_HYBRID_SPACE: - case ARGV_ERROR_HYBRID_EQUAL: - case ARGV_ERROR_HYBRID_COMMA: - argv_dprintf(fd,"-%s: illegal value assignment; valid syntax is " - "-%s%sVAL\n", - ctx->erropt->long_name, - ctx->erropt->long_name, - (ctx->erropt->flags & ARGV_OPTION_HYBRID_SPACE) - ? " " : (ctx->erropt->flags & ARGV_OPTION_HYBRID_EQUAL) - ? "=" : (ctx->erropt->flags & ARGV_OPTION_HYBRID_COMMA) - ? "," : ""); - - break; - - case ARGV_ERROR_INTERNAL: - argv_dprintf(fd,"internal error"); - break; - - default: - break; - } -} - -static void argv_show_status( - int fd, - const struct argv_option ** optv, - struct argv_ctx * ctx, - struct argv_meta * meta) -{ - int argc; - char ** argv; - struct argv_entry * entry; - const struct argv_option * option; - char short_name[2] = {0}; - const char * space = ""; - - (void)ctx; - - argv_dprintf(fd,"\n\nconcatenated command line:\n"); - for (argv=meta->argv; *argv; argv++) { - argv_dprintf(fd,"%s%s",space,*argv); - space = " "; - } - - argv_dprintf(fd,"\n\nargument vector:\n"); - for (argc=0,argv=meta->argv; *argv; argc++,argv++) - argv_dprintf(fd,"argv[%d]: %s\n",argc,*argv); - - argv_dprintf(fd,"\n\nparsed entries:\n"); - for (entry=meta->entries; entry->arg || entry->fopt; entry++) - if (entry->fopt) { - option = option_from_tag(optv,entry->tag); - short_name[0] = option->short_name; - - if (entry->fval) - argv_dprintf(fd,"[-%s,--%s] := %s\n", - short_name,option->long_name,entry->arg); - else - argv_dprintf(fd,"[-%s,--%s]\n", - short_name,option->long_name); - } else - argv_dprintf(fd," := %s\n",entry->arg); - - argv_dprintf(fd,"\n\n"); -} - -static struct argv_meta * argv_free_impl(struct argv_meta_impl * imeta) -{ - if (imeta->argv) - free(imeta->argv); - - if (imeta->strbuf) - free(imeta->strbuf); - - if (imeta->meta.entries) - free(imeta->meta.entries); - - free(imeta); - return 0; -} - -static struct argv_meta * argv_alloc(char ** argv, struct argv_ctx * ctx) -{ - struct argv_meta_impl * imeta; - char ** vector; - char * dst; - size_t size; - int argc; - int i; - - if (!(imeta = calloc(1,sizeof(*imeta)))) - return 0; - - if (ctx->flags & ARGV_CLONE_VECTOR) { - for (vector=argv,argc=0,size=0; *vector; vector++) { - size += strlen(*vector) + 1; - argc++; - } - - if (!(imeta->argv = calloc(argc+1,sizeof(char *)))) - return argv_free_impl(imeta); - else if (!(imeta->strbuf = calloc(1,size+1))) - return argv_free_impl(imeta); - - for (i=0,dst=imeta->strbuf; iargv[i] = dst; - dst += strlen(dst)+1; - } - - imeta->meta.argv = imeta->argv; - } else - imeta->meta.argv = argv; - - if (!(imeta->meta.entries = calloc( - ctx->nentries+1, - sizeof(struct argv_entry)))) - return argv_free_impl(imeta); - else - return &imeta->meta; -} - -static struct argv_meta * argv_get( - char ** argv, - const struct argv_option ** optv, - int flags, - int fd) -{ - struct argv_meta * meta; - struct argv_ctx ctx = {flags,ARGV_MODE_SCAN,0,0,0,0,0,0,0}; - - argv_scan(argv,optv,&ctx,0); - - if (ctx.errcode != ARGV_ERROR_OK) { - ctx.program = argv_program_name(argv[0]); - - if (ctx.flags & ARGV_VERBOSITY_ERRORS) - argv_show_error(fd,&ctx); - - return 0; - } - - if (!(meta = argv_alloc(argv,&ctx))) - return 0; - - ctx.mode = ARGV_MODE_COPY; - argv_scan(meta->argv,optv,&ctx,meta); - - if (ctx.errcode != ARGV_ERROR_OK) { - ctx.program = argv[0]; - ctx.errcode = ARGV_ERROR_INTERNAL; - argv_show_error(fd,&ctx); - argv_free(meta); - - return 0; - } - - if (ctx.flags & ARGV_VERBOSITY_STATUS) - argv_show_status(fd,optv,&ctx,meta); - - return meta; -} - -static void argv_free(struct argv_meta * xmeta) -{ - struct argv_meta_impl * imeta; - uintptr_t addr; - - if (xmeta) { - addr = (uintptr_t)xmeta - offsetof(struct argv_meta_impl,meta); - imeta = (struct argv_meta_impl *)addr; - argv_free_impl(imeta); - } -} - -static void argv_usage( - int fd, - const char * header, - const struct argv_option ** options, - const char * mode) -{ - const struct argv_option ** optv; - const struct argv_option * option; - bool fshort,flong,fboth; - size_t len,optlen,desclen; - char cache; - char * prefix; - char * desc; - char * mark; - char * cap; - char description[4096]; - char optstr [72]; - const size_t optcap = 64; - const size_t width = 80; - const char indent[] = " "; - const char creset[] = "\x1b[0m"; - const char cbold [] = "\x1b[1m"; - const char cgreen[] = "\x1b[32m"; - const char cblue [] = "\x1b[34m"; - const char ccyan [] = "\x1b[36m"; - const char * color = ccyan; - bool fcolor; - - fshort = mode ? !strcmp(mode,"short") : 0; - flong = fshort ? 0 : mode && !strcmp(mode,"long"); - fboth = !fshort && !flong; - fcolor = isatty(fd); - - if (fcolor) - argv_dprintf(fd,"%s%s",cbold,cgreen); - - if (header) - argv_dprintf(fd,"%s",header); - - for (optlen=0,optv=options; *optv; optv++) { - option = *optv; - - /* indent + comma */ - len = fboth ? sizeof(indent) + 1 : sizeof(indent); - - /* -o */ - if (fshort || fboth) - len += option->short_name - ? 2 : 0; - - /* --option */ - if (flong || fboth) - len += option->long_name - ? 2 + strlen(option->long_name) : 0; - - /* optlen */ - if (len > optlen) - optlen = len; - } - - if (optlen >= optcap) { - argv_dprintf(fd, - "Option strings exceed %zu characters, " - "please generate the usage screen manually.\n", - optcap); - return; - } - - optlen += ARGV_TAB_WIDTH; - optlen &= (~(ARGV_TAB_WIDTH-1)); - desclen = (optlen < width / 2) ? width - optlen : optlen; - - for (optv=options; *optv; optv++) { - option = *optv; - - /* color */ - if (fcolor) { - color = (color == ccyan) ? cblue : ccyan; - argv_dprintf(fd,color); - } - - /* description, using either paradigm or argname if applicable */ - snprintf(description,sizeof(description),option->description, - option->paradigm - ? option->paradigm - : option->argname ? option->argname : ""); - description[sizeof(description)-1] = 0; - - /* long/hybrid option prefix (-/--) */ - prefix = option->flags & ARGV_OPTION_HYBRID_ONLY - ? " -" : "--"; - - /* option string */ - if (fboth && option->short_name && option->long_name) - sprintf(optstr,"%s-%c,%s%s", - indent,option->short_name,prefix,option->long_name); - - else if ((fshort || fboth) && option->short_name) - sprintf(optstr,"%s-%c",indent,option->short_name); - - else if (flong && option->long_name) - sprintf(optstr,"%s%s%s", - indent,prefix,option->long_name); - - else if (fboth && option->long_name) - sprintf(optstr,"%s %s%s", - indent,prefix,option->long_name); - - else - optstr[0] = 0; - - /* right-indented option buffer */ - if (description[0]) { - len = strlen(optstr); - sprintf(&optstr[len],"%-*c",(int)(optlen-len),' '); - } - - /* single line? */ - if (optlen + strlen(description) < width) { - argv_dprintf(fd,"%s%s\n",optstr,description); - - } else { - desc = description; - cap = desc + strlen(description); - - while (desc < cap) { - mark = (desc + desclen >= cap) - ? cap : desc + desclen; - - while (*mark && (mark > desc) - && (*mark != ' ') - && (*mark != '|') - && (*mark != '\t') - && (*mark != '\n')) - mark--; - - if (mark == desc) { - mark = (desc + desclen >= cap) - ? cap : desc + desclen; - cache = *mark; - *mark = 0; - } else if (*mark == '|') { - cache = *mark; - *mark = 0; - } else { - cache = 0; - *mark = 0; - } - - /* first line? */ - if (desc == description) - argv_dprintf(fd,"%s%s\n",optstr,desc); - else - argv_dprintf(fd,"%-*c %s\n", - (*desc == '|') - ? (int)(optlen+1) - : (int)optlen, - ' ',desc); - - if (cache) - *mark = cache; - else - mark++; - - desc = mark; - } - } - } - - if (fcolor) - argv_dprintf(fd,creset); -} - -#endif - -#endif diff --git a/src/internal/sofort_dprintf_impl.c b/src/internal/sofort_dprintf_impl.c deleted file mode 100644 index 01f1ee1..0000000 --- a/src/internal/sofort_dprintf_impl.c +++ /dev/null @@ -1,56 +0,0 @@ -#include -#include -#include -#include -#include - -int sfrt_dprintf(int fd, const char * fmt, ...) -{ - int ret; - int cnt; - int size; - va_list ap; - char * ch; - char * buf; - char chbuf[2048]; - - va_start(ap,fmt); - - size = sizeof(chbuf); - buf = ((cnt = vsnprintf(chbuf,size,fmt,ap)) < size) - ? chbuf : malloc(cnt + 1); - - va_end(ap); - - if (buf == chbuf) { - (void)0; - - } else if (buf) { - va_start(ap,fmt); - vsprintf(buf,fmt,ap); - va_end(ap); - - } else { - return -1; - } - - ret = 0; - ch = buf; - - for (; cnt && ret>=0; ) { - ret = write(fd,ch,cnt); - - while ((ret < 0) && (errno == EINTR)) - ret = write(fd,ch,cnt); - - ch += ret; - cnt -= ret; - } - - ret = (ret < 0) ? -1 : ch - buf; - - if (buf != chbuf) - free(buf); - - return ret; -} diff --git a/src/internal/sofort_dprintf_impl.h b/src/internal/sofort_dprintf_impl.h deleted file mode 100644 index 1508ffc..0000000 --- a/src/internal/sofort_dprintf_impl.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef SOFORT_DPRINTF_IMPL_H -#define SOFORT_DPRINTF_IMPL_H - -#ifdef ARGV_DRIVER -#define argv_dprintf sfrt_dprintf -#endif - -int sfrt_dprintf(int fd, const char * fmt, ...); - -#endif diff --git a/src/internal/sofort_driver_impl.h b/src/internal/sofort_driver_impl.h deleted file mode 100644 index a7615ad..0000000 --- a/src/internal/sofort_driver_impl.h +++ /dev/null @@ -1,108 +0,0 @@ -#ifndef SOFORT_DRIVER_IMPL_H -#define SOFORT_DRIVER_IMPL_H - -#include -#include -#include - -#include -#include "sofort_dprintf_impl.h" -#include "argv/argv.h" - -#define SFRT_OPTV_ELEMENTS 64 - -extern const struct argv_option sfrt_default_options[]; - -enum app_tags { - TAG_HELP, - TAG_VERSION, - TAG_OUTPUT_DUMMY, /* dummy */ - TAG_OUTPUT_PROPERTY, /* dummy */ -}; - -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; - struct sfrt_error_info ** erricap; - struct sfrt_error_info * erriptr[64]; - struct sfrt_error_info erribuf[64]; -}; - -struct sfrt_unit_ctx_impl { - const char * path; - struct sfrt_input map; - struct sfrt_unit_ctx uctx; -}; - -static inline struct sfrt_driver_ctx_impl * sfrt_get_driver_ictx( - const struct sfrt_driver_ctx * dctx) -{ - uintptr_t addr; - - if (dctx) { - addr = (uintptr_t)dctx - offsetof(struct sfrt_driver_ctx_impl,ctx); - return (struct sfrt_driver_ctx_impl *)addr; - } - - return 0; -} - -static inline void sfrt_driver_set_ectx( - const struct sfrt_driver_ctx * dctx, - const struct sfrt_unit_ctx * uctx, - const char * unit) -{ - struct sfrt_driver_ctx_impl * ictx; - - ictx = sfrt_get_driver_ictx(dctx); - ictx->euctx = uctx; - 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/internal/sofort_errinfo_impl.c b/src/internal/sofort_errinfo_impl.c deleted file mode 100644 index 31b008a..0000000 --- a/src/internal/sofort_errinfo_impl.c +++ /dev/null @@ -1,39 +0,0 @@ -#include -#include "sofort_driver_impl.h" -#include "sofort_errinfo_impl.h" - -int sfrt_record_error( - const struct sfrt_driver_ctx * dctx, - int esyscode, - int elibcode, - const char * efunction, - int eline, - unsigned eflags, - void * eany) -{ - struct sfrt_driver_ctx_impl * ictx; - struct sfrt_error_info * erri; - - ictx = sfrt_get_driver_ictx(dctx); - - if (ictx->errinfp == ictx->erricap) - return -1; - - *ictx->errinfp = &ictx->erribuf[ictx->errinfp - ictx->erriptr]; - erri = *ictx->errinfp; - - erri->euctx = ictx->euctx; - erri->eunit = ictx->eunit; - - erri->edctx = dctx; - erri->esyscode = esyscode; - erri->elibcode = elibcode; - erri->efunction = efunction; - erri->eline = eline; - erri->eflags = eflags; - erri->eany = eany; - - ictx->errinfp++; - - return -1; -} diff --git a/src/internal/sofort_errinfo_impl.h b/src/internal/sofort_errinfo_impl.h deleted file mode 100644 index 8d42fc3..0000000 --- a/src/internal/sofort_errinfo_impl.h +++ /dev/null @@ -1,74 +0,0 @@ -#include -#include - -int sfrt_record_error( - const struct sfrt_driver_ctx *, - int esyscode, - int elibcode, - const char * efunction, - int eline, - unsigned eflags, - void * ectx); - -#define SFRT_SYSTEM_ERROR(dctx) \ - sfrt_record_error( \ - dctx, \ - errno, \ - 0, \ - __func__, \ - __LINE__, \ - SFRT_ERROR_TOP_LEVEL, \ - 0) - -#define SFRT_BUFFER_ERROR(dctx) \ - sfrt_record_error( \ - dctx, \ - ENOBUFS, \ - 0, \ - __func__, \ - __LINE__, \ - SFRT_ERROR_TOP_LEVEL, \ - 0) - -#define SFRT_SPAWN_ERROR(dctx) \ - sfrt_record_error( \ - dctx, \ - errno, \ - 0, \ - __func__, \ - __LINE__, \ - SFRT_ERROR_TOP_LEVEL \ - | (errno ? 0 \ - : SFRT_ERROR_CHILD), \ - 0) - -#define SFRT_FILE_ERROR(dctx) \ - sfrt_record_error( \ - dctx, \ - EIO, \ - 0, \ - __func__, \ - __LINE__, \ - SFRT_ERROR_TOP_LEVEL, \ - 0) - -#define SFRT_CUSTOM_ERROR(dctx,elibcode) \ - sfrt_record_error( \ - dctx, \ - 0, \ - elibcode, \ - __func__, \ - __LINE__, \ - SFRT_ERROR_TOP_LEVEL \ - | SFRT_ERROR_CUSTOM, \ - 0) - -#define SFRT_NESTED_ERROR(dctx) \ - sfrt_record_error( \ - dctx, \ - 0, \ - 0, \ - __func__, \ - __LINE__, \ - SFRT_ERROR_NESTED, \ - 0) diff --git a/src/logic/sfrt_map_input.c b/src/logic/sfrt_map_input.c deleted file mode 100644 index aba0427..0000000 --- a/src/logic/sfrt_map_input.c +++ /dev/null @@ -1,60 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#include -#include "sofort_driver_impl.h" -#include "sofort_errinfo_impl.h" - -int sfrt_map_input( - const struct sfrt_driver_ctx * dctx, - int fd, - const char * path, - int prot, - struct sfrt_input * map) -{ - int ret; - struct stat st; - bool fnew; - int fdcwd; - - fdcwd = sfrt_driver_fdcwd(dctx); - - if ((fnew = (fd < 0))) - fd = openat(fdcwd,path,O_RDONLY|O_CLOEXEC); - - if (fd < 0) - return SFRT_SYSTEM_ERROR(dctx); - - if ((ret = fstat(fd,&st) < 0) && fnew) - close(fd); - - else if ((st.st_size == 0) && fnew) - close(fd); - - if (ret < 0) - return SFRT_SYSTEM_ERROR(dctx); - - else if (st.st_size == 0) - return SFRT_CUSTOM_ERROR( - dctx,SFRT_ERR_SOURCE_SIZE_ZERO); - - map->size = st.st_size; - map->addr = mmap(0,map->size,prot,MAP_PRIVATE,fd,0); - - if (fnew) - close(fd); - - return (map->addr == MAP_FAILED) - ? SFRT_SYSTEM_ERROR(dctx) - : 0; -} - -int sfrt_unmap_input(struct sfrt_input * map) -{ - return munmap(map->addr,map->size); -} diff --git a/src/output/sfrt_output_address.c b/src/output/sfrt_output_address.c deleted file mode 100644 index c9b16a1..0000000 --- a/src/output/sfrt_output_address.c +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#include -#include -#include - -#include -#include "sofort_driver_impl.h" -#include "sofort_dprintf_impl.h" - -int sfrt_output_address( - const struct sfrt_driver_ctx * dctx, - const struct sfrt_unit_ctx * uctx) -{ - return sfrt_dprintf( - sfrt_driver_fdout(dctx), - "%*p\n", - (int)(2*sizeof(size_t)), - uctx->map->addr); -} diff --git a/src/output/sfrt_output_dummy.c b/src/output/sfrt_output_dummy.c deleted file mode 100644 index c29f7e1..0000000 --- a/src/output/sfrt_output_dummy.c +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include -#include -#include -#include - -#include -#include "sofort_driver_impl.h" -#include "sofort_dprintf_impl.h" - -int sfrt_output_dummy (const struct sfrt_driver_ctx * dctx) -{ - return sfrt_dprintf( - sfrt_driver_fdout(dctx), - "%s\n", - dctx->cctx->anystring - ? dctx->cctx->anystring - : "(null)"); -} diff --git a/src/output/sfrt_output_error.c b/src/output/sfrt_output_error.c deleted file mode 100644 index 24cb21a..0000000 --- a/src/output/sfrt_output_error.c +++ /dev/null @@ -1,229 +0,0 @@ -#include -#include -#include -#include - -#include -#include "sofort_driver_impl.h" -#include "sofort_dprintf_impl.h" - -static const char aclr_reset[] = "\x1b[0m"; -static const char aclr_bold[] = "\x1b[1m"; - -static const char aclr_red[] = "\x1b[31m"; -static const char aclr_green[] = "\x1b[32m"; -static const char aclr_blue[] = "\x1b[34m"; -static const char aclr_magenta[] = "\x1b[35m"; - -static const char const * const sfrt_error_strings[SFRT_ERR_CAP] = { - [SFRT_ERR_FLOW_ERROR] = "flow error: unexpected condition or other", - [SFRT_ERR_FLEE_ERROR] = "flees and bugs and cats and mice", - [SFRT_ERR_NULL_CONTEXT] = "null driver or unit context", - [SFRT_ERR_NULL_SOURCE] = "source file does not define any data", - [SFRT_ERR_INVALID_CONTEXT] = "invalid driver or unit context", - [SFRT_ERR_INVALID_SOURCE] = "invalid source file", - [SFRT_ERR_SOURCE_SIZE_ZERO] = "cannot map an empty source file", -}; - -static const char * sfrt_output_error_header(const struct sfrt_error_info * erri) -{ - if (erri->eflags & SFRT_ERROR_CHILD) - return "exec error upon"; - - else if (erri->eflags & SFRT_ERROR_TOP_LEVEL) - return "error logged in"; - - else if (erri->eflags & SFRT_ERROR_NESTED) - return "< returned to >"; - - else - return "distorted state"; -} - -static const char * sfrt_output_unit_header(const struct sfrt_error_info * erri) -{ - if (!(erri->eflags & SFRT_ERROR_CUSTOM)) - return "while opening"; - - else if (erri->elibcode == SFRT_ERR_SOURCE_SIZE_ZERO) - return "while mapping"; - - else - return "while parsing"; -} - -static const char * sfrt_output_strerror( - const struct sfrt_error_info * erri, - char (*errbuf)[256]) -{ - if (erri->eflags & SFRT_ERROR_CUSTOM) - return ((erri->elibcode < 0) || (erri->elibcode >= SFRT_ERR_CAP)) - ? "internal error: please report to the maintainer" - : sfrt_error_strings[erri->elibcode]; - - else if (erri->eflags & SFRT_ERROR_NESTED) - return ""; - - else if (erri->eflags & SFRT_ERROR_CHILD) - return "(see child process error messages)"; - - else if (erri->esyscode == ENOBUFS) - return "input error: string length exceeds buffer size."; - - else - return strerror_r(erri->esyscode,*errbuf,sizeof(*errbuf)) - ? "internal error: strerror_r(3) call failed" - : *errbuf; -} - -static int sfrt_output_error_record_plain( - const struct sfrt_driver_ctx * dctx, - const struct sfrt_error_info * erri) -{ - const char * epath; - char errbuf[256]; - - int fderr = sfrt_driver_fderr(dctx); - const char * errdesc = sfrt_output_strerror(erri,&errbuf); - - epath = erri->euctx - ? *erri->euctx->path - : erri->eunit; - - if (epath && !(erri->eflags & SFRT_ERROR_NESTED)) - if (sfrt_dprintf( - fderr, - "%s: [%s] '%s':\n", - dctx->program, - sfrt_output_unit_header(erri), - epath) < 0) - return -1; - - if (sfrt_dprintf( - fderr, - "%s: %s %s(), line %d%s%s.\n", - dctx->program, - sfrt_output_error_header(erri), - erri->efunction, - erri->eline, - strlen(errdesc) ? ": " : "", - errdesc) < 0) - return -1; - - return 0; -} - -static int sfrt_output_error_record_annotated( - const struct sfrt_driver_ctx * dctx, - const struct sfrt_error_info * erri) -{ - const char * epath; - char errbuf[256]; - - int fderr = sfrt_driver_fderr(dctx); - const char * errdesc = sfrt_output_strerror(erri,&errbuf); - - epath = erri->euctx - ? *erri->euctx->path - : erri->eunit; - - if (epath && !(erri->eflags & SFRT_ERROR_NESTED)) - if (sfrt_dprintf( - fderr, - "%s%s%s:%s %s[%s]%s %s%s'%s'%s:\n", - - aclr_bold,aclr_magenta, - dctx->program, - aclr_reset, - - aclr_bold, - sfrt_output_unit_header(erri), - aclr_reset, - - aclr_bold,aclr_red, - epath, - aclr_reset) < 0) - return -1; - - if (sfrt_dprintf( - fderr, - "%s%s%s:%s %s%s%s %s%s%s()%s, %s%sline %d%s%s%s%s%s.\n", - - aclr_bold,aclr_magenta, - dctx->program, - aclr_reset, - - aclr_bold, - sfrt_output_error_header(erri), - aclr_reset, - - aclr_bold,aclr_blue, - erri->efunction, - aclr_reset, - - aclr_bold,aclr_green, - erri->eline, - aclr_reset, - strlen(errdesc) ? ": " : "", - - aclr_bold, - errdesc, - aclr_reset) < 0) - return -1; - - return 0; -} - -int sfrt_output_error_record( - const struct sfrt_driver_ctx * dctx, - const struct sfrt_error_info * erri) -{ - if (dctx->cctx->drvflags & SFRT_DRIVER_ANNOTATE_NEVER) - return sfrt_output_error_record_plain(dctx,erri); - - else if (dctx->cctx->drvflags & SFRT_DRIVER_ANNOTATE_ALWAYS) - return sfrt_output_error_record_annotated(dctx,erri); - - else if (isatty(sfrt_driver_fderr(dctx))) - return sfrt_output_error_record_annotated(dctx,erri); - - else - return sfrt_output_error_record_plain(dctx,erri); -} - -static int sfrt_output_error_vector_plain(const struct sfrt_driver_ctx * dctx) -{ - struct sfrt_error_info ** perr; - - for (perr=dctx->errv; *perr; perr++) - if (sfrt_output_error_record_plain(dctx,*perr)) - return -1; - - return 0; -} - -static int sfrt_output_error_vector_annotated(const struct sfrt_driver_ctx * dctx) -{ - struct sfrt_error_info ** perr; - - for (perr=dctx->errv; *perr; perr++) - if (sfrt_output_error_record_annotated(dctx,*perr)) - return -1; - - return 0; -} - -int sfrt_output_error_vector(const struct sfrt_driver_ctx * dctx) -{ - if (dctx->cctx->drvflags & SFRT_DRIVER_ANNOTATE_NEVER) - return sfrt_output_error_vector_plain(dctx); - - else if (dctx->cctx->drvflags & SFRT_DRIVER_ANNOTATE_ALWAYS) - return sfrt_output_error_vector_annotated(dctx); - - else if (isatty(sfrt_driver_fderr(dctx))) - return sfrt_output_error_vector_annotated(dctx); - - else - return sfrt_output_error_vector_plain(dctx); -} diff --git a/src/output/sfrt_output_name.c b/src/output/sfrt_output_name.c deleted file mode 100644 index 7eb874e..0000000 --- a/src/output/sfrt_output_name.c +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include -#include -#include -#include - -#include -#include "sofort_driver_impl.h" -#include "sofort_dprintf_impl.h" - -int sfrt_output_name( - const struct sfrt_driver_ctx * dctx, - const struct sfrt_unit_ctx * uctx) -{ - return sfrt_dprintf( - sfrt_driver_fdout(dctx), - "%s\n", - *uctx->path); -} diff --git a/src/skin/sfrt_skin_default.c b/src/skin/sfrt_skin_default.c deleted file mode 100644 index 9ac7957..0000000 --- a/src/skin/sfrt_skin_default.c +++ /dev/null @@ -1,18 +0,0 @@ -#include "sofort_driver_impl.h" -#include "argv/argv.h" - -const struct argv_option sfrt_default_options[] = { - {"version", 'v',TAG_VERSION,ARGV_OPTARG_NONE,0,0,0, - "show version information"}, - - {"help", 'h',TAG_HELP,ARGV_OPTARG_OPTIONAL,0,"short|long",0, - "show usage information [listing %s options only]"}, - - {"output-dummy", 'o',TAG_OUTPUT_DUMMY,ARGV_OPTARG_REQUIRED,0,0,"", /* dummy */ - "output %s"}, /* dummy */ - /* dummy */ - {"output-property", 'p',TAG_OUTPUT_PROPERTY,ARGV_OPTARG_REQUIRED,0,"name|address",0,/* dummy */ - "output %s"}, /* dummy */ - /* dummy */ - {0,0,0,0,0,0,0,0} -}; diff --git a/src/sofort.c b/src/sofort.c deleted file mode 100644 index 0c1279e..0000000 --- a/src/sofort.c +++ /dev/null @@ -1,6 +0,0 @@ -#include - -int main(int argc, char ** argv, char ** envp) -{ - return sfrt_main(argv,envp,0); -}