From 89202529ea5460267161e45804ebaa0fc8f79183 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Wed, 25 Jan 2017 13:50:52 +0100 Subject: [PATCH 1/3] Move some code from luaa.c to awesome.c It makes more sense to append the xdg config dirs to our list of searchpath in awesome.c than in luaa.c. Plus, it simplifies some of the following work. Signed-off-by: Uli Schlachter --- awesome.c | 13 +++++++++++++ luaa.c | 13 ------------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/awesome.c b/awesome.c index 8aac69e6..2f0213ca 100644 --- a/awesome.c +++ b/awesome.c @@ -545,6 +545,19 @@ main(int argc, char **argv) /* Get XDG basedir data */ xdgInitHandle(&xdg); + /* add XDG_CONFIG_DIR as include path */ + const char * const *xdgconfigdirs = xdgSearchableConfigDirectories(&xdg); + for(; *xdgconfigdirs; xdgconfigdirs++) + { + /* Append /awesome to *xdgconfigdirs */ + const char *suffix = "/awesome"; + size_t len = a_strlen(*xdgconfigdirs) + a_strlen(suffix) + 1; + char *entry = p_new(char, len); + a_strcat(entry, len, *xdgconfigdirs); + a_strcat(entry, len, suffix); + string_array_append(&searchpath, entry); + } + /* init lua */ luaA_init(&xdg, &searchpath); string_array_wipe(&searchpath); diff --git a/luaa.c b/luaa.c index cfacd8b8..0f139d65 100644 --- a/luaa.c +++ b/luaa.c @@ -777,19 +777,6 @@ luaA_init(xdgHandle* xdg, string_array_t *searchpath) /* Export keys */ key_class_setup(L); - /* add XDG_CONFIG_DIR as include path */ - const char * const *xdgconfigdirs = xdgSearchableConfigDirectories(xdg); - for(; *xdgconfigdirs; xdgconfigdirs++) - { - /* Append /awesome to *xdgconfigdirs */ - const char *suffix = "/awesome"; - size_t len = a_strlen(*xdgconfigdirs) + a_strlen(suffix) + 1; - char *entry = p_new(char, len); - a_strcat(entry, len, *xdgconfigdirs); - a_strcat(entry, len, suffix); - string_array_append(searchpath, entry); - } - /* add Lua search paths */ lua_getglobal(L, "package"); if (LUA_TTABLE != lua_type(L, 1)) From 95b1305613dada364c437cf8005398f4d0769dd6 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Wed, 25 Jan 2017 14:06:41 +0100 Subject: [PATCH 2/3] Refactor config fallback Before this, there was a function which attempted to load different configuration files in the right order. There was a boolean argument that decided if we are actually loading it or just checking for syntax error (for the --check argument of awesome). This lead to some not-so-nice code since we do not want to fall-back to another config when checking for syntax errors. Refactor this so that there is now a function which calls a callback function with the different paths that should be tried. This function returns as soon as the callback function returns true. Since just checking if the config syntax is ok does not depend on any Lua state, an empty Lua state is now used for this. Signed-off-by: Uli Schlachter --- awesome.c | 25 +++++++++++++++++++++++-- luaa.c | 51 ++++++++++++++++++++++++--------------------------- luaa.h | 5 ++++- 3 files changed, 51 insertions(+), 30 deletions(-) diff --git a/awesome.c b/awesome.c index 2f0213ca..325b7842 100644 --- a/awesome.c +++ b/awesome.c @@ -433,6 +433,12 @@ restart_on_signal(gpointer data) return TRUE; } +static bool +true_config_callback(const char *unused) +{ + return true; +} + /** Print help and exit(2) with given exit_code. * \param exit_code The exit code. */ @@ -564,7 +570,22 @@ main(int argc, char **argv) if (run_test) { - if(!luaA_parserc(&xdg, confpath, false)) + bool success = true; + /* Get the first config that will be tried */ + const char *config = luaA_find_config(&xdg, confpath, true_config_callback); + + /* Try to parse it */ + lua_State *L = luaL_newstate(); + if(luaL_loadfile(L, config)) + { + const char *err = lua_tostring(L, -1); + fprintf(stderr, "%s\n", err); + success = false; + } + p_delete(&config); + lua_close(L); + + if(!success) { fprintf(stderr, "✘ Configuration file syntax error.\n"); return EXIT_FAILURE; @@ -738,7 +759,7 @@ main(int argc, char **argv) root_update_wallpaper(); /* Parse and run configuration file */ - if (!luaA_parserc(&xdg, confpath, true)) + if (!luaA_parserc(&xdg, confpath)) fatal("couldn't find any rc file"); p_delete(&confpath); diff --git a/luaa.c b/luaa.c index 0f139d65..26e78bdb 100644 --- a/luaa.c +++ b/luaa.c @@ -804,7 +804,7 @@ luaA_startup_error(const char *err) } static bool -luaA_loadrc(const char *confpath, bool run) +luaA_loadrc(const char *confpath) { lua_State *L = globalconf_get_lua_State(); if(luaL_loadfile(L, confpath)) @@ -816,12 +816,6 @@ luaA_loadrc(const char *confpath, bool run) return false; } - if(!run) - { - lua_pop(L, 1); - return true; - } - /* Set the conffile right now so it can be used inside the * configuration file. */ conffile = a_strdup(confpath); @@ -852,21 +846,28 @@ luaA_loadrc(const char *confpath, bool run) * \param run Run the configuration file. */ bool -luaA_parserc(xdgHandle* xdg, const char *confpatharg, bool run) +luaA_parserc(xdgHandle* xdg, const char *confpatharg) +{ + const char *confpath = luaA_find_config(xdg, confpatharg, luaA_loadrc); + bool ret = confpath != NULL; + p_delete(&confpath); + + return ret; +} + +/** Find a config file for which the given callback returns true. + * \param xdg An xdg handle to use to get XDG basedir. + * \param confpatharg The configuration file to load. + * \param callback The callback to call. + */ +const char * +luaA_find_config(xdgHandle* xdg, const char *confpatharg, luaA_config_callback *callback) { char *confpath = NULL; - bool ret = false; - /* try to load, return if it's ok */ - if(confpatharg) + if(confpatharg && callback(confpatharg)) { - if(luaA_loadrc(confpatharg, run)) - { - ret = true; - goto bailout; - } - else if(!run) - goto bailout; + return a_strdup(confpatharg); } confpath = xdgConfigFind("awesome/rc.lua", xdg); @@ -876,21 +877,17 @@ luaA_parserc(xdgHandle* xdg, const char *confpatharg, bool run) /* confpath is "string1\0string2\0string3\0\0" */ while(*tmp) { - if(luaA_loadrc(tmp, run)) + if(callback(tmp)) { - ret = true; - goto bailout; + const char *ret = a_strdup(tmp); + p_delete(&confpath); + return ret; } - else if(!run) - goto bailout; tmp += a_strlen(tmp) + 1; } -bailout: - p_delete(&confpath); - - return ret; + return NULL; } int diff --git a/luaa.h b/luaa.h index 01a03ec6..d1420d3d 100644 --- a/luaa.h +++ b/luaa.h @@ -303,8 +303,11 @@ luaA_registerfct(lua_State *L, int idx, int *fct) return luaA_register(L, idx, fct); } +typedef bool luaA_config_callback(const char *); + void luaA_init(xdgHandle *, string_array_t *); -bool luaA_parserc(xdgHandle *, const char *, bool); +const char *luaA_find_config(xdgHandle *, const char *, luaA_config_callback *); +bool luaA_parserc(xdgHandle *, const char *); /** Global signals */ signal_array_t global_signals; From b4b007796cccbda6aabfc3b8898740b4fb760edb Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Wed, 25 Jan 2017 14:55:27 +0100 Subject: [PATCH 3/3] Initialise Lua later This moves Lua initialisation to the latest possible point, just before loading the configuration file. This requires to also move screen initialisation and parts of EWMH initialisation to a later point. Signed-off-by: Uli Schlachter --- awesome.c | 17 +++++++++-------- ewmh.c | 6 +++++- ewmh.h | 1 + 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/awesome.c b/awesome.c index 325b7842..7ac16293 100644 --- a/awesome.c +++ b/awesome.c @@ -564,10 +564,6 @@ main(int argc, char **argv) string_array_append(&searchpath, entry); } - /* init lua */ - luaA_init(&xdg, &searchpath); - string_array_wipe(&searchpath); - if (run_test) { bool success = true; @@ -704,10 +700,6 @@ main(int argc, char **argv) /* init atom cache */ atoms_init(globalconf.connection); - /* init screens information */ - screen_scan(); - - /* do this only for real screen */ ewmh_init(); systray_init(); @@ -758,6 +750,15 @@ main(int argc, char **argv) /* get the current wallpaper, from now on we are informed when it changes */ root_update_wallpaper(); + /* init lua */ + luaA_init(&xdg, &searchpath); + string_array_wipe(&searchpath); + + ewmh_init_lua(); + + /* init screens information */ + screen_scan(); + /* Parse and run configuration file */ if (!luaA_parserc(&xdg, confpath)) fatal("couldn't find any rc file"); diff --git a/ewmh.c b/ewmh.c index f14f32e9..ca862a1e 100644 --- a/ewmh.c +++ b/ewmh.c @@ -125,7 +125,6 @@ ewmh_client_update_frame_extents(lua_State *L) void ewmh_init(void) { - lua_State *L = globalconf_get_lua_State(); xcb_window_t father; xcb_screen_t *xscreen = globalconf.screen; xcb_atom_t atom[] = @@ -206,7 +205,12 @@ ewmh_init(void) i = getpid(); xcb_change_property(globalconf.connection, XCB_PROP_MODE_REPLACE, father, _NET_WM_PID, XCB_ATOM_CARDINAL, 32, 1, &i); +} +void +ewmh_init_lua(void) +{ + lua_State *L = globalconf_get_lua_State(); luaA_class_connect_signal(L, &client_class, "focus", ewmh_update_net_active_window); luaA_class_connect_signal(L, &client_class, "unfocus", ewmh_update_net_active_window); diff --git a/ewmh.h b/ewmh.h index ad11d207..67e3f3ae 100644 --- a/ewmh.h +++ b/ewmh.h @@ -30,6 +30,7 @@ typedef struct client_t client_t; void ewmh_init(void); +void ewmh_init_lua(void); void ewmh_update_net_numbers_of_desktop(void); int ewmh_update_net_current_desktop(lua_State *); void ewmh_update_net_desktop_names(void);