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