config: use XDG for loading config

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-07-08 14:07:56 +02:00
parent 903e694611
commit fb13bff697
8 changed files with 75 additions and 103 deletions

View File

@ -59,7 +59,6 @@ set(AWE_SRCS
${SOURCE_DIR}/window.c
${SOURCE_DIR}/common/buffer.c
${SOURCE_DIR}/common/atoms.c
${SOURCE_DIR}/common/configopts.c
${SOURCE_DIR}/common/draw.c
${SOURCE_DIR}/common/markup.c
${SOURCE_DIR}/common/socket.c
@ -347,7 +346,7 @@ endif()
# {{{ Installation
install(TARGETS ${PROJECT_AWE_NAME} ${PROJECT_AWECLIENT_NAME} RUNTIME DESTINATION bin)
install(FILES ${AWE_LUA_FILES} ${AWE_LUAC_FILES} DESTINATION ${AWESOME_LUA_LIB_PATH})
install(FILES ${AWE_CONF_FILES} DESTINATION ${AWESOME_CONF_PATH})
install(FILES ${AWE_CONF_FILES} DESTINATION ${AWESOME_SYSCONFIG_DIR})
if(GENERATE_MANPAGES)
install(FILES ${AWE_MAN1_FILES} DESTINATION ${AWESOME_MAN_PATH}/man1)
install(FILES ${AWE_MAN5_FILES} DESTINATION ${AWESOME_MAN_PATH}/man5)

View File

@ -38,7 +38,6 @@
#include "systray.h"
#include "common/socket.h"
#include "common/version.h"
#include "common/configopts.h"
#include "common/atoms.h"
#include "config.h"
@ -418,17 +417,7 @@ main(int argc, char **argv)
/* init lua */
luaA_init();
/* parse config */
if(!confpath)
confpath = config_file();
if(!luaA_parserc(confpath))
{
const char *default_confpath = AWESOME_CONF_PATH "/awesomerc.lua";
warn("failed to load/parse configuration file %s", confpath);
warn("falling back to: %s", default_confpath);
if(!luaA_parserc(default_confpath))
fatal("failed to load any configuration file");
}
luaA_parserc(confpath);
/* scan existing windows */
scan();

View File

@ -224,7 +224,7 @@ set(AWESOME_COMPILE_MACHINE ${CMAKE_SYSTEM_PROCESSOR})
set(AWESOME_COMPILE_HOSTNAME ${BUILDHOSTNAME})
set(AWESOME_COMPILE_BY $ENV{USER})
set(AWESOME_RELEASE ${CODENAME})
set(AWESOME_CONF_PATH ${SYSCONFDIR}/${PROJECT_AWE_NAME})
set(AWESOME_SYSCONFIG_DIR ${SYSCONFDIR}/xdg/${PROJECT_AWE_NAME})
set(AWESOME_DATA_PATH ${PREFIX}/share/${PROJECT_AWE_NAME})
set(AWESOME_DOC_PATH ${PREFIX}/share/doc/${PROJECT_AWE_NAME})
set(AWESOME_MAN_PATH ${PREFIX}/share/man)

View File

@ -1,47 +0,0 @@
/*
* configopts.c - configuration options
*
* Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "common/configopts.h"
#include "common/util.h"
#define AWESOME_CONFIG_FILE ".awesomerc.lua"
/** Return default configuration file path
* \return path to the default configuration file
*/
char *
config_file(void)
{
const char *homedir;
char * confpath;
ssize_t confpath_len;
homedir = getenv("HOME");
confpath_len = a_strlen(homedir) + a_strlen(AWESOME_CONFIG_FILE) + 2;
confpath = p_new(char, confpath_len);
a_strcpy(confpath, confpath_len, homedir);
a_strcat(confpath, confpath_len, "/");
a_strcat(confpath, confpath_len, AWESOME_CONFIG_FILE);
return confpath;
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80

View File

@ -1,29 +0,0 @@
/*
* configopts.h - configuration options header
*
* Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#ifndef AWESOME_COMMON_CONFIGOPTS_H
#define AWESOME_COMMON_CONFIGOPTS_H
char * config_file(void);
#endif
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80

View File

@ -2,7 +2,7 @@
#define _CONFIG_H_
#define AWESOME_LUA_LIB_PATH "@AWESOME_LUA_LIB_PATH@"
#define AWESOME_CONF_PATH "@AWESOME_CONF_PATH@"
#define AWESOME_SYSCONFIG_DIR "@AWESOME_SYSCONFIG_DIR@"
#cmakedefine WITH_DBUS
#cmakedefine WITH_IMLIB2

76
lua.c
View File

@ -543,28 +543,88 @@ luaA_init(void)
luaA_dostring(L, "package.path = package.path .. \";" AWESOME_LUA_LIB_PATH "/?.lua\"");
}
/** Load a configuration file
*
#define XDG_CONFIG_HOME_DEFAULT "/.config/awesome/"
#define AWESOME_CONFIG_FILE "rc.lua"
/** Load a configuration file.
* \param rcfile The configuration file to load.
* \return True on succes, false on failure.
*/
bool
luaA_parserc(const char* rcfile)
void
luaA_parserc(const char *confpatharg)
{
int screen;
const char *confdir, *xdg_config_dirs;
char *confpath = NULL, **xdg_files, **buf;
ssize_t len;
if(luaL_dofile(globalconf.L, rcfile))
if(confpatharg)
{
if(luaL_dofile(globalconf.L, confpatharg))
fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
return false;
else
goto bailout;
}
confdir = getenv("XDG_CONFIG_HOME");
if(a_strlen(confdir))
{
len = a_strlen(confdir) + sizeof(AWESOME_CONFIG_FILE) + 2;
confpath = p_new(char, len);
a_strcpy(confpath, len, confdir);
a_strcat(confpath, len, "/");
}
else
{
confdir = getenv("HOME");
len = a_strlen(confdir) + sizeof(AWESOME_CONFIG_FILE) + sizeof(XDG_CONFIG_HOME_DEFAULT) + 1;
confpath = p_new(char, len);
a_strcpy(confpath, len, confdir);
a_strcat(confpath, len, XDG_CONFIG_HOME_DEFAULT);
}
a_strcat(confpath, len, AWESOME_CONFIG_FILE);
if(luaL_dofile(globalconf.L, confpath))
fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
else
goto bailout;
xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
if(!(len = a_strlen(xdg_config_dirs)))
{
xdg_config_dirs = AWESOME_SYSCONFIG_DIR;
len = sizeof(AWESOME_SYSCONFIG_DIR);
}
xdg_files = a_strsplit(xdg_config_dirs, len, ':');
for(buf = xdg_files; *buf; buf++)
{
p_delete(&confpath);
len = a_strlen(*buf) + sizeof("AWESOME_CONFIG_FILE") + 2;
confpath = p_new(char, len);
a_strcpy(confpath, len, *buf);
a_strcat(confpath, len, "/");
a_strcat(confpath, len, AWESOME_CONFIG_FILE);
if(luaL_dofile(globalconf.L, confpath))
fprintf(stderr, "%s\n", lua_tostring(globalconf.L, -1));
else
break;
}
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.screens_info->nscreen; screen++)
if(!globalconf.screens[screen].tags.len)
tag_append_to_screen(tag_new("default", sizeof("default"), layout_tile, 0.5, 1, 0), screen);
return true;
p_delete(&confpath);
}
/** Parse a command.

2
lua.h
View File

@ -190,7 +190,7 @@ luaA_usemetatable(lua_State *L, int idxobj, int idxfield)
}
void luaA_init(void);
bool luaA_parserc(const char *);
void luaA_parserc(const char *);
void luaA_pushpointer(lua_State *, void *, awesome_type_t);
void luaA_cs_init(void);
void luaA_cs_cleanup(void);