edit.c revision 1.9 1 /* $NetBSD: edit.c,v 1.9 2002/03/02 14:59:36 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
39 static char sccsid[] = "@(#)edit.c 8.1 (Berkeley) 6/6/93";
40 #else
41 __RCSID("$NetBSD: edit.c,v 1.9 2002/03/02 14:59:36 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 */
53 extern char *tempEdit;
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;
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, type, readonly);
115 if (fp != NULL) {
116 (void) fseek(otf, 0L, 2);
117 size = ftell(otf);
118 mp->m_block = blockof(size);
119 mp->m_offset = offsetof(size);
120 mp->m_size = fsize(fp);
121 mp->m_lines = 0;
122 mp->m_flag |= MODIFY;
123 rewind(fp);
124 while ((c = getc(fp)) != EOF) {
125 if (c == '\n')
126 mp->m_lines++;
127 if (putc(c, otf) == EOF)
128 break;
129 }
130 if (ferror(otf))
131 perror("/tmp");
132 (void) Fclose(fp);
133 }
134 (void) signal(SIGINT, sigint);
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;
152 struct stat statb;
153
154 if ((t = creat(tempEdit, readonly ? 0400 : 0600)) < 0) {
155 perror(tempEdit);
156 goto out;
157 }
158 if ((nf = Fdopen(t, "w")) == NULL) {
159 perror(tempEdit);
160 (void) unlink(tempEdit);
161 goto out;
162 }
163 if (size >= 0)
164 while (--size >= 0 && (t = getc(fp)) != EOF)
165 (void) putc(t, nf);
166 else
167 while ((t = getc(fp)) != EOF)
168 (void) putc(t, nf);
169 (void) fflush(nf);
170 if (ferror(nf)) {
171 (void) Fclose(nf);
172 perror(tempEdit);
173 (void) unlink(tempEdit);
174 nf = NULL;
175 goto out;
176 }
177 if (fstat(fileno(nf), &statb) < 0)
178 modtime = 0;
179 else
180 modtime = statb.st_mtime;
181 if (Fclose(nf) < 0) {
182 perror(tempEdit);
183 (void) unlink(tempEdit);
184 nf = NULL;
185 goto out;
186 }
187 nf = NULL;
188 if ((edit = value(type == 'e' ? "EDITOR" : "VISUAL")) == NOSTR)
189 edit = type == 'e' ? _PATH_EX : _PATH_VI;
190 if (run_command(edit, 0, -1, -1, tempEdit, NOSTR, NOSTR) < 0) {
191 (void) unlink(tempEdit);
192 goto out;
193 }
194 /*
195 * If in read only mode or file unchanged, just remove the editor
196 * temporary and return.
197 */
198 if (readonly) {
199 (void) unlink(tempEdit);
200 goto out;
201 }
202 if (stat(tempEdit, &statb) < 0) {
203 perror(tempEdit);
204 goto out;
205 }
206 if (modtime == statb.st_mtime) {
207 (void) unlink(tempEdit);
208 goto out;
209 }
210 /*
211 * Now switch to new file.
212 */
213 if ((nf = Fopen(tempEdit, "a+")) == NULL) {
214 perror(tempEdit);
215 (void) unlink(tempEdit);
216 goto out;
217 }
218 (void) unlink(tempEdit);
219 out:
220 return nf;
221 }
222