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 * 4. 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
32 static char sccsid
[] = "@(#)list.c 8.4 (Berkeley) 5/1/95";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
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(char *buf
, int *vector
, int flags
)
64 if (markall(buf
, flags
) < 0)
67 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
68 if (mp
->m_flag
& MMARK
)
69 *ip
++ = mp
- &message
[0] + 1;
75 * Mark all messages that the user wanted from the command
76 * line in the message structure. Return 0 on success, -1
81 * Bit values for colon modifiers.
84 #define CMNEW 01 /* New messages */
85 #define CMOLD 02 /* Old messages */
86 #define CMUNREAD 04 /* Unread messages */
87 #define CMDELETED 010 /* Deleted messages */
88 #define CMREAD 020 /* Read messages */
91 * The following table describes the letters which can follow
92 * the colon and gives the corresponding modifier bit.
95 static struct coltab
{
96 char co_char
; /* What to find past : */
97 int co_bit
; /* Associated modifier bit */
98 int co_mask
; /* m_status bits to mask */
99 int co_equal
; /* ... must equal this */
101 { 'n', CMNEW
, MNEW
, MNEW
},
102 { 'o', CMOLD
, MNEW
, 0 },
103 { 'u', CMUNREAD
, MREAD
, 0 },
104 { 'd', CMDELETED
, MDELETED
, MDELETED
},
105 { 'r', CMREAD
, MREAD
, MREAD
},
109 static int lastcolmod
;
112 markall(char buf
[], int f
)
117 char *namelist
[NMLSIZE
], *bufp
;
118 int tok
, beg
, mc
, star
, other
, valdot
, colmod
, colresult
;
120 valdot
= dot
- &message
[0] + 1;
122 for (i
= 1; i
<= msgCount
; i
++)
132 while (tok
!= TEOL
) {
137 printf("No numbers mixed with *\n");
143 if (check(lexnumber
, f
))
145 for (i
= beg
; i
<= lexnumber
; i
++)
146 if (f
== MDELETED
|| (message
[i
- 1].m_flag
& MDELETED
) == 0)
164 printf("Non-numeric second argument\n");
171 printf("Referencing beyond EOF\n");
174 } while ((message
[i
- 1].m_flag
& MDELETED
) != f
);
184 printf("Referencing before 1\n");
187 } while ((message
[i
- 1].m_flag
& MDELETED
) != f
);
194 printf("Non-numeric second argument\n");
198 if (lexstring
[0] == ':') {
199 colresult
= evalcol(lexstring
[1]);
200 if (colresult
== 0) {
201 printf("Unknown colon modifier \"%s\"\n",
208 *np
++ = savestr(lexstring
);
214 lexnumber
= metamess(lexstring
[0], f
);
221 printf("Can't mix \"*\" with anything\n");
236 for (i
= 0; i
< msgCount
; i
++)
237 if ((message
[i
].m_flag
& MDELETED
) == f
) {
242 printf("No applicable messages.\n");
249 * If no numbers were given, mark all of the messages,
250 * so that we can unmark any whose sender was not selected
251 * if any user names were given.
254 if ((np
> namelist
|| colmod
!= 0) && mc
== 0)
255 for (i
= 1; i
<= msgCount
; i
++)
256 if ((message
[i
-1].m_flag
& MDELETED
) == f
)
260 * If any names were given, go through and eliminate any
261 * messages whose senders were not requested.
265 for (i
= 1; i
<= msgCount
; i
++) {
266 for (mc
= 0, np
= &namelist
[0]; *np
!= NULL
; np
++)
268 if (matchfield(*np
, i
)) {
274 if (matchsender(*np
, i
)) {
284 * Make sure we got some decent messages.
288 for (i
= 1; i
<= msgCount
; i
++)
289 if (message
[i
-1].m_flag
& MMARK
) {
294 printf("No applicable messages from {%s",
296 for (np
= &namelist
[1]; *np
!= NULL
; np
++)
304 * If any colon modifiers were given, go through and
305 * unmark any messages which do not satisfy the modifiers.
309 for (i
= 1; i
<= msgCount
; i
++) {
312 mp
= &message
[i
- 1];
313 for (colp
= &coltab
[0]; colp
->co_char
!= '\0'; colp
++)
314 if (colp
->co_bit
& colmod
)
315 if ((mp
->m_flag
& colp
->co_mask
)
320 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
321 if (mp
->m_flag
& MMARK
)
323 if (mp
>= &message
[msgCount
]) {
326 printf("No messages satisfy");
327 for (colp
= &coltab
[0]; colp
->co_char
!= '\0'; colp
++)
328 if (colp
->co_bit
& colmod
)
329 printf(" :%c", colp
->co_char
);
338 * Turn the character after a colon modifier into a bit
348 for (colp
= &coltab
[0]; colp
->co_char
!= '\0'; colp
++)
349 if (colp
->co_char
== col
)
350 return (colp
->co_bit
);
355 * Check the passed message number for legality and proper flags.
356 * If f is MDELETED, then either kind will do. Otherwise, the message
357 * has to be undeleted.
360 check(int mesg
, int f
)
364 if (mesg
< 1 || mesg
> msgCount
) {
365 printf("%d: Invalid message number\n", mesg
);
368 mp
= &message
[mesg
-1];
369 if (f
!= MDELETED
&& (mp
->m_flag
& MDELETED
) != 0) {
370 printf("%d: Inappropriate message\n", mesg
);
377 * Scan out the list of string arguments, shell style
381 getrawlist(char line
[], char **argv
, int argc
)
383 char c
, *cp
, *cp2
, quotec
;
386 size_t linebufsize
= BUFSIZ
;
388 if ((linebuf
= malloc(linebufsize
)) == NULL
)
389 err(1, "Out of memory");
394 for (; *cp
== ' ' || *cp
== '\t'; cp
++)
398 if (argn
>= argc
- 1) {
400 "Too many elements in the list; excess discarded.\n");
405 while ((c
= *cp
) != '\0') {
406 /* Allocate more space if necessary */
407 if (cp2
- linebuf
== linebufsize
- 1) {
408 linebufsize
+= BUFSIZ
;
409 if ((linebuf
= realloc(linebuf
, linebufsize
)) == NULL
)
410 err(1, "Out of memory");
411 cp2
= linebuf
+ linebufsize
- BUFSIZ
- 1;
414 if (quotec
!= '\0') {
423 case '0': case '1': case '2': case '3':
424 case '4': case '5': case '6': case '7':
426 if (*cp
>= '0' && *cp
<= '7')
427 c
= c
* 8 + *cp
++ - '0';
428 if (*cp
>= '0' && *cp
<= '7')
429 c
= c
* 8 + *cp
++ - '0';
457 /* null doesn't show up anyway */
458 else if ((c
>= 'A' && c
<= '_') ||
459 (c
>= 'a' && c
<= 'z'))
467 } else if (c
== '"' || c
== '\'')
469 else if (c
== ' ' || c
== '\t')
475 argv
[argn
++] = savestr(linebuf
);
483 * scan out a single lexical item and return its token number,
484 * updating the string pointer passed **p. Also, store the value
485 * of the number or string scanned in lexnumber or lexstring as
486 * appropriate. In any event, store the scanned `thing' in lexstring.
513 strcpy(lexstring
, string_stack
[regretp
]);
514 lexnumber
= numberstack
[regretp
];
515 return (regretstack
[regretp
--]);
522 * strip away leading white space.
525 while (c
== ' ' || c
== '\t')
529 * If no characters remain, we are at end of line,
539 * If the leading character is a digit, scan
540 * the number and convert it on the fly.
541 * Return TNUMBER when done.
544 if (isdigit((unsigned char)c
)) {
546 while (isdigit((unsigned char)c
)) {
547 lexnumber
= lexnumber
*10 + c
- '0';
557 * Check for single character tokens; return such
561 for (lp
= &singles
[0]; lp
->l_char
!= '\0'; lp
++)
562 if (c
== lp
->l_char
) {
566 return (lp
->l_token
);
570 * We've got a string! Copy all the characters
571 * of the string into lexstring, until we see
572 * a null, space, or tab.
573 * If the lead character is a " or ', save it
574 * and scan until you get another.
578 if (c
== '\'' || c
== '"') {
587 if (quotec
== 0 && (c
== ' ' || c
== '\t'))
589 if (cp2
- lexstring
< STRINGLEN
-1)
593 if (quotec
&& c
== '\0') {
594 fprintf(stderr
, "Missing %c\n", quotec
);
603 * Unscan the named token by pushing it onto the regret stack.
608 if (++regretp
>= REGDEP
)
609 errx(1, "Too many regrets");
610 regretstack
[regretp
] = token
;
611 lexstring
[STRINGLEN
-1] = '\0';
612 string_stack
[regretp
] = savestr(lexstring
);
613 numberstack
[regretp
] = lexnumber
;
617 * Reset all the scanner global variables.
626 * Find the first message whose flags & m == f and return
627 * its message number.
638 for (mp
= dot
; mp
< &message
[msgCount
]; mp
++)
639 if ((mp
->m_flag
& m
) == f
)
640 return (mp
- message
+ 1);
641 for (mp
= dot
-1; mp
>= &message
[0]; mp
--)
642 if ((mp
->m_flag
& m
) == f
)
643 return (mp
- message
+ 1);
648 * See if the passed name sent the passed message number. Return true
652 matchsender(char *str
, int mesg
)
656 /* null string matches nothing instead of everything */
660 cp
= nameof(&message
[mesg
- 1], 0);
661 return (strcasestr(cp
, str
) != NULL
);
665 * See if the passed name received the passed message number. Return true
669 static char *to_fields
[] = { "to", "cc", "bcc", NULL
};
672 matchto(char *str
, int mesg
)
679 /* null string matches nothing instead of everything */
683 mp
= &message
[mesg
- 1];
685 for (to
= to_fields
; *to
!= NULL
; to
++) {
686 cp
= hfield(*to
, mp
);
687 if (cp
!= NULL
&& strcasestr(cp
, str
) != NULL
)
694 * See if the given substring is contained within the specified field. If
695 * 'searchheaders' is set, then the form '/x:y' will be accepted and matches
696 * any message with the substring 'y' in field 'x'. If 'x' is omitted or
697 * 'searchheaders' is not set, then the search matches any messages
698 * with the substring 'y' in the 'Subject'. The search is case insensitive.
700 * The form '/to:y' is a special case, and will match all messages
701 * containing the substring 'y' in the 'To', 'Cc', or 'Bcc' header
702 * fields. The search for 'to' is case sensitive, so that '/To:y' can
703 * be used to limit the search to just the 'To' field.
706 static char lastscan
[STRINGLEN
];
708 matchfield(char *str
, int mesg
)
717 strlcpy(lastscan
, str
, sizeof(lastscan
));
718 mp
= &message
[mesg
-1];
721 * Now look, ignoring case, for the word in the string.
724 if (value("searchheaders") && (cp
= strchr(str
, ':')) != NULL
) {
725 /* Check for special case "/to:" */
726 if (strncmp(str
, "to:", 3) == 0)
727 return (matchto(cp
, mesg
));
729 cp2
= hfield(*str
!= '\0' ? str
: "subject", mp
);
734 cp
= hfield("subject", mp
);
739 return (strcasestr(cp
, str
) != NULL
);
743 * Mark the named message by setting its mark bit.
751 if (i
< 1 || i
> msgCount
)
752 errx(1, "Bad message number to mark");
753 message
[i
-1].m_flag
|= MMARK
;
757 * Unmark the named message.
765 if (i
< 1 || i
> msgCount
)
766 errx(1, "Bad message number to unmark");
767 message
[i
-1].m_flag
&= ~MMARK
;
771 * Return the message number corresponding to the passed meta character.
774 metamess(int meta
, int f
)
783 * First 'good' message left.
785 for (mp
= &message
[0]; mp
< &message
[msgCount
]; mp
++)
786 if ((mp
->m_flag
& MDELETED
) == f
)
787 return (mp
- &message
[0] + 1);
788 printf("No applicable messages\n");
793 * Last 'good message left.
795 for (mp
= &message
[msgCount
-1]; mp
>= &message
[0]; mp
--)
796 if ((mp
->m_flag
& MDELETED
) == f
)
797 return (mp
- &message
[0] + 1);
798 printf("No applicable messages\n");
805 m
= dot
- &message
[0] + 1;
806 if ((dot
->m_flag
& MDELETED
) != f
) {
807 printf("%d: Inappropriate message\n", m
);
813 printf("Unknown metachar (%c)\n", c
);