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