From 9fb4ebd690a1cec28706fc7fea71cfd0ceabd6de Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sat, 20 Nov 2021 02:52:05 -0600 Subject: [PATCH] Update script/record-commands and rename logwrapper->logpath. --- scripts/record-commands | 36 +++++++++++++----------- toys/example/{logwrapper.c => logpath.c} | 34 +++++++++++----------- 2 files changed, 35 insertions(+), 35 deletions(-) rename toys/example/{logwrapper.c => logpath.c} (64%) diff --git a/scripts/record-commands b/scripts/record-commands index 8410966be..d2b779faf 100755 --- a/scripts/record-commands +++ b/scripts/record-commands @@ -3,38 +3,40 @@ # Set up command recording wrapper [ -z "$WRAPDIR" ] && WRAPDIR="$PWD"/record-commands && RM=$(which rm) -[ -z "$WRAPLOG" ] && export WRAPLOG="$PWD"/log.txt +[ -z "$LOGPATH" ] && export LOGPATH="$PWD"/log.txt -if [ $# -eq 0 ] +if [ ${#BASH_SOURCE[@]} -lt 2 ] && [ $# -eq 0 ] then - echo "usage: WRAPDIR=dir WRAPLOG=log.txt record-commands COMMAND..." + echo "usage: WRAPDIR=dir LOGPATH=log.txt record-commands COMMAND..." echo 'Then examine log.txt. "record-commands echo" to just setup $WRAPDIR' exit 1 fi -if [ ! -x "$WRAPDIR/logwrapper" ] +if [ ! -x "$WRAPDIR/logpath" ] then - make logwrapper - mkdir -p "$WRAPDIR" && mv logwrapper "$WRAPDIR" || exit 1 - + mkdir -p "$WRAPDIR" && PREFIX="$WRAPDIR/" scripts/single.sh logpath || exit 1 echo "$PATH" | tr : '\n' | while read DIR do - ls "$DIR/" | while read FILE + find "$DIR/" -type f,l -maxdepth 1 -executable -exec basename {} \; | \ + while read FILE do - ln -s logwrapper "$WRAPDIR/$FILE" 2>/dev/null + ln -s logpath "$WRAPDIR/$FILE" 2>/dev/null done done fi # Delete old log (if any) -rm -f "$WRAPLOG" +rm -f "$LOGPATH" -X=0 -if [ ! -z "$1" ] +# When sourced, set up wrapper for current context. +export PATH="$WRAPDIR:$PATH" +if [ ${#BASH_SOURCE[@]} -lt 2 ] then - PATH="$WRAPDIR:$PATH" "$@" -fi -X=$? -[ ! -z "$RM" ] && "$RM" -rf "$WRAPDIR" + "$@" + X=$? + + [ ! -z "$RM" ] && "$RM" -rf "$WRAPDIR" + + exit $X +fi -exit $X diff --git a/toys/example/logwrapper.c b/toys/example/logpath.c similarity index 64% rename from toys/example/logwrapper.c rename to toys/example/logpath.c index 0fa5a0aec..84fb712a3 100644 --- a/toys/example/logwrapper.c +++ b/toys/example/logpath.c @@ -1,37 +1,36 @@ -/* logwrapper.c - Record commands called out of $PATH to a log +/* logpath.c - Record commands called out of $PATH to a log * * Copyright 2019 Rob Landley * * I made it up. Must be built standalone to work. (Is its own multiplexer.) -USE_LOGWRAPPER(NEWTOY(logwrapper, 0, TOYFLAG_NOHELP|TOYFLAG_USR|TOYFLAG_BIN)) +USE_LOGPATH(NEWTOY(logpath, 0, TOYFLAG_NOHELP|TOYFLAG_USR|TOYFLAG_BIN)) -config LOGWRAPPER - bool "logwrapper" +config LOGPATH + bool "logpath" default n help - usage: logwrapper ... + usage: logpath ... - Append command line to $WRAPLOG, then call second instance + Append command line to $LOGPATH, then call second instance of command in $PATH. */ -#define FOR_logwrapper +#define FOR_logpath #include "toys.h" #if CFG_TOYBOX #warning Must be built standalone to work. #endif -void logwrapper_main(void) +void logpath_main(void) { - char *log = getenv("WRAPLOG"), *omnom = basename(*toys.argv), - *s, *ss, *sss; + char *log = getenv("LOGPATH"), *omnom = basename(*toys.argv), *s, *ss, *sss; struct string_list *list; int i, len; // Log the command line - if (!log) error_exit("no $WRAPLOG"); + if (!log) error_exit("no $LOGPATH"); len = strlen(omnom)+2; for (i = 0; istr; execve(list->str, toys.argv, environ); } + error_exit("no %s after %s in $PATH=%s", omnom, + **toys.argv == '/' ? *toys.argv : "logpath", getenv("PATH")); }