diff options
author | leo-arch <leonardoabramovich2@gmail.com> | 2021-06-06 00:05:23 -0300 |
---|---|---|
committer | leo-arch <leonardoabramovich2@gmail.com> | 2021-06-06 00:05:23 -0300 |
commit | 19db0fe2af7c318debc8eb8cfc12a73511943875 (patch) | |
tree | cf31285804739be771d11fc4f856024404fa9fb0 | |
parent | 71a75e88c1eca1fe8f07d11b698d5a6e16c66c38 (diff) |
Allow specifying alternative config dir
-rw-r--r-- | src/config.c | 55 | ||||
-rw-r--r-- | src/helpers.h | 3 | ||||
-rw-r--r-- | src/init.c | 46 | ||||
-rw-r--r-- | src/main.c | 1 | ||||
-rw-r--r-- | src/misc.c | 1 |
5 files changed, 76 insertions, 30 deletions
diff --git a/src/config.c b/src/config.c index 1f9ab95a..f681f247 100644 --- a/src/config.c +++ b/src/config.c @@ -544,22 +544,29 @@ define_config_file_names(void) { size_t pnl_len = strlen(PNL); - /* If $XDG_CONFIG_HOME is set, use it for the config file. - * Else, fall back to $HOME/.config */ - char *xdg_config_home = getenv("XDG_CONFIG_HOME"); - - if (xdg_config_home) { - size_t xdg_config_home_len = strlen(xdg_config_home); - - CONFIG_DIR_GRAL = (char *)xnmalloc(xdg_config_home_len + pnl_len + 2, sizeof(char)); - sprintf(CONFIG_DIR_GRAL, "%s/%s", xdg_config_home, PNL); - - xdg_config_home = (char *)NULL; - } - - else { - CONFIG_DIR_GRAL = (char *)xnmalloc(user.home_len + pnl_len + 11, sizeof(char)); - sprintf(CONFIG_DIR_GRAL, "%s/.config/%s", user.home, PNL); + if (alt_config_dir) { + CONFIG_DIR_GRAL = (char *)xnmalloc(strlen(alt_config_dir) + pnl_len + + 2, sizeof(char)); + sprintf(CONFIG_DIR_GRAL, "%s/%s", alt_config_dir, PNL); + free(alt_config_dir); + } else { + /* If $XDG_CONFIG_HOME is set, use it for the config file. + * Else, fall back to $HOME/.config */ + char *xdg_config_home = getenv("XDG_CONFIG_HOME"); + + if (xdg_config_home) { + size_t xdg_config_home_len = strlen(xdg_config_home); + + CONFIG_DIR_GRAL = (char *)xnmalloc(xdg_config_home_len + pnl_len + + 2, sizeof(char)); + sprintf(CONFIG_DIR_GRAL, "%s/%s", xdg_config_home, PNL); + + xdg_config_home = (char *)NULL; + } else { + CONFIG_DIR_GRAL = (char *)xnmalloc(user.home_len + pnl_len + 11, + sizeof(char)); + sprintf(CONFIG_DIR_GRAL, "%s/.config/%s", user.home, PNL); + } } size_t config_gral_len = strlen(CONFIG_DIR_GRAL); @@ -569,9 +576,7 @@ define_config_file_names(void) if (alt_profile) { CONFIG_DIR = (char *)xnmalloc(config_gral_len + strlen(alt_profile) + 11, sizeof(char)); sprintf(CONFIG_DIR, "%s/profiles/%s", CONFIG_DIR_GRAL, alt_profile); - } - - else { + } else { CONFIG_DIR = (char *)xnmalloc(config_gral_len + 18, sizeof(char)); sprintf(CONFIG_DIR, "%s/profiles/default", CONFIG_DIR_GRAL); } @@ -580,9 +585,7 @@ define_config_file_names(void) KBINDS_FILE = savestring(alt_kbinds_file, strlen(alt_kbinds_file)); free(alt_kbinds_file); alt_kbinds_file = (char *)NULL; - } - - else { + } else { /* Keybindings per user, not per profile */ KBINDS_FILE = (char *)xnmalloc(config_gral_len + 13, sizeof(char)); sprintf(KBINDS_FILE, "%s/keybindings", CONFIG_DIR_GRAL); @@ -613,9 +616,7 @@ define_config_file_names(void) if (!alt_bm_file) { BM_FILE = (char *)xnmalloc(config_len + 15, sizeof(char)); sprintf(BM_FILE, "%s/bookmarks.cfm", CONFIG_DIR); - } - - else { + } else { BM_FILE = savestring(alt_bm_file, strlen(alt_bm_file)); free(alt_bm_file); alt_bm_file = (char *)NULL; @@ -630,9 +631,7 @@ define_config_file_names(void) if (!alt_config_file) { CONFIG_FILE = (char *)xnmalloc(config_len + pnl_len + 4, sizeof(char)); sprintf(CONFIG_FILE, "%s/%src", CONFIG_DIR, PNL); - } - - else { + } else { CONFIG_FILE = savestring(alt_config_file, strlen(alt_config_file)); free(alt_config_file); alt_config_file = (char *)NULL; diff --git a/src/helpers.h b/src/helpers.h index 20c8c0ff..d10190d9 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -374,7 +374,7 @@ nm=01;32:bm=01;36:" /* Max length of the properties string in long view mode */ #define MAX_PROP_STR 55 -#define GRAL_USAGE "[-aAefFgGhiIlLmoOsSuUvxy] [-b FILE] [-c FILE] \ +#define GRAL_USAGE "[-aAefFgGhiIlLmoOsSuUvxy] [-b FILE] [-c FILE] [-D DIR] \ [-k FILE] [-P PROFILE] [-z METHOD] [PATH]" #define FALLBACK_SHELL "/bin/sh" @@ -704,6 +704,7 @@ extern char **sel_elements, *ACTIONS_FILE, + *alt_config_dir, *alt_bm_file, *alt_config_file, *alt_kbinds_file, @@ -39,6 +39,7 @@ #include "aux.h" #include "checks.h" #include "config.h" +#include "exec.h" #include "init.h" #include "mime.h" #include "misc.h" @@ -513,6 +514,7 @@ external_arguments(int argc, char **argv) {"show-hidden", no_argument, 0, 'A'}, {"bookmarks-file", no_argument, 0, 'b'}, {"config-file", no_argument, 0, 'c'}, + {"config-dir", required_argument, 0, 'D'}, {"no-eln", no_argument, 0, 'e'}, {"no-folders-first", no_argument, 0, 'f'}, {"folders-first", no_argument, 0, 'F'}, @@ -582,12 +584,13 @@ external_arguments(int argc, char **argv) /* Variables to store arguments to options (-c, -p and -P) */ char *path_value = (char *)NULL, *alt_profile_value = (char *)NULL, + *alt_dir_value = (char *)NULL, *config_value = (char *)NULL, *kbinds_value = (char *)NULL, *bm_value = (char *)NULL; while ((optc = getopt_long(argc, argv, - "+aAb:c:efFgGhiIk:lLmoOp:P:sSUuvw:xyz:", longopts, + "+aAb:c:D:efFgGhiIk:lLmoOp:P:sSUuvw:xyz:", longopts, (int *)0)) != EOF) { /* ':' and '::' in the short options string means 'required' and * 'optional argument' respectivelly. Thus, 'p' and 'P' require @@ -787,6 +790,10 @@ external_arguments(int argc, char **argv) config_value = optarg; break; + case 'D': + alt_dir_value = optarg; + break; + case 'e': xargs.noeln = no_eln = 1; break; @@ -990,6 +997,43 @@ external_arguments(int argc, char **argv) } } + if (alt_dir_value) { + char *dir_exp = (char *)NULL; + + if (*alt_dir_value == '~') { + dir_exp = tilde_expand(alt_dir_value); + alt_dir_value = dir_exp; + } + + int dir_ok = 1; + struct stat attr; + if (stat(alt_dir_value, &attr) == -1) { + char *tmp_cmd[] = {"mkdir", "-p", alt_dir_value, NULL}; + int ret = launch_execve(tmp_cmd, FOREGROUND, E_NOSTDERR); + if (ret != EXIT_SUCCESS) { + _err('e', PRINT_PROMPT, _("%s: %s: Cannot create directory (error %d)\n" + "Falling back to default configuration directory\n"), + PROGRAM_NAME, alt_dir_value, ret); + dir_ok = 0; + } + } + + if (access(alt_dir_value, W_OK) == -1) { + if (dir_ok) { + _err('e', PRINT_PROMPT, _("%s: %s: %s\n" + "Falling back to default configuration directory\n"), + PROGRAM_NAME, alt_dir_value, strerror(errno)); + } + } else { + alt_config_dir = savestring(alt_dir_value, strlen(alt_dir_value)); + _err(0, PRINT_PROMPT, _("%s: %s: Using alternative " + "configuration directory\n"), PROGRAM_NAME, alt_config_dir); + } + + if (dir_exp) + free(dir_exp); + } + if (kbinds_value) { char *kbinds_exp = (char *)NULL; @@ -200,6 +200,7 @@ char *ACTIONS_FILE = (char *)NULL, *alt_bm_file = (char *)NULL, + *alt_config_dir = (char *)NULL, *alt_config_file = (char *)NULL, *alt_kbinds_file = (char *)NULL, *alt_profile = (char *)NULL, @@ -1468,6 +1468,7 @@ help_function(void) \n -A, --show-hidden\t\t show hidden files\ \n -b, --bookmarks-file=FILE\t specify an alternative bookmarks file\ \n -c, --config-file=FILE\t\t specify an alternative configuration file\ +\n -D, --config-dir=DIR\t\t specify an alternative configuration directory\ \n -e, --no-eln\t\t\t do not print ELN (entry list number) at \ \n the left of each file name \ \n -f, --no-folders-first\t\t do not list folders first\ |