xref: /netbsd-src/usr.bin/mail/edit.c (revision 4fe1ef32f37334167b71a62286b21da7d9542909)
1*4fe1ef32Schristos /*	$NetBSD: edit.c,v 1.29 2017/11/09 20:27:50 christos Exp $	*/
288b833a7Schristos 
361f28255Scgd /*
42cb5542fSderaadt  * Copyright (c) 1980, 1993
52cb5542fSderaadt  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
1589aaa1bbSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
327c81c8f3Slukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3488b833a7Schristos #if 0
3588b833a7Schristos static char sccsid[] = "@(#)edit.c	8.1 (Berkeley) 6/6/93";
3688b833a7Schristos #else
37*4fe1ef32Schristos __RCSID("$NetBSD: edit.c,v 1.29 2017/11/09 20:27:50 christos Exp $");
3888b833a7Schristos #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd 
4161f28255Scgd #include "rcv.h"
422cb5542fSderaadt #include "extern.h"
43f3098750Schristos #include "thread.h"
44ca13337dSchristos #include "sig.h"
4561f28255Scgd 
4661f28255Scgd /*
4761f28255Scgd  * Mail -- a mail program
4861f28255Scgd  *
4961f28255Scgd  * Perform message editing functions.
5061f28255Scgd  */
5161f28255Scgd 
5261f28255Scgd /*
5361f28255Scgd  * Run an editor on the file at "fpp" of "size" bytes,
5461f28255Scgd  * and return a new file pointer.
5561f28255Scgd  * Signals must be handled by the caller.
564e972651Swiz  * "Editortype" is 'e' for _PATH_EX, 'v' for _PATH_VI.
5761f28255Scgd  */
58f3098750Schristos PUBLIC FILE *
run_editor(FILE * fp,off_t size,int editortype,int readonlyflag)594e972651Swiz run_editor(FILE *fp, off_t size, int editortype, int readonlyflag)
6061f28255Scgd {
617c81c8f3Slukem 	FILE *nf = NULL;
627c81c8f3Slukem 	int t;
6361f28255Scgd 	time_t modtime;
64ece0fd5cSchristos 	const char *editcmd;
65443084c8Swiz 	char tempname[PATHSIZE];
6661f28255Scgd 	struct stat statb;
6761f28255Scgd 
68443084c8Swiz 	(void)snprintf(tempname, sizeof(tempname),
69443084c8Swiz 	    "%s/mail.ReXXXXXXXXXX", tmpdir);
70443084c8Swiz 	if ((t = mkstemp(tempname)) == -1) {
71443084c8Swiz 		warn("%s", tempname);
72443084c8Swiz 		goto out;
73443084c8Swiz 	}
74443084c8Swiz 	if (readonlyflag && fchmod(t, 0400) == -1) {
75ca286310Schristos 		(void)close(t);
76443084c8Swiz 		warn("%s", tempname);
77443084c8Swiz 		(void)unlink(tempname);
7861f28255Scgd 		goto out;
7961f28255Scgd 	}
80*4fe1ef32Schristos 	if ((nf = Fdopen(t, "wef")) == NULL) {
81ca286310Schristos 		(void)close(t);
82443084c8Swiz 		warn("%s", tempname);
83443084c8Swiz 		(void)unlink(tempname);
8461f28255Scgd 		goto out;
8561f28255Scgd 	}
8661f28255Scgd 	if (size >= 0)
8761f28255Scgd 		while (--size >= 0 && (t = getc(fp)) != EOF)
8861f28255Scgd 			(void)putc(t, nf);
8961f28255Scgd 	else
9061f28255Scgd 		while ((t = getc(fp)) != EOF)
9161f28255Scgd 			(void)putc(t, nf);
9261f28255Scgd 	(void)fflush(nf);
9361f28255Scgd 	if (ferror(nf)) {
9461f28255Scgd 		(void)Fclose(nf);
95443084c8Swiz 		warn("%s", tempname);
96443084c8Swiz 		(void)unlink(tempname);
9761f28255Scgd 		nf = NULL;
9861f28255Scgd 		goto out;
9961f28255Scgd 	}
1002f2497e5Sbad 	if (fstat(fileno(nf), &statb) < 0)
1012f2497e5Sbad 		modtime = 0;
1022f2497e5Sbad 	else
1032f2497e5Sbad 		modtime = statb.st_mtime;
10461f28255Scgd 	if (Fclose(nf) < 0) {
105443084c8Swiz 		warn("%s", tempname);
106443084c8Swiz 		(void)unlink(tempname);
10761f28255Scgd 		nf = NULL;
10861f28255Scgd 		goto out;
10961f28255Scgd 	}
11061f28255Scgd 	nf = NULL;
111ca13337dSchristos 	editcmd = value(editortype == 'e' ? ENAME_EDITOR : ENAME_VISUAL);
112ca13337dSchristos 	if (editcmd == NULL)
1134e972651Swiz 		editcmd = editortype == 'e' ? _PATH_EX : _PATH_VI;
114ca13337dSchristos 	if (run_command(editcmd, NULL, 0, -1, tempname, NULL) < 0) {
115443084c8Swiz 		(void)unlink(tempname);
11661f28255Scgd 		goto out;
11761f28255Scgd 	}
11861f28255Scgd 	/*
11961f28255Scgd 	 * If in read only mode or file unchanged, just remove the editor
12061f28255Scgd 	 * temporary and return.
12161f28255Scgd 	 */
1224e972651Swiz 	if (readonlyflag) {
123443084c8Swiz 		(void)unlink(tempname);
12461f28255Scgd 		goto out;
12561f28255Scgd 	}
126443084c8Swiz 	if (stat(tempname, &statb) < 0) {
127443084c8Swiz 		warn("%s", tempname);
12861f28255Scgd 		goto out;
12961f28255Scgd 	}
13061f28255Scgd 	if (modtime == statb.st_mtime) {
131443084c8Swiz 		(void)unlink(tempname);
13261f28255Scgd 		goto out;
13361f28255Scgd 	}
13461f28255Scgd 	/*
13561f28255Scgd 	 * Now switch to new file.
13661f28255Scgd 	 */
137*4fe1ef32Schristos 	if ((nf = Fopen(tempname, "aef+")) == NULL) {
138443084c8Swiz 		warn("%s", tempname);
139443084c8Swiz 		(void)unlink(tempname);
14061f28255Scgd 		goto out;
14161f28255Scgd 	}
142443084c8Swiz 	(void)unlink(tempname);
14361f28255Scgd out:
14461f28255Scgd 	return nf;
14561f28255Scgd }
146f3098750Schristos 
147f3098750Schristos /*
148f3098750Schristos  * Edit a message by writing the message into a funnily-named file
149f3098750Schristos  * (which should not exist) and forking an editor on it.
150f3098750Schristos  * We get the editor from the stuff above.
151f3098750Schristos  */
152f3098750Schristos static int
edit1(int * msgvec,int editortype)153f3098750Schristos edit1(int *msgvec, int editortype)
154f3098750Schristos {
155f3098750Schristos 	int c;
156f3098750Schristos 	int i;
157f3098750Schristos 	int msgCount;
158f3098750Schristos 	FILE *fp;
159f3098750Schristos 	struct message *mp;
160f3098750Schristos 	off_t size;
161f3098750Schristos 
162f3098750Schristos 	/*
163f3098750Schristos 	 * Deal with each message to be edited . . .
164f3098750Schristos 	 */
165f3098750Schristos 	msgCount = get_msgCount();
166cdc14da5Schristos 	for (i = 0; i < msgCount && msgvec[i]; i++) {
167ca13337dSchristos 		sigset_t oset;
168ca13337dSchristos 		struct sigaction osa;
169f3098750Schristos 
170f3098750Schristos 		if (i > 0) {
171f3098750Schristos 			char buf[100];
172f3098750Schristos 			char *p;
173f3098750Schristos 
174f3098750Schristos 			(void)printf("Edit message %d [ynq]? ", msgvec[i]);
1757f5fd4a5Schristos 			if (fgets(buf, (int)sizeof(buf), stdin) == NULL)
176f3098750Schristos 				break;
177d727506fSchristos 			p = skip_WSP(buf);
178f3098750Schristos 			if (*p == 'q')
179f3098750Schristos 				break;
180f3098750Schristos 			if (*p == 'n')
181f3098750Schristos 				continue;
182f3098750Schristos 		}
183f3098750Schristos 		dot = mp = get_message(msgvec[i]);
184f3098750Schristos 		touch(mp);
185ca13337dSchristos 		sig_check();
186ca13337dSchristos 		(void)sig_ignore(SIGINT, &osa, &oset);
187f3098750Schristos 		fp = run_editor(setinput(mp), mp->m_size, editortype,
188f3098750Schristos 				readonly);
189f3098750Schristos 		if (fp != NULL) {
190f3098750Schristos 			(void)fseek(otf, 0L, 2);
191f3098750Schristos 			size = ftell(otf);
192f3098750Schristos 			mp->m_block = blockof(size);
193fc687570Sdogcow 			mp->m_offset = blkoffsetof(size);
194f3098750Schristos 			mp->m_size = fsize(fp);
195f3098750Schristos 			mp->m_lines = 0;
196f3098750Schristos 			mp->m_blines = 0;
197f3098750Schristos 			mp->m_flag |= MMODIFY;
198f3098750Schristos 			rewind(fp);
199f3098750Schristos 			while ((c = getc(fp)) != EOF) {
200f3098750Schristos 				/*
201f3098750Schristos 				 * XXX. if you edit a message, we treat
202f3098750Schristos 				 * header lines as body lines in the recount.
203f3098750Schristos 				 * This is because the original message copy
204f3098750Schristos 				 * and the edit reread use different code to
205f3098750Schristos 				 * count the lines, and this one here is
206f3098750Schristos 				 * simple-minded.
207f3098750Schristos 				 */
208f3098750Schristos 				if (c == '\n') {
209f3098750Schristos 					mp->m_lines++;
210f3098750Schristos 					mp->m_blines++;
211f3098750Schristos 				}
212f3098750Schristos 				if (putc(c, otf) == EOF)
213f3098750Schristos 					break;
214f3098750Schristos 			}
215f3098750Schristos 			if (ferror(otf))
216f3098750Schristos 				warn("/tmp");
217f3098750Schristos 			(void)Fclose(fp);
218f3098750Schristos 		}
219ca13337dSchristos 		(void)sig_restore(SIGINT, &osa, &oset);
220ca13337dSchristos 		sig_check();
221f3098750Schristos 	}
222f3098750Schristos 	return 0;
223f3098750Schristos }
224f3098750Schristos 
225f3098750Schristos /*
226f3098750Schristos  * Edit a message list.
227f3098750Schristos  */
228f3098750Schristos PUBLIC int
editor(void * v)229f3098750Schristos editor(void *v)
230f3098750Schristos {
231f3098750Schristos 	int *msgvec = v;
232f3098750Schristos 
233f3098750Schristos 	return edit1(msgvec, 'e');
234f3098750Schristos }
235f3098750Schristos 
236f3098750Schristos /*
237f3098750Schristos  * Invoke the visual editor on a message list.
238f3098750Schristos  */
239f3098750Schristos PUBLIC int
visual(void * v)240f3098750Schristos visual(void *v)
241f3098750Schristos {
242f3098750Schristos 	int *msgvec = v;
243f3098750Schristos 
244f3098750Schristos 	return edit1(msgvec, 'v');
245f3098750Schristos }
246