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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)list.c 8.4 (Berkeley) 5/1/95
30 * $FreeBSD: src/usr.bin/mail/list.c,v 1.2.12.3 2003/01/06 05:46:03 mikeh Exp $
31 * $DragonFly: src/usr.bin/mail/list.c,v 1.5 2004/09/08 03:01:11 joerg Exp $
38 static void mark(int);
39 static void unmark(int);
42 * Mail -- a mail program
44 * Message list handling.
48 * Convert the user string of message numbers and
49 * store the numbers into vector.
51 * Returns the count of messages picked up or -1 on error.
54 getmsglist(char *buf, int *vector, int flags)
63 if (markall(buf, flags) < 0)
66 for (mp = &message[0]; mp < &message[msgCount]; mp++)
67 if (mp->m_flag & MMARK)
68 *ip++ = mp - &message[0] + 1;
74 * Mark all messages that the user wanted from the command
75 * line in the message structure. Return 0 on success, -1
80 * Bit values for colon modifiers.
83 #define CMNEW 01 /* New messages */
84 #define CMOLD 02 /* Old messages */
85 #define CMUNREAD 04 /* Unread messages */
86 #define CMDELETED 010 /* Deleted messages */
87 #define CMREAD 020 /* Read messages */
90 * The following table describes the letters which can follow
91 * the colon and gives the corresponding modifier bit.
95 char co_char; /* What to find past : */
96 int co_bit; /* Associated modifier bit */
97 int co_mask; /* m_status bits to mask */
98 int co_equal; /* ... must equal this */
100 { 'n', CMNEW, MNEW, MNEW },
101 { 'o', CMOLD, MNEW, 0 },
102 { 'u', CMUNREAD, MREAD, 0 },
103 { 'd', CMDELETED, MDELETED, MDELETED},
104 { 'r', CMREAD, MREAD, MREAD },
108 static int lastcolmod;
111 markall(char *buf, int f)
116 char *namelist[NMLSIZE], *bufp;
117 int tok, beg, mc, star, other, valdot, colmod, colresult;
119 valdot = dot - &message[0] + 1;
121 for (i = 1; i <= msgCount; i++)
131 while (tok != TEOL) {
136 printf("No numbers mixed with *\n");
142 if (check(lexnumber, f))
144 for (i = beg; i <= lexnumber; i++)
145 if (f == MDELETED || (message[i - 1].m_flag & MDELETED) == 0)
163 printf("Non-numeric second argument\n");
170 printf("Referencing beyond EOF\n");
173 } while ((message[i - 1].m_flag & MDELETED) != f);
183 printf("Referencing before 1\n");
186 } while ((message[i - 1].m_flag & MDELETED) != f);
193 printf("Non-numeric second argument\n");
197 if (lexstring[0] == ':') {
198 colresult = evalcol(lexstring[1]);
199 if (colresult == 0) {
200 printf("Unknown colon modifier \"%s\"\n",
207 *np++ = savestr(lexstring);
213 lexnumber = metamess(lexstring[0], f);
220 printf("Can't mix \"*\" with anything\n");
235 for (i = 0; i < msgCount; i++)
236 if ((message[i].m_flag & MDELETED) == f) {
241 printf("No applicable messages.\n");
248 * If no numbers were given, mark all of the messages,
249 * so that we can unmark any whose sender was not selected
250 * if any user names were given.
253 if ((np > namelist || colmod != 0) && mc == 0)
254 for (i = 1; i <= msgCount; i++)
255 if ((message[i-1].m_flag & MDELETED) == f)
259 * If any names were given, go through and eliminate any
260 * messages whose senders were not requested.
264 for (i = 1; i <= msgCount; i++) {
265 for (mc = 0, np = &namelist[0]; *np != NULL; np++)
267 if (matchfield(*np, i)) {
273 if (matchsender(*np, i)) {
283 * Make sure we got some decent messages.
287 for (i = 1; i <= msgCount; i++)
288 if (message[i-1].m_flag & MMARK) {
293 printf("No applicable messages from {%s",
295 for (np = &namelist[1]; *np != NULL; np++)
303 * If any colon modifiers were given, go through and
304 * unmark any messages which do not satisfy the modifiers.
308 for (i = 1; i <= msgCount; i++) {
311 mp = &message[i - 1];
312 for (colp = &coltab[0]; colp->co_char != '\0'; colp++)
313 if (colp->co_bit & colmod)
314 if ((mp->m_flag & colp->co_mask)
319 for (mp = &message[0]; mp < &message[msgCount]; mp++)
320 if (mp->m_flag & MMARK)
322 if (mp >= &message[msgCount]) {
325 printf("No messages satisfy");
326 for (colp = &coltab[0]; colp->co_char != '\0'; colp++)
327 if (colp->co_bit & colmod)
328 printf(" :%c", colp->co_char);
337 * Turn the character after a colon modifier into a bit
347 for (colp = &coltab[0]; colp->co_char != '\0'; colp++)
348 if (colp->co_char == col)
349 return (colp->co_bit);
354 * Check the passed message number for legality and proper flags.
355 * If f is MDELETED, then either kind will do. Otherwise, the message
356 * has to be undeleted.
359 check(int mesg, int f)
363 if (mesg < 1 || mesg > msgCount) {
364 printf("%d: Invalid message number\n", mesg);
367 mp = &message[mesg-1];
368 if (f != MDELETED && (mp->m_flag & MDELETED) != 0) {
369 printf("%d: Inappropriate message\n", mesg);
376 * Scan out the list of string arguments, shell style
380 getrawlist(char *line, char **argv, int argc)
382 char c, *cp, *cp2, quotec;
385 size_t linebufsize = BUFSIZ;
387 if ((linebuf = malloc(linebufsize)) == NULL)
388 err(1, "Out of memory");
393 for (; *cp == ' ' || *cp == '\t'; cp++)
397 if (argn >= argc - 1) {
399 "Too many elements in the list; excess discarded.\n");
404 while ((c = *cp) != '\0') {
405 /* Allocate more space if necessary */
406 if (cp2 - linebuf == linebufsize - 1) {
407 linebufsize += BUFSIZ;
408 if ((linebuf = realloc(linebuf, linebufsize)) == NULL)
409 err(1, "Out of memory");
410 cp2 = linebuf + linebufsize - BUFSIZ - 1;
413 if (quotec != '\0') {
422 case '0': case '1': case '2': case '3':
423 case '4': case '5': case '6': case '7':
425 if (*cp >= '0' && *cp <= '7')
426 c = c * 8 + *cp++ - '0';
427 if (*cp >= '0' && *cp <= '7')
428 c = c * 8 + *cp++ - '0';
456 /* null doesn't show up anyway */
457 else if ((c >= 'A' && c <= '_') ||
458 (c >= 'a' && c <= 'z'))
466 } else if (c == '"' || c == '\'')
468 else if (c == ' ' || c == '\t')
474 argv[argn++] = savestr(linebuf);
482 * scan out a single lexical item and return its token number,
483 * updating the string pointer passed **p. Also, store the value
484 * of the number or string scanned in lexnumber or lexstring as
485 * appropriate. In any event, store the scanned `thing' in lexstring.
512 strcpy(lexstring, string_stack[regretp]);
513 lexnumber = numberstack[regretp];
514 return (regretstack[regretp--]);
521 * strip away leading white space.
524 while (c == ' ' || c == '\t')
528 * If no characters remain, we are at end of line,
538 * If the leading character is a digit, scan
539 * the number and convert it on the fly.
540 * Return TNUMBER when done.
543 if (isdigit((unsigned char)c)) {
545 while (isdigit((unsigned char)c)) {
546 lexnumber = lexnumber*10 + c - '0';
556 * Check for single character tokens; return such
560 for (lp = &singles[0]; lp->l_char != '\0'; lp++)
561 if (c == lp->l_char) {
565 return (lp->l_token);
569 * We've got a string! Copy all the characters
570 * of the string into lexstring, until we see
571 * a null, space, or tab.
572 * If the lead character is a " or ', save it
573 * and scan until you get another.
577 if (c == '\'' || c == '"') {
586 if (quotec == 0 && (c == ' ' || c == '\t'))
588 if (cp2 - lexstring < STRINGLEN-1)
592 if (quotec && c == '\0') {
593 fprintf(stderr, "Missing %c\n", quotec);
602 * Unscan the named token by pushing it onto the regret stack.
607 if (++regretp >= REGDEP)
608 errx(1, "Too many regrets");
609 regretstack[regretp] = token;
610 lexstring[STRINGLEN-1] = '\0';
611 string_stack[regretp] = savestr(lexstring);
612 numberstack[regretp] = lexnumber;
616 * Reset all the scanner global variables.
625 * Find the first message whose flags & m == f and return
626 * its message number.
637 for (mp = dot; mp < &message[msgCount]; mp++)
638 if ((mp->m_flag & m) == f)
639 return (mp - message + 1);
640 for (mp = dot-1; mp >= &message[0]; mp--)
641 if ((mp->m_flag & m) == f)
642 return (mp - message + 1);
647 * See if the passed name sent the passed message number. Return true
651 matchsender(char *str, int mesg)
655 /* null string matches nothing instead of everything */
659 cp = nameof(&message[mesg - 1], 0);
660 return (strcasestr(cp, str) != NULL);
664 * See if the passed name received the passed message number. Return true
668 static char *to_fields[] = { "to", "cc", "bcc", NULL };
671 matchto(char *str, int mesg)
678 /* null string matches nothing instead of everything */
682 mp = &message[mesg - 1];
684 for (to = to_fields; *to != NULL; to++) {
685 cp = hfield(*to, mp);
686 if (cp != NULL && strcasestr(cp, str) != NULL)
693 * See if the given substring is contained within the specified field. If
694 * 'searchheaders' is set, then the form '/x:y' will be accepted and matches
695 * any message with the substring 'y' in field 'x'. If 'x' is omitted or
696 * 'searchheaders' is not set, then the search matches any messages
697 * with the substring 'y' in the 'Subject'. The search is case insensitive.
699 * The form '/to:y' is a special case, and will match all messages
700 * containing the substring 'y' in the 'To', 'Cc', or 'Bcc' header
701 * fields. The search for 'to' is case sensitive, so that '/To:y' can
702 * be used to limit the search to just the 'To' field.
705 char lastscan[STRINGLEN];
707 matchfield(char *str, int mesg)
716 strlcpy(lastscan, str, sizeof(lastscan));
717 mp = &message[mesg-1];
720 * Now look, ignoring case, for the word in the string.
723 if (value("searchheaders") && (cp = strchr(str, ':')) != NULL) {
724 /* Check for special case "/to:" */
725 if (strncmp(str, "to:", 3) == 0)
726 return (matchto(cp, mesg));
728 cp2 = hfield(*str != '\0' ? str : "subject", mp);
733 cp = hfield("subject", mp);
738 return (strcasestr(cp, str) != NULL);
742 * Mark the named message by setting its mark bit.
750 if (i < 1 || i > msgCount)
751 errx(1, "Bad message number to mark");
752 message[i-1].m_flag |= MMARK;
756 * Unmark the named message.
764 if (i < 1 || i > msgCount)
765 errx(1, "Bad message number to unmark");
766 message[i-1].m_flag &= ~MMARK;
770 * Return the message number corresponding to the passed meta character.
773 metamess(int meta, int f)
782 * First 'good' message left.
784 for (mp = &message[0]; mp < &message[msgCount]; mp++)
785 if ((mp->m_flag & MDELETED) == f)
786 return (mp - &message[0] + 1);
787 printf("No applicable messages\n");
792 * Last 'good message left.
794 for (mp = &message[msgCount-1]; mp >= &message[0]; mp--)
795 if ((mp->m_flag & MDELETED) == f)
796 return (mp - &message[0] + 1);
797 printf("No applicable messages\n");
804 m = dot - &message[0] + 1;
805 if ((dot->m_flag & MDELETED) != f) {
806 printf("%d: Inappropriate message\n", m);
812 printf("Unknown metachar (%c)\n", c);