tty.c: GCC 4.9 warnings
[s-mailx.git] / edit.c
blobc7d395b206cd206dff5caba93f18a93ea06a6d26
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(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 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 char const *ed;
161 char *tempEdit;
162 NYD_ENTER;
164 if ((nf = Ftmp(&tempEdit, "runed", OF_WRONLY | OF_REGISTER,
165 (readonly ? 0400 : 0600))) == NULL) {
166 perror(tr(73, "temporary mail edit file"));
167 goto jleave;
170 if (hp != NULL) {
171 t = GTO | GSUBJECT | GCC | GBCC | GNL | GCOMMA;
172 if (hp->h_from != NULL || hp->h_replyto != NULL ||
173 hp->h_sender != NULL || hp->h_organization != NULL)
174 t |= GIDENT;
175 puthead(hp, nf, t, SEND_TODISP, CONV_NONE, NULL, NULL);
178 if (mp != NULL) {
179 sendmp(mp, nf, 0, NULL, action, NULL);
180 } else {
181 if (size >= 0)
182 while (--size >= 0 && (t = getc(fp)) != EOF)
183 putc(t, nf);
184 else
185 while ((t = getc(fp)) != EOF)
186 putc(t, nf);
189 fflush(nf);
190 if (fstat(fileno(nf), &statb) == -1)
191 modtime = 0;
192 else
193 modtime = statb.st_mtime;
194 t = ferror(nf);
195 if (Fclose(nf) < 0 || t != 0) {
196 perror(tempEdit);
197 t = 1;
199 nf = NULL;
200 if (t != 0)
201 goto jleave;
203 ed = (viored == 'e') ? ok_vlook(EDITOR) : ok_vlook(VISUAL);
204 if (ed == NULL)
205 ed = (viored == 'e') ? "ed" : "vi"; /* XXX no magics, -> nail.h */
207 sigemptyset(&cset);
208 if (run_command(ed, (oldint != SIG_IGN ? &cset : NULL), -1, -1, tempEdit,
209 NULL, NULL) < 0)
210 goto jleave;
212 /* If in read only mode or file unchanged, just remove the editor temporary
213 * and return. Otherwise switch to new file */
214 if (readonly)
215 goto jleave;
216 if (stat(tempEdit, &statb) == -1) {
217 perror(tempEdit);
218 goto jleave;
220 if (modtime != statb.st_mtime && (nf = Fopen(tempEdit, "a+")) == NULL)
221 perror(tempEdit);
222 jleave:
223 if (tempEdit != NULL) {
224 unlink(tempEdit);
225 Ftmp_free(&tempEdit);
227 NYD_LEAVE;
228 return nf;
231 /* vim:set fenc=utf-8:s-it-mode */