2000-01-09 20:58:00 +00:00
|
|
|
# Make prototypes from .c files
|
2008-05-07 13:39:42 +00:00
|
|
|
# $Id: make-proto.pl 14183 2004-09-03 08:50:57Z lha $
|
2000-01-09 20:58:00 +00:00
|
|
|
|
|
|
|
##use Getopt::Std;
|
|
|
|
require 'getopts.pl';
|
|
|
|
|
|
|
|
$brace = 0;
|
|
|
|
$line = "";
|
|
|
|
$debug = 0;
|
2002-08-30 21:23:27 +00:00
|
|
|
$oproto = 1;
|
|
|
|
$private_func_re = "^_";
|
2000-01-09 20:58:00 +00:00
|
|
|
|
2008-05-07 13:39:42 +00:00
|
|
|
do Getopts('x:m:o:p:dqE:R:P:') || die "foo";
|
2000-01-09 20:58:00 +00:00
|
|
|
|
|
|
|
if($opt_d) {
|
|
|
|
$debug = 1;
|
|
|
|
}
|
|
|
|
|
2002-08-30 21:23:27 +00:00
|
|
|
if($opt_q) {
|
|
|
|
$oproto = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($opt_R) {
|
|
|
|
$private_func_re = $opt_R;
|
|
|
|
}
|
2008-05-07 13:39:42 +00:00
|
|
|
%flags = (
|
|
|
|
'multiline-proto' => 1,
|
|
|
|
'header' => 1,
|
|
|
|
'function-blocking' => 0,
|
|
|
|
'gnuc-attribute' => 1,
|
|
|
|
'cxx' => 1
|
|
|
|
);
|
|
|
|
if($opt_m) {
|
|
|
|
foreach $i (split(/,/, $opt_m)) {
|
|
|
|
if($i eq "roken") {
|
|
|
|
$flags{"multiline-proto"} = 0;
|
|
|
|
$flags{"header"} = 0;
|
|
|
|
$flags{"function-blocking"} = 0;
|
|
|
|
$flags{"gnuc-attribute"} = 0;
|
|
|
|
$flags{"cxx"} = 0;
|
|
|
|
} else {
|
|
|
|
if(substr($i, 0, 3) eq "no-") {
|
|
|
|
$flags{substr($i, 3)} = 0;
|
|
|
|
} else {
|
|
|
|
$flags{$i} = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($opt_x) {
|
|
|
|
open(EXP, $opt_x);
|
|
|
|
while(<EXP>) {
|
|
|
|
chomp;
|
|
|
|
s/\#.*//g;
|
|
|
|
s/\s+/ /g;
|
|
|
|
if(/^([a-zA-Z0-9_]+)\s?(.*)$/) {
|
|
|
|
$exported{$1} = $2;
|
|
|
|
} else {
|
|
|
|
print $_, "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close EXP;
|
|
|
|
}
|
2002-08-30 21:23:27 +00:00
|
|
|
|
2000-01-09 20:58:00 +00:00
|
|
|
while(<>) {
|
|
|
|
print $brace, " ", $_ if($debug);
|
|
|
|
if(/^\#if 0/) {
|
|
|
|
$if_0 = 1;
|
|
|
|
}
|
|
|
|
if($if_0 && /^\#endif/) {
|
|
|
|
$if_0 = 0;
|
|
|
|
}
|
|
|
|
if($if_0) { next }
|
|
|
|
if(/^\s*\#/) {
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
if(/^\s*$/) {
|
|
|
|
$line = "";
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
if(/\{/){
|
2002-02-19 15:46:56 +00:00
|
|
|
if (!/\}/) {
|
|
|
|
$brace++;
|
|
|
|
}
|
2000-01-09 20:58:00 +00:00
|
|
|
$_ = $line;
|
|
|
|
while(s/\*\//\ca/){
|
|
|
|
s/\/\*(.|\n)*\ca//;
|
|
|
|
}
|
|
|
|
s/^\s*//;
|
2003-10-09 19:36:20 +00:00
|
|
|
s/\s*$//;
|
2000-01-09 20:58:00 +00:00
|
|
|
s/\s+/ /g;
|
2003-10-09 19:36:20 +00:00
|
|
|
if($_ =~ /\)$/){
|
2000-01-09 20:58:00 +00:00
|
|
|
if(!/^static/ && !/^PRIVATE/){
|
|
|
|
if(/(.*)(__attribute__\s?\(.*\))/) {
|
|
|
|
$attr = $2;
|
|
|
|
$_ = $1;
|
|
|
|
} else {
|
|
|
|
$attr = "";
|
|
|
|
}
|
|
|
|
# remove outer ()
|
2002-08-30 21:23:27 +00:00
|
|
|
s/\s*\(/</;
|
|
|
|
s/\)\s?$/>/;
|
2000-01-09 20:58:00 +00:00
|
|
|
# remove , within ()
|
|
|
|
while(s/\(([^()]*),(.*)\)/($1\$$2)/g){}
|
2002-08-30 21:23:27 +00:00
|
|
|
s/\<\s*void\s*\>/<>/;
|
|
|
|
# remove parameter names
|
|
|
|
if($opt_P eq "remove") {
|
|
|
|
s/(\s*)([a-zA-Z0-9_]+)([,>])/$3/g;
|
2008-05-07 13:39:42 +00:00
|
|
|
s/\s+\*/*/g;
|
2002-08-30 21:23:27 +00:00
|
|
|
s/\(\*(\s*)([a-zA-Z0-9_]+)\)/(*)/g;
|
|
|
|
} elsif($opt_P eq "comment") {
|
|
|
|
s/([a-zA-Z0-9_]+)([,>])/\/\*$1\*\/$2/g;
|
|
|
|
s/\(\*([a-zA-Z0-9_]+)\)/(*\/\*$1\*\/)/g;
|
|
|
|
}
|
|
|
|
s/\<\>/<void>/;
|
|
|
|
# add newlines before parameters
|
2008-05-07 13:39:42 +00:00
|
|
|
if($flags{"multiline-proto"}) {
|
|
|
|
s/,\s*/,\n\t/g;
|
|
|
|
} else {
|
|
|
|
s/,\s*/, /g;
|
|
|
|
}
|
2000-01-09 20:58:00 +00:00
|
|
|
# fix removed ,
|
|
|
|
s/\$/,/g;
|
|
|
|
# match function name
|
2002-08-30 21:23:27 +00:00
|
|
|
/([a-zA-Z0-9_]+)\s*\</;
|
2000-01-09 20:58:00 +00:00
|
|
|
$f = $1;
|
2002-08-30 21:23:27 +00:00
|
|
|
if($oproto) {
|
|
|
|
$LP = "__P((";
|
|
|
|
$RP = "))";
|
|
|
|
} else {
|
|
|
|
$LP = "(";
|
|
|
|
$RP = ")";
|
|
|
|
}
|
2000-01-09 20:58:00 +00:00
|
|
|
# only add newline if more than one parameter
|
2008-05-07 13:39:42 +00:00
|
|
|
if($flags{"multiline-proto"} && /,/){
|
2002-08-30 21:23:27 +00:00
|
|
|
s/\</ $LP\n\t/;
|
2000-01-09 20:58:00 +00:00
|
|
|
}else{
|
2002-08-30 21:23:27 +00:00
|
|
|
s/\</ $LP/;
|
2000-01-09 20:58:00 +00:00
|
|
|
}
|
2002-08-30 21:23:27 +00:00
|
|
|
s/\>/$RP/;
|
2000-01-09 20:58:00 +00:00
|
|
|
# insert newline before function name
|
2008-05-07 13:39:42 +00:00
|
|
|
if($flags{"multiline-proto"}) {
|
|
|
|
s/(.*)\s([a-zA-Z0-9_]+ \Q$LP\E)/$1\n$2/;
|
|
|
|
}
|
2000-01-09 20:58:00 +00:00
|
|
|
if($attr ne "") {
|
|
|
|
$_ .= "\n $attr";
|
|
|
|
}
|
|
|
|
$_ = $_ . ";";
|
|
|
|
$funcs{$f} = $_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$line = "";
|
|
|
|
}
|
|
|
|
if(/\}/){
|
|
|
|
$brace--;
|
|
|
|
}
|
|
|
|
if(/^\}/){
|
|
|
|
$brace = 0;
|
|
|
|
}
|
|
|
|
if($brace == 0) {
|
|
|
|
$line = $line . " " . $_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub foo {
|
|
|
|
local ($arg) = @_;
|
|
|
|
$_ = $arg;
|
|
|
|
s/.*\/([^\/]*)/$1/;
|
|
|
|
s/[^a-zA-Z0-9]/_/g;
|
|
|
|
"__" . $_ . "__";
|
|
|
|
}
|
|
|
|
|
|
|
|
if($opt_o) {
|
|
|
|
open(OUT, ">$opt_o");
|
|
|
|
$block = &foo($opt_o);
|
|
|
|
} else {
|
|
|
|
$block = "__public_h__";
|
|
|
|
}
|
|
|
|
|
|
|
|
if($opt_p) {
|
|
|
|
open(PRIV, ">$opt_p");
|
|
|
|
$private = &foo($opt_p);
|
|
|
|
} else {
|
|
|
|
$private = "__private_h__";
|
|
|
|
}
|
|
|
|
|
|
|
|
$public_h = "";
|
|
|
|
$private_h = "";
|
|
|
|
|
2008-05-07 13:39:42 +00:00
|
|
|
$public_h_header .= "/* This is a generated file */
|
2000-01-09 20:58:00 +00:00
|
|
|
#ifndef $block
|
|
|
|
#define $block
|
|
|
|
|
2002-08-30 21:23:27 +00:00
|
|
|
";
|
|
|
|
if ($oproto) {
|
2008-05-07 13:39:42 +00:00
|
|
|
$public_h_header .= "#ifdef __STDC__
|
2000-01-09 20:58:00 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#ifndef __P
|
|
|
|
#define __P(x) x
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#ifndef __P
|
|
|
|
#define __P(x) ()
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
";
|
2002-08-30 21:23:27 +00:00
|
|
|
} else {
|
|
|
|
$public_h_header .= "#include <stdarg.h>
|
|
|
|
|
|
|
|
";
|
|
|
|
}
|
2008-05-07 13:39:42 +00:00
|
|
|
$public_h_trailer = "";
|
2000-01-09 20:58:00 +00:00
|
|
|
|
|
|
|
$private_h_header = "/* This is a generated file */
|
|
|
|
#ifndef $private
|
|
|
|
#define $private
|
|
|
|
|
2002-08-30 21:23:27 +00:00
|
|
|
";
|
|
|
|
if($oproto) {
|
2008-05-07 13:39:42 +00:00
|
|
|
$private_h_header .= "#ifdef __STDC__
|
2000-01-09 20:58:00 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#ifndef __P
|
|
|
|
#define __P(x) x
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#ifndef __P
|
|
|
|
#define __P(x) ()
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
";
|
2002-08-30 21:23:27 +00:00
|
|
|
} else {
|
|
|
|
$private_h_header .= "#include <stdarg.h>
|
2000-01-09 20:58:00 +00:00
|
|
|
|
2002-08-30 21:23:27 +00:00
|
|
|
";
|
|
|
|
}
|
2008-05-07 13:39:42 +00:00
|
|
|
$private_h_trailer = "";
|
|
|
|
|
2000-01-09 20:58:00 +00:00
|
|
|
foreach(sort keys %funcs){
|
|
|
|
if(/^(main)$/) { next }
|
2008-05-07 13:39:42 +00:00
|
|
|
if(!defined($exported{$_}) && /$private_func_re/) {
|
2000-01-09 20:58:00 +00:00
|
|
|
$private_h .= $funcs{$_} . "\n\n";
|
|
|
|
if($funcs{$_} =~ /__attribute__/) {
|
|
|
|
$private_attribute_seen = 1;
|
|
|
|
}
|
|
|
|
} else {
|
2008-05-07 13:39:42 +00:00
|
|
|
if($flags{"function-blocking"}) {
|
|
|
|
$fupper = uc $_;
|
|
|
|
if($exported{$_} =~ /proto/) {
|
|
|
|
$public_h .= "#if !defined(HAVE_$fupper) || defined(NEED_${fupper}_PROTO)\n";
|
|
|
|
} else {
|
|
|
|
$public_h .= "#ifndef HAVE_$fupper\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$public_h .= $funcs{$_} . "\n";
|
2000-01-09 20:58:00 +00:00
|
|
|
if($funcs{$_} =~ /__attribute__/) {
|
|
|
|
$public_attribute_seen = 1;
|
|
|
|
}
|
2008-05-07 13:39:42 +00:00
|
|
|
if($flags{"function-blocking"}) {
|
|
|
|
$public_h .= "#endif\n";
|
|
|
|
}
|
|
|
|
$public_h .= "\n";
|
2000-01-09 20:58:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-07 13:39:42 +00:00
|
|
|
if($flags{"gnuc-attribute"}) {
|
|
|
|
if ($public_attribute_seen) {
|
|
|
|
$public_h_header .= "#if !defined(__GNUC__) && !defined(__attribute__)
|
2000-01-09 20:58:00 +00:00
|
|
|
#define __attribute__(x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
";
|
2008-05-07 13:39:42 +00:00
|
|
|
}
|
2000-01-09 20:58:00 +00:00
|
|
|
|
2008-05-07 13:39:42 +00:00
|
|
|
if ($private_attribute_seen) {
|
|
|
|
$private_h_header .= "#if !defined(__GNUC__) && !defined(__attribute__)
|
2000-01-09 20:58:00 +00:00
|
|
|
#define __attribute__(x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
";
|
2008-05-07 13:39:42 +00:00
|
|
|
}
|
2000-01-09 20:58:00 +00:00
|
|
|
}
|
2008-05-07 13:39:42 +00:00
|
|
|
if($flags{"cxx"}) {
|
|
|
|
$public_h_header .= "#ifdef __cplusplus
|
|
|
|
extern \"C\" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
";
|
|
|
|
$public_h_trailer .= "#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
";
|
|
|
|
|
|
|
|
}
|
|
|
|
if ($opt_E) {
|
|
|
|
$public_h_header .= "#ifndef $opt_E
|
|
|
|
#if defined(_WIN32)
|
|
|
|
#define $opt_E _stdcall
|
|
|
|
#else
|
|
|
|
#define $opt_E
|
|
|
|
#endif
|
|
|
|
#endif
|
2000-01-09 20:58:00 +00:00
|
|
|
|
2008-05-07 13:39:42 +00:00
|
|
|
";
|
|
|
|
|
|
|
|
$private_h_header .= "#ifndef $opt_E
|
|
|
|
#if defined(_WIN32)
|
|
|
|
#define $opt_E _stdcall
|
|
|
|
#else
|
|
|
|
#define $opt_E
|
|
|
|
#endif
|
|
|
|
#endif
|
2000-01-09 20:58:00 +00:00
|
|
|
|
2008-05-07 13:39:42 +00:00
|
|
|
";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($public_h ne "" && $flags{"header"}) {
|
|
|
|
$public_h = $public_h_header . $public_h .
|
|
|
|
$public_h_trailer . "#endif /* $block */\n";
|
2000-01-09 20:58:00 +00:00
|
|
|
}
|
2008-05-07 13:39:42 +00:00
|
|
|
if ($private_h ne "" && $flags{"header"}) {
|
|
|
|
$private_h = $private_h_header . $private_h .
|
|
|
|
$private_h_trailer . "#endif /* $private */\n";
|
2000-01-09 20:58:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if($opt_o) {
|
|
|
|
print OUT $public_h;
|
|
|
|
}
|
|
|
|
if($opt_p) {
|
|
|
|
print PRIV $private_h;
|
|
|
|
}
|
|
|
|
|
|
|
|
close OUT;
|
|
|
|
close PRIV;
|