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 <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2017-01-25 14:06:41 +01:00
parent 89202529ea
commit 95b1305613
3 changed files with 51 additions and 30 deletions

View File

@ -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);

51
luaa.c
View File

@ -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

5
luaa.h
View File

@ -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;