1 /* $NetBSD: calendar.c,v 1.47 2008/09/30 05:51:41 dholland Exp $ */
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
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)calendar.c 8.4 (Berkeley) 1/7/95";
42 __RCSID("$NetBSD: calendar.c,v 1.47 2008/09/30 05:51:41 dholland Exp $");
45 #include <sys/param.h>
64 #include "pathnames.h"
66 /* flags used by calendar file parser */
67 #define F_ISMONTH 0x01
70 #define F_WILDMONTH 0x10
71 #define F_WILDDAY 0x20
73 static unsigned short lookahead
= 1;
74 static unsigned short weekend
= 2;
75 static char *fname
= NULL
;
76 static char *datestr
= NULL
;
77 static const char *defaultnames
[] = {"calendar", ".calendar", _PATH_SYSTEM_CALENDAR
, NULL
};
78 static struct passwd
*pw
;
79 static char path
[MAXPATHLEN
+ 1];
80 static bool doall
= false;
81 static bool cpp_restricted
= false;
83 /* 1-based month, 0-based days, cumulative */
84 static const int daytab
[][14] = {
85 { 0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 },
86 { 0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
89 static const int *cumdays
;
90 static int offset
, yrdays
;
91 static char dayname
[10];
93 static struct iovec header
[] = {
94 { __UNCONST("From: "), 6 },
96 { __UNCONST(" (Reminder Service)\nTo: "), 24 },
98 { __UNCONST("\nSubject: "), 10 },
100 { __UNCONST("'s Calendar\nPrecedence: bulk\n\n"), 30 },
103 static const char *days
[] = {
104 "sun", "mon", "tue", "wed", "thu", "fri", "sat", NULL
,
107 static const char *months
[] = {
108 "jan", "feb", "mar", "apr", "may", "jun",
109 "jul", "aug", "sep", "oct", "nov", "dec", NULL
,
112 static void atodays(int, char *, unsigned short *);
113 static void cal(void);
114 static void closecal(FILE *);
115 static int getday(char *);
116 static int getfield(char *, char **, int *);
117 static void getmmdd(struct tm
*, char *);
118 static int getmonth(char *);
119 static bool isnow(char *);
120 static FILE *opencal(void);
121 static void settime(void);
122 static void usage(void) __dead
;
125 main(int argc
, char **argv
)
130 (void)setprogname(argv
[0]); /* for portability */
132 while ((ch
= getopt(argc
, argv
, "-ad:f:l:w:x")) != -1) {
134 case '-': /* backward contemptible */
138 err(EXIT_FAILURE
, NULL
);
149 atodays(ch
, optarg
, &lookahead
);
152 atodays(ch
, optarg
, &weekend
);
155 cpp_restricted
= true;
171 * XXX - This ignores the user's CALENDAR_DIR variable.
172 * Run under user's login shell?
174 while ((pw
= getpwent()) != NULL
) {
175 (void)setegid(pw
->pw_gid
);
176 (void)seteuid(pw
->pw_uid
);
177 if (chdir(pw
->pw_dir
) != -1)
181 } else if ((caldir
= getenv("CALENDAR_DIR")) != NULL
) {
182 if (chdir(caldir
) != -1)
184 } else if ((pw
= getpwuid(geteuid())) != NULL
) {
185 if (chdir(pw
->pw_dir
) != -1)
198 if ((fp
= opencal()) == NULL
)
201 while ((line
= fparseln(stdin
,
202 NULL
, NULL
, NULL
, FPARSELN_UNESCCOMM
)) != NULL
) {
206 printing
= isnow(line
);
208 (void)fprintf(fp
, "%s\n", line
);
221 tp
= localtime(&now
);
223 getmmdd(tp
, datestr
);
225 if (isleap(tp
->tm_year
+ TM_YEAR_BASE
)) {
226 yrdays
= DAYSPERLYEAR
;
229 yrdays
= DAYSPERNYEAR
;
232 /* Friday displays Monday's events */
233 offset
= tp
->tm_wday
== 5 ? lookahead
+ weekend
: lookahead
;
234 header
[5].iov_base
= dayname
;
235 header
[5].iov_len
= strftime(dayname
, sizeof(dayname
), "%A", tp
);
239 * Possible date formats include any combination of:
240 * 3-charmonth (January, Jan, Jan)
241 * 3-charweekday (Friday, Monday, mon.)
242 * numeric month or day (1, 2, 04)
244 * Any character may separate them, or they may not be separated. Any line,
245 * following a line that is matched, that starts with "whitespace", is shown
246 * along with the matched line.
259 /* didn't recognize anything, skip it */
260 if (!(v1
= getfield(endp
, &endp
, &flags
)))
263 if ((flags
& (F_ISDAY
|F_ISDOW
)) || v1
> 12) {
266 /* if no recognizable month, assume wildcard ('*') month */
267 if ((month
= getfield(endp
, &endp
, &flags
)) == 0) {
268 flags
|= F_ISMONTH
| F_WILDMONTH
;
269 month
= tp
->tm_mon
+ 1;
271 } else if (flags
& F_ISMONTH
) {
273 /* if no recognizable day, assume the first */
274 if ((day
= getfield(endp
, &endp
, &flags
)) == 0)
277 v2
= getfield(endp
, &endp
, &flags
);
278 if (flags
& F_ISMONTH
) {
282 /* F_ISDAY set, v2 > 12, or no way to tell */
284 /* if no recognizable day, assume the first */
288 /* if month is out of range, treat it as '*' */
289 if (month
< 1 || month
> 12) {
290 flags
|= F_ISMONTH
| F_WILDMONTH
;
291 month
= tp
->tm_mon
+ 1;
294 if (flags
& F_WILDMONTH
&& flags
& F_WILDDAY
)
297 if (flags
& F_WILDMONTH
&& flags
& F_ISDAY
&& day
== tp
->tm_mday
)
300 if (flags
& F_WILDMONTH
&& flags
& F_ISDOW
&& day
== tp
->tm_wday
+ 1)
303 if (flags
& F_ISMONTH
&& flags
& F_WILDDAY
&& month
== tp
->tm_mon
+ 1)
306 if (flags
& F_ISMONTH
&& flags
& F_ISDOW
&& month
== tp
->tm_mon
+ 1 &&
307 day
== tp
->tm_wday
+ 1)
311 day
= tp
->tm_mday
+ (((day
- 1) - tp
->tm_wday
+ 7) % 7);
312 day
= cumdays
[month
] + day
;
314 /* if today or today + offset days */
315 if (day
>= tp
->tm_yday
&& day
<= tp
->tm_yday
+ offset
)
318 /* if number of days left in this year + days to event in next year */
319 if (yrdays
- tp
->tm_yday
+ day
<= offset
)
326 getfield(char *p
, char **endp
, int *flags
)
333 * note this macro has an arg that isn't used ... it is retained
334 * (it is believed) to make the macro call look more "natural"
335 * and suggest at the call site what is happening.
337 #define FLDCHAR(a) (*p != '\0' && !isdigit((unsigned char)*p) && \
338 !isalpha((unsigned char)*p) && *p != '*')
341 for (/*EMPTY*/; FLDCHAR(*p
); ++p
)
343 if (*p
== '*') { /* `*' is current month */
344 if (!(*flags
& F_ISMONTH
)) {
345 *flags
|= F_ISMONTH
| F_WILDMONTH
;
347 return tp
->tm_mon
+ 1;
349 *flags
|= F_ISDAY
| F_WILDDAY
;
354 if (isdigit((unsigned char)*p
)) {
355 val
= (int)strtol(p
, &p
, 10); /* if 0, it's failure */
356 for (/*EMPTY*/; FLDCHAR(*p
); ++p
)
361 for (start
= p
; *p
!= '\0' && isalpha((unsigned char)*p
); p
++)
367 if ((val
= getmonth(start
)) != 0)
369 else if ((val
= getday(start
)) != 0)
377 for (*p
= savech
; FLDCHAR(*p
); ++p
)
390 /* open up calendar file as stdin */
392 for (name
= defaultnames
; *name
!= NULL
; name
++) {
393 if (freopen(*name
, "rf", stdin
) == NULL
)
401 err(EXIT_FAILURE
, "Cannot open calendar file");
403 } else if (freopen(fname
, "rf", stdin
) == NULL
) {
406 err(EXIT_FAILURE
, "Cannot open `%s'", fname
);
409 if (pipe(pdes
) == -1) {
410 warn("Cannot open pipe");
417 (void)close(pdes
[0]);
418 (void)close(pdes
[1]);
421 /* child -- stdin already setup, set stdout to pipe input */
422 if (pdes
[1] != STDOUT_FILENO
) {
423 (void)dup2(pdes
[1], STDOUT_FILENO
);
424 (void)close(pdes
[1]);
426 (void)close(pdes
[0]);
427 /* tell CPP to only open regular files */
428 if(!cpp_restricted
&& setenv("CPP_RESTRICTED", "", 1) == -1)
429 err(EXIT_FAILURE
, "Cannot restrict cpp");
430 cpp_restricted
= true;
432 (void)execl(_PATH_CPP
, "cpp", "-traditional", "-P", "-I.",
433 "-I" _PATH_CALENDARS
, NULL
);
434 err(EXIT_FAILURE
, "Cannot exec `%s'", _PATH_CPP
);
437 /* parent -- set stdin to pipe output */
438 (void)dup2(pdes
[0], STDIN_FILENO
);
439 (void)close(pdes
[0]);
440 (void)close(pdes
[1]);
442 /* not reading all calendar files, just set output to stdout */
447 * Set output to a temporary file, so if no output
450 (void)snprintf(path
, sizeof(path
), "%s/_calXXXXXX", _PATH_TMP
);
451 if ((fd
= mkstemp(path
)) == -1) {
452 warn("Cannot create temporary file");
455 return fdopen(fd
, "w+");
473 if (fstat(fileno(fp
), &sbuf
) == -1 || sbuf
.st_size
== 0)
475 if (pipe(pdes
) == -1)
481 (void)close(pdes
[0]);
482 (void)close(pdes
[1]);
485 /* child -- set stdin to pipe output */
486 if (pdes
[0] != STDIN_FILENO
) {
487 (void)dup2(pdes
[0], STDIN_FILENO
);
488 (void)close(pdes
[0]);
490 (void)close(pdes
[1]);
491 (void)execl(_PATH_SENDMAIL
, "sendmail", "-i", "-t", "-F",
492 "\"Reminder Service\"", "-f", "root", NULL
);
493 err(EXIT_FAILURE
, "Cannot exec `%s'", _PATH_SENDMAIL
);
496 /* parent -- write to pipe input */
497 (void)close(pdes
[0]);
499 header
[1].iov_base
= header
[3].iov_base
= (void *)pw
->pw_name
;
500 header
[1].iov_len
= header
[3].iov_len
= strlen(pw
->pw_name
);
501 (void)writev(pdes
[1], header
, 7);
502 while ((nread
= read(fileno(fp
), buf
, sizeof(buf
))) > 0)
503 (void)write(pdes
[1], buf
, (size_t)nread
);
504 (void)close(pdes
[1]);
508 done
: (void)fclose(fp
);
510 while (wait(&status
) != -1)
519 for (p
= months
; *p
; ++p
)
520 if (strncasecmp(s
, *p
, 3) == 0)
521 return (int)(p
- months
) + 1;
530 for (p
= days
; *p
; ++p
)
531 if (strncasecmp(s
, *p
, 3) == 0)
532 return (int)(p
- days
) + 1;
537 atodays(int ch
, char *arg
, unsigned short *rvp
)
542 if (u
< 0 || u
> 366)
543 warnx("-%c %d out of range 0-366, ignored.", ch
, u
);
548 #define todigit(x) ((x) - '0')
549 #define ATOI2(x) (todigit((x)[0]) * 10 + todigit((x)[1]))
550 #define ISDIG2(x) (isdigit((unsigned char)(x)[0]) && isdigit((unsigned char)(x)[1]))
553 getmmdd(struct tm
*ptm
, char *ds
)
562 ttm
.tm_mon
= ATOI2(ds
) - 1;
566 ttm
.tm_mday
= ATOI2(ds
);
571 if (ISDIG2(ds
) && ISDIG2(ds
+ 2)) {
572 ttm
.tm_year
= ATOI2(ds
) * 100 - TM_YEAR_BASE
;
574 ttm
.tm_year
+= ATOI2(ds
);
575 } else if (ISDIG2(ds
)) {
576 ttm
.tm_year
= ATOI2(ds
);
577 if (ttm
.tm_year
< 69)
578 ttm
.tm_year
+= 2000 - TM_YEAR_BASE
;
580 ttm
.tm_year
+= 1900 - TM_YEAR_BASE
;
583 if (ok
&& mktime(&ttm
) == -1)
589 warnx("Can't convert `%s' to date, ignored.", ds
);
598 (void)fprintf(stderr
, "usage: %s [-ax] [-d MMDD[[YY]YY]"
599 " [-f fname] [-l days] [-w days]\n", getprogname());