.mailmap: (sigh..)
[s-mailx.git] / edit.c
blobe15550273aafff1c0c9afd5887611bd2c118c3e4
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Perform message editing functions.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2014 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
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 #ifndef HAVE_AMALGAMATION
41 # include "nail.h"
42 #endif
44 /* Edit a message by writing the message into a funnily-named file (which
45 * should not exist) and forking an editor on it */
46 static int edit1(int *msgvec, int viored);
48 static int
49 edit1(int *msgvec, int viored)
51 int c, i;
52 FILE *fp = NULL;
53 struct message *mp;
54 off_t size;
55 bool_t wb, lastnl;
56 char *line = NULL;
57 size_t linesize = 0;
58 NYD_ENTER;
60 wb = ok_blook(writebackedited);
62 /* Deal with each message to be edited... */
63 for (i = 0; msgvec[i] != 0 && i < msgCount; ++i) {
64 sighandler_type sigint;
66 if (i > 0) {
67 char *p;
69 printf(tr(72, "Edit message %d [ynq]? "), msgvec[i]);
70 fflush(stdout);
71 if (readline_restart(stdin, &line, &linesize, 0) < 0)
72 break;
73 for (p = line; blankchar(*p); ++p)
75 if (*p == 'q')
76 break;
77 if (*p == 'n')
78 continue;
80 setdot(mp = &message[msgvec[i] - 1]);
81 did_print_dot = TRU1;
82 touch(mp);
84 sigint = safe_signal(SIGINT, SIG_IGN);
86 --mp->m_size; /* Strip final NL.. */
87 fp = run_editor(fp, -1/*mp->m_size*/, viored,
88 ((mb.mb_perm & MB_EDIT) == 0 || !wb), NULL, mp,
89 (wb ? SEND_MBOX : SEND_TODISP_ALL), sigint);
90 ++mp->m_size; /* And readd it */
92 if (fp != NULL) {
93 fseek(mb.mb_otf, 0L, SEEK_END);
94 size = ftell(mb.mb_otf);
95 mp->m_block = mailx_blockof(size);
96 mp->m_offset = mailx_offsetof(size);
97 mp->m_lines = 0;
98 mp->m_flag |= MODIFY;
99 rewind(fp);
100 lastnl = 0;
101 size = 0;
102 while ((c = getc(fp)) != EOF) {
103 if ((lastnl = (c == '\n')))
104 ++mp->m_lines;
105 if (putc(c, mb.mb_otf) == EOF)
106 break;
107 ++size;
109 if (!lastnl && putc('\n', mb.mb_otf) != EOF)
110 ++size;
111 if (putc('\n', mb.mb_otf) != EOF)
112 ++size;
113 mp->m_size = (size_t)size;
114 if (ferror(mb.mb_otf))
115 perror("/tmp");
116 Fclose(fp);
119 safe_signal(SIGINT, sigint);
122 if (line != NULL)
123 free(line);
124 NYD_LEAVE;
125 return 0;
128 FL int
129 c_editor(void *v)
131 int *msgvec = v, rv;
132 NYD_ENTER;
134 rv = edit1(msgvec, 'e');
135 NYD_LEAVE;
136 return rv;
139 FL int
140 c_visual(void *v)
142 int *msgvec = v, rv;
143 NYD_ENTER;
145 rv = edit1(msgvec, 'v');
146 NYD_LEAVE;
147 return rv;
150 FL FILE *
151 run_editor(FILE *fp, off_t size, int viored, int readonly, struct header *hp,
152 struct message *mp, enum sendaction action, sighandler_type oldint)
154 struct stat statb;
155 sigset_t cset;
156 FILE *nf = NULL;
157 int t;
158 time_t modtime;
159 char const *ed;
160 char *tempEdit;
161 NYD_ENTER;
163 if ((nf = Ftmp(&tempEdit, "runed", OF_WRONLY | OF_REGISTER,
164 (readonly ? 0400 : 0600))) == NULL) {
165 perror(tr(73, "temporary mail edit file"));
166 goto jleave;
169 if (hp != NULL) {
170 t = GTO | GSUBJECT | GCC | GBCC | GNL | GCOMMA;
171 if (hp->h_from || hp->h_replyto || hp->h_sender || hp->h_organization)
172 t |= GIDENT;
173 puthead(hp, nf, t, SEND_TODISP, CONV_NONE, NULL, NULL);
176 if (mp != NULL) {
177 sendmp(mp, nf, 0, NULL, action, NULL);
178 } else {
179 if (size >= 0)
180 while (--size >= 0 && (t = getc(fp)) != EOF)
181 putc(t, nf);
182 else
183 while ((t = getc(fp)) != EOF)
184 putc(t, nf);
187 fflush(nf);
188 if (fstat(fileno(nf), &statb) < 0)
189 modtime = 0;
190 else
191 modtime = statb.st_mtime;
192 t = ferror(nf);
193 if (Fclose(nf) < 0 || t != 0) {
194 perror(tempEdit);
195 t = 1;
197 nf = NULL;
198 if (t != 0)
199 goto jleave;
201 ed = (viored == 'e') ? ok_vlook(EDITOR) : ok_vlook(VISUAL);
202 if (ed == NULL)
203 ed = (viored == 'e') ? "ed" : "vi"; /* XXX no magics, -> nail.h */
205 sigemptyset(&cset);
206 if (run_command(ed, (oldint != SIG_IGN ? &cset : NULL), -1, -1, tempEdit,
207 NULL, NULL) < 0)
208 goto jleave;
210 /* If in read only mode or file unchanged, just remove the editor temporary
211 * and return. Otherwise switch to new file */
212 if (readonly)
213 goto jleave;
214 if (stat(tempEdit, &statb) < 0) {
215 perror(tempEdit);
216 goto jleave;
218 if (modtime != statb.st_mtime && (nf = Fopen(tempEdit, "a+")) == NULL)
219 perror(tempEdit);
220 jleave:
221 if (tempEdit != NULL) {
222 unlink(tempEdit);
223 Ftmp_free(&tempEdit);
225 NYD_LEAVE;
226 return nf;
229 /* vim:set fenc=utf-8:s-it-mode */