1 #
   2 # CDDL HEADER START
   3 #
   4 # The contents of this file are subject to the terms of the
   5 # Common Development and Distribution License (the "License").
   6 # You may not use this file except in compliance with the License.
   7 #
   8 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9 # or http://www.opensolaris.org/os/licensing.
  10 # See the License for the specific language governing permissions
  11 # and limitations under the License.
  12 #
  13 # When distributing Covered Code, include this CDDL HEADER in each
  14 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15 # If applicable, add the following below this CDDL HEADER, with the
  16 # fields enclosed by brackets "[]" replaced with your own identifying
  17 # information: Portions Copyright [yyyy] [name of copyright owner]
  18 #
  19 # CDDL HEADER END
  20 #
  21 
  22 #
  23 # Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  24 # Use is subject to license terms.
  25 #
  26 
  27 # Global variable used only in this file
  28 typeset _CMD_VAR=""
  29 typeset _CMD_ARR_NAME=""
  30 typeset _INDEX_ARR=""
  31 typeset _TMP_INDEX_ARR=""
  32 typeset _NEW_INDEX_ARR=""
  33 
  34 #
  35 # NAME
  36 #       extend
  37 #
  38 # DESCRIPTION
  39 #       Iterate "${_INDEX_ARR}", parse and extend items in 
  40 #       the command array.
  41 #       
  42 # ARGUMENT
  43 #       Use global variable _INDEX_ARR
  44 #       
  45 # RETURN
  46 #       No return 
  47 #
  48 function extend
  49 {
  50         typeset -i i
  51         _NEW_INDEX_ARR=""
  52 
  53         for i in ${_INDEX_ARR}
  54         do
  55                 #
  56                 # split a command string into three part
  57                 # Example 
  58                 # Split
  59                 #       itadm create ^SET_1 ^SET_2 ^SET_3
  60                 # Into
  61                 #       begin="itadm create"
  62                 #       term="SET_1"
  63                 #       end="^SET_2 ^SET_3"
  64                 #
  65                 typeset part_arr
  66                 typeset begin=""
  67                 typeset term=""
  68                 typeset end=""
  69                 
  70                 #eval "echo \"\${$_CMD_ARR_NAME[\${i}]}\""
  71 
  72                 eval begin="\${$_CMD_ARR_NAME[\${i}]%%^*}"
  73                 #echo ${pre}
  74 
  75                 eval set -A part_arr \${$_CMD_ARR_NAME[\${i}]#*^}               
  76                 term="${part_arr[0]}"
  77 
  78                 typeset -i j=1
  79                 while [[ ${j} -lt ${#part_arr[@]} ]]
  80                 do
  81                         end="${end} ${part_arr[${j}]}"
  82                         (( j+=1 ))
  83                 done
  84                 
  85                 # Call extend_term function
  86                 extend_cmd ${i} "${term}" "${begin}" "${end}"   
  87                 _NEW_INDEX_ARR="${_NEW_INDEX_ARR} ${_TMP_INDEX_ARR}"
  88         done
  89 
  90         _NEW_INDEX_ARR=`echo ${_NEW_INDEX_ARR}` 
  91 }
  92 
  93 # NAME
  94 #       extend_cmd
  95 #
  96 # DESCRIPTION
  97 #       Replace ^SET_* with the string it stand for 
  98 #
  99 # INPUT 
 100 #       $1 - index
 101 #       $2 - term
 102 #       $3 - begin
 103 #       $4 - end
 104 #       
 105 # RETURN
 106 #       void
 107 #
 108 function extend_cmd
 109 {
 110         typeset index=${1}
 111         eval "typeset cmd=\"\${${2}}\"" 
 112         typeset begin="${3}"
 113         typeset end="${4}"
 114         
 115         _TMP_INDEX_ARR=""
 116 
 117         typeset -i i=1
 118         typeset -i len=`echo ${cmd} | awk -F"|" '{print NF }'`
 119         
 120         while [[ ${i} -le ${len} ]] 
 121         do
 122                 field=`echo ${cmd} | cut -d"|" -f${i} | xargs echo`
 123 
 124                 #ignore the empty string
 125                 if [[ -z `echo ${field}` ]] ; then
 126                         (( i+=1 ))
 127                         continue
 128                 fi
 129                 if [[ "$( echo ${field} | grep NULL )" != "" ]] ; then
 130                         field=""
 131                 fi
 132 
 133                 typeset new_cmd="${begin} ${field} ${end}"
 134                 typeset next
 135 
 136                 if [[ ${i} == 1 ]] ; then
 137                         next=${index}
 138                 else
 139                         eval next=\${#$_CMD_ARR_NAME[@]}
 140                 fi
 141                 eval "$_CMD_ARR_NAME[\${next}]=\"${new_cmd}\""
 142 
 143                 #echo "${new_cmd}"
 144                 echo "${new_cmd}" | grep "\^" > /dev/null 2>&1
 145                 if [[ ${?} == 0 ]] ; then
 146                         # command line which need to be process further
 147                         _TMP_INDEX_ARR="${_TMP_INDEX_ARR} ${next}"
 148                 fi
 149                 (( i+=1 ))
 150         done
 151 }
 152 
 153 # NAME
 154 #       cmd_genreate
 155 #
 156 # DESCRIPTION
 157 #       Parse the first command line and set up
 158 #       "${_INDEX_ARR}", and start to parse the rest    
 159 #       
 160 # INPUT 
 161 #       $1 - Variable name of the command
 162 #       $2 - Variable name of the command array. Contain
 163 #            the results
 164 # RETURN
 165 #       void
 166 #
 167 function cmd_generate
 168 {
 169         _CMD_VAR="${1}" 
 170         _CMD_ARR_NAME="${2}"
 171 
 172         extend_cmd 0 "${1}" "" ""
 173         _INDEX_ARR="${_TMP_INDEX_ARR}"
 174 
 175         while [[ -n ${_INDEX_ARR} ]] 
 176         do
 177                 extend
 178                 _INDEX_ARR=`echo "${_NEW_INDEX_ARR}"`
 179         done
 180 }
 181 
 182 # NAME
 183 #       auto_generate_tp
 184 #
 185 # DESCRIPTION
 186 #       Base on the "CMD" defination, generate value of
 187 #       TET global variable "iclist" for a TC file
 188 # INPUT
 189 #       $1 - Variable name of the command
 190 #       $2 - Variable name of the command array. Contain
 191 #            the results
 192 #       $3 - Function name to call for each TP
 193 #
 194 # RETURN
 195 #       void
 196 #
 197 function auto_generate_tp
 198 {
 199         typeset cmd="${1}"
 200         typeset cmd_arr_name="${2}"
 201         typeset common_func="${3}"
 202 
 203         typeset -i i=1
 204         typeset -i len=0
 205 
 206         cmd_generate "${cmd}" "${cmd_arr_name}"                         
 207 
 208         eval len=\${#${cmd_arr_name}[@]}
 209 
 210         iclist=""
 211         while [[ ${i} -le ${len} ]] 
 212         do
 213                 iclist="${iclist} ic${i}"
 214                 eval "ic${i}=${common_func}"
 215                 (( i+=1 ))
 216         done
 217 }
 218