~kris/hacks

stask

ref: 6000827889e5288301e63b20e2aeff245801679c stask/doc.c -rw-r--r-- 5.7 KiB
60008278 — Kris Yotam v0.2: add markdown document system for tasks 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* stask - simple task manager
 * See LICENSE file for copyright and license details. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

#include "stask.h"

static void
ensure_dir(const char *path)
{
	char tmp[1024];
	char *p;

	snprintf(tmp, sizeof(tmp), "%s", path);
	for (p = tmp + 1; *p; p++) {
		if (*p == '/') {
			*p = '\0';
			mkdir(tmp, 0755);
			*p = '/';
		}
	}
	mkdir(tmp, 0755);
}

void
doc_dir(TaskDB *tdb, const char *listname, char *buf, int len)
{
	snprintf(buf, len, "%s/docs/%s", tdb->docdir, listname);
}

void
doc_path(TaskDB *tdb, const char *listname, int task_id,
	char *buf, int len)
{
	snprintf(buf, len, "%s/docs/%s/%d.md",
		tdb->docdir, listname, task_id);
}

int
doc_create(TaskDB *tdb, const char *listname,
	int task_id, const char *title, const char *date)
{
	char path[1024], dir[1024];
	FILE *fp;

	doc_dir(tdb, listname, dir, sizeof(dir));
	ensure_dir(dir);

	doc_path(tdb, listname, task_id, path, sizeof(path));
	fp = fopen(path, "w");
	if (!fp) {
		fprintf(stderr, "stask: cannot create %s\n", path);
		return -1;
	}

	fprintf(fp, "---\n");
	fprintf(fp, "title: \"%s\"\n", title);
	fprintf(fp, "date: %s\n", date);
	fprintf(fp, "done: false\n");
	fprintf(fp, "---\n\n");

	fclose(fp);
	return 0;
}

int
doc_delete(TaskDB *tdb, const char *listname, int task_id)
{
	char path[1024];

	doc_path(tdb, listname, task_id, path, sizeof(path));
	return unlink(path);
}

int
doc_delete_all(TaskDB *tdb, const char *listname)
{
	Task *tasks;
	int i, n;
	List list;
	char dir[1024];

	if (list_by_name(tdb, listname, &list) < 0)
		return -1;

	if (task_all(tdb, list.id, &tasks, &n) == 0) {
		for (i = 0; i < n; i++)
			doc_delete(tdb, listname, tasks[i].id);
		free(tasks);
	}

	doc_dir(tdb, listname, dir, sizeof(dir));
	rmdir(dir); /* only succeeds if empty */
	return 0;
}

int
doc_edit(TaskDB *tdb, const char *listname, int task_id)
{
	char path[1024];
	const char *editor;
	char cmd[2048];
	int ret;

	doc_path(tdb, listname, task_id, path, sizeof(path));

	if (access(path, F_OK) != 0) {
		fprintf(stderr,
			"stask: no doc for task %d\n", task_id);
		return -1;
	}

	editor = getenv("EDITOR");
	if (!editor)
		editor = "vi";

	snprintf(cmd, sizeof(cmd), "%s '%s'", editor, path);
	ret = system(cmd);

	if (ret == 0)
		doc_sync_title(tdb, listname, task_id);

	return ret;
}

int
doc_show(TaskDB *tdb, const char *listname, int task_id)
{
	char path[1024];
	FILE *fp;
	char line[1024];

	doc_path(tdb, listname, task_id, path, sizeof(path));

	fp = fopen(path, "r");
	if (!fp) {
		fprintf(stderr,
			"stask: no doc for task %d\n", task_id);
		return -1;
	}

	while (fgets(line, sizeof(line), fp))
		fputs(line, stdout);

	fclose(fp);
	return 0;
}

/* parse title from yaml frontmatter */
static int
parse_title(const char *path, char *title, int len)
{
	FILE *fp;
	char line[1024];
	int in_fm = 0;
	char *p, *end;

	fp = fopen(path, "r");
	if (!fp)
		return -1;

	while (fgets(line, sizeof(line), fp)) {
		/* trim trailing newline */
		p = line + strlen(line) - 1;
		while (p >= line && (*p == '\n' || *p == '\r'))
			*p-- = '\0';

		if (strcmp(line, "---") == 0) {
			if (!in_fm) {
				in_fm = 1;
				continue;
			} else {
				break; /* end of frontmatter */
			}
		}

		if (!in_fm)
			continue;

		if (strncmp(line, "title:", 6) == 0) {
			p = line + 6;
			while (*p == ' ' || *p == '\t')
				p++;
			/* strip quotes */
			if (*p == '"' || *p == '\'') {
				p++;
				end = p + strlen(p) - 1;
				if (end > p && (*end == '"' || *end == '\''))
					*end = '\0';
			}
			snprintf(title, len, "%s", p);
			fclose(fp);
			return 0;
		}
	}

	fclose(fp);
	return -1;
}

/* update done field in frontmatter */
static int
update_fm_field(const char *path, const char *field,
	const char *value)
{
	FILE *fp;
	char *buf;
	long fsize;
	char line[1024], out[65536];
	int in_fm = 0, found = 0, olen = 0;
	char *p, *src;

	fp = fopen(path, "r");
	if (!fp)
		return -1;

	fseek(fp, 0, SEEK_END);
	fsize = ftell(fp);
	fseek(fp, 0, SEEK_SET);

	buf = malloc(fsize + 1);
	if (!buf) {
		fclose(fp);
		return -1;
	}
	fsize = fread(buf, 1, fsize, fp);
	buf[fsize] = '\0';
	fclose(fp);

	src = buf;
	out[0] = '\0';

	while (*src) {
		/* read a line */
		p = strchr(src, '\n');
		if (p) {
			int llen = (int)(p - src);
			if (llen >= (int)sizeof(line) - 1)
				llen = sizeof(line) - 2;
			memcpy(line, src, llen);
			line[llen] = '\0';
			src = p + 1;
		} else {
			snprintf(line, sizeof(line), "%s", src);
			src += strlen(src);
		}

		if (strcmp(line, "---") == 0) {
			if (!in_fm) {
				in_fm = 1;
			} else {
				in_fm = 0;
			}
			olen += snprintf(out + olen,
				sizeof(out) - olen, "%s\n", line);
			continue;
		}

		if (in_fm && strncmp(line, field,
				strlen(field)) == 0
			&& line[strlen(field)] == ':') {
			olen += snprintf(out + olen,
				sizeof(out) - olen,
				"%s: %s\n", field, value);
			found = 1;
		} else {
			olen += snprintf(out + olen,
				sizeof(out) - olen,
				"%s\n", line);
		}
	}

	free(buf);

	if (!found)
		return -1;

	fp = fopen(path, "w");
	if (!fp)
		return -1;
	fwrite(out, 1, olen, fp);
	fclose(fp);
	return 0;
}

int
doc_sync_title(TaskDB *tdb, const char *listname,
	int task_id)
{
	char path[1024], title[512];

	doc_path(tdb, listname, task_id, path, sizeof(path));

	if (access(path, F_OK) != 0)
		return -1;

	if (parse_title(path, title, sizeof(title)) < 0)
		return -1;

	if (title[0] == '\0')
		return -1;

	return task_update_text(tdb, task_id, title);
}

/* called from data.c when task_done/task_undo changes */
void
doc_sync_done(TaskDB *tdb, const char *listname,
	int task_id, int done)
{
	char path[1024];

	doc_path(tdb, listname, task_id, path, sizeof(path));

	if (access(path, F_OK) != 0)
		return;

	update_fm_field(path, "done",
		done ? "true" : "false");
}