Blame src/driver/tbnf_amain.c

02e59c
/**************************************************************/
02e59c
/*  treebnf: a tree oriented bnf library                      */
02e59c
/*  Copyright (C) 2024  SysDeer Technologies, LLC             */
02e59c
/*  Released under GPLv2 and GPLv3; see COPYING.TREEBNF.      */
02e59c
/**************************************************************/
02e59c
02e59c
#include <stdio.h>
02e59c
#include <unistd.h>
02e59c
#include <treebnf/treebnf.h>
02e59c
#include "treebnf_driver_impl.h"
02e59c
#include "treebnf_dprintf_impl.h"
02e59c
02e59c
#ifndef TBNF_DRIVER_FLAGS
02e59c
#define TBNF_DRIVER_FLAGS	TBNF_DRIVER_VERBOSITY_ERRORS \
02e59c
				| TBNF_DRIVER_VERBOSITY_USAGE
02e59c
#endif
02e59c
02e59c
static const char vermsg[] = "%s%s%s (https://git.foss21.org/tulips/treebnf): "
02e59c
			     "version %s%d.%d.%d%s.\n"
02e59c
			     "[commit reference: %s%s%s]\n";
02e59c
02e59c
static const char * const tbnf_ver_color[6] = {
02e59c
		"\x1b[1m\x1b[35m","\x1b[0m",
02e59c
		"\x1b[1m\x1b[32m","\x1b[0m",
02e59c
		"\x1b[1m\x1b[34m","\x1b[0m"
02e59c
};
02e59c
02e59c
static const char * const tbnf_ver_plain[6] = {
02e59c
		"","",
02e59c
		"","",
02e59c
		"",""
02e59c
};
02e59c
02e59c
static ssize_t tbnf_version(struct tbnf_driver_ctx * dctx, int fdout)
02e59c
{
02e59c
	const struct tbnf_source_version * verinfo;
02e59c
	const char * const * verclr;
02e59c
02e59c
	verinfo = tbnf_api_source_version();
02e59c
	verclr  = isatty(fdout) ? tbnf_ver_color : tbnf_ver_plain;
02e59c
02e59c
	return tbnf_dprintf(
02e59c
			fdout,vermsg,
02e59c
			verclr[0],dctx->program,verclr[1],
02e59c
			verclr[2],verinfo->major,verinfo->minor,
02e59c
			verinfo->revision,verclr[3],
02e59c
			verclr[4],verinfo->commit,verclr[5]);
02e59c
}
02e59c
02e59c
static void tbnf_perform_unit_actions(
02e59c
	const struct tbnf_driver_ctx *	dctx,
02e59c
	struct tbnf_unit_ctx *		uctx)
02e59c
{
02e59c
	uint64_t flags = dctx->cctx->fmtflags;
02e59c
02e59c
	(void)dctx;
02e59c
	(void)uctx;
02e59c
	(void)flags;
02e59c
}
02e59c
02e59c
static int tbnf_exit(struct tbnf_driver_ctx * dctx, int ret)
02e59c
{
02e59c
	tbnf_output_error_vector(dctx);
02e59c
	tbnf_lib_free_driver_ctx(dctx);
02e59c
	return ret;
02e59c
}
02e59c
02e59c
int tbnf_main(char ** argv, char ** envp, const struct tbnf_fd_ctx * fdctx)
02e59c
{
02e59c
	int                         ret;
02e59c
	int                         fdout;
02e59c
	uint64_t                    flags;
02e59c
	struct tbnf_driver_ctx *    dctx;
02e59c
	struct tbnf_unit_ctx *      uctx;
02e59c
	const char **               unit;
02e59c
02e59c
	flags = TBNF_DRIVER_FLAGS;
02e59c
	fdout = fdctx ? fdctx->fdout : STDOUT_FILENO;
02e59c
02e59c
	if ((ret = tbnf_lib_get_driver_ctx(argv,envp,flags,fdctx,&dctx)))
02e59c
		return (ret == TBNF_USAGE)
02e59c
			? !argv || !argv[0] || !argv[1]
02e59c
			: TBNF_ERROR;
02e59c
02e59c
	if (dctx->cctx->drvflags & TBNF_DRIVER_VERSION)
02e59c
		if ((tbnf_version(dctx,fdout)) < 0)
02e59c
			return tbnf_exit(dctx,TBNF_ERROR);
02e59c
02e59c
	for (unit=dctx->units; *unit; unit++) {
02e59c
		if (!(tbnf_lib_get_unit_ctx(dctx,*unit,&uctx))) {
02e59c
			tbnf_perform_unit_actions(dctx,uctx);
02e59c
			tbnf_lib_free_unit_ctx(uctx);
02e59c
		}
02e59c
	}
02e59c
02e59c
	return tbnf_exit(dctx,dctx->errv[0] ? TBNF_ERROR : TBNF_OK);
02e59c
}