edit.c revision 1.21 1 1.21 christos /* $NetBSD: edit.c,v 1.21 2006/11/28 18:45:32 christos Exp $ */
2 1.5 christos
3 1.1 cgd /*
4 1.3 deraadt * Copyright (c) 1980, 1993
5 1.3 deraadt * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.18 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.6 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.5 christos #if 0
35 1.5 christos static char sccsid[] = "@(#)edit.c 8.1 (Berkeley) 6/6/93";
36 1.5 christos #else
37 1.21 christos __RCSID("$NetBSD: edit.c,v 1.21 2006/11/28 18:45:32 christos Exp $");
38 1.5 christos #endif
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.1 cgd #include "rcv.h"
42 1.3 deraadt #include "extern.h"
43 1.21 christos #include "thread.h"
44 1.1 cgd
45 1.1 cgd /*
46 1.1 cgd * Mail -- a mail program
47 1.1 cgd *
48 1.1 cgd * Perform message editing functions.
49 1.1 cgd */
50 1.1 cgd
51 1.1 cgd /*
52 1.1 cgd * Run an editor on the file at "fpp" of "size" bytes,
53 1.1 cgd * and return a new file pointer.
54 1.1 cgd * Signals must be handled by the caller.
55 1.10 wiz * "Editortype" is 'e' for _PATH_EX, 'v' for _PATH_VI.
56 1.1 cgd */
57 1.21 christos PUBLIC FILE *
58 1.10 wiz run_editor(FILE *fp, off_t size, int editortype, int readonlyflag)
59 1.1 cgd {
60 1.6 lukem FILE *nf = NULL;
61 1.6 lukem int t;
62 1.1 cgd time_t modtime;
63 1.19 christos const char *editcmd;
64 1.14 wiz char tempname[PATHSIZE];
65 1.1 cgd struct stat statb;
66 1.1 cgd
67 1.14 wiz (void)snprintf(tempname, sizeof(tempname),
68 1.14 wiz "%s/mail.ReXXXXXXXXXX", tmpdir);
69 1.14 wiz if ((t = mkstemp(tempname)) == -1) {
70 1.14 wiz warn("%s", tempname);
71 1.14 wiz goto out;
72 1.14 wiz }
73 1.14 wiz if (readonlyflag && fchmod(t, 0400) == -1) {
74 1.20 christos (void)close(t);
75 1.14 wiz warn("%s", tempname);
76 1.14 wiz (void)unlink(tempname);
77 1.1 cgd goto out;
78 1.1 cgd }
79 1.1 cgd if ((nf = Fdopen(t, "w")) == NULL) {
80 1.20 christos (void)close(t);
81 1.14 wiz warn("%s", tempname);
82 1.14 wiz (void)unlink(tempname);
83 1.1 cgd goto out;
84 1.1 cgd }
85 1.1 cgd if (size >= 0)
86 1.1 cgd while (--size >= 0 && (t = getc(fp)) != EOF)
87 1.12 wiz (void)putc(t, nf);
88 1.1 cgd else
89 1.1 cgd while ((t = getc(fp)) != EOF)
90 1.12 wiz (void)putc(t, nf);
91 1.12 wiz (void)fflush(nf);
92 1.1 cgd if (ferror(nf)) {
93 1.12 wiz (void)Fclose(nf);
94 1.14 wiz warn("%s", tempname);
95 1.14 wiz (void)unlink(tempname);
96 1.1 cgd nf = NULL;
97 1.1 cgd goto out;
98 1.1 cgd }
99 1.7 bad if (fstat(fileno(nf), &statb) < 0)
100 1.7 bad modtime = 0;
101 1.7 bad else
102 1.7 bad modtime = statb.st_mtime;
103 1.1 cgd if (Fclose(nf) < 0) {
104 1.14 wiz warn("%s", tempname);
105 1.14 wiz (void)unlink(tempname);
106 1.1 cgd nf = NULL;
107 1.1 cgd goto out;
108 1.1 cgd }
109 1.1 cgd nf = NULL;
110 1.10 wiz if ((editcmd =
111 1.21 christos value(editortype == 'e' ? ENAME_EDITOR : ENAME_VISUAL)) == NULL)
112 1.10 wiz editcmd = editortype == 'e' ? _PATH_EX : _PATH_VI;
113 1.17 christos if (run_command(editcmd, 0, 0, -1, tempname, NULL) < 0) {
114 1.14 wiz (void)unlink(tempname);
115 1.1 cgd goto out;
116 1.1 cgd }
117 1.1 cgd /*
118 1.1 cgd * If in read only mode or file unchanged, just remove the editor
119 1.1 cgd * temporary and return.
120 1.1 cgd */
121 1.10 wiz if (readonlyflag) {
122 1.14 wiz (void)unlink(tempname);
123 1.1 cgd goto out;
124 1.1 cgd }
125 1.14 wiz if (stat(tempname, &statb) < 0) {
126 1.14 wiz warn("%s", tempname);
127 1.1 cgd goto out;
128 1.1 cgd }
129 1.1 cgd if (modtime == statb.st_mtime) {
130 1.14 wiz (void)unlink(tempname);
131 1.1 cgd goto out;
132 1.1 cgd }
133 1.1 cgd /*
134 1.1 cgd * Now switch to new file.
135 1.1 cgd */
136 1.14 wiz if ((nf = Fopen(tempname, "a+")) == NULL) {
137 1.14 wiz warn("%s", tempname);
138 1.14 wiz (void)unlink(tempname);
139 1.1 cgd goto out;
140 1.1 cgd }
141 1.14 wiz (void)unlink(tempname);
142 1.1 cgd out:
143 1.1 cgd return nf;
144 1.1 cgd }
145 1.21 christos
146 1.21 christos /*
147 1.21 christos * Edit a message by writing the message into a funnily-named file
148 1.21 christos * (which should not exist) and forking an editor on it.
149 1.21 christos * We get the editor from the stuff above.
150 1.21 christos */
151 1.21 christos static int
152 1.21 christos edit1(int *msgvec, int editortype)
153 1.21 christos {
154 1.21 christos int c;
155 1.21 christos int i;
156 1.21 christos int msgCount;
157 1.21 christos FILE *fp;
158 1.21 christos struct message *mp;
159 1.21 christos off_t size;
160 1.21 christos
161 1.21 christos /*
162 1.21 christos * Deal with each message to be edited . . .
163 1.21 christos */
164 1.21 christos msgCount = get_msgCount();
165 1.21 christos for (i = 0; msgvec[i] && i < msgCount; i++) {
166 1.21 christos sig_t sigint;
167 1.21 christos
168 1.21 christos if (i > 0) {
169 1.21 christos char buf[100];
170 1.21 christos char *p;
171 1.21 christos
172 1.21 christos (void)printf("Edit message %d [ynq]? ", msgvec[i]);
173 1.21 christos if (fgets(buf, sizeof buf, stdin) == 0)
174 1.21 christos break;
175 1.21 christos for (p = buf; *p == ' ' || *p == '\t'; p++)
176 1.21 christos continue;
177 1.21 christos if (*p == 'q')
178 1.21 christos break;
179 1.21 christos if (*p == 'n')
180 1.21 christos continue;
181 1.21 christos }
182 1.21 christos dot = mp = get_message(msgvec[i]);
183 1.21 christos touch(mp);
184 1.21 christos sigint = signal(SIGINT, SIG_IGN);
185 1.21 christos fp = run_editor(setinput(mp), mp->m_size, editortype,
186 1.21 christos readonly);
187 1.21 christos if (fp != NULL) {
188 1.21 christos (void)fseek(otf, 0L, 2);
189 1.21 christos size = ftell(otf);
190 1.21 christos mp->m_block = blockof(size);
191 1.21 christos mp->m_offset = offsetof(size);
192 1.21 christos mp->m_size = fsize(fp);
193 1.21 christos mp->m_lines = 0;
194 1.21 christos mp->m_blines = 0;
195 1.21 christos mp->m_flag |= MMODIFY;
196 1.21 christos rewind(fp);
197 1.21 christos while ((c = getc(fp)) != EOF) {
198 1.21 christos /*
199 1.21 christos * XXX. if you edit a message, we treat
200 1.21 christos * header lines as body lines in the recount.
201 1.21 christos * This is because the original message copy
202 1.21 christos * and the edit reread use different code to
203 1.21 christos * count the lines, and this one here is
204 1.21 christos * simple-minded.
205 1.21 christos */
206 1.21 christos if (c == '\n') {
207 1.21 christos mp->m_lines++;
208 1.21 christos mp->m_blines++;
209 1.21 christos }
210 1.21 christos if (putc(c, otf) == EOF)
211 1.21 christos break;
212 1.21 christos }
213 1.21 christos if (ferror(otf))
214 1.21 christos warn("/tmp");
215 1.21 christos (void)Fclose(fp);
216 1.21 christos }
217 1.21 christos (void)signal(SIGINT, sigint);
218 1.21 christos }
219 1.21 christos return 0;
220 1.21 christos }
221 1.21 christos
222 1.21 christos /*
223 1.21 christos * Edit a message list.
224 1.21 christos */
225 1.21 christos PUBLIC int
226 1.21 christos editor(void *v)
227 1.21 christos {
228 1.21 christos int *msgvec = v;
229 1.21 christos
230 1.21 christos return edit1(msgvec, 'e');
231 1.21 christos }
232 1.21 christos
233 1.21 christos /*
234 1.21 christos * Invoke the visual editor on a message list.
235 1.21 christos */
236 1.21 christos PUBLIC int
237 1.21 christos visual(void *v)
238 1.21 christos {
239 1.21 christos int *msgvec = v;
240 1.21 christos
241 1.21 christos return edit1(msgvec, 'v');
242 1.21 christos }
243