lua: add --check option

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-09-30 16:59:08 +02:00
parent 581a8b1103
commit afbcd681be
4 changed files with 70 additions and 42 deletions

View File

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

View File

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

67
luaa.c
View File

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

2
luaa.h
View File

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