From 78ee0de29b46f5faed6ca2ae5222c64fbd9a2c1e Mon Sep 17 00:00:00 2001 From: sanine Date: Tue, 4 Jan 2022 23:18:10 -0600 Subject: allow overriding the builtin script --- src/argent.c | 17 +++++++++++++++-- src/lua-script/script.h | 4 ++-- src/lua-script/script.lua | 4 ++-- src/options.c | 10 ++++++++-- src/options.h | 1 + 5 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/argent.c b/src/argent.c index f057380..fb0d37b 100644 --- a/src/argent.c +++ b/src/argent.c @@ -36,6 +36,9 @@ int main(int argc, char **argv) argent_log(INFO, "log level: %s", level_string(argent_log_level)); argent_log(INFO, "configuration file: %s", opts.conf_filename); + if (opts.script_filename != NULL) { + argent_log(INFO, "script override: %s", opts.script_filename); + } argent_log(DEBUG, "create lua state"); lua_State *L = luaL_newstate(); @@ -55,8 +58,18 @@ int main(int argc, char **argv) // load argent lua script argent_log(DEBUG, "load main lua script"); - error = luaL_dostring(L, argent_script); - CHECK_LUA_ERROR(); + if (opts.script_filename == NULL) { + error = luaL_dostring(L, argent_script); + CHECK_LUA_ERROR(); + } + else { + error = luaL_dofile(L, opts.script_filename); + CHECK_LUA_ERROR(); + if (!lua_isfunction(L, -1)) { + argent_log(FATAL, "override script '%s' did not return a function!", opts.script_filename); + return 1; + } + } // load configuration file argent_log(DEBUG, "load configuration file"); diff --git a/src/lua-script/script.h b/src/lua-script/script.h index 0b58f11..8f0b7cc 100644 --- a/src/lua-script/script.h +++ b/src/lua-script/script.h @@ -55,7 +55,6 @@ const char *argent_script = " error('items must specify a pubDate!')\n" " else\n" " tbl.pubDate = to_rfc822(tbl.pubDate, 'Z')\n" - " print(tbl.pubDate)\n" " end\n" "\n" " table.insert(self.items, tbl)\n" @@ -438,7 +437,8 @@ const char *argent_script = " function process_lua_file(filename, parent)\n" " local success, result = pcall(loadfile(argent.config.site_directory..parent..filename))\n" " if not success then\n" - " print('WARNING: '..result)\n" + " argent.log('error', result)\n" + " argent.log('warn', fmt('%q will not be processed for output!', parent..filename))\n" " return\n" " end\n" "\n" diff --git a/src/lua-script/script.lua b/src/lua-script/script.lua index 65bf0cf..0a6323a 100644 --- a/src/lua-script/script.lua +++ b/src/lua-script/script.lua @@ -54,7 +54,6 @@ RssChannel.addItem = function(self, tbl) error('items must specify a pubDate!') else tbl.pubDate = to_rfc822(tbl.pubDate, 'Z') - print(tbl.pubDate) end table.insert(self.items, tbl) @@ -437,7 +436,8 @@ return function(config) function process_lua_file(filename, parent) local success, result = pcall(loadfile(argent.config.site_directory..parent..filename)) if not success then - print('WARNING: '..result) + argent.log('error', result) + argent.log('warn', fmt('%q will not be processed for output!', parent..filename)) return end diff --git a/src/options.c b/src/options.c index cab8079..fa6b999 100644 --- a/src/options.c +++ b/src/options.c @@ -6,10 +6,11 @@ static void print_usage(const char *progname) { - printf("Usage: %s [-c config_file] [-v[v[v]]] [-q[q[q]]] [-h]\n" + printf("Usage: %s [-c config_file] [-v[v[v]]] [-q[q[q]]] [-h] [-s script]\n" " -v Increase output verbosity (-vvv displays every log message)\n" " -q Decrease output verbosity (-qqq suppresses even fatal errors)\n" " -c Specify configuration file to read (default 'config.lua')\n" + " -s Override the built-in Lua script\n" " -h Print this help message and exit\n", progname); } @@ -18,9 +19,10 @@ int parse_options(struct argent_options *opts, int argc, char **argv) { opts->log_level = WARN; opts->conf_filename = "config.lua"; + opts->script_filename = NULL; int opt; - while ((opt = getopt(argc, argv, "hqvc:")) != -1) { + while ((opt = getopt(argc, argv, "hqvc:s:")) != -1) { switch (opt) { case 'q': opts->log_level -= 1; @@ -34,6 +36,10 @@ int parse_options(struct argent_options *opts, int argc, char **argv) opts->conf_filename = optarg; break; + case 's': + opts->script_filename = optarg; + break; + case 'h': print_usage(argv[0]); return 2; diff --git a/src/options.h b/src/options.h index a54e2fa..c144925 100644 --- a/src/options.h +++ b/src/options.h @@ -3,6 +3,7 @@ struct argent_options { const char *conf_filename; + const char *script_filename; int log_level; }; -- cgit v1.2.1