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