firasuke / cross / slibtool

Forked from cross/slibtool a month ago
Clone

Blame src/logic/slbt_exec_ar.c

11f3c7
/*******************************************************************/
11f3c7
/*  slibtool: a skinny libtool implementation, written in C        */
11f3c7
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
11f3c7
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
11f3c7
/*******************************************************************/
11f3c7
11f3c7
#include <slibtool/slibtool.h>
8afddf
#include <slibtool/slibtool_output.h>
11f3c7
#include "slibtool_driver_impl.h"
11f3c7
#include "slibtool_ar_impl.h"
11f3c7
#include "slibtool_errinfo_impl.h"
11f3c7
#include "argv/argv.h"
11f3c7
af36ce
#define SLBT_DRIVER_MODE_AR_ACTIONS     (SLBT_DRIVER_MODE_AR_CHECK)
af36ce
8afddf
#define SLBT_DRIVER_MODE_AR_OUTPUTS     (SLBT_OUTPUT_ARCHIVE_MEMBERS  \
8afddf
	                                 | SLBT_OUTPUT_ARCHIVE_HEADERS \
8afddf
	                                 | SLBT_OUTPUT_ARCHIVE_SYMBOLS  \
8afddf
	                                 | SLBT_OUTPUT_ARCHIVE_ARMAPS)
8afddf
8afddf
#define SLBT_PRETTY_FLAGS               (SLBT_PRETTY_YAML      \
8afddf
	                                 | SLBT_PRETTY_POSIX    \
8afddf
	                                 | SLBT_PRETTY_HEXDATA)
8afddf
11f3c7
static int slbt_ar_usage(
11f3c7
	int				fdout,
11f3c7
	const char *			program,
11f3c7
	const char *			arg,
11f3c7
	const struct argv_option **	optv,
11f3c7
	struct argv_meta *		meta,
11f3c7
	struct slbt_exec_ctx *		ectx,
11f3c7
	int				noclr)
11f3c7
{
11f3c7
	char		header[512];
11f3c7
	bool		armode;
11f3c7
	const char *	dash;
11f3c7
11f3c7
	armode = (dash = strrchr(program,'-'))
11f3c7
		&& !strcmp(++dash,"ar");
11f3c7
11f3c7
	snprintf(header,sizeof(header),
11f3c7
		"Usage: %s%s [options] [ARCHIVE-FILE] [ARCHIVE_FILE] ...\n"
11f3c7
		"Options:\n",
11f3c7
		program,
11f3c7
		armode ? "" : " --mode=ar");
11f3c7
11f3c7
	switch (noclr) {
11f3c7
		case 0:
ef2baa
			slbt_argv_usage(fdout,header,optv,arg);
11f3c7
			break;
11f3c7
11f3c7
		default:
ef2baa
			slbt_argv_usage_plain(fdout,header,optv,arg);
11f3c7
			break;
11f3c7
	}
11f3c7
11f3c7
	if (ectx)
11f3c7
		slbt_free_exec_ctx(ectx);
11f3c7
ef2baa
	slbt_argv_free(meta);
11f3c7
11f3c7
	return SLBT_USAGE;
11f3c7
}
11f3c7
11f3c7
static int slbt_exec_ar_fail(
11f3c7
	struct slbt_exec_ctx *	actx,
11f3c7
	struct argv_meta *	meta,
11f3c7
	int			ret)
11f3c7
{
ef2baa
	slbt_argv_free(meta);
11f3c7
	slbt_free_exec_ctx(actx);
11f3c7
	return ret;
11f3c7
}
11f3c7
8afddf
static int slbt_exec_ar_perform_archive_actions(
8afddf
	const struct slbt_driver_ctx *  dctx,
8afddf
	struct slbt_archive_ctx **      arctxv)
8afddf
{
8afddf
	struct slbt_archive_ctx **      arctxp;
8afddf
8afddf
	for (arctxp=arctxv; *arctxp; arctxp++) {
8afddf
		if (dctx->cctx->fmtflags & SLBT_DRIVER_MODE_AR_OUTPUTS)
8afddf
			if (slbt_ar_output_arname(*arctxp) < 0)
8afddf
				return SLBT_NESTED_ERROR(dctx);
8afddf
8afddf
		if (dctx->cctx->fmtflags & SLBT_OUTPUT_ARCHIVE_MEMBERS)
8afddf
			if (slbt_ar_output_members((*arctxp)->meta) < 0)
8afddf
				return SLBT_NESTED_ERROR(dctx);
8afddf
	}
8afddf
8afddf
	return 0;
8afddf
}
8afddf
11f3c7
int slbt_exec_ar(
11f3c7
	const struct slbt_driver_ctx *	dctx,
11f3c7
	struct slbt_exec_ctx *		ectx)
11f3c7
{
11f3c7
	int				ret;
11f3c7
	int				fdout;
af36ce
	int				fderr;
11f3c7
	char **				argv;
11f3c7
	char **				iargv;
af36ce
	struct slbt_driver_ctx_impl *	ictx;
af36ce
	const struct slbt_common_ctx *	cctx;
11f3c7
	struct slbt_archive_ctx **	arctxv;
7bab5a
	struct slbt_archive_ctx **	arctxp;
11f3c7
	const char **			unitv;
11f3c7
	const char **			unitp;
11f3c7
	size_t				nunits;
11f3c7
	struct slbt_exec_ctx *		actx;
11f3c7
	struct argv_meta *		meta;
11f3c7
	struct argv_entry *		entry;
11f3c7
	const struct argv_option *	optv[SLBT_OPTV_ELEMENTS];
11f3c7
11f3c7
	/* context */
11f3c7
	if (ectx)
11f3c7
		actx = 0;
11f3c7
	else if ((ret = slbt_get_exec_ctx(dctx,&ectx)))
11f3c7
		return ret;
11f3c7
	else
11f3c7
		actx = ectx;
11f3c7
11f3c7
	/* initial state, ar mode skin */
11f3c7
	slbt_reset_arguments(ectx);
11f3c7
	slbt_disable_placeholders(ectx);
af36ce
af36ce
	ictx  = slbt_get_driver_ictx(dctx);
af36ce
	cctx  = dctx->cctx;
11f3c7
	iargv = ectx->cargv;
af36ce
11f3c7
	fdout = slbt_driver_fdout(dctx);
af36ce
	fderr = slbt_driver_fderr(dctx);
11f3c7
11f3c7
	/* missing arguments? */
ef2baa
	slbt_optv_init(slbt_ar_options,optv);
11f3c7
11f3c7
	if (!iargv[1] && (dctx->cctx->drvflags & SLBT_DRIVER_VERBOSITY_USAGE))
11f3c7
		return slbt_ar_usage(
11f3c7
			fdout,
11f3c7
			dctx->program,
11f3c7
			0,optv,0,actx,
11f3c7
			dctx->cctx->drvflags & SLBT_DRIVER_ANNOTATE_NEVER);
11f3c7
11f3c7
	/* <ar> argv meta */
ef2baa
	if (!(meta = slbt_argv_get(
11f3c7
			iargv,optv,
11f3c7
			dctx->cctx->drvflags & SLBT_DRIVER_VERBOSITY_ERRORS
11f3c7
				? ARGV_VERBOSITY_ERRORS
11f3c7
				: ARGV_VERBOSITY_NONE,
11f3c7
			fdout)))
11f3c7
		return slbt_exec_ar_fail(
11f3c7
			actx,meta,
11f3c7
			SLBT_CUSTOM_ERROR(dctx,SLBT_ERR_AR_FAIL));
11f3c7
11f3c7
	/* dest, alternate argument vector options */
11f3c7
	argv    = ectx->altv;
11f3c7
	*argv++ = iargv[0];
11f3c7
	nunits  = 0;
11f3c7
11f3c7
	for (entry=meta->entries; entry->fopt || entry->arg; entry++) {
11f3c7
		if (entry->fopt) {
11f3c7
			switch (entry->tag) {
11f3c7
				case TAG_AR_HELP:
11f3c7
					slbt_ar_usage(
11f3c7
						fdout,
11f3c7
						dctx->program,
11f3c7
						0,optv,0,ectx,
11f3c7
						dctx->cctx->drvflags
11f3c7
							& SLBT_DRIVER_ANNOTATE_NEVER);
1eb928
a470d5
					ictx->cctx.drvflags |= SLBT_DRIVER_VERSION;
a470d5
					ictx->cctx.drvflags ^= SLBT_DRIVER_VERSION;
a470d5
ef2baa
					slbt_argv_free(meta);
1eb928
1eb928
					return SLBT_OK;
af36ce
b87f70
				case TAG_AR_VERSION:
b87f70
					ictx->cctx.drvflags |= SLBT_DRIVER_VERSION;
b87f70
					break;
b87f70
af36ce
				case TAG_AR_CHECK:
af36ce
					ictx->cctx.drvflags |= SLBT_DRIVER_MODE_AR_CHECK;
af36ce
					break;
8afddf
8afddf
				case TAG_AR_PRINT:
8afddf
					if (!entry->arg)
8afddf
						ictx->cctx.fmtflags |= SLBT_OUTPUT_ARCHIVE_MEMBERS;
8afddf
8afddf
					else if (!strcmp(entry->arg,"members"))
8afddf
						ictx->cctx.fmtflags |= SLBT_OUTPUT_ARCHIVE_MEMBERS;
8afddf
8afddf
					else if (!strcmp(entry->arg,"headers"))
8afddf
						ictx->cctx.fmtflags |= SLBT_OUTPUT_ARCHIVE_HEADERS;
8afddf
8afddf
					else if (!strcmp(entry->arg,"symbols"))
8afddf
						ictx->cctx.fmtflags |= SLBT_OUTPUT_ARCHIVE_SYMBOLS;
8afddf
8afddf
					else if (!strcmp(entry->arg,"armaps"))
8afddf
						ictx->cctx.fmtflags |= SLBT_OUTPUT_ARCHIVE_ARMAPS;
8afddf
8afddf
					break;
8afddf
8afddf
				case TAG_AR_PRETTY:
8afddf
					if (!strcmp(entry->arg,"yaml")) {
8afddf
						ictx->cctx.fmtflags &= ~(uint64_t)SLBT_PRETTY_FLAGS;
8afddf
						ictx->cctx.fmtflags |= SLBT_PRETTY_YAML;
8afddf
8afddf
					} else if (!strcmp(entry->arg,"posix")) {
8afddf
						ictx->cctx.fmtflags &= ~(uint64_t)SLBT_PRETTY_FLAGS;
8afddf
						ictx->cctx.fmtflags |= SLBT_PRETTY_POSIX;
8afddf
					}
8afddf
8afddf
					break;
191700
886865
				case TAG_AR_POSIX:
886865
					ictx->cctx.fmtflags &= ~(uint64_t)SLBT_PRETTY_FLAGS;
886865
					ictx->cctx.fmtflags |= SLBT_PRETTY_POSIX;
886865
					break;
886865
886865
				case TAG_AR_YAML:
886865
					ictx->cctx.fmtflags &= ~(uint64_t)SLBT_PRETTY_FLAGS;
886865
					ictx->cctx.fmtflags |= SLBT_PRETTY_YAML;
886865
					break;
886865
191700
				case TAG_AR_VERBOSE:
191700
					ictx->cctx.fmtflags |= SLBT_PRETTY_VERBOSE;
191700
					break;
11f3c7
			}
11f3c7
11f3c7
			if (entry->fval) {
11f3c7
				*argv++ = (char *)entry->arg;
11f3c7
			}
11f3c7
		} else {
11f3c7
			nunits++;
11f3c7
		};
11f3c7
	}
11f3c7
b87f70
	/* defer --version printing to slbt_main() as needed */
b87f70
	if (cctx->drvflags & SLBT_DRIVER_VERSION) {
ef2baa
		slbt_argv_free(meta);
b87f70
		slbt_free_exec_ctx(actx);
b87f70
		return SLBT_OK;
b87f70
	}
b87f70
af36ce
	/* at least one action must be specified */
8afddf
	if (cctx->fmtflags & SLBT_DRIVER_MODE_AR_OUTPUTS) {
8afddf
		(void)0;
8afddf
8afddf
	} else if (!(cctx->drvflags & SLBT_DRIVER_MODE_AR_ACTIONS)) {
af36ce
		if (cctx->drvflags & SLBT_DRIVER_VERBOSITY_ERRORS)
af36ce
			slbt_dprintf(fderr,
af36ce
				"%s: at least one action must be specified\n",
af36ce
				dctx->program);
af36ce
af36ce
		return slbt_exec_ar_fail(
af36ce
			actx,meta,
af36ce
			SLBT_CUSTOM_ERROR(
af36ce
				dctx,
af36ce
				SLBT_ERR_AR_NO_ACTION_SPECIFIED));
af36ce
	}
af36ce
af36ce
	/* at least one unit must be specified */
af36ce
	if (!nunits) {
af36ce
		if (cctx->drvflags & SLBT_DRIVER_VERBOSITY_ERRORS)
af36ce
			slbt_dprintf(fderr,
af36ce
				"%s: all actions require at least one input unit\n",
af36ce
				dctx->program);
af36ce
af36ce
		return slbt_exec_ar_fail(
af36ce
			actx,meta,
af36ce
			SLBT_CUSTOM_ERROR(
af36ce
				dctx,
af36ce
				SLBT_ERR_AR_NO_INPUT_SPECIFIED));
af36ce
	}
af36ce
11f3c7
	/* archive vector allocation */
11f3c7
	if (!(arctxv = calloc(nunits+1,sizeof(struct slbt_archive_ctx *))))
11f3c7
		return slbt_exec_ar_fail(
11f3c7
			actx,meta,
11f3c7
			SLBT_SYSTEM_ERROR(dctx,0));
11f3c7
11f3c7
	/* unit vector allocation */
11f3c7
	if (!(unitv = calloc(nunits+1,sizeof(const char *)))) {
11f3c7
		free (arctxv);
11f3c7
11f3c7
		return slbt_exec_ar_fail(
11f3c7
			actx,meta,
11f3c7
			SLBT_SYSTEM_ERROR(dctx,0));
11f3c7
	}
11f3c7
11f3c7
	/* unit vector initialization */
11f3c7
	for (entry=meta->entries,unitp=unitv; entry->fopt || entry->arg; entry++)
11f3c7
		if (!entry->fopt)
11f3c7
			*unitp++ = entry->arg;
11f3c7
7bab5a
	/* archive context vector initialization */
7bab5a
	for (unitp=unitv,arctxp=arctxv; *unitp; unitp++,arctxp++) {
7bab5a
		if (slbt_get_archive_ctx(dctx,*unitp,arctxp) < 0) {
7bab5a
			for (arctxp=arctxv; *arctxp; arctxp++)
7bab5a
				slbt_free_archive_ctx(*arctxp);
7bab5a
7bab5a
			free(unitv);
7bab5a
			free(arctxv);
7bab5a
7bab5a
			return slbt_exec_ar_fail(
7bab5a
				actx,meta,
7bab5a
				SLBT_NESTED_ERROR(dctx));
7bab5a
		}
7bab5a
	}
7bab5a
8afddf
	/* archive operations */
8afddf
	ret = slbt_exec_ar_perform_archive_actions(dctx,arctxv);
8afddf
11f3c7
	/* all done */
7bab5a
	for (arctxp=arctxv; *arctxp; arctxp++)
7bab5a
		slbt_free_archive_ctx(*arctxp);
7bab5a
11f3c7
	free(unitv);
11f3c7
	free(arctxv);
11f3c7
ef2baa
	slbt_argv_free(meta);
11f3c7
	slbt_free_exec_ctx(actx);
11f3c7
8afddf
	return ret;
11f3c7
}