Annotation of src/usr.bin/calendar/io.c, Revision 1.24
1.24 ! millert 1: /* $OpenBSD: io.c,v 1.23 2003/04/06 19:59:12 grange 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.24 ! millert 42: static char rcsid[] = "$OpenBSD: io.c,v 1.23 2003/04/06 19:59:12 grange 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;
121: }
1.17 mickey 122: if (bodun_maybe && strncmp(buf, "BODUN=", 6) == 0) {
1.15 mickey 123: bodun++;
1.17 mickey 124: if (prefix)
125: free(prefix);
126: if ((prefix = strdup(buf + 6)) == NULL)
127: err(1, NULL);
128: }
1.6 pjanzen 129: /* User defined names for special events */
130: if ((p = strchr(buf, '='))) {
131: for (i = 0; i < NUMEV; i++) {
132: if (strncasecmp(buf, spev[i].name, spev[i].nlen) == 0 &&
133: (p - buf == spev[i].nlen) && buf[spev[i].nlen + 1]) {
134: p++;
135: if (spev[i].uname != NULL)
136: free(spev[i].uname);
137: if ((spev[i].uname = strdup(p)) == NULL)
1.11 pjanzen 138: err(1, NULL);
1.6 pjanzen 139: spev[i].ulen = strlen(p);
140: i = NUMEV + 1;
141: }
142: }
143: if (i > NUMEV)
1.1 millert 144: continue;
145: }
146: if (buf[0] != '\t') {
1.15 mickey 147: printing = (m = isnow(buf, bodun)) ? 1 : 0;
1.6 pjanzen 148: if ((p = strchr(buf, '\t')) == NULL) {
149: printing = 0;
1.1 millert 150: continue;
1.6 pjanzen 151: }
1.5 pjanzen 152: /* Need the following to catch hardwired "variable"
153: * dates */
1.1 millert 154: if (p > buf && p[-1] == '*')
155: var = 1;
1.5 pjanzen 156: else
157: var = 0;
1.1 millert 158: if (printing) {
1.5 pjanzen 159: struct match *foo;
160:
1.6 pjanzen 161: ev1 = NULL;
1.5 pjanzen 162: while (m) {
163: cur_evt = (struct event *) malloc(sizeof(struct event));
164: if (cur_evt == NULL)
1.11 pjanzen 165: err(1, NULL);
1.1 millert 166:
1.6 pjanzen 167: cur_evt->when = m->when;
168: snprintf(cur_evt->print_date,
169: sizeof(cur_evt->print_date), "%s%c",
170: m->print_date, (var + m->var) ? '*' : ' ');
171: if (ev1) {
172: cur_evt->desc = ev1->desc;
173: cur_evt->ldesc = NULL;
174: } else {
1.17 mickey 175: if (m->bodun && prefix) {
176: int l1 = strlen(prefix);
1.15 mickey 177: int l2 = strlen(p);
1.22 deraadt 178: int len = l1 + l2 + 2;
1.15 mickey 179: if ((cur_evt->ldesc =
1.22 deraadt 180: malloc(len)) == NULL)
1.17 mickey 181: err(1, "malloc");
1.22 deraadt 182: snprintf(cur_evt->ldesc, len,
1.17 mickey 183: "\t%s %s", prefix, p + 1);
1.15 mickey 184: } else if ((cur_evt->ldesc =
185: strdup(p)) == NULL)
1.11 pjanzen 186: err(1, NULL);
1.6 pjanzen 187: cur_evt->desc = &(cur_evt->ldesc);
188: ev1 = cur_evt;
1.5 pjanzen 189: }
190: insert(&events, cur_evt);
191: foo = m;
192: m = m->next;
193: free(foo);
194: }
1.1 millert 195: }
196: }
1.5 pjanzen 197: else if (printing) {
1.23 grange 198: nlen = strlen(ev1->ldesc) + strlen(buf) + 2;
199: if ((ev1->ldesc = realloc(ev1->ldesc, nlen)) == NULL)
1.11 pjanzen 200: err(1, NULL);
1.23 grange 201: snprintf(ev1->ldesc, nlen, "%s\n%s", ev1->ldesc, buf);
1.5 pjanzen 202: }
203: }
204: tmp = events;
205: while (tmp) {
1.6 pjanzen 206: (void)fprintf(fp, "%s%s\n", tmp->print_date, *(tmp->desc));
207: tmp = tmp->next;
208: }
209: tmp = events;
210: while (tmp) {
1.5 pjanzen 211: events = tmp;
1.6 pjanzen 212: if (tmp->ldesc)
213: free(tmp->ldesc);
1.5 pjanzen 214: tmp = tmp->next;
215: free(events);
1.1 millert 216: }
217: closecal(fp);
218: }
219:
220: int
221: getfield(p, endp, flags)
222: char *p, **endp;
223: int *flags;
224: {
1.6 pjanzen 225: int val, var, i;
1.1 millert 226: char *start, savech;
227:
1.8 pjanzen 228: for (; !isdigit(*p) && !isalpha(*p) && *p != '*' && *p != '\t'; ++p)
1.1 millert 229: ;
1.6 pjanzen 230: if (*p == '*') { /* `*' is every month */
1.1 millert 231: *flags |= F_ISMONTH;
232: *endp = p+1;
1.6 pjanzen 233: return (-1); /* means 'every month' */
1.1 millert 234: }
235: if (isdigit(*p)) {
236: val = strtol(p, &p, 10); /* if 0, it's failure */
237: for (; !isdigit(*p) && !isalpha(*p) && *p != '*'; ++p)
238: ;
239: *endp = p;
240: return (val);
241: }
242: for (start = p; isalpha(*++p);)
243: ;
244:
245: /* Sunday-1 */
246: if (*p == '+' || *p == '-')
247: for(; isdigit(*++p);)
248: ;
249:
250: savech = *p;
251: *p = '\0';
252:
253: /* Month */
254: if ((val = getmonth(start)) != 0)
255: *flags |= F_ISMONTH;
256:
257: /* Day */
258: else if ((val = getday(start)) != 0) {
259: *flags |= F_ISDAY;
260:
261: /* variable weekday */
262: if ((var = getdayvar(start)) != 0) {
1.6 pjanzen 263: if (var <= 5 && var >= -4)
1.1 millert 264: val += var * 10;
265: #ifdef DEBUG
266: printf("var: %d\n", var);
267: #endif
268: }
269: }
270:
1.6 pjanzen 271: /* Try specials (Easter, Paskha, ...) */
1.1 millert 272: else {
1.6 pjanzen 273: for (i = 0; i < NUMEV; i++) {
274: if (strncasecmp(start, spev[i].name, spev[i].nlen) == 0) {
275: start += spev[i].nlen;
276: val = i + 1;
277: i = NUMEV + 1;
278: } else if (spev[i].uname != NULL &&
279: strncasecmp(start, spev[i].uname, spev[i].ulen) == 0) {
280: start += spev[i].ulen;
281: val = i + 1;
282: i = NUMEV + 1;
283: }
284: }
285: if (i > NUMEV) {
286: switch(*start) {
287: case '-':
288: case '+':
289: var = atoi(start);
290: if (var > 365 || var < -365)
291: return (0); /* Someone is just being silly */
292: val += (NUMEV + 1) * var;
293: /* We add one to the matching event and multiply by
294: * (NUMEV + 1) so as not to return 0 if there's a match.
295: * val will overflow if there is an obscenely large
296: * number of special events. */
297: break;
298: }
299: *flags |= F_SPECIAL;
300: }
301: if (!(*flags & F_SPECIAL)) {
302: /* undefined rest */
303: *p = savech;
304: return (0);
305: }
1.1 millert 306: }
1.8 pjanzen 307: for (*p = savech; !isdigit(*p) && !isalpha(*p) && *p != '*' && *p != '\t'; ++p)
1.1 millert 308: ;
309: *endp = p;
310: return (val);
311: }
312:
1.10 deraadt 313:
1.1 millert 314: FILE *
1.20 vincent 315: opencal(void)
1.1 millert 316: {
1.20 vincent 317: int pdes[2], fdin;
318: struct stat st;
1.1 millert 319:
320: /* open up calendar file as stdin */
1.20 vincent 321: if ((fdin = open(calendarFile, O_RDONLY)) == -1 ||
322: fstat(fdin, &st) == -1 || !S_ISREG(st.st_mode)) {
1.13 pjanzen 323: if (!doall) {
1.9 millert 324: char *home = getenv("HOME");
325: if (home == NULL || *home == '\0')
326: errx(1, "cannot get home directory");
1.10 deraadt 327: if (!(chdir(home) == 0 &&
328: chdir(calendarHome) == 0 &&
1.14 pjanzen 329: (fdin = open(calendarFile, O_RDONLY)) != -1))
1.18 deraadt 330: errx(1, "no calendar file: ``%s'' or ``~/%s/%s''",
1.2 millert 331: calendarFile, calendarHome, calendarFile);
1.1 millert 332: }
333: }
1.20 vincent 334:
1.1 millert 335: if (pipe(pdes) < 0)
336: return (NULL);
337: switch (vfork()) {
338: case -1: /* error */
339: (void)close(pdes[0]);
340: (void)close(pdes[1]);
341: return (NULL);
342: case 0:
1.10 deraadt 343: dup2(fdin, STDIN_FILENO);
344: /* child -- set stdout to pipe input */
1.1 millert 345: if (pdes[1] != STDOUT_FILENO) {
346: (void)dup2(pdes[1], STDOUT_FILENO);
347: (void)close(pdes[1]);
348: }
349: (void)close(pdes[0]);
1.20 vincent 350: /*
351: * Set stderr to /dev/null. Necessary so that cron does not
1.13 pjanzen 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: }