2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
33 static const char copyright[] =
34 "@(#) Copyright (c) 1989, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
60 #include <stringlist.h>
64 #include "pathnames.h"
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 */
77 static char path[MAXPATHLEN];
79 struct fixs neaster, npaskha, ncny, nfullmoon, nnewmoon;
80 struct fixs nmarequinox, nsepequinox, njunsolstice, ndecsolstice;
82 static int cal_parse(FILE *in, FILE *out);
84 static StringList *definitions = NULL;
85 static struct event *events[MAXCOUNT];
86 static char *extradata[MAXCOUNT];
94 while (isspace(*walk))
97 last = walk + strlen(walk) - 1;
98 while (last > walk && isspace(*last))
107 cal_fopen(const char *file)
110 char *home = getenv("HOME");
113 static bool warned = false;
115 if (home == NULL || *home == '\0') {
116 warnx("Cannot get home directory");
120 if (chdir(home) != 0) {
121 warnx("Cannot enter home directory");
125 for (i = 0; i < nitems(calendarHomes); i++) {
126 if (chdir(calendarHomes[i]) != 0)
129 if ((fp = fopen(file, "r")) != NULL)
133 warnx("can't open calendar file \"%s\"", file);
134 if (!warned && stat(_PATH_INCLUDE_LOCAL, &sb) != 0) {
135 warnx("calendar data files now provided by calendar-data pkg.");
143 token(char *line, FILE *out, int *skip)
147 if (strncmp(line, "endif", 5) == 0) {
153 if (strncmp(line, "ifdef", 5) == 0) {
158 warnx("Expecting arguments after #ifdef");
162 if (*skip != 0 || definitions == NULL || sl_find(definitions, walk) == NULL)
168 if (strncmp(line, "ifndef", 6) == 0) {
173 warnx("Expecting arguments after #ifndef");
177 if (*skip != 0 || (definitions != NULL && sl_find(definitions, walk) != NULL))
183 if (strncmp(line, "else", 4) == 0) {
188 warnx("Expecting no arguments after #else");
203 if (strncmp(line, "include", 7) == 0) {
209 warnx("Expecting arguments after #include");
213 if (*walk != '<' && *walk != '\"') {
214 warnx("Excecting '<' or '\"' after #include");
220 c = walk[strlen(walk) - 1];
225 warnx("Unterminated include expecting '\"'");
231 warnx("Unterminated include expecting '>'");
236 warnx("Unterminated include expecting '%c'",
237 a == '<' ? '>' : '\"' );
240 walk[strlen(walk) - 1] = '\0';
242 if (cal_parse(cal_fopen(walk), out))
248 if (strncmp(line, "define", 6) == 0) {
249 if (definitions == NULL)
250 definitions = sl_init();
255 warnx("Expecting arguments after #define");
259 sl_add(definitions, strdup(walk));
267 #define REPLACE(string, slen, struct_) \
268 if (strncasecmp(buf, (string), (slen)) == 0 && buf[(slen)]) { \
269 if (struct_.name != NULL) \
270 free(struct_.name); \
271 if ((struct_.name = strdup(buf + (slen))) == NULL) \
272 errx(1, "cannot allocate memory"); \
273 struct_.len = strlen(buf + (slen)); \
277 cal_parse(FILE *in, FILE *out)
284 static int d_first = -1;
285 static int count = 0;
305 while ((linelen = getline(&line, &linecap, in)) > 0) {
307 switch (token(line+1, out, &skip)) {
325 l > 0 && isspace((unsigned char)buf[l - 1]);
333 * Setting LANG in user's calendar was an old workaround
334 * for 'calendar -a' being run with C locale to properly
335 * print user's calendars in their native languages.
336 * Now that 'calendar -a' does fork with setusercontext(),
337 * and does not run iconv(), this variable has little use.
339 if (strncmp(buf, "LANG=", 5) == 0) {
340 (void)setlocale(LC_ALL, buf + 5);
341 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
349 /* Parse special definitions: Easter, Paskha etc */
350 REPLACE("Easter=", 7, neaster);
351 REPLACE("Paskha=", 7, npaskha);
352 REPLACE("ChineseNewYear=", 15, ncny);
353 REPLACE("NewMoon=", 8, nnewmoon);
354 REPLACE("FullMoon=", 9, nfullmoon);
355 REPLACE("MarEquinox=", 11, nmarequinox);
356 REPLACE("SepEquinox=", 11, nsepequinox);
357 REPLACE("JunSolstice=", 12, njunsolstice);
358 REPLACE("DecSolstice=", 12, ndecsolstice);
359 if (strncmp(buf, "SEQUENCE=", 9) == 0) {
360 setnsequences(buf + 9);
365 * If the line starts with a tab, the data has to be
366 * added to the previous line
368 if (buf[0] == '\t') {
369 for (i = 0; i < count; i++)
370 event_continue(events[i], buf);
374 /* Get rid of leading spaces (non-standard) */
375 while (isspace((unsigned char)buf[0]))
376 memcpy(buf, buf + 1, strlen(buf));
378 /* No tab in the line, then not a valid line */
379 if ((pp = strchr(buf, '\t')) == NULL)
382 /* Trim spaces in front of the tab */
383 while (isspace((unsigned char)pp[-1]))
388 if ((count = parsedaymonth(buf, year, month, day, &flags,
393 /* Show error status based on return value */
395 fprintf(stderr, "Ignored: %s\n", buf);
401 /* Find the last tab */
402 while (pp[1] == '\t')
406 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
408 for (i = 0; i < count; i++) {
409 tm.tm_mon = month[i] - 1;
411 tm.tm_year = year[i] - 1900;
412 (void)strftime(dbuf, sizeof(dbuf),
413 d_first ? "%e %b" : "%b %e", &tm);
415 fprintf(stderr, "got %s\n", pp);
416 events[i] = event_add(year[i], month[i], day[i], dbuf,
417 ((flags &= F_VARIABLE) != 0) ? 1 : 0, pp,
435 for (i = 0; i < MAXCOUNT; i++)
436 extradata[i] = (char *)calloc(1, 20);
439 if ((fpin = opencalin()) == NULL)
442 if ((fpout = opencalout()) == NULL) {
447 if (cal_parse(fpin, fpout))
450 event_print_all(fpout);
460 /* open up calendar file */
461 if ((fpin = fopen(calendarFile, "r")) == NULL) {
463 if (chdir(calendarHomes[0]) != 0)
465 if (stat(calendarNoMail, &sbuf) == 0)
467 if ((fpin = fopen(calendarFile, "r")) == NULL)
470 fpin = cal_fopen(calendarFile);
481 /* not reading all calendar files, just set output to stdout */
485 /* set output to a temporary file, so if no output don't send mail */
486 snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
487 if ((fd = mkstemp(path)) < 0)
489 return (fdopen(fd, "w+"));
496 int nread, pdes[2], status;
503 if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
509 (void)close(pdes[0]);
510 (void)close(pdes[1]);
513 /* child -- set stdin to pipe output */
514 if (pdes[0] != STDIN_FILENO) {
515 (void)dup2(pdes[0], STDIN_FILENO);
516 (void)close(pdes[0]);
518 (void)close(pdes[1]);
519 execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
520 "\"Reminder Service\"", (char *)NULL);
521 warn(_PATH_SENDMAIL);
524 /* parent -- write to pipe input */
525 (void)close(pdes[0]);
527 write(pdes[1], "From: \"Reminder Service\" <", 26);
528 write(pdes[1], pw->pw_name, strlen(pw->pw_name));
529 write(pdes[1], ">\nTo: <", 7);
530 write(pdes[1], pw->pw_name, strlen(pw->pw_name));
531 write(pdes[1], ">\nSubject: ", 11);
532 write(pdes[1], dayname, strlen(dayname));
533 write(pdes[1], "'s Calendar\nPrecedence: bulk\n\n", 30);
535 while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
536 (void)write(pdes[1], buf, nread);
537 (void)close(pdes[1]);
538 done: (void)fclose(fp);
540 while (wait(&status) >= 0);