From bccd37ef968dc07c2e3435123deba3e541662e0c Mon Sep 17 00:00:00 2001 From: Frederic Culot Date: Sun, 26 Jul 2009 20:26:14 +0000 Subject: --status flag added --- ChangeLog | 4 ++++ src/args.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/io.c | 27 ++++++++++++++++++++++++++- src/io.h | 3 ++- src/utils.c | 15 +++++++-------- 5 files changed, 92 insertions(+), 12 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0b59f65..6d36b03 100755 --- a/ChangeLog +++ b/ChangeLog @@ -4,6 +4,10 @@ again Chris for reporting it) * src/io.c (io_unset_lock): function removed + * src/io.c (io_get_pid): new function + + * src/args.c (status_arg): new function + * src/args.c: --status flag added 2009-07-23 Frederic Culot diff --git a/src/args.c b/src/args.c index d174b27..b8ff28c 100755 --- a/src/args.c +++ b/src/args.c @@ -1,4 +1,4 @@ -/* $calcurse: args.c,v 1.57 2009/07/26 12:30:23 culot Exp $ */ +/* $calcurse: args.c,v 1.58 2009/07/26 20:26:14 culot Exp $ */ /* * Calcurse - text-based organizer @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -165,6 +166,39 @@ help_arg () fputs (htext, stdout); } +/* + * Used to display the status of running instances of calcurse. + * The displayed message will look like one of the following ones: + * + * calcurse is running (pid #) + * calcurse is running in background (pid #) + * calcurse is not running + * + * The status is obtained by looking at pid files in user data directory + * (.calcurse.pid and .daemon.pid). + */ +static void +status_arg (void) +{ + int cpid, dpid; + + cpid = io_get_pid (path_cpid); + dpid = io_get_pid (path_dpid); + + EXIT_IF (cpid && dpid, + _("Error: both calcurse (pid: %d) and its daemon (pid: %d)\n" + "seem to be running at the same time!\n" + "Please check manually and restart calcurse.\n"), + cpid, dpid); + + if (cpid) + fprintf (stdout, _("calcurse is running (pid %d)\n"), cpid); + else if (dpid) + fprintf (stdout, _("calcurse is running in background (pid %d)\n"), dpid); + else + fprintf (stdout, _("calcurse is not running\n")); +} + /* * Display note contents if one is asociated with the currently displayed item * (to be used together with the '-a' or '-t' flag in non-interactive mode). @@ -660,12 +694,19 @@ parse_args (int argc, char **argv, conf_t *conf) int tflag = 0; /* -t: print todo list */ int vflag = 0; /* -v: print version number */ int xflag = 0; /* -x: export data */ - + int tnum = 0, xfmt = 0, non_interactive = 0, multiple_flag = 0, load_data = 0; char *ddate = "", *cfile = NULL, *range = NULL, *startday = NULL; char *datadir = NULL, *ifile = NULL; regex_t reg, *preg = NULL; + /* Long options only */ + int statusflag = 0; /* --status: get the status of running instances */ + enum + { + STATUS_OPT = CHAR_MAX + 1 + }; + static char *optstr = "hvnNax::t::d:c:r::s::S:D:i:"; struct option longopts[] = { @@ -680,6 +721,7 @@ parse_args (int argc, char **argv, conf_t *conf) {"range", optional_argument, NULL, 'r'}, {"startday", optional_argument, NULL, 's'}, {"search", required_argument, NULL, 'S'}, + {"status", no_argument, NULL, STATUS_OPT}, {"todo", optional_argument, NULL, 't'}, {"version", no_argument, NULL, 'v'}, {"export", optional_argument, NULL, 'x'}, @@ -690,6 +732,9 @@ parse_args (int argc, char **argv, conf_t *conf) { switch (ch) { + case STATUS_OPT: + statusflag = 1; + break; case 'a': aflag = 1; multiple_flag++; @@ -843,6 +888,12 @@ parse_args (int argc, char **argv, conf_t *conf) version_arg (); non_interactive = 1; } + else if (statusflag) + { + io_init (cfile, datadir); + status_arg (); + non_interactive = 1; + } else if (multiple_flag) { if (load_data) diff --git a/src/io.c b/src/io.c index 3cd765d..ed4266f 100755 --- a/src/io.c +++ b/src/io.c @@ -1,4 +1,4 @@ -/* $calcurse: io.c,v 1.73 2009/07/26 12:47:15 culot Exp $ */ +/* $calcurse: io.c,v 1.74 2009/07/26 20:26:15 culot Exp $ */ /* * Calcurse - text-based organizer @@ -2976,3 +2976,28 @@ io_dump_pid (char *file) return 1; } + +/* + * Return the pid number contained in a file previously created with + * io_dump_pid (). + * If no file was found, return 0. + */ +unsigned +io_get_pid (char *file) +{ + FILE *fp; + unsigned pid; + + if (!file) + return 0; + + if ((fp = fopen (file, "r")) == 0) + return 0; + + if (fscanf (fp, "%u", &pid) != 1) + return 0; + + (void)fclose (fp); + + return pid; +} diff --git a/src/io.h b/src/io.h index b910cd4..b68d11a 100755 --- a/src/io.h +++ b/src/io.h @@ -1,4 +1,4 @@ -/* $calcurse: io.h,v 1.24 2009/07/26 12:47:16 culot Exp $ */ +/* $calcurse: io.h,v 1.25 2009/07/26 20:26:15 culot Exp $ */ /* * Calcurse - text-based organizer @@ -88,5 +88,6 @@ void io_start_psave_thread (conf_t *); void io_stop_psave_thread (void); void io_set_lock (void); unsigned io_dump_pid (char *); +unsigned io_get_pid (char *); #endif /* !CALCURSE_IO_H */ diff --git a/src/utils.c b/src/utils.c index e825196..b4e7a93 100755 --- a/src/utils.c +++ b/src/utils.c @@ -1,4 +1,4 @@ -/* $calcurse: utils.c,v 1.79 2009/07/26 12:47:16 culot Exp $ */ +/* $calcurse: utils.c,v 1.80 2009/07/26 20:26:16 culot Exp $ */ /* * Calcurse - text-based organizer @@ -64,7 +64,7 @@ void exit_calcurse (int status) { - int remove_lock; + int was_interactive; if (ui_mode == UI_CURSES) { @@ -73,10 +73,10 @@ exit_calcurse (int status) refresh (); endwin (); ui_mode = UI_CMDLINE; - remove_lock = 1; + was_interactive = 1; } else - remove_lock = 0; + was_interactive = 0; calendar_stop_date_thread (); io_stop_psave_thread (); @@ -93,14 +93,13 @@ exit_calcurse (int status) notify_free_app (); keys_free (); mem_stats (); - if (remove_lock) + if (was_interactive) { if (unlink (path_cpid) != 0) EXIT (_("Could not remove calcurse lock file: %s\n"), - strerror (errno)); + strerror (errno)); + dmon_start (status); } - - dmon_start (status); exit (status); } -- cgit v1.2.3-54-g00ecf