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
20 Date *dates(Date**, Tm*);
21 void upper2lower(char*, char*, int);
22 void *alloc(unsigned int);
25 main(int argc, char *argv[])
31 Date *first, *last, *d;
46 fprint(2, "usage: calendar [-y] [-d] [files ...]\n");
50 /* make a list of dates */
54 first = dates(&last, tm);
70 now += ahead * Secondsperday;
75 for(i=0; i<argc || (i==0 && argc==0); i++){
77 snprint(buf, sizeof(buf),
78 "%s/lib/calendar", getenv("HOME"));
81 fd = open(buf, OREAD);
82 if(fd<0 || Binit(&in, fd, OREAD)<0){
83 fprint(2, "calendar: can't open %s: %r\n", buf);
87 /* go through the file */
88 while(line = Brdline(&in, '\n')){
89 line[Blinelen(&in) - 1] = 0;
90 upper2lower(buf, line, sizeof buf);
91 for(d=first; d; d=d->next)
92 if(regexec(d->p, buf, 0, 0)){
119 * Generate two Date structures. First has month followed by day;
120 * second has day followed by month. Link them into list after
121 * last, and return the first.
124 dates(Date **last, Tm *tm)
128 char mo[128], buf[128];
130 if(utflen(months[tm->mon]) > 3)
131 snprint(mo, sizeof mo, "%3.3s(%s)?",
132 months[tm->mon], months[tm->mon]+3);
134 snprint(mo, sizeof mo, "%3.3s", months[tm->mon]);
136 snprint(buf, sizeof buf,
137 "(^| |\t)((%s( |\t)+)|(%d/))%d( |\t|$)(((%d|%d)( |\t|$))|[^0-9]|([0-9]+[^0-9 \t]))",
138 mo, tm->mon+1, tm->mday, tm->year+1900, tm->year%100);
140 snprint(buf, sizeof buf,
141 "(^| |\t)((%s( |\t)+)|(%d/))%d( |\t|$)",
142 mo, tm->mon+1, tm->mday);
146 first = alloc(sizeof(Date));
148 (*last)->next = first;
149 first->p = regcomp(buf);
152 snprint(buf, sizeof buf,
153 "(^| |\t)%d( |\t)+(%s)( |\t|$)(((%d|%d)( |\t|$))|[^0-9]|([0-9]+[^0-9 \t]))",
154 tm->mday, mo, tm->year+1900, tm->year%100);
156 snprint(buf, sizeof buf,
157 "(^| |\t)%d( |\t)+(%s)( |\t|$)",
161 nd = alloc(sizeof(Date));
162 nd->p = regcomp(buf);
171 * Copy 'from' to 'to', converting to lower case
174 upper2lower(char *to, char *from, int len)
176 while(--len>0 && *from!='\0')
177 *to++ = tolower((uchar)*from++);
182 * Call malloc and check for errors
185 alloc(unsigned int n)
191 fprint(2, "calendar: malloc failed: %r\n");