bday

annotate bday.c @ 15:032af48d590b

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