xref: /freebsd/usr.bin/mail/cmd3.c (revision 5e3934b15a2741b2de6b217e77dc9d798d740804)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1980, 1993
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes  * are met:
109b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes  *    without specific prior written permission.
189b50d902SRodney W. Grimes  *
199b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes  * SUCH DAMAGE.
309b50d902SRodney W. Grimes  */
319b50d902SRodney W. Grimes 
329b50d902SRodney W. Grimes #include "rcv.h"
339b50d902SRodney W. Grimes #include "extern.h"
349b50d902SRodney W. Grimes 
359b50d902SRodney W. Grimes /*
369b50d902SRodney W. Grimes  * Mail -- a mail program
379b50d902SRodney W. Grimes  *
389b50d902SRodney W. Grimes  * Still more user commands.
399b50d902SRodney W. Grimes  */
409b50d902SRodney W. Grimes 
419b50d902SRodney W. Grimes /*
429b50d902SRodney W. Grimes  * Process a shell escape by saving signals, ignoring signals,
439b50d902SRodney W. Grimes  * and forking a sh -c
449b50d902SRodney W. Grimes  */
459b50d902SRodney W. Grimes int
shell(void * str)46*d28a9551SJohn Baldwin shell(void *str)
479b50d902SRodney W. Grimes {
489b50d902SRodney W. Grimes 	sig_t sigint = signal(SIGINT, SIG_IGN);
499ce73e90SMike Heffner 	char *sh;
509b50d902SRodney W. Grimes 	char cmd[BUFSIZ];
519b50d902SRodney W. Grimes 
520c3a8314SMike Heffner 	if (strlcpy(cmd, str, sizeof(cmd)) >= sizeof(cmd))
539ce73e90SMike Heffner 		return (1);
540c3a8314SMike Heffner 	if (bangexp(cmd, sizeof(cmd)) < 0)
559ce73e90SMike Heffner 		return (1);
569ce73e90SMike Heffner 	if ((sh = value("SHELL")) == NULL)
579ce73e90SMike Heffner 		sh = _PATH_CSHELL;
589ce73e90SMike Heffner 	(void)run_command(sh, 0, -1, -1, "-c", cmd, NULL);
599b50d902SRodney W. Grimes 	(void)signal(SIGINT, sigint);
609b50d902SRodney W. Grimes 	printf("!\n");
619ce73e90SMike Heffner 	return (0);
629b50d902SRodney W. Grimes }
639b50d902SRodney W. Grimes 
649b50d902SRodney W. Grimes /*
659b50d902SRodney W. Grimes  * Fork an interactive shell.
669b50d902SRodney W. Grimes  */
679b50d902SRodney W. Grimes /*ARGSUSED*/
689b50d902SRodney W. Grimes int
dosh(void * str __unused)69*d28a9551SJohn Baldwin dosh(void *str __unused)
709b50d902SRodney W. Grimes {
719b50d902SRodney W. Grimes 	sig_t sigint = signal(SIGINT, SIG_IGN);
729ce73e90SMike Heffner 	char *sh;
739b50d902SRodney W. Grimes 
749ce73e90SMike Heffner 	if ((sh = value("SHELL")) == NULL)
759ce73e90SMike Heffner 		sh = _PATH_CSHELL;
76b22a8699SPedro F. Giffuni 	(void)run_command(sh, 0, -1, -1, NULL);
779b50d902SRodney W. Grimes 	(void)signal(SIGINT, sigint);
789ce73e90SMike Heffner 	printf("\n");
799ce73e90SMike Heffner 	return (0);
809b50d902SRodney W. Grimes }
819b50d902SRodney W. Grimes 
829b50d902SRodney W. Grimes /*
839b50d902SRodney W. Grimes  * Expand the shell escape by expanding unescaped !'s into the
849b50d902SRodney W. Grimes  * last issued command where possible.
859b50d902SRodney W. Grimes  */
869b50d902SRodney W. Grimes int
bangexp(char * str,size_t strsize)876d8484b0SPhilippe Charnier bangexp(char *str, size_t strsize)
889b50d902SRodney W. Grimes {
899b50d902SRodney W. Grimes 	char bangbuf[BUFSIZ];
900c3a8314SMike Heffner 	static char lastbang[BUFSIZ];
919ce73e90SMike Heffner 	char *cp, *cp2;
929ce73e90SMike Heffner 	int n, changed = 0;
939b50d902SRodney W. Grimes 
949b50d902SRodney W. Grimes 	cp = str;
959b50d902SRodney W. Grimes 	cp2 = bangbuf;
960c3a8314SMike Heffner 	n = sizeof(bangbuf);
979ce73e90SMike Heffner 	while (*cp != '\0') {
989b50d902SRodney W. Grimes 		if (*cp == '!') {
99b22a8699SPedro F. Giffuni 			if (n < (int)strlen(lastbang)) {
1009b50d902SRodney W. Grimes overf:
1019b50d902SRodney W. Grimes 				printf("Command buffer overflow\n");
1029b50d902SRodney W. Grimes 				return (-1);
1039b50d902SRodney W. Grimes 			}
1049b50d902SRodney W. Grimes 			changed++;
1050c3a8314SMike Heffner 			if (strlcpy(cp2, lastbang, sizeof(bangbuf) - (cp2 - bangbuf))
1060c3a8314SMike Heffner 			    >= sizeof(bangbuf) - (cp2 - bangbuf))
1070c3a8314SMike Heffner 				goto overf;
1089b50d902SRodney W. Grimes 			cp2 += strlen(lastbang);
1099b50d902SRodney W. Grimes 			n -= strlen(lastbang);
1109b50d902SRodney W. Grimes 			cp++;
1119b50d902SRodney W. Grimes 			continue;
1129b50d902SRodney W. Grimes 		}
1139b50d902SRodney W. Grimes 		if (*cp == '\\' && cp[1] == '!') {
1149b50d902SRodney W. Grimes 			if (--n <= 1)
1159b50d902SRodney W. Grimes 				goto overf;
1169b50d902SRodney W. Grimes 			*cp2++ = '!';
1179b50d902SRodney W. Grimes 			cp += 2;
1189b50d902SRodney W. Grimes 			changed++;
1199b50d902SRodney W. Grimes 		}
1209b50d902SRodney W. Grimes 		if (--n <= 1)
1219b50d902SRodney W. Grimes 			goto overf;
1229b50d902SRodney W. Grimes 		*cp2++ = *cp++;
1239b50d902SRodney W. Grimes 	}
1249b50d902SRodney W. Grimes 	*cp2 = 0;
1259b50d902SRodney W. Grimes 	if (changed) {
1269b50d902SRodney W. Grimes 		printf("!%s\n", bangbuf);
1279ce73e90SMike Heffner 		(void)fflush(stdout);
1289b50d902SRodney W. Grimes 	}
1290c3a8314SMike Heffner 	if (strlcpy(str, bangbuf, strsize) >= strsize)
1300c3a8314SMike Heffner 		goto overf;
1310c3a8314SMike Heffner 	if (strlcpy(lastbang, bangbuf, sizeof(lastbang)) >= sizeof(lastbang))
1320c3a8314SMike Heffner 		goto overf;
1339b50d902SRodney W. Grimes 	return (0);
1349b50d902SRodney W. Grimes }
1359b50d902SRodney W. Grimes 
1369b50d902SRodney W. Grimes /*
1379b50d902SRodney W. Grimes  * Print out a nice help message from some file or another.
1389b50d902SRodney W. Grimes  */
1399b50d902SRodney W. Grimes 
1409b50d902SRodney W. Grimes int
help(void * arg __unused)141*d28a9551SJohn Baldwin help(void *arg __unused)
1429b50d902SRodney W. Grimes {
1439ce73e90SMike Heffner 	int c;
1449ce73e90SMike Heffner 	FILE *f;
1459b50d902SRodney W. Grimes 
1469b50d902SRodney W. Grimes 	if ((f = Fopen(_PATH_HELP, "r")) == NULL) {
1470c3a8314SMike Heffner 		warn("%s", _PATH_HELP);
1489b50d902SRodney W. Grimes 		return (1);
1499b50d902SRodney W. Grimes 	}
1509b50d902SRodney W. Grimes 	while ((c = getc(f)) != EOF)
1519b50d902SRodney W. Grimes 		putchar(c);
1529ce73e90SMike Heffner 	(void)Fclose(f);
1539b50d902SRodney W. Grimes 	return (0);
1549b50d902SRodney W. Grimes }
1559b50d902SRodney W. Grimes 
1569b50d902SRodney W. Grimes /*
1579b50d902SRodney W. Grimes  * Change user's working directory.
1589b50d902SRodney W. Grimes  */
1599b50d902SRodney W. Grimes int
schdir(void * v)160b948550dSPedro F. Giffuni schdir(void *v)
1619b50d902SRodney W. Grimes {
162b948550dSPedro F. Giffuni 	char **arglist = v;
1639b50d902SRodney W. Grimes 	char *cp;
1649b50d902SRodney W. Grimes 
1659ce73e90SMike Heffner 	if (*arglist == NULL) {
1669ce73e90SMike Heffner 		if (homedir == NULL)
1670c3a8314SMike Heffner 			return (1);
1689b50d902SRodney W. Grimes 		cp = homedir;
1690c3a8314SMike Heffner 	} else
1709ce73e90SMike Heffner 		if ((cp = expand(*arglist)) == NULL)
1719b50d902SRodney W. Grimes 			return (1);
1729b50d902SRodney W. Grimes 	if (chdir(cp) < 0) {
1730c3a8314SMike Heffner 		warn("%s", cp);
1749b50d902SRodney W. Grimes 		return (1);
1759b50d902SRodney W. Grimes 	}
1769ce73e90SMike Heffner 	return (0);
1779b50d902SRodney W. Grimes }
1789b50d902SRodney W. Grimes 
1799b50d902SRodney W. Grimes int
respond(void * v)180b948550dSPedro F. Giffuni respond(void *v)
1819b50d902SRodney W. Grimes {
182b948550dSPedro F. Giffuni 	int *msgvec = v;
183b948550dSPedro F. Giffuni 
1840077673eSMike Heffner 	if (value("Replyall") == NULL && value("flipr") == NULL)
18599bd6601SJoerg Wunsch 		return (dorespond(msgvec));
1869b50d902SRodney W. Grimes 	else
18799bd6601SJoerg Wunsch 		return (doRespond(msgvec));
1889b50d902SRodney W. Grimes }
1899b50d902SRodney W. Grimes 
1909b50d902SRodney W. Grimes /*
1919b50d902SRodney W. Grimes  * Reply to a list of messages.  Extract each name from the
1929b50d902SRodney W. Grimes  * message header and send them off to mail1()
1939b50d902SRodney W. Grimes  */
1949b50d902SRodney W. Grimes int
dorespond(int * msgvec)1956d8484b0SPhilippe Charnier dorespond(int *msgvec)
1969b50d902SRodney W. Grimes {
1979b50d902SRodney W. Grimes 	struct message *mp;
1989b50d902SRodney W. Grimes 	char *cp, *rcv, *replyto;
1999b50d902SRodney W. Grimes 	char **ap;
2009b50d902SRodney W. Grimes 	struct name *np;
2019b50d902SRodney W. Grimes 	struct header head;
2029b50d902SRodney W. Grimes 
2039b50d902SRodney W. Grimes 	if (msgvec[1] != 0) {
2049b50d902SRodney W. Grimes 		printf("Sorry, can't reply to multiple messages at once\n");
2059b50d902SRodney W. Grimes 		return (1);
2069b50d902SRodney W. Grimes 	}
2079b50d902SRodney W. Grimes 	mp = &message[msgvec[0] - 1];
2089b50d902SRodney W. Grimes 	touch(mp);
2099b50d902SRodney W. Grimes 	dot = mp;
2109ce73e90SMike Heffner 	if ((rcv = skin(hfield("from", mp))) == NULL)
2119b50d902SRodney W. Grimes 		rcv = skin(nameof(mp, 1));
2129ce73e90SMike Heffner 	if ((replyto = skin(hfield("reply-to", mp))) != NULL)
2139b50d902SRodney W. Grimes 		np = extract(replyto, GTO);
2149ce73e90SMike Heffner 	else if ((cp = skin(hfield("to", mp))) != NULL)
2159b50d902SRodney W. Grimes 		np = extract(cp, GTO);
2169b50d902SRodney W. Grimes 	else
2179ce73e90SMike Heffner 		np = NULL;
2189b50d902SRodney W. Grimes 	np = elide(np);
2199b50d902SRodney W. Grimes 	/*
2209b50d902SRodney W. Grimes 	 * Delete my name from the reply list,
2219b50d902SRodney W. Grimes 	 * and with it, all my alternate names.
2229b50d902SRodney W. Grimes 	 */
2239b50d902SRodney W. Grimes 	np = delname(np, myname);
2249b50d902SRodney W. Grimes 	if (altnames)
2259ce73e90SMike Heffner 		for (ap = altnames; *ap != NULL; ap++)
2269b50d902SRodney W. Grimes 			np = delname(np, *ap);
2279ce73e90SMike Heffner 	if (np != NULL && replyto == NULL)
2289b50d902SRodney W. Grimes 		np = cat(np, extract(rcv, GTO));
2299ce73e90SMike Heffner 	else if (np == NULL) {
2309ce73e90SMike Heffner 		if (replyto != NULL)
2319b50d902SRodney W. Grimes 			printf("Empty reply-to field -- replying to author\n");
2329b50d902SRodney W. Grimes 		np = extract(rcv, GTO);
2339b50d902SRodney W. Grimes 	}
2349b50d902SRodney W. Grimes 	head.h_to = np;
2359ce73e90SMike Heffner 	if ((head.h_subject = hfield("subject", mp)) == NULL)
2369b50d902SRodney W. Grimes 		head.h_subject = hfield("subj", mp);
2379b50d902SRodney W. Grimes 	head.h_subject = reedit(head.h_subject);
2389ce73e90SMike Heffner 	if (replyto == NULL && (cp = skin(hfield("cc", mp))) != NULL) {
2399b50d902SRodney W. Grimes 		np = elide(extract(cp, GCC));
2409b50d902SRodney W. Grimes 		np = delname(np, myname);
2419b50d902SRodney W. Grimes 		if (altnames != 0)
2429ce73e90SMike Heffner 			for (ap = altnames; *ap != NULL; ap++)
2439b50d902SRodney W. Grimes 				np = delname(np, *ap);
2449b50d902SRodney W. Grimes 		head.h_cc = np;
2459b50d902SRodney W. Grimes 	} else
2469ce73e90SMike Heffner 		head.h_cc = NULL;
2479ce73e90SMike Heffner 	head.h_bcc = NULL;
2489ce73e90SMike Heffner 	head.h_smopts = NULL;
24959c3f4f7SMike Heffner 	head.h_replyto = value("REPLYTO");
25099bd6601SJoerg Wunsch 	head.h_inreplyto = skin(hfield("message-id", mp));
2519b50d902SRodney W. Grimes 	mail1(&head, 1);
2529b50d902SRodney W. Grimes 	return (0);
2539b50d902SRodney W. Grimes }
2549b50d902SRodney W. Grimes 
2559b50d902SRodney W. Grimes /*
256dae3a64fSEitan Adler  * Modify the message subject to begin with "Re:" if
2579b50d902SRodney W. Grimes  * it does not already.
2589b50d902SRodney W. Grimes  */
2599b50d902SRodney W. Grimes char *
reedit(char * subj)2606d8484b0SPhilippe Charnier reedit(char *subj)
2619b50d902SRodney W. Grimes {
2629b50d902SRodney W. Grimes 	char *newsubj;
2639b50d902SRodney W. Grimes 
2649ce73e90SMike Heffner 	if (subj == NULL)
2659ce73e90SMike Heffner 		return (NULL);
2669b50d902SRodney W. Grimes 	if ((subj[0] == 'r' || subj[0] == 'R') &&
2679b50d902SRodney W. Grimes 	    (subj[1] == 'e' || subj[1] == 'E') &&
2689b50d902SRodney W. Grimes 	    subj[2] == ':')
2699ce73e90SMike Heffner 		return (subj);
2709b50d902SRodney W. Grimes 	newsubj = salloc(strlen(subj) + 5);
2710c3a8314SMike Heffner 	sprintf(newsubj, "Re: %s", subj);
2729ce73e90SMike Heffner 	return (newsubj);
2739b50d902SRodney W. Grimes }
2749b50d902SRodney W. Grimes 
2759b50d902SRodney W. Grimes /*
2769b50d902SRodney W. Grimes  * Preserve the named messages, so that they will be sent
2779b50d902SRodney W. Grimes  * back to the system mailbox.
2789b50d902SRodney W. Grimes  */
2799b50d902SRodney W. Grimes int
preserve(void * v)280b948550dSPedro F. Giffuni preserve(void *v)
2819b50d902SRodney W. Grimes {
282b948550dSPedro F. Giffuni 	int *msgvec = v;
2839ce73e90SMike Heffner 	int *ip, mesg;
2849ce73e90SMike Heffner 	struct message *mp;
2859b50d902SRodney W. Grimes 
2869b50d902SRodney W. Grimes 	if (edit) {
2879b50d902SRodney W. Grimes 		printf("Cannot \"preserve\" in edit mode\n");
2889b50d902SRodney W. Grimes 		return (1);
2899b50d902SRodney W. Grimes 	}
290d030d2d2SPoul-Henning Kamp 	for (ip = msgvec; *ip != 0; ip++) {
2919b50d902SRodney W. Grimes 		mesg = *ip;
2929b50d902SRodney W. Grimes 		mp = &message[mesg-1];
2939b50d902SRodney W. Grimes 		mp->m_flag |= MPRESERVE;
2949b50d902SRodney W. Grimes 		mp->m_flag &= ~MBOX;
2959b50d902SRodney W. Grimes 		dot = mp;
2969b50d902SRodney W. Grimes 	}
2979b50d902SRodney W. Grimes 	return (0);
2989b50d902SRodney W. Grimes }
2999b50d902SRodney W. Grimes 
3009b50d902SRodney W. Grimes /*
3019b50d902SRodney W. Grimes  * Mark all given messages as unread.
3029b50d902SRodney W. Grimes  */
3039b50d902SRodney W. Grimes int
unread(void * v)304b948550dSPedro F. Giffuni unread(void *v)
3059b50d902SRodney W. Grimes {
306b948550dSPedro F. Giffuni 	int *msgvec = v;
3079ce73e90SMike Heffner 	int *ip;
3089b50d902SRodney W. Grimes 
309d030d2d2SPoul-Henning Kamp 	for (ip = msgvec; *ip != 0; ip++) {
3109b50d902SRodney W. Grimes 		dot = &message[*ip-1];
3119b50d902SRodney W. Grimes 		dot->m_flag &= ~(MREAD|MTOUCH);
3129b50d902SRodney W. Grimes 		dot->m_flag |= MSTATUS;
3139b50d902SRodney W. Grimes 	}
3149b50d902SRodney W. Grimes 	return (0);
3159b50d902SRodney W. Grimes }
3169b50d902SRodney W. Grimes 
3179b50d902SRodney W. Grimes /*
3189b50d902SRodney W. Grimes  * Print the size of each message.
3199b50d902SRodney W. Grimes  */
3209b50d902SRodney W. Grimes int
messize(void * v)321b948550dSPedro F. Giffuni messize(void *v)
3229b50d902SRodney W. Grimes {
323b948550dSPedro F. Giffuni 	int *msgvec = v;
3249ce73e90SMike Heffner 	struct message *mp;
3259ce73e90SMike Heffner 	int *ip, mesg;
3269b50d902SRodney W. Grimes 
327d030d2d2SPoul-Henning Kamp 	for (ip = msgvec; *ip != 0; ip++) {
3289b50d902SRodney W. Grimes 		mesg = *ip;
3299b50d902SRodney W. Grimes 		mp = &message[mesg-1];
33022694ebaSBruce Evans 		printf("%d: %ld/%ld\n", mesg, mp->m_lines, mp->m_size);
3319b50d902SRodney W. Grimes 	}
3329b50d902SRodney W. Grimes 	return (0);
3339b50d902SRodney W. Grimes }
3349b50d902SRodney W. Grimes 
3359b50d902SRodney W. Grimes /*
3369b50d902SRodney W. Grimes  * Quit quickly.  If we are sourcing, just pop the input level
3379b50d902SRodney W. Grimes  * by returning an error.
3389b50d902SRodney W. Grimes  */
3399b50d902SRodney W. Grimes int
rexit(void * v)340b948550dSPedro F. Giffuni rexit(void *v)
3419b50d902SRodney W. Grimes {
3429b50d902SRodney W. Grimes 	if (sourcing)
3439b50d902SRodney W. Grimes 		return (1);
344856f23edSMike Heffner 	exit(0);
3459b50d902SRodney W. Grimes 	/*NOTREACHED*/
3469b50d902SRodney W. Grimes }
3479b50d902SRodney W. Grimes 
3489b50d902SRodney W. Grimes /*
3499b50d902SRodney W. Grimes  * Set or display a variable value.  Syntax is similar to that
3509b50d902SRodney W. Grimes  * of csh.
3519b50d902SRodney W. Grimes  */
3529b50d902SRodney W. Grimes int
set(void * v)353b948550dSPedro F. Giffuni set(void *v)
3549b50d902SRodney W. Grimes {
355b948550dSPedro F. Giffuni 	char **arglist = v;
3569ce73e90SMike Heffner 	struct var *vp;
3579ce73e90SMike Heffner 	char *cp, *cp2;
3589b50d902SRodney W. Grimes 	char varbuf[BUFSIZ], **ap, **p;
3599b50d902SRodney W. Grimes 	int errs, h, s;
3609b50d902SRodney W. Grimes 
3619ce73e90SMike Heffner 	if (*arglist == NULL) {
3629b50d902SRodney W. Grimes 		for (h = 0, s = 1; h < HSHSIZE; h++)
3639ce73e90SMike Heffner 			for (vp = variables[h]; vp != NULL; vp = vp->v_link)
3649b50d902SRodney W. Grimes 				s++;
3659ce73e90SMike Heffner 		ap = (char **)salloc(s * sizeof(*ap));
3669b50d902SRodney W. Grimes 		for (h = 0, p = ap; h < HSHSIZE; h++)
3679ce73e90SMike Heffner 			for (vp = variables[h]; vp != NULL; vp = vp->v_link)
3689b50d902SRodney W. Grimes 				*p++ = vp->v_name;
3699ce73e90SMike Heffner 		*p = NULL;
3709b50d902SRodney W. Grimes 		sort(ap);
3719ce73e90SMike Heffner 		for (p = ap; *p != NULL; p++)
3729b50d902SRodney W. Grimes 			printf("%s\t%s\n", *p, value(*p));
3739b50d902SRodney W. Grimes 		return (0);
3749b50d902SRodney W. Grimes 	}
3759b50d902SRodney W. Grimes 	errs = 0;
3769ce73e90SMike Heffner 	for (ap = arglist; *ap != NULL; ap++) {
3779b50d902SRodney W. Grimes 		cp = *ap;
3789b50d902SRodney W. Grimes 		cp2 = varbuf;
3790c3a8314SMike Heffner 		while (cp2 < varbuf + sizeof(varbuf) - 1 && *cp != '=' && *cp != '\0')
3809b50d902SRodney W. Grimes 			*cp2++ = *cp++;
3819b50d902SRodney W. Grimes 		*cp2 = '\0';
3829b50d902SRodney W. Grimes 		if (*cp == '\0')
3839b50d902SRodney W. Grimes 			cp = "";
3849b50d902SRodney W. Grimes 		else
3859b50d902SRodney W. Grimes 			cp++;
3869b50d902SRodney W. Grimes 		if (equal(varbuf, "")) {
3879b50d902SRodney W. Grimes 			printf("Non-null variable name required\n");
3889b50d902SRodney W. Grimes 			errs++;
3899b50d902SRodney W. Grimes 			continue;
3909b50d902SRodney W. Grimes 		}
3919b50d902SRodney W. Grimes 		assign(varbuf, cp);
3929b50d902SRodney W. Grimes 	}
3939b50d902SRodney W. Grimes 	return (errs);
3949b50d902SRodney W. Grimes }
3959b50d902SRodney W. Grimes 
3969b50d902SRodney W. Grimes /*
3979b50d902SRodney W. Grimes  * Unset a bunch of variable values.
3989b50d902SRodney W. Grimes  */
3999b50d902SRodney W. Grimes int
unset(void * v)400b948550dSPedro F. Giffuni unset(void *v)
4019b50d902SRodney W. Grimes {
402b948550dSPedro F. Giffuni 	char **arglist = v;
4039ce73e90SMike Heffner 	struct var *vp, *vp2;
4049b50d902SRodney W. Grimes 	int errs, h;
4059b50d902SRodney W. Grimes 	char **ap;
4069b50d902SRodney W. Grimes 
4079b50d902SRodney W. Grimes 	errs = 0;
4089ce73e90SMike Heffner 	for (ap = arglist; *ap != NULL; ap++) {
4099ce73e90SMike Heffner 		if ((vp2 = lookup(*ap)) == NULL) {
410856f23edSMike Heffner 			if (getenv(*ap))
411856f23edSMike Heffner 				unsetenv(*ap);
412856f23edSMike Heffner 			else if (!sourcing) {
4139b50d902SRodney W. Grimes 				printf("\"%s\": undefined variable\n", *ap);
4149b50d902SRodney W. Grimes 				errs++;
4159b50d902SRodney W. Grimes 			}
4169b50d902SRodney W. Grimes 			continue;
4179b50d902SRodney W. Grimes 		}
4189b50d902SRodney W. Grimes 		h = hash(*ap);
4199b50d902SRodney W. Grimes 		if (vp2 == variables[h]) {
4209b50d902SRodney W. Grimes 			variables[h] = variables[h]->v_link;
4219b50d902SRodney W. Grimes 			vfree(vp2->v_name);
4229b50d902SRodney W. Grimes 			vfree(vp2->v_value);
4239ce73e90SMike Heffner 			(void)free(vp2);
4249b50d902SRodney W. Grimes 			continue;
4259b50d902SRodney W. Grimes 		}
4269b50d902SRodney W. Grimes 		for (vp = variables[h]; vp->v_link != vp2; vp = vp->v_link)
4279b50d902SRodney W. Grimes 			;
4289b50d902SRodney W. Grimes 		vp->v_link = vp2->v_link;
4299b50d902SRodney W. Grimes 		vfree(vp2->v_name);
4309b50d902SRodney W. Grimes 		vfree(vp2->v_value);
4319ce73e90SMike Heffner 		(void)free(vp2);
4329b50d902SRodney W. Grimes 	}
4339b50d902SRodney W. Grimes 	return (errs);
4349b50d902SRodney W. Grimes }
4359b50d902SRodney W. Grimes 
4369b50d902SRodney W. Grimes /*
4379b50d902SRodney W. Grimes  * Put add users to a group.
4389b50d902SRodney W. Grimes  */
4399b50d902SRodney W. Grimes int
group(void * v)440b948550dSPedro F. Giffuni group(void *v)
4419b50d902SRodney W. Grimes {
442b948550dSPedro F. Giffuni 	char **argv = v;
4439ce73e90SMike Heffner 	struct grouphead *gh;
4449ce73e90SMike Heffner 	struct group *gp;
4459b50d902SRodney W. Grimes 	char **ap, *gname, **p;
4469ce73e90SMike Heffner 	int h, s;
4479b50d902SRodney W. Grimes 
4489ce73e90SMike Heffner 	if (*argv == NULL) {
4499b50d902SRodney W. Grimes 		for (h = 0, s = 1; h < HSHSIZE; h++)
4509ce73e90SMike Heffner 			for (gh = groups[h]; gh != NULL; gh = gh->g_link)
4519b50d902SRodney W. Grimes 				s++;
4529ce73e90SMike Heffner 		ap = (char **)salloc(s * sizeof(*ap));
4539b50d902SRodney W. Grimes 		for (h = 0, p = ap; h < HSHSIZE; h++)
4549ce73e90SMike Heffner 			for (gh = groups[h]; gh != NULL; gh = gh->g_link)
4559b50d902SRodney W. Grimes 				*p++ = gh->g_name;
4569ce73e90SMike Heffner 		*p = NULL;
4579b50d902SRodney W. Grimes 		sort(ap);
4589ce73e90SMike Heffner 		for (p = ap; *p != NULL; p++)
4599b50d902SRodney W. Grimes 			printgroup(*p);
4609b50d902SRodney W. Grimes 		return (0);
4619b50d902SRodney W. Grimes 	}
4629ce73e90SMike Heffner 	if (argv[1] == NULL) {
4639b50d902SRodney W. Grimes 		printgroup(*argv);
4649b50d902SRodney W. Grimes 		return (0);
4659b50d902SRodney W. Grimes 	}
4669b50d902SRodney W. Grimes 	gname = *argv;
4679b50d902SRodney W. Grimes 	h = hash(gname);
4689ce73e90SMike Heffner 	if ((gh = findgroup(gname)) == NULL) {
469dde0f2bfSPedro F. Giffuni 		if ((gh = calloc(1, sizeof(*gh))) == NULL)
470dde0f2bfSPedro F. Giffuni 			err(1, "Out of memory");
4719b50d902SRodney W. Grimes 		gh->g_name = vcopy(gname);
4729ce73e90SMike Heffner 		gh->g_list = NULL;
4739b50d902SRodney W. Grimes 		gh->g_link = groups[h];
4749b50d902SRodney W. Grimes 		groups[h] = gh;
4759b50d902SRodney W. Grimes 	}
4769b50d902SRodney W. Grimes 
4779b50d902SRodney W. Grimes 	/*
4789b50d902SRodney W. Grimes 	 * Insert names from the command list into the group.
4799b50d902SRodney W. Grimes 	 * Who cares if there are duplicates?  They get tossed
4809b50d902SRodney W. Grimes 	 * later anyway.
4819b50d902SRodney W. Grimes 	 */
4829b50d902SRodney W. Grimes 
4839ce73e90SMike Heffner 	for (ap = argv+1; *ap != NULL; ap++) {
484dde0f2bfSPedro F. Giffuni 		if ((gp = calloc(1, sizeof(*gp))) == NULL)
485dde0f2bfSPedro F. Giffuni 			err(1, "Out of memory");
4869b50d902SRodney W. Grimes 		gp->ge_name = vcopy(*ap);
4879b50d902SRodney W. Grimes 		gp->ge_link = gh->g_list;
4889b50d902SRodney W. Grimes 		gh->g_list = gp;
4899b50d902SRodney W. Grimes 	}
4909b50d902SRodney W. Grimes 	return (0);
4919b50d902SRodney W. Grimes }
4929b50d902SRodney W. Grimes 
4939b50d902SRodney W. Grimes /*
4949b50d902SRodney W. Grimes  * Sort the passed string vecotor into ascending dictionary
4959b50d902SRodney W. Grimes  * order.
4969b50d902SRodney W. Grimes  */
4979b50d902SRodney W. Grimes void
sort(char ** list)4986d8484b0SPhilippe Charnier sort(char **list)
4999b50d902SRodney W. Grimes {
5009ce73e90SMike Heffner 	char **ap;
5019b50d902SRodney W. Grimes 
5029ce73e90SMike Heffner 	for (ap = list; *ap != NULL; ap++)
5039b50d902SRodney W. Grimes 		;
5049b50d902SRodney W. Grimes 	if (ap-list < 2)
5059b50d902SRodney W. Grimes 		return;
5069b50d902SRodney W. Grimes 	qsort(list, ap-list, sizeof(*list), diction);
5079b50d902SRodney W. Grimes }
5089b50d902SRodney W. Grimes 
5099b50d902SRodney W. Grimes /*
5109b50d902SRodney W. Grimes  * Do a dictionary order comparison of the arguments from
5119b50d902SRodney W. Grimes  * qsort.
5129b50d902SRodney W. Grimes  */
5139b50d902SRodney W. Grimes int
diction(const void * a,const void * b)5146d8484b0SPhilippe Charnier diction(const void *a, const void *b)
5159b50d902SRodney W. Grimes {
5169ce73e90SMike Heffner 	return (strcmp(*(const char **)a, *(const char **)b));
5179b50d902SRodney W. Grimes }
5189b50d902SRodney W. Grimes 
5199b50d902SRodney W. Grimes /*
5209b50d902SRodney W. Grimes  * The do nothing command for comments.
5219b50d902SRodney W. Grimes  */
5229b50d902SRodney W. Grimes 
5239b50d902SRodney W. Grimes /*ARGSUSED*/
5249b50d902SRodney W. Grimes int
null(void * arg __unused)525*d28a9551SJohn Baldwin null(void *arg __unused)
5269b50d902SRodney W. Grimes {
5279ce73e90SMike Heffner 	return (0);
5289b50d902SRodney W. Grimes }
5299b50d902SRodney W. Grimes 
5309b50d902SRodney W. Grimes /*
5319b50d902SRodney W. Grimes  * Change to another file.  With no argument, print information about
5329b50d902SRodney W. Grimes  * the current file.
5339b50d902SRodney W. Grimes  */
5349b50d902SRodney W. Grimes int
file(void * arg)535*d28a9551SJohn Baldwin file(void *arg)
5369b50d902SRodney W. Grimes {
537*d28a9551SJohn Baldwin 	char **argv = arg;
5389b50d902SRodney W. Grimes 
5399ce73e90SMike Heffner 	if (argv[0] == NULL) {
540856f23edSMike Heffner 		newfileinfo(0);
5419ce73e90SMike Heffner 		return (0);
5429b50d902SRodney W. Grimes 	}
5439b50d902SRodney W. Grimes 	if (setfile(*argv) < 0)
5449ce73e90SMike Heffner 		return (1);
5459b50d902SRodney W. Grimes 	announce();
5469ce73e90SMike Heffner 	return (0);
5479b50d902SRodney W. Grimes }
5489b50d902SRodney W. Grimes 
5499b50d902SRodney W. Grimes /*
5509b50d902SRodney W. Grimes  * Expand file names like echo
5519b50d902SRodney W. Grimes  */
5529b50d902SRodney W. Grimes int
echo(void * arg)553*d28a9551SJohn Baldwin echo(void *arg)
5549b50d902SRodney W. Grimes {
555*d28a9551SJohn Baldwin 	char **argv = arg;
5569ce73e90SMike Heffner 	char **ap, *cp;
5579b50d902SRodney W. Grimes 
5589ce73e90SMike Heffner 	for (ap = argv; *ap != NULL; ap++) {
5599b50d902SRodney W. Grimes 		cp = *ap;
5609ce73e90SMike Heffner 		if ((cp = expand(cp)) != NULL) {
5619b50d902SRodney W. Grimes 			if (ap != argv)
5629ce73e90SMike Heffner 				printf(" ");
5639b50d902SRodney W. Grimes 			printf("%s", cp);
5649b50d902SRodney W. Grimes 		}
5659b50d902SRodney W. Grimes 	}
5669ce73e90SMike Heffner 	printf("\n");
5679ce73e90SMike Heffner 	return (0);
5689b50d902SRodney W. Grimes }
5699b50d902SRodney W. Grimes 
5709b50d902SRodney W. Grimes int
Respond(void * msgvec)571*d28a9551SJohn Baldwin Respond(void *msgvec)
5729b50d902SRodney W. Grimes {
5730077673eSMike Heffner 	if (value("Replyall") == NULL && value("flipr") == NULL)
57499bd6601SJoerg Wunsch 		return (doRespond(msgvec));
5759b50d902SRodney W. Grimes 	else
57699bd6601SJoerg Wunsch 		return (dorespond(msgvec));
5779b50d902SRodney W. Grimes }
5789b50d902SRodney W. Grimes 
5799b50d902SRodney W. Grimes /*
5809b50d902SRodney W. Grimes  * Reply to a series of messages by simply mailing to the senders
5819b50d902SRodney W. Grimes  * and not messing around with the To: and Cc: lists as in normal
5829b50d902SRodney W. Grimes  * reply.
5839b50d902SRodney W. Grimes  */
5849b50d902SRodney W. Grimes int
doRespond(int msgvec[])5856d8484b0SPhilippe Charnier doRespond(int msgvec[])
5869b50d902SRodney W. Grimes {
5879b50d902SRodney W. Grimes 	struct header head;
5889b50d902SRodney W. Grimes 	struct message *mp;
5899ce73e90SMike Heffner 	int *ap;
5909ce73e90SMike Heffner 	char *cp, *mid;
5919b50d902SRodney W. Grimes 
5929ce73e90SMike Heffner 	head.h_to = NULL;
5939b50d902SRodney W. Grimes 	for (ap = msgvec; *ap != 0; ap++) {
5949b50d902SRodney W. Grimes 		mp = &message[*ap - 1];
5959b50d902SRodney W. Grimes 		touch(mp);
5969b50d902SRodney W. Grimes 		dot = mp;
5979ce73e90SMike Heffner 		if ((cp = skin(hfield("from", mp))) == NULL)
5989b50d902SRodney W. Grimes 			cp = skin(nameof(mp, 2));
5999b50d902SRodney W. Grimes 		head.h_to = cat(head.h_to, extract(cp, GTO));
60099bd6601SJoerg Wunsch 		mid = skin(hfield("message-id", mp));
6019b50d902SRodney W. Grimes 	}
6029ce73e90SMike Heffner 	if (head.h_to == NULL)
6039ce73e90SMike Heffner 		return (0);
6049b50d902SRodney W. Grimes 	mp = &message[msgvec[0] - 1];
6059ce73e90SMike Heffner 	if ((head.h_subject = hfield("subject", mp)) == NULL)
6069b50d902SRodney W. Grimes 		head.h_subject = hfield("subj", mp);
6079b50d902SRodney W. Grimes 	head.h_subject = reedit(head.h_subject);
6089ce73e90SMike Heffner 	head.h_cc = NULL;
6099ce73e90SMike Heffner 	head.h_bcc = NULL;
6109ce73e90SMike Heffner 	head.h_smopts = NULL;
61159c3f4f7SMike Heffner 	head.h_replyto = value("REPLYTO");
61299bd6601SJoerg Wunsch 	head.h_inreplyto = mid;
6139b50d902SRodney W. Grimes 	mail1(&head, 1);
6149ce73e90SMike Heffner 	return (0);
6159b50d902SRodney W. Grimes }
6169b50d902SRodney W. Grimes 
6179b50d902SRodney W. Grimes /*
6189b50d902SRodney W. Grimes  * Conditional commands.  These allow one to parameterize one's
6199b50d902SRodney W. Grimes  * .mailrc and do some things if sending, others if receiving.
6209b50d902SRodney W. Grimes  */
6219b50d902SRodney W. Grimes int
ifcmd(void * arg)622*d28a9551SJohn Baldwin ifcmd(void *arg)
6239b50d902SRodney W. Grimes {
624*d28a9551SJohn Baldwin 	char **argv = arg;
6259ce73e90SMike Heffner 	char *cp;
6269b50d902SRodney W. Grimes 
6279b50d902SRodney W. Grimes 	if (cond != CANY) {
6289b50d902SRodney W. Grimes 		printf("Illegal nested \"if\"\n");
6299b50d902SRodney W. Grimes 		return (1);
6309b50d902SRodney W. Grimes 	}
6319b50d902SRodney W. Grimes 	cond = CANY;
6329b50d902SRodney W. Grimes 	cp = argv[0];
6339b50d902SRodney W. Grimes 	switch (*cp) {
6349b50d902SRodney W. Grimes 	case 'r': case 'R':
6359b50d902SRodney W. Grimes 		cond = CRCV;
6369b50d902SRodney W. Grimes 		break;
6379b50d902SRodney W. Grimes 
6389b50d902SRodney W. Grimes 	case 's': case 'S':
6399b50d902SRodney W. Grimes 		cond = CSEND;
6409b50d902SRodney W. Grimes 		break;
6419b50d902SRodney W. Grimes 
6429b50d902SRodney W. Grimes 	default:
6439b50d902SRodney W. Grimes 		printf("Unrecognized if-keyword: \"%s\"\n", cp);
6449b50d902SRodney W. Grimes 		return (1);
6459b50d902SRodney W. Grimes 	}
6469b50d902SRodney W. Grimes 	return (0);
6479b50d902SRodney W. Grimes }
6489b50d902SRodney W. Grimes 
6499b50d902SRodney W. Grimes /*
6509b50d902SRodney W. Grimes  * Implement 'else'.  This is pretty simple -- we just
6519b50d902SRodney W. Grimes  * flip over the conditional flag.
6529b50d902SRodney W. Grimes  */
6539b50d902SRodney W. Grimes int
elsecmd(void * arg __unused)654*d28a9551SJohn Baldwin elsecmd(void *arg __unused)
6559b50d902SRodney W. Grimes {
6569b50d902SRodney W. Grimes 
6579b50d902SRodney W. Grimes 	switch (cond) {
6589b50d902SRodney W. Grimes 	case CANY:
6599b50d902SRodney W. Grimes 		printf("\"Else\" without matching \"if\"\n");
6609b50d902SRodney W. Grimes 		return (1);
6619b50d902SRodney W. Grimes 
6629b50d902SRodney W. Grimes 	case CSEND:
6639b50d902SRodney W. Grimes 		cond = CRCV;
6649b50d902SRodney W. Grimes 		break;
6659b50d902SRodney W. Grimes 
6669b50d902SRodney W. Grimes 	case CRCV:
6679b50d902SRodney W. Grimes 		cond = CSEND;
6689b50d902SRodney W. Grimes 		break;
6699b50d902SRodney W. Grimes 
6709b50d902SRodney W. Grimes 	default:
6719b50d902SRodney W. Grimes 		printf("Mail's idea of conditions is screwed up\n");
6729b50d902SRodney W. Grimes 		cond = CANY;
6739b50d902SRodney W. Grimes 		break;
6749b50d902SRodney W. Grimes 	}
6759b50d902SRodney W. Grimes 	return (0);
6769b50d902SRodney W. Grimes }
6779b50d902SRodney W. Grimes 
6789b50d902SRodney W. Grimes /*
6799b50d902SRodney W. Grimes  * End of if statement.  Just set cond back to anything.
6809b50d902SRodney W. Grimes  */
6819b50d902SRodney W. Grimes int
endifcmd(void * arg __unused)682*d28a9551SJohn Baldwin endifcmd(void *arg __unused)
6839b50d902SRodney W. Grimes {
6849b50d902SRodney W. Grimes 
6859b50d902SRodney W. Grimes 	if (cond == CANY) {
6869b50d902SRodney W. Grimes 		printf("\"Endif\" without matching \"if\"\n");
6879b50d902SRodney W. Grimes 		return (1);
6889b50d902SRodney W. Grimes 	}
6899b50d902SRodney W. Grimes 	cond = CANY;
6909b50d902SRodney W. Grimes 	return (0);
6919b50d902SRodney W. Grimes }
6929b50d902SRodney W. Grimes 
6939b50d902SRodney W. Grimes /*
6949b50d902SRodney W. Grimes  * Set the list of alternate names.
6959b50d902SRodney W. Grimes  */
6969b50d902SRodney W. Grimes int
alternates(void * arg)697*d28a9551SJohn Baldwin alternates(void *arg)
6989b50d902SRodney W. Grimes {
699*d28a9551SJohn Baldwin 	char **namelist = arg;
7009ce73e90SMike Heffner 	int c;
7019ce73e90SMike Heffner 	char **ap, **ap2, *cp;
7029b50d902SRodney W. Grimes 
7039b50d902SRodney W. Grimes 	c = argcount(namelist) + 1;
7049b50d902SRodney W. Grimes 	if (c == 1) {
7059b50d902SRodney W. Grimes 		if (altnames == 0)
7069b50d902SRodney W. Grimes 			return (0);
7079ce73e90SMike Heffner 		for (ap = altnames; *ap != NULL; ap++)
7089b50d902SRodney W. Grimes 			printf("%s ", *ap);
7099b50d902SRodney W. Grimes 		printf("\n");
7109b50d902SRodney W. Grimes 		return (0);
7119b50d902SRodney W. Grimes 	}
7129b50d902SRodney W. Grimes 	if (altnames != 0)
7139ce73e90SMike Heffner 		(void)free(altnames);
714dde0f2bfSPedro F. Giffuni 	if ((altnames = calloc((unsigned)c, sizeof(char *))) == NULL)
715dde0f2bfSPedro F. Giffuni 		err(1, "Out of memory");
7169ce73e90SMike Heffner 	for (ap = namelist, ap2 = altnames; *ap != NULL; ap++, ap2++) {
7179ce73e90SMike Heffner 		cp = calloc((unsigned)strlen(*ap) + 1, sizeof(char));
7189b50d902SRodney W. Grimes 		strcpy(cp, *ap);
7199b50d902SRodney W. Grimes 		*ap2 = cp;
7209b50d902SRodney W. Grimes 	}
7219b50d902SRodney W. Grimes 	*ap2 = 0;
7229b50d902SRodney W. Grimes 	return (0);
7239b50d902SRodney W. Grimes }
724