]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/calendar/io.c
Remove spurious newline
[FreeBSD/FreeBSD.git] / usr.bin / calendar / io.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993, 1994
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  */
31
32 #ifndef lint
33 static const char copyright[] =
34 "@(#) Copyright (c) 1989, 1993\n\
35         The Regents of the University of California.  All rights reserved.\n";
36 #endif
37
38 #if 0
39 #ifndef lint
40 static char sccsid[] = "@(#)calendar.c  8.3 (Berkeley) 3/25/94";
41 #endif
42 #endif
43
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46
47 #include <sys/param.h>
48 #include <sys/stat.h>
49 #include <sys/wait.h>
50 #include <ctype.h>
51 #include <err.h>
52 #include <errno.h>
53 #include <langinfo.h>
54 #include <locale.h>
55 #include <pwd.h>
56 #include <stdbool.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <stringlist.h>
61 #include <time.h>
62 #include <unistd.h>
63
64 #include "pathnames.h"
65 #include "calendar.h"
66
67 enum {
68         T_OK = 0,
69         T_ERR,
70         T_PROCESS,
71 };
72
73 const char *calendarFile = "calendar";  /* default calendar file */
74 static const char *calendarHomes[] = {".calendar", _PATH_INCLUDE}; /* HOME */
75 static const char *calendarNoMail = "nomail";/* don't sent mail if file exist */
76
77 static char path[MAXPATHLEN];
78
79 struct fixs neaster, npaskha, ncny, nfullmoon, nnewmoon;
80 struct fixs nmarequinox, nsepequinox, njunsolstice, ndecsolstice;
81
82 static int cal_parse(FILE *in, FILE *out);
83
84 static StringList *definitions = NULL;
85 static struct event *events[MAXCOUNT];
86 static char *extradata[MAXCOUNT];
87
88 static void
89 trimlr(char **buf)
90 {
91         char *walk = *buf;
92         char *last;
93
94         while (isspace(*walk))
95                 walk++;
96         if (*walk != '\0') {
97                 last = walk + strlen(walk) - 1;
98                 while (last > walk && isspace(*last))
99                         last--;
100                 *(last+1) = 0;
101         }
102
103         *buf = walk;
104 }
105
106 static FILE *
107 cal_fopen(const char *file)
108 {
109         FILE *fp;
110         char *home = getenv("HOME");
111         unsigned int i;
112
113         if (home == NULL || *home == '\0') {
114                 warnx("Cannot get home directory");
115                 return (NULL);
116         }
117
118         if (chdir(home) != 0) {
119                 warnx("Cannot enter home directory");
120                 return (NULL);
121         }
122
123         for (i = 0; i < nitems(calendarHomes); i++) {
124                 if (chdir(calendarHomes[i]) != 0)
125                         continue;
126
127                 if ((fp = fopen(file, "r")) != NULL)
128                         return (fp);
129         }
130
131         warnx("can't open calendar file \"%s\"", file);
132
133         return (NULL);
134 }
135
136 static int
137 token(char *line, FILE *out, bool *skip)
138 {
139         char *walk, c, a;
140
141         if (strncmp(line, "endif", 5) == 0) {
142                 *skip = false;
143                 return (T_OK);
144         }
145
146         if (*skip)
147                 return (T_OK);
148
149         if (strncmp(line, "include", 7) == 0) {
150                 walk = line + 7;
151
152                 trimlr(&walk);
153
154                 if (*walk == '\0') {
155                         warnx("Expecting arguments after #include");
156                         return (T_ERR);
157                 }
158
159                 if (*walk != '<' && *walk != '\"') {
160                         warnx("Excecting '<' or '\"' after #include");
161                         return (T_ERR);
162                 }
163
164                 a = *walk;
165                 walk++;
166                 c = walk[strlen(walk) - 1];
167
168                 switch(c) {
169                 case '>':
170                         if (a != '<') {
171                                 warnx("Unterminated include expecting '\"'");
172                                 return (T_ERR);
173                         }
174                         break;
175                 case '\"':
176                         if (a != '\"') {
177                                 warnx("Unterminated include expecting '>'");
178                                 return (T_ERR);
179                         }
180                         break;
181                 default:
182                         warnx("Unterminated include expecting '%c'",
183                             a == '<' ? '>' : '\"' );
184                         return (T_ERR);
185                 }
186                 walk[strlen(walk) - 1] = '\0';
187
188                 if (cal_parse(cal_fopen(walk), out))
189                         return (T_ERR);
190
191                 return (T_OK);
192         }
193
194         if (strncmp(line, "define", 6) == 0) {
195                 if (definitions == NULL)
196                         definitions = sl_init();
197                 walk = line + 6;
198                 trimlr(&walk);
199
200                 if (*walk == '\0') {
201                         warnx("Expecting arguments after #define");
202                         return (T_ERR);
203                 }
204
205                 sl_add(definitions, strdup(walk));
206                 return (T_OK);
207         }
208
209         if (strncmp(line, "ifndef", 6) == 0) {
210                 walk = line + 6;
211                 trimlr(&walk);
212
213                 if (*walk == '\0') {
214                         warnx("Expecting arguments after #ifndef");
215                         return (T_ERR);
216                 }
217
218                 if (definitions != NULL && sl_find(definitions, walk) != NULL)
219                         *skip = true;
220
221                 return (T_OK);
222         }
223
224         return (T_PROCESS);
225
226 }
227
228 #define REPLACE(string, slen, struct_) \
229                 if (strncasecmp(buf, (string), (slen)) == 0 && buf[(slen)]) { \
230                         if (struct_.name != NULL)                             \
231                                 free(struct_.name);                           \
232                         if ((struct_.name = strdup(buf + (slen))) == NULL)    \
233                                 errx(1, "cannot allocate memory");            \
234                         struct_.len = strlen(buf + (slen));                   \
235                         continue;                                             \
236                 }
237 static int
238 cal_parse(FILE *in, FILE *out)
239 {
240         char *line = NULL;
241         char *buf;
242         size_t linecap = 0;
243         ssize_t linelen;
244         ssize_t l;
245         static int d_first = -1;
246         static int count = 0;
247         int i;
248         int month[MAXCOUNT];
249         int day[MAXCOUNT];
250         int year[MAXCOUNT];
251         bool skip = false;
252         char dbuf[80];
253         char *pp, p;
254         struct tm tm;
255         int flags;
256
257         /* Unused */
258         tm.tm_sec = 0;
259         tm.tm_min = 0;
260         tm.tm_hour = 0;
261         tm.tm_wday = 0;
262
263         if (in == NULL)
264                 return (1);
265
266         while ((linelen = getline(&line, &linecap, in)) > 0) {
267                 if (*line == '#') {
268                         switch (token(line+1, out, &skip)) {
269                         case T_ERR:
270                                 free(line);
271                                 return (1);
272                         case T_OK:
273                                 continue;
274                         case T_PROCESS:
275                                 break;
276                         default:
277                                 break;
278                         }
279                 }
280
281                 if (skip)
282                         continue;
283
284                 buf = line;
285                 for (l = linelen;
286                      l > 0 && isspace((unsigned char)buf[l - 1]);
287                      l--)
288                         ;
289                 buf[l] = '\0';
290                 if (buf[0] == '\0')
291                         continue;
292
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');
297 #ifdef WITH_ICONV
298                         set_new_encoding();
299 #endif
300                         setnnames();
301                         continue;
302                 }
303                 REPLACE("Easter=", 7, neaster);
304                 REPLACE("Paskha=", 7, npaskha);
305                 REPLACE("ChineseNewYear=", 15, ncny);
306                 REPLACE("NewMoon=", 8, nnewmoon);
307                 REPLACE("FullMoon=", 9, nfullmoon);
308                 REPLACE("MarEquinox=", 11, nmarequinox);
309                 REPLACE("SepEquinox=", 11, nsepequinox);
310                 REPLACE("JunSolstice=", 12, njunsolstice);
311                 REPLACE("DecSolstice=", 12, ndecsolstice);
312                 if (strncmp(buf, "SEQUENCE=", 9) == 0) {
313                         setnsequences(buf + 9);
314                         continue;
315                 }
316
317                 /*
318                  * If the line starts with a tab, the data has to be
319                  * added to the previous line
320                  */
321                 if (buf[0] == '\t') {
322                         for (i = 0; i < count; i++)
323                                 event_continue(events[i], buf);
324                         continue;
325                 }
326
327                 /* Get rid of leading spaces (non-standard) */
328                 while (isspace((unsigned char)buf[0]))
329                         memcpy(buf, buf + 1, strlen(buf));
330
331                 /* No tab in the line, then not a valid line */
332                 if ((pp = strchr(buf, '\t')) == NULL)
333                         continue;
334
335                 /* Trim spaces in front of the tab */
336                 while (isspace((unsigned char)pp[-1]))
337                         pp--;
338
339                 p = *pp;
340                 *pp = '\0';
341                 if ((count = parsedaymonth(buf, year, month, day, &flags,
342                     extradata)) == 0)
343                         continue;
344                 *pp = p;
345                 if (count < 0) {
346                         /* Show error status based on return value */
347                         if (debug)
348                                 fprintf(stderr, "Ignored: %s\n", buf);
349                         if (count == -1)
350                                 continue;
351                         count = -count + 1;
352                 }
353
354                 /* Find the last tab */
355                 while (pp[1] == '\t')
356                         pp++;
357
358                 if (d_first < 0)
359                         d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
360
361                 for (i = 0; i < count; i++) {
362                         tm.tm_mon = month[i] - 1;
363                         tm.tm_mday = day[i];
364                         tm.tm_year = year[i] - 1900;
365                         (void)strftime(dbuf, sizeof(dbuf),
366                             d_first ? "%e %b" : "%b %e", &tm);
367                         if (debug)
368                                 fprintf(stderr, "got %s\n", pp);
369                         events[i] = event_add(year[i], month[i], day[i], dbuf,
370                             ((flags &= F_VARIABLE) != 0) ? 1 : 0, pp,
371                             extradata[i]);
372                 }
373         }
374
375         free(line);
376         fclose(in);
377
378         return (0);
379 }
380
381 void
382 cal(void)
383 {
384         FILE *fpin;
385         FILE *fpout;
386         int i;
387
388         for (i = 0; i < MAXCOUNT; i++)
389                 extradata[i] = (char *)calloc(1, 20);
390
391
392         if ((fpin = opencalin()) == NULL)
393                 return;
394
395         if ((fpout = opencalout()) == NULL) {
396                 fclose(fpin);
397                 return;
398         }
399
400         if (cal_parse(fpin, fpout))
401                 return;
402
403         event_print_all(fpout);
404         closecal(fpout);
405 }
406
407 FILE *
408 opencalin(void)
409 {
410         struct stat sbuf;
411         FILE *fpin;
412
413         /* open up calendar file */
414         if ((fpin = fopen(calendarFile, "r")) == NULL) {
415                 if (doall) {
416                         if (chdir(calendarHomes[0]) != 0)
417                                 return (NULL);
418                         if (stat(calendarNoMail, &sbuf) == 0)
419                                 return (NULL);
420                         if ((fpin = fopen(calendarFile, "r")) == NULL)
421                                 return (NULL);
422                 } else {
423                         fpin = cal_fopen(calendarFile);
424                 }
425         }
426         return (fpin);
427 }
428
429 FILE *
430 opencalout(void)
431 {
432         int fd;
433
434         /* not reading all calendar files, just set output to stdout */
435         if (!doall)
436                 return (stdout);
437
438         /* set output to a temporary file, so if no output don't send mail */
439         snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
440         if ((fd = mkstemp(path)) < 0)
441                 return (NULL);
442         return (fdopen(fd, "w+"));
443 }
444
445 void
446 closecal(FILE *fp)
447 {
448         uid_t uid;
449         struct stat sbuf;
450         int nread, pdes[2], status;
451         char buf[1024];
452
453         if (!doall)
454                 return;
455
456         rewind(fp);
457         if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
458                 goto done;
459         if (pipe(pdes) < 0)
460                 goto done;
461         switch (fork()) {
462         case -1:                        /* error */
463                 (void)close(pdes[0]);
464                 (void)close(pdes[1]);
465                 goto done;
466         case 0:
467                 /* child -- set stdin to pipe output */
468                 if (pdes[0] != STDIN_FILENO) {
469                         (void)dup2(pdes[0], STDIN_FILENO);
470                         (void)close(pdes[0]);
471                 }
472                 (void)close(pdes[1]);
473                 uid = geteuid();
474                 if (setuid(getuid()) < 0) {
475                         warnx("setuid failed");
476                         _exit(1);
477                 }
478                 if (setgid(getegid()) < 0) {
479                         warnx("setgid failed");
480                         _exit(1);
481                 }
482                 if (setuid(uid) < 0) {
483                         warnx("setuid failed");
484                         _exit(1);
485                 }
486                 execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
487                     "\"Reminder Service\"", (char *)NULL);
488                 warn(_PATH_SENDMAIL);
489                 _exit(1);
490         }
491         /* parent -- write to pipe input */
492         (void)close(pdes[0]);
493
494         write(pdes[1], "From: \"Reminder Service\" <", 26);
495         write(pdes[1], pw->pw_name, strlen(pw->pw_name));
496         write(pdes[1], ">\nTo: <", 7);
497         write(pdes[1], pw->pw_name, strlen(pw->pw_name));
498         write(pdes[1], ">\nSubject: ", 11);
499         write(pdes[1], dayname, strlen(dayname));
500         write(pdes[1], "'s Calendar\nPrecedence: bulk\n\n", 30);
501
502         while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
503                 (void)write(pdes[1], buf, nread);
504         (void)close(pdes[1]);
505 done:   (void)fclose(fp);
506         (void)unlink(path);
507         while (wait(&status) >= 0);
508 }