Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Searching XDG directories for initialization files #19

Open
1 task
magicant opened this issue Sep 10, 2023 · 2 comments · May be fixed by #63
Open
1 task

Searching XDG directories for initialization files #19

magicant opened this issue Sep 10, 2023 · 2 comments · May be fixed by #63
Labels
enhancement New feature or request

Comments

@magicant
Copy link
Owner

(Feature request migrated from https://osdn.net/projects/yash/ticket/46546)

It might be helpful if the shell honors the $XDG_CONFIG_HOME variable when looking for initialization scripts (profile and rcfile).


Unresolved questions

  • How can the user set the $XDG_CONFIG_HOME variable before the shell gets started? Typically, user-specified variables are set in the initialization scripts. It does not make sense for a script to set a variable that defines its own pathname.
@magicant magicant added the enhancement New feature or request label Sep 10, 2023
@unrealapex
Copy link
Contributor

unrealapex commented Sep 16, 2024

I would be interested in implementing this feature. I am not a very competent C programmer, however I think implementing this would be a good way to understand the workings of real C codebases.

Addressing the unresolved question you mentioned, here's what the XDG spec has to say:

$XDG_CONFIG_HOME is either not set or empty, a default equal to $HOME/.config should be used.

We can use $HOME/.config. Z Shell gets around this same issue by defining an environment variable called ZDOTDIR that points to the configuration directory. However, the profile file would need to be in $HOME.


Looking at the original feature request. OP suggested putting the files in $XDG_CONFIG_HOME. I think it would be neater if they were in $XDG_CONFIG_HOME/yash instead.

@magicant
Copy link
Owner Author

Thank you for participating. You can find the code here:

yash/yash.c

Lines 259 to 348 in 75d0ddf

/* Executes "$HOME/.yash_profile". */
void execute_profile(const wchar_t *profile)
{
if (profile != NULL)
execute_file(profile);
else
execute_file_in_home(L".yash_profile");
}
/* Executes the initialization file.
* `rcfile' is the filename to source.
* If `rcfile' is NULL, it defaults to "$HOME/.yashrc" and if the file cannot be
* read it falls back to "initialization/default" from $YASH_LOADPATH.
* In the POSIXly-correct mode, `rcfile' is ignored and "$ENV" is used. */
void execute_rcfile(const wchar_t *rcfile)
{
if (posixly_correct) {
const wchar_t *env = getvar(L VAR_ENV);
if (env == NULL)
return;
wchar_t *path = parse_and_expand_string(env, "$ENV", false);
execute_file(path);
free(path);
return;
}
if (rcfile != NULL) {
execute_file(rcfile);
return;
}
if (execute_file_in_home(L".yashrc"))
return;
char *path = which("initialization/default", get_path_array(PA_LOADPATH),
is_readable_regular);
execute_file_mbs(path);
free(path);
}
/* Executes the specified file. The `path' must be relative to $HOME. */
bool execute_file_in_home(const wchar_t *path)
{
const wchar_t *home = getvar(L VAR_HOME);
if (home == NULL || home[0] == L'\0')
return false;
xwcsbuf_T fullpath;
wb_initwithmax(&fullpath, add(add(wcslen(home), wcslen(path)), 1));
wb_cat(&fullpath, home);
if (fullpath.contents[fullpath.length - 1] != L'/')
wb_wccat(&fullpath, L'/');
wb_cat(&fullpath, path);
bool executed = execute_file(fullpath.contents);
wb_destroy(&fullpath);
return executed;
}
/* Executes the specified file if `path' is non-NULL.
* Returns true iff the file was found. */
bool execute_file(const wchar_t *path)
{
if (path == NULL)
return false;
char *mbspath = malloc_wcstombs(path);
bool executed = execute_file_mbs(mbspath);
free(mbspath);
return executed;
}
/* Executes the specified file if `path' is non-NULL.
* Returns true iff the file was found. */
bool execute_file_mbs(const char *path)
{
if (path == NULL)
return false;
int fd = move_to_shellfd(open(path, O_RDONLY));
if (fd < 0)
return false;
exec_input(fd, path, XIO_SUBST_ALIAS);
cancel_return();
remove_shellfd(fd);
xclose(fd);
return true;
}

@unrealapex unrealapex linked a pull request Sep 17, 2024 that will close this issue
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants
@magicant @unrealapex and others