r20: Add the registry library. Still needs a lot of work,
[kai/samba.git] / source4 / script / 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" => "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                               /xo);
74
75                 if ($line =~ /^FN_/) {
76                         handle_loadparm($line);
77                         next;
78                 }
79
80                 if ( $line =~ /\(.*\)\s*$/o ) {
81                         chomp $line;
82                         print "$line;\n";
83                         next;
84                 }
85
86                 print $line;
87
88                 while ($line = <FH>) {
89                         if ($line =~ /\)\s*$/o) {
90                                 chomp $line;
91                                 print "$line;\n";
92                                 last;
93                         }
94                         print $line;
95                 }
96         }
97
98         close(FH);
99 }
100
101 sub process_files {
102         foreach my $filename (@ARGV) {
103                 process_file($filename);
104         }
105 }
106
107 print_header();
108 process_files();
109 print_footer();