load(): print error message when loading fails (Bob Tennent)..
[s-mailx.git] / edit.c
blob6322d023d9b66cc5d6be244d88d1ea35a9b2f8e4
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; /* TODO line pool */
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) { /* TODO getapproval(): return APPROV_{YES,NO,QUIT}: USE! */
67 char *p;
69 printf(_("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 mp = message + msgvec[i] - 1;
81 setdot(mp);
82 did_print_dot = TRU1;
83 touch(mp);
85 sigint = safe_signal(SIGINT, SIG_IGN);
87 --mp->m_size; /* Strip final NL.. TODO MAILVFS->MESSAGE->length() */
88 fp = run_editor(fp, -1/*mp->m_size TODO */, viored,
89 ((mb.mb_perm & MB_EDIT) == 0 || !wb), NULL, mp,
90 (wb ? SEND_MBOX : SEND_TODISP_ALL), sigint);
91 ++mp->m_size; /* And readd it TODO */
93 if (fp != NULL) {
94 fseek(mb.mb_otf, 0L, SEEK_END);
95 size = ftell(mb.mb_otf);
96 mp->m_block = mailx_blockof(size);
97 mp->m_offset = mailx_offsetof(size);
98 mp->m_lines = 0;
99 mp->m_flag |= MODIFY;
100 rewind(fp);
101 lastnl = 0;
102 size = 0;
103 while ((c = getc(fp)) != EOF) {
104 if ((lastnl = (c == '\n')))
105 ++mp->m_lines;
106 if (putc(c, mb.mb_otf) == EOF)
107 break;
108 ++size;
110 if (!lastnl && putc('\n', mb.mb_otf) != EOF)
111 ++size;
112 if (putc('\n', mb.mb_otf) != EOF)
113 ++size;
114 mp->m_size = (size_t)size;
115 if (ferror(mb.mb_otf))
116 perror("/tmp");
117 Fclose(fp);
120 safe_signal(SIGINT, sigint);
123 if (line != NULL)
124 free(line);
125 NYD_LEAVE;
126 return 0;
129 FL int
130 c_editor(void *v)
132 int *msgvec = v, rv;
133 NYD_ENTER;
135 rv = edit1(msgvec, 'e');
136 NYD_LEAVE;
137 return rv;
140 FL int
141 c_visual(void *v)
143 int *msgvec = v, rv;
144 NYD_ENTER;
146 rv = edit1(msgvec, 'v');
147 NYD_LEAVE;
148 return rv;
151 FL FILE *
152 run_editor(FILE *fp, off_t size, int viored, int readonly, struct header *hp,
153 struct message *mp, enum sendaction action, sighandler_type oldint)
155 struct stat statb;
156 sigset_t cset;
157 FILE *nf = NULL;
158 int t;
159 time_t modtime;
160 off_t modsize;
161 char const *ed;
162 char *tempEdit;
163 NYD_ENTER;
165 if ((nf = Ftmp(&tempEdit, "runed", OF_WRONLY | OF_REGISTER,
166 (readonly ? 0400 : 0600))) == NULL) {
167 perror(_("temporary mail edit file"));
168 goto jleave;
171 if (hp != NULL) {
172 t = GTO | GSUBJECT | GCC | GBCC | GNL | GCOMMA;
173 if (hp->h_from != NULL || hp->h_replyto != NULL ||
174 hp->h_sender != NULL || hp->h_organization != NULL)
175 t |= GIDENT;
176 puthead(hp, nf, t, SEND_TODISP, CONV_NONE, NULL, NULL);
179 if (mp != NULL) {
180 sendmp(mp, nf, 0, NULL, action, NULL);
181 } else {
182 if (size >= 0)
183 while (--size >= 0 && (t = getc(fp)) != EOF)
184 putc(t, nf);
185 else
186 while ((t = getc(fp)) != EOF)
187 putc(t, nf);
190 fflush(nf);
191 if (fstat(fileno(nf), &statb) == -1)
192 modtime = 0, modsize = 0;
193 else
194 modtime = statb.st_mtime, modsize = statb.st_size;
195 t = ferror(nf);
196 if (Fclose(nf) < 0 || t != 0) {
197 perror(tempEdit);
198 t = 1;
200 nf = NULL;
201 if (t != 0)
202 goto jleave;
204 ed = (viored == 'e') ? ok_vlook(EDITOR) : ok_vlook(VISUAL);
205 if (ed == NULL)
206 ed = (viored == 'e') ? "ed" : "vi"; /* XXX no magics, -> nail.h */
208 sigemptyset(&cset);
209 if (run_command(ed, (oldint != SIG_IGN ? &cset : NULL), -1, -1, tempEdit,
210 NULL, NULL) < 0)
211 goto jleave;
213 /* If in read only mode or file unchanged, just remove the editor temporary
214 * and return. Otherwise switch to new file */
215 if (readonly)
216 goto jleave;
217 if (stat(tempEdit, &statb) == -1) {
218 perror(tempEdit);
219 goto jleave;
222 if ((modtime != statb.st_mtime || modsize != statb.st_size) &&
223 (nf = Fopen(tempEdit, "a+")) == NULL)
224 perror(tempEdit);
225 jleave:
226 if (tempEdit != NULL) {
227 unlink(tempEdit);
228 Ftmp_free(&tempEdit);
230 NYD_LEAVE;
231 return nf;
234 /* vim:set fenc=utf-8:s-it-mode */