Blame src/driver/toks_amain.c

8b5736
/*********************************************************/
8b5736
/*  toksvc: a framework-native token broker service      */
8b5736
/*  Copyright (C) 2020  Z. Gilboa                        */
8b5736
/*  Released under GPLv2 and GPLv3; see COPYING.TOKSVC.  */
8b5736
/*********************************************************/
8b5736
8b5736
#include <ntapi/ntapi.h>
8b5736
#include <stdio.h>
8b5736
#include <unistd.h>
8b5736
8b5736
#include <toksvc/toksvc.h>
8b5736
#include "toksvc_init_impl.h"
8b5736
#include "toksvc_driver_impl.h"
8b5736
#include "toksvc_dprintf_impl.h"
8b5736
#include "toksvc_nolibc_impl.h"
8b5736
8b5736
#ifndef TOKS_DRIVER_FLAGS
8b5736
#define TOKS_DRIVER_FLAGS	TOKS_DRIVER_VERBOSITY_ERRORS \
8b5736
				| TOKS_DRIVER_VERBOSITY_USAGE
8b5736
#endif
8b5736
8b5736
static const char vermsg[] = "%s%s%s (git://midipix.org/toksvc): "
8b5736
			     "version %s%d.%d.%d%s.\n"
8b5736
			     "[commit reference: %s%s%s]\n";
8b5736
8b5736
static const char * const toks_ver_color[6] = {
8b5736
		"\x1b[1m\x1b[35m","\x1b[0m",
8b5736
		"\x1b[1m\x1b[32m","\x1b[0m",
8b5736
		"\x1b[1m\x1b[34m","\x1b[0m"
8b5736
};
8b5736
8b5736
static const char * const toks_ver_plain[6] = {
8b5736
		"","",
8b5736
		"","",
8b5736
		"",""
8b5736
};
8b5736
8b5736
static ssize_t toks_version(struct toks_driver_ctx * dctx)
8b5736
{
8b5736
	const struct toks_source_version * verinfo;
8b5736
	const char * const * verclr;
8b5736
8b5736
	verinfo = toks_source_version();
8b5736
	verclr  = isatty(STDOUT_FILENO) ? toks_ver_color : toks_ver_plain;
8b5736
8b5736
	return toks_dprintf(
8b5736
			STDOUT_FILENO,vermsg,
8b5736
			verclr[0],dctx->program,verclr[1],
8b5736
			verclr[2],verinfo->major,verinfo->minor,
8b5736
			verinfo->revision,verclr[3],
8b5736
			verclr[4],verinfo->commit,verclr[5]);
8b5736
}
8b5736
8b5736
static int toks_exit(struct toks_driver_ctx * dctx, int nerrors)
8b5736
{
8b5736
	toks_free_driver_ctx(dctx);
8b5736
	return nerrors ? 2 : 0;
8b5736
}
8b5736
8b5736
int toks_main(char ** argv, char ** envp)
8b5736
{
8b5736
	int				ret;
8b5736
	struct toks_driver_ctx *	dctx;
2dcff1
	struct toks_token_string	toks;
8b5736
8b5736
	if ((ret = toks_init()))
8b5736
		return ret;
8b5736
8b5736
	if ((ret = toks_get_driver_ctx(argv,envp,TOKS_DRIVER_FLAGS,&dctx)))
8b5736
		return (ret == TOKS_USAGE)
8b5736
			? !argv || !argv[0] || !argv[1]
8b5736
			: 2;
8b5736
8b5736
	if (dctx->cctx->drvflags & TOKS_DRIVER_VERSION)
8b5736
		if ((toks_version(dctx)) < 0)
8b5736
			return toks_exit(dctx,2);
8b5736
2dcff1
	if (dctx->cctx->drvflags & TOKS_DRIVER_ACTION_ACQUIRE) {
02451d
		ret = toks_client_acquire(dctx)
02451d
			? 2 : 0;
02451d
2dcff1
		if (ret == 0) {
2dcff1
			toks_client_token_to_str(dctx,&toks);
2dcff1
			toks_dprintf(STDOUT_FILENO,"%s\n",toks.token);
2dcff1
		}
2dcff1
	}
2dcff1
867b30
	if (dctx->cctx->drvflags & TOKS_DRIVER_ACTION_RELEASE) {
867b30
		ret = toks_client_release(dctx)
867b30
			? 2 : 0;
867b30
867b30
		if (ret == 0) {
867b30
			toks_client_token_to_str(dctx,&toks);
867b30
			toks_dprintf(STDOUT_FILENO,"token %s has been released\n",toks.token);
867b30
		}
867b30
867b30
		if (ret == 2) {
867b30
			toks_dprintf(STDERR_FILENO,"error: invalid token");
867b30
		}
867b30
	}
867b30
618937
	return (dctx->cctx->drvflags & TOKS_DRIVER_MODE_SERVER)
618937
		? NT_STATUS_SERVICE_NOTIFICATION
618937
		: toks_exit(dctx,ret);
8b5736
}