Skip to content

Commit 2a0319a

Browse files
committed
Support $XDG_CONFIG_HOME for storing tup config
1 parent 1d2fd51 commit 2a0319a

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/tup/option.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
#define OPT_MAX 256
4949

50+
static char xdg_config_home_loc[PATH_MAX];
5051
static char home_loc[PATH_MAX];
5152

5253
static struct {
@@ -58,6 +59,7 @@ static struct {
5859
{ .file = TUP_OPTIONS_FILE },
5960
{ .file = home_loc },
6061
#ifndef _WIN32
62+
{ .file = xdg_config_home_loc },
6163
{ .file = "/etc/tup/options" },
6264
#endif
6365
};
@@ -67,6 +69,9 @@ static int parse_option_file(int x);
6769
static const char *cpu_number(void);
6870
static const char *stdout_isatty(void);
6971
static const char *get_console_width(void);
72+
#ifndef _WIN32
73+
static int init_xdg_config_home_loc(void);
74+
#endif
7075
static int init_home_loc(void);
7176

7277
static struct option {
@@ -283,6 +288,11 @@ int tup_option_init(int argc, char **argv)
283288
options[x].default_value = options[x].generator();
284289
}
285290

291+
#ifndef _WIN32
292+
if (init_xdg_config_home_loc() < 0)
293+
return -1;
294+
#endif
295+
286296
if(init_home_loc() < 0)
287297
return -1;
288298

@@ -535,6 +545,28 @@ static const char *stdout_isatty(void)
535545
return buf;
536546
}
537547

548+
#ifndef _WIN32
549+
static int init_xdg_config_home_loc(void) {
550+
const char *base_dir;
551+
const char *fmt_string;
552+
553+
if ((base_dir = getenv("XDG_CONFIG_HOME")) && *base_dir != '\0')
554+
fmt_string = "%s/tup/options";
555+
else if ((base_dir = getenv("HOME")))
556+
fmt_string = "%s/.config/tup/options";
557+
else
558+
return 0;
559+
560+
if (snprintf(xdg_config_home_loc, sizeof(xdg_config_home_loc), fmt_string, base_dir)
561+
>= (signed)sizeof(xdg_config_home_loc)) {
562+
fprintf(stderr, "tup internal error: user-level options file string is too small.\n");
563+
return -1;
564+
}
565+
566+
return 0;
567+
}
568+
#endif
569+
538570
static int init_home_loc(void)
539571
{
540572
#if defined(_WIN32)

test/t1007-tupoptions.sh

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,34 @@ check()
3030
. ./tup.sh
3131
check_no_windows HOME environment variable
3232

33-
# Override HOME so we can control ~/.tupoptions
33+
# Override HOME and XDG_CONFIG_HOME so we can control
34+
# the location of the options files.
35+
36+
# Test ${XDG_CONFIG_HOME:-$HOME/.config}/tup/options
3437
export HOME=`pwd`
38+
export XDG_CONFIG_HOME=
3539
check keep_going 0
3640

41+
mkdir -p "$HOME/.config/tup"
42+
cat > .config/tup/options << HERE
43+
[updater]
44+
num_jobs = 4
45+
keep_going = 1
46+
HERE
47+
check num_jobs 4
48+
check keep_going 1
49+
50+
export XDG_CONFIG_HOME="$HOME/.alt_config"
51+
check keep_going 0
52+
53+
mkdir -p "$XDG_CONFIG_HOME/tup"
54+
cat > "$XDG_CONFIG_HOME/tup/options" << HERE
55+
[updater]
56+
num_jobs = 5
57+
HERE
58+
check num_jobs 5
59+
60+
# Test ~/.tupoptions, which overrides the previous file.
3761
cat > .tupoptions << HERE
3862
[updater]
3963
num_jobs = 2

0 commit comments

Comments
 (0)