IM enhancements
[MacVim.git] / runtime / tools / shtags.pl
blob48dcdc7476d2e4e5db5f55c21133b1a600a86e01
1 #!/usr/bin/env perl
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"
11 # obvious... :-)
12 sub usage
14 print <<_EOUSAGE_ ;
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.
24 _EOUSAGE_
25 exit 0
28 sub version
31 # Version information
33 @id = split( ', ', 'scripts/bin/shtags, /usr/local/, LOCAL_SCRIPTS, 1.1, 96/11/27, 19:46:06' );
34 $id[0] =~ s,.*/,,;
35 print <<_EOVERS;
36 $id[0]: $id[3]
37 Last Modified: @id[4,5]
38 Component: $id[1]
39 Release: $id[2]
40 _EOVERS
41 exit( 1 );
45 # initialisations
47 ($program = $0) =~ s,.*/,,;
48 require 'getopts.pl';
51 # parse command line
53 &Getopts( "t:s:vVwx" ) || &usage();
54 $tags_file = $opt_t || 'tags';
55 $explicit = $opt_x;
56 $variable_tags = $opt_v;
57 $allow_warnings = ! $opt_w;
58 &version if $opt_V;
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" ) )
65 while( <TAGS> )
67 /^\S+/;
68 $tags{$&} = $_;
70 close( TAGS );
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
78 while( <> )
80 &check_shell($_), ( $old_file = $ARGV ) if $ARGV ne $old_file;
81 next unless $shell;
82 if( $shell eq "sh" )
84 next unless /^\s*(((\w+)))\s*\(\s*\)/
85 || ( $variable_tags && /^(((\w+)=))/ );
86 $match = $3;
88 if( $shell eq "ksh" )
90 # ksh
91 next unless /^\s*function\s+(((\w+)))/
92 || ( $variable_tags && /^(((\w+)=))/ );
93 $match = $3;
95 if( $shell eq "perl" )
97 # perl
98 next unless /^\s*sub\s+(\w+('|::))?(\w+)/
99 || /^\s*(((\w+))):/
100 || ( $variable_tags && /^(([(\s]*[\$\@\%]{1}(\w+).*=))/ );
101 $match = $3;
103 if( $shell eq "tcl" )
105 next unless /^\s*proc\s+(((\S+)))/
106 || ( $variable_tags && /^\s*set\s+(((\w+)\s))/ );
107 $match = $3;
109 chop;
110 warn "$match - duplicate ignored\n"
111 if ( $new{$match}++
112 || !( $tags{$match} = sprintf( "%s\t%s\t?^%s\$?\n", $match, $ARGV, $_ ) ) )
113 && $allow_warnings;
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{$_}";
122 close( TAGS );
124 sub check_shell
126 local( $_ ) = @_;
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.
132 if( $opt_s )
134 $shell = $opt_s;
136 else
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";