366a0dc2f3377072a2959e0ce6f961b784689a3f
[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 #include <json/json.h>
25
26 #include "tw.h"
27
28 char *task_exec(char *opts)
29 {
30         FILE *f;
31         int ret, s;
32         char *str, *tmp, *cmd, buf[1024];
33
34         str = NULL;
35
36         cmd = malloc(strlen("task rc.json.array=on ") + strlen(opts) + 1);
37         strcpy(cmd, "task rc.json.array=on ");
38         strcat(cmd, opts);
39
40         printf("execute: %s\n", cmd);
41
42         f = popen(cmd, "r");
43
44         if (!f) {
45                 perror("popen");
46                 goto exit_free;
47         }
48
49         str = malloc(1);
50         str[0] = '\0';
51         while ((s = fread(buf, 1, 1024, f))) {
52                 tmp = malloc(strlen(str) + s + 1);
53                 memcpy(tmp, str, strlen(str));
54                 memcpy(tmp + strlen(str), buf, s);
55                 tmp[strlen(str) + s] = '\0';
56                 free(str);
57                 str = tmp;
58         }
59
60         ret = pclose(f);
61
62         if (ret == -1) {
63                 printf("pclose fails\n");
64                 perror("pclose");
65         }
66
67  exit_free:
68         free(cmd);
69
70         return str;
71 }
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