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