xref: /netbsd-src/usr.bin/mail/edit.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: edit.c,v 1.18 2003/08/07 11:14:37 agc 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.18 2003/08/07 11:14:37 agc Exp $");
38 #endif
39 #endif /* not lint */
40 
41 #include "rcv.h"
42 #include "extern.h"
43 
44 /*
45  * Mail -- a mail program
46  *
47  * Perform message editing functions.
48  */
49 extern char *tmpdir;
50 
51 /*
52  * Edit a message list.
53  */
54 int
55 editor(void *v)
56 {
57 	int *msgvec = v;
58 
59 	return edit1(msgvec, 'e');
60 }
61 
62 /*
63  * Invoke the visual editor on a message list.
64  */
65 int
66 visual(void *v)
67 {
68 	int *msgvec = v;
69 
70 	return edit1(msgvec, 'v');
71 }
72 
73 /*
74  * Edit a message by writing the message into a funnily-named file
75  * (which should not exist) and forking an editor on it.
76  * We get the editor from the stuff above.
77  */
78 int
79 edit1(int *msgvec, int editortype)
80 {
81 	int c;
82 	int i;
83 	FILE *fp;
84 	struct message *mp;
85 	off_t size;
86 
87 	/*
88 	 * Deal with each message to be edited . . .
89 	 */
90 	for (i = 0; msgvec[i] && i < msgCount; i++) {
91 		sig_t sigint;
92 
93 		if (i > 0) {
94 			char buf[100];
95 			char *p;
96 
97 			printf("Edit message %d [ynq]? ", msgvec[i]);
98 			if (fgets(buf, sizeof buf, stdin) == 0)
99 				break;
100 			for (p = buf; *p == ' ' || *p == '\t'; p++)
101 				;
102 			if (*p == 'q')
103 				break;
104 			if (*p == 'n')
105 				continue;
106 		}
107 		dot = mp = &message[msgvec[i] - 1];
108 		touch(mp);
109 		sigint = signal(SIGINT, SIG_IGN);
110 		fp = run_editor(setinput(mp), mp->m_size, editortype,
111 				readonly);
112 		if (fp != NULL) {
113 			(void)fseek(otf, 0L, 2);
114 			size = ftell(otf);
115 			mp->m_block = blockof(size);
116 			mp->m_offset = offsetof(size);
117 			mp->m_size = fsize(fp);
118 			mp->m_lines = 0;
119 			mp->m_blines = 0;
120 			mp->m_flag |= MODIFY;
121 			rewind(fp);
122 			while ((c = getc(fp)) != EOF) {
123 				//
124 				// XXX. if you edit a message, we treat
125 				// header lines as body lines in the recount.
126 				// This is because the original message copy
127 				// and the edit reread use different code to
128 				// count the lines, and this one here is
129 				// simple-minded.
130 				//
131 				if (c == '\n') {
132 					mp->m_lines++;
133 					mp->m_blines++;
134 				}
135 				if (putc(c, otf) == EOF)
136 					break;
137 			}
138 			if (ferror(otf))
139 				warn("/tmp");
140 			(void)Fclose(fp);
141 		}
142 		(void)signal(SIGINT, sigint);
143 	}
144 	return 0;
145 }
146 
147 /*
148  * Run an editor on the file at "fpp" of "size" bytes,
149  * and return a new file pointer.
150  * Signals must be handled by the caller.
151  * "Editortype" is 'e' for _PATH_EX, 'v' for _PATH_VI.
152  */
153 FILE *
154 run_editor(FILE *fp, off_t size, int editortype, int readonlyflag)
155 {
156 	FILE *nf = NULL;
157 	int t;
158 	time_t modtime;
159 	char *editcmd;
160 	char tempname[PATHSIZE];
161 	struct stat statb;
162 
163 	(void)snprintf(tempname, sizeof(tempname),
164 	    "%s/mail.ReXXXXXXXXXX", tmpdir);
165 	if ((t = mkstemp(tempname)) == -1) {
166 		warn("%s", tempname);
167 		goto out;
168 	}
169 	if (readonlyflag && fchmod(t, 0400) == -1) {
170 		close(t);
171 		warn("%s", tempname);
172 		(void)unlink(tempname);
173 		goto out;
174 	}
175 	if ((nf = Fdopen(t, "w")) == NULL) {
176 		close(t);
177 		warn("%s", tempname);
178 		(void)unlink(tempname);
179 		goto out;
180 	}
181 	if (size >= 0)
182 		while (--size >= 0 && (t = getc(fp)) != EOF)
183 			(void)putc(t, nf);
184 	else
185 		while ((t = getc(fp)) != EOF)
186 			(void)putc(t, nf);
187 	(void)fflush(nf);
188 	if (ferror(nf)) {
189 		(void)Fclose(nf);
190 		warn("%s", tempname);
191 		(void)unlink(tempname);
192 		nf = NULL;
193 		goto out;
194 	}
195 	if (fstat(fileno(nf), &statb) < 0)
196 		modtime = 0;
197 	else
198 		modtime = statb.st_mtime;
199 	if (Fclose(nf) < 0) {
200 		warn("%s", tempname);
201 		(void)unlink(tempname);
202 		nf = NULL;
203 		goto out;
204 	}
205 	nf = NULL;
206 	if ((editcmd =
207 	         value(editortype == 'e' ? "EDITOR" : "VISUAL")) == NULL)
208 		editcmd = editortype == 'e' ? _PATH_EX : _PATH_VI;
209 	if (run_command(editcmd, 0, 0, -1, tempname, NULL) < 0) {
210 		(void)unlink(tempname);
211 		goto out;
212 	}
213 	/*
214 	 * If in read only mode or file unchanged, just remove the editor
215 	 * temporary and return.
216 	 */
217 	if (readonlyflag) {
218 		(void)unlink(tempname);
219 		goto out;
220 	}
221 	if (stat(tempname, &statb) < 0) {
222 		warn("%s", tempname);
223 		goto out;
224 	}
225 	if (modtime == statb.st_mtime) {
226 		(void)unlink(tempname);
227 		goto out;
228 	}
229 	/*
230 	 * Now switch to new file.
231 	 */
232 	if ((nf = Fopen(tempname, "a+")) == NULL) {
233 		warn("%s", tempname);
234 		(void)unlink(tempname);
235 		goto out;
236 	}
237 	(void)unlink(tempname);
238 out:
239 	return nf;
240 }
241