Skip to content

Commit

Permalink
Merge branch 'xdg-compliance' of github.com:unrealapex/yash into xdg-…
Browse files Browse the repository at this point in the history
…compliance
  • Loading branch information
unrealapex committed Sep 27, 2024
2 parents 13d57a5 + b9070d5 commit db35ff4
Showing 1 changed file with 17 additions and 36 deletions.
53 changes: 17 additions & 36 deletions yash.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ static struct input_file_info_T *new_input_file_info(int fd, size_t bufsize)
__attribute__((malloc,warn_unused_result));
static void execute_profile(const wchar_t *profile);
static void execute_rcfile(const wchar_t *rcfile);
static bool execute_file_in_home(const wchar_t *path)
__attribute__((nonnull));
static bool execute_file_in_xdg_config_home(const wchar_t *path)
static bool execute_file_in(const wchar_t *dir_var_name, const wchar_t *path)
__attribute__((nonnull));
static bool execute_file(const wchar_t *path);
static bool execute_file_mbs(const char *path);
Expand Down Expand Up @@ -263,16 +261,18 @@ struct input_file_info_T *new_input_file_info(int fd, size_t bufsize)
*/
void execute_profile(const wchar_t *profile)
{
if (profile != NULL)
if (profile != NULL) {
execute_file(profile);
return;
}

if (execute_file_in_xdg_config_home(L"yash/profile"))
if (execute_file_in(L VAR_XDG_CONFIG_HOME, L"yash/profile"))
return;

if (execute_file_in_home(L".config/yash/profile"))
if (execute_file_in(L VAR_HOME, L".config/yash/profile"))
return;

execute_file_in_home(L".yash_profile");
execute_file_in(L VAR_HOME, L".yash_profile");

}

Expand Down Expand Up @@ -300,13 +300,13 @@ void execute_rcfile(const wchar_t *rcfile)
return;
}

if (execute_file_in_xdg_config_home(L"yash/rc"))
if (execute_file_in(L VAR_XDG_CONFIG_HOME, L"yash/rc"))
return;

if (execute_file_in_home(L".config/yash/rc"))
if (execute_file_in(L VAR_HOME, L".config/yash/rc"))
return;

if (execute_file_in_home(L".yashrc"))
if (execute_file_in(L VAR_HOME, L".yashrc"))
return;

char *path = which("initialization/default", get_path_array(PA_LOADPATH),
Expand All @@ -315,37 +315,18 @@ void execute_rcfile(const wchar_t *rcfile)
free(path);
}

/* Executes the specified file. The `path' must be relative to $HOME. */
bool execute_file_in_home(const wchar_t *path)
/* Executes the specified file in directory. The `path' must be relative to
* directory */
bool execute_file_in(const wchar_t *dir_var_name, 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. The `path' must be relative to $XDG_CONFIG_HOME. */
bool execute_file_in_xdg_config_home(const wchar_t *path)
{
const wchar_t *xdg_config_home = getvar(L VAR_XDG_CONFIG_HOME);
if (xdg_config_home == NULL || xdg_config_home[0] == L'\0')
const wchar_t *directory = getvar(dir_var_name);
if (directory == NULL || directory[0] == L'\0')
return false;

xwcsbuf_T fullpath;
wb_initwithmax(&fullpath, add(add(wcslen(xdg_config_home), wcslen(path)), 1));
wb_cat(&fullpath, xdg_config_home);
wb_initwithmax(&fullpath, add(add(wcslen(directory), wcslen(path)), 1));
wb_cat(&fullpath, directory);
if (fullpath.contents[fullpath.length - 1] != L'/')
wb_wccat(&fullpath, L'/');
wb_cat(&fullpath, path);
Expand Down

0 comments on commit db35ff4

Please sign in to comment.