xref: /openbsd/usr.bin/mail/edit.c (revision 78b63d65)
1 /*	$OpenBSD: edit.c,v 1.11 2001/11/21 15:26:39 millert Exp $	*/
2 /*	$NetBSD: edit.c,v 1.5 1996/06/08 19:48:20 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1980, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 #if 0
39 static const char sccsid[] = "@(#)edit.c	8.1 (Berkeley) 6/6/93";
40 #else
41 static const char rcsid[] = "$OpenBSD: edit.c,v 1.11 2001/11/21 15:26:39 millert Exp $";
42 #endif
43 #endif /* not lint */
44 
45 #include "rcv.h"
46 #include <fcntl.h>
47 #include "extern.h"
48 
49 /*
50  * Mail -- a mail program
51  *
52  * Perform message editing functions.
53  */
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 type)
84 {
85 	int c, i;
86 	FILE *fp;
87 	struct sigaction oact;
88 	sigset_t oset;
89 	struct message *mp;
90 	off_t size;
91 
92 	/*
93 	 * Deal with each message to be edited . . .
94 	 */
95 	for (i = 0; msgvec[i] && i < msgCount; i++) {
96 		if (i > 0) {
97 			char buf[100];
98 			char *p;
99 
100 			printf("Edit message %d [ynq]? ", msgvec[i]);
101 			if (fgets(buf, sizeof(buf), stdin) == 0)
102 				break;
103 			for (p = buf; *p == ' ' || *p == '\t'; p++)
104 				;
105 			if (*p == 'q')
106 				break;
107 			if (*p == 'n')
108 				continue;
109 		}
110 		dot = mp = &message[msgvec[i] - 1];
111 		touch(mp);
112 		(void)ignoresig(SIGINT, &oact, &oset);
113 		fp = run_editor(setinput(mp), mp->m_size, type, readonly);
114 		if (fp != NULL) {
115 			(void)fseek(otf, 0L, 2);
116 			size = ftell(otf);
117 			mp->m_block = blockof(size);
118 			mp->m_offset = offsetof(size);
119 			mp->m_size = fsize(fp);
120 			mp->m_lines = 0;
121 			mp->m_flag |= MODIFY;
122 			rewind(fp);
123 			while ((c = getc(fp)) != EOF) {
124 				if (c == '\n')
125 					mp->m_lines++;
126 				if (putc(c, otf) == EOF)
127 					break;
128 			}
129 			if (ferror(otf))
130 				warn("/tmp");
131 			(void)Fclose(fp);
132 		}
133 		(void)sigprocmask(SIG_SETMASK, &oset, NULL);
134 		(void)sigaction(SIGINT, &oact, NULL);
135 	}
136 	return(0);
137 }
138 
139 /*
140  * Run an editor on the file at "fpp" of "size" bytes,
141  * and return a new file pointer.
142  * Signals must be handled by the caller.
143  * "Type" is 'e' for _PATH_EX, 'v' for _PATH_VI.
144  */
145 FILE *
146 run_editor(FILE *fp, off_t size, int type, int readonly)
147 {
148 	FILE *nf = NULL;
149 	int t;
150 	time_t modtime;
151 	char *edit, tempname[PATHSIZE];
152 	struct stat statb;
153 
154 	(void)snprintf(tempname, sizeof(tempname),
155 	    "%s/mail.ReXXXXXXXXXX", tmpdir);
156 	if ((t = mkstemp(tempname)) == -1 ||
157 	    (nf = Fdopen(t, "w")) == NULL) {
158 		warn("%s", tempname);
159 		goto out;
160 	}
161 	if (readonly && fchmod(t, 0400) == -1) {
162 		warn("%s", tempname);
163 		(void)rm(tempname);
164 		goto out;
165 	}
166 	if (size >= 0)
167 		while (--size >= 0 && (t = getc(fp)) != EOF)
168 			(void)putc(t, nf);
169 	else
170 		while ((t = getc(fp)) != EOF)
171 			(void)putc(t, nf);
172 	(void)fflush(nf);
173 	if (fstat(fileno(nf), &statb) < 0)
174 		modtime = 0;
175 	else
176 		modtime = statb.st_mtime;
177 	if (ferror(nf)) {
178 		(void)Fclose(nf);
179 		warn("%s", tempname);
180 		(void)rm(tempname);
181 		nf = NULL;
182 		goto out;
183 	}
184 	if (Fclose(nf) < 0) {
185 		warn("%s", tempname);
186 		(void)rm(tempname);
187 		nf = NULL;
188 		goto out;
189 	}
190 	nf = NULL;
191 	if ((edit = value(type == 'e' ? "EDITOR" : "VISUAL")) == NULL)
192 		edit = type == 'e' ? _PATH_EX : _PATH_VI;
193 	if (run_command(edit, 0, 0, -1, tempname, NULL, NULL) < 0) {
194 		(void)rm(tempname);
195 		goto out;
196 	}
197 	/*
198 	 * If in read only mode or file unchanged, just remove the editor
199 	 * temporary and return.
200 	 */
201 	if (readonly) {
202 		(void)rm(tempname);
203 		goto out;
204 	}
205 	if (stat(tempname, &statb) < 0) {
206 		warn("%s", tempname);
207 		goto out;
208 	}
209 	if (modtime == statb.st_mtime) {
210 		(void)rm(tempname);
211 		goto out;
212 	}
213 	/*
214 	 * Now switch to new file.
215 	 */
216 	if ((nf = Fopen(tempname, "a+")) == NULL) {
217 		warn("%s", tempname);
218 		(void)rm(tempname);
219 		goto out;
220 	}
221 	(void)rm(tempname);
222 out:
223 	return(nf);
224 }
225