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
33 * @(#)list.c 8.4 (Berkeley) 5/1/95
34 * $FreeBSD: src/usr.bin/mail/list.c,v 1.2.12.3 2003/01/06 05:46:03 mikeh Exp $
35 * $DragonFly: src/usr.bin/mail/list.c,v 1.2 2003/06/17 04:29:28 dillon Exp $
43 * Mail -- a mail program
45 * Message list handling.
49 * Convert the user string of message numbers and
50 * store the numbers into vector.
52 * Returns the count of messages picked up or -1 on error.
55 getmsglist(buf, vector, flags)
66 if (markall(buf, flags) < 0)
69 for (mp = &message[0]; mp < &message[msgCount]; mp++)
70 if (mp->m_flag & MMARK)
71 *ip++ = mp - &message[0] + 1;
77 * Mark all messages that the user wanted from the command
78 * line in the message structure. Return 0 on success, -1
83 * Bit values for colon modifiers.
86 #define CMNEW 01 /* New messages */
87 #define CMOLD 02 /* Old messages */
88 #define CMUNREAD 04 /* Unread messages */
89 #define CMDELETED 010 /* Deleted messages */
90 #define CMREAD 020 /* Read messages */
93 * The following table describes the letters which can follow
94 * the colon and gives the corresponding modifier bit.
98 char co_char; /* What to find past : */
99 int co_bit; /* Associated modifier bit */
100 int co_mask; /* m_status bits to mask */
101 int co_equal; /* ... must equal this */
103 { 'n', CMNEW, MNEW, MNEW },
104 { 'o', CMOLD, MNEW, 0 },
105 { 'u', CMUNREAD, MREAD, 0 },
106 { 'd', CMDELETED, MDELETED, MDELETED},
107 { 'r', CMREAD, MREAD, MREAD },
111 static int lastcolmod;
121 char *namelist[NMLSIZE], *bufp;
122 int tok, beg, mc, star, other, valdot, colmod, colresult;
124 valdot = dot - &message[0] + 1;
126 for (i = 1; i <= msgCount; i++)
136 while (tok != TEOL) {
141 printf("No numbers mixed with *\n");
147 if (check(lexnumber, f))
149 for (i = beg; i <= lexnumber; i++)
150 if (f == MDELETED || (message[i - 1].m_flag & MDELETED) == 0)
168 printf("Non-numeric second argument\n");
175 printf("Referencing beyond EOF\n");
178 } while ((message[i - 1].m_flag & MDELETED) != f);
188 printf("Referencing before 1\n");
191 } while ((message[i - 1].m_flag & MDELETED) != f);
198 printf("Non-numeric second argument\n");
202 if (lexstring[0] == ':') {
203 colresult = evalcol(lexstring[1]);
204 if (colresult == 0) {
205 printf("Unknown colon modifier \"%s\"\n",
212 *np++ = savestr(lexstring);
218 lexnumber = metamess(lexstring[0], f);
225 printf("Can't mix \"*\" with anything\n");
240 for (i = 0; i < msgCount; i++)
241 if ((message[i].m_flag & MDELETED) == f) {
246 printf("No applicable messages.\n");
253 * If no numbers were given, mark all of the messages,
254 * so that we can unmark any whose sender was not selected
255 * if any user names were given.
258 if ((np > namelist || colmod != 0) && mc == 0)
259 for (i = 1; i <= msgCount; i++)
260 if ((message[i-1].m_flag & MDELETED) == f)
264 * If any names were given, go through and eliminate any
265 * messages whose senders were not requested.
269 for (i = 1; i <= msgCount; i++) {
270 for (mc = 0, np = &namelist[0]; *np != NULL; np++)
272 if (matchfield(*np, i)) {
278 if (matchsender(*np, i)) {
288 * Make sure we got some decent messages.
292 for (i = 1; i <= msgCount; i++)
293 if (message[i-1].m_flag & MMARK) {
298 printf("No applicable messages from {%s",
300 for (np = &namelist[1]; *np != NULL; np++)
308 * If any colon modifiers were given, go through and
309 * unmark any messages which do not satisfy the modifiers.
313 for (i = 1; i <= msgCount; i++) {
316 mp = &message[i - 1];
317 for (colp = &coltab[0]; colp->co_char != '\0'; colp++)
318 if (colp->co_bit & colmod)
319 if ((mp->m_flag & colp->co_mask)
324 for (mp = &message[0]; mp < &message[msgCount]; mp++)
325 if (mp->m_flag & MMARK)
327 if (mp >= &message[msgCount]) {
330 printf("No messages satisfy");
331 for (colp = &coltab[0]; colp->co_char != '\0'; colp++)
332 if (colp->co_bit & colmod)
333 printf(" :%c", colp->co_char);
342 * Turn the character after a colon modifier into a bit
353 for (colp = &coltab[0]; colp->co_char != '\0'; colp++)
354 if (colp->co_char == col)
355 return (colp->co_bit);
360 * Check the passed message number for legality and proper flags.
361 * If f is MDELETED, then either kind will do. Otherwise, the message
362 * has to be undeleted.
370 if (mesg < 1 || mesg > msgCount) {
371 printf("%d: Invalid message number\n", mesg);
374 mp = &message[mesg-1];
375 if (f != MDELETED && (mp->m_flag & MDELETED) != 0) {
376 printf("%d: Inappropriate message\n", mesg);
383 * Scan out the list of string arguments, shell style
387 getrawlist(line, argv, argc)
392 char c, *cp, *cp2, quotec;
395 size_t linebufsize = BUFSIZ;
397 if ((linebuf = malloc(linebufsize)) == NULL)
398 err(1, "Out of memory");
403 for (; *cp == ' ' || *cp == '\t'; cp++)
407 if (argn >= argc - 1) {
409 "Too many elements in the list; excess discarded.\n");
414 while ((c = *cp) != '\0') {
415 /* Allocate more space if necessary */
416 if (cp2 - linebuf == linebufsize - 1) {
417 linebufsize += BUFSIZ;
418 if ((linebuf = realloc(linebuf, linebufsize)) == NULL)
419 err(1, "Out of memory");
420 cp2 = linebuf + linebufsize - BUFSIZ - 1;
423 if (quotec != '\0') {
432 case '0': case '1': case '2': case '3':
433 case '4': case '5': case '6': case '7':
435 if (*cp >= '0' && *cp <= '7')
436 c = c * 8 + *cp++ - '0';
437 if (*cp >= '0' && *cp <= '7')
438 c = c * 8 + *cp++ - '0';
466 /* null doesn't show up anyway */
467 else if ((c >= 'A' && c <= '_') ||
468 (c >= 'a' && c <= 'z'))
476 } else if (c == '"' || c == '\'')
478 else if (c == ' ' || c == '\t')
484 argv[argn++] = savestr(linebuf);
492 * scan out a single lexical item and return its token number,
493 * updating the string pointer passed **p. Also, store the value
494 * of the number or string scanned in lexnumber or lexstring as
495 * appropriate. In any event, store the scanned `thing' in lexstring.
523 strcpy(lexstring, string_stack[regretp]);
524 lexnumber = numberstack[regretp];
525 return (regretstack[regretp--]);
532 * strip away leading white space.
535 while (c == ' ' || c == '\t')
539 * If no characters remain, we are at end of line,
549 * If the leading character is a digit, scan
550 * the number and convert it on the fly.
551 * Return TNUMBER when done.
554 if (isdigit((unsigned char)c)) {
556 while (isdigit((unsigned char)c)) {
557 lexnumber = lexnumber*10 + c - '0';
567 * Check for single character tokens; return such
571 for (lp = &singles[0]; lp->l_char != '\0'; lp++)
572 if (c == lp->l_char) {
576 return (lp->l_token);
580 * We've got a string! Copy all the characters
581 * of the string into lexstring, until we see
582 * a null, space, or tab.
583 * If the lead character is a " or ', save it
584 * and scan until you get another.
588 if (c == '\'' || c == '"') {
597 if (quotec == 0 && (c == ' ' || c == '\t'))
599 if (cp2 - lexstring < STRINGLEN-1)
603 if (quotec && c == '\0') {
604 fprintf(stderr, "Missing %c\n", quotec);
613 * Unscan the named token by pushing it onto the regret stack.
619 if (++regretp >= REGDEP)
620 errx(1, "Too many regrets");
621 regretstack[regretp] = token;
622 lexstring[STRINGLEN-1] = '\0';
623 string_stack[regretp] = savestr(lexstring);
624 numberstack[regretp] = lexnumber;
628 * Reset all the scanner global variables.
637 * Find the first message whose flags & m == f and return
638 * its message number.
650 for (mp = dot; mp < &message[msgCount]; mp++)
651 if ((mp->m_flag & m) == f)
652 return (mp - message + 1);
653 for (mp = dot-1; mp >= &message[0]; mp--)
654 if ((mp->m_flag & m) == f)
655 return (mp - message + 1);
660 * See if the passed name sent the passed message number. Return true
664 matchsender(str, mesg)
670 /* null string matches nothing instead of everything */
674 cp = nameof(&message[mesg - 1], 0);
675 return (strcasestr(cp, str) != NULL);
679 * See if the passed name received the passed message number. Return true
683 static char *to_fields[] = { "to", "cc", "bcc", NULL };
695 /* null string matches nothing instead of everything */
699 mp = &message[mesg - 1];
701 for (to = to_fields; *to != NULL; to++) {
702 cp = hfield(*to, mp);
703 if (cp != NULL && strcasestr(cp, str) != NULL)
710 * See if the given substring is contained within the specified field. If
711 * 'searchheaders' is set, then the form '/x:y' will be accepted and matches
712 * any message with the substring 'y' in field 'x'. If 'x' is omitted or
713 * 'searchheaders' is not set, then the search matches any messages
714 * with the substring 'y' in the 'Subject'. The search is case insensitive.
716 * The form '/to:y' is a special case, and will match all messages
717 * containing the substring 'y' in the 'To', 'Cc', or 'Bcc' header
718 * fields. The search for 'to' is case sensitive, so that '/To:y' can
719 * be used to limit the search to just the 'To' field.
722 char lastscan[STRINGLEN];
724 matchfield(str, mesg)
735 strlcpy(lastscan, str, sizeof(lastscan));
736 mp = &message[mesg-1];
739 * Now look, ignoring case, for the word in the string.
742 if (value("searchheaders") && (cp = strchr(str, ':')) != NULL) {
743 /* Check for special case "/to:" */
744 if (strncmp(str, "to:", 3) == 0)
745 return (matchto(cp, mesg));
747 cp2 = hfield(*str != '\0' ? str : "subject", mp);
752 cp = hfield("subject", mp);
757 return (strcasestr(cp, str) != NULL);
761 * Mark the named message by setting its mark bit.
770 if (i < 1 || i > msgCount)
771 errx(1, "Bad message number to mark");
772 message[i-1].m_flag |= MMARK;
776 * Unmark the named message.
785 if (i < 1 || i > msgCount)
786 errx(1, "Bad message number to unmark");
787 message[i-1].m_flag &= ~MMARK;
791 * Return the message number corresponding to the passed meta character.
804 * First 'good' message left.
806 for (mp = &message[0]; mp < &message[msgCount]; mp++)
807 if ((mp->m_flag & MDELETED) == f)
808 return (mp - &message[0] + 1);
809 printf("No applicable messages\n");
814 * Last 'good message left.
816 for (mp = &message[msgCount-1]; mp >= &message[0]; mp--)
817 if ((mp->m_flag & MDELETED) == f)
818 return (mp - &message[0] + 1);
819 printf("No applicable messages\n");
826 m = dot - &message[0] + 1;
827 if ((dot->m_flag & MDELETED) != f) {
828 printf("%d: Inappropriate message\n", m);
834 printf("Unknown metachar (%c)\n", c);