xref: /freebsd/usr.bin/mail/lex.c (revision 0c3a8314c00f58f16823e8cd6186757d5e8bcdcc)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1980, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
320e7f0658SWarner Losh  *
330e7f0658SWarner Losh  * $FreeBSD$
349b50d902SRodney W. Grimes  */
359b50d902SRodney W. Grimes 
369b50d902SRodney W. Grimes #ifndef lint
370c3a8314SMike Heffner #if 0
389b50d902SRodney W. Grimes static char sccsid[] = "@(#)lex.c	8.1 (Berkeley) 6/6/93";
390c3a8314SMike Heffner #endif
400c3a8314SMike Heffner static const char rcsid[] =
410c3a8314SMike Heffner   "$FreeBSD$";
429b50d902SRodney W. Grimes #endif /* not lint */
439b50d902SRodney W. Grimes 
449b50d902SRodney W. Grimes #include "rcv.h"
459b50d902SRodney W. Grimes #include <errno.h>
469b50d902SRodney W. Grimes #include <fcntl.h>
479b50d902SRodney W. Grimes #include "extern.h"
489b50d902SRodney W. Grimes 
499b50d902SRodney W. Grimes /*
509b50d902SRodney W. Grimes  * Mail -- a mail program
519b50d902SRodney W. Grimes  *
529b50d902SRodney W. Grimes  * Lexical processing of commands.
539b50d902SRodney W. Grimes  */
549b50d902SRodney W. Grimes 
559b50d902SRodney W. Grimes char	*prompt = "& ";
569b50d902SRodney W. Grimes 
579b50d902SRodney W. Grimes /*
589b50d902SRodney W. Grimes  * Set up editing on the given file name.
599b50d902SRodney W. Grimes  * If the first character of name is %, we are considered to be
609b50d902SRodney W. Grimes  * editing the file, otherwise we are reading our mail which has
619b50d902SRodney W. Grimes  * signficance for mbox and so forth.
629b50d902SRodney W. Grimes  */
639b50d902SRodney W. Grimes int
649b50d902SRodney W. Grimes setfile(name)
659b50d902SRodney W. Grimes 	char *name;
669b50d902SRodney W. Grimes {
679b50d902SRodney W. Grimes 	FILE *ibuf;
680c3a8314SMike Heffner 	int i, fd;
699b50d902SRodney W. Grimes 	struct stat stb;
709b50d902SRodney W. Grimes 	char isedit = *name != '%';
719b50d902SRodney W. Grimes 	char *who = name[1] ? name + 1 : myname;
720c3a8314SMike Heffner 	char tempname[PATHSIZE];
739b50d902SRodney W. Grimes 	static int shudclob;
749b50d902SRodney W. Grimes 
759b50d902SRodney W. Grimes 	if ((name = expand(name)) == NOSTR)
769b50d902SRodney W. Grimes 		return -1;
779b50d902SRodney W. Grimes 
789b50d902SRodney W. Grimes 	if ((ibuf = Fopen(name, "r")) == NULL) {
799b50d902SRodney W. Grimes 		if (!isedit && errno == ENOENT)
809b50d902SRodney W. Grimes 			goto nomail;
810c3a8314SMike Heffner 		warn("%s", name);
829b50d902SRodney W. Grimes 		return(-1);
839b50d902SRodney W. Grimes 	}
849b50d902SRodney W. Grimes 
859b50d902SRodney W. Grimes 	if (fstat(fileno(ibuf), &stb) < 0) {
860c3a8314SMike Heffner 		warn("fstat");
879b50d902SRodney W. Grimes 		Fclose(ibuf);
889b50d902SRodney W. Grimes 		return (-1);
899b50d902SRodney W. Grimes 	}
909b50d902SRodney W. Grimes 
910c3a8314SMike Heffner 	if (S_ISDIR(stb.st_mode) || !S_ISREG(stb.st_mode)) {
929b50d902SRodney W. Grimes 		Fclose(ibuf);
930c3a8314SMike Heffner 		errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL;
940c3a8314SMike Heffner 		warn("%s", name);
959b50d902SRodney W. Grimes 		return (-1);
969b50d902SRodney W. Grimes 	}
979b50d902SRodney W. Grimes 
989b50d902SRodney W. Grimes 	/*
999b50d902SRodney W. Grimes 	 * Looks like all will be well.  We must now relinquish our
1009b50d902SRodney W. Grimes 	 * hold on the current set of stuff.  Must hold signals
1019b50d902SRodney W. Grimes 	 * while we are reading the new file, else we will ruin
1029b50d902SRodney W. Grimes 	 * the message[] data structure.
1039b50d902SRodney W. Grimes 	 */
1049b50d902SRodney W. Grimes 
1059b50d902SRodney W. Grimes 	holdsigs();
1069b50d902SRodney W. Grimes 	if (shudclob)
1079b50d902SRodney W. Grimes 		quit();
1089b50d902SRodney W. Grimes 
1099b50d902SRodney W. Grimes 	/*
1109b50d902SRodney W. Grimes 	 * Copy the messages into /tmp
1119b50d902SRodney W. Grimes 	 * and set pointers.
1129b50d902SRodney W. Grimes 	 */
1139b50d902SRodney W. Grimes 
1149b50d902SRodney W. Grimes 	readonly = 0;
1159b50d902SRodney W. Grimes 	if ((i = open(name, 1)) < 0)
1169b50d902SRodney W. Grimes 		readonly++;
1179b50d902SRodney W. Grimes 	else
1189b50d902SRodney W. Grimes 		close(i);
1199b50d902SRodney W. Grimes 	if (shudclob) {
1209b50d902SRodney W. Grimes 		fclose(itf);
1219b50d902SRodney W. Grimes 		fclose(otf);
1229b50d902SRodney W. Grimes 	}
1239b50d902SRodney W. Grimes 	shudclob = 1;
1249b50d902SRodney W. Grimes 	edit = isedit;
1250c3a8314SMike Heffner 	strlcpy(prevfile, mailname, sizeof(prevfile));
1269b50d902SRodney W. Grimes 	if (name != mailname)
1270c3a8314SMike Heffner 		strlcpy(mailname, name, sizeof(mailname));
1289b50d902SRodney W. Grimes 	mailsize = fsize(ibuf);
1290c3a8314SMike Heffner 	snprintf(tempname, sizeof(tempname), "%s/mail.RxXXXXXXXXXX", tmpdir);
1300c3a8314SMike Heffner 	if ((fd = mkstemp(tempname)) == -1 || (otf = fdopen(fd, "w")) == NULL)
1310c3a8314SMike Heffner 		err(1, "%s", tempname);
1329b50d902SRodney W. Grimes 	(void) fcntl(fileno(otf), F_SETFD, 1);
1330c3a8314SMike Heffner 	if ((itf = fopen(tempname, "r")) == NULL)
1340c3a8314SMike Heffner 		err(1, "%s", tempname);
1359b50d902SRodney W. Grimes 	(void) fcntl(fileno(itf), F_SETFD, 1);
1360c3a8314SMike Heffner 	rm(tempname);
1379b50d902SRodney W. Grimes 	setptr(ibuf);
1389b50d902SRodney W. Grimes 	setmsize(msgCount);
1399b50d902SRodney W. Grimes 	Fclose(ibuf);
1409b50d902SRodney W. Grimes 	relsesigs();
1419b50d902SRodney W. Grimes 	sawcom = 0;
1429b50d902SRodney W. Grimes 	if (!edit && msgCount == 0) {
1439b50d902SRodney W. Grimes nomail:
1449b50d902SRodney W. Grimes 		fprintf(stderr, "No mail for %s\n", who);
1459b50d902SRodney W. Grimes 		return -1;
1469b50d902SRodney W. Grimes 	}
1479b50d902SRodney W. Grimes 	return(0);
1489b50d902SRodney W. Grimes }
1499b50d902SRodney W. Grimes 
1509b50d902SRodney W. Grimes int	*msgvec;
1519b50d902SRodney W. Grimes int	reset_on_stop;			/* do a reset() if stopped */
1529b50d902SRodney W. Grimes 
1539b50d902SRodney W. Grimes /*
1549b50d902SRodney W. Grimes  * Interpret user commands one by one.  If standard input is not a tty,
1559b50d902SRodney W. Grimes  * print no prompt.
1569b50d902SRodney W. Grimes  */
1579b50d902SRodney W. Grimes void
1589b50d902SRodney W. Grimes commands()
1599b50d902SRodney W. Grimes {
1609b50d902SRodney W. Grimes 	int eofloop = 0;
1619b50d902SRodney W. Grimes 	register int n;
1629b50d902SRodney W. Grimes 	char linebuf[LINESIZE];
1639b50d902SRodney W. Grimes 	void intr(), stop(), hangup();
1649b50d902SRodney W. Grimes 
1659b50d902SRodney W. Grimes 	if (!sourcing) {
1669b50d902SRodney W. Grimes 		if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1679b50d902SRodney W. Grimes 			signal(SIGINT, intr);
1689b50d902SRodney W. Grimes 		if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1699b50d902SRodney W. Grimes 			signal(SIGHUP, hangup);
1709b50d902SRodney W. Grimes 		signal(SIGTSTP, stop);
1719b50d902SRodney W. Grimes 		signal(SIGTTOU, stop);
1729b50d902SRodney W. Grimes 		signal(SIGTTIN, stop);
1739b50d902SRodney W. Grimes 	}
1749b50d902SRodney W. Grimes 	setexit();
1759b50d902SRodney W. Grimes 	for (;;) {
1769b50d902SRodney W. Grimes 		/*
1779b50d902SRodney W. Grimes 		 * Print the prompt, if needed.  Clear out
1789b50d902SRodney W. Grimes 		 * string space, and flush the output.
1799b50d902SRodney W. Grimes 		 */
1809b50d902SRodney W. Grimes 		if (!sourcing && value("interactive") != NOSTR) {
1819b50d902SRodney W. Grimes 			reset_on_stop = 1;
182c858db96SKris Kennaway 			printf("%s", prompt);
1839b50d902SRodney W. Grimes 		}
1849b50d902SRodney W. Grimes 		fflush(stdout);
1859b50d902SRodney W. Grimes 		sreset();
1869b50d902SRodney W. Grimes 		/*
1879b50d902SRodney W. Grimes 		 * Read a line of commands from the current input
1889b50d902SRodney W. Grimes 		 * and handle end of file specially.
1899b50d902SRodney W. Grimes 		 */
1909b50d902SRodney W. Grimes 		n = 0;
1919b50d902SRodney W. Grimes 		for (;;) {
1929b50d902SRodney W. Grimes 			if (readline(input, &linebuf[n], LINESIZE - n) < 0) {
1939b50d902SRodney W. Grimes 				if (n == 0)
1949b50d902SRodney W. Grimes 					n = -1;
1959b50d902SRodney W. Grimes 				break;
1969b50d902SRodney W. Grimes 			}
1979b50d902SRodney W. Grimes 			if ((n = strlen(linebuf)) == 0)
1989b50d902SRodney W. Grimes 				break;
1999b50d902SRodney W. Grimes 			n--;
2009b50d902SRodney W. Grimes 			if (linebuf[n] != '\\')
2019b50d902SRodney W. Grimes 				break;
2029b50d902SRodney W. Grimes 			linebuf[n++] = ' ';
2039b50d902SRodney W. Grimes 		}
2049b50d902SRodney W. Grimes 		reset_on_stop = 0;
2059b50d902SRodney W. Grimes 		if (n < 0) {
2069b50d902SRodney W. Grimes 				/* eof */
2079b50d902SRodney W. Grimes 			if (loading)
2089b50d902SRodney W. Grimes 				break;
2099b50d902SRodney W. Grimes 			if (sourcing) {
2109b50d902SRodney W. Grimes 				unstack();
2119b50d902SRodney W. Grimes 				continue;
2129b50d902SRodney W. Grimes 			}
2139b50d902SRodney W. Grimes 			if (value("interactive") != NOSTR &&
2149b50d902SRodney W. Grimes 			    value("ignoreeof") != NOSTR &&
2159b50d902SRodney W. Grimes 			    ++eofloop < 25) {
2169b50d902SRodney W. Grimes 				printf("Use \"quit\" to quit.\n");
2179b50d902SRodney W. Grimes 				continue;
2189b50d902SRodney W. Grimes 			}
2199b50d902SRodney W. Grimes 			break;
2209b50d902SRodney W. Grimes 		}
2219b50d902SRodney W. Grimes 		eofloop = 0;
2229b50d902SRodney W. Grimes 		if (execute(linebuf, 0))
2239b50d902SRodney W. Grimes 			break;
2249b50d902SRodney W. Grimes 	}
2259b50d902SRodney W. Grimes }
2269b50d902SRodney W. Grimes 
2279b50d902SRodney W. Grimes /*
2289b50d902SRodney W. Grimes  * Execute a single command.
2299b50d902SRodney W. Grimes  * Command functions return 0 for success, 1 for error, and -1
2309b50d902SRodney W. Grimes  * for abort.  A 1 or -1 aborts a load or source.  A -1 aborts
2319b50d902SRodney W. Grimes  * the interactive command loop.
2329b50d902SRodney W. Grimes  * Contxt is non-zero if called while composing mail.
2339b50d902SRodney W. Grimes  */
2349b50d902SRodney W. Grimes int
2359b50d902SRodney W. Grimes execute(linebuf, contxt)
2369b50d902SRodney W. Grimes 	char linebuf[];
2379b50d902SRodney W. Grimes 	int contxt;
2389b50d902SRodney W. Grimes {
2399b50d902SRodney W. Grimes 	char word[LINESIZE];
2409b50d902SRodney W. Grimes 	char *arglist[MAXARGC];
2419b50d902SRodney W. Grimes 	struct cmd *com;
2429b50d902SRodney W. Grimes 	register char *cp, *cp2;
2439b50d902SRodney W. Grimes 	register int c;
2449b50d902SRodney W. Grimes 	int muvec[2];
2459b50d902SRodney W. Grimes 	int e = 1;
2469b50d902SRodney W. Grimes 
2479b50d902SRodney W. Grimes 	/*
2489b50d902SRodney W. Grimes 	 * Strip the white space away from the beginning
2499b50d902SRodney W. Grimes 	 * of the command, then scan out a word, which
2509b50d902SRodney W. Grimes 	 * consists of anything except digits and white space.
2519b50d902SRodney W. Grimes 	 *
2529b50d902SRodney W. Grimes 	 * Handle ! escapes differently to get the correct
2539b50d902SRodney W. Grimes 	 * lexical conventions.
2549b50d902SRodney W. Grimes 	 */
2559b50d902SRodney W. Grimes 
2569b50d902SRodney W. Grimes 	for (cp = linebuf; isspace(*cp); cp++)
2579b50d902SRodney W. Grimes 		;
2589b50d902SRodney W. Grimes 	if (*cp == '!') {
2599b50d902SRodney W. Grimes 		if (sourcing) {
2609b50d902SRodney W. Grimes 			printf("Can't \"!\" while sourcing\n");
2619b50d902SRodney W. Grimes 			goto out;
2629b50d902SRodney W. Grimes 		}
2639b50d902SRodney W. Grimes 		shell(cp+1);
2649b50d902SRodney W. Grimes 		return(0);
2659b50d902SRodney W. Grimes 	}
2669b50d902SRodney W. Grimes 	cp2 = word;
2670c3a8314SMike Heffner 	while (*cp && strchr(" \t0123456789$^.:/-+*'\"", *cp) == NOSTR)
2689b50d902SRodney W. Grimes 		*cp2++ = *cp++;
2699b50d902SRodney W. Grimes 	*cp2 = '\0';
2709b50d902SRodney W. Grimes 
2719b50d902SRodney W. Grimes 	/*
2729b50d902SRodney W. Grimes 	 * Look up the command; if not found, bitch.
2739b50d902SRodney W. Grimes 	 * Normally, a blank command would map to the
2749b50d902SRodney W. Grimes 	 * first command in the table; while sourcing,
2759b50d902SRodney W. Grimes 	 * however, we ignore blank lines to eliminate
2769b50d902SRodney W. Grimes 	 * confusion.
2779b50d902SRodney W. Grimes 	 */
2789b50d902SRodney W. Grimes 
2799b50d902SRodney W. Grimes 	if (sourcing && *word == '\0')
2809b50d902SRodney W. Grimes 		return(0);
2819b50d902SRodney W. Grimes 	com = lex(word);
2829b50d902SRodney W. Grimes 	if (com == NONE) {
2839b50d902SRodney W. Grimes 		printf("Unknown command: \"%s\"\n", word);
2849b50d902SRodney W. Grimes 		goto out;
2859b50d902SRodney W. Grimes 	}
2869b50d902SRodney W. Grimes 
2879b50d902SRodney W. Grimes 	/*
2889b50d902SRodney W. Grimes 	 * See if we should execute the command -- if a conditional
2899b50d902SRodney W. Grimes 	 * we always execute it, otherwise, check the state of cond.
2909b50d902SRodney W. Grimes 	 */
2919b50d902SRodney W. Grimes 
2929b50d902SRodney W. Grimes 	if ((com->c_argtype & F) == 0)
2939b50d902SRodney W. Grimes 		if (cond == CRCV && !rcvmode || cond == CSEND && rcvmode)
2949b50d902SRodney W. Grimes 			return(0);
2959b50d902SRodney W. Grimes 
2969b50d902SRodney W. Grimes 	/*
2979b50d902SRodney W. Grimes 	 * Process the arguments to the command, depending
2989b50d902SRodney W. Grimes 	 * on the type he expects.  Default to an error.
2999b50d902SRodney W. Grimes 	 * If we are sourcing an interactive command, it's
3009b50d902SRodney W. Grimes 	 * an error.
3019b50d902SRodney W. Grimes 	 */
3029b50d902SRodney W. Grimes 
3039b50d902SRodney W. Grimes 	if (!rcvmode && (com->c_argtype & M) == 0) {
3049b50d902SRodney W. Grimes 		printf("May not execute \"%s\" while sending\n",
3059b50d902SRodney W. Grimes 		    com->c_name);
3069b50d902SRodney W. Grimes 		goto out;
3079b50d902SRodney W. Grimes 	}
3089b50d902SRodney W. Grimes 	if (sourcing && com->c_argtype & I) {
3099b50d902SRodney W. Grimes 		printf("May not execute \"%s\" while sourcing\n",
3109b50d902SRodney W. Grimes 		    com->c_name);
3119b50d902SRodney W. Grimes 		goto out;
3129b50d902SRodney W. Grimes 	}
3139b50d902SRodney W. Grimes 	if (readonly && com->c_argtype & W) {
3149b50d902SRodney W. Grimes 		printf("May not execute \"%s\" -- message file is read only\n",
3159b50d902SRodney W. Grimes 		   com->c_name);
3169b50d902SRodney W. Grimes 		goto out;
3179b50d902SRodney W. Grimes 	}
3189b50d902SRodney W. Grimes 	if (contxt && com->c_argtype & R) {
3199b50d902SRodney W. Grimes 		printf("Cannot recursively invoke \"%s\"\n", com->c_name);
3209b50d902SRodney W. Grimes 		goto out;
3219b50d902SRodney W. Grimes 	}
3229b50d902SRodney W. Grimes 	switch (com->c_argtype & ~(F|P|I|M|T|W|R)) {
3239b50d902SRodney W. Grimes 	case MSGLIST:
3249b50d902SRodney W. Grimes 		/*
3259b50d902SRodney W. Grimes 		 * A message list defaulting to nearest forward
3269b50d902SRodney W. Grimes 		 * legal message.
3279b50d902SRodney W. Grimes 		 */
3289b50d902SRodney W. Grimes 		if (msgvec == 0) {
3299b50d902SRodney W. Grimes 			printf("Illegal use of \"message list\"\n");
3309b50d902SRodney W. Grimes 			break;
3319b50d902SRodney W. Grimes 		}
3329b50d902SRodney W. Grimes 		if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0)
3339b50d902SRodney W. Grimes 			break;
3349b50d902SRodney W. Grimes 		if (c  == 0) {
3359b50d902SRodney W. Grimes 			*msgvec = first(com->c_msgflag,
3369b50d902SRodney W. Grimes 				com->c_msgmask);
337d030d2d2SPoul-Henning Kamp 			msgvec[1] = 0;
3389b50d902SRodney W. Grimes 		}
339d030d2d2SPoul-Henning Kamp 		if (*msgvec == 0) {
3409b50d902SRodney W. Grimes 			printf("No applicable messages\n");
3419b50d902SRodney W. Grimes 			break;
3429b50d902SRodney W. Grimes 		}
3439b50d902SRodney W. Grimes 		e = (*com->c_func)(msgvec);
3449b50d902SRodney W. Grimes 		break;
3459b50d902SRodney W. Grimes 
3469b50d902SRodney W. Grimes 	case NDMLIST:
3479b50d902SRodney W. Grimes 		/*
3489b50d902SRodney W. Grimes 		 * A message list with no defaults, but no error
3499b50d902SRodney W. Grimes 		 * if none exist.
3509b50d902SRodney W. Grimes 		 */
3519b50d902SRodney W. Grimes 		if (msgvec == 0) {
3529b50d902SRodney W. Grimes 			printf("Illegal use of \"message list\"\n");
3539b50d902SRodney W. Grimes 			break;
3549b50d902SRodney W. Grimes 		}
3559b50d902SRodney W. Grimes 		if (getmsglist(cp, msgvec, com->c_msgflag) < 0)
3569b50d902SRodney W. Grimes 			break;
3579b50d902SRodney W. Grimes 		e = (*com->c_func)(msgvec);
3589b50d902SRodney W. Grimes 		break;
3599b50d902SRodney W. Grimes 
3609b50d902SRodney W. Grimes 	case STRLIST:
3619b50d902SRodney W. Grimes 		/*
3629b50d902SRodney W. Grimes 		 * Just the straight string, with
3639b50d902SRodney W. Grimes 		 * leading blanks removed.
3649b50d902SRodney W. Grimes 		 */
3659b50d902SRodney W. Grimes 		while (isspace(*cp))
3669b50d902SRodney W. Grimes 			cp++;
3679b50d902SRodney W. Grimes 		e = (*com->c_func)(cp);
3689b50d902SRodney W. Grimes 		break;
3699b50d902SRodney W. Grimes 
3709b50d902SRodney W. Grimes 	case RAWLIST:
3719b50d902SRodney W. Grimes 		/*
3729b50d902SRodney W. Grimes 		 * A vector of strings, in shell style.
3739b50d902SRodney W. Grimes 		 */
3749b50d902SRodney W. Grimes 		if ((c = getrawlist(cp, arglist,
3759b50d902SRodney W. Grimes 				sizeof arglist / sizeof *arglist)) < 0)
3769b50d902SRodney W. Grimes 			break;
3779b50d902SRodney W. Grimes 		if (c < com->c_minargs) {
3789b50d902SRodney W. Grimes 			printf("%s requires at least %d arg(s)\n",
3799b50d902SRodney W. Grimes 				com->c_name, com->c_minargs);
3809b50d902SRodney W. Grimes 			break;
3819b50d902SRodney W. Grimes 		}
3829b50d902SRodney W. Grimes 		if (c > com->c_maxargs) {
3839b50d902SRodney W. Grimes 			printf("%s takes no more than %d arg(s)\n",
3849b50d902SRodney W. Grimes 				com->c_name, com->c_maxargs);
3859b50d902SRodney W. Grimes 			break;
3869b50d902SRodney W. Grimes 		}
3879b50d902SRodney W. Grimes 		e = (*com->c_func)(arglist);
3889b50d902SRodney W. Grimes 		break;
3899b50d902SRodney W. Grimes 
3909b50d902SRodney W. Grimes 	case NOLIST:
3919b50d902SRodney W. Grimes 		/*
3929b50d902SRodney W. Grimes 		 * Just the constant zero, for exiting,
3939b50d902SRodney W. Grimes 		 * eg.
3949b50d902SRodney W. Grimes 		 */
3959b50d902SRodney W. Grimes 		e = (*com->c_func)(0);
3969b50d902SRodney W. Grimes 		break;
3979b50d902SRodney W. Grimes 
3989b50d902SRodney W. Grimes 	default:
3990c3a8314SMike Heffner 		errx(1, "Unknown argtype");
4009b50d902SRodney W. Grimes 	}
4019b50d902SRodney W. Grimes 
4029b50d902SRodney W. Grimes out:
4039b50d902SRodney W. Grimes 	/*
4049b50d902SRodney W. Grimes 	 * Exit the current source file on
4059b50d902SRodney W. Grimes 	 * error.
4069b50d902SRodney W. Grimes 	 */
4079b50d902SRodney W. Grimes 	if (e) {
4089b50d902SRodney W. Grimes 		if (e < 0)
4099b50d902SRodney W. Grimes 			return 1;
4109b50d902SRodney W. Grimes 		if (loading)
4119b50d902SRodney W. Grimes 			return 1;
4129b50d902SRodney W. Grimes 		if (sourcing)
4139b50d902SRodney W. Grimes 			unstack();
4149b50d902SRodney W. Grimes 		return 0;
4159b50d902SRodney W. Grimes 	}
4169b50d902SRodney W. Grimes 	if (value("autoprint") != NOSTR && com->c_argtype & P)
4179b50d902SRodney W. Grimes 		if ((dot->m_flag & MDELETED) == 0) {
4189b50d902SRodney W. Grimes 			muvec[0] = dot - &message[0] + 1;
4199b50d902SRodney W. Grimes 			muvec[1] = 0;
4209b50d902SRodney W. Grimes 			type(muvec);
4219b50d902SRodney W. Grimes 		}
4229b50d902SRodney W. Grimes 	if (!sourcing && (com->c_argtype & T) == 0)
4239b50d902SRodney W. Grimes 		sawcom = 1;
4249b50d902SRodney W. Grimes 	return(0);
4259b50d902SRodney W. Grimes }
4269b50d902SRodney W. Grimes 
4279b50d902SRodney W. Grimes /*
4289b50d902SRodney W. Grimes  * Set the size of the message vector used to construct argument
4299b50d902SRodney W. Grimes  * lists to message list functions.
4309b50d902SRodney W. Grimes  */
4319b50d902SRodney W. Grimes void
4329b50d902SRodney W. Grimes setmsize(sz)
4339b50d902SRodney W. Grimes 	int sz;
4349b50d902SRodney W. Grimes {
4359b50d902SRodney W. Grimes 
4369b50d902SRodney W. Grimes 	if (msgvec != 0)
4379b50d902SRodney W. Grimes 		free((char *) msgvec);
4389b50d902SRodney W. Grimes 	msgvec = (int *) calloc((unsigned) (sz + 1), sizeof *msgvec);
4399b50d902SRodney W. Grimes }
4409b50d902SRodney W. Grimes 
4419b50d902SRodney W. Grimes /*
4429b50d902SRodney W. Grimes  * Find the correct command in the command table corresponding
4439b50d902SRodney W. Grimes  * to the passed command "word"
4449b50d902SRodney W. Grimes  */
4459b50d902SRodney W. Grimes 
4469b50d902SRodney W. Grimes struct cmd *
4479b50d902SRodney W. Grimes lex(word)
4489b50d902SRodney W. Grimes 	char word[];
4499b50d902SRodney W. Grimes {
4509b50d902SRodney W. Grimes 	register struct cmd *cp;
4519b50d902SRodney W. Grimes 	extern struct cmd cmdtab[];
4529b50d902SRodney W. Grimes 
453b5cfa4b2SJoerg Wunsch 	/*
454b5cfa4b2SJoerg Wunsch 	 * ignore trailing chars after `#'
455b5cfa4b2SJoerg Wunsch 	 *
456b5cfa4b2SJoerg Wunsch 	 * lines with beginning `#' are comments
4570c3a8314SMike Heffner 	 * spaces before `#' are ignored in execute()
458b5cfa4b2SJoerg Wunsch 	 */
459b5cfa4b2SJoerg Wunsch 
460b5cfa4b2SJoerg Wunsch 	if (*word == '#')
461b5cfa4b2SJoerg Wunsch 	    *(word+1) = '\0';
462b5cfa4b2SJoerg Wunsch 
463b5cfa4b2SJoerg Wunsch 
4649b50d902SRodney W. Grimes 	for (cp = &cmdtab[0]; cp->c_name != NOSTR; cp++)
4659b50d902SRodney W. Grimes 		if (isprefix(word, cp->c_name))
4669b50d902SRodney W. Grimes 			return(cp);
4679b50d902SRodney W. Grimes 	return(NONE);
4689b50d902SRodney W. Grimes }
4699b50d902SRodney W. Grimes 
4709b50d902SRodney W. Grimes /*
4719b50d902SRodney W. Grimes  * Determine if as1 is a valid prefix of as2.
4729b50d902SRodney W. Grimes  * Return true if yep.
4739b50d902SRodney W. Grimes  */
4749b50d902SRodney W. Grimes int
4759b50d902SRodney W. Grimes isprefix(as1, as2)
4769b50d902SRodney W. Grimes 	char *as1, *as2;
4779b50d902SRodney W. Grimes {
4789b50d902SRodney W. Grimes 	register char *s1, *s2;
4799b50d902SRodney W. Grimes 
4809b50d902SRodney W. Grimes 	s1 = as1;
4819b50d902SRodney W. Grimes 	s2 = as2;
4829b50d902SRodney W. Grimes 	while (*s1++ == *s2)
4839b50d902SRodney W. Grimes 		if (*s2++ == '\0')
4849b50d902SRodney W. Grimes 			return(1);
4859b50d902SRodney W. Grimes 	return(*--s1 == '\0');
4869b50d902SRodney W. Grimes }
4879b50d902SRodney W. Grimes 
4889b50d902SRodney W. Grimes /*
4899b50d902SRodney W. Grimes  * The following gets called on receipt of an interrupt.  This is
4909b50d902SRodney W. Grimes  * to abort printout of a command, mainly.
4919b50d902SRodney W. Grimes  * Dispatching here when command() is inactive crashes rcv.
4929b50d902SRodney W. Grimes  * Close all open files except 0, 1, 2, and the temporary.
4939b50d902SRodney W. Grimes  * Also, unstack all source files.
4949b50d902SRodney W. Grimes  */
4959b50d902SRodney W. Grimes 
4969b50d902SRodney W. Grimes int	inithdr;			/* am printing startup headers */
4979b50d902SRodney W. Grimes 
4989b50d902SRodney W. Grimes /*ARGSUSED*/
4999b50d902SRodney W. Grimes void
5009b50d902SRodney W. Grimes intr(s)
5019b50d902SRodney W. Grimes 	int s;
5029b50d902SRodney W. Grimes {
5039b50d902SRodney W. Grimes 
5049b50d902SRodney W. Grimes 	noreset = 0;
5059b50d902SRodney W. Grimes 	if (!inithdr)
5069b50d902SRodney W. Grimes 		sawcom++;
5079b50d902SRodney W. Grimes 	inithdr = 0;
5089b50d902SRodney W. Grimes 	while (sourcing)
5099b50d902SRodney W. Grimes 		unstack();
5109b50d902SRodney W. Grimes 
5119b50d902SRodney W. Grimes 	close_all_files();
5129b50d902SRodney W. Grimes 
5139b50d902SRodney W. Grimes 	if (image >= 0) {
5149b50d902SRodney W. Grimes 		close(image);
5159b50d902SRodney W. Grimes 		image = -1;
5169b50d902SRodney W. Grimes 	}
5179b50d902SRodney W. Grimes 	fprintf(stderr, "Interrupt\n");
5189b50d902SRodney W. Grimes 	reset(0);
5199b50d902SRodney W. Grimes }
5209b50d902SRodney W. Grimes 
5219b50d902SRodney W. Grimes /*
5229b50d902SRodney W. Grimes  * When we wake up after ^Z, reprint the prompt.
5239b50d902SRodney W. Grimes  */
5249b50d902SRodney W. Grimes void
5259b50d902SRodney W. Grimes stop(s)
5269b50d902SRodney W. Grimes 	int s;
5279b50d902SRodney W. Grimes {
5289b50d902SRodney W. Grimes 	sig_t old_action = signal(s, SIG_DFL);
5299b50d902SRodney W. Grimes 
5309b50d902SRodney W. Grimes 	sigsetmask(sigblock(0) & ~sigmask(s));
5319b50d902SRodney W. Grimes 	kill(0, s);
5329b50d902SRodney W. Grimes 	sigblock(sigmask(s));
5339b50d902SRodney W. Grimes 	signal(s, old_action);
5349b50d902SRodney W. Grimes 	if (reset_on_stop) {
5359b50d902SRodney W. Grimes 		reset_on_stop = 0;
5369b50d902SRodney W. Grimes 		reset(0);
5379b50d902SRodney W. Grimes 	}
5389b50d902SRodney W. Grimes }
5399b50d902SRodney W. Grimes 
5409b50d902SRodney W. Grimes /*
5419b50d902SRodney W. Grimes  * Branch here on hangup signal and simulate "exit".
5429b50d902SRodney W. Grimes  */
5439b50d902SRodney W. Grimes /*ARGSUSED*/
5449b50d902SRodney W. Grimes void
5459b50d902SRodney W. Grimes hangup(s)
5469b50d902SRodney W. Grimes 	int s;
5479b50d902SRodney W. Grimes {
5489b50d902SRodney W. Grimes 
5499b50d902SRodney W. Grimes 	/* nothing to do? */
5509b50d902SRodney W. Grimes 	exit(1);
5519b50d902SRodney W. Grimes }
5529b50d902SRodney W. Grimes 
5539b50d902SRodney W. Grimes /*
5549b50d902SRodney W. Grimes  * Announce the presence of the current Mail version,
5559b50d902SRodney W. Grimes  * give the message count, and print a header listing.
5569b50d902SRodney W. Grimes  */
5579b50d902SRodney W. Grimes void
5589b50d902SRodney W. Grimes announce()
5599b50d902SRodney W. Grimes {
5609b50d902SRodney W. Grimes 	int vec[2], mdot;
5619b50d902SRodney W. Grimes 
5629b50d902SRodney W. Grimes 	mdot = newfileinfo();
5639b50d902SRodney W. Grimes 	vec[0] = mdot;
5649b50d902SRodney W. Grimes 	vec[1] = 0;
5659b50d902SRodney W. Grimes 	dot = &message[mdot - 1];
5669b50d902SRodney W. Grimes 	if (msgCount > 0 && value("noheader") == NOSTR) {
5679b50d902SRodney W. Grimes 		inithdr++;
5689b50d902SRodney W. Grimes 		headers(vec);
5699b50d902SRodney W. Grimes 		inithdr = 0;
5709b50d902SRodney W. Grimes 	}
5719b50d902SRodney W. Grimes }
5729b50d902SRodney W. Grimes 
5739b50d902SRodney W. Grimes /*
5749b50d902SRodney W. Grimes  * Announce information about the file we are editing.
5759b50d902SRodney W. Grimes  * Return a likely place to set dot.
5769b50d902SRodney W. Grimes  */
5779b50d902SRodney W. Grimes int
5789b50d902SRodney W. Grimes newfileinfo()
5799b50d902SRodney W. Grimes {
5809b50d902SRodney W. Grimes 	register struct message *mp;
5819b50d902SRodney W. Grimes 	register int u, n, mdot, d, s;
5820c3a8314SMike Heffner 	char fname[PATHSIZE+1], zname[PATHSIZE+1], *ename;
5839b50d902SRodney W. Grimes 
5849b50d902SRodney W. Grimes 	for (mp = &message[0]; mp < &message[msgCount]; mp++)
5859b50d902SRodney W. Grimes 		if (mp->m_flag & MNEW)
5869b50d902SRodney W. Grimes 			break;
5879b50d902SRodney W. Grimes 	if (mp >= &message[msgCount])
5889b50d902SRodney W. Grimes 		for (mp = &message[0]; mp < &message[msgCount]; mp++)
5899b50d902SRodney W. Grimes 			if ((mp->m_flag & MREAD) == 0)
5909b50d902SRodney W. Grimes 				break;
5919b50d902SRodney W. Grimes 	if (mp < &message[msgCount])
5929b50d902SRodney W. Grimes 		mdot = mp - &message[0] + 1;
5939b50d902SRodney W. Grimes 	else
5949b50d902SRodney W. Grimes 		mdot = 1;
5959b50d902SRodney W. Grimes 	s = d = 0;
5969b50d902SRodney W. Grimes 	for (mp = &message[0], n = 0, u = 0; mp < &message[msgCount]; mp++) {
5979b50d902SRodney W. Grimes 		if (mp->m_flag & MNEW)
5989b50d902SRodney W. Grimes 			n++;
5999b50d902SRodney W. Grimes 		if ((mp->m_flag & MREAD) == 0)
6009b50d902SRodney W. Grimes 			u++;
6019b50d902SRodney W. Grimes 		if (mp->m_flag & MDELETED)
6029b50d902SRodney W. Grimes 			d++;
6039b50d902SRodney W. Grimes 		if (mp->m_flag & MSAVED)
6049b50d902SRodney W. Grimes 			s++;
6059b50d902SRodney W. Grimes 	}
6069b50d902SRodney W. Grimes 	ename = mailname;
6070c3a8314SMike Heffner 	if (getfold(fname, sizeof(fname) - 1) >= 0) {
6089b50d902SRodney W. Grimes 		strcat(fname, "/");
6099b50d902SRodney W. Grimes 		if (strncmp(fname, mailname, strlen(fname)) == 0) {
6100c3a8314SMike Heffner 			snprintf(zname, sizeof(zname), "+%s", mailname + strlen(fname));
6119b50d902SRodney W. Grimes 			ename = zname;
6129b50d902SRodney W. Grimes 		}
6139b50d902SRodney W. Grimes 	}
6149b50d902SRodney W. Grimes 	printf("\"%s\": ", ename);
6159b50d902SRodney W. Grimes 	if (msgCount == 1)
6169b50d902SRodney W. Grimes 		printf("1 message");
6179b50d902SRodney W. Grimes 	else
6189b50d902SRodney W. Grimes 		printf("%d messages", msgCount);
6199b50d902SRodney W. Grimes 	if (n > 0)
6209b50d902SRodney W. Grimes 		printf(" %d new", n);
6219b50d902SRodney W. Grimes 	if (u-n > 0)
6229b50d902SRodney W. Grimes 		printf(" %d unread", u);
6239b50d902SRodney W. Grimes 	if (d > 0)
6249b50d902SRodney W. Grimes 		printf(" %d deleted", d);
6259b50d902SRodney W. Grimes 	if (s > 0)
6269b50d902SRodney W. Grimes 		printf(" %d saved", s);
6279b50d902SRodney W. Grimes 	if (readonly)
6289b50d902SRodney W. Grimes 		printf(" [Read only]");
6299b50d902SRodney W. Grimes 	printf("\n");
6309b50d902SRodney W. Grimes 	return(mdot);
6319b50d902SRodney W. Grimes }
6329b50d902SRodney W. Grimes 
6339b50d902SRodney W. Grimes /*
6349b50d902SRodney W. Grimes  * Print the current version number.
6359b50d902SRodney W. Grimes  */
6369b50d902SRodney W. Grimes 
6379b50d902SRodney W. Grimes /*ARGSUSED*/
6389b50d902SRodney W. Grimes int
6399b50d902SRodney W. Grimes pversion(e)
6409b50d902SRodney W. Grimes 	int e;
6419b50d902SRodney W. Grimes {
6429b50d902SRodney W. Grimes 	extern char *version;
6439b50d902SRodney W. Grimes 
6449b50d902SRodney W. Grimes 	printf("Version %s\n", version);
6459b50d902SRodney W. Grimes 	return(0);
6469b50d902SRodney W. Grimes }
6479b50d902SRodney W. Grimes 
6489b50d902SRodney W. Grimes /*
6499b50d902SRodney W. Grimes  * Load a file of user definitions.
6509b50d902SRodney W. Grimes  */
6519b50d902SRodney W. Grimes void
6529b50d902SRodney W. Grimes load(name)
6539b50d902SRodney W. Grimes 	char *name;
6549b50d902SRodney W. Grimes {
6559b50d902SRodney W. Grimes 	register FILE *in, *oldin;
6569b50d902SRodney W. Grimes 
6579b50d902SRodney W. Grimes 	if ((in = Fopen(name, "r")) == NULL)
6589b50d902SRodney W. Grimes 		return;
6599b50d902SRodney W. Grimes 	oldin = input;
6609b50d902SRodney W. Grimes 	input = in;
6619b50d902SRodney W. Grimes 	loading = 1;
6629b50d902SRodney W. Grimes 	sourcing = 1;
6639b50d902SRodney W. Grimes 	commands();
6649b50d902SRodney W. Grimes 	loading = 0;
6659b50d902SRodney W. Grimes 	sourcing = 0;
6669b50d902SRodney W. Grimes 	input = oldin;
6679b50d902SRodney W. Grimes 	Fclose(in);
6689b50d902SRodney W. Grimes }
669