(no commit message)
[ptask.git] / src / tw.c
1 /*
2  * Copyright (C) 2010-2012 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24
25 char *task_exec(char *opts)
26 {
27         FILE *f;
28         int ret;
29         size_t s;
30         char *str, *tmp, *cmd, buf[1024];
31
32         cmd = malloc(strlen("task rc.json.array=on ") + strlen(opts) + 1);
33         strcpy(cmd, "task rc.json.array=on ");
34         strcat(cmd, opts);
35
36         printf("execute: %s\n", cmd);
37
38         f = popen(cmd, "r");
39
40         if (!f) {
41                 perror("popen");
42                 str = NULL;
43                 goto exit_free;
44         }
45
46         str = strdup("");
47         while ((s = fread(buf, 1, 1024, f))) {
48                 tmp = malloc(strlen(str) + s + (size_t)1);
49                 memcpy(tmp, str, strlen(str));
50                 memcpy(tmp + strlen(str), buf, s);
51                 tmp[strlen(str) + s] = '\0';
52                 free(str);
53                 str = tmp;
54         }
55
56         ret = pclose(f);
57
58         if (ret == -1) {
59                 printf("pclose fails\n");
60                 perror("pclose");
61         }
62
63  exit_free:
64         free(cmd);
65
66         return str;
67 }
68
69 #include <json/json.h>
70
71 #include "tw.h"
72
73 static struct json_object *task_exec_json(char *opts)
74 {
75         struct json_object *o;
76         char *str;
77
78         str = task_exec(opts);
79
80         if (str) {
81                 o = json_tokener_parse(str);
82                 free(str);
83                 return o;
84         }
85
86         return NULL;
87 }
88
89 struct task **get_all_tasks()
90 {
91         int i, n;
92         struct json_object *jtasks, *jtask, *json;
93         struct task **tasks;
94
95         jtasks = task_exec_json("export");
96
97         if (!jtasks)
98                 return NULL;
99
100         n = json_object_array_length(jtasks);
101
102         tasks = malloc((n + 1) * sizeof(struct task *));
103
104         for (i = 0; i < n; i++) {
105                 jtask = json_object_array_get_idx(jtasks, i);
106
107                 tasks[i] = malloc(sizeof(struct task));
108
109                 json = json_object_object_get(jtask, "id");
110                 tasks[i]->id = json_object_get_int(json);
111
112                 json = json_object_object_get(jtask, "description");
113                 tasks[i]->description = strdup(json_object_get_string(json));
114
115                 json = json_object_object_get(jtask, "status");
116                 tasks[i]->status = strdup(json_object_get_string(json));
117
118                 json = json_object_object_get(jtask, "project");
119                 if (json)
120                         tasks[i]->project
121                                 = strdup(json_object_get_string(json));
122                 else
123                         tasks[i]->project = NULL;
124
125                 json = json_object_object_get(jtask, "uuid");
126                 tasks[i]->uuid = strdup(json_object_get_string(json));
127
128                 tasks[i]->note = NULL;
129         }
130
131         tasks[n] = NULL;
132
133         json_object_put(jtasks);
134
135         return tasks;
136 }
137
138 char *escape(const char *txt)
139 {
140         char *result;
141         char *c;
142
143         result = malloc(2*strlen(txt)+1);
144         c = result;
145
146         while (*txt) {
147                 switch (*txt) {
148                 case '"':
149                         *c = '\\'; c++;
150                         *c = '"';
151                         break;
152                 case '$':
153                         *c = '\\'; c++;
154                         *c = '$';
155                         break;
156                 case '&':
157                         *c = '\\'; c++;
158                         *c = '&';
159                         break;
160                 default:
161                         *c = *txt;
162                 }
163                 c++;
164                 txt++;
165         }
166
167         *c = '\0';
168
169         return result;
170 }
171