Port the path resolution and config loading code from -next.

This commit is contained in:
Fernando Tarlá Cardoso Lemos 2010-11-14 22:54:40 -02:00 committed by Michael Stapelberg
parent c0c7d04264
commit bfa12a5819
5 changed files with 47 additions and 39 deletions

View File

@ -20,6 +20,7 @@ CFLAGS += -Wunused-value
CFLAGS += -Iinclude CFLAGS += -Iinclude
CFLAGS += -I/usr/local/include CFLAGS += -I/usr/local/include
CFLAGS += -DI3_VERSION=\"${GIT_VERSION}\" CFLAGS += -DI3_VERSION=\"${GIT_VERSION}\"
CFLAGS += -DSYSCONFDIR=\"${SYSCONFDIR}\"
LDFLAGS += -lm LDFLAGS += -lm
LDFLAGS += -lxcb-event LDFLAGS += -lxcb-event

View File

@ -127,7 +127,7 @@ struct Config {
} bar; } bar;
}; };
char *glob_path(const char *path); char *resolve_tilde(const char *path);
bool path_exists(const char *path); bool path_exists(const char *path);
/** /**

View File

@ -22,33 +22,38 @@
Config config; Config config;
struct modes_head modes; struct modes_head modes;
/* /*
* This function resolves ~ in pathnames. * This function resolves ~ in pathnames.
* It may resolve wildcards in the first part of the path, but if no match
* or multiple matches are found, it just returns a copy of path as given.
* *
*/ */
char *glob_path(const char *path) { char *resolve_tilde(const char *path) {
static glob_t globbuf; static glob_t globbuf;
if (glob(path, GLOB_NOCHECK | GLOB_TILDE, NULL, &globbuf) < 0) char *head, *tail, *result;
die("glob() failed");
char *result = sstrdup(globbuf.gl_pathc > 0 ? globbuf.gl_pathv[0] : path);
globfree(&globbuf);
/* If the file does not exist yet, we still may need to resolve tilde, tail = strchr(path, '/');
* so call wordexp */ head = strndup(path, tail ? tail - path : strlen(path));
if (strcmp(result, path) == 0) {
wordexp_t we; int res = glob(head, GLOB_TILDE, NULL, &globbuf);
wordexp(path, &we, WRDE_NOCMD); free(head);
if (we.we_wordc > 0) { /* no match, or many wildcard matches are bad */
free(result); if (res == GLOB_NOMATCH || globbuf.gl_pathc != 1)
result = sstrdup(we.we_wordv[0]); result = sstrdup(path);
} else if (res != 0) {
wordfree(&we); die("glob() failed");
} else {
head = globbuf.gl_pathv[0];
result = scalloc(strlen(head) + (tail ? strlen(tail) : 0) + 1);
strncpy(result, head, strlen(head));
strncat(result, tail, strlen(tail));
} }
globfree(&globbuf);
return result; return result;
} }
/* /*
* Checks if the given path exists by calling stat(). * Checks if the given path exists by calling stat().
* *
@ -205,18 +210,24 @@ void switch_mode(xcb_connection_t *conn, const char *new_mode) {
} }
/* /*
* Get the path of the first configuration file found. Checks the XDG folders * Get the path of the first configuration file found. Checks the home directory
* first ($XDG_CONFIG_HOME, $XDG_CONFIG_DIRS), then the traditional paths. * first, then the system directory first, always taking into account the XDG
* Base Directory Specification ($XDG_CONFIG_HOME, $XDG_CONFIG_DIRS)
* *
*/ */
static char *get_config_path() { static char *get_config_path() {
/* 1: check for $XDG_CONFIG_HOME/i3/config */
char *xdg_config_home, *xdg_config_dirs, *config_path; char *xdg_config_home, *xdg_config_dirs, *config_path;
/* 1: check the traditional path under the home directory */
config_path = resolve_tilde("~/.i3/config");
if (path_exists(config_path))
return config_path;
/* 2: check for $XDG_CONFIG_HOME/i3/config */
if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL) if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL)
xdg_config_home = "~/.config"; xdg_config_home = "~/.config";
xdg_config_home = glob_path(xdg_config_home); xdg_config_home = resolve_tilde(xdg_config_home);
if (asprintf(&config_path, "%s/i3/config", xdg_config_home) == -1) if (asprintf(&config_path, "%s/i3/config", xdg_config_home) == -1)
die("asprintf() failed"); die("asprintf() failed");
free(xdg_config_home); free(xdg_config_home);
@ -225,14 +236,19 @@ static char *get_config_path() {
return config_path; return config_path;
free(config_path); free(config_path);
/* 2: check for $XDG_CONFIG_DIRS/i3/config */ /* 3: check the traditional path under /etc */
config_path = SYSCONFDIR "/i3/config";
if (path_exists(config_path))
return sstrdup(config_path);
/* 4: check for $XDG_CONFIG_DIRS/i3/config */
if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL) if ((xdg_config_dirs = getenv("XDG_CONFIG_DIRS")) == NULL)
xdg_config_dirs = "/etc/xdg"; xdg_config_dirs = "/etc/xdg";
char *buf = strdup(xdg_config_dirs); char *buf = sstrdup(xdg_config_dirs);
char *tok = strtok(buf, ":"); char *tok = strtok(buf, ":");
while (tok != NULL) { while (tok != NULL) {
tok = glob_path(tok); tok = resolve_tilde(tok);
if (asprintf(&config_path, "%s/i3/config", tok) == -1) if (asprintf(&config_path, "%s/i3/config", tok) == -1)
die("asprintf() failed"); die("asprintf() failed");
free(tok); free(tok);
@ -245,18 +261,9 @@ static char *get_config_path() {
} }
free(buf); free(buf);
/* 3: check traditional paths */ die("Unable to find the configuration file (looked at "
config_path = glob_path("~/.i3/config"); "~/.i3/config, $XDG_CONFIG_HOME/i3/config, "
if (path_exists(config_path)) SYSCONFDIR "i3/config and $XDG_CONFIG_DIRS/i3/config)");
return config_path;
config_path = strdup("/etc/i3/config");
if (!path_exists(config_path))
die("Neither $XDG_CONFIG_HOME/i3/config, nor "
"$XDG_CONFIG_DIRS/i3/config, nor ~/.i3/config nor "
"/etc/i3/config exist.");
return config_path;
} }
/* /*

View File

@ -14,7 +14,7 @@ struct all_cons_head all_cons = TAILQ_HEAD_INITIALIZER(all_cons);
* *
*/ */
bool tree_restore() { bool tree_restore() {
char *globbed = glob_path("~/.i3/_restart.json"); char *globbed = resolve_tilde("~/.i3/_restart.json");
if (!path_exists(globbed)) { if (!path_exists(globbed)) {
LOG("%s does not exist, not restoring tree\n", globbed); LOG("%s does not exist, not restoring tree\n", globbed);
@ -27,7 +27,7 @@ bool tree_restore() {
focused = croot; focused = croot;
tree_append_json(globbed); tree_append_json(globbed);
char *old_restart = glob_path("~/.i3/_restart.json.old"); char *old_restart = resolve_tilde("~/.i3/_restart.json.old");
unlink(old_restart); unlink(old_restart);
rename(globbed, old_restart); rename(globbed, old_restart);
free(globbed); free(globbed);

View File

@ -485,7 +485,7 @@ void store_restart_layout() {
unsigned int length; unsigned int length;
y(get_buf, &payload, &length); y(get_buf, &payload, &length);
char *globbed = glob_path("~/.i3/_restart.json"); char *globbed = resolve_tilde("~/.i3/_restart.json");
int fd = open(globbed, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); int fd = open(globbed, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
free(globbed); free(globbed);
if (fd == -1) { if (fd == -1) {