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];
99 while (isspace(*walk))
104 while (*sep != '\0' && !isspace(*sep))
108 last = sep + strlen(sep) - 1;
109 while (last > walk && isspace(*last))
118 cal_fopen(const char *file)
121 char *home = getenv("HOME");
124 static bool warned = false;
125 char calendarhome[MAXPATHLEN];
127 if (home == NULL || *home == '\0') {
128 warnx("Cannot get home directory");
132 if (chdir(home) != 0) {
133 warnx("Cannot enter home directory \"%s\"", home);
137 for (i = 0; i < nitems(calendarHomes); i++) {
138 if (snprintf(calendarhome, sizeof (calendarhome), calendarHomes[i],
139 getlocalbase()) >= (int)sizeof (calendarhome))
142 if (chdir(calendarhome) != 0)
145 if ((fp = fopen(file, "r")) != NULL) {
147 cal_dir = calendarhome;
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.");
165 static char buffer[MAXPATHLEN + 10];
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);
172 snprintf(buffer, sizeof(buffer), "%s/%s/%s", cal_home, cal_dir, cal_file);
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)
182 cmptoken(char *line, const char* token)
184 char len = strlen(token);
186 if (strncmp(line, token, len) != 0)
192 token(char *line, FILE *out, int *skip, int *unskip)
194 char *walk, *sep, a, c;
195 const char *this_cal_home;
196 const char *this_cal_dir;
197 const char *this_cal_file;
200 while (isspace(*line))
203 if (cmptoken(line, "endif")) {
204 if (*skip + *unskip == 0) {
205 WARN0("#endif without prior #ifdef or #ifndef");
216 walk = cmptoken(line, "ifdef");
221 WARN0("Expecting arguments after #ifdef");
225 WARN1("Expecting a single word after #ifdef "
226 "but got \"%s\"", walk);
231 definitions == NULL || sl_find(definitions, walk) == NULL)
239 walk = cmptoken(line, "ifndef");
244 WARN0("Expecting arguments after #ifndef");
248 WARN1("Expecting a single word after #ifndef "
249 "but got \"%s\"", walk);
254 (definitions != NULL && sl_find(definitions, walk) != NULL))
262 walk = cmptoken(line, "else");
267 WARN0("Expecting no arguments after #else");
270 if (*skip + *unskip == 0) {
271 WARN0("#else without prior #ifdef or #ifndef");
278 } else if (*skip == 1) {
289 walk = cmptoken(line, "include");
294 WARN0("Expecting arguments after #include");
298 if (*walk != '<' && *walk != '\"') {
299 WARN0("Excecting '<' or '\"' after #include");
303 a = *walk == '<' ? '>' : '\"';
305 c = walk[strlen(walk) - 1];
308 WARN1("Unterminated include expecting '%c'", a);
311 walk[strlen(walk) - 1] = '\0';
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))
319 cal_home = this_cal_home;
320 cal_dir = this_cal_dir;
321 cal_file = this_cal_file;
322 cal_line = this_cal_line;
327 walk = cmptoken(line, "define");
329 if (definitions == NULL)
330 definitions = sl_init();
335 WARN0("Expecting arguments after #define");
339 if (sl_find(definitions, walk) == NULL)
340 sl_add(definitions, strdup(walk));
344 walk = cmptoken(line, "undef");
346 if (definitions != NULL) {
350 WARN0("Expecting arguments after #undef");
354 WARN1("Expecting a single word after #undef "
355 "but got \"%s\"", walk);
359 walk = sl_find(definitions, walk);
366 walk = cmptoken(line, "warning");
369 WARN1("Warning: %s", walk);
372 walk = cmptoken(line, "error");
375 WARN1("Error: %s", walk);
379 WARN1("Undefined pre-processor command \"#%s\"", line);
384 setup_locale(const char *locale)
386 (void)setlocale(LC_ALL, locale);
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)); \
404 cal_parse(FILE *in, FILE *out)
406 char *mylocale = NULL;
412 static int count = 0;
422 bool incomment = false;
428 while ((linelen = getline(&line, &linecap, in)) > 0) {
431 if (buf[linelen - 1] == '\n')
432 buf[--linelen] = '\0';
435 c = strstr(buf, "*/");
447 c = strstr(buf, "//");
448 cc = strstr(buf, "/*");
449 if (c != NULL && (cc == NULL || c - cc < 0)) {
450 /* single line comment */
454 } else if (cc != NULL) {
455 c = strstr(cc + 2, "*/");
457 /* multi-line comment ending on same line */
459 memmove(cc, c, buf + linelen + 1 - c);
462 /* multi-line comment */
469 } while (c != NULL || cc != NULL);
473 l > 0 && isspace((unsigned char)buf[l - 1]);
480 if (buf == line && *buf == '#') {
481 switch (token(buf+1, out, &skip, &unskip)) {
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.
504 if (strncmp(buf, "LANG=", 5) == 0) {
505 if (mylocale == NULL)
506 mylocale = strdup(setlocale(LC_ALL, NULL));
507 setup_locale(buf + 5);
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);
526 * If the line starts with a tab, the data has to be
527 * added to the previous line
529 if (buf[0] == '\t') {
530 for (i = 0; i < count; i++)
531 event_continue(events[i], buf);
535 /* Get rid of leading spaces (non-standard) */
536 while (isspace((unsigned char)buf[0]))
537 memcpy(buf, buf + 1, strlen(buf));
539 /* No tab in the line, then not a valid line */
540 if ((pp = strchr(buf, '\t')) == NULL)
543 /* Trim spaces in front of the tab */
544 while (isspace((unsigned char)pp[-1]))
549 if ((count = parsedaymonth(buf, year, month, day, &flags,
554 /* Show error status based on return value */
556 WARN1("Ignored: \"%s\"", buf);
562 /* Find the last tab */
563 while (pp[1] == '\t')
566 for (i = 0; i < count; i++) {
568 WARN1("got \"%s\"", pp);
569 events[i] = event_add(year[i], month[i], day[i],
570 ((flags &= F_VARIABLE) != 0) ? 1 : 0, pp,
574 while (skip-- > 0 || unskip-- > 0) {
576 WARN0("Missing #endif assumed");
581 if (mylocale != NULL) {
582 setup_locale(mylocale);
596 for (i = 0; i < MAXCOUNT; i++)
597 extradata[i] = (char *)calloc(1, 20);
600 if ((fpin = opencalin()) == NULL)
603 if ((fpout = opencalout()) == NULL) {
608 if (cal_parse(fpin, fpout))
611 event_print_all(fpout);
621 /* open up calendar file */
622 cal_file = calendarFile;
623 if ((fpin = fopen(calendarFile, "r")) == NULL) {
625 if (chdir(calendarHomes[0]) != 0)
627 if (stat(calendarNoMail, &sbuf) == 0)
629 if ((fpin = fopen(calendarFile, "r")) == NULL)
632 fpin = cal_fopen(calendarFile);
643 /* not reading all calendar files, just set output to stdout */
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)
651 return (fdopen(fd, "w+"));
658 int nread, pdes[2], status;
665 if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
671 (void)close(pdes[0]);
672 (void)close(pdes[1]);
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]);
680 (void)close(pdes[1]);
681 execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
682 "\"Reminder Service\"", (char *)NULL);
683 warn(_PATH_SENDMAIL);
686 /* parent -- write to pipe input */
687 (void)close(pdes[0]);
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);
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);
702 while (wait(&status) >= 0);