1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 1995 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*0Sstevel@tonic-gate /* All Rights Reserved */
29*0Sstevel@tonic-gate
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gate /*
32*0Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
33*0Sstevel@tonic-gate * The Regents of the University of California
34*0Sstevel@tonic-gate * All Rights Reserved
35*0Sstevel@tonic-gate *
36*0Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
37*0Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
38*0Sstevel@tonic-gate * contributors.
39*0Sstevel@tonic-gate */
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gate #include "rcv.h"
44*0Sstevel@tonic-gate #include <locale.h>
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate /*
47*0Sstevel@tonic-gate * mailx -- a modified version of a University of California at Berkeley
48*0Sstevel@tonic-gate * mail program
49*0Sstevel@tonic-gate *
50*0Sstevel@tonic-gate * More commands..
51*0Sstevel@tonic-gate */
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gate static char *stripquotes(char *str);
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate /*
56*0Sstevel@tonic-gate * pipe messages to cmd.
57*0Sstevel@tonic-gate */
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gate int
dopipe(char str[])60*0Sstevel@tonic-gate dopipe(char str[])
61*0Sstevel@tonic-gate {
62*0Sstevel@tonic-gate register int *ip, mesg;
63*0Sstevel@tonic-gate register struct message *mp;
64*0Sstevel@tonic-gate char *cp, *cmd;
65*0Sstevel@tonic-gate int f, *msgvec, nowait=0;
66*0Sstevel@tonic-gate void (*sigint)(int), (*sigpipe)(int);
67*0Sstevel@tonic-gate long lc, cc, t;
68*0Sstevel@tonic-gate register pid_t pid;
69*0Sstevel@tonic-gate int page, s, pivec[2];
70*0Sstevel@tonic-gate char *Shell;
71*0Sstevel@tonic-gate FILE *pio = NULL;
72*0Sstevel@tonic-gate extern jmp_buf pipestop;
73*0Sstevel@tonic-gate extern void brokpipe(int);
74*0Sstevel@tonic-gate
75*0Sstevel@tonic-gate msgvec = (int *) salloc((msgCount + 2) * sizeof *msgvec);
76*0Sstevel@tonic-gate if ((cmd = stripquotes(snarf(str, &f, 0))) == NOSTR) {
77*0Sstevel@tonic-gate if (f == -1) {
78*0Sstevel@tonic-gate printf(gettext("pipe command error\n"));
79*0Sstevel@tonic-gate return(1);
80*0Sstevel@tonic-gate }
81*0Sstevel@tonic-gate if ( (cmd = value("cmd")) == NOSTR) {
82*0Sstevel@tonic-gate printf(gettext("\"cmd\" not set, ignored.\n"));
83*0Sstevel@tonic-gate return(1);
84*0Sstevel@tonic-gate }
85*0Sstevel@tonic-gate }
86*0Sstevel@tonic-gate if (!f) {
87*0Sstevel@tonic-gate *msgvec = first(0, MMNORM);
88*0Sstevel@tonic-gate if (*msgvec == NULL) {
89*0Sstevel@tonic-gate printf(gettext("No messages to pipe.\n"));
90*0Sstevel@tonic-gate return(1);
91*0Sstevel@tonic-gate }
92*0Sstevel@tonic-gate msgvec[1] = NULL;
93*0Sstevel@tonic-gate }
94*0Sstevel@tonic-gate if (f && getmsglist(str, msgvec, 0) < 0)
95*0Sstevel@tonic-gate return(1);
96*0Sstevel@tonic-gate if (*(cp=cmd+strlen(cmd)-1)=='&') {
97*0Sstevel@tonic-gate *cp=0;
98*0Sstevel@tonic-gate nowait++;
99*0Sstevel@tonic-gate }
100*0Sstevel@tonic-gate printf(gettext("Pipe to: \"%s\"\n"), cmd);
101*0Sstevel@tonic-gate flush();
102*0Sstevel@tonic-gate
103*0Sstevel@tonic-gate if (setjmp(pipestop))
104*0Sstevel@tonic-gate goto err;
105*0Sstevel@tonic-gate /* setup pipe */
106*0Sstevel@tonic-gate if (pipe(pivec) < 0) {
107*0Sstevel@tonic-gate perror("pipe");
108*0Sstevel@tonic-gate return(0);
109*0Sstevel@tonic-gate }
110*0Sstevel@tonic-gate
111*0Sstevel@tonic-gate if ((pid = vfork()) == 0) {
112*0Sstevel@tonic-gate close(pivec[1]); /* child */
113*0Sstevel@tonic-gate close(0);
114*0Sstevel@tonic-gate dup(pivec[0]);
115*0Sstevel@tonic-gate close(pivec[0]);
116*0Sstevel@tonic-gate if ((Shell = value("SHELL")) == NOSTR || *Shell=='\0')
117*0Sstevel@tonic-gate Shell = SHELL;
118*0Sstevel@tonic-gate execlp(Shell, Shell, "-c", cmd, 0);
119*0Sstevel@tonic-gate perror(Shell);
120*0Sstevel@tonic-gate _exit(1);
121*0Sstevel@tonic-gate }
122*0Sstevel@tonic-gate if (pid == (pid_t)-1) { /* error */
123*0Sstevel@tonic-gate perror("fork");
124*0Sstevel@tonic-gate close(pivec[0]);
125*0Sstevel@tonic-gate close(pivec[1]);
126*0Sstevel@tonic-gate return(0);
127*0Sstevel@tonic-gate }
128*0Sstevel@tonic-gate
129*0Sstevel@tonic-gate close(pivec[0]); /* parent */
130*0Sstevel@tonic-gate pio=fdopen(pivec[1],"w");
131*0Sstevel@tonic-gate sigint = sigset(SIGINT, SIG_IGN);
132*0Sstevel@tonic-gate sigpipe = sigset(SIGPIPE, brokpipe);
133*0Sstevel@tonic-gate
134*0Sstevel@tonic-gate /* send all messages to cmd */
135*0Sstevel@tonic-gate page = (value("page")!=NOSTR);
136*0Sstevel@tonic-gate lc = cc = 0;
137*0Sstevel@tonic-gate for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
138*0Sstevel@tonic-gate mesg = *ip;
139*0Sstevel@tonic-gate touch(mesg);
140*0Sstevel@tonic-gate mp = &message[mesg-1];
141*0Sstevel@tonic-gate dot = mp;
142*0Sstevel@tonic-gate if ((t = msend(mp, pio,
143*0Sstevel@tonic-gate (value("alwaysignore") != NOSTR ||
144*0Sstevel@tonic-gate value("pipeignore") != NOSTR)
145*0Sstevel@tonic-gate ? M_IGNORE : 0, fputs)) < 0) {
146*0Sstevel@tonic-gate perror(cmd);
147*0Sstevel@tonic-gate sigset(SIGPIPE, sigpipe);
148*0Sstevel@tonic-gate sigset(SIGINT, sigint);
149*0Sstevel@tonic-gate fclose(pio);
150*0Sstevel@tonic-gate return(1);
151*0Sstevel@tonic-gate }
152*0Sstevel@tonic-gate lc += t;
153*0Sstevel@tonic-gate cc += mp->m_size;
154*0Sstevel@tonic-gate if (page) putc('\f', pio);
155*0Sstevel@tonic-gate }
156*0Sstevel@tonic-gate
157*0Sstevel@tonic-gate fflush(pio);
158*0Sstevel@tonic-gate if (ferror(pio))
159*0Sstevel@tonic-gate perror(cmd);
160*0Sstevel@tonic-gate fclose(pio);
161*0Sstevel@tonic-gate pio = NULL;
162*0Sstevel@tonic-gate
163*0Sstevel@tonic-gate /* wait */
164*0Sstevel@tonic-gate if (!nowait) {
165*0Sstevel@tonic-gate while (wait(&s) != pid);
166*0Sstevel@tonic-gate s &= 0377;
167*0Sstevel@tonic-gate if (s != 0)
168*0Sstevel@tonic-gate goto err;
169*0Sstevel@tonic-gate }
170*0Sstevel@tonic-gate
171*0Sstevel@tonic-gate printf("\"%s\" %ld/%ld\n", cmd, lc, cc);
172*0Sstevel@tonic-gate sigset(SIGPIPE, sigpipe);
173*0Sstevel@tonic-gate sigset(SIGINT, sigint);
174*0Sstevel@tonic-gate return(0);
175*0Sstevel@tonic-gate
176*0Sstevel@tonic-gate err:
177*0Sstevel@tonic-gate printf(gettext("Pipe to \"%s\" failed\n"), cmd);
178*0Sstevel@tonic-gate if (pio)
179*0Sstevel@tonic-gate fclose(pio);
180*0Sstevel@tonic-gate sigset(SIGPIPE, sigpipe);
181*0Sstevel@tonic-gate sigset(SIGINT, sigint);
182*0Sstevel@tonic-gate return(0);
183*0Sstevel@tonic-gate }
184*0Sstevel@tonic-gate
185*0Sstevel@tonic-gate /*
186*0Sstevel@tonic-gate * Load the named message from the named file.
187*0Sstevel@tonic-gate */
188*0Sstevel@tonic-gate int
loadmsg(char str[])189*0Sstevel@tonic-gate loadmsg(char str[])
190*0Sstevel@tonic-gate {
191*0Sstevel@tonic-gate char *file;
192*0Sstevel@tonic-gate int f, *msgvec;
193*0Sstevel@tonic-gate register int c, lastc = '\n';
194*0Sstevel@tonic-gate int blank;
195*0Sstevel@tonic-gate int lines;
196*0Sstevel@tonic-gate long ms;
197*0Sstevel@tonic-gate FILE *ibuf;
198*0Sstevel@tonic-gate struct message *mp;
199*0Sstevel@tonic-gate off_t size;
200*0Sstevel@tonic-gate
201*0Sstevel@tonic-gate msgvec = (int *) salloc((msgCount + 2) * sizeof *msgvec);
202*0Sstevel@tonic-gate if ((file = snarf(str, &f, 1)) == NOSTR)
203*0Sstevel@tonic-gate return(1);
204*0Sstevel@tonic-gate if (f==-1)
205*0Sstevel@tonic-gate return(1);
206*0Sstevel@tonic-gate if (!f) {
207*0Sstevel@tonic-gate *msgvec = first(0, MMNORM);
208*0Sstevel@tonic-gate if (*msgvec == NULL) {
209*0Sstevel@tonic-gate printf(gettext("No message to load into.\n"));
210*0Sstevel@tonic-gate return(1);
211*0Sstevel@tonic-gate }
212*0Sstevel@tonic-gate msgvec[1] = NULL;
213*0Sstevel@tonic-gate }
214*0Sstevel@tonic-gate if (f && getmsglist(str, msgvec, 0) < 0)
215*0Sstevel@tonic-gate return(1);
216*0Sstevel@tonic-gate if (msgvec[1] != NULL) {
217*0Sstevel@tonic-gate printf(gettext("Can only load into a single message.\n"));
218*0Sstevel@tonic-gate return(1);
219*0Sstevel@tonic-gate }
220*0Sstevel@tonic-gate if ((file = expand(file)) == NOSTR)
221*0Sstevel@tonic-gate return(1);
222*0Sstevel@tonic-gate printf("\"%s\" ", file);
223*0Sstevel@tonic-gate fflush(stdout);
224*0Sstevel@tonic-gate if ((ibuf = fopen(file, "r")) == NULL) {
225*0Sstevel@tonic-gate perror("");
226*0Sstevel@tonic-gate return(1);
227*0Sstevel@tonic-gate }
228*0Sstevel@tonic-gate mp = &message[*msgvec-1];
229*0Sstevel@tonic-gate dot = mp;
230*0Sstevel@tonic-gate mp->m_flag |= MODIFY;
231*0Sstevel@tonic-gate mp->m_flag &= ~MSAVED; /* should probably turn off more */
232*0Sstevel@tonic-gate fseek(otf, (long) 0, 2);
233*0Sstevel@tonic-gate size = fsize(otf);
234*0Sstevel@tonic-gate mp->m_offset = size;
235*0Sstevel@tonic-gate ms = 0L;
236*0Sstevel@tonic-gate lines = 0;
237*0Sstevel@tonic-gate while ((c = getc(ibuf)) != EOF) {
238*0Sstevel@tonic-gate if (c == '\n') {
239*0Sstevel@tonic-gate lines++;
240*0Sstevel@tonic-gate blank = lastc == '\n';
241*0Sstevel@tonic-gate }
242*0Sstevel@tonic-gate lastc = c;
243*0Sstevel@tonic-gate putc(c, otf);
244*0Sstevel@tonic-gate if (ferror(otf))
245*0Sstevel@tonic-gate break;
246*0Sstevel@tonic-gate ms++;
247*0Sstevel@tonic-gate }
248*0Sstevel@tonic-gate if (!blank) {
249*0Sstevel@tonic-gate putc('\n', otf);
250*0Sstevel@tonic-gate ms++;
251*0Sstevel@tonic-gate lines++;
252*0Sstevel@tonic-gate }
253*0Sstevel@tonic-gate mp->m_size = ms;
254*0Sstevel@tonic-gate mp->m_lines = lines;
255*0Sstevel@tonic-gate if (fferror(otf))
256*0Sstevel@tonic-gate perror("/tmp");
257*0Sstevel@tonic-gate fclose(ibuf);
258*0Sstevel@tonic-gate setclen(mp);
259*0Sstevel@tonic-gate printf(gettext("[Loaded] %d/%ld\n"), lines, ms);
260*0Sstevel@tonic-gate return(0);
261*0Sstevel@tonic-gate }
262*0Sstevel@tonic-gate
263*0Sstevel@tonic-gate /*
264*0Sstevel@tonic-gate * Display the named field.
265*0Sstevel@tonic-gate */
266*0Sstevel@tonic-gate int
field(char str[])267*0Sstevel@tonic-gate field(char str[])
268*0Sstevel@tonic-gate {
269*0Sstevel@tonic-gate register int *ip;
270*0Sstevel@tonic-gate register struct message *mp;
271*0Sstevel@tonic-gate register char *cp, *fld;
272*0Sstevel@tonic-gate int f, *msgvec;
273*0Sstevel@tonic-gate
274*0Sstevel@tonic-gate msgvec = (int *) salloc((msgCount + 2) * sizeof *msgvec);
275*0Sstevel@tonic-gate if ((fld = stripquotes(snarf(str, &f, 0))) == NOSTR) {
276*0Sstevel@tonic-gate if (f == -1)
277*0Sstevel@tonic-gate printf(gettext("Bad field\n"));
278*0Sstevel@tonic-gate else
279*0Sstevel@tonic-gate printf(gettext("No field specified\n"));
280*0Sstevel@tonic-gate return(1);
281*0Sstevel@tonic-gate }
282*0Sstevel@tonic-gate if (!f) {
283*0Sstevel@tonic-gate *msgvec = first(0, MMNORM);
284*0Sstevel@tonic-gate if (*msgvec == NULL) {
285*0Sstevel@tonic-gate printf(gettext("No messages to display.\n"));
286*0Sstevel@tonic-gate return(1);
287*0Sstevel@tonic-gate }
288*0Sstevel@tonic-gate msgvec[1] = NULL;
289*0Sstevel@tonic-gate }
290*0Sstevel@tonic-gate if (f && getmsglist(str, msgvec, 0) < 0)
291*0Sstevel@tonic-gate return(1);
292*0Sstevel@tonic-gate
293*0Sstevel@tonic-gate for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
294*0Sstevel@tonic-gate mp = &message[*ip - 1];
295*0Sstevel@tonic-gate dot = mp;
296*0Sstevel@tonic-gate if ((cp = hfield(fld, mp, addone)) != NULL)
297*0Sstevel@tonic-gate printf("%s\n", cp);
298*0Sstevel@tonic-gate }
299*0Sstevel@tonic-gate return(0);
300*0Sstevel@tonic-gate }
301*0Sstevel@tonic-gate
302*0Sstevel@tonic-gate /*
303*0Sstevel@tonic-gate * Remove the quotes from around the string passed in (if any). Return
304*0Sstevel@tonic-gate * the beginning of the result.
305*0Sstevel@tonic-gate */
306*0Sstevel@tonic-gate
307*0Sstevel@tonic-gate static char *
stripquotes(char * str)308*0Sstevel@tonic-gate stripquotes(char *str)
309*0Sstevel@tonic-gate {
310*0Sstevel@tonic-gate register int lastch;
311*0Sstevel@tonic-gate if (str == NOSTR) {
312*0Sstevel@tonic-gate return(NOSTR);
313*0Sstevel@tonic-gate }
314*0Sstevel@tonic-gate lastch = strlen(str)-1;
315*0Sstevel@tonic-gate if (any(*str, "\"'") && str[lastch] == *str) {
316*0Sstevel@tonic-gate str[lastch] = '\0';
317*0Sstevel@tonic-gate ++str;
318*0Sstevel@tonic-gate }
319*0Sstevel@tonic-gate return(str);
320*0Sstevel@tonic-gate }
321