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>
This commit is contained in:
Lukas Fleischer 2016-01-07 18:23:09 +01:00
parent ddfe49d9fe
commit 767214e4f1
5 changed files with 75 additions and 0 deletions

View File

@ -20,6 +20,7 @@ calcurse_SOURCES = \
event.c \
getstring.c \
help.c \
hooks.c \
ical.c \
io.c \
keys.c \

View File

@ -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;

63
src/hooks.c Normal file
View File

@ -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;
}

View File

@ -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,

View File

@ -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;