rev |
line source |
meillo@0
|
1 /*
|
meillo@8
|
2 bday
|
meillo@0
|
3
|
meillo@8
|
4 Birthday/Anniversary reminder
|
meillo@0
|
5
|
meillo@8
|
6 (c) 1996 AS Mortimer
|
meillo@8
|
7 (c) 2007 markus schnalke <meillo@marmaro.de>
|
meillo@0
|
8
|
meillo@8
|
9 This program is free software; you can redistribute it and/or
|
meillo@8
|
10 modify it under the terms of the GNU General Public License as
|
meillo@8
|
11 published by the Free Software Foundation; either version 2 of the
|
meillo@8
|
12 License, or (at your option) any later version.
|
meillo@0
|
13
|
meillo@8
|
14 This program is distributed in the hope that it will be useful,
|
meillo@8
|
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
meillo@8
|
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
meillo@0
|
17
|
meillo@8
|
18 You should have received a copy of the GNU General Public License
|
meillo@8
|
19 along with this program; if not, write to the Free Software
|
meillo@8
|
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
meillo@0
|
21
|
meillo@8
|
22 ===============================================================================
|
meillo@0
|
23
|
meillo@8
|
24 Input is read through standard input. For example: bday < ~/.birthdays
|
meillo@8
|
25 The input (file) has to have the following format:
|
meillo@7
|
26
|
meillo@8
|
27 text=date flags
|
meillo@7
|
28
|
meillo@8
|
29 where:
|
meillo@8
|
30 date is yyyy-mm-dd
|
meillo@8
|
31 flags is ONE or ZERO of
|
meillo@8
|
32 bd for a birthday (default)
|
meillo@8
|
33 ann for an anniversary
|
meillo@8
|
34 ev for an event
|
meillo@8
|
35 and zero or more of
|
meillo@8
|
36 w <n> to set the warn-in-advance time to n days (don't include the
|
meillo@8
|
37 brackets! :)
|
meillo@8
|
38 to <date>
|
meillo@8
|
39 for <days>
|
meillo@8
|
40 to specify the length of time taken by an event, for example a holiday.
|
meillo@7
|
41
|
meillo@8
|
42 Lines preceeded by # are treated as comments.
|
meillo@7
|
43
|
meillo@8
|
44 Note: If you deviate from this format, I cannot guarantee anything about
|
meillo@8
|
45 it's behaviour. In most cases, it will just quietly ignore the error,
|
meillo@8
|
46 which probably isn't ideal behaviour. Oh, well.
|
meillo@7
|
47
|
meillo@8
|
48 ===============================================================================
|
meillo@8
|
49 */
|
meillo@7
|
50
|
meillo@8
|
51
|
meillo@8
|
52 /* standard time to warn in advance, when no explicit w flag is given. */
|
meillo@8
|
53 #define DEF_WARN 14
|
meillo@0
|
54
|
meillo@7
|
55
|
meillo@7
|
56 #include <stdarg.h>
|
meillo@0
|
57 #include <stdio.h>
|
meillo@0
|
58 #include <stdlib.h>
|
meillo@7
|
59 #include <string.h>
|
meillo@7
|
60 #include <sys/types.h>
|
meillo@7
|
61 #include <time.h>
|
meillo@7
|
62 #include <unistd.h>
|
meillo@0
|
63
|
meillo@7
|
64
|
meillo@7
|
65
|
meillo@7
|
66 /* ========== Global constants and data types */
|
meillo@7
|
67
|
meillo@7
|
68
|
meillo@7
|
69 /* month lengths etc */
|
meillo@7
|
70 #define isleapyear(y) ((y)%4==0 && ((y)%100 != 0 || (y)%400 == 0))
|
meillo@7
|
71 const unsigned MLENDAT[];
|
meillo@7
|
72 #define mlen(m,y) (MLENDAT[(m)-1] != -1 ? MLENDAT[(m)-1] : (isleapyear((y)) ? 29 : 28))
|
meillo@7
|
73 #define before(a,b) ((a).month < (b).month || ((a).month == (b).month && (a).day < (b).day))
|
meillo@7
|
74 #define ydelta(a,b) ((int) (b).year - (a).year + before((a),(b)))
|
meillo@7
|
75
|
meillo@7
|
76 /* -------- modifier flags */
|
meillo@7
|
77 #define F_MTYPE 0x07
|
meillo@7
|
78 #define F_TBIRTHDAY 1
|
meillo@7
|
79 #define F_TANNIVERSARY 2
|
meillo@7
|
80 #define F_TEVENT 3
|
meillo@7
|
81
|
meillo@7
|
82 /* flags processed immediately on encountering */
|
meillo@7
|
83 #define F_MIMMEDIATE 0x24
|
meillo@7
|
84 #define F_WTIME_P 0x08
|
meillo@7
|
85 #define F_FORDAYS 0x16
|
meillo@7
|
86 #define F_TODATE 0x24
|
meillo@7
|
87
|
meillo@8
|
88 struct _ftable {char* txt; unsigned flag;};
|
meillo@7
|
89
|
meillo@7
|
90 const struct _ftable FTABLE[];
|
meillo@7
|
91
|
meillo@7
|
92 struct date {
|
meillo@7
|
93 unsigned day;
|
meillo@7
|
94 unsigned month;
|
meillo@7
|
95 unsigned year;
|
meillo@7
|
96 };
|
meillo@7
|
97
|
meillo@7
|
98 struct event {
|
meillo@8
|
99 char* text;
|
meillo@7
|
100 struct date date;
|
meillo@7
|
101 struct date enddate;
|
meillo@7
|
102 int warn;
|
meillo@7
|
103 };
|
meillo@7
|
104
|
meillo@7
|
105 typedef int (*prnfunc)(const char *);
|
meillo@7
|
106
|
meillo@7
|
107 /* ========== Global Variables */
|
meillo@7
|
108
|
meillo@7
|
109 struct event* readlist(void);
|
meillo@7
|
110 void gettoday(void);
|
meillo@8
|
111 unsigned delta(struct date*);
|
meillo@8
|
112 unsigned ddiff(struct date* D1, struct date* D2);
|
meillo@7
|
113 void liststrings(struct event* evl, prnfunc outf);
|
meillo@8
|
114 char* tdelta(struct date* d);
|
meillo@8
|
115 char* ttime(int yr, int mn, int wk, int dy);
|
meillo@8
|
116 int skptok(int j, char* ptr);
|
meillo@8
|
117 int evcmp(const void* e1, const void* e2);
|
meillo@7
|
118
|
meillo@7
|
119
|
meillo@7
|
120 struct date today;
|
meillo@7
|
121 int iDWarn = DEF_WARN;
|
meillo@7
|
122
|
meillo@7
|
123 const unsigned MLENDAT[]={31,-1,31,30,31,30,31,31,30,31,30,31};
|
meillo@7
|
124
|
meillo@7
|
125 const struct _ftable FTABLE[] = {
|
meillo@7
|
126 {"bd", F_TBIRTHDAY},
|
meillo@7
|
127 {"ann",F_TANNIVERSARY},
|
meillo@7
|
128 {"ev", F_TEVENT},
|
meillo@7
|
129 {"w", F_WTIME_P},
|
meillo@7
|
130 {"to", F_TODATE},
|
meillo@7
|
131 {"for", F_FORDAYS},
|
meillo@7
|
132 {NULL, 0}
|
meillo@7
|
133 };
|
meillo@7
|
134
|
meillo@7
|
135
|
meillo@7
|
136
|
meillo@7
|
137
|
meillo@7
|
138
|
meillo@7
|
139
|
meillo@7
|
140 /*
|
meillo@7
|
141 xmalloc/xrealloc functions
|
meillo@7
|
142 Note: the x* functions are lifted straight from the GNU libc info docs
|
meillo@7
|
143 $Id: xmalloc.c,v 1.2 1999/01/16 17:08:59 andy Exp $
|
meillo@7
|
144 */
|
meillo@7
|
145
|
meillo@7
|
146 void* xmalloc (size_t size) {
|
meillo@7
|
147 register void* value = malloc (size);
|
meillo@7
|
148 if (value == 0) {
|
meillo@7
|
149 fprintf(stderr, "virtual memory exhausted\n");
|
meillo@7
|
150 exit(1);
|
meillo@7
|
151 }
|
meillo@7
|
152 return value;
|
meillo@7
|
153 }
|
meillo@7
|
154
|
meillo@7
|
155 void* xrealloc (void* ptr, size_t size) {
|
meillo@7
|
156 register void* value = realloc (ptr, size);
|
meillo@7
|
157 if (value == 0) {
|
meillo@7
|
158 fprintf(stderr, "virtual memory exhausted\n");
|
meillo@7
|
159 exit(1);
|
meillo@7
|
160 }
|
meillo@7
|
161 return value;
|
meillo@7
|
162 }
|
meillo@7
|
163
|
meillo@8
|
164
|
meillo@7
|
165 /* ========== */
|
meillo@7
|
166
|
meillo@7
|
167
|
meillo@7
|
168 /* like strcat(), but lets the buffer automagically grow :-)
|
meillo@7
|
169 * (needs local variable "size" with the buffer size) */
|
meillo@7
|
170 #define append(where, what) do { \
|
meillo@7
|
171 if (strlen(what) > (size - strlen(where))) { \
|
meillo@7
|
172 xrealloc(where, size + 128 + strlen(what)); \
|
meillo@7
|
173 size += 128 + strlen(what); \
|
meillo@7
|
174 } \
|
meillo@7
|
175 strcat(where, what); \
|
meillo@7
|
176 } while(0)
|
meillo@7
|
177
|
meillo@7
|
178 /* ========== */
|
meillo@7
|
179
|
meillo@7
|
180 /* returns delta(d) in days, weeks, months, etc
|
meillo@7
|
181 * the returned buffer is malloc()ed, do not forget to free() it */
|
meillo@8
|
182 char* tdelta(struct date* d) {
|
meillo@7
|
183 int dy, wk, mn, yr;
|
meillo@8
|
184 char* tmp;
|
meillo@8
|
185 char* buf = xmalloc(128);
|
meillo@7
|
186 int size = 128;
|
meillo@7
|
187 *buf = 0;
|
meillo@7
|
188
|
meillo@7
|
189 switch (delta(d)) {
|
meillo@7
|
190 case 0:
|
meillo@7
|
191 append(buf, "today");
|
meillo@7
|
192 return buf;
|
meillo@7
|
193 case 1:
|
meillo@7
|
194 append(buf, "tomorrow");
|
meillo@7
|
195 return buf;
|
meillo@7
|
196 default:
|
meillo@7
|
197 /* like delta(), we ignore the year */
|
meillo@8
|
198 yr = -before(*d, today);
|
meillo@8
|
199 mn = d->month - today.month;
|
meillo@8
|
200 dy = d->day - today.day;
|
meillo@7
|
201
|
meillo@7
|
202 if (dy < 0) {
|
meillo@7
|
203 dy += mlen(today.month, today.year);
|
meillo@7
|
204 mn--;
|
meillo@7
|
205 }
|
meillo@7
|
206 if (mn < 0) {
|
meillo@7
|
207 mn += 12;
|
meillo@7
|
208 yr++;
|
meillo@7
|
209 }
|
meillo@7
|
210
|
meillo@7
|
211 wk = (dy / 7);
|
meillo@7
|
212 dy %= 7;
|
meillo@7
|
213
|
meillo@7
|
214 append(buf, "in ");
|
meillo@7
|
215 tmp = ttime(yr, mn, wk, dy);
|
meillo@7
|
216 append(buf, tmp);
|
meillo@7
|
217 free(tmp);
|
meillo@7
|
218
|
meillo@7
|
219 return buf;
|
meillo@7
|
220 }
|
meillo@7
|
221 }
|
meillo@7
|
222
|
meillo@7
|
223
|
meillo@7
|
224
|
meillo@7
|
225
|
meillo@7
|
226
|
meillo@7
|
227 /*
|
meillo@7
|
228 void donum(n,txt) {
|
meillo@7
|
229 do {
|
meillo@7
|
230 if (n > 0) {
|
meillo@7
|
231 snprintf(tmp, sizeof(tmp), "%d", n);
|
meillo@7
|
232 append(buf, tmp);
|
meillo@7
|
233 append(buf, " " txt);
|
meillo@7
|
234 if (n != 1)
|
meillo@7
|
235 append(buf, "s");
|
meillo@7
|
236 terms--;
|
meillo@7
|
237 if (orgterms > 1) {
|
meillo@7
|
238 if (terms == 1)
|
meillo@7
|
239 append(buf, " and ");
|
meillo@7
|
240 else if (terms > 1)
|
meillo@7
|
241 append(buf, ", ");
|
meillo@7
|
242 }
|
meillo@7
|
243 }
|
meillo@7
|
244 } while(0)
|
meillo@7
|
245 }
|
meillo@7
|
246 */
|
meillo@7
|
247
|
meillo@7
|
248
|
meillo@7
|
249 #define donum(n,txt) do { \
|
meillo@7
|
250 if (n > 0) { \
|
meillo@7
|
251 snprintf(tmp, sizeof(tmp), "%d", n); \
|
meillo@7
|
252 append(buf, tmp); \
|
meillo@7
|
253 append(buf, " " txt); \
|
meillo@7
|
254 if (n != 1) \
|
meillo@7
|
255 append(buf, "s"); \
|
meillo@7
|
256 terms--; \
|
meillo@7
|
257 if (orgterms > 1) { \
|
meillo@7
|
258 if (terms == 1) \
|
meillo@7
|
259 append(buf, " and "); \
|
meillo@7
|
260 else if (terms > 1) \
|
meillo@7
|
261 append(buf, ", "); \
|
meillo@7
|
262 } \
|
meillo@7
|
263 } \
|
meillo@7
|
264 } while(0)
|
meillo@7
|
265
|
meillo@7
|
266
|
meillo@7
|
267 /* returns allocated buffer, don't forget to free() */
|
meillo@7
|
268 char* ttime(int yr, int mn, int wk, int dy) {
|
meillo@7
|
269 char* buf = xmalloc(128);
|
meillo@7
|
270 int size = 128;
|
meillo@7
|
271 int terms, orgterms;
|
meillo@7
|
272 char tmp[128];
|
meillo@7
|
273
|
meillo@7
|
274 *buf = 0; /* Initialize buffer */
|
meillo@7
|
275 terms = orgterms = (yr!=0) + (mn!=0) + (wk!=0) + (dy!=0);
|
meillo@7
|
276
|
meillo@7
|
277 donum(yr, "year");
|
meillo@7
|
278 donum(mn, "month");
|
meillo@7
|
279 donum(wk, "week");
|
meillo@7
|
280 donum(dy, "day");
|
meillo@7
|
281
|
meillo@7
|
282 return buf;
|
meillo@7
|
283 }
|
meillo@7
|
284 #undef donum
|
meillo@7
|
285
|
meillo@7
|
286
|
meillo@7
|
287
|
meillo@7
|
288
|
meillo@7
|
289
|
meillo@7
|
290
|
meillo@7
|
291 /* lists the birthdays in their string format, one by one, and passes the string to a function. */
|
meillo@7
|
292 void liststrings(struct event* evl, prnfunc outf) {
|
meillo@7
|
293 int i,j;
|
meillo@7
|
294 char *buf, *tmp;
|
meillo@7
|
295 int size;
|
meillo@7
|
296
|
meillo@7
|
297 for (i = 0; evl[i].text != NULL; i++) {
|
meillo@7
|
298 buf = xmalloc(128);
|
meillo@7
|
299 *buf = '\0';
|
meillo@7
|
300 size = 128;
|
meillo@7
|
301
|
meillo@7
|
302 if (evl[i].warn == -1 && delta(&(evl[i].date))==0) {
|
meillo@7
|
303 append(buf, evl[i].text);
|
meillo@7
|
304 } else if (evl[i].enddate.day == 0) {
|
meillo@7
|
305 if (delta(&(evl[i].date)) <= evl[i].warn) {
|
meillo@7
|
306 append(buf, evl[i].text);
|
meillo@7
|
307 append(buf, " ");
|
meillo@7
|
308 tmp = tdelta(&(evl[i].date));
|
meillo@7
|
309 append(buf, tmp);
|
meillo@7
|
310 free(tmp);
|
meillo@7
|
311 }
|
meillo@7
|
312 } else {
|
meillo@7
|
313 if (delta(&(evl[i].date)) <= evl[i].warn) {
|
meillo@7
|
314 append(buf, evl[i].text);
|
meillo@7
|
315 append(buf, " for ");
|
meillo@7
|
316 /* +1 because, if the difference between two dates is one day, then the length of an event on those days is two days */
|
meillo@7
|
317 j = ddiff(&(evl[i].date),&(evl[i].enddate)) + 1;
|
meillo@7
|
318 tmp = ttime(0, 0, j/7, j%7);
|
meillo@7
|
319 append(buf, tmp);
|
meillo@7
|
320 free(tmp);
|
meillo@7
|
321 append(buf, " ");
|
meillo@7
|
322 tmp = tdelta(&(evl[i].date));
|
meillo@7
|
323 append(buf, tmp);
|
meillo@7
|
324 } else if (delta(&(evl[i].enddate)) <= evl[i].warn) {
|
meillo@7
|
325 append(buf, evl[i].text);
|
meillo@7
|
326 append(buf, " ");
|
meillo@7
|
327 j = delta(&(evl[i].enddate));
|
meillo@7
|
328 if (j) {
|
meillo@7
|
329 append(buf, "for ");
|
meillo@7
|
330 tmp = ttime(0, 0, j/7, j%7);
|
meillo@7
|
331 append(buf, tmp);
|
meillo@7
|
332 free(tmp);
|
meillo@7
|
333 append(buf, " longer");
|
meillo@7
|
334 } else {
|
meillo@7
|
335 append(buf, "finishes today");
|
meillo@7
|
336 }
|
meillo@7
|
337 }
|
meillo@7
|
338 }
|
meillo@7
|
339 if (*buf) {
|
meillo@7
|
340 append(buf, ".");
|
meillo@7
|
341 outf(buf);
|
meillo@7
|
342 }
|
meillo@7
|
343 free(buf);
|
meillo@7
|
344 }
|
meillo@7
|
345 }
|
meillo@7
|
346
|
meillo@7
|
347
|
meillo@7
|
348
|
meillo@7
|
349
|
meillo@7
|
350
|
meillo@7
|
351
|
meillo@7
|
352
|
meillo@7
|
353
|
meillo@7
|
354 /* sort the events by the time before the next time they come up, putting those
|
meillo@7
|
355 where the start has passed but we are still in the time-period first */
|
meillo@8
|
356 int evcmp(const void* p1, const void* p2) {
|
meillo@8
|
357 struct event* e1=(struct event*) p1;
|
meillo@8
|
358 struct event* e2=(struct event*) p2;
|
meillo@8
|
359 unsigned d1, d2;
|
meillo@7
|
360
|
meillo@7
|
361 /* if the delta for the enddate is less than that for the start date, then we
|
meillo@7
|
362 have passed the start date but not yet the end date, and so we should
|
meillo@7
|
363 display the enddate; otherwise, we should display the start date */
|
meillo@7
|
364
|
meillo@7
|
365 d1=delta(&(e1->date));
|
meillo@7
|
366 if (e1->enddate.day && delta(&(e1->enddate)) < d1)
|
meillo@7
|
367 d1=delta(&(e1->enddate));
|
meillo@7
|
368
|
meillo@7
|
369 d2=delta(&(e2->date));
|
meillo@7
|
370 if (e2->enddate.day && delta(&(e2->enddate)) < d2)
|
meillo@7
|
371 d2=delta(&(e2->enddate));
|
meillo@7
|
372
|
meillo@7
|
373 if (d1 < d2) return -1;
|
meillo@8
|
374 if (d1 > d2) return 1;
|
meillo@7
|
375
|
meillo@7
|
376 return strcmp(e1->text, e2->text);
|
meillo@7
|
377 }
|
meillo@7
|
378
|
meillo@7
|
379
|
meillo@7
|
380
|
meillo@7
|
381
|
meillo@7
|
382
|
meillo@7
|
383
|
meillo@7
|
384 /* difference in days between two dates */
|
meillo@7
|
385 /* it is assumed that D1 < D2, and so the result is always positive */
|
meillo@8
|
386 unsigned ddiff(struct date* D1, struct date* D2) {
|
meillo@8
|
387 struct date d1, d2;
|
meillo@8
|
388 int dd, m;
|
meillo@7
|
389
|
meillo@7
|
390 /* make working copies */
|
meillo@8
|
391 d1 = *D1;
|
meillo@8
|
392 d2 = *D2;
|
meillo@7
|
393
|
meillo@7
|
394 /* sort out zero years */
|
meillo@7
|
395 if (d1.year == 0 || d2.year==0) {
|
meillo@7
|
396 if (d1.year != d2.year) {
|
meillo@7
|
397 if (d1.year == 0) {
|
meillo@8
|
398 if (before(d1,d2))
|
meillo@8
|
399 d1.year = d2.year;
|
meillo@8
|
400 else
|
meillo@8
|
401 d1.year = d2.year - 1;
|
meillo@8
|
402 } else {
|
meillo@8
|
403 if (before(d1, d2))
|
meillo@8
|
404 d2.year = d1.year;
|
meillo@8
|
405 else
|
meillo@8
|
406 d2.year = d1.year + 1;
|
meillo@7
|
407 }
|
meillo@7
|
408 } else { /* both years zero */
|
meillo@8
|
409 if (before(d1, d2))
|
meillo@8
|
410 d1.year = d2.year = today.year;
|
meillo@7
|
411 else {
|
meillo@8
|
412 d1.year = today.year;
|
meillo@8
|
413 d2.year = d1.year + 1;
|
meillo@7
|
414 }
|
meillo@7
|
415 }
|
meillo@7
|
416 }
|
meillo@7
|
417
|
meillo@7
|
418 /* now we can actually do the comparison ... */
|
meillo@8
|
419 dd = 0;
|
meillo@7
|
420
|
meillo@7
|
421 /* to start with, we work in months */
|
meillo@7
|
422 for (m=d1.month; m < d2.month + (d2.year-d1.year)*12; m++)
|
meillo@7
|
423 dd += mlen(((m-1)%12)+1, d1.year + m/12);
|
meillo@7
|
424
|
meillo@7
|
425 /* and then we renormalise for the days within the months */
|
meillo@7
|
426 /* the first month was included in our calculations */
|
meillo@7
|
427 dd -= d1.day;
|
meillo@7
|
428 /* but the last one wasn't */
|
meillo@7
|
429 dd += d2.day;
|
meillo@7
|
430
|
meillo@7
|
431 return dd;
|
meillo@7
|
432 }
|
meillo@7
|
433
|
meillo@7
|
434
|
meillo@7
|
435
|
meillo@7
|
436
|
meillo@7
|
437
|
meillo@7
|
438
|
meillo@7
|
439
|
meillo@7
|
440
|
meillo@7
|
441 /* actually until the next anniversary of ... */
|
meillo@7
|
442 unsigned delta(struct date *date) {
|
meillo@7
|
443 struct date d;
|
meillo@7
|
444 unsigned dt, mn;
|
meillo@7
|
445
|
meillo@7
|
446 memcpy(&d, date, sizeof(struct date));
|
meillo@7
|
447
|
meillo@7
|
448 /* past the end of the year */
|
meillo@7
|
449 if (before(d, today)) {
|
meillo@7
|
450 d.year = 1;
|
meillo@7
|
451 } else {
|
meillo@7
|
452 d.year = 0;
|
meillo@7
|
453 }
|
meillo@7
|
454
|
meillo@8
|
455 for (mn = today.month, dt=0; mn < d.month + 12*d.year; mn++) {
|
meillo@7
|
456 dt += mlen(((mn-1)%12) + 1,today.year + mn/12);
|
meillo@8
|
457 }
|
meillo@7
|
458
|
meillo@7
|
459 dt -= today.day;
|
meillo@7
|
460 dt += d.day;
|
meillo@7
|
461
|
meillo@7
|
462 return dt;
|
meillo@7
|
463 }
|
meillo@7
|
464
|
meillo@7
|
465
|
meillo@7
|
466
|
meillo@7
|
467
|
meillo@7
|
468
|
meillo@7
|
469
|
meillo@7
|
470 void gettoday(void) {
|
meillo@7
|
471 struct tm *tm;
|
meillo@7
|
472 time_t t;
|
meillo@7
|
473
|
meillo@7
|
474 time(&t);
|
meillo@7
|
475 tm = localtime(&t);
|
meillo@7
|
476 today.day = tm->tm_mday;
|
meillo@7
|
477 today.month = tm->tm_mon + 1; /* 1-12 instead of 0-11 */
|
meillo@7
|
478 today.year = tm->tm_year + 1900;
|
meillo@7
|
479 }
|
meillo@7
|
480
|
meillo@7
|
481
|
meillo@7
|
482
|
meillo@7
|
483
|
meillo@7
|
484
|
meillo@7
|
485
|
meillo@7
|
486
|
meillo@7
|
487
|
meillo@7
|
488
|
meillo@7
|
489
|
meillo@7
|
490 struct event* readlist() {
|
meillo@7
|
491 int i, j, k, l, d;
|
meillo@7
|
492 struct event *evl;
|
meillo@7
|
493 char buf[1024], buf2[1024];
|
meillo@7
|
494 char *ptr;
|
meillo@7
|
495 unsigned flags;
|
meillo@7
|
496
|
meillo@7
|
497 /* initialise */
|
meillo@7
|
498 gettoday();
|
meillo@7
|
499
|
meillo@7
|
500 for (i = 0, evl = NULL; fgets(buf, sizeof(buf), stdin) != NULL; i++) {
|
meillo@7
|
501 evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1));
|
meillo@7
|
502
|
meillo@7
|
503 /* ignore comments and empty lines */
|
meillo@7
|
504 if (*buf == '#' || *buf == '\n') {
|
meillo@7
|
505 i--;
|
meillo@7
|
506 continue;
|
meillo@7
|
507 }
|
meillo@7
|
508
|
meillo@7
|
509 /* parse string in buf */
|
meillo@7
|
510 ptr = strrchr(buf, '='); /* allow '=' in text */
|
meillo@7
|
511
|
meillo@7
|
512 /* not a valid line, so ignore it! Cool, huh? */
|
meillo@7
|
513 /* Attention: only recognizes lines without '=' */
|
meillo@7
|
514 if (ptr == NULL) {
|
meillo@7
|
515 fprintf(stderr, "WARNING: Invalid line in input:\n%s", buf);
|
meillo@7
|
516 i--;
|
meillo@7
|
517 continue;
|
meillo@7
|
518 }
|
meillo@7
|
519
|
meillo@7
|
520 *(ptr++) = 0;
|
meillo@7
|
521
|
meillo@7
|
522 j = sscanf(ptr, "%u-%u-%u", &(evl[i].date.year), &(evl[i].date.month), &(evl[i].date.day));
|
meillo@7
|
523 /* ... unless it wasn't read, in which case set it to zero */
|
meillo@7
|
524 if (j==2) {
|
meillo@7
|
525 evl[i].date.year = 0;
|
meillo@7
|
526 }
|
meillo@7
|
527
|
meillo@7
|
528
|
meillo@7
|
529 /* parse flags */
|
meillo@7
|
530
|
meillo@7
|
531 evl[i].warn = iDWarn;
|
meillo@7
|
532 evl[i].enddate.day = 0;
|
meillo@7
|
533 evl[i].enddate.month = 0;
|
meillo@7
|
534 evl[i].enddate.year = 0;
|
meillo@7
|
535
|
meillo@7
|
536 flags = 0;
|
meillo@7
|
537 j = 0;
|
meillo@7
|
538
|
meillo@7
|
539 while(j = skptok(j, ptr), ptr[j] != 0) {
|
meillo@7
|
540 for (k = 0; FTABLE[k].txt != NULL && strncmp(FTABLE[k].txt, ptr + j, strlen(FTABLE[k].txt)); k++) {
|
meillo@7
|
541 }
|
meillo@7
|
542
|
meillo@7
|
543 switch (FTABLE[k].flag) {
|
meillo@7
|
544 case F_WTIME_P: /* w <n> -- sets warning time */
|
meillo@7
|
545 sscanf(ptr + j, "w %u", &(evl[i].warn));
|
meillo@7
|
546 break;
|
meillo@7
|
547 case F_FORDAYS: /* for <days> -- sets the duration of the event */
|
meillo@7
|
548 sscanf(ptr + j, "for %u", &d);
|
meillo@7
|
549 evl[i].enddate=evl[i].date;
|
meillo@7
|
550 for (l = 1; l < d; l++) {
|
meillo@7
|
551 evl[i].enddate.day++;
|
meillo@7
|
552 if (evl[i].enddate.day > mlen(evl[i].enddate.month, evl[i].enddate.year)) {
|
meillo@7
|
553 evl[i].enddate.month++;
|
meillo@7
|
554 evl[i].enddate.day = 1;
|
meillo@7
|
555 }
|
meillo@7
|
556 if (evl[i].enddate.month > 12) {
|
meillo@7
|
557 evl[i].enddate.year++;
|
meillo@7
|
558 evl[i].enddate.month = 1;
|
meillo@7
|
559 }
|
meillo@7
|
560 }
|
meillo@7
|
561 break;
|
meillo@7
|
562 case F_TODATE: /* to <date> -- sets the end date of the event */
|
meillo@7
|
563 l = sscanf(ptr + j, "to %u-%u-%u", &(evl[i].enddate.year), &(evl[i].enddate.month), &(evl[i].enddate.day));
|
meillo@7
|
564 if (l == 2) {
|
meillo@7
|
565 evl[i].enddate.year = 0;
|
meillo@7
|
566 }
|
meillo@7
|
567 break;
|
meillo@7
|
568 case 0:
|
meillo@7
|
569 break;
|
meillo@7
|
570 default:
|
meillo@7
|
571 flags |= FTABLE[k].flag;
|
meillo@7
|
572 break;
|
meillo@7
|
573 }
|
meillo@7
|
574 }
|
meillo@7
|
575
|
meillo@7
|
576
|
meillo@7
|
577 /* construct event text */
|
meillo@7
|
578
|
meillo@7
|
579 switch(flags & F_MTYPE) {
|
meillo@7
|
580 case F_TBIRTHDAY:
|
meillo@7
|
581 default: /* assume it's a birthday */
|
meillo@7
|
582 if (evl[i].date.year != 0) {
|
meillo@7
|
583 int tmp_age = ydelta(evl[i].date, today);
|
meillo@7
|
584 if (tmp_age != 1) {
|
meillo@7
|
585 sprintf(buf2, "%s is %d years old", buf, tmp_age);
|
meillo@7
|
586 } else {
|
meillo@7
|
587 sprintf(buf2, "%s is %d year old", buf, tmp_age);
|
meillo@7
|
588 }
|
meillo@7
|
589 } else {
|
meillo@7
|
590 sprintf(buf2, "%s has a birthday", buf);
|
meillo@7
|
591 }
|
meillo@7
|
592 break;
|
meillo@7
|
593 case F_TANNIVERSARY:
|
meillo@7
|
594 if (evl[i].date.year != 0) {
|
meillo@7
|
595 sprintf(buf2, "%s %d years ago", buf, ydelta(evl[i].date, today));
|
meillo@7
|
596 } else {
|
meillo@7
|
597 strcpy(buf2, buf);
|
meillo@7
|
598 }
|
meillo@7
|
599 break;
|
meillo@7
|
600 case F_TEVENT:
|
meillo@7
|
601 /* if a year was specified, and this warning isn't for it, ignore! */
|
meillo@7
|
602 if ((evl[i].date.year != 0 && ydelta(evl[i].date, today) != 0) && (evl[i].enddate.year == 0 || ydelta(evl[i].enddate, today) != 0)) {
|
meillo@7
|
603 i--;
|
meillo@7
|
604 continue;
|
meillo@7
|
605 }
|
meillo@7
|
606 strcpy(buf2, buf);
|
meillo@7
|
607 break;
|
meillo@7
|
608 }
|
meillo@7
|
609 evl[i].text = strdup(buf2);
|
meillo@7
|
610 }
|
meillo@7
|
611
|
meillo@7
|
612 evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1));
|
meillo@7
|
613 evl[i].date.day = 0;
|
meillo@7
|
614 evl[i].date.month = 0;
|
meillo@7
|
615 evl[i].date.year = 0;
|
meillo@7
|
616 evl[i].text = (char *) NULL;
|
meillo@7
|
617
|
meillo@7
|
618 fclose(stdin);
|
meillo@7
|
619
|
meillo@7
|
620 /* NB uses i from above */
|
meillo@7
|
621 qsort(evl, i, sizeof(struct event), evcmp);
|
meillo@7
|
622 return evl;
|
meillo@7
|
623 }
|
meillo@7
|
624
|
meillo@7
|
625
|
meillo@7
|
626
|
meillo@7
|
627
|
meillo@7
|
628
|
meillo@7
|
629 int skptok(int j, char *ptr) {
|
meillo@7
|
630 for (; ptr[j] != 0 && ptr[j] != ' ' && ptr[j] != '\t' ; j++);
|
meillo@7
|
631 for (; ptr[j] != 0 && (ptr[j] == ' ' || ptr[j] == '\t'); j++);
|
meillo@7
|
632
|
meillo@7
|
633 return j;
|
meillo@7
|
634 }
|
meillo@7
|
635
|
meillo@7
|
636
|
meillo@7
|
637
|
meillo@7
|
638
|
meillo@0
|
639
|
meillo@0
|
640
|
meillo@6
|
641 int main(int argc, char* argv[]) {
|
meillo@0
|
642
|
meillo@0
|
643 while (--argc > 0 && (*++argv)[0] == '-') {
|
meillo@5
|
644 if (strcmp(argv[0], "-W") == 0) {
|
meillo@6
|
645 /* TODO: catch if no value given */
|
meillo@0
|
646 iDWarn = atoi((++argv)[0]);
|
meillo@0
|
647 argc--;
|
meillo@0
|
648 } else {
|
meillo@3
|
649 fprintf(stderr, "unknown option %s\n", argv[0]);
|
meillo@0
|
650 exit(1);
|
meillo@0
|
651 }
|
meillo@0
|
652 }
|
meillo@0
|
653
|
meillo@6
|
654 liststrings(readlist(), puts);
|
meillo@0
|
655
|
meillo@0
|
656 return 0;
|
meillo@0
|
657 }
|