Imported Robodoc.
[robodoc.git] / todo.pl
1 #!/usr/bin/perl -w
2 # vi: ff=unix spell
3 #
4 # Usage:
5 #
6 #    todo.pl [filenames]
7 #
8
9
10 use strict;
11 use IO::Dir;
12 use IO::File;
13
14 my $count = 0;
15
16 sub scan_file {
17     my $filename = shift;
18     unless ( $filename =~ m/tags$/ ) {
19
20         my $file = IO::File->new("<$filename") or die "can't open $filename";
21         my %todos;
22         my $nr = 1;
23
24         # Scan for TODOs
25         while (my $line = <$file>) {
26             # The sentence after the TODO ~should start with a letter.
27             # This ensures we skip the TODO's like the one below.
28             if ($line =~ /TODO(|:)\s+[A-Za-z]/i) {
29                 $todos{$nr} = $line;
30             }
31             ++$nr;
32         }
33         $file->close();
34
35         # Show results.
36         if (scalar(keys %todos)) {
37             foreach my $key (sort { $a <=> $b } keys %todos) {
38                 my $line = $todos{$key};
39                 if ($line =~ m/TODO(.*)$/i) {
40                     my $comment = $1;
41                     $comment =~ s/^(:|\s)+//;
42                     $comment =~ s/\*\/\s*$//;
43                     # Print as:   foobar.c(10)  The stuff to be done
44                     printf( "%-30s %s\n", "$filename($key)", $comment );
45                     ++$count;
46                 }
47             }
48         }
49
50     }
51 }
52
53
54 # Scan all the files in a directory.
55 # Then repeat the process for all the subdirectories.
56 #
57
58 sub scan_directory {
59     my $dirname = shift;
60
61     my $dir = IO::Dir->new($dirname) or die "can't open $dirname : $!";
62     my @files = $dir->read();
63     $dir->close();
64
65     my @source_files = grep { -T "$dirname/$_" } @files;
66
67     foreach my $filename ( sort @source_files ) {
68         my $path = "$dirname/$filename";
69         $path =~ s/\.\///;
70         scan_file( $path );
71     }
72
73     # Repeat the process for all subdirectories.
74     foreach my $filename ( sort @files ) {
75         my $path = "$dirname/$filename";
76         if ( ( -d $path ) and ( $filename !~ m/^\./ ) ) {
77             scan_directory( $path );
78         }
79     }
80 }
81
82
83 sub main {
84     if (@ARGV) {
85         # We are given a set of file names on the command line.
86         foreach my $file (grep { -T } @ARGV) {
87             scan_file( $file );
88         }
89     } else {
90         # No parameters, scan the current directory and
91         # all its subfolders.
92         scan_directory( '.' );
93     }
94     print $count, " TODOs to go\n";
95
96 }
97
98 main;
99
100