2 * Heirloom mailx - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
7 * Copyright (c) 1980, 1993
8 * The Regents of the University of California. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 static char sccsid
[] = "@(#)edit.c 2.24 (gritter) 3/4/06";
51 * Mail -- a mail program
53 * Perform message editing functions.
56 static int edit1(int *msgvec
, int type
);
59 * Edit a message list.
66 return edit1(msgvec
, 'e');
70 * Invoke the visual editor on a message list.
77 return edit1(msgvec
, 'v');
81 * Edit a message by writing the message into a funnily-named file
82 * (which should not exist) and forking an editor on it.
83 * We get the editor from the stuff above.
86 edit1(int *msgvec
, int type
)
98 * Deal with each message to be edited . . .
100 wb
= value("writebackedited") != NULL
;
101 for (i
= 0; msgvec
[i
] && i
< msgCount
; i
++) {
102 sighandler_type sigint
;
107 printf(catgets(catd
, CATSET
, 72,
108 "Edit message %d [ynq]? "), msgvec
[i
]);
110 if (readline(stdin
, &line
, &linesize
) < 0)
112 for (p
= line
; blankchar(*p
& 0377); p
++);
118 setdot(mp
= &message
[msgvec
[i
] - 1]);
121 sigint
= safe_signal(SIGINT
, SIG_IGN
);
122 fp
= run_editor(fp
, mp
->m_size
, type
,
123 (mb
.mb_perm
& MB_EDIT
) == 0 || !wb
,
124 NULL
, mp
, wb
? SEND_MBOX
: SEND_TODISP_ALL
,
127 fseek(mb
.mb_otf
, 0L, SEEK_END
);
128 size
= ftell(mb
.mb_otf
);
129 mp
->m_block
= mailx_blockof(size
);
130 mp
->m_offset
= mailx_offsetof(size
);
131 mp
->m_size
= fsize(fp
);
133 mp
->m_flag
|= MODIFY
;
135 while ((c
= getc(fp
)) != EOF
) {
138 if (putc(c
, mb
.mb_otf
) == EOF
)
141 if (ferror(mb
.mb_otf
))
145 safe_signal(SIGINT
, sigint
);
153 * Run an editor on the file at "fpp" of "size" bytes,
154 * and return a new file pointer.
155 * Signals must be handled by the caller.
156 * "Type" is 'e' for ed, 'v' for vi.
159 run_editor(FILE *fp
, off_t size
, int type
, int readonly
,
160 struct header
*hp
, struct message
*mp
, enum sendaction action
,
161 sighandler_type oldint
)
171 if ((nf
= Ftemp(&tempEdit
, "Re", "w", readonly
? 0400 : 0600, 1))
173 perror(catgets(catd
, CATSET
, 73, "temporary mail edit file"));
177 t
= GTO
|GSUBJECT
|GCC
|GBCC
|GNL
|GCOMMA
;
178 if (hp
->h_from
|| hp
->h_replyto
|| hp
->h_sender
||
181 puthead(hp
, nf
, t
, SEND_TODISP
, CONV_NONE
, NULL
, NULL
);
184 send(mp
, nf
, 0, NULL
, action
, NULL
);
187 while (--size
>= 0 && (t
= getc(fp
)) != EOF
)
190 while ((t
= getc(fp
)) != EOF
)
194 if (fstat(fileno(nf
), &statb
) < 0)
197 modtime
= statb
.st_mtime
;
206 if (Fclose(nf
) < 0) {
214 if ((edit
= value(type
== 'e' ? "EDITOR" : "VISUAL")) == NULL
)
215 edit
= type
== 'e' ? "ed" : "vi";
217 if (run_command(edit
, oldint
!= SIG_IGN
? &set
: NULL
, -1, -1,
218 tempEdit
, NULL
, NULL
) < 0) {
224 * If in read only mode or file unchanged, just remove the editor
225 * temporary and return.
232 if (stat(tempEdit
, &statb
) < 0) {
237 if (modtime
== statb
.st_mtime
) {
243 * Now switch to new file.
245 if ((nf
= Fopen(tempEdit
, "a+")) == NULL
)