7 typedef struct Date Date;
9 Reprog *p; /* an RE to match this date */
10 Date *next; /* pointer to next in list */
14 Secondsperday = 24*60*60
22 void upper2lower(char*, char*, int);
23 void *emalloc(unsigned int);
28 fprint(2, "usage: calendar [-dy] [-p days] [files ...]\n");
33 main(int argc, char *argv[])
51 ahead = atoi(EARGF(usage()));
57 /* make a list of dates */
76 now += ahead * Secondsperday;
81 for(i=0; i<argc || (i==0 && argc==0); i++){
83 snprint(buf, sizeof buf,
84 "/usr/%s/lib/calendar", getuser());
86 strecpy(buf, buf+sizeof buf, argv[i]);
87 fd = open(buf, OREAD);
88 if(fd<0 || Binit(&in, fd, OREAD)<0){
89 fprint(2, "calendar: can't open %s: %r\n", buf);
93 /* go through the file */
94 while(line = Brdline(&in, '\n')){
95 line[Blinelen(&in) - 1] = 0;
96 upper2lower(buf, line, sizeof buf);
97 for(d=Base; d; d=d->next)
98 if(regexec(d->p, buf, 0, 0)){
141 * Generate two Date structures. First has month followed by day;
142 * second has day followed by month. Link them into list after
143 * last, and return the first.
149 char mo[128], day[128], buf[128];
151 if(utflen(days[tm->wday]) > 3)
152 snprint(day, sizeof day, "%3.3s(%s)?",
153 days[tm->wday], days[tm->wday]+3);
155 snprint(day, sizeof day, "%3.3s", days[tm->wday]);
157 if(utflen(months[tm->mon]) > 3)
158 snprint(mo, sizeof mo, "%3.3s(%s)?",
159 months[tm->mon], months[tm->mon]+3);
161 snprint(mo, sizeof mo, "%3.3s", months[tm->mon]);
163 snprint(buf, sizeof buf,
164 "^[\t ]*((%s( |\t)+)|(%d/))%d( |\t|$)(((%d|%d|%02d)( |\t|$))|[^0-9]|([0-9]+[^0-9 \t]))",
165 mo, tm->mon+1, tm->mday, tm->year+1900, tm->year%100, tm->year%100);
167 snprint(buf, sizeof buf,
168 "^[\t ]*((%s( |\t)+)|(%d/))%d( |\t|$)",
169 mo, tm->mon+1, tm->mday);
173 nd = emalloc(sizeof(Date));
174 nd->p = regcomp(buf);
179 snprint(buf, sizeof buf,
180 "^[\t ]*%d( |\t)+(%s)( |\t|$)(((%d|%d|%02d)( |\t|$))|[^0-9]|([0-9]+[^0-9 \t]))",
181 tm->mday, mo, tm->year+1900, tm->year%100, tm->year%100);
183 snprint(buf, sizeof buf,
184 "^[\t ]*%d( |\t)+(%s)( |\t|$)",
188 nd = emalloc(sizeof(Date));
189 nd->p = regcomp(buf);
193 snprint(buf, sizeof buf, "^[\t ]*every[ \t]+%s", day);
196 nd = emalloc(sizeof(Date));
197 nd->p = regcomp(buf);
201 snprint(buf, sizeof buf, "[\t ]*the[\t ]+%s[\t ]+%s", nth[(tm->mday-1)/7], day);
204 nd = emalloc(sizeof(Date));
205 nd->p = regcomp(buf);
211 * Copy 'from' to 'to', converting to lower case
214 upper2lower(char *to, char *from, int len)
216 while(--len>0 && *from!='\0')
217 *to++ = tolower(*from++);
222 * Call malloc and check for errors
225 emalloc(unsigned int n)
231 fprint(2, "calendar: malloc failed: %r\n");