xref: /freebsd/usr.bin/mail/lex.c (revision 9b50d9027575220cb6dd09b3e62f03f511e908b8)
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.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
349b50d902SRodney W. Grimes #ifndef lint
359b50d902SRodney W. Grimes static char sccsid[] = "@(#)lex.c	8.1 (Berkeley) 6/6/93";
369b50d902SRodney W. Grimes #endif /* not lint */
379b50d902SRodney W. Grimes 
389b50d902SRodney W. Grimes #include "rcv.h"
399b50d902SRodney W. Grimes #include <errno.h>
409b50d902SRodney W. Grimes #include <fcntl.h>
419b50d902SRodney W. Grimes #include "extern.h"
429b50d902SRodney W. Grimes 
439b50d902SRodney W. Grimes /*
449b50d902SRodney W. Grimes  * Mail -- a mail program
459b50d902SRodney W. Grimes  *
469b50d902SRodney W. Grimes  * Lexical processing of commands.
479b50d902SRodney W. Grimes  */
489b50d902SRodney W. Grimes 
499b50d902SRodney W. Grimes char	*prompt = "& ";
509b50d902SRodney W. Grimes 
519b50d902SRodney W. Grimes /*
529b50d902SRodney W. Grimes  * Set up editing on the given file name.
539b50d902SRodney W. Grimes  * If the first character of name is %, we are considered to be
549b50d902SRodney W. Grimes  * editing the file, otherwise we are reading our mail which has
559b50d902SRodney W. Grimes  * signficance for mbox and so forth.
569b50d902SRodney W. Grimes  */
579b50d902SRodney W. Grimes int
589b50d902SRodney W. Grimes setfile(name)
599b50d902SRodney W. Grimes 	char *name;
609b50d902SRodney W. Grimes {
619b50d902SRodney W. Grimes 	FILE *ibuf;
629b50d902SRodney W. Grimes 	int i;
639b50d902SRodney W. Grimes 	struct stat stb;
649b50d902SRodney W. Grimes 	char isedit = *name != '%';
659b50d902SRodney W. Grimes 	char *who = name[1] ? name + 1 : myname;
669b50d902SRodney W. Grimes 	static int shudclob;
679b50d902SRodney W. Grimes 	extern char tempMesg[];
689b50d902SRodney W. Grimes 	extern int errno;
699b50d902SRodney W. Grimes 
709b50d902SRodney W. Grimes 	if ((name = expand(name)) == NOSTR)
719b50d902SRodney W. Grimes 		return -1;
729b50d902SRodney W. Grimes 
739b50d902SRodney W. Grimes 	if ((ibuf = Fopen(name, "r")) == NULL) {
749b50d902SRodney W. Grimes 		if (!isedit && errno == ENOENT)
759b50d902SRodney W. Grimes 			goto nomail;
769b50d902SRodney W. Grimes 		perror(name);
779b50d902SRodney W. Grimes 		return(-1);
789b50d902SRodney W. Grimes 	}
799b50d902SRodney W. Grimes 
809b50d902SRodney W. Grimes 	if (fstat(fileno(ibuf), &stb) < 0) {
819b50d902SRodney W. Grimes 		perror("fstat");
829b50d902SRodney W. Grimes 		Fclose(ibuf);
839b50d902SRodney W. Grimes 		return (-1);
849b50d902SRodney W. Grimes 	}
859b50d902SRodney W. Grimes 
869b50d902SRodney W. Grimes 	switch (stb.st_mode & S_IFMT) {
879b50d902SRodney W. Grimes 	case S_IFDIR:
889b50d902SRodney W. Grimes 		Fclose(ibuf);
899b50d902SRodney W. Grimes 		errno = EISDIR;
909b50d902SRodney W. Grimes 		perror(name);
919b50d902SRodney W. Grimes 		return (-1);
929b50d902SRodney W. Grimes 
939b50d902SRodney W. Grimes 	case S_IFREG:
949b50d902SRodney W. Grimes 		break;
959b50d902SRodney W. Grimes 
969b50d902SRodney W. Grimes 	default:
979b50d902SRodney W. Grimes 		Fclose(ibuf);
989b50d902SRodney W. Grimes 		errno = EINVAL;
999b50d902SRodney W. Grimes 		perror(name);
1009b50d902SRodney W. Grimes 		return (-1);
1019b50d902SRodney W. Grimes 	}
1029b50d902SRodney W. Grimes 
1039b50d902SRodney W. Grimes 	/*
1049b50d902SRodney W. Grimes 	 * Looks like all will be well.  We must now relinquish our
1059b50d902SRodney W. Grimes 	 * hold on the current set of stuff.  Must hold signals
1069b50d902SRodney W. Grimes 	 * while we are reading the new file, else we will ruin
1079b50d902SRodney W. Grimes 	 * the message[] data structure.
1089b50d902SRodney W. Grimes 	 */
1099b50d902SRodney W. Grimes 
1109b50d902SRodney W. Grimes 	holdsigs();
1119b50d902SRodney W. Grimes 	if (shudclob)
1129b50d902SRodney W. Grimes 		quit();
1139b50d902SRodney W. Grimes 
1149b50d902SRodney W. Grimes 	/*
1159b50d902SRodney W. Grimes 	 * Copy the messages into /tmp
1169b50d902SRodney W. Grimes 	 * and set pointers.
1179b50d902SRodney W. Grimes 	 */
1189b50d902SRodney W. Grimes 
1199b50d902SRodney W. Grimes 	readonly = 0;
1209b50d902SRodney W. Grimes 	if ((i = open(name, 1)) < 0)
1219b50d902SRodney W. Grimes 		readonly++;
1229b50d902SRodney W. Grimes 	else
1239b50d902SRodney W. Grimes 		close(i);
1249b50d902SRodney W. Grimes 	if (shudclob) {
1259b50d902SRodney W. Grimes 		fclose(itf);
1269b50d902SRodney W. Grimes 		fclose(otf);
1279b50d902SRodney W. Grimes 	}
1289b50d902SRodney W. Grimes 	shudclob = 1;
1299b50d902SRodney W. Grimes 	edit = isedit;
1309b50d902SRodney W. Grimes 	strcpy(prevfile, mailname);
1319b50d902SRodney W. Grimes 	if (name != mailname)
1329b50d902SRodney W. Grimes 		strcpy(mailname, name);
1339b50d902SRodney W. Grimes 	mailsize = fsize(ibuf);
1349b50d902SRodney W. Grimes 	if ((otf = fopen(tempMesg, "w")) == NULL) {
1359b50d902SRodney W. Grimes 		perror(tempMesg);
1369b50d902SRodney W. Grimes 		exit(1);
1379b50d902SRodney W. Grimes 	}
1389b50d902SRodney W. Grimes 	(void) fcntl(fileno(otf), F_SETFD, 1);
1399b50d902SRodney W. Grimes 	if ((itf = fopen(tempMesg, "r")) == NULL) {
1409b50d902SRodney W. Grimes 		perror(tempMesg);
1419b50d902SRodney W. Grimes 		exit(1);
1429b50d902SRodney W. Grimes 	}
1439b50d902SRodney W. Grimes 	(void) fcntl(fileno(itf), F_SETFD, 1);
1449b50d902SRodney W. Grimes 	rm(tempMesg);
1459b50d902SRodney W. Grimes 	setptr(ibuf);
1469b50d902SRodney W. Grimes 	setmsize(msgCount);
1479b50d902SRodney W. Grimes 	Fclose(ibuf);
1489b50d902SRodney W. Grimes 	relsesigs();
1499b50d902SRodney W. Grimes 	sawcom = 0;
1509b50d902SRodney W. Grimes 	if (!edit && msgCount == 0) {
1519b50d902SRodney W. Grimes nomail:
1529b50d902SRodney W. Grimes 		fprintf(stderr, "No mail for %s\n", who);
1539b50d902SRodney W. Grimes 		return -1;
1549b50d902SRodney W. Grimes 	}
1559b50d902SRodney W. Grimes 	return(0);
1569b50d902SRodney W. Grimes }
1579b50d902SRodney W. Grimes 
1589b50d902SRodney W. Grimes int	*msgvec;
1599b50d902SRodney W. Grimes int	reset_on_stop;			/* do a reset() if stopped */
1609b50d902SRodney W. Grimes 
1619b50d902SRodney W. Grimes /*
1629b50d902SRodney W. Grimes  * Interpret user commands one by one.  If standard input is not a tty,
1639b50d902SRodney W. Grimes  * print no prompt.
1649b50d902SRodney W. Grimes  */
1659b50d902SRodney W. Grimes void
1669b50d902SRodney W. Grimes commands()
1679b50d902SRodney W. Grimes {
1689b50d902SRodney W. Grimes 	int eofloop = 0;
1699b50d902SRodney W. Grimes 	register int n;
1709b50d902SRodney W. Grimes 	char linebuf[LINESIZE];
1719b50d902SRodney W. Grimes 	void intr(), stop(), hangup();
1729b50d902SRodney W. Grimes 
1739b50d902SRodney W. Grimes 	if (!sourcing) {
1749b50d902SRodney W. Grimes 		if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1759b50d902SRodney W. Grimes 			signal(SIGINT, intr);
1769b50d902SRodney W. Grimes 		if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1779b50d902SRodney W. Grimes 			signal(SIGHUP, hangup);
1789b50d902SRodney W. Grimes 		signal(SIGTSTP, stop);
1799b50d902SRodney W. Grimes 		signal(SIGTTOU, stop);
1809b50d902SRodney W. Grimes 		signal(SIGTTIN, stop);
1819b50d902SRodney W. Grimes 	}
1829b50d902SRodney W. Grimes 	setexit();
1839b50d902SRodney W. Grimes 	for (;;) {
1849b50d902SRodney W. Grimes 		/*
1859b50d902SRodney W. Grimes 		 * Print the prompt, if needed.  Clear out
1869b50d902SRodney W. Grimes 		 * string space, and flush the output.
1879b50d902SRodney W. Grimes 		 */
1889b50d902SRodney W. Grimes 		if (!sourcing && value("interactive") != NOSTR) {
1899b50d902SRodney W. Grimes 			reset_on_stop = 1;
1909b50d902SRodney W. Grimes 			printf(prompt);
1919b50d902SRodney W. Grimes 		}
1929b50d902SRodney W. Grimes 		fflush(stdout);
1939b50d902SRodney W. Grimes 		sreset();
1949b50d902SRodney W. Grimes 		/*
1959b50d902SRodney W. Grimes 		 * Read a line of commands from the current input
1969b50d902SRodney W. Grimes 		 * and handle end of file specially.
1979b50d902SRodney W. Grimes 		 */
1989b50d902SRodney W. Grimes 		n = 0;
1999b50d902SRodney W. Grimes 		for (;;) {
2009b50d902SRodney W. Grimes 			if (readline(input, &linebuf[n], LINESIZE - n) < 0) {
2019b50d902SRodney W. Grimes 				if (n == 0)
2029b50d902SRodney W. Grimes 					n = -1;
2039b50d902SRodney W. Grimes 				break;
2049b50d902SRodney W. Grimes 			}
2059b50d902SRodney W. Grimes 			if ((n = strlen(linebuf)) == 0)
2069b50d902SRodney W. Grimes 				break;
2079b50d902SRodney W. Grimes 			n--;
2089b50d902SRodney W. Grimes 			if (linebuf[n] != '\\')
2099b50d902SRodney W. Grimes 				break;
2109b50d902SRodney W. Grimes 			linebuf[n++] = ' ';
2119b50d902SRodney W. Grimes 		}
2129b50d902SRodney W. Grimes 		reset_on_stop = 0;
2139b50d902SRodney W. Grimes 		if (n < 0) {
2149b50d902SRodney W. Grimes 				/* eof */
2159b50d902SRodney W. Grimes 			if (loading)
2169b50d902SRodney W. Grimes 				break;
2179b50d902SRodney W. Grimes 			if (sourcing) {
2189b50d902SRodney W. Grimes 				unstack();
2199b50d902SRodney W. Grimes 				continue;
2209b50d902SRodney W. Grimes 			}
2219b50d902SRodney W. Grimes 			if (value("interactive") != NOSTR &&
2229b50d902SRodney W. Grimes 			    value("ignoreeof") != NOSTR &&
2239b50d902SRodney W. Grimes 			    ++eofloop < 25) {
2249b50d902SRodney W. Grimes 				printf("Use \"quit\" to quit.\n");
2259b50d902SRodney W. Grimes 				continue;
2269b50d902SRodney W. Grimes 			}
2279b50d902SRodney W. Grimes 			break;
2289b50d902SRodney W. Grimes 		}
2299b50d902SRodney W. Grimes 		eofloop = 0;
2309b50d902SRodney W. Grimes 		if (execute(linebuf, 0))
2319b50d902SRodney W. Grimes 			break;
2329b50d902SRodney W. Grimes 	}
2339b50d902SRodney W. Grimes }
2349b50d902SRodney W. Grimes 
2359b50d902SRodney W. Grimes /*
2369b50d902SRodney W. Grimes  * Execute a single command.
2379b50d902SRodney W. Grimes  * Command functions return 0 for success, 1 for error, and -1
2389b50d902SRodney W. Grimes  * for abort.  A 1 or -1 aborts a load or source.  A -1 aborts
2399b50d902SRodney W. Grimes  * the interactive command loop.
2409b50d902SRodney W. Grimes  * Contxt is non-zero if called while composing mail.
2419b50d902SRodney W. Grimes  */
2429b50d902SRodney W. Grimes int
2439b50d902SRodney W. Grimes execute(linebuf, contxt)
2449b50d902SRodney W. Grimes 	char linebuf[];
2459b50d902SRodney W. Grimes 	int contxt;
2469b50d902SRodney W. Grimes {
2479b50d902SRodney W. Grimes 	char word[LINESIZE];
2489b50d902SRodney W. Grimes 	char *arglist[MAXARGC];
2499b50d902SRodney W. Grimes 	struct cmd *com;
2509b50d902SRodney W. Grimes 	register char *cp, *cp2;
2519b50d902SRodney W. Grimes 	register int c;
2529b50d902SRodney W. Grimes 	int muvec[2];
2539b50d902SRodney W. Grimes 	int e = 1;
2549b50d902SRodney W. Grimes 
2559b50d902SRodney W. Grimes 	/*
2569b50d902SRodney W. Grimes 	 * Strip the white space away from the beginning
2579b50d902SRodney W. Grimes 	 * of the command, then scan out a word, which
2589b50d902SRodney W. Grimes 	 * consists of anything except digits and white space.
2599b50d902SRodney W. Grimes 	 *
2609b50d902SRodney W. Grimes 	 * Handle ! escapes differently to get the correct
2619b50d902SRodney W. Grimes 	 * lexical conventions.
2629b50d902SRodney W. Grimes 	 */
2639b50d902SRodney W. Grimes 
2649b50d902SRodney W. Grimes 	for (cp = linebuf; isspace(*cp); cp++)
2659b50d902SRodney W. Grimes 		;
2669b50d902SRodney W. Grimes 	if (*cp == '!') {
2679b50d902SRodney W. Grimes 		if (sourcing) {
2689b50d902SRodney W. Grimes 			printf("Can't \"!\" while sourcing\n");
2699b50d902SRodney W. Grimes 			goto out;
2709b50d902SRodney W. Grimes 		}
2719b50d902SRodney W. Grimes 		shell(cp+1);
2729b50d902SRodney W. Grimes 		return(0);
2739b50d902SRodney W. Grimes 	}
2749b50d902SRodney W. Grimes 	cp2 = word;
2759b50d902SRodney W. Grimes 	while (*cp && index(" \t0123456789$^.:/-+*'\"", *cp) == NOSTR)
2769b50d902SRodney W. Grimes 		*cp2++ = *cp++;
2779b50d902SRodney W. Grimes 	*cp2 = '\0';
2789b50d902SRodney W. Grimes 
2799b50d902SRodney W. Grimes 	/*
2809b50d902SRodney W. Grimes 	 * Look up the command; if not found, bitch.
2819b50d902SRodney W. Grimes 	 * Normally, a blank command would map to the
2829b50d902SRodney W. Grimes 	 * first command in the table; while sourcing,
2839b50d902SRodney W. Grimes 	 * however, we ignore blank lines to eliminate
2849b50d902SRodney W. Grimes 	 * confusion.
2859b50d902SRodney W. Grimes 	 */
2869b50d902SRodney W. Grimes 
2879b50d902SRodney W. Grimes 	if (sourcing && *word == '\0')
2889b50d902SRodney W. Grimes 		return(0);
2899b50d902SRodney W. Grimes 	com = lex(word);
2909b50d902SRodney W. Grimes 	if (com == NONE) {
2919b50d902SRodney W. Grimes 		printf("Unknown command: \"%s\"\n", word);
2929b50d902SRodney W. Grimes 		goto out;
2939b50d902SRodney W. Grimes 	}
2949b50d902SRodney W. Grimes 
2959b50d902SRodney W. Grimes 	/*
2969b50d902SRodney W. Grimes 	 * See if we should execute the command -- if a conditional
2979b50d902SRodney W. Grimes 	 * we always execute it, otherwise, check the state of cond.
2989b50d902SRodney W. Grimes 	 */
2999b50d902SRodney W. Grimes 
3009b50d902SRodney W. Grimes 	if ((com->c_argtype & F) == 0)
3019b50d902SRodney W. Grimes 		if (cond == CRCV && !rcvmode || cond == CSEND && rcvmode)
3029b50d902SRodney W. Grimes 			return(0);
3039b50d902SRodney W. Grimes 
3049b50d902SRodney W. Grimes 	/*
3059b50d902SRodney W. Grimes 	 * Process the arguments to the command, depending
3069b50d902SRodney W. Grimes 	 * on the type he expects.  Default to an error.
3079b50d902SRodney W. Grimes 	 * If we are sourcing an interactive command, it's
3089b50d902SRodney W. Grimes 	 * an error.
3099b50d902SRodney W. Grimes 	 */
3109b50d902SRodney W. Grimes 
3119b50d902SRodney W. Grimes 	if (!rcvmode && (com->c_argtype & M) == 0) {
3129b50d902SRodney W. Grimes 		printf("May not execute \"%s\" while sending\n",
3139b50d902SRodney W. Grimes 		    com->c_name);
3149b50d902SRodney W. Grimes 		goto out;
3159b50d902SRodney W. Grimes 	}
3169b50d902SRodney W. Grimes 	if (sourcing && com->c_argtype & I) {
3179b50d902SRodney W. Grimes 		printf("May not execute \"%s\" while sourcing\n",
3189b50d902SRodney W. Grimes 		    com->c_name);
3199b50d902SRodney W. Grimes 		goto out;
3209b50d902SRodney W. Grimes 	}
3219b50d902SRodney W. Grimes 	if (readonly && com->c_argtype & W) {
3229b50d902SRodney W. Grimes 		printf("May not execute \"%s\" -- message file is read only\n",
3239b50d902SRodney W. Grimes 		   com->c_name);
3249b50d902SRodney W. Grimes 		goto out;
3259b50d902SRodney W. Grimes 	}
3269b50d902SRodney W. Grimes 	if (contxt && com->c_argtype & R) {
3279b50d902SRodney W. Grimes 		printf("Cannot recursively invoke \"%s\"\n", com->c_name);
3289b50d902SRodney W. Grimes 		goto out;
3299b50d902SRodney W. Grimes 	}
3309b50d902SRodney W. Grimes 	switch (com->c_argtype & ~(F|P|I|M|T|W|R)) {
3319b50d902SRodney W. Grimes 	case MSGLIST:
3329b50d902SRodney W. Grimes 		/*
3339b50d902SRodney W. Grimes 		 * A message list defaulting to nearest forward
3349b50d902SRodney W. Grimes 		 * legal message.
3359b50d902SRodney W. Grimes 		 */
3369b50d902SRodney W. Grimes 		if (msgvec == 0) {
3379b50d902SRodney W. Grimes 			printf("Illegal use of \"message list\"\n");
3389b50d902SRodney W. Grimes 			break;
3399b50d902SRodney W. Grimes 		}
3409b50d902SRodney W. Grimes 		if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0)
3419b50d902SRodney W. Grimes 			break;
3429b50d902SRodney W. Grimes 		if (c  == 0) {
3439b50d902SRodney W. Grimes 			*msgvec = first(com->c_msgflag,
3449b50d902SRodney W. Grimes 				com->c_msgmask);
3459b50d902SRodney W. Grimes 			msgvec[1] = NULL;
3469b50d902SRodney W. Grimes 		}
3479b50d902SRodney W. Grimes 		if (*msgvec == NULL) {
3489b50d902SRodney W. Grimes 			printf("No applicable messages\n");
3499b50d902SRodney W. Grimes 			break;
3509b50d902SRodney W. Grimes 		}
3519b50d902SRodney W. Grimes 		e = (*com->c_func)(msgvec);
3529b50d902SRodney W. Grimes 		break;
3539b50d902SRodney W. Grimes 
3549b50d902SRodney W. Grimes 	case NDMLIST:
3559b50d902SRodney W. Grimes 		/*
3569b50d902SRodney W. Grimes 		 * A message list with no defaults, but no error
3579b50d902SRodney W. Grimes 		 * if none exist.
3589b50d902SRodney W. Grimes 		 */
3599b50d902SRodney W. Grimes 		if (msgvec == 0) {
3609b50d902SRodney W. Grimes 			printf("Illegal use of \"message list\"\n");
3619b50d902SRodney W. Grimes 			break;
3629b50d902SRodney W. Grimes 		}
3639b50d902SRodney W. Grimes 		if (getmsglist(cp, msgvec, com->c_msgflag) < 0)
3649b50d902SRodney W. Grimes 			break;
3659b50d902SRodney W. Grimes 		e = (*com->c_func)(msgvec);
3669b50d902SRodney W. Grimes 		break;
3679b50d902SRodney W. Grimes 
3689b50d902SRodney W. Grimes 	case STRLIST:
3699b50d902SRodney W. Grimes 		/*
3709b50d902SRodney W. Grimes 		 * Just the straight string, with
3719b50d902SRodney W. Grimes 		 * leading blanks removed.
3729b50d902SRodney W. Grimes 		 */
3739b50d902SRodney W. Grimes 		while (isspace(*cp))
3749b50d902SRodney W. Grimes 			cp++;
3759b50d902SRodney W. Grimes 		e = (*com->c_func)(cp);
3769b50d902SRodney W. Grimes 		break;
3779b50d902SRodney W. Grimes 
3789b50d902SRodney W. Grimes 	case RAWLIST:
3799b50d902SRodney W. Grimes 		/*
3809b50d902SRodney W. Grimes 		 * A vector of strings, in shell style.
3819b50d902SRodney W. Grimes 		 */
3829b50d902SRodney W. Grimes 		if ((c = getrawlist(cp, arglist,
3839b50d902SRodney W. Grimes 				sizeof arglist / sizeof *arglist)) < 0)
3849b50d902SRodney W. Grimes 			break;
3859b50d902SRodney W. Grimes 		if (c < com->c_minargs) {
3869b50d902SRodney W. Grimes 			printf("%s requires at least %d arg(s)\n",
3879b50d902SRodney W. Grimes 				com->c_name, com->c_minargs);
3889b50d902SRodney W. Grimes 			break;
3899b50d902SRodney W. Grimes 		}
3909b50d902SRodney W. Grimes 		if (c > com->c_maxargs) {
3919b50d902SRodney W. Grimes 			printf("%s takes no more than %d arg(s)\n",
3929b50d902SRodney W. Grimes 				com->c_name, com->c_maxargs);
3939b50d902SRodney W. Grimes 			break;
3949b50d902SRodney W. Grimes 		}
3959b50d902SRodney W. Grimes 		e = (*com->c_func)(arglist);
3969b50d902SRodney W. Grimes 		break;
3979b50d902SRodney W. Grimes 
3989b50d902SRodney W. Grimes 	case NOLIST:
3999b50d902SRodney W. Grimes 		/*
4009b50d902SRodney W. Grimes 		 * Just the constant zero, for exiting,
4019b50d902SRodney W. Grimes 		 * eg.
4029b50d902SRodney W. Grimes 		 */
4039b50d902SRodney W. Grimes 		e = (*com->c_func)(0);
4049b50d902SRodney W. Grimes 		break;
4059b50d902SRodney W. Grimes 
4069b50d902SRodney W. Grimes 	default:
4079b50d902SRodney W. Grimes 		panic("Unknown argtype");
4089b50d902SRodney W. Grimes 	}
4099b50d902SRodney W. Grimes 
4109b50d902SRodney W. Grimes out:
4119b50d902SRodney W. Grimes 	/*
4129b50d902SRodney W. Grimes 	 * Exit the current source file on
4139b50d902SRodney W. Grimes 	 * error.
4149b50d902SRodney W. Grimes 	 */
4159b50d902SRodney W. Grimes 	if (e) {
4169b50d902SRodney W. Grimes 		if (e < 0)
4179b50d902SRodney W. Grimes 			return 1;
4189b50d902SRodney W. Grimes 		if (loading)
4199b50d902SRodney W. Grimes 			return 1;
4209b50d902SRodney W. Grimes 		if (sourcing)
4219b50d902SRodney W. Grimes 			unstack();
4229b50d902SRodney W. Grimes 		return 0;
4239b50d902SRodney W. Grimes 	}
4249b50d902SRodney W. Grimes 	if (value("autoprint") != NOSTR && com->c_argtype & P)
4259b50d902SRodney W. Grimes 		if ((dot->m_flag & MDELETED) == 0) {
4269b50d902SRodney W. Grimes 			muvec[0] = dot - &message[0] + 1;
4279b50d902SRodney W. Grimes 			muvec[1] = 0;
4289b50d902SRodney W. Grimes 			type(muvec);
4299b50d902SRodney W. Grimes 		}
4309b50d902SRodney W. Grimes 	if (!sourcing && (com->c_argtype & T) == 0)
4319b50d902SRodney W. Grimes 		sawcom = 1;
4329b50d902SRodney W. Grimes 	return(0);
4339b50d902SRodney W. Grimes }
4349b50d902SRodney W. Grimes 
4359b50d902SRodney W. Grimes /*
4369b50d902SRodney W. Grimes  * Set the size of the message vector used to construct argument
4379b50d902SRodney W. Grimes  * lists to message list functions.
4389b50d902SRodney W. Grimes  */
4399b50d902SRodney W. Grimes void
4409b50d902SRodney W. Grimes setmsize(sz)
4419b50d902SRodney W. Grimes 	int sz;
4429b50d902SRodney W. Grimes {
4439b50d902SRodney W. Grimes 
4449b50d902SRodney W. Grimes 	if (msgvec != 0)
4459b50d902SRodney W. Grimes 		free((char *) msgvec);
4469b50d902SRodney W. Grimes 	msgvec = (int *) calloc((unsigned) (sz + 1), sizeof *msgvec);
4479b50d902SRodney W. Grimes }
4489b50d902SRodney W. Grimes 
4499b50d902SRodney W. Grimes /*
4509b50d902SRodney W. Grimes  * Find the correct command in the command table corresponding
4519b50d902SRodney W. Grimes  * to the passed command "word"
4529b50d902SRodney W. Grimes  */
4539b50d902SRodney W. Grimes 
4549b50d902SRodney W. Grimes struct cmd *
4559b50d902SRodney W. Grimes lex(word)
4569b50d902SRodney W. Grimes 	char word[];
4579b50d902SRodney W. Grimes {
4589b50d902SRodney W. Grimes 	register struct cmd *cp;
4599b50d902SRodney W. Grimes 	extern struct cmd cmdtab[];
4609b50d902SRodney W. Grimes 
4619b50d902SRodney W. Grimes 	for (cp = &cmdtab[0]; cp->c_name != NOSTR; cp++)
4629b50d902SRodney W. Grimes 		if (isprefix(word, cp->c_name))
4639b50d902SRodney W. Grimes 			return(cp);
4649b50d902SRodney W. Grimes 	return(NONE);
4659b50d902SRodney W. Grimes }
4669b50d902SRodney W. Grimes 
4679b50d902SRodney W. Grimes /*
4689b50d902SRodney W. Grimes  * Determine if as1 is a valid prefix of as2.
4699b50d902SRodney W. Grimes  * Return true if yep.
4709b50d902SRodney W. Grimes  */
4719b50d902SRodney W. Grimes int
4729b50d902SRodney W. Grimes isprefix(as1, as2)
4739b50d902SRodney W. Grimes 	char *as1, *as2;
4749b50d902SRodney W. Grimes {
4759b50d902SRodney W. Grimes 	register char *s1, *s2;
4769b50d902SRodney W. Grimes 
4779b50d902SRodney W. Grimes 	s1 = as1;
4789b50d902SRodney W. Grimes 	s2 = as2;
4799b50d902SRodney W. Grimes 	while (*s1++ == *s2)
4809b50d902SRodney W. Grimes 		if (*s2++ == '\0')
4819b50d902SRodney W. Grimes 			return(1);
4829b50d902SRodney W. Grimes 	return(*--s1 == '\0');
4839b50d902SRodney W. Grimes }
4849b50d902SRodney W. Grimes 
4859b50d902SRodney W. Grimes /*
4869b50d902SRodney W. Grimes  * The following gets called on receipt of an interrupt.  This is
4879b50d902SRodney W. Grimes  * to abort printout of a command, mainly.
4889b50d902SRodney W. Grimes  * Dispatching here when command() is inactive crashes rcv.
4899b50d902SRodney W. Grimes  * Close all open files except 0, 1, 2, and the temporary.
4909b50d902SRodney W. Grimes  * Also, unstack all source files.
4919b50d902SRodney W. Grimes  */
4929b50d902SRodney W. Grimes 
4939b50d902SRodney W. Grimes int	inithdr;			/* am printing startup headers */
4949b50d902SRodney W. Grimes 
4959b50d902SRodney W. Grimes /*ARGSUSED*/
4969b50d902SRodney W. Grimes void
4979b50d902SRodney W. Grimes intr(s)
4989b50d902SRodney W. Grimes 	int s;
4999b50d902SRodney W. Grimes {
5009b50d902SRodney W. Grimes 
5019b50d902SRodney W. Grimes 	noreset = 0;
5029b50d902SRodney W. Grimes 	if (!inithdr)
5039b50d902SRodney W. Grimes 		sawcom++;
5049b50d902SRodney W. Grimes 	inithdr = 0;
5059b50d902SRodney W. Grimes 	while (sourcing)
5069b50d902SRodney W. Grimes 		unstack();
5079b50d902SRodney W. Grimes 
5089b50d902SRodney W. Grimes 	close_all_files();
5099b50d902SRodney W. Grimes 
5109b50d902SRodney W. Grimes 	if (image >= 0) {
5119b50d902SRodney W. Grimes 		close(image);
5129b50d902SRodney W. Grimes 		image = -1;
5139b50d902SRodney W. Grimes 	}
5149b50d902SRodney W. Grimes 	fprintf(stderr, "Interrupt\n");
5159b50d902SRodney W. Grimes 	reset(0);
5169b50d902SRodney W. Grimes }
5179b50d902SRodney W. Grimes 
5189b50d902SRodney W. Grimes /*
5199b50d902SRodney W. Grimes  * When we wake up after ^Z, reprint the prompt.
5209b50d902SRodney W. Grimes  */
5219b50d902SRodney W. Grimes void
5229b50d902SRodney W. Grimes stop(s)
5239b50d902SRodney W. Grimes 	int s;
5249b50d902SRodney W. Grimes {
5259b50d902SRodney W. Grimes 	sig_t old_action = signal(s, SIG_DFL);
5269b50d902SRodney W. Grimes 
5279b50d902SRodney W. Grimes 	sigsetmask(sigblock(0) & ~sigmask(s));
5289b50d902SRodney W. Grimes 	kill(0, s);
5299b50d902SRodney W. Grimes 	sigblock(sigmask(s));
5309b50d902SRodney W. Grimes 	signal(s, old_action);
5319b50d902SRodney W. Grimes 	if (reset_on_stop) {
5329b50d902SRodney W. Grimes 		reset_on_stop = 0;
5339b50d902SRodney W. Grimes 		reset(0);
5349b50d902SRodney W. Grimes 	}
5359b50d902SRodney W. Grimes }
5369b50d902SRodney W. Grimes 
5379b50d902SRodney W. Grimes /*
5389b50d902SRodney W. Grimes  * Branch here on hangup signal and simulate "exit".
5399b50d902SRodney W. Grimes  */
5409b50d902SRodney W. Grimes /*ARGSUSED*/
5419b50d902SRodney W. Grimes void
5429b50d902SRodney W. Grimes hangup(s)
5439b50d902SRodney W. Grimes 	int s;
5449b50d902SRodney W. Grimes {
5459b50d902SRodney W. Grimes 
5469b50d902SRodney W. Grimes 	/* nothing to do? */
5479b50d902SRodney W. Grimes 	exit(1);
5489b50d902SRodney W. Grimes }
5499b50d902SRodney W. Grimes 
5509b50d902SRodney W. Grimes /*
5519b50d902SRodney W. Grimes  * Announce the presence of the current Mail version,
5529b50d902SRodney W. Grimes  * give the message count, and print a header listing.
5539b50d902SRodney W. Grimes  */
5549b50d902SRodney W. Grimes void
5559b50d902SRodney W. Grimes announce()
5569b50d902SRodney W. Grimes {
5579b50d902SRodney W. Grimes 	int vec[2], mdot;
5589b50d902SRodney W. Grimes 
5599b50d902SRodney W. Grimes 	mdot = newfileinfo();
5609b50d902SRodney W. Grimes 	vec[0] = mdot;
5619b50d902SRodney W. Grimes 	vec[1] = 0;
5629b50d902SRodney W. Grimes 	dot = &message[mdot - 1];
5639b50d902SRodney W. Grimes 	if (msgCount > 0 && value("noheader") == NOSTR) {
5649b50d902SRodney W. Grimes 		inithdr++;
5659b50d902SRodney W. Grimes 		headers(vec);
5669b50d902SRodney W. Grimes 		inithdr = 0;
5679b50d902SRodney W. Grimes 	}
5689b50d902SRodney W. Grimes }
5699b50d902SRodney W. Grimes 
5709b50d902SRodney W. Grimes /*
5719b50d902SRodney W. Grimes  * Announce information about the file we are editing.
5729b50d902SRodney W. Grimes  * Return a likely place to set dot.
5739b50d902SRodney W. Grimes  */
5749b50d902SRodney W. Grimes int
5759b50d902SRodney W. Grimes newfileinfo()
5769b50d902SRodney W. Grimes {
5779b50d902SRodney W. Grimes 	register struct message *mp;
5789b50d902SRodney W. Grimes 	register int u, n, mdot, d, s;
5799b50d902SRodney W. Grimes 	char fname[BUFSIZ], zname[BUFSIZ], *ename;
5809b50d902SRodney W. Grimes 
5819b50d902SRodney W. Grimes 	for (mp = &message[0]; mp < &message[msgCount]; mp++)
5829b50d902SRodney W. Grimes 		if (mp->m_flag & MNEW)
5839b50d902SRodney W. Grimes 			break;
5849b50d902SRodney W. Grimes 	if (mp >= &message[msgCount])
5859b50d902SRodney W. Grimes 		for (mp = &message[0]; mp < &message[msgCount]; mp++)
5869b50d902SRodney W. Grimes 			if ((mp->m_flag & MREAD) == 0)
5879b50d902SRodney W. Grimes 				break;
5889b50d902SRodney W. Grimes 	if (mp < &message[msgCount])
5899b50d902SRodney W. Grimes 		mdot = mp - &message[0] + 1;
5909b50d902SRodney W. Grimes 	else
5919b50d902SRodney W. Grimes 		mdot = 1;
5929b50d902SRodney W. Grimes 	s = d = 0;
5939b50d902SRodney W. Grimes 	for (mp = &message[0], n = 0, u = 0; mp < &message[msgCount]; mp++) {
5949b50d902SRodney W. Grimes 		if (mp->m_flag & MNEW)
5959b50d902SRodney W. Grimes 			n++;
5969b50d902SRodney W. Grimes 		if ((mp->m_flag & MREAD) == 0)
5979b50d902SRodney W. Grimes 			u++;
5989b50d902SRodney W. Grimes 		if (mp->m_flag & MDELETED)
5999b50d902SRodney W. Grimes 			d++;
6009b50d902SRodney W. Grimes 		if (mp->m_flag & MSAVED)
6019b50d902SRodney W. Grimes 			s++;
6029b50d902SRodney W. Grimes 	}
6039b50d902SRodney W. Grimes 	ename = mailname;
6049b50d902SRodney W. Grimes 	if (getfold(fname) >= 0) {
6059b50d902SRodney W. Grimes 		strcat(fname, "/");
6069b50d902SRodney W. Grimes 		if (strncmp(fname, mailname, strlen(fname)) == 0) {
6079b50d902SRodney W. Grimes 			sprintf(zname, "+%s", mailname + strlen(fname));
6089b50d902SRodney W. Grimes 			ename = zname;
6099b50d902SRodney W. Grimes 		}
6109b50d902SRodney W. Grimes 	}
6119b50d902SRodney W. Grimes 	printf("\"%s\": ", ename);
6129b50d902SRodney W. Grimes 	if (msgCount == 1)
6139b50d902SRodney W. Grimes 		printf("1 message");
6149b50d902SRodney W. Grimes 	else
6159b50d902SRodney W. Grimes 		printf("%d messages", msgCount);
6169b50d902SRodney W. Grimes 	if (n > 0)
6179b50d902SRodney W. Grimes 		printf(" %d new", n);
6189b50d902SRodney W. Grimes 	if (u-n > 0)
6199b50d902SRodney W. Grimes 		printf(" %d unread", u);
6209b50d902SRodney W. Grimes 	if (d > 0)
6219b50d902SRodney W. Grimes 		printf(" %d deleted", d);
6229b50d902SRodney W. Grimes 	if (s > 0)
6239b50d902SRodney W. Grimes 		printf(" %d saved", s);
6249b50d902SRodney W. Grimes 	if (readonly)
6259b50d902SRodney W. Grimes 		printf(" [Read only]");
6269b50d902SRodney W. Grimes 	printf("\n");
6279b50d902SRodney W. Grimes 	return(mdot);
6289b50d902SRodney W. Grimes }
6299b50d902SRodney W. Grimes 
6309b50d902SRodney W. Grimes /*
6319b50d902SRodney W. Grimes  * Print the current version number.
6329b50d902SRodney W. Grimes  */
6339b50d902SRodney W. Grimes 
6349b50d902SRodney W. Grimes /*ARGSUSED*/
6359b50d902SRodney W. Grimes int
6369b50d902SRodney W. Grimes pversion(e)
6379b50d902SRodney W. Grimes 	int e;
6389b50d902SRodney W. Grimes {
6399b50d902SRodney W. Grimes 	extern char *version;
6409b50d902SRodney W. Grimes 
6419b50d902SRodney W. Grimes 	printf("Version %s\n", version);
6429b50d902SRodney W. Grimes 	return(0);
6439b50d902SRodney W. Grimes }
6449b50d902SRodney W. Grimes 
6459b50d902SRodney W. Grimes /*
6469b50d902SRodney W. Grimes  * Load a file of user definitions.
6479b50d902SRodney W. Grimes  */
6489b50d902SRodney W. Grimes void
6499b50d902SRodney W. Grimes load(name)
6509b50d902SRodney W. Grimes 	char *name;
6519b50d902SRodney W. Grimes {
6529b50d902SRodney W. Grimes 	register FILE *in, *oldin;
6539b50d902SRodney W. Grimes 
6549b50d902SRodney W. Grimes 	if ((in = Fopen(name, "r")) == NULL)
6559b50d902SRodney W. Grimes 		return;
6569b50d902SRodney W. Grimes 	oldin = input;
6579b50d902SRodney W. Grimes 	input = in;
6589b50d902SRodney W. Grimes 	loading = 1;
6599b50d902SRodney W. Grimes 	sourcing = 1;
6609b50d902SRodney W. Grimes 	commands();
6619b50d902SRodney W. Grimes 	loading = 0;
6629b50d902SRodney W. Grimes 	sourcing = 0;
6639b50d902SRodney W. Grimes 	input = oldin;
6649b50d902SRodney W. Grimes 	Fclose(in);
6659b50d902SRodney W. Grimes }
666