much faster inner loop and neater code
[ira/wip.git] / source / script / mkproto.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 my $header_name = '_PROTO_H_';
7
8 if ($ARGV[0] eq '-h') {
9         shift @ARGV;
10         $header_name = shift @ARGV;
11 }
12
13
14 sub print_header {
15         print "#ifndef $header_name\n";
16         print "#define $header_name\n\n";
17         print "/* This file is automatically generated with \"make proto\". DO NOT EDIT */\n\n";
18 }
19
20 sub print_footer {
21         printf "\n#endif /*  %s  */\n", $header_name;
22 }
23
24
25 sub handle_loadparm {
26         my $line = shift;
27
28         if ($line =~ /^FN_(GLOBAL|LOCAL)_(CONST_STRING|STRING|BOOL|CHAR|INTEGER|LIST)\((\w+),.*\)/o) {
29                 my $scope = $1;
30                 my $type = $2;
31                 my $name = $3;
32
33                 my %tmap = (
34                             "BOOL" => "BOOL ",
35                             "CONST_STRING" => "const char *",
36                             "STRING" => "char *",
37                             "INTEGER" => "int ",
38                             "CHAR" => "char ",
39                             "LIST" => "const char **",
40                             );
41
42                 my %smap = (
43                             "GLOBAL" => "void",
44                             "LOCAL" => "int "
45                             );
46
47                 print "$tmap{$type}$name($smap{$scope});\n";
48         }
49 }
50
51
52 sub process_file($) 
53 {
54         my $filename = shift;
55         my $line;
56         my $inheader;
57         my $gotstart;
58
59         open(FH, "< $filename") || die "Failed to open $filename";
60
61         $inheader = 0;
62         $gotstart = 0;
63
64         print "\n/* The following definitions come from $filename  */\n\n";
65
66         while ($line = <FH>) {        
67                 # this ignores most lines
68                 next if ($line =~ /^\s/);
69               
70                 $gotstart = 0;
71               
72                 if ($line =~ /^static|^extern/o ||
73                     $line !~ /^[a-zA-Z]/o ||
74                     $line =~ /[;]/o) {
75                         next;
76                 }
77                       
78                 if ($line =~ /^FN_/) {
79                         handle_loadparm($line);
80                 }
81
82                 next unless ($line =~ /\(/);
83               
84                 if ( $line =~ /
85                      ^void|^BOOL|^int|^struct|^char|^const|^\w+_[tT]\s|^uint|^unsigned|^long|
86                      ^NTSTATUS|^ADS_STATUS|^enum\s.*\(|^DATA_BLOB|^WERROR|^XFILE|^FILE|^DIR|
87                      ^double|^TDB_CONTEXT|^TDB_DATA|^TALLOC_CTX|^NTTIME
88                      /xo) {
89                         $gotstart = 1;
90                 }
91                 
92                 
93                 # goto next line if we don't have a start
94                 next unless $gotstart;
95
96                 if ( $line =~ /\(.*\)\s*$/o ) {
97                         chomp $line;
98                         print "$line;\n";
99                         next;
100                 }
101
102                 print $line;
103
104                 while ($line = <FH>) {
105                         chomp $line;
106                         if ($line =~ /\)\s*$/o) {
107                                 print "$line;\n";
108                                 last;
109                         }
110                         print "$line\n";
111                 }
112         }
113 }
114
115 sub process_files {
116         foreach my $filename (@ARGV) {
117                 process_file($filename);
118         }
119 }
120
121 print_header();
122 process_files();
123 print_footer();