Annotation of src/usr.bin/calendar/io.c, Revision 1.14
1.14 ! pjanzen 1: /* $OpenBSD: io.c,v 1.13 2001/09/03 15:53:00 pjanzen 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.14 ! pjanzen 46: static char rcsid[] = "$OpenBSD: io.c,v 1.13 2001/09/03 15:53:00 pjanzen 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: {
87: register int printing;
88: register char *p;
89: FILE *fp;
1.6 pjanzen 90: int ch, l, i;
1.1 millert 91: int var;
92: char buf[2048 + 1];
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();
113: continue;
114: }
1.6 pjanzen 115: /* User defined names for special events */
116: if ((p = strchr(buf, '='))) {
117: for (i = 0; i < NUMEV; i++) {
118: if (strncasecmp(buf, spev[i].name, spev[i].nlen) == 0 &&
119: (p - buf == spev[i].nlen) && buf[spev[i].nlen + 1]) {
120: p++;
121: if (spev[i].uname != NULL)
122: free(spev[i].uname);
123: if ((spev[i].uname = strdup(p)) == NULL)
1.11 pjanzen 124: err(1, NULL);
1.6 pjanzen 125: spev[i].ulen = strlen(p);
126: i = NUMEV + 1;
127: }
128: }
129: if (i > NUMEV)
1.1 millert 130: continue;
131: }
132: if (buf[0] != '\t') {
1.5 pjanzen 133: printing = (m = isnow(buf)) ? 1 : 0;
1.6 pjanzen 134: if ((p = strchr(buf, '\t')) == NULL) {
135: printing = 0;
1.1 millert 136: continue;
1.6 pjanzen 137: }
1.5 pjanzen 138: /* Need the following to catch hardwired "variable"
139: * dates */
1.1 millert 140: if (p > buf && p[-1] == '*')
141: var = 1;
1.5 pjanzen 142: else
143: var = 0;
1.1 millert 144: if (printing) {
1.5 pjanzen 145: struct match *foo;
146:
1.6 pjanzen 147: ev1 = NULL;
1.5 pjanzen 148: while (m) {
149: cur_evt = (struct event *) malloc(sizeof(struct event));
150: if (cur_evt == NULL)
1.11 pjanzen 151: err(1, NULL);
1.1 millert 152:
1.6 pjanzen 153: cur_evt->when = m->when;
154: snprintf(cur_evt->print_date,
155: sizeof(cur_evt->print_date), "%s%c",
156: m->print_date, (var + m->var) ? '*' : ' ');
157: if (ev1) {
158: cur_evt->desc = ev1->desc;
159: cur_evt->ldesc = NULL;
160: } else {
161: if ((cur_evt->ldesc = strdup(p)) == NULL)
1.11 pjanzen 162: err(1, NULL);
1.6 pjanzen 163: cur_evt->desc = &(cur_evt->ldesc);
164: ev1 = cur_evt;
1.5 pjanzen 165: }
166: insert(&events, cur_evt);
167: foo = m;
168: m = m->next;
169: free(foo);
170: }
1.1 millert 171: }
172: }
1.5 pjanzen 173: else if (printing) {
1.6 pjanzen 174: if ((ev1->ldesc = realloc(ev1->ldesc,
175: (2 + strlen(ev1->ldesc) + strlen(buf)))) == NULL)
1.11 pjanzen 176: err(1, NULL);
1.6 pjanzen 177: strcat(ev1->ldesc, "\n");
178: strcat(ev1->ldesc, buf);
1.5 pjanzen 179: }
180: }
181: tmp = events;
182: while (tmp) {
1.6 pjanzen 183: (void)fprintf(fp, "%s%s\n", tmp->print_date, *(tmp->desc));
184: tmp = tmp->next;
185: }
186: tmp = events;
187: while (tmp) {
1.5 pjanzen 188: events = tmp;
1.6 pjanzen 189: if (tmp->ldesc)
190: free(tmp->ldesc);
1.5 pjanzen 191: tmp = tmp->next;
192: free(events);
1.1 millert 193: }
194: closecal(fp);
195: }
196:
197: int
198: getfield(p, endp, flags)
199: char *p, **endp;
200: int *flags;
201: {
1.6 pjanzen 202: int val, var, i;
1.1 millert 203: char *start, savech;
204:
1.8 pjanzen 205: for (; !isdigit(*p) && !isalpha(*p) && *p != '*' && *p != '\t'; ++p)
1.1 millert 206: ;
1.6 pjanzen 207: if (*p == '*') { /* `*' is every month */
1.1 millert 208: *flags |= F_ISMONTH;
209: *endp = p+1;
1.6 pjanzen 210: return (-1); /* means 'every month' */
1.1 millert 211: }
212: if (isdigit(*p)) {
213: val = strtol(p, &p, 10); /* if 0, it's failure */
214: for (; !isdigit(*p) && !isalpha(*p) && *p != '*'; ++p)
215: ;
216: *endp = p;
217: return (val);
218: }
219: for (start = p; isalpha(*++p);)
220: ;
221:
222: /* Sunday-1 */
223: if (*p == '+' || *p == '-')
224: for(; isdigit(*++p);)
225: ;
226:
227: savech = *p;
228: *p = '\0';
229:
230: /* Month */
231: if ((val = getmonth(start)) != 0)
232: *flags |= F_ISMONTH;
233:
234: /* Day */
235: else if ((val = getday(start)) != 0) {
236: *flags |= F_ISDAY;
237:
238: /* variable weekday */
239: if ((var = getdayvar(start)) != 0) {
1.6 pjanzen 240: if (var <= 5 && var >= -4)
1.1 millert 241: val += var * 10;
242: #ifdef DEBUG
243: printf("var: %d\n", var);
244: #endif
245: }
246: }
247:
1.6 pjanzen 248: /* Try specials (Easter, Paskha, ...) */
1.1 millert 249: else {
1.6 pjanzen 250: for (i = 0; i < NUMEV; i++) {
251: if (strncasecmp(start, spev[i].name, spev[i].nlen) == 0) {
252: start += spev[i].nlen;
253: val = i + 1;
254: i = NUMEV + 1;
255: } else if (spev[i].uname != NULL &&
256: strncasecmp(start, spev[i].uname, spev[i].ulen) == 0) {
257: start += spev[i].ulen;
258: val = i + 1;
259: i = NUMEV + 1;
260: }
261: }
262: if (i > NUMEV) {
263: switch(*start) {
264: case '-':
265: case '+':
266: var = atoi(start);
267: if (var > 365 || var < -365)
268: return (0); /* Someone is just being silly */
269: val += (NUMEV + 1) * var;
270: /* We add one to the matching event and multiply by
271: * (NUMEV + 1) so as not to return 0 if there's a match.
272: * val will overflow if there is an obscenely large
273: * number of special events. */
274: break;
275: }
276: *flags |= F_SPECIAL;
277: }
278: if (!(*flags & F_SPECIAL)) {
279: /* undefined rest */
280: *p = savech;
281: return (0);
282: }
1.1 millert 283: }
1.8 pjanzen 284: for (*p = savech; !isdigit(*p) && !isalpha(*p) && *p != '*' && *p != '\t'; ++p)
1.1 millert 285: ;
286: *endp = p;
287: return (val);
288: }
289:
1.10 deraadt 290:
1.1 millert 291: FILE *
292: opencal()
293: {
1.13 pjanzen 294: int pdes[2];
1.10 deraadt 295: int fdin;
1.1 millert 296:
297: /* open up calendar file as stdin */
1.14 ! pjanzen 298: if ((fdin = open(calendarFile, O_RDONLY)) == -1) {
1.13 pjanzen 299: if (!doall) {
1.9 millert 300: char *home = getenv("HOME");
301: if (home == NULL || *home == '\0')
302: errx(1, "cannot get home directory");
1.10 deraadt 303: if (!(chdir(home) == 0 &&
304: chdir(calendarHome) == 0 &&
1.14 ! pjanzen 305: (fdin = open(calendarFile, O_RDONLY)) != -1))
1.2 millert 306: errx(1, "no calendar file: ``%s'' or ``~/%s/%s",
307: calendarFile, calendarHome, calendarFile);
1.1 millert 308: }
309: }
310: if (pipe(pdes) < 0)
311: return (NULL);
312: switch (vfork()) {
313: case -1: /* error */
314: (void)close(pdes[0]);
315: (void)close(pdes[1]);
316: return (NULL);
317: case 0:
1.10 deraadt 318: dup2(fdin, STDIN_FILENO);
319: /* child -- set stdout to pipe input */
1.1 millert 320: if (pdes[1] != STDOUT_FILENO) {
321: (void)dup2(pdes[1], STDOUT_FILENO);
322: (void)close(pdes[1]);
323: }
324: (void)close(pdes[0]);
1.13 pjanzen 325: /* Set stderr to /dev/null. Necessary so that cron does not
326: * wait for cpp to finish if it's running calendar -a.
327: */
328: if (doall) {
329: int fderr;
330: fderr = open(_PATH_DEVNULL, O_WRONLY, 0);
331: if (fderr == -1)
332: _exit(0);
333: (void)dup2(fderr, STDERR_FILENO);
334: (void)close(fderr);
335: }
1.12 deraadt 336: execl(_PATH_CPP, "cpp", "-P", "-I.", _PATH_INCLUDE, (char *)NULL);
1.1 millert 337: warn(_PATH_CPP);
338: _exit(1);
339: }
340: /* parent -- set stdin to pipe output */
341: (void)dup2(pdes[0], STDIN_FILENO);
342: (void)close(pdes[0]);
343: (void)close(pdes[1]);
344:
345: /* not reading all calendar files, just set output to stdout */
346: if (!doall)
347: return (stdout);
348:
349: /* set output to a temporary file, so if no output don't send mail */
1.13 pjanzen 350: return(tmpfile());
1.1 millert 351: }
352:
353: void
354: closecal(fp)
355: FILE *fp;
356: {
357: struct stat sbuf;
358: int nread, pdes[2], status;
359: char buf[1024];
360:
361: if (!doall)
362: return;
363:
364: (void)rewind(fp);
365: if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
366: goto done;
367: if (pipe(pdes) < 0)
368: goto done;
369: switch (vfork()) {
370: case -1: /* error */
371: (void)close(pdes[0]);
372: (void)close(pdes[1]);
373: goto done;
374: case 0:
375: /* child -- set stdin to pipe output */
376: if (pdes[0] != STDIN_FILENO) {
377: (void)dup2(pdes[0], STDIN_FILENO);
378: (void)close(pdes[0]);
379: }
380: (void)close(pdes[1]);
381: execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
1.12 deraadt 382: "\"Reminder Service\"", (char *)NULL);
1.1 millert 383: warn(_PATH_SENDMAIL);
384: _exit(1);
385: }
386: /* parent -- write to pipe input */
387: (void)close(pdes[0]);
388:
389: header[1].iov_base = header[3].iov_base = pw->pw_name;
390: header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
391: writev(pdes[1], header, 7);
392: while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
393: (void)write(pdes[1], buf, nread);
394: (void)close(pdes[1]);
395: done: (void)fclose(fp);
1.10 deraadt 396: while (wait(&status) >= 0)
397: ;
1.5 pjanzen 398: }
399:
400:
401: void
402: insert(head, cur_evt)
403: struct event **head;
404: struct event *cur_evt;
405: {
406: struct event *tmp, *tmp2;
407:
408: if (*head) {
409: /* Insert this one in order */
410: tmp = *head;
411: tmp2 = NULL;
412: while (tmp->next &&
413: tmp->when <= cur_evt->when) {
414: tmp2 = tmp;
415: tmp = tmp->next;
416: }
417: if (tmp->when > cur_evt->when) {
418: cur_evt->next = tmp;
419: if (tmp2)
420: tmp2->next = cur_evt;
421: else
422: *head = cur_evt;
423: } else {
424: cur_evt->next = tmp->next;
425: tmp->next = cur_evt;
426: }
427: } else {
428: *head = cur_evt;
429: cur_evt->next = NULL;
430: }
1.1 millert 431: }