one-true-awk: Avoid a NULL dereference.
[freebsd-src.git] / usr.bin / mail / list.c
blob3a5cc150efb1f38843455568b76b2a6c55ba4365
1 /*
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
7 * are met:
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
27 * SUCH DAMAGE.
30 #ifndef lint
31 #if 0
32 static char sccsid[] = "@(#)list.c 8.4 (Berkeley) 5/1/95";
33 #endif
34 #endif /* not lint */
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include "rcv.h"
39 #include <ctype.h>
40 #include "extern.h"
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.
54 int
55 getmsglist(char *buf, int *vector, int flags)
57 int *ip;
58 struct message *mp;
60 if (msgCount == 0) {
61 *vector = 0;
62 return (0);
64 if (markall(buf, flags) < 0)
65 return (-1);
66 ip = vector;
67 for (mp = &message[0]; mp < &message[msgCount]; mp++)
68 if (mp->m_flag & MMARK)
69 *ip++ = mp - &message[0] + 1;
70 *ip = 0;
71 return (ip - vector);
75 * Mark all messages that the user wanted from the command
76 * line in the message structure. Return 0 on success, -1
77 * on error.
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 */
100 } coltab[] = {
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 },
106 { 0, 0, 0, 0 }
109 static int lastcolmod;
112 markall(char buf[], int f)
114 char **np;
115 int i;
116 struct message *mp;
117 char *namelist[NMLSIZE], *bufp;
118 int tok, beg, mc, star, other, valdot, colmod, colresult;
120 valdot = dot - &message[0] + 1;
121 colmod = 0;
122 for (i = 1; i <= msgCount; i++)
123 unmark(i);
124 bufp = buf;
125 mc = 0;
126 np = &namelist[0];
127 scaninit();
128 tok = scan(&bufp);
129 star = 0;
130 other = 0;
131 beg = 0;
132 while (tok != TEOL) {
133 switch (tok) {
134 case TNUMBER:
135 number:
136 if (star) {
137 printf("No numbers mixed with *\n");
138 return (-1);
140 mc++;
141 other++;
142 if (beg != 0) {
143 if (check(lexnumber, f))
144 return (-1);
145 for (i = beg; i <= lexnumber; i++)
146 if (f == MDELETED || (message[i - 1].m_flag & MDELETED) == 0)
147 mark(i);
148 beg = 0;
149 break;
151 beg = lexnumber;
152 if (check(beg, f))
153 return (-1);
154 tok = scan(&bufp);
155 regret(tok);
156 if (tok != TDASH) {
157 mark(beg);
158 beg = 0;
160 break;
162 case TPLUS:
163 if (beg != 0) {
164 printf("Non-numeric second argument\n");
165 return (-1);
167 i = valdot;
168 do {
169 i++;
170 if (i > msgCount) {
171 printf("Referencing beyond EOF\n");
172 return (-1);
174 } while ((message[i - 1].m_flag & MDELETED) != f);
175 mark(i);
176 break;
178 case TDASH:
179 if (beg == 0) {
180 i = valdot;
181 do {
182 i--;
183 if (i <= 0) {
184 printf("Referencing before 1\n");
185 return (-1);
187 } while ((message[i - 1].m_flag & MDELETED) != f);
188 mark(i);
190 break;
192 case TSTRING:
193 if (beg != 0) {
194 printf("Non-numeric second argument\n");
195 return (-1);
197 other++;
198 if (lexstring[0] == ':') {
199 colresult = evalcol(lexstring[1]);
200 if (colresult == 0) {
201 printf("Unknown colon modifier \"%s\"\n",
202 lexstring);
203 return (-1);
205 colmod |= colresult;
207 else
208 *np++ = savestr(lexstring);
209 break;
211 case TDOLLAR:
212 case TUP:
213 case TDOT:
214 lexnumber = metamess(lexstring[0], f);
215 if (lexnumber == -1)
216 return (-1);
217 goto number;
219 case TSTAR:
220 if (other) {
221 printf("Can't mix \"*\" with anything\n");
222 return (-1);
224 star++;
225 break;
227 case TERROR:
228 return (-1);
230 tok = scan(&bufp);
232 lastcolmod = colmod;
233 *np = NULL;
234 mc = 0;
235 if (star) {
236 for (i = 0; i < msgCount; i++)
237 if ((message[i].m_flag & MDELETED) == f) {
238 mark(i+1);
239 mc++;
241 if (mc == 0) {
242 printf("No applicable messages.\n");
243 return (-1);
245 return (0);
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)
257 mark(i);
260 * If any names were given, go through and eliminate any
261 * messages whose senders were not requested.
264 if (np > namelist) {
265 for (i = 1; i <= msgCount; i++) {
266 for (mc = 0, np = &namelist[0]; *np != NULL; np++)
267 if (**np == '/') {
268 if (matchfield(*np, i)) {
269 mc++;
270 break;
273 else {
274 if (matchsender(*np, i)) {
275 mc++;
276 break;
279 if (mc == 0)
280 unmark(i);
284 * Make sure we got some decent messages.
287 mc = 0;
288 for (i = 1; i <= msgCount; i++)
289 if (message[i-1].m_flag & MMARK) {
290 mc++;
291 break;
293 if (mc == 0) {
294 printf("No applicable messages from {%s",
295 namelist[0]);
296 for (np = &namelist[1]; *np != NULL; np++)
297 printf(", %s", *np);
298 printf("}\n");
299 return (-1);
304 * If any colon modifiers were given, go through and
305 * unmark any messages which do not satisfy the modifiers.
308 if (colmod != 0) {
309 for (i = 1; i <= msgCount; i++) {
310 struct coltab *colp;
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)
316 != colp->co_equal)
317 unmark(i);
320 for (mp = &message[0]; mp < &message[msgCount]; mp++)
321 if (mp->m_flag & MMARK)
322 break;
323 if (mp >= &message[msgCount]) {
324 struct coltab *colp;
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);
330 printf("\n");
331 return (-1);
334 return (0);
338 * Turn the character after a colon modifier into a bit
339 * value.
342 evalcol(int col)
344 struct coltab *colp;
346 if (col == 0)
347 return (lastcolmod);
348 for (colp = &coltab[0]; colp->co_char != '\0'; colp++)
349 if (colp->co_char == col)
350 return (colp->co_bit);
351 return (0);
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)
362 struct message *mp;
364 if (mesg < 1 || mesg > msgCount) {
365 printf("%d: Invalid message number\n", mesg);
366 return (-1);
368 mp = &message[mesg-1];
369 if (f != MDELETED && (mp->m_flag & MDELETED) != 0) {
370 printf("%d: Inappropriate message\n", mesg);
371 return (-1);
373 return (0);
377 * Scan out the list of string arguments, shell style
378 * for a RAWLIST.
381 getrawlist(char line[], char **argv, int argc)
383 char c, *cp, *cp2, quotec;
384 int argn;
385 char *linebuf;
386 size_t linebufsize = BUFSIZ;
388 if ((linebuf = malloc(linebufsize)) == NULL)
389 err(1, "Out of memory");
391 argn = 0;
392 cp = line;
393 for (;;) {
394 for (; *cp == ' ' || *cp == '\t'; cp++)
396 if (*cp == '\0')
397 break;
398 if (argn >= argc - 1) {
399 printf(
400 "Too many elements in the list; excess discarded.\n");
401 break;
403 cp2 = linebuf;
404 quotec = '\0';
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;
413 cp++;
414 if (quotec != '\0') {
415 if (c == quotec)
416 quotec = '\0';
417 else if (c == '\\')
418 switch (c = *cp++) {
419 case '\0':
420 *cp2++ = '\\';
421 cp--;
422 break;
423 case '0': case '1': case '2': case '3':
424 case '4': case '5': case '6': case '7':
425 c -= '0';
426 if (*cp >= '0' && *cp <= '7')
427 c = c * 8 + *cp++ - '0';
428 if (*cp >= '0' && *cp <= '7')
429 c = c * 8 + *cp++ - '0';
430 *cp2++ = c;
431 break;
432 case 'b':
433 *cp2++ = '\b';
434 break;
435 case 'f':
436 *cp2++ = '\f';
437 break;
438 case 'n':
439 *cp2++ = '\n';
440 break;
441 case 'r':
442 *cp2++ = '\r';
443 break;
444 case 't':
445 *cp2++ = '\t';
446 break;
447 case 'v':
448 *cp2++ = '\v';
449 break;
450 default:
451 *cp2++ = c;
453 else if (c == '^') {
454 c = *cp++;
455 if (c == '?')
456 *cp2++ = '\177';
457 /* null doesn't show up anyway */
458 else if ((c >= 'A' && c <= '_') ||
459 (c >= 'a' && c <= 'z'))
460 *cp2++ = c & 037;
461 else {
462 *cp2++ = '^';
463 cp--;
465 } else
466 *cp2++ = c;
467 } else if (c == '"' || c == '\'')
468 quotec = c;
469 else if (c == ' ' || c == '\t')
470 break;
471 else
472 *cp2++ = c;
474 *cp2 = '\0';
475 argv[argn++] = savestr(linebuf);
477 argv[argn] = NULL;
478 (void)free(linebuf);
479 return (argn);
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.
489 static struct lex {
490 char l_char;
491 char l_token;
492 } singles[] = {
493 { '$', TDOLLAR },
494 { '.', TDOT },
495 { '^', TUP },
496 { '*', TSTAR },
497 { '-', TDASH },
498 { '+', TPLUS },
499 { '(', TOPEN },
500 { ')', TCLOSE },
501 { 0, 0 }
505 scan(char **sp)
507 char *cp, *cp2;
508 int c;
509 struct lex *lp;
510 int quotec;
512 if (regretp >= 0) {
513 strcpy(lexstring, string_stack[regretp]);
514 lexnumber = numberstack[regretp];
515 return (regretstack[regretp--]);
517 cp = *sp;
518 cp2 = lexstring;
519 c = *cp++;
522 * strip away leading white space.
525 while (c == ' ' || c == '\t')
526 c = *cp++;
529 * If no characters remain, we are at end of line,
530 * so report that.
533 if (c == '\0') {
534 *sp = --cp;
535 return (TEOL);
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)) {
545 lexnumber = 0;
546 while (isdigit((unsigned char)c)) {
547 lexnumber = lexnumber*10 + c - '0';
548 *cp2++ = c;
549 c = *cp++;
551 *cp2 = '\0';
552 *sp = --cp;
553 return (TNUMBER);
557 * Check for single character tokens; return such
558 * if found.
561 for (lp = &singles[0]; lp->l_char != '\0'; lp++)
562 if (c == lp->l_char) {
563 lexstring[0] = c;
564 lexstring[1] = '\0';
565 *sp = cp;
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.
577 quotec = 0;
578 if (c == '\'' || c == '"') {
579 quotec = c;
580 c = *cp++;
582 while (c != '\0') {
583 if (c == quotec) {
584 cp++;
585 break;
587 if (quotec == 0 && (c == ' ' || c == '\t'))
588 break;
589 if (cp2 - lexstring < STRINGLEN-1)
590 *cp2++ = c;
591 c = *cp++;
593 if (quotec && c == '\0') {
594 fprintf(stderr, "Missing %c\n", quotec);
595 return (TERROR);
597 *sp = --cp;
598 *cp2 = '\0';
599 return (TSTRING);
603 * Unscan the named token by pushing it onto the regret stack.
605 void
606 regret(int token)
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.
619 void
620 scaninit(void)
622 regretp = -1;
626 * Find the first message whose flags & m == f and return
627 * its message number.
630 first(int f, int m)
632 struct message *mp;
634 if (msgCount == 0)
635 return (0);
636 f &= MDELETED;
637 m &= MDELETED;
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);
644 return (0);
648 * See if the passed name sent the passed message number. Return true
649 * if so.
652 matchsender(char *str, int mesg)
654 char *cp;
656 /* null string matches nothing instead of everything */
657 if (*str == '\0')
658 return (0);
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
666 * if so.
669 static char *to_fields[] = { "to", "cc", "bcc", NULL };
671 static int
672 matchto(char *str, int mesg)
674 struct message *mp;
675 char *cp, **to;
677 str++;
679 /* null string matches nothing instead of everything */
680 if (*str == '\0')
681 return (0);
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)
688 return (1);
690 return (0);
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)
710 struct message *mp;
711 char *cp, *cp2;
713 str++;
714 if (*str == '\0')
715 str = lastscan;
716 else
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));
728 *cp++ = '\0';
729 cp2 = hfield(*str != '\0' ? str : "subject", mp);
730 cp[-1] = ':';
731 str = cp;
732 cp = cp2;
733 } else
734 cp = hfield("subject", mp);
736 if (cp == NULL)
737 return (0);
739 return (strcasestr(cp, str) != NULL);
743 * Mark the named message by setting its mark bit.
745 void
746 mark(int mesg)
748 int i;
750 i = mesg;
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.
759 void
760 unmark(int mesg)
762 int i;
764 i = mesg;
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)
776 int c, m;
777 struct message *mp;
779 c = meta;
780 switch (c) {
781 case '^':
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");
789 return (-1);
791 case '$':
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");
799 return (-1);
801 case '.':
803 * Current message.
805 m = dot - &message[0] + 1;
806 if ((dot->m_flag & MDELETED) != f) {
807 printf("%d: Inappropriate message\n", m);
808 return (-1);
810 return (m);
812 default:
813 printf("Unknown metachar (%c)\n", c);
814 return (-1);