1 /* $NetBSD: list.c,v 1.8 1997/10/19 05:03:32 lukem 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 #if 0 39 static char sccsid[] = "@(#)list.c 8.4 (Berkeley) 5/1/95"; 40 #else 41 __RCSID("$NetBSD: list.c,v 1.8 1997/10/19 05:03:32 lukem Exp $"); 42 #endif 43 #endif /* not lint */ 44 45 #include "rcv.h" 46 #include "extern.h" 47 48 int matchto __P((char *, int)); 49 50 /* 51 * Mail -- a mail program 52 * 53 * Message list handling. 54 */ 55 56 /* 57 * Convert the user string of message numbers and 58 * store the numbers into vector. 59 * 60 * Returns the count of messages picked up or -1 on error. 61 */ 62 int 63 getmsglist(buf, vector, flags) 64 char *buf; 65 int *vector, flags; 66 { 67 int *ip; 68 struct message *mp; 69 70 if (msgCount == 0) { 71 *vector = 0; 72 return 0; 73 } 74 if (markall(buf, flags) < 0) 75 return(-1); 76 ip = vector; 77 for (mp = &message[0]; mp < &message[msgCount]; mp++) 78 if (mp->m_flag & MMARK) 79 *ip++ = mp - &message[0] + 1; 80 *ip = 0; 81 return(ip - vector); 82 } 83 84 /* 85 * Mark all messages that the user wanted from the command 86 * line in the message structure. Return 0 on success, -1 87 * on error. 88 */ 89 90 /* 91 * Bit values for colon modifiers. 92 */ 93 94 #define CMNEW 01 /* New messages */ 95 #define CMOLD 02 /* Old messages */ 96 #define CMUNREAD 04 /* Unread messages */ 97 #define CMDELETED 010 /* Deleted messages */ 98 #define CMREAD 020 /* Read messages */ 99 100 /* 101 * The following table describes the letters which can follow 102 * the colon and gives the corresponding modifier bit. 103 */ 104 105 struct coltab { 106 char co_char; /* What to find past : */ 107 int co_bit; /* Associated modifier bit */ 108 int co_mask; /* m_status bits to mask */ 109 int co_equal; /* ... must equal this */ 110 } coltab[] = { 111 { 'n', CMNEW, MNEW, MNEW }, 112 { 'o', CMOLD, MNEW, 0 }, 113 { 'u', CMUNREAD, MREAD, 0 }, 114 { 'd', CMDELETED, MDELETED, MDELETED }, 115 { 'r', CMREAD, MREAD, MREAD }, 116 { 0, 0, 0, 0 } 117 }; 118 119 static int lastcolmod; 120 121 int 122 markall(buf, f) 123 char buf[]; 124 int f; 125 { 126 char **np; 127 int i; 128 struct message *mp; 129 char *namelist[NMLSIZE], *bufp; 130 int tok, beg, mc, star, other, valdot, colmod, colresult; 131 132 valdot = dot - &message[0] + 1; 133 colmod = 0; 134 for (i = 1; i <= msgCount; i++) 135 unmark(i); 136 bufp = buf; 137 mc = 0; 138 np = &namelist[0]; 139 scaninit(); 140 tok = scan(&bufp); 141 star = 0; 142 other = 0; 143 beg = 0; 144 while (tok != TEOL) { 145 switch (tok) { 146 case TNUMBER: 147 number: 148 if (star) { 149 printf("No numbers mixed with *\n"); 150 return(-1); 151 } 152 mc++; 153 other++; 154 if (beg != 0) { 155 if (check(lexnumber, f)) 156 return(-1); 157 for (i = beg; i <= lexnumber; i++) 158 if (f == MDELETED || (message[i - 1].m_flag & MDELETED) == 0) 159 mark(i); 160 beg = 0; 161 break; 162 } 163 beg = lexnumber; 164 if (check(beg, f)) 165 return(-1); 166 tok = scan(&bufp); 167 regret(tok); 168 if (tok != TDASH) { 169 mark(beg); 170 beg = 0; 171 } 172 break; 173 174 case TPLUS: 175 if (beg != 0) { 176 printf("Non-numeric second argument\n"); 177 return(-1); 178 } 179 i = valdot; 180 do { 181 i++; 182 if (i > msgCount) { 183 printf("Referencing beyond EOF\n"); 184 return(-1); 185 } 186 } while ((message[i - 1].m_flag & MDELETED) != f); 187 mark(i); 188 break; 189 190 case TDASH: 191 if (beg == 0) { 192 i = valdot; 193 do { 194 i--; 195 if (i <= 0) { 196 printf("Referencing before 1\n"); 197 return(-1); 198 } 199 } while ((message[i - 1].m_flag & MDELETED) != f); 200 mark(i); 201 } 202 break; 203 204 case TSTRING: 205 if (beg != 0) { 206 printf("Non-numeric second argument\n"); 207 return(-1); 208 } 209 other++; 210 if (lexstring[0] == ':') { 211 colresult = evalcol(lexstring[1]); 212 if (colresult == 0) { 213 printf("Unknown colon modifier \"%s\"\n", 214 lexstring); 215 return(-1); 216 } 217 colmod |= colresult; 218 } 219 else 220 *np++ = savestr(lexstring); 221 break; 222 223 case TDOLLAR: 224 case TUP: 225 case TDOT: 226 lexnumber = metamess(lexstring[0], f); 227 if (lexnumber == -1) 228 return(-1); 229 goto number; 230 231 case TSTAR: 232 if (other) { 233 printf("Can't mix \"*\" with anything\n"); 234 return(-1); 235 } 236 star++; 237 break; 238 239 case TERROR: 240 return -1; 241 } 242 tok = scan(&bufp); 243 } 244 lastcolmod = colmod; 245 *np = NOSTR; 246 mc = 0; 247 if (star) { 248 for (i = 0; i < msgCount; i++) 249 if ((message[i].m_flag & MDELETED) == f) { 250 mark(i+1); 251 mc++; 252 } 253 if (mc == 0) { 254 printf("No applicable messages.\n"); 255 return(-1); 256 } 257 return(0); 258 } 259 260 /* 261 * If no numbers were given, mark all of the messages, 262 * so that we can unmark any whose sender was not selected 263 * if any user names were given. 264 */ 265 266 if ((np > namelist || colmod != 0) && mc == 0) 267 for (i = 1; i <= msgCount; i++) 268 if ((message[i-1].m_flag & MDELETED) == f) 269 mark(i); 270 271 /* 272 * If any names were given, go through and eliminate any 273 * messages whose senders were not requested. 274 */ 275 276 if (np > namelist) { 277 for (i = 1; i <= msgCount; i++) { 278 for (mc = 0, np = &namelist[0]; *np != NOSTR; np++) 279 if (**np == '/') { 280 if (matchsubj(*np, i)) { 281 mc++; 282 break; 283 } 284 } 285 else { 286 if (matchsender(*np, i)) { 287 mc++; 288 break; 289 } 290 } 291 if (mc == 0) 292 unmark(i); 293 } 294 295 /* 296 * Make sure we got some decent messages. 297 */ 298 299 mc = 0; 300 for (i = 1; i <= msgCount; i++) 301 if (message[i-1].m_flag & MMARK) { 302 mc++; 303 break; 304 } 305 if (mc == 0) { 306 printf("No applicable messages from {%s", 307 namelist[0]); 308 for (np = &namelist[1]; *np != NOSTR; np++) 309 printf(", %s", *np); 310 printf("}\n"); 311 return(-1); 312 } 313 } 314 315 /* 316 * If any colon modifiers were given, go through and 317 * unmark any messages which do not satisfy the modifiers. 318 */ 319 320 if (colmod != 0) { 321 for (i = 1; i <= msgCount; i++) { 322 struct coltab *colp; 323 324 mp = &message[i - 1]; 325 for (colp = &coltab[0]; colp->co_char; colp++) 326 if (colp->co_bit & colmod) 327 if ((mp->m_flag & colp->co_mask) 328 != colp->co_equal) 329 unmark(i); 330 331 } 332 for (mp = &message[0]; mp < &message[msgCount]; mp++) 333 if (mp->m_flag & MMARK) 334 break; 335 if (mp >= &message[msgCount]) { 336 struct coltab *colp; 337 338 printf("No messages satisfy"); 339 for (colp = &coltab[0]; colp->co_char; colp++) 340 if (colp->co_bit & colmod) 341 printf(" :%c", colp->co_char); 342 printf("\n"); 343 return(-1); 344 } 345 } 346 return(0); 347 } 348 349 /* 350 * Turn the character after a colon modifier into a bit 351 * value. 352 */ 353 int 354 evalcol(col) 355 int col; 356 { 357 struct coltab *colp; 358 359 if (col == 0) 360 return(lastcolmod); 361 for (colp = &coltab[0]; colp->co_char; colp++) 362 if (colp->co_char == col) 363 return(colp->co_bit); 364 return(0); 365 } 366 367 /* 368 * Check the passed message number for legality and proper flags. 369 * If f is MDELETED, then either kind will do. Otherwise, the message 370 * has to be undeleted. 371 */ 372 int 373 check(mesg, f) 374 int mesg, f; 375 { 376 struct message *mp; 377 378 if (mesg < 1 || mesg > msgCount) { 379 printf("%d: Invalid message number\n", mesg); 380 return(-1); 381 } 382 mp = &message[mesg-1]; 383 if (f != MDELETED && (mp->m_flag & MDELETED) != 0) { 384 printf("%d: Inappropriate message\n", mesg); 385 return(-1); 386 } 387 return(0); 388 } 389 390 /* 391 * Scan out the list of string arguments, shell style 392 * for a RAWLIST. 393 */ 394 int 395 getrawlist(line, argv, argc) 396 char line[]; 397 char **argv; 398 int argc; 399 { 400 char c, *cp, *cp2, quotec; 401 int argn; 402 char linebuf[BUFSIZ]; 403 404 argn = 0; 405 cp = line; 406 for (;;) { 407 for (; *cp == ' ' || *cp == '\t'; cp++) 408 ; 409 if (*cp == '\0') 410 break; 411 if (argn >= argc - 1) { 412 printf( 413 "Too many elements in the list; excess discarded.\n"); 414 break; 415 } 416 cp2 = linebuf; 417 quotec = '\0'; 418 while ((c = *cp) != '\0') { 419 cp++; 420 if (quotec != '\0') { 421 if (c == quotec) 422 quotec = '\0'; 423 else if (c == '\\') 424 switch (c = *cp++) { 425 case '\0': 426 *cp2++ = '\\'; 427 cp--; 428 break; 429 case '0': case '1': case '2': case '3': 430 case '4': case '5': case '6': case '7': 431 c -= '0'; 432 if (*cp >= '0' && *cp <= '7') 433 c = c * 8 + *cp++ - '0'; 434 if (*cp >= '0' && *cp <= '7') 435 c = c * 8 + *cp++ - '0'; 436 *cp2++ = c; 437 break; 438 case 'b': 439 *cp2++ = '\b'; 440 break; 441 case 'f': 442 *cp2++ = '\f'; 443 break; 444 case 'n': 445 *cp2++ = '\n'; 446 break; 447 case 'r': 448 *cp2++ = '\r'; 449 break; 450 case 't': 451 *cp2++ = '\t'; 452 break; 453 case 'v': 454 *cp2++ = '\v'; 455 break; 456 default: 457 *cp2++ = c; 458 } 459 else if (c == '^') { 460 c = *cp++; 461 if (c == '?') 462 *cp2++ = '\177'; 463 /* null doesn't show up anyway */ 464 else if ((c >= 'A' && c <= '_') || 465 (c >= 'a' && c <= 'z')) 466 *cp2++ = c & 037; 467 else { 468 *cp2++ = '^'; 469 cp--; 470 } 471 } else 472 *cp2++ = c; 473 } else if (c == '"' || c == '\'') 474 quotec = c; 475 else if (c == ' ' || c == '\t') 476 break; 477 else 478 *cp2++ = c; 479 } 480 *cp2 = '\0'; 481 argv[argn++] = savestr(linebuf); 482 } 483 argv[argn] = NOSTR; 484 return argn; 485 } 486 487 /* 488 * scan out a single lexical item and return its token number, 489 * updating the string pointer passed **p. Also, store the value 490 * of the number or string scanned in lexnumber or lexstring as 491 * appropriate. In any event, store the scanned `thing' in lexstring. 492 */ 493 494 struct lex { 495 char l_char; 496 char l_token; 497 } singles[] = { 498 { '$', TDOLLAR }, 499 { '.', TDOT }, 500 { '^', TUP }, 501 { '*', TSTAR }, 502 { '-', TDASH }, 503 { '+', TPLUS }, 504 { '(', TOPEN }, 505 { ')', TCLOSE }, 506 { 0, 0 } 507 }; 508 509 int 510 scan(sp) 511 char **sp; 512 { 513 char *cp, *cp2; 514 int c; 515 struct lex *lp; 516 int quotec; 517 518 if (regretp >= 0) { 519 strcpy(lexstring, string_stack[regretp]); 520 lexnumber = numberstack[regretp]; 521 return(regretstack[regretp--]); 522 } 523 cp = *sp; 524 cp2 = lexstring; 525 c = *cp++; 526 527 /* 528 * strip away leading white space. 529 */ 530 531 while (c == ' ' || c == '\t') 532 c = *cp++; 533 534 /* 535 * If no characters remain, we are at end of line, 536 * so report that. 537 */ 538 539 if (c == '\0') { 540 *sp = --cp; 541 return(TEOL); 542 } 543 544 /* 545 * If the leading character is a digit, scan 546 * the number and convert it on the fly. 547 * Return TNUMBER when done. 548 */ 549 550 if (isdigit(c)) { 551 lexnumber = 0; 552 while (isdigit(c)) { 553 lexnumber = lexnumber*10 + c - '0'; 554 *cp2++ = c; 555 c = *cp++; 556 } 557 *cp2 = '\0'; 558 *sp = --cp; 559 return(TNUMBER); 560 } 561 562 /* 563 * Check for single character tokens; return such 564 * if found. 565 */ 566 567 for (lp = &singles[0]; lp->l_char != 0; lp++) 568 if (c == lp->l_char) { 569 lexstring[0] = c; 570 lexstring[1] = '\0'; 571 *sp = cp; 572 return(lp->l_token); 573 } 574 575 /* 576 * We've got a string! Copy all the characters 577 * of the string into lexstring, until we see 578 * a null, space, or tab. 579 * If the lead character is a " or ', save it 580 * and scan until you get another. 581 */ 582 583 quotec = 0; 584 if (c == '\'' || c == '"') { 585 quotec = c; 586 c = *cp++; 587 } 588 while (c != '\0') { 589 if (c == quotec) { 590 cp++; 591 break; 592 } 593 if (quotec == 0 && (c == ' ' || c == '\t')) 594 break; 595 if (cp2 - lexstring < STRINGLEN-1) 596 *cp2++ = c; 597 c = *cp++; 598 } 599 if (quotec && c == 0) { 600 fprintf(stderr, "Missing %c\n", quotec); 601 return TERROR; 602 } 603 *sp = --cp; 604 *cp2 = '\0'; 605 return(TSTRING); 606 } 607 608 /* 609 * Unscan the named token by pushing it onto the regret stack. 610 */ 611 void 612 regret(token) 613 int token; 614 { 615 if (++regretp >= REGDEP) 616 errx(1, "Too many regrets"); 617 regretstack[regretp] = token; 618 lexstring[STRINGLEN-1] = '\0'; 619 string_stack[regretp] = savestr(lexstring); 620 numberstack[regretp] = lexnumber; 621 } 622 623 /* 624 * Reset all the scanner global variables. 625 */ 626 void 627 scaninit() 628 { 629 regretp = -1; 630 } 631 632 /* 633 * Find the first message whose flags & m == f and return 634 * its message number. 635 */ 636 int 637 first(f, m) 638 int f, m; 639 { 640 struct message *mp; 641 642 if (msgCount == 0) 643 return 0; 644 f &= MDELETED; 645 m &= MDELETED; 646 for (mp = dot; mp < &message[msgCount]; mp++) 647 if ((mp->m_flag & m) == f) 648 return mp - message + 1; 649 for (mp = dot-1; mp >= &message[0]; mp--) 650 if ((mp->m_flag & m) == f) 651 return mp - message + 1; 652 return 0; 653 } 654 655 /* 656 * See if the passed name sent the passed message number. Return true 657 * if so. 658 */ 659 int 660 matchsender(str, mesg) 661 char *str; 662 int mesg; 663 { 664 char *cp, *cp2, *backup; 665 666 if (!*str) /* null string matches nothing instead of everything */ 667 return 0; 668 backup = cp2 = nameof(&message[mesg - 1], 0); 669 cp = str; 670 while (*cp2) { 671 if (*cp == 0) 672 return(1); 673 if (raise(*cp++) != raise(*cp2++)) { 674 cp2 = ++backup; 675 cp = str; 676 } 677 } 678 return(*cp == 0); 679 } 680 681 /* 682 * See if the passed name received the passed message number. Return true 683 * if so. 684 */ 685 686 static char *to_fields[] = { "to", "cc", "bcc", 0 }; 687 688 int 689 matchto(str, mesg) 690 char *str; 691 int mesg; 692 { 693 struct message *mp; 694 char *cp, *cp2, *backup, **to; 695 696 str++; 697 698 if (*str == 0) /* null string matches nothing instead of everything */ 699 return(0); 700 701 mp = &message[mesg-1]; 702 703 for (to = to_fields; *to; to++) { 704 cp = str; 705 cp2 = hfield(*to, mp); 706 if (cp2 != NOSTR) { 707 backup = cp2; 708 while (*cp2) { 709 if (*cp == 0) 710 return(1); 711 if (raise(*cp++) != raise(*cp2++)) { 712 cp2 = ++backup; 713 cp = str; 714 } 715 } 716 if (*cp == 0) 717 return(1); 718 } 719 } 720 return(0); 721 } 722 723 /* 724 * See if the given string matches inside the subject field of the 725 * given message. For the purpose of the scan, we ignore case differences. 726 * If it does, return true. The string search argument is assumed to 727 * have the form "/search-string." If it is of the form "/," we use the 728 * previous search string. 729 */ 730 731 char lastscan[STRINGLEN]; 732 int 733 matchsubj(str, mesg) 734 char *str; 735 int mesg; 736 { 737 struct message *mp; 738 char *cp, *cp2, *backup; 739 740 str++; 741 if (*str == '\0') 742 str = lastscan; 743 else { 744 strncpy(lastscan, str, STRINGLEN - 1); 745 lastscan[STRINGLEN - 1] = '\0' ; 746 } 747 mp = &message[mesg-1]; 748 749 /* 750 * Now look, ignoring case, for the word in the string. 751 */ 752 753 if (value("searchheaders") && (cp = index(str, ':'))) { 754 /* Check for special case "/To:" */ 755 if (raise(str[0]) == 'T' && raise(str[1]) == 'O' && 756 str[2] == ':') 757 return(matchto(cp, mesg)); 758 *cp++ = '\0'; 759 cp2 = hfield(*str ? str : "subject", mp); 760 cp[-1] = ':'; 761 str = cp; 762 } else { 763 cp = str; 764 cp2 = hfield("subject", mp); 765 } 766 if (cp2 == NOSTR) 767 return(0); 768 backup = cp2; 769 while (*cp2) { 770 if (*cp == 0) 771 return(1); 772 if (raise(*cp++) != raise(*cp2++)) { 773 cp2 = ++backup; 774 cp = str; 775 } 776 } 777 return(*cp == 0); 778 } 779 780 /* 781 * Mark the named message by setting its mark bit. 782 */ 783 void 784 mark(mesg) 785 int mesg; 786 { 787 int i; 788 789 i = mesg; 790 if (i < 1 || i > msgCount) 791 errx(1, "Bad message number to mark"); 792 message[i-1].m_flag |= MMARK; 793 } 794 795 /* 796 * Unmark the named message. 797 */ 798 void 799 unmark(mesg) 800 int mesg; 801 { 802 int i; 803 804 i = mesg; 805 if (i < 1 || i > msgCount) 806 errx(1, "Bad message number to unmark"); 807 message[i-1].m_flag &= ~MMARK; 808 } 809 810 /* 811 * Return the message number corresponding to the passed meta character. 812 */ 813 int 814 metamess(meta, f) 815 int meta, f; 816 { 817 int c, m; 818 struct message *mp; 819 820 c = meta; 821 switch (c) { 822 case '^': 823 /* 824 * First 'good' message left. 825 */ 826 for (mp = &message[0]; mp < &message[msgCount]; mp++) 827 if ((mp->m_flag & MDELETED) == f) 828 return(mp - &message[0] + 1); 829 printf("No applicable messages\n"); 830 return(-1); 831 832 case '$': 833 /* 834 * Last 'good message left. 835 */ 836 for (mp = &message[msgCount-1]; mp >= &message[0]; mp--) 837 if ((mp->m_flag & MDELETED) == f) 838 return(mp - &message[0] + 1); 839 printf("No applicable messages\n"); 840 return(-1); 841 842 case '.': 843 /* 844 * Current message. 845 */ 846 m = dot - &message[0] + 1; 847 if ((dot->m_flag & MDELETED) != f) { 848 printf("%d: Inappropriate message\n", m); 849 return(-1); 850 } 851 return(m); 852 853 default: 854 printf("Unknown metachar (%c)\n", c); 855 return(-1); 856 } 857 } 858