157718be8SEnji Cooper#!/bin/sh 257718be8SEnji Cooper# 3*640235e2SEnji Cooper# $NetBSD: scoped_command,v 1.2 2016/03/27 14:57:50 christos Exp $ 457718be8SEnji Cooper# 557718be8SEnji Cooper# Copyright (c) 2014 The NetBSD Foundation, Inc. 657718be8SEnji Cooper# All rights reserved. 757718be8SEnji Cooper# 857718be8SEnji Cooper# This code is derived from software contributed to The NetBSD Foundation 957718be8SEnji Cooper# by Jarmo Jaakkola. 1057718be8SEnji Cooper# 1157718be8SEnji Cooper# Redistribution and use in source and binary forms, with or without 1257718be8SEnji Cooper# modification, are permitted provided that the following conditions 1357718be8SEnji Cooper# are met: 1457718be8SEnji Cooper# 1. Redistributions of source code must retain the above copyright 1557718be8SEnji Cooper# notice, this list of conditions and the following disclaimer. 1657718be8SEnji Cooper# 2. Redistributions in binary form must reproduce the above copyright 1757718be8SEnji Cooper# notice, this list of conditions and the following disclaimer in the 1857718be8SEnji Cooper# documentation and/or other materials provided with the distribution. 1957718be8SEnji Cooper# 2057718be8SEnji Cooper# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 2157718be8SEnji Cooper# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2257718be8SEnji Cooper# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2357718be8SEnji Cooper# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 2457718be8SEnji Cooper# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2557718be8SEnji Cooper# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2657718be8SEnji Cooper# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2757718be8SEnji Cooper# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2857718be8SEnji Cooper# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2957718be8SEnji Cooper# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3057718be8SEnji Cooper# POSSIBILITY OF SUCH DAMAGE. 3157718be8SEnji Cooper# 3257718be8SEnji Cooper 33*640235e2SEnji Cooper: ${TEST_SH:=/bin/sh} 34*640235e2SEnji Cooper 35*640235e2SEnji Coopersane_sh() 36*640235e2SEnji Cooper{ 37*640235e2SEnji Cooper set -- ${TEST_SH} 38*640235e2SEnji Cooper case "$#" in 39*640235e2SEnji Cooper (0) set /bin/sh;; 40*640235e2SEnji Cooper (1|2) ;; 41*640235e2SEnji Cooper (*) set "$1";; # Just ignore options if we cannot make them work 42*640235e2SEnji Cooper esac 43*640235e2SEnji Cooper 44*640235e2SEnji Cooper case "$1" in 45*640235e2SEnji Cooper /*) TEST_SH="$1${2+ }$2";; 46*640235e2SEnji Cooper ./*) TEST_SH="${PWD}${1#.}${2+ }$2";; 47*640235e2SEnji Cooper */*) TEST_SH="${PWD}/$1${2+ }$2";; 48*640235e2SEnji Cooper *) TEST_SH="$( command -v "$1" )${2+ }$2";; 49*640235e2SEnji Cooper esac 50*640235e2SEnji Cooper} 51*640235e2SEnji Cooper 52*640235e2SEnji Coopersane_sh 53*640235e2SEnji Cooper 5457718be8SEnji Cooperset -e 5557718be8SEnji Cooper 5657718be8SEnji Cooper# USAGE: 5757718be8SEnji Cooper# scoped_command scope cmd msg var_suffix 5857718be8SEnji Cooper# 5957718be8SEnji Cooper# Write to stdout a piece of Bourne Shell script with _cmd_ in specific 6057718be8SEnji Cooper# _scope_. The execution of _cmd_ is bracketed by prints of "before _msg_" 6157718be8SEnji Cooper# and "after _msg_, return value ${?}". If the generated script uses 6257718be8SEnji Cooper# variables, __var_suffix_ is appended to their names to allow nesting of 6357718be8SEnji Cooper# scripts generated this way. 6457718be8SEnji Cooper# 6557718be8SEnji Cooper# _scope_ should be one of: case, compound, file, for, func, subshell, 6657718be8SEnji Cooper# until, while. 6757718be8SEnji Cooper# _cmd_ is the command line to execute. Remember proper quoting! 6857718be8SEnji Cooper# _msg_ is text that will be used inside single quotes. 6957718be8SEnji Cooper# _var_suffix_ is a syntactically valid identifier name. 7057718be8SEnji Cooper 7157718be8SEnji Cooper# don't rely on command lists (';') 7257718be8SEnji Coopercmd="echo 'before ${3}' 7357718be8SEnji Cooper${2} 7457718be8SEnji Cooperecho 'after ${3}, return value:' ${?}" 7557718be8SEnji Cooper 76*640235e2SEnji Cooperecho "#!${TEST_SH}" 7757718be8SEnji Cooper 7857718be8SEnji Cooper[ 'func' = "${1}" ] && cat <<EOF 7957718be8SEnji Cooperfunc() 8057718be8SEnji Cooper{ 8157718be8SEnji Cooper echo 'before ${3}' 8257718be8SEnji Cooper \${1} 8357718be8SEnji Cooper echo 'after ${3}' 8457718be8SEnji Cooper} 8557718be8SEnji Cooper 8657718be8SEnji Cooperecho 'before function' 8757718be8SEnji Cooperfunc "${2}" "${3}" # don't rely on 'shift' 8857718be8SEnji Cooperecho 'after function' 8957718be8SEnji CooperEOF 9057718be8SEnji Cooper 9157718be8SEnji Cooper[ 'case' = "${1}" ] && cat <<EOF 9257718be8SEnji Cooperecho 'before case' 9357718be8SEnji Coopercase 'a' in 9457718be8SEnji Cooper a) ${cmd};; 9557718be8SEnji Cooperesac 9657718be8SEnji Cooperecho 'after case' 9757718be8SEnji CooperEOF 9857718be8SEnji Cooper 9957718be8SEnji Cooper[ 'file' = "${1}" ] && cat <<EOF 10057718be8SEnji Cooper${cmd} 10157718be8SEnji CooperEOF 10257718be8SEnji Cooper 10357718be8SEnji Cooper[ 'while' = "${1}" ] && cat <<EOF 10457718be8SEnji Cooperecho 'before while' 10557718be8SEnji Coopercond_${4}='true true false' 10657718be8SEnji Cooperwhile \${cond_${4}} 10757718be8SEnji Cooperdo 10857718be8SEnji Cooper cond_${4}="\${cond_${4}#* }" 10957718be8SEnji Cooper ${cmd} 11057718be8SEnji Cooperdone 11157718be8SEnji Cooperecho 'after while' 11257718be8SEnji CooperEOF 11357718be8SEnji Cooper 11457718be8SEnji Cooper[ 'until' = "${1}" ] && cat <<EOF 11557718be8SEnji Cooperecho 'before until' 11657718be8SEnji Coopercond_${4}='false false true' 11757718be8SEnji Cooperuntil \${cond_${4}} 11857718be8SEnji Cooperdo 11957718be8SEnji Cooper cond_${4}="\${cond_${4}#* }" 12057718be8SEnji Cooper ${cmd} 12157718be8SEnji Cooperdone 12257718be8SEnji Cooperecho 'after until' 12357718be8SEnji CooperEOF 12457718be8SEnji Cooper 12557718be8SEnji Cooper[ 'for' = "${1}" ] && cat <<EOF 12657718be8SEnji Cooperecho 'before for' 12757718be8SEnji Cooperfor i_${4} in 1 2 12857718be8SEnji Cooperdo 12957718be8SEnji Cooper ${cmd} 13057718be8SEnji Cooperdone 13157718be8SEnji Cooperecho 'after for' 13257718be8SEnji CooperEOF 13357718be8SEnji Cooper 13457718be8SEnji Cooper[ 'subshell' = "${1}" ] && cat <<EOF 13557718be8SEnji Cooper( 13657718be8SEnji Cooper echo 'subshell start' 13757718be8SEnji Cooper ${cmd} 13857718be8SEnji Cooper echo 'subshell end' 13957718be8SEnji Cooper) 14057718be8SEnji CooperEOF 14157718be8SEnji Cooper 14257718be8SEnji Cooper[ 'compound' = "${1}" ] && cat <<EOF 14357718be8SEnji Cooper{ 14457718be8SEnji Cooper echo 'compound start' 14557718be8SEnji Cooper ${cmd}; 14657718be8SEnji Cooper echo 'compound end' 14757718be8SEnji Cooper} 14857718be8SEnji CooperEOF 14957718be8SEnji Cooper 15057718be8SEnji Cooperexit 0 151