io.c revision 15720
1/*
2 * Copyright (c) 1989, 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1989, 1993\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static const char sccsid[] = "@(#)calendar.c  8.3 (Berkeley) 3/25/94";
42#endif /* not lint */
43
44#include <sys/param.h>
45#include <stdio.h>
46#include <ctype.h>
47#include <sys/types.h>
48#include <sys/stat.h>
49#include <unistd.h>
50#include <err.h>
51#include <errno.h>
52#include <locale.h>
53#include <string.h>
54#include <sys/uio.h>
55#include <sys/time.h>
56#include <stdlib.h>
57#include <pwd.h>
58#include <sys/wait.h>
59
60#include "pathnames.h"
61#include "calendar.h"
62
63
64char *calendarFile = "calendar";  /* default calendar file */
65char *calendarHome = ".calendar"; /* HOME */
66char *calendarNoMail = "nomail";  /* don't sent mail if this file exist */
67
68struct fixs neaster, npaskha;
69
70struct iovec header[] = {
71	{"From: ", 6},
72	{NULL, 0},
73	{" (Reminder Service)\nTo: ", 24},
74	{NULL, 0},
75	{"\nSubject: ", 10},
76	{NULL, 0},
77	{"'s Calendar\nPrecedence: bulk\n\n",  30},
78};
79
80
81void
82cal()
83{
84	register int printing;
85	register char *p;
86	FILE *fp;
87	int ch, l;
88	int month;
89	int day;
90	int var;
91	char buf[2048 + 1];
92
93	if ((fp = opencal()) == NULL)
94		return;
95	for (printing = 0; fgets(buf, sizeof(buf), stdin) != NULL;) {
96		if ((p = strchr(buf, '\n')) != NULL)
97			*p = '\0';
98		else
99			while ((ch = getchar()) != '\n' && ch != EOF);
100		for (l = strlen(buf);
101		     l > 0 && isspace((unsigned char)buf[l - 1]);
102		     l--)
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();
110			continue;
111		}
112		if (strncasecmp(buf, "Easter=", 7) == 0 && buf[7]) {
113			if (neaster.name != NULL)
114				free(neaster.name);
115			neaster.name = strdup(buf + 7);
116			neaster.len = strlen(buf + 7);
117			continue;
118		}
119		if (strncasecmp(buf, "Paskha=", 7) == 0 && buf[7]) {
120			if (npaskha.name != NULL)
121				free(npaskha.name);
122			npaskha.name = strdup(buf + 7);
123			npaskha.len = strlen(buf + 7);
124			continue;
125		}
126		if (buf[0] != '\t') {
127			printing = isnow(buf, &month, &day, &var) ? 1 : 0;
128			if ((p = strchr(buf, '\t')) == NULL)
129				continue;
130			if (p > buf && p[-1] == '*')
131				var = 1;
132			if (printing)
133				(void)fprintf(fp, "%.2d/%.2d%c%s\n", month,
134				    day, var ? '*' : ' ', p);
135		}
136		else if (printing)
137			fprintf(fp, "%s\n", buf);
138	}
139	closecal(fp);
140}
141
142int
143getfield(p, endp, flags)
144	char *p, **endp;
145	int *flags;
146{
147	int val, var;
148	char *start, savech;
149
150	for (; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p) && *p != '*'; ++p);
151	if (*p == '*') {			/* `*' is current month */
152		*flags |= F_ISMONTH;
153		*endp = p+1;
154		return (tp->tm_mon + 1);
155	}
156	if (isdigit((unsigned char)*p)) {
157		val = strtol(p, &p, 10);	/* if 0, it's failure */
158		for (; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p) && *p != '*'; ++p);
159		*endp = p;
160		return (val);
161	}
162	for (start = p; isalpha((unsigned char)*++p););
163
164	/* Sunday-1 */
165	if (*p == '+' || *p == '-')
166	    for(; isdigit((unsigned char)*++p););
167
168	savech = *p;
169	*p = '\0';
170
171	/* Month */
172	if ((val = getmonth(start)) != 0)
173		*flags |= F_ISMONTH;
174
175	/* Day */
176	else if ((val = getday(start)) != 0) {
177	    *flags |= F_ISDAY;
178
179	    /* variable weekday */
180	    if ((var = getdayvar(start)) != 0) {
181		if (var <=5 && var >= -4)
182		    val += var * 10;
183#ifdef DEBUG
184		printf("var: %d\n", var);
185#endif
186	    }
187	}
188
189	/* Easter */
190	else if ((val = geteaster(start, tp->tm_year + 1900)) != 0)
191	    *flags |= F_EASTER;
192
193	/* Paskha */
194	else if ((val = getpaskha(start, tp->tm_year + 1900)) != 0)
195	    *flags |= F_EASTER;
196
197	/* undefined rest */
198	else {
199		*p = savech;
200		return (0);
201	}
202	for (*p = savech; !isdigit((unsigned char)*p) && !isalpha((unsigned char)*p) && *p != '*'; ++p);
203	*endp = p;
204	return (val);
205}
206
207char path[MAXPATHLEN + 1];
208
209FILE *
210opencal()
211{
212	int fd, pdes[2];
213	struct stat sbuf;
214
215	/* open up calendar file as stdin */
216	if (!freopen(calendarFile, "r", stdin)) {
217		if (doall) {
218		    if (chdir(calendarHome) != 0)
219			return (NULL);
220		    if (stat(calendarNoMail, &sbuf) == 0)
221		        return (NULL);
222		    if (!freopen(calendarFile, "r", stdin))
223		        return (NULL);
224		} else {
225		        chdir(getenv("HOME"));
226			if (!(chdir(calendarHome) == 0 &&
227			      freopen(calendarFile, "r", stdin)))
228				errx(1, "no calendar file: ``%s'' or ``~/%s/%s\n", calendarFile, calendarHome, calendarFile);
229		}
230	}
231	if (pipe(pdes) < 0)
232		return (NULL);
233	switch (vfork()) {
234	case -1:			/* error */
235		(void)close(pdes[0]);
236		(void)close(pdes[1]);
237		return (NULL);
238	case 0:
239		/* child -- stdin already setup, set stdout to pipe input */
240		if (pdes[1] != STDOUT_FILENO) {
241			(void)dup2(pdes[1], STDOUT_FILENO);
242			(void)close(pdes[1]);
243		}
244		(void)close(pdes[0]);
245		execl(_PATH_CPP, "cpp", "-P", "-I.", _PATH_INCLUDE, NULL);
246		(void)fprintf(stderr,
247		    "calendar: execl: %s: %s.\n", _PATH_CPP, strerror(errno));
248		_exit(1);
249	}
250	/* parent -- set stdin to pipe output */
251	(void)dup2(pdes[0], STDIN_FILENO);
252	(void)close(pdes[0]);
253	(void)close(pdes[1]);
254
255	/* not reading all calendar files, just set output to stdout */
256	if (!doall)
257		return (stdout);
258
259	/* set output to a temporary file, so if no output don't send mail */
260	(void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
261	if ((fd = mkstemp(path)) < 0)
262		return (NULL);
263	return (fdopen(fd, "w+"));
264}
265
266void
267closecal(fp)
268	FILE *fp;
269{
270	struct stat sbuf;
271	int nread, pdes[2], status;
272	char buf[1024];
273
274	if (!doall)
275		return;
276
277	(void)rewind(fp);
278	if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
279		goto done;
280	if (pipe(pdes) < 0)
281		goto done;
282	switch (vfork()) {
283	case -1:			/* error */
284		(void)close(pdes[0]);
285		(void)close(pdes[1]);
286		goto done;
287	case 0:
288		/* child -- set stdin to pipe output */
289		if (pdes[0] != STDIN_FILENO) {
290			(void)dup2(pdes[0], STDIN_FILENO);
291			(void)close(pdes[0]);
292		}
293		(void)close(pdes[1]);
294		execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
295		    "\"Reminder Service\"", "-f", "root", NULL);
296		(void)fprintf(stderr,
297		    "calendar: %s: %s.\n", _PATH_SENDMAIL, strerror(errno));
298		_exit(1);
299	}
300	/* parent -- write to pipe input */
301	(void)close(pdes[0]);
302
303	header[1].iov_base = header[3].iov_base = pw->pw_name;
304	header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
305	writev(pdes[1], header, 7);
306	while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
307		(void)write(pdes[1], buf, nread);
308	(void)close(pdes[1]);
309done:	(void)fclose(fp);
310	(void)unlink(path);
311	while (wait(&status) >= 0);
312}
313
314