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