1*0a6a1f1dSLionel Sambuc#!/bin/sh 2*0a6a1f1dSLionel Sambuc# 3*0a6a1f1dSLionel Sambuc# $NetBSD: scoped_command,v 1.1 2014/05/31 14:29:06 christos Exp $ 4*0a6a1f1dSLionel Sambuc# 5*0a6a1f1dSLionel Sambuc# Copyright (c) 2014 The NetBSD Foundation, Inc. 6*0a6a1f1dSLionel Sambuc# All rights reserved. 7*0a6a1f1dSLionel Sambuc# 8*0a6a1f1dSLionel Sambuc# This code is derived from software contributed to The NetBSD Foundation 9*0a6a1f1dSLionel Sambuc# by Jarmo Jaakkola. 10*0a6a1f1dSLionel Sambuc# 11*0a6a1f1dSLionel Sambuc# Redistribution and use in source and binary forms, with or without 12*0a6a1f1dSLionel Sambuc# modification, are permitted provided that the following conditions 13*0a6a1f1dSLionel Sambuc# are met: 14*0a6a1f1dSLionel Sambuc# 1. Redistributions of source code must retain the above copyright 15*0a6a1f1dSLionel Sambuc# notice, this list of conditions and the following disclaimer. 16*0a6a1f1dSLionel Sambuc# 2. Redistributions in binary form must reproduce the above copyright 17*0a6a1f1dSLionel Sambuc# notice, this list of conditions and the following disclaimer in the 18*0a6a1f1dSLionel Sambuc# documentation and/or other materials provided with the distribution. 19*0a6a1f1dSLionel Sambuc# 20*0a6a1f1dSLionel Sambuc# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21*0a6a1f1dSLionel Sambuc# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22*0a6a1f1dSLionel Sambuc# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23*0a6a1f1dSLionel Sambuc# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24*0a6a1f1dSLionel Sambuc# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25*0a6a1f1dSLionel Sambuc# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26*0a6a1f1dSLionel Sambuc# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27*0a6a1f1dSLionel Sambuc# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28*0a6a1f1dSLionel Sambuc# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29*0a6a1f1dSLionel Sambuc# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30*0a6a1f1dSLionel Sambuc# POSSIBILITY OF SUCH DAMAGE. 31*0a6a1f1dSLionel Sambuc# 32*0a6a1f1dSLionel Sambuc 33*0a6a1f1dSLionel Sambucset -e 34*0a6a1f1dSLionel Sambuc 35*0a6a1f1dSLionel Sambuc# USAGE: 36*0a6a1f1dSLionel Sambuc# scoped_command scope cmd msg var_suffix 37*0a6a1f1dSLionel Sambuc# 38*0a6a1f1dSLionel Sambuc# Write to stdout a piece of Bourne Shell script with _cmd_ in specific 39*0a6a1f1dSLionel Sambuc# _scope_. The execution of _cmd_ is bracketed by prints of "before _msg_" 40*0a6a1f1dSLionel Sambuc# and "after _msg_, return value ${?}". If the generated script uses 41*0a6a1f1dSLionel Sambuc# variables, __var_suffix_ is appended to their names to allow nesting of 42*0a6a1f1dSLionel Sambuc# scripts generated this way. 43*0a6a1f1dSLionel Sambuc# 44*0a6a1f1dSLionel Sambuc# _scope_ should be one of: case, compound, file, for, func, subshell, 45*0a6a1f1dSLionel Sambuc# until, while. 46*0a6a1f1dSLionel Sambuc# _cmd_ is the command line to execute. Remember proper quoting! 47*0a6a1f1dSLionel Sambuc# _msg_ is text that will be used inside single quotes. 48*0a6a1f1dSLionel Sambuc# _var_suffix_ is a syntactically valid identifier name. 49*0a6a1f1dSLionel Sambuc 50*0a6a1f1dSLionel Sambuc# don't rely on command lists (';') 51*0a6a1f1dSLionel Sambuccmd="echo 'before ${3}' 52*0a6a1f1dSLionel Sambuc${2} 53*0a6a1f1dSLionel Sambucecho 'after ${3}, return value:' ${?}" 54*0a6a1f1dSLionel Sambuc 55*0a6a1f1dSLionel Sambucecho "#!/bin/sh" 56*0a6a1f1dSLionel Sambuc 57*0a6a1f1dSLionel Sambuc[ 'func' = "${1}" ] && cat <<EOF 58*0a6a1f1dSLionel Sambucfunc() 59*0a6a1f1dSLionel Sambuc{ 60*0a6a1f1dSLionel Sambuc echo 'before ${3}' 61*0a6a1f1dSLionel Sambuc \${1} 62*0a6a1f1dSLionel Sambuc echo 'after ${3}' 63*0a6a1f1dSLionel Sambuc} 64*0a6a1f1dSLionel Sambuc 65*0a6a1f1dSLionel Sambucecho 'before function' 66*0a6a1f1dSLionel Sambucfunc "${2}" "${3}" # don't rely on 'shift' 67*0a6a1f1dSLionel Sambucecho 'after function' 68*0a6a1f1dSLionel SambucEOF 69*0a6a1f1dSLionel Sambuc 70*0a6a1f1dSLionel Sambuc[ 'case' = "${1}" ] && cat <<EOF 71*0a6a1f1dSLionel Sambucecho 'before case' 72*0a6a1f1dSLionel Sambuccase 'a' in 73*0a6a1f1dSLionel Sambuc a) ${cmd};; 74*0a6a1f1dSLionel Sambucesac 75*0a6a1f1dSLionel Sambucecho 'after case' 76*0a6a1f1dSLionel SambucEOF 77*0a6a1f1dSLionel Sambuc 78*0a6a1f1dSLionel Sambuc[ 'file' = "${1}" ] && cat <<EOF 79*0a6a1f1dSLionel Sambuc${cmd} 80*0a6a1f1dSLionel SambucEOF 81*0a6a1f1dSLionel Sambuc 82*0a6a1f1dSLionel Sambuc[ 'while' = "${1}" ] && cat <<EOF 83*0a6a1f1dSLionel Sambucecho 'before while' 84*0a6a1f1dSLionel Sambuccond_${4}='true true false' 85*0a6a1f1dSLionel Sambucwhile \${cond_${4}} 86*0a6a1f1dSLionel Sambucdo 87*0a6a1f1dSLionel Sambuc cond_${4}="\${cond_${4}#* }" 88*0a6a1f1dSLionel Sambuc ${cmd} 89*0a6a1f1dSLionel Sambucdone 90*0a6a1f1dSLionel Sambucecho 'after while' 91*0a6a1f1dSLionel SambucEOF 92*0a6a1f1dSLionel Sambuc 93*0a6a1f1dSLionel Sambuc[ 'until' = "${1}" ] && cat <<EOF 94*0a6a1f1dSLionel Sambucecho 'before until' 95*0a6a1f1dSLionel Sambuccond_${4}='false false true' 96*0a6a1f1dSLionel Sambucuntil \${cond_${4}} 97*0a6a1f1dSLionel Sambucdo 98*0a6a1f1dSLionel Sambuc cond_${4}="\${cond_${4}#* }" 99*0a6a1f1dSLionel Sambuc ${cmd} 100*0a6a1f1dSLionel Sambucdone 101*0a6a1f1dSLionel Sambucecho 'after until' 102*0a6a1f1dSLionel SambucEOF 103*0a6a1f1dSLionel Sambuc 104*0a6a1f1dSLionel Sambuc[ 'for' = "${1}" ] && cat <<EOF 105*0a6a1f1dSLionel Sambucecho 'before for' 106*0a6a1f1dSLionel Sambucfor i_${4} in 1 2 107*0a6a1f1dSLionel Sambucdo 108*0a6a1f1dSLionel Sambuc ${cmd} 109*0a6a1f1dSLionel Sambucdone 110*0a6a1f1dSLionel Sambucecho 'after for' 111*0a6a1f1dSLionel SambucEOF 112*0a6a1f1dSLionel Sambuc 113*0a6a1f1dSLionel Sambuc[ 'subshell' = "${1}" ] && cat <<EOF 114*0a6a1f1dSLionel Sambuc( 115*0a6a1f1dSLionel Sambuc echo 'subshell start' 116*0a6a1f1dSLionel Sambuc ${cmd} 117*0a6a1f1dSLionel Sambuc echo 'subshell end' 118*0a6a1f1dSLionel Sambuc) 119*0a6a1f1dSLionel SambucEOF 120*0a6a1f1dSLionel Sambuc 121*0a6a1f1dSLionel Sambuc[ 'compound' = "${1}" ] && cat <<EOF 122*0a6a1f1dSLionel Sambuc{ 123*0a6a1f1dSLionel Sambuc echo 'compound start' 124*0a6a1f1dSLionel Sambuc ${cmd}; 125*0a6a1f1dSLionel Sambuc echo 'compound end' 126*0a6a1f1dSLionel Sambuc} 127*0a6a1f1dSLionel SambucEOF 128*0a6a1f1dSLionel Sambuc 129*0a6a1f1dSLionel Sambucexit 0 130