1 /* $NetBSD: lex.c,v 1.27 2006/09/29 14:59:31 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.27 2006/09/29 14:59:31 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 n; 207 char linebuf[LINESIZE]; 208 volatile int eofloop = 0; /* avoid longjmp clobbering */ 209 210 if (!sourcing) { 211 if (signal(SIGINT, SIG_IGN) != SIG_IGN) 212 (void)signal(SIGINT, intr); 213 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) 214 (void)signal(SIGHUP, hangup); 215 (void)signal(SIGTSTP, stop); 216 (void)signal(SIGTTOU, stop); 217 (void)signal(SIGTTIN, stop); 218 } 219 setexit(); /* defined as (void)setjmp(srbuf) in def.h */ 220 for (;;) { 221 /* 222 * Print the prompt, if needed. Clear out 223 * string space, and flush the output. 224 */ 225 if (!sourcing && value("interactive") != NULL) { 226 if ((value("autoinc") != NULL) && (incfile() > 0)) 227 (void)printf("New mail has arrived.\n"); 228 reset_on_stop = 1; 229 #ifndef USE_READLINE 230 (void)printf("%s", prompt); 231 #endif 232 } 233 (void)fflush(stdout); 234 sreset(); 235 /* 236 * Read a line of commands from the current input 237 * and handle end of file specially. 238 */ 239 n = 0; 240 for (;;) { 241 #ifdef USE_READLINE 242 if (!sourcing) { 243 char *line; 244 245 if ((line = rl_gets(prompt)) == NULL) { 246 if (n == 0) 247 n = -1; 248 break; 249 } 250 strncpy(linebuf, line, LINESIZE); 251 } 252 else { 253 if (readline(input, &linebuf[n], LINESIZE - n) < 0) { 254 if (n == 0) 255 n = -1; 256 break; 257 } 258 } 259 #else /* USE_READLINE */ 260 if (readline(input, &linebuf[n], LINESIZE - n) < 0) { 261 if (n == 0) 262 n = -1; 263 break; 264 } 265 #endif /* USE_READLINE */ 266 267 if (sourcing) { /* allow comments in source files */ 268 char *ptr = strchr(linebuf, '#'); 269 if (ptr) 270 *ptr = '\0'; 271 } 272 if ((n = strlen(linebuf)) == 0) 273 break; 274 n--; 275 if (linebuf[n] != '\\') 276 break; 277 linebuf[n++] = ' '; 278 } 279 reset_on_stop = 0; 280 if (n < 0) { 281 /* eof */ 282 if (loading) 283 break; 284 if (sourcing) { 285 (void)unstack(); 286 continue; 287 } 288 #ifdef USE_READLINE 289 { 290 char *p; 291 if (value("interactive") != NULL && 292 (p=value("ignoreeof")) != NULL && 293 ++eofloop < (*p==0 ? 25 : atoi(p))) { 294 (void)printf("Use \"quit\" to quit.\n"); 295 continue; 296 } 297 } 298 #else 299 if (value("interactive") != NULL && 300 value("ignoreeof") != NULL && 301 ++eofloop < 25) { 302 (void)printf("Use \"quit\" to quit.\n"); 303 continue; 304 } 305 #endif 306 break; 307 } 308 eofloop = 0; 309 if (execute(linebuf, 0)) 310 break; 311 } 312 } 313 314 /* 315 * Execute a single command. 316 * Command functions return 0 for success, 1 for error, and -1 317 * for abort. A 1 or -1 aborts a load or source. A -1 aborts 318 * the interactive command loop. 319 * Contxt is non-zero if called while composing mail. 320 */ 321 int 322 execute(char linebuf[], int contxt) 323 { 324 char word[LINESIZE]; 325 char *arglist[MAXARGC]; 326 const struct cmd *com = NULL; 327 char *cp, *cp2; 328 int c; 329 int muvec[2]; 330 int e = 1; 331 332 /* 333 * Strip the white space away from the beginning 334 * of the command, then scan out a word, which 335 * consists of anything except digits and white space. 336 * 337 * Handle ! escapes differently to get the correct 338 * lexical conventions. 339 */ 340 341 for (cp = linebuf; isspace((unsigned char)*cp); cp++) 342 ; 343 if (*cp == '!') { 344 if (sourcing) { 345 (void)printf("Can't \"!\" while sourcing\n"); 346 goto out; 347 } 348 (void)shell(cp+1); 349 return(0); 350 } 351 cp2 = word; 352 while (*cp && strchr(" \t0123456789$^.:/-+*'\"", *cp) == NULL) 353 *cp2++ = *cp++; 354 *cp2 = '\0'; 355 356 /* 357 * Look up the command; if not found, bitch. 358 * Normally, a blank command would map to the 359 * first command in the table; while sourcing, 360 * however, we ignore blank lines to eliminate 361 * confusion. 362 */ 363 364 if (sourcing && *word == '\0') 365 return(0); 366 com = lex(word); 367 if (com == NULL) { 368 (void)printf("Unknown command: \"%s\"\n", word); 369 goto out; 370 } 371 372 /* 373 * See if we should execute the command -- if a conditional 374 * we always execute it, otherwise, check the state of cond. 375 */ 376 377 if ((com->c_argtype & F) == 0) 378 if ((cond == CRCV && !rcvmode) || (cond == CSEND && rcvmode)) 379 return(0); 380 381 /* 382 * Process the arguments to the command, depending 383 * on the type he expects. Default to an error. 384 * If we are sourcing an interactive command, it's 385 * an error. 386 */ 387 388 if (!rcvmode && (com->c_argtype & M) == 0) { 389 (void)printf("May not execute \"%s\" while sending\n", 390 com->c_name); 391 goto out; 392 } 393 if (sourcing && com->c_argtype & I) { 394 (void)printf("May not execute \"%s\" while sourcing\n", 395 com->c_name); 396 goto out; 397 } 398 if (readonly && com->c_argtype & W) { 399 (void)printf("May not execute \"%s\" -- message file is read only\n", 400 com->c_name); 401 goto out; 402 } 403 if (contxt && com->c_argtype & R) { 404 (void)printf("Cannot recursively invoke \"%s\"\n", com->c_name); 405 goto out; 406 } 407 switch (com->c_argtype & ~(F|P|I|M|T|W|R)) { 408 case MSGLIST: 409 /* 410 * A message list defaulting to nearest forward 411 * legal message. 412 */ 413 if (msgvec == 0) { 414 (void)printf("Illegal use of \"message list\"\n"); 415 break; 416 } 417 if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0) 418 break; 419 if (c == 0) { 420 *msgvec = first(com->c_msgflag, 421 com->c_msgmask); 422 msgvec[1] = 0; 423 } 424 if (*msgvec == 0) { 425 (void)printf("No applicable messages\n"); 426 break; 427 } 428 e = (*com->c_func)(msgvec); 429 break; 430 431 case NDMLIST: 432 /* 433 * A message list with no defaults, but no error 434 * if none exist. 435 */ 436 if (msgvec == 0) { 437 (void)printf("Illegal use of \"message list\"\n"); 438 break; 439 } 440 if (getmsglist(cp, msgvec, com->c_msgflag) < 0) 441 break; 442 e = (*com->c_func)(msgvec); 443 break; 444 445 case STRLIST: 446 /* 447 * Just the straight string, with 448 * leading blanks removed. 449 */ 450 while (isspace((unsigned char)*cp)) 451 cp++; 452 e = (*com->c_func)(cp); 453 break; 454 455 case RAWLIST: 456 /* 457 * A vector of strings, in shell style. 458 */ 459 if ((c = getrawlist(cp, arglist, 460 sizeof arglist / sizeof *arglist)) < 0) 461 break; 462 if (c < com->c_minargs) { 463 (void)printf("%s requires at least %d arg(s)\n", 464 com->c_name, com->c_minargs); 465 break; 466 } 467 if (c > com->c_maxargs) { 468 (void)printf("%s takes no more than %d arg(s)\n", 469 com->c_name, com->c_maxargs); 470 break; 471 } 472 e = (*com->c_func)(arglist); 473 break; 474 475 case NOLIST: 476 /* 477 * Just the constant zero, for exiting, 478 * eg. 479 */ 480 e = (*com->c_func)(0); 481 break; 482 483 default: 484 errx(1, "Unknown argtype"); 485 } 486 487 out: 488 /* 489 * Exit the current source file on 490 * error. 491 */ 492 if (e) { 493 if (e < 0) 494 return 1; 495 if (loading) 496 return 1; 497 if (sourcing) 498 (void)unstack(); 499 return 0; 500 } 501 if (com == NULL) 502 return(0); 503 if (value("autoprint") != NULL && com->c_argtype & P) 504 if ((dot->m_flag & MDELETED) == 0) { 505 muvec[0] = dot - &message[0] + 1; 506 muvec[1] = 0; 507 (void)type(muvec); 508 } 509 if (!sourcing && (com->c_argtype & T) == 0) 510 sawcom = 1; 511 return(0); 512 } 513 514 /* 515 * Set the size of the message vector used to construct argument 516 * lists to message list functions. 517 */ 518 void 519 setmsize(int sz) 520 { 521 522 if (msgvec != 0) 523 free( msgvec); 524 msgvec = calloc((size_t) (sz + 1), sizeof *msgvec); 525 } 526 527 /* 528 * Find the correct command in the command table corresponding 529 * to the passed command "word" 530 */ 531 532 const struct cmd * 533 lex(char word[]) 534 { 535 const struct cmd *cp; 536 537 for (cp = &cmdtab[0]; cp->c_name != NULL; cp++) 538 if (isprefix(word, cp->c_name)) 539 return(cp); 540 return(NULL); 541 } 542 543 /* 544 * Determine if as1 is a valid prefix of as2. 545 * Return true if yep. 546 */ 547 int 548 isprefix(char *as1, const char *as2) 549 { 550 char *s1; 551 const char *s2; 552 553 s1 = as1; 554 s2 = as2; 555 while (*s1++ == *s2) 556 if (*s2++ == '\0') 557 return(1); 558 return(*--s1 == '\0'); 559 } 560 561 /* 562 * The following gets called on receipt of an interrupt. This is 563 * to abort printout of a command, mainly. 564 * Dispatching here when command() is inactive crashes rcv. 565 * Close all open files except 0, 1, 2, and the temporary. 566 * Also, unstack all source files. 567 */ 568 569 int inithdr; /* am printing startup headers */ 570 571 /*ARGSUSED*/ 572 void 573 intr(int s) 574 { 575 576 noreset = 0; 577 if (!inithdr) 578 sawcom++; 579 inithdr = 0; 580 while (sourcing) 581 (void)unstack(); 582 583 close_all_files(); 584 585 if (image >= 0) { 586 (void)close(image); 587 image = -1; 588 } 589 (void)fprintf(stderr, "Interrupt\n"); 590 reset(0); 591 } 592 593 /* 594 * When we wake up after ^Z, reprint the prompt. 595 */ 596 void 597 stop(int s) 598 { 599 sig_t old_action = signal(s, SIG_DFL); 600 sigset_t nset; 601 602 (void)sigemptyset(&nset); 603 (void)sigaddset(&nset, s); 604 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL); 605 (void)kill(0, s); 606 (void)sigprocmask(SIG_BLOCK, &nset, NULL); 607 (void)signal(s, old_action); 608 if (reset_on_stop) { 609 reset_on_stop = 0; 610 reset(0); 611 } 612 } 613 614 /* 615 * Branch here on hangup signal and simulate "exit". 616 */ 617 /*ARGSUSED*/ 618 void 619 hangup(int s) 620 { 621 622 /* nothing to do? */ 623 exit(1); 624 } 625 626 /* 627 * Announce the presence of the current Mail version, 628 * give the message count, and print a header listing. 629 */ 630 void 631 announce(void) 632 { 633 int vec[2], mdot; 634 635 mdot = newfileinfo(0); 636 vec[0] = mdot; 637 vec[1] = 0; 638 dot = &message[mdot - 1]; 639 if (msgCount > 0 && value("noheader") == NULL) { 640 inithdr++; 641 (void)headers(vec); 642 inithdr = 0; 643 } 644 } 645 646 /* 647 * Announce information about the file we are editing. 648 * Return a likely place to set dot. 649 */ 650 int 651 newfileinfo(int omsgCount) 652 { 653 struct message *mp; 654 int u, n, mdot, d, s; 655 size_t l; 656 char fname[PATHSIZE], zname[PATHSIZE], *ename; 657 658 for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++) 659 if (mp->m_flag & MNEW) 660 break; 661 if (mp >= &message[msgCount]) 662 for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++) 663 if ((mp->m_flag & MREAD) == 0) 664 break; 665 if (mp < &message[msgCount]) 666 mdot = mp - &message[0] + 1; 667 else 668 mdot = omsgCount + 1; 669 s = d = 0; 670 for (mp = &message[0], n = 0, u = 0; mp < &message[msgCount]; mp++) { 671 if (mp->m_flag & MNEW) 672 n++; 673 if ((mp->m_flag & MREAD) == 0) 674 u++; 675 if (mp->m_flag & MDELETED) 676 d++; 677 if (mp->m_flag & MSAVED) 678 s++; 679 } 680 ename = mailname; 681 if (getfold(fname) >= 0) { 682 l = strlen(fname); 683 if (l < PATHSIZE - 1) 684 fname[l++] = '/'; 685 if (strncmp(fname, mailname, l) == 0) { 686 (void)snprintf(zname, PATHSIZE, "+%s", 687 mailname + l); 688 ename = zname; 689 } 690 } 691 (void)printf("\"%s\": ", ename); 692 if (msgCount == 1) 693 (void)printf("1 message"); 694 else 695 (void)printf("%d messages", msgCount); 696 if (n > 0) 697 (void)printf(" %d new", n); 698 if (u-n > 0) 699 (void)printf(" %d unread", u); 700 if (d > 0) 701 (void)printf(" %d deleted", d); 702 if (s > 0) 703 (void)printf(" %d saved", s); 704 if (readonly) 705 (void)printf(" [Read only]"); 706 (void)printf("\n"); 707 return(mdot); 708 } 709 710 /* 711 * Print the current version number. 712 */ 713 714 /*ARGSUSED*/ 715 int 716 pversion(void *v) 717 { 718 (void)printf("Version %s\n", version); 719 return(0); 720 } 721 722 /* 723 * Load a file of user definitions. 724 */ 725 void 726 load(const char *name) 727 { 728 FILE *in, *oldin; 729 730 if ((in = Fopen(name, "r")) == NULL) 731 return; 732 oldin = input; 733 input = in; 734 loading = 1; 735 sourcing = 1; 736 commands(); 737 loading = 0; 738 sourcing = 0; 739 input = oldin; 740 (void)Fclose(in); 741 } 742