19462261dSchristos#!/bin/sh 29462261dSchristos# 3*62d35c4eSkre# $NetBSD: scoped_command,v 1.3 2018/12/04 09:47:25 kre Exp $ 49462261dSchristos# 59462261dSchristos# Copyright (c) 2014 The NetBSD Foundation, Inc. 69462261dSchristos# All rights reserved. 79462261dSchristos# 89462261dSchristos# This code is derived from software contributed to The NetBSD Foundation 99462261dSchristos# by Jarmo Jaakkola. 109462261dSchristos# 119462261dSchristos# Redistribution and use in source and binary forms, with or without 129462261dSchristos# modification, are permitted provided that the following conditions 139462261dSchristos# are met: 149462261dSchristos# 1. Redistributions of source code must retain the above copyright 159462261dSchristos# notice, this list of conditions and the following disclaimer. 169462261dSchristos# 2. Redistributions in binary form must reproduce the above copyright 179462261dSchristos# notice, this list of conditions and the following disclaimer in the 189462261dSchristos# documentation and/or other materials provided with the distribution. 199462261dSchristos# 209462261dSchristos# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 219462261dSchristos# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 229462261dSchristos# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 239462261dSchristos# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 249462261dSchristos# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 259462261dSchristos# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 269462261dSchristos# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 279462261dSchristos# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 289462261dSchristos# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 299462261dSchristos# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 309462261dSchristos# POSSIBILITY OF SUCH DAMAGE. 319462261dSchristos# 329462261dSchristos 3332b59da6Schristos: ${TEST_SH:=/bin/sh} 3432b59da6Schristos 3532b59da6Schristossane_sh() 3632b59da6Schristos{ 3732b59da6Schristos set -- ${TEST_SH} 3832b59da6Schristos case "$#" in 3932b59da6Schristos (0) set /bin/sh;; 4032b59da6Schristos (1|2) ;; 4132b59da6Schristos (*) set "$1";; # Just ignore options if we cannot make them work 4232b59da6Schristos esac 4332b59da6Schristos 4432b59da6Schristos case "$1" in 4532b59da6Schristos /*) TEST_SH="$1${2+ }$2";; 4632b59da6Schristos ./*) TEST_SH="${PWD}${1#.}${2+ }$2";; 4732b59da6Schristos */*) TEST_SH="${PWD}/$1${2+ }$2";; 4832b59da6Schristos *) TEST_SH="$( command -v "$1" )${2+ }$2";; 4932b59da6Schristos esac 5032b59da6Schristos} 5132b59da6Schristos 5232b59da6Schristossane_sh 5332b59da6Schristos 549462261dSchristosset -e 559462261dSchristos 569462261dSchristos# USAGE: 579462261dSchristos# scoped_command scope cmd msg var_suffix 589462261dSchristos# 599462261dSchristos# Write to stdout a piece of Bourne Shell script with _cmd_ in specific 609462261dSchristos# _scope_. The execution of _cmd_ is bracketed by prints of "before _msg_" 619462261dSchristos# and "after _msg_, return value ${?}". If the generated script uses 629462261dSchristos# variables, __var_suffix_ is appended to their names to allow nesting of 639462261dSchristos# scripts generated this way. 649462261dSchristos# 659462261dSchristos# _scope_ should be one of: case, compound, file, for, func, subshell, 669462261dSchristos# until, while. 679462261dSchristos# _cmd_ is the command line to execute. Remember proper quoting! 689462261dSchristos# _msg_ is text that will be used inside single quotes. 699462261dSchristos# _var_suffix_ is a syntactically valid identifier name. 709462261dSchristos 719462261dSchristos# don't rely on command lists (';') 729462261dSchristoscmd="echo 'before ${3}' 739462261dSchristos${2} 74*62d35c4eSkreecho 'after ${3}, return value:' \${?}" 759462261dSchristos 7632b59da6Schristosecho "#!${TEST_SH}" 779462261dSchristos 789462261dSchristos[ 'func' = "${1}" ] && cat <<EOF 799462261dSchristosfunc() 809462261dSchristos{ 819462261dSchristos echo 'before ${3}' 829462261dSchristos \${1} 839462261dSchristos echo 'after ${3}' 849462261dSchristos} 859462261dSchristos 869462261dSchristosecho 'before function' 879462261dSchristosfunc "${2}" "${3}" # don't rely on 'shift' 889462261dSchristosecho 'after function' 899462261dSchristosEOF 909462261dSchristos 919462261dSchristos[ 'case' = "${1}" ] && cat <<EOF 929462261dSchristosecho 'before case' 939462261dSchristoscase 'a' in 949462261dSchristos a) ${cmd};; 959462261dSchristosesac 969462261dSchristosecho 'after case' 979462261dSchristosEOF 989462261dSchristos 999462261dSchristos[ 'file' = "${1}" ] && cat <<EOF 1009462261dSchristos${cmd} 1019462261dSchristosEOF 1029462261dSchristos 1039462261dSchristos[ 'while' = "${1}" ] && cat <<EOF 1049462261dSchristosecho 'before while' 1059462261dSchristoscond_${4}='true true false' 1069462261dSchristoswhile \${cond_${4}} 1079462261dSchristosdo 1089462261dSchristos cond_${4}="\${cond_${4}#* }" 1099462261dSchristos ${cmd} 1109462261dSchristosdone 1119462261dSchristosecho 'after while' 1129462261dSchristosEOF 1139462261dSchristos 1149462261dSchristos[ 'until' = "${1}" ] && cat <<EOF 1159462261dSchristosecho 'before until' 1169462261dSchristoscond_${4}='false false true' 1179462261dSchristosuntil \${cond_${4}} 1189462261dSchristosdo 1199462261dSchristos cond_${4}="\${cond_${4}#* }" 1209462261dSchristos ${cmd} 1219462261dSchristosdone 1229462261dSchristosecho 'after until' 1239462261dSchristosEOF 1249462261dSchristos 1259462261dSchristos[ 'for' = "${1}" ] && cat <<EOF 1269462261dSchristosecho 'before for' 1279462261dSchristosfor i_${4} in 1 2 1289462261dSchristosdo 1299462261dSchristos ${cmd} 1309462261dSchristosdone 1319462261dSchristosecho 'after for' 1329462261dSchristosEOF 1339462261dSchristos 1349462261dSchristos[ 'subshell' = "${1}" ] && cat <<EOF 1359462261dSchristos( 1369462261dSchristos echo 'subshell start' 1379462261dSchristos ${cmd} 1389462261dSchristos echo 'subshell end' 1399462261dSchristos) 1409462261dSchristosEOF 1419462261dSchristos 1429462261dSchristos[ 'compound' = "${1}" ] && cat <<EOF 1439462261dSchristos{ 1449462261dSchristos echo 'compound start' 1459462261dSchristos ${cmd}; 1469462261dSchristos echo 'compound end' 1479462261dSchristos} 1489462261dSchristosEOF 1499462261dSchristos 1509462261dSchristosexit 0 151