1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright 1996 Sun Microsystems, Inc. All rights reserved.
24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */
29*7c478bd9Sstevel@tonic-gate
30*7c478bd9Sstevel@tonic-gate
31*7c478bd9Sstevel@tonic-gate /*
32*7c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
33*7c478bd9Sstevel@tonic-gate * The Regents of the University of California
34*7c478bd9Sstevel@tonic-gate * All Rights Reserved
35*7c478bd9Sstevel@tonic-gate *
36*7c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
37*7c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
38*7c478bd9Sstevel@tonic-gate * contributors.
39*7c478bd9Sstevel@tonic-gate */
40*7c478bd9Sstevel@tonic-gate
41*7c478bd9Sstevel@tonic-gate #include "rcv.h"
42*7c478bd9Sstevel@tonic-gate #include <locale.h>
43*7c478bd9Sstevel@tonic-gate
44*7c478bd9Sstevel@tonic-gate
45*7c478bd9Sstevel@tonic-gate /*
46*7c478bd9Sstevel@tonic-gate * mailx -- a modified version of a University of California at Berkeley
47*7c478bd9Sstevel@tonic-gate * mail program
48*7c478bd9Sstevel@tonic-gate *
49*7c478bd9Sstevel@tonic-gate * Perform message editing functions.
50*7c478bd9Sstevel@tonic-gate */
51*7c478bd9Sstevel@tonic-gate
52*7c478bd9Sstevel@tonic-gate static void edit1(int *msgvec, char *ed);
53*7c478bd9Sstevel@tonic-gate
54*7c478bd9Sstevel@tonic-gate /*
55*7c478bd9Sstevel@tonic-gate * Edit a message list.
56*7c478bd9Sstevel@tonic-gate */
57*7c478bd9Sstevel@tonic-gate
58*7c478bd9Sstevel@tonic-gate int
editor(int * msgvec)59*7c478bd9Sstevel@tonic-gate editor(int *msgvec)
60*7c478bd9Sstevel@tonic-gate {
61*7c478bd9Sstevel@tonic-gate char *edname;
62*7c478bd9Sstevel@tonic-gate
63*7c478bd9Sstevel@tonic-gate if ((edname = value("EDITOR")) == NOSTR || *edname == '\0')
64*7c478bd9Sstevel@tonic-gate edname = EDITOR;
65*7c478bd9Sstevel@tonic-gate edit1(msgvec, edname);
66*7c478bd9Sstevel@tonic-gate return(0);
67*7c478bd9Sstevel@tonic-gate }
68*7c478bd9Sstevel@tonic-gate
69*7c478bd9Sstevel@tonic-gate /*
70*7c478bd9Sstevel@tonic-gate * Invoke the visual editor on a message list.
71*7c478bd9Sstevel@tonic-gate */
72*7c478bd9Sstevel@tonic-gate
73*7c478bd9Sstevel@tonic-gate int
visual(int * msgvec)74*7c478bd9Sstevel@tonic-gate visual(int *msgvec)
75*7c478bd9Sstevel@tonic-gate {
76*7c478bd9Sstevel@tonic-gate char *edname;
77*7c478bd9Sstevel@tonic-gate
78*7c478bd9Sstevel@tonic-gate if ((edname = value("VISUAL")) == NOSTR || *edname == '\0')
79*7c478bd9Sstevel@tonic-gate edname = VISUAL;
80*7c478bd9Sstevel@tonic-gate edit1(msgvec, edname);
81*7c478bd9Sstevel@tonic-gate return(0);
82*7c478bd9Sstevel@tonic-gate }
83*7c478bd9Sstevel@tonic-gate
84*7c478bd9Sstevel@tonic-gate /*
85*7c478bd9Sstevel@tonic-gate * Edit a message by writing the message into a funnily-named file
86*7c478bd9Sstevel@tonic-gate * (which should not exist) and forking an editor on it.
87*7c478bd9Sstevel@tonic-gate * We get the editor from the stuff above.
88*7c478bd9Sstevel@tonic-gate */
89*7c478bd9Sstevel@tonic-gate
90*7c478bd9Sstevel@tonic-gate static void
edit1(int * msgvec,char * ed)91*7c478bd9Sstevel@tonic-gate edit1(int *msgvec, char *ed)
92*7c478bd9Sstevel@tonic-gate {
93*7c478bd9Sstevel@tonic-gate register int c, lastc = '\n';
94*7c478bd9Sstevel@tonic-gate pid_t pid;
95*7c478bd9Sstevel@tonic-gate int *ip, mesg, blank = 1;
96*7c478bd9Sstevel@tonic-gate long ms, lines;
97*7c478bd9Sstevel@tonic-gate void (*sigint)(int), (*sigquit)(int);
98*7c478bd9Sstevel@tonic-gate FILE *ibuf, *obuf;
99*7c478bd9Sstevel@tonic-gate struct message *mp;
100*7c478bd9Sstevel@tonic-gate off_t size;
101*7c478bd9Sstevel@tonic-gate struct stat statb;
102*7c478bd9Sstevel@tonic-gate long modtime;
103*7c478bd9Sstevel@tonic-gate int fd = -1;
104*7c478bd9Sstevel@tonic-gate
105*7c478bd9Sstevel@tonic-gate /*
106*7c478bd9Sstevel@tonic-gate * Set signals; locate editor.
107*7c478bd9Sstevel@tonic-gate */
108*7c478bd9Sstevel@tonic-gate
109*7c478bd9Sstevel@tonic-gate sigint = sigset(SIGINT, SIG_IGN);
110*7c478bd9Sstevel@tonic-gate sigquit = sigset(SIGQUIT, SIG_IGN);
111*7c478bd9Sstevel@tonic-gate ed = safeexpand(ed);
112*7c478bd9Sstevel@tonic-gate
113*7c478bd9Sstevel@tonic-gate /*
114*7c478bd9Sstevel@tonic-gate * Deal with each message to be edited . . .
115*7c478bd9Sstevel@tonic-gate */
116*7c478bd9Sstevel@tonic-gate
117*7c478bd9Sstevel@tonic-gate for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
118*7c478bd9Sstevel@tonic-gate mesg = *ip;
119*7c478bd9Sstevel@tonic-gate touch(mesg);
120*7c478bd9Sstevel@tonic-gate mp = &message[mesg-1];
121*7c478bd9Sstevel@tonic-gate dot = mp;
122*7c478bd9Sstevel@tonic-gate if (mp->m_text) {
123*7c478bd9Sstevel@tonic-gate if (!access(tempZedit, 2)) {
124*7c478bd9Sstevel@tonic-gate printf(gettext("%s: file exists\n"), tempZedit);
125*7c478bd9Sstevel@tonic-gate goto out;
126*7c478bd9Sstevel@tonic-gate }
127*7c478bd9Sstevel@tonic-gate
128*7c478bd9Sstevel@tonic-gate /*
129*7c478bd9Sstevel@tonic-gate * Copy the message into the edit file.
130*7c478bd9Sstevel@tonic-gate */
131*7c478bd9Sstevel@tonic-gate
132*7c478bd9Sstevel@tonic-gate if ((fd = open(tempZedit, O_RDWR|O_CREAT|
133*7c478bd9Sstevel@tonic-gate O_EXCL, 0600)) < 0 ||
134*7c478bd9Sstevel@tonic-gate (obuf = fdopen(fd, "w")) == NULL) {
135*7c478bd9Sstevel@tonic-gate perror(tempZedit);
136*7c478bd9Sstevel@tonic-gate goto out;
137*7c478bd9Sstevel@tonic-gate }
138*7c478bd9Sstevel@tonic-gate if (msend(mp, obuf, 0, fputs) < 0) {
139*7c478bd9Sstevel@tonic-gate perror(tempZedit);
140*7c478bd9Sstevel@tonic-gate fclose(obuf);
141*7c478bd9Sstevel@tonic-gate removefile(tempZedit);
142*7c478bd9Sstevel@tonic-gate goto out;
143*7c478bd9Sstevel@tonic-gate }
144*7c478bd9Sstevel@tonic-gate fflush(obuf);
145*7c478bd9Sstevel@tonic-gate if (fferror(obuf)) {
146*7c478bd9Sstevel@tonic-gate perror(tempZedit);
147*7c478bd9Sstevel@tonic-gate fclose(obuf);
148*7c478bd9Sstevel@tonic-gate removefile(tempZedit);
149*7c478bd9Sstevel@tonic-gate goto out;
150*7c478bd9Sstevel@tonic-gate }
151*7c478bd9Sstevel@tonic-gate fclose(obuf);
152*7c478bd9Sstevel@tonic-gate
153*7c478bd9Sstevel@tonic-gate /*
154*7c478bd9Sstevel@tonic-gate * If we are in read only mode, make the
155*7c478bd9Sstevel@tonic-gate * temporary message file readonly as well.
156*7c478bd9Sstevel@tonic-gate */
157*7c478bd9Sstevel@tonic-gate
158*7c478bd9Sstevel@tonic-gate if (readonly)
159*7c478bd9Sstevel@tonic-gate chmod(tempZedit, 0400);
160*7c478bd9Sstevel@tonic-gate
161*7c478bd9Sstevel@tonic-gate /*
162*7c478bd9Sstevel@tonic-gate * Fork/execl the editor on the edit file.
163*7c478bd9Sstevel@tonic-gate */
164*7c478bd9Sstevel@tonic-gate
165*7c478bd9Sstevel@tonic-gate if (stat(tempZedit, &statb) < 0)
166*7c478bd9Sstevel@tonic-gate modtime = 0;
167*7c478bd9Sstevel@tonic-gate else
168*7c478bd9Sstevel@tonic-gate modtime = statb.st_mtime;
169*7c478bd9Sstevel@tonic-gate pid = vfork();
170*7c478bd9Sstevel@tonic-gate if (pid == (pid_t)-1) {
171*7c478bd9Sstevel@tonic-gate perror("fork");
172*7c478bd9Sstevel@tonic-gate removefile(tempZedit);
173*7c478bd9Sstevel@tonic-gate goto out;
174*7c478bd9Sstevel@tonic-gate }
175*7c478bd9Sstevel@tonic-gate if (pid == 0) {
176*7c478bd9Sstevel@tonic-gate sigchild();
177*7c478bd9Sstevel@tonic-gate if (sigint != SIG_IGN)
178*7c478bd9Sstevel@tonic-gate sigset(SIGINT, SIG_DFL);
179*7c478bd9Sstevel@tonic-gate if (sigquit != SIG_IGN)
180*7c478bd9Sstevel@tonic-gate sigset(SIGQUIT, SIG_DFL);
181*7c478bd9Sstevel@tonic-gate execlp(ed, ed, tempZedit, (char *)0);
182*7c478bd9Sstevel@tonic-gate perror(ed);
183*7c478bd9Sstevel@tonic-gate _exit(1);
184*7c478bd9Sstevel@tonic-gate }
185*7c478bd9Sstevel@tonic-gate while (wait(&mesg) != pid)
186*7c478bd9Sstevel@tonic-gate ;
187*7c478bd9Sstevel@tonic-gate
188*7c478bd9Sstevel@tonic-gate /*
189*7c478bd9Sstevel@tonic-gate * If in read only mode, just remove the editor
190*7c478bd9Sstevel@tonic-gate * temporary and return.
191*7c478bd9Sstevel@tonic-gate */
192*7c478bd9Sstevel@tonic-gate
193*7c478bd9Sstevel@tonic-gate if (readonly) {
194*7c478bd9Sstevel@tonic-gate removefile(tempZedit);
195*7c478bd9Sstevel@tonic-gate continue;
196*7c478bd9Sstevel@tonic-gate }
197*7c478bd9Sstevel@tonic-gate
198*7c478bd9Sstevel@tonic-gate /*
199*7c478bd9Sstevel@tonic-gate * Now copy the message to the end of the
200*7c478bd9Sstevel@tonic-gate * temp file.
201*7c478bd9Sstevel@tonic-gate */
202*7c478bd9Sstevel@tonic-gate
203*7c478bd9Sstevel@tonic-gate if (stat(tempZedit, &statb) < 0) {
204*7c478bd9Sstevel@tonic-gate perror(tempZedit);
205*7c478bd9Sstevel@tonic-gate continue;
206*7c478bd9Sstevel@tonic-gate }
207*7c478bd9Sstevel@tonic-gate if (modtime == statb.st_mtime) {
208*7c478bd9Sstevel@tonic-gate removefile(tempZedit);
209*7c478bd9Sstevel@tonic-gate continue;
210*7c478bd9Sstevel@tonic-gate }
211*7c478bd9Sstevel@tonic-gate if ((ibuf = fopen(tempZedit, "r")) == NULL) {
212*7c478bd9Sstevel@tonic-gate perror(tempZedit);
213*7c478bd9Sstevel@tonic-gate removefile(tempZedit);
214*7c478bd9Sstevel@tonic-gate continue;
215*7c478bd9Sstevel@tonic-gate }
216*7c478bd9Sstevel@tonic-gate removefile(tempZedit);
217*7c478bd9Sstevel@tonic-gate fseek(otf, (long) 0, 2);
218*7c478bd9Sstevel@tonic-gate size = fsize(otf);
219*7c478bd9Sstevel@tonic-gate mp->m_flag |= MODIFY;
220*7c478bd9Sstevel@tonic-gate mp->m_offset = size;
221*7c478bd9Sstevel@tonic-gate ms = 0L;
222*7c478bd9Sstevel@tonic-gate lines = 0;
223*7c478bd9Sstevel@tonic-gate while ((c = getc(ibuf)) != EOF) {
224*7c478bd9Sstevel@tonic-gate if (c == '\n') {
225*7c478bd9Sstevel@tonic-gate lines++;
226*7c478bd9Sstevel@tonic-gate blank = lastc == '\n';
227*7c478bd9Sstevel@tonic-gate }
228*7c478bd9Sstevel@tonic-gate lastc = c;
229*7c478bd9Sstevel@tonic-gate putc(c, otf);
230*7c478bd9Sstevel@tonic-gate if (ferror(otf))
231*7c478bd9Sstevel@tonic-gate break;
232*7c478bd9Sstevel@tonic-gate ms++;
233*7c478bd9Sstevel@tonic-gate }
234*7c478bd9Sstevel@tonic-gate if (!blank) {
235*7c478bd9Sstevel@tonic-gate putc('\n', otf);
236*7c478bd9Sstevel@tonic-gate ms++;
237*7c478bd9Sstevel@tonic-gate lines++;
238*7c478bd9Sstevel@tonic-gate }
239*7c478bd9Sstevel@tonic-gate mp->m_size = ms;
240*7c478bd9Sstevel@tonic-gate mp->m_lines = lines;
241*7c478bd9Sstevel@tonic-gate fflush(otf);
242*7c478bd9Sstevel@tonic-gate if (fferror(otf))
243*7c478bd9Sstevel@tonic-gate perror("/tmp");
244*7c478bd9Sstevel@tonic-gate fclose(ibuf);
245*7c478bd9Sstevel@tonic-gate setclen(mp);
246*7c478bd9Sstevel@tonic-gate } else {
247*7c478bd9Sstevel@tonic-gate printf("\n%s\n", gettext(
248*7c478bd9Sstevel@tonic-gate "*** Message content is not printable: pipe to command or save to a file ***"));
249*7c478bd9Sstevel@tonic-gate }
250*7c478bd9Sstevel@tonic-gate }
251*7c478bd9Sstevel@tonic-gate
252*7c478bd9Sstevel@tonic-gate /*
253*7c478bd9Sstevel@tonic-gate * Restore signals and return.
254*7c478bd9Sstevel@tonic-gate */
255*7c478bd9Sstevel@tonic-gate
256*7c478bd9Sstevel@tonic-gate out:
257*7c478bd9Sstevel@tonic-gate sigset(SIGINT, sigint);
258*7c478bd9Sstevel@tonic-gate sigset(SIGQUIT, sigquit);
259*7c478bd9Sstevel@tonic-gate }
260