xref: /freebsd/usr.bin/calendar/io.c (revision 6be3386466ab79a84b48429ae66244f21526d3df)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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. Neither the name of the University nor the names of its contributors
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
37 
38 #if 0
39 #ifndef lint
40 static char sccsid[] = "@(#)calendar.c  8.3 (Berkeley) 3/25/94";
41 #endif
42 #endif
43 
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46 
47 #include <sys/param.h>
48 #include <sys/stat.h>
49 #include <sys/wait.h>
50 #include <ctype.h>
51 #include <err.h>
52 #include <errno.h>
53 #include <libutil.h>
54 #include <locale.h>
55 #include <pwd.h>
56 #include <stdbool.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <stringlist.h>
61 #include <time.h>
62 #include <unistd.h>
63 
64 #include "pathnames.h"
65 #include "calendar.h"
66 
67 enum {
68 	T_OK = 0,
69 	T_ERR,
70 	T_PROCESS,
71 };
72 
73 const char *calendarFile = "calendar";	/* default calendar file */
74 static const char *calendarHomes[] = {".calendar", _PATH_INCLUDE_LOCAL, _PATH_INCLUDE}; /* HOME */
75 static const char *calendarNoMail = "nomail";/* don't sent mail if file exist */
76 
77 static char path[MAXPATHLEN];
78 static const char *cal_home;
79 static const char *cal_dir;
80 static const char *cal_file;
81 static int cal_line;
82 
83 struct fixs neaster, npaskha, ncny, nfullmoon, nnewmoon;
84 struct fixs nmarequinox, nsepequinox, njunsolstice, ndecsolstice;
85 
86 static int cal_parse(FILE *in, FILE *out);
87 
88 static StringList *definitions = NULL;
89 static struct event *events[MAXCOUNT];
90 static char *extradata[MAXCOUNT];
91 
92 static char *
93 trimlr(char **buf)
94 {
95 	char *walk = *buf;
96 	char *sep;
97 	char *last;
98 
99 	while (isspace(*walk))
100 		walk++;
101 	*buf = walk;
102 
103 	sep = walk;
104 	while (*sep != '\0' && !isspace(*sep))
105 		sep++;
106 
107 	if (*sep != '\0') {
108 		last = sep + strlen(sep) - 1;
109 		while (last > walk && isspace(*last))
110 			last--;
111 		*(last+1) = 0;
112 	}
113 
114 	return (sep);
115 }
116 
117 static FILE *
118 cal_fopen(const char *file)
119 {
120 	FILE *fp;
121 	char *home = getenv("HOME");
122 	unsigned int i;
123 	struct stat sb;
124 	static bool warned = false;
125 	static char calendarhome[MAXPATHLEN];
126 
127 	if (home == NULL || *home == '\0') {
128 		warnx("Cannot get home directory");
129 		return (NULL);
130 	}
131 
132 	if (chdir(home) != 0) {
133 		warnx("Cannot enter home directory \"%s\"", home);
134 		return (NULL);
135 	}
136 
137 	for (i = 0; i < nitems(calendarHomes); i++) {
138 		if (snprintf(calendarhome, sizeof (calendarhome), calendarHomes[i],
139 			getlocalbase()) >= (int)sizeof (calendarhome))
140 			continue;
141 
142 		if (chdir(calendarhome) != 0)
143 			continue;
144 
145 		if ((fp = fopen(file, "r")) != NULL) {
146 			cal_home = home;
147 			cal_dir = calendarhome;
148 			cal_file = file;
149 			return (fp);
150 		}
151 	}
152 
153 	warnx("can't open calendar file \"%s\"", file);
154 	if (!warned && stat(_PATH_INCLUDE_LOCAL, &sb) != 0) {
155 		warnx("calendar data files now provided by calendar-data pkg.");
156 		warned = true;
157 	}
158 
159 	return (NULL);
160 }
161 
162 static char*
163 cal_path(void)
164 {
165 	static char buffer[MAXPATHLEN + 10];
166 
167 	if (cal_dir == NULL)
168 		snprintf(buffer, sizeof(buffer), "%s", cal_file);
169 	else if (cal_dir[0] == '/')
170 		snprintf(buffer, sizeof(buffer), "%s/%s", cal_dir, cal_file);
171 	else
172 		snprintf(buffer, sizeof(buffer), "%s/%s/%s", cal_home, cal_dir, cal_file);
173 	return (buffer);
174 }
175 
176 #define	WARN0(format)		   \
177 	warnx(format " in %s line %d", cal_path(), cal_line)
178 #define	WARN1(format, arg1)		   \
179 	warnx(format " in %s line %d", arg1, cal_path(), cal_line)
180 
181 static char*
182 cmptoken(char *line, const char* token)
183 {
184 	char len = strlen(token);
185 
186 	if (strncmp(line, token, len) != 0)
187 		return NULL;
188 	return (line + len);
189 }
190 
191 static int
192 token(char *line, FILE *out, int *skip, int *unskip)
193 {
194 	char *walk, *sep, a, c;
195 	const char *this_cal_home;
196 	const char *this_cal_dir;
197 	const char *this_cal_file;
198 	int this_cal_line;
199 
200 	while (isspace(*line))
201 		line++;
202 
203 	if (cmptoken(line, "endif")) {
204 		if (*skip + *unskip == 0) {
205 			WARN0("#endif without prior #ifdef or #ifndef");
206 			return (T_ERR);
207 		}
208 		if (*skip > 0)
209 			--*skip;
210 		else
211 			--*unskip;
212 
213 		return (T_OK);
214 	}
215 
216 	walk = cmptoken(line, "ifdef");
217 	if (walk != NULL) {
218 		sep = trimlr(&walk);
219 
220 		if (*walk == '\0') {
221 			WARN0("Expecting arguments after #ifdef");
222 			return (T_ERR);
223 		}
224 		if (*sep != '\0') {
225 			WARN1("Expecting a single word after #ifdef "
226 			    "but got \"%s\"", walk);
227 			return (T_ERR);
228 		}
229 
230 		if (*skip != 0 ||
231 		    definitions == NULL || sl_find(definitions, walk) == NULL)
232 			++*skip;
233 		else
234 			++*unskip;
235 
236 		return (T_OK);
237 	}
238 
239 	walk = cmptoken(line, "ifndef");
240 	if (walk != NULL) {
241 		sep = trimlr(&walk);
242 
243 		if (*walk == '\0') {
244 			WARN0("Expecting arguments after #ifndef");
245 			return (T_ERR);
246 		}
247 		if (*sep != '\0') {
248 			WARN1("Expecting a single word after #ifndef "
249 			    "but got \"%s\"", walk);
250 			return (T_ERR);
251 		}
252 
253 		if (*skip != 0 ||
254 		    (definitions != NULL && sl_find(definitions, walk) != NULL))
255 			++*skip;
256 		else
257 			++*unskip;
258 
259 		return (T_OK);
260 	}
261 
262 	walk = cmptoken(line, "else");
263 	if (walk != NULL) {
264 		(void)trimlr(&walk);
265 
266 		if (*walk != '\0') {
267 			WARN0("Expecting no arguments after #else");
268 			return (T_ERR);
269 		}
270 		if (*skip + *unskip == 0) {
271 			WARN0("#else without prior #ifdef or #ifndef");
272 			return (T_ERR);
273 		}
274 
275 		if (*skip == 0) {
276 			++*skip;
277 			--*unskip;
278 		} else if (*skip == 1) {
279 			--*skip;
280 			++*unskip;
281 		}
282 
283 		return (T_OK);
284 	}
285 
286 	if (*skip != 0)
287 		return (T_OK);
288 
289 	walk = cmptoken(line, "include");
290 	if (walk != NULL) {
291 		(void)trimlr(&walk);
292 
293 		if (*walk == '\0') {
294 			WARN0("Expecting arguments after #include");
295 			return (T_ERR);
296 		}
297 
298 		if (*walk != '<' && *walk != '\"') {
299 			WARN0("Excecting '<' or '\"' after #include");
300 			return (T_ERR);
301 		}
302 
303 		a = *walk == '<' ? '>' : '\"';
304 		walk++;
305 		c = walk[strlen(walk) - 1];
306 
307 		if (a != c) {
308 			WARN1("Unterminated include expecting '%c'", a);
309 			return (T_ERR);
310 		}
311 		walk[strlen(walk) - 1] = '\0';
312 
313 		this_cal_home = cal_home;
314 		this_cal_dir = cal_dir;
315 		this_cal_file = cal_file;
316 		this_cal_line = cal_line;
317 		if (cal_parse(cal_fopen(walk), out))
318 			return (T_ERR);
319 		cal_home = this_cal_home;
320 		cal_dir = this_cal_dir;
321 		cal_file = this_cal_file;
322 		cal_line = this_cal_line;
323 
324 		return (T_OK);
325 	}
326 
327 	walk = cmptoken(line, "define");
328 	if (walk != NULL) {
329 		if (definitions == NULL)
330 			definitions = sl_init();
331 		sep = trimlr(&walk);
332 		*sep = '\0';
333 
334 		if (*walk == '\0') {
335 			WARN0("Expecting arguments after #define");
336 			return (T_ERR);
337 		}
338 
339 		if (sl_find(definitions, walk) == NULL)
340 			sl_add(definitions, strdup(walk));
341 		return (T_OK);
342 	}
343 
344 	walk = cmptoken(line, "undef");
345 	if (walk != NULL) {
346 		if (definitions != NULL) {
347 			sep = trimlr(&walk);
348 
349 			if (*walk == '\0') {
350 				WARN0("Expecting arguments after #undef");
351 				return (T_ERR);
352 			}
353 			if (*sep != '\0') {
354 				WARN1("Expecting a single word after #undef "
355 				    "but got \"%s\"", walk);
356 				return (T_ERR);
357 			}
358 
359 			walk = sl_find(definitions, walk);
360 			if (walk != NULL)
361 				walk[0] = '\0';
362 		}
363 		return (T_OK);
364 	}
365 
366 	walk = cmptoken(line, "warning");
367 	if (walk != NULL) {
368 		(void)trimlr(&walk);
369 		WARN1("Warning: %s", walk);
370 	}
371 
372 	walk = cmptoken(line, "error");
373 	if (walk != NULL) {
374 		(void)trimlr(&walk);
375 		WARN1("Error: %s", walk);
376 		return (T_ERR);
377 	}
378 
379 	WARN1("Undefined pre-processor command \"#%s\"", line);
380 	return (T_ERR);
381 }
382 
383 static void
384 setup_locale(const char *locale)
385 {
386 	(void)setlocale(LC_ALL, locale);
387 #ifdef WITH_ICONV
388 	if (!doall)
389 		set_new_encoding();
390 #endif
391 	setnnames();
392 }
393 
394 #define	REPLACE(string, slen, struct_) \
395 		if (strncasecmp(buf, (string), (slen)) == 0 && buf[(slen)]) { \
396 			if (struct_.name != NULL)			      \
397 				free(struct_.name);			      \
398 			if ((struct_.name = strdup(buf + (slen))) == NULL)    \
399 				errx(1, "cannot allocate memory");	      \
400 			struct_.len = strlen(buf + (slen));		      \
401 			continue;					      \
402 		}
403 static int
404 cal_parse(FILE *in, FILE *out)
405 {
406 	char *mylocale = NULL;
407 	char *line = NULL;
408 	char *buf;
409 	size_t linecap = 0;
410 	ssize_t linelen;
411 	ssize_t l;
412 	static int count = 0;
413 	int i;
414 	int month[MAXCOUNT];
415 	int day[MAXCOUNT];
416 	int year[MAXCOUNT];
417 	int skip = 0;
418 	int unskip = 0;
419 	char *pp, p;
420 	int flags;
421 	char *c, *cc;
422 	bool incomment = false;
423 
424 	if (in == NULL)
425 		return (1);
426 
427 	cal_line = 0;
428 	while ((linelen = getline(&line, &linecap, in)) > 0) {
429 		cal_line++;
430 		buf = line;
431 		if (buf[linelen - 1] == '\n')
432 			buf[--linelen] = '\0';
433 
434 		if (incomment) {
435 			c = strstr(buf, "*/");
436 			if (c) {
437 				c += 2;
438 				linelen -= c - buf;
439 				buf = c;
440 				incomment = false;
441 			} else {
442 				continue;
443 			}
444 		}
445 		if (!incomment) {
446 			do {
447 				c = strstr(buf, "//");
448 				cc = strstr(buf, "/*");
449 				if (c != NULL && (cc == NULL || c - cc < 0)) {
450 					/* single line comment */
451 					*c = '\0';
452 					linelen = c - buf;
453 					break;
454 				} else if (cc != NULL) {
455 					c = strstr(cc + 2, "*/");
456 					if (c != NULL) {
457 						/* multi-line comment ending on same line */
458 						c += 2;
459 						memmove(cc, c, buf + linelen + 1 - c);
460 						linelen -= c - cc;
461 					} else {
462 						/* multi-line comment */
463 						*cc = '\0';
464 						linelen = cc - buf;
465 						incomment = true;
466 						break;
467 					}
468 				}
469 			} while (c != NULL || cc != NULL);
470 		}
471 
472 		for (l = linelen;
473 		     l > 0 && isspace((unsigned char)buf[l - 1]);
474 		     l--)
475 			;
476 		buf[l] = '\0';
477 		if (buf[0] == '\0')
478 			continue;
479 
480 		if (buf == line && *buf == '#') {
481 			switch (token(buf+1, out, &skip, &unskip)) {
482 			case T_ERR:
483 				free(line);
484 				return (1);
485 			case T_OK:
486 				continue;
487 			case T_PROCESS:
488 				break;
489 			default:
490 				break;
491 			}
492 		}
493 
494 		if (skip != 0)
495 			continue;
496 
497 		/*
498 		 * Setting LANG in user's calendar was an old workaround
499 		 * for 'calendar -a' being run with C locale to properly
500 		 * print user's calendars in their native languages.
501 		 * Now that 'calendar -a' does fork with setusercontext(),
502 		 * and does not run iconv(), this variable has little use.
503 		 */
504 		if (strncmp(buf, "LANG=", 5) == 0) {
505 			if (mylocale == NULL)
506 				mylocale = strdup(setlocale(LC_ALL, NULL));
507 			setup_locale(buf + 5);
508 			continue;
509 		}
510 		/* Parse special definitions: Easter, Paskha etc */
511 		REPLACE("Easter=", 7, neaster);
512 		REPLACE("Paskha=", 7, npaskha);
513 		REPLACE("ChineseNewYear=", 15, ncny);
514 		REPLACE("NewMoon=", 8, nnewmoon);
515 		REPLACE("FullMoon=", 9, nfullmoon);
516 		REPLACE("MarEquinox=", 11, nmarequinox);
517 		REPLACE("SepEquinox=", 11, nsepequinox);
518 		REPLACE("JunSolstice=", 12, njunsolstice);
519 		REPLACE("DecSolstice=", 12, ndecsolstice);
520 		if (strncmp(buf, "SEQUENCE=", 9) == 0) {
521 			setnsequences(buf + 9);
522 			continue;
523 		}
524 
525 		/*
526 		 * If the line starts with a tab, the data has to be
527 		 * added to the previous line
528 		 */
529 		if (buf[0] == '\t') {
530 			for (i = 0; i < count; i++)
531 				event_continue(events[i], buf);
532 			continue;
533 		}
534 
535 		/* Get rid of leading spaces (non-standard) */
536 		while (isspace((unsigned char)buf[0]))
537 			memcpy(buf, buf + 1, strlen(buf));
538 
539 		/* No tab in the line, then not a valid line */
540 		if ((pp = strchr(buf, '\t')) == NULL)
541 			continue;
542 
543 		/* Trim spaces in front of the tab */
544 		while (isspace((unsigned char)pp[-1]))
545 			pp--;
546 
547 		p = *pp;
548 		*pp = '\0';
549 		if ((count = parsedaymonth(buf, year, month, day, &flags,
550 		    extradata)) == 0)
551 			continue;
552 		*pp = p;
553 		if (count < 0) {
554 			/* Show error status based on return value */
555 			if (debug)
556 				WARN1("Ignored: \"%s\"", buf);
557 			if (count == -1)
558 				continue;
559 			count = -count + 1;
560 		}
561 
562 		/* Find the last tab */
563 		while (pp[1] == '\t')
564 			pp++;
565 
566 		for (i = 0; i < count; i++) {
567 			if (debug)
568 				WARN1("got \"%s\"", pp);
569 			events[i] = event_add(year[i], month[i], day[i],
570 			    ((flags &= F_VARIABLE) != 0) ? 1 : 0, pp,
571 			    extradata[i]);
572 		}
573 	}
574 	while (skip-- > 0 || unskip-- > 0) {
575 		cal_line++;
576 		WARN0("Missing #endif assumed");
577 	}
578 
579 	free(line);
580 	fclose(in);
581 	if (mylocale != NULL) {
582 		setup_locale(mylocale);
583 		free(mylocale);
584 	}
585 
586 	return (0);
587 }
588 
589 void
590 cal(void)
591 {
592 	FILE *fpin;
593 	FILE *fpout;
594 	int i;
595 
596 	for (i = 0; i < MAXCOUNT; i++)
597 		extradata[i] = (char *)calloc(1, 20);
598 
599 
600 	if ((fpin = opencalin()) == NULL)
601 		return;
602 
603 	if ((fpout = opencalout()) == NULL) {
604 		fclose(fpin);
605 		return;
606 	}
607 
608 	if (cal_parse(fpin, fpout))
609 		return;
610 
611 	event_print_all(fpout);
612 	closecal(fpout);
613 }
614 
615 FILE *
616 opencalin(void)
617 {
618 	struct stat sbuf;
619 	FILE *fpin;
620 
621 	/* open up calendar file */
622 	cal_file = calendarFile;
623 	if ((fpin = fopen(calendarFile, "r")) == NULL) {
624 		if (doall) {
625 			if (chdir(calendarHomes[0]) != 0)
626 				return (NULL);
627 			if (stat(calendarNoMail, &sbuf) == 0)
628 				return (NULL);
629 			if ((fpin = fopen(calendarFile, "r")) == NULL)
630 				return (NULL);
631 		} else {
632 			fpin = cal_fopen(calendarFile);
633 		}
634 	}
635 	return (fpin);
636 }
637 
638 FILE *
639 opencalout(void)
640 {
641 	int fd;
642 
643 	/* not reading all calendar files, just set output to stdout */
644 	if (!doall)
645 		return (stdout);
646 
647 	/* set output to a temporary file, so if no output don't send mail */
648 	snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
649 	if ((fd = mkstemp(path)) < 0)
650 		return (NULL);
651 	return (fdopen(fd, "w+"));
652 }
653 
654 void
655 closecal(FILE *fp)
656 {
657 	struct stat sbuf;
658 	int nread, pdes[2], status;
659 	char buf[1024];
660 
661 	if (!doall)
662 		return;
663 
664 	rewind(fp);
665 	if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
666 		goto done;
667 	if (pipe(pdes) < 0)
668 		goto done;
669 	switch (fork()) {
670 	case -1:			/* error */
671 		(void)close(pdes[0]);
672 		(void)close(pdes[1]);
673 		goto done;
674 	case 0:
675 		/* child -- set stdin to pipe output */
676 		if (pdes[0] != STDIN_FILENO) {
677 			(void)dup2(pdes[0], STDIN_FILENO);
678 			(void)close(pdes[0]);
679 		}
680 		(void)close(pdes[1]);
681 		execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
682 		    "\"Reminder Service\"", (char *)NULL);
683 		warn(_PATH_SENDMAIL);
684 		_exit(1);
685 	}
686 	/* parent -- write to pipe input */
687 	(void)close(pdes[0]);
688 
689 	write(pdes[1], "From: \"Reminder Service\" <", 26);
690 	write(pdes[1], pw->pw_name, strlen(pw->pw_name));
691 	write(pdes[1], ">\nTo: <", 7);
692 	write(pdes[1], pw->pw_name, strlen(pw->pw_name));
693 	write(pdes[1], ">\nSubject: ", 11);
694 	write(pdes[1], dayname, strlen(dayname));
695 	write(pdes[1], "'s Calendar\nPrecedence: bulk\n\n", 30);
696 
697 	while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
698 		(void)write(pdes[1], buf, nread);
699 	(void)close(pdes[1]);
700 done:	(void)fclose(fp);
701 	(void)unlink(path);
702 	while (wait(&status) >= 0);
703 }
704