From owner-svn-src-all@FreeBSD.ORG Fri Oct 5 17:54:27 2012 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id A07BB106566B; Fri, 5 Oct 2012 17:54:27 +0000 (UTC) (envelope-from cracauer@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 8B3E78FC08; Fri, 5 Oct 2012 17:54:27 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.4/8.14.4) with ESMTP id q95HsRPH014585; Fri, 5 Oct 2012 17:54:27 GMT (envelope-from cracauer@svn.freebsd.org) Received: (from cracauer@localhost) by svn.freebsd.org (8.14.4/8.14.4/Submit) id q95HsRi8014583; Fri, 5 Oct 2012 17:54:27 GMT (envelope-from cracauer@svn.freebsd.org) Message-Id: <201210051754.q95HsRi8014583@svn.freebsd.org> From: Martin Cracauer Date: Fri, 5 Oct 2012 17:54:27 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r241230 - head/usr.bin/at X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 05 Oct 2012 17:54:27 -0000 Author: cracauer Date: Fri Oct 5 17:54:27 2012 New Revision: 241230 URL: http://svn.freebsd.org/changeset/base/241230 Log: Allow time offsets to be negative, e.g. `at 1530 - 15 minutes`. This is useful if you have been given some time for some event in some format and you want your computer to do something to prepare for it. Without having to do time arithmetic in a shellscript. The syntax matches what the at(1) usually used on Linux supports. Modified: head/usr.bin/at/parsetime.c Modified: head/usr.bin/at/parsetime.c ============================================================================== --- head/usr.bin/at/parsetime.c Fri Oct 5 16:44:00 2012 (r241229) +++ head/usr.bin/at/parsetime.c Fri Oct 5 17:54:27 2012 (r241230) @@ -64,7 +64,7 @@ enum { /* symbols */ MIDNIGHT, NOON, TEATIME, PM, AM, TOMORROW, TODAY, NOW, MINUTES, HOURS, DAYS, WEEKS, MONTHS, YEARS, - NUMBER, PLUS, DOT, SLASH, ID, JUNK, + NUMBER, PLUS, MINUS, DOT, SLASH, ID, JUNK, JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC, SUN, MON, TUE, WED, THU, FRI, SAT @@ -246,6 +246,8 @@ token(void) return sc_tokid = DOT; else if (sc_token[0] == '+') return sc_tokid = PLUS; + else if (sc_token[0] == '-') + return sc_tokid = MINUS; else if (sc_token[0] == '/') return sc_tokid = SLASH; else @@ -277,22 +279,14 @@ expect(int desired) /* - * plus() parses a now + time - * - * at [NOW] PLUS NUMBER [MINUTES|HOURS|DAYS|WEEKS|MONTHS|YEARS] - * + * plus_or_minus() holds functionality common to plus() and minus() */ - static void -plus(struct tm *tm) +plus_or_minus(struct tm *tm, int delay) { - int delay; int expectplur; - expect(NUMBER); - - delay = atoi(sc_token); - expectplur = (delay != 1) ? 1 : 0; + expectplur = (delay != 1 && delay != -1) ? 1 : 0; switch (token()) { case YEARS: @@ -323,11 +317,43 @@ plus(struct tm *tm) tm->tm_isdst = -1; if (mktime(tm) < 0) plonk(sc_tokid); +} /* plus_or_minus */ + + +/* + * plus() parses a now + time + * + * at [NOW] PLUS NUMBER [MINUTES|HOURS|DAYS|WEEKS|MONTHS|YEARS] + * + */ +static void +plus(struct tm *tm) +{ + int delay; + + expect(NUMBER); + delay = atoi(sc_token); + plus_or_minus(tm, delay); } /* plus */ /* + * minus() is like plus but can not be used with NOW + */ +static void +minus(struct tm *tm) +{ + int delay; + + expect(NUMBER); + + delay = -atoi(sc_token); + plus_or_minus(tm, delay); +} /* minus */ + + +/* * tod() computes the time of day * [NUMBER [DOT NUMBER] [AM|PM]] */ @@ -379,7 +405,8 @@ tod(struct tm *tm) * if we've gone past that time - but if we're specifying a time plus * a relative offset, it's okay to bump things */ - if ((sc_tokid == EOF || sc_tokid == PLUS) && tm->tm_hour > hour) { + if ((sc_tokid == EOF || sc_tokid == PLUS || sc_tokid == MINUS) && + tm->tm_hour > hour) { tm->tm_mday++; tm->tm_wday++; } @@ -456,6 +483,9 @@ month(struct tm *tm) case PLUS: plus(tm); break; + case MINUS: + minus(tm); + break; case TOMORROW: /* do something tomorrow */ @@ -588,6 +618,12 @@ parsetime(int argc, char **argv) plus(&runtime); break; + /* MINUS is different from PLUS in that NOW is not + * an optional prefix for it + */ + case MINUS: + minus(&runtime); + break; case NUMBER: tod(&runtime); month(&runtime);