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];
78 static const char *cal_home;
79 static const char *cal_dir;
80 static const char *cal_file;
83 struct fixs neaster, npaskha, ncny, nfullmoon, nnewmoon;
84 struct fixs nmarequinox, nsepequinox, njunsolstice, ndecsolstice;
86 static int cal_parse(FILE *in, FILE *out);
88 static StringList *definitions = NULL;
89 static struct event *events[MAXCOUNT];
90 static char *extradata[MAXCOUNT];
98 while (isspace(*walk))
101 last = walk + strlen(walk) - 1;
102 while (last > walk && isspace(*last))
111 cal_fopen(const char *file)
114 char *home = getenv("HOME");
117 static bool warned = false;
119 if (home == NULL || *home == '\0') {
120 warnx("Cannot get home directory");
124 if (chdir(home) != 0) {
125 warnx("Cannot enter home directory \"%s\"", home);
129 for (i = 0; i < nitems(calendarHomes); i++) {
130 if (chdir(calendarHomes[i]) != 0)
133 if ((fp = fopen(file, "r")) != NULL) {
135 cal_dir = calendarHomes[i];
141 warnx("can't open calendar file \"%s\"", file);
142 if (!warned && stat(_PATH_INCLUDE_LOCAL, &sb) != 0) {
143 warnx("calendar data files now provided by calendar-data pkg.");
150 #define WARN0(format) \
151 warnx(format " in %s/%s/%s line %d", cal_home, cal_dir, cal_file, cal_line)
152 #define WARN1(format, arg1) \
153 warnx(format " in %s/%s/%s line %d", arg1, cal_home, cal_dir, cal_file, cal_line)
156 token(char *line, FILE *out, int *skip)
159 const char *this_cal_home;
160 const char *this_cal_dir;
161 const char *this_cal_file;
164 if (strncmp(line, "endif", 5) == 0) {
170 if (strncmp(line, "ifdef", 5) == 0) {
175 WARN0("Expecting arguments after #ifdef");
179 if (*skip != 0 || definitions == NULL || sl_find(definitions, walk) == NULL)
185 if (strncmp(line, "ifndef", 6) == 0) {
190 WARN0("Expecting arguments after #ifndef");
194 if (*skip != 0 || (definitions != NULL && sl_find(definitions, walk) != NULL))
200 if (strncmp(line, "else", 4) == 0) {
205 WARN0("Expecting no arguments after #else");
220 if (strncmp(line, "include", 7) == 0) {
226 WARN0("Expecting arguments after #include");
230 if (*walk != '<' && *walk != '\"') {
231 WARN0("Excecting '<' or '\"' after #include");
235 a = *walk == '<' ? '>' : '\"';
237 c = walk[strlen(walk) - 1];
240 WARN1("Unterminated include expecting '%c'", a);
243 walk[strlen(walk) - 1] = '\0';
245 this_cal_home = cal_home;
246 this_cal_dir = cal_dir;
247 this_cal_file = cal_file;
248 this_cal_line = cal_line;
249 if (cal_parse(cal_fopen(walk), out))
251 cal_home = this_cal_home;
252 cal_dir = this_cal_dir;
253 cal_file = this_cal_file;
254 cal_line = this_cal_line;
259 if (strncmp(line, "define", 6) == 0) {
260 if (definitions == NULL)
261 definitions = sl_init();
266 WARN0("Expecting arguments after #define");
270 sl_add(definitions, strdup(walk));
278 #define REPLACE(string, slen, struct_) \
279 if (strncasecmp(buf, (string), (slen)) == 0 && buf[(slen)]) { \
280 if (struct_.name != NULL) \
281 free(struct_.name); \
282 if ((struct_.name = strdup(buf + (slen))) == NULL) \
283 errx(1, "cannot allocate memory"); \
284 struct_.len = strlen(buf + (slen)); \
288 cal_parse(FILE *in, FILE *out)
295 static int d_first = -1;
296 static int count = 0;
307 bool incomment = false;
319 while ((linelen = getline(&line, &linecap, in)) > 0) {
322 if (buf[linelen - 1] == '\n')
323 buf[--linelen] = '\0';
326 c = strstr(buf, "*/");
338 c = strstr(buf, "//");
339 cc = strstr(buf, "/*");
340 if (c != NULL && (cc == NULL || c - cc < 0)) {
341 /* single line comment */
345 } else if (cc != NULL) {
346 c = strstr(cc + 2, "*/");
348 /* multi-line comment ending on same line */
350 memmove(cc, c, buf + linelen + 1 - c);
353 /* multi-line comment */
360 } while (c != NULL || cc != NULL);
364 l > 0 && isspace((unsigned char)buf[l - 1]);
371 if (buf == line && *buf == '#') {
372 switch (token(buf+1, out, &skip)) {
389 * Setting LANG in user's calendar was an old workaround
390 * for 'calendar -a' being run with C locale to properly
391 * print user's calendars in their native languages.
392 * Now that 'calendar -a' does fork with setusercontext(),
393 * and does not run iconv(), this variable has little use.
395 if (strncmp(buf, "LANG=", 5) == 0) {
396 (void)setlocale(LC_ALL, buf + 5);
397 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
405 /* Parse special definitions: Easter, Paskha etc */
406 REPLACE("Easter=", 7, neaster);
407 REPLACE("Paskha=", 7, npaskha);
408 REPLACE("ChineseNewYear=", 15, ncny);
409 REPLACE("NewMoon=", 8, nnewmoon);
410 REPLACE("FullMoon=", 9, nfullmoon);
411 REPLACE("MarEquinox=", 11, nmarequinox);
412 REPLACE("SepEquinox=", 11, nsepequinox);
413 REPLACE("JunSolstice=", 12, njunsolstice);
414 REPLACE("DecSolstice=", 12, ndecsolstice);
415 if (strncmp(buf, "SEQUENCE=", 9) == 0) {
416 setnsequences(buf + 9);
421 * If the line starts with a tab, the data has to be
422 * added to the previous line
424 if (buf[0] == '\t') {
425 for (i = 0; i < count; i++)
426 event_continue(events[i], buf);
430 /* Get rid of leading spaces (non-standard) */
431 while (isspace((unsigned char)buf[0]))
432 memcpy(buf, buf + 1, strlen(buf));
434 /* No tab in the line, then not a valid line */
435 if ((pp = strchr(buf, '\t')) == NULL)
438 /* Trim spaces in front of the tab */
439 while (isspace((unsigned char)pp[-1]))
444 if ((count = parsedaymonth(buf, year, month, day, &flags,
449 /* Show error status based on return value */
451 fprintf(stderr, "Ignored: \"%s\" in %s/%s/%s line %d\n",
452 buf, cal_home, cal_dir, cal_file, cal_line);
458 /* Find the last tab */
459 while (pp[1] == '\t')
463 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
465 for (i = 0; i < count; i++) {
466 tm.tm_mon = month[i] - 1;
468 tm.tm_year = year[i] - 1900;
469 (void)strftime(dbuf, sizeof(dbuf),
470 d_first ? "%e %b" : "%b %e", &tm);
472 fprintf(stderr, "got \"%s\" in %s/%s/%s line %d\n",
473 pp, cal_home, cal_dir, cal_file, cal_line);
474 events[i] = event_add(year[i], month[i], day[i], dbuf,
475 ((flags &= F_VARIABLE) != 0) ? 1 : 0, pp,
493 for (i = 0; i < MAXCOUNT; i++)
494 extradata[i] = (char *)calloc(1, 20);
497 if ((fpin = opencalin()) == NULL)
500 if ((fpout = opencalout()) == NULL) {
505 if (cal_parse(fpin, fpout))
508 event_print_all(fpout);
518 /* open up calendar file */
519 if ((fpin = fopen(calendarFile, "r")) == NULL) {
521 if (chdir(calendarHomes[0]) != 0)
523 if (stat(calendarNoMail, &sbuf) == 0)
525 if ((fpin = fopen(calendarFile, "r")) == NULL)
528 fpin = cal_fopen(calendarFile);
539 /* not reading all calendar files, just set output to stdout */
543 /* set output to a temporary file, so if no output don't send mail */
544 snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
545 if ((fd = mkstemp(path)) < 0)
547 return (fdopen(fd, "w+"));
554 int nread, pdes[2], status;
561 if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
567 (void)close(pdes[0]);
568 (void)close(pdes[1]);
571 /* child -- set stdin to pipe output */
572 if (pdes[0] != STDIN_FILENO) {
573 (void)dup2(pdes[0], STDIN_FILENO);
574 (void)close(pdes[0]);
576 (void)close(pdes[1]);
577 execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
578 "\"Reminder Service\"", (char *)NULL);
579 warn(_PATH_SENDMAIL);
582 /* parent -- write to pipe input */
583 (void)close(pdes[0]);
585 write(pdes[1], "From: \"Reminder Service\" <", 26);
586 write(pdes[1], pw->pw_name, strlen(pw->pw_name));
587 write(pdes[1], ">\nTo: <", 7);
588 write(pdes[1], pw->pw_name, strlen(pw->pw_name));
589 write(pdes[1], ">\nSubject: ", 11);
590 write(pdes[1], dayname, strlen(dayname));
591 write(pdes[1], "'s Calendar\nPrecedence: bulk\n\n", 30);
593 while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
594 (void)write(pdes[1], buf, nread);
595 (void)close(pdes[1]);
596 done: (void)fclose(fp);
598 while (wait(&status) >= 0);