s3-param Generate parameter tables
[kai/samba-autobuild/.git] / source4 / script / mks3param.pl
1 #!/usr/bin/perl
2 # Generate loadparm interfaces tables for Samba3/Samba4 integration
3 # by Andrew Bartlett
4 # based on mkproto.pl Written by Jelmer Vernooij
5 # based on the original mkproto.sh by Andrew Tridgell
6
7 use strict;
8
9 # don't use warnings module as it is not portable enough
10 # use warnings;
11
12 use Getopt::Long;
13 use File::Basename;
14 use File::Path;
15
16 #####################################################################
17 # read a file into a string
18
19 my $file = undef;
20 my $public_define = undef;
21 my $_public = "";
22 my $_private = "";
23 my $public_data = \$_public;
24 my $builddir = ".";
25 my $srcdir = ".";
26
27 sub public($)
28 {
29         my ($d) = @_;
30         $$public_data .= $d;
31 }
32
33 sub usage()
34 {
35         print "Usage: mks3param.pl [options] [c files]\n";
36         print "OPTIONS:\n";
37         print "  --srcdir=path          Read files relative to this directory\n";
38         print "  --builddir=path        Write file relative to this directory\n";
39         print "  --help                 Print this help message\n\n";
40         exit 0;
41 }
42
43 GetOptions(
44         'file=s' => sub { my ($f,$v) = @_; $file = $v; },
45         'srcdir=s' => sub { my ($f,$v) = @_; $srcdir = $v; },
46         'builddir=s' => sub { my ($f,$v) = @_; $builddir = $v; },
47         'help' => \&usage
48 ) or exit(1);
49
50 sub normalize_define($$)
51 {
52         my ($define, $file) = @_;
53
54         if (not defined($define) and defined($file)) {
55                 $define = "__" . uc($file) . "__";
56                 $define =~ tr{./}{__};
57                 $define =~ tr{\-}{_};
58         } elsif (not defined($define)) {
59                 $define = '_S3_PARAM_H_';
60         }
61
62         return $define;
63 }
64
65 $public_define = normalize_define($public_define, $file);
66
67 sub file_load($)
68 {
69     my($filename) = @_;
70     local(*INPUTFILE);
71     open(INPUTFILE, $filename) or return undef;
72     my($saved_delim) = $/;
73     undef $/;
74     my($data) = <INPUTFILE>;
75     close(INPUTFILE);
76     $/ = $saved_delim;
77     return $data;
78 }
79
80 sub print_header($$)
81 {
82         my ($file, $header_name) = @_;
83         $file->("#ifndef $header_name\n");
84         $file->("#define $header_name\n\n");
85         $file->("/* This file was automatically generated by mks3param.pl. DO NOT EDIT */\n\n");
86         $file->("struct loadparm_s3_context\n");
87         $file->("{\n");
88         $file->("\tconst char * (*get_parametric)(const char *type, const char *option);");
89 }
90
91 sub print_footer($$) 
92 {
93         my ($file, $header_name) = @_;
94         $file->("};");
95         $file->("\n#endif /* $header_name */\n\n");
96 }
97
98 sub handle_loadparm($$) 
99 {
100         my ($file,$line) = @_;
101
102         if ($line =~ /^FN_(GLOBAL|LOCAL)_(CONST_STRING|STRING|BOOL|bool|CHAR|INTEGER|LIST)\((\w+),.*\)/o) {
103                 my $scope = $1;
104                 my $type = $2;
105                 my $name = $3;
106
107                 my %tmap = (
108                             "BOOL" => "bool ",
109                             "CONST_STRING" => "const char *",
110                             "STRING" => "const char *",
111                             "INTEGER" => "int ",
112                             "CHAR" => "char ",
113                             "LIST" => "const char **",
114                             );
115
116                 $file->("\t$tmap{$type} (*$name)(void);\n");
117         }
118 }
119
120 sub process_file($$) 
121 {
122         my ($file, $filename) = @_;
123
124         $filename =~ s/\.o$/\.c/g;
125
126         if ($filename =~ /^\//) {
127                 open(FH, "<$filename") or die("Failed to open $filename");
128         } elsif (!open(FH, "< $builddir/$filename")) {
129             open(FH, "< $srcdir/$filename") || die "Failed to open $filename";
130         }
131
132         my $comment = undef;
133         my $incomment = 0;
134         while (my $line = <FH>) {             
135                 if ($line =~ /^\/\*\*/) { 
136                         $comment = "";
137                         $incomment = 1;
138                 }
139
140                 if ($incomment) {
141                         $comment .= $line;
142                         if ($line =~ /\*\//) {
143                                 $incomment = 0;
144                         }
145                 } 
146
147                 # these are ordered for maximum speed
148                 next if ($line =~ /^\s/);
149               
150                 next unless ($line =~ /\(/);
151
152                 next if ($line =~ /^\/|[;]/);
153
154                 if ($line =~ /^FN_/) {
155                         handle_loadparm($file, $line);
156                 }
157                 next;
158         }
159
160         close(FH);
161 }
162
163
164 print_header(\&public, $public_define);
165
166 process_file(\&public, $_) foreach (@ARGV);
167 print_footer(\&public, $public_define);
168
169 if (not defined($file)) {
170         print STDOUT $$public_data;
171 }
172
173 mkpath(dirname($file), 0, 0755);
174 open(PUBLIC, ">$file") or die("Can't open `$file': $!"); 
175 print PUBLIC "$$public_data";
176 close(PUBLIC);
177