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 - 2018 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
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
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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #ifndef HAVE_AMALGAMATION
42 /* Edit a message by writing the message into a funnily-named file (which
43 * should not exist) and forking an editor on it */
44 static int edit1(int *msgvec
, int viored
);
47 edit1(int *msgvec
, int viored
)
56 wb
= ok_blook(writebackedited
);
58 /* Deal with each message to be edited... */
59 for (i
= 0; msgvec
[i
] != 0 && i
< msgCount
; ++i
) {
60 sighandler_type sigint
;
65 snprintf(prompt
, sizeof prompt
, _("Edit message %d"), msgvec
[i
]);
66 if(!getapproval(prompt
, TRU1
))
70 mp
= message
+ msgvec
[i
] - 1;
72 n_pstate
|= n_PS_DID_PRINT_DOT
;
75 sigint
= safe_signal(SIGINT
, SIG_IGN
);
77 --mp
->m_size
; /* Strip final NL.. TODO MAILVFS->MESSAGE->length() */
78 fp
= n_run_editor(fp
, -1/*mp->m_size TODO */, viored
,
79 ((mb
.mb_perm
& MB_EDIT
) == 0 || !wb
), NULL
, mp
,
80 (wb
? SEND_MBOX
: SEND_TODISP_ALL
), sigint
, NULL
);
81 ++mp
->m_size
; /* And readd it TODO */
84 fseek(mb
.mb_otf
, 0L, SEEK_END
);
85 size
= ftell(mb
.mb_otf
);
86 mp
->m_block
= mailx_blockof(size
);
87 mp
->m_offset
= mailx_offsetof(size
);
93 while ((c
= getc(fp
)) != EOF
) {
94 if ((lastnl
= (c
== '\n')))
96 if (putc(c
, mb
.mb_otf
) == EOF
)
100 if (!lastnl
&& putc('\n', mb
.mb_otf
) != EOF
)
102 if (putc('\n', mb
.mb_otf
) != EOF
)
104 mp
->m_size
= (size_t)size
;
105 if (ferror(mb
.mb_otf
))
106 n_perr(_("/tmp"), 0);
110 safe_signal(SIGINT
, sigint
);
122 rv
= edit1(msgvec
, 'e');
133 rv
= edit1(msgvec
, 'v');
139 n_run_editor(FILE *fp
, off_t size
, int viored
, bool_t readonly
,
140 struct header
*hp
, struct message
*mp
, enum sendaction action
,
141 sighandler_type oldint
, char const *pipecmd
)
149 FILE *nf
, *nf_pipetmp
, *nf_tmp
;
152 nf
= nf_pipetmp
= NULL
;
154 modtime
= 0, modsize
= 0;
156 if((nf_tmp
= Ftmp(&tmp_name
, "runed",
157 ((viored
== '|' ? OF_RDWR
: OF_WRONLY
) | OF_REGISTER
|
158 OF_REGISTER_UNLINK
| OF_FN_AUTOREC
))) == NULL
){
160 n_perr(_("temporary mail edit file"), 0);
166 if(!n_header_put4compose(nf_tmp
, hp
))
172 if(sendmp(mp
, nf_tmp
, NULL
, NULL
, action
, NULL
) < 0){
173 n_err(_("Failed to prepare editable message\n"));
178 while(--size
>= 0 && (t
= getc(fp
)) != EOF
)
179 if(putc(t
, nf_tmp
) == EOF
)
182 while((t
= getc(fp
)) != EOF
)
183 if(putc(t
, nf_tmp
) == EOF
)
190 if((t
= (fp
!= NULL
&& ferror(fp
))) == 0 && (t
= ferror(nf_tmp
)) == 0){
192 if(!fstat(fileno(nf_tmp
), &statb
))
193 modtime
= statb
.st_mtime
, modsize
= statb
.st_size
;
196 t
= (fchmod(fileno(nf_tmp
), S_IRUSR
) != 0);
206 assert(pipecmd
!= NULL
);
208 if((nf_pipetmp
= Ftmp(&tmp_name
, "runed", OF_WRONLY
| OF_REGISTER
|
209 OF_REGISTER_UNLINK
| OF_FN_AUTOREC
)) == NULL
)
211 really_rewind(nf
= nf_tmp
);
215 if(n_child_run(ok_vlook(SHELL
), 0, fileno(nf_pipetmp
), fileno(nf_tmp
),
216 "-c", pipecmd
, NULL
, NULL
, &ws
) < 0 || WEXITSTATUS(ws
) != 0)
220 if(n_child_run((viored
== 'e' ? ok_vlook(EDITOR
) : ok_vlook(VISUAL
)),
221 (oldint
!= SIG_IGN
? &cset
: NULL
),
222 n_CHILD_FD_PASS
, n_CHILD_FD_PASS
, tmp_name
, NULL
, NULL
, NULL
,
223 &ws
) < 0 || WEXITSTATUS(ws
) != 0)
227 /* If in read only mode or file unchanged, just remove the editor temporary
228 * and return. Otherwise switch to new file */
232 if(stat(tmp_name
, &statb
) == -1){
236 if(modtime
== statb
.st_mtime
&& modsize
== statb
.st_size
)
240 if((nf
= Fopen(tmp_name
, "r+")) == NULL
)
244 if(nf_pipetmp
!= NULL
)
246 if(nf_tmp
!= NULL
&& Fclose(nf_tmp
) < 0){