xref: /netbsd-src/usr.bin/mail/edit.c (revision ce099b40997c43048fb78bd578195f81d2456523)
1 /*	$NetBSD: edit.c,v 1.24 2007/10/29 23:20:38 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1980, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)edit.c	8.1 (Berkeley) 6/6/93";
36 #else
37 __RCSID("$NetBSD: edit.c,v 1.24 2007/10/29 23:20:38 christos Exp $");
38 #endif
39 #endif /* not lint */
40 
41 #include "rcv.h"
42 #include "extern.h"
43 #include "thread.h"
44 
45 /*
46  * Mail -- a mail program
47  *
48  * Perform message editing functions.
49  */
50 
51 /*
52  * Run an editor on the file at "fpp" of "size" bytes,
53  * and return a new file pointer.
54  * Signals must be handled by the caller.
55  * "Editortype" is 'e' for _PATH_EX, 'v' for _PATH_VI.
56  */
57 PUBLIC FILE *
58 run_editor(FILE *fp, off_t size, int editortype, int readonlyflag)
59 {
60 	FILE *nf = NULL;
61 	int t;
62 	time_t modtime;
63 	const char *editcmd;
64 	char tempname[PATHSIZE];
65 	struct stat statb;
66 
67 	(void)snprintf(tempname, sizeof(tempname),
68 	    "%s/mail.ReXXXXXXXXXX", tmpdir);
69 	if ((t = mkstemp(tempname)) == -1) {
70 		warn("%s", tempname);
71 		goto out;
72 	}
73 	if (readonlyflag && fchmod(t, 0400) == -1) {
74 		(void)close(t);
75 		warn("%s", tempname);
76 		(void)unlink(tempname);
77 		goto out;
78 	}
79 	if ((nf = Fdopen(t, "w")) == NULL) {
80 		(void)close(t);
81 		warn("%s", tempname);
82 		(void)unlink(tempname);
83 		goto out;
84 	}
85 	if (size >= 0)
86 		while (--size >= 0 && (t = getc(fp)) != EOF)
87 			(void)putc(t, nf);
88 	else
89 		while ((t = getc(fp)) != EOF)
90 			(void)putc(t, nf);
91 	(void)fflush(nf);
92 	if (ferror(nf)) {
93 		(void)Fclose(nf);
94 		warn("%s", tempname);
95 		(void)unlink(tempname);
96 		nf = NULL;
97 		goto out;
98 	}
99 	if (fstat(fileno(nf), &statb) < 0)
100 		modtime = 0;
101 	else
102 		modtime = statb.st_mtime;
103 	if (Fclose(nf) < 0) {
104 		warn("%s", tempname);
105 		(void)unlink(tempname);
106 		nf = NULL;
107 		goto out;
108 	}
109 	nf = NULL;
110 	if ((editcmd =
111 		value(editortype == 'e' ? ENAME_EDITOR : ENAME_VISUAL)) == NULL)
112 		editcmd = editortype == 'e' ? _PATH_EX : _PATH_VI;
113 	if (run_command(editcmd, 0, 0, -1, tempname, NULL) < 0) {
114 		(void)unlink(tempname);
115 		goto out;
116 	}
117 	/*
118 	 * If in read only mode or file unchanged, just remove the editor
119 	 * temporary and return.
120 	 */
121 	if (readonlyflag) {
122 		(void)unlink(tempname);
123 		goto out;
124 	}
125 	if (stat(tempname, &statb) < 0) {
126 		warn("%s", tempname);
127 		goto out;
128 	}
129 	if (modtime == statb.st_mtime) {
130 		(void)unlink(tempname);
131 		goto out;
132 	}
133 	/*
134 	 * Now switch to new file.
135 	 */
136 	if ((nf = Fopen(tempname, "a+")) == NULL) {
137 		warn("%s", tempname);
138 		(void)unlink(tempname);
139 		goto out;
140 	}
141 	(void)unlink(tempname);
142 out:
143 	return nf;
144 }
145 
146 /*
147  * Edit a message by writing the message into a funnily-named file
148  * (which should not exist) and forking an editor on it.
149  * We get the editor from the stuff above.
150  */
151 static int
152 edit1(int *msgvec, int editortype)
153 {
154 	int c;
155 	int i;
156 	int msgCount;
157 	FILE *fp;
158 	struct message *mp;
159 	off_t size;
160 
161 	/*
162 	 * Deal with each message to be edited . . .
163 	 */
164 	msgCount = get_msgCount();
165 	for (i = 0; msgvec[i] && i < msgCount; i++) {
166 		sig_t sigint;
167 
168 		if (i > 0) {
169 			char buf[100];
170 			char *p;
171 
172 			(void)printf("Edit message %d [ynq]? ", msgvec[i]);
173 			if (fgets(buf, sizeof(buf), stdin) == 0)
174 				break;
175 			p = skip_WSP(buf);
176 			if (*p == 'q')
177 				break;
178 			if (*p == 'n')
179 				continue;
180 		}
181 		dot = mp = get_message(msgvec[i]);
182 		touch(mp);
183 		sigint = signal(SIGINT, SIG_IGN);
184 		fp = run_editor(setinput(mp), mp->m_size, editortype,
185 				readonly);
186 		if (fp != NULL) {
187 			(void)fseek(otf, 0L, 2);
188 			size = ftell(otf);
189 			mp->m_block = blockof(size);
190 			mp->m_offset = blkoffsetof(size);
191 			mp->m_size = fsize(fp);
192 			mp->m_lines = 0;
193 			mp->m_blines = 0;
194 			mp->m_flag |= MMODIFY;
195 			rewind(fp);
196 			while ((c = getc(fp)) != EOF) {
197 				/*
198 				 * XXX. if you edit a message, we treat
199 				 * header lines as body lines in the recount.
200 				 * This is because the original message copy
201 				 * and the edit reread use different code to
202 				 * count the lines, and this one here is
203 				 * simple-minded.
204 				 */
205 				if (c == '\n') {
206 					mp->m_lines++;
207 					mp->m_blines++;
208 				}
209 				if (putc(c, otf) == EOF)
210 					break;
211 			}
212 			if (ferror(otf))
213 				warn("/tmp");
214 			(void)Fclose(fp);
215 		}
216 		(void)signal(SIGINT, sigint);
217 	}
218 	return 0;
219 }
220 
221 /*
222  * Edit a message list.
223  */
224 PUBLIC int
225 editor(void *v)
226 {
227 	int *msgvec = v;
228 
229 	return edit1(msgvec, 'e');
230 }
231 
232 /*
233  * Invoke the visual editor on a message list.
234  */
235 PUBLIC int
236 visual(void *v)
237 {
238 	int *msgvec = v;
239 
240 	return edit1(msgvec, 'v');
241 }
242