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