From 8056668eebf28eec3101cf567b63a0600e40f36d Mon Sep 17 00:00:00 2001
From: midipix <writeonce@midipix.org>
Date: Mon, 1 Apr 2024 04:14:04 +0000
Subject: driver: added -print-aux-dir and -print-m4-dir (for slibtool.m4
 integration).

---
 include/slibtool/slibtool.h         |  3 +++
 src/driver/slbt_amain.c             | 22 ++++++++++++++++++++++
 src/driver/slbt_driver_ctx.c        |  8 ++++++++
 src/driver/slbt_split_argv.c        | 14 ++++++++++----
 src/internal/slibtool_driver_impl.h |  2 ++
 src/skin/slbt_skin_default.c        | 13 +++++++++++++
 6 files changed, 58 insertions(+), 4 deletions(-)

diff --git a/include/slibtool/slibtool.h b/include/slibtool/slibtool.h
index 3be95e5..f05de74 100644
--- a/include/slibtool/slibtool.h
+++ b/include/slibtool/slibtool.h
@@ -71,8 +71,11 @@ extern "C" {
 #define SLBT_DRIVER_IMPLIB_DSOMETA	SLBT_DRIVER_XFLAG(0x0002)
 #define SLBT_DRIVER_EXPORT_DYNAMIC	SLBT_DRIVER_XFLAG(0x0010)
 #define SLBT_DRIVER_STATIC_LIBTOOL_LIBS	SLBT_DRIVER_XFLAG(0x0100)
+
 #define SLBT_DRIVER_OUTPUT_MACHINE	SLBT_DRIVER_XFLAG(0x1000)
 #define SLBT_DRIVER_OUTPUT_CONFIG	SLBT_DRIVER_XFLAG(0x2000)
+#define SLBT_DRIVER_OUTPUT_AUX_DIR	SLBT_DRIVER_XFLAG(0x4000)
+#define SLBT_DRIVER_OUTPUT_M4_DIR	SLBT_DRIVER_XFLAG(0x8000)
 
 #define SLBT_DRIVER_MODE_AR		SLBT_DRIVER_XFLAG(0x010000)
 #define SLBT_DRIVER_MODE_AR_CHECK	SLBT_DRIVER_XFLAG(0x020000)
diff --git a/src/driver/slbt_amain.c b/src/driver/slbt_amain.c
index 73d1462..e0f4241 100644
--- a/src/driver/slbt_amain.c
+++ b/src/driver/slbt_amain.c
@@ -57,6 +57,16 @@ static ssize_t slbt_version(struct slbt_driver_ctx * dctx, int fdout)
 			verclr[5],gitver ? "]" : "");
 }
 
+static ssize_t slbt_print_aux_dir(int fdout)
+{
+	return slbt_dprintf(fdout,"%s\n",SLBT_PACKAGE_DATADIR);
+}
+
+static ssize_t slbt_print_m4_dir(int fdout)
+{
+	return slbt_dprintf(fdout,"%s\n",SLBT_PACKAGE_DATADIR);
+}
+
 static void slbt_perform_driver_actions(struct slbt_driver_ctx * dctx)
 {
 	if (dctx->cctx->drvflags & SLBT_DRIVER_INFO)
@@ -197,6 +207,18 @@ int slbt_main(char ** argv, char ** envp, const struct slbt_fd_ctx * fdctx)
 					? slbt_exit(dctx,SLBT_ERROR)
 					: slbt_exit(dctx,SLBT_OK);
 
+	/* -print-aux-dir must be the first (and only) action */
+	if (dctx->cctx->drvflags & SLBT_DRIVER_OUTPUT_AUX_DIR)
+		return (slbt_print_aux_dir(fdout) < 0)
+			? slbt_exit(dctx,SLBT_ERROR)
+			: slbt_exit(dctx,SLBT_OK);
+
+	/* -print-m4-dir must be the first (and only) action */
+	if (dctx->cctx->drvflags & SLBT_DRIVER_OUTPUT_M4_DIR)
+		return (slbt_print_m4_dir(fdout) < 0)
+			? slbt_exit(dctx,SLBT_ERROR)
+			: slbt_exit(dctx,SLBT_OK);
+
 	/* perform all other actions */
 	slbt_perform_driver_actions(dctx);
 
diff --git a/src/driver/slbt_driver_ctx.c b/src/driver/slbt_driver_ctx.c
index e24942e..76c7c4a 100644
--- a/src/driver/slbt_driver_ctx.c
+++ b/src/driver/slbt_driver_ctx.c
@@ -609,6 +609,14 @@ int slbt_lib_get_driver_ctx(
 					cctx.drvflags |= SLBT_DRIVER_OUTPUT_MACHINE;
 					break;
 
+				case TAG_PRINT_AUX_DIR:
+					cctx.drvflags |= SLBT_DRIVER_OUTPUT_AUX_DIR;
+					break;
+
+				case TAG_PRINT_M4_DIR:
+					cctx.drvflags |= SLBT_DRIVER_OUTPUT_M4_DIR;
+					break;
+
 				case TAG_DEBUG:
 					cctx.drvflags |= SLBT_DRIVER_DEBUG;
 					break;
diff --git a/src/driver/slbt_split_argv.c b/src/driver/slbt_split_argv.c
index 7ad20a3..8fd131a 100644
--- a/src/driver/slbt_split_argv.c
+++ b/src/driver/slbt_split_argv.c
@@ -59,6 +59,7 @@ slbt_hidden int slbt_split_argv(
 	struct argv_entry *		features;
 	struct argv_entry *		ccwrap;
 	struct argv_entry *		dumpmachine;
+	struct argv_entry *		printdir;
 	struct argv_entry *		aropt;
 	struct argv_entry *		stoolieopt;
 	const struct argv_option **	popt;
@@ -124,7 +125,8 @@ slbt_hidden int slbt_split_argv(
 	}
 
 	/* missing all of --mode, --help, --version, --info, --config, --dumpmachine, --features, and --finish? */
-	mode = help = version = info = config = finish = features = ccwrap = dumpmachine = aropt = stoolieopt = 0;
+	/* as well as -print-aux-dir and -print-m4-dir? */
+	mode = help = version = info = config = finish = features = ccwrap = dumpmachine = printdir = aropt = stoolieopt = 0;
 
 	for (entry=meta->entries; entry->fopt; entry++)
 		if (entry->tag == TAG_MODE)
@@ -145,6 +147,10 @@ slbt_hidden int slbt_split_argv(
 			ccwrap = entry;
 		else if (entry->tag == TAG_DUMPMACHINE)
 			dumpmachine = entry;
+		else if (entry->tag == TAG_PRINT_AUX_DIR)
+			printdir = entry;
+		else if (entry->tag == TAG_PRINT_M4_DIR)
+			printdir = entry;
 
 	/* alternate execusion mode? */
 	if (!altmode && mode && !strcmp(mode->arg,"ar"))
@@ -169,7 +175,7 @@ slbt_hidden int slbt_split_argv(
 		return -1;
 	}
 
-	if (!mode && !help && !version && !info && !config && !finish && !features && !dumpmachine && !altmode) {
+	if (!mode && !help && !version && !info && !config && !finish && !features && !dumpmachine && !printdir && !altmode) {
 		slbt_dprintf(fderr,
 			"%s: error: --mode must be specified.\n",
 			program);
@@ -177,7 +183,7 @@ slbt_hidden int slbt_split_argv(
 	}
 
 	/* missing compiler? */
-	if (!ctx.unitidx && !help && !info && !config && !version && !finish && !features && !dumpmachine) {
+	if (!ctx.unitidx && !help && !info && !config && !version && !finish && !features && !dumpmachine && !printdir) {
 		if (!altmode && !aropt && !stoolieopt) {
 			if (flags & SLBT_DRIVER_VERBOSITY_ERRORS)
 				slbt_dprintf(fderr,
@@ -339,7 +345,7 @@ slbt_hidden int slbt_split_argv(
 	if (ctx.unitidx) {
 		(void)0;
 
-	} else if (help || version || features || info || config || dumpmachine || altmode) {
+	} else if (help || version || features || info || config || dumpmachine || printdir || altmode) {
 		for (i=0; i<argc; i++)
 			sargv->targv[i] = argv[i];
 
diff --git a/src/internal/slibtool_driver_impl.h b/src/internal/slibtool_driver_impl.h
index 7cbea42..429cdc4 100644
--- a/src/internal/slibtool_driver_impl.h
+++ b/src/internal/slibtool_driver_impl.h
@@ -30,6 +30,8 @@ enum app_tags {
 	TAG_CONFIG,
 	TAG_MKVARS,
 	TAG_DUMPMACHINE,
+	TAG_PRINT_AUX_DIR,
+	TAG_PRINT_M4_DIR,
 	TAG_DEBUG,
 	TAG_DRY_RUN,
 	TAG_FEATURES,
diff --git a/src/skin/slbt_skin_default.c b/src/skin/slbt_skin_default.c
index 476e36e..07f099d 100644
--- a/src/skin/slbt_skin_default.c
+++ b/src/skin/slbt_skin_default.c
@@ -33,6 +33,19 @@ const slbt_hidden struct argv_option slbt_default_options[] = {
 				"however its addition is expected before the "
 				"next major release."},
 
+	{"print-aux-dir",	0,TAG_PRINT_AUX_DIR,ARGV_OPTARG_NONE,
+				ARGV_OPTION_HYBRID_ONLY,0,0,
+				"print the directory of the package-installed, "
+				"backward-compatible ltmain.sh and slibtool.sh; "
+				"for additional information, see the slibtoolize(1) "
+				"manual page."},
+
+	{"print-m4-dir",	0,TAG_PRINT_M4_DIR,ARGV_OPTARG_NONE,
+				ARGV_OPTION_HYBRID_ONLY,0,0,
+				"print the directory of the package-installed slibtool.m4; "
+				"for additional information, see the slibtoolize(1) "
+				"manual page."},
+
 	{"finish",		0,TAG_FINISH,ARGV_OPTARG_NONE,0,0,0,
 				"same as --mode=finish"},
 
-- 
cgit v1.2.3