cfd6e029daba26c19a52d36c1332e1382fd8037d
[mat/samba.git] / script / mks3param_ctx_table.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 file_load($)
51 {
52     my($filename) = @_;
53     local(*INPUTFILE);
54     open(INPUTFILE, $filename) or return undef;
55     my($saved_delim) = $/;
56     undef $/;
57     my($data) = <INPUTFILE>;
58     close(INPUTFILE);
59     $/ = $saved_delim;
60     return $data;
61 }
62
63 sub print_header($)
64 {
65         my ($file) = @_;
66         $file->("/* This file was automatically generated by mks3param_ctx.pl. DO NOT EDIT */\n\n");
67         $file->("static const struct loadparm_s3_helpers s3_fns = \n");
68         $file->("{\n");
69         $file->("\t.get_parametric = lp_parm_const_string_service,\n");
70         $file->("\t.get_parm_struct = lp_get_parameter,\n");
71         $file->("\t.get_parm_ptr = lp_parm_ptr,\n");
72         $file->("\t.get_service = lp_service_for_s4_ctx,\n");
73         $file->("\t.get_servicebynum = lp_servicebynum_for_s4_ctx,\n");
74         $file->("\t.get_default_loadparm_service = lp_default_loadparm_service,\n");
75         $file->("\t.get_numservices = lp_numservices,\n");
76         $file->("\t.load = lp_load_for_s4_ctx,\n");
77         $file->("\t.set_cmdline = lp_set_cmdline,\n");
78         $file->("\t.dump = lp_dump,\n");
79 }
80
81 sub print_footer($)
82 {
83         my ($file) = @_;
84         $file->("};");
85 }
86
87 sub handle_loadparm($$)
88 {
89         my ($file,$line) = @_;
90
91         # STRING isn't handled here, as we still don't know what to do with the substituted vars */
92         # LOCAL also isn't handled here
93         if ($line =~ /^FN_(GLOBAL)_(CONST_STRING|BOOL|bool|CHAR|INTEGER|LIST)\((\w+),.*\)/o) {
94                 my $scope = $1;
95                 my $type = $2;
96                 my $name = $3;
97
98                 $file->(".$name = lp_$name,\n");
99         }
100 }
101
102 sub process_file($$)
103 {
104         my ($file, $filename) = @_;
105
106         $filename =~ s/\.o$/\.c/g;
107
108         if ($filename =~ /^\//) {
109                 open(FH, "<$filename") or die("Failed to open $filename");
110         } elsif (!open(FH, "< $builddir/$filename")) {
111             open(FH, "< $srcdir/$filename") || die "Failed to open $filename";
112         }
113
114         my $comment = undef;
115         my $incomment = 0;
116         while (my $line = <FH>) {
117                 if ($line =~ /^FN_/) {
118                         handle_loadparm($file, $line);
119                 }
120                 next;
121         }
122
123         close(FH);
124 }
125
126
127 print_header(\&public);
128
129 process_file(\&public, $_) foreach (@ARGV);
130 print_footer(\&public);
131
132 if (not defined($file)) {
133         print STDOUT $$public_data;
134 }
135
136 mkpath(dirname($file), 0, 0755);
137 open(PUBLIC, ">$file") or die("Can't open `$file': $!");
138 print PUBLIC "$$public_data";
139 close(PUBLIC);