diff options
author | midipix <writeonce@midipix.org> | 2016-06-25 15:31:05 -0400 |
---|---|---|
committer | midipix <writeonce@midipix.org> | 2016-06-25 15:31:05 -0400 |
commit | 9e67a4a4437bce28c494b957b7ee03a3683e60ed (patch) | |
tree | 57e8c81db38fc3ed79759b5dae6b31e5d238bdb5 /src | |
parent | c5881dbd61aa7a8eafed13af436e7a988c0ffd5d (diff) | |
download | sofort-9e67a4a4437bce28c494b957b7ee03a3683e60ed.tar.bz2 sofort-9e67a4a4437bce28c494b957b7ee03a3683e60ed.tar.xz |
utility: improved box integration, removed need for build-time macro.
Diffstat (limited to 'src')
-rw-r--r-- | src/driver/sfrt_amain.c | 91 | ||||
-rw-r--r-- | src/sofort.c | 94 |
2 files changed, 91 insertions, 94 deletions
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 <stdio.h> +#include <unistd.h> +#include <sofort/sofort.h> +#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 <stdio.h> -#include <unistd.h> #include <sofort/sofort.h> -#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 |