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