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

Annotation of src/usr.bin/calendar/io.c, Revision 1.29

1.29    ! otto        1: /*     $OpenBSD: io.c,v 1.28 2004/12/10 15:31:01 mickey Exp $  */
1.1       millert     2:
                      3: /*
                      4:  * Copyright (c) 1989, 1993, 1994
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
1.24      millert    15:  * 3. Neither the name of the University nor the names of its contributors
1.1       millert    16:  *    may be used to endorse or promote products derived from this software
                     17:  *    without specific prior written permission.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     29:  * SUCH DAMAGE.
                     30:  */
                     31:
                     32: #ifndef lint
                     33: static const char copyright[] =
                     34: "@(#) Copyright (c) 1989, 1993\n\
                     35:        The Regents of the University of California.  All rights reserved.\n";
                     36: #endif /* not lint */
                     37:
                     38: #ifndef lint
                     39: #if 0
                     40: static const char sccsid[] = "@(#)calendar.c  8.3 (Berkeley) 3/25/94";
                     41: #else
1.29    ! otto       42: static const char rcsid[] = "$OpenBSD: io.c,v 1.28 2004/12/10 15:31:01 mickey Exp $";
1.1       millert    43: #endif
                     44: #endif /* not lint */
                     45:
                     46: #include <sys/param.h>
                     47: #include <sys/stat.h>
                     48: #include <sys/time.h>
                     49: #include <sys/types.h>
                     50: #include <sys/uio.h>
                     51: #include <sys/wait.h>
                     52:
                     53: #include <ctype.h>
                     54: #include <err.h>
                     55: #include <errno.h>
1.14      pjanzen    56: #include <fcntl.h>
1.1       millert    57: #include <locale.h>
                     58: #include <pwd.h>
                     59: #include <stdio.h>
                     60: #include <stdlib.h>
                     61: #include <string.h>
1.4       deraadt    62: #include <tzfile.h>
1.1       millert    63: #include <unistd.h>
                     64:
                     65: #include "pathnames.h"
                     66: #include "calendar.h"
                     67:
                     68:
                     69: struct iovec header[] = {
                     70:        {"From: ", 6},
                     71:        {NULL, 0},
                     72:        {" (Reminder Service)\nTo: ", 24},
                     73:        {NULL, 0},
                     74:        {"\nSubject: ", 10},
                     75:        {NULL, 0},
                     76:        {"'s Calendar\nPrecedence: bulk\n\n",  30},
                     77: };
                     78:
                     79:
                     80: void
1.20      vincent    81: cal(void)
1.1       millert    82: {
1.19      mpech      83:        int printing;
                     84:        char *p;
1.1       millert    85:        FILE *fp;
1.16      mickey     86:        int ch, l, i, bodun = 0, bodun_maybe = 0;
1.1       millert    87:        int var;
1.17      mickey     88:        char buf[2048 + 1], *prefix = NULL;
1.6       pjanzen    89:        struct event *events, *cur_evt, *ev1, *tmp;
1.5       pjanzen    90:        struct match *m;
1.23      grange     91:        size_t nlen;
1.1       millert    92:
1.5       pjanzen    93:        events = NULL;
                     94:        cur_evt = NULL;
1.1       millert    95:        if ((fp = opencal()) == NULL)
                     96:                return;
                     97:        for (printing = 0; fgets(buf, sizeof(buf), stdin) != NULL;) {
                     98:                if ((p = strchr(buf, '\n')) != NULL)
                     99:                        *p = '\0';
                    100:                else
                    101:                        while ((ch = getchar()) != '\n' && ch != EOF);
1.10      deraadt   102:                for (l = strlen(buf); l > 0 && isspace(buf[l - 1]); l--)
1.1       millert   103:                        ;
                    104:                buf[l] = '\0';
                    105:                if (buf[0] == '\0')
                    106:                        continue;
                    107:                if (strncmp(buf, "LANG=", 5) == 0) {
                    108:                        (void) setlocale(LC_ALL, buf + 5);
                    109:                        setnnames();
1.15      mickey    110:                        if (!strcmp(buf + 5, "ru_RU.KOI8-R") ||
                    111:                            !strcmp(buf + 5, "uk_UA.KOI8-U") ||
                    112:                            !strcmp(buf + 5, "by_BY.KOI8-B")) {
                    113:                                bodun_maybe++;
1.17      mickey    114:                                bodun = 0;
                    115:                                if (prefix)
                    116:                                        free(prefix);
                    117:                                prefix = NULL;
1.15      mickey    118:                        } else
                    119:                                bodun_maybe = 0;
1.1       millert   120:                        continue;
1.27      mickey    121:                } else if (strncmp(buf, "CALENDAR=", 9) == 0) {
                    122:                        char *ep;
                    123:
                    124:                        if (buf[9] == '\0')
                    125:                                calendar = 0;
                    126:                        else if (!strcasecmp(buf + 9, "julian")) {
                    127:                                calendar = JULIAN;
                    128:                                errno = 0;
                    129:                                julian = strtoul(buf + 14, &ep, 10);
                    130:                                if (buf[0] == '\0' || *ep != '\0')
                    131:                                        julian = 13;
                    132:                                if ((errno == ERANGE && julian == ULONG_MAX) ||
                    133:                                    julian > 14)
                    134:                                        errx(1, "Julian calendar offset is too large");
                    135:                        } else if (!strcasecmp(buf + 9, "gregorian"))
                    136:                                calendar = GREGORIAN;
                    137:                        else if (!strcasecmp(buf + 9, "lunar"))
                    138:                                calendar = LUNAR;
                    139:                } else if (bodun_maybe && strncmp(buf, "BODUN=", 6) == 0) {
1.15      mickey    140:                        bodun++;
1.17      mickey    141:                        if (prefix)
                    142:                                free(prefix);
                    143:                        if ((prefix = strdup(buf + 6)) == NULL)
                    144:                                err(1, NULL);
1.27      mickey    145:                        continue;
1.17      mickey    146:                }
1.6       pjanzen   147:                /* User defined names for special events */
                    148:                if ((p = strchr(buf, '='))) {
                    149:                        for (i = 0; i < NUMEV; i++) {
                    150:                        if (strncasecmp(buf, spev[i].name, spev[i].nlen) == 0 &&
                    151:                            (p - buf == spev[i].nlen) && buf[spev[i].nlen + 1]) {
                    152:                                p++;
                    153:                                if (spev[i].uname != NULL)
                    154:                                        free(spev[i].uname);
                    155:                                if ((spev[i].uname = strdup(p)) == NULL)
1.11      pjanzen   156:                                        err(1, NULL);
1.6       pjanzen   157:                                spev[i].ulen = strlen(p);
                    158:                                i = NUMEV + 1;
                    159:                        }
                    160:                        }
                    161:                if (i > NUMEV)
1.1       millert   162:                        continue;
                    163:                }
                    164:                if (buf[0] != '\t') {
1.15      mickey    165:                        printing = (m = isnow(buf, bodun)) ? 1 : 0;
1.6       pjanzen   166:                        if ((p = strchr(buf, '\t')) == NULL) {
                    167:                                printing = 0;
1.1       millert   168:                                continue;
1.6       pjanzen   169:                        }
1.5       pjanzen   170:                        /* Need the following to catch hardwired "variable"
                    171:                         * dates */
1.1       millert   172:                        if (p > buf && p[-1] == '*')
                    173:                                var = 1;
1.5       pjanzen   174:                        else
                    175:                                var = 0;
1.1       millert   176:                        if (printing) {
1.5       pjanzen   177:                                struct match *foo;
                    178:
1.6       pjanzen   179:                                ev1 = NULL;
1.5       pjanzen   180:                                while (m) {
                    181:                                cur_evt = (struct event *) malloc(sizeof(struct event));
                    182:                                if (cur_evt == NULL)
1.11      pjanzen   183:                                        err(1, NULL);
1.1       millert   184:
1.6       pjanzen   185:                                cur_evt->when = m->when;
                    186:                                snprintf(cur_evt->print_date,
                    187:                                    sizeof(cur_evt->print_date), "%s%c",
                    188:                                    m->print_date, (var + m->var) ? '*' : ' ');
                    189:                                if (ev1) {
                    190:                                        cur_evt->desc = ev1->desc;
                    191:                                        cur_evt->ldesc = NULL;
                    192:                                } else {
1.17      mickey    193:                                        if (m->bodun && prefix) {
                    194:                                                int l1 = strlen(prefix);
1.15      mickey    195:                                                int l2 = strlen(p);
1.22      deraadt   196:                                                int len = l1 + l2 + 2;
1.15      mickey    197:                                                if ((cur_evt->ldesc =
1.22      deraadt   198:                                                    malloc(len)) == NULL)
1.26      tom       199:                                                        err(1, NULL);
1.22      deraadt   200:                                                snprintf(cur_evt->ldesc, len,
1.17      mickey    201:                                                    "\t%s %s", prefix, p + 1);
1.15      mickey    202:                                        } else if ((cur_evt->ldesc =
                    203:                                            strdup(p)) == NULL)
1.11      pjanzen   204:                                                err(1, NULL);
1.6       pjanzen   205:                                        cur_evt->desc = &(cur_evt->ldesc);
                    206:                                        ev1 = cur_evt;
1.5       pjanzen   207:                                }
                    208:                                insert(&events, cur_evt);
                    209:                                foo = m;
                    210:                                m = m->next;
                    211:                                free(foo);
                    212:                                }
1.1       millert   213:                        }
                    214:                }
1.5       pjanzen   215:                else if (printing) {
1.23      grange    216:                        nlen = strlen(ev1->ldesc) + strlen(buf) + 2;
                    217:                        if ((ev1->ldesc = realloc(ev1->ldesc, nlen)) == NULL)
1.11      pjanzen   218:                                err(1, NULL);
1.23      grange    219:                        snprintf(ev1->ldesc, nlen, "%s\n%s", ev1->ldesc, buf);
1.5       pjanzen   220:                }
                    221:        }
                    222:        tmp = events;
                    223:        while (tmp) {
1.6       pjanzen   224:                (void)fprintf(fp, "%s%s\n", tmp->print_date, *(tmp->desc));
                    225:                tmp = tmp->next;
                    226:        }
                    227:        tmp = events;
                    228:        while (tmp) {
1.5       pjanzen   229:                events = tmp;
1.6       pjanzen   230:                if (tmp->ldesc)
                    231:                        free(tmp->ldesc);
1.5       pjanzen   232:                tmp = tmp->next;
                    233:                free(events);
1.1       millert   234:        }
                    235:        closecal(fp);
                    236: }
                    237:
                    238: int
                    239: getfield(p, endp, flags)
                    240:        char *p, **endp;
                    241:        int *flags;
                    242: {
1.6       pjanzen   243:        int val, var, i;
1.1       millert   244:        char *start, savech;
                    245:
1.8       pjanzen   246:        for (; !isdigit(*p) && !isalpha(*p) && *p != '*' && *p != '\t'; ++p)
1.1       millert   247:                ;
1.6       pjanzen   248:        if (*p == '*') {                        /* `*' is every month */
1.1       millert   249:                *flags |= F_ISMONTH;
                    250:                *endp = p+1;
1.6       pjanzen   251:                return (-1);    /* means 'every month' */
1.1       millert   252:        }
                    253:        if (isdigit(*p)) {
                    254:                val = strtol(p, &p, 10);        /* if 0, it's failure */
                    255:                for (; !isdigit(*p) && !isalpha(*p) && *p != '*'; ++p)
                    256:                        ;
                    257:                *endp = p;
                    258:                return (val);
                    259:        }
                    260:        for (start = p; isalpha(*++p);)
                    261:                ;
                    262:
                    263:        /* Sunday-1 */
                    264:        if (*p == '+' || *p == '-')
                    265:            for(; isdigit(*++p);)
                    266:                ;
                    267:
                    268:        savech = *p;
                    269:        *p = '\0';
                    270:
                    271:        /* Month */
                    272:        if ((val = getmonth(start)) != 0)
                    273:                *flags |= F_ISMONTH;
                    274:
                    275:        /* Day */
                    276:        else if ((val = getday(start)) != 0) {
                    277:            *flags |= F_ISDAY;
                    278:
                    279:            /* variable weekday */
                    280:            if ((var = getdayvar(start)) != 0) {
1.6       pjanzen   281:                if (var <= 5 && var >= -4)
1.1       millert   282:                    val += var * 10;
                    283: #ifdef DEBUG
                    284:                printf("var: %d\n", var);
                    285: #endif
                    286:            }
                    287:        }
                    288:
1.6       pjanzen   289:        /* Try specials (Easter, Paskha, ...) */
1.1       millert   290:        else {
1.6       pjanzen   291:                for (i = 0; i < NUMEV; i++) {
                    292:                        if (strncasecmp(start, spev[i].name, spev[i].nlen) == 0) {
                    293:                                start += spev[i].nlen;
                    294:                                val = i + 1;
                    295:                                i = NUMEV + 1;
                    296:                        } else if (spev[i].uname != NULL &&
                    297:                            strncasecmp(start, spev[i].uname, spev[i].ulen) == 0) {
                    298:                                start += spev[i].ulen;
                    299:                                val = i + 1;
                    300:                                i = NUMEV + 1;
                    301:                        }
                    302:                }
                    303:                if (i > NUMEV) {
                    304:                        switch(*start) {
                    305:                        case '-':
                    306:                        case '+':
                    307:                           var = atoi(start);
                    308:                           if (var > 365 || var < -365)
                    309:                                   return (0); /* Someone is just being silly */
                    310:                           val += (NUMEV + 1) * var;
                    311:                           /* We add one to the matching event and multiply by
                    312:                            * (NUMEV + 1) so as not to return 0 if there's a match.
                    313:                            * val will overflow if there is an obscenely large
                    314:                            * number of special events. */
                    315:                           break;
                    316:                        }
                    317:                *flags |= F_SPECIAL;
                    318:                }
                    319:                if (!(*flags & F_SPECIAL)) {
                    320:                /* undefined rest */
                    321:                        *p = savech;
                    322:                        return (0);
                    323:                }
1.1       millert   324:        }
1.8       pjanzen   325:        for (*p = savech; !isdigit(*p) && !isalpha(*p) && *p != '*' && *p != '\t'; ++p)
1.1       millert   326:                ;
                    327:        *endp = p;
                    328:        return (val);
                    329: }
                    330:
1.10      deraadt   331:
1.1       millert   332: FILE *
1.20      vincent   333: opencal(void)
1.1       millert   334: {
1.20      vincent   335:        int pdes[2], fdin;
                    336:        struct stat st;
1.1       millert   337:
                    338:        /* open up calendar file as stdin */
1.20      vincent   339:        if ((fdin = open(calendarFile, O_RDONLY)) == -1 ||
                    340:            fstat(fdin, &st) == -1 || !S_ISREG(st.st_mode)) {
1.13      pjanzen   341:                if (!doall) {
1.9       millert   342:                        char *home = getenv("HOME");
                    343:                        if (home == NULL || *home == '\0')
                    344:                                errx(1, "cannot get home directory");
1.10      deraadt   345:                        if (!(chdir(home) == 0 &&
                    346:                            chdir(calendarHome) == 0 &&
1.14      pjanzen   347:                            (fdin = open(calendarFile, O_RDONLY)) != -1))
1.18      deraadt   348:                                errx(1, "no calendar file: ``%s'' or ``~/%s/%s''",
1.2       millert   349:                                    calendarFile, calendarHome, calendarFile);
1.1       millert   350:                }
                    351:        }
1.20      vincent   352:
1.1       millert   353:        if (pipe(pdes) < 0)
                    354:                return (NULL);
                    355:        switch (vfork()) {
                    356:        case -1:                        /* error */
                    357:                (void)close(pdes[0]);
                    358:                (void)close(pdes[1]);
                    359:                return (NULL);
                    360:        case 0:
1.10      deraadt   361:                dup2(fdin, STDIN_FILENO);
                    362:                /* child -- set stdout to pipe input */
1.1       millert   363:                if (pdes[1] != STDOUT_FILENO) {
                    364:                        (void)dup2(pdes[1], STDOUT_FILENO);
                    365:                        (void)close(pdes[1]);
                    366:                }
                    367:                (void)close(pdes[0]);
1.20      vincent   368:                /*
                    369:                 * Set stderr to /dev/null.  Necessary so that cron does not
1.13      pjanzen   370:                 * wait for cpp to finish if it's running calendar -a.
                    371:                 */
                    372:                if (doall) {
                    373:                        int fderr;
                    374:                        fderr = open(_PATH_DEVNULL, O_WRONLY, 0);
                    375:                        if (fderr == -1)
                    376:                                _exit(0);
                    377:                        (void)dup2(fderr, STDERR_FILENO);
                    378:                        (void)close(fderr);
                    379:                }
1.29    ! otto      380:                execl(_PATH_CPP, "cpp", "-traditional", "-undef", "-U__GNUC__",
        !           381:                    "-P", "-I.", _PATH_INCLUDE, (char *)NULL);
1.1       millert   382:                warn(_PATH_CPP);
                    383:                _exit(1);
                    384:        }
                    385:        /* parent -- set stdin to pipe output */
                    386:        (void)dup2(pdes[0], STDIN_FILENO);
                    387:        (void)close(pdes[0]);
                    388:        (void)close(pdes[1]);
                    389:
                    390:        /* not reading all calendar files, just set output to stdout */
                    391:        if (!doall)
                    392:                return (stdout);
                    393:
                    394:        /* set output to a temporary file, so if no output don't send mail */
1.13      pjanzen   395:        return(tmpfile());
1.1       millert   396: }
                    397:
                    398: void
                    399: closecal(fp)
                    400:        FILE *fp;
                    401: {
                    402:        struct stat sbuf;
                    403:        int nread, pdes[2], status;
                    404:        char buf[1024];
                    405:
                    406:        if (!doall)
                    407:                return;
                    408:
                    409:        (void)rewind(fp);
                    410:        if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
                    411:                goto done;
                    412:        if (pipe(pdes) < 0)
                    413:                goto done;
                    414:        switch (vfork()) {
                    415:        case -1:                        /* error */
                    416:                (void)close(pdes[0]);
                    417:                (void)close(pdes[1]);
                    418:                goto done;
                    419:        case 0:
                    420:                /* child -- set stdin to pipe output */
                    421:                if (pdes[0] != STDIN_FILENO) {
                    422:                        (void)dup2(pdes[0], STDIN_FILENO);
                    423:                        (void)close(pdes[0]);
                    424:                }
                    425:                (void)close(pdes[1]);
                    426:                execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
1.12      deraadt   427:                    "\"Reminder Service\"", (char *)NULL);
1.1       millert   428:                warn(_PATH_SENDMAIL);
                    429:                _exit(1);
                    430:        }
                    431:        /* parent -- write to pipe input */
                    432:        (void)close(pdes[0]);
                    433:
                    434:        header[1].iov_base = header[3].iov_base = pw->pw_name;
                    435:        header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
                    436:        writev(pdes[1], header, 7);
                    437:        while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
                    438:                (void)write(pdes[1], buf, nread);
                    439:        (void)close(pdes[1]);
                    440: done:  (void)fclose(fp);
1.10      deraadt   441:        while (wait(&status) >= 0)
                    442:                ;
1.5       pjanzen   443: }
                    444:
                    445:
                    446: void
                    447: insert(head, cur_evt)
                    448:        struct event **head;
                    449:        struct event *cur_evt;
                    450: {
                    451:        struct event *tmp, *tmp2;
                    452:
                    453:        if (*head) {
                    454:                /* Insert this one in order */
                    455:                tmp = *head;
                    456:                tmp2 = NULL;
                    457:                while (tmp->next &&
                    458:                    tmp->when <= cur_evt->when) {
                    459:                        tmp2 = tmp;
                    460:                        tmp = tmp->next;
                    461:                }
                    462:                if (tmp->when > cur_evt->when) {
                    463:                        cur_evt->next = tmp;
                    464:                        if (tmp2)
                    465:                                tmp2->next = cur_evt;
                    466:                        else
                    467:                                *head = cur_evt;
                    468:                } else {
                    469:                        cur_evt->next = tmp->next;
                    470:                        tmp->next = cur_evt;
                    471:                }
                    472:        } else {
                    473:                *head = cur_evt;
                    474:                cur_evt->next = NULL;
                    475:        }
1.1       millert   476: }