mk-conf.sh, mk-mk.in: use variables for *all* utilities
[s-mailx.git] / edit.c
blob5a375f14f2e02af8ca805010f6fbcd3664d74142
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 static int edit1(int *msgvec, int viored);
47 * Edit a message list.
49 FL int
50 editor(void *v)
52 int *msgvec = v;
54 return edit1(msgvec, 'e');
58 * Invoke the visual editor on a message list.
60 FL int
61 visual(void *v)
63 int *msgvec = v;
65 return edit1(msgvec, 'v');
69 * Edit a message by writing the message into a funnily-named file
70 * (which should not exist) and forking an editor on it.
71 * We get the editor from the stuff above.
73 static int
74 edit1(int *msgvec, int viored)
76 int c, i, wb, lastnl;
77 FILE *fp = NULL;
78 struct message *mp;
79 off_t size;
80 char *line = NULL;
81 size_t linesize = 0;
84 * Deal with each message to be edited . . .
86 wb = value("writebackedited") != NULL;
87 for (i = 0; msgvec[i] && i < msgCount; i++) {
88 sighandler_type sigint;
90 if (i > 0) {
91 char *p;
93 printf(tr(72, "Edit message %d [ynq]? "), msgvec[i]);
94 fflush(stdout);
95 if (readline_restart(stdin, &line, &linesize, 0) < 0)
96 break;
97 for (p = line; blankchar(*p); p++)
99 if (*p == 'q')
100 break;
101 if (*p == 'n')
102 continue;
104 setdot(mp = &message[msgvec[i] - 1]);
105 did_print_dot = TRU1;
106 touch(mp);
108 sigint = safe_signal(SIGINT, SIG_IGN);
109 --mp->m_size; /* Strip final NL.. */
110 fp = run_editor(fp, -1/*mp->m_size*/, viored,
111 (mb.mb_perm & MB_EDIT) == 0 || ! wb,
112 NULL, mp, wb ? SEND_MBOX : SEND_TODISP_ALL,
113 sigint);
114 ++mp->m_size; /* And readd it */
115 if (fp != NULL) {
116 fseek(mb.mb_otf, 0L, SEEK_END);
117 size = ftell(mb.mb_otf);
118 mp->m_block = mailx_blockof(size);
119 mp->m_offset = mailx_offsetof(size);
120 mp->m_lines = 0;
121 mp->m_flag |= MODIFY;
122 rewind(fp);
123 lastnl = 0;
124 size = 0;
125 while ((c = getc(fp)) != EOF) {
126 if ((lastnl = c == '\n'))
127 mp->m_lines++;
128 if (putc(c, mb.mb_otf) == EOF)
129 break;
130 ++size;
132 if (! lastnl && putc('\n', mb.mb_otf) != EOF)
133 ++size;
134 if (putc('\n', mb.mb_otf) != EOF)
135 ++size;
136 mp->m_size = (size_t)size;
137 if (ferror(mb.mb_otf))
138 perror("/tmp");
139 Fclose(fp);
141 safe_signal(SIGINT, sigint);
143 if (line)
144 free(line);
145 return 0;
149 * Run an editor on the file at "fpp" of "size" bytes,
150 * and return a new file pointer.
151 * Signals must be handled by the caller.
152 * "viored" is 'e' for ed, 'v' for vi.
154 FL FILE *
155 run_editor(FILE *fp, off_t size, int viored, int readonly,
156 struct header *hp, struct message *mp, enum sendaction action,
157 sighandler_type oldint)
159 FILE *nf = NULL;
160 int t;
161 time_t modtime;
162 char const *ed;
163 struct stat statb;
164 char *tempEdit;
165 sigset_t cset;
167 if ((nf = Ftemp(&tempEdit, "Re", "w", readonly ? 0400 : 0600, 1))
168 == NULL) {
169 perror(tr(73, "temporary mail edit file"));
170 goto out;
172 if (hp) {
173 t = GTO|GSUBJECT|GCC|GBCC|GNL|GCOMMA;
174 if (hp->h_from || hp->h_replyto || hp->h_sender ||
175 hp->h_organization)
176 t |= GIDENT;
177 puthead(hp, nf, t, SEND_TODISP, CONV_NONE, NULL, NULL);
179 if (mp) {
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);
189 fflush(nf);
190 if (fstat(fileno(nf), &statb) < 0)
191 modtime = 0;
192 else
193 modtime = statb.st_mtime;
194 if (ferror(nf)) {
195 Fclose(nf);
196 perror(tempEdit);
197 nf = NULL;
198 goto out;
200 if (Fclose(nf) < 0) {
201 perror(tempEdit);
202 nf = NULL;
203 goto out;
205 nf = NULL;
207 if ((ed = value(viored == 'e' ? "EDITOR" : "VISUAL")) == NULL)
208 ed = (viored == 'e') ? "ed" : "vi";
209 sigemptyset(&cset);
210 if (run_command(ed, oldint != SIG_IGN ? &cset : NULL, -1, -1,
211 tempEdit, NULL, NULL) < 0)
212 goto out;
215 * If in read only mode or file unchanged, just remove the editor
216 * temporary and return. Otherwise switch to new file.
218 if (readonly)
219 goto out;
220 if (stat(tempEdit, &statb) < 0) {
221 perror(tempEdit);
222 Ftfree(&tempEdit);
223 goto out;
225 if (modtime != statb.st_mtime && (nf = Fopen(tempEdit, "a+")) == NULL)
226 perror(tempEdit);
227 out:
228 if (tempEdit != NULL) {
229 unlink(tempEdit);
230 Ftfree(&tempEdit);
232 return nf;