6324 Add an `ndp' tool for manipulating the neighbors table
[illumos-gate.git] / usr / src / tools / scripts / ws.sh
blob3d9084c42845485a9c5f8fcf8337c64716fe9e6c
1 #!/bin/ksh -p
3 # CDDL HEADER START
5 # The contents of this file are subject to the terms of the
6 # Common Development and Distribution License (the "License").
7 # You may not use this file except in compliance with the License.
9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 # or http://www.opensolaris.org/os/licensing.
11 # See the License for the specific language governing permissions
12 # and limitations under the License.
14 # When distributing Covered Code, include this CDDL HEADER in each
15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 # If applicable, add the following below this CDDL HEADER, with the
17 # fields enclosed by brackets "[]" replaced with your own identifying
18 # information: Portions Copyright [yyyy] [name of copyright owner]
20 # CDDL HEADER END
23 # Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 # Use is subject to license terms.
26 # Copyright 2011 Nexenta Systems, Inc. All rights reserved.
28 # This script sets up the environment variables for a SunOS
29 # codemgr workspace and spawns a shell with the environment
30 # setup.
32 # The following Environment variables are set:
33 # CODEMGR_WS
34 # ONBLD_DIR
35 # SRC
36 # TSRC
37 # ROOT
38 # PARENT_ROOT
39 # MACH
40 # MAKEFLAGS
41 # ENVCPPFLAGS{1-4}
42 # ENVLDLIBS{1-3}
44 # The MAKEFLAGS environment variable is set to force make
45 # to read default make variables from the environment.
47 # Workspace names can be specified in two forms: pathname
48 # and hostname:pathname. If the hostname:pathname form is used
49 # the script accesses the environment through the /net automounter
50 # map.
54 # function to produce a pathname from a workspace name or subdirectory.
55 # The workspace name can have hostname:pathname format.
58 fmtwsname()
60 awk -F: '$1 != $0 { print "/net/"$1$2 } \
61 $1 == $0 { print $0 }'
65 # Return a valid proto area, if one exists.
67 check_proto()
69 if [[ -z $1 ]]; then
70 return
73 if [[ "$SCM_MODE" = "teamware" ]]; then
74 # Check for problematic parent specification and adjust
75 proto=`echo $1|fmtwsname`
76 echo "${proto}/root_${MACH}"
77 elif [[ "$SCM_MODE" = "mercurial" ]]; then
78 proto=$1
80 # If the proto is a local repository then we can use it
81 # to point to the parents proto area. Don't bother to
82 # check if it exists or not, we never did for Teamware,
83 # since it might appear later anyway.
85 if [[ "${proto##ssh://}" == "$proto" && \
86 "${proto##http://}" == "$proto" && \
87 "${proto##https://}" == "$proto" ]]; then
88 echo "${proto}/root_${MACH}"
90 elif [[ "$SCM_MODE" = "git" ]]; then
92 # For git, we make no attempt to deal with the possibility of
93 # remote parent workspaces because, in the protodefs file, we
94 # don't actually acknowledge the concept of a parent workspace
95 # at all, in keeping with the rest of our git support.
97 echo "${1}/root_${MACH}"
101 cleanup_env()
103 # keep the env. clean when returning
104 unset setenv osbld_flag os_rev wsosdir protofile wsname ofs proto \
105 pwd parent PROTO1 PROTO2 PROTO3 tmpwsname
106 return 0
109 if [[ "$1" = "-e" ]]; then
110 setenv=true
111 shift
112 else
113 setenv=false
116 WHICH_SCM=$(/bin/dirname $(whence $0))/which_scm
117 if [[ ! -x $WHICH_SCM ]]; then
118 WHICH_SCM=which_scm
122 # No workspace/repository path was given, so try and detect one from our
123 # current directory we're in
125 if [[ $# -lt 1 ]]; then
126 if env CODEMGR_WS="" $WHICH_SCM | read SCM_MODE tmpwsname && \
127 [[ $SCM_MODE != unknown ]]; then
128 echo "Defaulting to $SCM_MODE repository $tmpwsname"
129 else
130 echo "usage: ws [-e] [workspace_name]" >&2
131 if $setenv; then
132 cleanup_env
133 return 1
134 else
135 exit 1
138 else
140 # A workspace/repository path was passed in, grab it and pop
141 # it off the stack
143 tmpwsname=$1
144 shift
148 # This variable displays the nested activations of workspaces.
149 # This is done here to get the exact name the user entered.
151 WS_STACK="$tmpwsname $WS_STACK"; export WS_STACK
154 # Set the workspace name and unset tmpwsname (as we reuse it later)
156 wsname=`echo $tmpwsname|fmtwsname`
157 unset tmpwsname
160 # Checking for CODEMGR_WSPATH
162 if [[ -n ${CODEMGR_WSPATH} && ( ! -d $wsname ) && \
163 ( `expr "$wsname" : "\/"` = "0" ) ]]
164 then
165 ofs=$IFS
166 IFS=": "
167 for i in $CODEMGR_WSPATH
169 if [[ -d ${i}/${wsname} ]]; then
170 wsname=${i}/${wsname}
171 break
173 done
174 IFS=$ofs
178 # to translate it to an absolute pathname. We need an
179 # absolute pathname in order to set CODEMGR_WS.
181 if [[ `expr "$wsname" : "\/"` = "0" ]]
182 then
183 pwd=`pwd`
184 wsname="$pwd/$wsname"
188 # Check to see if this is a valid workspace
190 if [[ ! -d $wsname ]]; then
191 echo "$wsname . . . no such directory" >&2
192 if $setenv; then
193 cleanup_env
194 return 1
195 else
196 exit 1
201 # This catches the case of a passed in workspace path
202 # Check which type of SCM is in use by $wsname.
204 (cd $wsname && env CODEMGR_WS="" $WHICH_SCM) | read SCM_MODE tmpwsname
205 if [[ $? != 0 || "$SCM_MODE" == unknown ]]; then
206 echo "Error: Unable to detect a supported SCM repository in $wsname"
207 if $setenv; then
208 cleanup_env
209 return 1
210 else
211 exit 1
215 wsname=$tmpwsname
216 CODEMGR_WS=$wsname ; export CODEMGR_WS
217 SRC=$wsname/usr/src; export SRC
218 TSRC=$wsname/usr/ontest; export TSRC
220 if [[ "$SCM_MODE" = "teamware" && -d ${wsname}/Codemgr_wsdata ]]; then
221 CM_DATA="Codemgr_wsdata"
222 wsosdir=$CODEMGR_WS/$CM_DATA/sunos
223 protofile=$wsosdir/protodefs
224 elif [[ "$SCM_MODE" = "mercurial" && -d ${wsname}/.hg ]]; then
225 CM_DATA=".hg"
226 wsosdir=$CODEMGR_WS/$CM_DATA
227 protofile=$wsosdir/org.opensolaris.protodefs
228 elif [[ "$SCM_MODE" = "git" && -d ${wsname}/.git ]]; then
229 CM_DATA=".git"
230 wsosdir=$CODEMGR_WS/$CM_DATA
231 protofile=$wsosdir/org.opensolaris.protodefs
232 else
233 echo "$wsname is not a supported workspace; type is $SCM_MODE" >&2
234 if $setenv; then
235 cleanup_env
236 return 1
237 else
238 exit 1
242 MACH=`uname -p`
244 if [[ ! -f $protofile ]]; then
245 if [[ ! -w $CODEMGR_WS/$CM_DATA ]]; then
247 # The workspace doesn't have a protodefs file and I am
248 # unable to create one. Tell user and use /tmp instead.
250 echo "Unable to create the proto defaults file ($protofile)."
252 # Just make one in /tmp
253 wsosdir=/tmp
254 protofile=$wsosdir/protodefs
257 if [[ ! -d $wsosdir ]]; then
258 mkdir $wsosdir
261 cat << PROTOFILE_EoF > $protofile
262 #!/bin/sh
264 # Set default proto areas for this workspace
265 # NOTE: This file was initially automatically generated.
267 # Feel free to edit this file. If this file is removed
268 # it will be rebuilt containing default values.
270 # The variable CODEMGR_WS is available to this script.
272 # PROTO1 is the first proto area searched and is typically set
273 # to a proto area associated with the workspace. The ROOT
274 # environment variable is set to the same as PROTO1. If you
275 # will be doing make installs this proto area needs to be writable.
277 # PROTO2 and PROTO3 are set to proto areas to search before the
278 # search proceeds to the local machine or the proto area specified by
279 # TERMPROTO.
281 # TERMPROTO (if specified) is the last place searched. If
282 # TERMPROTO is not specified the search will end at the local
283 # machine.
286 PROTO1=\$CODEMGR_WS/proto
287 PROTOFILE_EoF
289 if [[ "$SCM_MODE" = "teamware" ]]; then
290 cat << PROTOFILE_EoF >> $protofile
291 if [[ -f "\$CODEMGR_WS/Codemgr_wsdata/parent" ]]; then
293 # If this workspace has an codemgr parent then set PROTO2 to
294 # point to the parents proto space.
296 parent=\`workspace parent \$CODEMGR_WS\`
297 if [[ -n \$parent ]]; then
298 PROTO2=\$parent/proto
301 PROTOFILE_EoF
302 elif [[ "$SCM_MODE" = "mercurial" ]]; then
303 cat << PROTOFILE_EoF >> $protofile
304 parent=\`(cd \$CODEMGR_WS && hg path default 2>/dev/null)\`
305 if [[ \$? -eq 0 && -n \$parent ]]; then
306 [[ -n \$(check_proto \$parent/proto) ]] && PROTO2=\$parent/proto
308 PROTOFILE_EoF
312 . $protofile
314 # This means you don't have to type make -e all of the time
316 MAKEFLAGS=e; export MAKEFLAGS
319 # Set up the environment variables
321 ROOT=/proto/root_${MACH} # default
323 ENVCPPFLAGS1=
324 ENVCPPFLAGS2=
325 ENVCPPFLAGS3=
326 ENVCPPFLAGS4=
327 ENVLDLIBS1=
328 ENVLDLIBS2=
329 ENVLDLIBS3=
331 PROTO1=`check_proto $PROTO1`
332 if [[ -n "$PROTO1" ]]; then # first proto area specifed
333 ROOT=$PROTO1
334 ENVCPPFLAGS1=-I$ROOT/usr/include
335 export ENVCPPFLAGS1
336 ENVLDLIBS1="-L$ROOT/lib -L$ROOT/usr/lib"
337 export ENVLDLIBS1
339 PROTO2=`check_proto $PROTO2`
340 if [[ -n "$PROTO2" ]]; then # second proto area specifed
341 ENVCPPFLAGS2=-I$PROTO2/usr/include
342 export ENVCPPFLAGS2
343 ENVLDLIBS2="-L$PROTO2/lib -L$PROTO2/usr/lib"
344 export ENVLDLIBS2
346 PROTO3=`check_proto $PROTO3`
347 if [[ -n "$PROTO3" ]]; then # third proto area specifed
348 ENVCPPFLAGS3=-I$PROTO3/usr/include
349 export ENVCPPFLAGS3
350 ENVLDLIBS3="-L$PROTO3/lib -L$PROTO3/usr/lib"
351 export ENVLDLIBS3
356 export ROOT
358 if [[ -n "$TERMPROTO" ]]; then # fallback area specifed
359 TERMPROTO=`check_proto $TERMPROTO`
360 ENVCPPFLAGS4="-Y I,$TERMPROTO/usr/include"
361 export ENVCPPFLAGS4
362 ENVLDLIBS3="$ENVLDLIBS3 -Y P,$TERMPROTO/lib:$TERMPROTO/usr/lib"
363 export ENVLDLIBS3
366 osbld_flag=0
368 if [[ -z "$ONBLD_DIR" ]]; then
369 ONBLD_DIR=$(/bin/dirname $(whence $0))
372 if ! echo ":$PATH:" | grep ":${ONBLD_DIR}:" > /dev/null; then
373 PATH="${ONBLD_DIR}:${ONBLD_DIR}/${MACH}:${PATH}"
374 osbld_flag=1
377 export PATH
379 if [[ -n "$PROTO2" ]]; then
380 # This should point to the parent's proto
381 PARENT_ROOT=$PROTO2
382 export PARENT_ROOT
383 else
384 # Clear it in case it's already in the env.
385 PARENT_ROOT=
387 export ONBLD_DIR
388 export MACH
390 os_rev=`uname -r`
391 os_name=`uname -s`
393 if [[ $os_name != "SunOS" || `expr $os_rev : "5\."` != "2" ]]; then
395 # This is not a SunOS 5.x machine - something is wrong
397 echo "***WARNING: this script is meant to be run on SunOS 5.x."
398 echo " This machine appears to be running: $os_name $os_rev"
401 echo ""
402 echo "Workspace : $wsname"
403 if [[ -n "$parent" ]]; then
404 echo "Workspace Parent : $parent"
406 echo "Proto area (\$ROOT) : $ROOT"
407 if [[ -n "$PARENT_ROOT" ]]; then
408 echo "Parent proto area (\$PARENT_ROOT) : $PARENT_ROOT"
410 echo "Root of source (\$SRC) : $SRC"
411 echo "Root of test source (\$TSRC) : $TSRC"
412 if [[ $osbld_flag = "1" ]]; then
413 echo "Prepended to PATH : $ONBLD_DIR"
415 echo "Current directory (\$PWD) : $wsname"
416 echo ""
418 cd $wsname
420 if $setenv; then
421 cleanup_env
422 else
423 exec ${SHELL:-sh} "$@"