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