summaryrefslogtreecommitdiff
path: root/extComments/extComments.pl
blob: 39dbeb5d885985cbed157b73be91fd95e04cec03 (plain)
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
#!/usr/bin/perl

#
#    Copyright (C) 2009-2014  Yuki Manabe and Daniel M. German
#
#    This program is free software; you can redistribute it and/or
#    modify it under the terms of the GNU General Public License as
#    published by the Free Software Foundation; either version 2 of
#    the License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see
#    <http://www.gnu.org/licenses/>.
#

use Getopt::Std;
use strict;

my $path = $0;

$path =~ s/\/+[^\/]+$//;
if ($path eq '') {
    $path = './';
}

# set parameters
my %opts = ();
if (!getopts ('vc:p:',\%opts) or scalar(@ARGV) != 1 ) {
print STDERR "Usage $0 -v <filename>

  -v verbose
  -c count of comment blocks

\n";

    die;
}

my $f = $ARGV[0];

my $original = $f;

$f =~ s/'/\\'/g;
$f =~ s/\$/\\\$/g;
$f =~ s/;/\\;/g;
$f =~ s/ /\\ /g;

#die "illegal file [$f]"  if $f =~ m@/\.@;

my $numberComments = 1;
$numberComments = $opts{c} if exists $opts{c};
my $verbose = 1;
$verbose = exists $opts{v};

if (get_size($original) == 0) {
    print STDERR "Empty file, just exit\n" if $verbose;
    exit 0; # nothing to report, just end
}





my $commentsCmd = Determine_Comments_Extractor($f);

print execute("$commentsCmd");

exit 0;


sub Determine_Comments_Extractor
{
    my ($f) = @_;

    if ($f =~ /\.([^\.]+)$/) {
        my $ext= $1;

        if ($ext =~ /^(pl|pm|py)$/
            ) {
########################
# for the time being, let us just extract the top 400 lines

            return "cat $f | head -400";
#            return "$path/hashComments.pl -p '#' '$f'";
        } elsif ($ext eq 'jl' or
                 $ext eq 'el'
            ) {
            return "cat $f | head -400";
#            return "$path/hashComments.pl -p ';' $f";;
        } elsif ($ext =~ /^(java|c|cpp|h|cxx|c\+\+|cc)$/ ) {
            my $comm = `which comments`;
            if ($comm ne '') {
                return "comments -o -c1 $f 2> /dev/null";
            } else {
                return "head -400 $f";
            }
        } else {
            return "head -700 $f";
        }
    } else {
        return "head -700 $f";
    }
}

sub execute
{
    my ($c) = @_;
    my $r = `$c`;
    my $status = ($? >> 8);
    die "execution of program [$c] failed: status [$status]" if ($status != 0);
    return $r;
}


sub get_size
{
    my ($f) = @_;
    my $size = (stat($f))[7];
    return $size;
}