From 8b57360a637e529d617590101d79c8bbd0a133b2 Mon Sep 17 00:00:00 2001 From: midipix Date: Jun 08 2020 01:00:09 +0000 Subject: driver: added toks_main(). --- diff --git a/project/common.mk b/project/common.mk index f880226..daf40a3 100644 --- a/project/common.mk +++ b/project/common.mk @@ -5,6 +5,7 @@ INTERNAL_SRCS = \ src/daemon/toks_daemon_init.c \ src/daemon/toks_daemon_loop.c \ src/daemon/toks_daemon_signal.c \ + src/driver/toks_amain.c \ src/driver/toks_driver_ctx.c \ src/internal/nolibc/toksvc_compiler.c \ src/internal/toksvc_dprintf_impl.c \ diff --git a/src/driver/toks_amain.c b/src/driver/toks_amain.c new file mode 100644 index 0000000..5853c5a --- /dev/null +++ b/src/driver/toks_amain.c @@ -0,0 +1,78 @@ +/*********************************************************/ +/* toksvc: a framework-native token broker service */ +/* Copyright (C) 2020 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.TOKSVC. */ +/*********************************************************/ + +#include +#include +#include + +#include +#include "toksvc_init_impl.h" +#include "toksvc_driver_impl.h" +#include "toksvc_dprintf_impl.h" +#include "toksvc_nolibc_impl.h" + +#ifndef TOKS_DRIVER_FLAGS +#define TOKS_DRIVER_FLAGS TOKS_DRIVER_VERBOSITY_ERRORS \ + | TOKS_DRIVER_VERBOSITY_USAGE +#endif + +static const char vermsg[] = "%s%s%s (git://midipix.org/toksvc): " + "version %s%d.%d.%d%s.\n" + "[commit reference: %s%s%s]\n"; + +static const char * const toks_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 toks_ver_plain[6] = { + "","", + "","", + "","" +}; + +static ssize_t toks_version(struct toks_driver_ctx * dctx) +{ + const struct toks_source_version * verinfo; + const char * const * verclr; + + verinfo = toks_source_version(); + verclr = isatty(STDOUT_FILENO) ? toks_ver_color : toks_ver_plain; + + return toks_dprintf( + STDOUT_FILENO,vermsg, + verclr[0],dctx->program,verclr[1], + verclr[2],verinfo->major,verinfo->minor, + verinfo->revision,verclr[3], + verclr[4],verinfo->commit,verclr[5]); +} + +static int toks_exit(struct toks_driver_ctx * dctx, int nerrors) +{ + toks_free_driver_ctx(dctx); + return nerrors ? 2 : 0; +} + +int toks_main(char ** argv, char ** envp) +{ + int ret; + struct toks_driver_ctx * dctx; + + if ((ret = toks_init())) + return ret; + + if ((ret = toks_get_driver_ctx(argv,envp,TOKS_DRIVER_FLAGS,&dctx))) + return (ret == TOKS_USAGE) + ? !argv || !argv[0] || !argv[1] + : 2; + + if (dctx->cctx->drvflags & TOKS_DRIVER_VERSION) + if ((toks_version(dctx)) < 0) + return toks_exit(dctx,2); + + return toks_exit(dctx,ret); +}