summaryrefslogtreecommitdiffstats
path: root/tools/genmodparm
blob: e4aa5d4e9a68a10c5568db1d06be343fb2abb441 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/perl

# Mandrake Graphic Install
# Copyright (C) 1999 MandrakeSoft (fpons@mandrakesoft.com)
#
# 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, 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

$srctop = $ARGV[0];

unless (-d $srctop) {
  print STDERR "usage: $0 <linux_src_top>\n";
  print STDERR "   <linux_src_top> is the linux source top directory,\n";
  print STDERR "                   for example /usr/src/linux\n";
  exit 1;
}

open (FILE_LIST, "find $srctop/ -name \"*.c\" |");

while (<FILE_LIST>) {
  chomp;

  my $file = $_;
  my $dir = $file;
  my $module = $file;
  my $incfile;
  my @incfiles;
  my %parms;
  my $oldline;
  my $descline;
  my $default;
  my %substvars;

  # get mormalized directory name.
  $dir =~ s/^(.*)\/[^\/]*$/$1/;

  # get mormalized module name.
  $module  =~ s/^.*\/([^\/]*)\.c$/$1/;

  # search for recogniwed special keywords.
  open (F, $file);
  while (<F>) {
    # track for include files.
    if (/^\#\s*include\s+[\<\"]([\w-\.\/]*)[\"\>]/) {
      # search from /usr/src/linux/include directory.
      push @incfiles, "/usr/src/linux/include/$1";

      # search from current working directory.
      push @incfiles, "$dir/$1";
    }

    if (/^\s*MODULE_PARM\s*\((\w*)\s*,\s*\"/) {
	$parms{$1}{type} = '?';
    }
    if (/^\s*MODULE_PARM\s*\((\w*)\s*,\s*\"([^\"]*)\"\s*\)/) {
      $parms{$1}{type} = $2;
    }
    if (/^\s*MODULE_PARM_DESC\s*\((\w*)\s*,\s*\"([^\"]*)\"\s*\)/) {
      $parms{$1}{desc} = $2;
    }
  }
  close F;

  # parse associated include file if exist.
  foreach $incfile (@incfiles) {
    if (-r $incfile) {
      open (F, $incfile);
      while (<F>) {
	s/^(.*)\/\*.*$/$1/g;
	if (/^\#\s*define\s*(\w*)\s*(.*)$/) {
	  $substvars{$1}=$2;
	}
      }
      close F;
    }
  }

  # search for comments about each module parameter.
  open (F, $file);
  while (<F>) {
    my $line = $_;

    # manage simple preprocessor.
    s/^(.*)\/\*.*$/$1/g;
    if (/^\#\s*define\s*(\w*)\s*(.*)$/) {
      $substvars{$1}=$2;
    }

    # parse for parameters definition.
    foreach $parm (keys %parms) {
      if ($line =~ /^\s*(static\s+)?((short|long|signed|unsigned)\s+)?\w+(\s*\**\s+|\s+\**\s*)$parm(\s*\[.*\]\s*)?\s*=\s*([^\;]*)\;/) {
	$default = $descline = $6;
	$default =~ s/^(.*)\/\*.*$/$1/g;

	# remove hypothetic couple of { }.
	$default =~ s/^(\s*\{\s*)(.*)(\s*\}\s*)$/$2/;

	# subsitute variable.
	foreach $substvar (keys %substvars) {
	  $default =~ s/$substvar/$substvars{$substvar}/g;
	}
	$default =~ s/NULL/0/g;
	$default =~ s/^\s*(.*?)\s*$/$1/;
	$default = '' if $default =~ /\(\s*\(\s*void*\s*\*\)\s*0\s*\)\s*,?/;

	# store value.
	$parms{$parm}{default} = $default;

	# try to search a comment on the previous line.
	if (!defined($parms{$parm}{desc})) {
	  if ($oldline =~ /^\s*\/\*\s*(.*)\s*\*\/\s*$/ || /\/\*\s*(.*)\s*\*\/\s*$/) {
	    $parms{$parm}{desc} = $1;
	  }
	}

	# try to search a comment on the line (multiline not supported).
	if (!defined($parms{$parm}{desc})) {
	  if ($descline =~ /^.*\/\*\s*(.*)\s*\*\/\s*$/) {
	    $parms{$parm}{desc} = $1;
	  }
	}
      }
    }
    $oldline = $_;
  }
  close F;

  # dump all result to stdout associated to current module.
  foreach $parm (keys %parms) {
    print "$module:$parm:$parms{$parm}{type}:$parms{$parm}{default}:$parms{$parm}{desc}\n";
  }
}

close FILE_LIST;