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