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