Annotation of src/usr.bin/mail/list.c, Revision 1.4
1.4 ! deraadt 1: /* $OpenBSD: list.c,v 1.3 1997/01/17 07:12:49 millert Exp $ */
1.2 deraadt 2: /* $NetBSD: list.c,v 1.4 1996/06/08 19:48:30 christos Exp $ */
3:
1.1 deraadt 4: /*
5: * Copyright (c) 1980, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
1.2 deraadt 38: #if 0
39: static char sccsid[] = "@(#)list.c 8.2 (Berkeley) 4/19/94";
40: #else
1.4 ! deraadt 41: static char rcsid[] = "$OpenBSD: list.c,v 1.3 1997/01/17 07:12:49 millert Exp $";
1.2 deraadt 42: #endif
1.1 deraadt 43: #endif /* not lint */
44:
45: #include "rcv.h"
46: #include <ctype.h>
47: #include "extern.h"
48:
49: /*
50: * Mail -- a mail program
51: *
52: * Message list handling.
53: */
54:
55: /*
56: * Convert the user string of message numbers and
57: * store the numbers into vector.
58: *
59: * Returns the count of messages picked up or -1 on error.
60: */
61: int
62: getmsglist(buf, vector, flags)
63: char *buf;
64: int *vector, flags;
65: {
66: register int *ip;
67: register struct message *mp;
68:
69: if (msgCount == 0) {
70: *vector = 0;
71: return 0;
72: }
73: if (markall(buf, flags) < 0)
74: return(-1);
75: ip = vector;
76: for (mp = &message[0]; mp < &message[msgCount]; mp++)
77: if (mp->m_flag & MMARK)
78: *ip++ = mp - &message[0] + 1;
79: *ip = 0;
80: return(ip - vector);
81: }
82:
83: /*
84: * Mark all messages that the user wanted from the command
85: * line in the message structure. Return 0 on success, -1
86: * on error.
87: */
88:
89: /*
90: * Bit values for colon modifiers.
91: */
92:
93: #define CMNEW 01 /* New messages */
94: #define CMOLD 02 /* Old messages */
95: #define CMUNREAD 04 /* Unread messages */
96: #define CMDELETED 010 /* Deleted messages */
97: #define CMREAD 020 /* Read messages */
98:
99: /*
100: * The following table describes the letters which can follow
101: * the colon and gives the corresponding modifier bit.
102: */
103:
104: struct coltab {
105: char co_char; /* What to find past : */
106: int co_bit; /* Associated modifier bit */
107: int co_mask; /* m_status bits to mask */
108: int co_equal; /* ... must equal this */
109: } coltab[] = {
1.2 deraadt 110: { 'n', CMNEW, MNEW, MNEW },
111: { 'o', CMOLD, MNEW, 0 },
112: { 'u', CMUNREAD, MREAD, 0 },
113: { 'd', CMDELETED, MDELETED, MDELETED },
114: { 'r', CMREAD, MREAD, MREAD },
115: { 0, 0, 0, 0 }
1.1 deraadt 116: };
117:
118: static int lastcolmod;
119:
120: int
121: markall(buf, f)
122: char buf[];
123: int f;
124: {
125: register char **np;
126: register int i;
127: register struct message *mp;
128: char *namelist[NMLSIZE], *bufp;
129: int tok, beg, mc, star, other, valdot, colmod, colresult;
130:
131: valdot = dot - &message[0] + 1;
132: colmod = 0;
133: for (i = 1; i <= msgCount; i++)
134: unmark(i);
135: bufp = buf;
136: mc = 0;
137: np = &namelist[0];
138: scaninit();
139: tok = scan(&bufp);
140: star = 0;
141: other = 0;
142: beg = 0;
143: while (tok != TEOL) {
144: switch (tok) {
145: case TNUMBER:
146: number:
147: if (star) {
148: printf("No numbers mixed with *\n");
149: return(-1);
150: }
151: mc++;
152: other++;
153: if (beg != 0) {
154: if (check(lexnumber, f))
155: return(-1);
156: for (i = beg; i <= lexnumber; i++)
157: if (f == MDELETED || (message[i - 1].m_flag & MDELETED) == 0)
158: mark(i);
159: beg = 0;
160: break;
161: }
162: beg = lexnumber;
163: if (check(beg, f))
164: return(-1);
165: tok = scan(&bufp);
166: regret(tok);
167: if (tok != TDASH) {
168: mark(beg);
169: beg = 0;
170: }
171: break;
172:
173: case TPLUS:
174: if (beg != 0) {
175: printf("Non-numeric second argument\n");
176: return(-1);
177: }
178: i = valdot;
179: do {
180: i++;
181: if (i > msgCount) {
182: printf("Referencing beyond EOF\n");
183: return(-1);
184: }
185: } while ((message[i - 1].m_flag & MDELETED) != f);
186: mark(i);
187: break;
188:
189: case TDASH:
190: if (beg == 0) {
191: i = valdot;
192: do {
193: i--;
194: if (i <= 0) {
195: printf("Referencing before 1\n");
196: return(-1);
197: }
198: } while ((message[i - 1].m_flag & MDELETED) != f);
199: mark(i);
200: }
201: break;
202:
203: case TSTRING:
204: if (beg != 0) {
205: printf("Non-numeric second argument\n");
206: return(-1);
207: }
208: other++;
209: if (lexstring[0] == ':') {
210: colresult = evalcol(lexstring[1]);
211: if (colresult == 0) {
212: printf("Unknown colon modifier \"%s\"\n",
213: lexstring);
214: return(-1);
215: }
216: colmod |= colresult;
217: }
218: else
219: *np++ = savestr(lexstring);
220: break;
221:
222: case TDOLLAR:
223: case TUP:
224: case TDOT:
225: lexnumber = metamess(lexstring[0], f);
226: if (lexnumber == -1)
227: return(-1);
228: goto number;
229:
230: case TSTAR:
231: if (other) {
232: printf("Can't mix \"*\" with anything\n");
233: return(-1);
234: }
235: star++;
236: break;
237:
238: case TERROR:
239: return -1;
240: }
241: tok = scan(&bufp);
242: }
243: lastcolmod = colmod;
244: *np = NOSTR;
245: mc = 0;
246: if (star) {
247: for (i = 0; i < msgCount; i++)
248: if ((message[i].m_flag & MDELETED) == f) {
249: mark(i+1);
250: mc++;
251: }
252: if (mc == 0) {
253: printf("No applicable messages.\n");
254: return(-1);
255: }
256: return(0);
257: }
258:
259: /*
260: * If no numbers were given, mark all of the messages,
261: * so that we can unmark any whose sender was not selected
262: * if any user names were given.
263: */
264:
265: if ((np > namelist || colmod != 0) && mc == 0)
266: for (i = 1; i <= msgCount; i++)
267: if ((message[i-1].m_flag & MDELETED) == f)
268: mark(i);
269:
270: /*
271: * If any names were given, go through and eliminate any
272: * messages whose senders were not requested.
273: */
274:
275: if (np > namelist) {
276: for (i = 1; i <= msgCount; i++) {
277: for (mc = 0, np = &namelist[0]; *np != NOSTR; np++)
278: if (**np == '/') {
279: if (matchsubj(*np, i)) {
280: mc++;
281: break;
282: }
283: }
284: else {
285: if (matchsender(*np, i)) {
286: mc++;
287: break;
288: }
289: }
290: if (mc == 0)
291: unmark(i);
292: }
293:
294: /*
295: * Make sure we got some decent messages.
296: */
297:
298: mc = 0;
299: for (i = 1; i <= msgCount; i++)
300: if (message[i-1].m_flag & MMARK) {
301: mc++;
302: break;
303: }
304: if (mc == 0) {
305: printf("No applicable messages from {%s",
306: namelist[0]);
307: for (np = &namelist[1]; *np != NOSTR; np++)
308: printf(", %s", *np);
309: printf("}\n");
310: return(-1);
311: }
312: }
313:
314: /*
315: * If any colon modifiers were given, go through and
316: * unmark any messages which do not satisfy the modifiers.
317: */
318:
319: if (colmod != 0) {
320: for (i = 1; i <= msgCount; i++) {
321: register struct coltab *colp;
322:
323: mp = &message[i - 1];
324: for (colp = &coltab[0]; colp->co_char; colp++)
325: if (colp->co_bit & colmod)
326: if ((mp->m_flag & colp->co_mask)
327: != colp->co_equal)
328: unmark(i);
329:
330: }
331: for (mp = &message[0]; mp < &message[msgCount]; mp++)
332: if (mp->m_flag & MMARK)
333: break;
334: if (mp >= &message[msgCount]) {
335: register struct coltab *colp;
336:
337: printf("No messages satisfy");
338: for (colp = &coltab[0]; colp->co_char; colp++)
339: if (colp->co_bit & colmod)
340: printf(" :%c", colp->co_char);
341: printf("\n");
342: return(-1);
343: }
344: }
345: return(0);
346: }
347:
348: /*
349: * Turn the character after a colon modifier into a bit
350: * value.
351: */
352: int
353: evalcol(col)
354: int col;
355: {
356: register struct coltab *colp;
357:
358: if (col == 0)
359: return(lastcolmod);
360: for (colp = &coltab[0]; colp->co_char; colp++)
361: if (colp->co_char == col)
362: return(colp->co_bit);
363: return(0);
364: }
365:
366: /*
367: * Check the passed message number for legality and proper flags.
368: * If f is MDELETED, then either kind will do. Otherwise, the message
369: * has to be undeleted.
370: */
371: int
372: check(mesg, f)
373: int mesg, f;
374: {
375: register struct message *mp;
376:
377: if (mesg < 1 || mesg > msgCount) {
378: printf("%d: Invalid message number\n", mesg);
379: return(-1);
380: }
381: mp = &message[mesg-1];
382: if (f != MDELETED && (mp->m_flag & MDELETED) != 0) {
383: printf("%d: Inappropriate message\n", mesg);
384: return(-1);
385: }
386: return(0);
387: }
388:
389: /*
390: * Scan out the list of string arguments, shell style
391: * for a RAWLIST.
392: */
393: int
394: getrawlist(line, argv, argc)
395: char line[];
396: char **argv;
397: int argc;
398: {
399: register char c, *cp, *cp2, quotec;
400: int argn;
401: char linebuf[BUFSIZ];
402:
403: argn = 0;
404: cp = line;
405: for (;;) {
406: for (; *cp == ' ' || *cp == '\t'; cp++)
407: ;
408: if (*cp == '\0')
409: break;
410: if (argn >= argc - 1) {
411: printf(
412: "Too many elements in the list; excess discarded.\n");
413: break;
414: }
415: cp2 = linebuf;
416: quotec = '\0';
417: while ((c = *cp) != '\0') {
418: cp++;
419: if (quotec != '\0') {
420: if (c == quotec)
421: quotec = '\0';
422: else if (c == '\\')
423: switch (c = *cp++) {
424: case '\0':
425: *cp2++ = '\\';
426: cp--;
427: break;
428: case '0': case '1': case '2': case '3':
429: case '4': case '5': case '6': case '7':
430: c -= '0';
431: if (*cp >= '0' && *cp <= '7')
432: c = c * 8 + *cp++ - '0';
433: if (*cp >= '0' && *cp <= '7')
434: c = c * 8 + *cp++ - '0';
435: *cp2++ = c;
436: break;
437: case 'b':
438: *cp2++ = '\b';
439: break;
440: case 'f':
441: *cp2++ = '\f';
442: break;
443: case 'n':
444: *cp2++ = '\n';
445: break;
446: case 'r':
447: *cp2++ = '\r';
448: break;
449: case 't':
450: *cp2++ = '\t';
451: break;
452: case 'v':
453: *cp2++ = '\v';
454: break;
455: default:
456: *cp2++ = c;
457: }
458: else if (c == '^') {
459: c = *cp++;
460: if (c == '?')
461: *cp2++ = '\177';
462: /* null doesn't show up anyway */
1.2 deraadt 463: else if ((c >= 'A' && c <= '_') ||
464: (c >= 'a' && c <= 'z'))
1.1 deraadt 465: *cp2++ = c & 037;
466: else {
467: *cp2++ = '^';
468: cp--;
469: }
470: } else
471: *cp2++ = c;
472: } else if (c == '"' || c == '\'')
473: quotec = c;
474: else if (c == ' ' || c == '\t')
475: break;
476: else
477: *cp2++ = c;
478: }
479: *cp2 = '\0';
480: argv[argn++] = savestr(linebuf);
481: }
482: argv[argn] = NOSTR;
483: return argn;
484: }
485:
486: /*
487: * scan out a single lexical item and return its token number,
488: * updating the string pointer passed **p. Also, store the value
489: * of the number or string scanned in lexnumber or lexstring as
490: * appropriate. In any event, store the scanned `thing' in lexstring.
491: */
492:
493: struct lex {
494: char l_char;
495: char l_token;
496: } singles[] = {
1.2 deraadt 497: { '$', TDOLLAR },
498: { '.', TDOT },
499: { '^', TUP },
500: { '*', TSTAR },
501: { '-', TDASH },
502: { '+', TPLUS },
503: { '(', TOPEN },
504: { ')', TCLOSE },
505: { 0, 0 }
1.1 deraadt 506: };
507:
508: int
509: scan(sp)
510: char **sp;
511: {
512: register char *cp, *cp2;
513: register int c;
514: register struct lex *lp;
515: int quotec;
516:
517: if (regretp >= 0) {
518: strcpy(lexstring, string_stack[regretp]);
519: lexnumber = numberstack[regretp];
520: return(regretstack[regretp--]);
521: }
522: cp = *sp;
523: cp2 = lexstring;
524: c = *cp++;
525:
526: /*
527: * strip away leading white space.
528: */
529:
530: while (c == ' ' || c == '\t')
531: c = *cp++;
532:
533: /*
534: * If no characters remain, we are at end of line,
535: * so report that.
536: */
537:
538: if (c == '\0') {
539: *sp = --cp;
540: return(TEOL);
541: }
542:
543: /*
544: * If the leading character is a digit, scan
545: * the number and convert it on the fly.
546: * Return TNUMBER when done.
547: */
548:
549: if (isdigit(c)) {
550: lexnumber = 0;
551: while (isdigit(c)) {
552: lexnumber = lexnumber*10 + c - '0';
553: *cp2++ = c;
554: c = *cp++;
555: }
556: *cp2 = '\0';
557: *sp = --cp;
558: return(TNUMBER);
559: }
560:
561: /*
562: * Check for single character tokens; return such
563: * if found.
564: */
565:
566: for (lp = &singles[0]; lp->l_char != 0; lp++)
567: if (c == lp->l_char) {
568: lexstring[0] = c;
569: lexstring[1] = '\0';
570: *sp = cp;
571: return(lp->l_token);
572: }
573:
574: /*
575: * We've got a string! Copy all the characters
576: * of the string into lexstring, until we see
577: * a null, space, or tab.
578: * If the lead character is a " or ', save it
579: * and scan until you get another.
580: */
581:
582: quotec = 0;
583: if (c == '\'' || c == '"') {
584: quotec = c;
585: c = *cp++;
586: }
587: while (c != '\0') {
588: if (c == quotec) {
589: cp++;
590: break;
591: }
592: if (quotec == 0 && (c == ' ' || c == '\t'))
593: break;
594: if (cp2 - lexstring < STRINGLEN-1)
595: *cp2++ = c;
596: c = *cp++;
597: }
598: if (quotec && c == 0) {
599: fprintf(stderr, "Missing %c\n", quotec);
600: return TERROR;
601: }
602: *sp = --cp;
603: *cp2 = '\0';
604: return(TSTRING);
605: }
606:
607: /*
608: * Unscan the named token by pushing it onto the regret stack.
609: */
610: void
611: regret(token)
612: int token;
613: {
614: if (++regretp >= REGDEP)
615: panic("Too many regrets");
616: regretstack[regretp] = token;
617: lexstring[STRINGLEN-1] = '\0';
618: string_stack[regretp] = savestr(lexstring);
619: numberstack[regretp] = lexnumber;
620: }
621:
622: /*
623: * Reset all the scanner global variables.
624: */
625: void
626: scaninit()
627: {
628: regretp = -1;
629: }
630:
631: /*
632: * Find the first message whose flags & m == f and return
633: * its message number.
634: */
635: int
636: first(f, m)
637: int f, m;
638: {
639: register struct message *mp;
640:
641: if (msgCount == 0)
642: return 0;
643: f &= MDELETED;
644: m &= MDELETED;
645: for (mp = dot; mp < &message[msgCount]; mp++)
646: if ((mp->m_flag & m) == f)
647: return mp - message + 1;
648: for (mp = dot-1; mp >= &message[0]; mp--)
649: if ((mp->m_flag & m) == f)
650: return mp - message + 1;
651: return 0;
652: }
653:
654: /*
655: * See if the passed name sent the passed message number. Return true
656: * if so.
657: */
658: int
659: matchsender(str, mesg)
660: char *str;
661: int mesg;
662: {
663: register char *cp, *cp2, *backup;
664:
665: if (!*str) /* null string matches nothing instead of everything */
666: return 0;
667: backup = cp2 = nameof(&message[mesg - 1], 0);
668: cp = str;
669: while (*cp2) {
670: if (*cp == 0)
671: return(1);
672: if (raise(*cp++) != raise(*cp2++)) {
673: cp2 = ++backup;
674: cp = str;
675: }
676: }
677: return(*cp == 0);
678: }
679:
680: /*
681: * See if the given string matches inside the subject field of the
682: * given message. For the purpose of the scan, we ignore case differences.
683: * If it does, return true. The string search argument is assumed to
684: * have the form "/search-string." If it is of the form "/," we use the
685: * previous search string.
686: */
687:
1.4 ! deraadt 688: char lastscan[STRINGLEN];
1.1 deraadt 689: int
690: matchsubj(str, mesg)
691: char *str;
692: int mesg;
693: {
694: register struct message *mp;
695: register char *cp, *cp2, *backup;
696:
697: str++;
698: if (strlen(str) == 0)
699: str = lastscan;
1.4 ! deraadt 700: else {
! 701: strncpy(lastscan, str, sizeof lastscan-1);
! 702: lastscan[sizeof lastscan-1] = '\0';
! 703: }
! 704:
1.1 deraadt 705: mp = &message[mesg-1];
706:
707: /*
708: * Now look, ignoring case, for the word in the string.
709: */
710:
1.3 millert 711: if (value("searchheaders") && (cp = strchr(str, ':'))) {
1.1 deraadt 712: *cp++ = '\0';
713: cp2 = hfield(str, mp);
714: cp[-1] = ':';
715: str = cp;
716: } else {
717: cp = str;
718: cp2 = hfield("subject", mp);
719: }
720: if (cp2 == NOSTR)
721: return(0);
722: backup = cp2;
723: while (*cp2) {
724: if (*cp == 0)
725: return(1);
726: if (raise(*cp++) != raise(*cp2++)) {
727: cp2 = ++backup;
728: cp = str;
729: }
730: }
731: return(*cp == 0);
732: }
733:
734: /*
735: * Mark the named message by setting its mark bit.
736: */
737: void
738: mark(mesg)
739: int mesg;
740: {
741: register int i;
742:
743: i = mesg;
744: if (i < 1 || i > msgCount)
745: panic("Bad message number to mark");
746: message[i-1].m_flag |= MMARK;
747: }
748:
749: /*
750: * Unmark the named message.
751: */
752: void
753: unmark(mesg)
754: int mesg;
755: {
756: register int i;
757:
758: i = mesg;
759: if (i < 1 || i > msgCount)
760: panic("Bad message number to unmark");
761: message[i-1].m_flag &= ~MMARK;
762: }
763:
764: /*
765: * Return the message number corresponding to the passed meta character.
766: */
767: int
768: metamess(meta, f)
769: int meta, f;
770: {
771: register int c, m;
772: register struct message *mp;
773:
774: c = meta;
775: switch (c) {
776: case '^':
777: /*
778: * First 'good' message left.
779: */
780: for (mp = &message[0]; mp < &message[msgCount]; mp++)
781: if ((mp->m_flag & MDELETED) == f)
782: return(mp - &message[0] + 1);
783: printf("No applicable messages\n");
784: return(-1);
785:
786: case '$':
787: /*
788: * Last 'good message left.
789: */
790: for (mp = &message[msgCount-1]; mp >= &message[0]; mp--)
791: if ((mp->m_flag & MDELETED) == f)
792: return(mp - &message[0] + 1);
793: printf("No applicable messages\n");
794: return(-1);
795:
796: case '.':
797: /*
798: * Current message.
799: */
800: m = dot - &message[0] + 1;
801: if ((dot->m_flag & MDELETED) != f) {
802: printf("%d: Inappropriate message\n", m);
803: return(-1);
804: }
805: return(m);
806:
807: default:
808: printf("Unknown metachar (%c)\n", c);
809: return(-1);
810: }
811: }