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
|
#!/usr/bin/perl
use Getopt::Std;
our $header_tmpl = <<'HTMPL';
#include <stdio.h>
#define __USE_GNU // This is needed for the RTLD_NEXT definition
#include <stdlib.h>
#include <dlfcn.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
#include <netinet/in.h>
#include <resolv.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
HTMPL
our $func_tmpl = <<'FTMPL';
%ret%
%func%(%args%)
{
int (*lib_%func%)(%args%);
char *error;
lib_%func% = dlsym(RTLD_NEXT, "%func%");
if ((error = dlerror()) != NULL) {
fprintf(stderr, "unable to load %func%: %s\n", error);
exit(1);
}
fprintf(stderr, "libval_shim: %func% called: pass-thru\n");
return (%ret%)lib_%func%(%vals%);
}
FTMPL
our $nfunc_tmpl = <<'NFTMPL';
%ret%
%func%(%args%)
{
// int (*lib_%func%)(%args%);
// char *error;
// lib_%func% = dlsym(RTLD_NEXT, "%func%");
//
// if ((error = dlerror()) != NULL) {
// fprintf(stderr, "unable to load %func%: %s\n", error);
// exit(1);
// }
fprintf(stderr, "libval_shim: %func%: called: not-avail\n");
return (%ret%)NULL;
}
NFTMPL
getopts("Ff:o:");
our $infile = $opt_f || "./libval_shim.funcs";
our $outfile = $opt_o || "./libval_shim.c";
our $force = $opt_F;
open(IN,"<$infile") or die "unable to open \"$infile\" for input\n";
if (-f $outfile and not $force) {
die "\"$outfile\" exists and would be overwritten. aborting..\n";
}
open(OUT,">$outfile") or die "unable to open \"$outfile\" for output\n";
while (<IN>) {
next if /^\s*\#/ or /^\s*$/;
my ($ret, $func, $args, $not_avail) = /^\s*(.*?)\s*(\w+)\s*\(\s*([^\)]+)\s*\)\s*\;\s*(\%not-avail\%)?\s*$/;
my $vals = join(', ', map(/^.*?(\w+)$/, split(/\s*,\s*/, $args)));
undef $vals if $vals =~ /^\s*void\s*$/;
push(@funcs, [$ret, $func, $args, $vals, $not_avail])
}
close(IN);
print OUT $header_tmpl;
foreach $func (@funcs) {
$out = ($func->[4] ? $nfunc_tmpl : $func_tmpl);
$out =~ s/\%ret\%/$func->[0]/g;
$out =~ s/\%func\%/$func->[1]/g;
$out =~ s/\%args\%/$func->[2]/g;
$out =~ s/\%vals\%/$func->[3]/g;
print OUT $out;
}
close(OUT);
|