File: cflow.sh

package info (click to toggle)
cflow 2.0-12.1
  • links: PTS
  • area: main
  • in suites: hamm, potato, slink
  • size: 128 kB
  • ctags: 163
  • sloc: ansic: 1,117; sh: 235; makefile: 102
file content (287 lines) | stat: -rw-r--r-- 4,203 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#! @CFLOW_SHELL@

# cflow - print a function call hierarchy
# This script is contributed to the public domain by Andrew Moore
# of Talke Studio.

# exit on errors..

# this values can be overriden by devining them in the
# enviornment
F2C=${F2C:=f2c}
LEX=${LEX:=ex}
YACC={YACC:=yacc}
CC=${CC:=cc}
CPP=${CPP:=cpp}
PRCC=${PRCC:=@PRCC@}
PRCG=${PRCG:=@PRCG@}
TMPFILE=`tempfile -p cflow`  #/tmp/cflow.$$


function prcc {
	if [ $verbose ]; then
		echo $PRCC $* >/dev/tty
	fi
		
	command $PRCC $*
}

function prcg {

	if [ $verbose ]; then
		echo $PRCG $* >/dev/tty
	fi
	command $PRCG $*
}

function echo {
	builtin echo -e "$*" 
}


# test for dos...
if [ X$OSMODE != X ]; then
	swap extend
	echo=echo
else
	echo="builtin echo -e"
	 trap 'remove_tmp_file ; remove_exclude_files; exit '  1 2 15
fi

progname=$0

function usage
{
	echo $progname [-AaginPXVvxh] [-d n] [-w n] [-r name] [cpp-opts] files
	echo "\t-a\tprint a seperate cal graph for each function"
	echo "\t-A\teliminate ansi keywords"
	echo "\t-P\teliminate posix keywords"
	echo "\t-d n\tprint call graph to depth n"
	echo "\t-g\teliminate gcc keywords"
	echo "\t-r name\\tstart tree at name"
	echo "\t-D and -I options "
	echo "\t-x\tprint each subgraph in full"
	echo "\t-i\tinverted index"
	echo "\t-s\tsave temporary files"
	echo "\t-n\tdon't run prcg (useful for debugging)"
	echo 
	echo "\t-v\tinclude variables"
	echo "\t-X\texclude files "
	echo "\t-V\tverbose"
	echo "\nAccepts the following from the environment"
	echo "\tCPP (default $CPP)"
	echo "\tCC  (default $CC)"
	echo "\tPRCG (default $PRCG)"	
	echo "\tPRCC (default $PRCC)"
	exit 1
}



invert=""
verbose=""
exclude_files=""
exclude_directory=/tmp/cflow.excludes
if [ -z "$OSMODE" ]; then
	exclude_directory="$exclude_directory".$$
fi

function do_mkdir 
{
	for i in $*
	do
		mkdir -p $i
		if [ $? -ne 0 ]; then
			echo "can't make $i; exiting"
			exit 1
		fi
	done
}

function remove_exclude_files
{
	if [ -d $exclude_directory ]; then
		rm -rf $exclude_directory
	fi
} 


function remove_tmp_file
{
	if [ "$save_temps" ] ; then
		echo temp file in  $TMPFILE
	else
		rm $TMPFILE
	fi
}

# call this once when we get are include files
# called only if we want to exclude files
function exclude 
{
	if [ "$exclude_files" ]; then
		do_mkdir $exclude_directory
	fi

	for i in $exclude_files
	do
		case $i in 
			*/* )
				echo "can't make subdirectory $i yet"
				continue
				;;
		esac
		>$exclude_directory/$i

	done		
}

#builtin -d echo 	# use gnu echo

#  echo command line is $* >& 2
while getopts aghsAPD:I:X:nU:d:ir:Vvw:x c; do
#	echo option is $c
#	echo OPTARG = $OPTARG
	case $c in
	D)
		CPP="$CPP -D$OPTARG"
		;;
	I)
		CPP="$CPP -I$OPTARG"
		;;
	U)
		CPP="$CPP -U$OPTARG"
		;;
	a)
		PRCG="$PRCG -a"
		;;
	n)
		no_prcg="true"
		;;
	V)
		verbose=1
		;;
	d)
		PRCG="$PRCG -d$OPTARG"
		;;
	g)	
		PRCC="$PRCC -g"
		;;
	P)
		PRCC="$PRCC -p"
		;;
	A)
		PRCC="$PRCC -a"
		;;
	i)
		PRCG="$PRCG -i"
		invert=1
		;;
	r)
		PRCG="$PRCG -r$OPTARG"
		;;
	v)
		PRCC="$PRCC -v"
		;;
	w)
		PRCG="$PRCG -w$OPTARG"
		;;
	x)
		PRCG="$PRCG -x"
		;;
	s)
		save_temps=1
		;;
	X)	
		exclude_files="$exclude_files $OPTARG"
		;;
	\? | h)
		usage >&2
		exit 2
		;;
	esac
done

if [ "$exclude_files" ]; then
	exclude
	CPP="$CPP -I$exclude_directory" 

fi




let count=$OPTIND-1
shift $count
unset count

if [ $# -eq 0 ]; then
	usage
fi

for c in $*
do
	case $c in
	*.c|*.cc|*.C)
		cname=$c
		;;
	*.f)
		cname=`basename $c .f`.c
		cat $c | $F2C >$cname
		;;
	*.F)
		cname=`basename $c .F`.C
		cat $c | $F2C >$cname
		;;
	*.l)
		cname=`basename $c .l`.c
		$LEX $c
		sed '/# line/d' lex.yy.c >$cname
		;;
	*.y)
		cname=`basename $c .y`.c
		$YACC $c
		sed '/# line/d' y.tab.c >$cname
		;;
	*.s)
		echo "cflow: assembler source not supported" >&2
		shift
		[ $# -le 0 ] && break
		c=$1
		continue
		;;
	*.h)
		shift
		[ $# -le 0 ] && break
		c=$1
		continue
		;;
	*)	
		usage >&2
		exit 2
		;;
	esac
	if [ $verbose ]; then
		echo $CPP $cname >& 2
	fi

	$CPP $cname  >>$TMPFILE
done

if [ "$no_prcg" ]; then
	PRCG=cat
fi


if [ X$invert = X ]; then
 prcc < $TMPFILE | prcg 
else
	prcc < $TMPFILE |
	sed 's/\(.*	\)\(.*	\)/\2\1/' |
	sort +0 -1 +2 |
	uniq |
	prcg	
fi

remove_tmp_file
remove_exclude_files