ne icon indicating copy to clipboard operation
ne copied to clipboard

Change config directory location

Open rnhmjoj opened this issue 10 years ago • 6 comments

Could you use the xdg base dir spec or add an env variable to specify the location of .ne directory?

rnhmjoj avatar Aug 07 '15 13:08 rnhmjoj

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.

utoddl avatar Feb 24 '17 22:02 utoddl

Any update on this?

Michael2MacDonald avatar Sep 22 '22 20:09 Michael2MacDonald

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.

utoddl avatar Sep 22 '22 21:09 utoddl

I am proposing the following changes:

  • If ~/.ne already exists, it will use that directory
  • Then it checks ~/.config/ne and uses it if it exists
  • If ~/.config/ne does not exist, it creates it
  • If ~/.config/ne is a file, it defaults back to ~/.ne
  • If ~/.ne and ~/.config/ne are 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.

Michael2MacDonald avatar Oct 26 '22 02:10 Michael2MacDonald

Mac much?

utoddl avatar Oct 26 '22 11:10 utoddl

Hmm, I didn't even think of that.

Michael2MacDonald avatar Oct 26 '22 16:10 Michael2MacDonald