Home | History | Annotate | Line # | Download | only in mail
lex.c revision 1.7
      1  1.7  christos /*	$NetBSD: lex.c,v 1.7 1996/06/08 19:48:28 christos Exp $	*/
      2  1.7  christos 
      3  1.1       cgd /*
      4  1.4   deraadt  * Copyright (c) 1980, 1993
      5  1.4   deraadt  *	The Regents of the University of California.  All rights reserved.
      6  1.1       cgd  *
      7  1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8  1.1       cgd  * modification, are permitted provided that the following conditions
      9  1.1       cgd  * are met:
     10  1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11  1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12  1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14  1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16  1.1       cgd  *    must display the following acknowledgement:
     17  1.1       cgd  *	This product includes software developed by the University of
     18  1.1       cgd  *	California, Berkeley and its contributors.
     19  1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20  1.1       cgd  *    may be used to endorse or promote products derived from this software
     21  1.1       cgd  *    without specific prior written permission.
     22  1.1       cgd  *
     23  1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1       cgd  * SUCH DAMAGE.
     34  1.1       cgd  */
     35  1.1       cgd 
     36  1.1       cgd #ifndef lint
     37  1.7  christos #if 0
     38  1.7  christos static char sccsid[] = "@(#)lex.c	8.1 (Berkeley) 6/6/93";
     39  1.7  christos #else
     40  1.7  christos static char rcsid[] = "$NetBSD: lex.c,v 1.7 1996/06/08 19:48:28 christos Exp $";
     41  1.7  christos #endif
     42  1.1       cgd #endif /* not lint */
     43  1.1       cgd 
     44  1.1       cgd #include "rcv.h"
     45  1.1       cgd #include <errno.h>
     46  1.4   deraadt #include <fcntl.h>
     47  1.4   deraadt #include "extern.h"
     48  1.1       cgd 
     49  1.1       cgd /*
     50  1.1       cgd  * Mail -- a mail program
     51  1.1       cgd  *
     52  1.1       cgd  * Lexical processing of commands.
     53  1.1       cgd  */
     54  1.1       cgd 
     55  1.1       cgd char	*prompt = "& ";
     56  1.1       cgd 
     57  1.1       cgd /*
     58  1.1       cgd  * Set up editing on the given file name.
     59  1.1       cgd  * If the first character of name is %, we are considered to be
     60  1.1       cgd  * editing the file, otherwise we are reading our mail which has
     61  1.1       cgd  * signficance for mbox and so forth.
     62  1.1       cgd  */
     63  1.4   deraadt int
     64  1.1       cgd setfile(name)
     65  1.1       cgd 	char *name;
     66  1.1       cgd {
     67  1.1       cgd 	FILE *ibuf;
     68  1.1       cgd 	int i;
     69  1.1       cgd 	struct stat stb;
     70  1.1       cgd 	char isedit = *name != '%';
     71  1.1       cgd 	char *who = name[1] ? name + 1 : myname;
     72  1.1       cgd 	static int shudclob;
     73  1.5       jtc 	extern char *tempMesg;
     74  1.1       cgd 	extern int errno;
     75  1.1       cgd 
     76  1.1       cgd 	if ((name = expand(name)) == NOSTR)
     77  1.1       cgd 		return -1;
     78  1.1       cgd 
     79  1.1       cgd 	if ((ibuf = Fopen(name, "r")) == NULL) {
     80  1.1       cgd 		if (!isedit && errno == ENOENT)
     81  1.1       cgd 			goto nomail;
     82  1.1       cgd 		perror(name);
     83  1.1       cgd 		return(-1);
     84  1.1       cgd 	}
     85  1.1       cgd 
     86  1.1       cgd 	if (fstat(fileno(ibuf), &stb) < 0) {
     87  1.1       cgd 		perror("fstat");
     88  1.1       cgd 		Fclose(ibuf);
     89  1.1       cgd 		return (-1);
     90  1.1       cgd 	}
     91  1.1       cgd 
     92  1.1       cgd 	switch (stb.st_mode & S_IFMT) {
     93  1.1       cgd 	case S_IFDIR:
     94  1.1       cgd 		Fclose(ibuf);
     95  1.1       cgd 		errno = EISDIR;
     96  1.1       cgd 		perror(name);
     97  1.1       cgd 		return (-1);
     98  1.1       cgd 
     99  1.1       cgd 	case S_IFREG:
    100  1.1       cgd 		break;
    101  1.1       cgd 
    102  1.1       cgd 	default:
    103  1.1       cgd 		Fclose(ibuf);
    104  1.1       cgd 		errno = EINVAL;
    105  1.1       cgd 		perror(name);
    106  1.1       cgd 		return (-1);
    107  1.1       cgd 	}
    108  1.1       cgd 
    109  1.1       cgd 	/*
    110  1.1       cgd 	 * Looks like all will be well.  We must now relinquish our
    111  1.1       cgd 	 * hold on the current set of stuff.  Must hold signals
    112  1.1       cgd 	 * while we are reading the new file, else we will ruin
    113  1.1       cgd 	 * the message[] data structure.
    114  1.1       cgd 	 */
    115  1.1       cgd 
    116  1.1       cgd 	holdsigs();
    117  1.1       cgd 	if (shudclob)
    118  1.1       cgd 		quit();
    119  1.1       cgd 
    120  1.1       cgd 	/*
    121  1.1       cgd 	 * Copy the messages into /tmp
    122  1.1       cgd 	 * and set pointers.
    123  1.1       cgd 	 */
    124  1.1       cgd 
    125  1.1       cgd 	readonly = 0;
    126  1.1       cgd 	if ((i = open(name, 1)) < 0)
    127  1.1       cgd 		readonly++;
    128  1.1       cgd 	else
    129  1.1       cgd 		close(i);
    130  1.1       cgd 	if (shudclob) {
    131  1.1       cgd 		fclose(itf);
    132  1.1       cgd 		fclose(otf);
    133  1.1       cgd 	}
    134  1.1       cgd 	shudclob = 1;
    135  1.1       cgd 	edit = isedit;
    136  1.1       cgd 	strcpy(prevfile, mailname);
    137  1.1       cgd 	if (name != mailname)
    138  1.1       cgd 		strcpy(mailname, name);
    139  1.1       cgd 	mailsize = fsize(ibuf);
    140  1.1       cgd 	if ((otf = fopen(tempMesg, "w")) == NULL) {
    141  1.1       cgd 		perror(tempMesg);
    142  1.1       cgd 		exit(1);
    143  1.1       cgd 	}
    144  1.4   deraadt 	(void) fcntl(fileno(otf), F_SETFD, 1);
    145  1.1       cgd 	if ((itf = fopen(tempMesg, "r")) == NULL) {
    146  1.1       cgd 		perror(tempMesg);
    147  1.1       cgd 		exit(1);
    148  1.1       cgd 	}
    149  1.4   deraadt 	(void) fcntl(fileno(itf), F_SETFD, 1);
    150  1.1       cgd 	rm(tempMesg);
    151  1.1       cgd 	setptr(ibuf);
    152  1.1       cgd 	setmsize(msgCount);
    153  1.1       cgd 	Fclose(ibuf);
    154  1.1       cgd 	relsesigs();
    155  1.1       cgd 	sawcom = 0;
    156  1.1       cgd 	if (!edit && msgCount == 0) {
    157  1.1       cgd nomail:
    158  1.1       cgd 		fprintf(stderr, "No mail for %s\n", who);
    159  1.1       cgd 		return -1;
    160  1.1       cgd 	}
    161  1.1       cgd 	return(0);
    162  1.1       cgd }
    163  1.1       cgd 
    164  1.1       cgd int	*msgvec;
    165  1.1       cgd int	reset_on_stop;			/* do a reset() if stopped */
    166  1.1       cgd 
    167  1.1       cgd /*
    168  1.1       cgd  * Interpret user commands one by one.  If standard input is not a tty,
    169  1.1       cgd  * print no prompt.
    170  1.1       cgd  */
    171  1.4   deraadt void
    172  1.1       cgd commands()
    173  1.1       cgd {
    174  1.1       cgd 	int eofloop = 0;
    175  1.1       cgd 	register int n;
    176  1.1       cgd 	char linebuf[LINESIZE];
    177  1.7  christos #if __GNUC__
    178  1.7  christos 	/* Avoid longjmp clobbering */
    179  1.7  christos 	(void) &eofloop;
    180  1.7  christos #endif
    181  1.1       cgd 
    182  1.1       cgd 	if (!sourcing) {
    183  1.1       cgd 		if (signal(SIGINT, SIG_IGN) != SIG_IGN)
    184  1.1       cgd 			signal(SIGINT, intr);
    185  1.1       cgd 		if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
    186  1.1       cgd 			signal(SIGHUP, hangup);
    187  1.1       cgd 		signal(SIGTSTP, stop);
    188  1.1       cgd 		signal(SIGTTOU, stop);
    189  1.1       cgd 		signal(SIGTTIN, stop);
    190  1.1       cgd 	}
    191  1.1       cgd 	setexit();
    192  1.1       cgd 	for (;;) {
    193  1.1       cgd 		/*
    194  1.1       cgd 		 * Print the prompt, if needed.  Clear out
    195  1.1       cgd 		 * string space, and flush the output.
    196  1.1       cgd 		 */
    197  1.1       cgd 		if (!sourcing && value("interactive") != NOSTR) {
    198  1.1       cgd 			reset_on_stop = 1;
    199  1.1       cgd 			printf(prompt);
    200  1.1       cgd 		}
    201  1.1       cgd 		fflush(stdout);
    202  1.1       cgd 		sreset();
    203  1.1       cgd 		/*
    204  1.1       cgd 		 * Read a line of commands from the current input
    205  1.1       cgd 		 * and handle end of file specially.
    206  1.1       cgd 		 */
    207  1.1       cgd 		n = 0;
    208  1.1       cgd 		for (;;) {
    209  1.1       cgd 			if (readline(input, &linebuf[n], LINESIZE - n) < 0) {
    210  1.1       cgd 				if (n == 0)
    211  1.1       cgd 					n = -1;
    212  1.1       cgd 				break;
    213  1.1       cgd 			}
    214  1.1       cgd 			if ((n = strlen(linebuf)) == 0)
    215  1.1       cgd 				break;
    216  1.1       cgd 			n--;
    217  1.1       cgd 			if (linebuf[n] != '\\')
    218  1.1       cgd 				break;
    219  1.1       cgd 			linebuf[n++] = ' ';
    220  1.1       cgd 		}
    221  1.1       cgd 		reset_on_stop = 0;
    222  1.1       cgd 		if (n < 0) {
    223  1.1       cgd 				/* eof */
    224  1.1       cgd 			if (loading)
    225  1.1       cgd 				break;
    226  1.1       cgd 			if (sourcing) {
    227  1.1       cgd 				unstack();
    228  1.1       cgd 				continue;
    229  1.1       cgd 			}
    230  1.1       cgd 			if (value("interactive") != NOSTR &&
    231  1.1       cgd 			    value("ignoreeof") != NOSTR &&
    232  1.1       cgd 			    ++eofloop < 25) {
    233  1.1       cgd 				printf("Use \"quit\" to quit.\n");
    234  1.1       cgd 				continue;
    235  1.1       cgd 			}
    236  1.1       cgd 			break;
    237  1.1       cgd 		}
    238  1.1       cgd 		eofloop = 0;
    239  1.1       cgd 		if (execute(linebuf, 0))
    240  1.1       cgd 			break;
    241  1.1       cgd 	}
    242  1.1       cgd }
    243  1.1       cgd 
    244  1.1       cgd /*
    245  1.1       cgd  * Execute a single command.
    246  1.1       cgd  * Command functions return 0 for success, 1 for error, and -1
    247  1.1       cgd  * for abort.  A 1 or -1 aborts a load or source.  A -1 aborts
    248  1.1       cgd  * the interactive command loop.
    249  1.1       cgd  * Contxt is non-zero if called while composing mail.
    250  1.1       cgd  */
    251  1.4   deraadt int
    252  1.1       cgd execute(linebuf, contxt)
    253  1.1       cgd 	char linebuf[];
    254  1.4   deraadt 	int contxt;
    255  1.1       cgd {
    256  1.1       cgd 	char word[LINESIZE];
    257  1.1       cgd 	char *arglist[MAXARGC];
    258  1.7  christos 	const struct cmd *com = NULL;
    259  1.1       cgd 	register char *cp, *cp2;
    260  1.1       cgd 	register int c;
    261  1.1       cgd 	int muvec[2];
    262  1.1       cgd 	int e = 1;
    263  1.1       cgd 
    264  1.1       cgd 	/*
    265  1.1       cgd 	 * Strip the white space away from the beginning
    266  1.1       cgd 	 * of the command, then scan out a word, which
    267  1.1       cgd 	 * consists of anything except digits and white space.
    268  1.1       cgd 	 *
    269  1.1       cgd 	 * Handle ! escapes differently to get the correct
    270  1.1       cgd 	 * lexical conventions.
    271  1.1       cgd 	 */
    272  1.1       cgd 
    273  1.1       cgd 	for (cp = linebuf; isspace(*cp); cp++)
    274  1.1       cgd 		;
    275  1.1       cgd 	if (*cp == '!') {
    276  1.1       cgd 		if (sourcing) {
    277  1.1       cgd 			printf("Can't \"!\" while sourcing\n");
    278  1.1       cgd 			goto out;
    279  1.1       cgd 		}
    280  1.1       cgd 		shell(cp+1);
    281  1.1       cgd 		return(0);
    282  1.1       cgd 	}
    283  1.1       cgd 	cp2 = word;
    284  1.1       cgd 	while (*cp && index(" \t0123456789$^.:/-+*'\"", *cp) == NOSTR)
    285  1.1       cgd 		*cp2++ = *cp++;
    286  1.1       cgd 	*cp2 = '\0';
    287  1.1       cgd 
    288  1.1       cgd 	/*
    289  1.1       cgd 	 * Look up the command; if not found, bitch.
    290  1.1       cgd 	 * Normally, a blank command would map to the
    291  1.1       cgd 	 * first command in the table; while sourcing,
    292  1.1       cgd 	 * however, we ignore blank lines to eliminate
    293  1.1       cgd 	 * confusion.
    294  1.1       cgd 	 */
    295  1.1       cgd 
    296  1.1       cgd 	if (sourcing && *word == '\0')
    297  1.1       cgd 		return(0);
    298  1.1       cgd 	com = lex(word);
    299  1.1       cgd 	if (com == NONE) {
    300  1.1       cgd 		printf("Unknown command: \"%s\"\n", word);
    301  1.1       cgd 		goto out;
    302  1.1       cgd 	}
    303  1.1       cgd 
    304  1.1       cgd 	/*
    305  1.1       cgd 	 * See if we should execute the command -- if a conditional
    306  1.1       cgd 	 * we always execute it, otherwise, check the state of cond.
    307  1.1       cgd 	 */
    308  1.1       cgd 
    309  1.1       cgd 	if ((com->c_argtype & F) == 0)
    310  1.7  christos 		if ((cond == CRCV && !rcvmode) || (cond == CSEND && rcvmode))
    311  1.1       cgd 			return(0);
    312  1.1       cgd 
    313  1.1       cgd 	/*
    314  1.1       cgd 	 * Process the arguments to the command, depending
    315  1.1       cgd 	 * on the type he expects.  Default to an error.
    316  1.1       cgd 	 * If we are sourcing an interactive command, it's
    317  1.1       cgd 	 * an error.
    318  1.1       cgd 	 */
    319  1.1       cgd 
    320  1.1       cgd 	if (!rcvmode && (com->c_argtype & M) == 0) {
    321  1.1       cgd 		printf("May not execute \"%s\" while sending\n",
    322  1.1       cgd 		    com->c_name);
    323  1.1       cgd 		goto out;
    324  1.1       cgd 	}
    325  1.1       cgd 	if (sourcing && com->c_argtype & I) {
    326  1.1       cgd 		printf("May not execute \"%s\" while sourcing\n",
    327  1.1       cgd 		    com->c_name);
    328  1.1       cgd 		goto out;
    329  1.1       cgd 	}
    330  1.1       cgd 	if (readonly && com->c_argtype & W) {
    331  1.1       cgd 		printf("May not execute \"%s\" -- message file is read only\n",
    332  1.1       cgd 		   com->c_name);
    333  1.1       cgd 		goto out;
    334  1.1       cgd 	}
    335  1.1       cgd 	if (contxt && com->c_argtype & R) {
    336  1.1       cgd 		printf("Cannot recursively invoke \"%s\"\n", com->c_name);
    337  1.1       cgd 		goto out;
    338  1.1       cgd 	}
    339  1.1       cgd 	switch (com->c_argtype & ~(F|P|I|M|T|W|R)) {
    340  1.1       cgd 	case MSGLIST:
    341  1.1       cgd 		/*
    342  1.1       cgd 		 * A message list defaulting to nearest forward
    343  1.1       cgd 		 * legal message.
    344  1.1       cgd 		 */
    345  1.1       cgd 		if (msgvec == 0) {
    346  1.1       cgd 			printf("Illegal use of \"message list\"\n");
    347  1.1       cgd 			break;
    348  1.1       cgd 		}
    349  1.1       cgd 		if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0)
    350  1.1       cgd 			break;
    351  1.1       cgd 		if (c  == 0) {
    352  1.1       cgd 			*msgvec = first(com->c_msgflag,
    353  1.1       cgd 				com->c_msgmask);
    354  1.1       cgd 			msgvec[1] = NULL;
    355  1.1       cgd 		}
    356  1.1       cgd 		if (*msgvec == NULL) {
    357  1.1       cgd 			printf("No applicable messages\n");
    358  1.1       cgd 			break;
    359  1.1       cgd 		}
    360  1.1       cgd 		e = (*com->c_func)(msgvec);
    361  1.1       cgd 		break;
    362  1.1       cgd 
    363  1.1       cgd 	case NDMLIST:
    364  1.1       cgd 		/*
    365  1.1       cgd 		 * A message list with no defaults, but no error
    366  1.1       cgd 		 * if none exist.
    367  1.1       cgd 		 */
    368  1.1       cgd 		if (msgvec == 0) {
    369  1.1       cgd 			printf("Illegal use of \"message list\"\n");
    370  1.1       cgd 			break;
    371  1.1       cgd 		}
    372  1.1       cgd 		if (getmsglist(cp, msgvec, com->c_msgflag) < 0)
    373  1.1       cgd 			break;
    374  1.1       cgd 		e = (*com->c_func)(msgvec);
    375  1.1       cgd 		break;
    376  1.1       cgd 
    377  1.1       cgd 	case STRLIST:
    378  1.1       cgd 		/*
    379  1.1       cgd 		 * Just the straight string, with
    380  1.1       cgd 		 * leading blanks removed.
    381  1.1       cgd 		 */
    382  1.1       cgd 		while (isspace(*cp))
    383  1.1       cgd 			cp++;
    384  1.1       cgd 		e = (*com->c_func)(cp);
    385  1.1       cgd 		break;
    386  1.1       cgd 
    387  1.1       cgd 	case RAWLIST:
    388  1.1       cgd 		/*
    389  1.1       cgd 		 * A vector of strings, in shell style.
    390  1.1       cgd 		 */
    391  1.1       cgd 		if ((c = getrawlist(cp, arglist,
    392  1.1       cgd 				sizeof arglist / sizeof *arglist)) < 0)
    393  1.1       cgd 			break;
    394  1.1       cgd 		if (c < com->c_minargs) {
    395  1.1       cgd 			printf("%s requires at least %d arg(s)\n",
    396  1.1       cgd 				com->c_name, com->c_minargs);
    397  1.1       cgd 			break;
    398  1.1       cgd 		}
    399  1.1       cgd 		if (c > com->c_maxargs) {
    400  1.1       cgd 			printf("%s takes no more than %d arg(s)\n",
    401  1.1       cgd 				com->c_name, com->c_maxargs);
    402  1.1       cgd 			break;
    403  1.1       cgd 		}
    404  1.1       cgd 		e = (*com->c_func)(arglist);
    405  1.1       cgd 		break;
    406  1.1       cgd 
    407  1.1       cgd 	case NOLIST:
    408  1.1       cgd 		/*
    409  1.1       cgd 		 * Just the constant zero, for exiting,
    410  1.1       cgd 		 * eg.
    411  1.1       cgd 		 */
    412  1.1       cgd 		e = (*com->c_func)(0);
    413  1.1       cgd 		break;
    414  1.1       cgd 
    415  1.1       cgd 	default:
    416  1.1       cgd 		panic("Unknown argtype");
    417  1.1       cgd 	}
    418  1.1       cgd 
    419  1.1       cgd out:
    420  1.1       cgd 	/*
    421  1.1       cgd 	 * Exit the current source file on
    422  1.1       cgd 	 * error.
    423  1.1       cgd 	 */
    424  1.1       cgd 	if (e) {
    425  1.1       cgd 		if (e < 0)
    426  1.1       cgd 			return 1;
    427  1.1       cgd 		if (loading)
    428  1.1       cgd 			return 1;
    429  1.1       cgd 		if (sourcing)
    430  1.1       cgd 			unstack();
    431  1.1       cgd 		return 0;
    432  1.1       cgd 	}
    433  1.7  christos 	if (com == NULL)
    434  1.7  christos 		return(0);
    435  1.1       cgd 	if (value("autoprint") != NOSTR && com->c_argtype & P)
    436  1.1       cgd 		if ((dot->m_flag & MDELETED) == 0) {
    437  1.1       cgd 			muvec[0] = dot - &message[0] + 1;
    438  1.1       cgd 			muvec[1] = 0;
    439  1.1       cgd 			type(muvec);
    440  1.1       cgd 		}
    441  1.1       cgd 	if (!sourcing && (com->c_argtype & T) == 0)
    442  1.1       cgd 		sawcom = 1;
    443  1.1       cgd 	return(0);
    444  1.1       cgd }
    445  1.1       cgd 
    446  1.1       cgd /*
    447  1.1       cgd  * Set the size of the message vector used to construct argument
    448  1.1       cgd  * lists to message list functions.
    449  1.1       cgd  */
    450  1.4   deraadt void
    451  1.1       cgd setmsize(sz)
    452  1.4   deraadt 	int sz;
    453  1.1       cgd {
    454  1.1       cgd 
    455  1.1       cgd 	if (msgvec != 0)
    456  1.3       jtc 		free((char *) msgvec);
    457  1.1       cgd 	msgvec = (int *) calloc((unsigned) (sz + 1), sizeof *msgvec);
    458  1.1       cgd }
    459  1.1       cgd 
    460  1.1       cgd /*
    461  1.1       cgd  * Find the correct command in the command table corresponding
    462  1.1       cgd  * to the passed command "word"
    463  1.1       cgd  */
    464  1.1       cgd 
    465  1.6       jtc const struct cmd *
    466  1.1       cgd lex(word)
    467  1.1       cgd 	char word[];
    468  1.1       cgd {
    469  1.6       jtc 	extern const struct cmd cmdtab[];
    470  1.6       jtc 	register const struct cmd *cp;
    471  1.1       cgd 
    472  1.1       cgd 	for (cp = &cmdtab[0]; cp->c_name != NOSTR; cp++)
    473  1.1       cgd 		if (isprefix(word, cp->c_name))
    474  1.1       cgd 			return(cp);
    475  1.1       cgd 	return(NONE);
    476  1.1       cgd }
    477  1.1       cgd 
    478  1.1       cgd /*
    479  1.1       cgd  * Determine if as1 is a valid prefix of as2.
    480  1.1       cgd  * Return true if yep.
    481  1.1       cgd  */
    482  1.4   deraadt int
    483  1.1       cgd isprefix(as1, as2)
    484  1.1       cgd 	char *as1, *as2;
    485  1.1       cgd {
    486  1.1       cgd 	register char *s1, *s2;
    487  1.1       cgd 
    488  1.1       cgd 	s1 = as1;
    489  1.1       cgd 	s2 = as2;
    490  1.1       cgd 	while (*s1++ == *s2)
    491  1.1       cgd 		if (*s2++ == '\0')
    492  1.1       cgd 			return(1);
    493  1.1       cgd 	return(*--s1 == '\0');
    494  1.1       cgd }
    495  1.1       cgd 
    496  1.1       cgd /*
    497  1.1       cgd  * The following gets called on receipt of an interrupt.  This is
    498  1.1       cgd  * to abort printout of a command, mainly.
    499  1.1       cgd  * Dispatching here when command() is inactive crashes rcv.
    500  1.1       cgd  * Close all open files except 0, 1, 2, and the temporary.
    501  1.1       cgd  * Also, unstack all source files.
    502  1.1       cgd  */
    503  1.1       cgd 
    504  1.1       cgd int	inithdr;			/* am printing startup headers */
    505  1.1       cgd 
    506  1.1       cgd /*ARGSUSED*/
    507  1.1       cgd void
    508  1.1       cgd intr(s)
    509  1.4   deraadt 	int s;
    510  1.1       cgd {
    511  1.1       cgd 
    512  1.1       cgd 	noreset = 0;
    513  1.1       cgd 	if (!inithdr)
    514  1.1       cgd 		sawcom++;
    515  1.1       cgd 	inithdr = 0;
    516  1.1       cgd 	while (sourcing)
    517  1.1       cgd 		unstack();
    518  1.1       cgd 
    519  1.1       cgd 	close_all_files();
    520  1.1       cgd 
    521  1.1       cgd 	if (image >= 0) {
    522  1.1       cgd 		close(image);
    523  1.1       cgd 		image = -1;
    524  1.1       cgd 	}
    525  1.1       cgd 	fprintf(stderr, "Interrupt\n");
    526  1.1       cgd 	reset(0);
    527  1.1       cgd }
    528  1.1       cgd 
    529  1.1       cgd /*
    530  1.1       cgd  * When we wake up after ^Z, reprint the prompt.
    531  1.1       cgd  */
    532  1.1       cgd void
    533  1.1       cgd stop(s)
    534  1.4   deraadt 	int s;
    535  1.1       cgd {
    536  1.1       cgd 	sig_t old_action = signal(s, SIG_DFL);
    537  1.7  christos 	sigset_t nset;
    538  1.1       cgd 
    539  1.7  christos 	sigemptyset(&nset);
    540  1.7  christos 	sigaddset(&nset, s);
    541  1.7  christos 	sigprocmask(SIG_UNBLOCK, &nset, NULL);
    542  1.1       cgd 	kill(0, s);
    543  1.7  christos 	sigprocmask(SIG_BLOCK, &nset, NULL);
    544  1.1       cgd 	signal(s, old_action);
    545  1.1       cgd 	if (reset_on_stop) {
    546  1.1       cgd 		reset_on_stop = 0;
    547  1.1       cgd 		reset(0);
    548  1.1       cgd 	}
    549  1.1       cgd }
    550  1.1       cgd 
    551  1.1       cgd /*
    552  1.1       cgd  * Branch here on hangup signal and simulate "exit".
    553  1.1       cgd  */
    554  1.1       cgd /*ARGSUSED*/
    555  1.1       cgd void
    556  1.1       cgd hangup(s)
    557  1.4   deraadt 	int s;
    558  1.1       cgd {
    559  1.1       cgd 
    560  1.1       cgd 	/* nothing to do? */
    561  1.1       cgd 	exit(1);
    562  1.1       cgd }
    563  1.1       cgd 
    564  1.1       cgd /*
    565  1.1       cgd  * Announce the presence of the current Mail version,
    566  1.1       cgd  * give the message count, and print a header listing.
    567  1.1       cgd  */
    568  1.4   deraadt void
    569  1.1       cgd announce()
    570  1.1       cgd {
    571  1.1       cgd 	int vec[2], mdot;
    572  1.1       cgd 
    573  1.1       cgd 	mdot = newfileinfo();
    574  1.1       cgd 	vec[0] = mdot;
    575  1.1       cgd 	vec[1] = 0;
    576  1.1       cgd 	dot = &message[mdot - 1];
    577  1.1       cgd 	if (msgCount > 0 && value("noheader") == NOSTR) {
    578  1.1       cgd 		inithdr++;
    579  1.1       cgd 		headers(vec);
    580  1.1       cgd 		inithdr = 0;
    581  1.1       cgd 	}
    582  1.1       cgd }
    583  1.1       cgd 
    584  1.1       cgd /*
    585  1.1       cgd  * Announce information about the file we are editing.
    586  1.1       cgd  * Return a likely place to set dot.
    587  1.1       cgd  */
    588  1.4   deraadt int
    589  1.1       cgd newfileinfo()
    590  1.1       cgd {
    591  1.1       cgd 	register struct message *mp;
    592  1.1       cgd 	register int u, n, mdot, d, s;
    593  1.1       cgd 	char fname[BUFSIZ], zname[BUFSIZ], *ename;
    594  1.1       cgd 
    595  1.1       cgd 	for (mp = &message[0]; mp < &message[msgCount]; mp++)
    596  1.1       cgd 		if (mp->m_flag & MNEW)
    597  1.1       cgd 			break;
    598  1.1       cgd 	if (mp >= &message[msgCount])
    599  1.1       cgd 		for (mp = &message[0]; mp < &message[msgCount]; mp++)
    600  1.1       cgd 			if ((mp->m_flag & MREAD) == 0)
    601  1.1       cgd 				break;
    602  1.1       cgd 	if (mp < &message[msgCount])
    603  1.1       cgd 		mdot = mp - &message[0] + 1;
    604  1.1       cgd 	else
    605  1.1       cgd 		mdot = 1;
    606  1.1       cgd 	s = d = 0;
    607  1.1       cgd 	for (mp = &message[0], n = 0, u = 0; mp < &message[msgCount]; mp++) {
    608  1.1       cgd 		if (mp->m_flag & MNEW)
    609  1.1       cgd 			n++;
    610  1.1       cgd 		if ((mp->m_flag & MREAD) == 0)
    611  1.1       cgd 			u++;
    612  1.1       cgd 		if (mp->m_flag & MDELETED)
    613  1.1       cgd 			d++;
    614  1.1       cgd 		if (mp->m_flag & MSAVED)
    615  1.1       cgd 			s++;
    616  1.1       cgd 	}
    617  1.1       cgd 	ename = mailname;
    618  1.1       cgd 	if (getfold(fname) >= 0) {
    619  1.1       cgd 		strcat(fname, "/");
    620  1.1       cgd 		if (strncmp(fname, mailname, strlen(fname)) == 0) {
    621  1.1       cgd 			sprintf(zname, "+%s", mailname + strlen(fname));
    622  1.1       cgd 			ename = zname;
    623  1.1       cgd 		}
    624  1.1       cgd 	}
    625  1.1       cgd 	printf("\"%s\": ", ename);
    626  1.1       cgd 	if (msgCount == 1)
    627  1.1       cgd 		printf("1 message");
    628  1.1       cgd 	else
    629  1.1       cgd 		printf("%d messages", msgCount);
    630  1.1       cgd 	if (n > 0)
    631  1.1       cgd 		printf(" %d new", n);
    632  1.1       cgd 	if (u-n > 0)
    633  1.1       cgd 		printf(" %d unread", u);
    634  1.1       cgd 	if (d > 0)
    635  1.1       cgd 		printf(" %d deleted", d);
    636  1.1       cgd 	if (s > 0)
    637  1.1       cgd 		printf(" %d saved", s);
    638  1.1       cgd 	if (readonly)
    639  1.1       cgd 		printf(" [Read only]");
    640  1.1       cgd 	printf("\n");
    641  1.1       cgd 	return(mdot);
    642  1.1       cgd }
    643  1.1       cgd 
    644  1.1       cgd /*
    645  1.1       cgd  * Print the current version number.
    646  1.1       cgd  */
    647  1.1       cgd 
    648  1.1       cgd /*ARGSUSED*/
    649  1.4   deraadt int
    650  1.7  christos pversion(v)
    651  1.7  christos 	void *v;
    652  1.1       cgd {
    653  1.1       cgd 	extern char *version;
    654  1.1       cgd 
    655  1.1       cgd 	printf("Version %s\n", version);
    656  1.1       cgd 	return(0);
    657  1.1       cgd }
    658  1.1       cgd 
    659  1.1       cgd /*
    660  1.1       cgd  * Load a file of user definitions.
    661  1.1       cgd  */
    662  1.4   deraadt void
    663  1.1       cgd load(name)
    664  1.1       cgd 	char *name;
    665  1.1       cgd {
    666  1.1       cgd 	register FILE *in, *oldin;
    667  1.1       cgd 
    668  1.1       cgd 	if ((in = Fopen(name, "r")) == NULL)
    669  1.1       cgd 		return;
    670  1.1       cgd 	oldin = input;
    671  1.1       cgd 	input = in;
    672  1.1       cgd 	loading = 1;
    673  1.1       cgd 	sourcing = 1;
    674  1.1       cgd 	commands();
    675  1.1       cgd 	loading = 0;
    676  1.1       cgd 	sourcing = 0;
    677  1.1       cgd 	input = oldin;
    678  1.1       cgd 	Fclose(in);
    679  1.1       cgd }
    680