meillo@0: /* meillo@8: bday meillo@0: meillo@8: Birthday/Anniversary reminder meillo@0: meillo@8: (c) 2007 markus schnalke meillo@9: (c) 1994-1999 AS Mortimer meillo@0: meillo@8: This program is free software; you can redistribute it and/or meillo@8: modify it under the terms of the GNU General Public License as meillo@8: published by the Free Software Foundation; either version 2 of the meillo@9: License, or (at your option) any later version. You may also meillo@9: distribute it under the Artistic License, as comes with Perl. meillo@0: meillo@8: This program is distributed in the hope that it will be useful, meillo@8: but WITHOUT ANY WARRANTY; without even the implied warranty of meillo@8: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. meillo@0: meillo@8: You should have received a copy of the GNU General Public License meillo@8: along with this program; if not, write to the Free Software meillo@8: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA meillo@0: meillo@9: You should also have recieved a copy of the Artistic license with meillo@9: this program. meillo@9: meillo@8: =============================================================================== meillo@0: meillo@8: Input is read through standard input. For example: bday < ~/.birthdays meillo@8: The input (file) has to have the following format: meillo@7: meillo@8: text=date flags meillo@7: meillo@8: where: meillo@8: date is yyyy-mm-dd meillo@8: flags is ONE or ZERO of meillo@8: bd for a birthday (default) meillo@8: ann for an anniversary meillo@8: ev for an event meillo@8: and zero or more of meillo@8: w to set the warn-in-advance time to n days (don't include the meillo@8: brackets! :) meillo@8: to meillo@8: for meillo@8: to specify the length of time taken by an event, for example a holiday. meillo@7: meillo@8: Lines preceeded by # are treated as comments. meillo@7: meillo@8: Note: If you deviate from this format, I cannot guarantee anything about meillo@8: it's behaviour. In most cases, it will just quietly ignore the error, meillo@8: which probably isn't ideal behaviour. Oh, well. meillo@7: meillo@8: =============================================================================== 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: /* month lengths etc */ meillo@7: #define isleapyear(y) ((y)%4==0 && ((y)%100 != 0 || (y)%400 == 0)) meillo@7: const unsigned MLENDAT[]; meillo@7: #define mlen(m,y) (MLENDAT[(m)-1] != -1 ? MLENDAT[(m)-1] : (isleapyear((y)) ? 29 : 28)) meillo@7: #define before(a,b) ((a).month < (b).month || ((a).month == (b).month && (a).day < (b).day)) meillo@7: #define ydelta(a,b) ((int) (b).year - (a).year + before((a),(b))) meillo@7: meillo@7: /* -------- modifier flags */ meillo@7: #define F_MTYPE 0x07 meillo@7: #define F_TBIRTHDAY 1 meillo@7: #define F_TANNIVERSARY 2 meillo@7: #define F_TEVENT 3 meillo@7: meillo@7: /* flags processed immediately on encountering */ meillo@7: #define F_MIMMEDIATE 0x24 meillo@7: #define F_WTIME_P 0x08 meillo@7: #define F_FORDAYS 0x16 meillo@7: #define F_TODATE 0x24 meillo@7: meillo@8: struct _ftable {char* txt; unsigned flag;}; meillo@7: meillo@7: const struct _ftable FTABLE[]; meillo@7: meillo@7: struct date { meillo@7: unsigned day; meillo@7: unsigned month; meillo@7: unsigned year; meillo@7: }; meillo@7: meillo@7: struct event { meillo@8: char* text; meillo@7: struct date date; meillo@7: struct date enddate; meillo@7: int warn; meillo@7: }; meillo@7: meillo@7: typedef int (*prnfunc)(const char *); meillo@7: meillo@7: /* ========== Global Variables */ meillo@7: meillo@7: struct event* readlist(void); meillo@7: void gettoday(void); meillo@8: unsigned delta(struct date*); meillo@8: unsigned ddiff(struct date* D1, struct date* D2); meillo@7: void liststrings(struct event* evl, prnfunc outf); meillo@8: char* tdelta(struct date* d); meillo@8: char* ttime(int yr, int mn, int wk, int dy); meillo@8: int skptok(int j, char* ptr); meillo@8: int evcmp(const void* e1, const void* e2); meillo@7: meillo@7: meillo@7: struct date today; meillo@7: int iDWarn = DEF_WARN; meillo@7: meillo@7: const unsigned MLENDAT[]={31,-1,31,30,31,30,31,31,30,31,30,31}; meillo@7: meillo@7: const struct _ftable FTABLE[] = { meillo@7: {"bd", F_TBIRTHDAY}, meillo@7: {"ann",F_TANNIVERSARY}, meillo@7: {"ev", F_TEVENT}, meillo@7: {"w", F_WTIME_P}, meillo@7: {"to", F_TODATE}, meillo@7: {"for", F_FORDAYS}, meillo@7: {NULL, 0} meillo@7: }; meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: /* meillo@7: xmalloc/xrealloc functions meillo@7: Note: the x* functions are lifted straight from the GNU libc info docs meillo@7: $Id: xmalloc.c,v 1.2 1999/01/16 17:08:59 andy Exp $ meillo@7: */ meillo@7: meillo@7: void* xmalloc (size_t size) { meillo@7: 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@7: void* xrealloc (void* ptr, size_t size) { meillo@7: 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@7: /* like strcat(), but lets the buffer automagically grow :-) meillo@7: * (needs local variable "size" with the buffer size) */ meillo@7: #define append(where, what) do { \ meillo@7: if (strlen(what) > (size - strlen(where))) { \ meillo@7: xrealloc(where, size + 128 + strlen(what)); \ meillo@7: size += 128 + strlen(what); \ meillo@7: } \ meillo@7: strcat(where, what); \ meillo@7: } while(0) meillo@7: meillo@7: /* ========== */ meillo@7: meillo@7: /* returns delta(d) in days, weeks, months, etc meillo@7: * the returned buffer is malloc()ed, do not forget to free() it */ meillo@8: char* tdelta(struct date* d) { meillo@7: int dy, wk, mn, yr; meillo@8: char* tmp; meillo@8: char* buf = xmalloc(128); meillo@7: int size = 128; meillo@7: *buf = 0; meillo@7: meillo@7: switch (delta(d)) { meillo@7: case 0: meillo@7: append(buf, "today"); meillo@7: return buf; meillo@7: case 1: meillo@7: append(buf, "tomorrow"); meillo@7: return buf; meillo@7: default: meillo@7: /* like delta(), we ignore the year */ meillo@8: yr = -before(*d, today); meillo@8: mn = d->month - today.month; meillo@8: dy = d->day - today.day; meillo@7: meillo@7: if (dy < 0) { meillo@7: dy += mlen(today.month, today.year); meillo@7: mn--; meillo@7: } meillo@7: if (mn < 0) { meillo@7: mn += 12; meillo@7: yr++; meillo@7: } meillo@7: meillo@7: wk = (dy / 7); meillo@7: dy %= 7; meillo@7: meillo@7: append(buf, "in "); meillo@7: tmp = ttime(yr, mn, wk, dy); meillo@7: append(buf, tmp); meillo@7: free(tmp); meillo@7: meillo@7: return buf; meillo@7: } meillo@7: } meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: /* meillo@7: void donum(n,txt) { meillo@7: do { meillo@7: if (n > 0) { meillo@7: snprintf(tmp, sizeof(tmp), "%d", n); meillo@7: append(buf, tmp); meillo@7: append(buf, " " txt); meillo@7: if (n != 1) meillo@7: append(buf, "s"); meillo@7: terms--; meillo@7: if (orgterms > 1) { meillo@7: if (terms == 1) meillo@7: append(buf, " and "); meillo@7: else if (terms > 1) meillo@7: append(buf, ", "); meillo@7: } meillo@7: } meillo@7: } while(0) meillo@7: } meillo@7: */ meillo@7: meillo@7: meillo@7: #define donum(n,txt) do { \ meillo@7: if (n > 0) { \ meillo@7: snprintf(tmp, sizeof(tmp), "%d", n); \ meillo@7: append(buf, tmp); \ meillo@7: append(buf, " " txt); \ meillo@7: if (n != 1) \ meillo@7: append(buf, "s"); \ meillo@7: terms--; \ meillo@7: if (orgterms > 1) { \ meillo@7: if (terms == 1) \ meillo@7: append(buf, " and "); \ meillo@7: else if (terms > 1) \ meillo@7: append(buf, ", "); \ meillo@7: } \ meillo@7: } \ meillo@7: } while(0) meillo@7: meillo@7: meillo@7: /* returns allocated buffer, don't forget to free() */ meillo@7: char* ttime(int yr, int mn, int wk, int dy) { meillo@7: char* buf = xmalloc(128); meillo@7: int size = 128; meillo@7: int terms, orgterms; meillo@7: char tmp[128]; meillo@7: meillo@7: *buf = 0; /* Initialize buffer */ meillo@7: terms = orgterms = (yr!=0) + (mn!=0) + (wk!=0) + (dy!=0); meillo@7: meillo@7: donum(yr, "year"); meillo@7: donum(mn, "month"); meillo@7: donum(wk, "week"); meillo@7: donum(dy, "day"); meillo@7: meillo@7: return buf; meillo@7: } meillo@7: #undef donum meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: /* lists the birthdays in their string format, one by one, and passes the string to a function. */ meillo@7: void liststrings(struct event* evl, prnfunc outf) { meillo@7: int i,j; meillo@7: char *buf, *tmp; meillo@7: int size; meillo@7: meillo@7: for (i = 0; evl[i].text != NULL; i++) { meillo@7: buf = xmalloc(128); meillo@7: *buf = '\0'; meillo@7: size = 128; meillo@7: meillo@7: if (evl[i].warn == -1 && delta(&(evl[i].date))==0) { meillo@7: append(buf, evl[i].text); meillo@7: } else if (evl[i].enddate.day == 0) { meillo@7: if (delta(&(evl[i].date)) <= evl[i].warn) { meillo@7: append(buf, evl[i].text); meillo@7: append(buf, " "); meillo@7: tmp = tdelta(&(evl[i].date)); meillo@7: append(buf, tmp); meillo@7: free(tmp); meillo@7: } meillo@7: } else { meillo@7: if (delta(&(evl[i].date)) <= evl[i].warn) { meillo@7: append(buf, evl[i].text); meillo@7: append(buf, " for "); meillo@14: /* +1 because, if the difference between two dates is one day, meillo@14: then the length of an event on those days is two days */ meillo@7: j = ddiff(&(evl[i].date),&(evl[i].enddate)) + 1; meillo@7: tmp = ttime(0, 0, j/7, j%7); meillo@7: append(buf, tmp); meillo@7: free(tmp); meillo@7: append(buf, " "); meillo@7: tmp = tdelta(&(evl[i].date)); meillo@7: append(buf, tmp); meillo@7: } else if (delta(&(evl[i].enddate)) <= evl[i].warn) { meillo@7: append(buf, evl[i].text); meillo@7: append(buf, " "); meillo@7: j = delta(&(evl[i].enddate)); meillo@7: if (j) { meillo@7: append(buf, "for "); meillo@7: tmp = ttime(0, 0, j/7, j%7); meillo@7: append(buf, tmp); meillo@7: free(tmp); meillo@7: append(buf, " longer"); meillo@7: } else { meillo@7: append(buf, "finishes today"); meillo@7: } meillo@7: } meillo@7: } meillo@7: if (*buf) { meillo@7: append(buf, "."); meillo@7: outf(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@7: /* sort the events by the time before the next time they come up, putting those meillo@7: where the start has passed but we are still in the time-period first */ meillo@8: int evcmp(const void* p1, const void* p2) { meillo@8: struct event* e1=(struct event*) p1; meillo@8: struct event* e2=(struct event*) p2; meillo@8: unsigned d1, d2; meillo@7: meillo@7: /* if the delta for the enddate is less than that for the start date, then we meillo@7: have passed the start date but not yet the end date, and so we should meillo@7: display the enddate; otherwise, we should display the start date */ meillo@7: meillo@7: d1=delta(&(e1->date)); meillo@7: if (e1->enddate.day && delta(&(e1->enddate)) < d1) meillo@7: d1=delta(&(e1->enddate)); meillo@7: meillo@7: d2=delta(&(e2->date)); meillo@7: if (e2->enddate.day && delta(&(e2->enddate)) < d2) meillo@7: d2=delta(&(e2->enddate)); meillo@7: meillo@7: if (d1 < d2) return -1; meillo@8: if (d1 > d2) return 1; meillo@7: meillo@7: return strcmp(e1->text, e2->text); meillo@7: } meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: meillo@7: /* difference in days between two dates */ meillo@7: /* it is assumed that D1 < D2, and so the result is always positive */ meillo@8: unsigned ddiff(struct date* D1, struct date* D2) { 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@7: /* and then we renormalise for the days within the months */ meillo@7: /* the first month was included in our calculations */ 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@7: /* actually until the next anniversary of ... */ meillo@7: unsigned delta(struct date *date) { 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@7: void gettoday(void) { 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@7: struct event* readlist() { meillo@7: int i, j, k, l, d; meillo@7: struct event *evl; meillo@7: char buf[1024], buf2[1024]; meillo@7: char *ptr; meillo@7: unsigned flags; meillo@7: meillo@7: /* initialise */ meillo@7: gettoday(); meillo@7: meillo@7: 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@7: ptr = strrchr(buf, '='); /* allow '=' in text */ meillo@7: meillo@7: /* not a valid line, so ignore it! Cool, huh? */ meillo@7: /* Attention: only recognizes lines without '=' */ meillo@7: if (ptr == NULL) { meillo@7: fprintf(stderr, "WARNING: Invalid line in input:\n%s", buf); meillo@7: i--; meillo@7: continue; meillo@7: } meillo@7: meillo@7: *(ptr++) = 0; meillo@7: meillo@7: j = sscanf(ptr, "%u-%u-%u", &(evl[i].date.year), &(evl[i].date.month), &(evl[i].date.day)); meillo@7: /* ... unless it wasn't read, in which case set it to zero */ meillo@7: if (j==2) { meillo@7: evl[i].date.year = 0; meillo@7: } meillo@7: meillo@7: meillo@7: /* parse flags */ meillo@7: meillo@7: evl[i].warn = iDWarn; meillo@7: evl[i].enddate.day = 0; meillo@7: evl[i].enddate.month = 0; meillo@7: evl[i].enddate.year = 0; meillo@7: meillo@7: flags = 0; meillo@7: j = 0; meillo@7: meillo@7: while(j = skptok(j, ptr), ptr[j] != 0) { meillo@7: for (k = 0; FTABLE[k].txt != NULL && strncmp(FTABLE[k].txt, ptr + j, strlen(FTABLE[k].txt)); k++) { meillo@7: } meillo@7: meillo@7: switch (FTABLE[k].flag) { meillo@7: case F_WTIME_P: /* w -- sets warning time */ meillo@7: sscanf(ptr + j, "w %u", &(evl[i].warn)); meillo@7: break; meillo@7: case F_FORDAYS: /* for -- sets the duration of the event */ meillo@7: sscanf(ptr + j, "for %u", &d); meillo@7: evl[i].enddate=evl[i].date; meillo@7: for (l = 1; l < d; l++) { meillo@7: evl[i].enddate.day++; meillo@7: if (evl[i].enddate.day > mlen(evl[i].enddate.month, evl[i].enddate.year)) { meillo@7: evl[i].enddate.month++; meillo@7: evl[i].enddate.day = 1; meillo@7: } meillo@7: if (evl[i].enddate.month > 12) { meillo@7: evl[i].enddate.year++; meillo@7: evl[i].enddate.month = 1; meillo@7: } meillo@7: } meillo@7: break; meillo@7: case F_TODATE: /* to -- sets the end date of the event */ meillo@7: l = sscanf(ptr + j, "to %u-%u-%u", &(evl[i].enddate.year), &(evl[i].enddate.month), &(evl[i].enddate.day)); meillo@7: if (l == 2) { meillo@7: evl[i].enddate.year = 0; meillo@7: } meillo@7: break; meillo@7: case 0: meillo@7: break; meillo@7: default: meillo@7: flags |= FTABLE[k].flag; meillo@7: break; meillo@7: } meillo@7: } meillo@7: meillo@7: meillo@7: /* construct event text */ meillo@7: meillo@7: switch(flags & F_MTYPE) { meillo@7: case F_TBIRTHDAY: meillo@7: default: /* assume it's a birthday */ meillo@7: if (evl[i].date.year != 0) { meillo@7: int tmp_age = ydelta(evl[i].date, today); meillo@7: if (tmp_age != 1) { meillo@7: sprintf(buf2, "%s is %d years old", buf, tmp_age); meillo@7: } else { meillo@7: sprintf(buf2, "%s is %d year old", buf, tmp_age); meillo@7: } meillo@7: } else { meillo@7: sprintf(buf2, "%s has a birthday", buf); meillo@7: } meillo@7: break; meillo@7: case F_TANNIVERSARY: meillo@7: if (evl[i].date.year != 0) { meillo@7: sprintf(buf2, "%s %d years ago", buf, ydelta(evl[i].date, today)); meillo@7: } else { meillo@7: strcpy(buf2, buf); meillo@7: } meillo@7: break; meillo@7: case F_TEVENT: meillo@7: /* if a year was specified, and this warning isn't for it, ignore! */ meillo@14: if ((evl[i].date.year != 0 && ydelta(evl[i].date, today) != 0) meillo@14: && (evl[i].enddate.year == 0 || ydelta(evl[i].enddate, today) != 0)) { meillo@7: i--; meillo@7: continue; meillo@7: } meillo@7: strcpy(buf2, buf); 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@7: int skptok(int j, char *ptr) { meillo@7: for (; ptr[j] != 0 && ptr[j] != ' ' && ptr[j] != '\t' ; j++); meillo@7: for (; ptr[j] != 0 && (ptr[j] == ' ' || ptr[j] == '\t'); j++); meillo@7: meillo@7: return j; meillo@7: } meillo@7: meillo@7: meillo@7: meillo@7: meillo@0: meillo@0: meillo@6: int main(int argc, char* argv[]) { meillo@0: meillo@0: while (--argc > 0 && (*++argv)[0] == '-') { meillo@5: if (strcmp(argv[0], "-W") == 0) { meillo@6: /* TODO: catch if no value given */ meillo@0: iDWarn = 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@0: meillo@6: liststrings(readlist(), puts); meillo@0: meillo@0: return 0; meillo@0: }