Rewrite of io_init()

The introduction of the "-C <confdir>" option is an opportunity to
review the initialization of data paths. It lead to a rewrite.

Two "root" directories are used (data and configuration files); by
default they are identical. The statically allocated path buffers are
turned into dynamically allocated buffers.

Missing files/directories now include hooks.

Signed-off-by: Lars Henriksen <LarsHenriksen@get2net.dk>
Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
This commit is contained in:
Lars Henriksen 2018-08-17 21:49:39 +02:00 committed by Lukas Fleischer
parent 6f22e99ad5
commit 4285e88593
4 changed files with 60 additions and 97 deletions

View File

@ -727,8 +727,10 @@ int parse_args(int argc, char **argv)
from = date_sec_change(to, 0, range); from = date_sec_change(to, 0, range);
io_init(cfile, datadir, confdir); io_init(cfile, datadir, confdir);
io_check_dir(path_dir); io_check_dir(path_ddir);
io_check_dir(path_notes); io_check_dir(path_notes);
io_check_dir(path_cdir);
io_check_dir(path_hooks);
vars_init(); vars_init();
if (status) { if (status) {

View File

@ -95,16 +95,6 @@
#define NOTES_DIR_NAME "notes/" #define NOTES_DIR_NAME "notes/"
#define HOOKS_DIR_NAME "hooks/" #define HOOKS_DIR_NAME "hooks/"
#define TODO_PATH DIR_NAME TODO_PATH_NAME
#define APTS_PATH DIR_NAME APTS_PATH_NAME
#define CONF_PATH DIR_NAME CONF_PATH_NAME
#define KEYS_PATH DIR_NAME KEYS_PATH_NAME
#define CPID_PATH DIR_NAME CPID_PATH_NAME
#define DLOG_PATH DIR_NAME DLOG_PATH_NAME
#define DPID_PATH DIR_NAME DPID_PATH_NAME
#define NOTES_DIR DIR_NAME NOTES_DIR_NAME
#define HOOKS_DIR DIR_NAME HOOKS_DIR_NAME
#define DEFAULT_EDITOR "vi" #define DEFAULT_EDITOR "vi"
#define DEFAULT_PAGER "less" #define DEFAULT_PAGER "less"
#define DEFAULT_MERGETOOL "vimdiff" #define DEFAULT_MERGETOOL "vimdiff"
@ -1219,19 +1209,19 @@ extern enum ui_mode ui_mode;
extern int read_only; extern int read_only;
extern int quiet; extern int quiet;
extern int want_reload; extern int want_reload;
extern const char *datefmt_str[DATE_FORMATS]; extern const char *datefmt_str[];
extern int days[12]; extern int days[];
extern char path_dir[BUFSIZ]; extern char *path_ddir;
extern char path_conf_dir[BUFSIZ]; extern char *path_cdir;
extern char path_todo[BUFSIZ]; extern char *path_todo;
extern char path_apts[BUFSIZ]; extern char *path_apts;
extern char path_conf[BUFSIZ]; extern char *path_conf;
extern char path_keys[BUFSIZ]; extern char *path_keys;
extern char path_notes[BUFSIZ]; extern char *path_notes;
extern char path_cpid[BUFSIZ]; extern char *path_cpid;
extern char path_dpid[BUFSIZ]; extern char *path_dpid;
extern char path_dmon_log[BUFSIZ]; extern char *path_dmon_log;
extern char path_hooks[BUFSIZ]; extern char *path_hooks;
extern struct conf conf; extern struct conf conf;
extern struct pad apad; extern struct pad apad;
extern struct nbar nbar; extern struct nbar nbar;

View File

@ -143,64 +143,36 @@ unsigned io_fprintln(const char *fname, const char *fmt, ...)
*/ */
void io_init(const char *cfile, const char *datadir, const char *confdir) void io_init(const char *cfile, const char *datadir, const char *confdir)
{ {
const char *home; if (!datadir) {
const char *conf_home; if (!(datadir = getenv("HOME")))
datadir = ".";
asprintf(&path_ddir, "%s%s", datadir, "/" DIR_NAME);
} else
asprintf(&path_ddir, "%s%s", datadir, "/");
if (datadir != NULL) { if (!confdir)
home = datadir; asprintf(&path_cdir, "%s%s", path_ddir, "/");
if (confdir == NULL)
conf_home = home;
else else
conf_home = confdir; asprintf(&path_cdir, "%s%s", confdir, "/");
snprintf(path_dir, BUFSIZ, "%s", home); /* Data files */
if (cfile) {
snprintf(path_conf_dir, BUFSIZ, "%s", conf_home); path_apts = mem_strdup(cfile);
snprintf(path_conf, BUFSIZ, "%s/" CONF_PATH_NAME, conf_home);
snprintf(path_keys, BUFSIZ, "%s/" KEYS_PATH_NAME, conf_home);
snprintf(path_hooks, BUFSIZ, "%s/" HOOKS_DIR_NAME, conf_home);
snprintf(path_todo, BUFSIZ, "%s/" TODO_PATH_NAME, home);
snprintf(path_cpid, BUFSIZ, "%s/" CPID_PATH_NAME, home);
snprintf(path_dpid, BUFSIZ, "%s/" DPID_PATH_NAME, home);
snprintf(path_notes, BUFSIZ, "%s/" NOTES_DIR_NAME, home);
snprintf(path_dmon_log, BUFSIZ, "%s/" DLOG_PATH_NAME, home);
} else {
home = getenv("HOME");
if (home == NULL) {
home = ".";
}
if (confdir == NULL)
conf_home = home;
else
conf_home = confdir;
snprintf(path_dir, BUFSIZ, "%s/" DIR_NAME, home);
snprintf(path_conf_dir, BUFSIZ, "%s", conf_home);
snprintf(path_conf, BUFSIZ, "%s/" CONF_PATH, conf_home);
snprintf(path_keys, BUFSIZ, "%s/" KEYS_PATH, conf_home);
snprintf(path_hooks, BUFSIZ, "%s/" HOOKS_DIR, conf_home);
snprintf(path_todo, BUFSIZ, "%s/" TODO_PATH, home);
snprintf(path_cpid, BUFSIZ, "%s/" CPID_PATH, home);
snprintf(path_dpid, BUFSIZ, "%s/" DPID_PATH, home);
snprintf(path_notes, BUFSIZ, "%s/" NOTES_DIR, home);
snprintf(path_dmon_log, BUFSIZ, "%s/" DLOG_PATH, home);
}
if (cfile == NULL) {
if (datadir != NULL) {
snprintf(path_apts, BUFSIZ, "%s/" APTS_PATH_NAME,
home);
} else {
snprintf(path_apts, BUFSIZ, "%s/" APTS_PATH, home);
}
} else {
snprintf(path_apts, BUFSIZ, "%s", cfile);
EXIT_IF(!io_file_exists(path_apts), _("%s does not exist"), EXIT_IF(!io_file_exists(path_apts), _("%s does not exist"),
path_apts); path_apts);
} else {
asprintf(&path_apts, "%s%s", path_ddir, APTS_PATH_NAME);
} }
asprintf(&path_todo, "%s%s", path_ddir, TODO_PATH_NAME);
asprintf(&path_cpid, "%s%s", path_ddir, CPID_PATH_NAME);
asprintf(&path_dpid, "%s%s", path_ddir, DPID_PATH_NAME);
asprintf(&path_notes, "%s%s", path_ddir, NOTES_DIR_NAME);
asprintf(&path_dmon_log, "%s%s", path_ddir, DLOG_PATH_NAME);
/* Configuration files */
asprintf(&path_conf, "%s%s", path_cdir, CONF_PATH_NAME);
asprintf(&path_keys, "%s%s", path_cdir, KEYS_PATH_NAME);
asprintf(&path_hooks, "%s%s", path_cdir, HOOKS_DIR_NAME);
} }
void io_extract_data(char *dst_data, const char *org, int len) void io_extract_data(char *dst_data, const char *org, int len)
@ -1146,24 +1118,23 @@ int io_check_file(const char *file)
* Checks if data files exist. If not, create them. * Checks if data files exist. If not, create them.
* The following structure has to be created: * The following structure has to be created:
* *
* $HOME/.calcurse/ * <datadir> <configdir> (default for both: $HOME/.calcurse/)
* | * | |
* +--- notes/ * |__ apts |___ conf
* |___ conf * |__ todo |___ keys
* |___ keys * |__ notes/ |___ hooks/
* |___ apts
* |___ todo
*/ */
int io_check_data_files(void) int io_check_data_files(void)
{ {
int missing = 0; int missing = 0;
missing += io_check_dir(path_dir) ? 0 : 1; missing += io_check_dir(path_ddir) ? 0 : 1;
missing += io_check_dir(path_conf_dir) ? 0 : 1;
missing += io_check_dir(path_notes) ? 0 : 1; missing += io_check_dir(path_notes) ? 0 : 1;
missing += io_check_file(path_todo) ? 0 : 1; missing += io_check_file(path_todo) ? 0 : 1;
missing += io_check_file(path_apts) ? 0 : 1; missing += io_check_file(path_apts) ? 0 : 1;
missing += io_check_dir(path_cdir) ? 0 : 1;
missing += io_check_file(path_conf) ? 0 : 1; missing += io_check_file(path_conf) ? 0 : 1;
missing += io_check_dir(path_hooks) ? 0 : 1;
if (!io_check_file(path_keys)) { if (!io_check_file(path_keys)) {
missing++; missing++;

View File

@ -79,17 +79,17 @@ int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
* variables to store data path names, which are initialized in * variables to store data path names, which are initialized in
* io_init() * io_init()
*/ */
char path_dir[] = ""; char *path_ddir = NULL;
char path_conf_dir[] = ""; char *path_cdir = NULL;
char path_todo[] = ""; char *path_todo = NULL;
char path_apts[] = ""; char *path_apts = NULL;
char path_conf[] = ""; char *path_conf = NULL;
char path_notes[] = ""; char *path_notes = NULL;
char path_keys[] = ""; char *path_keys = NULL;
char path_cpid[] = ""; char *path_cpid = NULL;
char path_dpid[] = ""; char *path_dpid = NULL;
char path_dmon_log[] = ""; char *path_dmon_log = NULL;
char path_hooks[] = ""; char *path_hooks = NULL;
/* Variable to store global configuration. */ /* Variable to store global configuration. */
struct conf conf; struct conf conf;