diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-07-26 10:49:23 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-07-26 10:49:23 +0100 |
commit | 537a7180a0d5132c11636c4fd8b1450cd99d352c (patch) | |
tree | fb9f8c84955a411bdffbd6371ea32f2716fb3687 /src/rspamadm/lua_repl.c | |
parent | 5fd7a90fdaa33f52c59bdb0ca84451e5c1e22365 (diff) | |
download | rspamd-537a7180a0d5132c11636c4fd8b1450cd99d352c.tar.gz rspamd-537a7180a0d5132c11636c4fd8b1450cd99d352c.zip |
[Rework] Use clang-format to unify formatting in all sources
No meaningful changes.
Diffstat (limited to 'src/rspamadm/lua_repl.c')
-rw-r--r-- | src/rspamadm/lua_repl.c | 672 |
1 files changed, 331 insertions, 341 deletions
diff --git a/src/rspamadm/lua_repl.c b/src/rspamadm/lua_repl.c index de0f7a0ab..bc1291a21 100644 --- a/src/rspamadm/lua_repl.c +++ b/src/rspamadm/lua_repl.c @@ -55,17 +55,17 @@ static Replxx *rx_instance = NULL; #endif #define MULTILINE_PROMPT "... " -static void rspamadm_lua (gint argc, gchar **argv, - const struct rspamadm_command *cmd); -static const char *rspamadm_lua_help (gboolean full_help, - const struct rspamadm_command *cmd); +static void rspamadm_lua(gint argc, gchar **argv, + const struct rspamadm_command *cmd); +static const char *rspamadm_lua_help(gboolean full_help, + const struct rspamadm_command *cmd); struct rspamadm_command lua_command = { - .name = "lua", - .flags = 0, - .help = rspamadm_lua_help, - .run = rspamadm_lua, - .lua_subrs = NULL, + .name = "lua", + .flags = 0, + .help = rspamadm_lua_help, + .run = rspamadm_lua, + .lua_subrs = NULL, }; /* @@ -78,75 +78,66 @@ struct rspamadm_lua_dot_command { rspamadm_lua_dot_handler handler; }; -static void rspamadm_lua_help_handler (lua_State *L, gint argc, gchar **argv); -static void rspamadm_lua_load_handler (lua_State *L, gint argc, gchar **argv); -static void rspamadm_lua_exec_handler (lua_State *L, gint argc, gchar **argv); -static void rspamadm_lua_message_handler (lua_State *L, gint argc, gchar **argv); +static void rspamadm_lua_help_handler(lua_State *L, gint argc, gchar **argv); +static void rspamadm_lua_load_handler(lua_State *L, gint argc, gchar **argv); +static void rspamadm_lua_exec_handler(lua_State *L, gint argc, gchar **argv); +static void rspamadm_lua_message_handler(lua_State *L, gint argc, gchar **argv); -static void lua_thread_error_cb (struct thread_entry *thread, int ret, const char *msg); -static void lua_thread_finish_cb (struct thread_entry *thread, int ret); +static void lua_thread_error_cb(struct thread_entry *thread, int ret, const char *msg); +static void lua_thread_finish_cb(struct thread_entry *thread, int ret); static struct rspamadm_lua_dot_command cmds[] = { - { - .name = "help", - .description = "shows help for commands", - .handler = rspamadm_lua_help_handler - }, - { - .name = "load", - .description = "load lua file", - .handler = rspamadm_lua_load_handler - }, - { - .name = "exec", - .description = "exec lua file", - .handler = rspamadm_lua_exec_handler - }, - { - .name = "message", - .description = "scans message using specified callback: .message <callback_name> <file>...", - .handler = rspamadm_lua_message_handler - }, + {.name = "help", + .description = "shows help for commands", + .handler = rspamadm_lua_help_handler}, + {.name = "load", + .description = "load lua file", + .handler = rspamadm_lua_load_handler}, + {.name = "exec", + .description = "exec lua file", + .handler = rspamadm_lua_exec_handler}, + {.name = "message", + .description = "scans message using specified callback: .message <callback_name> <file>...", + .handler = rspamadm_lua_message_handler}, }; static GHashTable *cmds_hash = NULL; static GOptionEntry entries[] = { - {"script", 's', 0, G_OPTION_ARG_STRING_ARRAY, &scripts, - "Load specified scripts", NULL}, - {"path", 'P', 0, G_OPTION_ARG_STRING_ARRAY, &paths, - "Add specified paths to lua paths", NULL}, - {"history-file", 'H', 0, G_OPTION_ARG_FILENAME, &histfile, - "Load history from the specified file", NULL}, - {"max-history", 'm', 0, G_OPTION_ARG_INT, &max_history, - "Store this number of history entries", NULL}, - {"serve", 'S', 0, G_OPTION_ARG_STRING, &serve, - "Serve http lua server", NULL}, - {"batch", 'b', 0, G_OPTION_ARG_NONE, &batch, - "Batch execution mode", NULL}, - {"exec", 'e', 0, G_OPTION_ARG_STRING, &exec_line, - "Execute specified script", NULL}, - {"args", 'a', 0, G_OPTION_ARG_STRING_ARRAY, &lua_args, - "Arguments to pass to Lua", NULL}, - {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL} -}; + {"script", 's', 0, G_OPTION_ARG_STRING_ARRAY, &scripts, + "Load specified scripts", NULL}, + {"path", 'P', 0, G_OPTION_ARG_STRING_ARRAY, &paths, + "Add specified paths to lua paths", NULL}, + {"history-file", 'H', 0, G_OPTION_ARG_FILENAME, &histfile, + "Load history from the specified file", NULL}, + {"max-history", 'm', 0, G_OPTION_ARG_INT, &max_history, + "Store this number of history entries", NULL}, + {"serve", 'S', 0, G_OPTION_ARG_STRING, &serve, + "Serve http lua server", NULL}, + {"batch", 'b', 0, G_OPTION_ARG_NONE, &batch, + "Batch execution mode", NULL}, + {"exec", 'e', 0, G_OPTION_ARG_STRING, &exec_line, + "Execute specified script", NULL}, + {"args", 'a', 0, G_OPTION_ARG_STRING_ARRAY, &lua_args, + "Arguments to pass to Lua", NULL}, + {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}}; static const char * -rspamadm_lua_help (gboolean full_help, const struct rspamadm_command *cmd) +rspamadm_lua_help(gboolean full_help, const struct rspamadm_command *cmd) { const char *help_str; if (full_help) { help_str = "Run lua read/execute/print loop\n\n" - "Usage: rspamadm lua [-P paths] [-s scripts]\n" - "Where options are:\n\n" - "-P: add additional lua paths (may be repeated)\n" - "-p: split input to lines and feed each line to the script\n" - "-s: load scripts on start from specified files (may be repeated)\n" - "-S: listen on a specified address as HTTP server\n" - "-a: pass argument to lua (may be repeated)\n" - "-e: execute script specified in command line" - "--help: shows available options and commands"; + "Usage: rspamadm lua [-P paths] [-s scripts]\n" + "Where options are:\n\n" + "-P: add additional lua paths (may be repeated)\n" + "-p: split input to lines and feed each line to the script\n" + "-s: load scripts on start from specified files (may be repeated)\n" + "-S: listen on a specified address as HTTP server\n" + "-a: pass argument to lua (may be repeated)\n" + "-e: execute script specified in command line" + "--help: shows available options and commands"; } else { help_str = "Run LUA interpreter"; @@ -156,34 +147,34 @@ rspamadm_lua_help (gboolean full_help, const struct rspamadm_command *cmd) } static void -rspamadm_lua_add_path (lua_State *L, const gchar *path) +rspamadm_lua_add_path(lua_State *L, const gchar *path) { const gchar *old_path; gsize len; GString *new_path; - lua_getglobal (L, "package"); - lua_getfield (L, -1, "path"); - old_path = luaL_checklstring (L, -1, &len); + lua_getglobal(L, "package"); + lua_getfield(L, -1, "path"); + old_path = luaL_checklstring(L, -1, &len); - new_path = g_string_sized_new (len + strlen (path) + sizeof("/?.lua")); + new_path = g_string_sized_new(len + strlen(path) + sizeof("/?.lua")); - if (strstr (path, "?.lua") == NULL) { - rspamd_printf_gstring (new_path, "%s/?.lua;%s", path, old_path); + if (strstr(path, "?.lua") == NULL) { + rspamd_printf_gstring(new_path, "%s/?.lua;%s", path, old_path); } else { - rspamd_printf_gstring (new_path, "%s;%s", path, old_path); + rspamd_printf_gstring(new_path, "%s;%s", path, old_path); } - lua_pushlstring (L, new_path->str, new_path->len); - lua_setfield (L, -2, "path"); - lua_settop (L, 0); - g_string_free (new_path, TRUE); + lua_pushlstring(L, new_path->str, new_path->len); + lua_setfield(L, -2, "path"); + lua_settop(L, 0); + g_string_free(new_path, TRUE); } static void -lua_thread_finish_cb (struct thread_entry *thread, int ret) +lua_thread_finish_cb(struct thread_entry *thread, int ret) { struct lua_call_data *cd = thread->cd; @@ -191,51 +182,51 @@ lua_thread_finish_cb (struct thread_entry *thread, int ret) } static void -lua_thread_error_cb (struct thread_entry *thread, int ret, const char *msg) +lua_thread_error_cb(struct thread_entry *thread, int ret, const char *msg) { struct lua_call_data *cd = thread->cd; - rspamd_fprintf (stderr, "call failed: %s\n", msg); + rspamd_fprintf(stderr, "call failed: %s\n", msg); cd->ret = ret; } static void -lua_thread_str_error_cb (struct thread_entry *thread, int ret, const char *msg) +lua_thread_str_error_cb(struct thread_entry *thread, int ret, const char *msg) { struct lua_call_data *cd = thread->cd; const char *what = cd->ud; - rspamd_fprintf (stderr, "call to %s failed: %s\n", what, msg); + rspamd_fprintf(stderr, "call to %s failed: %s\n", what, msg); cd->ret = ret; } static gboolean -rspamadm_lua_load_script (lua_State *L, const gchar *path) +rspamadm_lua_load_script(lua_State *L, const gchar *path) { - struct thread_entry *thread = lua_thread_pool_get_for_config (rspamd_main->cfg); + struct thread_entry *thread = lua_thread_pool_get_for_config(rspamd_main->cfg); L = thread->lua_state; - if (luaL_loadfile (L, path) != 0) { - rspamd_fprintf (stderr, "cannot load script %s: %s\n", - path, lua_tostring (L, -1)); - lua_settop (L, 0); + if (luaL_loadfile(L, path) != 0) { + rspamd_fprintf(stderr, "cannot load script %s: %s\n", + path, lua_tostring(L, -1)); + lua_settop(L, 0); return FALSE; } - if (lua_repl_thread_call (thread, 0, (void *)path, lua_thread_str_error_cb) != 0) { + if (lua_repl_thread_call(thread, 0, (void *) path, lua_thread_str_error_cb) != 0) { return FALSE; } - lua_settop (L, 0); + lua_settop(L, 0); return TRUE; } static void -rspamadm_exec_input (lua_State *L, const gchar *input) +rspamadm_exec_input(lua_State *L, const gchar *input) { GString *tb; gint i, cbref; @@ -243,67 +234,67 @@ rspamadm_exec_input (lua_State *L, const gchar *input) gchar outbuf[8192]; struct lua_logger_trace tr; - struct thread_entry *thread = lua_thread_pool_get_for_config (rspamd_main->cfg); + struct thread_entry *thread = lua_thread_pool_get_for_config(rspamd_main->cfg); L = thread->lua_state; /* First try return + input */ - tb = g_string_sized_new (strlen (input) + sizeof ("return ")); - rspamd_printf_gstring (tb, "return %s", input); + tb = g_string_sized_new(strlen(input) + sizeof("return ")); + rspamd_printf_gstring(tb, "return %s", input); - int r = luaL_loadstring (L, tb->str); + int r = luaL_loadstring(L, tb->str); if (r != 0) { /* Reset stack */ - lua_settop (L, 0); + lua_settop(L, 0); /* Try with no return */ - if (luaL_loadstring (L, input) != 0) { - rspamd_fprintf (stderr, "cannot load string %s\n", - input); - g_string_free (tb, TRUE); - lua_settop (L, 0); + if (luaL_loadstring(L, input) != 0) { + rspamd_fprintf(stderr, "cannot load string %s\n", + input); + g_string_free(tb, TRUE); + lua_settop(L, 0); - lua_thread_pool_return (rspamd_main->cfg->lua_thread_pool, thread); + lua_thread_pool_return(rspamd_main->cfg->lua_thread_pool, thread); return; } } - g_string_free (tb, TRUE); + g_string_free(tb, TRUE); - top = lua_gettop (L); + top = lua_gettop(L); - if (lua_repl_thread_call (thread, 0, NULL, NULL) == 0) { + if (lua_repl_thread_call(thread, 0, NULL, NULL) == 0) { /* Print output */ - for (i = top; i <= lua_gettop (L); i++) { - if (lua_isfunction (L, i)) { - lua_pushvalue (L, i); - cbref = luaL_ref (L, LUA_REGISTRYINDEX); - - rspamd_printf ("local function: %d\n", cbref); - } else { - memset (&tr, 0, sizeof (tr)); - lua_logger_out_type (L, i, outbuf, sizeof (outbuf) - 1, &tr, - LUA_ESCAPE_UNPRINTABLE); - rspamd_printf ("%s\n", outbuf); + for (i = top; i <= lua_gettop(L); i++) { + if (lua_isfunction(L, i)) { + lua_pushvalue(L, i); + cbref = luaL_ref(L, LUA_REGISTRYINDEX); + + rspamd_printf("local function: %d\n", cbref); + } + else { + memset(&tr, 0, sizeof(tr)); + lua_logger_out_type(L, i, outbuf, sizeof(outbuf) - 1, &tr, + LUA_ESCAPE_UNPRINTABLE); + rspamd_printf("%s\n", outbuf); } } } } static void -wait_session_events (void) +wait_session_events(void) { /* XXX: it's probably worth to add timeout here - not to wait forever */ - while (rspamd_session_events_pending (rspamadm_session) > 0) { - ev_loop (rspamd_main->event_loop, EVRUN_ONCE); + while (rspamd_session_events_pending(rspamadm_session) > 0) { + ev_loop(rspamd_main->event_loop, EVRUN_ONCE); } } -gint -lua_repl_thread_call (struct thread_entry *thread, gint narg, gpointer ud, lua_thread_error_t error_func) +gint lua_repl_thread_call(struct thread_entry *thread, gint narg, gpointer ud, lua_thread_error_t error_func) { int ret; - struct lua_call_data *cd = g_new0 (struct lua_call_data, 1); - cd->top = lua_gettop (thread->lua_state); + struct lua_call_data *cd = g_new0(struct lua_call_data, 1); + cd->top = lua_gettop(thread->lua_state); cd->ud = ud; thread->finish_callback = lua_thread_finish_cb; @@ -315,84 +306,84 @@ lua_repl_thread_call (struct thread_entry *thread, gint narg, gpointer ud, lua_t } thread->cd = cd; - lua_thread_call (thread, narg); + lua_thread_call(thread, narg); - wait_session_events (); + wait_session_events(); ret = cd->ret; - g_free (cd); + g_free(cd); return ret; } static void -rspamadm_lua_help_handler (lua_State *L, gint argc, gchar **argv) +rspamadm_lua_help_handler(lua_State *L, gint argc, gchar **argv) { guint i; struct rspamadm_lua_dot_command *cmd; if (argv[1] == NULL) { /* Print all commands */ - for (i = 0; i < G_N_ELEMENTS (cmds); i ++) { - rspamd_printf ("%s: %s\n", cmds[i].name, cmds[i].description); + for (i = 0; i < G_N_ELEMENTS(cmds); i++) { + rspamd_printf("%s: %s\n", cmds[i].name, cmds[i].description); } - rspamd_printf ("{{: start multiline input\n"); - rspamd_printf ("}}: end multiline input\n"); + rspamd_printf("{{: start multiline input\n"); + rspamd_printf("}}: end multiline input\n"); } else { - for (i = 1; argv[i] != NULL; i ++) { - cmd = g_hash_table_lookup (cmds_hash, argv[i]); + for (i = 1; argv[i] != NULL; i++) { + cmd = g_hash_table_lookup(cmds_hash, argv[i]); if (cmd) { - rspamd_printf ("%s: %s\n", cmds->name, cmds->description); + rspamd_printf("%s: %s\n", cmds->name, cmds->description); } else { - rspamd_printf ("%s: no such command\n", argv[i]); + rspamd_printf("%s: no such command\n", argv[i]); } } } } static void -rspamadm_lua_load_handler (lua_State *L, gint argc, gchar **argv) +rspamadm_lua_load_handler(lua_State *L, gint argc, gchar **argv) { guint i; gboolean ret; - for (i = 1; argv[i] != NULL; i ++) { - ret = rspamadm_lua_load_script (L, argv[i]); - rspamd_printf ("%s: %sloaded\n", argv[i], ret ? "" : "NOT "); + for (i = 1; argv[i] != NULL; i++) { + ret = rspamadm_lua_load_script(L, argv[i]); + rspamd_printf("%s: %sloaded\n", argv[i], ret ? "" : "NOT "); } } static void -rspamadm_lua_exec_handler (lua_State *L, gint argc, gchar **argv) +rspamadm_lua_exec_handler(lua_State *L, gint argc, gchar **argv) { gint i; - struct thread_entry *thread = lua_thread_pool_get_for_config (rspamd_main->cfg); + struct thread_entry *thread = lua_thread_pool_get_for_config(rspamd_main->cfg); L = thread->lua_state; - for (i = 1; argv[i] != NULL; i ++) { + for (i = 1; argv[i] != NULL; i++) { - if (luaL_loadfile (L, argv[i]) != 0) { - rspamd_fprintf (stderr, "cannot load script %s: %s\n", - argv[i], lua_tostring (L, -1)); - lua_settop (L, 0); + if (luaL_loadfile(L, argv[i]) != 0) { + rspamd_fprintf(stderr, "cannot load script %s: %s\n", + argv[i], lua_tostring(L, -1)); + lua_settop(L, 0); return; } - if (lua_repl_thread_call (thread, 0, argv[i], lua_thread_str_error_cb) != 0) { + if (lua_repl_thread_call(thread, 0, argv[i], lua_thread_str_error_cb) != 0) { return; } } } static void -rspamadm_lua_message_handler (lua_State *L, gint argc, gchar **argv) +rspamadm_lua_message_handler(lua_State *L, gint argc, gchar **argv) { gulong cbref; gint old_top, func_idx, i, j; @@ -403,109 +394,109 @@ rspamadm_lua_message_handler (lua_State *L, gint argc, gchar **argv) struct lua_logger_trace tr; if (argv[1] == NULL) { - rspamd_printf ("no callback is specified\n"); + rspamd_printf("no callback is specified\n"); return; } - for (i = 2; argv[i] != NULL; i ++) { - struct thread_entry *thread = lua_thread_pool_get_for_config (rspamd_main->cfg); + for (i = 2; argv[i] != NULL; i++) { + struct thread_entry *thread = lua_thread_pool_get_for_config(rspamd_main->cfg); L = thread->lua_state; - if (rspamd_strtoul (argv[1], strlen (argv[1]), &cbref)) { - lua_rawgeti (L, LUA_REGISTRYINDEX, cbref); + if (rspamd_strtoul(argv[1], strlen(argv[1]), &cbref)) { + lua_rawgeti(L, LUA_REGISTRYINDEX, cbref); } else { - lua_getglobal (L, argv[1]); + lua_getglobal(L, argv[1]); } - if (lua_type (L, -1) != LUA_TFUNCTION) { - rspamd_printf ("bad callback type: %s\n", lua_typename (L, lua_type (L, -1))); - lua_thread_pool_return (rspamd_main->cfg->lua_thread_pool, thread); + if (lua_type(L, -1) != LUA_TFUNCTION) { + rspamd_printf("bad callback type: %s\n", lua_typename(L, lua_type(L, -1))); + lua_thread_pool_return(rspamd_main->cfg->lua_thread_pool, thread); return; } /* Save index to reuse */ - func_idx = lua_gettop (L); + func_idx = lua_gettop(L); - map = rspamd_file_xmap (argv[i], PROT_READ, &len, TRUE); + map = rspamd_file_xmap(argv[i], PROT_READ, &len, TRUE); if (map == NULL) { - rspamd_printf ("cannot open %s: %s\n", argv[i], strerror (errno)); + rspamd_printf("cannot open %s: %s\n", argv[i], strerror(errno)); } else { - task = rspamd_task_new (NULL, rspamd_main->cfg, NULL, NULL, NULL, FALSE); + task = rspamd_task_new(NULL, rspamd_main->cfg, NULL, NULL, NULL, FALSE); - if (!rspamd_task_load_message (task, NULL, map, len)) { - rspamd_printf ("cannot load %s\n", argv[i]); - rspamd_task_free (task); - munmap (map, len); + if (!rspamd_task_load_message(task, NULL, map, len)) { + rspamd_printf("cannot load %s\n", argv[i]); + rspamd_task_free(task); + munmap(map, len); continue; } - if (!rspamd_message_parse (task)) { - rspamd_printf ("cannot parse %s: %e\n", argv[i], task->err); - rspamd_task_free (task); - munmap (map, len); + if (!rspamd_message_parse(task)) { + rspamd_printf("cannot parse %s: %e\n", argv[i], task->err); + rspamd_task_free(task); + munmap(map, len); continue; } - rspamd_message_process (task); - old_top = lua_gettop (L); + rspamd_message_process(task); + old_top = lua_gettop(L); - lua_pushvalue (L, func_idx); - ptask = lua_newuserdata (L, sizeof (*ptask)); + lua_pushvalue(L, func_idx); + ptask = lua_newuserdata(L, sizeof(*ptask)); *ptask = task; - rspamd_lua_setclass (L, "rspamd{task}", -1); + rspamd_lua_setclass(L, "rspamd{task}", -1); - if (lua_repl_thread_call (thread, 1, argv[i], lua_thread_str_error_cb) == 0) { - rspamd_printf ("lua callback for %s returned:\n", argv[i]); + if (lua_repl_thread_call(thread, 1, argv[i], lua_thread_str_error_cb) == 0) { + rspamd_printf("lua callback for %s returned:\n", argv[i]); - for (j = old_top + 1; j <= lua_gettop (L); j ++) { - memset (&tr, 0, sizeof (tr)); - lua_logger_out_type (L, j, outbuf, sizeof (outbuf), &tr, - LUA_ESCAPE_UNPRINTABLE); - rspamd_printf ("%s\n", outbuf); + for (j = old_top + 1; j <= lua_gettop(L); j++) { + memset(&tr, 0, sizeof(tr)); + lua_logger_out_type(L, j, outbuf, sizeof(outbuf), &tr, + LUA_ESCAPE_UNPRINTABLE); + rspamd_printf("%s\n", outbuf); } } - rspamd_task_free (task); - munmap (map, len); + rspamd_task_free(task); + munmap(map, len); /* Pop all but the original function */ - lua_settop (L, func_idx); + lua_settop(L, func_idx); } } - lua_settop (L, 0); + lua_settop(L, 0); } static gboolean -rspamadm_lua_try_dot_command (lua_State *L, const gchar *input) +rspamadm_lua_try_dot_command(lua_State *L, const gchar *input) { struct rspamadm_lua_dot_command *cmd; gchar **argv; - argv = g_strsplit_set (input + 1, " ", -1); + argv = g_strsplit_set(input + 1, " ", -1); if (argv == NULL || argv[0] == NULL) { if (argv) { - g_strfreev (argv); + g_strfreev(argv); } return FALSE; } - cmd = g_hash_table_lookup (cmds_hash, argv[0]); + cmd = g_hash_table_lookup(cmds_hash, argv[0]); if (cmd) { - cmd->handler (L, g_strv_length (argv), argv); - g_strfreev (argv); + cmd->handler(L, g_strv_length(argv), argv); + g_strfreev(argv); return TRUE; } - g_strfreev (argv); + g_strfreev(argv); return FALSE; } @@ -514,31 +505,31 @@ rspamadm_lua_try_dot_command (lua_State *L, const gchar *input) static gint lex_ref_idx = -1; static void -lua_syntax_highlighter (const char *str, ReplxxColor *colours, int size, void *ud) +lua_syntax_highlighter(const char *str, ReplxxColor *colours, int size, void *ud) { - lua_State *L = (lua_State *)ud; + lua_State *L = (lua_State *) ud; if (lex_ref_idx == -1) { - if (!rspamd_lua_require_function (L, "lua_lexer", "lex_to_table")) { - fprintf (stderr, "cannot require lua_lexer!\n"); + if (!rspamd_lua_require_function(L, "lua_lexer", "lex_to_table")) { + fprintf(stderr, "cannot require lua_lexer!\n"); - exit (EXIT_FAILURE); + exit(EXIT_FAILURE); } - lex_ref_idx = luaL_ref (L, LUA_REGISTRYINDEX); + lex_ref_idx = luaL_ref(L, LUA_REGISTRYINDEX); } - lua_rawgeti (L, LUA_REGISTRYINDEX, lex_ref_idx); - lua_pushstring (L, str); + lua_rawgeti(L, LUA_REGISTRYINDEX, lex_ref_idx); + lua_pushstring(L, str); - if (lua_pcall (L, 1, 1, 0) != 0) { - fprintf (stderr, "cannot lex a string!\n"); + if (lua_pcall(L, 1, 1, 0) != 0) { + fprintf(stderr, "cannot lex a string!\n"); } else { /* Process what we have after lexing */ - gsize nelts = rspamd_lua_table_size (L, -1); + gsize nelts = rspamd_lua_table_size(L, -1); - for (gsize i = 0; i < nelts; i ++) { + for (gsize i = 0; i < nelts; i++) { /* * Indexes in the table: * 1 - type of element (string) @@ -550,66 +541,66 @@ lua_syntax_highlighter (const char *str, ReplxxColor *colours, int size, void *u gsize column, tlen, cur_top, elt_pos; ReplxxColor elt_color = REPLXX_COLOR_DEFAULT; - cur_top = lua_gettop (L); - lua_rawgeti (L, -1, i + 1); - elt_pos = lua_gettop (L); - lua_rawgeti (L, elt_pos, 1); - what = lua_tostring (L, -1); - lua_rawgeti (L, elt_pos, 2); - lua_tolstring (L, -1, &tlen); - lua_rawgeti (L, elt_pos, 4); - column = lua_tointeger (L, -1); + cur_top = lua_gettop(L); + lua_rawgeti(L, -1, i + 1); + elt_pos = lua_gettop(L); + lua_rawgeti(L, elt_pos, 1); + what = lua_tostring(L, -1); + lua_rawgeti(L, elt_pos, 2); + lua_tolstring(L, -1, &tlen); + lua_rawgeti(L, elt_pos, 4); + column = lua_tointeger(L, -1); - g_assert (column > 0); - column --; /* Start from 0 */ + g_assert(column > 0); + column--; /* Start from 0 */ if (column + tlen > size) { /* Likely utf8 case, too complicated to match */ - lua_settop (L, cur_top); + lua_settop(L, cur_top); continue; } /* Check what and adjust color */ - if (strcmp (what, "identifier") == 0) { + if (strcmp(what, "identifier") == 0) { elt_color = REPLXX_COLOR_NORMAL; } - else if (strcmp (what, "number") == 0) { + else if (strcmp(what, "number") == 0) { elt_color = REPLXX_COLOR_BLUE; } - else if (strcmp (what, "string") == 0) { + else if (strcmp(what, "string") == 0) { elt_color = REPLXX_COLOR_GREEN; } - else if (strcmp (what, "keyword") == 0) { + else if (strcmp(what, "keyword") == 0) { elt_color = REPLXX_COLOR_WHITE; } - else if (strcmp (what, "constant") == 0) { + else if (strcmp(what, "constant") == 0) { elt_color = REPLXX_COLOR_WHITE; } - else if (strcmp (what, "operator") == 0) { + else if (strcmp(what, "operator") == 0) { elt_color = REPLXX_COLOR_CYAN; } - else if (strcmp (what, "comment") == 0) { + else if (strcmp(what, "comment") == 0) { elt_color = REPLXX_COLOR_BRIGHTGREEN; } - else if (strcmp (what, "error") == 0) { + else if (strcmp(what, "error") == 0) { elt_color = REPLXX_COLOR_ERROR; } - for (gsize j = column; j < column + tlen; j ++) { + for (gsize j = column; j < column + tlen; j++) { colours[j] = elt_color; } /* Restore stack */ - lua_settop (L, cur_top); + lua_settop(L, cur_top); } } - lua_settop (L, 0); + lua_settop(L, 0); } #endif static void -rspamadm_lua_run_repl (lua_State *L, bool is_batch) +rspamadm_lua_run_repl(lua_State *L, bool is_batch) { gchar *input; #ifdef WITH_LUA_REPL @@ -631,7 +622,7 @@ rspamadm_lua_run_repl (lua_State *L, bool is_batch) if (linelen > 0) { if (input[linelen - 1] == '\n') { input[linelen - 1] = '\0'; - linelen --; + linelen--; } if (linelen > 0) { @@ -653,7 +644,7 @@ rspamadm_lua_run_repl (lua_State *L, bool is_batch) else { #ifdef WITH_LUA_REPL replxx_set_highlighter_callback(rx_instance, lua_syntax_highlighter, - L); + L); if (!is_multiline) { input = (gchar *) replxx_input(rx_instance, MAIN_PROMPT); @@ -730,56 +721,56 @@ struct rspamadm_lua_repl_session { }; static void -rspamadm_lua_accept_cb (EV_P_ ev_io *w, int revents) +rspamadm_lua_accept_cb(EV_P_ ev_io *w, int revents) { struct rspamadm_lua_repl_context *ctx = - (struct rspamadm_lua_repl_context *)w->data; + (struct rspamadm_lua_repl_context *) w->data; rspamd_inet_addr_t *addr = NULL; struct rspamadm_lua_repl_session *session; gint nfd; if ((nfd = - rspamd_accept_from_socket (w->fd, &addr, NULL, NULL)) == -1) { - rspamd_fprintf (stderr, "accept failed: %s", strerror (errno)); + rspamd_accept_from_socket(w->fd, &addr, NULL, NULL)) == -1) { + rspamd_fprintf(stderr, "accept failed: %s", strerror(errno)); return; } /* Check for EAGAIN */ if (nfd == 0) { - rspamd_inet_address_free (addr); + rspamd_inet_address_free(addr); return; } - session = g_malloc0 (sizeof (*session)); + session = g_malloc0(sizeof(*session)); session->rt = ctx->rt; session->ctx = ctx; session->addr = addr; session->sock = nfd; - rspamd_http_router_handle_socket (ctx->rt, nfd, session); + rspamd_http_router_handle_socket(ctx->rt, nfd, session); } static void -rspamadm_lua_error_handler (struct rspamd_http_connection_entry *conn_ent, - GError *err) +rspamadm_lua_error_handler(struct rspamd_http_connection_entry *conn_ent, + GError *err) { - rspamd_fprintf (stderr, "http error occurred: %s\n", err->message); + rspamd_fprintf(stderr, "http error occurred: %s\n", err->message); } static void -rspamadm_lua_finish_handler (struct rspamd_http_connection_entry *conn_ent) +rspamadm_lua_finish_handler(struct rspamd_http_connection_entry *conn_ent) { struct rspamadm_lua_repl_session *session = conn_ent->ud; - g_free (session); + g_free(session); } static void -lua_thread_http_error_cb (struct thread_entry *thread, int ret, const char *msg) +lua_thread_http_error_cb(struct thread_entry *thread, int ret, const char *msg) { struct lua_call_data *cd = thread->cd; struct rspamd_http_connection_entry *conn_ent = cd->ud; - rspamd_controller_send_error (conn_ent, 500, "call failed: %s\n", msg); + rspamd_controller_send_error(conn_ent, 500, "call failed: %s\n", msg); cd->ret = ret; } @@ -792,8 +783,8 @@ lua_thread_http_error_cb (struct thread_entry *thread, int ret, const char *msg) * reply: json {"status": "ok", "reply": {<lua json object>}} */ static int -rspamadm_lua_handle_exec (struct rspamd_http_connection_entry *conn_ent, - struct rspamd_http_message *msg) +rspamadm_lua_handle_exec(struct rspamd_http_connection_entry *conn_ent, + struct rspamd_http_message *msg) { GString *tb; gint err_idx, i; @@ -801,68 +792,68 @@ rspamadm_lua_handle_exec (struct rspamd_http_connection_entry *conn_ent, ucl_object_t *obj, *elt; const gchar *body; gsize body_len; - struct thread_entry *thread = lua_thread_pool_get_for_config (rspamd_main->cfg); + struct thread_entry *thread = lua_thread_pool_get_for_config(rspamd_main->cfg); L = thread->lua_state; - body = rspamd_http_message_get_body (msg, &body_len); + body = rspamd_http_message_get_body(msg, &body_len); if (body == NULL) { - rspamd_controller_send_error (conn_ent, 400, "Empty lua script"); + rspamd_controller_send_error(conn_ent, 400, "Empty lua script"); return 0; } - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); /* First try return + input */ - tb = g_string_sized_new (body_len + sizeof ("return ")); - rspamd_printf_gstring (tb, "return %*s", (gint)body_len, body); + tb = g_string_sized_new(body_len + sizeof("return ")); + rspamd_printf_gstring(tb, "return %*s", (gint) body_len, body); - if (luaL_loadstring (L, tb->str) != 0) { + if (luaL_loadstring(L, tb->str) != 0) { /* Reset stack */ - lua_settop (L, 0); - lua_pushcfunction (L, &rspamd_lua_traceback); - err_idx = lua_gettop (L); + lua_settop(L, 0); + lua_pushcfunction(L, &rspamd_lua_traceback); + err_idx = lua_gettop(L); /* Try with no return */ - if (luaL_loadbuffer (L, body, body_len, "http input") != 0) { - rspamd_controller_send_error (conn_ent, 400, "Invalid lua script"); + if (luaL_loadbuffer(L, body, body_len, "http input") != 0) { + rspamd_controller_send_error(conn_ent, 400, "Invalid lua script"); return 0; } } - g_string_free (tb, TRUE); + g_string_free(tb, TRUE); - if (lua_repl_thread_call (thread, 0, conn_ent, lua_thread_http_error_cb) != 0) { + if (lua_repl_thread_call(thread, 0, conn_ent, lua_thread_http_error_cb) != 0) { return 0; } - obj = ucl_object_typed_new (UCL_ARRAY); + obj = ucl_object_typed_new(UCL_ARRAY); - for (i = err_idx + 1; i <= lua_gettop (L); i ++) { - if (lua_isfunction (L, i)) { + for (i = err_idx + 1; i <= lua_gettop(L); i++) { + if (lua_isfunction(L, i)) { /* XXX: think about API */ } else { - elt = ucl_object_lua_import (L, i); + elt = ucl_object_lua_import(L, i); if (elt) { - ucl_array_append (obj, elt); + ucl_array_append(obj, elt); } } } - rspamd_controller_send_ucl (conn_ent, obj); - ucl_object_unref (obj); - lua_settop (L, 0); + rspamd_controller_send_ucl(conn_ent, obj); + ucl_object_unref(obj); + lua_settop(L, 0); return 0; } static void -rspamadm_lua (gint argc, gchar **argv, const struct rspamadm_command *cmd) +rspamadm_lua(gint argc, gchar **argv, const struct rspamadm_command *cmd) { GOptionContext *context; GError *error = NULL; @@ -870,25 +861,23 @@ rspamadm_lua (gint argc, gchar **argv, const struct rspamadm_command *cmd) guint i; lua_State *L = rspamd_main->cfg->lua_state; - context = g_option_context_new ("lua - run lua interpreter"); - g_option_context_set_summary (context, - "Summary:\n Rspamd administration utility version " - RVERSION - "\n Release id: " - RID); - g_option_context_add_main_entries (context, entries, NULL); - - if (!g_option_context_parse (context, &argc, &argv, &error)) { - fprintf (stderr, "option parsing failed: %s\n", error->message); - g_error_free (error); - g_option_context_free (context); - exit (EXIT_FAILURE); + context = g_option_context_new("lua - run lua interpreter"); + g_option_context_set_summary(context, + "Summary:\n Rspamd administration utility version " RVERSION + "\n Release id: " RID); + g_option_context_add_main_entries(context, entries, NULL); + + if (!g_option_context_parse(context, &argc, &argv, &error)) { + fprintf(stderr, "option parsing failed: %s\n", error->message); + g_error_free(error); + g_option_context_free(context); + exit(EXIT_FAILURE); } - g_option_context_free (context); + g_option_context_free(context); if (batch == -1) { - if (isatty (STDIN_FILENO)) { + if (isatty(STDIN_FILENO)) { batch = 0; } else { @@ -897,36 +886,36 @@ rspamadm_lua (gint argc, gchar **argv, const struct rspamadm_command *cmd) } if (paths) { - for (elt = paths; *elt != NULL; elt ++) { - rspamadm_lua_add_path (L, *elt); + for (elt = paths; *elt != NULL; elt++) { + rspamadm_lua_add_path(L, *elt); } } if (lua_args) { i = 1; - lua_newtable (L); + lua_newtable(L); - for (elt = lua_args; *elt != NULL; elt ++) { - lua_pushinteger (L, i); - lua_pushstring (L, *elt); - lua_settable (L, -3); + for (elt = lua_args; *elt != NULL; elt++) { + lua_pushinteger(L, i); + lua_pushstring(L, *elt); + lua_settable(L, -3); i++; } - lua_setglobal (L, "arg"); + lua_setglobal(L, "arg"); } if (scripts) { - for (elt = scripts; *elt != NULL; elt ++) { - if (!rspamadm_lua_load_script (L, *elt)) { - exit (EXIT_FAILURE); + for (elt = scripts; *elt != NULL; elt++) { + if (!rspamadm_lua_load_script(L, *elt)) { + exit(EXIT_FAILURE); } } } if (exec_line) { - rspamadm_exec_input (L, exec_line); + rspamadm_exec_input(L, exec_line); } if (serve) { @@ -938,97 +927,98 @@ rspamadm_lua (gint argc, gchar **argv, const struct rspamadm_command *cmd) gint fd; struct rspamadm_lua_repl_context *ctx; - if (rspamd_parse_host_port_priority (serve, &addrs, NULL, &name, - 10000, TRUE, NULL) == RSPAMD_PARSE_ADDR_FAIL) { - fprintf (stderr, "cannot listen on %s", serve); - exit (EXIT_FAILURE); + if (rspamd_parse_host_port_priority(serve, &addrs, NULL, &name, + 10000, TRUE, NULL) == RSPAMD_PARSE_ADDR_FAIL) { + fprintf(stderr, "cannot listen on %s", serve); + exit(EXIT_FAILURE); } ev_base = rspamd_main->event_loop; - ctx = g_malloc0 (sizeof (*ctx)); - http = rspamd_http_router_new (rspamadm_lua_error_handler, - rspamadm_lua_finish_handler, - 0.0, - NULL, - rspamd_main->http_ctx); + ctx = g_malloc0(sizeof(*ctx)); + http = rspamd_http_router_new(rspamadm_lua_error_handler, + rspamadm_lua_finish_handler, + 0.0, + NULL, + rspamd_main->http_ctx); ctx->L = L; ctx->rt = http; - rspamd_http_router_add_path (http, - "/exec", - rspamadm_lua_handle_exec); + rspamd_http_router_add_path(http, + "/exec", + rspamadm_lua_handle_exec); - for (i = 0; i < addrs->len; i ++) { - rspamd_inet_addr_t *addr = g_ptr_array_index (addrs, i); + for (i = 0; i < addrs->len; i++) { + rspamd_inet_addr_t *addr = g_ptr_array_index(addrs, i); - fd = rspamd_inet_address_listen (addr, SOCK_STREAM, - RSPAMD_INET_ADDRESS_LISTEN_ASYNC, -1); + fd = rspamd_inet_address_listen(addr, SOCK_STREAM, + RSPAMD_INET_ADDRESS_LISTEN_ASYNC, -1); if (fd != -1) { static ev_io ev; ev.data = ctx; - ev_io_init (&ev, rspamadm_lua_accept_cb, fd, EV_READ); - ev_io_start (ev_base, &ev); - rspamd_printf ("listen on %s\n", - rspamd_inet_address_to_string_pretty (addr)); + ev_io_init(&ev, rspamadm_lua_accept_cb, fd, EV_READ); + ev_io_start(ev_base, &ev); + rspamd_printf("listen on %s\n", + rspamd_inet_address_to_string_pretty(addr)); } } - ev_loop (ev_base, 0); + ev_loop(ev_base, 0); - exit (EXIT_SUCCESS); + exit(EXIT_SUCCESS); } if (histfile == NULL) { const gchar *homedir; GString *hist_path; - homedir = getenv ("HOME"); + homedir = getenv("HOME"); if (homedir) { - hist_path = g_string_sized_new (strlen (homedir) + - strlen (default_history_file) + 1); - rspamd_printf_gstring (hist_path, "%s/%s", homedir, - default_history_file); + hist_path = g_string_sized_new(strlen(homedir) + + strlen(default_history_file) + 1); + rspamd_printf_gstring(hist_path, "%s/%s", homedir, + default_history_file); } else { - hist_path = g_string_sized_new (strlen (default_history_file) + 2); - rspamd_printf_gstring (hist_path, "./%s", default_history_file); + hist_path = g_string_sized_new(strlen(default_history_file) + 2); + rspamd_printf_gstring(hist_path, "./%s", default_history_file); } histfile = hist_path->str; - g_string_free (hist_path, FALSE); + g_string_free(hist_path, FALSE); } if (argc > 1) { - for (i = 1; i < argc; i ++) { - if (!rspamadm_lua_load_script (L, argv[i])) { - exit (EXIT_FAILURE); + for (i = 1; i < argc; i++) { + if (!rspamadm_lua_load_script(L, argv[i])) { + exit(EXIT_FAILURE); } } - exit (EXIT_SUCCESS); + exit(EXIT_SUCCESS); } /* Init dot commands */ - cmds_hash = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal); + cmds_hash = g_hash_table_new(rspamd_strcase_hash, rspamd_strcase_equal); - for (i = 0; i < G_N_ELEMENTS (cmds); i ++) { - g_hash_table_insert (cmds_hash, (gpointer)cmds[i].name, &cmds[i]); + for (i = 0; i < G_N_ELEMENTS(cmds); i++) { + g_hash_table_insert(cmds_hash, (gpointer) cmds[i].name, &cmds[i]); } if (!batch) { #ifdef WITH_LUA_REPL - rx_instance = replxx_init (); - replxx_set_max_history_size (rx_instance, max_history); - replxx_history_load (rx_instance, histfile); + rx_instance = replxx_init(); + replxx_set_max_history_size(rx_instance, max_history); + replxx_history_load(rx_instance, histfile); #endif - rspamadm_lua_run_repl (L, false); + rspamadm_lua_run_repl(L, false); #ifdef WITH_LUA_REPL - replxx_history_save (rx_instance, histfile); - replxx_end (rx_instance); + replxx_history_save(rx_instance, histfile); + replxx_end(rx_instance); #endif - } else { - rspamadm_lua_run_repl (L, true); + } + else { + rspamadm_lua_run_repl(L, true); } } |