edit.c revision 1.13
1/*	$NetBSD: edit.c,v 1.13 2002/03/05 21:29:30 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
39static char sccsid[] = "@(#)edit.c	8.1 (Berkeley) 6/6/93";
40#else
41__RCSID("$NetBSD: edit.c,v 1.13 2002/03/05 21:29:30 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 */
53extern char *tempEdit;
54
55/*
56 * Edit a message list.
57 */
58int
59editor(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 */
69int
70visual(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 */
82int
83edit1(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 */
146FILE *
147run_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	struct stat statb;
154
155	if ((t = creat(tempEdit, readonlyflag ? 0400 : 0600)) < 0) {
156		warn("%s", tempEdit);
157		goto out;
158	}
159	if ((nf = Fdopen(t, "w")) == NULL) {
160		warn("%s", tempEdit);
161		(void)unlink(tempEdit);
162		goto out;
163	}
164	if (size >= 0)
165		while (--size >= 0 && (t = getc(fp)) != EOF)
166			(void)putc(t, nf);
167	else
168		while ((t = getc(fp)) != EOF)
169			(void)putc(t, nf);
170	(void)fflush(nf);
171	if (ferror(nf)) {
172		(void)Fclose(nf);
173		warn("%s", tempEdit);
174		(void)unlink(tempEdit);
175		nf = NULL;
176		goto out;
177	}
178	if (fstat(fileno(nf), &statb) < 0)
179		modtime = 0;
180	else
181		modtime = statb.st_mtime;
182	if (Fclose(nf) < 0) {
183		warn("%s", tempEdit);
184		(void)unlink(tempEdit);
185		nf = NULL;
186		goto out;
187	}
188	nf = NULL;
189	if ((editcmd =
190	         value(editortype == 'e' ? "EDITOR" : "VISUAL")) == NULL)
191		editcmd = editortype == 'e' ? _PATH_EX : _PATH_VI;
192	if (run_command(editcmd, 0, -1, -1, tempEdit, NULL, NULL) < 0) {
193		(void)unlink(tempEdit);
194		goto out;
195	}
196	/*
197	 * If in read only mode or file unchanged, just remove the editor
198	 * temporary and return.
199	 */
200	if (readonlyflag) {
201		(void)unlink(tempEdit);
202		goto out;
203	}
204	if (stat(tempEdit, &statb) < 0) {
205		warn("%s", tempEdit);
206		goto out;
207	}
208	if (modtime == statb.st_mtime) {
209		(void)unlink(tempEdit);
210		goto out;
211	}
212	/*
213	 * Now switch to new file.
214	 */
215	if ((nf = Fopen(tempEdit, "a+")) == NULL) {
216		warn("%s", tempEdit);
217		(void)unlink(tempEdit);
218		goto out;
219	}
220	(void)unlink(tempEdit);
221out:
222	return nf;
223}
224