3 # shtags: create a tags file for perl scripts
5 # Author: Stephen Riehm
6 # Last Changed: 96/11/27 19:46:06
8 # "@(#) shtags 1.1 by S. Riehm"
15 USAGE
: $program [-kvwVx
] [-t
<file
>] <files
>
16 -t
<file
> Name of tags file to create
. (default is
'tags')
17 -s
<shell
> Name of the shell language
in the script
18 -v Include variable definitions
.
19 (variables mentioned at the start of a line
)
20 -V Print version information
.
21 -w Suppress
"duplicate tag" warnings
.
22 -x Explicitly create a new tags file
. Normally tags are merged
.
23 <files
> List of files to scan
for tags
.
33 @id = split( ', ', 'scripts/bin/shtags, /usr/local/, LOCAL_SCRIPTS, 1.1, 96/11/27, 19:46:06' );
37 Last Modified
: @id[4,5]
47 ($program = $0) =~ s
,.*/,,;
53 &Getopts
( "t:s:vVwx" ) || &usage
();
54 $tags_file = $opt_t || 'tags';
56 $variable_tags = $opt_v;
57 $allow_warnings = ! $opt_w;
59 &usage
() unless @ARGV != 0;
61 # slurp up the existing tags. Some will be replaced, the ones that aren't
62 # will be re-written exactly as they were read
63 if( ! $explicit && open( TAGS
, "< $tags_file" ) )
74 # for each line of every file listed on the command line, look for a
75 # 'sub' definition, or, if variables are wanted aswell, look for a
76 # variable definition at the start of a line
80 &check_shell
($_), ( $old_file = $ARGV ) if $ARGV ne $old_file;
84 next unless /^\s*(((\w+)))\s*\(\s*\)/
85 || ( $variable_tags && /^(((\w+)=))/ );
91 next unless /^\s*function\s+(((\w+)))/
92 || ( $variable_tags && /^(((\w+)=))/ );
95 if( $shell eq "perl" )
98 next unless /^\s*sub\s+(\w+('|::))?(\w+)/
100 || ( $variable_tags && /^(([(\s]*[\$\@\%]{1}(\w+).*=))/ );
103 if( $shell eq "tcl" )
105 next unless /^\s*proc\s+(((\S+)))/
106 || ( $variable_tags && /^\s*set\s+(((\w+)\s))/ );
110 warn "$match - duplicate ignored\n"
112 || !( $tags{$match} = sprintf( "%s\t%s\t?^%s\$?\n", $match, $ARGV, $_ ) ) )
116 # write the new tags to the tags file - note that the whole file is rewritten
117 open( TAGS
, "> $tags_file" );
118 foreach( sort( keys %tags ) )
120 print TAGS
"$tags{$_}";
127 # read the first line of a script, and work out which shell it is,
128 # unless a shell was specified on the command line
130 # This routine can't handle clever scripts which start sh and then
131 # use sh to start the shell they really wanted.
138 $shell = "sh" if /^:$/ || /^#!.*\/bin\
/sh/;
139 $shell = "ksh" if /^#!.*\/ksh
/;
140 $shell = "perl" if /^#!.*\/perl
/;
141 $shell = "tcl" if /^#!.*\/wish
/;
142 printf "Using $shell for $ARGV\n";