10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
52931Sas145665 * Common Development and Distribution License (the "License").
62931Sas145665 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate
220Sstevel@tonic-gate /*
23*7418SViswanathan.Kannappan@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
2718Srobbin /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
2818Srobbin /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate * The Regents of the University of California
330Sstevel@tonic-gate * All Rights Reserved
340Sstevel@tonic-gate *
350Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate * contributors.
380Sstevel@tonic-gate */
390Sstevel@tonic-gate
400Sstevel@tonic-gate #include "rcv.h"
410Sstevel@tonic-gate #include <locale.h>
420Sstevel@tonic-gate
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate * mailx -- a modified version of a University of California at Berkeley
450Sstevel@tonic-gate * mail program
460Sstevel@tonic-gate *
470Sstevel@tonic-gate * More user commands.
480Sstevel@tonic-gate */
490Sstevel@tonic-gate
500Sstevel@tonic-gate static int igshow(void);
510Sstevel@tonic-gate static int igcomp(const void *l, const void *r);
520Sstevel@tonic-gate static int save1(char str[], int mark);
530Sstevel@tonic-gate static int Save1(int *msgvec, int mark);
540Sstevel@tonic-gate static void savemsglist(char *file, int *msgvec, int flag);
550Sstevel@tonic-gate static int put1(char str[], int doign);
560Sstevel@tonic-gate static int svputs(const char *line, FILE *obuf);
570Sstevel@tonic-gate static int wrputs(const char *line, FILE *obuf);
580Sstevel@tonic-gate static int retshow(void);
590Sstevel@tonic-gate
600Sstevel@tonic-gate /* flags for savemsglist() */
610Sstevel@tonic-gate #define S_MARK 1 /* mark the message as saved */
620Sstevel@tonic-gate #define S_NOHEADER 2 /* don't write out the header */
630Sstevel@tonic-gate #define S_SAVING 4 /* doing save/copy */
640Sstevel@tonic-gate #define S_NOIGNORE 8 /* don't do ignore processing */
650Sstevel@tonic-gate
660Sstevel@tonic-gate /*
672931Sas145665 * If any arguments were given, print the first message
682931Sas145665 * identified by the first argument. If no arguments are given,
692931Sas145665 * print the next applicable message after dot.
700Sstevel@tonic-gate */
710Sstevel@tonic-gate
722931Sas145665 int
next(int * msgvec)730Sstevel@tonic-gate next(int *msgvec)
740Sstevel@tonic-gate {
750Sstevel@tonic-gate register struct message *mp;
762931Sas145665 int list[2];
770Sstevel@tonic-gate
780Sstevel@tonic-gate if (*msgvec != NULL) {
792931Sas145665 if (*msgvec < 0) {
802931Sas145665 printf((gettext("Negative message given\n")));
812931Sas145665 return (1);
822931Sas145665 }
832931Sas145665 mp = &message[*msgvec - 1];
842931Sas145665 if ((mp->m_flag & MDELETED) == 0) {
852931Sas145665 dot = mp;
862931Sas145665 goto hitit;
872931Sas145665 }
882931Sas145665 printf(gettext("No applicable message\n"));
892931Sas145665 return (1);
900Sstevel@tonic-gate }
910Sstevel@tonic-gate
920Sstevel@tonic-gate /*
930Sstevel@tonic-gate * If this is the first command, select message 1.
940Sstevel@tonic-gate * Note that this must exist for us to get here at all.
950Sstevel@tonic-gate */
960Sstevel@tonic-gate if (!sawcom)
970Sstevel@tonic-gate goto hitit;
980Sstevel@tonic-gate
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate * Just find the next good message after dot, no
1010Sstevel@tonic-gate * wraparound.
1020Sstevel@tonic-gate */
1030Sstevel@tonic-gate for (mp = dot+1; mp < &message[msgCount]; mp++)
1040Sstevel@tonic-gate if ((mp->m_flag & (MDELETED|MSAVED)) == 0)
1050Sstevel@tonic-gate break;
1060Sstevel@tonic-gate if (mp >= &message[msgCount]) {
1070Sstevel@tonic-gate printf(gettext("At EOF\n"));
1082931Sas145665 return (0);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate dot = mp;
1110Sstevel@tonic-gate hitit:
1120Sstevel@tonic-gate /*
1130Sstevel@tonic-gate * Print dot.
1140Sstevel@tonic-gate */
1150Sstevel@tonic-gate list[0] = dot - &message[0] + 1;
1160Sstevel@tonic-gate list[1] = NULL;
1172931Sas145665 return (type(list));
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate /*
1210Sstevel@tonic-gate * Save a message in a file. Mark the message as saved
1220Sstevel@tonic-gate * so we can discard when the user quits.
1230Sstevel@tonic-gate */
1242931Sas145665 int
save(char str[])1250Sstevel@tonic-gate save(char str[])
1260Sstevel@tonic-gate {
1272931Sas145665 return (save1(str, S_MARK));
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate /*
1310Sstevel@tonic-gate * Copy a message to a file without affected its saved-ness
1320Sstevel@tonic-gate */
1332931Sas145665 int
copycmd(char str[])1340Sstevel@tonic-gate copycmd(char str[])
1350Sstevel@tonic-gate {
1362931Sas145665 return (save1(str, 0));
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate /*
1400Sstevel@tonic-gate * Save/copy the indicated messages at the end of the passed file name.
1410Sstevel@tonic-gate * If mark is true, mark the message "saved."
1420Sstevel@tonic-gate */
1432931Sas145665 static int
save1(char str[],int mark)1440Sstevel@tonic-gate save1(char str[], int mark)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate char *file, *cmd;
1470Sstevel@tonic-gate int f, *msgvec;
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate cmd = mark ? "save" : "copy";
1502931Sas145665 msgvec = (int *)salloc((msgCount + 2) * sizeof (*msgvec));
1510Sstevel@tonic-gate if ((file = snarf(str, &f, 0)) == NOSTR)
1520Sstevel@tonic-gate file = Getf("MBOX");
1532931Sas145665 if (f == -1)
1542931Sas145665 return (1);
1550Sstevel@tonic-gate if (!f) {
1560Sstevel@tonic-gate *msgvec = first(0, MMNORM);
1570Sstevel@tonic-gate if (*msgvec == NULL) {
1580Sstevel@tonic-gate printf(gettext("No messages to %s.\n"), cmd);
1592931Sas145665 return (1);
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate msgvec[1] = NULL;
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate if (f && getmsglist(str, msgvec, 0) < 0)
1642931Sas145665 return (1);
1650Sstevel@tonic-gate if ((file = expand(file)) == NOSTR)
1662931Sas145665 return (1);
1670Sstevel@tonic-gate savemsglist(file, msgvec, mark | S_SAVING);
1682931Sas145665 return (0);
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate
1712931Sas145665 int
Save(int * msgvec)1720Sstevel@tonic-gate Save(int *msgvec)
1730Sstevel@tonic-gate {
1742931Sas145665 return (Save1(msgvec, S_MARK));
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate
1772931Sas145665 int
Copy(int * msgvec)1780Sstevel@tonic-gate Copy(int *msgvec)
1790Sstevel@tonic-gate {
1802931Sas145665 return (Save1(msgvec, 0));
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate /*
1840Sstevel@tonic-gate * save/copy the indicated messages at the end of a file named
1850Sstevel@tonic-gate * by the sender of the first message in the msglist.
1860Sstevel@tonic-gate */
1872931Sas145665 static int
Save1(int * msgvec,int mark)1880Sstevel@tonic-gate Save1(int *msgvec, int mark)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate register char *from;
1910Sstevel@tonic-gate char recfile[BUFSIZ];
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate #ifdef notdef
1940Sstevel@tonic-gate from = striphosts(nameof(&message[*msgvec-1], 0));
1950Sstevel@tonic-gate #else
1960Sstevel@tonic-gate from = nameof(&message[*msgvec-1]);
1970Sstevel@tonic-gate #endif
1980Sstevel@tonic-gate getrecf(from, recfile, 1, sizeof (recfile));
1990Sstevel@tonic-gate if (*recfile != '\0')
2000Sstevel@tonic-gate savemsglist(safeexpand(recfile), msgvec, mark | S_SAVING);
2012931Sas145665 return (0);
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate
2042931Sas145665 int
sput(char str[])2050Sstevel@tonic-gate sput(char str[])
2060Sstevel@tonic-gate {
2072931Sas145665 return (put1(str, 0));
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate
2102931Sas145665 int
Sput(char str[])2110Sstevel@tonic-gate Sput(char str[])
2120Sstevel@tonic-gate {
2132931Sas145665 return (put1(str, S_NOIGNORE));
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate /*
2170Sstevel@tonic-gate * Put the indicated messages at the end of the passed file name.
2180Sstevel@tonic-gate */
2192931Sas145665 static int
put1(char str[],int doign)2200Sstevel@tonic-gate put1(char str[], int doign)
2210Sstevel@tonic-gate {
2220Sstevel@tonic-gate char *file;
2230Sstevel@tonic-gate int f, *msgvec;
2240Sstevel@tonic-gate
2252931Sas145665 msgvec = (int *)salloc((msgCount + 2) * sizeof (*msgvec));
2260Sstevel@tonic-gate if ((file = snarf(str, &f, 0)) == NOSTR)
2270Sstevel@tonic-gate file = Getf("MBOX");
2282931Sas145665 if (f == -1)
2292931Sas145665 return (1);
2300Sstevel@tonic-gate if (!f) {
2310Sstevel@tonic-gate *msgvec = first(0, MMNORM);
2320Sstevel@tonic-gate if (*msgvec == NULL) {
2330Sstevel@tonic-gate printf(gettext("No messages to put.\n"));
2342931Sas145665 return (1);
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate msgvec[1] = NULL;
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate if (f && getmsglist(str, msgvec, 0) < 0)
2392931Sas145665 return (1);
2400Sstevel@tonic-gate if ((file = expand(file)) == NOSTR)
2412931Sas145665 return (1);
2420Sstevel@tonic-gate savemsglist(file, msgvec, doign);
2432931Sas145665 return (0);
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate /*
2470Sstevel@tonic-gate * save a message list in a file.
2480Sstevel@tonic-gate * if wr set, doing "write" instead
2490Sstevel@tonic-gate * of "save" or "copy" so don't put
2500Sstevel@tonic-gate * out header.
2510Sstevel@tonic-gate */
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate static int wr_linecount; /* count of lines written */
2540Sstevel@tonic-gate static int wr_charcount; /* char count of lines written */
2550Sstevel@tonic-gate static int wr_inlines; /* count of lines read */
2560Sstevel@tonic-gate static long wr_maxlines; /* total lines in message */
2570Sstevel@tonic-gate static int wr_inhead; /* in header of message */
2580Sstevel@tonic-gate
2592931Sas145665 static void
savemsglist(char * file,int * msgvec,int flag)2600Sstevel@tonic-gate savemsglist(char *file, int *msgvec, int flag)
2610Sstevel@tonic-gate {
2620Sstevel@tonic-gate register int *ip, mesg;
2630Sstevel@tonic-gate register struct message *mp;
2640Sstevel@tonic-gate char *disp;
2650Sstevel@tonic-gate FILE *obuf;
2660Sstevel@tonic-gate struct stat statb;
2670Sstevel@tonic-gate long lc, cc, t;
2680Sstevel@tonic-gate int bnry, mflag;
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate printf("\"%s\" ", file);
2710Sstevel@tonic-gate flush();
2720Sstevel@tonic-gate if (stat(file, &statb) >= 0)
2730Sstevel@tonic-gate disp = "[Appended]";
2740Sstevel@tonic-gate else
2750Sstevel@tonic-gate disp = "[New file]";
2760Sstevel@tonic-gate if ((obuf = fopen(file, "a")) == NULL) {
2770Sstevel@tonic-gate perror("");
2780Sstevel@tonic-gate return;
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate lc = cc = 0;
2810Sstevel@tonic-gate bnry = 0;
2820Sstevel@tonic-gate if (flag & S_SAVING)
2830Sstevel@tonic-gate mflag = (int)value("alwaysignore")?(M_IGNORE|M_SAVING):M_SAVING;
2840Sstevel@tonic-gate else if (flag & S_NOIGNORE)
2850Sstevel@tonic-gate mflag = 0;
2860Sstevel@tonic-gate else
2870Sstevel@tonic-gate mflag = M_IGNORE;
2880Sstevel@tonic-gate for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
2890Sstevel@tonic-gate mesg = *ip;
2900Sstevel@tonic-gate mp = &message[mesg-1];
2910Sstevel@tonic-gate if (!mp->m_text) {
2920Sstevel@tonic-gate bnry = 1;
2930Sstevel@tonic-gate }
2940Sstevel@tonic-gate wr_linecount = 0;
2950Sstevel@tonic-gate wr_charcount = 0;
2960Sstevel@tonic-gate if (flag & S_NOHEADER) {
2970Sstevel@tonic-gate wr_inhead = 1;
2980Sstevel@tonic-gate wr_maxlines = mp->m_lines;
2990Sstevel@tonic-gate wr_inlines = 0;
3000Sstevel@tonic-gate t = msend(mp, obuf, 0, wrputs);
3010Sstevel@tonic-gate } else {
3020Sstevel@tonic-gate t = msend(mp, obuf, mflag, svputs);
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate if (t < 0) {
3050Sstevel@tonic-gate perror(file);
3060Sstevel@tonic-gate fclose(obuf);
3070Sstevel@tonic-gate return;
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate touch(mesg);
3100Sstevel@tonic-gate dot = mp;
3110Sstevel@tonic-gate lc += wr_linecount;
3120Sstevel@tonic-gate cc += wr_charcount;
3130Sstevel@tonic-gate if (flag & S_MARK)
3140Sstevel@tonic-gate mp->m_flag |= MSAVED;
3150Sstevel@tonic-gate }
3160Sstevel@tonic-gate fflush(obuf);
3170Sstevel@tonic-gate if (fferror(obuf))
3180Sstevel@tonic-gate perror(file);
3190Sstevel@tonic-gate fclose(obuf);
3200Sstevel@tonic-gate if (!bnry) {
3210Sstevel@tonic-gate printf("%s %ld/%ld\n", disp, lc, cc);
3220Sstevel@tonic-gate } else {
3230Sstevel@tonic-gate printf("%s binary/%ld\n", disp, cc);
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate }
3260Sstevel@tonic-gate
3270Sstevel@tonic-gate static int
svputs(const char * line,FILE * obuf)3280Sstevel@tonic-gate svputs(const char *line, FILE *obuf)
3290Sstevel@tonic-gate {
3300Sstevel@tonic-gate wr_linecount++;
3310Sstevel@tonic-gate wr_charcount += strlen(line);
3322931Sas145665 return (fputs(line, obuf));
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate
3350Sstevel@tonic-gate static int
wrputs(const char * line,FILE * obuf)3360Sstevel@tonic-gate wrputs(const char *line, FILE *obuf)
3370Sstevel@tonic-gate {
3380Sstevel@tonic-gate /*
3390Sstevel@tonic-gate * If this is a header line or
3400Sstevel@tonic-gate * the last line, don't write it out. Since we may add a
3410Sstevel@tonic-gate * "Status" line the line count may be off by one so insist
3420Sstevel@tonic-gate * that the last line is blank before we skip it.
3430Sstevel@tonic-gate */
3440Sstevel@tonic-gate wr_inlines++;
3450Sstevel@tonic-gate if (wr_inhead) {
3460Sstevel@tonic-gate if (strcmp(line, "\n") == 0)
3470Sstevel@tonic-gate wr_inhead = 0;
3482931Sas145665 return (0);
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate if (wr_inlines >= wr_maxlines && strcmp(line, "\n") == 0)
3512931Sas145665 return (0);
3520Sstevel@tonic-gate wr_linecount++;
3530Sstevel@tonic-gate wr_charcount += strlen(line);
3542931Sas145665 return (fputs(line, obuf));
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate
3570Sstevel@tonic-gate /*
3580Sstevel@tonic-gate * Write the indicated messages at the end of the passed
3590Sstevel@tonic-gate * file name, minus header and trailing blank line.
3600Sstevel@tonic-gate */
3610Sstevel@tonic-gate
3622931Sas145665 int
swrite(char str[])3630Sstevel@tonic-gate swrite(char str[])
3640Sstevel@tonic-gate {
3650Sstevel@tonic-gate register char *file;
3660Sstevel@tonic-gate int f, *msgvec;
3670Sstevel@tonic-gate
3682931Sas145665 msgvec = (int *)salloc((msgCount + 2) * sizeof (*msgvec));
3690Sstevel@tonic-gate if ((file = snarf(str, &f, 1)) == NOSTR)
3702931Sas145665 return (1);
3712931Sas145665 if (f == -1)
3722931Sas145665 return (1);
3730Sstevel@tonic-gate if ((file = expand(file)) == NOSTR)
3742931Sas145665 return (1);
3750Sstevel@tonic-gate if (!f) {
3760Sstevel@tonic-gate *msgvec = first(0, MMNORM);
3770Sstevel@tonic-gate if (*msgvec == NULL) {
3780Sstevel@tonic-gate printf(gettext("No messages to write.\n"));
3792931Sas145665 return (1);
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate msgvec[1] = NULL;
3820Sstevel@tonic-gate }
3830Sstevel@tonic-gate if (f && getmsglist(str, msgvec, 0) < 0)
3842931Sas145665 return (1);
3850Sstevel@tonic-gate savemsglist(file, msgvec, S_MARK|S_NOHEADER);
3862931Sas145665 return (0);
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate /*
3900Sstevel@tonic-gate * Snarf the file from the end of the command line and
3910Sstevel@tonic-gate * return a pointer to it. If there is no file attached,
3920Sstevel@tonic-gate * just return NOSTR. Put a null in front of the file
3930Sstevel@tonic-gate * name so that the message list processing won't see it,
3940Sstevel@tonic-gate * unless the file name is the only thing on the line, in
3950Sstevel@tonic-gate * which case, return 0 in the reference flag variable.
3960Sstevel@tonic-gate */
3970Sstevel@tonic-gate
3980Sstevel@tonic-gate /*
3990Sstevel@tonic-gate * The following definitions are used to characterize the syntactic
4000Sstevel@tonic-gate * category of the preceding character in the following parse procedure.
4010Sstevel@tonic-gate * The variable pc_type assumes these values.
4020Sstevel@tonic-gate */
4030Sstevel@tonic-gate
4040Sstevel@tonic-gate #define SN_DELIM 1 /* Delimiter (<blank> or line beginning) */
4050Sstevel@tonic-gate #define SN_TOKEN 2 /* A part of a token */
4060Sstevel@tonic-gate #define SN_QUOTE 4 /* An entire quoted string (ie, "...") */
4070Sstevel@tonic-gate
4080Sstevel@tonic-gate char *
snarf(char linebuf[],int * flag,int erf)4090Sstevel@tonic-gate snarf(char linebuf[], int *flag, int erf)
4100Sstevel@tonic-gate {
4110Sstevel@tonic-gate register char *p; /* utility pointer */
4120Sstevel@tonic-gate register char qc; /* quotation character to match */
4130Sstevel@tonic-gate register unsigned int pc_type; /* preceding character type */
4140Sstevel@tonic-gate register char *tok_beg; /* beginning of last token */
4150Sstevel@tonic-gate register char *tok_end; /* end of last token */
4160Sstevel@tonic-gate char *line_beg; /* beginning of line, after */
4170Sstevel@tonic-gate /* leading whitespace */
4180Sstevel@tonic-gate
4192931Sas145665 /*
4200Sstevel@tonic-gate * Skip leading whitespace.
4210Sstevel@tonic-gate */
4220Sstevel@tonic-gate for (line_beg = linebuf;
423*7418SViswanathan.Kannappan@Sun.COM *line_beg && any(*line_beg, " \t");
424*7418SViswanathan.Kannappan@Sun.COM line_beg++) {
4250Sstevel@tonic-gate /* empty body */
4260Sstevel@tonic-gate }
4270Sstevel@tonic-gate if (!*line_beg) {
4280Sstevel@tonic-gate if (erf) {
429*7418SViswanathan.Kannappan@Sun.COM printf(gettext("No file specified.\n"));
4300Sstevel@tonic-gate }
4310Sstevel@tonic-gate *flag = 0;
4322931Sas145665 return (NOSTR);
4330Sstevel@tonic-gate }
4340Sstevel@tonic-gate /*
4350Sstevel@tonic-gate * Process line from left-to-right, 1 char at a time.
4360Sstevel@tonic-gate */
4372931Sas145665 pc_type = SN_DELIM;
4382931Sas145665 tok_beg = tok_end = NOSTR;
4392931Sas145665 p = line_beg;
4402931Sas145665 while (*p != '\0') {
4410Sstevel@tonic-gate if (any(*p, " \t")) {
4420Sstevel@tonic-gate /* This character is a DELIMITER */
4430Sstevel@tonic-gate if (pc_type & (SN_TOKEN|SN_QUOTE)) {
4440Sstevel@tonic-gate tok_end = p - 1;
4450Sstevel@tonic-gate }
4460Sstevel@tonic-gate pc_type = SN_DELIM;
4470Sstevel@tonic-gate p++;
4480Sstevel@tonic-gate } else if ((qc = *p) == '"' || qc == '\'') {
4490Sstevel@tonic-gate /* This character is a QUOTE character */
4500Sstevel@tonic-gate if (pc_type == SN_TOKEN) {
4510Sstevel@tonic-gate /* embedded quotation symbols are simply */
4520Sstevel@tonic-gate /* token characters. */
4530Sstevel@tonic-gate p++;
4540Sstevel@tonic-gate continue;
4550Sstevel@tonic-gate }
4560Sstevel@tonic-gate /* Search for the matching QUOTE character */
4570Sstevel@tonic-gate for (tok_beg = p, tok_end = NOSTR, p++;
4582931Sas145665 *p != '\0' && *p != qc;
459*7418SViswanathan.Kannappan@Sun.COM p++) {
4600Sstevel@tonic-gate if (*p == '\\' && *(p+1) == qc) {
4610Sstevel@tonic-gate p++;
4620Sstevel@tonic-gate }
4630Sstevel@tonic-gate }
4640Sstevel@tonic-gate if (*p == '\0') {
4650Sstevel@tonic-gate printf(gettext("Syntax error: missing "
4662931Sas145665 "%c.\n"), qc);
4670Sstevel@tonic-gate *flag = -1;
4682931Sas145665 return (NOSTR);
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate tok_end = p;
4710Sstevel@tonic-gate pc_type = SN_QUOTE;
4720Sstevel@tonic-gate p++;
4730Sstevel@tonic-gate } else {
4740Sstevel@tonic-gate /* This character should be a TOKEN character */
4750Sstevel@tonic-gate if (pc_type & (SN_DELIM|SN_TOKEN)) {
4760Sstevel@tonic-gate if (pc_type & SN_DELIM) {
4770Sstevel@tonic-gate tok_beg = p;
4780Sstevel@tonic-gate tok_end = NOSTR;
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate } else {
4810Sstevel@tonic-gate printf(gettext("improper quotes"
4822931Sas145665 " at \"%s\".\n"), p);
4830Sstevel@tonic-gate *flag = -1;
4842931Sas145665 return (NOSTR);
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate if (*p == '\\' && *++p == '\0') {
4870Sstevel@tonic-gate printf(gettext("\'\\\' at "
4882931Sas145665 "end of line.\n"));
4890Sstevel@tonic-gate *flag = -1;
4902931Sas145665 return (NOSTR);
4910Sstevel@tonic-gate }
4920Sstevel@tonic-gate pc_type = SN_TOKEN;
4930Sstevel@tonic-gate p++;
4940Sstevel@tonic-gate }
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate if (pc_type == SN_TOKEN) {
4970Sstevel@tonic-gate tok_end = p - 1;
4980Sstevel@tonic-gate }
4990Sstevel@tonic-gate if (tok_beg != NOSTR && tok_end != NOSTR) {
5000Sstevel@tonic-gate if (tok_beg == line_beg) {
5010Sstevel@tonic-gate *flag = 0;
5020Sstevel@tonic-gate } else {
5030Sstevel@tonic-gate tok_beg[-1] = '\0';
5040Sstevel@tonic-gate *flag = 1;
5050Sstevel@tonic-gate }
5060Sstevel@tonic-gate tok_end[1] = '\0';
5072931Sas145665 return (tok_beg);
5080Sstevel@tonic-gate } else {
5090Sstevel@tonic-gate if (erf) {
510*7418SViswanathan.Kannappan@Sun.COM printf(gettext("No file specified.\n"));
5110Sstevel@tonic-gate }
5120Sstevel@tonic-gate *flag = 0;
5132931Sas145665 return (NOSTR);
5140Sstevel@tonic-gate }
5150Sstevel@tonic-gate }
5160Sstevel@tonic-gate
5170Sstevel@tonic-gate /*
5180Sstevel@tonic-gate * Delete messages, then type the new dot.
5190Sstevel@tonic-gate */
5200Sstevel@tonic-gate
5212931Sas145665 int
deltype(int msgvec[])5220Sstevel@tonic-gate deltype(int msgvec[])
5230Sstevel@tonic-gate {
5240Sstevel@tonic-gate int list[2];
5250Sstevel@tonic-gate int lastdot;
5260Sstevel@tonic-gate
5270Sstevel@tonic-gate lastdot = dot - &message[0] + 1;
5280Sstevel@tonic-gate if (delm(msgvec) >= 0) {
5290Sstevel@tonic-gate list[0] = dot - &message[0];
5300Sstevel@tonic-gate list[0]++;
5310Sstevel@tonic-gate if (list[0] > lastdot) {
5320Sstevel@tonic-gate touch(list[0]);
5330Sstevel@tonic-gate list[1] = NULL;
5342931Sas145665 return (type(list));
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate printf(gettext("At EOF\n"));
5372931Sas145665 return (0);
5382931Sas145665 } else {
5390Sstevel@tonic-gate printf(gettext("No more messages\n"));
5402931Sas145665 return (0);
5410Sstevel@tonic-gate }
5420Sstevel@tonic-gate }
5430Sstevel@tonic-gate
5440Sstevel@tonic-gate /*
5450Sstevel@tonic-gate * Delete the indicated messages.
5460Sstevel@tonic-gate * Set dot to some nice place afterwards.
5470Sstevel@tonic-gate */
5482931Sas145665 int
delm(int * msgvec)5490Sstevel@tonic-gate delm(int *msgvec)
5500Sstevel@tonic-gate {
5510Sstevel@tonic-gate register struct message *mp;
55218Srobbin int *ip, mesg;
5530Sstevel@tonic-gate int last;
5540Sstevel@tonic-gate
5550Sstevel@tonic-gate last = NULL;
5560Sstevel@tonic-gate for (ip = msgvec; *ip != NULL; ip++) {
5570Sstevel@tonic-gate mesg = *ip;
5580Sstevel@tonic-gate touch(mesg);
5590Sstevel@tonic-gate mp = &message[mesg-1];
5600Sstevel@tonic-gate mp->m_flag |= MDELETED|MTOUCH;
5610Sstevel@tonic-gate mp->m_flag &= ~(MPRESERVE|MSAVED|MBOX);
5620Sstevel@tonic-gate last = mesg;
5630Sstevel@tonic-gate }
5640Sstevel@tonic-gate if (last != NULL) {
5650Sstevel@tonic-gate dot = &message[last-1];
5660Sstevel@tonic-gate last = first(0, MDELETED);
5670Sstevel@tonic-gate if (last != NULL) {
5680Sstevel@tonic-gate dot = &message[last-1];
5692931Sas145665 return (0);
5702931Sas145665 } else {
5710Sstevel@tonic-gate dot = &message[0];
5722931Sas145665 return (-1);
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate }
5750Sstevel@tonic-gate
5760Sstevel@tonic-gate /*
5770Sstevel@tonic-gate * Following can't happen -- it keeps lint happy
5780Sstevel@tonic-gate */
5790Sstevel@tonic-gate
5802931Sas145665 return (-1);
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate
5830Sstevel@tonic-gate /*
5840Sstevel@tonic-gate * Undelete the indicated messages.
5850Sstevel@tonic-gate */
5862931Sas145665 int
undelete(int * msgvec)5870Sstevel@tonic-gate undelete(int *msgvec)
5880Sstevel@tonic-gate {
5890Sstevel@tonic-gate register struct message *mp;
59018Srobbin int *ip, mesg;
5910Sstevel@tonic-gate
5920Sstevel@tonic-gate for (ip = msgvec; ip-msgvec < msgCount; ip++) {
5930Sstevel@tonic-gate mesg = *ip;
5940Sstevel@tonic-gate if (mesg == 0)
5952931Sas145665 return (0);
5960Sstevel@tonic-gate touch(mesg);
5970Sstevel@tonic-gate mp = &message[mesg-1];
5980Sstevel@tonic-gate dot = mp;
5990Sstevel@tonic-gate mp->m_flag &= ~MDELETED;
6000Sstevel@tonic-gate }
6012931Sas145665 return (0);
6020Sstevel@tonic-gate }
6030Sstevel@tonic-gate
6040Sstevel@tonic-gate /*
6050Sstevel@tonic-gate * Add the given header fields to the retained list.
6060Sstevel@tonic-gate * If no arguments, print the current list of retained fields.
6070Sstevel@tonic-gate */
6082931Sas145665 int
retfield(char * list[])6090Sstevel@tonic-gate retfield(char *list[])
6100Sstevel@tonic-gate {
6110Sstevel@tonic-gate char field[BUFSIZ];
6120Sstevel@tonic-gate register int h;
6130Sstevel@tonic-gate register struct ignore *igp;
6140Sstevel@tonic-gate char **ap;
6150Sstevel@tonic-gate
6160Sstevel@tonic-gate if (argcount(list) == 0)
6172931Sas145665 return (retshow());
6180Sstevel@tonic-gate for (ap = list; *ap != 0; ap++) {
6190Sstevel@tonic-gate istrcpy(field, sizeof (field), *ap);
6200Sstevel@tonic-gate
6210Sstevel@tonic-gate if (member(field, retain))
6220Sstevel@tonic-gate continue;
6230Sstevel@tonic-gate
6240Sstevel@tonic-gate h = hash(field);
6250Sstevel@tonic-gate if ((igp = (struct ignore *)
6260Sstevel@tonic-gate calloc(1, sizeof (struct ignore))) == NULL) {
6270Sstevel@tonic-gate panic("Couldn't allocate memory");
6280Sstevel@tonic-gate }
6290Sstevel@tonic-gate if ((igp->i_field = (char *)
6300Sstevel@tonic-gate calloc(strlen(field) + 1, sizeof (char))) == NULL) {
6310Sstevel@tonic-gate panic("Couldn't allocate memory");
6320Sstevel@tonic-gate }
6330Sstevel@tonic-gate strcpy(igp->i_field, field);
6340Sstevel@tonic-gate igp->i_link = retain[h];
6350Sstevel@tonic-gate retain[h] = igp;
6360Sstevel@tonic-gate nretained++;
6370Sstevel@tonic-gate }
6382931Sas145665 return (0);
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate
6410Sstevel@tonic-gate /*
6420Sstevel@tonic-gate * Print out all currently retained fields.
6430Sstevel@tonic-gate */
6442931Sas145665 static int
retshow(void)6450Sstevel@tonic-gate retshow(void)
6460Sstevel@tonic-gate {
6470Sstevel@tonic-gate register int h, count;
6480Sstevel@tonic-gate struct ignore *igp;
6490Sstevel@tonic-gate char **ap, **ring;
6500Sstevel@tonic-gate
6510Sstevel@tonic-gate count = 0;
6520Sstevel@tonic-gate for (h = 0; h < HSHSIZE; h++)
6530Sstevel@tonic-gate for (igp = retain[h]; igp != 0; igp = igp->i_link)
6540Sstevel@tonic-gate count++;
6550Sstevel@tonic-gate if (count == 0) {
6560Sstevel@tonic-gate printf(gettext("No fields currently being retained.\n"));
6572931Sas145665 return (0);
6580Sstevel@tonic-gate }
6592931Sas145665 ring = (char **)salloc((count + 1) * sizeof (char *));
6600Sstevel@tonic-gate ap = ring;
6610Sstevel@tonic-gate for (h = 0; h < HSHSIZE; h++)
6620Sstevel@tonic-gate for (igp = retain[h]; igp != 0; igp = igp->i_link)
6630Sstevel@tonic-gate *ap++ = igp->i_field;
6640Sstevel@tonic-gate *ap = 0;
6650Sstevel@tonic-gate qsort(ring, count, sizeof (char *), igcomp);
6660Sstevel@tonic-gate for (ap = ring; *ap != 0; ap++)
6670Sstevel@tonic-gate printf("%s\n", *ap);
6682931Sas145665 return (0);
6690Sstevel@tonic-gate }
6700Sstevel@tonic-gate
6710Sstevel@tonic-gate /*
6720Sstevel@tonic-gate * Remove a list of fields from the retain list.
6730Sstevel@tonic-gate */
6742931Sas145665 int
unretfield(char * list[])6750Sstevel@tonic-gate unretfield(char *list[])
6760Sstevel@tonic-gate {
6770Sstevel@tonic-gate char **ap, field[BUFSIZ];
6780Sstevel@tonic-gate register int h, count = 0;
6790Sstevel@tonic-gate register struct ignore *ig1, *ig2;
6800Sstevel@tonic-gate
6810Sstevel@tonic-gate if (argcount(list) == 0) {
6820Sstevel@tonic-gate for (h = 0; h < HSHSIZE; h++) {
6830Sstevel@tonic-gate ig1 = retain[h];
6840Sstevel@tonic-gate while (ig1) {
6850Sstevel@tonic-gate free(ig1->i_field);
6860Sstevel@tonic-gate ig2 = ig1->i_link;
6872931Sas145665 free((char *)ig1);
6880Sstevel@tonic-gate ig1 = ig2;
6890Sstevel@tonic-gate count++;
6900Sstevel@tonic-gate }
6910Sstevel@tonic-gate retain[h] = NULL;
6920Sstevel@tonic-gate }
6930Sstevel@tonic-gate if (count == 0)
6940Sstevel@tonic-gate printf(gettext(
6950Sstevel@tonic-gate "No fields currently being retained.\n"));
6960Sstevel@tonic-gate nretained = 0;
6972931Sas145665 return (0);
6980Sstevel@tonic-gate }
6990Sstevel@tonic-gate for (ap = list; *ap; ap++) {
7000Sstevel@tonic-gate istrcpy(field, sizeof (field), *ap);
7010Sstevel@tonic-gate h = hash(field);
7020Sstevel@tonic-gate for (ig1 = retain[h]; ig1; ig2 = ig1, ig1 = ig1->i_link)
7030Sstevel@tonic-gate if (strcmp(ig1->i_field, field) == 0) {
7040Sstevel@tonic-gate if (ig1 == retain[h])
7050Sstevel@tonic-gate retain[h] = ig1->i_link;
7060Sstevel@tonic-gate else
7070Sstevel@tonic-gate ig2->i_link = ig1->i_link;
7080Sstevel@tonic-gate free(ig1->i_field);
7092931Sas145665 free((char *)ig1);
7100Sstevel@tonic-gate nretained--;
7110Sstevel@tonic-gate break;
7120Sstevel@tonic-gate }
7130Sstevel@tonic-gate }
7142931Sas145665 return (0);
7150Sstevel@tonic-gate }
7160Sstevel@tonic-gate
7170Sstevel@tonic-gate /*
7180Sstevel@tonic-gate * Add the given header fields to the ignored list.
7190Sstevel@tonic-gate * If no arguments, print the current list of ignored fields.
7200Sstevel@tonic-gate */
7212931Sas145665 int
igfield(char * list[])7220Sstevel@tonic-gate igfield(char *list[])
7230Sstevel@tonic-gate {
7240Sstevel@tonic-gate char field[BUFSIZ];
7250Sstevel@tonic-gate register int h;
7260Sstevel@tonic-gate register struct ignore *igp;
7270Sstevel@tonic-gate char **ap;
7280Sstevel@tonic-gate
7290Sstevel@tonic-gate if (argcount(list) == 0)
7302931Sas145665 return (igshow());
7310Sstevel@tonic-gate for (ap = list; *ap != 0; ap++) {
7320Sstevel@tonic-gate if (isign(*ap, 0))
7330Sstevel@tonic-gate continue;
7340Sstevel@tonic-gate istrcpy(field, sizeof (field), *ap);
7350Sstevel@tonic-gate h = hash(field);
7360Sstevel@tonic-gate if ((igp = (struct ignore *)
7370Sstevel@tonic-gate calloc(1, sizeof (struct ignore))) == NULL) {
7380Sstevel@tonic-gate panic("Couldn't allocate memory");
7390Sstevel@tonic-gate }
7400Sstevel@tonic-gate if ((igp->i_field = (char *)
7410Sstevel@tonic-gate calloc((unsigned)strlen(field) + 1,
7420Sstevel@tonic-gate sizeof (char))) == NULL) {
7430Sstevel@tonic-gate panic("Couldn't allocate memory");
7440Sstevel@tonic-gate }
7450Sstevel@tonic-gate strcpy(igp->i_field, field);
7460Sstevel@tonic-gate igp->i_link = ignore[h];
7470Sstevel@tonic-gate ignore[h] = igp;
7480Sstevel@tonic-gate }
7492931Sas145665 return (0);
7500Sstevel@tonic-gate }
7510Sstevel@tonic-gate
7520Sstevel@tonic-gate /*
7530Sstevel@tonic-gate * Print out all currently ignored fields.
7540Sstevel@tonic-gate */
7552931Sas145665 static int
igshow(void)7560Sstevel@tonic-gate igshow(void)
7570Sstevel@tonic-gate {
7580Sstevel@tonic-gate register int h, count;
7590Sstevel@tonic-gate struct ignore *igp;
7600Sstevel@tonic-gate char **ap, **ring;
7610Sstevel@tonic-gate
7620Sstevel@tonic-gate count = 0;
7630Sstevel@tonic-gate for (h = 0; h < HSHSIZE; h++)
7640Sstevel@tonic-gate for (igp = ignore[h]; igp != 0; igp = igp->i_link)
7650Sstevel@tonic-gate count++;
7660Sstevel@tonic-gate if (count == 0) {
7670Sstevel@tonic-gate printf(gettext("No fields currently being ignored.\n"));
7682931Sas145665 return (0);
7690Sstevel@tonic-gate }
7702931Sas145665 ring = (char **)salloc((count + 1) * sizeof (char *));
7710Sstevel@tonic-gate ap = ring;
7720Sstevel@tonic-gate for (h = 0; h < HSHSIZE; h++)
7730Sstevel@tonic-gate for (igp = ignore[h]; igp != 0; igp = igp->i_link)
7740Sstevel@tonic-gate *ap++ = igp->i_field;
7750Sstevel@tonic-gate *ap = 0;
7762931Sas145665 qsort((char *)ring, (unsigned)count, sizeof (char *), igcomp);
7770Sstevel@tonic-gate for (ap = ring; *ap != 0; ap++)
7780Sstevel@tonic-gate printf("%s\n", *ap);
7792931Sas145665 return (0);
7800Sstevel@tonic-gate }
7810Sstevel@tonic-gate
7820Sstevel@tonic-gate /*
7830Sstevel@tonic-gate * Compare two names for sorting ignored field list.
7840Sstevel@tonic-gate */
7852931Sas145665 static int
igcomp(const void * l,const void * r)7860Sstevel@tonic-gate igcomp(const void *l, const void *r)
7870Sstevel@tonic-gate {
7882931Sas145665 return (strcmp(*(char **)l, *(char **)r));
7890Sstevel@tonic-gate }
7900Sstevel@tonic-gate
7910Sstevel@tonic-gate /*
7920Sstevel@tonic-gate * Remove a list of fields from the ignore list.
7930Sstevel@tonic-gate */
7942931Sas145665 int
unigfield(char * list[])7950Sstevel@tonic-gate unigfield(char *list[])
7960Sstevel@tonic-gate {
7970Sstevel@tonic-gate char **ap, field[BUFSIZ];
7980Sstevel@tonic-gate register int h, count = 0;
7990Sstevel@tonic-gate register struct ignore *ig1, *ig2;
8000Sstevel@tonic-gate
8010Sstevel@tonic-gate if (argcount(list) == 0) {
8020Sstevel@tonic-gate for (h = 0; h < HSHSIZE; h++) {
8030Sstevel@tonic-gate ig1 = ignore[h];
8040Sstevel@tonic-gate while (ig1) {
8050Sstevel@tonic-gate free(ig1->i_field);
8060Sstevel@tonic-gate ig2 = ig1->i_link;
8072931Sas145665 free((char *)ig1);
8080Sstevel@tonic-gate ig1 = ig2;
8090Sstevel@tonic-gate count++;
8100Sstevel@tonic-gate }
8110Sstevel@tonic-gate ignore[h] = NULL;
8120Sstevel@tonic-gate }
8130Sstevel@tonic-gate if (count == 0)
8140Sstevel@tonic-gate printf(gettext("No fields currently being ignored.\n"));
8152931Sas145665 return (0);
8160Sstevel@tonic-gate }
8170Sstevel@tonic-gate for (ap = list; *ap; ap++) {
8180Sstevel@tonic-gate istrcpy(field, sizeof (field), *ap);
8190Sstevel@tonic-gate h = hash(field);
8200Sstevel@tonic-gate for (ig1 = ignore[h]; ig1; ig2 = ig1, ig1 = ig1->i_link)
8210Sstevel@tonic-gate if (strcmp(ig1->i_field, field) == 0) {
8220Sstevel@tonic-gate if (ig1 == ignore[h])
8230Sstevel@tonic-gate ignore[h] = ig1->i_link;
8240Sstevel@tonic-gate else
8250Sstevel@tonic-gate ig2->i_link = ig1->i_link;
8260Sstevel@tonic-gate free(ig1->i_field);
8272931Sas145665 free((char *)ig1);
8280Sstevel@tonic-gate break;
8290Sstevel@tonic-gate }
8300Sstevel@tonic-gate }
8312931Sas145665 return (0);
8320Sstevel@tonic-gate }
833