BEGIN {
aborting = 0
if(ARGC != 3) {
aborting = 1
exit 1
}
name = ARGV[1]
gsub(/^.*\//, "", name)
gsub(/\.c$/, "", name)
name = ARGV[2] "_" name
gsub(/\//, "_", name)
ARGC--
printf "E#ifndef have_%s_globals\n", name
printf "E#define have_%s_globals\n", name
printf "E\n"
}
/^\/\*\*\/$/ {
line = ""
isfunc = 0
while(1) {
if(getline <= 0) {
aborting = 1
exit 1
}
if (line == "" && $0 ~ /^[ \t]* printf "E%s\n", $0
printf "L%s\n", $0
next
}
gsub(/\t/, " ")
line = line " " $0
gsub(/\/\*([^*]|\*+[^*\/])*\*+\//, " ", line)
if(line ~ /\/\*/)
continue
if(line ~ /\) *(VA_DCL )*[{].*$/) isfunc = 1
if(sub(/ *[{;].*$/, "", line)) break
}
if (!match(line, /VA_ALIST/)) {
while(match(line, /[^_0-9A-Za-z ][_0-9A-Za-z]/) ||
match(line, /[_0-9A-Za-z][^_0-9A-Za-z ]/))
line = substr(line, 1, RSTART) " " substr(line, RSTART+1)
}
gsub(/ _ +/, " _ ", line)
while(1) {
if(isfunc && match(line, /\([^()]*\)$/))
line = substr(line, 1, RSTART-1) " _ (" substr(line, RSTART) ")"
else if(match(line, / _ \(\([^,()]*,/))
line = substr(line, 1, RSTART+RLENGTH-2) "@!" substr(line, RSTART+RLENGTH)
else if(match(line, / _ \(\([^,()]*\)\)/))
line = substr(line, 1, RSTART-1) "@{" substr(line, RSTART+5, RLENGTH-7) "@}" substr(line, RSTART+RLENGTH)
else if(match(line, /\([^,()]*\)/))
line = substr(line, 1, RSTART-1) "@<" substr(line, RSTART+1, RLENGTH-2) "@>" substr(line, RSTART+RLENGTH)
else
break
}
sub(/^ */, "", line)
match(line, /^((const|enum|mod_export|static|struct|union) +)*([_0-9A-Za-z]+ +|((char|double|float|int|long|short|unsigned|void) +)+)((const|static) +)*/)
dtype = substr(line, 1, RLENGTH)
sub(/ *$/, "", dtype)
if(" " dtype " " ~ / static /)
locality = "L"
else
locality = "E"
exported = " " dtype " " ~ / mod_export /
line = substr(line, RLENGTH+1) ","
if (match(line, /VA_ALIST/)) {
gsub(/@[{]/, "((", line)
gsub(/@}/, "))", line)
gsub(/@</, "(", line)
gsub(/@>/, ")", line)
gsub(/@!/, ",", line)
sub(/,$/, ";", line)
gsub(/mod_export/, "mod_import_function", dtype)
gsub(/VA_ALIST/, "VA_ALIST_PROTO", line)
sub(/ VA_DCL/, "", line)
if(locality ~ /E/)
dtype = "extern " dtype
if (match(line, /[_0-9A-Za-z]+\(VA_ALIST/))
dnam = substr(line, RSTART, RLENGTH-9)
if (exported)
printf "X%s\n", dnam
printf "%s%s %s\n", locality, dtype, line
} else {
while(match(line, /^[^,]*,/)) {
dcltor = substr(line, 1, RLENGTH-1)
line = substr(line, RLENGTH+1)
sub(/\=.*$/, "", dcltor)
match(dcltor, /^([^_0-9A-Za-z]| const )*/)
dcltor = substr(dcltor, 1, RLENGTH) "@+" substr(dcltor, RLENGTH+1)
match(dcltor, /^.*@\+[_0-9A-Za-z]+/)
dcltor = substr(dcltor, 1, RLENGTH) "@-" substr(dcltor, RLENGTH+1)
dnam = dcltor
sub(/^.*@\+/, "", dnam)
sub(/@-.*$/, "", dnam)
gsub(/@[{]/, " _((", dcltor)
gsub(/@}/, "))", dcltor)
gsub(/@</, "(", dcltor)
gsub(/@>/, ")", dcltor)
gsub(/@!/, ",", dcltor)
if(exported)
printf "X%s\n", dnam
dcl = dtype " " dcltor ";"
if(locality ~ /E/)
dcl = "extern " dcl
if(isfunc)
gsub(/ mod_export /, " mod_import_function ", dcl)
else
gsub(/ mod_export /, " mod_import_variable ", dcl)
gsub(/@[+-]/, "", dcl)
gsub(/ +/, " ", dcl)
while(match(dcl, /[^_0-9A-Za-z] ./) || match(dcl, /. [^_0-9A-Za-z]/))
dcl = substr(dcl, 1, RSTART) substr(dcl, RSTART+2)
printf "%s%s\n", locality, dcl
}
}
}
END {
if(aborting)
exit 1
printf "E\n"
printf "E#endif /* !have_%s_globals */\n", name
}