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