summaryrefslogtreecommitdiff
path: root/source4/script/mkproto.pl
blob: 5db9424cd8d9e59f012154588a53d53698ccdd68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/perl

use strict;

# don't use warnings module as it is not portable enough
# use warnings;


use Getopt::Long;

my $public_file = undef;
my $private_file = undef;
my $public_define = undef;
my $private_define = undef;
my $public_fd = \*STDOUT;
my $private_fd = \*STDOUT;

GetOptions(
	'public=s' => sub { my ($f,$v) = @_; $public_file = $v; },
	'private=s' => sub { my ($f,$v) = @_; $private_file = $v; },
	'define=s' => sub { 
		my ($f,$v) = @_; 
		$public_define = $v; 
		$private_define = "$v\_PRIVATE"; 
	},
	'public-define=s' => \$public_define,
	'private-define=s' => \$private_define
);

if ($public_define eq undef and $public_file ne undef) {
	$public_define = $public_file;
	$public_define =~ tr{./}{__};
} elsif ($public_define eq undef) {
	$public_define = '_PROTO_H_';
}

if ($private_define eq undef and $private_file ne undef) {
	$private_define = $private_file;
	$private_define =~ tr{./}{__};
} elsif ($public_define eq undef) {
	$public_define = '_PROTO_H_';
}

if ($public_file ne undef) {
	open PUBLIC, ">$public_file"; 
	$public_fd = \*PUBLIC;
}

if ($private_file eq $public_file) {
	$private_fd = $public_fd;
} elsif ($private_file ne undef) {
	open PRIVATE, ">$private_file"; 
	$private_fd = \*PRIVATE;
}

sub print_header($$)
{
	my ($file, $header_name) = @_;
	print $file "#ifndef $header_name\n";
	print $file "#define $header_name\n\n";
	print $file "/* This file is automatically generated with \"make proto\". DO NOT EDIT */\n\n";
}

sub print_footer($$) 
{
	my ($file, $header_name) = @_;
	printf $file "\n#endif /*  %s  */\n", $header_name;
}

sub handle_loadparm($$) 
{
	my ($file,$line) = @_;

	if ($line =~ /^FN_(GLOBAL|LOCAL)_(CONST_STRING|STRING|BOOL|CHAR|INTEGER|LIST)\((\w+),.*\)/o) {
		my $scope = $1;
		my $type = $2;
		my $name = $3;

		my %tmap = (
			    "BOOL" => "BOOL ",
			    "CONST_STRING" => "const char *",
			    "STRING" => "const char *",
			    "INTEGER" => "int ",
			    "CHAR" => "char ",
			    "LIST" => "const char **",
			    );

		my %smap = (
			    "GLOBAL" => "void",
			    "LOCAL" => "int "
			    );

		print $file "$tmap{$type}$name($smap{$scope});\n";
	}
}

sub process_file($$$) 
{
	my ($public_file, $private_file, $filename) = @_;

	$filename =~ s/\.o$/\.c/g;

	open(FH, "< $filename") || die "Failed to open $filename";

	print $private_file "\n/* The following definitions come from $filename  */\n\n";

	while (my $line = <FH>) {	      
		my $target = $private_file;

		# these are ordered for maximum speed
		next if ($line =~ /^\s/);
	      
		next unless ($line =~ /\(/);

		next if ($line =~ /^\/|[;]/);

		next unless ( $line =~ /
			      ^void|^BOOL|^int|^struct|^char|^const|^\w+_[tT]\s|^uint|^unsigned|^long|
			      ^NTSTATUS|^ADS_STATUS|^enum\s.*\(|^DATA_BLOB|^WERROR|^XFILE|^FILE|^DIR|
			      ^double|^TDB_CONTEXT|^TDB_DATA|^TALLOC_CTX|^NTTIME|^FN_|^REG_KEY|^REG_HANDLE|^REG_VAL|
			      ^GtkWidget|^GType|^smb_ucs2_t
			      /xo);

		next if ($line =~ /^int\s*main/);

		if ($line =~ /^FN_/) {
			handle_loadparm($public_file, $line);
			next;
		}

		if ($line =~ s/_PUBLIC_//xo) {
			$target = $public_file;
		}

		if ( $line =~ /\(.*\)\s*$/o ) {
			chomp $line;
			print $target "$line;\n";
			next;
		}

		print $target $line;

		while ($line = <FH>) {
			if ($line =~ /\)\s*$/o) {
				chomp $line;
				print $target "$line;\n";
				last;
			}
			print $target $line;
		}
	}

	close(FH);
}

if ($public_file != $private_file) {
	print_header($private_fd, $private_define);
}
print_header($public_fd, $public_define);
process_file($public_fd, $private_fd, $_) foreach (@ARGV);
print_footer($public_fd, $public_define);
if ($public_file != $private_file) {
	print_footer($private_fd, $private_define);
}