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