tty_addhist(): don't add empty and strings with [0]=U+0020
[s-mailx.git] / edit.c
blobcdab8cd47cd95f41e8f9ae989ea99b1585e55c5f
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 "rcv.h"
42 #include <sys/stat.h>
43 #include <unistd.h>
45 #include "extern.h"
47 static int edit1(int *msgvec, int type);
50 * Edit a message list.
52 int
53 editor(void *v)
55 int *msgvec = v;
57 return edit1(msgvec, 'e');
61 * Invoke the visual editor on a message list.
63 int
64 visual(void *v)
66 int *msgvec = v;
68 return edit1(msgvec, 'v');
72 * Edit a message by writing the message into a funnily-named file
73 * (which should not exist) and forking an editor on it.
74 * We get the editor from the stuff above.
76 static int
77 edit1(int *msgvec, int type)
79 int c, i, wb, lastnl;
80 FILE *fp = NULL;
81 struct message *mp;
82 off_t size;
83 char *line = NULL;
84 size_t linesize;
87 * Deal with each message to be edited . . .
89 wb = value("writebackedited") != NULL;
90 for (i = 0; msgvec[i] && i < msgCount; i++) {
91 sighandler_type sigint;
93 if (i > 0) {
94 char *p;
96 printf(tr(72, "Edit message %d [ynq]? "), msgvec[i]);
97 fflush(stdout);
98 if (readline_restart(stdin, &line, &linesize, 0) < 0)
99 break;
100 for (p = line; blankchar(*p); p++)
102 if (*p == 'q')
103 break;
104 if (*p == 'n')
105 continue;
107 setdot(mp = &message[msgvec[i] - 1]);
108 did_print_dot = TRU1;
109 touch(mp);
111 sigint = safe_signal(SIGINT, SIG_IGN);
112 --mp->m_size; /* Strip final NL.. */
113 fp = run_editor(fp, -1/*mp->m_size*/, type,
114 (mb.mb_perm & MB_EDIT) == 0 || ! wb,
115 NULL, mp, wb ? SEND_MBOX : SEND_TODISP_ALL,
116 sigint);
117 ++mp->m_size; /* And readd it */
118 if (fp != NULL) {
119 fseek(mb.mb_otf, 0L, SEEK_END);
120 size = ftell(mb.mb_otf);
121 mp->m_block = mailx_blockof(size);
122 mp->m_offset = mailx_offsetof(size);
123 mp->m_lines = 0;
124 mp->m_flag |= MODIFY;
125 rewind(fp);
126 lastnl = 0;
127 size = 0;
128 while ((c = getc(fp)) != EOF) {
129 if ((lastnl = c == '\n'))
130 mp->m_lines++;
131 if (putc(c, mb.mb_otf) == EOF)
132 break;
133 ++size;
135 if (! lastnl && putc('\n', mb.mb_otf) != EOF)
136 ++size;
137 if (putc('\n', mb.mb_otf) != EOF)
138 ++size;
139 mp->m_size = (size_t)size;
140 if (ferror(mb.mb_otf))
141 perror("/tmp");
142 Fclose(fp);
144 safe_signal(SIGINT, sigint);
146 if (line)
147 free(line);
148 return 0;
152 * Run an editor on the file at "fpp" of "size" bytes,
153 * and return a new file pointer.
154 * Signals must be handled by the caller.
155 * "Type" is 'e' for ed, 'v' for vi.
157 FILE *
158 run_editor(FILE *fp, off_t size, int type, int readonly,
159 struct header *hp, struct message *mp, enum sendaction action,
160 sighandler_type oldint)
162 FILE *nf = NULL;
163 int t;
164 time_t modtime;
165 char const *editor;
166 struct stat statb;
167 char *tempEdit;
168 sigset_t set;
170 if ((nf = Ftemp(&tempEdit, "Re", "w", readonly ? 0400 : 0600, 1))
171 == NULL) {
172 perror(catgets(catd, CATSET, 73, "temporary mail edit file"));
173 goto out;
175 if (hp) {
176 t = GTO|GSUBJECT|GCC|GBCC|GNL|GCOMMA;
177 if (hp->h_from || hp->h_replyto || hp->h_sender ||
178 hp->h_organization)
179 t |= GIDENT;
180 puthead(hp, nf, t, SEND_TODISP, CONV_NONE, NULL, NULL);
182 if (mp) {
183 send(mp, nf, 0, NULL, action, NULL);
184 } else {
185 if (size >= 0)
186 while (--size >= 0 && (t = getc(fp)) != EOF)
187 putc(t, nf);
188 else
189 while ((t = getc(fp)) != EOF)
190 putc(t, nf);
192 fflush(nf);
193 if (fstat(fileno(nf), &statb) < 0)
194 modtime = 0;
195 else
196 modtime = statb.st_mtime;
197 if (ferror(nf)) {
198 Fclose(nf);
199 perror(tempEdit);
200 nf = NULL;
201 goto out;
203 if (Fclose(nf) < 0) {
204 perror(tempEdit);
205 nf = NULL;
206 goto out;
208 nf = NULL;
210 if ((editor = value(type == 'e' ? "EDITOR" : "VISUAL")) == NULL)
211 editor = (type == 'e') ? "ed" : "vi";
212 sigemptyset(&set);
213 if (run_command(editor, oldint != SIG_IGN ? &set : NULL, -1, -1,
214 tempEdit, NULL, NULL) < 0)
215 goto out;
218 * If in read only mode or file unchanged, just remove the editor
219 * temporary and return. Otherwise switch to new file.
221 if (readonly)
222 goto out;
223 if (stat(tempEdit, &statb) < 0) {
224 perror(tempEdit);
225 Ftfree(&tempEdit);
226 goto out;
228 if (modtime != statb.st_mtime && (nf = Fopen(tempEdit, "a+")) == NULL)
229 perror(tempEdit);
230 out:
231 if (tempEdit != NULL) {
232 unlink(tempEdit);
233 Ftfree(&tempEdit);
235 return nf;