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.
This commit is contained in:
grog 2007-06-09 05:54:13 +00:00
parent b7b020d418
commit 4781a8b910
3 changed files with 119 additions and 32 deletions

View File

@ -42,6 +42,7 @@ extern struct iovec header[];
extern struct tm *tp;
extern const char *calendarFile;
extern int *cumdays;
extern int yrdays;
extern struct fixs neaster, npaskha;
void cal(void);
@ -68,7 +69,7 @@ void setnnames(void);
#define F_ISDAYVAR 0x04 /* variables day of week, like SundayLast */
#define F_EASTER 0x08 /* Easter or easter depending days */
extern int f_dayAfter; /* days after current date */
extern int f_dayAfter; /* days after current date */
extern int f_dayBefore; /* days bevore current date */
extern int Friday; /* day before weekend */
@ -77,3 +78,16 @@ struct fixs {
int len;
};
struct event *event_add(struct event *events, int month, int day, char *date,
int var, char *txt);
void event_continue(struct event *events, char *txt);
void event_print_all(FILE *fp, struct event *events);
/* Stored calendar event */
struct event {
int month;
int day;
int var;
char *date;
char *text;
struct event *next;
};

View File

@ -49,7 +49,7 @@ __FBSDID("$FreeBSD$");
struct tm *tp;
static const struct tm tm0;
int *cumdays, offset, yrdays;
int *cumdays, yrdays;
char dayname[10];
@ -151,7 +151,8 @@ settime(time_t now)
cumdays = daytab[0];
}
/* Friday displays Monday's events */
offset = tp->tm_wday == Friday ? 3 : 1;
if (f_dayAfter == 0 && f_dayBefore == 0 && Friday != -1)
f_dayAfter = tp->tm_wday == Friday ? 3 : 1;
header[5].iov_base = dayname;
oldl = NULL;
@ -375,27 +376,10 @@ isnow(char *endp, int *monthp, int *dayp, int *varp)
}
#ifdef DEBUG
fprintf(stderr, "day2: day %d(%d-%d) yday %d\n", *dayp, day, cumdays[month], tp->tm_yday);
fprintf(stderr, "day2: day %d(%d-%d) yday %d\n", *dayp, day,
cumdays[month], tp->tm_yday);
#endif
/* when only today and tomorrow (or today and the next three days if
it is friday) is needed */
if (f_dayBefore == 0 &&
f_dayAfter == 0 ) {
/* no year rollover */
if (day >= tp->tm_yday &&
day <= tp->tm_yday + offset)
return (1);
/* year rollover */
if (tp->tm_yday + offset >= yrdays) {
int end = tp->tm_yday + offset - yrdays;
if (day <= end)
return (1);
}
return (0);
}
/* When days before or days after is specified */
/* no year rollover */
if (day >= tp->tm_yday - f_dayBefore &&

View File

@ -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);
}