added --debug
[ptask.git] / src / tw.c
index ac8f82d..2d95614 100644 (file)
--- a/src/tw.c
+++ b/src/tw.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010-2012 jeanfi@gmail.com
+ * Copyright (C) 2012-2013 jeanfi@gmail.com
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/stat.h>
 
+#include <json/json.h>
+
+#include <log.h>
 #include "note.h"
+#include "tw.h"
+
+static int has_taskrc()
+{
+       char *home, *path;
+       int ret;
+       struct stat st;
+
+       home = getenv("HOME");
+
+       if (!home) {
+               log_err("HOME environment variable not defined");
+               return 0;
+       }
+
+       path = malloc(strlen(home) + 1 + strlen(".taskrc") + 1);
+       sprintf(path, "%s/%s", home, ".taskrc");
+
+       ret = lstat(path, &st);
 
-char *task_exec(char *opts)
+       free(path);
+
+       return ret == 0;
+
+}
+
+static char *task_exec(char *opts)
 {
        FILE *f;
        int ret;
        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 ");
        strcat(cmd, opts);
 
-       printf("execute: %s\n", cmd);
+       log_debug("execute: %s", cmd);
 
        f = popen(cmd, "r");
 
@@ -56,10 +88,8 @@ char *task_exec(char *opts)
 
        ret = pclose(f);
 
-       if (ret == -1) {
-               printf("pclose fails\n");
-               perror("pclose");
-       }
+       if (ret == -1)
+               log_err("pclose fails");
 
  exit_free:
        free(cmd);
@@ -67,10 +97,6 @@ char *task_exec(char *opts)
        return str;
 }
 
-#include <json/json.h>
-
-#include "tw.h"
-
 static struct json_object *task_exec_json(char *opts)
 {
        struct json_object *o;
@@ -148,7 +174,7 @@ struct task **tw_get_all_tasks(const char *status)
        return tasks;
 }
 
-char *escape(const char *txt)
+static char *escape(const char *txt)
 {
        char *result;
        char *c;