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