xref: /illumos-gate/usr/src/tools/scripts/flg.flp.sh (revision a237e38e9161f0acd6451439d4a7dd597e66291d)
1#! /bin/ksh -p
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License, Version 1.0 only
7# (the "License").  You may not use this file except in compliance
8# with the License.
9#
10# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11# or http://www.opensolaris.org/os/licensing.
12# See the License for the specific language governing permissions
13# and limitations under the License.
14#
15# When distributing Covered Code, include this CDDL HEADER in each
16# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17# If applicable, add the following below this CDDL HEADER, with the
18# fields enclosed by brackets "[]" replaced with your own identifying
19# information: Portions Copyright [yyyy] [name of copyright owner]
20#
21# CDDL HEADER END
22#
23#
24# Copyright 1991, 2002-2003 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27# ident	"%Z%%M%	%I%	%E% SMI"
28#
29# Generates the list of source files that would get brought over with the
30# specified subtree as a result of inc.flg and req.flg files.  If no subtree
31# is named, then the current directory is assumed.
32#
33# Based loosely on ON's version of Teamware's def.dir.flp.
34#
35
36PATH=/usr/bin export PATH
37PROG=`basename $0`
38
39#
40# The CDPATH variable causes ksh's `cd' builtin to emit messages to stdout
41# under certain circumstances, which will screw up consumers of incflg()
42# (and perhaps other things as well); unset it.
43#
44unset CDPATH
45
46#
47# Print the usage message and exit with an error.
48#
49usage()
50{
51	echo "usage: $PROG [-r] [<dir>]" > /dev/stderr
52	exit 1
53}
54
55#
56# Print the provided failure message and exit with an error.
57#
58fail()
59{
60	echo $PROG: $@ > /dev/stderr
61	exit 1
62}
63
64#
65# Find the files matching the pattern specified by the first argument in the
66# directories named by the remaining arguments.  Unlike def.dir.flp, print
67# the name of the source file since we want to make a list of source files,
68# not SCCS files.
69#
70find_files()
71{
72   	pat=$1
73   	shift
74
75	for dir; do
76		if [ -d $CODEMGR_WS/$dir ]; then
77			cd $CODEMGR_WS
78    			find $dir -name "$pat" | sed -n s:/SCCS/s.:/:p | prpath
79 			cd - > /dev/null
80		fi
81	done
82}
83
84#
85# Echo the filename if it exists in the workspace.
86#
87echo_file()
88{
89	[ -f $CODEMGR_WS/$1 ] && echo $1 | prpath
90}
91
92#
93# Source the named script, specified as either a full path or a path relative
94# to $CODEMGR_WS.  Although def.dir.flp allows for situations in which the
95# script is actually executed (rather than sourced), this feature has never
96# been used in ON, since it precludes use of echo_file() and find_files().
97#
98exec_file()
99{
100	if [ "${1##/}" = "$1" ]; then
101		. $CODEMGR_WS/$1
102	else
103		. $1
104	fi
105}
106
107#
108# Iterate up through all directories below the named directory, and
109# execute any inc.flg's that may exist.
110#
111incflg()
112{
113	cd $1
114	for i in * .*; 	do
115    		case $i in
116		'*'|.|..)
117			;;
118		inc.flg)
119			exec_file $1/$i
120			;;
121		*)
122			if [ -d $i -a ! -h $i ]; then
123				incflg $1/$i
124				cd $1
125			fi
126			;;
127		esac
128	done
129}
130
131#
132# Convert the absolute pathnames named on input to relative pathnames (if
133# necessary) and print them.
134#
135prpath()
136{
137	reltree=${CURTREE##$CODEMGR_WS/}
138
139	while read srcfile; do
140		if [ "$RELPATHS" != y ]; then
141			echo $srcfile
142			continue
143		fi
144
145		dots=
146		tree=$reltree
147		while [ "${srcfile##$tree}" = "$srcfile" ]; do
148			dots=../$dots
149			tree=`dirname $tree`
150		done
151		echo ${dots}${srcfile##$tree/}
152	done
153}
154
155[ -z "$CODEMGR_WS" ] && fail "No active workspace; run \"ws <workspace_name>\""
156
157while getopts r flag; do
158	case $flag in
159	r)
160		RELPATHS=y
161		;;
162	\?)
163		usage
164		;;
165	esac
166done
167
168shift $((OPTIND - 1))
169
170[ $# -gt 1 ] && usage
171
172CURTREE=`/bin/pwd`
173
174#
175# Determine the subtree being examined.
176#
177if [ $# -eq 0 ]; then
178	SUBTREE=$CURTREE
179elif [ -d $1 ]; then
180	SUBTREE=$1
181elif [ -d $CODEMGR_WS/$1 ]; then
182	SUBTREE=$CODEMGR_WS/$1
183else
184	fail "neither \$CODEMGR_WS/$1 nor $1 exists as a directory"
185fi
186
187#
188# Get the canonical path to the subtree.
189#
190cd $SUBTREE
191SUBTREE=`/bin/pwd`
192
193#
194# Get the canonical path to the current directory.
195#
196cd $CURTREE
197CURTREE=`/bin/pwd`
198
199#
200# Get the canonical path to the workspace.
201#
202cd $CODEMGR_WS
203CODEMGR_WS=`/bin/pwd`
204
205if [ "${SUBTREE##$CODEMGR_WS}" = "$SUBTREE" ]; then
206	fail "$SUBTREE is not a subtree of \$CODEMGR_WS"
207fi
208
209if [ "${CURTREE##$CODEMGR_WS}" = "$CURTREE" ]; then
210	fail "$CURTREE is not a subtree of \$CODEMGR_WS"
211fi
212
213#
214# Find and execute all inc.flg's below our subtree.
215#
216incflg $SUBTREE
217
218#
219# Find and execute all req.flg's at or above our subtree.
220#
221TREE=$SUBTREE
222while [ $TREE != $CODEMGR_WS ]; do
223	[ -f $TREE/req.flg ] && exec_file $TREE/req.flg
224	TREE=`dirname $TREE`
225done
226
227exit 0
228