Annotation of src/usr.bin/calendar/io.c, Revision 1.41
1.41 ! zhuk 1: /* $OpenBSD: io.c,v 1.40 2015/10/23 11:01:30 zhuk 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: #include <sys/stat.h>
33: #include <sys/time.h>
34: #include <sys/types.h>
35: #include <sys/uio.h>
36: #include <sys/wait.h>
37:
38: #include <ctype.h>
39: #include <err.h>
40: #include <errno.h>
1.14 pjanzen 41: #include <fcntl.h>
1.1 millert 42: #include <locale.h>
43: #include <pwd.h>
44: #include <stdio.h>
45: #include <stdlib.h>
46: #include <string.h>
47: #include <unistd.h>
1.37 deraadt 48: #include <limits.h>
1.1 millert 49:
50: #include "pathnames.h"
51: #include "calendar.h"
52:
53:
54: struct iovec header[] = {
1.31 deraadt 55: { "From: ", 6 },
56: { NULL, 0 },
57: { " (Reminder Service)\nTo: ", 24 },
58: { NULL, 0 },
59: { "\nSubject: ", 10 },
60: { NULL, 0 },
1.33 deraadt 61: { "'s Calendar\nPrecedence: bulk\n", 29 },
62: { "Auto-Submitted: auto-generated\n\n", 32 },
1.1 millert 63: };
64:
65:
66: void
1.20 vincent 67: cal(void)
1.1 millert 68: {
1.31 deraadt 69: int ch, l, i, bodun = 0, bodun_maybe = 0, var, printing;
1.6 pjanzen 70: struct event *events, *cur_evt, *ev1, *tmp;
1.31 deraadt 71: char buf[2048 + 1], *prefix = NULL, *p;
1.5 pjanzen 72: struct match *m;
1.31 deraadt 73: FILE *fp;
1.1 millert 74:
1.5 pjanzen 75: events = NULL;
76: cur_evt = NULL;
1.1 millert 77: if ((fp = opencal()) == NULL)
78: return;
79: for (printing = 0; fgets(buf, sizeof(buf), stdin) != NULL;) {
80: if ((p = strchr(buf, '\n')) != NULL)
81: *p = '\0';
82: else
83: while ((ch = getchar()) != '\n' && ch != EOF);
1.10 deraadt 84: for (l = strlen(buf); l > 0 && isspace(buf[l - 1]); l--)
1.1 millert 85: ;
86: buf[l] = '\0';
87: if (buf[0] == '\0')
88: continue;
89: if (strncmp(buf, "LANG=", 5) == 0) {
90: (void) setlocale(LC_ALL, buf + 5);
91: setnnames();
1.40 zhuk 92: /* XXX remove KOI8 lines after 5.9 is out */
93: if (!strcmp(buf + 5, "ru_RU.UTF-8") ||
94: !strcmp(buf + 5, "uk_UA.UTF-8") ||
95: !strcmp(buf + 5, "by_BY.UTF-8")) ||
96: !strcmp(buf + 5, "ru_RU.KOI8-R") ||
1.15 mickey 97: !strcmp(buf + 5, "uk_UA.KOI8-U") ||
98: !strcmp(buf + 5, "by_BY.KOI8-B")) {
99: bodun_maybe++;
1.17 mickey 100: bodun = 0;
101: if (prefix)
102: free(prefix);
103: prefix = NULL;
1.15 mickey 104: } else
105: bodun_maybe = 0;
1.1 millert 106: continue;
1.27 mickey 107: } else if (strncmp(buf, "CALENDAR=", 9) == 0) {
108: char *ep;
109:
110: if (buf[9] == '\0')
111: calendar = 0;
112: else if (!strcasecmp(buf + 9, "julian")) {
113: calendar = JULIAN;
114: errno = 0;
115: julian = strtoul(buf + 14, &ep, 10);
116: if (buf[0] == '\0' || *ep != '\0')
117: julian = 13;
118: if ((errno == ERANGE && julian == ULONG_MAX) ||
119: julian > 14)
120: errx(1, "Julian calendar offset is too large");
121: } else if (!strcasecmp(buf + 9, "gregorian"))
122: calendar = GREGORIAN;
123: else if (!strcasecmp(buf + 9, "lunar"))
124: calendar = LUNAR;
125: } else 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);
1.27 mickey 131: continue;
1.17 mickey 132: }
1.6 pjanzen 133: /* User defined names for special events */
134: if ((p = strchr(buf, '='))) {
135: for (i = 0; i < NUMEV; i++) {
1.31 deraadt 136: if (strncasecmp(buf, spev[i].name,
137: spev[i].nlen) == 0 &&
138: (p - buf == spev[i].nlen) &&
139: buf[spev[i].nlen + 1]) {
140: p++;
141: if (spev[i].uname != NULL)
142: free(spev[i].uname);
143: if ((spev[i].uname = strdup(p)) == NULL)
144: err(1, NULL);
145: spev[i].ulen = strlen(p);
146: i = NUMEV + 1;
147: }
1.6 pjanzen 148: }
1.31 deraadt 149: if (i > NUMEV)
150: continue;
1.1 millert 151: }
152: if (buf[0] != '\t') {
1.15 mickey 153: printing = (m = isnow(buf, bodun)) ? 1 : 0;
1.6 pjanzen 154: if ((p = strchr(buf, '\t')) == NULL) {
155: printing = 0;
1.1 millert 156: continue;
1.6 pjanzen 157: }
1.5 pjanzen 158: /* Need the following to catch hardwired "variable"
159: * dates */
1.1 millert 160: if (p > buf && p[-1] == '*')
161: var = 1;
1.5 pjanzen 162: else
163: var = 0;
1.1 millert 164: if (printing) {
1.5 pjanzen 165: struct match *foo;
1.41 ! zhuk 166:
1.6 pjanzen 167: ev1 = NULL;
1.5 pjanzen 168: while (m) {
1.31 deraadt 169: cur_evt = malloc(sizeof(struct event));
170: if (cur_evt == NULL)
171: err(1, NULL);
1.41 ! zhuk 172:
1.31 deraadt 173: cur_evt->when = m->when;
174: snprintf(cur_evt->print_date,
175: sizeof(cur_evt->print_date), "%s%c",
176: m->print_date, (var + m->var) ? '*' : ' ');
177: if (ev1) {
178: cur_evt->desc = ev1->desc;
179: cur_evt->ldesc = NULL;
180: } else {
181: if (m->bodun && prefix) {
182: if (asprintf(&cur_evt->ldesc,
183: "\t%s %s", prefix,
184: p + 1) == -1)
185: err(1, NULL);
186: } else if ((cur_evt->ldesc =
187: strdup(p)) == NULL)
1.26 tom 188: err(1, NULL);
1.31 deraadt 189: cur_evt->desc = &(cur_evt->ldesc);
190: ev1 = cur_evt;
191: }
192: insert(&events, cur_evt);
193: foo = m;
194: m = m->next;
195: free(foo);
1.5 pjanzen 196: }
1.1 millert 197: }
1.30 deraadt 198: } else if (printing) {
1.32 moritz 199: if (asprintf(&p, "%s\n%s", ev1->ldesc,
1.30 deraadt 200: buf) == -1)
1.11 pjanzen 201: err(1, NULL);
1.32 moritz 202: free(ev1->ldesc);
203: ev1->ldesc = p;
1.5 pjanzen 204: }
205: }
206: tmp = events;
207: while (tmp) {
1.6 pjanzen 208: (void)fprintf(fp, "%s%s\n", tmp->print_date, *(tmp->desc));
209: tmp = tmp->next;
210: }
211: tmp = events;
212: while (tmp) {
1.5 pjanzen 213: events = tmp;
1.6 pjanzen 214: if (tmp->ldesc)
215: free(tmp->ldesc);
1.5 pjanzen 216: tmp = tmp->next;
217: free(events);
1.1 millert 218: }
219: closecal(fp);
220: }
221:
222: int
1.31 deraadt 223: getfield(char *p, char **endp, int *flags)
1.1 millert 224: {
1.6 pjanzen 225: int val, var, i;
1.1 millert 226: char *start, savech;
227:
1.36 deraadt 228: for (; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p) &&
229: *p != '*' && *p != '\t'; ++p)
1.1 millert 230: ;
1.6 pjanzen 231: if (*p == '*') { /* `*' is every month */
1.1 millert 232: *flags |= F_ISMONTH;
233: *endp = p+1;
1.6 pjanzen 234: return (-1); /* means 'every month' */
1.1 millert 235: }
1.36 deraadt 236: if (isdigit((unsigned char)*p)) {
1.1 millert 237: val = strtol(p, &p, 10); /* if 0, it's failure */
1.36 deraadt 238: for (; !isdigit((unsigned char)*p) &&
239: !isalpha((unsigned char)*p) && *p != '*'; ++p)
1.1 millert 240: ;
241: *endp = p;
242: return (val);
243: }
1.36 deraadt 244: for (start = p; isalpha((unsigned char)*++p);)
1.1 millert 245: ;
246:
247: /* Sunday-1 */
248: if (*p == '+' || *p == '-')
1.36 deraadt 249: for(; isdigit((unsigned char)*++p); )
1.31 deraadt 250: ;
1.1 millert 251:
252: savech = *p;
253: *p = '\0';
254:
255: /* Month */
256: if ((val = getmonth(start)) != 0)
257: *flags |= F_ISMONTH;
258:
259: /* Day */
260: else if ((val = getday(start)) != 0) {
1.31 deraadt 261: *flags |= F_ISDAY;
1.1 millert 262:
1.31 deraadt 263: /* variable weekday */
264: if ((var = getdayvar(start)) != 0) {
265: if (var <= 5 && var >= -4)
266: val += var * 10;
1.1 millert 267: #ifdef DEBUG
1.31 deraadt 268: printf("var: %d\n", var);
1.1 millert 269: #endif
1.31 deraadt 270: }
1.1 millert 271: }
272:
1.6 pjanzen 273: /* Try specials (Easter, Paskha, ...) */
1.1 millert 274: else {
1.6 pjanzen 275: for (i = 0; i < NUMEV; i++) {
276: if (strncasecmp(start, spev[i].name, spev[i].nlen) == 0) {
277: start += spev[i].nlen;
278: val = i + 1;
279: i = NUMEV + 1;
280: } else if (spev[i].uname != NULL &&
281: strncasecmp(start, spev[i].uname, spev[i].ulen) == 0) {
282: start += spev[i].ulen;
283: val = i + 1;
284: i = NUMEV + 1;
285: }
286: }
287: if (i > NUMEV) {
1.39 deraadt 288: const char *errstr;
289:
290: switch (*start) {
1.6 pjanzen 291: case '-':
292: case '+':
1.39 deraadt 293: var = strtonum(start + 1, 0, 365, &errstr);
294: if (errstr)
1.31 deraadt 295: return (0); /* Someone is just being silly */
1.39 deraadt 296: if (*start == '-')
297: var = -var;
1.31 deraadt 298: val += (NUMEV + 1) * var;
299: /* We add one to the matching event and multiply by
300: * (NUMEV + 1) so as not to return 0 if there's a match.
301: * val will overflow if there is an obscenely large
302: * number of special events. */
303: break;
1.6 pjanzen 304: }
1.41 ! zhuk 305: *flags |= F_SPECIAL;
1.6 pjanzen 306: }
307: if (!(*flags & F_SPECIAL)) {
1.31 deraadt 308: /* undefined rest */
1.6 pjanzen 309: *p = savech;
310: return (0);
311: }
1.1 millert 312: }
1.36 deraadt 313: for (*p = savech; !isdigit((unsigned char)*p) &&
314: !isalpha((unsigned char)*p) && *p != '*' && *p != '\t'; ++p)
1.1 millert 315: ;
316: *endp = p;
317: return (val);
318: }
319:
1.10 deraadt 320:
1.1 millert 321: FILE *
1.20 vincent 322: opencal(void)
1.1 millert 323: {
1.20 vincent 324: int pdes[2], fdin;
325: struct stat st;
1.1 millert 326:
327: /* open up calendar file as stdin */
1.20 vincent 328: if ((fdin = open(calendarFile, O_RDONLY)) == -1 ||
329: fstat(fdin, &st) == -1 || !S_ISREG(st.st_mode)) {
1.13 pjanzen 330: if (!doall) {
1.9 millert 331: char *home = getenv("HOME");
332: if (home == NULL || *home == '\0')
333: errx(1, "cannot get home directory");
1.10 deraadt 334: if (!(chdir(home) == 0 &&
335: chdir(calendarHome) == 0 &&
1.14 pjanzen 336: (fdin = open(calendarFile, O_RDONLY)) != -1))
1.18 deraadt 337: errx(1, "no calendar file: ``%s'' or ``~/%s/%s''",
1.2 millert 338: calendarFile, calendarHome, calendarFile);
1.1 millert 339: }
340: }
1.20 vincent 341:
1.1 millert 342: if (pipe(pdes) < 0)
343: return (NULL);
344: switch (vfork()) {
345: case -1: /* error */
346: (void)close(pdes[0]);
347: (void)close(pdes[1]);
348: return (NULL);
349: case 0:
1.10 deraadt 350: dup2(fdin, STDIN_FILENO);
351: /* child -- set stdout to pipe input */
1.1 millert 352: if (pdes[1] != STDOUT_FILENO) {
353: (void)dup2(pdes[1], STDOUT_FILENO);
354: (void)close(pdes[1]);
355: }
356: (void)close(pdes[0]);
1.41 ! zhuk 357: /*
1.20 vincent 358: * Set stderr to /dev/null. Necessary so that cron does not
1.13 pjanzen 359: * wait for cpp to finish if it's running calendar -a.
360: */
361: if (doall) {
362: int fderr;
363: fderr = open(_PATH_DEVNULL, O_WRONLY, 0);
364: if (fderr == -1)
365: _exit(0);
366: (void)dup2(fderr, STDERR_FILENO);
367: (void)close(fderr);
368: }
1.29 otto 369: execl(_PATH_CPP, "cpp", "-traditional", "-undef", "-U__GNUC__",
370: "-P", "-I.", _PATH_INCLUDE, (char *)NULL);
1.1 millert 371: warn(_PATH_CPP);
372: _exit(1);
373: }
374: /* parent -- set stdin to pipe output */
375: (void)dup2(pdes[0], STDIN_FILENO);
376: (void)close(pdes[0]);
377: (void)close(pdes[1]);
378:
379: /* not reading all calendar files, just set output to stdout */
380: if (!doall)
381: return (stdout);
382:
383: /* set output to a temporary file, so if no output don't send mail */
1.13 pjanzen 384: return(tmpfile());
1.1 millert 385: }
386:
387: void
1.31 deraadt 388: closecal(FILE *fp)
1.1 millert 389: {
390: struct stat sbuf;
391: int nread, pdes[2], status;
392: char buf[1024];
393:
394: if (!doall)
395: return;
396:
397: (void)rewind(fp);
398: if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
399: goto done;
400: if (pipe(pdes) < 0)
401: goto done;
402: switch (vfork()) {
403: case -1: /* error */
404: (void)close(pdes[0]);
405: (void)close(pdes[1]);
406: goto done;
407: case 0:
408: /* child -- set stdin to pipe output */
409: if (pdes[0] != STDIN_FILENO) {
410: (void)dup2(pdes[0], STDIN_FILENO);
411: (void)close(pdes[0]);
412: }
413: (void)close(pdes[1]);
414: execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
1.12 deraadt 415: "\"Reminder Service\"", (char *)NULL);
1.1 millert 416: warn(_PATH_SENDMAIL);
417: _exit(1);
418: }
419: /* parent -- write to pipe input */
420: (void)close(pdes[0]);
421:
422: header[1].iov_base = header[3].iov_base = pw->pw_name;
423: header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
1.33 deraadt 424: writev(pdes[1], header, 8);
1.1 millert 425: while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
426: (void)write(pdes[1], buf, nread);
427: (void)close(pdes[1]);
428: done: (void)fclose(fp);
1.10 deraadt 429: while (wait(&status) >= 0)
430: ;
1.5 pjanzen 431: }
432:
433:
434: void
1.31 deraadt 435: insert(struct event **head, struct event *cur_evt)
1.5 pjanzen 436: {
437: struct event *tmp, *tmp2;
438:
439: if (*head) {
440: /* Insert this one in order */
441: tmp = *head;
442: tmp2 = NULL;
443: while (tmp->next &&
444: tmp->when <= cur_evt->when) {
445: tmp2 = tmp;
446: tmp = tmp->next;
447: }
448: if (tmp->when > cur_evt->when) {
449: cur_evt->next = tmp;
450: if (tmp2)
451: tmp2->next = cur_evt;
452: else
453: *head = cur_evt;
454: } else {
455: cur_evt->next = tmp->next;
456: tmp->next = cur_evt;
457: }
458: } else {
459: *head = cur_evt;
460: cur_evt->next = NULL;
461: }
1.1 millert 462: }