1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <regexp.h>
#include <ctype.h>
typedef struct Date Date;
struct Date {
Reprog *p; /* an RE to match this date */
Date *next; /* pointer to next in list */
};
enum{
Secondsperday = 24*60*60
};
Date *Base = nil;
Biobuf in;
int debug, matchyear;
void dates(Tm*);
void upper2lower(char*, char*, int);
void *emalloc(unsigned int);
void
usage(void)
{
fprint(2, "usage: calendar [-dy] [-p days] [files ...]\n");
exits("usage");
}
void
main(int argc, char *argv[])
{
int i, fd, ahead;
long now;
char *line;
Tm *tm;
Date *d;
char buf[1024];
ahead = 0;
ARGBEGIN{
case 'y':
matchyear = 1;
break;
case 'd':
debug = 1;
break;
case 'p':
ahead = atoi(EARGF(usage()));
break;
default:
usage();
}ARGEND;
/* make a list of dates */
now = time(0);
tm = localtime(now);
dates(tm);
now += Secondsperday;
tm = localtime(now);
dates(tm);
if(tm->wday == 6){
now += Secondsperday;
tm = localtime(now);
dates(tm);
}
if(tm->wday == 0){
now += Secondsperday;
tm = localtime(now);
dates(tm);
}
if(ahead){
now = time(0);
now += ahead * Secondsperday;
tm = localtime(now);
dates(tm);
}
for(i=0; i<argc || (i==0 && argc==0); i++){
if(i==0 && argc==0)
snprint(buf, sizeof buf,
"/usr/%s/lib/calendar", getuser());
else
strecpy(buf, buf+sizeof buf, argv[i]);
fd = open(buf, OREAD);
if(fd<0 || Binit(&in, fd, OREAD)<0){
fprint(2, "calendar: can't open %s: %r\n", buf);
exits("open");
}
/* go through the file */
while(line = Brdline(&in, '\n')){
line[Blinelen(&in) - 1] = 0;
upper2lower(buf, line, sizeof buf);
for(d=Base; d; d=d->next)
if(regexec(d->p, buf, 0, 0)){
print("%s\n", line);
break;
}
}
close(fd);
}
exits("");
}
char *months[] =
{
"january",
"february",
"march",
"april",
"may",
"june",
"july",
"august",
"september",
"october",
"november",
"december"
};
char *nth[] = {
"first",
"second",
"third",
"fourth",
"fifth"
};
char *days[] = {
"sunday",
"monday",
"tuesday",
"wednesday",
"thursday",
"friday",
"saturday"
};
/*
* Generate two Date structures. First has month followed by day;
* second has day followed by month. Link them into list after
* last, and return the first.
*/
void
dates(Tm *tm)
{
Date *nd;
char mo[128], day[128], buf[128];
if(utflen(days[tm->wday]) > 3)
snprint(day, sizeof day, "%3.3s(%s)?",
days[tm->wday], days[tm->wday]+3);
else
snprint(day, sizeof day, "%3.3s", days[tm->wday]);
if(utflen(months[tm->mon]) > 3)
snprint(mo, sizeof mo, "%3.3s(%s)?",
months[tm->mon], months[tm->mon]+3);
else
snprint(mo, sizeof mo, "%3.3s", months[tm->mon]);
if (matchyear)
snprint(buf, sizeof buf,
"^[\t ]*((%s( |\t)+)|(%d/))%d( |\t|$)(((%d|%d|%02d)( |\t|$))|[^0-9]|([0-9]+[^0-9 \t]))",
mo, tm->mon+1, tm->mday, tm->year+1900, tm->year%100, tm->year%100);
else
snprint(buf, sizeof buf,
"^[\t ]*((%s( |\t)+)|(%d/))%d( |\t|$)",
mo, tm->mon+1, tm->mday);
if(debug)
print("%s\n", buf);
nd = emalloc(sizeof(Date));
nd->p = regcomp(buf);
nd->next = Base;
Base = nd;
if (matchyear)
snprint(buf, sizeof buf,
"^[\t ]*%d( |\t)+(%s)( |\t|$)(((%d|%d|%02d)( |\t|$))|[^0-9]|([0-9]+[^0-9 \t]))",
tm->mday, mo, tm->year+1900, tm->year%100, tm->year%100);
else
snprint(buf, sizeof buf,
"^[\t ]*%d( |\t)+(%s)( |\t|$)",
tm->mday, mo);
if(debug)
print("%s\n", buf);
nd = emalloc(sizeof(Date));
nd->p = regcomp(buf);
nd->next = Base;
Base = nd;
snprint(buf, sizeof buf, "^[\t ]*every[ \t]+%s", day);
if(debug)
print("%s\n", buf);
nd = emalloc(sizeof(Date));
nd->p = regcomp(buf);
nd->next = Base;
Base = nd;
snprint(buf, sizeof buf, "[\t ]*the[\t ]+%s[\t ]+%s", nth[(tm->mday-1)/7], day);
if(debug)
print("%s\n", buf);
nd = emalloc(sizeof(Date));
nd->p = regcomp(buf);
nd->next = Base;
Base = nd;
}
/*
* Copy 'from' to 'to', converting to lower case
*/
void
upper2lower(char *to, char *from, int len)
{
while(--len>0 && *from!='\0')
*to++ = tolower(*from++);
*to = 0;
}
/*
* Call malloc and check for errors
*/
void*
emalloc(unsigned int n)
{
void *p;
p = malloc(n);
if(p == 0){
fprint(2, "calendar: malloc failed: %r\n");
exits("malloc");
}
return p;
}
|