Home | History | Annotate | Line # | Download | only in mail
edit.c revision 1.24.14.1
      1  1.24.14.1       jym /*	$NetBSD: edit.c,v 1.24.14.1 2009/05/13 19:19:56 jym Exp $	*/
      2        1.5  christos 
      3        1.1       cgd /*
      4        1.3   deraadt  * Copyright (c) 1980, 1993
      5        1.3   deraadt  *	The Regents of the University of California.  All rights reserved.
      6        1.1       cgd  *
      7        1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8        1.1       cgd  * modification, are permitted provided that the following conditions
      9        1.1       cgd  * are met:
     10        1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11        1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12        1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14        1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15       1.18       agc  * 3. Neither the name of the University nor the names of its contributors
     16        1.1       cgd  *    may be used to endorse or promote products derived from this software
     17        1.1       cgd  *    without specific prior written permission.
     18        1.1       cgd  *
     19        1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20        1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21        1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22        1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23        1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24        1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25        1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26        1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27        1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28        1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29        1.1       cgd  * SUCH DAMAGE.
     30        1.1       cgd  */
     31        1.1       cgd 
     32        1.6     lukem #include <sys/cdefs.h>
     33        1.1       cgd #ifndef lint
     34        1.5  christos #if 0
     35        1.5  christos static char sccsid[] = "@(#)edit.c	8.1 (Berkeley) 6/6/93";
     36        1.5  christos #else
     37  1.24.14.1       jym __RCSID("$NetBSD: edit.c,v 1.24.14.1 2009/05/13 19:19:56 jym Exp $");
     38        1.5  christos #endif
     39        1.1       cgd #endif /* not lint */
     40        1.1       cgd 
     41        1.1       cgd #include "rcv.h"
     42        1.3   deraadt #include "extern.h"
     43       1.21  christos #include "thread.h"
     44  1.24.14.1       jym #include "sig.h"
     45        1.1       cgd 
     46        1.1       cgd /*
     47        1.1       cgd  * Mail -- a mail program
     48        1.1       cgd  *
     49        1.1       cgd  * Perform message editing functions.
     50        1.1       cgd  */
     51        1.1       cgd 
     52        1.1       cgd /*
     53        1.1       cgd  * Run an editor on the file at "fpp" of "size" bytes,
     54        1.1       cgd  * and return a new file pointer.
     55        1.1       cgd  * Signals must be handled by the caller.
     56       1.10       wiz  * "Editortype" is 'e' for _PATH_EX, 'v' for _PATH_VI.
     57        1.1       cgd  */
     58       1.21  christos PUBLIC FILE *
     59       1.10       wiz run_editor(FILE *fp, off_t size, int editortype, int readonlyflag)
     60        1.1       cgd {
     61        1.6     lukem 	FILE *nf = NULL;
     62        1.6     lukem 	int t;
     63        1.1       cgd 	time_t modtime;
     64       1.19  christos 	const char *editcmd;
     65       1.14       wiz 	char tempname[PATHSIZE];
     66        1.1       cgd 	struct stat statb;
     67        1.1       cgd 
     68       1.14       wiz 	(void)snprintf(tempname, sizeof(tempname),
     69       1.14       wiz 	    "%s/mail.ReXXXXXXXXXX", tmpdir);
     70       1.14       wiz 	if ((t = mkstemp(tempname)) == -1) {
     71       1.14       wiz 		warn("%s", tempname);
     72       1.14       wiz 		goto out;
     73       1.14       wiz 	}
     74       1.14       wiz 	if (readonlyflag && fchmod(t, 0400) == -1) {
     75       1.20  christos 		(void)close(t);
     76       1.14       wiz 		warn("%s", tempname);
     77       1.14       wiz 		(void)unlink(tempname);
     78        1.1       cgd 		goto out;
     79        1.1       cgd 	}
     80        1.1       cgd 	if ((nf = Fdopen(t, "w")) == NULL) {
     81       1.20  christos 		(void)close(t);
     82       1.14       wiz 		warn("%s", tempname);
     83       1.14       wiz 		(void)unlink(tempname);
     84        1.1       cgd 		goto out;
     85        1.1       cgd 	}
     86        1.1       cgd 	if (size >= 0)
     87        1.1       cgd 		while (--size >= 0 && (t = getc(fp)) != EOF)
     88       1.12       wiz 			(void)putc(t, nf);
     89        1.1       cgd 	else
     90        1.1       cgd 		while ((t = getc(fp)) != EOF)
     91       1.12       wiz 			(void)putc(t, nf);
     92       1.12       wiz 	(void)fflush(nf);
     93        1.1       cgd 	if (ferror(nf)) {
     94       1.12       wiz 		(void)Fclose(nf);
     95       1.14       wiz 		warn("%s", tempname);
     96       1.14       wiz 		(void)unlink(tempname);
     97        1.1       cgd 		nf = NULL;
     98        1.1       cgd 		goto out;
     99        1.1       cgd 	}
    100        1.7       bad 	if (fstat(fileno(nf), &statb) < 0)
    101        1.7       bad 		modtime = 0;
    102        1.7       bad 	else
    103        1.7       bad 		modtime = statb.st_mtime;
    104        1.1       cgd 	if (Fclose(nf) < 0) {
    105       1.14       wiz 		warn("%s", tempname);
    106       1.14       wiz 		(void)unlink(tempname);
    107        1.1       cgd 		nf = NULL;
    108        1.1       cgd 		goto out;
    109        1.1       cgd 	}
    110        1.1       cgd 	nf = NULL;
    111  1.24.14.1       jym 	editcmd = value(editortype == 'e' ? ENAME_EDITOR : ENAME_VISUAL);
    112  1.24.14.1       jym 	if (editcmd == NULL)
    113       1.10       wiz 		editcmd = editortype == 'e' ? _PATH_EX : _PATH_VI;
    114  1.24.14.1       jym 	if (run_command(editcmd, NULL, 0, -1, tempname, NULL) < 0) {
    115       1.14       wiz 		(void)unlink(tempname);
    116        1.1       cgd 		goto out;
    117        1.1       cgd 	}
    118        1.1       cgd 	/*
    119        1.1       cgd 	 * If in read only mode or file unchanged, just remove the editor
    120        1.1       cgd 	 * temporary and return.
    121        1.1       cgd 	 */
    122       1.10       wiz 	if (readonlyflag) {
    123       1.14       wiz 		(void)unlink(tempname);
    124        1.1       cgd 		goto out;
    125        1.1       cgd 	}
    126       1.14       wiz 	if (stat(tempname, &statb) < 0) {
    127       1.14       wiz 		warn("%s", tempname);
    128        1.1       cgd 		goto out;
    129        1.1       cgd 	}
    130        1.1       cgd 	if (modtime == statb.st_mtime) {
    131       1.14       wiz 		(void)unlink(tempname);
    132        1.1       cgd 		goto out;
    133        1.1       cgd 	}
    134        1.1       cgd 	/*
    135        1.1       cgd 	 * Now switch to new file.
    136        1.1       cgd 	 */
    137       1.14       wiz 	if ((nf = Fopen(tempname, "a+")) == NULL) {
    138       1.14       wiz 		warn("%s", tempname);
    139       1.14       wiz 		(void)unlink(tempname);
    140        1.1       cgd 		goto out;
    141        1.1       cgd 	}
    142       1.14       wiz 	(void)unlink(tempname);
    143        1.1       cgd out:
    144        1.1       cgd 	return nf;
    145        1.1       cgd }
    146       1.21  christos 
    147       1.21  christos /*
    148       1.21  christos  * Edit a message by writing the message into a funnily-named file
    149       1.21  christos  * (which should not exist) and forking an editor on it.
    150       1.21  christos  * We get the editor from the stuff above.
    151       1.21  christos  */
    152       1.21  christos static int
    153       1.21  christos edit1(int *msgvec, int editortype)
    154       1.21  christos {
    155       1.21  christos 	int c;
    156       1.21  christos 	int i;
    157       1.21  christos 	int msgCount;
    158       1.21  christos 	FILE *fp;
    159       1.21  christos 	struct message *mp;
    160       1.21  christos 	off_t size;
    161       1.21  christos 
    162       1.21  christos 	/*
    163       1.21  christos 	 * Deal with each message to be edited . . .
    164       1.21  christos 	 */
    165       1.21  christos 	msgCount = get_msgCount();
    166       1.21  christos 	for (i = 0; msgvec[i] && i < msgCount; i++) {
    167  1.24.14.1       jym 		sigset_t oset;
    168  1.24.14.1       jym 		struct sigaction osa;
    169       1.21  christos 
    170       1.21  christos 		if (i > 0) {
    171       1.21  christos 			char buf[100];
    172       1.21  christos 			char *p;
    173       1.21  christos 
    174       1.21  christos 			(void)printf("Edit message %d [ynq]? ", msgvec[i]);
    175  1.24.14.1       jym 			if (fgets(buf, (int)sizeof(buf), stdin) == 0)
    176       1.21  christos 				break;
    177       1.23  christos 			p = skip_WSP(buf);
    178       1.21  christos 			if (*p == 'q')
    179       1.21  christos 				break;
    180       1.21  christos 			if (*p == 'n')
    181       1.21  christos 				continue;
    182       1.21  christos 		}
    183       1.21  christos 		dot = mp = get_message(msgvec[i]);
    184       1.21  christos 		touch(mp);
    185  1.24.14.1       jym 		sig_check();
    186  1.24.14.1       jym 		(void)sig_ignore(SIGINT, &osa, &oset);
    187       1.21  christos 		fp = run_editor(setinput(mp), mp->m_size, editortype,
    188       1.21  christos 				readonly);
    189       1.21  christos 		if (fp != NULL) {
    190       1.21  christos 			(void)fseek(otf, 0L, 2);
    191       1.21  christos 			size = ftell(otf);
    192       1.21  christos 			mp->m_block = blockof(size);
    193       1.22    dogcow 			mp->m_offset = blkoffsetof(size);
    194       1.21  christos 			mp->m_size = fsize(fp);
    195       1.21  christos 			mp->m_lines = 0;
    196       1.21  christos 			mp->m_blines = 0;
    197       1.21  christos 			mp->m_flag |= MMODIFY;
    198       1.21  christos 			rewind(fp);
    199       1.21  christos 			while ((c = getc(fp)) != EOF) {
    200       1.21  christos 				/*
    201       1.21  christos 				 * XXX. if you edit a message, we treat
    202       1.21  christos 				 * header lines as body lines in the recount.
    203       1.21  christos 				 * This is because the original message copy
    204       1.21  christos 				 * and the edit reread use different code to
    205       1.21  christos 				 * count the lines, and this one here is
    206       1.21  christos 				 * simple-minded.
    207       1.21  christos 				 */
    208       1.21  christos 				if (c == '\n') {
    209       1.21  christos 					mp->m_lines++;
    210       1.21  christos 					mp->m_blines++;
    211       1.21  christos 				}
    212       1.21  christos 				if (putc(c, otf) == EOF)
    213       1.21  christos 					break;
    214       1.21  christos 			}
    215       1.21  christos 			if (ferror(otf))
    216       1.21  christos 				warn("/tmp");
    217       1.21  christos 			(void)Fclose(fp);
    218       1.21  christos 		}
    219  1.24.14.1       jym 		(void)sig_restore(SIGINT, &osa, &oset);
    220  1.24.14.1       jym 		sig_check();
    221       1.21  christos 	}
    222       1.21  christos 	return 0;
    223       1.21  christos }
    224       1.21  christos 
    225       1.21  christos /*
    226       1.21  christos  * Edit a message list.
    227       1.21  christos  */
    228       1.21  christos PUBLIC int
    229       1.21  christos editor(void *v)
    230       1.21  christos {
    231       1.21  christos 	int *msgvec = v;
    232       1.21  christos 
    233       1.21  christos 	return edit1(msgvec, 'e');
    234       1.21  christos }
    235       1.21  christos 
    236       1.21  christos /*
    237       1.21  christos  * Invoke the visual editor on a message list.
    238       1.21  christos  */
    239       1.21  christos PUBLIC int
    240       1.21  christos visual(void *v)
    241       1.21  christos {
    242       1.21  christos 	int *msgvec = v;
    243       1.21  christos 
    244       1.21  christos 	return edit1(msgvec, 'v');
    245       1.21  christos }
    246