-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstage8.5.c
65 lines (53 loc) · 2.29 KB
/
stage8.5.c
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
#include <EXTERN.h>
#include <perl.h>
static char *do_call(PerlInterpreter *my_perl, char *foo) {
dSP; /* initialize stack pointer */
ENTER; /* everything created after here */
SAVETMPS; /* ...is a temporary variable. */
PUSHMARK(SP); /* remember the stack pointer */
XPUSHs(sv_2mortal(Perl_newSVpv(my_perl, foo, strlen(foo)))); /* push foo onto the stack */
PUTBACK; /* make local stack pointer global */
call_pv("blep", G_SCALAR); /* call blep */
SPAGAIN; /* refresh stack pointer */
SV *svp = POPs; /* pop the return value from stack */
char *rv = strdup(SvPVx_nolen(svp));
PUTBACK;
FREETMPS; /* free that return value */
LEAVE; /* ...and the XPUSHed "mortal" args. */
return rv;
}
/* see https://www.xav.com/perl/lib/Pod/perlembed.html */
EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
static void xs_init(pTHX) {
char *file = __FILE__;
/* DynaLoader is a special case */
newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
}
/* HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK *
* K HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK H */
void Perl_xs_apiversion_bootcheck(pTHX_ SV *module, const char *api_p, STRLEN api_len) {}
char *stage8h_blep(char *foo) {
printf("Stage 8.5: %s\n", foo);
PerlInterpreter *my_perl; /* NOTE: Due to shitty programming, this variable *must* have this name. Fuck Perl. */
int argc1 = 0;
char **argv1 = NULL;
char *argv2[] = {(char*)"", (char*)"stage9.pl"}; /* peeeerl */
PERL_SYS_INIT(&argc1, &argv1);
my_perl = perl_alloc();
perl_construct(my_perl);
perl_parse(my_perl, xs_init, 2, argv2, (char **)NULL);
PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
perl_run(my_perl);
char *rv = do_call(my_perl, foo);
perl_destruct(my_perl);
perl_free(my_perl);
PERL_SYS_TERM();
printf("Return value[8.5]: %s\n", rv);
return rv;
}
int main(int argc, char **argv) {
if (argc < 2)
return 1;
printf("%s\n", stage8h_blep(argv[1]));
return 0;
}