summary refs log tree commit diff
path: root/bin
diff options
context:
space:
mode:
Diffstat (limited to 'bin')
-rw-r--r--bin/.gitignore1
-rw-r--r--bin/Makefile1
-rw-r--r--bin/man1/setopt.148
-rw-r--r--bin/setopt.c55
4 files changed, 105 insertions, 0 deletions
diff --git a/bin/.gitignore b/bin/.gitignore
index 7a4e360f..4e25a4fd 100644
--- a/bin/.gitignore
+++ b/bin/.gitignore
@@ -23,6 +23,7 @@ psfed
 scheme
 scheme.h
 scheme.png
+setopt
 tags
 ttpre
 up
diff --git a/bin/Makefile b/bin/Makefile
index f0128b16..16a5ba9e 100644
--- a/bin/Makefile
+++ b/bin/Makefile
@@ -18,6 +18,7 @@ BINS += pbd
 BINS += pngo
 BINS += psf2png
 BINS += scheme
+BINS += setopt
 BINS += ttpre
 BINS += up
 BINS += wake
diff --git a/bin/man1/setopt.1 b/bin/man1/setopt.1
new file mode 100644
index 00000000..a148b28f
--- /dev/null
+++ b/bin/man1/setopt.1
@@ -0,0 +1,48 @@
+.Dd February 24, 2019
+.Dt SETOPT 1
+.Os
+.
+.Sh NAME
+.Nm setopt
+.Nd parse command options
+.
+.Sh SYNOPSIS
+.Nm
+.Ar optstring
+.Op Ar args ...
+.
+.Sh DESCRIPTION
+.Nm
+behaves similarly to
+.Xr getopt 1 ,
+except that it produces quoted output
+appropriate for use with the
+.Nm eval
+shell builtin.
+This allows arguments containing whitespace
+to survive parsing.
+.
+.Sh EXAMPLES
+.Bd -literal -offset indent
+args=$(setopt abo: "$@")
+eval set -- "$args"
+.Ed
+.
+.Sh SEE ALSO
+.Xr getopt 1 ,
+.Xr sh 1 ,
+.Xr getopt 3
+.
+.Sh HISTORY
+The
+.Xr getopt 1
+manual page of
+.Fx
+reads,
+.Do
+Arguments containing white space
+or embedded shell metacharacters
+generally will not survive intact;
+this looks easy to fix but isn't.
+.Dc
+Yet here we are.
diff --git a/bin/setopt.c b/bin/setopt.c
new file mode 100644
index 00000000..75197f51
--- /dev/null
+++ b/bin/setopt.c
@@ -0,0 +1,55 @@
+/* Copyright (C) 2019  C. McEnroe <june@causal.agency>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sysexits.h>
+#include <unistd.h>
+
+static void printArg(const char *str) {
+	printf(" ");
+	if (!str[0]) {
+		printf("''");
+		return;
+	}
+	while (str[0]) {
+		size_t sq = strcspn(str, "'");
+		if (sq) {
+			printf("'%.*s'", (int)sq, str);
+		} else {
+			printf("\"'\"");
+			sq++;
+		}
+		str = &str[sq];
+	}
+}
+
+int main(int argc, char *argv[]) {
+	if (argc < 2) return EX_USAGE;
+	int opt;
+	optind = 2;
+	while (0 < (optarg = NULL, opt = getopt(argc, argv, argv[1]))) {
+		if (opt == '?') return EX_USAGE;
+		printf(" -%c", opt);
+		if (optarg) printArg(optarg);
+	}
+	printf(" --");
+	for (; optind < argc; ++optind) {
+		printArg(argv[optind]);
+	}
+	printf("\n");
+}