Annotation of src/usr.bin/calendar/io.c, Revision 1.19
1.19 ! mpech 1: /* $OpenBSD: io.c,v 1.18 2001/10/03 18:17:48 deraadt 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.19 ! mpech 46: static char rcsid[] = "$OpenBSD: io.c,v 1.18 2001/10/03 18:17:48 deraadt 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
85: cal()
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);
181: if ((cur_evt->ldesc =
182: malloc(l1 + l2)) == NULL)
1.17 mickey 183: err(1, "malloc");
184: sprintf(cur_evt->ldesc,
185: "\t%s %s", prefix, p + 1);
1.15 mickey 186: } else if ((cur_evt->ldesc =
187: strdup(p)) == NULL)
1.11 pjanzen 188: err(1, NULL);
1.6 pjanzen 189: cur_evt->desc = &(cur_evt->ldesc);
190: ev1 = cur_evt;
1.5 pjanzen 191: }
192: insert(&events, cur_evt);
193: foo = m;
194: m = m->next;
195: free(foo);
196: }
1.1 millert 197: }
198: }
1.5 pjanzen 199: else if (printing) {
1.6 pjanzen 200: if ((ev1->ldesc = realloc(ev1->ldesc,
201: (2 + strlen(ev1->ldesc) + strlen(buf)))) == NULL)
1.11 pjanzen 202: err(1, NULL);
1.6 pjanzen 203: strcat(ev1->ldesc, "\n");
204: strcat(ev1->ldesc, buf);
1.5 pjanzen 205: }
206: }
207: tmp = events;
208: while (tmp) {
1.6 pjanzen 209: (void)fprintf(fp, "%s%s\n", tmp->print_date, *(tmp->desc));
210: tmp = tmp->next;
211: }
212: tmp = events;
213: while (tmp) {
1.5 pjanzen 214: events = tmp;
1.6 pjanzen 215: if (tmp->ldesc)
216: free(tmp->ldesc);
1.5 pjanzen 217: tmp = tmp->next;
218: free(events);
1.1 millert 219: }
220: closecal(fp);
221: }
222:
223: int
224: getfield(p, endp, flags)
225: char *p, **endp;
226: int *flags;
227: {
1.6 pjanzen 228: int val, var, i;
1.1 millert 229: char *start, savech;
230:
1.8 pjanzen 231: for (; !isdigit(*p) && !isalpha(*p) && *p != '*' && *p != '\t'; ++p)
1.1 millert 232: ;
1.6 pjanzen 233: if (*p == '*') { /* `*' is every month */
1.1 millert 234: *flags |= F_ISMONTH;
235: *endp = p+1;
1.6 pjanzen 236: return (-1); /* means 'every month' */
1.1 millert 237: }
238: if (isdigit(*p)) {
239: val = strtol(p, &p, 10); /* if 0, it's failure */
240: for (; !isdigit(*p) && !isalpha(*p) && *p != '*'; ++p)
241: ;
242: *endp = p;
243: return (val);
244: }
245: for (start = p; isalpha(*++p);)
246: ;
247:
248: /* Sunday-1 */
249: if (*p == '+' || *p == '-')
250: for(; isdigit(*++p);)
251: ;
252:
253: savech = *p;
254: *p = '\0';
255:
256: /* Month */
257: if ((val = getmonth(start)) != 0)
258: *flags |= F_ISMONTH;
259:
260: /* Day */
261: else if ((val = getday(start)) != 0) {
262: *flags |= F_ISDAY;
263:
264: /* variable weekday */
265: if ((var = getdayvar(start)) != 0) {
1.6 pjanzen 266: if (var <= 5 && var >= -4)
1.1 millert 267: val += var * 10;
268: #ifdef DEBUG
269: printf("var: %d\n", var);
270: #endif
271: }
272: }
273:
1.6 pjanzen 274: /* Try specials (Easter, Paskha, ...) */
1.1 millert 275: else {
1.6 pjanzen 276: for (i = 0; i < NUMEV; i++) {
277: if (strncasecmp(start, spev[i].name, spev[i].nlen) == 0) {
278: start += spev[i].nlen;
279: val = i + 1;
280: i = NUMEV + 1;
281: } else if (spev[i].uname != NULL &&
282: strncasecmp(start, spev[i].uname, spev[i].ulen) == 0) {
283: start += spev[i].ulen;
284: val = i + 1;
285: i = NUMEV + 1;
286: }
287: }
288: if (i > NUMEV) {
289: switch(*start) {
290: case '-':
291: case '+':
292: var = atoi(start);
293: if (var > 365 || var < -365)
294: return (0); /* Someone is just being silly */
295: val += (NUMEV + 1) * var;
296: /* We add one to the matching event and multiply by
297: * (NUMEV + 1) so as not to return 0 if there's a match.
298: * val will overflow if there is an obscenely large
299: * number of special events. */
300: break;
301: }
302: *flags |= F_SPECIAL;
303: }
304: if (!(*flags & F_SPECIAL)) {
305: /* undefined rest */
306: *p = savech;
307: return (0);
308: }
1.1 millert 309: }
1.8 pjanzen 310: for (*p = savech; !isdigit(*p) && !isalpha(*p) && *p != '*' && *p != '\t'; ++p)
1.1 millert 311: ;
312: *endp = p;
313: return (val);
314: }
315:
1.10 deraadt 316:
1.1 millert 317: FILE *
318: opencal()
319: {
1.13 pjanzen 320: int pdes[2];
1.10 deraadt 321: int fdin;
1.1 millert 322:
323: /* open up calendar file as stdin */
1.14 pjanzen 324: if ((fdin = open(calendarFile, O_RDONLY)) == -1) {
1.13 pjanzen 325: if (!doall) {
1.9 millert 326: char *home = getenv("HOME");
327: if (home == NULL || *home == '\0')
328: errx(1, "cannot get home directory");
1.10 deraadt 329: if (!(chdir(home) == 0 &&
330: chdir(calendarHome) == 0 &&
1.14 pjanzen 331: (fdin = open(calendarFile, O_RDONLY)) != -1))
1.18 deraadt 332: errx(1, "no calendar file: ``%s'' or ``~/%s/%s''",
1.2 millert 333: calendarFile, calendarHome, calendarFile);
1.1 millert 334: }
335: }
336: if (pipe(pdes) < 0)
337: return (NULL);
338: switch (vfork()) {
339: case -1: /* error */
340: (void)close(pdes[0]);
341: (void)close(pdes[1]);
342: return (NULL);
343: case 0:
1.10 deraadt 344: dup2(fdin, STDIN_FILENO);
345: /* child -- set stdout to pipe input */
1.1 millert 346: if (pdes[1] != STDOUT_FILENO) {
347: (void)dup2(pdes[1], STDOUT_FILENO);
348: (void)close(pdes[1]);
349: }
350: (void)close(pdes[0]);
1.13 pjanzen 351: /* Set stderr to /dev/null. Necessary so that cron does not
352: * wait for cpp to finish if it's running calendar -a.
353: */
354: if (doall) {
355: int fderr;
356: fderr = open(_PATH_DEVNULL, O_WRONLY, 0);
357: if (fderr == -1)
358: _exit(0);
359: (void)dup2(fderr, STDERR_FILENO);
360: (void)close(fderr);
361: }
1.12 deraadt 362: execl(_PATH_CPP, "cpp", "-P", "-I.", _PATH_INCLUDE, (char *)NULL);
1.1 millert 363: warn(_PATH_CPP);
364: _exit(1);
365: }
366: /* parent -- set stdin to pipe output */
367: (void)dup2(pdes[0], STDIN_FILENO);
368: (void)close(pdes[0]);
369: (void)close(pdes[1]);
370:
371: /* not reading all calendar files, just set output to stdout */
372: if (!doall)
373: return (stdout);
374:
375: /* set output to a temporary file, so if no output don't send mail */
1.13 pjanzen 376: return(tmpfile());
1.1 millert 377: }
378:
379: void
380: closecal(fp)
381: FILE *fp;
382: {
383: struct stat sbuf;
384: int nread, pdes[2], status;
385: char buf[1024];
386:
387: if (!doall)
388: return;
389:
390: (void)rewind(fp);
391: if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
392: goto done;
393: if (pipe(pdes) < 0)
394: goto done;
395: switch (vfork()) {
396: case -1: /* error */
397: (void)close(pdes[0]);
398: (void)close(pdes[1]);
399: goto done;
400: case 0:
401: /* child -- set stdin to pipe output */
402: if (pdes[0] != STDIN_FILENO) {
403: (void)dup2(pdes[0], STDIN_FILENO);
404: (void)close(pdes[0]);
405: }
406: (void)close(pdes[1]);
407: execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
1.12 deraadt 408: "\"Reminder Service\"", (char *)NULL);
1.1 millert 409: warn(_PATH_SENDMAIL);
410: _exit(1);
411: }
412: /* parent -- write to pipe input */
413: (void)close(pdes[0]);
414:
415: header[1].iov_base = header[3].iov_base = pw->pw_name;
416: header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
417: writev(pdes[1], header, 7);
418: while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
419: (void)write(pdes[1], buf, nread);
420: (void)close(pdes[1]);
421: done: (void)fclose(fp);
1.10 deraadt 422: while (wait(&status) >= 0)
423: ;
1.5 pjanzen 424: }
425:
426:
427: void
428: insert(head, cur_evt)
429: struct event **head;
430: struct event *cur_evt;
431: {
432: struct event *tmp, *tmp2;
433:
434: if (*head) {
435: /* Insert this one in order */
436: tmp = *head;
437: tmp2 = NULL;
438: while (tmp->next &&
439: tmp->when <= cur_evt->when) {
440: tmp2 = tmp;
441: tmp = tmp->next;
442: }
443: if (tmp->when > cur_evt->when) {
444: cur_evt->next = tmp;
445: if (tmp2)
446: tmp2->next = cur_evt;
447: else
448: *head = cur_evt;
449: } else {
450: cur_evt->next = tmp->next;
451: tmp->next = cur_evt;
452: }
453: } else {
454: *head = cur_evt;
455: cur_evt->next = NULL;
456: }
1.1 millert 457: }