From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pizarro.uberspace.de ([185.26.156.189]) by ewsd; Thu Jul 2 10:20:06 EDT 2020 Received: (qmail 31819 invoked from network); 2 Jul 2020 14:19:48 -0000 Received: from localhost (HELO localhost) (127.0.0.1) by pizarro.uberspace.de with SMTP; 2 Jul 2020 14:19:48 -0000 Message-ID: <319D46C3FB4CBC39E29649E2C255F5C6@pizarro.uberspace.de> To: 9front@9front.org Subject: patch: calendar look ahead Date: Thu, 2 Jul 2020 16:19:47 +0200 From: sirjofri@sirjofri.de MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="upas-epwztzgequkpharumnvspdvpbs" List-ID: <9front.9front.org> List-Help: X-Glyph: ➈ X-Bullshit: distributed managed deep-learning GPU extension This is a multi-part message in MIME format. --upas-epwztzgequkpharumnvspdvpbs Content-Disposition: inline Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit Hey 9front. finally I hope this mail arrives... The following patch changes calendar behaviour. Today is the first. If I call calendar with -p 5 it looks at the 1,2,6 and misses my dates on 3-5. That doesn't really make sense and is not how I understand the man page. This patch changes it's behavior to look at all dates between today and today+5, so in the example above it looks at 1,2,6,5,4,3 (in that order). (/sys/src/cmd/calendar.c) 74c74 < if(ahead){ --- > while(ahead > 1){ 78a79 > ahead--; --upas-epwztzgequkpharumnvspdvpbs Content-Disposition: inline Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit #include #include #include #include #include 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); } while(ahead > 1){ now = time(0); now += ahead * Secondsperday; tm = localtime(now); dates(tm); ahead--; } for(i=0; inext) 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; } --upas-epwztzgequkpharumnvspdvpbs--