Home | History | Annotate | Line # | Download | only in mail
cmd2.c revision 1.19
      1 /*	$NetBSD: cmd2.c,v 1.19 2005/07/19 23:07:10 christos 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)cmd2.c	8.1 (Berkeley) 6/6/93";
     36 #else
     37 __RCSID("$NetBSD: cmd2.c,v 1.19 2005/07/19 23:07:10 christos Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 #include "rcv.h"
     42 #include "extern.h"
     43 
     44 /*
     45  * Mail -- a mail program
     46  *
     47  * More user commands.
     48  */
     49 static int igcomp(const void *, const void *);
     50 
     51 /*
     52  * If any arguments were given, go to the next applicable argument
     53  * following dot, otherwise, go to the next applicable message.
     54  * If given as first command with no arguments, print first message.
     55  */
     56 int
     57 next(void *v)
     58 {
     59 	int *msgvec = v;
     60 	struct message *mp;
     61 	int *ip, *ip2;
     62 	int list[2], mdot;
     63 
     64 	if (*msgvec != 0) {
     65 
     66 		/*
     67 		 * If some messages were supplied, find the
     68 		 * first applicable one following dot using
     69 		 * wrap around.
     70 		 */
     71 
     72 		mdot = dot - &message[0] + 1;
     73 
     74 		/*
     75 		 * Find the first message in the supplied
     76 		 * message list which follows dot.
     77 		 */
     78 
     79 		for (ip = msgvec; *ip != 0; ip++)
     80 			if (*ip > mdot)
     81 				break;
     82 		if (*ip == 0)
     83 			ip = msgvec;
     84 		ip2 = ip;
     85 		do {
     86 			mp = &message[*ip2 - 1];
     87 			if ((mp->m_flag & MDELETED) == 0) {
     88 				dot = mp;
     89 				goto hitit;
     90 			}
     91 			if (*ip2 != 0)
     92 				ip2++;
     93 			if (*ip2 == 0)
     94 				ip2 = msgvec;
     95 		} while (ip2 != ip);
     96 		(void)printf("No messages applicable\n");
     97 		return(1);
     98 	}
     99 
    100 	/*
    101 	 * If this is the first command, select message 1.
    102 	 * Note that this must exist for us to get here at all.
    103 	 */
    104 
    105 	if (!sawcom)
    106 		goto hitit;
    107 
    108 	/*
    109 	 * Just find the next good message after dot, no
    110 	 * wraparound.
    111 	 */
    112 
    113 	for (mp = dot+1; mp < &message[msgCount]; mp++)
    114 		if ((mp->m_flag & (MDELETED|MSAVED)) == 0)
    115 			break;
    116 	if (mp >= &message[msgCount]) {
    117 		(void)printf("At EOF\n");
    118 		return(0);
    119 	}
    120 	dot = mp;
    121 hitit:
    122 	/*
    123 	 * Print dot.
    124 	 */
    125 
    126 	list[0] = dot - &message[0] + 1;
    127 	list[1] = 0;
    128 	return(type(list));
    129 }
    130 
    131 /*
    132  * Save a message in a file.  Mark the message as saved
    133  * so we can discard when the user quits.
    134  */
    135 int
    136 save(void *v)
    137 {
    138 	char *str = v;
    139 
    140 	return save1(str, 1, "save", saveignore);
    141 }
    142 
    143 /*
    144  * Save a message in a file.  Mark the message as saved
    145  * so we can discard when the user quits.  Save all fields
    146  * overriding saveignore and saveretain.
    147  */
    148 int
    149 Save(v)
    150 	void *v;
    151 {
    152 	char *str = v;
    153 
    154 	return save1(str, 1, "Save", NULL);
    155 }
    156 
    157 /*
    158  * Copy a message to a file without affected its saved-ness
    159  */
    160 int
    161 copycmd(void *v)
    162 {
    163 	char *str = v;
    164 
    165 	return save1(str, 0, "copy", saveignore);
    166 }
    167 
    168 /*
    169  * Save/copy the indicated messages at the end of the passed file name.
    170  * If markmsg is true, mark the message "saved."
    171  */
    172 int
    173 save1(char str[], int markmsg, const char *cmd, struct ignoretab *ignoretabs)
    174 {
    175 	int *ip;
    176 	struct message *mp;
    177 	const char *fn;
    178 	const char *disp;
    179 	int f, *msgvec;
    180 	FILE *obuf;
    181 
    182 	msgvec = salloc((msgCount + 2) * sizeof *msgvec);
    183 	if ((fn = snarf(str, &f)) == NULL)
    184 		return(1);
    185 	if (!f) {
    186 		*msgvec = first(0, MMNORM);
    187 		if (*msgvec == 0) {
    188 			(void)printf("No messages to %s.\n", cmd);
    189 			return(1);
    190 		}
    191 		msgvec[1] = 0;
    192 	}
    193 	if (f && getmsglist(str, msgvec, 0) < 0)
    194 		return(1);
    195 	if ((fn = expand(fn)) == NULL)
    196 		return(1);
    197 	(void)printf("\"%s\" ", fn);
    198 	(void)fflush(stdout);
    199 	if (access(fn, 0) >= 0)
    200 		disp = "[Appended]";
    201 	else
    202 		disp = "[New file]";
    203 	if ((obuf = Fopen(fn, "a")) == NULL) {
    204 		warn(NULL);
    205 		return(1);
    206 	}
    207 	for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
    208 		mp = &message[*ip - 1];
    209 		touch(mp);
    210 		if (sendmessage(mp, obuf, ignoretabs, NULL) < 0) {
    211 			warn("%s", fn);
    212 			(void)Fclose(obuf);
    213 			return(1);
    214 		}
    215 		if (markmsg)
    216 			mp->m_flag |= MSAVED;
    217 	}
    218 	(void)fflush(obuf);
    219 	if (ferror(obuf))
    220 		warn("%s", fn);
    221 	(void)Fclose(obuf);
    222 	(void)printf("%s\n", disp);
    223 	return(0);
    224 }
    225 
    226 /*
    227  * Write the indicated messages at the end of the passed
    228  * file name, minus header and trailing blank line.
    229  */
    230 int
    231 swrite(void *v)
    232 {
    233 	char *str = v;
    234 
    235 	return save1(str, 1, "write", ignoreall);
    236 }
    237 
    238 /*
    239  * Snarf the file from the end of the command line and
    240  * return a pointer to it.  If there is no file attached,
    241  * just return NULL.  Put a null in front of the file
    242  * name so that the message list processing won't see it,
    243  * unless the file name is the only thing on the line, in
    244  * which case, return 0 in the reference flag variable.
    245  */
    246 
    247 char *
    248 snarf(char linebuf[], int *flag)
    249 {
    250 	char *cp;
    251 
    252 	*flag = 1;
    253 	cp = strlen(linebuf) + linebuf - 1;
    254 
    255 	/*
    256 	 * Strip away trailing blanks.
    257 	 */
    258 
    259 	while (cp > linebuf && isspace((unsigned char)*cp))
    260 		cp--;
    261 	*++cp = 0;
    262 
    263 	/*
    264 	 * Now search for the beginning of the file name.
    265 	 */
    266 
    267 	while (cp > linebuf && !isspace((unsigned char)*cp))
    268 		cp--;
    269 	if (*cp == '\0') {
    270 		(void)printf("No file specified.\n");
    271 		return(NULL);
    272 	}
    273 	if (isspace((unsigned char)*cp))
    274 		*cp++ = 0;
    275 	else
    276 		*flag = 0;
    277 	return(cp);
    278 }
    279 
    280 /*
    281  * Delete messages.
    282  */
    283 int
    284 delete(void *v)
    285 {
    286 	int *msgvec = v;
    287 	(void)delm(msgvec);
    288 	return 0;
    289 }
    290 
    291 /*
    292  * Delete messages, then type the new dot.
    293  */
    294 int
    295 deltype(void *v)
    296 {
    297 	int *msgvec = v;
    298 	int list[2];
    299 	int lastdot;
    300 
    301 	lastdot = dot - &message[0] + 1;
    302 	if (delm(msgvec) >= 0) {
    303 		list[0] = dot - &message[0] + 1;
    304 		if (list[0] > lastdot) {
    305 			touch(dot);
    306 			list[1] = 0;
    307 			return(type(list));
    308 		}
    309 		(void)printf("At EOF\n");
    310 	} else
    311 		(void)printf("No more messages\n");
    312 	return(0);
    313 }
    314 
    315 /*
    316  * Delete the indicated messages.
    317  * Set dot to some nice place afterwards.
    318  * Internal interface.
    319  */
    320 int
    321 delm(int *msgvec)
    322 {
    323 	struct message *mp;
    324 	int *ip;
    325 	int last;
    326 
    327 	last = 0;
    328 	for (ip = msgvec; *ip != 0; ip++) {
    329 		mp = &message[*ip - 1];
    330 		touch(mp);
    331 		mp->m_flag |= MDELETED|MTOUCH;
    332 		mp->m_flag &= ~(MPRESERVE|MSAVED|MBOX);
    333 		last = *ip;
    334 	}
    335 	if (last != 0) {
    336 		dot = &message[last-1];
    337 		last = first(0, MDELETED);
    338 		if (last != 0) {
    339 			dot = &message[last-1];
    340 			return(0);
    341 		}
    342 		else {
    343 			dot = &message[0];
    344 			return(-1);
    345 		}
    346 	}
    347 
    348 	/*
    349 	 * Following can't happen -- it keeps lint happy
    350 	 */
    351 
    352 	return(-1);
    353 }
    354 
    355 /*
    356  * Undelete the indicated messages.
    357  */
    358 int
    359 undeletecmd(void *v)
    360 {
    361 	int *msgvec = v;
    362 	struct message *mp;
    363 	int *ip;
    364 
    365 	for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
    366 		mp = &message[*ip - 1];
    367 		touch(mp);
    368 		dot = mp;
    369 		mp->m_flag &= ~MDELETED;
    370 	}
    371 	return 0;
    372 }
    373 
    374 /*
    375  * Interactively dump core on "core"
    376  */
    377 int
    378 /*ARGSUSED*/
    379 core(void *v)
    380 {
    381 	int pid;
    382 
    383 	switch (pid = vfork()) {
    384 	case -1:
    385 		warn("fork");
    386 		return(1);
    387 	case 0:
    388 		abort();
    389 		_exit(1);
    390 	}
    391 	(void)printf("Okie dokie");
    392 	(void)fflush(stdout);
    393 	(void)wait_child(pid);
    394 	if (WCOREDUMP(wait_status))
    395 		(void)printf(" -- Core dumped.\n");
    396 	else
    397 		(void)printf(" -- Can't dump core.\n");
    398 	return 0;
    399 }
    400 
    401 /*
    402  * Clobber as many bytes of stack as the user requests.
    403  */
    404 int
    405 clobber(void *v)
    406 {
    407 	char **argv = v;
    408 	int times;
    409 
    410 	if (argv[0] == 0)
    411 		times = 1;
    412 	else
    413 		times = (atoi(argv[0]) + 511) / 512;
    414 	clob1(times);
    415 	return 0;
    416 }
    417 
    418 /*
    419  * Clobber the stack.
    420  */
    421 void
    422 clob1(int n)
    423 {
    424 	char buf[512];
    425 	char *cp;
    426 
    427 	if (n <= 0)
    428 		return;
    429 	for (cp = buf; cp < &buf[512]; *cp++ = (char)0xFF)
    430 		;
    431 	clob1(n - 1);
    432 }
    433 
    434 /*
    435  * Add the given header fields to the retained list.
    436  * If no arguments, print the current list of retained fields.
    437  */
    438 int
    439 retfield(void *v)
    440 {
    441 	char **list = v;
    442 
    443 	return ignore1(list, ignore + 1, "retained");
    444 }
    445 
    446 /*
    447  * Add the given header fields to the ignored list.
    448  * If no arguments, print the current list of ignored fields.
    449  */
    450 int
    451 igfield(void *v)
    452 {
    453 	char **list = v;
    454 
    455 	return ignore1(list, ignore, "ignored");
    456 }
    457 
    458 int
    459 saveretfield(void *v)
    460 {
    461 	char **list = v;
    462 
    463 	return ignore1(list, saveignore + 1, "retained");
    464 }
    465 
    466 int
    467 saveigfield(void *v)
    468 {
    469 	char **list = v;
    470 
    471 	return ignore1(list, saveignore, "ignored");
    472 }
    473 
    474 int
    475 ignore1(char *list[], struct ignoretab *tab, const char *which)
    476 {
    477 	char field[LINESIZE];
    478 	int h;
    479 	struct ignore *igp;
    480 	char **ap;
    481 
    482 	if (*list == NULL)
    483 		return igshow(tab, which);
    484 	for (ap = list; *ap != 0; ap++) {
    485 		istrcpy(field, *ap);
    486 		if (member(field, tab))
    487 			continue;
    488 		h = hash(field);
    489 		igp = (struct ignore *) calloc(1, sizeof (struct ignore));
    490 		igp->i_field = calloc((unsigned) strlen(field) + 1,
    491 			sizeof (char));
    492 		(void)strcpy(igp->i_field, field);
    493 		igp->i_link = tab->i_head[h];
    494 		tab->i_head[h] = igp;
    495 		tab->i_count++;
    496 	}
    497 	return 0;
    498 }
    499 
    500 /*
    501  * Print out all currently retained fields.
    502  */
    503 int
    504 igshow(struct ignoretab *tab, const char *which)
    505 {
    506 	int h;
    507 	struct ignore *igp;
    508 	char **ap, **ring;
    509 
    510 	if (tab->i_count == 0) {
    511 		(void)printf("No fields currently being %s.\n", which);
    512 		return 0;
    513 	}
    514 	ring = salloc((tab->i_count + 1) * sizeof (char *));
    515 	ap = ring;
    516 	for (h = 0; h < HSHSIZE; h++)
    517 		for (igp = tab->i_head[h]; igp != 0; igp = igp->i_link)
    518 			*ap++ = igp->i_field;
    519 	*ap = 0;
    520 	qsort(ring, tab->i_count, sizeof (char *), igcomp);
    521 	for (ap = ring; *ap != 0; ap++)
    522 		(void)printf("%s\n", *ap);
    523 	return 0;
    524 }
    525 
    526 /*
    527  * Compare two names for sorting ignored field list.
    528  */
    529 static int
    530 igcomp(const void *l, const void *r)
    531 {
    532 	return (strcmp(*(const char *const *)l, *(const char *const *)r));
    533 }
    534