From 9e67a4a4437bce28c494b957b7ee03a3683e60ed Mon Sep 17 00:00:00 2001 From: midipix Date: Jun 25 2016 19:31:05 +0000 Subject: utility: improved box integration, removed need for build-time macro. --- diff --git a/project/common.mk b/project/common.mk index 3bc5e54..5f11b1b 100644 --- a/project/common.mk +++ b/project/common.mk @@ -1,4 +1,5 @@ COMMON_SRCS = \ + src/driver/sfrt_amain.c \ src/driver/sfrt_driver_ctx.c \ src/driver/sfrt_unit_ctx.c \ src/logic/sfrt_map_input.c \ diff --git a/src/driver/sfrt_amain.c b/src/driver/sfrt_amain.c new file mode 100644 index 0000000..3766b67 --- /dev/null +++ b/src/driver/sfrt_amain.c @@ -0,0 +1,91 @@ +#include +#include +#include +#include "sofort_driver_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) +{ + const struct sfrt_source_version * verinfo; + const char * const * verclr; + + verinfo = sfrt_source_version(); + verclr = isatty(STDOUT_FILENO) ? sfrt_ver_color : sfrt_ver_plain; + + return fprintf(stdout,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(struct sfrt_unit_ctx * uctx) +{ + uint64_t flags = uctx->cctx->actflags; /* dummy */ + /* dummy */ + if (flags & SFRT_OUTPUT_NAME) { /* dummy */ + uctx->status = sfrt_output_name(uctx,stdout); /* dummy */ + uctx->nerrors += !!uctx->status; /* dummy */ + } /* dummy */ + /* dummy */ + if (flags & SFRT_OUTPUT_ADDRESS) { /* dummy */ + uctx->status = sfrt_output_address(uctx,stdout);/* dummy */ + uctx->nerrors += !!uctx->status; /* dummy */ + } /* dummy */ + (void)uctx; +} + +static int sfrt_exit(struct sfrt_driver_ctx * dctx, int nerrors) +{ + sfrt_free_driver_ctx(dctx); + return nerrors ? 2 : 0; +} + +int sfrt_main(int argc, char ** argv, char ** envp) +{ + int ret; + 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))) + return (ret == SFRT_USAGE) ? !--argc : 2; + + if (dctx->cctx->drvflags & SFRT_DRIVER_VERSION) + if ((sfrt_version(dctx)) < 0) + return sfrt_exit(dctx,2); + + if (dctx->cctx->anystring) /* dummy */ + if ((sfrt_output_dummy(dctx->cctx,stdout)) < 0) /* dummy */ + return sfrt_exit(dctx,2); /* dummy */ + /* dummy */ + for (unit=dctx->units; *unit; unit++) { + if (!(sfrt_get_unit_ctx(dctx,*unit,&uctx))) { + sfrt_perform_unit_actions(uctx); + ret += uctx->nerrors; + sfrt_free_unit_ctx(uctx); + } + } + + return sfrt_exit(dctx,ret); +} diff --git a/src/sofort.c b/src/sofort.c index 553bda6..112823d 100644 --- a/src/sofort.c +++ b/src/sofort.c @@ -1,100 +1,6 @@ -#include -#include #include -#include "sofort_driver_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) -{ - const struct sfrt_source_version * verinfo; - const char * const * verclr; - - verinfo = sfrt_source_version(); - verclr = isatty(STDOUT_FILENO) ? sfrt_ver_color : sfrt_ver_plain; - - return fprintf(stdout,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(struct sfrt_unit_ctx * uctx) -{ - uint64_t flags = uctx->cctx->actflags; /* dummy */ - /* dummy */ - if (flags & SFRT_OUTPUT_NAME) { /* dummy */ - uctx->status = sfrt_output_name(uctx,stdout); /* dummy */ - uctx->nerrors += !!uctx->status; /* dummy */ - } /* dummy */ - /* dummy */ - if (flags & SFRT_OUTPUT_ADDRESS) { /* dummy */ - uctx->status = sfrt_output_address(uctx,stdout);/* dummy */ - uctx->nerrors += !!uctx->status; /* dummy */ - } /* dummy */ - (void)uctx; -} - -static int sfrt_exit(struct sfrt_driver_ctx * dctx, int nerrors) -{ - sfrt_free_driver_ctx(dctx); - return nerrors ? 2 : 0; -} - -int sfrt_main(int argc, char ** argv, char ** envp) -{ - int ret; - 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))) - return (ret == SFRT_USAGE) ? !--argc : 2; - - if (dctx->cctx->drvflags & SFRT_DRIVER_VERSION) - if ((sfrt_version(dctx)) < 0) - return sfrt_exit(dctx,2); - - if (dctx->cctx->anystring) /* dummy */ - if ((sfrt_output_dummy(dctx->cctx,stdout)) < 0) /* dummy */ - return sfrt_exit(dctx,2); /* dummy */ - /* dummy */ - for (unit=dctx->units; *unit; unit++) { - if (!(sfrt_get_unit_ctx(dctx,*unit,&uctx))) { - sfrt_perform_unit_actions(uctx); - ret += uctx->nerrors; - sfrt_free_unit_ctx(uctx); - } - } - - return sfrt_exit(dctx,ret); -} - -#ifndef SOFORT_IN_A_BOX int main(int argc, char ** argv, char ** envp) { return sfrt_main(argc,argv,envp); } - -#endif