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 SYNOPSIS
-------- --------
*awesome* [*-v* | *--version*] [*-h* | *--help*] [*-c* | *--config* 'FILE'] *awesome* [*-v* | *--version*] [*-h* | *--help*] [*-c* | *--config* 'FILE'] [*-k* | *--check*]
DESCRIPTION DESCRIPTION
----------- -----------
@ -40,6 +40,8 @@ OPTIONS
Print help information, then exit. Print help information, then exit.
*-c*, *--config* 'FILE':: *-c*, *--config* 'FILE'::
Use an alternate configuration file instead of '$XDG_CONFIG_HOME/awesome/rc.lua'. Use an alternate configuration file instead of '$XDG_CONFIG_HOME/awesome/rc.lua'.
*-k*, *--check*::
Check configuration file syntax.
DEFAULT MOUSE BINDINGS DEFAULT MOUSE BINDINGS
----------------------- -----------------------

View File

@ -290,7 +290,8 @@ exit_help(int exit_code)
"Usage: awesome [OPTION]\n\ "Usage: awesome [OPTION]\n\
-h, --help show help\n\ -h, --help show help\n\
-v, --version show version\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); exit(exit_code);
} }
@ -309,10 +310,11 @@ main(int argc, char **argv)
ssize_t cmdlen = 1; ssize_t cmdlen = 1;
static struct option long_options[] = static struct option long_options[] =
{ {
{"help", 0, NULL, 'h'}, { "help", 0, NULL, 'h' },
{"version", 0, NULL, 'v'}, { "version", 0, NULL, 'v' },
{"config", 1, NULL, 'c'}, { "config", 1, NULL, 'c' },
{NULL, 0, NULL, 0} { "check", 0, NULL, 'k' },
{ NULL, 0, NULL, 0 }
}; };
/* event loop watchers */ /* event loop watchers */
@ -339,8 +341,14 @@ main(int argc, char **argv)
a_strcat(globalconf.argv, cmdlen, argv[i]); a_strcat(globalconf.argv, cmdlen, argv[i]);
} }
/* Text won't be printed correctly otherwise */
setlocale(LC_CTYPE, "");
/* init lua */
luaA_init();
/* check args */ /* check args */
while((opt = getopt_long(argc, argv, "vhc:", while((opt = getopt_long(argc, argv, "vhkc:",
long_options, NULL)) != -1) long_options, NULL)) != -1)
switch(opt) switch(opt)
{ {
@ -350,6 +358,17 @@ main(int argc, char **argv)
case 'h': case 'h':
exit_help(EXIT_SUCCESS); exit_help(EXIT_SUCCESS);
break; 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': case 'c':
if(a_strlen(optarg)) if(a_strlen(optarg))
confpath = a_strdup(optarg); confpath = a_strdup(optarg);
@ -358,8 +377,6 @@ main(int argc, char **argv)
break; break;
} }
/* Text won't be printed correctly otherwise */
setlocale(LC_CTYPE, "");
globalconf.loop = ev_default_loop(0); globalconf.loop = ev_default_loop(0);
ev_timer_init(&globalconf.timer, &luaA_on_timer, 0., 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.keysyms, &globalconf.numlockmask,
&globalconf.shiftlockmask, &globalconf.capslockmask); &globalconf.shiftlockmask, &globalconf.capslockmask);
/* init lua */ /* Parse and run configuration file */
luaA_init(); luaA_parserc(confpath, true);
luaA_parserc(confpath);
/* scan existing windows */ /* scan existing windows */
scan(); scan();

67
luaa.c
View File

@ -702,27 +702,47 @@ luaA_init(void)
#define AWESOME_CONFIG_FILE "/awesome/rc.lua" #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. /** 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 bool
luaA_parserc(const char *confpatharg) luaA_parserc(const char *confpatharg, bool run)
{ {
int screen; int screen;
const char *confdir, *xdg_config_dirs; const char *confdir, *xdg_config_dirs;
char *confpath = NULL, **xdg_files, **buf, path[1024]; char *confpath = NULL, **xdg_files, **buf, path[1024];
ssize_t len; ssize_t len;
bool ret;
if(confpatharg) ret = luaA_loadrc(confpatharg, run);
{
if(luaL_dofile(globalconf.L, confpatharg))
fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
else
{
globalconf.conffile = a_strdup(confpatharg);
goto bailout;
}
}
confdir = getenv("XDG_CONFIG_HOME"); confdir = getenv("XDG_CONFIG_HOME");
@ -748,13 +768,8 @@ luaA_parserc(const char *confpatharg)
} }
a_strcat(confpath, len, AWESOME_CONFIG_FILE); a_strcat(confpath, len, AWESOME_CONFIG_FILE);
if(luaL_dofile(globalconf.L, confpath)) if(!ret)
fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1)); ret = luaA_loadrc(confpath, run);
else
{
globalconf.conffile = a_strdup(confpath);
goto bailout;
}
xdg_config_dirs = getenv("XDG_CONFIG_DIRS"); xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
@ -775,20 +790,14 @@ luaA_parserc(const char *confpatharg)
a_strcat(confpath, len, AWESOME_CONFIG_FILE); a_strcat(confpath, len, AWESOME_CONFIG_FILE);
snprintf(path, sizeof(path), "package.path = package.path .. \";%s/awesome/?.lua\"", *buf); snprintf(path, sizeof(path), "package.path = package.path .. \";%s/awesome/?.lua\"", *buf);
luaA_dostring(globalconf.L, path); luaA_dostring(globalconf.L, path);
if(luaL_dofile(globalconf.L, confpath)) if(!ret)
fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1)); ret = luaA_loadrc(confpath, run);
else
{
globalconf.conffile = a_strdup(confpath);
break;
}
} }
for(buf = xdg_files; *buf; buf++) for(buf = xdg_files; *buf; buf++)
p_delete(buf); p_delete(buf);
p_delete(&xdg_files); p_delete(&xdg_files);
bailout:
/* Assure there's at least one tag */ /* Assure there's at least one tag */
for(screen = 0; screen < globalconf.nscreen; screen++) for(screen = 0; screen < globalconf.nscreen; screen++)
if(!globalconf.screens[screen].tags.len) if(!globalconf.screens[screen].tags.len)
@ -796,6 +805,8 @@ luaA_parserc(const char *confpatharg)
&globalconf.screens[screen]); &globalconf.screens[screen]);
p_delete(&confpath); p_delete(&confpath);
return ret;
} }
/** Parse a command. /** Parse a command.

2
luaa.h
View File

@ -255,7 +255,7 @@ luaA_otable_new(lua_State *L)
} }
void luaA_init(void); void luaA_init(void);
void luaA_parserc(const char *); bool luaA_parserc(const char *, bool);
void luaA_cs_init(void); void luaA_cs_init(void);
void luaA_cs_cleanup(void); void luaA_cs_cleanup(void);
void luaA_on_timer(EV_P_ ev_timer *w, int revents); void luaA_on_timer(EV_P_ ev_timer *w, int revents);