Fix -u, change meaning of $USER to be EQ -u etc..
[s-mailx.git] / edit.c
blob588989b227120b93cebdcba6d556597aa9c0fc31
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 - 2013 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 #include "nail.h"
42 static int edit1(int *msgvec, int viored);
45 * Edit a message list.
47 int
48 editor(void *v)
50 int *msgvec = v;
52 return edit1(msgvec, 'e');
56 * Invoke the visual editor on a message list.
58 int
59 visual(void *v)
61 int *msgvec = v;
63 return edit1(msgvec, 'v');
67 * Edit a message by writing the message into a funnily-named file
68 * (which should not exist) and forking an editor on it.
69 * We get the editor from the stuff above.
71 static int
72 edit1(int *msgvec, int viored)
74 int c, i, wb, lastnl;
75 FILE *fp = NULL;
76 struct message *mp;
77 off_t size;
78 char *line = NULL;
79 size_t linesize;
82 * Deal with each message to be edited . . .
84 wb = value("writebackedited") != NULL;
85 for (i = 0; msgvec[i] && i < msgCount; i++) {
86 sighandler_type sigint;
88 if (i > 0) {
89 char *p;
91 printf(tr(72, "Edit message %d [ynq]? "), msgvec[i]);
92 fflush(stdout);
93 if (readline_restart(stdin, &line, &linesize, 0) < 0)
94 break;
95 for (p = line; blankchar(*p); p++)
97 if (*p == 'q')
98 break;
99 if (*p == 'n')
100 continue;
102 setdot(mp = &message[msgvec[i] - 1]);
103 did_print_dot = TRU1;
104 touch(mp);
106 sigint = safe_signal(SIGINT, SIG_IGN);
107 --mp->m_size; /* Strip final NL.. */
108 fp = run_editor(fp, -1/*mp->m_size*/, viored,
109 (mb.mb_perm & MB_EDIT) == 0 || ! wb,
110 NULL, mp, wb ? SEND_MBOX : SEND_TODISP_ALL,
111 sigint);
112 ++mp->m_size; /* And readd it */
113 if (fp != NULL) {
114 fseek(mb.mb_otf, 0L, SEEK_END);
115 size = ftell(mb.mb_otf);
116 mp->m_block = mailx_blockof(size);
117 mp->m_offset = mailx_offsetof(size);
118 mp->m_lines = 0;
119 mp->m_flag |= MODIFY;
120 rewind(fp);
121 lastnl = 0;
122 size = 0;
123 while ((c = getc(fp)) != EOF) {
124 if ((lastnl = c == '\n'))
125 mp->m_lines++;
126 if (putc(c, mb.mb_otf) == EOF)
127 break;
128 ++size;
130 if (! lastnl && putc('\n', mb.mb_otf) != EOF)
131 ++size;
132 if (putc('\n', mb.mb_otf) != EOF)
133 ++size;
134 mp->m_size = (size_t)size;
135 if (ferror(mb.mb_otf))
136 perror("/tmp");
137 Fclose(fp);
139 safe_signal(SIGINT, sigint);
141 if (line)
142 free(line);
143 return 0;
147 * Run an editor on the file at "fpp" of "size" bytes,
148 * and return a new file pointer.
149 * Signals must be handled by the caller.
150 * "viored" is 'e' for ed, 'v' for vi.
152 FILE *
153 run_editor(FILE *fp, off_t size, int viored, int readonly,
154 struct header *hp, struct message *mp, enum sendaction action,
155 sighandler_type oldint)
157 FILE *nf = NULL;
158 int t;
159 time_t modtime;
160 char const *ed;
161 struct stat statb;
162 char *tempEdit;
163 sigset_t cset;
165 if ((nf = Ftemp(&tempEdit, "Re", "w", readonly ? 0400 : 0600, 1))
166 == NULL) {
167 perror(tr(73, "temporary mail edit file"));
168 goto out;
170 if (hp) {
171 t = GTO|GSUBJECT|GCC|GBCC|GNL|GCOMMA;
172 if (hp->h_from || hp->h_replyto || hp->h_sender ||
173 hp->h_organization)
174 t |= GIDENT;
175 puthead(hp, nf, t, SEND_TODISP, CONV_NONE, NULL, NULL);
177 if (mp) {
178 sendmp(mp, nf, 0, NULL, action, NULL);
179 } else {
180 if (size >= 0)
181 while (--size >= 0 && (t = getc(fp)) != EOF)
182 putc(t, nf);
183 else
184 while ((t = getc(fp)) != EOF)
185 putc(t, nf);
187 fflush(nf);
188 if (fstat(fileno(nf), &statb) < 0)
189 modtime = 0;
190 else
191 modtime = statb.st_mtime;
192 if (ferror(nf)) {
193 Fclose(nf);
194 perror(tempEdit);
195 nf = NULL;
196 goto out;
198 if (Fclose(nf) < 0) {
199 perror(tempEdit);
200 nf = NULL;
201 goto out;
203 nf = NULL;
205 if ((ed = value(viored == 'e' ? "EDITOR" : "VISUAL")) == NULL)
206 ed = (viored == 'e') ? "ed" : "vi";
207 sigemptyset(&cset);
208 if (run_command(ed, oldint != SIG_IGN ? &cset : NULL, -1, -1,
209 tempEdit, NULL, NULL) < 0)
210 goto out;
213 * If in read only mode or file unchanged, just remove the editor
214 * temporary and return. Otherwise switch to new file.
216 if (readonly)
217 goto out;
218 if (stat(tempEdit, &statb) < 0) {
219 perror(tempEdit);
220 Ftfree(&tempEdit);
221 goto out;
223 if (modtime != statb.st_mtime && (nf = Fopen(tempEdit, "a+")) == NULL)
224 perror(tempEdit);
225 out:
226 if (tempEdit != NULL) {
227 unlink(tempEdit);
228 Ftfree(&tempEdit);
230 return nf;