Blame src/logic/slbt_exec_stoolie.c

4c3fb7
/*******************************************************************/
4c3fb7
/*  slibtool: a strong libtool implementation, written in C        */
4c3fb7
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
4c3fb7
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
4c3fb7
/*******************************************************************/
4c3fb7
4c3fb7
#include <slibtool/slibtool.h>
4c3fb7
#include <slibtool/slibtool_output.h>
4c3fb7
#include "slibtool_driver_impl.h"
4c3fb7
#include "slibtool_stoolie_impl.h"
4c3fb7
#include "slibtool_errinfo_impl.h"
4c3fb7
#include "argv/argv.h"
4c3fb7
4c3fb7
static int slbt_stoolie_usage(
4c3fb7
	int				fdout,
4c3fb7
	const char *			program,
4c3fb7
	const char *			arg,
4c3fb7
	const struct argv_option **	optv,
4c3fb7
	struct argv_meta *		meta,
4c3fb7
	struct slbt_exec_ctx *		ectx,
4c3fb7
	int				noclr)
4c3fb7
{
4c3fb7
	char    header[512];
4c3fb7
	bool    stooliemode;
4c3fb7
4c3fb7
	stooliemode = !strcmp(program,"slibtoolize");
4c3fb7
4c3fb7
	snprintf(header,sizeof(header),
4c3fb7
		"Usage: %s%s [options] ...\n"
4c3fb7
		"Options:\n",
4c3fb7
		program,
4c3fb7
		stooliemode ? "" : " --mode=slibtoolize");
4c3fb7
4c3fb7
	switch (noclr) {
4c3fb7
		case 0:
4c3fb7
			slbt_argv_usage(fdout,header,optv,arg);
4c3fb7
			break;
4c3fb7
4c3fb7
		default:
4c3fb7
			slbt_argv_usage_plain(fdout,header,optv,arg);
4c3fb7
			break;
4c3fb7
	}
4c3fb7
4c3fb7
	if (ectx)
4c3fb7
		slbt_ectx_free_exec_ctx(ectx);
4c3fb7
4c3fb7
	slbt_argv_free(meta);
4c3fb7
4c3fb7
	return SLBT_USAGE;
4c3fb7
}
4c3fb7
4c3fb7
static int slbt_exec_stoolie_fail(
4c3fb7
	struct slbt_exec_ctx *	ectx,
4c3fb7
	struct argv_meta *	meta,
4c3fb7
	int			ret)
4c3fb7
{
4c3fb7
	slbt_argv_free(meta);
4c3fb7
	slbt_ectx_free_exec_ctx(ectx);
4c3fb7
	return ret;
4c3fb7
}
4c3fb7
4c3fb7
static int slbt_exec_stoolie_perform_actions(
4c3fb7
	const struct slbt_driver_ctx *  dctx)
4c3fb7
{
4c3fb7
	(void)dctx;
4c3fb7
	return 0;
4c3fb7
}
4c3fb7
4c3fb7
int slbt_exec_stoolie(const struct slbt_driver_ctx * dctx)
4c3fb7
{
4c3fb7
	int				ret;
4c3fb7
	int				fdout;
4c3fb7
	int				fderr;
4c3fb7
	char **				argv;
4c3fb7
	char **				iargv;
4c3fb7
	struct slbt_exec_ctx *		ectx;
4c3fb7
	struct slbt_driver_ctx_impl *	ictx;
4c3fb7
	const struct slbt_common_ctx *	cctx;
4c3fb7
	struct argv_meta *		meta;
4c3fb7
	struct argv_entry *		entry;
4c3fb7
	size_t				nunits;
4c3fb7
	const struct argv_option *	optv[SLBT_OPTV_ELEMENTS];
4c3fb7
4c3fb7
	/* context */
4c3fb7
	if (slbt_ectx_get_exec_ctx(dctx,&ectx) < 0)
4c3fb7
		return SLBT_NESTED_ERROR(dctx);
4c3fb7
4c3fb7
	/* initial state, slibtoolize (stoolie) mode skin */
4c3fb7
	slbt_ectx_reset_arguments(ectx);
4c3fb7
	slbt_disable_placeholders(ectx);
4c3fb7
4c3fb7
	ictx  = slbt_get_driver_ictx(dctx);
4c3fb7
	cctx  = dctx->cctx;
4c3fb7
	iargv = ectx->cargv;
4c3fb7
4c3fb7
	fdout = slbt_driver_fdout(dctx);
4c3fb7
	fderr = slbt_driver_fderr(dctx);
4c3fb7
4c3fb7
	(void)fderr;
4c3fb7
4c3fb7
	/* missing arguments? */
4c3fb7
	slbt_optv_init(slbt_stoolie_options,optv);
4c3fb7
4c3fb7
	if (!iargv[1] && (dctx->cctx->drvflags & SLBT_DRIVER_VERBOSITY_USAGE))
4c3fb7
		return slbt_stoolie_usage(
4c3fb7
			fdout,
4c3fb7
			dctx->program,
4c3fb7
			0,optv,0,ectx,
4c3fb7
			dctx->cctx->drvflags & SLBT_DRIVER_ANNOTATE_NEVER);
4c3fb7
4c3fb7
	/* <stoolie> argv meta */
4c3fb7
	if (!(meta = slbt_argv_get(
4c3fb7
			iargv,optv,
4c3fb7
			dctx->cctx->drvflags & SLBT_DRIVER_VERBOSITY_ERRORS
4c3fb7
				? ARGV_VERBOSITY_ERRORS
4c3fb7
				: ARGV_VERBOSITY_NONE,
4c3fb7
			fdout)))
4c3fb7
		return slbt_exec_stoolie_fail(
4c3fb7
			ectx,meta,
4c3fb7
			SLBT_CUSTOM_ERROR(dctx,SLBT_ERR_AR_FAIL));
4c3fb7
4c3fb7
	/* dest, alternate argument vector options */
4c3fb7
	argv    = ectx->altv;
4c3fb7
	*argv++ = iargv[0];
4c3fb7
	nunits  = 0;
4c3fb7
4c3fb7
	for (entry=meta->entries; entry->fopt || entry->arg; entry++) {
4c3fb7
		if (entry->fopt) {
4c3fb7
			switch (entry->tag) {
4c3fb7
				case TAG_STLE_HELP:
4c3fb7
					slbt_stoolie_usage(
4c3fb7
						fdout,
4c3fb7
						dctx->program,
4c3fb7
						0,optv,0,ectx,
4c3fb7
						dctx->cctx->drvflags
4c3fb7
							& SLBT_DRIVER_ANNOTATE_NEVER);
4c3fb7
4c3fb7
					ictx->cctx.drvflags |= SLBT_DRIVER_VERSION;
4c3fb7
					ictx->cctx.drvflags ^= SLBT_DRIVER_VERSION;
4c3fb7
4c3fb7
					slbt_argv_free(meta);
4c3fb7
4c3fb7
					return SLBT_OK;
4c3fb7
4c3fb7
				case TAG_STLE_VERSION:
4c3fb7
					ictx->cctx.drvflags |= SLBT_DRIVER_VERSION;
4c3fb7
					break;
4c3fb7
			}
4c3fb7
4c3fb7
			if (entry->fval) {
4c3fb7
				*argv++ = (char *)entry->arg;
4c3fb7
			}
4c3fb7
		} else {
4c3fb7
			nunits++;
4c3fb7
		};
4c3fb7
	}
4c3fb7
4c3fb7
	/* defer --version printing to slbt_main() as needed */
4c3fb7
	if (cctx->drvflags & SLBT_DRIVER_VERSION) {
4c3fb7
		slbt_argv_free(meta);
4c3fb7
		slbt_ectx_free_exec_ctx(ectx);
4c3fb7
		return SLBT_OK;
4c3fb7
	}
4c3fb7
4c3fb7
	/* slibtoolize operations */
4c3fb7
	ret = slbt_exec_stoolie_perform_actions(dctx);
4c3fb7
4c3fb7
	/* all done */
4c3fb7
	slbt_argv_free(meta);
4c3fb7
	slbt_ectx_free_exec_ctx(ectx);
4c3fb7
4c3fb7
	return ret;
4c3fb7
}
4c3fb7
4c3fb7
int slbt_exec_slibtoolize(const struct slbt_driver_ctx * dctx)
4c3fb7
{
4c3fb7
	return slbt_exec_stoolie(dctx);
4c3fb7
}