/
calendar.c
283 lines (267 loc) · 6.42 KB
/
calendar.c
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
#include <sys/time.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "util.h"
/* day pattern */
struct DPattern {
/*
* This structure express a day pattern. For convenience, let's
* express a DPattern entry as YYYY/MM/DD/m/w, where:
* - year is YYYY (1 to INT_MAX)
* - month is MM (1 to 12)
* - monthday is DD (1 to 31)
* - monthweek is m (-5 to 5)
* - weekday is w (1-Monday to 7-Sunday)
*
* For example, 2020/03/11/2/3 matches 11 March 2020, which was
* a Wednesday (3) on the second week of March. This date can
* also be matched by 2020/03/11/-4/3, because this date was on
* the fourth to last week of that month. A zero value matches
* anything. For example:
* - 0000/12/25/0/0 matches 25 December of every year.
* - 0000/05/00/2/7 matches the second Sunday of May.
* - 2020/03/11/2/3 matches 11 March 2020.
*/
struct DPattern *next; /* pointer to next day on linked list */
int year;
int month;
int monthday;
int monthweek;
int weekday;
};
/* event */
struct Event {
struct Event *next; /* pointer to next event on linked list */
struct DPattern *days; /* list of day patterns */
char *name; /* event name */
char *filename; /* file event came from */
};
/* collection of events */
struct Calendar {
struct Event *head, *tail; /* pointers to singly linked list of events */
};
/* show usage and exit */
static void
usage(void)
{
(void)fprintf(stderr, "usage: calendar [-l] [-T YYYY-MM-DD] [-n num] [file ...]\n");
exit(1);
}
/* check if c is separator */
static int
isseparator(int c)
{
return c == '-' || c == '.' || c == '/';
}
/* get patterns for event s; also return its name */
static int
parseline(void *p, char *line, char *filename)
{
struct Calendar *calendar = p;
struct Event *ev;
struct DPattern *patt, *oldpatt;
struct DPattern d;
struct tm tm;
int n;
char *t, *end;
patt = NULL;
for (;;) {
d = (struct DPattern){
.next = NULL,
.year = 0,
.month = 0,
.monthday = 0,
.monthweek = 0,
.weekday = 0,
};
while (isspace(*(unsigned char *)line))
line++;
n = strtol(line, &end, 10);
if (n > 0 && isseparator(*end)) {
/* got numeric year or month */
d.month = n;
line = end + 1;
n = strtol(line, &end, 10);
if (n > 0 && isseparator(*end)) {
/* got numeric month after year */
d.year = d.month;
d.month = n;
line = end + 1;
} else if ((t = strptime(line, "%b", &tm)) != NULL && isseparator(*t)){
/* got month name after year */
d.year = d.month;
d.month = tm.tm_mon + 1;
line = t + 1;
}
} else if ((t = strptime(line, "%b", &tm)) != NULL && isseparator(*t)) {
/* got month name */
d.month = tm.tm_mon + 1;
line = t + 1;
}
n = strtol(line, &end, 10);
if (n > 0 && *end != '\0') {
/* got month day */
d.monthday = n;
line = end;
}
if ((t = strptime(line, "%a", &tm)) != NULL) {
/* got week day */
d.weekday = tm.tm_wday + 1;
line = t;
}
if (d.monthday == 0 && d.weekday == 0)
break;
n = strtol(line, &end, 10);
if (n >= -5 && n <= 5 && *end != '\0') {
d.monthweek = n;
line = end;
}
oldpatt = patt;
patt = emalloc(sizeof(*patt));
*patt = d;
patt->next = oldpatt;
while (isspace(*(unsigned char *)line))
line++;
if (*line == ',') {
line++;
} else {
break;
}
}
if (patt == NULL)
return -1;
while (isspace(*(unsigned char *)line))
line++;
ev = emalloc(sizeof(*ev));
ev->next = NULL;
ev->days = patt;
ev->name = estrdup(line);
ev->filename = filename;
if (calendar->head == NULL)
calendar->head = ev;
if (calendar->tail != NULL)
calendar->tail->next = ev;
calendar->tail = ev;
return 0;
}
/* check if event occurs today */
static int
occurstoday(struct Date *today, struct DPattern *patts)
{
struct DPattern *d;
for (d = patts; d != NULL; d = d->next) {
if ((d->year == 0 || d->year == today->y) &&
(d->month == 0 || d->month == today->m) &&
(d->monthday == 0 || d->monthday == today->d) &&
(d->weekday == 0 || d->weekday == today->w + 1) &&
(d->monthweek == 0 ||
(d->monthweek < 0 && d->monthweek == today->nmw) ||
(d->monthweek == today->pmw))) {
return 1;
}
}
return 0;
}
/* print events for today and after days */
static void
printcalendar(struct Calendar *calendar, struct Date *today, int after, int lflag, int prefix)
{
struct tm tm;
struct Event *ev;
char buf1[128];
char buf2[128];
buf1[0] = buf2[0] = '\0';
while (after-- >= 0) {
tm.tm_year = today->y - 1900;
tm.tm_wday = today->w;
tm.tm_mday = today->d;
tm.tm_mon = today->m - 1;
if (lflag) {
strftime(buf1, sizeof(buf1), "%A", &tm);
strftime(buf2, sizeof(buf2), "%d %B %Y", &tm);
printf("%-10s %s\n", buf1, buf2);
} else {
strftime(buf1, sizeof(buf1), "%m-%d", &tm);
}
for (ev = calendar->head; ev != NULL; ev = ev->next) {
if (occurstoday(today, ev->days)) {
if (!lflag)
printf("%s", buf1);
printf("\t");
if (prefix)
printf("%s: ", ev->filename);
printf("%s\n", ev->name);
}
}
incrdate(today);
}
}
/* free events, their name and day patterns */
static void
freecalendar(struct Calendar *calendar)
{
struct Event *e;
struct DPattern *d;
while (calendar->head) {
e = calendar->head;
calendar->head = calendar->head->next;
while (e->days) {
d = e->days;
e->days = e->days->next;
free(d);
}
free(e->name);
free(e);
}
}
/* calendar: print upcoming events */
int
main(int argc, char *argv[])
{
static struct Calendar calendar = {
.head = NULL,
.tail = NULL,
};
struct Date today;
int after = 1; /* number of days after today */
int lflag = 0; /* whether to print in long format */
int exitval = 0;
int ch;
if (gettoday(&today) == -1)
err(1, NULL);
if (today.w == FRIDAY)
after = 3;
else if (today.w == SATURDAY)
after = 2;
while ((ch = getopt(argc, argv, "ln:T:")) != -1) {
switch (ch) {
case 'l':
lflag = 1;
break;
case 'n':
after = strtonum(optarg, 0, INT_MAX);
break;
case 'T':
if (strtodate(&today, optarg, NULL) == -1)
errx(1, "improper argument date: %s", optarg);
break;
default:
usage();
break;
}
}
argc -= optind;
argv += optind;
if (readinput(parseline, &calendar, argc, argv) == -1)
exitval = 1;
printcalendar(&calendar, &today, after, lflag, argc > 1);
freecalendar(&calendar);
return exitval;
}