Home | History | Annotate | Line # | Download | only in mail
edit.c revision 1.17
      1 /*	$NetBSD: edit.c,v 1.17 2003/07/14 20:49:14 christos 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. 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.
     22  *
     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
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)edit.c	8.1 (Berkeley) 6/6/93";
     40 #else
     41 __RCSID("$NetBSD: edit.c,v 1.17 2003/07/14 20:49:14 christos Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include "rcv.h"
     46 #include "extern.h"
     47 
     48 /*
     49  * Mail -- a mail program
     50  *
     51  * Perform message editing functions.
     52  */
     53 extern char *tmpdir;
     54 
     55 /*
     56  * Edit a message list.
     57  */
     58 int
     59 editor(void *v)
     60 {
     61 	int *msgvec = v;
     62 
     63 	return edit1(msgvec, 'e');
     64 }
     65 
     66 /*
     67  * Invoke the visual editor on a message list.
     68  */
     69 int
     70 visual(void *v)
     71 {
     72 	int *msgvec = v;
     73 
     74 	return edit1(msgvec, 'v');
     75 }
     76 
     77 /*
     78  * Edit a message by writing the message into a funnily-named file
     79  * (which should not exist) and forking an editor on it.
     80  * We get the editor from the stuff above.
     81  */
     82 int
     83 edit1(int *msgvec, int editortype)
     84 {
     85 	int c;
     86 	int i;
     87 	FILE *fp;
     88 	struct message *mp;
     89 	off_t size;
     90 
     91 	/*
     92 	 * Deal with each message to be edited . . .
     93 	 */
     94 	for (i = 0; msgvec[i] && i < msgCount; i++) {
     95 		sig_t sigint;
     96 
     97 		if (i > 0) {
     98 			char buf[100];
     99 			char *p;
    100 
    101 			printf("Edit message %d [ynq]? ", msgvec[i]);
    102 			if (fgets(buf, sizeof buf, stdin) == 0)
    103 				break;
    104 			for (p = buf; *p == ' ' || *p == '\t'; p++)
    105 				;
    106 			if (*p == 'q')
    107 				break;
    108 			if (*p == 'n')
    109 				continue;
    110 		}
    111 		dot = mp = &message[msgvec[i] - 1];
    112 		touch(mp);
    113 		sigint = signal(SIGINT, SIG_IGN);
    114 		fp = run_editor(setinput(mp), mp->m_size, editortype,
    115 				readonly);
    116 		if (fp != NULL) {
    117 			(void)fseek(otf, 0L, 2);
    118 			size = ftell(otf);
    119 			mp->m_block = blockof(size);
    120 			mp->m_offset = offsetof(size);
    121 			mp->m_size = fsize(fp);
    122 			mp->m_lines = 0;
    123 			mp->m_blines = 0;
    124 			mp->m_flag |= MODIFY;
    125 			rewind(fp);
    126 			while ((c = getc(fp)) != EOF) {
    127 				//
    128 				// XXX. if you edit a message, we treat
    129 				// header lines as body lines in the recount.
    130 				// This is because the original message copy
    131 				// and the edit reread use different code to
    132 				// count the lines, and this one here is
    133 				// simple-minded.
    134 				//
    135 				if (c == '\n') {
    136 					mp->m_lines++;
    137 					mp->m_blines++;
    138 				}
    139 				if (putc(c, otf) == EOF)
    140 					break;
    141 			}
    142 			if (ferror(otf))
    143 				warn("/tmp");
    144 			(void)Fclose(fp);
    145 		}
    146 		(void)signal(SIGINT, sigint);
    147 	}
    148 	return 0;
    149 }
    150 
    151 /*
    152  * Run an editor on the file at "fpp" of "size" bytes,
    153  * and return a new file pointer.
    154  * Signals must be handled by the caller.
    155  * "Editortype" is 'e' for _PATH_EX, 'v' for _PATH_VI.
    156  */
    157 FILE *
    158 run_editor(FILE *fp, off_t size, int editortype, int readonlyflag)
    159 {
    160 	FILE *nf = NULL;
    161 	int t;
    162 	time_t modtime;
    163 	char *editcmd;
    164 	char tempname[PATHSIZE];
    165 	struct stat statb;
    166 
    167 	(void)snprintf(tempname, sizeof(tempname),
    168 	    "%s/mail.ReXXXXXXXXXX", tmpdir);
    169 	if ((t = mkstemp(tempname)) == -1) {
    170 		warn("%s", tempname);
    171 		goto out;
    172 	}
    173 	if (readonlyflag && fchmod(t, 0400) == -1) {
    174 		close(t);
    175 		warn("%s", tempname);
    176 		(void)unlink(tempname);
    177 		goto out;
    178 	}
    179 	if ((nf = Fdopen(t, "w")) == NULL) {
    180 		close(t);
    181 		warn("%s", tempname);
    182 		(void)unlink(tempname);
    183 		goto out;
    184 	}
    185 	if (size >= 0)
    186 		while (--size >= 0 && (t = getc(fp)) != EOF)
    187 			(void)putc(t, nf);
    188 	else
    189 		while ((t = getc(fp)) != EOF)
    190 			(void)putc(t, nf);
    191 	(void)fflush(nf);
    192 	if (ferror(nf)) {
    193 		(void)Fclose(nf);
    194 		warn("%s", tempname);
    195 		(void)unlink(tempname);
    196 		nf = NULL;
    197 		goto out;
    198 	}
    199 	if (fstat(fileno(nf), &statb) < 0)
    200 		modtime = 0;
    201 	else
    202 		modtime = statb.st_mtime;
    203 	if (Fclose(nf) < 0) {
    204 		warn("%s", tempname);
    205 		(void)unlink(tempname);
    206 		nf = NULL;
    207 		goto out;
    208 	}
    209 	nf = NULL;
    210 	if ((editcmd =
    211 	         value(editortype == 'e' ? "EDITOR" : "VISUAL")) == NULL)
    212 		editcmd = editortype == 'e' ? _PATH_EX : _PATH_VI;
    213 	if (run_command(editcmd, 0, 0, -1, tempname, NULL) < 0) {
    214 		(void)unlink(tempname);
    215 		goto out;
    216 	}
    217 	/*
    218 	 * If in read only mode or file unchanged, just remove the editor
    219 	 * temporary and return.
    220 	 */
    221 	if (readonlyflag) {
    222 		(void)unlink(tempname);
    223 		goto out;
    224 	}
    225 	if (stat(tempname, &statb) < 0) {
    226 		warn("%s", tempname);
    227 		goto out;
    228 	}
    229 	if (modtime == statb.st_mtime) {
    230 		(void)unlink(tempname);
    231 		goto out;
    232 	}
    233 	/*
    234 	 * Now switch to new file.
    235 	 */
    236 	if ((nf = Fopen(tempname, "a+")) == NULL) {
    237 		warn("%s", tempname);
    238 		(void)unlink(tempname);
    239 		goto out;
    240 	}
    241 	(void)unlink(tempname);
    242 out:
    243 	return nf;
    244 }
    245