From afbcd681bee019105a97f02ae040781bdfb31de2 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Tue, 30 Sep 2008 16:59:08 +0200 Subject: [PATCH] lua: add --check option Signed-off-by: Julien Danjou --- awesome.1.txt | 4 ++- awesome.c | 39 +++++++++++++++++++++--------- luaa.c | 67 ++++++++++++++++++++++++++++++--------------------- luaa.h | 2 +- 4 files changed, 70 insertions(+), 42 deletions(-) diff --git a/awesome.1.txt b/awesome.1.txt index a1961b52..a86fbe29 100644 --- a/awesome.1.txt +++ b/awesome.1.txt @@ -9,7 +9,7 @@ awesome - awesome window manager SYNOPSIS -------- -*awesome* [*-v* | *--version*] [*-h* | *--help*] [*-c* | *--config* 'FILE'] +*awesome* [*-v* | *--version*] [*-h* | *--help*] [*-c* | *--config* 'FILE'] [*-k* | *--check*] DESCRIPTION ----------- @@ -40,6 +40,8 @@ OPTIONS Print help information, then exit. *-c*, *--config* 'FILE':: Use an alternate configuration file instead of '$XDG_CONFIG_HOME/awesome/rc.lua'. +*-k*, *--check*:: + Check configuration file syntax. DEFAULT MOUSE BINDINGS ----------------------- diff --git a/awesome.c b/awesome.c index dcc8c207..144d7652 100644 --- a/awesome.c +++ b/awesome.c @@ -290,7 +290,8 @@ exit_help(int exit_code) "Usage: awesome [OPTION]\n\ -h, --help show help\n\ -v, --version show version\n\ - -c, --config FILE configuration file to use\n"); + -c, --config FILE configuration file to use\n\ + -k, --check check configuration file syntax\n"); exit(exit_code); } @@ -309,10 +310,11 @@ main(int argc, char **argv) ssize_t cmdlen = 1; static struct option long_options[] = { - {"help", 0, NULL, 'h'}, - {"version", 0, NULL, 'v'}, - {"config", 1, NULL, 'c'}, - {NULL, 0, NULL, 0} + { "help", 0, NULL, 'h' }, + { "version", 0, NULL, 'v' }, + { "config", 1, NULL, 'c' }, + { "check", 0, NULL, 'k' }, + { NULL, 0, NULL, 0 } }; /* event loop watchers */ @@ -339,8 +341,14 @@ main(int argc, char **argv) a_strcat(globalconf.argv, cmdlen, argv[i]); } + /* Text won't be printed correctly otherwise */ + setlocale(LC_CTYPE, ""); + + /* init lua */ + luaA_init(); + /* check args */ - while((opt = getopt_long(argc, argv, "vhc:", + while((opt = getopt_long(argc, argv, "vhkc:", long_options, NULL)) != -1) switch(opt) { @@ -350,6 +358,17 @@ main(int argc, char **argv) case 'h': exit_help(EXIT_SUCCESS); break; + case 'k': + if(!luaA_parserc(confpath, false)) + { + fprintf(stderr, "✘ Configuration file error.\n"); + return EXIT_FAILURE; + } + else + { + fprintf(stderr, "✔ Configuration file OK.\n"); + return EXIT_SUCCESS; + } case 'c': if(a_strlen(optarg)) confpath = a_strdup(optarg); @@ -358,8 +377,6 @@ main(int argc, char **argv) break; } - /* Text won't be printed correctly otherwise */ - setlocale(LC_CTYPE, ""); globalconf.loop = ev_default_loop(0); ev_timer_init(&globalconf.timer, &luaA_on_timer, 0., 0.); @@ -457,10 +474,8 @@ main(int argc, char **argv) globalconf.keysyms, &globalconf.numlockmask, &globalconf.shiftlockmask, &globalconf.capslockmask); - /* init lua */ - luaA_init(); - - luaA_parserc(confpath); + /* Parse and run configuration file */ + luaA_parserc(confpath, true); /* scan existing windows */ scan(); diff --git a/luaa.c b/luaa.c index 263856e3..bafe174c 100644 --- a/luaa.c +++ b/luaa.c @@ -702,27 +702,47 @@ luaA_init(void) #define AWESOME_CONFIG_FILE "/awesome/rc.lua" +static bool +luaA_loadrc(const char *confpath, bool run) +{ + if(confpath) + { + if(!luaL_loadfile(globalconf.L, confpath)) + { + if(run) + { + if(lua_pcall(globalconf.L, 0, LUA_MULTRET, 0)) + fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1)); + else + { + globalconf.conffile = a_strdup(confpath); + return true; + } + } + else + lua_pop(globalconf.L, 1); + return true; + } + else + fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1)); + } + return false; +} + /** Load a configuration file. - * \param rcfile The configuration file to load. + * \param confpatharg The configuration file to load. + * \param run Run the configuration file. */ -void -luaA_parserc(const char *confpatharg) +bool +luaA_parserc(const char *confpatharg, bool run) { int screen; const char *confdir, *xdg_config_dirs; char *confpath = NULL, **xdg_files, **buf, path[1024]; ssize_t len; + bool ret; - if(confpatharg) - { - if(luaL_dofile(globalconf.L, confpatharg)) - fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1)); - else - { - globalconf.conffile = a_strdup(confpatharg); - goto bailout; - } - } + ret = luaA_loadrc(confpatharg, run); confdir = getenv("XDG_CONFIG_HOME"); @@ -748,13 +768,8 @@ luaA_parserc(const char *confpatharg) } a_strcat(confpath, len, AWESOME_CONFIG_FILE); - if(luaL_dofile(globalconf.L, confpath)) - fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1)); - else - { - globalconf.conffile = a_strdup(confpath); - goto bailout; - } + if(!ret) + ret = luaA_loadrc(confpath, run); xdg_config_dirs = getenv("XDG_CONFIG_DIRS"); @@ -775,20 +790,14 @@ luaA_parserc(const char *confpatharg) a_strcat(confpath, len, AWESOME_CONFIG_FILE); snprintf(path, sizeof(path), "package.path = package.path .. \";%s/awesome/?.lua\"", *buf); luaA_dostring(globalconf.L, path); - if(luaL_dofile(globalconf.L, confpath)) - fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1)); - else - { - globalconf.conffile = a_strdup(confpath); - break; - } + if(!ret) + ret = luaA_loadrc(confpath, run); } for(buf = xdg_files; *buf; buf++) p_delete(buf); p_delete(&xdg_files); - bailout: /* Assure there's at least one tag */ for(screen = 0; screen < globalconf.nscreen; screen++) if(!globalconf.screens[screen].tags.len) @@ -796,6 +805,8 @@ luaA_parserc(const char *confpatharg) &globalconf.screens[screen]); p_delete(&confpath); + + return ret; } /** Parse a command. diff --git a/luaa.h b/luaa.h index 0c9e6c15..cb210920 100644 --- a/luaa.h +++ b/luaa.h @@ -255,7 +255,7 @@ luaA_otable_new(lua_State *L) } void luaA_init(void); -void luaA_parserc(const char *); +bool luaA_parserc(const char *, bool); void luaA_cs_init(void); void luaA_cs_cleanup(void); void luaA_on_timer(EV_P_ ev_timer *w, int revents);