xref: /freebsd/usr.bin/mail/cmd2.c (revision ce4946daa5ce852d28008dac492029500ab2ee95)
1 /*
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)cmd2.c	8.1 (Berkeley) 6/6/93";
37 #endif
38 static const char rcsid[] =
39   "$FreeBSD$";
40 #endif /* not lint */
41 
42 #include "rcv.h"
43 #include <sys/wait.h>
44 #include "extern.h"
45 
46 /*
47  * Mail -- a mail program
48  *
49  * More user commands.
50  */
51 
52 /*
53  * If any arguments were given, go to the next applicable argument
54  * following dot, otherwise, go to the next applicable message.
55  * If given as first command with no arguments, print first message.
56  */
57 int
58 next(msgvec)
59 	int *msgvec;
60 {
61 	register struct message *mp;
62 	register int *ip, *ip2;
63 	int list[2], mdot;
64 
65 	if (*msgvec != 0) {
66 
67 		/*
68 		 * If some messages were supplied, find the
69 		 * first applicable one following dot using
70 		 * wrap around.
71 		 */
72 
73 		mdot = dot - &message[0] + 1;
74 
75 		/*
76 		 * Find the first message in the supplied
77 		 * message list which follows dot.
78 		 */
79 
80 		for (ip = msgvec; *ip != 0; ip++)
81 			if (*ip > mdot)
82 				break;
83 		if (*ip == 0)
84 			ip = msgvec;
85 		ip2 = ip;
86 		do {
87 			mp = &message[*ip2 - 1];
88 			if ((mp->m_flag & MDELETED) == 0) {
89 				dot = mp;
90 				goto hitit;
91 			}
92 			if (*ip2 != 0)
93 				ip2++;
94 			if (*ip2 == 0)
95 				ip2 = msgvec;
96 		} while (ip2 != ip);
97 		printf("No messages applicable\n");
98 		return(1);
99 	}
100 
101 	/*
102 	 * If this is the first command, select message 1.
103 	 * Note that this must exist for us to get here at all.
104 	 */
105 
106 	if (!sawcom)
107 		goto hitit;
108 
109 	/*
110 	 * Just find the next good message after dot, no
111 	 * wraparound.
112 	 */
113 
114 	for (mp = dot+1; mp < &message[msgCount]; mp++)
115 		if ((mp->m_flag & (MDELETED|MSAVED)) == 0)
116 			break;
117 	if (mp >= &message[msgCount]) {
118 		printf("At EOF\n");
119 		return(0);
120 	}
121 	dot = mp;
122 hitit:
123 	/*
124 	 * Print dot.
125 	 */
126 
127 	list[0] = dot - &message[0] + 1;
128 	list[1] = 0;
129 	return(type(list));
130 }
131 
132 /*
133  * Save a message in a file.  Mark the message as saved
134  * so we can discard when the user quits.
135  */
136 int
137 save(str)
138 	char str[];
139 {
140 
141 	return save1(str, 1, "save", saveignore);
142 }
143 
144 /*
145  * Copy a message to a file without affected its saved-ness
146  */
147 int
148 copycmd(str)
149 	char str[];
150 {
151 
152 	return save1(str, 0, "copy", saveignore);
153 }
154 
155 /*
156  * Save/copy the indicated messages at the end of the passed file name.
157  * If mark is true, mark the message "saved."
158  */
159 int
160 save1(str, mark, cmd, ignore)
161 	char str[];
162 	int mark;
163 	char *cmd;
164 	struct ignoretab *ignore;
165 {
166 	register int *ip;
167 	register struct message *mp;
168 	char *file, *disp;
169 	int f, *msgvec;
170 	FILE *obuf;
171 
172 	msgvec = (int *) salloc((msgCount + 2) * sizeof *msgvec);
173 	if ((file = snarf(str, &f)) == NOSTR)
174 		return(1);
175 	if (!f) {
176 		*msgvec = first(0, MMNORM);
177 		if (*msgvec == 0) {
178 			printf("No messages to %s.\n", cmd);
179 			return(1);
180 		}
181 		msgvec[1] = 0;
182 	}
183 	if (f && getmsglist(str, msgvec, 0) < 0)
184 		return(1);
185 	if ((file = expand(file)) == NOSTR)
186 		return(1);
187 	printf("\"%s\" ", file);
188 	fflush(stdout);
189 	if (access(file, 0) >= 0)
190 		disp = "[Appended]";
191 	else
192 		disp = "[New file]";
193 	if ((obuf = Fopen(file, "a")) == NULL) {
194 		warn(NOSTR);
195 		return(1);
196 	}
197 	for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
198 		mp = &message[*ip - 1];
199 		touch(mp);
200 		if (sendmessage(mp, obuf, ignore, NOSTR) < 0) {
201 			warnx("%s", file);
202 			Fclose(obuf);
203 			return(1);
204 		}
205 		if (mark)
206 			mp->m_flag |= MSAVED;
207 	}
208 	fflush(obuf);
209 	if (ferror(obuf))
210 		warn("%s", file);
211 	Fclose(obuf);
212 	printf("%s\n", disp);
213 	return(0);
214 }
215 
216 /*
217  * Write the indicated messages at the end of the passed
218  * file name, minus header and trailing blank line.
219  */
220 int
221 swrite(str)
222 	char str[];
223 {
224 
225 	return save1(str, 1, "write", ignoreall);
226 }
227 
228 /*
229  * Snarf the file from the end of the command line and
230  * return a pointer to it.  If there is no file attached,
231  * just return NOSTR.  Put a null in front of the file
232  * name so that the message list processing won't see it,
233  * unless the file name is the only thing on the line, in
234  * which case, return 0 in the reference flag variable.
235  */
236 
237 char *
238 snarf(linebuf, flag)
239 	char linebuf[];
240 	int *flag;
241 {
242 	register char *cp;
243 
244 	*flag = 1;
245 	cp = strlen(linebuf) + linebuf - 1;
246 
247 	/*
248 	 * Strip away trailing blanks.
249 	 */
250 
251 	while (cp > linebuf && isspace(*cp))
252 		cp--;
253 	*++cp = 0;
254 
255 	/*
256 	 * Now search for the beginning of the file name.
257 	 */
258 
259 	while (cp > linebuf && !isspace(*cp))
260 		cp--;
261 	if (*cp == '\0') {
262 		printf("No file specified.\n");
263 		return(NOSTR);
264 	}
265 	if (isspace(*cp))
266 		*cp++ = 0;
267 	else
268 		*flag = 0;
269 	return(cp);
270 }
271 
272 /*
273  * Delete messages.
274  */
275 int
276 delete(msgvec)
277 	int msgvec[];
278 {
279 	delm(msgvec);
280 	return 0;
281 }
282 
283 /*
284  * Delete messages, then type the new dot.
285  */
286 int
287 deltype(msgvec)
288 	int msgvec[];
289 {
290 	int list[2];
291 	int lastdot;
292 
293 	lastdot = dot - &message[0] + 1;
294 	if (delm(msgvec) >= 0) {
295 		list[0] = dot - &message[0] + 1;
296 		if (list[0] > lastdot) {
297 			touch(dot);
298 			list[1] = 0;
299 			return(type(list));
300 		}
301 		printf("At EOF\n");
302 	} else
303 		printf("No more messages\n");
304 	return(0);
305 }
306 
307 /*
308  * Delete the indicated messages.
309  * Set dot to some nice place afterwards.
310  * Internal interface.
311  */
312 int
313 delm(msgvec)
314 	int *msgvec;
315 {
316 	register struct message *mp;
317 	register *ip;
318 	int last;
319 
320 	last = 0;
321 	for (ip = msgvec; *ip != 0; ip++) {
322 		mp = &message[*ip - 1];
323 		touch(mp);
324 		mp->m_flag |= MDELETED|MTOUCH;
325 		mp->m_flag &= ~(MPRESERVE|MSAVED|MBOX);
326 		last = *ip;
327 	}
328 	if (last != 0) {
329 		dot = &message[last-1];
330 		last = first(0, MDELETED);
331 		if (last != 0) {
332 			dot = &message[last-1];
333 			return(0);
334 		}
335 		else {
336 			dot = &message[0];
337 			return(-1);
338 		}
339 	}
340 
341 	/*
342 	 * Following can't happen -- it keeps lint happy
343 	 */
344 
345 	return(-1);
346 }
347 
348 /*
349  * Undelete the indicated messages.
350  */
351 int
352 undelete_messages(msgvec)
353 	int *msgvec;
354 {
355 	register struct message *mp;
356 	register *ip;
357 
358 	for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
359 		mp = &message[*ip - 1];
360 		touch(mp);
361 		dot = mp;
362 		mp->m_flag &= ~MDELETED;
363 	}
364 	return 0;
365 }
366 
367 /*
368  * Interactively dump core on "core"
369  */
370 int
371 core()
372 {
373 	int pid;
374 	extern int wait_status;
375 
376 	switch (pid = fork()) {
377 	case -1:
378 		warn("fork");
379 		return(1);
380 	case 0:
381 		abort();
382 		_exit(1);
383 	}
384 	printf("Okie dokie");
385 	fflush(stdout);
386 	wait_child(pid);
387 	if (WIFSIGNALED(wait_status) && WCOREDUMP(wait_status))
388 		printf(" -- Core dumped.\n");
389 	else
390 		printf(" -- Can't dump core.\n");
391 	return 0;
392 }
393 
394 /*
395  * Clobber as many bytes of stack as the user requests.
396  */
397 int
398 clobber(argv)
399 	char **argv;
400 {
401 	register int times;
402 
403 	if (argv[0] == 0)
404 		times = 1;
405 	else
406 		times = (atoi(argv[0]) + 511) / 512;
407 	clob1(times);
408 	return 0;
409 }
410 
411 /*
412  * Clobber the stack.
413  */
414 void
415 clob1(n)
416 	int n;
417 {
418 	char buf[512];
419 	register char *cp;
420 
421 	if (n <= 0)
422 		return;
423 	for (cp = buf; cp < &buf[512]; *cp++ = 0xFF)
424 		;
425 	clob1(n - 1);
426 }
427 
428 /*
429  * Add the given header fields to the retained list.
430  * If no arguments, print the current list of retained fields.
431  */
432 int
433 retfield(list)
434 	char *list[];
435 {
436 
437 	return ignore1(list, ignore + 1, "retained");
438 }
439 
440 /*
441  * Add the given header fields to the ignored list.
442  * If no arguments, print the current list of ignored fields.
443  */
444 int
445 igfield(list)
446 	char *list[];
447 {
448 
449 	return ignore1(list, ignore, "ignored");
450 }
451 
452 int
453 saveretfield(list)
454 	char *list[];
455 {
456 
457 	return ignore1(list, saveignore + 1, "retained");
458 }
459 
460 int
461 saveigfield(list)
462 	char *list[];
463 {
464 
465 	return ignore1(list, saveignore, "ignored");
466 }
467 
468 int
469 ignore1(list, tab, which)
470 	char *list[];
471 	struct ignoretab *tab;
472 	char *which;
473 {
474 	char field[LINESIZE];
475 	register int h;
476 	register struct ignore *igp;
477 	char **ap;
478 
479 	if (*list == NOSTR)
480 		return igshow(tab, which);
481 	for (ap = list; *ap != 0; ap++) {
482 		istrncpy(field, *ap, sizeof(field));
483 		if (member(field, tab))
484 			continue;
485 		h = hash(field);
486 		igp = (struct ignore *) calloc(1, sizeof (struct ignore));
487 		igp->i_field = calloc((unsigned) strlen(field) + 1,
488 			sizeof (char));
489 		strcpy(igp->i_field, field);
490 		igp->i_link = tab->i_head[h];
491 		tab->i_head[h] = igp;
492 		tab->i_count++;
493 	}
494 	return 0;
495 }
496 
497 /*
498  * Print out all currently retained fields.
499  */
500 int
501 igshow(tab, which)
502 	struct ignoretab *tab;
503 	char *which;
504 {
505 	register int h;
506 	struct ignore *igp;
507 	char **ap, **ring;
508 	int igcomp();
509 
510 	if (tab->i_count == 0) {
511 		printf("No fields currently being %s.\n", which);
512 		return 0;
513 	}
514 	ring = (char **) salloc((tab->i_count + 1) * sizeof (char *));
515 	ap = ring;
516 	for (h = 0; h < HSHSIZE; h++)
517 		for (igp = tab->i_head[h]; igp != 0; igp = igp->i_link)
518 			*ap++ = igp->i_field;
519 	*ap = 0;
520 	qsort(ring, tab->i_count, sizeof (char *), igcomp);
521 	for (ap = ring; *ap != 0; ap++)
522 		printf("%s\n", *ap);
523 	return 0;
524 }
525 
526 /*
527  * Compare two names for sorting ignored field list.
528  */
529 int
530 igcomp(l, r)
531 	const void *l, *r;
532 {
533 	return (strcmp(*(char **)l, *(char **)r));
534 }
535