style(9)ify usr.bin/calendar
PR: bin/118644 Approved by: bde@ (mentor) MFC after: 1 week
This commit is contained in:
parent
7f05f04afd
commit
35304a1641
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=181322
@ -58,22 +58,22 @@ __FBSDID("$FreeBSD$");
|
||||
#include "pathnames.h"
|
||||
#include "calendar.h"
|
||||
|
||||
struct passwd *pw;
|
||||
int doall = 0;
|
||||
time_t f_time = 0;
|
||||
struct passwd *pw;
|
||||
int doall = 0;
|
||||
time_t f_time = 0;
|
||||
|
||||
int f_dayAfter = 0; /* days after current date */
|
||||
int f_dayBefore = 0; /* days before current date */
|
||||
int Friday = 5; /* day before weekend */
|
||||
int f_dayAfter = 0; /* days after current date */
|
||||
int f_dayBefore = 0; /* days before current date */
|
||||
int Friday = 5; /* day before weekend */
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int ch;
|
||||
|
||||
(void) setlocale(LC_ALL, "");
|
||||
(void)setlocale(LC_ALL, "");
|
||||
|
||||
while ((ch = getopt(argc, argv, "-af:t:A:B:F:W:")) != -1)
|
||||
while ((ch = getopt(argc, argv, "-A:aB:F:f:t:W:")) != -1)
|
||||
switch (ch) {
|
||||
case '-': /* backward contemptible */
|
||||
case 'a':
|
||||
@ -84,25 +84,24 @@ main(int argc, char *argv[])
|
||||
doall = 1;
|
||||
break;
|
||||
|
||||
|
||||
case 'f': /* other calendar file */
|
||||
calendarFile = optarg;
|
||||
calendarFile = optarg;
|
||||
break;
|
||||
|
||||
case 't': /* other date, undocumented, for tests */
|
||||
f_time = Mktime (optarg);
|
||||
f_time = Mktime(optarg);
|
||||
break;
|
||||
|
||||
case 'W': /* we don't need no steenking Fridays */
|
||||
Friday = -1;
|
||||
|
||||
/* FALLTHROUGH */
|
||||
/* FALLTHROUGH */
|
||||
case 'A': /* days after current date */
|
||||
f_dayAfter = atoi(optarg);
|
||||
break;
|
||||
|
||||
case 'B': /* days before current date */
|
||||
f_dayBefore = atoi(optarg);
|
||||
f_dayBefore = atoi(optarg);
|
||||
break;
|
||||
|
||||
case 'F':
|
||||
@ -113,6 +112,7 @@ main(int argc, char *argv[])
|
||||
default:
|
||||
usage();
|
||||
}
|
||||
|
||||
argc -= optind;
|
||||
argv += optind;
|
||||
|
||||
@ -120,11 +120,11 @@ main(int argc, char *argv[])
|
||||
usage();
|
||||
|
||||
/* use current time */
|
||||
if (f_time <= 0)
|
||||
(void)time(&f_time);
|
||||
if (f_time <= 0)
|
||||
(void)time(&f_time);
|
||||
|
||||
settime(f_time);
|
||||
|
||||
|
||||
if (doall)
|
||||
while ((pw = getpwent()) != NULL) {
|
||||
(void)setegid(pw->pw_gid);
|
||||
@ -143,11 +143,10 @@ main(int argc, char *argv[])
|
||||
void
|
||||
usage(void)
|
||||
{
|
||||
(void)fprintf(stderr, "%s\n%s\n",
|
||||
|
||||
fprintf(stderr, "%s\n%s\n",
|
||||
"usage: calendar [-a] [-A days] [-B days] [-F friday] "
|
||||
"[-f calendarfile]",
|
||||
" [-t dd[.mm[.year]]] [-W days]");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
|
@ -45,49 +45,53 @@ extern int *cumdays;
|
||||
extern int yrdays;
|
||||
extern struct fixs neaster, npaskha;
|
||||
|
||||
void cal(void);
|
||||
void closecal(FILE *);
|
||||
int getday(char *);
|
||||
int getdayvar(char *);
|
||||
int getfield(char *, char **, int *);
|
||||
int getmonth(char *);
|
||||
int geteaster(char *, int);
|
||||
int getpaskha(char *, int);
|
||||
int easter(int);
|
||||
int isnow(char *, int *, int *, int *);
|
||||
void cal(void);
|
||||
void closecal(FILE *);
|
||||
int getday(char *);
|
||||
int getdayvar(char *);
|
||||
int getfield(char *, char **, int *);
|
||||
int getmonth(char *);
|
||||
int geteaster(char *, int);
|
||||
int getpaskha(char *, int);
|
||||
int easter(int);
|
||||
int isnow(char *, int *, int *, int *);
|
||||
FILE *opencal(void);
|
||||
void settime(time_t);
|
||||
time_t Mktime(char *);
|
||||
void usage(void);
|
||||
void setnnames(void);
|
||||
void settime(time_t);
|
||||
time_t Mktime(char *);
|
||||
void usage(void);
|
||||
void setnnames(void);
|
||||
|
||||
#define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
|
||||
|
||||
/* some flags */
|
||||
#define F_ISMONTH 0x01 /* month (Januar ...) */
|
||||
#define F_ISDAY 0x02 /* day of week (Sun, Mon, ...) */
|
||||
#define F_ISDAYVAR 0x04 /* variables day of week, like SundayLast */
|
||||
#define F_EASTER 0x08 /* Easter or easter depending days */
|
||||
#define F_ISMONTH 0x01 /* month (January ...) */
|
||||
#define F_ISDAY 0x02 /* day of week (Sun, Mon, ...) */
|
||||
#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_dayBefore; /* days bevore current date */
|
||||
extern int Friday; /* day before weekend */
|
||||
extern int f_dayAfter; /* days after current date */
|
||||
extern int f_dayBefore; /* days before current date */
|
||||
extern int Friday; /* day before weekend */
|
||||
|
||||
struct fixs {
|
||||
char *name;
|
||||
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 */
|
||||
/*
|
||||
* Event sorting related functions:
|
||||
* - Use event_add() to create a new event
|
||||
* - Use event_continue() to add more text to the last added event
|
||||
* - Use event_print_all() to display them in time chronological order
|
||||
*/
|
||||
struct event *event_add(struct event *, int, int, char *, int, char *);
|
||||
void event_continue(struct event *events, char *txt);
|
||||
void event_print_all(FILE *fp, struct event *events);
|
||||
struct event {
|
||||
int month;
|
||||
int day;
|
||||
int var;
|
||||
char *date;
|
||||
char *text;
|
||||
int month;
|
||||
int day;
|
||||
int var;
|
||||
char *date;
|
||||
char *text;
|
||||
struct event *next;
|
||||
};
|
||||
|
||||
struct fixs {
|
||||
char *name;
|
||||
int len;
|
||||
};
|
||||
|
@ -47,16 +47,16 @@ __FBSDID("$FreeBSD$");
|
||||
#include "pathnames.h"
|
||||
#include "calendar.h"
|
||||
|
||||
struct tm *tp;
|
||||
static const struct tm tm0;
|
||||
int *cumdays, yrdays;
|
||||
char dayname[10];
|
||||
struct tm *tp;
|
||||
static const struct tm tm0;
|
||||
int *cumdays, yrdays;
|
||||
char dayname[10];
|
||||
|
||||
|
||||
/* 1-based month, 0-based days, cumulative */
|
||||
int daytab[][14] = {
|
||||
{ 0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 },
|
||||
{ 0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
|
||||
int daytab[][14] = {
|
||||
{0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364},
|
||||
{0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
|
||||
};
|
||||
|
||||
static char const *days[] = {
|
||||
@ -68,11 +68,11 @@ static const char *months[] = {
|
||||
"jul", "aug", "sep", "oct", "nov", "dec", NULL,
|
||||
};
|
||||
|
||||
static struct fixs fndays[8]; /* full national days names */
|
||||
static struct fixs ndays[8]; /* short national days names */
|
||||
static struct fixs fndays[8]; /* full national days names */
|
||||
static struct fixs ndays[8]; /* short national days names */
|
||||
|
||||
static struct fixs fnmonths[13]; /* full national months names */
|
||||
static struct fixs nmonths[13]; /* short national month names */
|
||||
static struct fixs fnmonths[13]; /* full national months names */
|
||||
static struct fixs nmonths[13]; /* short national month names */
|
||||
|
||||
|
||||
void
|
||||
@ -143,7 +143,7 @@ settime(time_t now)
|
||||
char *oldl, *lbufp;
|
||||
|
||||
tp = localtime(&now);
|
||||
if ( isleap(tp->tm_year + 1900) ) {
|
||||
if (isleap(tp->tm_year + 1900)) {
|
||||
yrdays = 366;
|
||||
cumdays = daytab[1];
|
||||
} else {
|
||||
@ -159,9 +159,9 @@ settime(time_t now)
|
||||
lbufp = setlocale(LC_TIME, NULL);
|
||||
if (lbufp != NULL && (oldl = strdup(lbufp)) == NULL)
|
||||
errx(1, "cannot allocate memory");
|
||||
(void) setlocale(LC_TIME, "C");
|
||||
(void)setlocale(LC_TIME, "C");
|
||||
header[5].iov_len = strftime(dayname, sizeof(dayname), "%A", tp);
|
||||
(void) setlocale(LC_TIME, (oldl != NULL ? oldl : ""));
|
||||
(void)setlocale(LC_TIME, (oldl != NULL ? oldl : ""));
|
||||
if (oldl != NULL)
|
||||
free(oldl);
|
||||
|
||||
@ -172,38 +172,38 @@ settime(time_t now)
|
||||
* Day: two digits, Month: two digits, Year: digits
|
||||
*/
|
||||
time_t
|
||||
Mktime (char *dp)
|
||||
Mktime(char *dp)
|
||||
{
|
||||
time_t t;
|
||||
int d, m, y;
|
||||
struct tm tm;
|
||||
time_t t;
|
||||
int d, m, y;
|
||||
struct tm tm;
|
||||
|
||||
(void)time(&t);
|
||||
tp = localtime(&t);
|
||||
(void)time(&t);
|
||||
tp = localtime(&t);
|
||||
|
||||
tm = tm0;
|
||||
tm.tm_mday = tp->tm_mday;
|
||||
tm.tm_mon = tp->tm_mon;
|
||||
tm.tm_year = tp->tm_year;
|
||||
tm = tm0;
|
||||
tm.tm_mday = tp->tm_mday;
|
||||
tm.tm_mon = tp->tm_mon;
|
||||
tm.tm_year = tp->tm_year;
|
||||
|
||||
switch (sscanf(dp, "%d.%d.%d", &d, &m, &y)) {
|
||||
case 3:
|
||||
if (y > 1900)
|
||||
y -= 1900;
|
||||
tm.tm_year = y;
|
||||
/* FALLTHROUGH */
|
||||
case 2:
|
||||
tm.tm_mon = m - 1;
|
||||
/* FALLTHROUGH */
|
||||
case 1:
|
||||
tm.tm_mday = d;
|
||||
}
|
||||
switch (sscanf(dp, "%d.%d.%d", &d, &m, &y)) {
|
||||
case 3:
|
||||
if (y > 1900)
|
||||
y -= 1900;
|
||||
tm.tm_year = y;
|
||||
/* FALLTHROUGH */
|
||||
case 2:
|
||||
tm.tm_mon = m - 1;
|
||||
/* FALLTHROUGH */
|
||||
case 1:
|
||||
tm.tm_mday = d;
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "Mktime: %d %d %s\n", (int)mktime(&tm), (int)t,
|
||||
asctime(&tm));
|
||||
fprintf(stderr, "Mktime: %d %d %s\n",
|
||||
(int)mktime(&tm), (int)t, asctime(&tm));
|
||||
#endif
|
||||
return(mktime(&tm));
|
||||
return (mktime(&tm));
|
||||
}
|
||||
|
||||
/*
|
||||
@ -240,7 +240,7 @@ isnow(char *endp, int *monthp, int *dayp, int *varp)
|
||||
|
||||
/* Easter or Easter depending days */
|
||||
if (flags & F_EASTER)
|
||||
day = v1 - 1; /* days since January 1 [0-365] */
|
||||
day = v1 - 1; /* days since January 1 [0-365] */
|
||||
|
||||
/*
|
||||
* 1. {Weekday,Day} XYZ ...
|
||||
@ -299,85 +299,86 @@ isnow(char *endp, int *monthp, int *dayp, int *varp)
|
||||
*/
|
||||
if (flags & F_ISDAY) {
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "\nday: %d %s month %d\n", day, endp, month);
|
||||
fprintf(stderr, "\nday: %d %s month %d\n", day, endp, month);
|
||||
#endif
|
||||
|
||||
*varp = 1;
|
||||
/* variable weekday, SundayLast, MondayFirst ... */
|
||||
if (day < 0 || day >= 10) {
|
||||
|
||||
/* negative offset; last, -4 .. -1 */
|
||||
if (day < 0) {
|
||||
v1 = day/10 - 1; /* offset -4 ... -1 */
|
||||
day = 10 + (day % 10); /* day 1 ... 7 */
|
||||
|
||||
/* day, eg '22nd' */
|
||||
v2 = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
|
||||
|
||||
/* (month length - day) / 7 + 1 */
|
||||
if (cumdays[month+1] - cumdays[month] >= v2
|
||||
&& ((int)((cumdays[month+1] -
|
||||
cumdays[month] - v2) / 7) + 1) == -v1)
|
||||
/* bingo ! */
|
||||
day = v2;
|
||||
|
||||
/* set to yesterday */
|
||||
else {
|
||||
day = tp->tm_mday - 1;
|
||||
if (day == 0)
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
|
||||
/* first, second ... +1 ... +5 */
|
||||
else {
|
||||
v1 = day/10; /* offset: +1 (first Sunday) ... */
|
||||
day = day % 10;
|
||||
|
||||
/* day, eg '22th' */
|
||||
v2 = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
|
||||
|
||||
/* Hurrah! matched */
|
||||
if ( ((v2 - 1 + 7) / 7) == v1 )
|
||||
day = v2;
|
||||
|
||||
/* set to yesterday */
|
||||
else {
|
||||
day = tp->tm_mday - 1;
|
||||
if (day == 0)
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* wired */
|
||||
else {
|
||||
day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
|
||||
*varp = 1;
|
||||
}
|
||||
/* variable weekday, SundayLast, MondayFirst ... */
|
||||
if (day < 0 || day >= 10) {
|
||||
|
||||
/* negative offset; last, -4 .. -1 */
|
||||
if (day < 0) {
|
||||
v1 = day / 10 - 1; /* offset -4 ... -1 */
|
||||
day = 10 + (day % 10); /* day 1 ... 7 */
|
||||
|
||||
/* day, eg '22nd' */
|
||||
v2 = tp->tm_mday +
|
||||
(((day - 1) - tp->tm_wday + 7) % 7);
|
||||
|
||||
/* (month length - day) / 7 + 1 */
|
||||
if (cumdays[month + 1] - cumdays[month] >= v2
|
||||
&& ((int)((cumdays[month + 1] -
|
||||
cumdays[month] - v2) / 7) + 1) == -v1)
|
||||
day = v2; /* bingo ! */
|
||||
|
||||
/* set to yesterday */
|
||||
else {
|
||||
day = tp->tm_mday - 1;
|
||||
if (day == 0)
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
|
||||
/* first, second ... +1 ... +5 */
|
||||
else {
|
||||
/* offset: +1 (first Sunday) ... */
|
||||
v1 = day / 10;
|
||||
day = day % 10;
|
||||
|
||||
/* day, eg '22th' */
|
||||
v2 = tp->tm_mday +
|
||||
(((day - 1) - tp->tm_wday + 7) % 7);
|
||||
|
||||
/* Hurrah! matched */
|
||||
if (((v2 - 1 + 7) / 7) == v1 )
|
||||
day = v2;
|
||||
|
||||
else {
|
||||
/* set to yesterday */
|
||||
day = tp->tm_mday - 1;
|
||||
if (day == 0)
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/* wired */
|
||||
day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
|
||||
*varp = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (!(flags & F_EASTER)) {
|
||||
if (day + cumdays[month] > cumdays[month + 1]) { /* off end of month */
|
||||
day -= (cumdays[month + 1] - cumdays[month]); /* adjust */
|
||||
if (++month > 12) /* next year */
|
||||
month = 1;
|
||||
}
|
||||
*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;
|
||||
if (day + cumdays[month] > cumdays[month + 1]) {
|
||||
/* off end of month, adjust */
|
||||
day -= (cumdays[month + 1] - cumdays[month]);
|
||||
/* next year */
|
||||
if (++month > 12)
|
||||
month = 1;
|
||||
}
|
||||
*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;
|
||||
}
|
||||
|
||||
#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 days before or days after is specified */
|
||||
@ -395,7 +396,7 @@ isnow(char *endp, int *monthp, int *dayp, int *varp)
|
||||
|
||||
/* previous year */
|
||||
if (tp->tm_yday - f_dayBefore < 0) {
|
||||
int before = yrdays + (tp->tm_yday - f_dayBefore );
|
||||
int before = yrdays + (tp->tm_yday - f_dayBefore);
|
||||
if (day >= before)
|
||||
return (1);
|
||||
}
|
||||
@ -451,39 +452,35 @@ getdayvar(char *s)
|
||||
{
|
||||
int offs;
|
||||
|
||||
|
||||
offs = strlen(s);
|
||||
|
||||
|
||||
/* Sun+1 or Wednesday-2
|
||||
* ^ ^ */
|
||||
|
||||
/* fprintf(stderr, "x: %s %s %d\n", s, s + offs - 2, offs); */
|
||||
switch(*(s + offs - 2)) {
|
||||
switch (*(s + offs - 2)) {
|
||||
case '-':
|
||||
return(-(atoi(s + offs - 1)));
|
||||
return (-(atoi(s + offs - 1)));
|
||||
case '+':
|
||||
return(atoi(s + offs - 1));
|
||||
return (atoi(s + offs - 1));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* some aliases: last, first, second, third, fourth
|
||||
*/
|
||||
|
||||
/* last */
|
||||
if (offs > 4 && !strcasecmp(s + offs - 4, "last"))
|
||||
return(-1);
|
||||
return (-1);
|
||||
else if (offs > 5 && !strcasecmp(s + offs - 5, "first"))
|
||||
return(+1);
|
||||
return (+1);
|
||||
else if (offs > 6 && !strcasecmp(s + offs - 6, "second"))
|
||||
return(+2);
|
||||
return (+2);
|
||||
else if (offs > 5 && !strcasecmp(s + offs - 5, "third"))
|
||||
return(+3);
|
||||
return (+3);
|
||||
else if (offs > 6 && !strcasecmp(s + offs - 6, "fourth"))
|
||||
return(+4);
|
||||
|
||||
return (+4);
|
||||
|
||||
/* no offset detected */
|
||||
return(0);
|
||||
return (0);
|
||||
}
|
||||
|
@ -67,9 +67,11 @@ __FBSDID("$FreeBSD$");
|
||||
#include "calendar.h"
|
||||
|
||||
|
||||
const char *calendarFile = "calendar"; /* default calendar file */
|
||||
const char *calendarHomes[] = { ".calendar", _PATH_INCLUDE }; /* HOME */
|
||||
const char *calendarNoMail = "nomail"; /* don't sent mail if this file exist */
|
||||
const char *calendarFile = "calendar"; /* default calendar file */
|
||||
const char *calendarHomes[] = {".calendar", _PATH_INCLUDE}; /* HOME */
|
||||
const char *calendarNoMail = "nomail"; /* don't sent mail if this file exist */
|
||||
|
||||
char path[MAXPATHLEN];
|
||||
|
||||
struct fixs neaster, npaskha;
|
||||
|
||||
@ -80,10 +82,9 @@ struct iovec header[] = {
|
||||
{NULL, 0},
|
||||
{"\nSubject: ", 10},
|
||||
{NULL, 0},
|
||||
{"'s Calendar\nPrecedence: bulk\n\n", 30},
|
||||
{"'s Calendar\nPrecedence: bulk\n\n", 30},
|
||||
};
|
||||
|
||||
|
||||
void
|
||||
cal(void)
|
||||
{
|
||||
@ -113,7 +114,7 @@ cal(void)
|
||||
if (buf[0] == '\0')
|
||||
continue;
|
||||
if (strncmp(buf, "LANG=", 5) == 0) {
|
||||
(void) setlocale(LC_ALL, buf + 5);
|
||||
(void)setlocale(LC_ALL, buf + 5);
|
||||
d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
|
||||
setnnames();
|
||||
continue;
|
||||
@ -145,36 +146,43 @@ cal(void)
|
||||
char dbuf[80];
|
||||
|
||||
if (d_first < 0)
|
||||
d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
|
||||
tm.tm_sec = 0; /* unused */
|
||||
tm.tm_min = 0; /* unused */
|
||||
tm.tm_hour = 0; /* unused */
|
||||
tm.tm_wday = 0; /* unused */
|
||||
d_first =
|
||||
(*nl_langinfo(D_MD_ORDER) == 'd');
|
||||
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),
|
||||
d_first ? "%e %b" : "%b %e",
|
||||
&tm);
|
||||
events = event_add(events, month, day, dbuf, var, p);
|
||||
d_first ? "%e %b" : "%b %e", &tm);
|
||||
events = event_add(events, month, day, dbuf,
|
||||
var, p);
|
||||
}
|
||||
} else {
|
||||
if (printing)
|
||||
event_continue(events, buf);
|
||||
}
|
||||
else if (printing)
|
||||
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)
|
||||
event_add(struct event *events, int month, int day,
|
||||
char *date, int var, char *txt)
|
||||
{
|
||||
struct event *e;
|
||||
|
||||
/*
|
||||
* Creating a new event:
|
||||
* - Create a new event
|
||||
* - Copy the machine readable day and month
|
||||
* - Copy the human readable and language specific date
|
||||
* - Copy the text of the event
|
||||
*/
|
||||
e = (struct event *)calloc(1, sizeof(struct event));
|
||||
if (e == NULL)
|
||||
errx(1, "event_add: cannot allocate memory");
|
||||
@ -197,6 +205,13 @@ event_continue(struct event *e, char *txt)
|
||||
{
|
||||
char *text;
|
||||
|
||||
/*
|
||||
* Adding text to the event:
|
||||
* - Save a copy of the old text (unknown length, so strdup())
|
||||
* - Allocate enough space for old text + \n + new text + 0
|
||||
* - Store the old text + \n + new text
|
||||
* - Destroy the saved copy.
|
||||
*/
|
||||
text = strdup(e->text);
|
||||
if (text == NULL)
|
||||
errx(1, "event_continue: cannot allocate memory");
|
||||
@ -217,15 +232,30 @@ 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++) {
|
||||
/*
|
||||
* Print all events:
|
||||
* - We know the number of days to be counted (f_dayAfter + f_dayBefore)
|
||||
* - We know the current day of the year ("now" - f_dayBefore + counter)
|
||||
* - We know the number of days in the year (yrdays, set in settime())
|
||||
* - So we know the date on which the current daycounter is on the
|
||||
* calendar in days and months.
|
||||
* - Go through the list of events, and print all matching dates
|
||||
*/
|
||||
for (daycounter = 0; daycounter <= f_dayAfter + f_dayBefore;
|
||||
daycounter++) {
|
||||
day = tp->tm_yday - f_dayBefore + daycounter;
|
||||
if (day < 0) day += yrdays;
|
||||
if (day >= yrdays) day -= yrdays;
|
||||
if (day < 0)
|
||||
day += yrdays;
|
||||
if (day >= yrdays)
|
||||
day -= yrdays;
|
||||
|
||||
/*
|
||||
* When we know the day of the year, we can determine the day
|
||||
* of the month and the month.
|
||||
*/
|
||||
month = 1;
|
||||
while (month <= 12) {
|
||||
if (day <= cumdays[month])
|
||||
@ -236,10 +266,15 @@ event_print_all(FILE *fp, struct event *events)
|
||||
day -= cumdays[month];
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr,"event_print_allmonth: %d, day: %d\n",month,day);
|
||||
fprintf(stderr, "event_print_allmonth: %d, day: %d\n",
|
||||
month, day);
|
||||
#endif
|
||||
|
||||
for (e = events; e != NULL; e = e_next ) {
|
||||
/*
|
||||
* Go through all events and print the text of the matching
|
||||
* dates
|
||||
*/
|
||||
for (e = events; e != NULL; e = e_next) {
|
||||
e_next = e->next;
|
||||
|
||||
if (month != e->month || day != e->day)
|
||||
@ -258,10 +293,11 @@ getfield(char *p, char **endp, int *flags)
|
||||
char *start, savech;
|
||||
|
||||
for (; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p)
|
||||
&& *p != '*'; ++p);
|
||||
&& *p != '*'; ++p)
|
||||
;
|
||||
if (*p == '*') { /* `*' is current month */
|
||||
*flags |= F_ISMONTH;
|
||||
*endp = p+1;
|
||||
*endp = p + 1;
|
||||
return (tp->tm_mon + 1);
|
||||
}
|
||||
if (isdigit((unsigned char)*p)) {
|
||||
@ -275,7 +311,8 @@ getfield(char *p, char **endp, int *flags)
|
||||
|
||||
/* Sunday-1 */
|
||||
if (*p == '+' || *p == '-')
|
||||
for(; isdigit((unsigned char)*++p););
|
||||
for(; isdigit((unsigned char)*++p);)
|
||||
;
|
||||
|
||||
savech = *p;
|
||||
*p = '\0';
|
||||
@ -286,25 +323,25 @@ getfield(char *p, char **endp, int *flags)
|
||||
|
||||
/* Day */
|
||||
else if ((val = getday(start)) != 0) {
|
||||
*flags |= F_ISDAY;
|
||||
*flags |= F_ISDAY;
|
||||
|
||||
/* variable weekday */
|
||||
if ((var = getdayvar(start)) != 0) {
|
||||
if (var <=5 && var >= -4)
|
||||
val += var * 10;
|
||||
/* variable weekday */
|
||||
if ((var = getdayvar(start)) != 0) {
|
||||
if (var <= 5 && var >= -4)
|
||||
val += var * 10;
|
||||
#ifdef DEBUG
|
||||
printf("var: %d\n", var);
|
||||
printf("var: %d\n", var);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Easter */
|
||||
else if ((val = geteaster(start, tp->tm_year + 1900)) != 0)
|
||||
*flags |= F_EASTER;
|
||||
*flags |= F_EASTER;
|
||||
|
||||
/* Paskha */
|
||||
else if ((val = getpaskha(start, tp->tm_year + 1900)) != 0)
|
||||
*flags |= F_EASTER;
|
||||
*flags |= F_EASTER;
|
||||
|
||||
/* undefined rest */
|
||||
else {
|
||||
@ -312,13 +349,12 @@ getfield(char *p, char **endp, int *flags)
|
||||
return (0);
|
||||
}
|
||||
for (*p = savech; !isdigit((unsigned char)*p)
|
||||
&& !isalpha((unsigned char)*p) && *p != '*'; ++p);
|
||||
&& !isalpha((unsigned char)*p) && *p != '*'; ++p)
|
||||
;
|
||||
*endp = p;
|
||||
return (val);
|
||||
}
|
||||
|
||||
char path[MAXPATHLEN];
|
||||
|
||||
FILE *
|
||||
opencal(void)
|
||||
{
|
||||
@ -330,12 +366,12 @@ opencal(void)
|
||||
/* open up calendar file as stdin */
|
||||
if (!freopen(calendarFile, "r", stdin)) {
|
||||
if (doall) {
|
||||
if (chdir(calendarHomes[0]) != 0)
|
||||
return (NULL);
|
||||
if (stat(calendarNoMail, &sbuf) == 0)
|
||||
return (NULL);
|
||||
if (!freopen(calendarFile, "r", stdin))
|
||||
return (NULL);
|
||||
if (chdir(calendarHomes[0]) != 0)
|
||||
return (NULL);
|
||||
if (stat(calendarNoMail, &sbuf) == 0)
|
||||
return (NULL);
|
||||
if (!freopen(calendarFile, "r", stdin))
|
||||
return (NULL);
|
||||
} else {
|
||||
char *home = getenv("HOME");
|
||||
if (home == NULL || *home == '\0')
|
||||
@ -343,14 +379,15 @@ opencal(void)
|
||||
chdir(home);
|
||||
for (found = i = 0; i < sizeof(calendarHomes) /
|
||||
sizeof(calendarHomes[0]); i++)
|
||||
if (chdir(calendarHomes[i]) == 0 &&
|
||||
freopen(calendarFile, "r", stdin)) {
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
if (chdir(calendarHomes[i]) == 0 &&
|
||||
freopen(calendarFile, "r", stdin)) {
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
if (!found)
|
||||
errx(1, "can't open calendar file \"%s\": %s (%d)",
|
||||
calendarFile, strerror(errno), errno);
|
||||
errx(1,
|
||||
"can't open calendar file \"%s\": %s (%d)",
|
||||
calendarFile, strerror(errno), errno);
|
||||
}
|
||||
}
|
||||
if (pipe(pdes) < 0)
|
||||
|
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 1996 Wolfram Schneider <wosch@FreeBSD.org>. Berlin.
|
||||
* All rights reserved.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
@ -46,28 +46,28 @@ __FBSDID("$FreeBSD$");
|
||||
int
|
||||
easter(int year) /* 0 ... abcd, NOT since 1900 */
|
||||
{
|
||||
int G, /* Golden number - 1 */
|
||||
C, /* Century */
|
||||
H, /* 23 - epact % 30 */
|
||||
I, /* days from 21 March to Paschal full moon */
|
||||
J, /* weekday of full moon */
|
||||
L; /* days from 21 March to Sunday on of before full moon */
|
||||
int G, /* Golden number - 1 */
|
||||
C, /* Century */
|
||||
H, /* 23 - epact % 30 */
|
||||
I, /* days from 21 March to Paschal full moon */
|
||||
J, /* weekday of full moon */
|
||||
L; /* days from 21 March to Sunday on of before full moon */
|
||||
|
||||
G = year % 19;
|
||||
C = year / 100;
|
||||
H = (C - C/4 - (8*C+13)/25 + 19*G + 15) % 30;
|
||||
I = H - (H/28)*(1 - (H/28)*(29/(H + 1))*((21 - G)/11));
|
||||
J = (year + year/4 + I + 2 - C + C/4) % 7;
|
||||
G = year % 19;
|
||||
C = year / 100;
|
||||
H = (C - C / 4 - (8 * C + 13) / 25 + 19 * G + 15) % 30;
|
||||
I = H - (H / 28) * (1 - (H / 28) * (29 / (H + 1)) * ((21 - G) / 11));
|
||||
J = (year + year / 4 + I + 2 - C + C / 4) % 7;
|
||||
|
||||
L = I - J;
|
||||
L = I - J;
|
||||
|
||||
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
|
||||
return 31 + 29 + 21 + L + 7;
|
||||
else
|
||||
return 31 + 28 + 21 + L + 7;
|
||||
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
|
||||
return 31 + 29 + 21 + L + 7;
|
||||
else
|
||||
return 31 + 28 + 21 + L + 7;
|
||||
}
|
||||
|
||||
/* return year day for Easter or easter depending days
|
||||
/* return year day for Easter or easter depending days
|
||||
* Match: Easter([+-][0-9]+)?
|
||||
* e.g: Easter-2 is Good Friday (2 days before Easter)
|
||||
*/
|
||||
@ -77,17 +77,16 @@ geteaster(char *s, int year)
|
||||
{
|
||||
int offset = 0;
|
||||
|
||||
#define EASTER "easter"
|
||||
#define EASTERNAMELEN (sizeof(EASTER) - 1)
|
||||
#define EASTER "easter"
|
||||
#define EASTERNAMELEN (sizeof(EASTER) - 1)
|
||||
|
||||
if (strncasecmp(s, EASTER, EASTERNAMELEN) == 0)
|
||||
s += EASTERNAMELEN;
|
||||
else if ( neaster.name != NULL
|
||||
&& strncasecmp(s, neaster.name, neaster.len) == 0
|
||||
)
|
||||
s += neaster.len;
|
||||
s += EASTERNAMELEN;
|
||||
else if (neaster.name != NULL
|
||||
&& strncasecmp(s, neaster.name, neaster.len) == 0)
|
||||
s += neaster.len;
|
||||
else
|
||||
return(0);
|
||||
return (0);
|
||||
|
||||
#if DEBUG
|
||||
printf("%s %d %d\n", s, year, EASTERNAMELEN);
|
||||
@ -96,16 +95,16 @@ geteaster(char *s, int year)
|
||||
/* Easter+1 or Easter-2
|
||||
* ^ ^ */
|
||||
|
||||
switch(*s) {
|
||||
switch (*s) {
|
||||
|
||||
case '-':
|
||||
case '+':
|
||||
offset = atoi(s);
|
||||
break;
|
||||
offset = atoi(s);
|
||||
break;
|
||||
|
||||
default:
|
||||
offset = 0;
|
||||
offset = 0;
|
||||
}
|
||||
|
||||
|
||||
return (easter(year) + offset);
|
||||
}
|
||||
|
@ -34,10 +34,10 @@ __FBSDID("$FreeBSD$");
|
||||
|
||||
#include "calendar.h"
|
||||
|
||||
#define PASKHA "paskha"
|
||||
#define PASKHALEN (sizeof(PASKHA) - 1)
|
||||
#define PASKHA "paskha"
|
||||
#define PASKHALEN (sizeof(PASKHA) - 1)
|
||||
|
||||
static int paskha (int);
|
||||
static int paskha(int);
|
||||
|
||||
/* return year day for Orthodox Easter using Gauss formula */
|
||||
/* (old style result) */
|
||||
@ -52,8 +52,8 @@ paskha(int R) /*year*/
|
||||
a = R % 19;
|
||||
b = R % 4;
|
||||
c = R % 7;
|
||||
d = (19*a + x) % 30;
|
||||
e = (2*b + 4*c + 6*d + y) % 7;
|
||||
d = (19 * a + x) % 30;
|
||||
e = (2 * b + 4 * c + 6 * d + y) % 7;
|
||||
return (((cumdays[3] + 1) + 22) + (d + e));
|
||||
}
|
||||
|
||||
@ -65,29 +65,27 @@ getpaskha(char *s, int year)
|
||||
int offset;
|
||||
|
||||
if (strncasecmp(s, PASKHA, PASKHALEN) == 0)
|
||||
s += PASKHALEN;
|
||||
else if ( npaskha.name != NULL
|
||||
&& strncasecmp(s, npaskha.name, npaskha.len) == 0
|
||||
)
|
||||
s += npaskha.len;
|
||||
s += PASKHALEN;
|
||||
else if (npaskha.name != NULL
|
||||
&& strncasecmp(s, npaskha.name, npaskha.len) == 0)
|
||||
s += npaskha.len;
|
||||
else
|
||||
return 0;
|
||||
|
||||
return 0;
|
||||
|
||||
/* Paskha+1 or Paskha-2
|
||||
* ^ ^ */
|
||||
|
||||
switch(*s) {
|
||||
switch (*s) {
|
||||
|
||||
case '-':
|
||||
case '+':
|
||||
offset = atoi(s);
|
||||
break;
|
||||
offset = atoi(s);
|
||||
break;
|
||||
|
||||
default:
|
||||
offset = 0;
|
||||
break;
|
||||
offset = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
return (paskha(year) + offset + 13/* new style */);
|
||||
|
||||
return (paskha(year) + offset + 13 /* new style */);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user