summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/calendar/calendar.h27
-rw-r--r--usr.bin/calendar/day.c56
-rw-r--r--usr.bin/calendar/io.c108
3 files changed, 143 insertions, 48 deletions
diff --git a/usr.bin/calendar/calendar.h b/usr.bin/calendar/calendar.h
index 02390a093af..cd0bed0acff 100644
--- a/usr.bin/calendar/calendar.h
+++ b/usr.bin/calendar/calendar.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: calendar.h,v 1.2 1998/03/30 06:59:24 deraadt Exp $ */
+/* $OpenBSD: calendar.h,v 1.3 1998/11/08 04:31:13 pjanzen Exp $ */
/*
* Copyright (c) 1989, 1993, 1994
@@ -41,6 +41,23 @@ extern struct tm *tp;
extern char *calendarFile;
extern char *optarg;
+struct fixs {
+ char *name;
+ int len;
+};
+
+struct event {
+ time_t when;
+ char print_date[31];
+ char *desc;
+ struct event *next;
+};
+
+struct match {
+ int year, month, day, var;
+ struct match *next;
+};
+
void cal __P((void));
void closecal __P((FILE *));
int getday __P((char *));
@@ -50,7 +67,8 @@ int getmonth __P((char *));
int geteaster __P((char *, int));
int getpaskha __P((char *, int));
int easter __P((int));
-int isnow __P((char *, int *, int *, int *));
+void insert __P((struct event **, struct event *));
+struct match *isnow __P((char *));
FILE *opencal __P((void));
void settime __P((time_t));
time_t Mktime __P((char *));
@@ -65,8 +83,3 @@ void setnnames __P((void));
extern int f_dayAfter; /* days after current date */
extern int f_dayBefore; /* days before current date */
-
-struct fixs {
- char *name;
- int len;
-};
diff --git a/usr.bin/calendar/day.c b/usr.bin/calendar/day.c
index 011c70e9688..141f905d2f1 100644
--- a/usr.bin/calendar/day.c
+++ b/usr.bin/calendar/day.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: day.c,v 1.5 1998/11/04 11:32:02 pjanzen Exp $ */
+/* $OpenBSD: day.c,v 1.6 1998/11/08 04:31:13 pjanzen Exp $ */
/*
* Copyright (c) 1989, 1993, 1994
@@ -43,7 +43,7 @@ static const char copyright[] =
#if 0
static const char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94";
#else
-static char rcsid[] = "$OpenBSD: day.c,v 1.5 1998/11/04 11:32:02 pjanzen Exp $";
+static char rcsid[] = "$OpenBSD: day.c,v 1.6 1998/11/08 04:31:13 pjanzen Exp $";
#endif
#endif /* not lint */
@@ -231,14 +231,13 @@ time_t Mktime (date)
* following a line that is matched, that starts with "whitespace", is shown
* along with the matched line.
*/
-int
-isnow(endp, monthp, dayp, varp)
+struct match *
+isnow(endp)
char *endp;
- int *monthp;
- int *dayp;
- int *varp;
{
int day, flags = 0, month = 0, v1, v2;
+ int monthp, dayp, varp;
+ struct match *matches;
/*
* CONVENTION
@@ -253,7 +252,7 @@ isnow(endp, monthp, dayp, varp)
/* read first field */
/* didn't recognize anything, skip it */
if (!(v1 = getfield(endp, &endp, &flags)))
- return (0);
+ return (NULL);
/* Easter or Easter depending days */
if (flags & F_EASTER)
@@ -297,7 +296,7 @@ isnow(endp, monthp, dayp, varp)
if (flags & F_ISMONTH) {
day = v1;
month = v2;
- *varp = 0;
+ varp = 0;
}
/* {Month} {Weekday,Day} ... */
@@ -306,7 +305,7 @@ isnow(endp, monthp, dayp, varp)
month = v1;
/* if no recognizable day, assume the first */
day = v2 ? v2 : 1;
- *varp = 0;
+ varp = 0;
}
}
@@ -319,7 +318,7 @@ isnow(endp, monthp, dayp, varp)
fprintf(stderr, "\nday: %d %s month %d\n", day, endp, month);
#endif
- *varp = 1;
+ varp = 1;
/* variable weekday, SundayLast, MondayFirst ... */
if (day < 0 || day >= 10) {
@@ -356,36 +355,45 @@ isnow(endp, monthp, dayp, varp)
#endif
}
}
+ else
+ day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
}
if (!(flags & F_EASTER)) {
- *monthp = month;
- *dayp = day;
+ monthp = month;
+ dayp = day;
day = cumdays[month] + day;
}
else {
for (v1 = 0; day > cumdays[v1]; v1++)
;
- *monthp = v1 - 1;
- *dayp = day - cumdays[v1 - 1];
- *varp = 1;
+ monthp = v1 - 1;
+ dayp = day - cumdays[v1 - 1];
+ varp = 1;
}
#if DEBUG
- fprintf(stderr, "day2: day %d(%d) yday %d\n", *dayp, day, tp->tm_yday);
+ fprintf(stderr, "day2: day %d(%d) yday %d\n", dayp, day, tp->tm_yday);
#endif
/* if today or today + offset days */
- if (day >= tp->tm_yday - f_dayBefore &&
- day <= tp->tm_yday + offset + f_dayAfter)
- return (1);
+ if ((day >= tp->tm_yday - f_dayBefore &&
+ day <= tp->tm_yday + offset + f_dayAfter) ||
/* if number of days left in this year + days to event in next year */
- if (yrdays - tp->tm_yday + day <= offset + f_dayAfter ||
+ (yrdays - tp->tm_yday + day <= offset + f_dayAfter ||
/* a year backward, eg. 6 Jan and 10 days before -> 27. Dec */
tp->tm_yday + day - f_dayBefore < 0
- )
- return (1);
- return (0);
+ )) {
+ if ((matches = malloc(sizeof(struct match))) == NULL)
+ errx(1,"cannot allocate memory");
+ matches->month = monthp;
+ matches->day = dayp;
+ matches->var = varp;
+ matches->year = tp->tm_year; /* XXX */
+ matches->next = NULL;
+ return (matches);
+ }
+ return (NULL);
}
diff --git a/usr.bin/calendar/io.c b/usr.bin/calendar/io.c
index 22cc9a1a166..8f238772b23 100644
--- a/usr.bin/calendar/io.c
+++ b/usr.bin/calendar/io.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: io.c,v 1.4 1998/03/30 06:59:27 deraadt Exp $ */
+/* $OpenBSD: io.c,v 1.5 1998/11/08 04:31:13 pjanzen Exp $ */
/*
* Copyright (c) 1989, 1993, 1994
@@ -43,7 +43,7 @@ static const char copyright[] =
#if 0
static const char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94";
#else
-static char rcsid[] = "$OpenBSD: io.c,v 1.4 1998/03/30 06:59:27 deraadt Exp $";
+static char rcsid[] = "$OpenBSD: io.c,v 1.5 1998/11/08 04:31:13 pjanzen Exp $";
#endif
#endif /* not lint */
@@ -93,11 +93,13 @@ cal()
register char *p;
FILE *fp;
int ch, l;
- int month;
- int day;
int var;
char buf[2048 + 1];
+ struct event *events, *cur_evt, *tmp;
+ struct match *m;
+ events = NULL;
+ cur_evt = NULL;
if ((fp = opencal()) == NULL)
return;
for (printing = 0; fgets(buf, sizeof(buf), stdin) != NULL;) {
@@ -134,34 +136,73 @@ cal()
continue;
}
if (buf[0] != '\t') {
- printing = isnow(buf, &month, &day, &var) ? 1 : 0;
+ printing = (m = isnow(buf)) ? 1 : 0;
if ((p = strchr(buf, '\t')) == NULL)
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;
+ char *dsc;
+
+ dsc = 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_hour = 12; /* unused */
tm.tm_wday = 0; /* unused */
- tm.tm_mon = month - 1;
- tm.tm_mday = day;
- tm.tm_year = tp->tm_year; /* unused */
+ tm.tm_mon = m->month - 1;
+ tm.tm_mday = m->day;
+ tm.tm_year = m->year;
tm.tm_isdst = tp->tm_isdst; /* unused */
tm.tm_gmtoff = tp->tm_gmtoff; /* unused */
tm.tm_zone = tp->tm_zone; /* unused */
- (void)strftime(dbuf, sizeof(dbuf), "%a %b %d",
- &tm);
- (void)fprintf(fp, "%s%c%s\n",
- dbuf + 4/* skip weekdays */,
- var ? '*' : ' ', p);
+ (void)strftime(cur_evt->print_date,
+ sizeof(cur_evt->print_date) - 1,
+ /* "%a %b %d", &tm); Skip weekdays */
+ "%b %d", &tm);
+ strcat(cur_evt->print_date,
+ (var + m->var) ? "*" : " ");
+ cur_evt->when = mktime(&tm);
+ if (dsc)
+ cur_evt->desc = dsc;
+ else {
+ if ((cur_evt->desc = strdup(p)) == NULL)
+ errx(1, "cannot allocate memory");
+ dsc = cur_evt->desc;
+ }
+ insert(&events, cur_evt);
+ foo = m;
+ m = m->next;
+ free(foo);
+ }
}
}
- else if (printing)
- fprintf(fp, "%s\n", buf);
+ else if (printing) {
+ if ((cur_evt->desc = realloc(cur_evt->desc,
+ (2 + strlen(cur_evt->desc) + strlen(buf)))) == NULL)
+ errx(1, "cannot allocate memory");
+ strcat(cur_evt->desc, "\n");
+ strcat(cur_evt->desc, buf);
+ }
+ }
+ tmp = events;
+ while (tmp) {
+ (void)fprintf(fp, "%s%s\n", tmp->print_date, tmp->desc);
+ /* Can't free descriptions since they may be shared */
+ (void)realloc(tmp->desc, 0);
+ events = tmp;
+ tmp = tmp->next;
+ free(events);
}
closecal(fp);
}
@@ -345,3 +386,36 @@ 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;
+ }
+}