]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/calendar/io.c
Add file names and line numbers to debug messages
[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_LOCAL, _PATH_INCLUDE}; /* HOME */
75 static const char *calendarNoMail = "nomail";/* don't sent mail if file exist */
76
77 static char path[MAXPATHLEN];
78 static const char *cal_home;
79 static const char *cal_dir;
80 static const char *cal_file;
81 static int cal_line;
82
83 struct fixs neaster, npaskha, ncny, nfullmoon, nnewmoon;
84 struct fixs nmarequinox, nsepequinox, njunsolstice, ndecsolstice;
85
86 static int cal_parse(FILE *in, FILE *out);
87
88 static StringList *definitions = NULL;
89 static struct event *events[MAXCOUNT];
90 static char *extradata[MAXCOUNT];
91
92 static void
93 trimlr(char **buf)
94 {
95         char *walk = *buf;
96         char *last;
97
98         while (isspace(*walk))
99                 walk++;
100         if (*walk != '\0') {
101                 last = walk + strlen(walk) - 1;
102                 while (last > walk && isspace(*last))
103                         last--;
104                 *(last+1) = 0;
105         }
106
107         *buf = walk;
108 }
109
110 static FILE *
111 cal_fopen(const char *file)
112 {
113         FILE *fp;
114         char *home = getenv("HOME");
115         unsigned int i;
116         struct stat sb;
117         static bool warned = false;
118
119         if (home == NULL || *home == '\0') {
120                 warnx("Cannot get home directory");
121                 return (NULL);
122         }
123
124         if (chdir(home) != 0) {
125                 warnx("Cannot enter home directory \"%s\"", home);
126                 return (NULL);
127         }
128
129         for (i = 0; i < nitems(calendarHomes); i++) {
130                 if (chdir(calendarHomes[i]) != 0)
131                         continue;
132
133                 if ((fp = fopen(file, "r")) != NULL) {
134                         cal_home = home;
135                         cal_dir = calendarHomes[i];
136                         cal_file = file;
137                         return (fp);
138                 }
139         }
140
141         warnx("can't open calendar file \"%s\"", file);
142         if (!warned && stat(_PATH_INCLUDE_LOCAL, &sb) != 0) {
143                 warnx("calendar data files now provided by calendar-data pkg.");
144                 warned = true;
145         }
146
147         return (NULL);
148 }
149
150 #define WARN0(format)              \
151         warnx(format " in %s/%s/%s line %d", cal_home, cal_dir, cal_file, cal_line)
152 #define WARN1(format, arg1)                \
153         warnx(format " in %s/%s/%s line %d", arg1, cal_home, cal_dir, cal_file, cal_line)
154
155 static int
156 token(char *line, FILE *out, int *skip)
157 {
158         char *walk, c, a;
159         const char *this_cal_home;
160         const char *this_cal_dir;
161         const char *this_cal_file;
162         int this_cal_line;
163
164         if (strncmp(line, "endif", 5) == 0) {
165                 if (*skip > 0)
166                         --*skip;
167                 return (T_OK);
168         }
169
170         if (strncmp(line, "ifdef", 5) == 0) {
171                 walk = line + 5;
172                 trimlr(&walk);
173
174                 if (*walk == '\0') {
175                         WARN0("Expecting arguments after #ifdef");
176                         return (T_ERR);
177                 }
178
179                 if (*skip != 0 || definitions == NULL || sl_find(definitions, walk) == NULL)
180                         ++*skip;
181
182                 return (T_OK);
183         }
184
185         if (strncmp(line, "ifndef", 6) == 0) {
186                 walk = line + 6;
187                 trimlr(&walk);
188
189                 if (*walk == '\0') {
190                         WARN0("Expecting arguments after #ifndef");
191                         return (T_ERR);
192                 }
193
194                 if (*skip != 0 || (definitions != NULL && sl_find(definitions, walk) != NULL))
195                         ++*skip;
196
197                 return (T_OK);
198         }
199
200         if (strncmp(line, "else", 4) == 0) {
201                 walk = line + 4;
202                 trimlr(&walk);
203
204                 if (*walk != '\0') {
205                         WARN0("Expecting no arguments after #else");
206                         return (T_ERR);
207                 }
208
209                 if (*skip == 0)
210                         *skip = 1;
211                 else if (*skip == 1)
212                         *skip = 0;
213
214                 return (T_OK);
215         }
216
217         if (*skip != 0)
218                 return (T_OK);
219
220         if (strncmp(line, "include", 7) == 0) {
221                 walk = line + 7;
222
223                 trimlr(&walk);
224
225                 if (*walk == '\0') {
226                         WARN0("Expecting arguments after #include");
227                         return (T_ERR);
228                 }
229
230                 if (*walk != '<' && *walk != '\"') {
231                         WARN0("Excecting '<' or '\"' after #include");
232                         return (T_ERR);
233                 }
234
235                 a = *walk == '<' ? '>' : '\"';
236                 walk++;
237                 c = walk[strlen(walk) - 1];
238
239                 if (a != c) {
240                         WARN1("Unterminated include expecting '%c'", a);
241                         return (T_ERR);
242                 }
243                 walk[strlen(walk) - 1] = '\0';
244
245                 this_cal_home = cal_home;
246                 this_cal_dir = cal_dir;
247                 this_cal_file = cal_file;
248                 this_cal_line = cal_line;
249                 if (cal_parse(cal_fopen(walk), out))
250                         return (T_ERR);
251                 cal_home = this_cal_home;
252                 cal_dir = this_cal_dir;
253                 cal_file = this_cal_file;
254                 cal_line = this_cal_line;
255
256                 return (T_OK);
257         }
258
259         if (strncmp(line, "define", 6) == 0) {
260                 if (definitions == NULL)
261                         definitions = sl_init();
262                 walk = line + 6;
263                 trimlr(&walk);
264
265                 if (*walk == '\0') {
266                         WARN0("Expecting arguments after #define");
267                         return (T_ERR);
268                 }
269
270                 sl_add(definitions, strdup(walk));
271                 return (T_OK);
272         }
273
274         return (T_PROCESS);
275
276 }
277
278 #define REPLACE(string, slen, struct_) \
279                 if (strncasecmp(buf, (string), (slen)) == 0 && buf[(slen)]) { \
280                         if (struct_.name != NULL)                             \
281                                 free(struct_.name);                           \
282                         if ((struct_.name = strdup(buf + (slen))) == NULL)    \
283                                 errx(1, "cannot allocate memory");            \
284                         struct_.len = strlen(buf + (slen));                   \
285                         continue;                                             \
286                 }
287 static int
288 cal_parse(FILE *in, FILE *out)
289 {
290         char *line = NULL;
291         char *buf;
292         size_t linecap = 0;
293         ssize_t linelen;
294         ssize_t l;
295         static int d_first = -1;
296         static int count = 0;
297         int i;
298         int month[MAXCOUNT];
299         int day[MAXCOUNT];
300         int year[MAXCOUNT];
301         int skip = 0;
302         char dbuf[80];
303         char *pp, p;
304         struct tm tm;
305         int flags;
306         char *c, *cc;
307         bool incomment = false;
308
309         /* Unused */
310         tm.tm_sec = 0;
311         tm.tm_min = 0;
312         tm.tm_hour = 0;
313         tm.tm_wday = 0;
314
315         if (in == NULL)
316                 return (1);
317
318         cal_line = 0;
319         while ((linelen = getline(&line, &linecap, in)) > 0) {
320                 cal_line++;
321                 buf = line;
322                 if (buf[linelen - 1] == '\n')
323                         buf[--linelen] = '\0';
324
325                 if (incomment) {
326                         c = strstr(buf, "*/");
327                         if (c) {
328                                 c += 2;
329                                 linelen -= c - buf;
330                                 buf = c;
331                                 incomment = false;
332                         } else {
333                                 continue;
334                         }
335                 }
336                 if (!incomment) {
337                         do {
338                                 c = strstr(buf, "//");
339                                 cc = strstr(buf, "/*");
340                                 if (c != NULL && (cc == NULL || c - cc < 0)) {
341                                         /* single line comment */
342                                         *c = '\0';
343                                         linelen = c - buf;
344                                         break;
345                                 } else if (cc != NULL) {
346                                         c = strstr(cc + 2, "*/");
347                                         if (c != NULL) {
348                                                 /* multi-line comment ending on same line */
349                                                 c += 2;
350                                                 memmove(cc, c, buf + linelen + 1 - c);
351                                                 linelen -= c - cc;
352                                         } else {
353                                                 /* multi-line comment */
354                                                 *cc = '\0';
355                                                 linelen = cc - buf;
356                                                 incomment = true;
357                                                 break;
358                                         }
359                                 }
360                         } while (c != NULL || cc != NULL);
361                 }
362
363                 for (l = linelen;
364                      l > 0 && isspace((unsigned char)buf[l - 1]);
365                      l--)
366                         ;
367                 buf[l] = '\0';
368                 if (buf[0] == '\0')
369                         continue;
370
371                 if (buf == line && *buf == '#') {
372                         switch (token(buf+1, out, &skip)) {
373                         case T_ERR:
374                                 free(line);
375                                 return (1);
376                         case T_OK:
377                                 continue;
378                         case T_PROCESS:
379                                 break;
380                         default:
381                                 break;
382                         }
383                 }
384
385                 if (skip != 0)
386                         continue;
387
388                 /*
389                  * Setting LANG in user's calendar was an old workaround
390                  * for 'calendar -a' being run with C locale to properly
391                  * print user's calendars in their native languages.
392                  * Now that 'calendar -a' does fork with setusercontext(),
393                  * and does not run iconv(), this variable has little use.
394                  */
395                 if (strncmp(buf, "LANG=", 5) == 0) {
396                         (void)setlocale(LC_ALL, buf + 5);
397                         d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
398 #ifdef WITH_ICONV
399                         if (!doall)
400                                 set_new_encoding();
401 #endif
402                         setnnames();
403                         continue;
404                 }
405                 /* Parse special definitions: Easter, Paskha etc */
406                 REPLACE("Easter=", 7, neaster);
407                 REPLACE("Paskha=", 7, npaskha);
408                 REPLACE("ChineseNewYear=", 15, ncny);
409                 REPLACE("NewMoon=", 8, nnewmoon);
410                 REPLACE("FullMoon=", 9, nfullmoon);
411                 REPLACE("MarEquinox=", 11, nmarequinox);
412                 REPLACE("SepEquinox=", 11, nsepequinox);
413                 REPLACE("JunSolstice=", 12, njunsolstice);
414                 REPLACE("DecSolstice=", 12, ndecsolstice);
415                 if (strncmp(buf, "SEQUENCE=", 9) == 0) {
416                         setnsequences(buf + 9);
417                         continue;
418                 }
419
420                 /*
421                  * If the line starts with a tab, the data has to be
422                  * added to the previous line
423                  */
424                 if (buf[0] == '\t') {
425                         for (i = 0; i < count; i++)
426                                 event_continue(events[i], buf);
427                         continue;
428                 }
429
430                 /* Get rid of leading spaces (non-standard) */
431                 while (isspace((unsigned char)buf[0]))
432                         memcpy(buf, buf + 1, strlen(buf));
433
434                 /* No tab in the line, then not a valid line */
435                 if ((pp = strchr(buf, '\t')) == NULL)
436                         continue;
437
438                 /* Trim spaces in front of the tab */
439                 while (isspace((unsigned char)pp[-1]))
440                         pp--;
441
442                 p = *pp;
443                 *pp = '\0';
444                 if ((count = parsedaymonth(buf, year, month, day, &flags,
445                     extradata)) == 0)
446                         continue;
447                 *pp = p;
448                 if (count < 0) {
449                         /* Show error status based on return value */
450                         if (debug)
451                                 fprintf(stderr, "Ignored: \"%s\" in %s/%s/%s line %d\n",
452                                     buf, cal_home, cal_dir, cal_file, cal_line);
453                         if (count == -1)
454                                 continue;
455                         count = -count + 1;
456                 }
457
458                 /* Find the last tab */
459                 while (pp[1] == '\t')
460                         pp++;
461
462                 if (d_first < 0)
463                         d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
464
465                 for (i = 0; i < count; i++) {
466                         tm.tm_mon = month[i] - 1;
467                         tm.tm_mday = day[i];
468                         tm.tm_year = year[i] - 1900;
469                         (void)strftime(dbuf, sizeof(dbuf),
470                             d_first ? "%e %b" : "%b %e", &tm);
471                         if (debug)
472                                 fprintf(stderr, "got \"%s\" in  %s/%s/%s line %d\n",
473                                     pp, cal_home, cal_dir, cal_file, cal_line);
474                         events[i] = event_add(year[i], month[i], day[i], dbuf,
475                             ((flags &= F_VARIABLE) != 0) ? 1 : 0, pp,
476                             extradata[i]);
477                 }
478         }
479
480         free(line);
481         fclose(in);
482
483         return (0);
484 }
485
486 void
487 cal(void)
488 {
489         FILE *fpin;
490         FILE *fpout;
491         int i;
492
493         for (i = 0; i < MAXCOUNT; i++)
494                 extradata[i] = (char *)calloc(1, 20);
495
496
497         if ((fpin = opencalin()) == NULL)
498                 return;
499
500         if ((fpout = opencalout()) == NULL) {
501                 fclose(fpin);
502                 return;
503         }
504
505         if (cal_parse(fpin, fpout))
506                 return;
507
508         event_print_all(fpout);
509         closecal(fpout);
510 }
511
512 FILE *
513 opencalin(void)
514 {
515         struct stat sbuf;
516         FILE *fpin;
517
518         /* open up calendar file */
519         if ((fpin = fopen(calendarFile, "r")) == NULL) {
520                 if (doall) {
521                         if (chdir(calendarHomes[0]) != 0)
522                                 return (NULL);
523                         if (stat(calendarNoMail, &sbuf) == 0)
524                                 return (NULL);
525                         if ((fpin = fopen(calendarFile, "r")) == NULL)
526                                 return (NULL);
527                 } else {
528                         fpin = cal_fopen(calendarFile);
529                 }
530         }
531         return (fpin);
532 }
533
534 FILE *
535 opencalout(void)
536 {
537         int fd;
538
539         /* not reading all calendar files, just set output to stdout */
540         if (!doall)
541                 return (stdout);
542
543         /* set output to a temporary file, so if no output don't send mail */
544         snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
545         if ((fd = mkstemp(path)) < 0)
546                 return (NULL);
547         return (fdopen(fd, "w+"));
548 }
549
550 void
551 closecal(FILE *fp)
552 {
553         struct stat sbuf;
554         int nread, pdes[2], status;
555         char buf[1024];
556
557         if (!doall)
558                 return;
559
560         rewind(fp);
561         if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
562                 goto done;
563         if (pipe(pdes) < 0)
564                 goto done;
565         switch (fork()) {
566         case -1:                        /* error */
567                 (void)close(pdes[0]);
568                 (void)close(pdes[1]);
569                 goto done;
570         case 0:
571                 /* child -- set stdin to pipe output */
572                 if (pdes[0] != STDIN_FILENO) {
573                         (void)dup2(pdes[0], STDIN_FILENO);
574                         (void)close(pdes[0]);
575                 }
576                 (void)close(pdes[1]);
577                 execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
578                     "\"Reminder Service\"", (char *)NULL);
579                 warn(_PATH_SENDMAIL);
580                 _exit(1);
581         }
582         /* parent -- write to pipe input */
583         (void)close(pdes[0]);
584
585         write(pdes[1], "From: \"Reminder Service\" <", 26);
586         write(pdes[1], pw->pw_name, strlen(pw->pw_name));
587         write(pdes[1], ">\nTo: <", 7);
588         write(pdes[1], pw->pw_name, strlen(pw->pw_name));
589         write(pdes[1], ">\nSubject: ", 11);
590         write(pdes[1], dayname, strlen(dayname));
591         write(pdes[1], "'s Calendar\nPrecedence: bulk\n\n", 30);
592
593         while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
594                 (void)write(pdes[1], buf, nread);
595         (void)close(pdes[1]);
596 done:   (void)fclose(fp);
597         (void)unlink(path);
598         while (wait(&status) >= 0);
599 }