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