summaryrefslogtreecommitdiffstats
path: root/quart.c
blob: 4f2ad3bbf225eab8b43ea2221fe79971ce0b2335 (plain) (blame)
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/**
 * quart -- manage the day one quarter at a time
 * Copyright (C) 2019  Anastasis Grammenos
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <getopt.h>

#define BUFFER_LEN 8192

static char buf[BUFFER_LEN]  = "";
static char prev[BUFFER_LEN] = "";
static uint line = 0;

#define FERR(format, ...) do {                        \
    fprintf(stderr, "%s:%d error: ", ctx.file, line); \
    fprintf(stderr, format, __VA_ARGS__);             \
    fprintf(stderr, "\n"); } while(0);

typedef enum mods {
    NONE = 0,
    URGENT,
    MAYBE,
    DEPENDS,
} mods;

typedef struct Q {
    time_t start;
    char **entry;
    mods *m;
    unsigned int n;
} Q;

struct context {
    char *file;
    int later;
    int current;
    int next;
    int remaining;
};

static struct context ctx = {
    NULL,
    0,
    0,
    0,
    1
};

void trim(char *str)
{
    int i;
    int begin = 0;
    int end = strlen(str) - 1;

    while (isspace((unsigned char) str[begin]))
        begin++;

    while ((end >= begin) && isspace((unsigned char) str[end]))
        end--;

    // Shift all characters back to the start of the string array.
    for (i = begin; i <= end; i++)
        str[i - begin] = str[i];

    str[i - begin] = '\0'; // Null terminate string.
}

static void print_help(char *argv0)
{
    fprintf(stderr,
	    "quart - manage the day one quarter at a time\n");
    fprintf(stderr, "Copyright (C) 2019  Anastasis Grammenos\n");
    fprintf(stderr,
	    "This program is licenced under GPLv2. See source tree for details.\n\n");
    fprintf(stderr, "Usage:\n~~~~~~\n");
    fprintf(stderr, "%s [file] [...]\n\n", argv0);
    fprintf(stderr, "%8s %4s  %10s %55s\n", "Options", "", "Mnemonic",
	    "Description");
    fprintf(stderr, "%8s %4s  %10s %55s\n", "~~~~~~~", "", "~~~~~~~~",
	    "~~~~~~~~~~~");
    fprintf(stderr, "%8s %4s  %10s %55s\n", "-h", "", "help",
	    "Print this help message");
    fprintf(stderr, "%8s %4s  %10s %55s\n\n", "file", "[..]", "",
	    "The file to parse the daily schedule from");
    fprintf(stderr, "%8s %4s  %10s %55s\n", "-c", "", "current",
	    "Print what you should be doing now");
    fprintf(stderr, "%8s %4s  %10s %55s\n", "-n", "", "next",
	    "Print what you should be doing next");
    fprintf(stderr, "%8s %4s  %10s %55s\n", "-l", "[..]", "later",
	    "Print what you should be doing `l` quarters later");
    fprintf(stderr, "%8s %4s  %10s %55s\n", "", "", "",
	    "(Accepts numbers. -l 1 is the same as -n)");
    fprintf(stderr, "%8s %4s  %10s %55s\n", "-s", "", "summary",
	    "Print the day's summary");
    fprintf(stderr, "%8s %4s  %10s %55s\n", "-r", "", "remaining",
	    "Print what remains to be done (The default)");

}

static int parse_cli(const int argc, char * const argv[])
{
    if (argc < 2)
	return -1;
    char c;
    while ((c = getopt(argc, argv, "-schrnl:")) != -1) {
	switch (c) {
        case 'h':
            print_help(argv[0]);
            return -1;
	case '\1':
            ctx.file = optarg;
	    break;
        case 's':
            ctx.remaining = 0;
            ctx.current = 0;
            ctx.next = 0;
            ctx.later = 0;
	    break;
        case 'c':
            ctx.remaining = 0;
            ctx.current = 1;
            ctx.later = 0;
            ctx.next = 0;
	    break;
        case 'n':
            ctx.remaining = 0;
            ctx.next = 1;
            ctx.current = 0;
            ctx.later = 0;
	    break;
        case 'l':
            ctx.remaining = 0;
            ctx.next = 0;
            ctx.current = 0;
            ctx.later = atoi(optarg);
	    break;
        case 'r':
            ctx.remaining = 1;
            ctx.next = 0;
            ctx.current = 0;
            ctx.later = 0;
	    break;
	default:
	    return -1;
	}
    }
    return 0;
}

void initQ(Q *q)
{
    q->n = 0;
    q->start = 0;
    q->entry = (char **) malloc(sizeof(char *));
    q->m = (mods *) malloc(sizeof(mods));
}

void freeQ(Q *q)
{
    for (uint i = 0; i < q->n; i++)
        free(q->entry[i]);
    free(q->entry);
    free(q->m);
}

/**
 * str must be in the form of 00:00 */
int str_to_time(time_t *t, char *str)
{
    char *c = strtok(str, ":");
    if (!c) return -1;
    uint hour = atoi(c);
    if (hour > 23) return -1;
    c = strtok(NULL, ":");
    if (!c) return -1;
    uint minute = atoi(c);
    if (minute > 59) return -1;
    
    time_t tmp;
    time(&tmp);
    struct tm *tt = localtime(&tmp);

    tt->tm_hour = hour;
    tt->tm_min = minute;
    tt->tm_sec = 0;

    *t = mktime(tt);
    if (*t == -1) return -1;
    return 0;
}

int parse_header(Q *q)
{
    char *c = strtok(buf, " ");
    if (!c) return -1;
    if ((strcmp(c, "#+start:") != 0) &&
        (strcmp(c, "#+START:") != 0))
        return -1;
    c = strtok(NULL, " ");
    if (!c) return -1;
    if (str_to_time(&q->start, c)) {
        FERR("can't convert string \"%s\" to timestamp", c);
        return -1;
    }
    return 0;
}

int add(Q *q)
{
    const char mod = buf[0];
    char *ret = strchr(buf, mod);
    if (ret) {
        switch(mod) {
        case '!':
            q->m[q->n] = URGENT;
            break;
        case '?':
            q->m[q->n] = MAYBE;
            break;
        case '@':
            q->m[q->n] = DEPENDS;
            break;
        default:
            q->m[q->n] = NONE;
        }
        if (q->m[q->n] != NONE) {
            ret++;
            trim(ret);
        }
        q->entry[q->n] = strdup(ret);
        q->n++;
        return 0;
    }
    return -1;
}

int add_entry(Q *q)
{
    char *str = NULL;
    q->entry = (char **)realloc(q->entry, sizeof(char *) * (q->n + 1));
    q->m = (mods *)realloc(q->m, sizeof(mods) * (q->n + 1));
    if (buf[0] == '=') {
        if (strcmp(prev,"") == 0)
            FERR("\"%s\" there is no previous entry", buf);
        strcpy(buf, prev);
        return add(q);
    }
    else if (buf[0] == '-') {
        q->entry[q->n] = strdup("");
        q->m[q->n] = NONE;
        q->n++;
        return 0;
    }
    else {
        if (add(q))
            return -1;
    }

    if (str)
        strcpy(prev, str);
    else
        strcpy(prev, buf);
    return 0;
}

int parseQ(FILE *f, Q *q)
{
    initQ(q);
    int head = 0;
    int entries = 0;

    while (fgets(buf, BUFFER_LEN, f) != NULL) {
        line++;
        trim(buf);
        if (buf[0] == '#' && buf[1] == '+') {
            if (entries) {
                FERR("\"%s\" can't put header information inside entry list", buf);
                return -1;
            }
            if (parse_header(q)) {
                FERR("\"%s\" wrong header", buf);
                return -1;
            }
            head = 1;
        }
        else if (buf[0] == '#') {
            continue;
        }
        else if (buf[0] == '*') {
            entries = 1;
            buf[0] = ' ';
            trim(buf);
            if (!head) {
                FERR("Can't add \"%s\", missing #start header", buf)
                return -1;
            }
            if (strchr("123456789", buf[0]) != NULL) {
                int i = atoi(buf);
                int digits = (int)(floor(log(i)/log(10)) + 1);
                for (int j = 0; j < digits; j++) {
                    buf[j] = ' ';
                }
                trim(buf);
                for (int j = 1; j < i; j++) {
                    add_entry(q);
                }
            }
            if(add_entry(q)) {
                FERR("\"%s\" could not add entry", buf)
                return -1;
            }
        }
        else if (buf[0] == ';' || buf[0] == '\0') {
            continue;
        }
        else {
            FERR("\"%s\" is not a valid entry", buf)
        }
    }
    return head ? 0 : -1;
}

const char * get_mod(const mods m) {
    switch(m) {
    case NONE:
        return "---";
    case URGENT:
        return "!!!";
    case MAYBE:
        return "???";
    case DEPENDS:
        return "@@@";
    default:
        return "";
    }
}

#define P(before, timestr, entry, mod)                                  \
    printf("%s%5s  %3s  %s\n", before, timestr, mod, entry);

void printQ(const Q *q)
{
    time_t now;
    time(&now);

    char timestr[100];
    time_t t = q->start;
    for (uint i = 0; i < q->n; i++) {
        strftime(timestr, sizeof(timestr), "%R", localtime(&t));
        double d = difftime(t, now);
        if (ctx.current) {
            if (d > -900 && d <= 0)
                P("Now: ", timestr, q->entry[i], get_mod(q->m[i]));
        }
        else if (ctx.next) {
            if (d > 0 && d <= 900)
                P("Next: ", timestr, q->entry[i], get_mod(q->m[i]));
        }
        else if (ctx.later) {
            if (d > 900 * (ctx.later - 1) && d <= 900 * (ctx.later))
                P("Later: ", timestr, q->entry[i], get_mod(q->m[i]));
        }
        else if (ctx.remaining) {
            if (d > -900)
                P("", timestr, q->entry[i], get_mod(q->m[i]));
        }
        else {
            P("", timestr, q->entry[i], get_mod(q->m[i]));
        }
        t+=(15*60);
    }
}

int main(int argc, char *argv[])
{
    if (argc < 2) {
        print_help(argv[0]);
        return 0;
    }

    if (parse_cli(argc, argv))
        return -1;

    FILE *f = fopen(ctx.file, "r");
    if (!f) {
        printf("file %s does not exist\n", ctx.file);
        return -1;
    }
    Q q;

    if (parseQ(f, &q)) {
        goto end;
    }

    printQ(&q);
end:
    fclose(f);
    freeQ(&q);
}