From 4285e8859353c31b39fa9539a89e58dc3a020264 Mon Sep 17 00:00:00 2001
From: Lars Henriksen <LarsHenriksen@get2net.dk>
Date: Fri, 17 Aug 2018 21:49:39 +0200
Subject: 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>
---
 src/args.c     |  4 ++-
 src/calcurse.h | 36 ++++++++--------------
 src/io.c       | 95 ++++++++++++++++++++--------------------------------------
 src/vars.c     | 22 +++++++-------
 4 files changed, 60 insertions(+), 97 deletions(-)

(limited to 'src')

diff --git a/src/args.c b/src/args.c
index 9e22d8d..e166f5a 100644
--- a/src/args.c
+++ b/src/args.c
@@ -727,8 +727,10 @@ int parse_args(int argc, char **argv)
 		from = date_sec_change(to, 0, range);
 
 	io_init(cfile, datadir, confdir);
-	io_check_dir(path_dir);
+	io_check_dir(path_ddir);
 	io_check_dir(path_notes);
+	io_check_dir(path_cdir);
+	io_check_dir(path_hooks);
 
 	vars_init();
 	if (status) {
diff --git a/src/calcurse.h b/src/calcurse.h
index 616d954..8626393 100644
--- a/src/calcurse.h
+++ b/src/calcurse.h
@@ -95,16 +95,6 @@
 #define NOTES_DIR_NAME   "notes/"
 #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_PAGER      "less"
 #define DEFAULT_MERGETOOL  "vimdiff"
@@ -1219,19 +1209,19 @@ extern enum ui_mode ui_mode;
 extern int read_only;
 extern int quiet;
 extern int want_reload;
-extern const char *datefmt_str[DATE_FORMATS];
-extern int days[12];
-extern char path_dir[BUFSIZ];
-extern char path_conf_dir[BUFSIZ];
-extern char path_todo[BUFSIZ];
-extern char path_apts[BUFSIZ];
-extern char path_conf[BUFSIZ];
-extern char path_keys[BUFSIZ];
-extern char path_notes[BUFSIZ];
-extern char path_cpid[BUFSIZ];
-extern char path_dpid[BUFSIZ];
-extern char path_dmon_log[BUFSIZ];
-extern char path_hooks[BUFSIZ];
+extern const char *datefmt_str[];
+extern int days[];
+extern char *path_ddir;
+extern char *path_cdir;
+extern char *path_todo;
+extern char *path_apts;
+extern char *path_conf;
+extern char *path_keys;
+extern char *path_notes;
+extern char *path_cpid;
+extern char *path_dpid;
+extern char *path_dmon_log;
+extern char *path_hooks;
 extern struct conf conf;
 extern struct pad apad;
 extern struct nbar nbar;
diff --git a/src/io.c b/src/io.c
index 1adf382..0e7baea 100644
--- a/src/io.c
+++ b/src/io.c
@@ -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)
 {
-	const char *home;
-	const char *conf_home;
-
-	if (datadir != NULL) {
-		home = datadir;
-		if (confdir == NULL)
-			conf_home = home;
-		else
-			conf_home = confdir;
-
-		snprintf(path_dir, BUFSIZ, "%s", home);
-
-		snprintf(path_conf_dir, BUFSIZ, "%s",  conf_home);
-		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);
+	if (!datadir) {
+		if (!(datadir = getenv("HOME")))
+			datadir = ".";
+		asprintf(&path_ddir, "%s%s", datadir, "/" DIR_NAME);
+	} else
+		asprintf(&path_ddir, "%s%s", datadir, "/");
 
-		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 (!confdir)
+		asprintf(&path_cdir, "%s%s", path_ddir, "/");
+	else
+		asprintf(&path_cdir, "%s%s", confdir, "/");
 
-	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);
+	/* Data files */
+	if (cfile) {
+		path_apts = mem_strdup(cfile);
 		EXIT_IF(!io_file_exists(path_apts), _("%s does not exist"),
 			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)
@@ -1146,24 +1118,23 @@ int io_check_file(const char *file)
  * Checks if data files exist. If not, create them.
  * The following structure has to be created:
  *
- *	$HOME/.calcurse/
- *                 |
- *                 +--- notes/
- *                 |___ conf
- *                 |___ keys
- *                 |___ apts
- *                 |___ todo
+ *   <datadir>   <configdir> (default for both: $HOME/.calcurse/)
+ *        |             |
+ *        |__ apts      |___ conf
+ *        |__ todo      |___ keys
+ *        |__ notes/    |___ hooks/
  */
 int io_check_data_files(void)
 {
 	int missing = 0;
 
-	missing += io_check_dir(path_dir) ? 0 : 1;
-	missing += io_check_dir(path_conf_dir) ? 0 : 1;
+	missing += io_check_dir(path_ddir) ? 0 : 1;
 	missing += io_check_dir(path_notes) ? 0 : 1;
 	missing += io_check_file(path_todo) ? 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_dir(path_hooks) ? 0 : 1;
 
 	if (!io_check_file(path_keys)) {
 		missing++;
diff --git a/src/vars.c b/src/vars.c
index 0ebac8f..4e51b98 100644
--- a/src/vars.c
+++ b/src/vars.c
@@ -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
  * io_init()
  */
-char path_dir[] = "";
-char path_conf_dir[] = "";
-char path_todo[] = "";
-char path_apts[] = "";
-char path_conf[] = "";
-char path_notes[] = "";
-char path_keys[] = "";
-char path_cpid[] = "";
-char path_dpid[] = "";
-char path_dmon_log[] = "";
-char path_hooks[] = "";
+char *path_ddir = NULL;
+char *path_cdir = NULL;
+char *path_todo = NULL;
+char *path_apts = NULL;
+char *path_conf = NULL;
+char *path_notes = NULL;
+char *path_keys = NULL;
+char *path_cpid = NULL;
+char *path_dpid = NULL;
+char *path_dmon_log = NULL;
+char *path_hooks = NULL;
 
 /* Variable to store global configuration. */
 struct conf conf;
-- 
cgit v1.2.3-70-g09d2