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>
59 #include <stringlist.h>
63 #include "pathnames.h"
72 const char *calendarFile = "calendar"; /* default calendar file */
73 static const char *calendarHomes[] = {".calendar", _PATH_INCLUDE_LOCAL, _PATH_INCLUDE}; /* HOME */
74 static const char *calendarNoMail = "nomail";/* don't sent mail if file exist */
76 static char path[MAXPATHLEN];
77 static const char *cal_home;
78 static const char *cal_dir;
79 static const char *cal_file;
82 struct fixs neaster, npaskha, ncny, nfullmoon, nnewmoon;
83 struct fixs nmarequinox, nsepequinox, njunsolstice, ndecsolstice;
85 static int cal_parse(FILE *in, FILE *out);
87 static StringList *definitions = NULL;
88 static struct event *events[MAXCOUNT];
89 static char *extradata[MAXCOUNT];
98 while (isspace(*walk))
103 while (*sep != '\0' && !isspace(*sep))
107 last = sep + strlen(sep) - 1;
108 while (last > walk && isspace(*last))
117 cal_fopen(const char *file)
120 char *home = getenv("HOME");
123 static bool warned = false;
125 if (home == NULL || *home == '\0') {
126 warnx("Cannot get home directory");
130 if (chdir(home) != 0) {
131 warnx("Cannot enter home directory \"%s\"", home);
135 for (i = 0; i < nitems(calendarHomes); i++) {
136 if (chdir(calendarHomes[i]) != 0)
139 if ((fp = fopen(file, "r")) != NULL) {
141 cal_dir = calendarHomes[i];
147 warnx("can't open calendar file \"%s\"", file);
148 if (!warned && stat(_PATH_INCLUDE_LOCAL, &sb) != 0) {
149 warnx("calendar data files now provided by calendar-data pkg.");
159 static char buffer[MAXPATHLEN + 10];
162 snprintf(buffer, sizeof(buffer), "%s", cal_file);
163 else if (cal_dir[0] == '/')
164 snprintf(buffer, sizeof(buffer), "%s/%s", cal_dir, cal_file);
166 snprintf(buffer, sizeof(buffer), "%s/%s/%s", cal_home, cal_dir, cal_file);
170 #define WARN0(format) \
171 warnx(format " in %s line %d", cal_path(), cal_line)
172 #define WARN1(format, arg1) \
173 warnx(format " in %s line %d", arg1, cal_path(), cal_line)
176 cmptoken(char *line, const char* token)
178 char len = strlen(token);
180 if (strncmp(line, token, len) != 0)
186 token(char *line, FILE *out, int *skip, int *unskip)
188 char *walk, *sep, a, c;
189 const char *this_cal_home;
190 const char *this_cal_dir;
191 const char *this_cal_file;
194 while (isspace(*line))
197 if (cmptoken(line, "endif")) {
198 if (*skip + *unskip == 0) {
199 WARN0("#endif without prior #ifdef or #ifndef");
210 walk = cmptoken(line, "ifdef");
215 WARN0("Expecting arguments after #ifdef");
219 WARN1("Expecting a single word after #ifdef "
220 "but got \"%s\"", walk);
225 definitions == NULL || sl_find(definitions, walk) == NULL)
233 walk = cmptoken(line, "ifndef");
238 WARN0("Expecting arguments after #ifndef");
242 WARN1("Expecting a single word after #ifndef "
243 "but got \"%s\"", walk);
248 (definitions != NULL && sl_find(definitions, walk) != NULL))
256 walk = cmptoken(line, "else");
261 WARN0("Expecting no arguments after #else");
264 if (*skip + *unskip == 0) {
265 WARN0("#else without prior #ifdef or #ifndef");
272 } else if (*skip == 1) {
283 walk = cmptoken(line, "include");
288 WARN0("Expecting arguments after #include");
292 if (*walk != '<' && *walk != '\"') {
293 WARN0("Excecting '<' or '\"' after #include");
297 a = *walk == '<' ? '>' : '\"';
299 c = walk[strlen(walk) - 1];
302 WARN1("Unterminated include expecting '%c'", a);
305 walk[strlen(walk) - 1] = '\0';
307 this_cal_home = cal_home;
308 this_cal_dir = cal_dir;
309 this_cal_file = cal_file;
310 this_cal_line = cal_line;
311 if (cal_parse(cal_fopen(walk), out))
313 cal_home = this_cal_home;
314 cal_dir = this_cal_dir;
315 cal_file = this_cal_file;
316 cal_line = this_cal_line;
321 walk = cmptoken(line, "define");
323 if (definitions == NULL)
324 definitions = sl_init();
329 WARN0("Expecting arguments after #define");
333 if (sl_find(definitions, walk) == NULL)
334 sl_add(definitions, strdup(walk));
338 walk = cmptoken(line, "undef");
340 if (definitions != NULL) {
344 WARN0("Expecting arguments after #undef");
348 WARN1("Expecting a single word after #undef "
349 "but got \"%s\"", walk);
353 walk = sl_find(definitions, walk);
360 walk = cmptoken(line, "warning");
363 WARN1("Warning: %s", walk);
366 walk = cmptoken(line, "error");
369 WARN1("Error: %s", walk);
373 WARN1("Undefined pre-processor command \"#%s\"", line);
378 setup_locale(const char *locale)
380 (void)setlocale(LC_ALL, locale);
388 #define REPLACE(string, slen, struct_) \
389 if (strncasecmp(buf, (string), (slen)) == 0 && buf[(slen)]) { \
390 if (struct_.name != NULL) \
391 free(struct_.name); \
392 if ((struct_.name = strdup(buf + (slen))) == NULL) \
393 errx(1, "cannot allocate memory"); \
394 struct_.len = strlen(buf + (slen)); \
398 cal_parse(FILE *in, FILE *out)
400 char *mylocale = NULL;
406 static int count = 0;
416 bool incomment = false;
422 while ((linelen = getline(&line, &linecap, in)) > 0) {
425 if (buf[linelen - 1] == '\n')
426 buf[--linelen] = '\0';
429 c = strstr(buf, "*/");
441 c = strstr(buf, "//");
442 cc = strstr(buf, "/*");
443 if (c != NULL && (cc == NULL || c - cc < 0)) {
444 /* single line comment */
448 } else if (cc != NULL) {
449 c = strstr(cc + 2, "*/");
451 /* multi-line comment ending on same line */
453 memmove(cc, c, buf + linelen + 1 - c);
456 /* multi-line comment */
463 } while (c != NULL || cc != NULL);
467 l > 0 && isspace((unsigned char)buf[l - 1]);
474 if (buf == line && *buf == '#') {
475 switch (token(buf+1, out, &skip, &unskip)) {
492 * Setting LANG in user's calendar was an old workaround
493 * for 'calendar -a' being run with C locale to properly
494 * print user's calendars in their native languages.
495 * Now that 'calendar -a' does fork with setusercontext(),
496 * and does not run iconv(), this variable has little use.
498 if (strncmp(buf, "LANG=", 5) == 0) {
499 if (mylocale == NULL)
500 mylocale = strdup(setlocale(LC_ALL, NULL));
501 setup_locale(buf + 5);
504 /* Parse special definitions: Easter, Paskha etc */
505 REPLACE("Easter=", 7, neaster);
506 REPLACE("Paskha=", 7, npaskha);
507 REPLACE("ChineseNewYear=", 15, ncny);
508 REPLACE("NewMoon=", 8, nnewmoon);
509 REPLACE("FullMoon=", 9, nfullmoon);
510 REPLACE("MarEquinox=", 11, nmarequinox);
511 REPLACE("SepEquinox=", 11, nsepequinox);
512 REPLACE("JunSolstice=", 12, njunsolstice);
513 REPLACE("DecSolstice=", 12, ndecsolstice);
514 if (strncmp(buf, "SEQUENCE=", 9) == 0) {
515 setnsequences(buf + 9);
520 * If the line starts with a tab, the data has to be
521 * added to the previous line
523 if (buf[0] == '\t') {
524 for (i = 0; i < count; i++)
525 event_continue(events[i], buf);
529 /* Get rid of leading spaces (non-standard) */
530 while (isspace((unsigned char)buf[0]))
531 memcpy(buf, buf + 1, strlen(buf));
533 /* No tab in the line, then not a valid line */
534 if ((pp = strchr(buf, '\t')) == NULL)
537 /* Trim spaces in front of the tab */
538 while (isspace((unsigned char)pp[-1]))
543 if ((count = parsedaymonth(buf, year, month, day, &flags,
548 /* Show error status based on return value */
550 WARN1("Ignored: \"%s\"", buf);
556 /* Find the last tab */
557 while (pp[1] == '\t')
560 for (i = 0; i < count; i++) {
562 WARN1("got \"%s\"", pp);
563 events[i] = event_add(year[i], month[i], day[i],
564 ((flags &= F_VARIABLE) != 0) ? 1 : 0, pp,
568 while (skip-- > 0 || unskip-- > 0) {
570 WARN0("Missing #endif assumed");
575 if (mylocale != NULL) {
576 setup_locale(mylocale);
590 for (i = 0; i < MAXCOUNT; i++)
591 extradata[i] = (char *)calloc(1, 20);
594 if ((fpin = opencalin()) == NULL)
597 if ((fpout = opencalout()) == NULL) {
602 if (cal_parse(fpin, fpout))
605 event_print_all(fpout);
615 /* open up calendar file */
616 cal_file = calendarFile;
617 if ((fpin = fopen(calendarFile, "r")) == NULL) {
619 if (chdir(calendarHomes[0]) != 0)
621 if (stat(calendarNoMail, &sbuf) == 0)
623 if ((fpin = fopen(calendarFile, "r")) == NULL)
626 fpin = cal_fopen(calendarFile);
637 /* not reading all calendar files, just set output to stdout */
641 /* set output to a temporary file, so if no output don't send mail */
642 snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
643 if ((fd = mkstemp(path)) < 0)
645 return (fdopen(fd, "w+"));
652 int nread, pdes[2], status;
659 if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
665 (void)close(pdes[0]);
666 (void)close(pdes[1]);
669 /* child -- set stdin to pipe output */
670 if (pdes[0] != STDIN_FILENO) {
671 (void)dup2(pdes[0], STDIN_FILENO);
672 (void)close(pdes[0]);
674 (void)close(pdes[1]);
675 execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
676 "\"Reminder Service\"", (char *)NULL);
677 warn(_PATH_SENDMAIL);
680 /* parent -- write to pipe input */
681 (void)close(pdes[0]);
683 write(pdes[1], "From: \"Reminder Service\" <", 26);
684 write(pdes[1], pw->pw_name, strlen(pw->pw_name));
685 write(pdes[1], ">\nTo: <", 7);
686 write(pdes[1], pw->pw_name, strlen(pw->pw_name));
687 write(pdes[1], ">\nSubject: ", 11);
688 write(pdes[1], dayname, strlen(dayname));
689 write(pdes[1], "'s Calendar\nPrecedence: bulk\n\n", 30);
691 while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
692 (void)write(pdes[1], buf, nread);
693 (void)close(pdes[1]);
694 done: (void)fclose(fp);
696 while (wait(&status) >= 0);