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