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