diff options
author | grog <grog@FreeBSD.org> | 2007-06-09 05:54:13 +0000 |
---|---|---|
committer | grog <grog@FreeBSD.org> | 2007-06-09 05:54:13 +0000 |
commit | 4781a8b910b4c59faae7b58451af6a94ceefaf50 (patch) | |
tree | 83caadd380eb6003121ef65c512f1584f506a7e7 /usr.bin/calendar/io.c | |
parent | b7b020d418be38f9550b3cf851085a207d35cb1f (diff) | |
download | FreeBSD-src-4781a8b910b4c59faae7b58451af6a94ceefaf50.zip FreeBSD-src-4781a8b910b4c59faae7b58451af6a94ceefaf50.tar.gz |
Sort events by date.
Correct long-standing off-by-one error in -W option.
Submitted by: edwin@
Shorten some long lines. These files are still not completely
style(9) compliant.
Diffstat (limited to 'usr.bin/calendar/io.c')
-rw-r--r-- | usr.bin/calendar/io.c | 109 |
1 files changed, 99 insertions, 10 deletions
diff --git a/usr.bin/calendar/io.c b/usr.bin/calendar/io.c index ee5b988..e5a0a08 100644 --- a/usr.bin/calendar/io.c +++ b/usr.bin/calendar/io.c @@ -96,6 +96,7 @@ cal(void) int var; static int d_first = -1; char buf[2048 + 1]; + struct event *events = NULL; if ((fp = opencal()) == NULL) return; @@ -155,23 +156,109 @@ cal(void) (void)strftime(dbuf, sizeof(dbuf), d_first ? "%e %b" : "%b %e", &tm); - (void)fprintf(fp, "%s%c%s\n", dbuf, - var ? '*' : ' ', p); + events = event_add(events, month, day, dbuf, var, p); } } else if (printing) - fprintf(fp, "%s\n", buf); + event_continue(events, buf); } + + event_print_all(fp, events); closecal(fp); } +/* + * Functions to handle buffered calendar events. + */ +struct event * +event_add(struct event *events, int month, int day, char *date, int var, char *txt) +{ + struct event *e; + + e = (struct event *)calloc(1, sizeof(struct event)); + if (e == NULL) + errx(1, "event_add: cannot allocate memory"); + e->month = month; + e->day = day; + e->var = var; + e->date = strdup(date); + if (e->date == NULL) + errx(1, "event_add: cannot allocate memory"); + e->text = strdup(txt); + if (e->text == NULL) + errx(1, "event_add: cannot allocate memory"); + e->next = events; + + return e; +} + +void +event_continue(struct event *e, char *txt) +{ + char *text; + + text = strdup(e->text); + if (text == NULL) + errx(1, "event_continue: cannot allocate memory"); + + free(e->text); + e->text = (char *)malloc(strlen(text) + strlen(txt) + 3); + if (e->text == NULL) + errx(1, "event_continue: cannot allocate memory"); + strcpy(e->text, text); + strcat(e->text, "\n"); + strcat(e->text, txt); + free(text); + + return; +} + +void +event_print_all(FILE *fp, struct event *events) +{ + struct event *e, *e_next; + int daycount = f_dayAfter + f_dayBefore; + int daycounter; + int day, month; + + for (daycounter = 0; daycounter <= daycount; daycounter++) { + day = tp->tm_yday - f_dayBefore + daycounter; + if (day < 0) day += yrdays; + if (day >= yrdays) day -= yrdays; + + month = 1; + while (month <= 12) { + if (day <= cumdays[month]) + break; + month++; + } + month--; + day -= cumdays[month]; + +#ifdef DEBUG + fprintf(stderr,"event_print_allmonth: %d, day: %d\n",month,day); +#endif + + for (e = events; e != NULL; e = e_next ) { + e_next = e->next; + + if (month != e->month || day != e->day) + continue; + + (void)fprintf(fp, "%s%c%s\n", e->date, + e->var ? '*' : ' ', e->text); + } + } +} + int getfield(char *p, char **endp, int *flags) { int val, var; char *start, savech; - for (; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p) && *p != '*'; ++p); + for (; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p) + && *p != '*'; ++p); if (*p == '*') { /* `*' is current month */ *flags |= F_ISMONTH; *endp = p+1; @@ -179,16 +266,17 @@ getfield(char *p, char **endp, int *flags) } if (isdigit((unsigned char)*p)) { val = strtol(p, &p, 10); /* if 0, it's failure */ - for (; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p) && *p != '*'; ++p); + for (; !isdigit((unsigned char)*p) + && !isalpha((unsigned char)*p) && *p != '*'; ++p); *endp = p; return (val); } for (start = p; isalpha((unsigned char)*++p);); - + /* Sunday-1 */ - if (*p == '+' || *p == '-') + if (*p == '+' || *p == '-') for(; isdigit((unsigned char)*++p);); - + savech = *p; *p = '\0'; @@ -207,7 +295,7 @@ getfield(char *p, char **endp, int *flags) #ifdef DEBUG printf("var: %d\n", var); #endif - } + } } /* Easter */ @@ -223,7 +311,8 @@ getfield(char *p, char **endp, int *flags) *p = savech; return (0); } - for (*p = savech; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p) && *p != '*'; ++p); + for (*p = savech; !isdigit((unsigned char)*p) + && !isalpha((unsigned char)*p) && *p != '*'; ++p); *endp = p; return (val); } |