17e1c014aSSimon J. Gerraty: 27e1c014aSSimon J. Gerraty# SPDX-License-Identifier: BSD-2-Clause 37e1c014aSSimon J. Gerraty 47e1c014aSSimon J. Gerraty# NAME: 57e1c014aSSimon J. Gerraty# hooks.sh - provide hooks for customization 67e1c014aSSimon J. Gerraty# 77e1c014aSSimon J. Gerraty# SYNOPSIS: 87e1c014aSSimon J. Gerraty# hooks_add_all HOOKS [--first] func [...] 97e1c014aSSimon J. Gerraty# hooks_add_once HOOKS [--first] func [...] 107e1c014aSSimon J. Gerraty# hooks_add_default_set {all,once} 117e1c014aSSimon J. Gerraty# hooks_add HOOKS func [...] 127e1c014aSSimon J. Gerraty# hooks_get [--lifo] HOOKS 137e1c014aSSimon J. Gerraty# hooks_run [--lifo] HOOKS ["args"] 147e1c014aSSimon J. Gerraty# hooks_run_all [--lifo] HOOKS ["args"] 157e1c014aSSimon J. Gerraty# hooks_has HOOKS func 167e1c014aSSimon J. Gerraty# 177e1c014aSSimon J. Gerraty# add_hooks HOOKS [--first] func [...] 187e1c014aSSimon J. Gerraty# run_hooks HOOKS [LIFO] ["args"] 197e1c014aSSimon J. Gerraty# run_hooks_all HOOKS [LIFO] ["args"] 207e1c014aSSimon J. Gerraty# 217e1c014aSSimon J. Gerraty# DESCRIPTION: 227e1c014aSSimon J. Gerraty# The functions add_hooks and run_hooks are retained for 237e1c014aSSimon J. Gerraty# backwards compatability. They are aliases for hooks_add and 247e1c014aSSimon J. Gerraty# hooks_run. 257e1c014aSSimon J. Gerraty# 267e1c014aSSimon J. Gerraty# hooks_add_all simply adds the "func"s to the list "HOOKS". 277e1c014aSSimon J. Gerraty# 287e1c014aSSimon J. Gerraty# If the first arg is '--first' "func"s are added to the start 297e1c014aSSimon J. Gerraty# of the list. 307e1c014aSSimon J. Gerraty# 317e1c014aSSimon J. Gerraty# hooks_add_once does the same but only if "func" is not in "HOOKS". 327e1c014aSSimon J. Gerraty# hooks_add uses one of the above based on "option", '--all' (default) 337e1c014aSSimon J. Gerraty# or '--once'. 347e1c014aSSimon J. Gerraty# 357e1c014aSSimon J. Gerraty# hooks_add_default_set sets the default behavior of hooks_add 367e1c014aSSimon J. Gerraty# 377e1c014aSSimon J. Gerraty# hooks_get simply returns the named list of functions. 387e1c014aSSimon J. Gerraty# 397e1c014aSSimon J. Gerraty# hooks_has indicates whether "func" in in "HOOKS". 407e1c014aSSimon J. Gerraty# 417e1c014aSSimon J. Gerraty# hooks_run runs each "func" in $HOOKS and stops if any of them 427e1c014aSSimon J. Gerraty# return a bad status. 437e1c014aSSimon J. Gerraty# 447e1c014aSSimon J. Gerraty# hooks_run_all does the same but does not stop on error. 457e1c014aSSimon J. Gerraty# 467e1c014aSSimon J. Gerraty# If run_hooks or run_hooks_all is given a flag of '--lifo' or 477e1c014aSSimon J. Gerraty# 2nd argument of LIFO the hooks are run in the reverse order of 487e1c014aSSimon J. Gerraty# calls to hooks_add. 497e1c014aSSimon J. Gerraty# Any "args" specified are passed to each hook function. 507e1c014aSSimon J. Gerraty# 517e1c014aSSimon J. Gerraty 527e1c014aSSimon J. Gerraty# RCSid: 53*203027b2SSimon J. Gerraty# $Id: hooks.sh,v 1.24 2024/12/13 03:55:52 sjg Exp $ 547e1c014aSSimon J. Gerraty# 557e1c014aSSimon J. Gerraty# @(#)Copyright (c) 2000-2024 Simon J. Gerraty 567e1c014aSSimon J. Gerraty# 577e1c014aSSimon J. Gerraty# This file is provided in the hope that it will 587e1c014aSSimon J. Gerraty# be of use. There is absolutely NO WARRANTY. 597e1c014aSSimon J. Gerraty# Permission to copy, redistribute or otherwise 607e1c014aSSimon J. Gerraty# use this file is hereby granted provided that 617e1c014aSSimon J. Gerraty# the above copyright notice and this notice are 627e1c014aSSimon J. Gerraty# left intact. 637e1c014aSSimon J. Gerraty 647e1c014aSSimon J. Gerraty# avoid multiple inclusion 657e1c014aSSimon J. Gerraty_HOOKS_SH=: 667e1c014aSSimon J. Gerraty 67*203027b2SSimon J. Gerraty# does local *actually* work? 68*203027b2SSimon J. Gerratylocal_works() { 69*203027b2SSimon J. Gerraty local _fu 70*203027b2SSimon J. Gerraty} 71*203027b2SSimon J. Gerraty 72*203027b2SSimon J. Gerratyif local_works > /dev/null 2>&1; then 73*203027b2SSimon J. Gerraty _local=local 747e1c014aSSimon J. Gerratyelse 75*203027b2SSimon J. Gerraty _local=: 767e1c014aSSimon J. Gerratyfi 77*203027b2SSimon J. Gerraty# for backwards compatability 78*203027b2SSimon J. Gerratylocal=$_local 79*203027b2SSimon J. Gerraty 807e1c014aSSimon J. Gerraty 817e1c014aSSimon J. Gerraty## 827e1c014aSSimon J. Gerraty# hooks_add_all list func ... 837e1c014aSSimon J. Gerraty# 847e1c014aSSimon J. Gerraty# add "func"s to "list" regardless 857e1c014aSSimon J. Gerraty# 867e1c014aSSimon J. Gerratyhooks_add_all() { 87*203027b2SSimon J. Gerraty eval $_local __h 887e1c014aSSimon J. Gerraty __h=$1; shift 897e1c014aSSimon J. Gerraty case "$1" in 907e1c014aSSimon J. Gerraty --first) 917e1c014aSSimon J. Gerraty shift 927e1c014aSSimon J. Gerraty eval "$__h=\"$* \$$__h\"" 937e1c014aSSimon J. Gerraty ;; 947e1c014aSSimon J. Gerraty *) eval "$__h=\"\$$__h $*\"";; 957e1c014aSSimon J. Gerraty esac 967e1c014aSSimon J. Gerraty} 977e1c014aSSimon J. Gerraty 987e1c014aSSimon J. Gerraty## 997e1c014aSSimon J. Gerraty# hooks_add_once list func ... 1007e1c014aSSimon J. Gerraty# 1017e1c014aSSimon J. Gerraty# add "func"s to "list" if not already there 1027e1c014aSSimon J. Gerraty# 1037e1c014aSSimon J. Gerratyhooks_add_once() { 104*203027b2SSimon J. Gerraty eval $_local __h __hh __first 1057e1c014aSSimon J. Gerraty __h=$1; shift 1067e1c014aSSimon J. Gerraty case "$1" in 1077e1c014aSSimon J. Gerraty --first) shift; __first=:;; 1087e1c014aSSimon J. Gerraty *) __first=;; 1097e1c014aSSimon J. Gerraty esac 1107e1c014aSSimon J. Gerraty eval "__hh=\$$__h" 1117e1c014aSSimon J. Gerraty while [ $# -gt 0 ] 1127e1c014aSSimon J. Gerraty do 1137e1c014aSSimon J. Gerraty : __hh="$__hh" 1="$1" 1147e1c014aSSimon J. Gerraty case "$__first $__hh " in 1157e1c014aSSimon J. Gerraty *" $1 "*) ;; # dupe 1167e1c014aSSimon J. Gerraty :*) __hh="$1 $__hh";; 1177e1c014aSSimon J. Gerraty *) __hh="$__hh $1";; 1187e1c014aSSimon J. Gerraty esac 1197e1c014aSSimon J. Gerraty shift 1207e1c014aSSimon J. Gerraty done 1217e1c014aSSimon J. Gerraty eval "$__h=\"$__hh\"" 1227e1c014aSSimon J. Gerraty} 1237e1c014aSSimon J. Gerraty 1247e1c014aSSimon J. Gerraty## 1257e1c014aSSimon J. Gerraty# hooks_add_default_set [--]{all,once} 1267e1c014aSSimon J. Gerraty# 1277e1c014aSSimon J. Gerraty# change the default method of hooks_add 1287e1c014aSSimon J. Gerraty# 1297e1c014aSSimon J. Gerratyhooks_add_default_set() { 1307e1c014aSSimon J. Gerraty case "$1" in 1317e1c014aSSimon J. Gerraty once|--once) HOOKS_ADD_DEFAULT=once;; 1327e1c014aSSimon J. Gerraty *) HOOKS_ADD_DEFAULT=all;; 1337e1c014aSSimon J. Gerraty esac 1347e1c014aSSimon J. Gerraty} 1357e1c014aSSimon J. Gerraty 1367e1c014aSSimon J. Gerraty## 1377e1c014aSSimon J. Gerraty# hooks_add [--{all,once}] list func ... 1387e1c014aSSimon J. Gerraty# 1397e1c014aSSimon J. Gerraty# add "func"s to "list" 1407e1c014aSSimon J. Gerraty# 1417e1c014aSSimon J. Gerraty# If '--once' use hooks_add_once, 1427e1c014aSSimon J. Gerraty# default is hooks_add_all. 1437e1c014aSSimon J. Gerraty# 1447e1c014aSSimon J. Gerratyhooks_add() { 1457e1c014aSSimon J. Gerraty case "$1" in 1467e1c014aSSimon J. Gerraty --all) shift; hooks_add_all "$@";; 1477e1c014aSSimon J. Gerraty --once) shift; hooks_add_once "$@";; 1487e1c014aSSimon J. Gerraty *) hooks_add_${HOOKS_ADD_DEFAULT:-all} "$@";; 1497e1c014aSSimon J. Gerraty esac 1507e1c014aSSimon J. Gerraty} 1517e1c014aSSimon J. Gerraty 1527e1c014aSSimon J. Gerraty## 1537e1c014aSSimon J. Gerraty# hooks_get [--lifo] list [LIFO] 1547e1c014aSSimon J. Gerraty# 1557e1c014aSSimon J. Gerraty# return $list 1567e1c014aSSimon J. Gerraty# 1577e1c014aSSimon J. Gerratyhooks_get() { 158*203027b2SSimon J. Gerraty eval $_local __h __h2 e __l 1597e1c014aSSimon J. Gerraty case "$1" in 1607e1c014aSSimon J. Gerraty --lifo) __l=LIFO; shift;; 1617e1c014aSSimon J. Gerraty esac 1627e1c014aSSimon J. Gerraty eval "__h=\$$1" 1637e1c014aSSimon J. Gerraty case "$__l$2" in 1647e1c014aSSimon J. Gerraty LIFO*) 1657e1c014aSSimon J. Gerraty __h2="$__h" 1667e1c014aSSimon J. Gerraty __h= 1677e1c014aSSimon J. Gerraty for e in $__h2 1687e1c014aSSimon J. Gerraty do 1697e1c014aSSimon J. Gerraty __h="$e $__h" 1707e1c014aSSimon J. Gerraty done 1717e1c014aSSimon J. Gerraty ;; 1727e1c014aSSimon J. Gerraty esac 1737e1c014aSSimon J. Gerraty echo "$__h" 1747e1c014aSSimon J. Gerraty} 1757e1c014aSSimon J. Gerraty 1767e1c014aSSimon J. Gerraty## 1777e1c014aSSimon J. Gerraty# hooks_has list func 1787e1c014aSSimon J. Gerraty# 1797e1c014aSSimon J. Gerraty# is func in $list ? 1807e1c014aSSimon J. Gerraty# 1817e1c014aSSimon J. Gerratyhooks_has() { 182*203027b2SSimon J. Gerraty eval $_local __h 1837e1c014aSSimon J. Gerraty eval "__h=\$$1" 1847e1c014aSSimon J. Gerraty case " $__h " in 1857e1c014aSSimon J. Gerraty *" $1 "*) return 0;; 1867e1c014aSSimon J. Gerraty esac 1877e1c014aSSimon J. Gerraty return 1 1887e1c014aSSimon J. Gerraty} 1897e1c014aSSimon J. Gerraty 1907e1c014aSSimon J. Gerraty## 1917e1c014aSSimon J. Gerraty# hooks_run [--all] [--lifo] list [LIFO] [args] 1927e1c014aSSimon J. Gerraty# 1937e1c014aSSimon J. Gerraty# pass "args" to each function in "list" 1947e1c014aSSimon J. Gerraty# Without '--all'; if any return non-zero return that immediately 1957e1c014aSSimon J. Gerraty# 1967e1c014aSSimon J. Gerratyhooks_run() { 197*203027b2SSimon J. Gerraty eval $_local __a e __h __hl __h2 __l 1987e1c014aSSimon J. Gerraty __a=return 1997e1c014aSSimon J. Gerraty __l= 2007e1c014aSSimon J. Gerraty 2017e1c014aSSimon J. Gerraty while : 2027e1c014aSSimon J. Gerraty do 2037e1c014aSSimon J. Gerraty case "$1" in 2047e1c014aSSimon J. Gerraty --all) __a=:; shift;; 2057e1c014aSSimon J. Gerraty --lifo) __l=$1; shift;; 2067e1c014aSSimon J. Gerraty *) break;; 2077e1c014aSSimon J. Gerraty esac 2087e1c014aSSimon J. Gerraty done 2097e1c014aSSimon J. Gerraty __hl=$1; shift 2107e1c014aSSimon J. Gerraty case "$1" in 2117e1c014aSSimon J. Gerraty LIFO) __l=--lifo; shift;; 2127e1c014aSSimon J. Gerraty esac 2137e1c014aSSimon J. Gerraty __h=`hooks_get $__l $__hl` 2147e1c014aSSimon J. Gerraty for e in $__h 2157e1c014aSSimon J. Gerraty do 2167e1c014aSSimon J. Gerraty $e "$@" || $__a $? 2177e1c014aSSimon J. Gerraty done 2187e1c014aSSimon J. Gerraty} 2197e1c014aSSimon J. Gerraty 2207e1c014aSSimon J. Gerraty## 2217e1c014aSSimon J. Gerraty# hooks_run_all [--lifo] list [LIFO] [args] 2227e1c014aSSimon J. Gerraty# 2237e1c014aSSimon J. Gerraty# pass "args" to each function in "list" 2247e1c014aSSimon J. Gerraty# 2257e1c014aSSimon J. Gerratyhooks_run_all() { 2267e1c014aSSimon J. Gerraty hooks_run --all "$@" 2277e1c014aSSimon J. Gerraty} 2287e1c014aSSimon J. Gerraty 2297e1c014aSSimon J. Gerraty## 2307e1c014aSSimon J. Gerraty# add_hooks,run_hooks[_all] aliases 2317e1c014aSSimon J. Gerraty# 2327e1c014aSSimon J. Gerratyadd_hooks() { 2337e1c014aSSimon J. Gerraty hooks_add "$@" 2347e1c014aSSimon J. Gerraty} 2357e1c014aSSimon J. Gerraty 2367e1c014aSSimon J. Gerratyrun_hooks() { 2377e1c014aSSimon J. Gerraty hooks_run "$@" 2387e1c014aSSimon J. Gerraty} 2397e1c014aSSimon J. Gerraty 2407e1c014aSSimon J. Gerratyrun_hooks_all() { 2417e1c014aSSimon J. Gerraty hooks_run --all "$@" 2427e1c014aSSimon J. Gerraty} 2437e1c014aSSimon J. Gerraty 2447e1c014aSSimon J. Gerraty 2457e1c014aSSimon J. Gerratycase /$0 in 2467e1c014aSSimon J. Gerraty*/hooks.sh) 2477e1c014aSSimon J. Gerraty # simple unit-test 2487e1c014aSSimon J. Gerraty list=HOOKS 2497e1c014aSSimon J. Gerraty flags= 2507e1c014aSSimon J. Gerraty while : 2517e1c014aSSimon J. Gerraty do 2527e1c014aSSimon J. Gerraty : 1=$1 2537e1c014aSSimon J. Gerraty case "$1" in 2547e1c014aSSimon J. Gerraty HOOKS|*hooks) list=$1; shift;; 2557e1c014aSSimon J. Gerraty --*) flags="$flags $1"; shift;; 2567e1c014aSSimon J. Gerraty *) break;; 2577e1c014aSSimon J. Gerraty esac 2587e1c014aSSimon J. Gerraty done 2597e1c014aSSimon J. Gerraty for f in "$@" 2607e1c014aSSimon J. Gerraty do 2617e1c014aSSimon J. Gerraty : f=$f 2627e1c014aSSimon J. Gerraty case "$f" in 2637e1c014aSSimon J. Gerraty LIFO) ;; 2647e1c014aSSimon J. Gerraty false|true) ;; 2657e1c014aSSimon J. Gerraty *) eval "$f() { echo This is $f; }";; 2667e1c014aSSimon J. Gerraty esac 2677e1c014aSSimon J. Gerraty done 2687e1c014aSSimon J. Gerraty echo hooks_add $flags $list "$@" 2697e1c014aSSimon J. Gerraty hooks_add $flags $list "$@" 2707e1c014aSSimon J. Gerraty echo hooks_run $list 2717e1c014aSSimon J. Gerraty hooks_run $list 2727e1c014aSSimon J. Gerraty echo hooks_run --all --lifo $list 2737e1c014aSSimon J. Gerraty hooks_run --all --lifo $list 2747e1c014aSSimon J. Gerraty echo hooks_run $list LIFO 2757e1c014aSSimon J. Gerraty hooks_run $list LIFO 2767e1c014aSSimon J. Gerraty ;; 2777e1c014aSSimon J. Gerratyesac 278