(no commit message)
[ptask.git] / src / tw.c
index 366a0dc..9a551bc 100644 (file)
--- a/src/tw.c
+++ b/src/tw.c
 #include <stdlib.h>
 #include <string.h>
 
-#include <json/json.h>
-
-#include "tw.h"
 
 char *task_exec(char *opts)
 {
        FILE *f;
-       int ret, s;
+       int ret;
+       size_t s;
        char *str, *tmp, *cmd, buf[1024];
 
-       str = NULL;
-
        cmd = malloc(strlen("task rc.json.array=on ") + strlen(opts) + 1);
        strcpy(cmd, "task rc.json.array=on ");
        strcat(cmd, opts);
@@ -43,13 +39,13 @@ char *task_exec(char *opts)
 
        if (!f) {
                perror("popen");
+               str = NULL;
                goto exit_free;
        }
 
-       str = malloc(1);
-       str[0] = '\0';
+       str = strdup("");
        while ((s = fread(buf, 1, 1024, f))) {
-               tmp = malloc(strlen(str) + s + 1);
+               tmp = malloc(strlen(str) + s + (size_t)1);
                memcpy(tmp, str, strlen(str));
                memcpy(tmp + strlen(str), buf, s);
                tmp[strlen(str) + s] = '\0';
@@ -70,6 +66,10 @@ 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;
@@ -86,13 +86,18 @@ static struct json_object *task_exec_json(char *opts)
        return NULL;
 }
 
-struct task **get_all_tasks()
+struct task **tw_get_all_tasks(const char *status)
 {
        int i, n;
        struct json_object *jtasks, *jtask, *json;
        struct task **tasks;
+       char *opts;
 
-       jtasks = task_exec_json("export");
+       opts = malloc(strlen("export status:") + strlen(status) + 1);
+       sprintf(opts, "export status:%s", status);
+
+       jtasks = task_exec_json(opts);
+       free(opts);
 
        if (!jtasks)
                return NULL;
@@ -143,8 +148,8 @@ char *escape(const char *txt)
        result = malloc(2*strlen(txt)+1);
        c = result;
 
-       while(*txt) {
-               switch(*txt) {
+       while (*txt) {
+               switch (*txt) {
                case '"':
                        *c = '\\'; c++;
                        *c = '"';
@@ -169,3 +174,64 @@ char *escape(const char *txt)
        return result;
 }
 
+void tw_modify_description(const char *uuid, const char *newdesc)
+{
+       char *str;
+       char *opts;
+
+       str = escape(newdesc);
+
+       opts = malloc(1
+                     + strlen(uuid)
+                     + strlen(" modify :\"")
+                     + strlen(str)
+                     + strlen("\"")
+                     + 1);
+       sprintf(opts, " %s modify \"%s\"", uuid, str);
+
+       task_exec(opts);
+
+       free(str);
+       free(opts);
+}
+
+void tw_modify_project(const char *uuid, const char *newproject)
+{
+       char *str;
+       char *opts;
+
+       str = escape(newproject);
+
+       opts = malloc(1
+                     + strlen(uuid)
+                     + strlen(" modify project:\"")
+                     + strlen(str)
+                     + strlen("\"")
+                     + 1);
+       sprintf(opts, " %s modify project:\"%s\"", uuid, str);
+
+       task_exec(opts);
+
+       free(str);
+       free(opts);
+}
+
+void tw_add(const char *newdesc)
+{
+       char *str;
+       char *opts;
+
+       str = escape(newdesc);
+
+       opts = malloc(1
+                     + strlen(" add \"")
+                     + strlen(str)
+                     + strlen("\"")
+                     + 1);
+       sprintf(opts, " add \"%s\"", str);
+
+       task_exec(opts);
+
+       free(str);
+       free(opts);
+}