Skip to content

Commit 2423880

Browse files
committed
Config file is specified by PSPG_CONF. When PSPG_CONF doesn't exist then
try to use ~/pspg_conf. When this file doesn't exist, then try to use XDG_CONFIG_HOME or ~/.config/pspg.conf
1 parent 1e4f11d commit 2423880

5 files changed

Lines changed: 90 additions & 45 deletions

File tree

src/config.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ do { \
104104
} while (0)
105105

106106
bool
107-
save_config(char *path, Options *opts)
107+
save_config(const char *path, Options *opts)
108108
{
109109
FILE *f;
110110
int result;
@@ -116,7 +116,11 @@ save_config(char *path, Options *opts)
116116

117117
if (chmod(path, 0644) != 0)
118118
{
119+
int _errno = errno;
120+
119121
fclose(f);
122+
errno = _errno;
123+
120124
return false;
121125
}
122126

@@ -253,19 +257,13 @@ assign_str(char *key, char **target, char *value, int type)
253257
* not significant.
254258
*/
255259
bool
256-
load_config(char *path, Options *opts)
260+
load_config(FILE *f, Options *opts)
257261
{
258-
FILE *f;
259262
char *line = NULL;
260263
ssize_t read;
261264
size_t len;
262265
bool is_valid = true;
263266

264-
errno = 0;
265-
f = fopen(path, "r");
266-
if (f == NULL)
267-
return false;
268-
269267
while ((read = getline(&line, &len, f)) != -1)
270268
{
271269
char key[100];
@@ -368,7 +366,5 @@ load_config(char *path, Options *opts)
368366

369367
free(line);
370368

371-
fclose(f);
372-
373369
return is_valid;
374370
}

src/config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ typedef struct
107107
bool direct_color;
108108
} Options;
109109

110-
extern bool save_config(char *path, Options *opts);
111-
extern bool load_config(char *path, Options *opts);
110+
extern bool save_config(const char *path, Options *opts);
111+
extern bool load_config(FILE *cf, Options *opts);
112112

113113
#endif

src/pspg.c

Lines changed: 78 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2647,10 +2647,7 @@ main(int argc, char *argv[])
26472647
bool size_is_valid = false;
26482648
int ioctl_result;
26492649

2650-
char pspg_conf_path[MAXPATHLEN];
2651-
char pspg_history_path[MAXPATHLEN];
2652-
const char *XDG_CONFIG_HOME;
2653-
const char *XDG_STATE_HOME;
2650+
FILE *cf = NULL;
26542651

26552652
#if NCURSES_MOUSE_VERSION > 1
26562653

@@ -2743,42 +2740,91 @@ main(int argc, char *argv[])
27432740
PSPG_CONF = getenv("PSPG_CONF");
27442741
if (PSPG_CONF)
27452742
{
2746-
snprintf(pspg_conf_path, MAXPATHLEN, "%s", PSPG_CONF);
2743+
tilde(state.pspg_conf_path, PSPG_CONF);
2744+
2745+
errno = 0;
2746+
cf = fopen(state.pspg_conf_path, "r");
2747+
if (cf == NULL)
2748+
fprintf(stderr, "cannot to open from config file \"%s\" specified by PSPG_CONF (%s), ignored\n",
2749+
PSPG_CONF,
2750+
strerror(errno));
27472751
}
27482752
else
27492753
{
2750-
XDG_CONFIG_HOME = getenv("XDG_CONFIG_HOME");
2751-
if (XDG_CONFIG_HOME)
2752-
{
2753-
snprintf(pspg_conf_path, MAXPATHLEN, "%s/pspg.conf", XDG_CONFIG_HOME);
2754-
}
2755-
else
2754+
tilde(state.pspg_conf_path, "~/.pspgconf");
2755+
2756+
errno = 0;
2757+
cf = fopen(state.pspg_conf_path, "r");
2758+
2759+
/*
2760+
* when config is not on old path, try to use XDG specification,
2761+
* but only when oldconfig doesn't exists.
2762+
*/
2763+
if (cf == NULL && errno == ENOENT)
27562764
{
2757-
snprintf(pspg_conf_path, MAXPATHLEN, "%s", "~/.pspgconf");
2765+
const char *XDG_CONFIG_HOME;
2766+
const char *untilded;
2767+
struct stat sb;
2768+
2769+
XDG_CONFIG_HOME = getenv("XDG_CONFIG_HOME");
2770+
if (!XDG_CONFIG_HOME)
2771+
XDG_CONFIG_HOME = "~/.config";
2772+
2773+
untilded = tilde(NULL, XDG_CONFIG_HOME);
2774+
2775+
/* untilded must be a directory, else use old config path */
2776+
if (stat(untilded, &sb) == 0 && S_ISDIR(sb.st_mode))
2777+
{
2778+
snprintf(state.pspg_conf_path, MAXPATHLEN, "%s/pspg.conf", untilded);
2779+
2780+
cf = fopen(state.pspg_conf_path, "r");
2781+
2782+
}
27582783
}
27592784
}
27602785

2761-
load_config(tilde(NULL, pspg_conf_path), &opts);
2762-
if (PSPG_CONF && errno)
2763-
fprintf(stderr, "cannot to read from config file \"%s\" specified by PSPG_CONF (%s), ignored\n",
2764-
PSPG_CONF,
2765-
strerror(errno));
2786+
if (cf)
2787+
{
2788+
/*
2789+
* in this moment, the log file is not initialized,
2790+
* use strerr instead.
2791+
*/
2792+
logfile = stderr;
2793+
2794+
if (!load_config(cf, &opts))
2795+
fprintf(stderr, "warning: found errors while reading of config file");
2796+
2797+
logfile = NULL;
2798+
2799+
fclose(cf);
2800+
cf = NULL;
2801+
}
27662802

27672803
PSPG_HISTORY = getenv("PSPG_HISTORY");
27682804
if (PSPG_HISTORY)
27692805
{
2770-
snprintf(pspg_history_path, MAXPATHLEN, "%s", PSPG_HISTORY);
2806+
tilde(state.pspg_hist_path, PSPG_HISTORY);
27712807
}
27722808
else
27732809
{
2774-
XDG_STATE_HOME = getenv("XDG_STATE_HOME");
2775-
if (XDG_STATE_HOME)
2776-
{
2777-
snprintf(pspg_history_path, MAXPATHLEN, "%s/pspg_history", XDG_STATE_HOME);
2778-
}
2779-
else
2810+
struct stat sb;
2811+
const char *untilded;
2812+
2813+
tilde(state.pspg_hist_path, "~/.pspg_history");
2814+
2815+
if (stat(state.pspg_hist_path, &sb) != 0 || !S_ISREG(sb.st_mode))
27802816
{
2781-
snprintf(pspg_history_path, MAXPATHLEN, "%s", "~/.pspg_history");
2817+
const char *XDG_STATE_HOME;
2818+
2819+
XDG_STATE_HOME = getenv("XDG_STATE_HOME");
2820+
if (!XDG_STATE_HOME)
2821+
XDG_STATE_HOME = "~/.local/state/";
2822+
2823+
/* the directory should to exists, or use old path */
2824+
untilded = tilde(NULL, XDG_STATE_HOME);
2825+
2826+
if (stat(untilded, &sb) == 0 && S_ISDIR(sb.st_mode))
2827+
snprintf(state.pspg_hist_path, MAXPATHLEN, "%s/pspg_history", untilded);
27822828
}
27832829
}
27842830

@@ -3389,7 +3435,7 @@ main(int argc, char *argv[])
33893435
last_nullstr[0] = '\0';
33903436

33913437
/* initialize readline if it is active */
3392-
pspg_init_readline(pspg_history_path);
3438+
pspg_init_readline(state.pspg_hist_path);
33933439

33943440
#ifdef COMPILE_MENU
33953441

@@ -4295,23 +4341,23 @@ main(int argc, char *argv[])
42954341
break;
42964342

42974343
case cmd_SaveSetup:
4298-
if (!save_config(tilde(NULL, pspg_conf_path), &opts))
4344+
if (!save_config(state.pspg_conf_path, &opts))
42994345
{
43004346
if (errno != 0)
43014347
{
4302-
char buffer1[1000];
4348+
char buffer1[1000];
43034349

43044350
snprintf(buffer1, 1000, "Cannot write to \"%.800s\" (%s)",
4305-
pspg_conf_path, strerror(errno));
4351+
state.pspg_conf_path, strerror(errno));
43064352

43074353
show_info_wait(buffer1, strerror(errno), true, true, false, true);
43084354
}
43094355
else
4310-
show_info_wait(" Cannot write to \"%s\"", pspg_conf_path,
4356+
show_info_wait(" Cannot write to \"%s\"", state.pspg_conf_path,
43114357
true, true, false, true);
43124358
}
43134359
else
4314-
show_info_wait(" Setup saved to \"%s\"", pspg_conf_path,
4360+
show_info_wait(" Setup saved to \"%s\"", state.pspg_conf_path,
43154361
true, true, true, false);
43164362
break;
43174363

@@ -7063,7 +7109,7 @@ main(int argc, char *argv[])
70637109

70647110
close_tty_stream();
70657111

7066-
pspg_save_history(pspg_history_path, &opts);
7112+
pspg_save_history(state.pspg_hist_path, &opts);
70677113

70687114
/*
70697115
* Try to release all allocated memory, although this has not

src/pspg.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,9 @@ typedef struct
293293
int menu_template;
294294

295295
char *last_query; /* last query in querystream mode */
296+
297+
char pspg_conf_path[MAXPATHLEN];
298+
char pspg_hist_path[MAXPATHLEN];
296299
} StateData;
297300

298301

src/readline.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ pspg_save_history(const char *histfile, Options *opts)
135135
if (opts->hist_size >= 0)
136136
stifle_history(opts->hist_size);
137137

138-
write_history(tilde(NULL, histfile));
138+
write_history(histfile);
139139

140140
#if RL_READLINE_VERSION >= 0x0603
141141

0 commit comments

Comments
 (0)