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.5 2004/09/08 03:01:11 joerg Exp $
42 static void mark(int);
43 static void unmark(int);
46 * Mail -- a mail program
48 * Message list handling.
52 * Convert the user string of message numbers and
53 * store the numbers into vector.
55 * Returns the count of messages picked up or -1 on error.
58 getmsglist(char *buf, int *vector, int flags)
67 if (markall(buf, flags) < 0)
70 for (mp = &message[0]; mp < &message[msgCount]; mp++)
71 if (mp->m_flag & MMARK)
72 *ip++ = mp - &message[0] + 1;
78 * Mark all messages that the user wanted from the command
79 * line in the message structure. Return 0 on success, -1
84 * Bit values for colon modifiers.
87 #define CMNEW 01 /* New messages */
88 #define CMOLD 02 /* Old messages */
89 #define CMUNREAD 04 /* Unread messages */
90 #define CMDELETED 010 /* Deleted messages */
91 #define CMREAD 020 /* Read messages */
94 * The following table describes the letters which can follow
95 * the colon and gives the corresponding modifier bit.
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 */
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 },
112 static int lastcolmod;
115 markall(char *buf, int f)
120 char *namelist[NMLSIZE], *bufp;
121 int tok, beg, mc, star, other, valdot, colmod, colresult;
123 valdot = dot - &message[0] + 1;
125 for (i = 1; i <= msgCount; i++)
135 while (tok != TEOL) {
140 printf("No numbers mixed with *\n");
146 if (check(lexnumber, f))
148 for (i = beg; i <= lexnumber; i++)
149 if (f == MDELETED || (message[i - 1].m_flag & MDELETED) == 0)
167 printf("Non-numeric second argument\n");
174 printf("Referencing beyond EOF\n");
177 } while ((message[i - 1].m_flag & MDELETED) != f);
187 printf("Referencing before 1\n");
190 } while ((message[i - 1].m_flag & MDELETED) != f);
197 printf("Non-numeric second argument\n");
201 if (lexstring[0] == ':') {
202 colresult = evalcol(lexstring[1]);
203 if (colresult == 0) {
204 printf("Unknown colon modifier \"%s\"\n",
211 *np++ = savestr(lexstring);
217 lexnumber = metamess(lexstring[0], f);
224 printf("Can't mix \"*\" with anything\n");
239 for (i = 0; i < msgCount; i++)
240 if ((message[i].m_flag & MDELETED) == f) {
245 printf("No applicable messages.\n");
252 * If no numbers were given, mark all of the messages,
253 * so that we can unmark any whose sender was not selected
254 * if any user names were given.
257 if ((np > namelist || colmod != 0) && mc == 0)
258 for (i = 1; i <= msgCount; i++)
259 if ((message[i-1].m_flag & MDELETED) == f)
263 * If any names were given, go through and eliminate any
264 * messages whose senders were not requested.
268 for (i = 1; i <= msgCount; i++) {
269 for (mc = 0, np = &namelist[0]; *np != NULL; np++)
271 if (matchfield(*np, i)) {
277 if (matchsender(*np, i)) {
287 * Make sure we got some decent messages.
291 for (i = 1; i <= msgCount; i++)
292 if (message[i-1].m_flag & MMARK) {
297 printf("No applicable messages from {%s",
299 for (np = &namelist[1]; *np != NULL; np++)
307 * If any colon modifiers were given, go through and
308 * unmark any messages which do not satisfy the modifiers.
312 for (i = 1; i <= msgCount; i++) {
315 mp = &message[i - 1];
316 for (colp = &coltab[0]; colp->co_char != '\0'; colp++)
317 if (colp->co_bit & colmod)
318 if ((mp->m_flag & colp->co_mask)
323 for (mp = &message[0]; mp < &message[msgCount]; mp++)
324 if (mp->m_flag & MMARK)
326 if (mp >= &message[msgCount]) {
329 printf("No messages satisfy");
330 for (colp = &coltab[0]; colp->co_char != '\0'; colp++)
331 if (colp->co_bit & colmod)
332 printf(" :%c", colp->co_char);
341 * Turn the character after a colon modifier into a bit
351 for (colp = &coltab[0]; colp->co_char != '\0'; colp++)
352 if (colp->co_char == col)
353 return (colp->co_bit);
358 * Check the passed message number for legality and proper flags.
359 * If f is MDELETED, then either kind will do. Otherwise, the message
360 * has to be undeleted.
363 check(int mesg, int f)
367 if (mesg < 1 || mesg > msgCount) {
368 printf("%d: Invalid message number\n", mesg);
371 mp = &message[mesg-1];
372 if (f != MDELETED && (mp->m_flag & MDELETED) != 0) {
373 printf("%d: Inappropriate message\n", mesg);
380 * Scan out the list of string arguments, shell style
384 getrawlist(char *line, char **argv, int argc)
386 char c, *cp, *cp2, quotec;
389 size_t linebufsize = BUFSIZ;
391 if ((linebuf = malloc(linebufsize)) == NULL)
392 err(1, "Out of memory");
397 for (; *cp == ' ' || *cp == '\t'; cp++)
401 if (argn >= argc - 1) {
403 "Too many elements in the list; excess discarded.\n");
408 while ((c = *cp) != '\0') {
409 /* Allocate more space if necessary */
410 if (cp2 - linebuf == linebufsize - 1) {
411 linebufsize += BUFSIZ;
412 if ((linebuf = realloc(linebuf, linebufsize)) == NULL)
413 err(1, "Out of memory");
414 cp2 = linebuf + linebufsize - BUFSIZ - 1;
417 if (quotec != '\0') {
426 case '0': case '1': case '2': case '3':
427 case '4': case '5': case '6': case '7':
429 if (*cp >= '0' && *cp <= '7')
430 c = c * 8 + *cp++ - '0';
431 if (*cp >= '0' && *cp <= '7')
432 c = c * 8 + *cp++ - '0';
460 /* null doesn't show up anyway */
461 else if ((c >= 'A' && c <= '_') ||
462 (c >= 'a' && c <= 'z'))
470 } else if (c == '"' || c == '\'')
472 else if (c == ' ' || c == '\t')
478 argv[argn++] = savestr(linebuf);
486 * scan out a single lexical item and return its token number,
487 * updating the string pointer passed **p. Also, store the value
488 * of the number or string scanned in lexnumber or lexstring as
489 * appropriate. In any event, store the scanned `thing' in lexstring.
516 strcpy(lexstring, string_stack[regretp]);
517 lexnumber = numberstack[regretp];
518 return (regretstack[regretp--]);
525 * strip away leading white space.
528 while (c == ' ' || c == '\t')
532 * If no characters remain, we are at end of line,
542 * If the leading character is a digit, scan
543 * the number and convert it on the fly.
544 * Return TNUMBER when done.
547 if (isdigit((unsigned char)c)) {
549 while (isdigit((unsigned char)c)) {
550 lexnumber = lexnumber*10 + c - '0';
560 * Check for single character tokens; return such
564 for (lp = &singles[0]; lp->l_char != '\0'; lp++)
565 if (c == lp->l_char) {
569 return (lp->l_token);
573 * We've got a string! Copy all the characters
574 * of the string into lexstring, until we see
575 * a null, space, or tab.
576 * If the lead character is a " or ', save it
577 * and scan until you get another.
581 if (c == '\'' || c == '"') {
590 if (quotec == 0 && (c == ' ' || c == '\t'))
592 if (cp2 - lexstring < STRINGLEN-1)
596 if (quotec && c == '\0') {
597 fprintf(stderr, "Missing %c\n", quotec);
606 * Unscan the named token by pushing it onto the regret stack.
611 if (++regretp >= REGDEP)
612 errx(1, "Too many regrets");
613 regretstack[regretp] = token;
614 lexstring[STRINGLEN-1] = '\0';
615 string_stack[regretp] = savestr(lexstring);
616 numberstack[regretp] = lexnumber;
620 * Reset all the scanner global variables.
629 * Find the first message whose flags & m == f and return
630 * its message number.
641 for (mp = dot; mp < &message[msgCount]; mp++)
642 if ((mp->m_flag & m) == f)
643 return (mp - message + 1);
644 for (mp = dot-1; mp >= &message[0]; mp--)
645 if ((mp->m_flag & m) == f)
646 return (mp - message + 1);
651 * See if the passed name sent the passed message number. Return true
655 matchsender(char *str, int mesg)
659 /* null string matches nothing instead of everything */
663 cp = nameof(&message[mesg - 1], 0);
664 return (strcasestr(cp, str) != NULL);
668 * See if the passed name received the passed message number. Return true
672 static char *to_fields[] = { "to", "cc", "bcc", NULL };
675 matchto(char *str, int mesg)
682 /* null string matches nothing instead of everything */
686 mp = &message[mesg - 1];
688 for (to = to_fields; *to != NULL; to++) {
689 cp = hfield(*to, mp);
690 if (cp != NULL && strcasestr(cp, str) != NULL)
697 * See if the given substring is contained within the specified field. If
698 * 'searchheaders' is set, then the form '/x:y' will be accepted and matches
699 * any message with the substring 'y' in field 'x'. If 'x' is omitted or
700 * 'searchheaders' is not set, then the search matches any messages
701 * with the substring 'y' in the 'Subject'. The search is case insensitive.
703 * The form '/to:y' is a special case, and will match all messages
704 * containing the substring 'y' in the 'To', 'Cc', or 'Bcc' header
705 * fields. The search for 'to' is case sensitive, so that '/To:y' can
706 * be used to limit the search to just the 'To' field.
709 char lastscan[STRINGLEN];
711 matchfield(char *str, int mesg)
720 strlcpy(lastscan, str, sizeof(lastscan));
721 mp = &message[mesg-1];
724 * Now look, ignoring case, for the word in the string.
727 if (value("searchheaders") && (cp = strchr(str, ':')) != NULL) {
728 /* Check for special case "/to:" */
729 if (strncmp(str, "to:", 3) == 0)
730 return (matchto(cp, mesg));
732 cp2 = hfield(*str != '\0' ? str : "subject", mp);
737 cp = hfield("subject", mp);
742 return (strcasestr(cp, str) != NULL);
746 * Mark the named message by setting its mark bit.
754 if (i < 1 || i > msgCount)
755 errx(1, "Bad message number to mark");
756 message[i-1].m_flag |= MMARK;
760 * Unmark the named message.
768 if (i < 1 || i > msgCount)
769 errx(1, "Bad message number to unmark");
770 message[i-1].m_flag &= ~MMARK;
774 * Return the message number corresponding to the passed meta character.
777 metamess(int meta, int f)
786 * First 'good' message left.
788 for (mp = &message[0]; mp < &message[msgCount]; mp++)
789 if ((mp->m_flag & MDELETED) == f)
790 return (mp - &message[0] + 1);
791 printf("No applicable messages\n");
796 * Last 'good message left.
798 for (mp = &message[msgCount-1]; mp >= &message[0]; mp--)
799 if ((mp->m_flag & MDELETED) == f)
800 return (mp - &message[0] + 1);
801 printf("No applicable messages\n");
808 m = dot - &message[0] + 1;
809 if ((dot->m_flag & MDELETED) != f) {
810 printf("%d: Inappropriate message\n", m);
816 printf("Unknown metachar (%c)\n", c);