-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjj_plugin.c
74 lines (63 loc) · 2.62 KB
/
jj_plugin.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
66
67
68
69
70
71
72
73
74
/*
* j2 - A simple concatenative programming language that can understand
* the structure of and dynamically link with compatible C binaries.
*
* Copyright (C) 2011 Jason Nyberg <[email protected]>
* Copyright (C) 2018 Jason Nyberg <[email protected]> (dual-licensed)
* (C) Copyright 2019 Hewlett Packard Enterprise Development LP.
*
* This file is part of j2.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either
*
* * the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at
* your option) any later version
*
* or
*
* * the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at
* your option) any later version
*
* or both in parallel, as here.
*
* j2 is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received copies of the GNU General Public License and
* the GNU Lesser General Public License along with this program. If
* not, see <http://www.gnu.org/licenses/>.
*/
#ifdef DIRECT_VM // see CMakeLists.txt: prelink, no bootstrap script
#include "vm.h"
const char *bootstrap=
"[@input_stream [brl(input_stream) ! lambda!]@lambda lambda! |]@repl\n" // define repl
"ROOT<repl(get_stdin())> [RETURN] ARG0 @"; // read from stdin
int main(int argc, char *argv[]) { return vm_bootstrap(argc>1?argv[1]:(char *) bootstrap); }
#else // link w/dlopen and dynamically load reflection module, and run via bootstrap script
#include <stdio.h>
#include <dlfcn.h> // dlopen/dlsym/dlclose
const char *bootstrap =
"[@input_stream [brl(input_stream) ! lambda!]@lambda lambda! |]@repl\n" // define repl
"ROOT<repl([bootstrap.edict] [r] file_open!)> [RETURN] ARG0 @";
int
main(int argc, char *argv[]) {
int status = 0;
void *dlhandle = NULL;
dlerror(); // reset
printf("Running J2 via plugin, dynamically linking libreflect.so...\n");
if (dlhandle = dlopen("libreflect.so", RTLD_LAZY | RTLD_GLOBAL | RTLD_NODELETE | RTLD_DEEPBIND)) {
dlerror(); // reset
int *(*vm_bootstrap)(char *) = dlsym(dlhandle, "vm_bootstrap");
if (vm_bootstrap) vm_bootstrap(argc > 1 ? argv[1] : (char *)bootstrap);
dlclose(dlhandle);
} else {
printf("dlopen libreflect.so failed, error %s\n", dlerror());
}
return status;
}
#endif // DIRECT_VM