summaryrefslogtreecommitdiff
path: root/wrapper
diff options
context:
space:
mode:
Diffstat (limited to 'wrapper')
-rw-r--r--wrapper/.gitignore3
-rw-r--r--wrapper/Makefile36
-rw-r--r--wrapper/inner.sh.m4156
-rw-r--r--wrapper/outer.c63
-rw-r--r--wrapper/runcmd.mk15
5 files changed, 273 insertions, 0 deletions
diff --git a/wrapper/.gitignore b/wrapper/.gitignore
new file mode 100644
index 0000000..0c3c90a
--- /dev/null
+++ b/wrapper/.gitignore
@@ -0,0 +1,3 @@
+/outer
+/inner
+/inner.sh
diff --git a/wrapper/Makefile b/wrapper/Makefile
new file mode 100644
index 0000000..65a6613
--- /dev/null
+++ b/wrapper/Makefile
@@ -0,0 +1,36 @@
+#!/usr/bin/make -f
+# Copyright (C) 2009, 2015 Luke Shumaker
+#
+# 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 $(dir $(lastword $(MAKEFILE_LIST)))/../config.mk
+include $(topsrcdir)/automake.head.mk
+
+makefiles = Makefile
+am_src_files += outer.c inner.sh.m4 runcmd.mk
+am_out_files += outer inner
+am_clean_files += inner.sh
+am_sys_files += $(bindir)/$(PACKAGE) $(pkglibexecdir)/$(PACKAGE) $(pkglibexecdir)/runcmd.mk
+
+$(outdir)/outer.o: $(topoutdir)/config.h
+$(outdir)/inner.sh: $(topoutdir)/config.sh
+
+$(DESTDIR)$(bindir)/$(PACKAGE) : $(outdir)/outer | $(DESTDIR)$(bindir)
+ $(INSTALL_PROGRAM) $< $@
+$(DESTDIR)$(pkglibexecdir)/$(PACKAGE) : $(outdir)/inner | $(DESTDIR)$(pkglibexecdir)
+ $(INSTALL_PROGRAM) $< $@
+$(DESTDIR)$(pkglibexecdir)/runcmd.mk : $(srcdir)/runcmd.mk | $(DESTDIR)$(pkglibexecdir)
+ $(INSTALL_PROGRAM) $< $@
+
+include $(topsrcdir)/automake.tail.mk
diff --git a/wrapper/inner.sh.m4 b/wrapper/inner.sh.m4
new file mode 100644
index 0000000..b07199e
--- /dev/null
+++ b/wrapper/inner.sh.m4
@@ -0,0 +1,156 @@
+#!/usr/bin/env bash
+# rvs inner.sh - The main RVS program
+# Copyright (C) 2009-2010, 2015 Luke Shumaker
+#
+# This file is part of rvs.
+#
+# 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/>.
+set -u
+
+m4_include(config.sh)
+
+declare -r varname_REPO="${PACKAGE^^}_REPO"
+declare -r varname_EXEC_PATH="${PACKAGE^^}_EXEC_PATH"
+
+declare -r program_name="$1"; shift
+
+
+
+_() {
+ if type gettext &>/dev/null; then
+ TEXTDOMAINDIR=$localedir gettext "$pkgtextdomain" "$1";
+ else
+ echo "$1";
+ fi
+}
+
+sighandler_exit() {
+ local signal=$1; shift
+ echo
+ error 0 "$@"
+ trap -- "$signal"
+ kill -"$signal" "$$"
+}
+
+install_sighandlers() {
+ set -E
+ for signal in TERM HUP QUIT; do
+ trap "signalhandler_exit $signal '%s signal cought. Exiting...'" $signal
+ done
+ trap 'signalhandler_exit INT "Aborted by user. Exiting..."' INT
+ trap 'kill -USR1 "$$"' ERR
+}
+
+# Like GLibC's error(3), but call gettext on the format string
+error() {
+ >&2 printf "%s: $(_ "$2")\n" "$program_name" "${@:3}"
+ [[ $1 -eq 0 ]] || exit $1
+}
+
+errusage() {
+ >&2 printf "$(_ "$2")\n" "$program_name" "${@:3}"
+ [[ $1 -eq 0 ]] || exit $1
+}
+
+_runcmd() {
+ [[ $# -ge 1 ]] || errusage 1 'Usage: %q <command> [<args>]'
+ local cmd=$1; shift
+ local args_str=''
+ [[ $# -eq 0 ]] || printf -v args_str '%q ' "$@"
+
+ local exec_path="${!varname_EXEC_PATH:-$pkglibexecdir}"
+
+ shopt -s nullglob
+ local files=("${exec_path}"/modules/*/"$cmd")
+ if [[ ${#files[@]} -eq 0 ]]; then
+ error 127 '%s: Not a %s command' "$cmd" "$PACKAGE"
+ fi
+ files=("${files[@]#"${exec_path/modules/}"}")
+
+ local tmpdir
+ trap '[ -z "${tmpdir:-}" ] || rm -rf -- "$tmpdir"' EXIT
+ tmpdir="$(mktemp -dt "${PACKAGE}.XXXXXXXXXX")"
+ mkdir -- "$tmpdir/output"
+
+ local repo
+ repo="$(_repo)"
+ export "${varname_REPO}=${repo}"
+
+ local cwd
+ printf -v cwd '%q' "$PWD"
+
+ make -j1 \
+ -f "$exec_path/runcmd.mk" \
+ -C "$tmpdir/output" \
+ CWD="$cwd" \
+ ARGS="$args_str" \
+ EXEC_PATH="$exec_path" \
+ TMPDIR="$tmpdir" \
+ -- "${files[@]}"
+ exit $?
+}
+
+_repo() {
+ [[ $# -ne 0 ]] || errusage 1 'Usage: %q repo'
+ if [ -z "${!varname_REPO:-}" ]; then # we aren't getting a value from then env
+ local repo=".${PACKAGE,,}"
+
+ # [------can ascend-----] && ! [-not found repo--]
+ while [ "$PWD" != "$OLDPWD" ] && ! [ -d "$PWD/$repo" ]; do
+ cd ..
+ done
+
+ if [ -d "$PWD/$repo" ]; then
+ # we found a repository
+ printf '%s\n' "$PWD/$repo"
+ else
+ # we didn't find a repository
+ error 128 "No %s repository found" "$PACKAGE"
+ fi
+ else
+ printf '%s\n' "${!varname_REPO}"
+ fi
+}
+
+_init() {
+ [[ $# -gt 1 ]] || errusage 1 'Usage: %q init [directory]'
+ local dir="${1:-$PWD}"
+ mkdir -p -- "$dir"
+ cd "$dir"
+ repo="$(_repo 2> /dev/null)" || true
+ if [ -n "${repo:-}" ]; then
+ error 129 "Repository already exists at \`%s'" "$repo"
+ fi
+ export "$varname_REPO=$PWD/.${PACKAGE,,}"
+ mkdir "${!varname_REPO}"
+ _runcmd init "$dir"
+}
+
+main() {
+ install_sighandlers
+ [[ $# -ge 1 ]] || error 'No command specified';;
+ export "${PACKAGE^^}=$program_name"
+
+ local cmd=$1; shift
+ case "$cmd" in
+ repo) _repo "$@";;
+ init) _init "$@";;
+ *) _runcmd "$cmd" "$@";;
+ esac
+}
+
+main "$@"
+
+# Copy/Paste Virus 1.3c Please copy and paste this text anywhere. Track
+# its progress by searching for this MD5#f7eac285ebfe21c4587bfebb9582f90d
diff --git a/wrapper/outer.c b/wrapper/outer.c
new file mode 100644
index 0000000..c465555
--- /dev/null
+++ b/wrapper/outer.c
@@ -0,0 +1,63 @@
+/* RVS outer.c - A wrapper for $(bindir) to call the main RVS program
+ * Copyright (C) 2015 Luke Shumaker
+ *
+ * This file is part of rvs.
+ *
+ * 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 <errno.h> /* for errno */
+#include <error.h> /* for error(3) */
+#include <libintl.h> /* for dgettext(3) */
+#include <locale.h> /* for bindtextdomain(3) and textdomain(3) */
+#include <stdio.h> /* for asprintf(3) */
+#include <stdlib.h> /* for getenv(3), calloc(3) */
+#include <string.h> /* for mempcy(3) */
+#include <unistd.h> /* for execv(3) */
+
+#include "config.h"
+#define _ gettext
+
+#define EXIT_FAILURE_OOM 126
+#define EXIT_FAILURE_EXEC 127
+
+int
+main(int argc, char *argv[]) {
+ bindtextdomain(pkgtextdomain, localedir);
+ textdomain(pkgtextdomain);
+
+ unsetenv("ENV");
+ unsetenv("BASH_ENV");
+
+ const char *varname = PACKAGE_UPPER "_EXEC_PATH";
+
+ char *exec_path = getenv(varname);
+ if (!exec_path)
+ exec_path = pkglibexecdir;
+
+ char *exec_file = NULL;
+ if (asprintf(&exec_file, "%s/" PACKAGE, exec_path) < 0)
+ error(EXIT_FAILURE_OOM, errno,
+ _("Could not allocate memory for string"));
+
+ char **args = calloc(argc+2, sizeof(char*));
+ if (!args)
+ error(EXIT_FAILURE_OOM, errno,
+ _("Could not allocate cleared memory"));
+ args[0] = exec_file;
+ memcpy(&args[1], argv, sizeof(char*) * argc);
+
+ execv(exec_file, args);
+ error(EXIT_FAILURE_EXEC, errno, _("Could not execute: %s"), exec_file);
+ return EXIT_FAILURE_EXEC;
+}
diff --git a/wrapper/runcmd.mk b/wrapper/runcmd.mk
new file mode 100644
index 0000000..7340d11
--- /dev/null
+++ b/wrapper/runcmd.mk
@@ -0,0 +1,15 @@
+#!/usr/bin/make -f
+
+# Environment/command line variables:
+# - ARGS
+# - EXEC_DIR
+# - CWD
+
+SHELL = bash -o pipefail
+
+export OUTPUT_DIR := $(realpath .)
+
+% : $(EXEC_PATH)/modules/%
+ cd $(CWD) && '$<' $(ARGS) | tee -- '$@' | sed 's,^,$@:,' >/dev/tty
+
+include $(wildcard $(EXEC_PATH)/modules/*.mk)