[BACK]Return to parsetime.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / at

Annotation of src/usr.bin/at/parsetime.c, Revision 1.18

1.18    ! krw         1: /*     $OpenBSD: parsetime.c,v 1.17 2009/10/27 23:59:35 deraadt Exp $  */
1.1       deraadt     2:
1.14      millert     3: /*
1.1       deraadt     4:  * parsetime.c - parse time for at(1)
1.4       millert     5:  * Copyright (C) 1993, 1994  Thomas Koenig
1.1       deraadt     6:  *
                      7:  * modifications for english-language times
                      8:  * Copyright (C) 1993  David Parsons
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. The name of the author(s) may not be used to endorse or promote
                     16:  *    products derived from this software without specific prior written
                     17:  *    permission.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
                     20:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     21:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1.4       millert    22:  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
1.1       deraadt    23:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     24:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     25:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1.18    ! krw        26:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1.1       deraadt    27:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     28:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     29:  *
                     30:  *  at [NOW] PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS
                     31:  *     /NUMBER [DOT NUMBER] [AM|PM]\ /[MONTH NUMBER [NUMBER]]             \
                     32:  *     |NOON                       | |[TOMORROW]                          |
1.4       millert    33:  *     |MIDNIGHT                   | |[DAY OF WEEK]                       |
                     34:  *     \TEATIME                    / |NUMBER [SLASH NUMBER [SLASH NUMBER]]|
                     35:  *                                   \PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS/
1.1       deraadt    36:  */
                     37:
                     38: #include <sys/types.h>
                     39: #include <errno.h>
1.11      millert    40: #include <ctype.h>
1.1       deraadt    41: #include <stdio.h>
                     42: #include <stdlib.h>
                     43: #include <string.h>
                     44: #include <time.h>
1.8       alex       45: #include <tzfile.h>
1.1       deraadt    46: #include <unistd.h>
                     47:
1.13      millert    48: #include "globals.h"
1.1       deraadt    49: #include "at.h"
                     50:
                     51: /* Structures and unions */
                     52:
                     53: enum { /* symbols */
1.4       millert    54:        MIDNIGHT, NOON, TEATIME,
                     55:        PM, AM, TOMORROW, TODAY, NOW,
                     56:        MINUTES, HOURS, DAYS, WEEKS,
                     57:        NUMBER, PLUS, DOT, SLASH, ID, JUNK,
                     58:        JAN, FEB, MAR, APR, MAY, JUN,
                     59:        JUL, AUG, SEP, OCT, NOV, DEC,
                     60:        SUN, MON, TUE, WED, THU, FRI, SAT
1.1       deraadt    61: };
                     62:
                     63: /*
                     64:  * parse translation table - table driven parsers can be your FRIEND!
                     65:  */
                     66: struct {
1.4       millert    67:        char *name;     /* token name */
                     68:        int value;      /* token id */
                     69:        int plural;     /* is this plural? */
1.1       deraadt    70: } Specials[] = {
1.4       millert    71:        { "midnight", MIDNIGHT, 0 },    /* 00:00:00 of today or tomorrow */
                     72:        { "noon", NOON, 0 },            /* 12:00:00 of today or tomorrow */
                     73:        { "teatime", TEATIME, 0 },      /* 16:00:00 of today or tomorrow */
                     74:        { "am", AM, 0 },                /* morning times for 0-12 clock */
                     75:        { "pm", PM, 0 },                /* evening times for 0-12 clock */
                     76:        { "tomorrow", TOMORROW, 0 },    /* execute 24 hours from time */
                     77:        { "today", TODAY, 0 },          /* execute today - don't advance time */
                     78:        { "now", NOW, 0 },              /* opt prefix for PLUS */
                     79:
                     80:        { "minute", MINUTES, 0 },       /* minutes multiplier */
                     81:        { "min", MINUTES, 0 },
                     82:        { "m", MINUTES, 0 },
                     83:        { "minutes", MINUTES, 1 },      /* (pluralized) */
                     84:        { "hour", HOURS, 0 },           /* hours ... */
                     85:        { "hr", HOURS, 0 },             /* abbreviated */
                     86:        { "h", HOURS, 0 },
                     87:        { "hours", HOURS, 1 },          /* (pluralized) */
                     88:        { "day", DAYS, 0 },             /* days ... */
                     89:        { "d", DAYS, 0 },
                     90:        { "days", DAYS, 1 },            /* (pluralized) */
                     91:        { "week", WEEKS, 0 },           /* week ... */
                     92:        { "w", WEEKS, 0 },
                     93:        { "weeks", WEEKS, 1 },          /* (pluralized) */
                     94:        { "jan", JAN, 0 },
                     95:        { "feb", FEB, 0 },
                     96:        { "mar", MAR, 0 },
                     97:        { "apr", APR, 0 },
                     98:        { "may", MAY, 0 },
                     99:        { "jun", JUN, 0 },
                    100:        { "jul", JUL, 0 },
                    101:        { "aug", AUG, 0 },
                    102:        { "sep", SEP, 0 },
                    103:        { "oct", OCT, 0 },
                    104:        { "nov", NOV, 0 },
                    105:        { "dec", DEC, 0 },
1.7       deraadt   106:        { "january", JAN,0 },
                    107:        { "february", FEB,0 },
                    108:        { "march", MAR,0 },
                    109:        { "april", APR,0 },
                    110:        { "may", MAY,0 },
                    111:        { "june", JUN,0 },
                    112:        { "july", JUL,0 },
                    113:        { "august", AUG,0 },
                    114:        { "september", SEP,0 },
                    115:        { "october", OCT,0 },
                    116:        { "november", NOV,0 },
                    117:        { "december", DEC,0 },
1.4       millert   118:        { "sunday", SUN, 0 },
                    119:        { "sun", SUN, 0 },
                    120:        { "monday", MON, 0 },
                    121:        { "mon", MON, 0 },
                    122:        { "tuesday", TUE, 0 },
                    123:        { "tue", TUE, 0 },
                    124:        { "wednesday", WED, 0 },
                    125:        { "wed", WED, 0 },
                    126:        { "thursday", THU, 0 },
                    127:        { "thu", THU, 0 },
                    128:        { "friday", FRI, 0 },
                    129:        { "fri", FRI, 0 },
                    130:        { "saturday", SAT, 0 },
                    131:        { "sat", SAT, 0 },
                    132: };
1.1       deraadt   133:
                    134: static char **scp;     /* scanner - pointer at arglist */
1.15      cloder    135: static int scc;                /* scanner - count of remaining arguments */
1.1       deraadt   136: static char *sct;      /* scanner - next char pointer in current argument */
                    137: static int need;       /* scanner - need to advance to next argument */
                    138: static char *sc_token; /* scanner - token buffer */
1.12      todd      139: static size_t sc_len;   /* scanner - length of token buffer */
1.1       deraadt   140: static int sc_tokid;   /* scanner - token id */
1.4       millert   141: static int sc_tokplur; /* scanner - is token plural? */
1.1       deraadt   142:
                    143: /*
                    144:  * parse a token, checking if it's something special to us
                    145:  */
                    146: static int
1.10      millert   147: parse_token(char *arg)
1.1       deraadt   148: {
1.4       millert   149:        int i;
1.1       deraadt   150:
1.4       millert   151:        for (i=0; i < sizeof(Specials) / sizeof(Specials[0]); i++) {
                    152:                if (strcasecmp(Specials[i].name, arg) == 0) {
                    153:                        sc_tokplur = Specials[i].plural;
                    154:                        return (sc_tokid = Specials[i].value);
                    155:                }
1.1       deraadt   156:        }
                    157:
1.4       millert   158:        /* not special - must be some random id */
                    159:        return (ID);
1.11      millert   160: }
1.1       deraadt   161:
                    162:
                    163: /*
                    164:  * init_scanner() sets up the scanner to eat arguments
                    165:  */
1.13      millert   166: static int
1.10      millert   167: init_scanner(int argc, char **argv)
1.1       deraadt   168: {
1.4       millert   169:        scp = argv;
                    170:        scc = argc;
                    171:        need = 1;
                    172:        sc_len = 1;
                    173:        while (argc-- > 0)
                    174:                sc_len += strlen(*argv++);
                    175:
1.13      millert   176:        if ((sc_token = (char *) malloc(sc_len)) == NULL) {
                    177:                fprintf(stderr, "%s: Insufficient virtual memory\n",
                    178:                    ProgramName);
                    179:                return (-1);
                    180:        }
                    181:        return (0);
1.11      millert   182: }
1.1       deraadt   183:
                    184: /*
                    185:  * token() fetches a token from the input stream
                    186:  */
                    187: static int
1.10      millert   188: token(void)
1.1       deraadt   189: {
1.4       millert   190:        int idx;
1.1       deraadt   191:
1.10      millert   192:        for (;;) {
1.13      millert   193:                bzero(sc_token, sc_len);
1.4       millert   194:                sc_tokid = EOF;
                    195:                sc_tokplur = 0;
                    196:                idx = 0;
1.1       deraadt   197:
1.4       millert   198:                /*
                    199:                 * if we need to read another argument, walk along the
                    200:                 * argument list; when we fall off the arglist, we'll
                    201:                 * just return EOF forever
                    202:                 */
                    203:                if (need) {
                    204:                        if (scc < 1)
                    205:                                return (sc_tokid);
                    206:                        sct = *scp;
                    207:                        scp++;
                    208:                        scc--;
                    209:                        need = 0;
                    210:                }
                    211:                /*
                    212:                 * eat whitespace now - if we walk off the end of the argument,
                    213:                 * we'll continue, which puts us up at the top of the while loop
                    214:                 * to fetch the next argument in
                    215:                 */
                    216:                while (isspace(*sct))
                    217:                        ++sct;
                    218:                if (!*sct) {
                    219:                        need = 1;
                    220:                        continue;
                    221:                }
1.1       deraadt   222:
1.4       millert   223:                /*
                    224:                 * preserve the first character of the new token
                    225:                 */
                    226:                sc_token[0] = *sct++;
1.1       deraadt   227:
1.4       millert   228:                /*
                    229:                 * then see what it is
                    230:                 */
                    231:                if (isdigit(sc_token[0])) {
                    232:                        while (isdigit(*sct))
                    233:                                sc_token[++idx] = *sct++;
                    234:                        sc_token[++idx] = 0;
                    235:                        return ((sc_tokid = NUMBER));
                    236:                } else if (isalpha(sc_token[0])) {
                    237:                        while (isalpha(*sct))
                    238:                                sc_token[++idx] = *sct++;
                    239:                        sc_token[++idx] = 0;
                    240:                        return (parse_token(sc_token));
                    241:                }
                    242:                else if (sc_token[0] == ':' || sc_token[0] == '.')
                    243:                        return ((sc_tokid = DOT));
                    244:                else if (sc_token[0] == '+')
                    245:                        return ((sc_tokid = PLUS));
                    246:                else if (sc_token[0] == '/')
                    247:                        return ((sc_tokid = SLASH));
                    248:                else
                    249:                        return ((sc_tokid = JUNK));
1.11      millert   250:        }
                    251: }
1.1       deraadt   252:
                    253:
                    254: /*
                    255:  * plonk() gives an appropriate error message if a token is incorrect
                    256:  */
                    257: static void
1.10      millert   258: plonk(int tok)
1.1       deraadt   259: {
1.13      millert   260:        fprintf(stderr, "%s: %s time\n", ProgramName,
                    261:            (tok == EOF) ? "incomplete" : "garbled");
1.11      millert   262: }
1.1       deraadt   263:
                    264:
1.14      millert   265: /*
1.13      millert   266:  * expect() gets a token and returns -1 if it's not the token we want
1.1       deraadt   267:  */
1.13      millert   268: static int
1.10      millert   269: expect(int desired)
1.1       deraadt   270: {
1.13      millert   271:        if (token() != desired) {
                    272:                plonk(sc_tokid);
                    273:                return (-1);
                    274:        }
                    275:        return (0);
1.11      millert   276: }
1.1       deraadt   277:
                    278:
                    279: /*
                    280:  * dateadd() adds a number of minutes to a date.  It is extraordinarily
                    281:  * stupid regarding day-of-month overflow, and will most likely not
                    282:  * work properly
                    283:  */
                    284: static void
1.10      millert   285: dateadd(int minutes, struct tm *tm)
1.1       deraadt   286: {
1.4       millert   287:        /* increment days */
1.1       deraadt   288:
1.4       millert   289:        while (minutes > 24*60) {
                    290:                minutes -= 24*60;
                    291:                tm->tm_mday++;
1.1       deraadt   292:        }
                    293:
1.4       millert   294:        /* increment hours */
                    295:        while (minutes > 60) {
                    296:                minutes -= 60;
                    297:                tm->tm_hour++;
                    298:                if (tm->tm_hour > 23) {
                    299:                        tm->tm_mday++;
                    300:                        tm->tm_hour = 0;
                    301:                }
                    302:        }
                    303:
                    304:        /* increment minutes */
                    305:        tm->tm_min += minutes;
1.1       deraadt   306:
1.4       millert   307:        if (tm->tm_min > 59) {
                    308:                tm->tm_hour++;
                    309:                tm->tm_min -= 60;
                    310:
                    311:                if (tm->tm_hour > 23) {
                    312:                        tm->tm_mday++;
                    313:                        tm->tm_hour = 0;
                    314:                }
1.1       deraadt   315:        }
1.11      millert   316: }
1.1       deraadt   317:
                    318:
                    319: /*
                    320:  * plus() parses a now + time
                    321:  *
                    322:  *  at [NOW] PLUS NUMBER [MINUTES|HOURS|DAYS|WEEKS]
                    323:  *
                    324:  */
1.13      millert   325: static int
1.10      millert   326: plus(struct tm *tm)
1.1       deraadt   327: {
1.4       millert   328:        int delay;
                    329:        int expectplur;
                    330:
1.13      millert   331:        if (expect(NUMBER) != 0)
                    332:                return (-1);
1.1       deraadt   333:
1.4       millert   334:        delay = atoi(sc_token);
                    335:        expectplur = (delay != 1) ? 1 : 0;
1.1       deraadt   336:
1.4       millert   337:        switch (token()) {
                    338:        case WEEKS:
                    339:                delay *= 7;
1.15      cloder    340:                /* FALLTHROUGH */
1.4       millert   341:        case DAYS:
                    342:                delay *= 24;
1.15      cloder    343:                /* FALLTHROUGH */
1.4       millert   344:        case HOURS:
                    345:                delay *= 60;
1.15      cloder    346:                /* FALLTHROUGH */
1.4       millert   347:        case MINUTES:
                    348:                if (expectplur != sc_tokplur)
1.13      millert   349:                        fprintf(stderr, "%s: pluralization is wrong\n",
                    350:                            ProgramName);
1.4       millert   351:                dateadd(delay, tm);
1.13      millert   352:                return (0);
1.4       millert   353:        }
1.1       deraadt   354:
1.4       millert   355:        plonk(sc_tokid);
1.13      millert   356:        return (-1);
1.11      millert   357: }
1.1       deraadt   358:
                    359:
                    360: /*
                    361:  * tod() computes the time of day
                    362:  *     [NUMBER [DOT NUMBER] [AM|PM]]
                    363:  */
1.13      millert   364: static int
1.10      millert   365: tod(struct tm *tm)
1.1       deraadt   366: {
1.4       millert   367:        int hour, minute = 0;
                    368:        size_t tlen;
                    369:
                    370:        hour = atoi(sc_token);
                    371:        tlen = strlen(sc_token);
                    372:
                    373:        /*
                    374:         * first pick out the time of day - if it's 4 digits, we assume
                    375:         * a HHMM time, otherwise it's HH DOT MM time
                    376:         */
                    377:        if (token() == DOT) {
1.13      millert   378:                if (expect(NUMBER) != 0)
                    379:                        return (-1);
1.4       millert   380:                minute = atoi(sc_token);
                    381:                if (minute > 59)
1.13      millert   382:                        goto bad;
1.4       millert   383:                token();
                    384:        } else if (tlen == 4) {
                    385:                minute = hour % 100;
                    386:                if (minute > 59)
1.13      millert   387:                        goto bad;
1.4       millert   388:                hour = hour / 100;
                    389:        }
1.1       deraadt   390:
1.4       millert   391:        /*
                    392:         * check if an AM or PM specifier was given
                    393:         */
                    394:        if (sc_tokid == AM || sc_tokid == PM) {
                    395:                if (hour > 12)
1.13      millert   396:                        goto bad;
1.4       millert   397:
                    398:                if (sc_tokid == PM) {
                    399:                        if (hour != 12) /* 12:xx PM is 12:xx, not 24:xx */
                    400:                                hour += 12;
                    401:                } else {
                    402:                        if (hour == 12) /* 12:xx AM is 00:xx, not 12:xx */
                    403:                                hour = 0;
                    404:                }
                    405:                token();
                    406:        } else if (hour > 23)
1.13      millert   407:                goto bad;
1.1       deraadt   408:
1.4       millert   409:        /*
                    410:         * if we specify an absolute time, we don't want to bump the day even
                    411:         * if we've gone past that time - but if we're specifying a time plus
                    412:         * a relative offset, it's okay to bump things
                    413:         */
                    414:        if ((sc_tokid == EOF || sc_tokid == PLUS) && tm->tm_hour > hour) {
                    415:                tm->tm_mday++;
                    416:                tm->tm_wday++;
                    417:        }
1.1       deraadt   418:
1.4       millert   419:        tm->tm_hour = hour;
                    420:        tm->tm_min = minute;
                    421:        if (tm->tm_hour == 24) {
                    422:                tm->tm_hour = 0;
                    423:                tm->tm_mday++;
                    424:        }
1.13      millert   425:        return (0);
                    426: bad:
                    427:        fprintf(stderr, "%s: garbled time\n", ProgramName);
                    428:        return (-1);
1.11      millert   429: }
1.1       deraadt   430:
                    431:
                    432: /*
                    433:  * assign_date() assigns a date, wrapping to next year if needed
                    434:  */
                    435: static void
1.10      millert   436: assign_date(struct tm *tm, int mday, int mon, int year)
1.1       deraadt   437: {
1.9       millert   438:
                    439:        /*
                    440:         * Convert year into tm_year format (year - 1900).
                    441:         * We may be given the year in 2 digit, 4 digit, or tm_year format.
                    442:         */
                    443:        if (year != -1) {
                    444:                if (year >= TM_YEAR_BASE)
                    445:                        year -= TM_YEAR_BASE;   /* convert from 4 digit year */
                    446:                else if (year < 100) {
                    447:                        /* Convert to tm_year assuming current century */
                    448:                        year += (tm->tm_year / 100) * 100;
                    449:
                    450:                        if (year == tm->tm_year - 1)
                    451:                                year++;         /* Common off by one error */
                    452:                        else if (year < tm->tm_year)
                    453:                                year += 100;    /* must be in next century */
                    454:                }
1.4       millert   455:        }
                    456:
                    457:        if (year < 0 &&
                    458:            (tm->tm_mon > mon ||(tm->tm_mon == mon && tm->tm_mday > mday)))
                    459:                year = tm->tm_year + 1;
1.1       deraadt   460:
1.4       millert   461:        tm->tm_mday = mday;
                    462:        tm->tm_mon = mon;
1.1       deraadt   463:
1.4       millert   464:        if (year >= 0)
                    465:                tm->tm_year = year;
1.11      millert   466: }
1.1       deraadt   467:
                    468:
1.14      millert   469: /*
1.1       deraadt   470:  * month() picks apart a month specification
                    471:  *
                    472:  *  /[<month> NUMBER [NUMBER]]           \
                    473:  *  |[TOMORROW]                          |
1.4       millert   474:  *  |[DAY OF WEEK]                       |
1.1       deraadt   475:  *  |NUMBER [SLASH NUMBER [SLASH NUMBER]]|
                    476:  *  \PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS/
                    477:  */
1.13      millert   478: static int
1.10      millert   479: month(struct tm *tm)
1.1       deraadt   480: {
1.4       millert   481:        int year = (-1);
                    482:        int mday, wday, mon;
                    483:        size_t tlen;
                    484:
                    485:        switch (sc_tokid) {
                    486:        case PLUS:
1.13      millert   487:                if (plus(tm) != 0)
                    488:                        return (-1);
1.4       millert   489:                break;
                    490:
                    491:        case TOMORROW:
                    492:                /* do something tomorrow */
                    493:                tm->tm_mday++;
                    494:                tm->tm_wday++;
                    495:        case TODAY:
                    496:                /* force ourselves to stay in today - no further processing */
1.1       deraadt   497:                token();
1.4       millert   498:                break;
1.1       deraadt   499:
1.4       millert   500:        case JAN: case FEB: case MAR: case APR: case MAY: case JUN:
                    501:        case JUL: case AUG: case SEP: case OCT: case NOV: case DEC:
                    502:                /*
                    503:                 * do month mday [year]
                    504:                 */
                    505:                mon = sc_tokid - JAN;
1.13      millert   506:                if (expect(NUMBER) != 0)
                    507:                        return (-1);
1.4       millert   508:                mday = atoi(sc_token);
                    509:                if (token() == NUMBER) {
                    510:                        year = atoi(sc_token);
                    511:                        token();
1.1       deraadt   512:                }
1.4       millert   513:                assign_date(tm, mday, mon, year);
                    514:                break;
                    515:
                    516:        case SUN: case MON: case TUE:
                    517:        case WED: case THU: case FRI:
                    518:        case SAT:
                    519:                /* do a particular day of the week */
                    520:                wday = sc_tokid - SUN;
                    521:
                    522:                mday = tm->tm_mday;
                    523:
                    524:                /* if this day is < today, then roll to next week */
                    525:                if (wday < tm->tm_wday)
                    526:                        mday += 7 - (tm->tm_wday - wday);
                    527:                else
                    528:                        mday += (wday - tm->tm_wday);
                    529:
                    530:                tm->tm_wday = wday;
                    531:
                    532:                assign_date(tm, mday, tm->tm_mon, tm->tm_year);
                    533:                break;
1.1       deraadt   534:
1.4       millert   535:        case NUMBER:
1.1       deraadt   536:                /*
1.4       millert   537:                 * get numeric MMDDYY, mm/dd/yy, or dd.mm.yy
1.1       deraadt   538:                 */
1.4       millert   539:                tlen = strlen(sc_token);
                    540:                mon = atoi(sc_token);
                    541:                token();
1.1       deraadt   542:
1.4       millert   543:                if (sc_tokid == SLASH || sc_tokid == DOT) {
                    544:                        int sep;
1.1       deraadt   545:
1.4       millert   546:                        sep = sc_tokid;
1.13      millert   547:                        if (expect(NUMBER) != 0)
                    548:                                return (-1);
1.4       millert   549:                        mday = atoi(sc_token);
                    550:                        if (token() == sep) {
1.13      millert   551:                                if (expect(NUMBER) != 0)
                    552:                                        return (-1);
1.4       millert   553:                                year = atoi(sc_token);
                    554:                                token();
                    555:                        }
                    556:
                    557:                        /*
                    558:                         * flip months and days for european timing
                    559:                         */
                    560:                        if (sep == DOT) {
                    561:                                int x = mday;
                    562:                                mday = mon;
                    563:                                mon = x;
                    564:                        }
                    565:                } else if (tlen == 6 || tlen == 8) {
                    566:                        if (tlen == 8) {
1.8       alex      567:                                year = (mon % 10000) - TM_YEAR_BASE;
1.4       millert   568:                                mon /= 10000;
                    569:                        } else {
                    570:                                year = mon % 100;
                    571:                                mon /= 100;
                    572:                        }
                    573:                        mday = mon % 100;
                    574:                        mon /= 100;
                    575:                } else
1.13      millert   576:                        goto bad;
1.4       millert   577:
                    578:                mon--;
                    579:                if (mon < 0 || mon > 11 || mday < 1 || mday > 31)
1.13      millert   580:                        goto bad;
1.4       millert   581:
                    582:                assign_date(tm, mday, mon, year);
                    583:                break;
1.13      millert   584:        }
                    585:        return (0);
                    586: bad:
                    587:        fprintf(stderr, "%s: garbled time\n", ProgramName);
                    588:        return (-1);
1.11      millert   589: }
1.1       deraadt   590:
                    591:
                    592: time_t
1.10      millert   593: parsetime(int argc, char **argv)
1.1       deraadt   594: {
1.4       millert   595:        /*
                    596:         * Do the argument parsing, die if necessary, and return the
                    597:         * time the job should be run.
                    598:         */
                    599:        time_t nowtimer, runtimer;
                    600:        struct tm nowtime, runtime;
                    601:        int hr = 0;
                    602:        /* this MUST be initialized to zero for midnight/noon/teatime */
                    603:
1.13      millert   604:        if (argc == 0)
                    605:                return (-1);
                    606:
1.4       millert   607:        nowtimer = time(NULL);
                    608:        nowtime = *localtime(&nowtimer);
                    609:
                    610:        runtime = nowtime;
                    611:        runtime.tm_sec = 0;
                    612:        runtime.tm_isdst = 0;
                    613:
1.13      millert   614:        if (init_scanner(argc, argv) == -1)
                    615:                return (-1);
1.4       millert   616:
                    617:        switch (token()) {
                    618:        case NOW:       /* now is optional prefix for PLUS tree */
1.5       millert   619:                token();
                    620:                if (sc_tokid == EOF) {
                    621:                        runtime = nowtime;
                    622:                        break;
                    623:                }
                    624:                else if (sc_tokid != PLUS)
                    625:                        plonk(sc_tokid);
1.4       millert   626:        case PLUS:
1.13      millert   627:                if (plus(&runtime) != 0)
                    628:                        return (-1);
1.4       millert   629:                break;
                    630:
                    631:        case NUMBER:
1.13      millert   632:                if (tod(&runtime) != 0 || month(&runtime) != 0)
                    633:                        return (-1);
1.4       millert   634:                break;
                    635:
                    636:                /*
                    637:                 * evil coding for TEATIME|NOON|MIDNIGHT - we've initialised
                    638:                 * hr to zero up above, then fall into this case in such a
                    639:                 * way so we add +12 +4 hours to it for teatime, +12 hours
                    640:                 * to it for noon, and nothing at all for midnight, then
                    641:                 * set our runtime to that hour before leaping into the
                    642:                 * month scanner
                    643:                 */
                    644:        case TEATIME:
                    645:                hr += 4;
1.15      cloder    646:                /* FALLTHROUGH */
1.4       millert   647:        case NOON:
                    648:                hr += 12;
1.15      cloder    649:                /* FALLTHROUGH */
1.4       millert   650:        case MIDNIGHT:
                    651:                if (runtime.tm_hour >= hr) {
                    652:                        runtime.tm_mday++;
                    653:                        runtime.tm_wday++;
                    654:                }
                    655:                runtime.tm_hour = hr;
                    656:                runtime.tm_min = 0;
                    657:                token();
                    658:                /* fall through to month setting */
1.15      cloder    659:                /* FALLTHROUGH */
1.4       millert   660:        default:
1.13      millert   661:                if (month(&runtime) != 0)
                    662:                        return (-1);
1.4       millert   663:                break;
                    664:        } /* ugly case statement */
1.13      millert   665:        if (expect(EOF) != 0)
                    666:                return (-1);
1.4       millert   667:
                    668:        /*
                    669:         * adjust for daylight savings time
                    670:         */
                    671:        runtime.tm_isdst = -1;
1.1       deraadt   672:        runtimer = mktime(&runtime);
1.4       millert   673:        if (runtime.tm_isdst > 0) {
                    674:                runtimer -= 3600;
                    675:                runtimer = mktime(&runtime);
                    676:        }
1.1       deraadt   677:
1.13      millert   678:        if (runtimer < 0) {
                    679:                fprintf(stderr, "%s: garbled time\n", ProgramName);
                    680:                return (-1);
                    681:        }
1.1       deraadt   682:
1.13      millert   683:        if (nowtimer > runtimer) {
1.16      millert   684:                fprintf(stderr, "%s: cannot schedule jobs in the past\n",
1.13      millert   685:                    ProgramName);
                    686:                return (-1);
                    687:        }
1.1       deraadt   688:
1.4       millert   689:        return (runtimer);
1.11      millert   690: }