cortav  Diff

Differences From Artifact [cea771ab78]:

To Artifact [f7d9ea5be8]:


    31     31   
    32     32   	// load and run our payload
    33     33   	int e = luaL_loadbufferx(l, ct_bytecode, sizeof(ct_bytecode), "cortav", "b");
    34     34   	if (e != LUA_OK) {
    35     35   		printf("some kind of error idk fam\n");
    36     36   		return -1;
    37     37   	}
           38  +
           39  +	// create the native interface table, if nothing
           40  +	// else to signal that cortav is running under a
           41  +	// binary shim
           42  +	lua_pushglobaltable(l);
           43  +		lua_newtable(l);
           44  +			_lua_env_setup
           45  +		lua_setfield(l, -2, "native");
           46  +	lua_pop(l, 1);
    38     47   
    39     48   	if (lua_pcall(l, 0, 0, 0) != LUA_OK) {
    40     49   		size_t len;
    41     50   		const char* msg = luaL_tolstring(l, -1, &len);
    42     51   		if (isatty(2)) {
    43     52   			fprintf(stderr, "\33[31;1m(fatal)\33[m %.*s\n", (int)len, msg);
    44     53   		} else {
................................................................................
    60     69   		io.stderr:write('(' .. arg[0]..' fatal) cannot open file '..arg[i])
    61     70   	end
    62     71   	return dflt
    63     72   end
    64     73   
    65     74   local src = setfile(1, io.stdin, "rb")
    66     75   local dest = setfile(2, io.stdout, "w")
           76  +local binds = {}
           77  +for i=3,#arg do
           78  +	io.stderr:write(string.format("(%s  info) including loader for bind %s\n", arg[0], arg[i]))
           79  +	table.insert(binds, arg[i])
           80  +end
    67     81   
    68     82   local cstr = {}
    69     83   local strtpl = [[static char ct_bytecode [%u] = {
    70     84   %s
    71     85   };]]
           86  +
           87  +local bindfn = [[#define _lua_env_setup]]
           88  +
           89  +if next(binds) then
           90  +	-- make cpp do our interpolation for us
           91  +	bindfn = [[#define _lua_env_setup init_binds(l);]]
           92  +	local calls = {}
           93  +	local decls = {}
           94  +	local ctpl = [[
           95  +	luaopen_bind_%s(l);
           96  +	lua_setfield(l, -2, "%s");
           97  +]]
           98  +	for n,b in ipairs(binds) do
           99  +		table.insert(decls, string.format('extern int luaopen_bind_%s(lua_State* l);\n', b))
          100  +		table.insert(calls, string.format(ctpl, b, b))
          101  +	end
          102  +	local hdr = [[
          103  +static void init_binds(lua_State* l) {
          104  +]]
          105  +	local foot = [[
          106  +}
          107  +]]
          108  +	bindfn = bindfn .. '\n'
          109  +		.. table.concat(decls)
          110  +		.. hdr
          111  +			.. table.concat(calls)
          112  +		.. foot
          113  +end
    72    114   
    73    115   local bytes = {}
    74    116   
    75    117   local bn = 1
    76    118   local len = 0
    77    119   while true do
    78    120   	local byte = src:read(1)
................................................................................
    90    132   	bytes[bn] = str
    91    133   	bn = bn + 1
    92    134   end
    93    135   
    94    136   local lines = {
    95    137   	includes;
    96    138   	strtpl:format(#bytes, table.concat(bytes));
          139  +	bindfn;
    97    140   	main;
    98    141   }
    99    142   
   100    143   dest:write(table.concat(lines, '\n'))