Esempio n. 1
0
int vm_hw_lua_cpu_handle_irq_get(lua_State* L)
{
	// Table is at 1, key is at 2.
	vm_t* vm = vm_hw_lua_cpu_extract_cpu(L, 1);
	uint16_t idx;
	bstring enabled = bfromcstr("enabled");
	if (lua_isstring(L, 2) && biseqcstrcaseless(enabled, lua_tostring(L, 2)))
	{
		lua_pushboolean(L, vm->queue_interrupts);
		return 1;
	}
	else if (!lua_isnumber(L, 2))
	{
		lua_pushstring(L, "irq access must be by numeric value (use array operator) or \"enabled\" field");
		lua_error(L);
		return 0;
	}
	idx = (uint16_t)lua_tonumber(L, 2);
	if (idx < 1 || idx > vm->irq_count)
	{
		lua_pushstring(L, "irq access out of bounds");
		lua_error(L);
		return 0;
	}
	lua_pushnumber(L, vm->irq[idx]);
	return 1;
}
Esempio n. 2
0
///
/// Performs preprocessing.
///
void ppimpl(freed_bstring filename, int line, freed_bstring lang, has_t has_input, pop_t input, push_t output)
{
    state_t state;
    list_init(&state.cached_input);
    list_init(&state.cached_output);
    list_init(&state.handlers);
    list_init(&state.scopes);
    list_attributes_comparator(&state.cached_input, list_comparator_char_t);
    list_attributes_comparator(&state.cached_output, list_comparator_char_t);
    list_attributes_copy(&state.cached_input, list_meter_char_t, false);
    list_attributes_copy(&state.cached_output, list_meter_char_t, false);
    list_attributes_hash_computer(&state.cached_input, list_hashcomputer_char_t);
    list_attributes_hash_computer(&state.cached_output, list_hashcomputer_char_t);
    state.has_input = has_input;
    state.input = input;
    state.output = output;
    state.current_line = line;
    state.current_filename = bstrcpy(filename.ref);
    state.default_filename = bfromcstr("<unknown>");
    state.in_single_string = false;
    state.in_double_string = false;
    if (biseqcstrcaseless(lang.ref, "asm"))
    {
        ppimpl_asm_line_register(&state);
        ppimpl_asm_expr_register(&state);
        ppimpl_asm_define_register(&state);
        ppimpl_asm_include_register(&state);
        ppimpl_asm_lua_register(&state);
        ppimpl_asm_init(&state);
    }
    else if (biseqcstrcaseless(lang.ref, "c"))
    {
        ppimpl_c_line_register(&state);
        ppimpl_c_expr_register(&state);
        ppimpl_c_define_register(&state);
        ppimpl_c_include_register(&state);
        ppimpl_c_init(&state);
    }
    ppimpl_process(&state);
    bautodestroy(lang);
    bautodestroy(filename);
}
Esempio n. 3
0
static inline void check_should_close(Connection *conn, Request *req)
{
    if(req->version && biseqcstr(req->version, "HTTP/1.0")) {
        debug("HTTP 1.0 request coming in from %s", conn->remote);
        conn->close = 1;
    } else {
        bstring conn_close = Request_get(req, &HTTP_CONNECTION);

        if(conn_close && biseqcstrcaseless(conn_close, "close")) {
            conn->close = 1;
        } else {
            conn->close = 0;
        }
    }
}
Esempio n. 4
0
struct lua_hardware* vm_hw_lua_load(vm_t* vm, bstring name)
{
	bstring path, modtype;
	struct lua_hardware* hw;
	int module, hwinfo;

	// Calculate full path to file.
	path = osutil_getarg0path();
#ifdef _WIN32
	bcatcstr(path, "modules\\");
#else
	bcatcstr(path, "/modules/");
#endif
	bconcat(path, name);

	// Create the new lua hardware structure.
	hw = malloc(sizeof(struct lua_hardware));
	hw->vm = vm;
	hw->state = lua_open();
	assert(hw->state != NULL);
	luaL_openlibs(hw->state);
	luaX_loadexpressionlib(hw->state);

	// Execute the code in the new Lua context.
	if (luaL_dofile(hw->state, path->data) != 0)
	{
		printf("lua error was %s.\n", lua_tostring(hw->state, -1));

		// Return NULL.
		lua_close(hw->state);
		free(hw);
		bdestroy(path);
		return NULL;
	}

	// Load tables.
	lua_getglobal(hw->state, "MODULE");
	module = lua_gettop(hw->state);
	lua_getglobal(hw->state, "HARDWARE");
	hwinfo = lua_gettop(hw->state);

	// Ensure both tables were provided.
	if (lua_isnoneornil(hw->state, module) || lua_isnoneornil(hw->state, hwinfo))
	{
		printf("failed to load hardware from %s.\n", path->data);

		// Return NULL.
		lua_close(hw->state);
		free(hw);
		bdestroy(path);
		return NULL;
	}

	// Check to see whether the module is
	// a hardware module.
	lua_getfield(hw->state, module, "Type");
	modtype = bfromcstr(lua_tostring(hw->state, -1));
	if (!biseqcstrcaseless(modtype, "Hardware"))
	{
		// Return NULL.
		lua_pop(hw->state, 1);
		lua_close(hw->state);
		free(hw);
		bdestroy(modtype);
		bdestroy(path);
		return NULL;
	}
	lua_pop(hw->state, 1);
	bdestroy(modtype);

	// Store information into the Lua
	// hardware structure.
	lua_getfield(hw->state, hwinfo, "ID");
	lua_getfield(hw->state, hwinfo, "Version");
	lua_getfield(hw->state, hwinfo, "Manufacturer");
	hw->device.id = (uint32_t)lua_tonumber(hw->state, lua_gettop(hw->state) - 2);
	hw->device.version = (uint16_t)lua_tonumber(hw->state, lua_gettop(hw->state) - 1);
	hw->device.manufacturer = (uint32_t)lua_tonumber(hw->state, lua_gettop(hw->state));
	lua_pop(hw->state, 3);

	printf("hardware loaded ID: %u, Version: %u, Manufacturer: %u.\n", hw->device.id, hw->device.version, hw->device.manufacturer);

	// Register the hardware.
	hw->device.handler = &vm_hw_lua_interrupt;
	hw->device.userdata = hw;
	vm_hw_register(vm, hw->device);

	// Register the hooks.
	hw->cycle_id = vm_hook_register(vm, &vm_hw_lua_cycle, HOOK_ON_POST_CYCLE, hw);
	hw->write_id = vm_hook_register(vm, &vm_hw_lua_write, HOOK_ON_WRITE, hw);

	// Pop tables from stack.
	lua_pop(hw->state, 2);

	// Return new hardware.
	return hw;
}
Esempio n. 5
0
int main(int argc, char* argv[])
{
    CURL* curl;
    bstring command;
    bstring name;
    bstring modpath;
    int all;

    // Define arguments.
    struct arg_lit* show_help = arg_lit0("h", "help", "Show this help.");
    struct arg_str* cmdopt = arg_str1(NULL, NULL, "<command>", "The command; either 'search', 'install', 'uninstall', 'enable' or 'disable'.");
    struct arg_str* nameopt = arg_str0(NULL, NULL, "<name>", "The name of the module to search for, install, uninstall, enable or disable.");
    struct arg_lit* all_flag = arg_lit0("a", "all", "Apply this command to all available / installed modules.");
    struct arg_lit* verbose = arg_litn("v", NULL, 0, LEVEL_EVERYTHING - LEVEL_DEFAULT, "Increase verbosity.");
    struct arg_lit* quiet = arg_litn("q", NULL,  0, LEVEL_DEFAULT - LEVEL_SILENT, "Decrease verbosity.");
    struct arg_end* end = arg_end(20);
    void* argtable[] = { show_help, cmdopt, all_flag, nameopt, verbose, quiet, end };

    // Parse arguments.
    int nerrors = arg_parse(argc, argv, argtable);

    if (nerrors != 0 || show_help->count != 0 || (all_flag->count == 0 && nameopt->count == 0))
    {
        if (all_flag->count == 0 && nameopt->count == 0)
            printd(LEVEL_ERROR, "error: must have either module name or -a.");
        if (show_help->count != 0)
            arg_print_errors(stderr, end, "mm");

        fprintf(stderr, "syntax:\n    dtmm");
        arg_print_syntax(stderr, argtable, "\n");
        fprintf(stderr, "options:\n");
        arg_print_glossary(stderr, argtable, "    %-25s %s\n");
        arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
        return 1;
    }

    // Set verbosity level.
    debug_setlevel(LEVEL_DEFAULT + verbose->count - quiet->count);
    
    // Show version information.
    version_print(bautofree(bfromcstr("Module Manager")));

    // Set argument 0 and convert parameters.
    osutil_setarg0(bautofree(bfromcstr(argv[0])));
    command = bfromcstr(cmdopt->sval[0]);
    name = bfromcstr(nameopt->sval[0]);

    // Initialize curl or exit.
    curl = curl_easy_init();
    if (!curl)
    {
        printd(LEVEL_ERROR, "unable to initialize curl.\n");
        return 1;
    }

    // Ensure module path exists.
    modpath = osutil_getmodulepath();
    if (modpath == NULL)
    {
        printd(LEVEL_ERROR, "module path does not exist (searched TOOLCHAIN_MODULES and modules/).\n");
        return 1;
    }
    bdestroy(modpath);

    // Convert all flag.
    all = (all_flag->count > 0);

    // If all is set, set the name back to "".
    if (all)
        bassigncstr(name, "");

    // If the name is "all" or "*", handle this as the all
    // boolean flag.
    if (biseqcstr(name, "all") || biseqcstr(name, "*"))
    {
        bassigncstr(name, "");
        all = 1;
        printd(LEVEL_WARNING, "treating name as -a (all) flag");
    }

    if (biseqcstrcaseless(command, "search") || biseqcstrcaseless(command, "se"))
        return do_search(curl, name, all);
    else if (biseqcstrcaseless(command, "install") || biseqcstrcaseless(command, "in"))
    {
        if (all)
            return do_install_all(curl);
        else
            return do_install(curl, name);
    }
    else if (biseqcstrcaseless(command, "uninstall") || biseqcstrcaseless(command, "rm"))
    {
        if (all)
            return do_uninstall_all(curl);
        else
            return do_uninstall(curl, name);
    }
    else if (biseqcstrcaseless(command, "enable") || biseqcstrcaseless(command, "en"))
    {
        if (all)
            return do_enable_all(curl);
        else
            return do_enable(curl, name);
    }
    else if (biseqcstrcaseless(command, "disable") || biseqcstrcaseless(command, "di") || biseqcstrcaseless(command, "dis"))
    {
        if (all)
            return do_disable_all(curl);
        else
            return do_disable(curl, name);
    }
    else
    {
        printd(LEVEL_ERROR, "unknown command (must be search, install, uninstall, enable or disable).");
        return 1;
    }

    return 0;
}
Esempio n. 6
0
struct lua_debugst* dbg_lua_load(bstring name)
{
    bstring path, modtype;
    struct lua_debugst* ds;
    int module;

    // Calculate full path to file.
    path = osutil_getmodulepath();
    bconchar(path, '/');
    bconcat(path, name);

    // Create the new lua preprocessor structure.
    ds = malloc(sizeof(struct lua_debugst));
    ds->state = lua_open();
    assert(ds->state != NULL);
    luaL_openlibs(ds->state);
    luaX_loadexpressionlib(ds->state);

    // Load globals.
    dcpu_lua_set_constants(ds->state);

    // Execute the code in the new Lua context.
    if (luaL_dofile(ds->state, path->data) != 0)
    {
        printd(LEVEL_ERROR, "lua error was %s.\n", lua_tostring(ds->state, -1));

        // Return NULL.
        lua_close(ds->state);
        free(ds);
        bdestroy(path);
        return NULL;
    }

    // Load tables.
    lua_getglobal(ds->state, "MODULE");
    module = lua_gettop(ds->state);

    // Ensure module table was provided.
    if (lua_isnoneornil(ds->state, module))
    {
        printd(LEVEL_ERROR, "failed to load debugger module from %s.\n", path->data);

        // Return NULL.
        lua_close(ds->state);
        free(ds);
        bdestroy(path);
        return NULL;
    }

    // Check to see whether the module is
    // a preprocessor module.
    lua_getfield(ds->state, module, "Type");
    modtype = bfromcstr(lua_tostring(ds->state, -1));
    if (!biseqcstrcaseless(modtype, "Debugger"))
    {
        // Return NULL.
        lua_pop(ds->state, 1);
        lua_close(ds->state);
        free(ds);
        bdestroy(modtype);
        bdestroy(path);
        return NULL;
    }
    lua_pop(ds->state, 1);
    bdestroy(modtype);

    // Create the handler tables.
    lua_newtable(ds->state);
    lua_setglobal(ds->state, HANDLER_TABLE_COMMANDS_NAME);
    lua_newtable(ds->state);
    lua_setglobal(ds->state, HANDLER_TABLE_HOOKS_NAME);
    lua_newtable(ds->state);
    lua_setglobal(ds->state, HANDLER_TABLE_SYMBOLS_NAME);

    // Set the global functions.
    lua_pushcfunction(ds->state, &dbg_lua_add_command);
    lua_setglobal(ds->state, "add_command");
    lua_pushcfunction(ds->state, &dbg_lua_add_hook);
    lua_setglobal(ds->state, "add_hook");
    lua_pushcfunction(ds->state, &dbg_lua_add_symbol_hook);
    lua_setglobal(ds->state, "add_symbol_hook");

    // Run the setup function.
    lua_getglobal(ds->state, "setup");
    if (lua_pcall(ds->state, 0, 0, 0) != 0)
    {
        printd(LEVEL_ERROR, "failed to run setup() in debugger module from %s.\n", path->data);
        printd(LEVEL_ERROR, "lua error was %s.\n", lua_tostring(ds->state, -1));
    }

    // Unset the global functions.
    lua_pushnil(ds->state);
    lua_setglobal(ds->state, "add_command");
    lua_pushnil(ds->state);
    lua_setglobal(ds->state, "add_hook");
    lua_pushnil(ds->state);
    lua_setglobal(ds->state, "add_symbol_hook");

    // Pop tables from stack.
    lua_pop(ds->state, 2);

    // Return new debugger module.
    return ds;
}
Esempio n. 7
0
struct lua_preproc* pp_lua_load(bstring name)
{
	bstring path, modtype;
	struct lua_preproc* pp;
	int module;

	// Calculate full path to file.
	path = osutil_getmodulepath();
	bconchar(path, '/');
	bconcat(path, name);

	// Create the new lua preprocessor structure.
	pp = malloc(sizeof(struct lua_preproc));
	pp->state = lua_open();
	assert(pp->state != NULL);
	luaL_openlibs(pp->state);
	luaX_loadexpressionlib(pp->state);

	// Execute the code in the new Lua context.
	if (luaL_dofile(pp->state, path->data) != 0)
	{
		printd(LEVEL_ERROR, "lua error was %s.\n", lua_tostring(pp->state, -1));

		// Return NULL.
		lua_close(pp->state);
		free(pp);
		bdestroy(path);
		return NULL;
	}

	// Load tables.
	lua_getglobal(pp->state, "MODULE");
	module = lua_gettop(pp->state);

	// Ensure module table was provided.
	if (lua_isnoneornil(pp->state, module))
	{
		printd(LEVEL_ERROR, "failed to load preprocessor module from %s.\n", path->data);

		// Return NULL.
		lua_close(pp->state);
		free(pp);
		bdestroy(path);
		return NULL;
	}

	// Check to see whether the module is
	// a preprocessor module.
	lua_getfield(pp->state, module, "Type");
	modtype = bfromcstr(lua_tostring(pp->state, -1));
	if (!biseqcstrcaseless(modtype, "Preprocessor"))
	{
		// Return NULL.
		lua_pop(pp->state, 1);
		lua_close(pp->state);
		free(pp);
		bdestroy(modtype);
		bdestroy(path);
		return NULL;
	}
	lua_pop(pp->state, 1);
	bdestroy(modtype);

	// Create the handler table.
	lua_newtable(pp->state);
	lua_setglobal(pp->state, HANDLER_TABLE_NAME);

	// Set the global add_preprocessor_directive function.
	lua_pushcfunction(pp->state, &pp_lua_add_preprocessor_directive);
	lua_setglobal(pp->state, "add_preprocessor_directive");

	// Run the setup function.
	lua_getglobal(pp->state, "setup");
	if (lua_pcall(pp->state, 0, 0, 0) != 0)
	{
		printd(LEVEL_ERROR, "failed to run setup() in preprocessor module from %s.\n", path->data);
	}

	// Unset the add_preprocessor_directive function.
	lua_pushnil(pp->state);
	lua_setglobal(pp->state, "add_preprocessor_directive");

	// Pop tables from stack.
	lua_pop(pp->state, 2);

	// Return new preprocessor module.
	return pp;
}
Esempio n. 8
0
int vm_hw_lua_cpu_handle_register_set(lua_State* L)
{
	// Table is at 1, key is at 2, value is at 3.
	vm_t* vm = vm_hw_lua_cpu_extract_cpu(L, 1);
	bstring name = bfromcstr(lua_tostring(L, 2));

	if (biseqcstrcaseless(name, "A"))
		vm->registers[REG_A] = (uint16_t)lua_tonumber(L, 3);
	else if (biseqcstrcaseless(name, "B"))
		vm->registers[REG_B] = (uint16_t)lua_tonumber(L, 3);
	else if (biseqcstrcaseless(name, "C"))
		vm->registers[REG_C] = (uint16_t)lua_tonumber(L, 3);
	else if (biseqcstrcaseless(name, "X"))
		vm->registers[REG_X] = (uint16_t)lua_tonumber(L, 3);
	else if (biseqcstrcaseless(name, "Y"))
		vm->registers[REG_Y] = (uint16_t)lua_tonumber(L, 3);
	else if (biseqcstrcaseless(name, "Z"))
		vm->registers[REG_Z] = (uint16_t)lua_tonumber(L, 3);
	else if (biseqcstrcaseless(name, "I"))
		vm->registers[REG_I] = (uint16_t)lua_tonumber(L, 3);
	else if (biseqcstrcaseless(name, "J"))
		vm->registers[REG_J] = (uint16_t)lua_tonumber(L, 3);
	else if (biseqcstrcaseless(name, "SP"))
		vm->sp = (uint16_t)lua_tonumber(L, 3);
	else if (biseqcstrcaseless(name, "PC"))
		vm->pc = (uint16_t)lua_tonumber(L, 3);
	else if (biseqcstrcaseless(name, "IA"))
		vm->ia = (uint16_t)lua_tonumber(L, 3);
	else if (biseqcstrcaseless(name, "EX"))
		vm->ex = (uint16_t)lua_tonumber(L, 3);

	return 0;
}
Esempio n. 9
0
int vm_hw_lua_cpu_handle_register_get(lua_State* L)
{
	// Table is at 1, key is at 2.
	vm_t* vm = vm_hw_lua_cpu_extract_cpu(L, 1);
	bstring name = bfromcstr(lua_tostring(L, 2));

	if (biseqcstrcaseless(name, "A"))
		lua_pushnumber(L, vm->registers[REG_A]);
	else if (biseqcstrcaseless(name, "B"))
		lua_pushnumber(L, vm->registers[REG_B]);
	else if (biseqcstrcaseless(name, "C"))
		lua_pushnumber(L, vm->registers[REG_C]);
	else if (biseqcstrcaseless(name, "X"))
		lua_pushnumber(L, vm->registers[REG_X]);
	else if (biseqcstrcaseless(name, "Y"))
		lua_pushnumber(L, vm->registers[REG_Y]);
	else if (biseqcstrcaseless(name, "Z"))
		lua_pushnumber(L, vm->registers[REG_Z]);
	else if (biseqcstrcaseless(name, "I"))
		lua_pushnumber(L, vm->registers[REG_I]);
	else if (biseqcstrcaseless(name, "J"))
		lua_pushnumber(L, vm->registers[REG_J]);
	else if (biseqcstrcaseless(name, "SP"))
		lua_pushnumber(L, vm->sp);
	else if (biseqcstrcaseless(name, "PC"))
		lua_pushnumber(L, vm->pc);
	else if (biseqcstrcaseless(name, "IA"))
		lua_pushnumber(L, vm->ia);
	else if (biseqcstrcaseless(name, "EX"))
		lua_pushnumber(L, vm->ex);
	else
		lua_pushnil(L);

	return 1;
}