2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)list.c 8.4 (Berkeley) 5/1/95";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
47 * Mail -- a mail program
49 * Message list handling.
53 * Convert the user string of message numbers and
54 * store the numbers into vector.
56 * Returns the count of messages picked up or -1 on error.
59 getmsglist(buf, vector, flags)
70 if (markall(buf, flags) < 0)
73 for (mp = &message[0]; mp < &message[msgCount]; mp++)
74 if (mp->m_flag & MMARK)
75 *ip++ = mp - &message[0] + 1;
81 * Mark all messages that the user wanted from the command
82 * line in the message structure. Return 0 on success, -1
87 * Bit values for colon modifiers.
90 #define CMNEW 01 /* New messages */
91 #define CMOLD 02 /* Old messages */
92 #define CMUNREAD 04 /* Unread messages */
93 #define CMDELETED 010 /* Deleted messages */
94 #define CMREAD 020 /* Read messages */
97 * The following table describes the letters which can follow
98 * the colon and gives the corresponding modifier bit.
101 static struct coltab {
102 char co_char; /* What to find past : */
103 int co_bit; /* Associated modifier bit */
104 int co_mask; /* m_status bits to mask */
105 int co_equal; /* ... must equal this */
107 { 'n', CMNEW, MNEW, MNEW },
108 { 'o', CMOLD, MNEW, 0 },
109 { 'u', CMUNREAD, MREAD, 0 },
110 { 'd', CMDELETED, MDELETED, MDELETED},
111 { 'r', CMREAD, MREAD, MREAD },
115 static int lastcolmod;
125 char *namelist[NMLSIZE], *bufp;
126 int tok, beg, mc, star, other, valdot, colmod, colresult;
128 valdot = dot - &message[0] + 1;
130 for (i = 1; i <= msgCount; i++)
140 while (tok != TEOL) {
145 printf("No numbers mixed with *\n");
151 if (check(lexnumber, f))
153 for (i = beg; i <= lexnumber; i++)
154 if (f == MDELETED || (message[i - 1].m_flag & MDELETED) == 0)
172 printf("Non-numeric second argument\n");
179 printf("Referencing beyond EOF\n");
182 } while ((message[i - 1].m_flag & MDELETED) != f);
192 printf("Referencing before 1\n");
195 } while ((message[i - 1].m_flag & MDELETED) != f);
202 printf("Non-numeric second argument\n");
206 if (lexstring[0] == ':') {
207 colresult = evalcol(lexstring[1]);
208 if (colresult == 0) {
209 printf("Unknown colon modifier \"%s\"\n",
216 *np++ = savestr(lexstring);
222 lexnumber = metamess(lexstring[0], f);
229 printf("Can't mix \"*\" with anything\n");
244 for (i = 0; i < msgCount; i++)
245 if ((message[i].m_flag & MDELETED) == f) {
250 printf("No applicable messages.\n");
257 * If no numbers were given, mark all of the messages,
258 * so that we can unmark any whose sender was not selected
259 * if any user names were given.
262 if ((np > namelist || colmod != 0) && mc == 0)
263 for (i = 1; i <= msgCount; i++)
264 if ((message[i-1].m_flag & MDELETED) == f)
268 * If any names were given, go through and eliminate any
269 * messages whose senders were not requested.
273 for (i = 1; i <= msgCount; i++) {
274 for (mc = 0, np = &namelist[0]; *np != NULL; np++)
276 if (matchfield(*np, i)) {
282 if (matchsender(*np, i)) {
292 * Make sure we got some decent messages.
296 for (i = 1; i <= msgCount; i++)
297 if (message[i-1].m_flag & MMARK) {
302 printf("No applicable messages from {%s",
304 for (np = &namelist[1]; *np != NULL; np++)
312 * If any colon modifiers were given, go through and
313 * unmark any messages which do not satisfy the modifiers.
317 for (i = 1; i <= msgCount; i++) {
320 mp = &message[i - 1];
321 for (colp = &coltab[0]; colp->co_char != '\0'; colp++)
322 if (colp->co_bit & colmod)
323 if ((mp->m_flag & colp->co_mask)
328 for (mp = &message[0]; mp < &message[msgCount]; mp++)
329 if (mp->m_flag & MMARK)
331 if (mp >= &message[msgCount]) {
334 printf("No messages satisfy");
335 for (colp = &coltab[0]; colp->co_char != '\0'; colp++)
336 if (colp->co_bit & colmod)
337 printf(" :%c", colp->co_char);
346 * Turn the character after a colon modifier into a bit
357 for (colp = &coltab[0]; colp->co_char != '\0'; colp++)
358 if (colp->co_char == col)
359 return (colp->co_bit);
364 * Check the passed message number for legality and proper flags.
365 * If f is MDELETED, then either kind will do. Otherwise, the message
366 * has to be undeleted.
374 if (mesg < 1 || mesg > msgCount) {
375 printf("%d: Invalid message number\n", mesg);
378 mp = &message[mesg-1];
379 if (f != MDELETED && (mp->m_flag & MDELETED) != 0) {
380 printf("%d: Inappropriate message\n", mesg);
387 * Scan out the list of string arguments, shell style
391 getrawlist(line, argv, argc)
396 char c, *cp, *cp2, quotec;
399 size_t linebufsize = BUFSIZ;
401 if ((linebuf = malloc(linebufsize)) == NULL)
402 err(1, "Out of memory");
407 for (; *cp == ' ' || *cp == '\t'; cp++)
411 if (argn >= argc - 1) {
413 "Too many elements in the list; excess discarded.\n");
418 while ((c = *cp) != '\0') {
419 /* Allocate more space if necessary */
420 if (cp2 - linebuf == linebufsize - 1) {
421 linebufsize += BUFSIZ;
422 if ((linebuf = realloc(linebuf, linebufsize)) == NULL)
423 err(1, "Out of memory");
424 cp2 = linebuf + linebufsize - BUFSIZ - 1;
427 if (quotec != '\0') {
436 case '0': case '1': case '2': case '3':
437 case '4': case '5': case '6': case '7':
439 if (*cp >= '0' && *cp <= '7')
440 c = c * 8 + *cp++ - '0';
441 if (*cp >= '0' && *cp <= '7')
442 c = c * 8 + *cp++ - '0';
470 /* null doesn't show up anyway */
471 else if ((c >= 'A' && c <= '_') ||
472 (c >= 'a' && c <= 'z'))
480 } else if (c == '"' || c == '\'')
482 else if (c == ' ' || c == '\t')
488 argv[argn++] = savestr(linebuf);
496 * scan out a single lexical item and return its token number,
497 * updating the string pointer passed **p. Also, store the value
498 * of the number or string scanned in lexnumber or lexstring as
499 * appropriate. In any event, store the scanned `thing' in lexstring.
527 strcpy(lexstring, string_stack[regretp]);
528 lexnumber = numberstack[regretp];
529 return (regretstack[regretp--]);
536 * strip away leading white space.
539 while (c == ' ' || c == '\t')
543 * If no characters remain, we are at end of line,
553 * If the leading character is a digit, scan
554 * the number and convert it on the fly.
555 * Return TNUMBER when done.
558 if (isdigit((unsigned char)c)) {
560 while (isdigit((unsigned char)c)) {
561 lexnumber = lexnumber*10 + c - '0';
571 * Check for single character tokens; return such
575 for (lp = &singles[0]; lp->l_char != '\0'; lp++)
576 if (c == lp->l_char) {
580 return (lp->l_token);
584 * We've got a string! Copy all the characters
585 * of the string into lexstring, until we see
586 * a null, space, or tab.
587 * If the lead character is a " or ', save it
588 * and scan until you get another.
592 if (c == '\'' || c == '"') {
601 if (quotec == 0 && (c == ' ' || c == '\t'))
603 if (cp2 - lexstring < STRINGLEN-1)
607 if (quotec && c == '\0') {
608 fprintf(stderr, "Missing %c\n", quotec);
617 * Unscan the named token by pushing it onto the regret stack.
623 if (++regretp >= REGDEP)
624 errx(1, "Too many regrets");
625 regretstack[regretp] = token;
626 lexstring[STRINGLEN-1] = '\0';
627 string_stack[regretp] = savestr(lexstring);
628 numberstack[regretp] = lexnumber;
632 * Reset all the scanner global variables.
641 * Find the first message whose flags & m == f and return
642 * its message number.
654 for (mp = dot; mp < &message[msgCount]; mp++)
655 if ((mp->m_flag & m) == f)
656 return (mp - message + 1);
657 for (mp = dot-1; mp >= &message[0]; mp--)
658 if ((mp->m_flag & m) == f)
659 return (mp - message + 1);
664 * See if the passed name sent the passed message number. Return true
668 matchsender(str, mesg)
674 /* null string matches nothing instead of everything */
678 cp = nameof(&message[mesg - 1], 0);
679 return (strcasestr(cp, str) != NULL);
683 * See if the passed name received the passed message number. Return true
687 static char *to_fields[] = { "to", "cc", "bcc", NULL };
699 /* null string matches nothing instead of everything */
703 mp = &message[mesg - 1];
705 for (to = to_fields; *to != NULL; to++) {
706 cp = hfield(*to, mp);
707 if (cp != NULL && strcasestr(cp, str) != NULL)
714 * See if the given substring is contained within the specified field. If
715 * 'searchheaders' is set, then the form '/x:y' will be accepted and matches
716 * any message with the substring 'y' in field 'x'. If 'x' is omitted or
717 * 'searchheaders' is not set, then the search matches any messages
718 * with the substring 'y' in the 'Subject'. The search is case insensitive.
720 * The form '/to:y' is a special case, and will match all messages
721 * containing the substring 'y' in the 'To', 'Cc', or 'Bcc' header
722 * fields. The search for 'to' is case sensitive, so that '/To:y' can
723 * be used to limit the search to just the 'To' field.
726 static char lastscan[STRINGLEN];
728 matchfield(str, mesg)
739 strlcpy(lastscan, str, sizeof(lastscan));
740 mp = &message[mesg-1];
743 * Now look, ignoring case, for the word in the string.
746 if (value("searchheaders") && (cp = strchr(str, ':')) != NULL) {
747 /* Check for special case "/to:" */
748 if (strncmp(str, "to:", 3) == 0)
749 return (matchto(cp, mesg));
751 cp2 = hfield(*str != '\0' ? str : "subject", mp);
756 cp = hfield("subject", mp);
761 return (strcasestr(cp, str) != NULL);
765 * Mark the named message by setting its mark bit.
774 if (i < 1 || i > msgCount)
775 errx(1, "Bad message number to mark");
776 message[i-1].m_flag |= MMARK;
780 * Unmark the named message.
789 if (i < 1 || i > msgCount)
790 errx(1, "Bad message number to unmark");
791 message[i-1].m_flag &= ~MMARK;
795 * Return the message number corresponding to the passed meta character.
808 * First 'good' message left.
810 for (mp = &message[0]; mp < &message[msgCount]; mp++)
811 if ((mp->m_flag & MDELETED) == f)
812 return (mp - &message[0] + 1);
813 printf("No applicable messages\n");
818 * Last 'good message left.
820 for (mp = &message[msgCount-1]; mp >= &message[0]; mp--)
821 if ((mp->m_flag & MDELETED) == f)
822 return (mp - &message[0] + 1);
823 printf("No applicable messages\n");
830 m = dot - &message[0] + 1;
831 if ((dot->m_flag & MDELETED) != f) {
832 printf("%d: Inappropriate message\n", m);
838 printf("Unknown metachar (%c)\n", c);