meillo@0: /* meillo@15: bday -- Birthday/Anniversary reminder meillo@0: meillo@15: (c) 2007,2014 markus schnalke meillo@15: (c) 1994-1999 AS Mortimer meillo@0: meillo@15: This program is free software; you can redistribute it and/or meillo@15: modify it under the terms of the GNU General Public License as meillo@15: published by the Free Software Foundation; either version 2 of the meillo@15: License, or (at your option) any later version. You may also meillo@15: distribute it under the Artistic License, as comes with Perl. meillo@0: meillo@15: This program is distributed in the hope that it will be useful, meillo@15: but WITHOUT ANY WARRANTY; without even the implied warranty of meillo@15: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. meillo@0: meillo@15: You should have received a copy of the GNU General Public License meillo@15: along with this program; if not, write to the Free Software meillo@15: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA meillo@0: meillo@15: You should also have recieved a copy of the Artistic license with meillo@15: this program. meillo@0: meillo@15: ===================================================================== meillo@9: meillo@15: Input is read through standard input. For example: bday < ~/.birthdays meillo@15: The input (file) has to have the following format: meillo@0: meillo@16: date flags text meillo@7: meillo@15: where: meillo@16: date is YYYY-MM-DD meillo@18: flags is optionally meillo@16: #ann for an anniversary meillo@18: and optionally meillo@16: #w to set the warn-in-advance time to n days meillo@15: (don't include the brackets! :) meillo@16: separated by spaces. meillo@7: meillo@15: Lines preceeded by # are treated as comments. meillo@7: meillo@15: Note: If you deviate from this format, I cannot guarantee anything about meillo@15: it's behaviour. In most cases, it will just quietly ignore the meillo@15: error, which probably isn't ideal behaviour. Oh, well. meillo@7: meillo@15: ===================================================================== meillo@8: */ meillo@7: meillo@8: meillo@8: /* standard time to warn in advance, when no explicit w flag is given. */ meillo@8: #define DEF_WARN 14 meillo@0: meillo@7: meillo@7: #include meillo@0: #include meillo@0: #include meillo@7: #include meillo@7: #include meillo@7: #include meillo@7: #include meillo@0: meillo@7: meillo@7: meillo@7: /* ========== Global constants and data types */ meillo@7: meillo@7: meillo@7: /* -------- modifier flags */ meillo@7: #define F_MTYPE 0x07 meillo@7: #define F_TANNIVERSARY 2 meillo@7: meillo@7: /* flags processed immediately on encountering */ meillo@7: #define F_MIMMEDIATE 0x24 meillo@7: #define F_WTIME_P 0x08 meillo@7: meillo@16: struct _ftable { meillo@16: char* txt; meillo@16: unsigned flag; meillo@16: }; meillo@7: const struct _ftable FTABLE[]; meillo@7: meillo@7: struct date { meillo@15: unsigned day; meillo@15: unsigned month; meillo@15: unsigned year; meillo@7: }; meillo@7: meillo@7: struct event { meillo@15: char* text; meillo@15: struct date date; meillo@15: int warn; meillo@7: }; meillo@7: meillo@7: /* ========== Global Variables */ meillo@7: meillo@15: struct event *readlist(void); meillo@7: void gettoday(void); meillo@15: unsigned delta(struct date *); meillo@15: unsigned ddiff(struct date *D1, struct date *D2); meillo@16: void liststrings(struct event *evl); meillo@15: char *tdelta(struct date *d); meillo@15: char *ttime(int yr, int mn, int wk, int dy); meillo@16: char *skptok(char *ptr); meillo@15: int evcmp(const void *e1, const void *e2); meillo@7: meillo@7: meillo@7: struct date today; meillo@16: int def_warn = DEF_WARN; meillo@7: meillo@7: meillo@7: const struct _ftable FTABLE[] = { meillo@16: {"#ann",F_TANNIVERSARY}, meillo@16: {"#w", F_WTIME_P}, meillo@7: {NULL, 0} meillo@7: }; meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: /* meillo@15: xmalloc/xrealloc functions meillo@15: Note: the x* functions are lifted straight from the GNU libc info docs meillo@15: $Id: xmalloc.c,v 1.2 1999/01/16 17:08:59 andy Exp $ meillo@7: */ meillo@7: meillo@15: void * meillo@15: xmalloc(size_t size) meillo@15: { meillo@15: register void *value = malloc (size); meillo@7: if (value == 0) { meillo@7: fprintf(stderr, "virtual memory exhausted\n"); meillo@7: exit(1); meillo@7: } meillo@7: return value; meillo@7: } meillo@7: meillo@15: void * meillo@15: xrealloc(void *ptr, size_t size) meillo@15: { meillo@15: register void *value = realloc (ptr, size); meillo@7: if (value == 0) { meillo@7: fprintf(stderr, "virtual memory exhausted\n"); meillo@7: exit(1); meillo@7: } meillo@7: return value; meillo@7: } meillo@7: meillo@8: meillo@7: /* ========== */ meillo@7: meillo@7: meillo@15: /* meillo@15: like strcat(), but lets the buffer automagically grow :-) meillo@15: */ meillo@16: int meillo@16: append(char *where, int size, char *what) meillo@16: { meillo@16: if (strlen(what) > ((size) - strlen(where))) { meillo@16: xrealloc(where, (size) + 128 + strlen(what)); meillo@16: size += 128 + strlen(what); meillo@16: } meillo@16: strcat(where, what); meillo@16: return size; meillo@16: } meillo@7: meillo@7: /* ========== */ meillo@7: meillo@16: meillo@16: int meillo@16: before(struct date a, struct date b) meillo@16: { meillo@16: if (a.month < b.month) { meillo@16: return 1; meillo@16: } else if (a.month == b.month && a.day < b.day) { meillo@16: return 1; meillo@16: } else { meillo@16: return 0; meillo@16: } meillo@16: } meillo@16: meillo@16: int meillo@16: ydelta(struct date a, struct date b) meillo@16: { meillo@16: return b.year - a.year + before(a, b); meillo@16: } meillo@16: meillo@16: /* meillo@16: returns the length of the given month meillo@16: */ meillo@16: int meillo@16: mlen(int month, int year) meillo@16: { meillo@16: unsigned mlendat[] = {31,0,31,30,31,30,31,31,30,31,30,31}; meillo@16: meillo@16: if (mlendat[month - 1]) { meillo@16: return mlendat[month - 1]; meillo@16: } else { meillo@16: if (year%4==0 && (year%100!=0 || year%400==0)) { meillo@16: return 29; meillo@16: } else { meillo@16: return 28; meillo@16: } meillo@16: } meillo@16: } meillo@16: meillo@16: meillo@16: meillo@15: /* meillo@15: returns delta(d) in days, weeks, months, etc meillo@15: the returned buffer is malloc()ed, do not forget to free() it meillo@15: */ meillo@15: char * meillo@15: tdelta(struct date *d) meillo@15: { meillo@7: int dy, wk, mn, yr; meillo@15: char *tmp; meillo@15: char *buf = xmalloc(128); meillo@7: int size = 128; meillo@15: meillo@7: *buf = 0; meillo@15: switch (delta(d)) { meillo@15: case 0: meillo@16: size = append(buf, size, "TODAY"); meillo@15: return buf; meillo@15: case 1: meillo@16: size = append(buf, size, "Tomorrow"); meillo@15: return buf; meillo@15: default: meillo@15: /* like delta(), we ignore the year */ meillo@15: yr = -before(*d, today); meillo@15: mn = d->month - today.month; meillo@15: dy = d->day - today.day; meillo@7: meillo@15: if (dy < 0) { meillo@15: dy += mlen(today.month, today.year); meillo@15: mn--; meillo@15: } meillo@15: if (mn < 0) { meillo@15: mn += 12; meillo@15: yr++; meillo@15: } meillo@7: meillo@15: wk = (dy / 7); meillo@15: dy %= 7; meillo@7: meillo@16: size = append(buf, size, "In "); meillo@15: tmp = ttime(yr, mn, wk, dy); meillo@16: size = append(buf, size, tmp); meillo@15: free(tmp); meillo@7: meillo@15: return buf; meillo@7: } meillo@7: } meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@15: void meillo@16: donum(char *buf, int size, int n, char *txt, int *terms) meillo@15: { meillo@16: char tmp[128]; meillo@16: meillo@16: if (n <= 0) { meillo@16: return; meillo@16: } meillo@16: snprintf(tmp, sizeof(tmp), "%d", n); meillo@16: size = append(buf, size, tmp); meillo@16: size = append(buf, size, " "); meillo@16: size = append(buf, size, txt); meillo@16: if (n != 1) { meillo@16: size = append(buf, size, "s"); meillo@16: } meillo@16: if (--*terms == 1) { meillo@16: size = append(buf, size, " and "); meillo@16: } else if (*terms > 1) { meillo@16: size = append(buf, size, ", "); meillo@15: } meillo@7: } meillo@7: meillo@7: meillo@7: /* returns allocated buffer, don't forget to free() */ meillo@15: char * meillo@15: ttime(int yr, int mn, int wk, int dy) meillo@15: { meillo@7: int size = 128; meillo@16: char *buf = xmalloc(size); meillo@16: int terms = (yr!=0) + (mn!=0) + (wk!=0) + (dy!=0); meillo@7: meillo@16: *buf = '\0'; /* Initialize buffer */ meillo@7: meillo@16: donum(buf, size, yr, "year", &terms); meillo@16: donum(buf, size, mn, "month", &terms); meillo@16: donum(buf, size, wk, "week", &terms); meillo@16: donum(buf, size, dy, "day", &terms); meillo@7: meillo@7: return buf; meillo@7: } meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@15: /* meillo@15: lists the birthdays in their string format, one by one, and passes meillo@15: the string to a function. meillo@15: */ meillo@15: void meillo@16: liststrings(struct event *evl) meillo@15: { meillo@18: int i; meillo@7: char *buf, *tmp; meillo@7: int size; meillo@7: meillo@16: for (i=0; evl[i].text; i++) { meillo@16: size = 128; meillo@16: buf = xmalloc(size); meillo@7: *buf = '\0'; meillo@7: meillo@7: if (evl[i].warn == -1 && delta(&(evl[i].date))==0) { meillo@16: size = append(buf, size, evl[i].text); meillo@18: } else if (delta(&(evl[i].date)) <= evl[i].warn) { meillo@18: tmp = tdelta(&(evl[i].date)); meillo@18: size = append(buf, size, tmp); meillo@18: size = append(buf, size, ": "); meillo@18: size = append(buf, size, evl[i].text); meillo@18: free(tmp); meillo@7: } meillo@7: if (*buf) { meillo@16: size = append(buf, size, "."); meillo@16: puts(buf); meillo@7: } meillo@7: free(buf); meillo@7: } meillo@7: } meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@15: /* meillo@18: sort the events by the time before the next time they come up meillo@15: */ meillo@15: int meillo@15: evcmp(const void *p1, const void *p2) meillo@15: { meillo@15: struct event *e1=(struct event *) p1; meillo@15: struct event *e2=(struct event *) p2; meillo@8: unsigned d1, d2; meillo@7: meillo@7: d1=delta(&(e1->date)); meillo@7: d2=delta(&(e2->date)); meillo@7: meillo@7: if (d1 < d2) return -1; meillo@8: if (d1 > d2) return 1; meillo@7: return strcmp(e1->text, e2->text); meillo@7: } meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@15: /* meillo@15: difference in days between two dates meillo@15: it is assumed that D1 < D2, and so the result is always positive meillo@15: */ meillo@15: unsigned meillo@15: ddiff(struct date *D1, struct date *D2) meillo@15: { meillo@8: struct date d1, d2; meillo@8: int dd, m; meillo@7: meillo@7: /* make working copies */ meillo@8: d1 = *D1; meillo@8: d2 = *D2; meillo@7: meillo@7: /* sort out zero years */ meillo@7: if (d1.year == 0 || d2.year==0) { meillo@7: if (d1.year != d2.year) { meillo@7: if (d1.year == 0) { meillo@8: if (before(d1,d2)) meillo@8: d1.year = d2.year; meillo@8: else meillo@8: d1.year = d2.year - 1; meillo@8: } else { meillo@8: if (before(d1, d2)) meillo@8: d2.year = d1.year; meillo@8: else meillo@8: d2.year = d1.year + 1; meillo@7: } meillo@7: } else { /* both years zero */ meillo@8: if (before(d1, d2)) meillo@8: d1.year = d2.year = today.year; meillo@7: else { meillo@8: d1.year = today.year; meillo@8: d2.year = d1.year + 1; meillo@7: } meillo@7: } meillo@7: } meillo@7: meillo@7: /* now we can actually do the comparison ... */ meillo@8: dd = 0; meillo@7: meillo@7: /* to start with, we work in months */ meillo@7: for (m=d1.month; m < d2.month + (d2.year-d1.year)*12; m++) meillo@7: dd += mlen(((m-1)%12)+1, d1.year + m/12); meillo@7: meillo@15: /* meillo@15: and then we renormalise for the days within the months meillo@15: the first month was included in our calculations meillo@15: */ meillo@7: dd -= d1.day; meillo@7: /* but the last one wasn't */ meillo@7: dd += d2.day; meillo@7: meillo@7: return dd; meillo@7: } meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@15: /* meillo@15: actually until the next anniversary of ... meillo@15: */ meillo@15: unsigned meillo@15: delta(struct date *date) meillo@15: { meillo@7: struct date d; meillo@7: unsigned dt, mn; meillo@7: meillo@7: memcpy(&d, date, sizeof(struct date)); meillo@7: meillo@7: /* past the end of the year */ meillo@7: if (before(d, today)) { meillo@7: d.year = 1; meillo@7: } else { meillo@7: d.year = 0; meillo@7: } meillo@7: meillo@8: for (mn = today.month, dt=0; mn < d.month + 12*d.year; mn++) { meillo@7: dt += mlen(((mn-1)%12) + 1,today.year + mn/12); meillo@8: } meillo@7: meillo@7: dt -= today.day; meillo@7: dt += d.day; meillo@7: meillo@7: return dt; meillo@7: } meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@15: void meillo@15: gettoday(void) meillo@15: { meillo@7: struct tm *tm; meillo@7: time_t t; meillo@7: meillo@7: time(&t); meillo@7: tm = localtime(&t); meillo@7: today.day = tm->tm_mday; meillo@7: today.month = tm->tm_mon + 1; /* 1-12 instead of 0-11 */ meillo@7: today.year = tm->tm_year + 1900; meillo@7: } meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@15: struct event * meillo@15: readlist() meillo@15: { meillo@18: int i, j, k; meillo@7: struct event *evl; meillo@7: char buf[1024], buf2[1024]; meillo@16: char *ptr, *cp; meillo@7: unsigned flags; meillo@7: meillo@7: /* initialise */ meillo@7: gettoday(); meillo@7: meillo@18: for (i=0, evl=NULL; fgets(buf, sizeof(buf), stdin) != NULL; i++) { meillo@7: evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1)); meillo@7: meillo@7: /* ignore comments and empty lines */ meillo@7: if (*buf == '#' || *buf == '\n') { meillo@7: i--; meillo@7: continue; meillo@7: } meillo@7: meillo@7: /* parse string in buf */ meillo@16: meillo@16: ptr = strchr(buf, ' '); /* start of text */ meillo@7: meillo@7: /* not a valid line, so ignore it! Cool, huh? */ meillo@7: /* Attention: only recognizes lines without '=' */ meillo@16: if (!ptr) { meillo@16: fprintf(stderr, "WARNING: Invalid input line:\n\t%s", buf); meillo@7: i--; meillo@7: continue; meillo@7: } meillo@7: meillo@16: *(ptr++) = '\0'; meillo@16: ptr[strlen(ptr)-1] = '\0'; meillo@7: meillo@16: j = sscanf(buf, "%u-%u-%u", &(evl[i].date.year), meillo@15: &(evl[i].date.month), &(evl[i].date.day)); meillo@16: if (j != 3) { meillo@16: fprintf(stderr, "Error: Invalid date:\t%s\n", buf); meillo@16: i--; meillo@16: continue; meillo@7: } meillo@7: meillo@7: /* parse flags */ meillo@7: meillo@16: evl[i].warn = def_warn; meillo@7: flags = 0; meillo@7: j = 0; meillo@16: cp = skptok(ptr); meillo@16: for (cp=ptr; *cp && *cp=='#'; cp=skptok(cp)) { meillo@16: for (k = 0; FTABLE[k].txt && strncmp(FTABLE[k].txt, cp, strlen(FTABLE[k].txt)); k++) { meillo@7: } meillo@7: meillo@7: switch (FTABLE[k].flag) { meillo@16: case F_WTIME_P: /* #w -- sets warning time */ meillo@16: sscanf(cp, "#w%u", &(evl[i].warn)); meillo@15: break; meillo@15: case 0: meillo@15: break; meillo@15: default: meillo@15: flags |= FTABLE[k].flag; meillo@15: break; meillo@7: } meillo@7: } meillo@7: meillo@7: meillo@7: /* construct event text */ meillo@7: meillo@7: switch(flags & F_MTYPE) { meillo@7: default: /* assume it's a birthday */ meillo@16: if (!evl[i].date.year) { meillo@16: sprintf(buf2, "%s has a birthday", cp); meillo@16: break; meillo@7: } meillo@16: int tmp_age = ydelta(evl[i].date, today); meillo@16: sprintf(buf2, "%s is %d year%s old", meillo@16: cp, tmp_age, (tmp_age>1)?"s":""); meillo@7: break; meillo@7: case F_TANNIVERSARY: meillo@16: if (evl[i].date.year) { meillo@16: sprintf(buf2, "%s %d years ago", meillo@16: cp, ydelta(evl[i].date, today)); meillo@7: } else { meillo@16: strcpy(buf2, cp); meillo@7: } meillo@7: break; meillo@7: } meillo@7: evl[i].text = strdup(buf2); meillo@7: } meillo@7: meillo@7: evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1)); meillo@7: evl[i].date.day = 0; meillo@7: evl[i].date.month = 0; meillo@7: evl[i].date.year = 0; meillo@7: evl[i].text = (char *) NULL; meillo@7: meillo@7: fclose(stdin); meillo@7: meillo@7: /* NB uses i from above */ meillo@7: qsort(evl, i, sizeof(struct event), evcmp); meillo@7: return evl; meillo@7: } meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@16: char * meillo@16: skptok(char *ptr) meillo@15: { meillo@16: while (*ptr && (*ptr!=' ' && *ptr!='\t')) { meillo@16: ptr++; meillo@16: } meillo@16: while (*ptr && (*ptr==' ' || *ptr=='\t')) { meillo@16: ptr++; meillo@16: } meillo@16: return ptr; meillo@7: } meillo@7: meillo@7: meillo@7: meillo@7: meillo@0: meillo@0: meillo@15: int meillo@15: main(int argc, char *argv[]) meillo@15: { meillo@16: while (--argc > 0 && **++argv == '-') { meillo@17: if (strcmp(argv[0], "-w") == 0) { meillo@6: /* TODO: catch if no value given */ meillo@16: def_warn = atoi((++argv)[0]); meillo@0: argc--; meillo@0: } else { meillo@3: fprintf(stderr, "unknown option %s\n", argv[0]); meillo@0: exit(1); meillo@0: } meillo@0: } meillo@16: liststrings(readlist()); meillo@15: return 0; meillo@0: }