[dbench @ tridge@samba.org-20070625020409-1ky1oe54k6bhn4kq]
[tridge/dbench.git] / mkproto.pl
1 #!/usr/bin/perl
2
3 use strict;
4
5 # don't use warnings module as it is not portable enough
6 # use warnings;
7
8 my $header_name = '_PROTO_H_';
9
10 if ($ARGV[0] eq '-h') {
11         shift @ARGV;
12         $header_name = shift @ARGV;
13 }
14
15 sub print_header {
16         print "#ifndef $header_name\n";
17         print "#define $header_name\n\n";
18         print "/* This file is automatically generated with \"make proto\". DO NOT EDIT */\n\n";
19 }
20
21 sub print_footer {
22         printf "\n#endif /*  %s  */\n", $header_name;
23 }
24
25
26 sub handle_loadparm {
27         my $line = shift;
28
29         if ($line =~ /^FN_(GLOBAL|LOCAL)_(CONST_STRING|STRING|BOOL|CHAR|INTEGER|LIST)\((\w+),.*\)/o) {
30                 my $scope = $1;
31                 my $type = $2;
32                 my $name = $3;
33
34                 my %tmap = (
35                             "BOOL" => "BOOL ",
36                             "CONST_STRING" => "const char *",
37                             "STRING" => "const char *",
38                             "INTEGER" => "int ",
39                             "CHAR" => "char ",
40                             "LIST" => "const char **",
41                             );
42
43                 my %smap = (
44                             "GLOBAL" => "void",
45                             "LOCAL" => "int "
46                             );
47
48                 print "$tmap{$type}$name($smap{$scope});\n";
49         }
50 }
51
52
53 sub process_file($) 
54 {
55         my $filename = shift;
56
57         open(FH, "< $filename") || die "Failed to open $filename";
58
59         print "\n/* The following definitions come from $filename  */\n\n";
60
61         while (my $line = <FH>) {             
62                 # these are ordered for maximum speed
63                 next if ($line =~ /^\s/);
64               
65                 next unless ($line =~ /\(/);
66
67                 next if ($line =~ /^\/|[;]/);
68
69                 next unless ( $line =~ /
70                               ^void|^BOOL|^int|^struct|^char|^const|^\w+_[tT]\s|^uint|^unsigned|^long|
71                               ^NTSTATUS|^ADS_STATUS|^enum\s.*\(|^DATA_BLOB|^WERROR|^XFILE|^FILE|^DIR|
72                               ^double|^TDB_CONTEXT|^TDB_DATA|^TALLOC_CTX|^NTTIME|^FN_|^REG_KEY|^REG_HANDLE|^REG_VAL|
73                               ^GtkWidget|^GType|^smb_ucs2_t
74                               /xo);
75
76                 next if ($line =~ /^int\s*main/);
77
78                 if ($line =~ /^FN_/) {
79                         handle_loadparm($line);
80                         next;
81                 }
82
83                 if ( $line =~ /\(.*\)\s*$/o ) {
84                         chomp $line;
85                         print "$line;\n";
86                         next;
87                 }
88
89                 print $line;
90
91                 while ($line = <FH>) {
92                         if ($line =~ /\)\s*$/o) {
93                                 chomp $line;
94                                 print "$line;\n";
95                                 last;
96                         }
97                         print $line;
98                 }
99         }
100
101         close(FH);
102 }
103
104 sub process_files {
105         foreach my $filename (@ARGV) {
106                 process_file($filename);
107         }
108 }
109
110 print_header();
111 process_files();
112 print_footer();