1#!/bin/sh
2# This utility maintains the database for cscope on a recursive dir set
3# Author:   donwo	Tue Jun 25 15:36:39 PDT 1996
4# Modified: hops Jan 2000  chg defaults to not update if files exist and force
5#
6# These comments ARE the manual.  What more do you really need?
7# if using unadorned cscope with this use cscope -d so not trash db
8#                    cscope -L -0 <ptn> - to display ptn matches on stdout
9#
10# The lists of files are kept in two forms:
11#	TMP	cscope.tmplst
12#               The list generated by this program
13#		This list will be updated each time this program
14#		is executed.
15#
16#	LST	cscope.lst
17#               The fixed list generated by some other process
18#		This list will be used, if found, rather than
19#		the TMP form generated here.
20#
21#	CSD	cscope.csd
22#               The fixed list generated by some other process
23#		This list will be used, if found, rather than
24#		the LST or TMP forms.  CSD differs from LST in
25#		that the CSD list is used where the source files
26#		change only seldom.  Cscope is requested not to
27#		check for changed files.  This can be
28#		significantly faster on large source trees.
29#
30#	INC	cscope.inc
31#               This is a list of additional directories
32#		in which to search for include files.
33#
34# Three hierarchies of libraries are supported:
35#	Local	In the current directory	./
36#		This is most useful for transient projects or
37#		where the information is of no use to others on
38#		the system.  This type is NOT usable on source
39#		directories that are read-only.
40#	Home	In users home directory		$HOME/lib/cs/`pwd`
41#		This is good for items that seldom change but are
42#		of use only the the current user.  This type is
43#		usable on source directories that are read-only.
44#	System	In a global system directory	$SYSDIR/`pwd`
45#		This is for items that are of interest to all accounts.
46#		This option is not available unless the system directory
47#		is writable by the current user.  This type is usable
48#		on source directories that are read-only.
49#
50# If a shell script named ./cscope.rc is found and is
51# executable, the execution of it will be included within this
52# script after defaults_set/cmdline_parse and locating the
53# database.
54#
55# Command line options:
56#	-x	set shell debugging
57#	-f	force
58#		  o  Do not ask about regenerating TMP.  Just do it.
59#		  o  Allow cscope to regenerate libraries for CSD lists.
60#	-q	Tell cscope to build an inverted index for quick
61#		symbol searching.  There is a SIGNIFICANT
62#		increase in speed with this option however the
63#		disk space used is doubled.  Once the quick
64#		database is generated, cs will detect the files
65#		and continue to use them.
66#	-d	Do not regenerate.  Intended for cscope sub-tasks.
67#	-u	Update/regenerate.
68
69#
70# Here is where we put things
71
72CSCOPE=cscope
73HOMEDIR=${HOME}/lib/cs
74
75#set the default value for SYSDIR
76if [ -z "${SYSDIR}" ]; then
77   SYSDIR=/usr/local/lib/cs
78   echo setting default sysdir
79fi
80
81#check that SYSDIR exists
82if [ ! -d ${SYSDIR} ]; then
83   echo -n $SYSDIR does not exist.
84   echo Please create the directory and set SYSDIR appropriately
85   exit
86fi
87
88# Check that cscope is in PATH
89type cscope 1>/dev/null 2>&1
90
91if [ $? -ne 0 ]
92then
93	echo "ERROR: cscope is not in \$PATH" >&2
94	echo "       Please set \$PATH correctly or make sure cscope is installed" >&2
95	exit 1
96fi
97
98# popup editor
99#XCS_EDITOR=${HOME}/bin/x_cscope_editor
100XCS_EDITOR=${HOME}/bin/xme
101if [ -n "$DISPLAY" -a -x ${XCS_EDITOR} ]
102then
103	EDITOR=${XCS_EDITOR}
104	export EDITOR
105fi
106unset XCS_EDITOR
107
108#
109# Misc defaults
110
111#FORCE=N
112#NOUPDATE=
113FORCE=Y         # hops - default to force rather than query
114NOUPDATE=-d     # hops - default to no update if files already exist
115QUICK=
116SPECDEST=       # hops - query for files
117
118#
119# Parse the command line
120
121set -- `getopt xfqdu $*`
122
123if [ $? -ne 0 ]
124then
125    echo "Use: cs [-x] [-f] [-q] [-u]" >&2
126    echo " -x debug on " >&2
127    echo " -q quick Index - faster search but larger index" >&2
128    echo " -f ask about about regeneration" >&2
129    echo " -d don't update database (default)" >&2
130    echo " -u update database" >&2
131    echo " -s specify where files go" >&2
132    exit 1
133fi
134
135for arg
136do
137    case $arg in
138	-x )	set -x;     shift	;;
139	-f )	FORCE=N; NOUPDATE=; shift;;
140	-q )	QUICK=-q;    shift	;;
141	-d )	NOUPDATE=-d; shift	;;
142	-u )	NOUPDATE=;   shift	;;
143	-s )	SPECDEST=Y;   shift	;;
144    esac
145done
146
147#
148# Here is the security hole.  Execute whatever is needed for
149# this project.  A per-project setup script may be available.
150
151[ -x ./cscope.rc ] && {
152    . ./cscope.rc
153}
154
155#
156# We look hard for appropriate files to scope.  We ignore items
157# containing "SCCS" assuming that these are directories of
158# source code control data.
159
160create_list()
161{
162    LIST=$1
163
164    if [ -f ${LIST} ]
165    then
166	[ -n "${NOUPDATE}" ] && return
167
168	if [ "${FORCE}" != "Y" ]
169	then
170	    echo "\n${LIST}"
171	    echo "Update the library? <(Y)es, (N)o, (Q)uit> [n] \c"
172	    read x y
173	    case $x in
174		[Yy]* )	;;
175		[Qq]* )	exit 1	;;
176		*)	return	;;
177	    esac
178	fi
179	echo "Updating library:\n  ${LIST} \c"
180    else
181	echo "Creating library:\n  ${LIST} \c"
182    fi
183
184    (
185	find . -follow -type f \( -name \*.[sScChHlyG] -o \
186				  -name \*.asm -o \
187				  -name \*.cc -o \
188				  -name \*.cxx -o \
189				  -name \*.ccP -o \
190				  -name \*.hP -o \
191				  -name \*.inc -o \
192				  -name \*.ed -o \
193				  -name vuifile -o \
194				  -name Gensymvals -o \
195				  -name \[mM\]ake\* \) \
196				  -print
197   ) | grep -v SCCS | sort -u > ${LIST}
198
199    echo "\n`cat ${LIST} | wc -l` files listed"
200}
201
202#
203# Expand the include file list into command line arguments
204
205exp_inc()
206{
207    theInc=$1
208
209    if [ -s "${theInc}" ]
210    then
211	for i in `cat ${theInc}`
212	do
213	    echo "-I $i \c"
214	done
215    fi
216}
217
218#
219# This routine does not return to the caller
220
221do_cscope()
222{
223    LIST=$1
224    CSLIB=$2
225    INC=$3
226    shift;shift;shift
227    ARGS="$*"
228
229    INCARGS=`exp_inc ${INC}`
230
231    echo "exec cscope"
232    exec $CSCOPE ${ARGS} -p 2 ${INCARGS} -i ${LIST} -f ${CSLIB}
233    echo "exec of $CSCOPE failed" >&2
234    exit 1
235}
236
237#
238# If we have existing libraries, we should use them.
239std_libs()
240{
241    DIR=$1
242    OUT=${DIR}/cscope.out
243    LST=${DIR}/cscope.lst
244    CSD=${DIR}/cscope.csd
245    TMP=${DIR}/cscope.tmplst
246    INC=${DIR}/cscope.inc
247    QCK=${DIR}/cscope.out.po
248
249    [ -s ${QCK} ] && QUICK=-q
250
251    [ -f ${CSD} ] && {
252	if [ "${FORCE}" = "Y" ]
253	then
254	    do_cscope ${CSD} ${OUT} ${INC} ${QUICK}
255	else
256	    do_cscope ${CSD} ${OUT} ${INC} ${QUICK} -d
257	fi
258    }
259
260    [ -f ${LST} ] && do_cscope ${LST} ${OUT} ${INC} ${QUICK} ${NOUPDATE}
261
262    [ -f ${TMP} ] && {
263	create_list ${TMP}
264	do_cscope ${TMP} ${OUT} ${INC} ${QUICK} ${NOUPDATE}
265    }
266}
267
268#
269# ######## main() #######
270
271umask 0
272PWD=`pwd`
273
274umask 02
275
276#
277# Check for existing libraries
278
279std_libs $PWD
280std_libs ${HOMEDIR}$PWD
281std_libs ${SYSDIR}$PWD
282
283#
284# We may need to create one for this area
285
286DIR=$PWD
287if [ ! -n "${NOUPDATE}" -o -n "${SPECDEST}" ] ; then
288echo "Create new library? <(L)ocal, (H)ome, (S)ystem, (Q)uit> [q] \c"
289
290# shellcheck disable=SC2034
291read x y
292case $x in
293    [Ll]* )	DIR=$PWD		;;
294    [Hh]* )	DIR=${HOMEDIR}$PWD	;;
295    [Ss]* )	DIR=${SYSDIR}$PWD	;;
296    *)		exit 1			;;
297esac
298fi
299[ -d $DIR ] || {
300    mkdir -p $DIR || exit $?
301}
302
303OUT=${DIR}/cscope.out
304TMP=${DIR}/cscope.tmplst
305INC=${DIR}/cscope.inc
306
307create_list ${TMP}
308do_cscope ${TMP} ${OUT} ${INC} ${QUICK}
309
310