Change config directory location
Could you use the xdg base dir spec or add an env variable to specify the location of .ne directory?
The question (from August, 2015) was, "Could you use the xdg base dir spec or add an env variable to specify the location of .ne directory?"
That's both a good idea and possible. We also need to change some things around inside the .ne directory, and handling them both at the same time makes more sense than two separate disruptive changes. The problems come from trying to cleanly handle people switching between older and newer versions. These things are being thought about. Sorry for having taken so long to respond.
Any update on this?
Wow, has it really been 5 years already?
Nothing has changed, except it's an even better idea now than it was then. Maybe it's time to put more than thought into it.
I am proposing the following changes:
- If
~/.nealready exists, it will use that directory - Then it checks
~/.config/neand uses it if it exists - If
~/.config/nedoes not exist, it creates it - If
~/.config/neis a file, it defaults back to~/.ne - If
~/.neand~/.config/neare files it returns null
// In "prefs.c"
char *exists_prefs_dir(void) {
static char *prefs_dir;
/* If we have been already called, we already computed the name. */
if (prefs_dir) return prefs_dir;
/* In the UN*X case, we first get the home directory. Then
we allocate space for the directory name. */
char * home_dir;
if (!(home_dir = getenv("HOME"))) home_dir = ".";
char * xdg_dir;
if (!(xdg_dir = getenv("XDG_CONFIG_HOME"))) xdg_dir = ".config"; // I think this is right????
if (prefs_dir = malloc(strlen(home_dir) + strlen(PREFS_DIR) + 3)) {
strcat(strcat(strcpy(prefs_dir, home_dir), "/"), PREFS_DIR);
bool home_is_file; // 0 = doesn't exist, 1 = error: is a file
char *prefs_dir_tmp; // Just in case we can't use XDG_CONFIG_HOME and have to default to $HOME/.ne
struct stat s;
if (stat(prefs_dir, &s)) { // Directory/file does not exist.
home_is_file = false;
if (prefs_dir_tmp = malloc(strlen(prefs_dir) + 1)) {
strcpy(prefs_dir_tmp, prefs_dir);
}
else return NULL;
}
else if (S_ISDIR(s.st_mode)) { // Directory does exist.
return strcat(prefs_dir, "/");
}
else if (!S_ISDIR(s.st_mode)) { // It does exist but is a file.
home_is_file = true;
}
// Build the XDG_CONFIG_HOME path
if (prefs_dir = realloc(prefs_dir, strlen(home_dir) + strlen(xdg_dir) + strlen(PREFS_DIR_XDG) + 4)) {
strcat(strcat(strcpy(strcpy(strcpy(prefs_dir, home_dir), "/"), xdg_dir), "/"), PREFS_DIR_XDG);
// Test XDG_CONFIG_HOME path
if (stat(prefs_dir, &s)) {
// Directory/file does not exist.
if (mkdir(prefs_dir, 0700)) {
free(prefs_dir);
return prefs_dir = NULL;
}
}
else if (!S_ISDIR(s.st_mode)) {
// It does exist but is a file.
if (!home_is_file) {
// Use $HOME/.ne
if (prefs_dir = realloc(prefs_dir, strlen(prefs_dir_tmp) + 1)) {
strcpy(prefs_dir, prefs_dir_tmp);
if (mkdir(prefs_dir, 0700)) {
free(prefs_dir);
return prefs_dir = NULL;
}
} else {
free(prefs_dir);
return prefs_dir = NULL;
}
}
else {
// Both $HOME/.ne and XDG_CONFIG_HOME/ne are files
free(prefs_dir);
return prefs_dir = NULL;
}
}
}
else return NULL;
return strcat(prefs_dir, "/");
}
else return NULL;
}
NOTE: These changes have not been tested and they do not fix the init_history() function that has tilde_expand("~/.ne/.history") hard coded. Comments and docs will also need to be changed.
Mac much?
Hmm, I didn't even think of that.