xref: /onnv-gate/usr/src/cmd/mailx/cmd2.c (revision 0)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate /*
26*0Sstevel@tonic-gate  * Copyright 2001-2002 Sun Microsystems, Inc.  All rights reserved.
27*0Sstevel@tonic-gate  * Use is subject to license terms.
28*0Sstevel@tonic-gate  */
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate /*
32*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
33*0Sstevel@tonic-gate  * The Regents of the University of California
34*0Sstevel@tonic-gate  * All Rights Reserved
35*0Sstevel@tonic-gate  *
36*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
37*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
38*0Sstevel@tonic-gate  * contributors.
39*0Sstevel@tonic-gate  */
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #include "rcv.h"
44*0Sstevel@tonic-gate #include <locale.h>
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate /*
47*0Sstevel@tonic-gate  * mailx -- a modified version of a University of California at Berkeley
48*0Sstevel@tonic-gate  *	mail program
49*0Sstevel@tonic-gate  *
50*0Sstevel@tonic-gate  * More user commands.
51*0Sstevel@tonic-gate  */
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate static int	igshow(void);
54*0Sstevel@tonic-gate static int	igcomp(const void *l, const void *r);
55*0Sstevel@tonic-gate static int	save1(char str[], int mark);
56*0Sstevel@tonic-gate static int	Save1(int *msgvec, int mark);
57*0Sstevel@tonic-gate static void	savemsglist(char *file, int *msgvec, int flag);
58*0Sstevel@tonic-gate static int	put1(char str[], int doign);
59*0Sstevel@tonic-gate static int	svputs(const char *line, FILE *obuf);
60*0Sstevel@tonic-gate static int	wrputs(const char *line, FILE *obuf);
61*0Sstevel@tonic-gate static int	retshow(void);
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate /* flags for savemsglist() */
64*0Sstevel@tonic-gate #define	S_MARK		1		/* mark the message as saved */
65*0Sstevel@tonic-gate #define	S_NOHEADER	2		/* don't write out the header */
66*0Sstevel@tonic-gate #define	S_SAVING	4		/* doing save/copy */
67*0Sstevel@tonic-gate #define	S_NOIGNORE	8		/* don't do ignore processing */
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate /*
70*0Sstevel@tonic-gate  * If any arguments were given, go to the next applicable argument
71*0Sstevel@tonic-gate  * following dot, otherwise, go to the next applicable message.
72*0Sstevel@tonic-gate  * If given as first command with no arguments, print first message.
73*0Sstevel@tonic-gate  */
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate int
76*0Sstevel@tonic-gate next(int *msgvec)
77*0Sstevel@tonic-gate {
78*0Sstevel@tonic-gate 	register struct message *mp;
79*0Sstevel@tonic-gate 	register int *ip, *ip2;
80*0Sstevel@tonic-gate 	int list[2], mdot;
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate 	if (*msgvec != NULL) {
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 		/*
85*0Sstevel@tonic-gate 		 * If some messages were supplied, find the
86*0Sstevel@tonic-gate 		 * first applicable one following dot using
87*0Sstevel@tonic-gate 		 * wrap around.
88*0Sstevel@tonic-gate 		 */
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 		mdot = dot - &message[0] + 1;
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 		/*
93*0Sstevel@tonic-gate 		 * Find the first message in the supplied
94*0Sstevel@tonic-gate 		 * message list which follows dot.
95*0Sstevel@tonic-gate 		 */
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 		for (ip = msgvec; *ip != NULL; ip++)
98*0Sstevel@tonic-gate 			if (*ip > mdot)
99*0Sstevel@tonic-gate 				break;
100*0Sstevel@tonic-gate 		if (*ip == NULL)
101*0Sstevel@tonic-gate 			ip = msgvec;
102*0Sstevel@tonic-gate 		ip2 = ip;
103*0Sstevel@tonic-gate 		do {
104*0Sstevel@tonic-gate 			mp = &message[*ip2 - 1];
105*0Sstevel@tonic-gate 			if ((mp->m_flag & MDELETED) == 0) {
106*0Sstevel@tonic-gate 				dot = mp;
107*0Sstevel@tonic-gate 				goto hitit;
108*0Sstevel@tonic-gate 			}
109*0Sstevel@tonic-gate 			if (*ip2 != NULL)
110*0Sstevel@tonic-gate 				ip2++;
111*0Sstevel@tonic-gate 			if (*ip2 == NULL)
112*0Sstevel@tonic-gate 				ip2 = msgvec;
113*0Sstevel@tonic-gate 		} while (ip2 != ip);
114*0Sstevel@tonic-gate 		printf(gettext("No messages applicable\n"));
115*0Sstevel@tonic-gate 		return(1);
116*0Sstevel@tonic-gate 	}
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 	/*
119*0Sstevel@tonic-gate 	 * If this is the first command, select message 1.
120*0Sstevel@tonic-gate 	 * Note that this must exist for us to get here at all.
121*0Sstevel@tonic-gate 	 */
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 	if (!sawcom)
124*0Sstevel@tonic-gate 		goto hitit;
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	/*
127*0Sstevel@tonic-gate 	 * Just find the next good message after dot, no
128*0Sstevel@tonic-gate 	 * wraparound.
129*0Sstevel@tonic-gate 	 */
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	for (mp = dot+1; mp < &message[msgCount]; mp++)
132*0Sstevel@tonic-gate 		if ((mp->m_flag & (MDELETED|MSAVED)) == 0)
133*0Sstevel@tonic-gate 			break;
134*0Sstevel@tonic-gate 	if (mp >= &message[msgCount]) {
135*0Sstevel@tonic-gate 		printf(gettext("At EOF\n"));
136*0Sstevel@tonic-gate 		return(0);
137*0Sstevel@tonic-gate 	}
138*0Sstevel@tonic-gate 	dot = mp;
139*0Sstevel@tonic-gate hitit:
140*0Sstevel@tonic-gate 	/*
141*0Sstevel@tonic-gate 	 * Print dot.
142*0Sstevel@tonic-gate 	 */
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	list[0] = dot - &message[0] + 1;
145*0Sstevel@tonic-gate 	list[1] = NULL;
146*0Sstevel@tonic-gate 	return(type(list));
147*0Sstevel@tonic-gate }
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate /*
150*0Sstevel@tonic-gate  * Save a message in a file.  Mark the message as saved
151*0Sstevel@tonic-gate  * so we can discard when the user quits.
152*0Sstevel@tonic-gate  */
153*0Sstevel@tonic-gate int
154*0Sstevel@tonic-gate save(char str[])
155*0Sstevel@tonic-gate {
156*0Sstevel@tonic-gate 	return(save1(str, S_MARK));
157*0Sstevel@tonic-gate }
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate /*
160*0Sstevel@tonic-gate  * Copy a message to a file without affected its saved-ness
161*0Sstevel@tonic-gate  */
162*0Sstevel@tonic-gate int
163*0Sstevel@tonic-gate copycmd(char str[])
164*0Sstevel@tonic-gate {
165*0Sstevel@tonic-gate 	return(save1(str, 0));
166*0Sstevel@tonic-gate }
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate /*
169*0Sstevel@tonic-gate  * Save/copy the indicated messages at the end of the passed file name.
170*0Sstevel@tonic-gate  * If mark is true, mark the message "saved."
171*0Sstevel@tonic-gate  */
172*0Sstevel@tonic-gate static int
173*0Sstevel@tonic-gate save1(char str[], int mark)
174*0Sstevel@tonic-gate {
175*0Sstevel@tonic-gate 	char *file, *cmd;
176*0Sstevel@tonic-gate 	int f, *msgvec;
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	cmd = mark ? "save" : "copy";
179*0Sstevel@tonic-gate 	msgvec = (int *) salloc((msgCount + 2) * sizeof *msgvec);
180*0Sstevel@tonic-gate 	if ((file = snarf(str, &f, 0)) == NOSTR)
181*0Sstevel@tonic-gate 		file = Getf("MBOX");
182*0Sstevel@tonic-gate 	if (f==-1)
183*0Sstevel@tonic-gate 		return(1);
184*0Sstevel@tonic-gate 	if (!f) {
185*0Sstevel@tonic-gate 		*msgvec = first(0, MMNORM);
186*0Sstevel@tonic-gate 		if (*msgvec == NULL) {
187*0Sstevel@tonic-gate 			printf(gettext("No messages to %s.\n"), cmd);
188*0Sstevel@tonic-gate 			return(1);
189*0Sstevel@tonic-gate 		}
190*0Sstevel@tonic-gate 		msgvec[1] = NULL;
191*0Sstevel@tonic-gate 	}
192*0Sstevel@tonic-gate 	if (f && getmsglist(str, msgvec, 0) < 0)
193*0Sstevel@tonic-gate 		return(1);
194*0Sstevel@tonic-gate 	if ((file = expand(file)) == NOSTR)
195*0Sstevel@tonic-gate 		return(1);
196*0Sstevel@tonic-gate 	savemsglist(file, msgvec, mark | S_SAVING);
197*0Sstevel@tonic-gate 	return(0);
198*0Sstevel@tonic-gate }
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate int
201*0Sstevel@tonic-gate Save(int *msgvec)
202*0Sstevel@tonic-gate {
203*0Sstevel@tonic-gate 	return(Save1(msgvec, S_MARK));
204*0Sstevel@tonic-gate }
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate int
207*0Sstevel@tonic-gate Copy(int *msgvec)
208*0Sstevel@tonic-gate {
209*0Sstevel@tonic-gate 	return(Save1(msgvec, 0));
210*0Sstevel@tonic-gate }
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate /*
213*0Sstevel@tonic-gate  * save/copy the indicated messages at the end of a file named
214*0Sstevel@tonic-gate  * by the sender of the first message in the msglist.
215*0Sstevel@tonic-gate  */
216*0Sstevel@tonic-gate static int
217*0Sstevel@tonic-gate Save1(int *msgvec, int mark)
218*0Sstevel@tonic-gate {
219*0Sstevel@tonic-gate 	register char *from;
220*0Sstevel@tonic-gate 	char recfile[BUFSIZ];
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate #ifdef notdef
223*0Sstevel@tonic-gate 	from = striphosts(nameof(&message[*msgvec-1], 0));
224*0Sstevel@tonic-gate #else
225*0Sstevel@tonic-gate 	from = nameof(&message[*msgvec-1]);
226*0Sstevel@tonic-gate #endif
227*0Sstevel@tonic-gate 	getrecf(from, recfile, 1, sizeof (recfile));
228*0Sstevel@tonic-gate 	if (*recfile != '\0')
229*0Sstevel@tonic-gate 		savemsglist(safeexpand(recfile), msgvec, mark | S_SAVING);
230*0Sstevel@tonic-gate 	return(0);
231*0Sstevel@tonic-gate }
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate int
234*0Sstevel@tonic-gate sput(char str[])
235*0Sstevel@tonic-gate {
236*0Sstevel@tonic-gate 	return(put1(str, 0));
237*0Sstevel@tonic-gate }
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate int
240*0Sstevel@tonic-gate Sput(char str[])
241*0Sstevel@tonic-gate {
242*0Sstevel@tonic-gate 	return(put1(str, S_NOIGNORE));
243*0Sstevel@tonic-gate }
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate /*
246*0Sstevel@tonic-gate  * Put the indicated messages at the end of the passed file name.
247*0Sstevel@tonic-gate  */
248*0Sstevel@tonic-gate static int
249*0Sstevel@tonic-gate put1(char str[], int doign)
250*0Sstevel@tonic-gate {
251*0Sstevel@tonic-gate 	char *file;
252*0Sstevel@tonic-gate 	int f, *msgvec;
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 	msgvec = (int *) salloc((msgCount + 2) * sizeof *msgvec);
255*0Sstevel@tonic-gate 	if ((file = snarf(str, &f, 0)) == NOSTR)
256*0Sstevel@tonic-gate 		file = Getf("MBOX");
257*0Sstevel@tonic-gate 	if (f==-1)
258*0Sstevel@tonic-gate 		return(1);
259*0Sstevel@tonic-gate 	if (!f) {
260*0Sstevel@tonic-gate 		*msgvec = first(0, MMNORM);
261*0Sstevel@tonic-gate 		if (*msgvec == NULL) {
262*0Sstevel@tonic-gate 			printf(gettext("No messages to put.\n"));
263*0Sstevel@tonic-gate 			return(1);
264*0Sstevel@tonic-gate 		}
265*0Sstevel@tonic-gate 		msgvec[1] = NULL;
266*0Sstevel@tonic-gate 	}
267*0Sstevel@tonic-gate 	if (f && getmsglist(str, msgvec, 0) < 0)
268*0Sstevel@tonic-gate 		return(1);
269*0Sstevel@tonic-gate 	if ((file = expand(file)) == NOSTR)
270*0Sstevel@tonic-gate 		return(1);
271*0Sstevel@tonic-gate 	savemsglist(file, msgvec, doign);
272*0Sstevel@tonic-gate 	return(0);
273*0Sstevel@tonic-gate }
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate /*
276*0Sstevel@tonic-gate  * save a message list in a file.
277*0Sstevel@tonic-gate  * if wr set, doing "write" instead
278*0Sstevel@tonic-gate  * of "save" or "copy" so don't put
279*0Sstevel@tonic-gate  * out header.
280*0Sstevel@tonic-gate  */
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate static	int wr_linecount;		/* count of lines written */
283*0Sstevel@tonic-gate static	int wr_charcount;		/* char count of lines written */
284*0Sstevel@tonic-gate static	int wr_inlines;			/* count of lines read */
285*0Sstevel@tonic-gate static	long wr_maxlines;		/* total lines in message */
286*0Sstevel@tonic-gate static	int wr_inhead;			/* in header of message */
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate static void
289*0Sstevel@tonic-gate savemsglist(char *file, int *msgvec, int flag)
290*0Sstevel@tonic-gate {
291*0Sstevel@tonic-gate 	register int *ip, mesg;
292*0Sstevel@tonic-gate 	register struct message *mp;
293*0Sstevel@tonic-gate 	char *disp;
294*0Sstevel@tonic-gate 	FILE *obuf;
295*0Sstevel@tonic-gate 	struct stat statb;
296*0Sstevel@tonic-gate 	long lc, cc, t;
297*0Sstevel@tonic-gate 	int bnry, mflag;
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	printf("\"%s\" ", file);
300*0Sstevel@tonic-gate 	flush();
301*0Sstevel@tonic-gate 	if (stat(file, &statb) >= 0)
302*0Sstevel@tonic-gate 		disp = "[Appended]";
303*0Sstevel@tonic-gate 	else
304*0Sstevel@tonic-gate 		disp = "[New file]";
305*0Sstevel@tonic-gate 	if ((obuf = fopen(file, "a")) == NULL) {
306*0Sstevel@tonic-gate 		perror("");
307*0Sstevel@tonic-gate 		return;
308*0Sstevel@tonic-gate 	}
309*0Sstevel@tonic-gate 	lc = cc = 0;
310*0Sstevel@tonic-gate 	bnry = 0;
311*0Sstevel@tonic-gate 	if (flag & S_SAVING)
312*0Sstevel@tonic-gate 		mflag = (int)value("alwaysignore")?(M_IGNORE|M_SAVING):M_SAVING;
313*0Sstevel@tonic-gate 	else if (flag & S_NOIGNORE)
314*0Sstevel@tonic-gate 		mflag = 0;
315*0Sstevel@tonic-gate 	else
316*0Sstevel@tonic-gate 		mflag = M_IGNORE;
317*0Sstevel@tonic-gate 	for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
318*0Sstevel@tonic-gate 		mesg = *ip;
319*0Sstevel@tonic-gate 		mp = &message[mesg-1];
320*0Sstevel@tonic-gate 		if (!mp->m_text) {
321*0Sstevel@tonic-gate 			bnry = 1;
322*0Sstevel@tonic-gate 		}
323*0Sstevel@tonic-gate 		wr_linecount = 0;
324*0Sstevel@tonic-gate 		wr_charcount = 0;
325*0Sstevel@tonic-gate 		if (flag & S_NOHEADER) {
326*0Sstevel@tonic-gate 			wr_inhead = 1;
327*0Sstevel@tonic-gate 			wr_maxlines = mp->m_lines;
328*0Sstevel@tonic-gate 			wr_inlines = 0;
329*0Sstevel@tonic-gate 			t = msend(mp, obuf, 0, wrputs);
330*0Sstevel@tonic-gate 		} else {
331*0Sstevel@tonic-gate 			t = msend(mp, obuf, mflag, svputs);
332*0Sstevel@tonic-gate 		}
333*0Sstevel@tonic-gate 		if (t < 0) {
334*0Sstevel@tonic-gate 			perror(file);
335*0Sstevel@tonic-gate 			fclose(obuf);
336*0Sstevel@tonic-gate 			return;
337*0Sstevel@tonic-gate 		}
338*0Sstevel@tonic-gate 		touch(mesg);
339*0Sstevel@tonic-gate 		dot = mp;
340*0Sstevel@tonic-gate 		lc += wr_linecount;
341*0Sstevel@tonic-gate 		cc += wr_charcount;
342*0Sstevel@tonic-gate 		if (flag & S_MARK)
343*0Sstevel@tonic-gate 			mp->m_flag |= MSAVED;
344*0Sstevel@tonic-gate 	}
345*0Sstevel@tonic-gate 	fflush(obuf);
346*0Sstevel@tonic-gate 	if (fferror(obuf))
347*0Sstevel@tonic-gate 		perror(file);
348*0Sstevel@tonic-gate 	fclose(obuf);
349*0Sstevel@tonic-gate 	if (!bnry) {
350*0Sstevel@tonic-gate 		printf("%s %ld/%ld\n", disp, lc, cc);
351*0Sstevel@tonic-gate 	} else {
352*0Sstevel@tonic-gate 		printf("%s binary/%ld\n", disp, cc);
353*0Sstevel@tonic-gate 	}
354*0Sstevel@tonic-gate }
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate static int
357*0Sstevel@tonic-gate svputs(const char *line, FILE *obuf)
358*0Sstevel@tonic-gate {
359*0Sstevel@tonic-gate 	wr_linecount++;
360*0Sstevel@tonic-gate 	wr_charcount += strlen(line);
361*0Sstevel@tonic-gate 	return(fputs(line, obuf));
362*0Sstevel@tonic-gate }
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate static int
365*0Sstevel@tonic-gate wrputs(const char *line, FILE *obuf)
366*0Sstevel@tonic-gate {
367*0Sstevel@tonic-gate 	/*
368*0Sstevel@tonic-gate 	 * If this is a header line or
369*0Sstevel@tonic-gate 	 * the last line, don't write it out.  Since we may add a
370*0Sstevel@tonic-gate 	 * "Status" line the line count may be off by one so insist
371*0Sstevel@tonic-gate 	 * that the last line is blank before we skip it.
372*0Sstevel@tonic-gate 	 */
373*0Sstevel@tonic-gate 	wr_inlines++;
374*0Sstevel@tonic-gate 	if (wr_inhead) {
375*0Sstevel@tonic-gate 		if (strcmp(line, "\n") == 0)
376*0Sstevel@tonic-gate 			wr_inhead = 0;
377*0Sstevel@tonic-gate 		return(0);
378*0Sstevel@tonic-gate 	}
379*0Sstevel@tonic-gate 	if (wr_inlines >= wr_maxlines && strcmp(line, "\n") == 0)
380*0Sstevel@tonic-gate 		return(0);
381*0Sstevel@tonic-gate 	wr_linecount++;
382*0Sstevel@tonic-gate 	wr_charcount += strlen(line);
383*0Sstevel@tonic-gate 	return(fputs(line, obuf));
384*0Sstevel@tonic-gate }
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate /*
387*0Sstevel@tonic-gate  * Write the indicated messages at the end of the passed
388*0Sstevel@tonic-gate  * file name, minus header and trailing blank line.
389*0Sstevel@tonic-gate  */
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate int
392*0Sstevel@tonic-gate swrite(char str[])
393*0Sstevel@tonic-gate {
394*0Sstevel@tonic-gate 	register char *file;
395*0Sstevel@tonic-gate 	int f, *msgvec;
396*0Sstevel@tonic-gate 
397*0Sstevel@tonic-gate 	msgvec = (int *) salloc((msgCount + 2) * sizeof *msgvec);
398*0Sstevel@tonic-gate 	if ((file = snarf(str, &f, 1)) == NOSTR)
399*0Sstevel@tonic-gate 		return(1);
400*0Sstevel@tonic-gate 	if (f==-1)
401*0Sstevel@tonic-gate 		return(1);
402*0Sstevel@tonic-gate 	if ((file = expand(file)) == NOSTR)
403*0Sstevel@tonic-gate 		return(1);
404*0Sstevel@tonic-gate 	if (!f) {
405*0Sstevel@tonic-gate 		*msgvec = first(0, MMNORM);
406*0Sstevel@tonic-gate 		if (*msgvec == NULL) {
407*0Sstevel@tonic-gate 			printf(gettext("No messages to write.\n"));
408*0Sstevel@tonic-gate 			return(1);
409*0Sstevel@tonic-gate 		}
410*0Sstevel@tonic-gate 		msgvec[1] = NULL;
411*0Sstevel@tonic-gate 	}
412*0Sstevel@tonic-gate 	if (f && getmsglist(str, msgvec, 0) < 0)
413*0Sstevel@tonic-gate 		return(1);
414*0Sstevel@tonic-gate 	savemsglist(file, msgvec, S_MARK|S_NOHEADER);
415*0Sstevel@tonic-gate 	return(0);
416*0Sstevel@tonic-gate }
417*0Sstevel@tonic-gate 
418*0Sstevel@tonic-gate /*
419*0Sstevel@tonic-gate  * Snarf the file from the end of the command line and
420*0Sstevel@tonic-gate  * return a pointer to it.  If there is no file attached,
421*0Sstevel@tonic-gate  * just return NOSTR.  Put a null in front of the file
422*0Sstevel@tonic-gate  * name so that the message list processing won't see it,
423*0Sstevel@tonic-gate  * unless the file name is the only thing on the line, in
424*0Sstevel@tonic-gate  * which case, return 0 in the reference flag variable.
425*0Sstevel@tonic-gate  */
426*0Sstevel@tonic-gate 
427*0Sstevel@tonic-gate /*
428*0Sstevel@tonic-gate  * The following definitions are used to characterize the syntactic
429*0Sstevel@tonic-gate  * category of the preceding character in the following parse procedure.
430*0Sstevel@tonic-gate  * The variable pc_type assumes these values.
431*0Sstevel@tonic-gate  */
432*0Sstevel@tonic-gate 
433*0Sstevel@tonic-gate #define	SN_DELIM	1	/* Delimiter (<blank> or line beginning) */
434*0Sstevel@tonic-gate #define	SN_TOKEN	2	/* A part of a token */
435*0Sstevel@tonic-gate #define	SN_QUOTE	4	/* An entire quoted string (ie, "...") */
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate char *
438*0Sstevel@tonic-gate snarf(char linebuf[], int *flag, int erf)
439*0Sstevel@tonic-gate {
440*0Sstevel@tonic-gate 	register char *p;		/* utility pointer */
441*0Sstevel@tonic-gate 	register char qc;		/* quotation character to match */
442*0Sstevel@tonic-gate 	register unsigned int  pc_type;	/* preceding character type */
443*0Sstevel@tonic-gate 	register char *tok_beg;		/* beginning of last token */
444*0Sstevel@tonic-gate 	register char *tok_end;		/* end of last token */
445*0Sstevel@tonic-gate 	char *line_beg;			/* beginning of line, after */
446*0Sstevel@tonic-gate 					/* leading whitespace */
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate 	/*
449*0Sstevel@tonic-gate 	 * Skip leading whitespace.
450*0Sstevel@tonic-gate 	 */
451*0Sstevel@tonic-gate 	for (line_beg = linebuf;
452*0Sstevel@tonic-gate 	     *line_beg && any(*line_beg, " \t");
453*0Sstevel@tonic-gate 	     line_beg++) {
454*0Sstevel@tonic-gate 		/* empty body */
455*0Sstevel@tonic-gate 	}
456*0Sstevel@tonic-gate 	if (!*line_beg) {
457*0Sstevel@tonic-gate 		if (erf) {
458*0Sstevel@tonic-gate 			printf(gettext("No file specified\n."));
459*0Sstevel@tonic-gate 		}
460*0Sstevel@tonic-gate 		*flag = 0;
461*0Sstevel@tonic-gate 		return(NOSTR);
462*0Sstevel@tonic-gate 	}
463*0Sstevel@tonic-gate 	/*
464*0Sstevel@tonic-gate 	 * Process line from left-to-right, 1 char at a time.
465*0Sstevel@tonic-gate 	 */
466*0Sstevel@tonic-gate 	for (pc_type = SN_DELIM, tok_beg = tok_end = NOSTR, p = line_beg;
467*0Sstevel@tonic-gate 	     *p != '\0'; ) {
468*0Sstevel@tonic-gate 		if (any(*p, " \t")) {
469*0Sstevel@tonic-gate 			/* This character is a DELIMITER */
470*0Sstevel@tonic-gate 			if (pc_type & (SN_TOKEN|SN_QUOTE)) {
471*0Sstevel@tonic-gate 				tok_end = p - 1;
472*0Sstevel@tonic-gate 			}
473*0Sstevel@tonic-gate 			pc_type = SN_DELIM;
474*0Sstevel@tonic-gate 			p++;
475*0Sstevel@tonic-gate 		} else if ((qc = *p) == '"' || qc == '\'') {
476*0Sstevel@tonic-gate 			/* This character is a QUOTE character */
477*0Sstevel@tonic-gate 			if (pc_type == SN_TOKEN) {
478*0Sstevel@tonic-gate 				/* embedded quotation symbols are simply */
479*0Sstevel@tonic-gate 				/* token characters. */
480*0Sstevel@tonic-gate 				p++;
481*0Sstevel@tonic-gate 				continue;
482*0Sstevel@tonic-gate 			}
483*0Sstevel@tonic-gate 			/* Search for the matching QUOTE character */
484*0Sstevel@tonic-gate 			for (tok_beg = p, tok_end = NOSTR, p++;
485*0Sstevel@tonic-gate 			     *p != '\0' && *p != qc;
486*0Sstevel@tonic-gate 			     p++) {
487*0Sstevel@tonic-gate 				if (*p == '\\' && *(p+1) == qc) {
488*0Sstevel@tonic-gate 					p++;
489*0Sstevel@tonic-gate 				}
490*0Sstevel@tonic-gate 			}
491*0Sstevel@tonic-gate 			if (*p == '\0') {
492*0Sstevel@tonic-gate 				printf(gettext("Syntax error: missing "
493*0Sstevel@tonic-gate 					       "%c.\n"), qc);
494*0Sstevel@tonic-gate 				*flag = -1;
495*0Sstevel@tonic-gate 				return(NOSTR);
496*0Sstevel@tonic-gate 			}
497*0Sstevel@tonic-gate 			tok_end = p;
498*0Sstevel@tonic-gate 			pc_type = SN_QUOTE;
499*0Sstevel@tonic-gate 			p++;
500*0Sstevel@tonic-gate 		} else {
501*0Sstevel@tonic-gate 			/* This character should be a TOKEN character */
502*0Sstevel@tonic-gate 			if (pc_type & (SN_DELIM|SN_TOKEN)) {
503*0Sstevel@tonic-gate 				if (pc_type & SN_DELIM) {
504*0Sstevel@tonic-gate 					tok_beg = p;
505*0Sstevel@tonic-gate 					tok_end = NOSTR;
506*0Sstevel@tonic-gate 				}
507*0Sstevel@tonic-gate 			} else {
508*0Sstevel@tonic-gate 				printf(gettext("improper quotes"
509*0Sstevel@tonic-gate 					       " at \"%s\".\n"), p);
510*0Sstevel@tonic-gate 				*flag = -1;
511*0Sstevel@tonic-gate 				return(NOSTR);
512*0Sstevel@tonic-gate 			}
513*0Sstevel@tonic-gate 			if (*p == '\\' && *++p == '\0') {
514*0Sstevel@tonic-gate 				printf(gettext("\'\\\' at "
515*0Sstevel@tonic-gate 					       "end of line.\n"));
516*0Sstevel@tonic-gate 				*flag = -1;
517*0Sstevel@tonic-gate 				return(NOSTR);
518*0Sstevel@tonic-gate 			}
519*0Sstevel@tonic-gate 			pc_type = SN_TOKEN;
520*0Sstevel@tonic-gate 			p++;
521*0Sstevel@tonic-gate 		}
522*0Sstevel@tonic-gate 	}
523*0Sstevel@tonic-gate 	if (pc_type == SN_TOKEN) {
524*0Sstevel@tonic-gate 		tok_end = p - 1;
525*0Sstevel@tonic-gate 	}
526*0Sstevel@tonic-gate 	if (tok_beg != NOSTR && tok_end != NOSTR) {
527*0Sstevel@tonic-gate 		if (tok_beg == line_beg) {
528*0Sstevel@tonic-gate 			*flag = 0;
529*0Sstevel@tonic-gate 		} else {
530*0Sstevel@tonic-gate 			tok_beg[-1] = '\0';
531*0Sstevel@tonic-gate 			*flag = 1;
532*0Sstevel@tonic-gate 		}
533*0Sstevel@tonic-gate 		tok_end[1] = '\0';
534*0Sstevel@tonic-gate 		return(tok_beg);
535*0Sstevel@tonic-gate 	} else {
536*0Sstevel@tonic-gate 		if (erf) {
537*0Sstevel@tonic-gate 			printf(gettext("No file specified\n."));
538*0Sstevel@tonic-gate 		}
539*0Sstevel@tonic-gate 		*flag = 0;
540*0Sstevel@tonic-gate 		return(NOSTR);
541*0Sstevel@tonic-gate 	}
542*0Sstevel@tonic-gate }
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate /*
545*0Sstevel@tonic-gate  * Delete messages, then type the new dot.
546*0Sstevel@tonic-gate  */
547*0Sstevel@tonic-gate 
548*0Sstevel@tonic-gate int
549*0Sstevel@tonic-gate deltype(int msgvec[])
550*0Sstevel@tonic-gate {
551*0Sstevel@tonic-gate 	int list[2];
552*0Sstevel@tonic-gate 	int lastdot;
553*0Sstevel@tonic-gate 
554*0Sstevel@tonic-gate 	lastdot = dot - &message[0] + 1;
555*0Sstevel@tonic-gate 	if (delm(msgvec) >= 0) {
556*0Sstevel@tonic-gate 		list[0] = dot - &message[0];
557*0Sstevel@tonic-gate 		list[0]++;
558*0Sstevel@tonic-gate 		if (list[0] > lastdot) {
559*0Sstevel@tonic-gate 			touch(list[0]);
560*0Sstevel@tonic-gate 			list[1] = NULL;
561*0Sstevel@tonic-gate 			return(type(list));
562*0Sstevel@tonic-gate 		}
563*0Sstevel@tonic-gate 		printf(gettext("At EOF\n"));
564*0Sstevel@tonic-gate 		return(0);
565*0Sstevel@tonic-gate 	}
566*0Sstevel@tonic-gate 	else {
567*0Sstevel@tonic-gate 		printf(gettext("No more messages\n"));
568*0Sstevel@tonic-gate 		return(0);
569*0Sstevel@tonic-gate 	}
570*0Sstevel@tonic-gate }
571*0Sstevel@tonic-gate 
572*0Sstevel@tonic-gate /*
573*0Sstevel@tonic-gate  * Delete the indicated messages.
574*0Sstevel@tonic-gate  * Set dot to some nice place afterwards.
575*0Sstevel@tonic-gate  */
576*0Sstevel@tonic-gate int
577*0Sstevel@tonic-gate delm(int *msgvec)
578*0Sstevel@tonic-gate {
579*0Sstevel@tonic-gate 	register struct message *mp;
580*0Sstevel@tonic-gate 	register *ip, mesg;
581*0Sstevel@tonic-gate 	int last;
582*0Sstevel@tonic-gate 
583*0Sstevel@tonic-gate 	last = NULL;
584*0Sstevel@tonic-gate 	for (ip = msgvec; *ip != NULL; ip++) {
585*0Sstevel@tonic-gate 		mesg = *ip;
586*0Sstevel@tonic-gate 		touch(mesg);
587*0Sstevel@tonic-gate 		mp = &message[mesg-1];
588*0Sstevel@tonic-gate 		mp->m_flag |= MDELETED|MTOUCH;
589*0Sstevel@tonic-gate 		mp->m_flag &= ~(MPRESERVE|MSAVED|MBOX);
590*0Sstevel@tonic-gate 		last = mesg;
591*0Sstevel@tonic-gate 	}
592*0Sstevel@tonic-gate 	if (last != NULL) {
593*0Sstevel@tonic-gate 		dot = &message[last-1];
594*0Sstevel@tonic-gate 		last = first(0, MDELETED);
595*0Sstevel@tonic-gate 		if (last != NULL) {
596*0Sstevel@tonic-gate 			dot = &message[last-1];
597*0Sstevel@tonic-gate 			return(0);
598*0Sstevel@tonic-gate 		}
599*0Sstevel@tonic-gate 		else {
600*0Sstevel@tonic-gate 			dot = &message[0];
601*0Sstevel@tonic-gate 			return(-1);
602*0Sstevel@tonic-gate 		}
603*0Sstevel@tonic-gate 	}
604*0Sstevel@tonic-gate 
605*0Sstevel@tonic-gate 	/*
606*0Sstevel@tonic-gate 	 * Following can't happen -- it keeps lint happy
607*0Sstevel@tonic-gate 	 */
608*0Sstevel@tonic-gate 
609*0Sstevel@tonic-gate 	return(-1);
610*0Sstevel@tonic-gate }
611*0Sstevel@tonic-gate 
612*0Sstevel@tonic-gate /*
613*0Sstevel@tonic-gate  * Undelete the indicated messages.
614*0Sstevel@tonic-gate  */
615*0Sstevel@tonic-gate int
616*0Sstevel@tonic-gate undelete(int *msgvec)
617*0Sstevel@tonic-gate {
618*0Sstevel@tonic-gate 	register struct message *mp;
619*0Sstevel@tonic-gate 	register *ip, mesg;
620*0Sstevel@tonic-gate 
621*0Sstevel@tonic-gate 	for (ip = msgvec; ip-msgvec < msgCount; ip++) {
622*0Sstevel@tonic-gate 		mesg = *ip;
623*0Sstevel@tonic-gate 		if (mesg == 0)
624*0Sstevel@tonic-gate 			return(0);
625*0Sstevel@tonic-gate 		touch(mesg);
626*0Sstevel@tonic-gate 		mp = &message[mesg-1];
627*0Sstevel@tonic-gate 		dot = mp;
628*0Sstevel@tonic-gate 		mp->m_flag &= ~MDELETED;
629*0Sstevel@tonic-gate 	}
630*0Sstevel@tonic-gate 	return(0);
631*0Sstevel@tonic-gate }
632*0Sstevel@tonic-gate 
633*0Sstevel@tonic-gate /*
634*0Sstevel@tonic-gate  * Add the given header fields to the retained list.
635*0Sstevel@tonic-gate  * If no arguments, print the current list of retained fields.
636*0Sstevel@tonic-gate  */
637*0Sstevel@tonic-gate int
638*0Sstevel@tonic-gate retfield(char *list[])
639*0Sstevel@tonic-gate {
640*0Sstevel@tonic-gate 	char field[BUFSIZ];
641*0Sstevel@tonic-gate 	register int h;
642*0Sstevel@tonic-gate 	register struct ignore *igp;
643*0Sstevel@tonic-gate 	char **ap;
644*0Sstevel@tonic-gate 
645*0Sstevel@tonic-gate 	if (argcount(list) == 0)
646*0Sstevel@tonic-gate 		return(retshow());
647*0Sstevel@tonic-gate 	for (ap = list; *ap != 0; ap++) {
648*0Sstevel@tonic-gate 		istrcpy(field, sizeof (field), *ap);
649*0Sstevel@tonic-gate 
650*0Sstevel@tonic-gate 		if (member(field, retain))
651*0Sstevel@tonic-gate 			continue;
652*0Sstevel@tonic-gate 
653*0Sstevel@tonic-gate 		h = hash(field);
654*0Sstevel@tonic-gate 		if ((igp = (struct ignore *)
655*0Sstevel@tonic-gate 		    calloc(1, sizeof (struct ignore))) == NULL) {
656*0Sstevel@tonic-gate 			panic("Couldn't allocate memory");
657*0Sstevel@tonic-gate 		}
658*0Sstevel@tonic-gate 		if ((igp->i_field = (char *)
659*0Sstevel@tonic-gate 		    calloc(strlen(field) + 1, sizeof (char))) == NULL) {
660*0Sstevel@tonic-gate 			panic("Couldn't allocate memory");
661*0Sstevel@tonic-gate 		}
662*0Sstevel@tonic-gate 		strcpy(igp->i_field, field);
663*0Sstevel@tonic-gate 		igp->i_link = retain[h];
664*0Sstevel@tonic-gate 		retain[h] = igp;
665*0Sstevel@tonic-gate 		nretained++;
666*0Sstevel@tonic-gate 	}
667*0Sstevel@tonic-gate 	return(0);
668*0Sstevel@tonic-gate }
669*0Sstevel@tonic-gate 
670*0Sstevel@tonic-gate /*
671*0Sstevel@tonic-gate  * Print out all currently retained fields.
672*0Sstevel@tonic-gate  */
673*0Sstevel@tonic-gate static int
674*0Sstevel@tonic-gate retshow(void)
675*0Sstevel@tonic-gate {
676*0Sstevel@tonic-gate 	register int h, count;
677*0Sstevel@tonic-gate 	struct ignore *igp;
678*0Sstevel@tonic-gate 	char **ap, **ring;
679*0Sstevel@tonic-gate 
680*0Sstevel@tonic-gate 	count = 0;
681*0Sstevel@tonic-gate 	for (h = 0; h < HSHSIZE; h++)
682*0Sstevel@tonic-gate 		for (igp = retain[h]; igp != 0; igp = igp->i_link)
683*0Sstevel@tonic-gate 			count++;
684*0Sstevel@tonic-gate 	if (count == 0) {
685*0Sstevel@tonic-gate 		printf(gettext("No fields currently being retained.\n"));
686*0Sstevel@tonic-gate 		return(0);
687*0Sstevel@tonic-gate 	}
688*0Sstevel@tonic-gate 	ring = (char **) salloc((count + 1) * sizeof (char *));
689*0Sstevel@tonic-gate 	ap = ring;
690*0Sstevel@tonic-gate 	for (h = 0; h < HSHSIZE; h++)
691*0Sstevel@tonic-gate 		for (igp = retain[h]; igp != 0; igp = igp->i_link)
692*0Sstevel@tonic-gate 			*ap++ = igp->i_field;
693*0Sstevel@tonic-gate 	*ap = 0;
694*0Sstevel@tonic-gate 	qsort(ring, count, sizeof (char *), igcomp);
695*0Sstevel@tonic-gate 	for (ap = ring; *ap != 0; ap++)
696*0Sstevel@tonic-gate 		printf("%s\n", *ap);
697*0Sstevel@tonic-gate 	return(0);
698*0Sstevel@tonic-gate }
699*0Sstevel@tonic-gate 
700*0Sstevel@tonic-gate /*
701*0Sstevel@tonic-gate  * Remove a list of fields from the retain list.
702*0Sstevel@tonic-gate  */
703*0Sstevel@tonic-gate int
704*0Sstevel@tonic-gate unretfield(char *list[])
705*0Sstevel@tonic-gate {
706*0Sstevel@tonic-gate 	char **ap, field[BUFSIZ];
707*0Sstevel@tonic-gate 	register int h, count = 0;
708*0Sstevel@tonic-gate 	register struct ignore *ig1, *ig2;
709*0Sstevel@tonic-gate 
710*0Sstevel@tonic-gate 	if (argcount(list) == 0) {
711*0Sstevel@tonic-gate 		for (h = 0; h < HSHSIZE; h++) {
712*0Sstevel@tonic-gate 			ig1 = retain[h];
713*0Sstevel@tonic-gate 			while (ig1) {
714*0Sstevel@tonic-gate 				free(ig1->i_field);
715*0Sstevel@tonic-gate 				ig2 = ig1->i_link;
716*0Sstevel@tonic-gate 				free((char *) ig1);
717*0Sstevel@tonic-gate 				ig1 = ig2;
718*0Sstevel@tonic-gate 				count++;
719*0Sstevel@tonic-gate 			}
720*0Sstevel@tonic-gate 			retain[h] = NULL;
721*0Sstevel@tonic-gate 		}
722*0Sstevel@tonic-gate 		if (count == 0)
723*0Sstevel@tonic-gate 			printf(gettext(
724*0Sstevel@tonic-gate 			    "No fields currently being retained.\n"));
725*0Sstevel@tonic-gate 		nretained = 0;
726*0Sstevel@tonic-gate 		return 0;
727*0Sstevel@tonic-gate 	}
728*0Sstevel@tonic-gate 	for (ap = list; *ap; ap++) {
729*0Sstevel@tonic-gate 		istrcpy(field, sizeof (field), *ap);
730*0Sstevel@tonic-gate 		h = hash(field);
731*0Sstevel@tonic-gate 		for (ig1 = retain[h]; ig1; ig2 = ig1, ig1 = ig1->i_link)
732*0Sstevel@tonic-gate 			if (strcmp(ig1->i_field, field) == 0) {
733*0Sstevel@tonic-gate 				if (ig1 == retain[h])
734*0Sstevel@tonic-gate 					retain[h] = ig1->i_link;
735*0Sstevel@tonic-gate 				else
736*0Sstevel@tonic-gate 					ig2->i_link = ig1->i_link;
737*0Sstevel@tonic-gate 				free(ig1->i_field);
738*0Sstevel@tonic-gate 				free((char *) ig1);
739*0Sstevel@tonic-gate 				nretained--;
740*0Sstevel@tonic-gate 				break;
741*0Sstevel@tonic-gate 			}
742*0Sstevel@tonic-gate 	}
743*0Sstevel@tonic-gate 	return 0;
744*0Sstevel@tonic-gate }
745*0Sstevel@tonic-gate 
746*0Sstevel@tonic-gate /*
747*0Sstevel@tonic-gate  * Add the given header fields to the ignored list.
748*0Sstevel@tonic-gate  * If no arguments, print the current list of ignored fields.
749*0Sstevel@tonic-gate  */
750*0Sstevel@tonic-gate int
751*0Sstevel@tonic-gate igfield(char *list[])
752*0Sstevel@tonic-gate {
753*0Sstevel@tonic-gate 	char field[BUFSIZ];
754*0Sstevel@tonic-gate 	register int h;
755*0Sstevel@tonic-gate 	register struct ignore *igp;
756*0Sstevel@tonic-gate 	char **ap;
757*0Sstevel@tonic-gate 
758*0Sstevel@tonic-gate 	if (argcount(list) == 0)
759*0Sstevel@tonic-gate 		return(igshow());
760*0Sstevel@tonic-gate 	for (ap = list; *ap != 0; ap++) {
761*0Sstevel@tonic-gate 		if (isign(*ap, 0))
762*0Sstevel@tonic-gate 			continue;
763*0Sstevel@tonic-gate 		istrcpy(field, sizeof (field), *ap);
764*0Sstevel@tonic-gate 		h = hash(field);
765*0Sstevel@tonic-gate 		if ((igp = (struct ignore *)
766*0Sstevel@tonic-gate 		    calloc(1, sizeof (struct ignore))) == NULL) {
767*0Sstevel@tonic-gate 			panic("Couldn't allocate memory");
768*0Sstevel@tonic-gate 		}
769*0Sstevel@tonic-gate 		if ((igp->i_field = (char *)
770*0Sstevel@tonic-gate 		    calloc((unsigned)strlen(field) + 1,
771*0Sstevel@tonic-gate 		    sizeof (char))) == NULL) {
772*0Sstevel@tonic-gate 			panic("Couldn't allocate memory");
773*0Sstevel@tonic-gate 		}
774*0Sstevel@tonic-gate 		strcpy(igp->i_field, field);
775*0Sstevel@tonic-gate 		igp->i_link = ignore[h];
776*0Sstevel@tonic-gate 		ignore[h] = igp;
777*0Sstevel@tonic-gate 	}
778*0Sstevel@tonic-gate 	return(0);
779*0Sstevel@tonic-gate }
780*0Sstevel@tonic-gate 
781*0Sstevel@tonic-gate /*
782*0Sstevel@tonic-gate  * Print out all currently ignored fields.
783*0Sstevel@tonic-gate  */
784*0Sstevel@tonic-gate static int
785*0Sstevel@tonic-gate igshow(void)
786*0Sstevel@tonic-gate {
787*0Sstevel@tonic-gate 	register int h, count;
788*0Sstevel@tonic-gate 	struct ignore *igp;
789*0Sstevel@tonic-gate 	char **ap, **ring;
790*0Sstevel@tonic-gate 
791*0Sstevel@tonic-gate 	count = 0;
792*0Sstevel@tonic-gate 	for (h = 0; h < HSHSIZE; h++)
793*0Sstevel@tonic-gate 		for (igp = ignore[h]; igp != 0; igp = igp->i_link)
794*0Sstevel@tonic-gate 			count++;
795*0Sstevel@tonic-gate 	if (count == 0) {
796*0Sstevel@tonic-gate 		printf(gettext("No fields currently being ignored.\n"));
797*0Sstevel@tonic-gate 		return(0);
798*0Sstevel@tonic-gate 	}
799*0Sstevel@tonic-gate 	ring = (char **) salloc((count + 1) * sizeof (char *));
800*0Sstevel@tonic-gate 	ap = ring;
801*0Sstevel@tonic-gate 	for (h = 0; h < HSHSIZE; h++)
802*0Sstevel@tonic-gate 		for (igp = ignore[h]; igp != 0; igp = igp->i_link)
803*0Sstevel@tonic-gate 			*ap++ = igp->i_field;
804*0Sstevel@tonic-gate 	*ap = 0;
805*0Sstevel@tonic-gate 	qsort((char *) ring, (unsigned) count, sizeof (char *), igcomp);
806*0Sstevel@tonic-gate 	for (ap = ring; *ap != 0; ap++)
807*0Sstevel@tonic-gate 		printf("%s\n", *ap);
808*0Sstevel@tonic-gate 	return(0);
809*0Sstevel@tonic-gate }
810*0Sstevel@tonic-gate 
811*0Sstevel@tonic-gate /*
812*0Sstevel@tonic-gate  * Compare two names for sorting ignored field list.
813*0Sstevel@tonic-gate  */
814*0Sstevel@tonic-gate static int
815*0Sstevel@tonic-gate igcomp(const void *l, const void *r)
816*0Sstevel@tonic-gate {
817*0Sstevel@tonic-gate 	return(strcmp(*(char **)l, *(char **)r));
818*0Sstevel@tonic-gate }
819*0Sstevel@tonic-gate 
820*0Sstevel@tonic-gate /*
821*0Sstevel@tonic-gate  * Remove a list of fields from the ignore list.
822*0Sstevel@tonic-gate  */
823*0Sstevel@tonic-gate int
824*0Sstevel@tonic-gate unigfield(char *list[])
825*0Sstevel@tonic-gate {
826*0Sstevel@tonic-gate 	char **ap, field[BUFSIZ];
827*0Sstevel@tonic-gate 	register int h, count = 0;
828*0Sstevel@tonic-gate 	register struct ignore *ig1, *ig2;
829*0Sstevel@tonic-gate 
830*0Sstevel@tonic-gate 	if (argcount(list) == 0) {
831*0Sstevel@tonic-gate 		for (h = 0; h < HSHSIZE; h++) {
832*0Sstevel@tonic-gate 			ig1 = ignore[h];
833*0Sstevel@tonic-gate 			while (ig1) {
834*0Sstevel@tonic-gate 				free(ig1->i_field);
835*0Sstevel@tonic-gate 				ig2 = ig1->i_link;
836*0Sstevel@tonic-gate 				free((char *) ig1);
837*0Sstevel@tonic-gate 				ig1 = ig2;
838*0Sstevel@tonic-gate 				count++;
839*0Sstevel@tonic-gate 			}
840*0Sstevel@tonic-gate 			ignore[h] = NULL;
841*0Sstevel@tonic-gate 		}
842*0Sstevel@tonic-gate 		if (count == 0)
843*0Sstevel@tonic-gate 			printf(gettext("No fields currently being ignored.\n"));
844*0Sstevel@tonic-gate 		return 0;
845*0Sstevel@tonic-gate 	}
846*0Sstevel@tonic-gate 	for (ap = list; *ap; ap++) {
847*0Sstevel@tonic-gate 		istrcpy(field, sizeof (field), *ap);
848*0Sstevel@tonic-gate 		h = hash(field);
849*0Sstevel@tonic-gate 		for (ig1 = ignore[h]; ig1; ig2 = ig1, ig1 = ig1->i_link)
850*0Sstevel@tonic-gate 			if (strcmp(ig1->i_field, field) == 0) {
851*0Sstevel@tonic-gate 				if (ig1 == ignore[h])
852*0Sstevel@tonic-gate 					ignore[h] = ig1->i_link;
853*0Sstevel@tonic-gate 				else
854*0Sstevel@tonic-gate 					ig2->i_link = ig1->i_link;
855*0Sstevel@tonic-gate 				free(ig1->i_field);
856*0Sstevel@tonic-gate 				free((char *) ig1);
857*0Sstevel@tonic-gate 				break;
858*0Sstevel@tonic-gate 			}
859*0Sstevel@tonic-gate 	}
860*0Sstevel@tonic-gate 	return 0;
861*0Sstevel@tonic-gate }
862