xref: /titanic_50/usr/src/cmd/mailx/cmd1.c (revision 6c83d09f819e9de7126c8539546eca2e276df44a)
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  */
22*6c83d09fSrobbin /*
23*6c83d09fSrobbin  * Copyright 2001 Sun Microsystems, Inc.  All rights reserved.
24*6c83d09fSrobbin  * Use is subject to license terms.
25*6c83d09fSrobbin  */
26*6c83d09fSrobbin 
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  * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate  * The Regents of the University of California
337c478bd9Sstevel@tonic-gate  * All Rights Reserved
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate  * contributors.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include "rcv.h"
437c478bd9Sstevel@tonic-gate #include <locale.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * mailx -- a modified version of a University of California at Berkeley
477c478bd9Sstevel@tonic-gate  *	mail program
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  * User commands.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static char	*dispname(char *hdr);
537c478bd9Sstevel@tonic-gate static void	print(register struct message *mp, FILE *obuf, int doign);
547c478bd9Sstevel@tonic-gate static int	type1(int *msgvec, int doign, int page);
557c478bd9Sstevel@tonic-gate static int	topputs(const char *line, FILE *obuf);
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate void	brokpipe(int sig);
587c478bd9Sstevel@tonic-gate jmp_buf	pipestop;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * Print the current active headings.
627c478bd9Sstevel@tonic-gate  * Don't change dot if invoker didn't give an argument.
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static int curscreen = 0, oldscreensize = 0;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate int
687c478bd9Sstevel@tonic-gate headers(int *msgvec)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 	register int n, mesg, flag;
717c478bd9Sstevel@tonic-gate 	register struct message *mp;
727c478bd9Sstevel@tonic-gate 	int size;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	size = screensize();
757c478bd9Sstevel@tonic-gate 	n = msgvec[0];
767c478bd9Sstevel@tonic-gate 	if (n != 0)
777c478bd9Sstevel@tonic-gate 		curscreen = (n-1)/size;
787c478bd9Sstevel@tonic-gate 	if (curscreen < 0)
797c478bd9Sstevel@tonic-gate 		curscreen = 0;
807c478bd9Sstevel@tonic-gate 	mp = &message[curscreen * size];
817c478bd9Sstevel@tonic-gate 	if (mp >= &message[msgCount])
827c478bd9Sstevel@tonic-gate 		mp = &message[msgCount - size];
837c478bd9Sstevel@tonic-gate 	if (mp < &message[0])
847c478bd9Sstevel@tonic-gate 		mp = &message[0];
857c478bd9Sstevel@tonic-gate 	flag = 0;
867c478bd9Sstevel@tonic-gate 	mesg = mp - &message[0];
877c478bd9Sstevel@tonic-gate 	if (dot != &message[n-1])
887c478bd9Sstevel@tonic-gate 		dot = mp;
897c478bd9Sstevel@tonic-gate 	if (Hflag)
907c478bd9Sstevel@tonic-gate 		mp = message;
917c478bd9Sstevel@tonic-gate 	for (; mp < &message[msgCount]; mp++) {
927c478bd9Sstevel@tonic-gate 		mesg++;
937c478bd9Sstevel@tonic-gate 		if (mp->m_flag & MDELETED)
947c478bd9Sstevel@tonic-gate 			continue;
957c478bd9Sstevel@tonic-gate 		if (flag++ >= size && !Hflag)
967c478bd9Sstevel@tonic-gate 			break;
977c478bd9Sstevel@tonic-gate 		printhead(mesg);
987c478bd9Sstevel@tonic-gate 		sreset();
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 	if (flag == 0) {
1017c478bd9Sstevel@tonic-gate 		printf(gettext("No more mail.\n"));
1027c478bd9Sstevel@tonic-gate 		return (1);
1037c478bd9Sstevel@tonic-gate 	}
1047c478bd9Sstevel@tonic-gate 	return (0);
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  * Scroll to the next/previous screen
1097c478bd9Sstevel@tonic-gate  */
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate int
1127c478bd9Sstevel@tonic-gate scroll(char arg[])
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate 	register int s, size;
1157c478bd9Sstevel@tonic-gate 	int cur[1];
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	cur[0] = 0;
1187c478bd9Sstevel@tonic-gate 	size = screensize();
1197c478bd9Sstevel@tonic-gate 	s = curscreen;
1207c478bd9Sstevel@tonic-gate 	switch (*arg) {
1217c478bd9Sstevel@tonic-gate 	case 0:
1227c478bd9Sstevel@tonic-gate 	case '+':
1237c478bd9Sstevel@tonic-gate 		s++;
1247c478bd9Sstevel@tonic-gate 		if (s * size > msgCount) {
1257c478bd9Sstevel@tonic-gate 			printf(gettext("On last screenful of messages\n"));
1267c478bd9Sstevel@tonic-gate 			return (0);
1277c478bd9Sstevel@tonic-gate 		}
1287c478bd9Sstevel@tonic-gate 		curscreen = s;
1297c478bd9Sstevel@tonic-gate 		break;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	case '-':
1327c478bd9Sstevel@tonic-gate 		if (--s < 0) {
1337c478bd9Sstevel@tonic-gate 			printf(gettext("On first screenful of messages\n"));
1347c478bd9Sstevel@tonic-gate 			return (0);
1357c478bd9Sstevel@tonic-gate 		}
1367c478bd9Sstevel@tonic-gate 		curscreen = s;
1377c478bd9Sstevel@tonic-gate 		break;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	default:
1407c478bd9Sstevel@tonic-gate 		printf(gettext("Unrecognized scrolling command \"%s\"\n"), arg);
1417c478bd9Sstevel@tonic-gate 		return (1);
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 	return (headers(cur));
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate  * Compute what the screen size should be.
1487c478bd9Sstevel@tonic-gate  * We use the following algorithm:
1497c478bd9Sstevel@tonic-gate  *	If user specifies with screen option, use that.
1507c478bd9Sstevel@tonic-gate  *	If baud rate < 1200, use  5
1517c478bd9Sstevel@tonic-gate  *	If baud rate = 1200, use 10
1527c478bd9Sstevel@tonic-gate  *	If baud rate > 1200, use 20
1537c478bd9Sstevel@tonic-gate  */
1547c478bd9Sstevel@tonic-gate int
1557c478bd9Sstevel@tonic-gate screensize(void)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate 	register char *cp;
1587c478bd9Sstevel@tonic-gate 	register int newscreensize, tmp;
1597c478bd9Sstevel@tonic-gate #ifdef	TIOCGWINSZ
1607c478bd9Sstevel@tonic-gate 	struct winsize ws;
1617c478bd9Sstevel@tonic-gate #endif
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	if ((cp = value("screen")) != NOSTR && (tmp = atoi(cp)) > 0)
1647c478bd9Sstevel@tonic-gate 		newscreensize = tmp;
1657c478bd9Sstevel@tonic-gate 	else if (baud < B1200)
1667c478bd9Sstevel@tonic-gate 		newscreensize = 5;
1677c478bd9Sstevel@tonic-gate 	else if (baud == B1200)
1687c478bd9Sstevel@tonic-gate 		newscreensize = 10;
1697c478bd9Sstevel@tonic-gate #ifdef	TIOCGWINSZ
1707c478bd9Sstevel@tonic-gate 	else if (ioctl(fileno(stdout), TIOCGWINSZ, &ws) == 0 && ws.ws_row > 4)
1717c478bd9Sstevel@tonic-gate 		newscreensize = ws.ws_row - 4;
1727c478bd9Sstevel@tonic-gate #endif
1737c478bd9Sstevel@tonic-gate 	else
1747c478bd9Sstevel@tonic-gate 		newscreensize = 20;
1757c478bd9Sstevel@tonic-gate 	/* renormalize the value of curscreen */
1767c478bd9Sstevel@tonic-gate 	if (newscreensize != oldscreensize) {
1777c478bd9Sstevel@tonic-gate 		curscreen = curscreen * oldscreensize / newscreensize;
1787c478bd9Sstevel@tonic-gate 		oldscreensize = newscreensize;
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 	return (newscreensize);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /*
1847c478bd9Sstevel@tonic-gate  * Print out the headlines for each message
1857c478bd9Sstevel@tonic-gate  * in the passed message list.
1867c478bd9Sstevel@tonic-gate  */
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate int
1897c478bd9Sstevel@tonic-gate from(int *msgvec)
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate 	register int *ip;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	for (ip = msgvec; *ip != NULL; ip++) {
1947c478bd9Sstevel@tonic-gate 		printhead(*ip);
1957c478bd9Sstevel@tonic-gate 		sreset();
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate 	if (--ip >= msgvec)
1987c478bd9Sstevel@tonic-gate 		dot = &message[*ip - 1];
1997c478bd9Sstevel@tonic-gate 	return (0);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate /*
2037c478bd9Sstevel@tonic-gate  * Print out the header of a specific message.
2047c478bd9Sstevel@tonic-gate  * This is a slight improvement to the standard one.
2057c478bd9Sstevel@tonic-gate  */
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate void
2087c478bd9Sstevel@tonic-gate printhead(int mesg)
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate 	struct message *mp;
2117c478bd9Sstevel@tonic-gate 	FILE *ibuf;
2127c478bd9Sstevel@tonic-gate 	char headline[LINESIZE], *subjline, dispc, curind;
2137c478bd9Sstevel@tonic-gate 	char *fromline;
2147c478bd9Sstevel@tonic-gate 	char pbuf[LINESIZE];
2157c478bd9Sstevel@tonic-gate 	char name[LINESIZE];
2167c478bd9Sstevel@tonic-gate 	struct headline hl;
2177c478bd9Sstevel@tonic-gate 	register char *cp;
2187c478bd9Sstevel@tonic-gate 	int showto;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	mp = &message[mesg-1];
2217c478bd9Sstevel@tonic-gate 	ibuf = setinput(mp);
2227c478bd9Sstevel@tonic-gate 	readline(ibuf, headline);
2237c478bd9Sstevel@tonic-gate 	if ((subjline = hfield("subject", mp, addone)) == NOSTR &&
2247c478bd9Sstevel@tonic-gate 	    (subjline = hfield("subj", mp, addone)) == NOSTR &&
2257c478bd9Sstevel@tonic-gate 	    (subjline = hfield("message-status", mp, addone)) == NOSTR)
2267c478bd9Sstevel@tonic-gate 		subjline = "";
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	curind = (!Hflag && dot == mp) ? '>' : ' ';
2297c478bd9Sstevel@tonic-gate 	dispc = ' ';
2307c478bd9Sstevel@tonic-gate 	showto = 0;
2317c478bd9Sstevel@tonic-gate 	if ((mp->m_flag & (MREAD|MNEW)) == (MREAD|MNEW))
2327c478bd9Sstevel@tonic-gate 		dispc = 'R';
2337c478bd9Sstevel@tonic-gate 	if (!(int)value("bsdcompat") && (mp->m_flag & (MREAD|MNEW)) == MREAD)
2347c478bd9Sstevel@tonic-gate 		dispc = 'O';
2357c478bd9Sstevel@tonic-gate 	if ((mp->m_flag & (MREAD|MNEW)) == MNEW)
2367c478bd9Sstevel@tonic-gate 		dispc = 'N';
2377c478bd9Sstevel@tonic-gate 	if ((mp->m_flag & (MREAD|MNEW)) == 0)
2387c478bd9Sstevel@tonic-gate 		dispc = 'U';
2397c478bd9Sstevel@tonic-gate 	if (mp->m_flag & MSAVED)
2407c478bd9Sstevel@tonic-gate 		if ((int)value("bsdcompat"))
2417c478bd9Sstevel@tonic-gate 			dispc = '*';
2427c478bd9Sstevel@tonic-gate 		else
2437c478bd9Sstevel@tonic-gate 			dispc = 'S';
2447c478bd9Sstevel@tonic-gate 	if (mp->m_flag & MPRESERVE)
2457c478bd9Sstevel@tonic-gate 		if ((int)value("bsdcompat"))
2467c478bd9Sstevel@tonic-gate 			dispc = 'P';
2477c478bd9Sstevel@tonic-gate 		else
2487c478bd9Sstevel@tonic-gate 			dispc = 'H';
2497c478bd9Sstevel@tonic-gate 	if (mp->m_flag & MBOX)
2507c478bd9Sstevel@tonic-gate 		dispc = 'M';
2517c478bd9Sstevel@tonic-gate 	parse(headline, &hl, pbuf);
2527c478bd9Sstevel@tonic-gate 	if (hl.l_date == NOSTR)
2537c478bd9Sstevel@tonic-gate 		hl.l_date = "<Unknown date>";
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	/*
2567c478bd9Sstevel@tonic-gate 	 * Netnews interface?
2577c478bd9Sstevel@tonic-gate 	 */
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	if (newsflg) {
2607c478bd9Sstevel@tonic-gate 		if ((fromline = hfield("newsgroups", mp, addone)) == NOSTR &&
2617c478bd9Sstevel@tonic-gate 		    (fromline = hfield("article-id", mp, addone)) == NOSTR)
2627c478bd9Sstevel@tonic-gate 			fromline = "<>";
2637c478bd9Sstevel@tonic-gate 		else
2647c478bd9Sstevel@tonic-gate 			for (cp = fromline; *cp; cp++) { /* limit length */
2657c478bd9Sstevel@tonic-gate 				if (any(*cp, " ,\n")) {
2667c478bd9Sstevel@tonic-gate 					*cp = '\0';
2677c478bd9Sstevel@tonic-gate 					break;
2687c478bd9Sstevel@tonic-gate 				}
2697c478bd9Sstevel@tonic-gate 			}
2707c478bd9Sstevel@tonic-gate 	/*
2717c478bd9Sstevel@tonic-gate 	 * else regular.
2727c478bd9Sstevel@tonic-gate 	 */
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	} else {
2757c478bd9Sstevel@tonic-gate 		fromline = nameof(mp);
2767c478bd9Sstevel@tonic-gate 		if (value("showto") &&
2777c478bd9Sstevel@tonic-gate 		    samebody(myname, skin(fromline), FALSE) &&
2787c478bd9Sstevel@tonic-gate 		    (cp = hfield("to", mp, addto))) {
2797c478bd9Sstevel@tonic-gate 			showto = 1;
2807c478bd9Sstevel@tonic-gate 			yankword(cp, fromline = name, sizeof (name),
2817c478bd9Sstevel@tonic-gate 				docomma(cp));
2827c478bd9Sstevel@tonic-gate 		}
2837c478bd9Sstevel@tonic-gate 		if (value("showname"))
2847c478bd9Sstevel@tonic-gate 			fromline = dispname(fromline);
2857c478bd9Sstevel@tonic-gate 		else
2867c478bd9Sstevel@tonic-gate 			fromline = skin(fromline);
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 	printf("%c%c%3d ", curind, dispc, mesg);
2897c478bd9Sstevel@tonic-gate 	if ((int)value("showfull")) {
2907c478bd9Sstevel@tonic-gate 		if (showto)
2917c478bd9Sstevel@tonic-gate 			printf("To %-15s ", fromline);
2927c478bd9Sstevel@tonic-gate 		else
2937c478bd9Sstevel@tonic-gate 			printf("%-18s ", fromline);
2947c478bd9Sstevel@tonic-gate 	} else {
2957c478bd9Sstevel@tonic-gate 		if (showto)
2967c478bd9Sstevel@tonic-gate 			printf("To %-15.15s ", fromline);
2977c478bd9Sstevel@tonic-gate 		else
2987c478bd9Sstevel@tonic-gate 			printf("%-18.18s ", fromline);
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 	if (mp->m_text) {
3017c478bd9Sstevel@tonic-gate 		printf("%16.16s %4ld/%-5ld %-.25s\n",
3027c478bd9Sstevel@tonic-gate 			hl.l_date, mp->m_lines, mp->m_size, subjline);
3037c478bd9Sstevel@tonic-gate 	} else {
3047c478bd9Sstevel@tonic-gate 		printf("%16.16s binary/%-5ld %-.25s\n", hl.l_date, mp->m_size,
3057c478bd9Sstevel@tonic-gate 		    subjline);
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate /*
3107c478bd9Sstevel@tonic-gate  * Return the full name from an RFC-822 header line
3117c478bd9Sstevel@tonic-gate  * or the last two (or one) component of the address.
3127c478bd9Sstevel@tonic-gate  */
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate static char *
3157c478bd9Sstevel@tonic-gate dispname(char *hdr)
3167c478bd9Sstevel@tonic-gate {
3177c478bd9Sstevel@tonic-gate 	char *cp, *cp2;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	if (hdr == 0)
3207c478bd9Sstevel@tonic-gate 		return (0);
3217c478bd9Sstevel@tonic-gate 	if (((cp = strchr(hdr, '<')) != 0) && (cp > hdr)) {
3227c478bd9Sstevel@tonic-gate 		*cp = 0;
3237c478bd9Sstevel@tonic-gate 		if ((*hdr == '"') && ((cp = strrchr(++hdr, '"')) != 0))
3247c478bd9Sstevel@tonic-gate 			*cp = 0;
3257c478bd9Sstevel@tonic-gate 		return (hdr);
3267c478bd9Sstevel@tonic-gate 	} else if ((cp = strchr(hdr, '(')) != 0) {
3277c478bd9Sstevel@tonic-gate 		hdr = ++cp;
3287c478bd9Sstevel@tonic-gate 		if ((cp = strchr(hdr, '+')) != 0)
3297c478bd9Sstevel@tonic-gate 			*cp = 0;
3307c478bd9Sstevel@tonic-gate 		if ((cp = strrchr(hdr, ')')) != 0)
3317c478bd9Sstevel@tonic-gate 			*cp = 0;
3327c478bd9Sstevel@tonic-gate 		return (hdr);
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 	cp = skin(hdr);
3357c478bd9Sstevel@tonic-gate 	if ((cp2 = strrchr(cp, '!')) != 0) {
3367c478bd9Sstevel@tonic-gate 		while (cp2 >= cp && *--cp2 != '!');
3377c478bd9Sstevel@tonic-gate 		cp = ++cp2;
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 	return (cp);
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate /*
3437c478bd9Sstevel@tonic-gate  * Print out the value of dot.
3447c478bd9Sstevel@tonic-gate  */
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate int
3477c478bd9Sstevel@tonic-gate pdot(void)
3487c478bd9Sstevel@tonic-gate {
3497c478bd9Sstevel@tonic-gate 	printf("%d\n", dot - &message[0] + 1);
3507c478bd9Sstevel@tonic-gate 	return (0);
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate /*
3547c478bd9Sstevel@tonic-gate  * Print out all the possible commands.
3557c478bd9Sstevel@tonic-gate  */
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate int
3587c478bd9Sstevel@tonic-gate pcmdlist(void)
3597c478bd9Sstevel@tonic-gate {
3607c478bd9Sstevel@tonic-gate 	register const struct cmd *cp;
3617c478bd9Sstevel@tonic-gate 	register int cc;
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	printf("Commands are:\n");
3647c478bd9Sstevel@tonic-gate 	for (cc = 0, cp = cmdtab; cp->c_name != NULL; cp++) {
3657c478bd9Sstevel@tonic-gate 		cc += strlen(cp->c_name) + 2;
3667c478bd9Sstevel@tonic-gate 		if (cc > 72) {
3677c478bd9Sstevel@tonic-gate 			printf("\n");
3687c478bd9Sstevel@tonic-gate 			cc = strlen(cp->c_name) + 2;
3697c478bd9Sstevel@tonic-gate 		}
3707c478bd9Sstevel@tonic-gate 		if ((cp+1)->c_name != NOSTR)
3717c478bd9Sstevel@tonic-gate 			printf("%s, ", cp->c_name);
3727c478bd9Sstevel@tonic-gate 		else
3737c478bd9Sstevel@tonic-gate 			printf("%s\n", cp->c_name);
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 	return (0);
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate /*
3797c478bd9Sstevel@tonic-gate  * Paginate messages, honor ignored fields.
3807c478bd9Sstevel@tonic-gate  */
3817c478bd9Sstevel@tonic-gate int
3827c478bd9Sstevel@tonic-gate more(int *msgvec)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	return (type1(msgvec, 1, 1));
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate /*
3887c478bd9Sstevel@tonic-gate  * Paginate messages, even printing ignored fields.
3897c478bd9Sstevel@tonic-gate  */
3907c478bd9Sstevel@tonic-gate int
3917c478bd9Sstevel@tonic-gate More(int *msgvec)
3927c478bd9Sstevel@tonic-gate {
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	return (type1(msgvec, 0, 1));
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate /*
3987c478bd9Sstevel@tonic-gate  * Type out messages, honor ignored fields.
3997c478bd9Sstevel@tonic-gate  */
4007c478bd9Sstevel@tonic-gate int
4017c478bd9Sstevel@tonic-gate type(int *msgvec)
4027c478bd9Sstevel@tonic-gate {
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	return (type1(msgvec, 1, 0));
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate /*
4087c478bd9Sstevel@tonic-gate  * Type out messages, even printing ignored fields.
4097c478bd9Sstevel@tonic-gate  */
4107c478bd9Sstevel@tonic-gate int
4117c478bd9Sstevel@tonic-gate Type(int *msgvec)
4127c478bd9Sstevel@tonic-gate {
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	return (type1(msgvec, 0, 0));
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate /*
4187c478bd9Sstevel@tonic-gate  * Type out the messages requested.
4197c478bd9Sstevel@tonic-gate  */
4207c478bd9Sstevel@tonic-gate static int
4217c478bd9Sstevel@tonic-gate type1(int *msgvec, int doign, int page)
4227c478bd9Sstevel@tonic-gate {
423*6c83d09fSrobbin 	int *ip;
4247c478bd9Sstevel@tonic-gate 	register struct message *mp;
4257c478bd9Sstevel@tonic-gate 	register int mesg;
4267c478bd9Sstevel@tonic-gate 	register char *cp;
4277c478bd9Sstevel@tonic-gate 	long nlines;
4287c478bd9Sstevel@tonic-gate 	FILE *obuf;
4297c478bd9Sstevel@tonic-gate 	void (*sigint)(int), (*sigpipe)(int);
4307c478bd9Sstevel@tonic-gate 	int setsigs = 0;
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	obuf = stdout;
4337c478bd9Sstevel@tonic-gate 	if (setjmp(pipestop)) {
4347c478bd9Sstevel@tonic-gate 		if (obuf != stdout) {
4357c478bd9Sstevel@tonic-gate 			pipef = NULL;
4367c478bd9Sstevel@tonic-gate 			npclose(obuf);
4377c478bd9Sstevel@tonic-gate 		}
4387c478bd9Sstevel@tonic-gate 		goto ret0;
4397c478bd9Sstevel@tonic-gate 	}
4407c478bd9Sstevel@tonic-gate 	if (intty && outtty && (page || (cp = value("crt")) != NOSTR)) {
4417c478bd9Sstevel@tonic-gate 		if (!page) {
4427c478bd9Sstevel@tonic-gate 			nlines = 0;
4437c478bd9Sstevel@tonic-gate 			for (ip = msgvec, nlines = 0;
4447c478bd9Sstevel@tonic-gate 			    *ip && ip-msgvec < msgCount; ip++)
4457c478bd9Sstevel@tonic-gate 				nlines += message[*ip - 1].m_lines;
4467c478bd9Sstevel@tonic-gate 		}
4477c478bd9Sstevel@tonic-gate 		if (page ||
4487c478bd9Sstevel@tonic-gate 		    nlines > (*cp == '\0' ? screensize() - 2 : atoi(cp))) {
4497c478bd9Sstevel@tonic-gate 			obuf = npopen(MORE, "w");
4507c478bd9Sstevel@tonic-gate 			if (obuf == NULL) {
4517c478bd9Sstevel@tonic-gate 				perror(MORE);
4527c478bd9Sstevel@tonic-gate 				obuf = stdout;
4537c478bd9Sstevel@tonic-gate 			} else {
4547c478bd9Sstevel@tonic-gate 				pipef = obuf;
4557c478bd9Sstevel@tonic-gate 				sigint = sigset(SIGINT, SIG_IGN);
4567c478bd9Sstevel@tonic-gate 				sigpipe = sigset(SIGPIPE, brokpipe);
4577c478bd9Sstevel@tonic-gate 				setsigs++;
4587c478bd9Sstevel@tonic-gate 			}
4597c478bd9Sstevel@tonic-gate 		}
4607c478bd9Sstevel@tonic-gate 	}
4617c478bd9Sstevel@tonic-gate 	for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
4627c478bd9Sstevel@tonic-gate 		mesg = *ip;
4637c478bd9Sstevel@tonic-gate 		touch(mesg);
4647c478bd9Sstevel@tonic-gate 		mp = &message[mesg-1];
4657c478bd9Sstevel@tonic-gate 		dot = mp;
4667c478bd9Sstevel@tonic-gate 		print(mp, obuf, doign);
4677c478bd9Sstevel@tonic-gate 	}
4687c478bd9Sstevel@tonic-gate 	if (obuf != stdout) {
4697c478bd9Sstevel@tonic-gate 		pipef = NULL;
4707c478bd9Sstevel@tonic-gate 		npclose(obuf);
4717c478bd9Sstevel@tonic-gate 	}
4727c478bd9Sstevel@tonic-gate ret0:
4737c478bd9Sstevel@tonic-gate 	if (setsigs) {
4747c478bd9Sstevel@tonic-gate 		sigset(SIGPIPE, sigpipe);
4757c478bd9Sstevel@tonic-gate 		sigset(SIGINT, sigint);
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate 	return (0);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate /*
4817c478bd9Sstevel@tonic-gate  * Respond to a broken pipe signal --
4827c478bd9Sstevel@tonic-gate  * probably caused by user quitting more.
4837c478bd9Sstevel@tonic-gate  */
4847c478bd9Sstevel@tonic-gate void
4857c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
4867c478bd9Sstevel@tonic-gate brokpipe(int)
4877c478bd9Sstevel@tonic-gate #else
4887c478bd9Sstevel@tonic-gate /* ARGSUSED */
4897c478bd9Sstevel@tonic-gate brokpipe(int s)
4907c478bd9Sstevel@tonic-gate #endif
4917c478bd9Sstevel@tonic-gate {
4927c478bd9Sstevel@tonic-gate #ifdef OLD_BSD_SIGS
4937c478bd9Sstevel@tonic-gate 	sigrelse(SIGPIPE);
4947c478bd9Sstevel@tonic-gate #endif
4957c478bd9Sstevel@tonic-gate 	longjmp(pipestop, 1);
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate /*
4997c478bd9Sstevel@tonic-gate  * Print the indicated message on standard output.
5007c478bd9Sstevel@tonic-gate  */
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate static void
5037c478bd9Sstevel@tonic-gate print(register struct message *mp, FILE *obuf, int doign)
5047c478bd9Sstevel@tonic-gate {
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	if (value("quiet") == NOSTR && (!doign || !isign("message", 0)))
5077c478bd9Sstevel@tonic-gate 		fprintf(obuf, "Message %2d:\n", mp - &message[0] + 1);
5087c478bd9Sstevel@tonic-gate 	touch(mp - &message[0] + 1);
5097c478bd9Sstevel@tonic-gate 	if (mp->m_text) {
5107c478bd9Sstevel@tonic-gate 		(void) msend(mp, obuf, doign ? M_IGNORE : 0, fputs);
5117c478bd9Sstevel@tonic-gate 	} else {
5127c478bd9Sstevel@tonic-gate 		fprintf(obuf, "\n%s\n", gettext(binmsg));
5137c478bd9Sstevel@tonic-gate 	}
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate /*
5177c478bd9Sstevel@tonic-gate  * Print the top so many lines of each desired message.
5187c478bd9Sstevel@tonic-gate  * The number of lines is taken from the variable "toplines"
5197c478bd9Sstevel@tonic-gate  * and defaults to 5.
5207c478bd9Sstevel@tonic-gate  */
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate static	long	top_linecount, top_maxlines, top_lineb;
5237c478bd9Sstevel@tonic-gate static	jmp_buf	top_buf;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate int
5267c478bd9Sstevel@tonic-gate top(int *msgvec)
5277c478bd9Sstevel@tonic-gate {
5287c478bd9Sstevel@tonic-gate 	register int *ip;
5297c478bd9Sstevel@tonic-gate 	register struct message *mp;
5307c478bd9Sstevel@tonic-gate 	register int mesg;
5317c478bd9Sstevel@tonic-gate 	char *valtop;
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	top_maxlines = 5;
5347c478bd9Sstevel@tonic-gate 	valtop = value("toplines");
5357c478bd9Sstevel@tonic-gate 	if (valtop != NOSTR) {
5367c478bd9Sstevel@tonic-gate 		top_maxlines = atoi(valtop);
5377c478bd9Sstevel@tonic-gate 		if (top_maxlines < 0 || top_maxlines > 10000)
5387c478bd9Sstevel@tonic-gate 			top_maxlines = 5;
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 	top_lineb = 1;
5417c478bd9Sstevel@tonic-gate 	for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
5427c478bd9Sstevel@tonic-gate 		mesg = *ip;
5437c478bd9Sstevel@tonic-gate 		touch(mesg);
5447c478bd9Sstevel@tonic-gate 		mp = &message[mesg-1];
5457c478bd9Sstevel@tonic-gate 		dot = mp;
5467c478bd9Sstevel@tonic-gate 		if (value("quiet") == NOSTR)
5477c478bd9Sstevel@tonic-gate 			printf("Message %2d:\n", mesg);
5487c478bd9Sstevel@tonic-gate 		if (!top_lineb)
5497c478bd9Sstevel@tonic-gate 			printf("\n");
5507c478bd9Sstevel@tonic-gate 		top_linecount = 0;
5517c478bd9Sstevel@tonic-gate 		if (setjmp(top_buf) == 0) {
5527c478bd9Sstevel@tonic-gate 			if (mp->m_text) {
5537c478bd9Sstevel@tonic-gate 				(void) msend(mp, stdout, M_IGNORE, topputs);
5547c478bd9Sstevel@tonic-gate 			} else {
5557c478bd9Sstevel@tonic-gate 				printf("\n%s\n", gettext(binmsg));
5567c478bd9Sstevel@tonic-gate 			}
5577c478bd9Sstevel@tonic-gate 		}
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate 	return (0);
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate int
5637c478bd9Sstevel@tonic-gate topputs(const char *line, FILE *obuf)
5647c478bd9Sstevel@tonic-gate {
5657c478bd9Sstevel@tonic-gate 	if (top_linecount++ >= top_maxlines)
5667c478bd9Sstevel@tonic-gate 		longjmp(top_buf, 1);
5677c478bd9Sstevel@tonic-gate 	top_lineb = blankline(line);
5687c478bd9Sstevel@tonic-gate 	return (fputs(line, obuf));
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate /*
5727c478bd9Sstevel@tonic-gate  * Touch all the given messages so that they will
5737c478bd9Sstevel@tonic-gate  * get mboxed.
5747c478bd9Sstevel@tonic-gate  */
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate int
5777c478bd9Sstevel@tonic-gate stouch(int msgvec[])
5787c478bd9Sstevel@tonic-gate {
5797c478bd9Sstevel@tonic-gate 	register int *ip;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	for (ip = msgvec; *ip != 0; ip++) {
5827c478bd9Sstevel@tonic-gate 		dot = &message[*ip-1];
5837c478bd9Sstevel@tonic-gate 		dot->m_flag |= MTOUCH;
5847c478bd9Sstevel@tonic-gate 		dot->m_flag &= ~MPRESERVE;
5857c478bd9Sstevel@tonic-gate 	}
5867c478bd9Sstevel@tonic-gate 	return (0);
5877c478bd9Sstevel@tonic-gate }
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate /*
5907c478bd9Sstevel@tonic-gate  * Make sure all passed messages get mboxed.
5917c478bd9Sstevel@tonic-gate  */
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate int
5947c478bd9Sstevel@tonic-gate mboxit(int msgvec[])
5957c478bd9Sstevel@tonic-gate {
5967c478bd9Sstevel@tonic-gate 	register int *ip;
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	for (ip = msgvec; *ip != 0; ip++) {
5997c478bd9Sstevel@tonic-gate 		dot = &message[*ip-1];
6007c478bd9Sstevel@tonic-gate 		dot->m_flag |= MTOUCH|MBOX;
6017c478bd9Sstevel@tonic-gate 		dot->m_flag &= ~MPRESERVE;
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate 	return (0);
6047c478bd9Sstevel@tonic-gate }
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate /*
6077c478bd9Sstevel@tonic-gate  * List the folders the user currently has.
6087c478bd9Sstevel@tonic-gate  */
6097c478bd9Sstevel@tonic-gate int
6107c478bd9Sstevel@tonic-gate folders(char **arglist)
6117c478bd9Sstevel@tonic-gate {
6127c478bd9Sstevel@tonic-gate 	char dirname[BUFSIZ], cmd[BUFSIZ];
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	if (getfold(dirname) < 0) {
6157c478bd9Sstevel@tonic-gate 		printf(gettext("No value set for \"folder\"\n"));
6167c478bd9Sstevel@tonic-gate 		return (-1);
6177c478bd9Sstevel@tonic-gate 	}
6187c478bd9Sstevel@tonic-gate 	if (*arglist) {
6197c478bd9Sstevel@tonic-gate 		nstrcat(dirname, sizeof (dirname), "/");
6207c478bd9Sstevel@tonic-gate 		nstrcat(dirname, sizeof (dirname), *arglist);
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate 	snprintf(cmd, sizeof (cmd), "%s %s", LS, dirname);
6237c478bd9Sstevel@tonic-gate 	return (system(cmd));
6247c478bd9Sstevel@tonic-gate }
625