bday

annotate bday.c @ 24:c621d710d12e

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