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