xref: /illumos-gate/usr/src/cmd/mailx/cmd4.c (revision 55fea89d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 1995 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
337c478bd9Sstevel@tonic-gate  * The Regents of the University of California
347c478bd9Sstevel@tonic-gate  * All Rights Reserved
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
377c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
387c478bd9Sstevel@tonic-gate  * contributors.
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include "rcv.h"
427c478bd9Sstevel@tonic-gate #include <locale.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * mailx -- a modified version of a University of California at Berkeley
467c478bd9Sstevel@tonic-gate  *	mail program
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * More commands..
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static char *stripquotes(char *str);
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * pipe messages to cmd.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate 
57*55fea89dSDan Cross int
dopipe(char str[])587c478bd9Sstevel@tonic-gate dopipe(char str[])
597c478bd9Sstevel@tonic-gate {
6046d64c14SToomas Soome 	int *ip, mesg;
6146d64c14SToomas Soome 	struct message *mp;
627c478bd9Sstevel@tonic-gate 	char *cp, *cmd;
637c478bd9Sstevel@tonic-gate 	int f, *msgvec, nowait=0;
647c478bd9Sstevel@tonic-gate 	void (*sigint)(int), (*sigpipe)(int);
657c478bd9Sstevel@tonic-gate 	long lc, cc, t;
6646d64c14SToomas Soome 	pid_t pid;
677c478bd9Sstevel@tonic-gate 	int page, s, pivec[2];
687c478bd9Sstevel@tonic-gate 	char *Shell;
697c478bd9Sstevel@tonic-gate 	FILE *pio = NULL;
707c478bd9Sstevel@tonic-gate 	extern jmp_buf pipestop;
717c478bd9Sstevel@tonic-gate 	extern void brokpipe(int);
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	msgvec = (int *) salloc((msgCount + 2) * sizeof *msgvec);
747c478bd9Sstevel@tonic-gate 	if ((cmd = stripquotes(snarf(str, &f, 0))) == NOSTR) {
757c478bd9Sstevel@tonic-gate 		if (f == -1) {
767c478bd9Sstevel@tonic-gate 			printf(gettext("pipe command error\n"));
777c478bd9Sstevel@tonic-gate 			return(1);
787c478bd9Sstevel@tonic-gate 		}
797c478bd9Sstevel@tonic-gate 		if ( (cmd = value("cmd")) == NOSTR) {
807c478bd9Sstevel@tonic-gate 			printf(gettext("\"cmd\" not set, ignored.\n"));
817c478bd9Sstevel@tonic-gate 			return(1);
827c478bd9Sstevel@tonic-gate 		}
837c478bd9Sstevel@tonic-gate 	}
847c478bd9Sstevel@tonic-gate 	if (!f) {
857c478bd9Sstevel@tonic-gate 		*msgvec = first(0, MMNORM);
8646d64c14SToomas Soome 		if (*msgvec == 0) {
877c478bd9Sstevel@tonic-gate 			printf(gettext("No messages to pipe.\n"));
887c478bd9Sstevel@tonic-gate 			return(1);
897c478bd9Sstevel@tonic-gate 		}
9046d64c14SToomas Soome 		msgvec[1] = 0;
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate 	if (f && getmsglist(str, msgvec, 0) < 0)
937c478bd9Sstevel@tonic-gate 		return(1);
947c478bd9Sstevel@tonic-gate 	if (*(cp=cmd+strlen(cmd)-1)=='&') {
957c478bd9Sstevel@tonic-gate 		*cp=0;
967c478bd9Sstevel@tonic-gate 		nowait++;
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate 	printf(gettext("Pipe to: \"%s\"\n"), cmd);
997c478bd9Sstevel@tonic-gate 	flush();
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	if (setjmp(pipestop))
1027c478bd9Sstevel@tonic-gate 		goto err;
1037c478bd9Sstevel@tonic-gate 					/*  setup pipe */
1047c478bd9Sstevel@tonic-gate 	if (pipe(pivec) < 0) {
1057c478bd9Sstevel@tonic-gate 		perror("pipe");
1067c478bd9Sstevel@tonic-gate 		return(0);
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	if ((pid = vfork()) == 0) {
1107c478bd9Sstevel@tonic-gate 		close(pivec[1]);	/* child */
1117c478bd9Sstevel@tonic-gate 		close(0);
1127c478bd9Sstevel@tonic-gate 		dup(pivec[0]);
1137c478bd9Sstevel@tonic-gate 		close(pivec[0]);
1147c478bd9Sstevel@tonic-gate 		if ((Shell = value("SHELL")) == NOSTR || *Shell=='\0')
1157c478bd9Sstevel@tonic-gate 			Shell = SHELL;
1167c478bd9Sstevel@tonic-gate 		execlp(Shell, Shell, "-c", cmd, 0);
1177c478bd9Sstevel@tonic-gate 		perror(Shell);
1187c478bd9Sstevel@tonic-gate 		_exit(1);
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 	if (pid == (pid_t)-1) {		/* error */
1217c478bd9Sstevel@tonic-gate 		perror("fork");
1227c478bd9Sstevel@tonic-gate 		close(pivec[0]);
1237c478bd9Sstevel@tonic-gate 		close(pivec[1]);
1247c478bd9Sstevel@tonic-gate 		return(0);
1257c478bd9Sstevel@tonic-gate 	}
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	close(pivec[0]);		/* parent */
1287c478bd9Sstevel@tonic-gate 	pio=fdopen(pivec[1],"w");
1297c478bd9Sstevel@tonic-gate 	sigint = sigset(SIGINT, SIG_IGN);
1307c478bd9Sstevel@tonic-gate 	sigpipe = sigset(SIGPIPE, brokpipe);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 					/* send all messages to cmd */
1337c478bd9Sstevel@tonic-gate 	page = (value("page")!=NOSTR);
1347c478bd9Sstevel@tonic-gate 	lc = cc = 0;
1357c478bd9Sstevel@tonic-gate 	for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
1367c478bd9Sstevel@tonic-gate 		mesg = *ip;
1377c478bd9Sstevel@tonic-gate 		touch(mesg);
1387c478bd9Sstevel@tonic-gate 		mp = &message[mesg-1];
1397c478bd9Sstevel@tonic-gate 		dot = mp;
1407c478bd9Sstevel@tonic-gate 		if ((t = msend(mp, pio,
1417c478bd9Sstevel@tonic-gate 		    (value("alwaysignore") != NOSTR ||
1427c478bd9Sstevel@tonic-gate 		     value("pipeignore") != NOSTR)
1437c478bd9Sstevel@tonic-gate 		     ? M_IGNORE : 0, fputs)) < 0) {
1447c478bd9Sstevel@tonic-gate 			perror(cmd);
1457c478bd9Sstevel@tonic-gate 			sigset(SIGPIPE, sigpipe);
1467c478bd9Sstevel@tonic-gate 			sigset(SIGINT, sigint);
1477c478bd9Sstevel@tonic-gate 			fclose(pio);
1487c478bd9Sstevel@tonic-gate 			return(1);
1497c478bd9Sstevel@tonic-gate 		}
1507c478bd9Sstevel@tonic-gate 		lc += t;
1517c478bd9Sstevel@tonic-gate 		cc += mp->m_size;
1527c478bd9Sstevel@tonic-gate 		if (page) putc('\f', pio);
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	fflush(pio);
1567c478bd9Sstevel@tonic-gate 	if (ferror(pio))
1577c478bd9Sstevel@tonic-gate 	      perror(cmd);
1587c478bd9Sstevel@tonic-gate 	fclose(pio);
1597c478bd9Sstevel@tonic-gate 	pio = NULL;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 					/* wait */
1627c478bd9Sstevel@tonic-gate 	if (!nowait) {
1637c478bd9Sstevel@tonic-gate 		while (wait(&s) != pid);
1647c478bd9Sstevel@tonic-gate 		s &= 0377;
1657c478bd9Sstevel@tonic-gate 		if (s != 0)
1667c478bd9Sstevel@tonic-gate 			goto err;
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	printf("\"%s\" %ld/%ld\n", cmd, lc, cc);
1707c478bd9Sstevel@tonic-gate 	sigset(SIGPIPE, sigpipe);
1717c478bd9Sstevel@tonic-gate 	sigset(SIGINT, sigint);
1727c478bd9Sstevel@tonic-gate 	return(0);
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate err:
1757c478bd9Sstevel@tonic-gate 	printf(gettext("Pipe to \"%s\" failed\n"), cmd);
1767c478bd9Sstevel@tonic-gate 	if (pio)
1777c478bd9Sstevel@tonic-gate 		fclose(pio);
1787c478bd9Sstevel@tonic-gate 	sigset(SIGPIPE, sigpipe);
1797c478bd9Sstevel@tonic-gate 	sigset(SIGINT, sigint);
1807c478bd9Sstevel@tonic-gate 	return(0);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /*
1847c478bd9Sstevel@tonic-gate  * Load the named message from the named file.
1857c478bd9Sstevel@tonic-gate  */
186*55fea89dSDan Cross int
loadmsg(char str[])1877c478bd9Sstevel@tonic-gate loadmsg(char str[])
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate 	char *file;
1907c478bd9Sstevel@tonic-gate 	int f, *msgvec;
19146d64c14SToomas Soome 	int c, lastc = '\n';
1927c478bd9Sstevel@tonic-gate 	int blank;
1937c478bd9Sstevel@tonic-gate 	int lines;
1947c478bd9Sstevel@tonic-gate 	long ms;
1957c478bd9Sstevel@tonic-gate 	FILE *ibuf;
1967c478bd9Sstevel@tonic-gate 	struct message *mp;
1977c478bd9Sstevel@tonic-gate 	off_t size;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	msgvec = (int *) salloc((msgCount + 2) * sizeof *msgvec);
2007c478bd9Sstevel@tonic-gate 	if ((file = snarf(str, &f, 1)) == NOSTR)
2017c478bd9Sstevel@tonic-gate 		return(1);
2027c478bd9Sstevel@tonic-gate 	if (f==-1)
2037c478bd9Sstevel@tonic-gate 		return(1);
2047c478bd9Sstevel@tonic-gate 	if (!f) {
2057c478bd9Sstevel@tonic-gate 		*msgvec = first(0, MMNORM);
20646d64c14SToomas Soome 		if (*msgvec == 0) {
2077c478bd9Sstevel@tonic-gate 			printf(gettext("No message to load into.\n"));
2087c478bd9Sstevel@tonic-gate 			return(1);
2097c478bd9Sstevel@tonic-gate 		}
21046d64c14SToomas Soome 		msgvec[1] = 0;
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 	if (f && getmsglist(str, msgvec, 0) < 0)
2137c478bd9Sstevel@tonic-gate 		return(1);
21446d64c14SToomas Soome 	if (msgvec[1] != 0) {
2157c478bd9Sstevel@tonic-gate 		printf(gettext("Can only load into a single message.\n"));
2167c478bd9Sstevel@tonic-gate 		return(1);
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 	if ((file = expand(file)) == NOSTR)
2197c478bd9Sstevel@tonic-gate 		return(1);
2207c478bd9Sstevel@tonic-gate 	printf("\"%s\" ", file);
2217c478bd9Sstevel@tonic-gate 	fflush(stdout);
2227c478bd9Sstevel@tonic-gate 	if ((ibuf = fopen(file, "r")) == NULL) {
2237c478bd9Sstevel@tonic-gate 		perror("");
2247c478bd9Sstevel@tonic-gate 		return(1);
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 	mp = &message[*msgvec-1];
2277c478bd9Sstevel@tonic-gate 	dot = mp;
2287c478bd9Sstevel@tonic-gate 	mp->m_flag |= MODIFY;
2297c478bd9Sstevel@tonic-gate 	mp->m_flag &= ~MSAVED;		/* should probably turn off more */
2307c478bd9Sstevel@tonic-gate 	fseek(otf, (long) 0, 2);
2317c478bd9Sstevel@tonic-gate 	size = fsize(otf);
2327c478bd9Sstevel@tonic-gate 	mp->m_offset = size;
2337c478bd9Sstevel@tonic-gate 	ms = 0L;
2347c478bd9Sstevel@tonic-gate 	lines = 0;
2357c478bd9Sstevel@tonic-gate 	while ((c = getc(ibuf)) != EOF) {
2367c478bd9Sstevel@tonic-gate 		if (c == '\n') {
2377c478bd9Sstevel@tonic-gate 			lines++;
2387c478bd9Sstevel@tonic-gate 			blank = lastc == '\n';
2397c478bd9Sstevel@tonic-gate 		}
2407c478bd9Sstevel@tonic-gate 		lastc = c;
2417c478bd9Sstevel@tonic-gate 		putc(c, otf);
2427c478bd9Sstevel@tonic-gate 		if (ferror(otf))
2437c478bd9Sstevel@tonic-gate 			break;
2447c478bd9Sstevel@tonic-gate 		ms++;
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 	if (!blank) {
2477c478bd9Sstevel@tonic-gate 		putc('\n', otf);
2487c478bd9Sstevel@tonic-gate 		ms++;
2497c478bd9Sstevel@tonic-gate 		lines++;
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 	mp->m_size = ms;
2527c478bd9Sstevel@tonic-gate 	mp->m_lines = lines;
2537c478bd9Sstevel@tonic-gate 	if (fferror(otf))
2547c478bd9Sstevel@tonic-gate 		perror("/tmp");
2557c478bd9Sstevel@tonic-gate 	fclose(ibuf);
2567c478bd9Sstevel@tonic-gate 	setclen(mp);
2577c478bd9Sstevel@tonic-gate 	printf(gettext("[Loaded] %d/%ld\n"), lines, ms);
2587c478bd9Sstevel@tonic-gate 	return(0);
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate /*
2627c478bd9Sstevel@tonic-gate  * Display the named field.
2637c478bd9Sstevel@tonic-gate  */
264*55fea89dSDan Cross int
field(char str[])2657c478bd9Sstevel@tonic-gate field(char str[])
2667c478bd9Sstevel@tonic-gate {
26746d64c14SToomas Soome 	int *ip;
26846d64c14SToomas Soome 	struct message *mp;
26946d64c14SToomas Soome 	char *cp, *fld;
2707c478bd9Sstevel@tonic-gate 	int f, *msgvec;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	msgvec = (int *) salloc((msgCount + 2) * sizeof *msgvec);
2737c478bd9Sstevel@tonic-gate 	if ((fld = stripquotes(snarf(str, &f, 0))) == NOSTR) {
2747c478bd9Sstevel@tonic-gate 		if (f == -1)
2757c478bd9Sstevel@tonic-gate 			printf(gettext("Bad field\n"));
2767c478bd9Sstevel@tonic-gate 		else
2777c478bd9Sstevel@tonic-gate 			printf(gettext("No field specified\n"));
2787c478bd9Sstevel@tonic-gate 		return(1);
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 	if (!f) {
2817c478bd9Sstevel@tonic-gate 		*msgvec = first(0, MMNORM);
28246d64c14SToomas Soome 		if (*msgvec == 0) {
2837c478bd9Sstevel@tonic-gate 			printf(gettext("No messages to display.\n"));
2847c478bd9Sstevel@tonic-gate 			return(1);
2857c478bd9Sstevel@tonic-gate 		}
28646d64c14SToomas Soome 		msgvec[1] = 0;
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 	if (f && getmsglist(str, msgvec, 0) < 0)
2897c478bd9Sstevel@tonic-gate 		return(1);
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
2927c478bd9Sstevel@tonic-gate 		mp = &message[*ip - 1];
2937c478bd9Sstevel@tonic-gate 		dot = mp;
2947c478bd9Sstevel@tonic-gate 		if ((cp = hfield(fld, mp, addone)) != NULL)
2957c478bd9Sstevel@tonic-gate 			printf("%s\n", cp);
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 	return(0);
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate /*
3017c478bd9Sstevel@tonic-gate  *  Remove the quotes from around the string passed in (if any).  Return
3027c478bd9Sstevel@tonic-gate  *  the beginning of the result.
3037c478bd9Sstevel@tonic-gate  */
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate static char *
stripquotes(char * str)3067c478bd9Sstevel@tonic-gate stripquotes(char *str)
3077c478bd9Sstevel@tonic-gate {
30846d64c14SToomas Soome 	int lastch;
3097c478bd9Sstevel@tonic-gate 	if (str == NOSTR) {
3107c478bd9Sstevel@tonic-gate 		return(NOSTR);
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 	lastch = strlen(str)-1;
3137c478bd9Sstevel@tonic-gate 	if (any(*str, "\"'") && str[lastch] == *str) {
3147c478bd9Sstevel@tonic-gate 		str[lastch] = '\0';
3157c478bd9Sstevel@tonic-gate 		++str;
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 	return(str);
3187c478bd9Sstevel@tonic-gate }
319