From 6a0d154a9ccfb0e2d2470dfdca06621e864895b3 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Wed, 5 Aug 2015 18:22:53 +0200 Subject: [PATCH] OSX compatibility --- .gitignore | 8 ++ Makefile.am | 7 ++ README.md | 5 ++ bin/Makefile.am | 2 + bin/btr | 14 +-- bin/btr-hook | 14 +-- bin/btr-setsid.c | 34 ++++++++ bin/btrc | 21 +++-- bin/{btrd => btrd.in} | 14 +-- configure.ac | 60 +++++++++++-- share/btr/btr.sh | 28 +++--- share/btr/btrc.args | 1 - share/btr/btrc.sh | 20 ++--- share/btr/btrd.sh | 79 +++++++++-------- share/btr/{common.sh => common.sh.in} | 121 ++++++++++++++------------ share/btr/{opt.awk => opt.awk.in} | 4 +- 16 files changed, 281 insertions(+), 151 deletions(-) create mode 100644 bin/Makefile.am create mode 100644 bin/btr-setsid.c rename bin/{btrd => btrd.in} (52%) rename share/btr/{common.sh => common.sh.in} (77%) rename share/btr/{opt.awk => opt.awk.in} (88%) diff --git a/.gitignore b/.gitignore index 5c80847..6acac89 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,11 @@ config.log config.status config.cache btr-?.?.* +bin/btr-setsid +bin/btrd +compile +depcomp +*.o +.deps +share/btr/common.sh +share/btr/opt.awk diff --git a/Makefile.am b/Makefile.am index ec71d7b..4ef424a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,5 +1,12 @@ EXTRA_SCRIPTS = btrdir = $(prefix) +#bin_SCRIPTS = share/btr/opt.awk share/btr/common.sh bin/btrd dist_bin_SCRIPTS = $(BTR_BIN) nobase_dist_btr_SCRIPTS = $(BTR_SCRIPTS) nobase_dist_btr_DATA = $(BTR_SHARE) + +#CLEANFILES = share/btr/opt.awk share/btr/common.sh bin/btrd + +SUBDIRS = bin + +# vim: noet diff --git a/README.md b/README.md index 6f15c99..8459995 100644 --- a/README.md +++ b/README.md @@ -62,3 +62,8 @@ Usage: btr [-hyvqcC] [] See also curl.example.conf ``` + +#### Prerequisites + +* GNU getopt +* nmap's ncat (for btrd/btrc) diff --git a/bin/Makefile.am b/bin/Makefile.am new file mode 100644 index 0000000..f2a3741 --- /dev/null +++ b/bin/Makefile.am @@ -0,0 +1,2 @@ +bin_PROGRAMS = btr-setsid +btr_setsid_SOURCES = btr-setsid.c \ No newline at end of file diff --git a/bin/btr b/bin/btr index a8a29a4..db82d6f 100755 --- a/bin/btr +++ b/bin/btr @@ -1,21 +1,21 @@ #!/bin/bash -export BTR_BINDIR=$(realpath $(dirname $0)/) -export BTR_LIBDIR=$(realpath $(dirname $0)/../share/btr) +export BTR_BINDIR=$(dirname $0)/ +export BTR_LIBDIR=$(dirname $0)/../share/btr BTR_BANNER="Build, test & report. Simple and stupid." . $BTR_LIBDIR/common.sh . $BTR_LIBDIR/btr.sh -btr-parseopts "$@" -btr-setup +btr_parseopts "$@" +btr_setup if $BTR_VERBOSE then - btr-conf-show - btr-confirm "Everything setup. Do you want to continue?" + btr_conf_show + btr_confirm "Everything setup. Do you want to continue?" fi -btr-run +btr_run # vim: noet diff --git a/bin/btr-hook b/bin/btr-hook index 644d60c..099fea0 100755 --- a/bin/btr-hook +++ b/bin/btr-hook @@ -1,21 +1,21 @@ #!/bin/bash -export BTR_BINDIR=$(realpath $(dirname $0)/) -export BTR_LIBDIR=$(realpath $(dirname $0)/../share/btr) +export BTR_BINDIR=$(dirname $0)/ +export BTR_LIBDIR=$(dirname $0)/../share/btr BTR_BANNER="Install btr hook into your VCS reporitory." . $BTR_LIBDIR/common.sh . $BTR_LIBDIR/btr.sh -btr-parseopts "$@" -btr-setup false +btr_parseopts "$@" +btr_setup false if $BTR_VERBOSE then - btr-conf-show + btr_conf_show fi -btr-confirm "Everything setup. Do you want to install the '$BTR_EXTRA_ARGS' hook for '$BTR_SOURCE_RULES'?" +btr_confirm "Everything setup. Do you want to install the '$BTR_EXTRA_ARGS' hook for '$BTR_SOURCE_RULES'?" case "$BTR_SOURCE_RULES" in git) @@ -29,7 +29,7 @@ git) else echo "#!/bin/bash" fi - echo $(btr-conf-dump) 'BTR_BRANCH=$(basename $(git symbolic-ref HEAD))' "'$BTR_BINDIR/btr' -qy &" + echo $(btr_conf_dump) 'BTR_BRANCH=$(basename $(git symbolic-ref HEAD))' "'$BTR_BINDIR/btr' -qy &" ) >"$BTR_HOOK_FILE.tmp" && \ chmod +x "$BTR_HOOK_FILE.tmp" && \ mv "$BTR_HOOK_FILE.tmp" "$BTR_HOOK_FILE" && \ diff --git a/bin/btr-setsid.c b/bin/btr-setsid.c new file mode 100644 index 0000000..f293efa --- /dev/null +++ b/bin/btr-setsid.c @@ -0,0 +1,34 @@ +#include +#include +#include + +int main(int argc, char *argv[]) +{ + if (argc < 2) { + fprintf(stderr, "Usage: %s [args...]\n", argv[0]); + return EXIT_FAILURE; + } + + if (getpid() == getpgrp()) { + pid_t pid = fork(); + + switch (pid) { + case 0: + break; + case -1: + perror("fork"); + return EXIT_FAILURE; + default: + return EXIT_SUCCESS; + } + } + + if (setsid() < 0) { + perror("setsid"); + return EXIT_FAILURE; + } + + execvp(argv[1], &argv[1]); + perror("exec"); + return EXIT_FAILURE; +} diff --git a/bin/btrc b/bin/btrc index 82b9b7f..658372d 100755 --- a/bin/btrc +++ b/bin/btrc @@ -1,27 +1,32 @@ #!/bin/bash -export BTR_BINDIR=$(realpath $(dirname $0)/) -export BTR_LIBDIR=$(realpath $(dirname $0)/../share/btr) +export BTR_BINDIR=$(dirname $0)/ +export BTR_LIBDIR=$(dirname $0)/../share/btr BTR_BANNER="Control \`btrd\` daemons." . $BTR_LIBDIR/common.sh . $BTR_LIBDIR/btrc.sh -btr-parseopts "$@" -btrc-setup +btr_parseopts "$@" +btrc_setup case "$BTR_ACTION" in status) - ncat --recv-only -U "$BTR_COMFILE" + # see https://github.com/nmap/nmap/issues/193 + if test $(uname -s) = Darwin; then + ncat --recv-only 127.0.0.1 $(cat "$BTR_COMFILE") + else + ncat --recv-only -U "$BTR_COMFILE" + fi ;; cancel) - btrc-signal INT + btrc_signal INT ;; run) - btrc-signal HUP + btrc_signal HUP ;; term) - btrc-signal TERM + btrc_signal TERM ;; esac diff --git a/bin/btrd b/bin/btrd.in similarity index 52% rename from bin/btrd rename to bin/btrd.in index 055bd7b..bcb1038 100755 --- a/bin/btrd +++ b/bin/btrd.in @@ -1,19 +1,19 @@ #!/bin/bash -export BTR_BINDIR=$(realpath $(dirname $0)/) -export BTR_LIBDIR=$(realpath $(dirname $0)/../share/btr) +export BTR_BINDIR=$(dirname $0)/ +export BTR_LIBDIR=$(dirname $0)/../share/btr BTR_BANNER="Run btr as background daemon receiving jobs for a repo." . $BTR_LIBDIR/common.sh . $BTR_LIBDIR/btr.sh -btr-parseopts "$@" -btr-setup +btr_parseopts "$@" +btr_setup if $BTR_VERBOSE then - btr-conf-show - btr-confirm "Everything setup. Do you want to start the daemon?" + btr_conf_show + btr_confirm "Everything setup. Do you want to start the daemon?" elif ! $BTR_QUIET then echo "Starting the daemon. Control with \`btrc $BTR_BUILD\`" @@ -21,6 +21,6 @@ else echo "$BUILD" fi -setsid $SHELL -c "$BTR_LIBDIR/btrd.sh &" +@SETSID@ $SHELL -c "$BTR_LIBDIR/btrd.sh &" # vim: noet diff --git a/configure.ac b/configure.ac index 98676b6..a3ab9da 100644 --- a/configure.ac +++ b/configure.ac @@ -1,22 +1,72 @@ -AC_INIT([btr], [0.4.0], [mike@php.net]) +AC_INIT([btr], [0.5.0], [mike@php.net]) AM_INIT_AUTOMAKE([foreign]) -AM_SILENT_RULES() +AM_SILENT_RULES AC_CONFIG_SRCDIR([./share/btr/btr.sh]) +AC_PATH_PROG([GETOPT], [getopt], [getopt]) +AC_ARG_VAR([GETOPT], [location of gnu-getopt, see --with-gnu-getopt; e.g. /usr/local/opt/gnu-getopt/bin/getopt]) +AC_ARG_WITH([gnu-getopt], [AS_HELP_STRING([--with-gnu-getopt=path], [ + Location of the enhanced GNU getopt program])], [ +], [ + with_gnu_getopt=$GETOPT +]) + +AC_MSG_CHECKING([for GNU getopt]) +$with_gnu_getopt --test >/dev/null +AS_IF([test $? -ne 4], [ + AC_MSG_RESULT([no]) + AC_MSG_ERROR([$with_gnu_getopt looks like the BSD version, please provide the path to the GNU version --with-gnu-getopt]) +], [ + AC_MSG_RESULT([yes, $with_gnu_getopt]) + AC_SUBST([GETOPT], [$with_gnu_getopt]) +]) + +AC_PROG_AWK +AC_PATH_PROG([AWK_PATH], [$AWK], [/bin/$AWK]) + +AC_CHECK_PROGS([SETSID], [setsid], [nothing]) +AC_ARG_VAR([SETSID], [location of a setsid program; a simple replacement will be built if none found or provided]) +AS_IF([test "$SETSID" = nothing], [ + SETSID="\$BTR_BINDIR/btr-setsid" + AC_PROG_CC + AC_CONFIG_FILES([bin/Makefile]) +], [:]); + +AC_CHECK_PROGS([NPROC], [nproc sysctl], [nothing]) +AC_MSG_CHECKING([for number of cores]) +AS_CASE([$NPROC], + [nproc], [ + BTR_CPUS=$(nproc) + ], + [sysctl], [ + BTR_CPUS=$(sysctl -n hw.ncpu 2>/dev/null || echo 1) + ], + [ + BTR_CPUS=1 + ] +) +AC_MSG_RESULT([$BTR_CPUS]) +AC_SUBST([BTR_CPUS], [$BTR_CPUS]) + AC_MSG_CHECKING([for bin/ scripts]); - BTR_BIN=$(find $srcdir/bin -type f -perm -a=x | xargs) + BTR_BIN=$(find $srcdir/bin -type f -perm -a=x | sed 's/\.in$//' | xargs) AC_MSG_RESULT([$BTR_BIN]) AC_SUBST([BTR_BIN], [$BTR_BIN]) AC_MSG_CHECKING([for share/ scripts]) - BTR_SCRIPTS=$(find $srcdir/share/btr -type f -perm -a=x | xargs) + BTR_SCRIPTS=$(find $srcdir/share/btr -type f -perm -a=x | sed 's/\.in$//' | xargs) AC_MSG_RESULT([$BTR_SCRIPTS]) AC_SUBST([BTR_SCRIPTS], [$BTR_SCRIPTS]) AC_MSG_CHECKING([for share/ data]) - BTR_SHARE=$(find $srcdir/share/btr -type f ! -perm -a=x | xargs) + BTR_SHARE=$(find $srcdir/share/btr -type f ! -perm -a=x | sed 's/\.in$//' | xargs) AC_MSG_RESULT([$BTR_SHARE]) AC_SUBST([BTR_SHARE], [$BTR_SHARE]) +AC_CONFIG_FILES([bin/btrd], [chmod +x bin/btrd]) +AC_CONFIG_FILES([share/btr/opt.awk], [chmod +x share/btr/opt.awk]) +AC_CONFIG_FILES([share/btr/common.sh], [chmod +x share/btr/common.sh]) AC_CONFIG_FILES([Makefile]) AC_OUTPUT + +dnl vim: noet diff --git a/share/btr/btr.sh b/share/btr/btr.sh index 04ac847..518afd1 100755 --- a/share/btr/btr.sh +++ b/share/btr/btr.sh @@ -1,14 +1,14 @@ #!/bin/sh -function btr-setup { +function btr_setup { if test -z "$BTR_SOURCE_RULES" -o -z "$BTR_BUILD_RULES" -o -z "$BTR_REPORT_RULES" then - btr-banner - btr-help + btr_banner + btr_help fi - btr-setup-verbosity ${1:-true} - btr-setup-rundir + btr_setup_verbosity ${1:-true} + btr_setup_rundir export BTR_SOURCE_RULES BTR_BUILD_RULES BTR_REPORT_RULES test -z "$BTR_SOURCE_ARGS" || export BTR_SOURCE_ARGS @@ -17,7 +17,7 @@ function btr-setup { test -z "$BTR_BUILD_CLEAN" || export BTR_BUILD_CLEAN test -z "$BTR_TEST_ARGS" || export BTR_TEST_ARGS test -z "$BTR_REPORT_ARGS" || export BTR_REPORT_ARGS - BTR_REPO=$(basename $(sed -re 's~^.*[/:#]~~' <<<"$BTR_SOURCE_ARGS") .git) + BTR_REPO=$(basename $(sed 's~^.*[/:#]~~' <<<"$BTR_SOURCE_ARGS") .git) BTR_SAFE_BRANCH=$(tr ":/" "_" <<<$(basename "$BTR_BRANCH")) export BTR_REPO BTR_BRANCH BTR_SAFE_BRANCH @@ -38,9 +38,9 @@ function btr-setup { export BTR_LAST_REPORT=$(basename $(ls -t "$BTR_RUNDIR/$BTR_LOG_DIR/test@"* 2>/dev/null | head -n1) 2>/dev/null) export BTR_REPORT="$BTR_LOG_DIR/report@$BTR_DATE.log" } -export -f btr-setup +export -f btr_setup -function btr-conf-show { +function btr_conf_show { echo echo "# Configuration:" echo @@ -72,15 +72,15 @@ function btr-conf-show { echo "BTR_LAST_REPORT = $BTR_LAST_REPORT" echo } -export -f btr-conf-show +export -f btr_conf_show -function btr-run { +function btr_run { set -e - make -e $BTR_SILENT_FLAG -C $BTR_RUNDIR -f $BTR_LIBDIR/source/$BTR_SOURCE_RULES.mk - make -e $BTR_SILENT_FLAG -C $BTR_RUNDIR -f $BTR_LIBDIR/build/$BTR_BUILD_RULES.mk - make -e $BTR_SILENT_FLAG -C $BTR_RUNDIR -f $BTR_LIBDIR/report/$BTR_REPORT_RULES.mk + make -e $BTR_SILENT_FLAG -C $BTR_RUNDIR -f $(pwd)/$BTR_LIBDIR/source/$BTR_SOURCE_RULES.mk + make -e $BTR_SILENT_FLAG -C $BTR_RUNDIR -f $(pwd)/$BTR_LIBDIR/build/$BTR_BUILD_RULES.mk + make -e $BTR_SILENT_FLAG -C $BTR_RUNDIR -f $(pwd)/$BTR_LIBDIR/report/$BTR_REPORT_RULES.mk set +e } -export -f btr-run +export -f btr_run # vim: noet diff --git a/share/btr/btrc.args b/share/btr/btrc.args index 9526539..ea25776 100644 --- a/share/btr/btrc.args +++ b/share/btr/btrc.args @@ -1,5 +1,4 @@ [action] s[t[at[us]]] Show the status of the build\\nr[un] Make a BTR run\\nc[anc[el]] Cancel currently running job\\nt[erm[inate]] Terminate the BTR daemon The build id of the `btrd` daemon.\\nUsually similar to $repo@$branch[-$suffix]. - # vim: noet ts=24 sw=24 diff --git a/share/btr/btrc.sh b/share/btr/btrc.sh index 8a38bb3..a230318 100755 --- a/share/btr/btrc.sh +++ b/share/btr/btrc.sh @@ -1,6 +1,6 @@ #!/bin/bash -function btrc-parseargs { +function btrc_parseargs { eval set -- "$BTR_EXTRA_ARGS" while test $# -gt 0 do @@ -30,13 +30,13 @@ function btrc-parseargs { done } -function btrc-setup { - btrc-parseargs +function btrc_setup { + btrc_parseargs if test -z "$BTR_BUILD" then - btr-banner - btr-help + btr_banner + btr_help fi if test -z "$BTR_ACTION" @@ -44,8 +44,8 @@ function btrc-setup { BTR_ACTION=status fi - btr-setup-rundir - btr-setup-verbosity false + btr_setup_rundir + btr_setup_verbosity false BTR_PIDFILE="$BTR_RUNDIR/$BTR_BUILD.pid" BTR_LOGFILE="$BTR_RUNDIR/$BTR_BUILD.log" @@ -63,9 +63,9 @@ function btrc-setup { error "Could not find btrd pid file of '$BTR_BUILD' in $BTR_RUNDIR." fi } -export -f btrc-setup +export -f btrc_setup -function btrc-signal { +function btrc_signal { local sig=$1 local pid=$(cat "$BTR_PIDFILE") kill -s $sig $pid @@ -82,7 +82,7 @@ function btrc-signal { ;; esac } -export -f btrc-signal +export -f btrc_signal # vim: noet diff --git a/share/btr/btrd.sh b/share/btr/btrd.sh index 725f0e4..77aa951 100755 --- a/share/btr/btrd.sh +++ b/share/btr/btrd.sh @@ -1,49 +1,48 @@ #!/bin/bash -function btrd-start { - btrd-cancel +function btrd_start { + btrd_cancel BTR_DATE=$(date +%Y%m%d%H%M%S) - btr-setup - btr-run & + btr_setup + btr_run & BTR_WORKER=$! } -export -f btrd-start +export -f btrd_start -function btrd-cancel { - if btrd-worker-started +function btrd_cancel { + if btrd_worker_started then - kill $BTR_WORKER - wait $BTR_WORKER + btrd_worker_kill + btrd_worker_reap fi - BTR_WORKER=0 } -export -f btrd-cancel +export -f btrd_cancel -function btrd-stop { +function btrd_stop { BTR_DAEMON=false } -export -f btrd-stop +export -f btrd_stop -function btrd-ctime { +function btrd_ctime { stat -c %y "$1" } -export -f btrd-ctime +export -f btrd_ctime -function btrd-fsize { +function btrd_fsize { local bytes=$(stat -c %s "$1") } -export -f btrd-fsize +export -f btrd_fsize -function btrd-status { +function btrd_status { echo "BTR_BUILD='$BTR_BUILD'" echo "BTR_SERVER='$BTR_SERVER'" echo "BTR_PIDFILE='$BTR_PIDFILE'" echo "BTR_LOGFILE='$BTR_LOGFILE'" echo "BTR_COMFILE='$BTR_COMFILE'" } -export -f btrd-status +export -f btrd_status -function btrd-logrotate { +function btrd_logrotate { local i=1 local f="$BTR_LOGFILE" @@ -57,28 +56,28 @@ function btrd-logrotate { fi } -export -f btrd-logrotate +export -f btrd_logrotate -function btrd-worker-started { +function btrd_worker_started { test "$BTR_WORKER" -gt 1 } -export -f btrd-worker-started +export -f btrd_worker_started -function btrd-worker-running { +function btrd_worker_running { kill -s 0 $BTR_WORKER } -export -f btrd-worker-running +export -f btrd_worker_running -function btrd-worker-reap { +function btrd_worker_reap { wait $BTR_WORKER BTR_WORKER=0 } -export -f btrd-worker-reap +export -f btrd_worker_reap -function btrd-worker-kill { +function btrd_worker_kill { kill $BTR_WORKER } -export -f btrd-worker-kill +export -f btrd_worker_kill BTR_DAEMON=true BTR_WORKER=0 @@ -89,29 +88,35 @@ BTR_COMFILE="$BTR_RUNDIR/$BTR_BUILD.socket" export BTR_DAEMON BTR_WORKER BTR_SERVER BTR_PIDFILE BTR_LOGFILE BTR_COMFILE -btrd-logrotate +btrd_logrotate exec >"$BTR_LOGFILE" 2>&1 echo $$ >"$BTR_PIDFILE" -ncat -lkU -c btrd-status "$BTR_COMFILE" & +# see https://github.com/nmap/nmap/issues/193 +if test $(uname -s) = Darwin; then + echo $(($(cksum <<<"$BTR_BUILD" | cut -d" " -f 1) % 64511 + 1024)) > "$BTR_COMFILE" + ncat -nlkc btrd_status 127.0.0.1 $(cat "$BTR_COMFILE") & +else + ncat -nlkc btrd_status -U "$BTR_COMFILE" & +fi BTR_SERVER=$! -trap btrd-start HUP -trap btrd-cancel INT -trap btrd-stop TERM +trap btrd_start HUP +trap btrd_cancel INT +trap btrd_stop TERM while $BTR_DAEMON do - if btrd-worker-started && btrd-worker-running + if btrd_worker_started && btrd_worker_running then - btrd-worker-reap + btrd_worker_reap else kill -s STOP $$ fi done -btrd-cancel +btrd_cancel if test "$BTR_SERVER" -gt 1 then diff --git a/share/btr/common.sh b/share/btr/common.sh.in similarity index 77% rename from share/btr/common.sh rename to share/btr/common.sh.in index 88f522e..8c8b7e8 100755 --- a/share/btr/common.sh +++ b/share/btr/common.sh.in @@ -1,7 +1,7 @@ #!/bin/bash export BTR_DATE=$(date +%Y%m%d%H%M%S) -export BTR_CPUS=${CPUS:-$(nproc)} +export BTR_CPUS=${CPUS:-@BTR_CPUS@} export BTR_PROG=$(basename "$0") export BTR_QUIET=false export BTR_VERBOSE=false @@ -13,16 +13,16 @@ function error { } export -f error -function btr-banner { - echo "$BTR_PROG v0.4.0, (c) Michael Wallner " +function btr_banner { + echo "$BTR_PROG v@PACKAGE_VERSION@, (c) Michael Wallner " if test "$BTR_BANNER" then echo "$BTR_BANNER" fi } -export -f btr-banner +export -f btr_banner -function btr-confirm { +function btr_confirm { local CONTINUE if ! $BTR_FORCEYES then @@ -39,31 +39,43 @@ function btr-confirm { esac fi } -export -f btr-confirm +export -f btr_confirm -function btr-setup-rundir { +function btr_setup_rundir { local default_rundir="${1:-/tmp/btr}" - + if test -z "$BTR_RUNDIR" then export BTR_RUNDIR="$default_rundir" else - export BTR_RUNDIR=$(realpath "$BTR_RUNDIR") + export BTR_RUNDIR fi - + mkdir -p "$BTR_RUNDIR" || error "Could not create directory '$BTR_RUNDIR'" } -export -f btr-setup-rundir +export -f btr_setup_rundir -function btr-setup-verbosity { +btr_verbose_echo_n=false +function btr_verbose_echo { + if test "$1" != "-n"; then + $btr_verbose_echo_n || echo + btr_verbose_echo_n=false + else + btr_verbose_echo_n=true + fi + echo "$@" +} +export -f btr_verbose_echo + +function btr_setup_verbosity { local for_make=${1:-false} - + if ${BTR_VERBOSE:-false} then BTR_QUIET_FLAG= BTR_SILENT_FLAG= BTR_VERBOSE_FLAG="-v" - SAY="echo; echo" + SAY="btr_verbose_echo" elif $BTR_QUIET then BTR_QUIET_FLAG="-q" @@ -76,21 +88,21 @@ function btr-setup-verbosity { BTR_VERBOSE_FLAG= SAY="echo" fi - + if $for_make then SAY="@$SAY" fi - + export BTR_QUIET BTR_VERBOSE BTR_FORCEYES BTR_QUIET_FLAG BTR_SILENT_FLAG BTR_VERBOSE_FLAG SAY } -export -f btr-setup-verbosity +export -f btr_setup_verbosity -function btr-shortoptions { +function btr_shortoptions { ( local f local e - + for f in common $BTR_PROG do for e in flags opts @@ -100,13 +112,13 @@ function btr-shortoptions { done ) | xargs | tr -d " " } -export -f btr-shortoptions +export -f btr_shortoptions -function btr-longoptions { +function btr_longoptions { ( local f local e - + for f in common $BTR_PROG do for e in flags opts @@ -114,24 +126,24 @@ function btr-longoptions { test -e "$BTR_LIBDIR/$f.$e" && cut -sf2 <"$BTR_LIBDIR/$f.$e" done done - ) | sed -r -e 's/(:+).*/\1/' | xargs | tr " " "," + ) | sed 's/\(:\).*/\1/' | xargs | tr " " "," } -export -f btr-longoptions +export -f btr_longoptions -function btr-flags { +function btr_flags { ( local f local e - + for f in common $BTR_PROG do test -e "$BTR_LIBDIR/$f.flags" && cut -sf1 <"$BTR_LIBDIR/$f.flags" done ) | xargs | tr -d " " } -export -f btr-flags +export -f btr_flags -function btr-help-options { +function btr_help_options { local f o=$( for f in common $BTR_PROG do @@ -152,42 +164,43 @@ function btr-help-options { echo "$o" fi } -export -f btr-help-options +export -f btr_help_options -function btr-help-args { +function btr_help_args { local a d l - + if test -e "$BTR_LIBDIR/$BTR_PROG.args" then echo echo " Arguments:" - while read a d + sed -e 's/#.*//' -e '/^$/d' < "$BTR_LIBDIR/$BTR_PROG.args" | while read a d do + test -z "$a" && continue printf "%b\n" "$d" | fold -sw46 | while read l do printf " %-16s %s\n" "$a" "$l" a= done echo - done <"$BTR_LIBDIR/$BTR_PROG.args" + done fi } -export -f btr-help-args +export -f btr_help_args -function btr-args { +function btr_args { if test -e "$BTR_LIBDIR/$BTR_PROG.args" then cut -sf1 <"$BTR_LIBDIR/$BTR_PROG.args" | xargs fi } -export -f btr-args +export -f btr_args -function btr-help { +function btr_help { echo - echo "Usage: $BTR_PROG [-$(btr-flags)] []" $(btr-args) - btr-help-options flags - btr-help-options opts - btr-help-args + echo "Usage: $BTR_PROG [-$(btr_flags)] []" $(btr_args) + btr_help_options flags + btr_help_options opts + btr_help_args if test $BTR_PROG != "btrc" then echo @@ -209,14 +222,14 @@ function btr-help { fi exit } -export -f btr-help +export -f btr_help -function btr-parseopts { - local shortoptions="$(btr-shortoptions common btr-flags btr-options)" - local longoptions="$(btr-longoptions common btr-flags btr-options)" +function btr_parseopts { + local shortoptions="$(btr_shortoptions common btr-flags btr-options)" + local longoptions="$(btr_longoptions common btr-flags btr-options)" local options - - options=$(getopt \ + + options=$(@GETOPT@ \ --name $BTR_PROG \ --options "$shortoptions" \ --longoptions "$longoptions" \ @@ -226,15 +239,15 @@ function btr-parseopts { then btr-help fi - + eval set -- "$options" - + while test $# -gt 0 do case "$1" in -h|--help) - btr-banner - btr-help + btr_banner + btr_help ;; -v|--verbose) BTR_QUIET=false @@ -312,9 +325,9 @@ function btr-parseopts { shift done } -export -f btr-parseopts +export -f btr_parseopts -function btr-conf-dump { +function btr_conf_dump { echo "BTR_QUIET='$BTR_QUIET'" echo "BTR_VERBOSE='$BTR_VEROSE'" echo "BTR_FORCEYES='$BTR_FORCEYES'" @@ -331,6 +344,6 @@ function btr-conf-dump { echo "BTR_REPORT_RULES='$BTR_REPORT_RULES'" test ${BTR_REPORT_ARGS+defined} && echo "BTR_REPORT_ARGS='$BTR_REPORT_ARGS'" } -export -f btr-conf-dump +export -f btr_conf_dump # vim: noet diff --git a/share/btr/opt.awk b/share/btr/opt.awk.in similarity index 88% rename from share/btr/opt.awk rename to share/btr/opt.awk.in index cdc0b76..e9ed958 100755 --- a/share/btr/opt.awk +++ b/share/btr/opt.awk.in @@ -1,4 +1,4 @@ -#!/bin/awk -f +#!@AWK_PATH@ -f BEGIN { FS="\t" @@ -17,3 +17,5 @@ BEGIN { printf " -%s, --%-24s %s\n", $1, $2, $3 } } + +# vim: noet -- 2.30.2