summaryrefslogtreecommitdiff
path: root/source4/script/mkproto.pl
blob: 662fda1f4160d01f397906fe989a03c29ce78bbf (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
#!/usr/bin/perl

use strict;
use warnings;

my $header_name = '_PROTO_H_';

if ($ARGV[0] eq '-h') {
	shift @ARGV;
	$header_name = shift @ARGV;
}


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

sub print_footer {
	printf "\n#endif /*  %s  */\n", $header_name;
}


sub handle_loadparm {
	my $line = shift;

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

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

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

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


sub process_files {
	my $line;
	my $inheader;
	my $gotstart;

      FILE: foreach my $filename (@ARGV) {
		next FILE unless (open(FH, "< $filename")); # skip over file unless it can be opened
		print "\n/* The following definitions come from $filename  */\n\n";

		$inheader = 0;
		$gotstart = 0;
	      LINE: while (defined($line = <FH>)) {

			if ($inheader) {
				# this chomp is somewhat expensive, so don't do it unless we know
				# that we probably want to use it
				chomp $line;
				if ($line =~ /\)\s*$/o) {
					$inheader = 0;
					print "$line;\n";
				} else {
					print "$line\n";
				}
				next LINE;
			}

			$gotstart = 0;

			# ignore static and extern declarations
			if ($line =~ /^static|^extern/o ||
			    $line !~ /^[a-zA-Z]/o ||
			    $line =~ /[;]/o) {
				next LINE;
			}


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


			# I'm going to leave these as is for now - perl can probably handle larger regex, though -- vance
			# I've also sort of put these in approximate order of most commonly called

			if ( $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
			     /x) {
				$gotstart = 1;
			}


			# goto next line if we don't have a start
			next LINE unless $gotstart;

			if ( $line =~ /\(.*\)\s*$/o ) {
			# now that we're here, we know we
				chomp $line;
				print "$line;\n";
				next LINE;
			}
			elsif ( $line =~ /\(/o ) {

				$inheader = 1;
				# line hasn't been chomped, so we can assume it already has the \n
				print $line;
				next LINE;
			}
		}
	}
}

print_header();
process_files();
print_footer();