Tweak *rfc822-show-all* manual text
[s-mailx.git] / edit.c
blobdd252478d1b6fb1d697d4e10980eed9e8f536c48
1 /*
2 * S-nail - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 Steffen "Daode" Nurpmeso.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
40 #include "rcv.h"
41 #include "extern.h"
42 #include <sys/stat.h>
43 #include <unistd.h>
46 * Mail -- a mail program
48 * Perform message editing functions.
51 static int edit1(int *msgvec, int type);
54 * Edit a message list.
56 int
57 editor(void *v)
59 int *msgvec = v;
61 return edit1(msgvec, 'e');
65 * Invoke the visual editor on a message list.
67 int
68 visual(void *v)
70 int *msgvec = v;
72 return edit1(msgvec, 'v');
76 * Edit a message by writing the message into a funnily-named file
77 * (which should not exist) and forking an editor on it.
78 * We get the editor from the stuff above.
80 static int
81 edit1(int *msgvec, int type)
83 int c, i, wb, lastnl;
84 FILE *fp = NULL;
85 struct message *mp;
86 off_t size;
87 char *line = NULL;
88 size_t linesize;
91 * Deal with each message to be edited . . .
93 wb = value("writebackedited") != NULL;
94 for (i = 0; msgvec[i] && i < msgCount; i++) {
95 sighandler_type sigint;
97 if (i > 0) {
98 char *p;
100 printf(tr(72, "Edit message %d [ynq]? "), msgvec[i]);
101 fflush(stdout);
102 if (readline(stdin, &line, &linesize) < 0)
103 break;
104 for (p = line; blankchar(*p & 0377); p++);
105 if (*p == 'q')
106 break;
107 if (*p == 'n')
108 continue;
110 setdot(mp = &message[msgvec[i] - 1]);
111 did_print_dot = 1;
112 touch(mp);
113 sigint = safe_signal(SIGINT, SIG_IGN);
114 --mp->m_size; /* XXX[edithack] strip final NL */
115 fp = run_editor(fp, -1/*mp->m_size*/, type,
116 (mb.mb_perm & MB_EDIT) == 0 || !wb,
117 NULL, mp, wb ? SEND_MBOX : SEND_TODISP_ALL,
118 sigint);
119 ++mp->m_size; /* XXX[edithack] */
120 if (fp != NULL) {
121 fseek(mb.mb_otf, 0L, SEEK_END);
122 size = ftell(mb.mb_otf);
123 mp->m_block = mailx_blockof(size);
124 mp->m_offset = mailx_offsetof(size);
125 mp->m_lines = 0;
126 mp->m_flag |= MODIFY;
127 rewind(fp);
128 lastnl = 0;
129 size = 0;
130 while ((c = getc(fp)) != EOF) {
131 if ((lastnl = c == '\n'))
132 mp->m_lines++;
133 if (putc(c, mb.mb_otf) == EOF)
134 break;
135 ++size;
137 /* MBOX finalize XXX[edithack] is this always MBOX? */
138 if (! lastnl && putc('\n', mb.mb_otf) != EOF)
139 ++size;
140 if (putc('\n', mb.mb_otf) != EOF)
141 ++size;
142 mp->m_size = (size_t)size;/*XXX[edithack] inc.MBOX?!? */
143 if (ferror(mb.mb_otf))
144 perror("/tmp");
145 Fclose(fp);
147 safe_signal(SIGINT, sigint);
149 if (line)
150 free(line);
151 return 0;
155 * Run an editor on the file at "fpp" of "size" bytes,
156 * and return a new file pointer.
157 * Signals must be handled by the caller.
158 * "Type" is 'e' for ed, 'v' for vi.
160 FILE *
161 run_editor(FILE *fp, off_t size, int type, int readonly,
162 struct header *hp, struct message *mp, enum sendaction action,
163 sighandler_type oldint)
165 FILE *nf = NULL;
166 int t;
167 time_t modtime;
168 char *edit;
169 struct stat statb;
170 char *tempEdit;
171 sigset_t set;
173 if ((nf = Ftemp(&tempEdit, "Re", "w", readonly ? 0400 : 0600, 1))
174 == NULL) {
175 perror(catgets(catd, CATSET, 73, "temporary mail edit file"));
176 goto out;
178 if (hp) {
179 t = GTO|GSUBJECT|GCC|GBCC|GNL|GCOMMA;
180 if (hp->h_from || hp->h_replyto || hp->h_sender ||
181 hp->h_organization)
182 t |= GIDENT;
183 puthead(hp, nf, t, SEND_TODISP, CONV_NONE, NULL, NULL);
185 if (mp) {
186 send(mp, nf, 0, NULL, action, NULL);
187 } else {
188 if (size >= 0)
189 while (--size >= 0 && (t = getc(fp)) != EOF)
190 putc(t, nf);
191 else
192 while ((t = getc(fp)) != EOF)
193 putc(t, nf);
195 fflush(nf);
196 if (fstat(fileno(nf), &statb) < 0)
197 modtime = 0;
198 else
199 modtime = statb.st_mtime;
200 if (ferror(nf)) {
201 Fclose(nf);
202 perror(tempEdit);
203 unlink(tempEdit);
204 Ftfree(&tempEdit);
205 nf = NULL;
206 goto out;
208 if (Fclose(nf) < 0) {
209 perror(tempEdit);
210 unlink(tempEdit);
211 Ftfree(&tempEdit);
212 nf = NULL;
213 goto out;
215 nf = NULL;
216 if ((edit = value(type == 'e' ? "EDITOR" : "VISUAL")) == NULL)
217 edit = type == 'e' ? "ed" : "vi";
218 sigemptyset(&set);
219 if (run_command(edit, oldint != SIG_IGN ? &set : NULL, -1, -1,
220 tempEdit, NULL, NULL) < 0) {
221 unlink(tempEdit);
222 Ftfree(&tempEdit);
223 goto out;
226 * If in read only mode or file unchanged, just remove the editor
227 * temporary and return.
229 if (readonly) {
230 unlink(tempEdit);
231 Ftfree(&tempEdit);
232 goto out;
234 if (stat(tempEdit, &statb) < 0) {
235 perror(tempEdit);
236 Ftfree(&tempEdit);
237 goto out;
239 if (modtime == statb.st_mtime) {
240 unlink(tempEdit);
241 Ftfree(&tempEdit);
242 goto out;
245 * Now switch to new file.
247 if ((nf = Fopen(tempEdit, "a+")) == NULL)
248 perror(tempEdit);
249 out:
250 if (tempEdit) {
251 unlink(tempEdit);
252 Ftfree(&tempEdit);
254 return nf;