edit.c revision 1.7 1 /* $NetBSD: edit.c,v 1.7 1997/11/25 17:58:17 bad 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.7 1997/11/25 17:58:17 bad 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
54 /*
55 * Edit a message list.
56 */
57 int
58 editor(v)
59 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(v)
71 void *v;
72 {
73 int *msgvec = v;
74
75 return edit1(msgvec, 'v');
76 }
77
78 /*
79 * Edit a message by writing the message into a funnily-named file
80 * (which should not exist) and forking an editor on it.
81 * We get the editor from the stuff above.
82 */
83 int
84 edit1(msgvec, type)
85 int *msgvec;
86 int type;
87 {
88 int c;
89 int i;
90 FILE *fp;
91 struct message *mp;
92 off_t size;
93
94 /*
95 * Deal with each message to be edited . . .
96 */
97 for (i = 0; msgvec[i] && i < msgCount; i++) {
98 sig_t sigint;
99
100 if (i > 0) {
101 char buf[100];
102 char *p;
103
104 printf("Edit message %d [ynq]? ", msgvec[i]);
105 if (fgets(buf, sizeof buf, stdin) == 0)
106 break;
107 for (p = buf; *p == ' ' || *p == '\t'; p++)
108 ;
109 if (*p == 'q')
110 break;
111 if (*p == 'n')
112 continue;
113 }
114 dot = mp = &message[msgvec[i] - 1];
115 touch(mp);
116 sigint = signal(SIGINT, SIG_IGN);
117 fp = run_editor(setinput(mp), mp->m_size, type, readonly);
118 if (fp != NULL) {
119 (void) fseek(otf, 0L, 2);
120 size = ftell(otf);
121 mp->m_block = blockof(size);
122 mp->m_offset = offsetof(size);
123 mp->m_size = fsize(fp);
124 mp->m_lines = 0;
125 mp->m_flag |= MODIFY;
126 rewind(fp);
127 while ((c = getc(fp)) != EOF) {
128 if (c == '\n')
129 mp->m_lines++;
130 if (putc(c, otf) == EOF)
131 break;
132 }
133 if (ferror(otf))
134 perror("/tmp");
135 (void) Fclose(fp);
136 }
137 (void) signal(SIGINT, sigint);
138 }
139 return 0;
140 }
141
142 /*
143 * Run an editor on the file at "fpp" of "size" bytes,
144 * and return a new file pointer.
145 * Signals must be handled by the caller.
146 * "Type" is 'e' for _PATH_EX, 'v' for _PATH_VI.
147 */
148 FILE *
149 run_editor(fp, size, type, readonly)
150 FILE *fp;
151 off_t size;
152 int type, readonly;
153 {
154 FILE *nf = NULL;
155 int t;
156 time_t modtime;
157 char *edit;
158 struct stat statb;
159 extern char *tempEdit;
160
161 if ((t = creat(tempEdit, readonly ? 0400 : 0600)) < 0) {
162 perror(tempEdit);
163 goto out;
164 }
165 if ((nf = Fdopen(t, "w")) == NULL) {
166 perror(tempEdit);
167 (void) unlink(tempEdit);
168 goto out;
169 }
170 if (size >= 0)
171 while (--size >= 0 && (t = getc(fp)) != EOF)
172 (void) putc(t, nf);
173 else
174 while ((t = getc(fp)) != EOF)
175 (void) putc(t, nf);
176 (void) fflush(nf);
177 if (ferror(nf)) {
178 (void) Fclose(nf);
179 perror(tempEdit);
180 (void) unlink(tempEdit);
181 nf = NULL;
182 goto out;
183 }
184 if (fstat(fileno(nf), &statb) < 0)
185 modtime = 0;
186 else
187 modtime = statb.st_mtime;
188 if (Fclose(nf) < 0) {
189 perror(tempEdit);
190 (void) unlink(tempEdit);
191 nf = NULL;
192 goto out;
193 }
194 nf = NULL;
195 if ((edit = value(type == 'e' ? "EDITOR" : "VISUAL")) == NOSTR)
196 edit = type == 'e' ? _PATH_EX : _PATH_VI;
197 if (run_command(edit, 0, -1, -1, tempEdit, NOSTR, NOSTR) < 0) {
198 (void) unlink(tempEdit);
199 goto out;
200 }
201 /*
202 * If in read only mode or file unchanged, just remove the editor
203 * temporary and return.
204 */
205 if (readonly) {
206 (void) unlink(tempEdit);
207 goto out;
208 }
209 if (stat(tempEdit, &statb) < 0) {
210 perror(tempEdit);
211 goto out;
212 }
213 if (modtime == statb.st_mtime) {
214 (void) unlink(tempEdit);
215 goto out;
216 }
217 /*
218 * Now switch to new file.
219 */
220 if ((nf = Fopen(tempEdit, "a+")) == NULL) {
221 perror(tempEdit);
222 (void) unlink(tempEdit);
223 goto out;
224 }
225 (void) unlink(tempEdit);
226 out:
227 return nf;
228 }
229