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