NetBSD/usr.bin/mail/edit.c

245 lines
5.9 KiB
C
Raw Normal View History

/* $NetBSD: edit.c,v 1.15 2002/03/29 15:07:52 ross Exp $ */
1993-03-21 12:45:37 +03:00
/*
1994-06-29 09:09:04 +04:00
* Copyright (c) 1980, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
#if 0
static char sccsid[] = "@(#)edit.c 8.1 (Berkeley) 6/6/93";
#else
__RCSID("$NetBSD: edit.c,v 1.15 2002/03/29 15:07:52 ross Exp $");
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#include "rcv.h"
1994-06-29 09:09:04 +04:00
#include "extern.h"
1993-03-21 12:45:37 +03:00
/*
* Mail -- a mail program
*
* Perform message editing functions.
*/
extern char *tmpdir;
1993-03-21 12:45:37 +03:00
/*
* Edit a message list.
*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
editor(void *v)
1993-03-21 12:45:37 +03:00
{
int *msgvec = v;
1993-03-21 12:45:37 +03:00
return edit1(msgvec, 'e');
}
/*
* Invoke the visual editor on a message list.
*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
visual(void *v)
1993-03-21 12:45:37 +03:00
{
int *msgvec = v;
1993-03-21 12:45:37 +03:00
return edit1(msgvec, 'v');
}
/*
* Edit a message by writing the message into a funnily-named file
* (which should not exist) and forking an editor on it.
* We get the editor from the stuff above.
*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 18:27:51 +03:00
edit1(int *msgvec, int editortype)
1993-03-21 12:45:37 +03:00
{
int c;
1993-03-21 12:45:37 +03:00
int i;
FILE *fp;
struct message *mp;
1993-03-21 12:45:37 +03:00
off_t size;
/*
* Deal with each message to be edited . . .
*/
for (i = 0; msgvec[i] && i < msgCount; i++) {
sig_t sigint;
if (i > 0) {
char buf[100];
char *p;
printf("Edit message %d [ynq]? ", msgvec[i]);
if (fgets(buf, sizeof buf, stdin) == 0)
break;
for (p = buf; *p == ' ' || *p == '\t'; p++)
;
if (*p == 'q')
break;
if (*p == 'n')
continue;
}
dot = mp = &message[msgvec[i] - 1];
touch(mp);
sigint = signal(SIGINT, SIG_IGN);
2002-03-02 18:27:51 +03:00
fp = run_editor(setinput(mp), mp->m_size, editortype,
readonly);
1993-03-21 12:45:37 +03:00
if (fp != NULL) {
2002-03-06 00:18:14 +03:00
(void)fseek(otf, 0L, 2);
1993-03-21 12:45:37 +03:00
size = ftell(otf);
mp->m_block = blockof(size);
mp->m_offset = offsetof(size);
mp->m_size = fsize(fp);
mp->m_lines = 0;
mp->m_blines = 0;
1993-03-21 12:45:37 +03:00
mp->m_flag |= MODIFY;
rewind(fp);
while ((c = getc(fp)) != EOF) {
//
// XXX. if you edit a message, we treat
// header lines as body lines in the recount.
// This is because the original message copy
// and the edit reread use different code to
// count the lines, and this one here is
// simple-minded.
//
if (c == '\n') {
1993-03-21 12:45:37 +03:00
mp->m_lines++;
mp->m_blines++;
}
1993-03-21 12:45:37 +03:00
if (putc(c, otf) == EOF)
break;
}
if (ferror(otf))
2002-03-06 00:29:30 +03:00
warn("/tmp");
2002-03-06 00:18:14 +03:00
(void)Fclose(fp);
1993-03-21 12:45:37 +03:00
}
2002-03-06 00:18:14 +03:00
(void)signal(SIGINT, sigint);
1993-03-21 12:45:37 +03:00
}
return 0;
}
/*
* Run an editor on the file at "fpp" of "size" bytes,
* and return a new file pointer.
* Signals must be handled by the caller.
2002-03-02 18:27:51 +03:00
* "Editortype" is 'e' for _PATH_EX, 'v' for _PATH_VI.
1993-03-21 12:45:37 +03:00
*/
FILE *
2002-03-02 18:27:51 +03:00
run_editor(FILE *fp, off_t size, int editortype, int readonlyflag)
1993-03-21 12:45:37 +03:00
{
FILE *nf = NULL;
int t;
1993-03-21 12:45:37 +03:00
time_t modtime;
2002-03-02 18:27:51 +03:00
char *editcmd;
char tempname[PATHSIZE];
1993-03-21 12:45:37 +03:00
struct stat statb;
(void)snprintf(tempname, sizeof(tempname),
"%s/mail.ReXXXXXXXXXX", tmpdir);
if ((t = mkstemp(tempname)) == -1) {
warn("%s", tempname);
goto out;
}
if (readonlyflag && fchmod(t, 0400) == -1) {
close(t);
warn("%s", tempname);
(void)unlink(tempname);
1993-03-21 12:45:37 +03:00
goto out;
}
if ((nf = Fdopen(t, "w")) == NULL) {
close(t);
warn("%s", tempname);
(void)unlink(tempname);
1993-03-21 12:45:37 +03:00
goto out;
}
if (size >= 0)
while (--size >= 0 && (t = getc(fp)) != EOF)
2002-03-06 00:18:14 +03:00
(void)putc(t, nf);
1993-03-21 12:45:37 +03:00
else
while ((t = getc(fp)) != EOF)
2002-03-06 00:18:14 +03:00
(void)putc(t, nf);
(void)fflush(nf);
1993-03-21 12:45:37 +03:00
if (ferror(nf)) {
2002-03-06 00:18:14 +03:00
(void)Fclose(nf);
warn("%s", tempname);
(void)unlink(tempname);
1993-03-21 12:45:37 +03:00
nf = NULL;
goto out;
}
if (fstat(fileno(nf), &statb) < 0)
modtime = 0;
else
modtime = statb.st_mtime;
1993-03-21 12:45:37 +03:00
if (Fclose(nf) < 0) {
warn("%s", tempname);
(void)unlink(tempname);
1993-03-21 12:45:37 +03:00
nf = NULL;
goto out;
}
nf = NULL;
2002-03-02 18:27:51 +03:00
if ((editcmd =
value(editortype == 'e' ? "EDITOR" : "VISUAL")) == NULL)
2002-03-02 18:27:51 +03:00
editcmd = editortype == 'e' ? _PATH_EX : _PATH_VI;
if (run_command(editcmd, 0, -1, -1, tempname, NULL, NULL) < 0) {
(void)unlink(tempname);
1993-03-21 12:45:37 +03:00
goto out;
}
/*
* If in read only mode or file unchanged, just remove the editor
* temporary and return.
*/
2002-03-02 18:27:51 +03:00
if (readonlyflag) {
(void)unlink(tempname);
1993-03-21 12:45:37 +03:00
goto out;
}
if (stat(tempname, &statb) < 0) {
warn("%s", tempname);
1993-03-21 12:45:37 +03:00
goto out;
}
if (modtime == statb.st_mtime) {
(void)unlink(tempname);
1993-03-21 12:45:37 +03:00
goto out;
}
/*
* Now switch to new file.
*/
if ((nf = Fopen(tempname, "a+")) == NULL) {
warn("%s", tempname);
(void)unlink(tempname);
1993-03-21 12:45:37 +03:00
goto out;
}
(void)unlink(tempname);
1993-03-21 12:45:37 +03:00
out:
return nf;
}