xref: /freebsd/usr.bin/mail/edit.c (revision bdcbfde31e8e9b343f113a1956384bdf30d1ed62)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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 #ifndef lint
33 #endif /* not lint */
34 #include <sys/cdefs.h>
35 #include "rcv.h"
36 #include <fcntl.h>
37 #include "extern.h"
38 
39 /*
40  * Mail -- a mail program
41  *
42  * Perform message editing functions.
43  */
44 
45 /*
46  * Edit a message list.
47  */
48 int
49 editor(void *msgvec)
50 {
51 
52 	return (edit1(msgvec, 'e'));
53 }
54 
55 /*
56  * Invoke the visual editor on a message list.
57  */
58 int
59 visual(void *msgvec)
60 {
61 
62 	return (edit1(msgvec, 'v'));
63 }
64 
65 /*
66  * Edit a message by writing the message into a funnily-named file
67  * (which should not exist) and forking an editor on it.
68  * We get the editor from the stuff above.
69  */
70 int
71 edit1(int *msgvec, int type)
72 {
73 	int c, i;
74 	FILE *fp;
75 	struct message *mp;
76 	off_t size;
77 
78 	/*
79 	 * Deal with each message to be edited . . .
80 	 */
81 	for (i = 0; i < msgCount && msgvec[i]; i++) {
82 		sig_t sigint;
83 
84 		if (i > 0) {
85 			char buf[100];
86 			char *p;
87 
88 			printf("Edit message %d [ynq]? ", msgvec[i]);
89 			if (fgets(buf, sizeof(buf), stdin) == NULL)
90 				break;
91 			for (p = buf; *p == ' ' || *p == '\t'; p++)
92 				;
93 			if (*p == 'q')
94 				break;
95 			if (*p == 'n')
96 				continue;
97 		}
98 		dot = mp = &message[msgvec[i] - 1];
99 		touch(mp);
100 		sigint = signal(SIGINT, SIG_IGN);
101 		fp = run_editor(setinput(mp), mp->m_size, type, readonly);
102 		if (fp != NULL) {
103 			(void)fseeko(otf, (off_t)0, SEEK_END);
104 			size = ftello(otf);
105 			mp->m_block = blockof(size);
106 			mp->m_offset = boffsetof(size);
107 			mp->m_size = (long)fsize(fp);
108 			mp->m_lines = 0;
109 			mp->m_flag |= MODIFY;
110 			rewind(fp);
111 			while ((c = getc(fp)) != EOF) {
112 				if (c == '\n')
113 					mp->m_lines++;
114 				if (putc(c, otf) == EOF)
115 					break;
116 			}
117 			if (ferror(otf))
118 				warnx("/tmp");
119 			(void)Fclose(fp);
120 		}
121 		(void)signal(SIGINT, sigint);
122 	}
123 	return (0);
124 }
125 
126 /*
127  * Run an editor on the file at "fpp" of "size" bytes,
128  * and return a new file pointer.
129  * Signals must be handled by the caller.
130  * "Type" is 'e' for _PATH_EX, 'v' for _PATH_VI.
131  */
132 FILE *
133 run_editor(FILE *fp, off_t size, int type, int readonly)
134 {
135 	FILE *nf = NULL;
136 	int t;
137 	time_t modtime;
138 	char *edit, tempname[PATHSIZE];
139 	struct stat statb;
140 
141 	(void)snprintf(tempname, sizeof(tempname),
142 	    "%s/mail.ReXXXXXXXXXX", tmpdir);
143 	if ((t = mkstemp(tempname)) == -1 ||
144 	    (nf = Fdopen(t, "w")) == NULL) {
145 		warn("%s", tempname);
146 		goto out;
147 	}
148 	if (readonly && fchmod(t, 0400) == -1) {
149 		warn("%s", tempname);
150 		(void)rm(tempname);
151 		goto out;
152 	}
153 	if (size >= 0)
154 		while (--size >= 0 && (t = getc(fp)) != EOF)
155 			(void)putc(t, nf);
156 	else
157 		while ((t = getc(fp)) != EOF)
158 			(void)putc(t, nf);
159 	(void)fflush(nf);
160 	if (fstat(fileno(nf), &statb) < 0)
161 		modtime = 0;
162 	else
163 		modtime = statb.st_mtime;
164 	if (ferror(nf)) {
165 		(void)Fclose(nf);
166 		warnx("%s", tempname);
167 		(void)rm(tempname);
168 		nf = NULL;
169 		goto out;
170 	}
171 	if (Fclose(nf) < 0) {
172 		warn("%s", tempname);
173 		(void)rm(tempname);
174 		nf = NULL;
175 		goto out;
176 	}
177 	nf = NULL;
178 	if ((edit = value(type == 'e' ? "EDITOR" : "VISUAL")) == NULL)
179 		edit = type == 'e' ? _PATH_EX : _PATH_VI;
180 	if (run_command(edit, 0, -1, -1, tempname, NULL) < 0) {
181 		(void)rm(tempname);
182 		goto out;
183 	}
184 	/*
185 	 * If in read only mode or file unchanged, just remove the editor
186 	 * temporary and return.
187 	 */
188 	if (readonly) {
189 		(void)rm(tempname);
190 		goto out;
191 	}
192 	if (stat(tempname, &statb) < 0) {
193 		warn("%s", tempname);
194 		goto out;
195 	}
196 	if (modtime == statb.st_mtime) {
197 		(void)rm(tempname);
198 		goto out;
199 	}
200 	/*
201 	 * Now switch to new file.
202 	 */
203 	if ((nf = Fopen(tempname, "a+")) == NULL) {
204 		warn("%s", tempname);
205 		(void)rm(tempname);
206 		goto out;
207 	}
208 	(void)rm(tempname);
209 out:
210 	return (nf);
211 }
212