2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1989, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
59 #include <stringlist.h>
62 #include "pathnames.h"
71 const char *calendarFile = "calendar"; /* default calendar file */
72 static const char *calendarHomes[] = {".calendar", _PATH_INCLUDE}; /* HOME */
73 static const char *calendarNoMail = "nomail";/* don't sent mail if file exist */
75 static char path[MAXPATHLEN];
77 struct fixs neaster, npaskha, ncny, nfullmoon, nnewmoon;
78 struct fixs nmarequinox, nsepequinox, njunsolstice, ndecsolstice;
80 static int cal_parse(FILE *in, FILE *out);
82 static StringList *definitions = NULL;
83 static struct event *events[MAXCOUNT];
84 static char *extradata[MAXCOUNT];
91 while (isspace(*walk))
93 while (isspace(walk[strlen(walk) -1]))
94 walk[strlen(walk) -1] = '\0';
100 cal_fopen(const char *file)
103 char *home = getenv("HOME");
106 if (home == NULL || *home == '\0') {
107 warnx("Cannot get home directory");
111 if (chdir(home) != 0) {
112 warnx("Cannot enter home directory");
116 for (i = 0; i < sizeof(calendarHomes)/sizeof(calendarHomes[0]) ; i++) {
117 if (chdir(calendarHomes[i]) != 0)
120 if ((fp = fopen(file, "r")) != NULL)
124 warnx("can't open calendar file \"%s\"", file);
130 token(char *line, FILE *out, bool *skip)
134 if (strncmp(line, "endif", 5) == 0) {
142 if (strncmp(line, "include", 7) == 0) {
148 warnx("Expecting arguments after #include");
152 if (*walk != '<' && *walk != '\"') {
153 warnx("Excecting '<' or '\"' after #include");
159 c = walk[strlen(walk) - 1];
164 warnx("Unterminated include expecting '\"'");
170 warnx("Unterminated include expecting '>'");
175 warnx("Unterminated include expecting '%c'",
176 a == '<' ? '>' : '\"' );
179 walk[strlen(walk) - 1] = '\0';
181 if (cal_parse(cal_fopen(walk), out))
187 if (strncmp(line, "define", 6) == 0) {
188 if (definitions == NULL)
189 definitions = sl_init();
194 warnx("Expecting arguments after #define");
198 sl_add(definitions, strdup(walk));
202 if (strncmp(line, "ifndef", 6) == 0) {
207 warnx("Expecting arguments after #ifndef");
211 if (definitions != NULL && sl_find(definitions, walk) != NULL)
221 #define REPLACE(string, slen, struct_) \
222 if (strncasecmp(buf, (string), (slen)) == 0 && buf[(slen)]) { \
223 if (struct_.name != NULL) \
224 free(struct_.name); \
225 if ((struct_.name = strdup(buf + (slen))) == NULL) \
226 errx(1, "cannot allocate memory"); \
227 struct_.len = strlen(buf + (slen)); \
231 cal_parse(FILE *in, FILE *out)
238 static int d_first = -1;
239 static int count = 0;
259 while ((linelen = getline(&line, &linecap, in)) > 0) {
264 switch (token(line+1, out, &skip)) {
282 l > 0 && isspace((unsigned char)buf[l - 1]);
289 /* Parse special definitions: LANG, Easter, Paskha etc */
290 if (strncmp(buf, "LANG=", 5) == 0) {
291 (void)setlocale(LC_ALL, buf + 5);
292 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
296 REPLACE("Easter=", 7, neaster);
297 REPLACE("Paskha=", 7, npaskha);
298 REPLACE("ChineseNewYear=", 15, ncny);
299 REPLACE("NewMoon=", 8, nnewmoon);
300 REPLACE("FullMoon=", 9, nfullmoon);
301 REPLACE("MarEquinox=", 11, nmarequinox);
302 REPLACE("SepEquinox=", 11, nsepequinox);
303 REPLACE("JunSolstice=", 12, njunsolstice);
304 REPLACE("DecSolstice=", 12, ndecsolstice);
305 if (strncmp(buf, "SEQUENCE=", 9) == 0) {
306 setnsequences(buf + 9);
311 * If the line starts with a tab, the data has to be
312 * added to the previous line
314 if (buf[0] == '\t') {
315 for (i = 0; i < count; i++)
316 event_continue(events[i], buf);
320 /* Get rid of leading spaces (non-standard) */
321 while (isspace((unsigned char)buf[0]))
322 memcpy(buf, buf + 1, strlen(buf));
324 /* No tab in the line, then not a valid line */
325 if ((pp = strchr(buf, '\t')) == NULL)
328 /* Trim spaces in front of the tab */
329 while (isspace((unsigned char)pp[-1]))
334 if ((count = parsedaymonth(buf, year, month, day, &flags,
339 /* Show error status based on return value */
341 fprintf(stderr, "Ignored: %s\n", buf);
347 /* Find the last tab */
348 while (pp[1] == '\t')
352 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
354 for (i = 0; i < count; i++) {
355 tm.tm_mon = month[i] - 1;
357 tm.tm_year = year[i] - 1900;
358 (void)strftime(dbuf, sizeof(dbuf),
359 d_first ? "%e %b" : "%b %e", &tm);
361 fprintf(stderr, "got %s\n", pp);
362 events[i] = event_add(year[i], month[i], day[i], dbuf,
363 ((flags &= F_VARIABLE) != 0) ? 1 : 0, pp,
381 for (i = 0; i < MAXCOUNT; i++)
382 extradata[i] = (char *)calloc(1, 20);
385 if ((fpin = opencalin()) == NULL)
388 if ((fpout = opencalout()) == NULL) {
393 if (cal_parse(fpin, fpout))
396 event_print_all(fpout);
406 /* open up calendar file */
407 if ((fpin = fopen(calendarFile, "r")) == NULL) {
409 if (chdir(calendarHomes[0]) != 0)
411 if (stat(calendarNoMail, &sbuf) == 0)
413 if ((fpin = fopen(calendarFile, "r")) == NULL)
416 fpin = cal_fopen(calendarFile);
427 /* not reading all calendar files, just set output to stdout */
431 /* set output to a temporary file, so if no output don't send mail */
432 snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
433 if ((fd = mkstemp(path)) < 0)
435 return (fdopen(fd, "w+"));
443 int nread, pdes[2], status;
450 if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
456 (void)close(pdes[0]);
457 (void)close(pdes[1]);
460 /* child -- set stdin to pipe output */
461 if (pdes[0] != STDIN_FILENO) {
462 (void)dup2(pdes[0], STDIN_FILENO);
463 (void)close(pdes[0]);
465 (void)close(pdes[1]);
467 if (setuid(getuid()) < 0) {
468 warnx("setuid failed");
471 if (setgid(getegid()) < 0) {
472 warnx("setgid failed");
475 if (setuid(uid) < 0) {
476 warnx("setuid failed");
479 execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
480 "\"Reminder Service\"", (char *)NULL);
481 warn(_PATH_SENDMAIL);
484 /* parent -- write to pipe input */
485 (void)close(pdes[0]);
487 write(pdes[1], "From: \"Reminder Service\" <", 26);
488 write(pdes[1], pw->pw_name, strlen(pw->pw_name));
489 write(pdes[1], ">\nTo: <", 7);
490 write(pdes[1], pw->pw_name, strlen(pw->pw_name));
491 write(pdes[1], ">\nSubject: ", 11);
492 write(pdes[1], dayname, strlen(dayname));
493 write(pdes[1], "'s Calendar\nPrecedence: bulk\n\n", 30);
495 while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
496 (void)write(pdes[1], buf, nread);
497 (void)close(pdes[1]);
498 done: (void)fclose(fp);
500 while (wait(&status) >= 0);