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