2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static char sccsid[] = "@(#)list.c 8.4 (Berkeley) 5/1/95";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
45 * Mail -- a mail program
47 * Message list handling.
51 * Convert the user string of message numbers and
52 * store the numbers into vector.
54 * Returns the count of messages picked up or -1 on error.
57 getmsglist(char *buf, int *vector, int 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.
97 static struct coltab {
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;
114 markall(char buf[], int f)
119 char *namelist[NMLSIZE], *bufp;
120 int tok, beg, mc, star, other, valdot, colmod, colresult;
122 valdot = dot - &message[0] + 1;
124 for (i = 1; i <= msgCount; i++)
134 while (tok != TEOL) {
139 printf("No numbers mixed with *\n");
145 if (check(lexnumber, f))
147 for (i = beg; i <= lexnumber; i++)
148 if (f == MDELETED || (message[i - 1].m_flag & MDELETED) == 0)
166 printf("Non-numeric second argument\n");
173 printf("Referencing beyond EOF\n");
176 } while ((message[i - 1].m_flag & MDELETED) != f);
186 printf("Referencing before 1\n");
189 } while ((message[i - 1].m_flag & MDELETED) != f);
196 printf("Non-numeric second argument\n");
200 if (lexstring[0] == ':') {
201 colresult = evalcol(lexstring[1]);
202 if (colresult == 0) {
203 printf("Unknown colon modifier \"%s\"\n",
210 *np++ = savestr(lexstring);
216 lexnumber = metamess(lexstring[0], f);
223 printf("Can't mix \"*\" with anything\n");
238 for (i = 0; i < msgCount; i++)
239 if ((message[i].m_flag & MDELETED) == f) {
244 printf("No applicable messages.\n");
251 * If no numbers were given, mark all of the messages,
252 * so that we can unmark any whose sender was not selected
253 * if any user names were given.
256 if ((np > namelist || colmod != 0) && mc == 0)
257 for (i = 1; i <= msgCount; i++)
258 if ((message[i-1].m_flag & MDELETED) == f)
262 * If any names were given, go through and eliminate any
263 * messages whose senders were not requested.
267 for (i = 1; i <= msgCount; i++) {
268 for (mc = 0, np = &namelist[0]; *np != NULL; np++)
270 if (matchfield(*np, i)) {
276 if (matchsender(*np, i)) {
286 * Make sure we got some decent messages.
290 for (i = 1; i <= msgCount; i++)
291 if (message[i-1].m_flag & MMARK) {
296 printf("No applicable messages from {%s",
298 for (np = &namelist[1]; *np != NULL; np++)
306 * If any colon modifiers were given, go through and
307 * unmark any messages which do not satisfy the modifiers.
311 for (i = 1; i <= msgCount; i++) {
314 mp = &message[i - 1];
315 for (colp = &coltab[0]; colp->co_char != '\0'; colp++)
316 if (colp->co_bit & colmod)
317 if ((mp->m_flag & colp->co_mask)
322 for (mp = &message[0]; mp < &message[msgCount]; mp++)
323 if (mp->m_flag & MMARK)
325 if (mp >= &message[msgCount]) {
328 printf("No messages satisfy");
329 for (colp = &coltab[0]; colp->co_char != '\0'; colp++)
330 if (colp->co_bit & colmod)
331 printf(" :%c", colp->co_char);
340 * Turn the character after a colon modifier into a bit
350 for (colp = &coltab[0]; colp->co_char != '\0'; colp++)
351 if (colp->co_char == col)
352 return (colp->co_bit);
357 * Check the passed message number for legality and proper flags.
358 * If f is MDELETED, then either kind will do. Otherwise, the message
359 * has to be undeleted.
362 check(int mesg, int f)
366 if (mesg < 1 || mesg > msgCount) {
367 printf("%d: Invalid message number\n", mesg);
370 mp = &message[mesg-1];
371 if (f != MDELETED && (mp->m_flag & MDELETED) != 0) {
372 printf("%d: Inappropriate message\n", mesg);
379 * Scan out the list of string arguments, shell style
383 getrawlist(char line[], char **argv, int argc)
385 char c, *cp, *cp2, quotec;
388 size_t linebufsize = BUFSIZ;
390 if ((linebuf = malloc(linebufsize)) == NULL)
391 err(1, "Out of memory");
396 for (; *cp == ' ' || *cp == '\t'; cp++)
400 if (argn >= argc - 1) {
402 "Too many elements in the list; excess discarded.\n");
407 while ((c = *cp) != '\0') {
408 /* Allocate more space if necessary */
409 if (cp2 - linebuf == linebufsize - 1) {
410 linebufsize += BUFSIZ;
411 if ((linebuf = realloc(linebuf, linebufsize)) == NULL)
412 err(1, "Out of memory");
413 cp2 = linebuf + linebufsize - BUFSIZ - 1;
416 if (quotec != '\0') {
425 case '0': case '1': case '2': case '3':
426 case '4': case '5': case '6': case '7':
428 if (*cp >= '0' && *cp <= '7')
429 c = c * 8 + *cp++ - '0';
430 if (*cp >= '0' && *cp <= '7')
431 c = c * 8 + *cp++ - '0';
459 /* null doesn't show up anyway */
460 else if ((c >= 'A' && c <= '_') ||
461 (c >= 'a' && c <= 'z'))
469 } else if (c == '"' || c == '\'')
471 else if (c == ' ' || c == '\t')
477 argv[argn++] = savestr(linebuf);
485 * scan out a single lexical item and return its token number,
486 * updating the string pointer passed **p. Also, store the value
487 * of the number or string scanned in lexnumber or lexstring as
488 * appropriate. In any event, store the scanned `thing' in lexstring.
515 strcpy(lexstring, string_stack[regretp]);
516 lexnumber = numberstack[regretp];
517 return (regretstack[regretp--]);
524 * strip away leading white space.
527 while (c == ' ' || c == '\t')
531 * If no characters remain, we are at end of line,
541 * If the leading character is a digit, scan
542 * the number and convert it on the fly.
543 * Return TNUMBER when done.
546 if (isdigit((unsigned char)c)) {
548 while (isdigit((unsigned char)c)) {
549 lexnumber = lexnumber*10 + c - '0';
559 * Check for single character tokens; return such
563 for (lp = &singles[0]; lp->l_char != '\0'; lp++)
564 if (c == lp->l_char) {
568 return (lp->l_token);
572 * We've got a string! Copy all the characters
573 * of the string into lexstring, until we see
574 * a null, space, or tab.
575 * If the lead character is a " or ', save it
576 * and scan until you get another.
580 if (c == '\'' || c == '"') {
589 if (quotec == 0 && (c == ' ' || c == '\t'))
591 if (cp2 - lexstring < STRINGLEN-1)
595 if (quotec && c == '\0') {
596 fprintf(stderr, "Missing %c\n", quotec);
605 * Unscan the named token by pushing it onto the regret stack.
610 if (++regretp >= REGDEP)
611 errx(1, "Too many regrets");
612 regretstack[regretp] = token;
613 lexstring[STRINGLEN-1] = '\0';
614 string_stack[regretp] = savestr(lexstring);
615 numberstack[regretp] = lexnumber;
619 * Reset all the scanner global variables.
628 * Find the first message whose flags & m == f and return
629 * its message number.
640 for (mp = dot; mp < &message[msgCount]; mp++)
641 if ((mp->m_flag & m) == f)
642 return (mp - message + 1);
643 for (mp = dot-1; mp >= &message[0]; mp--)
644 if ((mp->m_flag & m) == f)
645 return (mp - message + 1);
650 * See if the passed name sent the passed message number. Return true
654 matchsender(char *str, int mesg)
658 /* null string matches nothing instead of everything */
662 cp = nameof(&message[mesg - 1], 0);
663 return (strcasestr(cp, str) != NULL);
667 * See if the passed name received the passed message number. Return true
671 static char *to_fields[] = { "to", "cc", "bcc", NULL };
674 matchto(char *str, int mesg)
681 /* null string matches nothing instead of everything */
685 mp = &message[mesg - 1];
687 for (to = to_fields; *to != NULL; to++) {
688 cp = hfield(*to, mp);
689 if (cp != NULL && strcasestr(cp, str) != NULL)
696 * See if the given substring is contained within the specified field. If
697 * 'searchheaders' is set, then the form '/x:y' will be accepted and matches
698 * any message with the substring 'y' in field 'x'. If 'x' is omitted or
699 * 'searchheaders' is not set, then the search matches any messages
700 * with the substring 'y' in the 'Subject'. The search is case insensitive.
702 * The form '/to:y' is a special case, and will match all messages
703 * containing the substring 'y' in the 'To', 'Cc', or 'Bcc' header
704 * fields. The search for 'to' is case sensitive, so that '/To:y' can
705 * be used to limit the search to just the 'To' field.
708 static char lastscan[STRINGLEN];
710 matchfield(char *str, int mesg)
719 strlcpy(lastscan, str, sizeof(lastscan));
720 mp = &message[mesg-1];
723 * Now look, ignoring case, for the word in the string.
726 if (value("searchheaders") && (cp = strchr(str, ':')) != NULL) {
727 /* Check for special case "/to:" */
728 if (strncmp(str, "to:", 3) == 0)
729 return (matchto(cp, mesg));
731 cp2 = hfield(*str != '\0' ? str : "subject", mp);
736 cp = hfield("subject", mp);
741 return (strcasestr(cp, str) != NULL);
745 * Mark the named message by setting its mark bit.
753 if (i < 1 || i > msgCount)
754 errx(1, "Bad message number to mark");
755 message[i-1].m_flag |= MMARK;
759 * Unmark the named message.
767 if (i < 1 || i > msgCount)
768 errx(1, "Bad message number to unmark");
769 message[i-1].m_flag &= ~MMARK;
773 * Return the message number corresponding to the passed meta character.
776 metamess(int meta, int f)
785 * First 'good' message left.
787 for (mp = &message[0]; mp < &message[msgCount]; mp++)
788 if ((mp->m_flag & MDELETED) == f)
789 return (mp - &message[0] + 1);
790 printf("No applicable messages\n");
795 * Last 'good message left.
797 for (mp = &message[msgCount-1]; mp >= &message[0]; mp--)
798 if ((mp->m_flag & MDELETED) == f)
799 return (mp - &message[0] + 1);
800 printf("No applicable messages\n");
807 m = dot - &message[0] + 1;
808 if ((dot->m_flag & MDELETED) != f) {
809 printf("%d: Inappropriate message\n", m);
815 printf("Unknown metachar (%c)\n", c);