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
31 * @(#)calendar.c 8.3 (Berkeley) 3/25/94
32 * $FreeBSD: head/usr.bin/calendar/io.c 327117 2017-12-23 21:04:32Z eadler $
35 #include <sys/param.h>
52 #include <stringlist.h>
58 struct iovec header[] = {
59 { __DECONST(char *, "From: "), 6 },
61 { __DECONST(char *, " (Reminder Service)\nTo: "), 24 },
63 { __DECONST(char *, "\nSubject: "), 10 },
65 { __DECONST(char *, "'s Calendar\nPrecedence: bulk\n"), 29 },
66 { __DECONST(char *, "Auto-Submitted: auto-generated\n\n"), 32 },
75 struct fixs neaster, npaskha, ncny, nfullmoon, nnewmoon;
76 struct fixs nmarequinox, nsepequinox, njunsolstice, ndecsolstice;
78 const char *calendarFile = "calendar"; /* default calendar file */
79 static const char *calendarHomes[] = {".calendar", "/usr/share/calendar"};
80 static const char *calendarNoMail = "nomail"; /* don't sent mail if file exist */
82 static char path[MAXPATHLEN];
84 static StringList *definitions = NULL;
85 static struct event *events[MAXCOUNT];
86 static char *extradata[MAXCOUNT];
88 static FILE *cal_fopen(const char *file);
89 static bool cal_parse(FILE *in, FILE *out);
90 static void closecal(FILE *fp);
91 static FILE *opencalin(void);
92 static FILE *opencalout(void);
93 static int token(char *line, FILE *out, bool *skip);
94 static void trimlr(char **buf);
103 while (isspace(*walk))
106 last = walk + strlen(walk) - 1;
107 while (last > walk && isspace(*last))
116 cal_fopen(const char *file)
120 char cwdpath[MAXPATHLEN];
123 /* Already in home directory before calling cal() in main() */
124 if (getcwd(cwdpath, sizeof(cwdpath)) != NULL)
127 warnx("Cannot get current working directory");
129 for (i = 0; i < nitems(calendarHomes); i++) {
130 if (chdir(calendarHomes[i]) != 0)
133 if ((fp = fopen(file, "r")) != NULL)
137 if (cwd && chdir(cwdpath) != 0)
138 warnx("Cannot back to original directory: \"%s\"", cwdpath);
141 warnx("Cannot open calendar file \"%s\"", file);
147 token(char *line, FILE *out, bool *skip)
151 if (strncmp(line, "endif", 5) == 0) {
159 if (strncmp(line, "include", 7) == 0) {
165 warnx("Expecting arguments after #include");
169 if (*walk != '<' && *walk != '\"') {
170 warnx("Excecting '<' or '\"' after #include");
176 c = walk[strlen(walk) - 1];
181 warnx("Unterminated include expecting '\"'");
187 warnx("Unterminated include expecting '>'");
192 warnx("Unterminated include expecting '%c'",
193 a == '<' ? '>' : '\"' );
196 walk[strlen(walk) - 1] = '\0';
198 if (!cal_parse(cal_fopen(walk), out))
204 if (strncmp(line, "define", 6) == 0) {
205 if (definitions == NULL)
206 definitions = sl_init();
211 warnx("Expecting arguments after #define");
215 sl_add(definitions, strdup(walk));
219 if (strncmp(line, "ifndef", 6) == 0) {
224 warnx("Expecting arguments after #ifndef");
228 if (definitions != NULL && sl_find(definitions, walk) != NULL)
238 cal_parse(FILE *in, FILE *out)
245 static int d_first = -1;
246 static int count = 0;
266 while ((linelen = getline(&line, &linecap, in)) > 0) {
268 switch (token(line+1, out, &skip)) {
286 l > 0 && isspace((unsigned char)buf[l - 1]);
293 /* Parse special definitions: LANG, Easter, Paskha etc */
294 if (strncmp(buf, "LANG=", 5) == 0) {
295 (void)setlocale(LC_ALL, buf + 5);
296 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
301 #define REPLACE(string, slen, struct_) \
302 if (strncasecmp(buf, (string), (slen)) == 0 && buf[(slen)]) { \
303 if (struct_.name != NULL) \
304 free(struct_.name); \
305 if ((struct_.name = strdup(buf + (slen))) == NULL) \
306 errx(1, "cannot allocate memory"); \
307 struct_.len = strlen(buf + (slen)); \
311 REPLACE("Easter=", 7, neaster);
312 REPLACE("Paskha=", 7, npaskha);
313 REPLACE("ChineseNewYear=", 15, ncny);
314 REPLACE("NewMoon=", 8, nnewmoon);
315 REPLACE("FullMoon=", 9, nfullmoon);
316 REPLACE("MarEquinox=", 11, nmarequinox);
317 REPLACE("SepEquinox=", 11, nsepequinox);
318 REPLACE("JunSolstice=", 12, njunsolstice);
319 REPLACE("DecSolstice=", 12, ndecsolstice);
322 if (strncmp(buf, "SEQUENCE=", 9) == 0) {
323 setnsequences(buf + 9);
328 * If the line starts with a tab, the data has to be
329 * added to the previous line
331 if (buf[0] == '\t') {
332 for (i = 0; i < count; i++)
333 event_continue(events[i], buf);
337 /* Get rid of leading spaces (non-standard) */
338 while (isspace((unsigned char)buf[0]))
339 memcpy(buf, buf + 1, strlen(buf));
341 /* No tab in the line, then not a valid line */
342 if ((pp = strchr(buf, '\t')) == NULL)
345 /* Trim spaces in front of the tab */
346 while (isspace((unsigned char)pp[-1]))
351 if ((count = parsedaymonth(buf, year, month, day, &flags,
356 /* Show error status based on return value */
358 fprintf(stderr, "Ignored: %s\n", buf);
364 /* Find the last tab */
365 while (pp[1] == '\t')
369 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
371 for (i = 0; i < count; i++) {
372 tm.tm_mon = month[i] - 1;
374 tm.tm_year = year[i] - 1900;
375 strftime(dbuf, sizeof(dbuf),
376 d_first ? "%e %b" : "%b %e", &tm);
378 fprintf(stderr, "got %s\n", pp);
379 events[i] = event_add(year[i], month[i], day[i], dbuf,
380 ((flags &= F_VARIABLE) != 0) ? 1 : 0, pp,
397 for (i = 0; i < MAXCOUNT; i++)
398 extradata[i] = (char *)calloc(1, 20);
400 if ((fpin = opencalin()) == NULL)
403 if ((fpout = opencalout()) == NULL) {
408 if (!cal_parse(fpin, fpout))
411 event_print_all(fpout);
421 /* open up calendar file */
422 if ((fpin = fopen(calendarFile, "r")) == NULL) {
424 if (chdir(calendarHomes[0]) != 0)
426 if (stat(calendarNoMail, &sbuf) == 0)
428 if ((fpin = fopen(calendarFile, "r")) == NULL)
431 fpin = cal_fopen(calendarFile);
442 /* not reading all calendar files, just set output to stdout */
446 /* set output to a temporary file, so if no output don't send mail */
447 snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
448 if ((fd = mkstemp(path)) < 0)
450 return (fdopen(fd, "w+"));
457 int nread, pdes[2], status;
464 if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
476 /* child -- set stdin to pipe output */
477 if (pdes[0] != STDIN_FILENO) {
478 dup2(pdes[0], STDIN_FILENO);
482 execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
483 "\"Reminder Service\"", (char *)NULL);
484 warn(_PATH_SENDMAIL);
487 /* parent -- write to pipe input */
490 header[1].iov_base = header[3].iov_base = pw->pw_name;
491 header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
492 writev(pdes[1], header, 8);
493 while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
494 write(pdes[1], buf, nread);
500 while (wait(&status) >= 0)