X-Git-Url: http://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Ftw.c;h=2b8269322d9be393e4155bff625d1a43f2963d12;hb=82cf3f9476fae353dbbe4330da49293fc41f500d;hp=1868d46c57727f331e58fc59bc286dfe636cea45;hpb=172950598f01d9a5b1ca7d39c740499ae130e1dc;p=ptask.git diff --git a/src/tw.c b/src/tw.c index 1868d46..2b82693 100644 --- a/src/tw.c +++ b/src/tw.c @@ -26,8 +26,10 @@ #include #include "note.h" +#include #include "tw.h" + static int has_taskrc() { char *home, *path; @@ -58,21 +60,19 @@ static char *task_exec(char *opts) size_t s; char *str, *tmp, *cmd, buf[1024]; - if (!has_taskrc()) - return NULL; - - cmd = malloc(strlen("task rc.json.array=on ") + strlen(opts) + 1); - strcpy(cmd, "task rc.json.array=on "); + cmd = malloc(strlen("task ") + strlen(opts) + 1); + strcpy(cmd, "task "); strcat(cmd, opts); log_debug("execute: %s", cmd); f = popen(cmd, "r"); + free(cmd); + if (!f) { perror("popen"); - str = NULL; - goto exit_free; + return NULL; } str = strdup(""); @@ -90,26 +90,72 @@ static char *task_exec(char *opts) if (ret == -1) log_err("pclose fails"); - exit_free: - free(cmd); - return str; } +static char *task_get_version() +{ + char *out; + + out = task_exec("--version"); + + trim(out); + + return out; +} + +static int task_check_version() +{ + char *ver; + + ver = task_get_version(); + + if (!ver) + return 0; + + log_debug("task version: %s", ver); + + if (!strcmp(ver, "2.2.0")) + return 1; + else + return 0; +} + +static char *tw_exec(char *opts) +{ + if (!has_taskrc()) + return NULL; + + if (!task_check_version()) { + log_err("ptask is not compatible with the installed version of" + " taskwarrior."); + return NULL; + } + + return task_exec(opts); +} + static struct json_object *task_exec_json(char *opts) { struct json_object *o; - char *str; + char *str, *cmd; - str = task_exec(opts); + cmd = malloc(strlen("rc.json.array=on ") + strlen(opts) + 1); + strcpy(cmd, "rc.json.array=on "); + strcat(cmd, opts); + + str = tw_exec(cmd); if (str) { o = json_tokener_parse(str); free(str); - return o; + } else { + o = NULL; } - return NULL; + free(cmd); + + return o; } struct task **tw_get_all_tasks(const char *status) @@ -222,7 +268,7 @@ void tw_modify_description(const char *uuid, const char *newdesc) + 1); sprintf(opts, " %s modify \"%s\"", uuid, str); - task_exec(opts); + tw_exec(opts); free(str); free(opts); @@ -243,7 +289,7 @@ void tw_modify_project(const char *uuid, const char *newproject) + 1); sprintf(opts, " %s modify project:\"%s\"", uuid, str); - task_exec(opts); + tw_exec(opts); free(str); free(opts); @@ -264,7 +310,7 @@ void tw_modify_priority(const char *uuid, const char *priority) + 1); sprintf(opts, " %s modify priority:\"%s\"", uuid, str); - task_exec(opts); + tw_exec(opts); free(str); free(opts); @@ -284,7 +330,7 @@ void tw_add(const char *newdesc) + 1); sprintf(opts, " add \"%s\"", str); - task_exec(opts); + tw_exec(opts); free(str); free(opts); @@ -300,7 +346,7 @@ void tw_done(const char *uuid) + 1); sprintf(opts, " %s done", uuid); - task_exec(opts); + tw_exec(opts); free(opts); }