=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/calendar/io.c,v retrieving revision 1.1 retrieving revision 1.7 diff -u -r1.1 -r1.7 --- src/usr.bin/calendar/io.c 1996/12/05 06:04:40 1.1 +++ src/usr.bin/calendar/io.c 1999/04/20 23:03:25 1.7 @@ -1,4 +1,4 @@ -/* $OpenBSD: io.c,v 1.1 1996/12/05 06:04:40 millert Exp $ */ +/* $OpenBSD: io.c,v 1.7 1999/04/20 23:03:25 pjanzen Exp $ */ /* * Copyright (c) 1989, 1993, 1994 @@ -43,7 +43,7 @@ #if 0 static const char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94"; #else -static char rcsid[] = "$OpenBSD: io.c,v 1.1 1996/12/05 06:04:40 millert Exp $"; +static char rcsid[] = "$OpenBSD: io.c,v 1.7 1999/04/20 23:03:25 pjanzen Exp $"; #endif #endif /* not lint */ @@ -62,6 +62,7 @@ #include #include #include +#include #include #include "pathnames.h" @@ -72,8 +73,6 @@ char *calendarHome = ".calendar"; /* HOME */ char *calendarNoMail = "nomail"; /* don't sent mail if this file exist */ -struct fixs neaster, npaskha; - struct iovec header[] = { {"From: ", 6}, {NULL, 0}, @@ -91,12 +90,14 @@ register int printing; register char *p; FILE *fp; - int ch, l; - int month; - int day; + int ch, l, i; int var; char buf[2048 + 1]; + struct event *events, *cur_evt, *ev1, *tmp; + struct match *m; + events = NULL; + cur_evt = NULL; if ((fp = opencal()) == NULL) return; for (printing = 0; fgets(buf, sizeof(buf), stdin) != NULL;) { @@ -116,49 +117,85 @@ setnnames(); continue; } - if (strncasecmp(buf, "Easter=", 7) == 0 && buf[7]) { - if (neaster.name != NULL) - free(neaster.name); - if ((neaster.name = strdup(buf + 7)) == NULL) - errx(1, "cannot allocate memory"); - neaster.len = strlen(buf + 7); + /* User defined names for special events */ + if ((p = strchr(buf, '='))) { + for (i = 0; i < NUMEV; i++) { + if (strncasecmp(buf, spev[i].name, spev[i].nlen) == 0 && + (p - buf == spev[i].nlen) && buf[spev[i].nlen + 1]) { + p++; + if (spev[i].uname != NULL) + free(spev[i].uname); + if ((spev[i].uname = strdup(p)) == NULL) + errx(1, "cannot allocate memory"); + spev[i].ulen = strlen(p); + i = NUMEV + 1; + } + } + if (i > NUMEV) continue; } - if (strncasecmp(buf, "Paskha=", 7) == 0 && buf[7]) { - if (npaskha.name != NULL) - free(npaskha.name); - if ((npaskha.name = strdup(buf + 7)) == NULL) - errx(1, "cannot allocate memory"); - npaskha.len = strlen(buf + 7); - continue; - } if (buf[0] != '\t') { - printing = isnow(buf, &month, &day, &var) ? 1 : 0; - if ((p = strchr(buf, '\t')) == NULL) + printing = (m = isnow(buf)) ? 1 : 0; + if ((p = strchr(buf, '\t')) == NULL) { + printing = 0; continue; + } + /* Need the following to catch hardwired "variable" + * dates */ if (p > buf && p[-1] == '*') var = 1; + else + var = 0; if (printing) { - struct tm tm; - char dbuf[30]; + struct match *foo; + + ev1 = NULL; + while (m) { + cur_evt = (struct event *) malloc(sizeof(struct event)); + if (cur_evt == NULL) + errx(1, "cannot allocate memory"); - tm.tm_sec = 0; /* unused */ - tm.tm_min = 0; /* unused */ - tm.tm_hour = 0; /* unused */ - tm.tm_wday = 0; /* unused */ - tm.tm_mon = month - 1; - tm.tm_mday = day; - tm.tm_year = tp->tm_year; /* unused */ - (void)strftime(dbuf, sizeof(dbuf), "%c", &tm); - dbuf[10] = '\0'; - (void)fprintf(fp, "%s%c%s\n", - dbuf + 4/* skip weekdays */, - var ? '*' : ' ', p); + cur_evt->when = m->when; + snprintf(cur_evt->print_date, + sizeof(cur_evt->print_date), "%s%c", + m->print_date, (var + m->var) ? '*' : ' '); + if (ev1) { + cur_evt->desc = ev1->desc; + cur_evt->ldesc = NULL; + } else { + if ((cur_evt->ldesc = strdup(p)) == NULL) + errx(1, "cannot allocate memory"); + cur_evt->desc = &(cur_evt->ldesc); + ev1 = cur_evt; + } + insert(&events, cur_evt); + foo = m; + m = m->next; + free(foo); + } } } - else if (printing) - fprintf(fp, "%s\n", buf); + else if (printing) { + if ((ev1->ldesc = realloc(ev1->ldesc, + (2 + strlen(ev1->ldesc) + strlen(buf)))) == NULL) + errx(1, "cannot allocate memory"); + strcat(ev1->ldesc, "\n"); + strcat(ev1->ldesc, buf); + } } + tmp = events; + while (tmp) { + (void)fprintf(fp, "%s%s\n", tmp->print_date, *(tmp->desc)); + tmp = tmp->next; + } + tmp = events; + while (tmp) { + events = tmp; + if (tmp->ldesc) + free(tmp->ldesc); + tmp = tmp->next; + free(events); + } closecal(fp); } @@ -167,15 +204,15 @@ char *p, **endp; int *flags; { - int val, var; + int val, var, i; char *start, savech; for (; !isdigit(*p) && !isalpha(*p) && *p != '*'; ++p) ; - if (*p == '*') { /* `*' is current month */ + if (*p == '*') { /* `*' is every month */ *flags |= F_ISMONTH; *endp = p+1; - return (tp->tm_mon + 1); + return (-1); /* means 'every month' */ } if (isdigit(*p)) { val = strtol(p, &p, 10); /* if 0, it's failure */ @@ -205,7 +242,7 @@ /* variable weekday */ if ((var = getdayvar(start)) != 0) { - if (var <=5 && var >= -4) + if (var <= 5 && var >= -4) val += var * 10; #ifdef DEBUG printf("var: %d\n", var); @@ -213,18 +250,41 @@ } } - /* Easter */ - else if ((val = geteaster(start, tp->tm_year + 1900)) != 0) - *flags |= F_EASTER; - - /* Paskha */ - else if ((val = getpaskha(start, tp->tm_year + 1900)) != 0) - *flags |= F_EASTER; - - /* undefined rest */ + /* Try specials (Easter, Paskha, ...) */ else { - *p = savech; - return (0); + for (i = 0; i < NUMEV; i++) { + if (strncasecmp(start, spev[i].name, spev[i].nlen) == 0) { + start += spev[i].nlen; + val = i + 1; + i = NUMEV + 1; + } else if (spev[i].uname != NULL && + strncasecmp(start, spev[i].uname, spev[i].ulen) == 0) { + start += spev[i].ulen; + val = i + 1; + i = NUMEV + 1; + } + } + if (i > NUMEV) { + switch(*start) { + case '-': + case '+': + var = atoi(start); + if (var > 365 || var < -365) + return (0); /* Someone is just being silly */ + val += (NUMEV + 1) * var; + /* We add one to the matching event and multiply by + * (NUMEV + 1) so as not to return 0 if there's a match. + * val will overflow if there is an obscenely large + * number of special events. */ + break; + } + *flags |= F_SPECIAL; + } + if (!(*flags & F_SPECIAL)) { + /* undefined rest */ + *p = savech; + return (0); + } } for (*p = savech; !isdigit(*p) && !isalpha(*p) && *p != '*'; ++p) ; @@ -253,7 +313,8 @@ chdir(getenv("HOME")); if (!(chdir(calendarHome) == 0 && freopen(calendarFile, "r", stdin))) - errx(1, "no calendar file: ``%s'' or ``~/%s/%s\n", calendarFile, calendarHome, calendarFile); + errx(1, "no calendar file: ``%s'' or ``~/%s/%s", + calendarFile, calendarHome, calendarFile); } } if (pipe(pdes) < 0) @@ -323,7 +384,7 @@ (void)setuid(geteuid()); (void)setgid(getegid()); execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F", - "\"Reminder Service\"", "-f", "root", NULL); + "\"Reminder Service\"", NULL); warn(_PATH_SENDMAIL); _exit(1); } @@ -339,4 +400,37 @@ done: (void)fclose(fp); (void)unlink(path); while (wait(&status) >= 0); +} + + +void +insert(head, cur_evt) + struct event **head; + struct event *cur_evt; +{ + struct event *tmp, *tmp2; + + if (*head) { + /* Insert this one in order */ + tmp = *head; + tmp2 = NULL; + while (tmp->next && + tmp->when <= cur_evt->when) { + tmp2 = tmp; + tmp = tmp->next; + } + if (tmp->when > cur_evt->when) { + cur_evt->next = tmp; + if (tmp2) + tmp2->next = cur_evt; + else + *head = cur_evt; + } else { + cur_evt->next = tmp->next; + tmp->next = cur_evt; + } + } else { + *head = cur_evt; + cur_evt->next = NULL; + } }