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