s3:libsmb/async_smb: don't remove pending requests if the mid is set
[Samba.git] / source4 / script / mks3param.pl
blob76322b10764904e9ccf24220c942b3bcec01815c
1 #!/usr/bin/perl
2 # Simple script for generating prototypes for C functions
3 # Written by Jelmer Vernooij
4 # based on the original mkproto.sh by Andrew Tridgell
6 use strict;
8 # don't use warnings module as it is not portable enough
9 # use warnings;
11 use Getopt::Long;
12 use File::Basename;
13 use File::Path;
15 #####################################################################
16 # read a file into a string
18 my $file = undef;
19 my $public_define = undef;
20 my $_public = "";
21 my $_private = "";
22 my $public_data = \$_public;
23 my $builddir = ".";
24 my $srcdir = ".";
26 sub public($)
28 my ($d) = @_;
29 $$public_data .= $d;
32 sub usage()
34 print "Usage: mks3param.pl [options] [c files]\n";
35 print "OPTIONS:\n";
36 print " --srcdir=path Read files relative to this directory\n";
37 print " --builddir=path Write file relative to this directory\n";
38 print " --help Print this help message\n\n";
39 exit 0;
42 GetOptions(
43 'file=s' => sub { my ($f,$v) = @_; $file = $v; },
44 'srcdir=s' => sub { my ($f,$v) = @_; $srcdir = $v; },
45 'builddir=s' => sub { my ($f,$v) = @_; $builddir = $v; },
46 'help' => \&usage
47 ) or exit(1);
49 sub normalize_define($$)
51 my ($define, $file) = @_;
53 if (not defined($define) and defined($file)) {
54 $define = "__" . uc($file) . "__";
55 $define =~ tr{./}{__};
56 $define =~ tr{\-}{_};
57 } elsif (not defined($define)) {
58 $define = '_S3_PARAM_H_';
61 return $define;
64 $public_define = normalize_define($public_define, $file);
66 sub file_load($)
68 my($filename) = @_;
69 local(*INPUTFILE);
70 open(INPUTFILE, $filename) or return undef;
71 my($saved_delim) = $/;
72 undef $/;
73 my($data) = <INPUTFILE>;
74 close(INPUTFILE);
75 $/ = $saved_delim;
76 return $data;
79 sub print_header($$)
81 my ($file, $header_name) = @_;
82 $file->("#ifndef $header_name\n");
83 $file->("#define $header_name\n\n");
84 $file->("/* This file was automatically generated by mks3param.pl. DO NOT EDIT */\n\n");
85 $file->("struct loadparm_s3_context\n");
86 $file->("{\n");
87 $file->("\tconst char * (*get_parametric)(const char *type, const char *option);");
90 sub print_footer($$)
92 my ($file, $header_name) = @_;
93 $file->("};");
94 $file->("\n#endif /* $header_name */\n\n");
97 sub handle_loadparm($$)
99 my ($file,$line) = @_;
101 if ($line =~ /^FN_(GLOBAL|LOCAL)_(CONST_STRING|STRING|BOOL|bool|CHAR|INTEGER|LIST)\((\w+),.*\)/o) {
102 my $scope = $1;
103 my $type = $2;
104 my $name = $3;
106 my %tmap = (
107 "BOOL" => "bool ",
108 "CONST_STRING" => "const char *",
109 "STRING" => "const char *",
110 "INTEGER" => "int ",
111 "CHAR" => "char ",
112 "LIST" => "const char **",
115 $file->("\t$tmap{$type} (*$name)(void);\n");
119 sub process_file($$)
121 my ($file, $filename) = @_;
123 $filename =~ s/\.o$/\.c/g;
125 if ($filename =~ /^\//) {
126 open(FH, "<$filename") or die("Failed to open $filename");
127 } elsif (!open(FH, "< $builddir/$filename")) {
128 open(FH, "< $srcdir/$filename") || die "Failed to open $filename";
131 my $comment = undef;
132 my $incomment = 0;
133 while (my $line = <FH>) {
134 if ($line =~ /^\/\*\*/) {
135 $comment = "";
136 $incomment = 1;
139 if ($incomment) {
140 $comment .= $line;
141 if ($line =~ /\*\//) {
142 $incomment = 0;
146 # these are ordered for maximum speed
147 next if ($line =~ /^\s/);
149 next unless ($line =~ /\(/);
151 next if ($line =~ /^\/|[;]/);
153 if ($line =~ /^FN_/) {
154 handle_loadparm($file, $line);
156 next;
159 close(FH);
163 print_header(\&public, $public_define);
165 process_file(\&public, $_) foreach (@ARGV);
166 print_footer(\&public, $public_define);
168 if (not defined($file)) {
169 print STDOUT $$public_data;
172 mkpath(dirname($file), 0, 0755);
173 open(PUBLIC, ">$file") or die("Can't open `$file': $!");
174 print PUBLIC "$$public_data";
175 close(PUBLIC);