summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLukas Fleischer <lfleischer@calcurse.org>2016-01-07 18:23:09 +0100
committerLukas Fleischer <lfleischer@calcurse.org>2016-01-07 19:09:46 +0100
commit767214e4f157ae4a74be0b557bd49e24d972970b (patch)
tree46ee50d0e2c729e2892db8c96abc61294980a5fe
parentddfe49d9feeba0d280e501f67cda64646e79d901 (diff)
downloadcalcurse-767214e4f157ae4a74be0b557bd49e24d972970b.zip
Add pre-save and post-save hooks
This adds support for hooks which are executed before/after saving calcurse data. Hooks can be placed under hooks/pre-save and hooks/post-save in the data directory and need to be executable. Potential use cases include: * Automatically commit any changes to the data files using a VCS. * Automatically sync with some sever component on data file changes. Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
-rw-r--r--src/Makefile.am1
-rw-r--r--src/calcurse.h6
-rw-r--r--src/hooks.c63
-rw-r--r--src/io.c4
-rw-r--r--src/vars.c1
5 files changed, 75 insertions, 0 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index edb83d8..823f7c9 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -20,6 +20,7 @@ calcurse_SOURCES = \
event.c \
getstring.c \
help.c \
+ hooks.c \
ical.c \
io.c \
keys.c \
diff --git a/src/calcurse.h b/src/calcurse.h
index efbdb81..1ab5363 100644
--- a/src/calcurse.h
+++ b/src/calcurse.h
@@ -93,6 +93,7 @@
#define DPID_PATH_NAME ".daemon.pid"
#define DLOG_PATH_NAME "daemon.log"
#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
@@ -102,6 +103,7 @@
#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"
@@ -779,6 +781,9 @@ int updatestring(WINDOW *, char **, int, int);
/* help.c */
int display_help(const char *);
+/* hooks.c */
+int run_hook(const char *);
+
/* ical.c */
void ical_import_data(FILE *, FILE *, unsigned *, unsigned *, unsigned *,
unsigned *, unsigned *);
@@ -1111,6 +1116,7 @@ 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 struct conf conf;
extern struct pad apad;
extern struct nbar nbar;
diff --git a/src/hooks.c b/src/hooks.c
new file mode 100644
index 0000000..5fffb70
--- /dev/null
+++ b/src/hooks.c
@@ -0,0 +1,63 @@
+/*
+ * Calcurse - text-based organizer
+ *
+ * Copyright (c) 2004-2016 calcurse Development Team <misc@calcurse.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above
+ * copyright notice, this list of conditions and the
+ * following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the
+ * following disclaimer in the documentation and/or other
+ * materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Send your feedback or comments to : misc@calcurse.org
+ * Calcurse home page : http://calcurse.org
+ *
+ */
+
+#include <stddef.h>
+
+#include "calcurse.h"
+
+int run_hook(const char *name)
+{
+ char *hook_path;
+ char const *arg[2];
+ int pid, ret = -127;
+
+ asprintf(&hook_path, "%s/%s", path_hooks, name);
+ arg[0] = hook_path;
+ arg[1] = NULL;
+
+ if (!io_file_exists(hook_path))
+ return 0;
+
+ wins_prepare_external();
+ if ((pid = shell_exec(NULL, NULL, *arg, arg))) {
+ ret = child_wait(NULL, NULL, pid);
+ if (ret)
+ press_any_key();
+ }
+ wins_unprepare_external();
+
+ return ret;
+}
diff --git a/src/io.c b/src/io.c
index f12bac9..7411a6b 100644
--- a/src/io.c
+++ b/src/io.c
@@ -239,6 +239,7 @@ void io_init(const char *cfile, const char *datadir)
snprintf(path_dpid, BUFSIZ, "%s/" DPID_PATH_NAME, home);
snprintf(path_dmon_log, BUFSIZ, "%s/" DLOG_PATH_NAME,
home);
+ snprintf(path_hooks, BUFSIZ, "%s/" HOOKS_DIR_NAME, home);
} else {
home = getenv("HOME");
if (home == NULL) {
@@ -252,6 +253,7 @@ void io_init(const char *cfile, const char *datadir)
snprintf(path_dpid, BUFSIZ, "%s/" DPID_PATH, home);
snprintf(path_dmon_log, BUFSIZ, "%s/" DLOG_PATH, home);
snprintf(path_notes, BUFSIZ, "%s/" NOTES_DIR, home);
+ snprintf(path_hooks, BUFSIZ, "%s/" HOOKS_DIR, home);
}
if (cfile == NULL) {
@@ -391,6 +393,7 @@ void io_save_cal(enum save_display display)
if (read_only)
return;
+ run_hook("pre-save");
pthread_mutex_lock(&io_save_mutex);
show_bar = 0;
@@ -428,6 +431,7 @@ void io_save_cal(enum save_display display)
}
pthread_mutex_unlock(&io_save_mutex);
+ run_hook("post-save");
}
static void io_load_error(const char *filename, unsigned line,
diff --git a/src/vars.c b/src/vars.c
index f6fd685..d674393 100644
--- a/src/vars.c
+++ b/src/vars.c
@@ -111,6 +111,7 @@ char path_keys[] = "";
char path_cpid[] = "";
char path_dpid[] = "";
char path_dmon_log[] = "";
+char path_hooks[] = "";
/* Variable to store global configuration. */
struct conf conf;