[BACK]Return to list.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mail

Annotation of src/usr.bin/mail/list.c, Revision 1.8

1.8     ! millert     1: /*     $OpenBSD: list.c,v 1.7 1997/07/31 02:36:33 millert Exp $        */
1.5       millert     2: /*     $NetBSD: list.c,v 1.7 1997/07/09 05:23:36 mikel Exp $   */
1.2       deraadt     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
1.5       millert    39: static char sccsid[] = "@(#)list.c     8.4 (Berkeley) 5/1/95";
1.2       deraadt    40: #else
1.8     ! millert    41: static char rcsid[] = "$OpenBSD: list.c,v 1.7 1997/07/31 02:36:33 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;
1.5       millert    71:                return(0);
1.1       deraadt    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) {
1.5       millert   148:                                puts("No numbers mixed with *");
1.1       deraadt   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) {
1.5       millert   175:                                puts("Non-numeric second argument");
1.1       deraadt   176:                                return(-1);
                    177:                        }
                    178:                        i = valdot;
                    179:                        do {
                    180:                                i++;
                    181:                                if (i > msgCount) {
1.5       millert   182:                                        puts("Referencing beyond EOF");
1.1       deraadt   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) {
1.5       millert   195:                                                puts("Referencing before 1");
1.1       deraadt   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) {
1.5       millert   205:                                puts("Non-numeric second argument");
1.1       deraadt   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) {
1.5       millert   232:                                puts("Can't mix \"*\" with anything");
1.1       deraadt   233:                                return(-1);
                    234:                        }
                    235:                        star++;
                    236:                        break;
                    237:
                    238:                case TERROR:
1.5       millert   239:                        return(-1);
1.1       deraadt   240:                }
                    241:                tok = scan(&bufp);
                    242:        }
                    243:        lastcolmod = colmod;
1.6       millert   244:        *np = NULL;
1.1       deraadt   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) {
1.5       millert   253:                        puts("No applicable messages.");
1.1       deraadt   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++) {
1.6       millert   277:                        for (mc = 0, np = &namelist[0]; *np != NULL; np++)
1.1       deraadt   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]);
1.6       millert   307:                        for (np = &namelist[1]; *np != NULL; np++)
1.1       deraadt   308:                                printf(", %s", *np);
1.5       millert   309:                        puts("}");
1.1       deraadt   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:
1.5       millert   337:                        fputs("No messages satisfy", stdout);
1.1       deraadt   338:                        for (colp = &coltab[0]; colp->co_char; colp++)
                    339:                                if (colp->co_bit & colmod)
                    340:                                        printf(" :%c", colp->co_char);
1.5       millert   341:                        putchar('\n');
1.1       deraadt   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;
1.7       millert   401:        char *linebuf;
                    402:        size_t linebufsize = BUFSIZ;
                    403:
                    404:        if ((linebuf = (char *)malloc(linebufsize)) == NULL)
                    405:                panic("Out of memory");
1.1       deraadt   406:
                    407:        argn = 0;
                    408:        cp = line;
                    409:        for (;;) {
                    410:                for (; *cp == ' ' || *cp == '\t'; cp++)
                    411:                        ;
                    412:                if (*cp == '\0')
                    413:                        break;
                    414:                if (argn >= argc - 1) {
1.5       millert   415:                        puts("Too many elements in the list; excess discarded.");
1.1       deraadt   416:                        break;
                    417:                }
                    418:                cp2 = linebuf;
                    419:                quotec = '\0';
                    420:                while ((c = *cp) != '\0') {
1.7       millert   421:                        /* Alloc more space if necessary */
                    422:                        if (cp2 - linebuf == linebufsize - 1) {
                    423:                                linebufsize += BUFSIZ;
1.8     ! millert   424:                                linebuf = (char *)realloc(linebuf, linebufsize);
        !           425:                                if (linebuf == NULL)
1.7       millert   426:                                        panic("Out of memory");
1.8     ! millert   427:                                cp2 = linebuf + linebufsize - BUFSIZ - 1;
1.7       millert   428:                        }
1.1       deraadt   429:                        cp++;
                    430:                        if (quotec != '\0') {
                    431:                                if (c == quotec)
                    432:                                        quotec = '\0';
                    433:                                else if (c == '\\')
                    434:                                        switch (c = *cp++) {
                    435:                                        case '\0':
                    436:                                                *cp2++ = '\\';
                    437:                                                cp--;
                    438:                                                break;
                    439:                                        case '0': case '1': case '2': case '3':
                    440:                                        case '4': case '5': case '6': case '7':
                    441:                                                c -= '0';
                    442:                                                if (*cp >= '0' && *cp <= '7')
                    443:                                                        c = c * 8 + *cp++ - '0';
                    444:                                                if (*cp >= '0' && *cp <= '7')
                    445:                                                        c = c * 8 + *cp++ - '0';
                    446:                                                *cp2++ = c;
                    447:                                                break;
                    448:                                        case 'b':
                    449:                                                *cp2++ = '\b';
                    450:                                                break;
                    451:                                        case 'f':
                    452:                                                *cp2++ = '\f';
                    453:                                                break;
                    454:                                        case 'n':
                    455:                                                *cp2++ = '\n';
                    456:                                                break;
                    457:                                        case 'r':
                    458:                                                *cp2++ = '\r';
                    459:                                                break;
                    460:                                        case 't':
                    461:                                                *cp2++ = '\t';
                    462:                                                break;
                    463:                                        case 'v':
                    464:                                                *cp2++ = '\v';
                    465:                                                break;
                    466:                                        default:
                    467:                                                *cp2++ = c;
                    468:                                        }
                    469:                                else if (c == '^') {
                    470:                                        c = *cp++;
                    471:                                        if (c == '?')
                    472:                                                *cp2++ = '\177';
                    473:                                        /* null doesn't show up anyway */
1.2       deraadt   474:                                        else if ((c >= 'A' && c <= '_') ||
                    475:                                                 (c >= 'a' && c <= 'z'))
1.1       deraadt   476:                                                *cp2++ = c & 037;
                    477:                                        else {
                    478:                                                *cp2++ = '^';
                    479:                                                cp--;
                    480:                                        }
                    481:                                } else
                    482:                                        *cp2++ = c;
                    483:                        } else if (c == '"' || c == '\'')
                    484:                                quotec = c;
                    485:                        else if (c == ' ' || c == '\t')
                    486:                                break;
                    487:                        else
                    488:                                *cp2++ = c;
                    489:                }
                    490:                *cp2 = '\0';
                    491:                argv[argn++] = savestr(linebuf);
                    492:        }
1.6       millert   493:        argv[argn] = NULL;
1.7       millert   494:        (void)free(linebuf);
1.5       millert   495:        return(argn);
1.1       deraadt   496: }
                    497:
                    498: /*
                    499:  * scan out a single lexical item and return its token number,
                    500:  * updating the string pointer passed **p.  Also, store the value
                    501:  * of the number or string scanned in lexnumber or lexstring as
                    502:  * appropriate.  In any event, store the scanned `thing' in lexstring.
                    503:  */
                    504:
                    505: struct lex {
                    506:        char    l_char;
                    507:        char    l_token;
                    508: } singles[] = {
1.2       deraadt   509:        { '$',  TDOLLAR },
                    510:        { '.',  TDOT },
                    511:        { '^',  TUP },
                    512:        { '*',  TSTAR },
                    513:        { '-',  TDASH },
                    514:        { '+',  TPLUS },
                    515:        { '(',  TOPEN },
                    516:        { ')',  TCLOSE },
                    517:        { 0,    0 }
1.1       deraadt   518: };
                    519:
                    520: int
                    521: scan(sp)
                    522:        char **sp;
                    523: {
                    524:        register char *cp, *cp2;
                    525:        register int c;
                    526:        register struct lex *lp;
                    527:        int quotec;
                    528:
                    529:        if (regretp >= 0) {
                    530:                strcpy(lexstring, string_stack[regretp]);
                    531:                lexnumber = numberstack[regretp];
                    532:                return(regretstack[regretp--]);
                    533:        }
                    534:        cp = *sp;
                    535:        cp2 = lexstring;
                    536:        c = *cp++;
                    537:
                    538:        /*
                    539:         * strip away leading white space.
                    540:         */
                    541:
                    542:        while (c == ' ' || c == '\t')
                    543:                c = *cp++;
                    544:
                    545:        /*
                    546:         * If no characters remain, we are at end of line,
                    547:         * so report that.
                    548:         */
                    549:
                    550:        if (c == '\0') {
                    551:                *sp = --cp;
                    552:                return(TEOL);
                    553:        }
                    554:
                    555:        /*
                    556:         * If the leading character is a digit, scan
                    557:         * the number and convert it on the fly.
                    558:         * Return TNUMBER when done.
                    559:         */
                    560:
                    561:        if (isdigit(c)) {
                    562:                lexnumber = 0;
                    563:                while (isdigit(c)) {
                    564:                        lexnumber = lexnumber*10 + c - '0';
                    565:                        *cp2++ = c;
                    566:                        c = *cp++;
                    567:                }
                    568:                *cp2 = '\0';
                    569:                *sp = --cp;
                    570:                return(TNUMBER);
                    571:        }
                    572:
                    573:        /*
                    574:         * Check for single character tokens; return such
                    575:         * if found.
                    576:         */
                    577:
                    578:        for (lp = &singles[0]; lp->l_char != 0; lp++)
                    579:                if (c == lp->l_char) {
                    580:                        lexstring[0] = c;
                    581:                        lexstring[1] = '\0';
                    582:                        *sp = cp;
                    583:                        return(lp->l_token);
                    584:                }
                    585:
                    586:        /*
                    587:         * We've got a string!  Copy all the characters
                    588:         * of the string into lexstring, until we see
                    589:         * a null, space, or tab.
                    590:         * If the lead character is a " or ', save it
                    591:         * and scan until you get another.
                    592:         */
                    593:
                    594:        quotec = 0;
                    595:        if (c == '\'' || c == '"') {
                    596:                quotec = c;
                    597:                c = *cp++;
                    598:        }
                    599:        while (c != '\0') {
                    600:                if (c == quotec) {
                    601:                        cp++;
                    602:                        break;
                    603:                }
                    604:                if (quotec == 0 && (c == ' ' || c == '\t'))
                    605:                        break;
                    606:                if (cp2 - lexstring < STRINGLEN-1)
                    607:                        *cp2++ = c;
                    608:                c = *cp++;
                    609:        }
                    610:        if (quotec && c == 0) {
                    611:                fprintf(stderr, "Missing %c\n", quotec);
1.5       millert   612:                return(TERROR);
1.1       deraadt   613:        }
                    614:        *sp = --cp;
                    615:        *cp2 = '\0';
                    616:        return(TSTRING);
                    617: }
                    618:
                    619: /*
                    620:  * Unscan the named token by pushing it onto the regret stack.
                    621:  */
                    622: void
                    623: regret(token)
                    624:        int token;
                    625: {
                    626:        if (++regretp >= REGDEP)
                    627:                panic("Too many regrets");
                    628:        regretstack[regretp] = token;
                    629:        lexstring[STRINGLEN-1] = '\0';
                    630:        string_stack[regretp] = savestr(lexstring);
                    631:        numberstack[regretp] = lexnumber;
                    632: }
                    633:
                    634: /*
                    635:  * Reset all the scanner global variables.
                    636:  */
                    637: void
                    638: scaninit()
                    639: {
                    640:        regretp = -1;
                    641: }
                    642:
                    643: /*
                    644:  * Find the first message whose flags & m == f  and return
                    645:  * its message number.
                    646:  */
                    647: int
                    648: first(f, m)
                    649:        int f, m;
                    650: {
                    651:        register struct message *mp;
                    652:
                    653:        if (msgCount == 0)
1.5       millert   654:                return(0);
1.1       deraadt   655:        f &= MDELETED;
                    656:        m &= MDELETED;
                    657:        for (mp = dot; mp < &message[msgCount]; mp++)
                    658:                if ((mp->m_flag & m) == f)
1.5       millert   659:                        return(mp - message + 1);
1.1       deraadt   660:        for (mp = dot-1; mp >= &message[0]; mp--)
                    661:                if ((mp->m_flag & m) == f)
1.5       millert   662:                        return(mp - message + 1);
                    663:        return(0);
1.1       deraadt   664: }
                    665:
                    666: /*
                    667:  * See if the passed name sent the passed message number.  Return true
                    668:  * if so.
                    669:  */
                    670: int
                    671: matchsender(str, mesg)
                    672:        char *str;
                    673:        int mesg;
                    674: {
                    675:        register char *cp, *cp2, *backup;
                    676:
                    677:        if (!*str)      /* null string matches nothing instead of everything */
1.5       millert   678:                return(0);
1.1       deraadt   679:        backup = cp2 = nameof(&message[mesg - 1], 0);
                    680:        cp = str;
                    681:        while (*cp2) {
                    682:                if (*cp == 0)
                    683:                        return(1);
                    684:                if (raise(*cp++) != raise(*cp2++)) {
                    685:                        cp2 = ++backup;
                    686:                        cp = str;
                    687:                }
                    688:        }
                    689:        return(*cp == 0);
                    690: }
                    691:
                    692: /*
1.5       millert   693:  * See if the passed name received the passed message number.  Return true
                    694:  * if so.
                    695:  */
                    696:
                    697: static char *to_fields[] = { "to", "cc", "bcc", NULL };
                    698:
                    699: int
                    700: matchto(str, mesg)
                    701:        char *str;
                    702: {
                    703:        register struct message *mp;
                    704:        register char *cp, *cp2, *backup, **to;
                    705:
                    706:        str++;
                    707:
                    708:        if (*str == 0)  /* null string matches nothing instead of everything */
                    709:                return(0);
                    710:
                    711:        mp = &message[mesg-1];
                    712:
                    713:        for (to = to_fields; *to; to++) {
                    714:                cp = str;
                    715:                cp2 = hfield(*to, mp);
1.6       millert   716:                if (cp2 != NULL) {
1.5       millert   717:                        backup = cp2;
                    718:                        while (*cp2) {
                    719:                                if (*cp == 0)
                    720:                                        return(1);
                    721:                                if (raise(*cp++) != raise(*cp2++)) {
                    722:                                        cp2 = ++backup;
                    723:                                        cp = str;
                    724:                                }
                    725:                        }
                    726:                        if (*cp == 0)
                    727:                                return(1);
                    728:                }
                    729:        }
                    730:        return(0);
                    731: }
                    732:
                    733: /*
1.1       deraadt   734:  * See if the given string matches inside the subject field of the
                    735:  * given message.  For the purpose of the scan, we ignore case differences.
                    736:  * If it does, return true.  The string search argument is assumed to
                    737:  * have the form "/search-string."  If it is of the form "/," we use the
                    738:  * previous search string.
                    739:  */
                    740:
1.4       deraadt   741: char lastscan[STRINGLEN];
1.1       deraadt   742: int
                    743: matchsubj(str, mesg)
                    744:        char *str;
                    745:        int mesg;
                    746: {
                    747:        register struct message *mp;
                    748:        register char *cp, *cp2, *backup;
                    749:
                    750:        str++;
1.5       millert   751:        if (*str == '\0')
1.1       deraadt   752:                str = lastscan;
1.4       deraadt   753:        else {
1.5       millert   754:                strncpy(lastscan, str, sizeof(lastscan) - 1);
                    755:                lastscan[sizeof(lastscan) - 1] = '\0';
1.4       deraadt   756:        }
1.1       deraadt   757:        mp = &message[mesg-1];
                    758:
                    759:        /*
                    760:         * Now look, ignoring case, for the word in the string.
                    761:         */
                    762:
1.3       millert   763:        if (value("searchheaders") && (cp = strchr(str, ':'))) {
1.5       millert   764:                /* Check for special case "/To:" */
                    765:                if (raise(str[0]) == 'T' && raise(str[1]) == 'O' &&
                    766:                    str[2] == ':')
                    767:                        return(matchto(cp, mesg));
1.1       deraadt   768:                *cp++ = '\0';
1.5       millert   769:                cp2 = hfield(*str ? str : "subject", mp);
1.1       deraadt   770:                cp[-1] = ':';
                    771:                str = cp;
                    772:        } else {
                    773:                cp = str;
                    774:                cp2 = hfield("subject", mp);
                    775:        }
1.6       millert   776:        if (cp2 == NULL)
1.1       deraadt   777:                return(0);
                    778:        backup = cp2;
                    779:        while (*cp2) {
                    780:                if (*cp == 0)
                    781:                        return(1);
                    782:                if (raise(*cp++) != raise(*cp2++)) {
                    783:                        cp2 = ++backup;
                    784:                        cp = str;
                    785:                }
                    786:        }
                    787:        return(*cp == 0);
                    788: }
                    789:
                    790: /*
                    791:  * Mark the named message by setting its mark bit.
                    792:  */
                    793: void
                    794: mark(mesg)
                    795:        int mesg;
                    796: {
                    797:        register int i;
                    798:
                    799:        i = mesg;
                    800:        if (i < 1 || i > msgCount)
                    801:                panic("Bad message number to mark");
                    802:        message[i-1].m_flag |= MMARK;
                    803: }
                    804:
                    805: /*
                    806:  * Unmark the named message.
                    807:  */
                    808: void
                    809: unmark(mesg)
                    810:        int mesg;
                    811: {
                    812:        register int i;
                    813:
                    814:        i = mesg;
                    815:        if (i < 1 || i > msgCount)
                    816:                panic("Bad message number to unmark");
                    817:        message[i-1].m_flag &= ~MMARK;
                    818: }
                    819:
                    820: /*
                    821:  * Return the message number corresponding to the passed meta character.
                    822:  */
                    823: int
                    824: metamess(meta, f)
                    825:        int meta, f;
                    826: {
                    827:        register int c, m;
                    828:        register struct message *mp;
                    829:
                    830:        c = meta;
                    831:        switch (c) {
                    832:        case '^':
                    833:                /*
                    834:                 * First 'good' message left.
                    835:                 */
                    836:                for (mp = &message[0]; mp < &message[msgCount]; mp++)
                    837:                        if ((mp->m_flag & MDELETED) == f)
                    838:                                return(mp - &message[0] + 1);
1.5       millert   839:                puts("No applicable messages");
1.1       deraadt   840:                return(-1);
                    841:
                    842:        case '$':
                    843:                /*
                    844:                 * Last 'good message left.
                    845:                 */
                    846:                for (mp = &message[msgCount-1]; mp >= &message[0]; mp--)
                    847:                        if ((mp->m_flag & MDELETED) == f)
                    848:                                return(mp - &message[0] + 1);
1.5       millert   849:                puts("No applicable messages");
1.1       deraadt   850:                return(-1);
                    851:
                    852:        case '.':
1.5       millert   853:                /*
1.1       deraadt   854:                 * Current message.
                    855:                 */
                    856:                m = dot - &message[0] + 1;
                    857:                if ((dot->m_flag & MDELETED) != f) {
                    858:                        printf("%d: Inappropriate message\n", m);
                    859:                        return(-1);
                    860:                }
                    861:                return(m);
                    862:
                    863:        default:
                    864:                printf("Unknown metachar (%c)\n", c);
                    865:                return(-1);
                    866:        }
                    867: }