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