xref: /freebsd/usr.bin/mail/lex.c (revision 5e3934b1)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1980, 1993
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes  * are met:
109b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes  *    without specific prior written permission.
189b50d902SRodney W. Grimes  *
199b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes  * SUCH DAMAGE.
309b50d902SRodney W. Grimes  */
319b50d902SRodney W. Grimes 
329b50d902SRodney W. Grimes #include "rcv.h"
339b50d902SRodney W. Grimes #include <errno.h>
349b50d902SRodney W. Grimes #include <fcntl.h>
359b50d902SRodney W. Grimes #include "extern.h"
369b50d902SRodney W. Grimes 
379b50d902SRodney W. Grimes /*
389b50d902SRodney W. Grimes  * Mail -- a mail program
399b50d902SRodney W. Grimes  *
409b50d902SRodney W. Grimes  * Lexical processing of commands.
419b50d902SRodney W. Grimes  */
429b50d902SRodney W. Grimes 
43e7d53813SDiomidis Spinellis static const char	*prompt = "& ";
449ce73e90SMike Heffner 
459ce73e90SMike Heffner extern const struct cmd cmdtab[];
469ce73e90SMike Heffner extern const char *version;
479b50d902SRodney W. Grimes 
489b50d902SRodney W. Grimes /*
499b50d902SRodney W. Grimes  * Set up editing on the given file name.
509b50d902SRodney W. Grimes  * If the first character of name is %, we are considered to be
519b50d902SRodney W. Grimes  * editing the file, otherwise we are reading our mail which has
529b50d902SRodney W. Grimes  * signficance for mbox and so forth.
53c92dfc23SMike Heffner  *
54c92dfc23SMike Heffner  * If the -e option is being passed to mail, this function has a
55c92dfc23SMike Heffner  * tri-state return code: -1 on error, 0 on no mail, 1 if there is
56c92dfc23SMike Heffner  * mail.
579b50d902SRodney W. Grimes  */
589b50d902SRodney W. Grimes int
setfile(char * name)596d8484b0SPhilippe Charnier setfile(char *name)
609b50d902SRodney W. Grimes {
619b50d902SRodney W. Grimes 	FILE *ibuf;
62c92dfc23SMike Heffner 	int checkmode, i, fd;
639b50d902SRodney W. Grimes 	struct stat stb;
64856f23edSMike Heffner 	char isedit = *name != '%' || getuserid(myname) != getuid();
659b50d902SRodney W. Grimes 	char *who = name[1] ? name + 1 : myname;
660c3a8314SMike Heffner 	char tempname[PATHSIZE];
679b50d902SRodney W. Grimes 	static int shudclob;
689b50d902SRodney W. Grimes 
69761981e0SMike Heffner 	checkmode = value("checkmode") != NULL;
709ce73e90SMike Heffner 	if ((name = expand(name)) == NULL)
719ce73e90SMike Heffner 		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;
760c3a8314SMike Heffner 		warn("%s", name);
779b50d902SRodney W. Grimes 		return (-1);
789b50d902SRodney W. Grimes 	}
799b50d902SRodney W. Grimes 
809b50d902SRodney W. Grimes 	if (fstat(fileno(ibuf), &stb) < 0) {
810c3a8314SMike Heffner 		warn("fstat");
829ce73e90SMike Heffner 		(void)Fclose(ibuf);
839b50d902SRodney W. Grimes 		return (-1);
849b50d902SRodney W. Grimes 	}
859b50d902SRodney W. Grimes 
860c3a8314SMike Heffner 	if (S_ISDIR(stb.st_mode) || !S_ISREG(stb.st_mode)) {
879ce73e90SMike Heffner 		(void)Fclose(ibuf);
880c3a8314SMike Heffner 		errno = S_ISDIR(stb.st_mode) ? EISDIR : EINVAL;
890c3a8314SMike Heffner 		warn("%s", name);
909b50d902SRodney W. Grimes 		return (-1);
919b50d902SRodney W. Grimes 	}
929b50d902SRodney W. Grimes 
939b50d902SRodney W. Grimes 	/*
949b50d902SRodney W. Grimes 	 * Looks like all will be well.  We must now relinquish our
959b50d902SRodney W. Grimes 	 * hold on the current set of stuff.  Must hold signals
969b50d902SRodney W. Grimes 	 * while we are reading the new file, else we will ruin
979b50d902SRodney W. Grimes 	 * the message[] data structure.
989b50d902SRodney W. Grimes 	 */
999b50d902SRodney W. Grimes 
1009b50d902SRodney W. Grimes 	holdsigs();
1019b50d902SRodney W. Grimes 	if (shudclob)
1029b50d902SRodney W. Grimes 		quit();
1039b50d902SRodney W. Grimes 
1049b50d902SRodney W. Grimes 	/*
1059b50d902SRodney W. Grimes 	 * Copy the messages into /tmp
1069b50d902SRodney W. Grimes 	 * and set pointers.
1079b50d902SRodney W. Grimes 	 */
1089b50d902SRodney W. Grimes 
1099b50d902SRodney W. Grimes 	readonly = 0;
1109b50d902SRodney W. Grimes 	if ((i = open(name, 1)) < 0)
1119b50d902SRodney W. Grimes 		readonly++;
1129b50d902SRodney W. Grimes 	else
1139ce73e90SMike Heffner 		(void)close(i);
1149b50d902SRodney W. Grimes 	if (shudclob) {
1159ce73e90SMike Heffner 		(void)fclose(itf);
1169ce73e90SMike Heffner 		(void)fclose(otf);
1179b50d902SRodney W. Grimes 	}
1189b50d902SRodney W. Grimes 	shudclob = 1;
1199b50d902SRodney W. Grimes 	edit = isedit;
1200c3a8314SMike Heffner 	strlcpy(prevfile, mailname, sizeof(prevfile));
1219b50d902SRodney W. Grimes 	if (name != mailname)
1220c3a8314SMike Heffner 		strlcpy(mailname, name, sizeof(mailname));
1239b50d902SRodney W. Grimes 	mailsize = fsize(ibuf);
1249ce73e90SMike Heffner 	(void)snprintf(tempname, sizeof(tempname),
1259ce73e90SMike Heffner 	    "%s/mail.RxXXXXXXXXXX", tmpdir);
1260c3a8314SMike Heffner 	if ((fd = mkstemp(tempname)) == -1 || (otf = fdopen(fd, "w")) == NULL)
1270c3a8314SMike Heffner 		err(1, "%s", tempname);
1289b50d902SRodney W. Grimes 	(void)fcntl(fileno(otf), F_SETFD, 1);
1290c3a8314SMike Heffner 	if ((itf = fopen(tempname, "r")) == NULL)
1300c3a8314SMike Heffner 		err(1, "%s", tempname);
1319b50d902SRodney W. Grimes 	(void)fcntl(fileno(itf), F_SETFD, 1);
1329ce73e90SMike Heffner 	(void)rm(tempname);
133856f23edSMike Heffner 	setptr(ibuf, 0);
1349b50d902SRodney W. Grimes 	setmsize(msgCount);
135856f23edSMike Heffner 	/*
136856f23edSMike Heffner 	 * New mail may have arrived while we were reading
137856f23edSMike Heffner 	 * the mail file, so reset mailsize to be where
138856f23edSMike Heffner 	 * we really are in the file...
139856f23edSMike Heffner 	 */
1406d48fa43SAndrey A. Chernov 	mailsize = ftello(ibuf);
1419ce73e90SMike Heffner 	(void)Fclose(ibuf);
1429b50d902SRodney W. Grimes 	relsesigs();
1439b50d902SRodney W. Grimes 	sawcom = 0;
144c92dfc23SMike Heffner 
145c92dfc23SMike Heffner 	if ((checkmode || !edit) && msgCount == 0) {
1469b50d902SRodney W. Grimes nomail:
147c92dfc23SMike Heffner 		if (!checkmode) {
1489b50d902SRodney W. Grimes 			fprintf(stderr, "No mail for %s\n", who);
1499ce73e90SMike Heffner 			return (-1);
150c92dfc23SMike Heffner 		} else
1519b50d902SRodney W. Grimes 			return (0);
1529b50d902SRodney W. Grimes 	}
153c92dfc23SMike Heffner 	return (checkmode ? 1 : 0);
154c92dfc23SMike Heffner }
1559b50d902SRodney W. Grimes 
156856f23edSMike Heffner /*
157856f23edSMike Heffner  * Incorporate any new mail that has arrived since we first
158856f23edSMike Heffner  * started reading mail.
159856f23edSMike Heffner  */
160856f23edSMike Heffner int
incfile(void)1616d8484b0SPhilippe Charnier incfile(void)
162856f23edSMike Heffner {
1636d48fa43SAndrey A. Chernov 	off_t newsize;
164856f23edSMike Heffner 	int omsgCount = msgCount;
165856f23edSMike Heffner 	FILE *ibuf;
166856f23edSMike Heffner 
167856f23edSMike Heffner 	ibuf = Fopen(mailname, "r");
168856f23edSMike Heffner 	if (ibuf == NULL)
169856f23edSMike Heffner 		return (-1);
170856f23edSMike Heffner 	holdsigs();
171856f23edSMike Heffner 	newsize = fsize(ibuf);
172856f23edSMike Heffner 	if (newsize == 0)
173856f23edSMike Heffner 		return (-1);		/* mail box is now empty??? */
174856f23edSMike Heffner 	if (newsize < mailsize)
175856f23edSMike Heffner 		return (-1);		/* mail box has shrunk??? */
176856f23edSMike Heffner 	if (newsize == mailsize)
177856f23edSMike Heffner 		return (0);		/* no new mail */
178856f23edSMike Heffner 	setptr(ibuf, mailsize);
179856f23edSMike Heffner 	setmsize(msgCount);
1806d48fa43SAndrey A. Chernov 	mailsize = ftello(ibuf);
181856f23edSMike Heffner 	(void)Fclose(ibuf);
182856f23edSMike Heffner 	relsesigs();
183856f23edSMike Heffner 	return (msgCount - omsgCount);
184856f23edSMike Heffner }
185856f23edSMike Heffner 
186e7d53813SDiomidis Spinellis static int	*msgvec;
187e7d53813SDiomidis Spinellis static int	reset_on_stop;		/* do a reset() if stopped */
1889b50d902SRodney W. Grimes 
1899b50d902SRodney W. Grimes /*
1909b50d902SRodney W. Grimes  * Interpret user commands one by one.  If standard input is not a tty,
1919b50d902SRodney W. Grimes  * print no prompt.
1929b50d902SRodney W. Grimes  */
1939b50d902SRodney W. Grimes void
commands(void)1946d8484b0SPhilippe Charnier commands(void)
1959b50d902SRodney W. Grimes {
1969ce73e90SMike Heffner 	int n, eofloop = 0;
1979b50d902SRodney W. Grimes 	char linebuf[LINESIZE];
1989b50d902SRodney W. Grimes 
1999b50d902SRodney W. Grimes 	if (!sourcing) {
2009b50d902SRodney W. Grimes 		if (signal(SIGINT, SIG_IGN) != SIG_IGN)
2019ce73e90SMike Heffner 			(void)signal(SIGINT, intr);
2029b50d902SRodney W. Grimes 		if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
2039ce73e90SMike Heffner 			(void)signal(SIGHUP, hangup);
2049ce73e90SMike Heffner 		(void)signal(SIGTSTP, stop);
2059ce73e90SMike Heffner 		(void)signal(SIGTTOU, stop);
2069ce73e90SMike Heffner 		(void)signal(SIGTTIN, stop);
2079b50d902SRodney W. Grimes 	}
2089b50d902SRodney W. Grimes 	setexit();
2099b50d902SRodney W. Grimes 	for (;;) {
2109b50d902SRodney W. Grimes 		/*
2119b50d902SRodney W. Grimes 		 * Print the prompt, if needed.  Clear out
2129b50d902SRodney W. Grimes 		 * string space, and flush the output.
2139b50d902SRodney W. Grimes 		 */
2149ce73e90SMike Heffner 		if (!sourcing && value("interactive") != NULL) {
215856f23edSMike Heffner 			if ((value("autoinc") != NULL) && (incfile() > 0))
216856f23edSMike Heffner 				printf("New mail has arrived.\n");
2179b50d902SRodney W. Grimes 			reset_on_stop = 1;
218c858db96SKris Kennaway 			printf("%s", prompt);
2199b50d902SRodney W. Grimes 		}
2209ce73e90SMike Heffner 		(void)fflush(stdout);
2219b50d902SRodney W. Grimes 		sreset();
2229b50d902SRodney W. Grimes 		/*
2239b50d902SRodney W. Grimes 		 * Read a line of commands from the current input
2249b50d902SRodney W. Grimes 		 * and handle end of file specially.
2259b50d902SRodney W. Grimes 		 */
2269b50d902SRodney W. Grimes 		n = 0;
2279b50d902SRodney W. Grimes 		for (;;) {
2289b50d902SRodney W. Grimes 			if (readline(input, &linebuf[n], LINESIZE - n) < 0) {
2299b50d902SRodney W. Grimes 				if (n == 0)
2309b50d902SRodney W. Grimes 					n = -1;
2319b50d902SRodney W. Grimes 				break;
2329b50d902SRodney W. Grimes 			}
2339b50d902SRodney W. Grimes 			if ((n = strlen(linebuf)) == 0)
2349b50d902SRodney W. Grimes 				break;
2359b50d902SRodney W. Grimes 			n--;
2369b50d902SRodney W. Grimes 			if (linebuf[n] != '\\')
2379b50d902SRodney W. Grimes 				break;
2389b50d902SRodney W. Grimes 			linebuf[n++] = ' ';
2399b50d902SRodney W. Grimes 		}
2409b50d902SRodney W. Grimes 		reset_on_stop = 0;
2419b50d902SRodney W. Grimes 		if (n < 0) {
2429b50d902SRodney W. Grimes 				/* eof */
2439b50d902SRodney W. Grimes 			if (loading)
2449b50d902SRodney W. Grimes 				break;
2459b50d902SRodney W. Grimes 			if (sourcing) {
2469b50d902SRodney W. Grimes 				unstack();
2479b50d902SRodney W. Grimes 				continue;
2489b50d902SRodney W. Grimes 			}
2499ce73e90SMike Heffner 			if (value("interactive") != NULL &&
2509ce73e90SMike Heffner 			    value("ignoreeof") != NULL &&
2519b50d902SRodney W. Grimes 			    ++eofloop < 25) {
2529b50d902SRodney W. Grimes 				printf("Use \"quit\" to quit.\n");
2539b50d902SRodney W. Grimes 				continue;
2549b50d902SRodney W. Grimes 			}
2559b50d902SRodney W. Grimes 			break;
2569b50d902SRodney W. Grimes 		}
2579b50d902SRodney W. Grimes 		eofloop = 0;
2589b50d902SRodney W. Grimes 		if (execute(linebuf, 0))
2599b50d902SRodney W. Grimes 			break;
2609b50d902SRodney W. Grimes 	}
2619b50d902SRodney W. Grimes }
2629b50d902SRodney W. Grimes 
2639b50d902SRodney W. Grimes /*
2649b50d902SRodney W. Grimes  * Execute a single command.
2659b50d902SRodney W. Grimes  * Command functions return 0 for success, 1 for error, and -1
2669b50d902SRodney W. Grimes  * for abort.  A 1 or -1 aborts a load or source.  A -1 aborts
2679b50d902SRodney W. Grimes  * the interactive command loop.
2689b50d902SRodney W. Grimes  * Contxt is non-zero if called while composing mail.
2699b50d902SRodney W. Grimes  */
2709b50d902SRodney W. Grimes int
execute(char linebuf[],int contxt)2716d8484b0SPhilippe Charnier execute(char linebuf[], int contxt)
2729b50d902SRodney W. Grimes {
2739b50d902SRodney W. Grimes 	char word[LINESIZE];
2749b50d902SRodney W. Grimes 	char *arglist[MAXARGC];
2759ce73e90SMike Heffner 	const struct cmd *com;
2769ce73e90SMike Heffner 	char *cp, *cp2;
2779ce73e90SMike Heffner 	int c, muvec[2];
2789b50d902SRodney W. Grimes 	int e = 1;
2799b50d902SRodney W. Grimes 
2809b50d902SRodney W. Grimes 	/*
2819b50d902SRodney W. Grimes 	 * Strip the white space away from the beginning
2829b50d902SRodney W. Grimes 	 * of the command, then scan out a word, which
2839b50d902SRodney W. Grimes 	 * consists of anything except digits and white space.
2849b50d902SRodney W. Grimes 	 *
2859b50d902SRodney W. Grimes 	 * Handle ! escapes differently to get the correct
2869b50d902SRodney W. Grimes 	 * lexical conventions.
2879b50d902SRodney W. Grimes 	 */
2889b50d902SRodney W. Grimes 
2896d48fa43SAndrey A. Chernov 	for (cp = linebuf; isspace((unsigned char)*cp); cp++)
2909b50d902SRodney W. Grimes 		;
2919b50d902SRodney W. Grimes 	if (*cp == '!') {
2929b50d902SRodney W. Grimes 		if (sourcing) {
2939b50d902SRodney W. Grimes 			printf("Can't \"!\" while sourcing\n");
2949b50d902SRodney W. Grimes 			goto out;
2959b50d902SRodney W. Grimes 		}
2969b50d902SRodney W. Grimes 		shell(cp+1);
2979b50d902SRodney W. Grimes 		return (0);
2989b50d902SRodney W. Grimes 	}
2999b50d902SRodney W. Grimes 	cp2 = word;
3009ce73e90SMike Heffner 	while (*cp != '\0' && strchr(" \t0123456789$^.:/-+*'\"", *cp) == NULL)
3019b50d902SRodney W. Grimes 		*cp2++ = *cp++;
3029b50d902SRodney W. Grimes 	*cp2 = '\0';
3039b50d902SRodney W. Grimes 
3049b50d902SRodney W. Grimes 	/*
3059b50d902SRodney W. Grimes 	 * Look up the command; if not found, bitch.
3069b50d902SRodney W. Grimes 	 * Normally, a blank command would map to the
3079b50d902SRodney W. Grimes 	 * first command in the table; while sourcing,
3089b50d902SRodney W. Grimes 	 * however, we ignore blank lines to eliminate
3099b50d902SRodney W. Grimes 	 * confusion.
3109b50d902SRodney W. Grimes 	 */
3119b50d902SRodney W. Grimes 
3129b50d902SRodney W. Grimes 	if (sourcing && *word == '\0')
3139b50d902SRodney W. Grimes 		return (0);
3149b50d902SRodney W. Grimes 	com = lex(word);
3159ce73e90SMike Heffner 	if (com == NULL) {
3169b50d902SRodney W. Grimes 		printf("Unknown command: \"%s\"\n", word);
3179b50d902SRodney W. Grimes 		goto out;
3189b50d902SRodney W. Grimes 	}
3199b50d902SRodney W. Grimes 
3209b50d902SRodney W. Grimes 	/*
3219b50d902SRodney W. Grimes 	 * See if we should execute the command -- if a conditional
3229b50d902SRodney W. Grimes 	 * we always execute it, otherwise, check the state of cond.
3239b50d902SRodney W. Grimes 	 */
3249b50d902SRodney W. Grimes 
3259b50d902SRodney W. Grimes 	if ((com->c_argtype & F) == 0)
3269ce73e90SMike Heffner 		if ((cond == CRCV && !rcvmode) || (cond == CSEND && rcvmode))
3279b50d902SRodney W. Grimes 			return (0);
3289b50d902SRodney W. Grimes 
3299b50d902SRodney W. Grimes 	/*
3309b50d902SRodney W. Grimes 	 * Process the arguments to the command, depending
3319b50d902SRodney W. Grimes 	 * on the type he expects.  Default to an error.
3329b50d902SRodney W. Grimes 	 * If we are sourcing an interactive command, it's
3339b50d902SRodney W. Grimes 	 * an error.
3349b50d902SRodney W. Grimes 	 */
3359b50d902SRodney W. Grimes 
3369b50d902SRodney W. Grimes 	if (!rcvmode && (com->c_argtype & M) == 0) {
3379b50d902SRodney W. Grimes 		printf("May not execute \"%s\" while sending\n",
3389b50d902SRodney W. Grimes 		    com->c_name);
3399b50d902SRodney W. Grimes 		goto out;
3409b50d902SRodney W. Grimes 	}
3419b50d902SRodney W. Grimes 	if (sourcing && com->c_argtype & I) {
3429b50d902SRodney W. Grimes 		printf("May not execute \"%s\" while sourcing\n",
3439b50d902SRodney W. Grimes 		    com->c_name);
3449b50d902SRodney W. Grimes 		goto out;
3459b50d902SRodney W. Grimes 	}
3469b50d902SRodney W. Grimes 	if (readonly && com->c_argtype & W) {
3479b50d902SRodney W. Grimes 		printf("May not execute \"%s\" -- message file is read only\n",
3489b50d902SRodney W. Grimes 		   com->c_name);
3499b50d902SRodney W. Grimes 		goto out;
3509b50d902SRodney W. Grimes 	}
3519b50d902SRodney W. Grimes 	if (contxt && com->c_argtype & R) {
3529b50d902SRodney W. Grimes 		printf("Cannot recursively invoke \"%s\"\n", com->c_name);
3539b50d902SRodney W. Grimes 		goto out;
3549b50d902SRodney W. Grimes 	}
3559b50d902SRodney W. Grimes 	switch (com->c_argtype & ~(F|P|I|M|T|W|R)) {
3569b50d902SRodney W. Grimes 	case MSGLIST:
3579b50d902SRodney W. Grimes 		/*
3589b50d902SRodney W. Grimes 		 * A message list defaulting to nearest forward
3599b50d902SRodney W. Grimes 		 * legal message.
3609b50d902SRodney W. Grimes 		 */
3619b50d902SRodney W. Grimes 		if (msgvec == 0) {
3629b50d902SRodney W. Grimes 			printf("Illegal use of \"message list\"\n");
3639b50d902SRodney W. Grimes 			break;
3649b50d902SRodney W. Grimes 		}
3659b50d902SRodney W. Grimes 		if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0)
3669b50d902SRodney W. Grimes 			break;
3679b50d902SRodney W. Grimes 		if (c  == 0) {
3689ce73e90SMike Heffner 			*msgvec = first(com->c_msgflag, com->c_msgmask);
369d030d2d2SPoul-Henning Kamp 			msgvec[1] = 0;
3709b50d902SRodney W. Grimes 		}
371d030d2d2SPoul-Henning Kamp 		if (*msgvec == 0) {
3729b50d902SRodney W. Grimes 			printf("No applicable messages\n");
3739b50d902SRodney W. Grimes 			break;
3749b50d902SRodney W. Grimes 		}
3759b50d902SRodney W. Grimes 		e = (*com->c_func)(msgvec);
3769b50d902SRodney W. Grimes 		break;
3779b50d902SRodney W. Grimes 
3789b50d902SRodney W. Grimes 	case NDMLIST:
3799b50d902SRodney W. Grimes 		/*
3809b50d902SRodney W. Grimes 		 * A message list with no defaults, but no error
3819b50d902SRodney W. Grimes 		 * if none exist.
3829b50d902SRodney W. Grimes 		 */
3839b50d902SRodney W. Grimes 		if (msgvec == 0) {
3849b50d902SRodney W. Grimes 			printf("Illegal use of \"message list\"\n");
3859b50d902SRodney W. Grimes 			break;
3869b50d902SRodney W. Grimes 		}
3879b50d902SRodney W. Grimes 		if (getmsglist(cp, msgvec, com->c_msgflag) < 0)
3889b50d902SRodney W. Grimes 			break;
3899b50d902SRodney W. Grimes 		e = (*com->c_func)(msgvec);
3909b50d902SRodney W. Grimes 		break;
3919b50d902SRodney W. Grimes 
3929b50d902SRodney W. Grimes 	case STRLIST:
3939b50d902SRodney W. Grimes 		/*
3949b50d902SRodney W. Grimes 		 * Just the straight string, with
3959b50d902SRodney W. Grimes 		 * leading blanks removed.
3969b50d902SRodney W. Grimes 		 */
3976d48fa43SAndrey A. Chernov 		while (isspace((unsigned char)*cp))
3989b50d902SRodney W. Grimes 			cp++;
3999b50d902SRodney W. Grimes 		e = (*com->c_func)(cp);
4009b50d902SRodney W. Grimes 		break;
4019b50d902SRodney W. Grimes 
4029b50d902SRodney W. Grimes 	case RAWLIST:
4039b50d902SRodney W. Grimes 		/*
4049b50d902SRodney W. Grimes 		 * A vector of strings, in shell style.
4059b50d902SRodney W. Grimes 		 */
4069b50d902SRodney W. Grimes 		if ((c = getrawlist(cp, arglist,
4079ce73e90SMike Heffner 		    sizeof(arglist) / sizeof(*arglist))) < 0)
4089b50d902SRodney W. Grimes 			break;
4099b50d902SRodney W. Grimes 		if (c < com->c_minargs) {
4109b50d902SRodney W. Grimes 			printf("%s requires at least %d arg(s)\n",
4119b50d902SRodney W. Grimes 			    com->c_name, com->c_minargs);
4129b50d902SRodney W. Grimes 			break;
4139b50d902SRodney W. Grimes 		}
4149b50d902SRodney W. Grimes 		if (c > com->c_maxargs) {
4159b50d902SRodney W. Grimes 			printf("%s takes no more than %d arg(s)\n",
4169b50d902SRodney W. Grimes 			    com->c_name, com->c_maxargs);
4179b50d902SRodney W. Grimes 			break;
4189b50d902SRodney W. Grimes 		}
4199b50d902SRodney W. Grimes 		e = (*com->c_func)(arglist);
4209b50d902SRodney W. Grimes 		break;
4219b50d902SRodney W. Grimes 
4229b50d902SRodney W. Grimes 	case NOLIST:
4239b50d902SRodney W. Grimes 		/*
4249b50d902SRodney W. Grimes 		 * Just the constant zero, for exiting,
4259b50d902SRodney W. Grimes 		 * eg.
4269b50d902SRodney W. Grimes 		 */
4279b50d902SRodney W. Grimes 		e = (*com->c_func)(0);
4289b50d902SRodney W. Grimes 		break;
4299b50d902SRodney W. Grimes 
4309b50d902SRodney W. Grimes 	default:
4310c3a8314SMike Heffner 		errx(1, "Unknown argtype");
4329b50d902SRodney W. Grimes 	}
4339b50d902SRodney W. Grimes 
4349b50d902SRodney W. Grimes out:
4359b50d902SRodney W. Grimes 	/*
4369b50d902SRodney W. Grimes 	 * Exit the current source file on
4379b50d902SRodney W. Grimes 	 * error.
4389b50d902SRodney W. Grimes 	 */
4399b50d902SRodney W. Grimes 	if (e) {
4409b50d902SRodney W. Grimes 		if (e < 0)
4419ce73e90SMike Heffner 			return (1);
4429b50d902SRodney W. Grimes 		if (loading)
4439ce73e90SMike Heffner 			return (1);
4449b50d902SRodney W. Grimes 		if (sourcing)
4459b50d902SRodney W. Grimes 			unstack();
4469ce73e90SMike Heffner 		return (0);
4479b50d902SRodney W. Grimes 	}
448856f23edSMike Heffner 	if (com == NULL)
449856f23edSMike Heffner 		return (0);
4509ce73e90SMike Heffner 	if (value("autoprint") != NULL && com->c_argtype & P)
4519b50d902SRodney W. Grimes 		if ((dot->m_flag & MDELETED) == 0) {
4529b50d902SRodney W. Grimes 			muvec[0] = dot - &message[0] + 1;
4539b50d902SRodney W. Grimes 			muvec[1] = 0;
4549b50d902SRodney W. Grimes 			type(muvec);
4559b50d902SRodney W. Grimes 		}
4569b50d902SRodney W. Grimes 	if (!sourcing && (com->c_argtype & T) == 0)
4579b50d902SRodney W. Grimes 		sawcom = 1;
4589b50d902SRodney W. Grimes 	return (0);
4599b50d902SRodney W. Grimes }
4609b50d902SRodney W. Grimes 
4619b50d902SRodney W. Grimes /*
4629b50d902SRodney W. Grimes  * Set the size of the message vector used to construct argument
4639b50d902SRodney W. Grimes  * lists to message list functions.
4649b50d902SRodney W. Grimes  */
4659b50d902SRodney W. Grimes void
setmsize(int sz)4666d8484b0SPhilippe Charnier setmsize(int sz)
4679b50d902SRodney W. Grimes {
4689b50d902SRodney W. Grimes 
469856f23edSMike Heffner 	if (msgvec != NULL)
4709ce73e90SMike Heffner 		(void)free(msgvec);
4719ce73e90SMike Heffner 	msgvec = calloc((unsigned)(sz + 1), sizeof(*msgvec));
4729b50d902SRodney W. Grimes }
4739b50d902SRodney W. Grimes 
4749b50d902SRodney W. Grimes /*
4759b50d902SRodney W. Grimes  * Find the correct command in the command table corresponding
4769b50d902SRodney W. Grimes  * to the passed command "word"
4779b50d902SRodney W. Grimes  */
4789b50d902SRodney W. Grimes 
479f6ab8089SEd Schouten const struct cmd *
lex(char word[])4806d8484b0SPhilippe Charnier lex(char word[])
4819b50d902SRodney W. Grimes {
4829ce73e90SMike Heffner 	const struct cmd *cp;
4839b50d902SRodney W. Grimes 
484b5cfa4b2SJoerg Wunsch 	/*
485b5cfa4b2SJoerg Wunsch 	 * ignore trailing chars after `#'
486b5cfa4b2SJoerg Wunsch 	 *
487b5cfa4b2SJoerg Wunsch 	 * lines with beginning `#' are comments
4880c3a8314SMike Heffner 	 * spaces before `#' are ignored in execute()
489b5cfa4b2SJoerg Wunsch 	 */
490b5cfa4b2SJoerg Wunsch 
491b5cfa4b2SJoerg Wunsch 	if (*word == '#')
492b5cfa4b2SJoerg Wunsch 	    *(word+1) = '\0';
493b5cfa4b2SJoerg Wunsch 
494b5cfa4b2SJoerg Wunsch 
4959ce73e90SMike Heffner 	for (cp = &cmdtab[0]; cp->c_name != NULL; cp++)
4969b50d902SRodney W. Grimes 		if (isprefix(word, cp->c_name))
4979b50d902SRodney W. Grimes 			return (cp);
4989ce73e90SMike Heffner 	return (NULL);
4999b50d902SRodney W. Grimes }
5009b50d902SRodney W. Grimes 
5019b50d902SRodney W. Grimes /*
5029b50d902SRodney W. Grimes  * Determine if as1 is a valid prefix of as2.
5039b50d902SRodney W. Grimes  * Return true if yep.
5049b50d902SRodney W. Grimes  */
5059b50d902SRodney W. Grimes int
isprefix(const char * as1,const char * as2)5066d8484b0SPhilippe Charnier isprefix(const char *as1, const char *as2)
5079b50d902SRodney W. Grimes {
5089ce73e90SMike Heffner 	const char *s1, *s2;
5099b50d902SRodney W. Grimes 
5109b50d902SRodney W. Grimes 	s1 = as1;
5119b50d902SRodney W. Grimes 	s2 = as2;
5129b50d902SRodney W. Grimes 	while (*s1++ == *s2)
5139b50d902SRodney W. Grimes 		if (*s2++ == '\0')
5149b50d902SRodney W. Grimes 			return (1);
5159b50d902SRodney W. Grimes 	return (*--s1 == '\0');
5169b50d902SRodney W. Grimes }
5179b50d902SRodney W. Grimes 
5189b50d902SRodney W. Grimes /*
5199b50d902SRodney W. Grimes  * The following gets called on receipt of an interrupt.  This is
5209b50d902SRodney W. Grimes  * to abort printout of a command, mainly.
5219b50d902SRodney W. Grimes  * Dispatching here when command() is inactive crashes rcv.
5229b50d902SRodney W. Grimes  * Close all open files except 0, 1, 2, and the temporary.
5239b50d902SRodney W. Grimes  * Also, unstack all source files.
5249b50d902SRodney W. Grimes  */
5259b50d902SRodney W. Grimes 
526e7d53813SDiomidis Spinellis static int	inithdr;		/* am printing startup headers */
5279b50d902SRodney W. Grimes 
5289b50d902SRodney W. Grimes void
intr(int s __unused)5296d8484b0SPhilippe Charnier intr(int s __unused)
5309b50d902SRodney W. Grimes {
5319b50d902SRodney W. Grimes 
5329b50d902SRodney W. Grimes 	noreset = 0;
5339b50d902SRodney W. Grimes 	if (!inithdr)
5349b50d902SRodney W. Grimes 		sawcom++;
5359b50d902SRodney W. Grimes 	inithdr = 0;
5369b50d902SRodney W. Grimes 	while (sourcing)
5379b50d902SRodney W. Grimes 		unstack();
5389b50d902SRodney W. Grimes 
5399b50d902SRodney W. Grimes 	close_all_files();
5409b50d902SRodney W. Grimes 
5419b50d902SRodney W. Grimes 	if (image >= 0) {
5429ce73e90SMike Heffner 		(void)close(image);
5439b50d902SRodney W. Grimes 		image = -1;
5449b50d902SRodney W. Grimes 	}
5459b50d902SRodney W. Grimes 	fprintf(stderr, "Interrupt\n");
5469b50d902SRodney W. Grimes 	reset(0);
5479b50d902SRodney W. Grimes }
5489b50d902SRodney W. Grimes 
5499b50d902SRodney W. Grimes /*
5509b50d902SRodney W. Grimes  * When we wake up after ^Z, reprint the prompt.
5519b50d902SRodney W. Grimes  */
5529b50d902SRodney W. Grimes void
stop(int s)5536d8484b0SPhilippe Charnier stop(int s)
5549b50d902SRodney W. Grimes {
5559b50d902SRodney W. Grimes 	sig_t old_action = signal(s, SIG_DFL);
556856f23edSMike Heffner 	sigset_t nset;
5579b50d902SRodney W. Grimes 
558856f23edSMike Heffner 	(void)sigemptyset(&nset);
559856f23edSMike Heffner 	(void)sigaddset(&nset, s);
560856f23edSMike Heffner 	(void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
5619ce73e90SMike Heffner 	(void)kill(0, s);
562856f23edSMike Heffner 	(void)sigprocmask(SIG_BLOCK, &nset, NULL);
5639ce73e90SMike Heffner 	(void)signal(s, old_action);
5649b50d902SRodney W. Grimes 	if (reset_on_stop) {
5659b50d902SRodney W. Grimes 		reset_on_stop = 0;
5669b50d902SRodney W. Grimes 		reset(0);
5679b50d902SRodney W. Grimes 	}
5689b50d902SRodney W. Grimes }
5699b50d902SRodney W. Grimes 
5709b50d902SRodney W. Grimes /*
5719b50d902SRodney W. Grimes  * Branch here on hangup signal and simulate "exit".
5729b50d902SRodney W. Grimes  */
5739b50d902SRodney W. Grimes void
hangup(int s __unused)5746d8484b0SPhilippe Charnier hangup(int s __unused)
5759b50d902SRodney W. Grimes {
5769b50d902SRodney W. Grimes 
5779b50d902SRodney W. Grimes 	/* nothing to do? */
5789b50d902SRodney W. Grimes 	exit(1);
5799b50d902SRodney W. Grimes }
5809b50d902SRodney W. Grimes 
5819b50d902SRodney W. Grimes /*
5829b50d902SRodney W. Grimes  * Announce the presence of the current Mail version,
5839b50d902SRodney W. Grimes  * give the message count, and print a header listing.
5849b50d902SRodney W. Grimes  */
5859b50d902SRodney W. Grimes void
announce(void)5866d8484b0SPhilippe Charnier announce(void)
5879b50d902SRodney W. Grimes {
5889b50d902SRodney W. Grimes 	int vec[2], mdot;
5899b50d902SRodney W. Grimes 
590856f23edSMike Heffner 	mdot = newfileinfo(0);
5919b50d902SRodney W. Grimes 	vec[0] = mdot;
5929b50d902SRodney W. Grimes 	vec[1] = 0;
5939b50d902SRodney W. Grimes 	dot = &message[mdot - 1];
5949ce73e90SMike Heffner 	if (msgCount > 0 && value("noheader") == NULL) {
5959b50d902SRodney W. Grimes 		inithdr++;
5969b50d902SRodney W. Grimes 		headers(vec);
5979b50d902SRodney W. Grimes 		inithdr = 0;
5989b50d902SRodney W. Grimes 	}
5999b50d902SRodney W. Grimes }
6009b50d902SRodney W. Grimes 
6019b50d902SRodney W. Grimes /*
6029b50d902SRodney W. Grimes  * Announce information about the file we are editing.
6039b50d902SRodney W. Grimes  * Return a likely place to set dot.
6049b50d902SRodney W. Grimes  */
6059b50d902SRodney W. Grimes int
newfileinfo(int omsgCount)6066d8484b0SPhilippe Charnier newfileinfo(int omsgCount)
6079b50d902SRodney W. Grimes {
6089ce73e90SMike Heffner 	struct message *mp;
6099ce73e90SMike Heffner 	int u, n, mdot, d, s;
6100c3a8314SMike Heffner 	char fname[PATHSIZE+1], zname[PATHSIZE+1], *ename;
6119b50d902SRodney W. Grimes 
612856f23edSMike Heffner 	for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
6139b50d902SRodney W. Grimes 		if (mp->m_flag & MNEW)
6149b50d902SRodney W. Grimes 			break;
6159b50d902SRodney W. Grimes 	if (mp >= &message[msgCount])
616856f23edSMike Heffner 		for (mp = &message[omsgCount]; mp < &message[msgCount]; mp++)
6179b50d902SRodney W. Grimes 			if ((mp->m_flag & MREAD) == 0)
6189b50d902SRodney W. Grimes 				break;
6199b50d902SRodney W. Grimes 	if (mp < &message[msgCount])
6209b50d902SRodney W. Grimes 		mdot = mp - &message[0] + 1;
6219b50d902SRodney W. Grimes 	else
622856f23edSMike Heffner 		mdot = omsgCount + 1;
6239b50d902SRodney W. Grimes 	s = d = 0;
6249b50d902SRodney W. Grimes 	for (mp = &message[0], n = 0, u = 0; mp < &message[msgCount]; mp++) {
6259b50d902SRodney W. Grimes 		if (mp->m_flag & MNEW)
6269b50d902SRodney W. Grimes 			n++;
6279b50d902SRodney W. Grimes 		if ((mp->m_flag & MREAD) == 0)
6289b50d902SRodney W. Grimes 			u++;
6299b50d902SRodney W. Grimes 		if (mp->m_flag & MDELETED)
6309b50d902SRodney W. Grimes 			d++;
6319b50d902SRodney W. Grimes 		if (mp->m_flag & MSAVED)
6329b50d902SRodney W. Grimes 			s++;
6339b50d902SRodney W. Grimes 	}
6349b50d902SRodney W. Grimes 	ename = mailname;
6350c3a8314SMike Heffner 	if (getfold(fname, sizeof(fname) - 1) >= 0) {
6369b50d902SRodney W. Grimes 		strcat(fname, "/");
6379b50d902SRodney W. Grimes 		if (strncmp(fname, mailname, strlen(fname)) == 0) {
6389ce73e90SMike Heffner 			(void)snprintf(zname, sizeof(zname), "+%s",
6399ce73e90SMike Heffner 			    mailname + strlen(fname));
6409b50d902SRodney W. Grimes 			ename = zname;
6419b50d902SRodney W. Grimes 		}
6429b50d902SRodney W. Grimes 	}
6439b50d902SRodney W. Grimes 	printf("\"%s\": ", ename);
6449b50d902SRodney W. Grimes 	if (msgCount == 1)
6459b50d902SRodney W. Grimes 		printf("1 message");
6469b50d902SRodney W. Grimes 	else
6479b50d902SRodney W. Grimes 		printf("%d messages", msgCount);
6489b50d902SRodney W. Grimes 	if (n > 0)
6499b50d902SRodney W. Grimes 		printf(" %d new", n);
6509b50d902SRodney W. Grimes 	if (u-n > 0)
6519b50d902SRodney W. Grimes 		printf(" %d unread", u);
6529b50d902SRodney W. Grimes 	if (d > 0)
6539b50d902SRodney W. Grimes 		printf(" %d deleted", d);
6549b50d902SRodney W. Grimes 	if (s > 0)
6559b50d902SRodney W. Grimes 		printf(" %d saved", s);
6569b50d902SRodney W. Grimes 	if (readonly)
6579b50d902SRodney W. Grimes 		printf(" [Read only]");
6589b50d902SRodney W. Grimes 	printf("\n");
6599b50d902SRodney W. Grimes 	return (mdot);
6609b50d902SRodney W. Grimes }
6619b50d902SRodney W. Grimes 
6629b50d902SRodney W. Grimes /*
6639b50d902SRodney W. Grimes  * Print the current version number.
6649b50d902SRodney W. Grimes  */
6659b50d902SRodney W. Grimes 
6669b50d902SRodney W. Grimes int
pversion(void * arg __unused)667d28a9551SJohn Baldwin pversion(void *arg __unused)
6689b50d902SRodney W. Grimes {
6699b50d902SRodney W. Grimes 
6709b50d902SRodney W. Grimes 	printf("Version %s\n", version);
6719b50d902SRodney W. Grimes 	return (0);
6729b50d902SRodney W. Grimes }
6739b50d902SRodney W. Grimes 
6749b50d902SRodney W. Grimes /*
6759b50d902SRodney W. Grimes  * Load a file of user definitions.
6769b50d902SRodney W. Grimes  */
6779b50d902SRodney W. Grimes void
load(char * name)6786d8484b0SPhilippe Charnier load(char *name)
6799b50d902SRodney W. Grimes {
6809ce73e90SMike Heffner 	FILE *in, *oldin;
6819b50d902SRodney W. Grimes 
6829b50d902SRodney W. Grimes 	if ((in = Fopen(name, "r")) == NULL)
6839b50d902SRodney W. Grimes 		return;
6849b50d902SRodney W. Grimes 	oldin = input;
6859b50d902SRodney W. Grimes 	input = in;
6869b50d902SRodney W. Grimes 	loading = 1;
6879b50d902SRodney W. Grimes 	sourcing = 1;
6889b50d902SRodney W. Grimes 	commands();
6899b50d902SRodney W. Grimes 	loading = 0;
6909b50d902SRodney W. Grimes 	sourcing = 0;
6919b50d902SRodney W. Grimes 	input = oldin;
6929ce73e90SMike Heffner 	(void)Fclose(in);
6939b50d902SRodney W. Grimes }
694