1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1980, 1993 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 "rcv.h" 33 #include <errno.h> 34 #include <fcntl.h> 35 #include "extern.h" 36 37 /* 38 * Mail -- a mail program 39 * 40 * Lexical processing of commands. 41 */ 42 43 static const char *prompt = "& "; 44 45 extern const struct cmd cmdtab[]; 46 extern const char *version; 47 48 /* 49 * Set up editing on the given file name. 50 * If the first character of name is %, we are considered to be 51 * editing the file, otherwise we are reading our mail which has 52 * signficance for mbox and so forth. 53 * 54 * If the -e option is being passed to mail, this function has a 55 * tri-state return code: -1 on error, 0 on no mail, 1 if there is 56 * mail. 57 */ 58 int 59 setfile(char *name) 60 { 61 FILE *ibuf; 62 int checkmode, i, fd; 63 struct stat stb; 64 char isedit = *name != '%' || getuserid(myname) != getuid(); 65 char *who = name[1] ? name + 1 : myname; 66 char tempname[PATHSIZE]; 67 static int shudclob; 68 69 checkmode = value("checkmode") != NULL; 70 if ((name = expand(name)) == NULL) 71 return (-1); 72 73 if ((ibuf = Fopen(name, "r")) == NULL) { 74 if (!isedit && errno == ENOENT) 75 goto nomail; 76 warn("%s", name); 77 return (-1); 78 } 79 80 if (fstat(fileno(ibuf), &stb) < 0) { 81 warn("fstat"); 82 (void)Fclose(ibuf); 83 return (-1); 84 } 85 86 if (S_ISDIR(stb.st_mode) || !S_ISREG(stb.st_mode)) { 87 (void)Fclose(ibuf); 88 errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL; 89 warn("%s", name); 90 return (-1); 91 } 92 93 /* 94 * Looks like all will be well. We must now relinquish our 95 * hold on the current set of stuff. Must hold signals 96 * while we are reading the new file, else we will ruin 97 * the message[] data structure. 98 */ 99 100 holdsigs(); 101 if (shudclob) 102 quit(); 103 104 /* 105 * Copy the messages into /tmp 106 * and set pointers. 107 */ 108 109 readonly = 0; 110 if ((i = open(name, 1)) < 0) 111 readonly++; 112 else 113 (void)close(i); 114 if (shudclob) { 115 (void)fclose(itf); 116 (void)fclose(otf); 117 } 118 shudclob = 1; 119 edit = isedit; 120 strlcpy(prevfile, mailname, sizeof(prevfile)); 121 if (name != mailname) 122 strlcpy(mailname, name, sizeof(mailname)); 123 mailsize = fsize(ibuf); 124 (void)snprintf(tempname, sizeof(tempname), 125 "%s/mail.RxXXXXXXXXXX", tmpdir); 126 if ((fd = mkstemp(tempname)) == -1 || (otf = fdopen(fd, "w")) == NULL) 127 err(1, "%s", tempname); 128 (void)fcntl(fileno(otf), F_SETFD, 1); 129 if ((itf = fopen(tempname, "r")) == NULL) 130 err(1, "%s", tempname); 131 (void)fcntl(fileno(itf), F_SETFD, 1); 132 (void)rm(tempname); 133 setptr(ibuf, 0); 134 setmsize(msgCount); 135 /* 136 * New mail may have arrived while we were reading 137 * the mail file, so reset mailsize to be where 138 * we really are in the file... 139 */ 140 mailsize = ftello(ibuf); 141 (void)Fclose(ibuf); 142 relsesigs(); 143 sawcom = 0; 144 145 if ((checkmode || !edit) && msgCount == 0) { 146 nomail: 147 if (!checkmode) { 148 fprintf(stderr, "No mail for %s\n", who); 149 return (-1); 150 } else 151 return (0); 152 } 153 return (checkmode ? 1 : 0); 154 } 155 156 /* 157 * Incorporate any new mail that has arrived since we first 158 * started reading mail. 159 */ 160 int 161 incfile(void) 162 { 163 off_t newsize; 164 int omsgCount = msgCount; 165 FILE *ibuf; 166 167 ibuf = Fopen(mailname, "r"); 168 if (ibuf == NULL) 169 return (-1); 170 holdsigs(); 171 newsize = fsize(ibuf); 172 if (newsize == 0) 173 return (-1); /* mail box is now empty??? */ 174 if (newsize < mailsize) 175 return (-1); /* mail box has shrunk??? */ 176 if (newsize == mailsize) 177 return (0); /* no new mail */ 178 setptr(ibuf, mailsize); 179 setmsize(msgCount); 180 mailsize = ftello(ibuf); 181 (void)Fclose(ibuf); 182 relsesigs(); 183 return (msgCount - omsgCount); 184 } 185 186 static int *msgvec; 187 static int reset_on_stop; /* do a reset() if stopped */ 188 189 /* 190 * Interpret user commands one by one. If standard input is not a tty, 191 * print no prompt. 192 */ 193 void 194 commands(void) 195 { 196 int n, eofloop = 0; 197 char linebuf[LINESIZE]; 198 199 if (!sourcing) { 200 if (signal(SIGINT, SIG_IGN) != SIG_IGN) 201 (void)signal(SIGINT, intr); 202 (void)signal(SIGTSTP, stop); 203 (void)signal(SIGTTOU, stop); 204 (void)signal(SIGTTIN, stop); 205 } 206 setexit(); 207 for (;;) { 208 /* 209 * Print the prompt, if needed. Clear out 210 * string space, and flush the output. 211 */ 212 if (!sourcing && value("interactive") != NULL) { 213 if ((value("autoinc") != NULL) && (incfile() > 0)) 214 printf("New mail has arrived.\n"); 215 reset_on_stop = 1; 216 printf("%s", prompt); 217 } 218 (void)fflush(stdout); 219 sreset(); 220 /* 221 * Read a line of commands from the current input 222 * and handle end of file specially. 223 */ 224 n = 0; 225 for (;;) { 226 if (readline(input, &linebuf[n], LINESIZE - n) < 0) { 227 if (n == 0) 228 n = -1; 229 break; 230 } 231 if ((n = strlen(linebuf)) == 0) 232 break; 233 n--; 234 if (linebuf[n] != '\\') 235 break; 236 linebuf[n++] = ' '; 237 } 238 reset_on_stop = 0; 239 if (n < 0) { 240 /* eof */ 241 if (loading) 242 break; 243 if (sourcing) { 244 unstack(); 245 continue; 246 } 247 if (value("interactive") != NULL && 248 value("ignoreeof") != NULL && 249 ++eofloop < 25) { 250 printf("Use \"quit\" to quit.\n"); 251 continue; 252 } 253 break; 254 } 255 eofloop = 0; 256 if (execute(linebuf, 0)) 257 break; 258 } 259 } 260 261 /* 262 * Execute a single command. 263 * Command functions return 0 for success, 1 for error, and -1 264 * for abort. A 1 or -1 aborts a load or source. A -1 aborts 265 * the interactive command loop. 266 * Contxt is non-zero if called while composing mail. 267 */ 268 int 269 execute(char linebuf[], int contxt) 270 { 271 char word[LINESIZE]; 272 char *arglist[MAXARGC]; 273 const struct cmd *com; 274 char *cp, *cp2; 275 int c, muvec[2]; 276 int e = 1; 277 278 /* 279 * Strip the white space away from the beginning 280 * of the command, then scan out a word, which 281 * consists of anything except digits and white space. 282 * 283 * Handle ! escapes differently to get the correct 284 * lexical conventions. 285 */ 286 287 for (cp = linebuf; isspace((unsigned char)*cp); cp++) 288 ; 289 if (*cp == '!') { 290 if (sourcing) { 291 printf("Can't \"!\" while sourcing\n"); 292 goto out; 293 } 294 shell(cp+1); 295 return (0); 296 } 297 cp2 = word; 298 while (*cp != '\0' && strchr(" \t0123456789$^.:/-+*'\"", *cp) == NULL) 299 *cp2++ = *cp++; 300 *cp2 = '\0'; 301 302 /* 303 * Look up the command; if not found, bitch. 304 * Normally, a blank command would map to the 305 * first command in the table; while sourcing, 306 * however, we ignore blank lines to eliminate 307 * confusion. 308 */ 309 310 if (sourcing && *word == '\0') 311 return (0); 312 com = lex(word); 313 if (com == NULL) { 314 printf("Unknown command: \"%s\"\n", word); 315 goto out; 316 } 317 318 /* 319 * See if we should execute the command -- if a conditional 320 * we always execute it, otherwise, check the state of cond. 321 */ 322 323 if ((com->c_argtype & F) == 0) 324 if ((cond == CRCV && !rcvmode) || (cond == CSEND && rcvmode)) 325 return (0); 326 327 /* 328 * Process the arguments to the command, depending 329 * on the type he expects. Default to an error. 330 * If we are sourcing an interactive command, it's 331 * an error. 332 */ 333 334 if (!rcvmode && (com->c_argtype & M) == 0) { 335 printf("May not execute \"%s\" while sending\n", 336 com->c_name); 337 goto out; 338 } 339 if (sourcing && com->c_argtype & I) { 340 printf("May not execute \"%s\" while sourcing\n", 341 com->c_name); 342 goto out; 343 } 344 if (readonly && com->c_argtype & W) { 345 printf("May not execute \"%s\" -- message file is read only\n", 346 com->c_name); 347 goto out; 348 } 349 if (contxt && com->c_argtype & R) { 350 printf("Cannot recursively invoke \"%s\"\n", com->c_name); 351 goto out; 352 } 353 switch (com->c_argtype & ~(F|P|I|M|T|W|R)) { 354 case MSGLIST: 355 /* 356 * A message list defaulting to nearest forward 357 * legal message. 358 */ 359 if (msgvec == 0) { 360 printf("Illegal use of \"message list\"\n"); 361 break; 362 } 363 if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0) 364 break; 365 if (c == 0) { 366 *msgvec = first(com->c_msgflag, com->c_msgmask); 367 msgvec[1] = 0; 368 } 369 if (*msgvec == 0) { 370 printf("No applicable messages\n"); 371 break; 372 } 373 e = (*com->c_func)(msgvec); 374 break; 375 376 case NDMLIST: 377 /* 378 * A message list with no defaults, but no error 379 * if none exist. 380 */ 381 if (msgvec == 0) { 382 printf("Illegal use of \"message list\"\n"); 383 break; 384 } 385 if (getmsglist(cp, msgvec, com->c_msgflag) < 0) 386 break; 387 e = (*com->c_func)(msgvec); 388 break; 389 390 case STRLIST: 391 /* 392 * Just the straight string, with 393 * leading blanks removed. 394 */ 395 while (isspace((unsigned char)*cp)) 396 cp++; 397 e = (*com->c_func)(cp); 398 break; 399 400 case RAWLIST: 401 /* 402 * A vector of strings, in shell style. 403 */ 404 if ((c = getrawlist(cp, arglist, 405 sizeof(arglist) / sizeof(*arglist))) < 0) 406 break; 407 if (c < com->c_minargs) { 408 printf("%s requires at least %d arg(s)\n", 409 com->c_name, com->c_minargs); 410 break; 411 } 412 if (c > com->c_maxargs) { 413 printf("%s takes no more than %d arg(s)\n", 414 com->c_name, com->c_maxargs); 415 break; 416 } 417 e = (*com->c_func)(arglist); 418 break; 419 420 case NOLIST: 421 /* 422 * Just the constant zero, for exiting, 423 * eg. 424 */ 425 e = (*com->c_func)(0); 426 break; 427 428 default: 429 errx(1, "Unknown argtype"); 430 } 431 432 out: 433 /* 434 * Exit the current source file on 435 * error. 436 */ 437 if (e) { 438 if (e < 0) 439 return (1); 440 if (loading) 441 return (1); 442 if (sourcing) 443 unstack(); 444 return (0); 445 } 446 if (com == NULL) 447 return (0); 448 if (value("autoprint") != NULL && com->c_argtype & P) 449 if ((dot->m_flag & MDELETED) == 0) { 450 muvec[0] = dot - &message[0] + 1; 451 muvec[1] = 0; 452 type(muvec); 453 } 454 if (!sourcing && (com->c_argtype & T) == 0) 455 sawcom = 1; 456 return (0); 457 } 458 459 /* 460 * Set the size of the message vector used to construct argument 461 * lists to message list functions. 462 */ 463 void 464 setmsize(int sz) 465 { 466 467 if (msgvec != NULL) 468 (void)free(msgvec); 469 msgvec = calloc((unsigned)(sz + 1), sizeof(*msgvec)); 470 } 471 472 /* 473 * Find the correct command in the command table corresponding 474 * to the passed command "word" 475 */ 476 477 const struct cmd * 478 lex(char word[]) 479 { 480 const struct cmd *cp; 481 482 /* 483 * ignore trailing chars after `#' 484 * 485 * lines with beginning `#' are comments 486 * spaces before `#' are ignored in execute() 487 */ 488 489 if (*word == '#') 490 *(word+1) = '\0'; 491 492 493 for (cp = &cmdtab[0]; cp->c_name != NULL; cp++) 494 if (isprefix(word, cp->c_name)) 495 return (cp); 496 return (NULL); 497 } 498 499 /* 500 * Determine if as1 is a valid prefix of as2. 501 * Return true if yep. 502 */ 503 int 504 isprefix(const char *as1, const char *as2) 505 { 506 const char *s1, *s2; 507 508 s1 = as1; 509 s2 = as2; 510 while (*s1++ == *s2) 511 if (*s2++ == '\0') 512 return (1); 513 return (*--s1 == '\0'); 514 } 515 516 /* 517 * The following gets called on receipt of an interrupt. This is 518 * to abort printout of a command, mainly. 519 * Dispatching here when command() is inactive crashes rcv. 520 * Close all open files except 0, 1, 2, and the temporary. 521 * Also, unstack all source files. 522 */ 523 524 static int inithdr; /* am printing startup headers */ 525 526 void 527 intr(int s __unused) 528 { 529 530 noreset = 0; 531 if (!inithdr) 532 sawcom++; 533 inithdr = 0; 534 while (sourcing) 535 unstack(); 536 537 close_all_files(); 538 539 if (image >= 0) { 540 (void)close(image); 541 image = -1; 542 } 543 fprintf(stderr, "Interrupt\n"); 544 reset(0); 545 } 546 547 /* 548 * When we wake up after ^Z, reprint the prompt. 549 */ 550 void 551 stop(int s) 552 { 553 sig_t old_action = signal(s, SIG_DFL); 554 sigset_t nset; 555 556 (void)sigemptyset(&nset); 557 (void)sigaddset(&nset, s); 558 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL); 559 (void)kill(0, s); 560 (void)sigprocmask(SIG_BLOCK, &nset, NULL); 561 (void)signal(s, old_action); 562 if (reset_on_stop) { 563 reset_on_stop = 0; 564 reset(0); 565 } 566 } 567 568 /* 569 * Announce the presence of the current Mail version, 570 * give the message count, and print a header listing. 571 */ 572 void 573 announce(void) 574 { 575 int vec[2], mdot; 576 577 mdot = newfileinfo(0); 578 vec[0] = mdot; 579 vec[1] = 0; 580 dot = &message[mdot - 1]; 581 if (msgCount > 0 && value("noheader") == NULL) { 582 inithdr++; 583 headers(vec); 584 inithdr = 0; 585 } 586 } 587 588 /* 589 * Announce information about the file we are editing. 590 * Return a likely place to set dot. 591 */ 592 int 593 newfileinfo(int omsgCount) 594 { 595 struct message *mp; 596 int u, n, mdot, d, s; 597 char fname[PATHSIZE+1], zname[PATHSIZE+1], *ename; 598 599 for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++) 600 if (mp->m_flag & MNEW) 601 break; 602 if (mp >= &message[msgCount]) 603 for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++) 604 if ((mp->m_flag & MREAD) == 0) 605 break; 606 if (mp < &message[msgCount]) 607 mdot = mp - &message[0] + 1; 608 else 609 mdot = omsgCount + 1; 610 s = d = 0; 611 for (mp = &message[0], n = 0, u = 0; mp < &message[msgCount]; mp++) { 612 if (mp->m_flag & MNEW) 613 n++; 614 if ((mp->m_flag & MREAD) == 0) 615 u++; 616 if (mp->m_flag & MDELETED) 617 d++; 618 if (mp->m_flag & MSAVED) 619 s++; 620 } 621 ename = mailname; 622 if (getfold(fname, sizeof(fname) - 1) >= 0) { 623 strcat(fname, "/"); 624 if (strncmp(fname, mailname, strlen(fname)) == 0) { 625 (void)snprintf(zname, sizeof(zname), "+%s", 626 mailname + strlen(fname)); 627 ename = zname; 628 } 629 } 630 printf("\"%s\": ", ename); 631 if (msgCount == 1) 632 printf("1 message"); 633 else 634 printf("%d messages", msgCount); 635 if (n > 0) 636 printf(" %d new", n); 637 if (u-n > 0) 638 printf(" %d unread", u); 639 if (d > 0) 640 printf(" %d deleted", d); 641 if (s > 0) 642 printf(" %d saved", s); 643 if (readonly) 644 printf(" [Read only]"); 645 printf("\n"); 646 return (mdot); 647 } 648 649 /* 650 * Print the current version number. 651 */ 652 653 int 654 pversion(void *arg __unused) 655 { 656 657 printf("Version %s\n", version); 658 return (0); 659 } 660 661 /* 662 * Load a file of user definitions. 663 */ 664 void 665 load(char *name) 666 { 667 FILE *in, *oldin; 668 669 if ((in = Fopen(name, "r")) == NULL) 670 return; 671 oldin = input; 672 input = in; 673 loading = 1; 674 sourcing = 1; 675 commands(); 676 loading = 0; 677 sourcing = 0; 678 input = oldin; 679 (void)Fclose(in); 680 } 681