Home | History | Annotate | Line # | Download | only in mail
cmd3.c revision 1.29
      1  1.29  christos /*	$NetBSD: cmd3.c,v 1.29 2006/09/18 19:46:21 christos Exp $	*/
      2   1.5  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.21       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32   1.9     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34   1.5  christos #if 0
     35   1.6       tls static char sccsid[] = "@(#)cmd3.c	8.2 (Berkeley) 4/20/95";
     36   1.5  christos #else
     37  1.29  christos __RCSID("$NetBSD: cmd3.c,v 1.29 2006/09/18 19:46:21 christos Exp $");
     38   1.5  christos #endif
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41   1.1       cgd #include "rcv.h"
     42   1.4   deraadt #include "extern.h"
     43   1.1       cgd 
     44   1.1       cgd /*
     45   1.1       cgd  * Mail -- a mail program
     46   1.1       cgd  *
     47   1.1       cgd  * Still more user commands.
     48   1.1       cgd  */
     49  1.28  christos static int delgroup(const char *);
     50  1.12       wiz static int diction(const void *, const void *);
     51   1.1       cgd 
     52   1.1       cgd /*
     53   1.1       cgd  * Process a shell escape by saving signals, ignoring signals,
     54   1.1       cgd  * and forking a sh -c
     55   1.1       cgd  */
     56   1.4   deraadt int
     57  1.12       wiz shell(void *v)
     58   1.1       cgd {
     59   1.5  christos 	char *str = v;
     60   1.1       cgd 	sig_t sigint = signal(SIGINT, SIG_IGN);
     61  1.26  christos 	const char *shellcmd;
     62   1.1       cgd 	char cmd[BUFSIZ];
     63   1.1       cgd 
     64  1.16       wiz 	(void)strcpy(cmd, str);
     65   1.1       cgd 	if (bangexp(cmd) < 0)
     66   1.1       cgd 		return 1;
     67  1.14       wiz 	if ((shellcmd = value("SHELL")) == NULL)
     68  1.13       wiz 		shellcmd = _PATH_CSHELL;
     69  1.22      ross 	(void)run_command(shellcmd, 0, 0, 1, "-c", cmd, NULL);
     70  1.16       wiz 	(void)signal(SIGINT, sigint);
     71  1.27  christos 	(void)printf("!\n");
     72   1.1       cgd 	return 0;
     73   1.1       cgd }
     74   1.1       cgd 
     75   1.1       cgd /*
     76   1.1       cgd  * Fork an interactive shell.
     77   1.1       cgd  */
     78   1.1       cgd /*ARGSUSED*/
     79   1.4   deraadt int
     80  1.12       wiz dosh(void *v)
     81   1.1       cgd {
     82   1.1       cgd 	sig_t sigint = signal(SIGINT, SIG_IGN);
     83  1.26  christos 	const char *shellcmd;
     84   1.1       cgd 
     85  1.14       wiz 	if ((shellcmd = value("SHELL")) == NULL)
     86  1.13       wiz 		shellcmd = _PATH_CSHELL;
     87  1.22      ross 	(void)run_command(shellcmd, 0, 0, 1, NULL);
     88  1.16       wiz 	(void)signal(SIGINT, sigint);
     89  1.27  christos 	(void)putchar('\n');
     90   1.1       cgd 	return 0;
     91   1.1       cgd }
     92   1.1       cgd 
     93   1.1       cgd /*
     94   1.1       cgd  * Expand the shell escape by expanding unescaped !'s into the
     95   1.1       cgd  * last issued command where possible.
     96   1.1       cgd  */
     97   1.1       cgd 
     98   1.1       cgd char	lastbang[128];
     99   1.1       cgd 
    100   1.4   deraadt int
    101  1.12       wiz bangexp(char *str)
    102   1.1       cgd {
    103   1.1       cgd 	char bangbuf[BUFSIZ];
    104   1.9     lukem 	char *cp, *cp2;
    105   1.9     lukem 	int n;
    106   1.1       cgd 	int changed = 0;
    107   1.1       cgd 
    108   1.1       cgd 	cp = str;
    109   1.1       cgd 	cp2 = bangbuf;
    110   1.1       cgd 	n = BUFSIZ;
    111   1.1       cgd 	while (*cp) {
    112   1.1       cgd 		if (*cp == '!') {
    113   1.1       cgd 			if (n < strlen(lastbang)) {
    114   1.1       cgd overf:
    115  1.27  christos 				(void)printf("Command buffer overflow\n");
    116   1.1       cgd 				return(-1);
    117   1.1       cgd 			}
    118   1.1       cgd 			changed++;
    119  1.27  christos 			(void)strcpy(cp2, lastbang);
    120   1.1       cgd 			cp2 += strlen(lastbang);
    121   1.1       cgd 			n -= strlen(lastbang);
    122   1.1       cgd 			cp++;
    123   1.1       cgd 			continue;
    124   1.1       cgd 		}
    125   1.1       cgd 		if (*cp == '\\' && cp[1] == '!') {
    126   1.1       cgd 			if (--n <= 1)
    127   1.1       cgd 				goto overf;
    128   1.1       cgd 			*cp2++ = '!';
    129   1.1       cgd 			cp += 2;
    130   1.1       cgd 			changed++;
    131   1.1       cgd 		}
    132   1.1       cgd 		if (--n <= 1)
    133   1.1       cgd 			goto overf;
    134   1.1       cgd 		*cp2++ = *cp++;
    135   1.1       cgd 	}
    136   1.1       cgd 	*cp2 = 0;
    137   1.1       cgd 	if (changed) {
    138  1.27  christos 		(void)printf("!%s\n", bangbuf);
    139  1.27  christos 		(void)fflush(stdout);
    140   1.1       cgd 	}
    141  1.27  christos 	(void)strcpy(str, bangbuf);
    142  1.27  christos 	(void)strncpy(lastbang, bangbuf, 128);
    143   1.1       cgd 	lastbang[127] = 0;
    144   1.1       cgd 	return(0);
    145   1.1       cgd }
    146   1.1       cgd 
    147   1.1       cgd /*
    148   1.1       cgd  * Print out a nice help message from some file or another.
    149   1.1       cgd  */
    150   1.1       cgd 
    151   1.4   deraadt int
    152  1.27  christos /*ARGSUSED*/
    153  1.12       wiz help(void *v)
    154   1.1       cgd {
    155   1.9     lukem 	int c;
    156   1.9     lukem 	FILE *f;
    157   1.1       cgd 
    158   1.1       cgd 	if ((f = Fopen(_PATH_HELP, "r")) == NULL) {
    159  1.17       wiz 		warn(_PATH_HELP);
    160   1.1       cgd 		return(1);
    161   1.1       cgd 	}
    162   1.1       cgd 	while ((c = getc(f)) != EOF)
    163  1.27  christos 		(void)putchar(c);
    164  1.27  christos 	(void)Fclose(f);
    165   1.1       cgd 	return(0);
    166   1.1       cgd }
    167   1.1       cgd 
    168   1.1       cgd /*
    169   1.1       cgd  * Change user's working directory.
    170   1.1       cgd  */
    171   1.4   deraadt int
    172  1.12       wiz schdir(void *v)
    173   1.1       cgd {
    174   1.5  christos 	char **arglist = v;
    175  1.26  christos 	const char *cp;
    176   1.1       cgd 
    177  1.14       wiz 	if (*arglist == NULL)
    178   1.1       cgd 		cp = homedir;
    179   1.1       cgd 	else
    180  1.14       wiz 		if ((cp = expand(*arglist)) == NULL)
    181   1.1       cgd 			return(1);
    182   1.1       cgd 	if (chdir(cp) < 0) {
    183  1.17       wiz 		warn("%s", cp);
    184   1.1       cgd 		return(1);
    185   1.1       cgd 	}
    186   1.1       cgd 	return 0;
    187   1.1       cgd }
    188   1.1       cgd 
    189   1.4   deraadt int
    190  1.12       wiz respond(void *v)
    191   1.1       cgd {
    192   1.5  christos 	int *msgvec = v;
    193  1.14       wiz 	if (value("Replyall") == NULL)
    194   1.1       cgd 		return (_respond(msgvec));
    195   1.1       cgd 	else
    196   1.1       cgd 		return (_Respond(msgvec));
    197   1.1       cgd }
    198   1.1       cgd 
    199  1.29  christos static struct name *
    200  1.29  christos set_smopts(struct message *mp)
    201  1.29  christos {
    202  1.29  christos 	char *cp;
    203  1.29  christos 	struct name *np = NULL;
    204  1.29  christos 	char *reply_from = value("ReplyFrom");
    205  1.29  christos 
    206  1.29  christos 	if (reply_from &&
    207  1.29  christos 	    (cp = skin(hfield("to", mp))) != NULL &&
    208  1.29  christos 	    extract(cp, GTO)->n_flink == NULL) {  /* check for one recepient */
    209  1.29  christos 		char *p, *q;
    210  1.29  christos 		int len = strlen(cp);
    211  1.29  christos 		for (p = q = reply_from ; *p ; p = q) {
    212  1.29  christos 			while (*q != '\0' && *q != ',' && *q != ' ' && *q != '\t')
    213  1.29  christos 				q++;
    214  1.29  christos 			if (p + len == q && strncmp(cp, p, len) == 0)
    215  1.29  christos 				return np;
    216  1.29  christos 			while (*q == ',' || *q == ' ' || *q == '\t')
    217  1.29  christos 				q++;
    218  1.29  christos 		}
    219  1.29  christos 		np = extract(__UNCONST("-f"), GSMOPTS);
    220  1.29  christos 		np = cat(np, extract(cp, GSMOPTS));
    221  1.29  christos 	}
    222  1.29  christos 
    223  1.29  christos 	return np;
    224  1.29  christos }
    225  1.29  christos 
    226   1.1       cgd /*
    227   1.1       cgd  * Reply to a list of messages.  Extract each name from the
    228   1.1       cgd  * message header and send them off to mail1()
    229   1.1       cgd  */
    230   1.4   deraadt int
    231  1.12       wiz _respond(int *msgvec)
    232   1.1       cgd {
    233   1.1       cgd 	struct message *mp;
    234   1.1       cgd 	char *cp, *rcv, *replyto;
    235   1.1       cgd 	char **ap;
    236   1.1       cgd 	struct name *np;
    237   1.1       cgd 	struct header head;
    238   1.1       cgd 
    239   1.1       cgd 	if (msgvec[1] != 0) {
    240  1.27  christos 		(void)printf("Sorry, can't reply to multiple messages at once\n");
    241   1.1       cgd 		return(1);
    242   1.1       cgd 	}
    243   1.1       cgd 	mp = &message[msgvec[0] - 1];
    244   1.1       cgd 	touch(mp);
    245   1.1       cgd 	dot = mp;
    246  1.14       wiz 	if ((rcv = skin(hfield("from", mp))) == NULL)
    247   1.1       cgd 		rcv = skin(nameof(mp, 1));
    248  1.14       wiz 	if ((replyto = skin(hfield("reply-to", mp))) != NULL)
    249   1.1       cgd 		np = extract(replyto, GTO);
    250  1.14       wiz 	else if ((cp = skin(hfield("to", mp))) != NULL)
    251   1.1       cgd 		np = extract(cp, GTO);
    252   1.1       cgd 	else
    253  1.15       wiz 		np = NULL;
    254   1.1       cgd 	np = elide(np);
    255   1.1       cgd 	/*
    256   1.1       cgd 	 * Delete my name from the reply list,
    257   1.1       cgd 	 * and with it, all my alternate names.
    258   1.1       cgd 	 */
    259   1.1       cgd 	np = delname(np, myname);
    260   1.1       cgd 	if (altnames)
    261   1.1       cgd 		for (ap = altnames; *ap; ap++)
    262   1.1       cgd 			np = delname(np, *ap);
    263  1.15       wiz 	if (np != NULL && replyto == NULL)
    264   1.1       cgd 		np = cat(np, extract(rcv, GTO));
    265  1.15       wiz 	else if (np == NULL) {
    266  1.14       wiz 		if (replyto != NULL)
    267  1.27  christos 			(void)printf("Empty reply-to field -- replying to author\n");
    268   1.1       cgd 		np = extract(rcv, GTO);
    269   1.1       cgd 	}
    270   1.1       cgd 	head.h_to = np;
    271  1.14       wiz 	if ((head.h_subject = hfield("subject", mp)) == NULL)
    272   1.1       cgd 		head.h_subject = hfield("subj", mp);
    273   1.1       cgd 	head.h_subject = reedit(head.h_subject);
    274  1.14       wiz 	if (replyto == NULL && (cp = skin(hfield("cc", mp))) != NULL) {
    275   1.1       cgd 		np = elide(extract(cp, GCC));
    276   1.1       cgd 		np = delname(np, myname);
    277   1.1       cgd 		if (altnames != 0)
    278   1.1       cgd 			for (ap = altnames; *ap; ap++)
    279   1.1       cgd 				np = delname(np, *ap);
    280   1.1       cgd 		head.h_cc = np;
    281   1.1       cgd 	} else
    282  1.15       wiz 		head.h_cc = NULL;
    283  1.15       wiz 	head.h_bcc = NULL;
    284  1.29  christos 	head.h_smopts = set_smopts(mp);
    285   1.1       cgd 	mail1(&head, 1);
    286   1.1       cgd 	return(0);
    287   1.1       cgd }
    288   1.1       cgd 
    289   1.1       cgd /*
    290   1.1       cgd  * Modify the subject we are replying to to begin with Re: if
    291   1.1       cgd  * it does not already.
    292   1.1       cgd  */
    293   1.1       cgd char *
    294  1.12       wiz reedit(char *subj)
    295   1.1       cgd {
    296   1.1       cgd 	char *newsubj;
    297   1.1       cgd 
    298  1.14       wiz 	if (subj == NULL)
    299  1.14       wiz 		return NULL;
    300   1.1       cgd 	if ((subj[0] == 'r' || subj[0] == 'R') &&
    301   1.1       cgd 	    (subj[1] == 'e' || subj[1] == 'E') &&
    302   1.1       cgd 	    subj[2] == ':')
    303   1.1       cgd 		return subj;
    304   1.1       cgd 	newsubj = salloc(strlen(subj) + 5);
    305  1.27  christos 	(void)strcpy(newsubj, "Re: ");
    306  1.27  christos 	(void)strcpy(newsubj + 4, subj);
    307   1.1       cgd 	return newsubj;
    308   1.1       cgd }
    309   1.1       cgd 
    310   1.1       cgd /*
    311   1.1       cgd  * Preserve the named messages, so that they will be sent
    312   1.1       cgd  * back to the system mailbox.
    313   1.1       cgd  */
    314   1.4   deraadt int
    315  1.12       wiz preserve(void *v)
    316   1.1       cgd {
    317   1.5  christos 	int *msgvec = v;
    318   1.9     lukem 	struct message *mp;
    319   1.9     lukem 	int *ip, mesg;
    320   1.1       cgd 
    321   1.1       cgd 	if (edit) {
    322  1.27  christos 		(void)printf("Cannot \"preserve\" in edit mode\n");
    323   1.1       cgd 		return(1);
    324   1.1       cgd 	}
    325   1.7        pk 	for (ip = msgvec; *ip != 0; ip++) {
    326   1.1       cgd 		mesg = *ip;
    327   1.1       cgd 		mp = &message[mesg-1];
    328   1.1       cgd 		mp->m_flag |= MPRESERVE;
    329   1.1       cgd 		mp->m_flag &= ~MBOX;
    330   1.1       cgd 		dot = mp;
    331   1.1       cgd 	}
    332   1.1       cgd 	return(0);
    333   1.1       cgd }
    334   1.1       cgd 
    335   1.1       cgd /*
    336   1.1       cgd  * Mark all given messages as unread.
    337   1.1       cgd  */
    338   1.4   deraadt int
    339  1.12       wiz unread(void *v)
    340   1.1       cgd {
    341   1.9     lukem 	int *msgvec = v;
    342   1.9     lukem 	int *ip;
    343   1.1       cgd 
    344   1.7        pk 	for (ip = msgvec; *ip != 0; ip++) {
    345   1.1       cgd 		dot = &message[*ip-1];
    346   1.1       cgd 		dot->m_flag &= ~(MREAD|MTOUCH);
    347   1.1       cgd 		dot->m_flag |= MSTATUS;
    348   1.1       cgd 	}
    349   1.1       cgd 	return(0);
    350   1.1       cgd }
    351   1.1       cgd 
    352   1.1       cgd /*
    353   1.1       cgd  * Print the size of each message.
    354   1.1       cgd  */
    355   1.4   deraadt int
    356  1.12       wiz messize(void *v)
    357   1.1       cgd {
    358   1.5  christos 	int *msgvec = v;
    359   1.9     lukem 	struct message *mp;
    360   1.9     lukem 	int *ip, mesg;
    361   1.1       cgd 
    362   1.7        pk 	for (ip = msgvec; *ip != 0; ip++) {
    363   1.1       cgd 		mesg = *ip;
    364   1.1       cgd 		mp = &message[mesg-1];
    365  1.27  christos 		(void)printf("%d: %ld/%llu\n", mesg, mp->m_blines,
    366  1.27  christos 		    /*LINTED*/
    367  1.27  christos 		    (unsigned long long)mp->m_size);
    368   1.1       cgd 	}
    369   1.1       cgd 	return(0);
    370   1.1       cgd }
    371   1.1       cgd 
    372   1.1       cgd /*
    373   1.1       cgd  * Quit quickly.  If we are sourcing, just pop the input level
    374   1.1       cgd  * by returning an error.
    375   1.1       cgd  */
    376   1.4   deraadt int
    377  1.27  christos /*ARGSUSED*/
    378  1.12       wiz rexit(void *v)
    379   1.1       cgd {
    380   1.1       cgd 	if (sourcing)
    381   1.1       cgd 		return(1);
    382   1.5  christos 	exit(0);
    383   1.1       cgd 	/*NOTREACHED*/
    384   1.1       cgd }
    385   1.1       cgd 
    386   1.1       cgd /*
    387   1.1       cgd  * Set or display a variable value.  Syntax is similar to that
    388   1.1       cgd  * of csh.
    389   1.1       cgd  */
    390   1.4   deraadt int
    391  1.12       wiz set(void *v)
    392   1.1       cgd {
    393   1.5  christos 	char **arglist = v;
    394   1.9     lukem 	struct var *vp;
    395  1.26  christos 	const char *cp;
    396   1.1       cgd 	char varbuf[BUFSIZ], **ap, **p;
    397   1.1       cgd 	int errs, h, s;
    398  1.23      ross 	size_t l;
    399   1.1       cgd 
    400  1.14       wiz 	if (*arglist == NULL) {
    401   1.1       cgd 		for (h = 0, s = 1; h < HSHSIZE; h++)
    402  1.15       wiz 			for (vp = variables[h]; vp != NULL; vp = vp->v_link)
    403   1.1       cgd 				s++;
    404  1.26  christos 		ap = salloc(s * sizeof *ap);
    405   1.1       cgd 		for (h = 0, p = ap; h < HSHSIZE; h++)
    406  1.15       wiz 			for (vp = variables[h]; vp != NULL; vp = vp->v_link)
    407   1.1       cgd 				*p++ = vp->v_name;
    408  1.14       wiz 		*p = NULL;
    409   1.1       cgd 		sort(ap);
    410  1.14       wiz 		for (p = ap; *p != NULL; p++)
    411  1.27  christos 			(void)printf("%s\t%s\n", *p, value(*p));
    412   1.1       cgd 		return(0);
    413   1.1       cgd 	}
    414   1.1       cgd 	errs = 0;
    415  1.14       wiz 	for (ap = arglist; *ap != NULL; ap++) {
    416   1.1       cgd 		cp = *ap;
    417   1.1       cgd 		while (*cp != '=' && *cp != '\0')
    418  1.23      ross 			++cp;
    419  1.23      ross 		l = cp - *ap;
    420  1.23      ross 		if (l >= sizeof varbuf)
    421  1.23      ross 			l = sizeof varbuf - 1;
    422  1.27  christos 		(void)strncpy(varbuf, *ap, l);
    423  1.24      ross 		varbuf[l] = '\0';
    424   1.1       cgd 		if (*cp == '\0')
    425   1.1       cgd 			cp = "";
    426   1.1       cgd 		else
    427   1.1       cgd 			cp++;
    428   1.1       cgd 		if (equal(varbuf, "")) {
    429  1.27  christos 			(void)printf("Non-null variable name required\n");
    430   1.1       cgd 			errs++;
    431   1.1       cgd 			continue;
    432   1.1       cgd 		}
    433   1.1       cgd 		assign(varbuf, cp);
    434   1.1       cgd 	}
    435   1.1       cgd 	return(errs);
    436   1.1       cgd }
    437   1.1       cgd 
    438   1.1       cgd /*
    439   1.1       cgd  * Unset a bunch of variable values.
    440   1.1       cgd  */
    441   1.4   deraadt int
    442  1.12       wiz unset(void *v)
    443   1.1       cgd {
    444   1.5  christos 	char **arglist = v;
    445   1.9     lukem 	struct var *vp, *vp2;
    446   1.1       cgd 	int errs, h;
    447   1.1       cgd 	char **ap;
    448   1.1       cgd 
    449   1.1       cgd 	errs = 0;
    450  1.14       wiz 	for (ap = arglist; *ap != NULL; ap++) {
    451  1.15       wiz 		if ((vp2 = lookup(*ap)) == NULL) {
    452  1.11      dean 			if (getenv(*ap)) {
    453  1.27  christos 				(void)unsetenv(*ap);
    454  1.11      dean 			} else if (!sourcing) {
    455  1.27  christos 				(void)printf("\"%s\": undefined variable\n", *ap);
    456   1.1       cgd 				errs++;
    457   1.1       cgd 			}
    458   1.1       cgd 			continue;
    459   1.1       cgd 		}
    460   1.1       cgd 		h = hash(*ap);
    461   1.1       cgd 		if (vp2 == variables[h]) {
    462   1.1       cgd 			variables[h] = variables[h]->v_link;
    463  1.10  wsanchez 			v_free(vp2->v_name);
    464  1.10  wsanchez                         v_free(vp2->v_value);
    465  1.27  christos 			free(vp2);
    466   1.1       cgd 			continue;
    467   1.1       cgd 		}
    468   1.1       cgd 		for (vp = variables[h]; vp->v_link != vp2; vp = vp->v_link)
    469   1.1       cgd 			;
    470   1.1       cgd 		vp->v_link = vp2->v_link;
    471  1.10  wsanchez                 v_free(vp2->v_name);
    472  1.10  wsanchez                 v_free(vp2->v_value);
    473  1.27  christos 		free(vp2);
    474   1.1       cgd 	}
    475   1.1       cgd 	return(errs);
    476   1.1       cgd }
    477   1.1       cgd 
    478   1.1       cgd /*
    479  1.29  christos  * Show a variable value.
    480  1.29  christos  */
    481  1.29  christos int
    482  1.29  christos show(void *v)
    483  1.29  christos {
    484  1.29  christos 	char **arglist = v;
    485  1.29  christos 	struct var *vp;
    486  1.29  christos 	char **ap, **p;
    487  1.29  christos 	int h, s;
    488  1.29  christos 
    489  1.29  christos 	if (*arglist == NULL) {
    490  1.29  christos 		for (h = 0, s = 1; h < HSHSIZE; h++)
    491  1.29  christos 			for (vp = variables[h]; vp != NULL; vp = vp->v_link)
    492  1.29  christos 				s++;
    493  1.29  christos 		ap = salloc(s * sizeof *ap);
    494  1.29  christos 		for (h = 0, p = ap; h < HSHSIZE; h++)
    495  1.29  christos 			for (vp = variables[h]; vp != NULL; vp = vp->v_link)
    496  1.29  christos 				*p++ = vp->v_name;
    497  1.29  christos 		*p = NULL;
    498  1.29  christos 		sort(ap);
    499  1.29  christos 		for (p = ap; *p != NULL; p++)
    500  1.29  christos 			(void)printf("%s=%s\n", *p, value(*p));
    501  1.29  christos 		return(0);
    502  1.29  christos 	}
    503  1.29  christos 
    504  1.29  christos 	for (ap = arglist; *ap != NULL; ap++) {
    505  1.29  christos 		char *val = value(*ap);
    506  1.29  christos 		(void)printf("%s=%s\n", *ap, val ? val : "<null>");
    507  1.29  christos 	}
    508  1.29  christos 	return 0;
    509  1.29  christos }
    510  1.29  christos 
    511  1.29  christos /*
    512   1.1       cgd  * Put add users to a group.
    513   1.1       cgd  */
    514   1.4   deraadt int
    515  1.12       wiz group(void *v)
    516   1.1       cgd {
    517   1.5  christos 	char **argv = v;
    518   1.9     lukem 	struct grouphead *gh;
    519   1.9     lukem 	struct group *gp;
    520   1.9     lukem 	int h;
    521   1.1       cgd 	int s;
    522   1.1       cgd 	char **ap, *gname, **p;
    523   1.1       cgd 
    524  1.14       wiz 	if (*argv == NULL) {
    525   1.1       cgd 		for (h = 0, s = 1; h < HSHSIZE; h++)
    526  1.15       wiz 			for (gh = groups[h]; gh != NULL; gh = gh->g_link)
    527   1.1       cgd 				s++;
    528  1.26  christos 		ap = salloc(s * sizeof *ap);
    529   1.1       cgd 		for (h = 0, p = ap; h < HSHSIZE; h++)
    530  1.15       wiz 			for (gh = groups[h]; gh != NULL; gh = gh->g_link)
    531   1.1       cgd 				*p++ = gh->g_name;
    532  1.14       wiz 		*p = NULL;
    533   1.1       cgd 		sort(ap);
    534  1.14       wiz 		for (p = ap; *p != NULL; p++)
    535   1.1       cgd 			printgroup(*p);
    536   1.1       cgd 		return(0);
    537   1.1       cgd 	}
    538  1.14       wiz 	if (argv[1] == NULL) {
    539   1.1       cgd 		printgroup(*argv);
    540   1.1       cgd 		return(0);
    541   1.1       cgd 	}
    542   1.1       cgd 	gname = *argv;
    543   1.1       cgd 	h = hash(gname);
    544  1.15       wiz 	if ((gh = findgroup(gname)) == NULL) {
    545  1.19    itojun 		gh = (struct grouphead *) calloc(1, sizeof *gh);
    546   1.1       cgd 		gh->g_name = vcopy(gname);
    547  1.15       wiz 		gh->g_list = NULL;
    548   1.1       cgd 		gh->g_link = groups[h];
    549   1.1       cgd 		groups[h] = gh;
    550   1.1       cgd 	}
    551   1.1       cgd 
    552   1.1       cgd 	/*
    553   1.1       cgd 	 * Insert names from the command list into the group.
    554   1.1       cgd 	 * Who cares if there are duplicates?  They get tossed
    555   1.1       cgd 	 * later anyway.
    556   1.1       cgd 	 */
    557   1.1       cgd 
    558  1.14       wiz 	for (ap = argv+1; *ap != NULL; ap++) {
    559  1.19    itojun 		gp = (struct group *) calloc(1, sizeof *gp);
    560   1.1       cgd 		gp->ge_name = vcopy(*ap);
    561   1.1       cgd 		gp->ge_link = gh->g_list;
    562   1.1       cgd 		gh->g_list = gp;
    563   1.1       cgd 	}
    564   1.1       cgd 	return(0);
    565   1.1       cgd }
    566   1.1       cgd 
    567   1.1       cgd /*
    568  1.28  christos  * The unalias command takes a list of alises
    569  1.28  christos  * and discards the remembered groups of users.
    570  1.28  christos  */
    571  1.28  christos int
    572  1.28  christos unalias(void *v)
    573  1.28  christos {
    574  1.28  christos 	char **ap;
    575  1.28  christos 
    576  1.28  christos 	for (ap = v; *ap != NULL; ap++)
    577  1.28  christos 		(void)delgroup(*ap);
    578  1.28  christos 	return 0;
    579  1.28  christos }
    580  1.28  christos 
    581  1.28  christos /*
    582  1.28  christos  * Delete the named group alias. Return zero if the group was
    583  1.28  christos  * successfully deleted, or -1 if there was no such group.
    584  1.28  christos  */
    585  1.28  christos static int
    586  1.28  christos delgroup(const char *name)
    587  1.28  christos {
    588  1.28  christos 	struct grouphead *gh, *p;
    589  1.28  christos 	struct group *g;
    590  1.28  christos 	int h;
    591  1.28  christos 
    592  1.28  christos 	h = hash(name);
    593  1.28  christos 	for (gh = groups[h], p = NULL; gh != NULL; p = gh, gh = gh->g_link)
    594  1.28  christos 		if (strcmp(gh->g_name, name) == 0) {
    595  1.28  christos 			if (p == NULL)
    596  1.28  christos 				groups[h] = gh->g_link;
    597  1.28  christos 			else
    598  1.28  christos 				p->g_link = gh->g_link;
    599  1.28  christos 			while (gh->g_list != NULL) {
    600  1.28  christos 				g = gh->g_list;
    601  1.28  christos 				gh->g_list = g->ge_link;
    602  1.28  christos 				free(g->ge_name);
    603  1.28  christos 				free(g);
    604  1.28  christos 			}
    605  1.28  christos 			free(gh->g_name);
    606  1.28  christos 			free(gh);
    607  1.28  christos 			return 0;
    608  1.28  christos 		}
    609  1.28  christos 	return -1;
    610  1.28  christos }
    611  1.28  christos 
    612  1.28  christos /*
    613   1.1       cgd  * Sort the passed string vecotor into ascending dictionary
    614   1.1       cgd  * order.
    615   1.1       cgd  */
    616   1.4   deraadt void
    617  1.12       wiz sort(char **list)
    618   1.1       cgd {
    619   1.9     lukem 	char **ap;
    620   1.1       cgd 
    621  1.14       wiz 	for (ap = list; *ap != NULL; ap++)
    622   1.1       cgd 		;
    623   1.1       cgd 	if (ap-list < 2)
    624   1.1       cgd 		return;
    625  1.27  christos 	qsort(list, (size_t)(ap-list), sizeof(*list), diction);
    626   1.1       cgd }
    627   1.1       cgd 
    628   1.1       cgd /*
    629   1.1       cgd  * Do a dictionary order comparison of the arguments from
    630   1.1       cgd  * qsort.
    631   1.1       cgd  */
    632   1.5  christos static int
    633  1.12       wiz diction(const void *a, const void *b)
    634   1.1       cgd {
    635  1.26  christos 	return(strcmp(*(const char *const *)a, *(const char *const *)b));
    636   1.1       cgd }
    637   1.1       cgd 
    638   1.1       cgd /*
    639   1.1       cgd  * The do nothing command for comments.
    640   1.1       cgd  */
    641   1.1       cgd 
    642   1.1       cgd /*ARGSUSED*/
    643   1.4   deraadt int
    644  1.12       wiz null(void *v)
    645   1.1       cgd {
    646   1.1       cgd 	return 0;
    647   1.1       cgd }
    648   1.1       cgd 
    649   1.1       cgd /*
    650   1.1       cgd  * Change to another file.  With no argument, print information about
    651   1.1       cgd  * the current file.
    652   1.1       cgd  */
    653   1.4   deraadt int
    654  1.12       wiz file(void *v)
    655   1.1       cgd {
    656   1.5  christos 	char **argv = v;
    657   1.1       cgd 
    658  1.14       wiz 	if (argv[0] == NULL) {
    659  1.27  christos 		(void)newfileinfo(0);
    660   1.1       cgd 		return 0;
    661   1.1       cgd 	}
    662   1.1       cgd 	if (setfile(*argv) < 0)
    663   1.1       cgd 		return 1;
    664   1.1       cgd 	announce();
    665   1.1       cgd 	return 0;
    666   1.1       cgd }
    667   1.1       cgd 
    668   1.1       cgd /*
    669   1.1       cgd  * Expand file names like echo
    670   1.1       cgd  */
    671   1.4   deraadt int
    672  1.12       wiz echo(void *v)
    673   1.1       cgd {
    674   1.5  christos 	char **argv = v;
    675   1.9     lukem 	char **ap;
    676  1.26  christos 	const char *cp;
    677   1.1       cgd 
    678  1.14       wiz 	for (ap = argv; *ap != NULL; ap++) {
    679   1.1       cgd 		cp = *ap;
    680  1.14       wiz 		if ((cp = expand(cp)) != NULL) {
    681   1.1       cgd 			if (ap != argv)
    682  1.27  christos 				(void)putchar(' ');
    683  1.27  christos 			(void)printf("%s", cp);
    684   1.1       cgd 		}
    685   1.1       cgd 	}
    686  1.27  christos 	(void)putchar('\n');
    687   1.1       cgd 	return 0;
    688   1.1       cgd }
    689   1.1       cgd 
    690   1.4   deraadt int
    691  1.12       wiz Respond(void *v)
    692   1.1       cgd {
    693   1.5  christos 	int *msgvec = v;
    694  1.14       wiz 	if (value("Replyall") == NULL)
    695   1.1       cgd 		return (_Respond(msgvec));
    696   1.1       cgd 	else
    697   1.1       cgd 		return (_respond(msgvec));
    698   1.1       cgd }
    699   1.1       cgd 
    700   1.1       cgd /*
    701   1.1       cgd  * Reply to a series of messages by simply mailing to the senders
    702   1.1       cgd  * and not messing around with the To: and Cc: lists as in normal
    703   1.1       cgd  * reply.
    704   1.1       cgd  */
    705   1.4   deraadt int
    706  1.12       wiz _Respond(int msgvec[])
    707   1.1       cgd {
    708   1.1       cgd 	struct header head;
    709   1.1       cgd 	struct message *mp;
    710   1.9     lukem 	int *ap;
    711   1.9     lukem 	char *cp;
    712   1.1       cgd 
    713  1.15       wiz 	head.h_to = NULL;
    714   1.1       cgd 	for (ap = msgvec; *ap != 0; ap++) {
    715   1.1       cgd 		mp = &message[*ap - 1];
    716   1.1       cgd 		touch(mp);
    717   1.1       cgd 		dot = mp;
    718  1.14       wiz 		if ((cp = skin(hfield("from", mp))) == NULL)
    719   1.1       cgd 			cp = skin(nameof(mp, 2));
    720   1.1       cgd 		head.h_to = cat(head.h_to, extract(cp, GTO));
    721   1.1       cgd 	}
    722  1.15       wiz 	if (head.h_to == NULL)
    723   1.1       cgd 		return 0;
    724   1.1       cgd 	mp = &message[msgvec[0] - 1];
    725  1.14       wiz 	if ((head.h_subject = hfield("subject", mp)) == NULL)
    726   1.1       cgd 		head.h_subject = hfield("subj", mp);
    727   1.1       cgd 	head.h_subject = reedit(head.h_subject);
    728  1.15       wiz 	head.h_cc = NULL;
    729  1.15       wiz 	head.h_bcc = NULL;
    730  1.29  christos 	head.h_smopts = set_smopts(mp);
    731   1.1       cgd 	mail1(&head, 1);
    732   1.1       cgd 	return 0;
    733   1.1       cgd }
    734   1.1       cgd 
    735   1.1       cgd /*
    736   1.1       cgd  * Conditional commands.  These allow one to parameterize one's
    737   1.1       cgd  * .mailrc and do some things if sending, others if receiving.
    738   1.1       cgd  */
    739   1.4   deraadt int
    740  1.12       wiz ifcmd(void *v)
    741   1.1       cgd {
    742   1.5  christos 	char **argv = v;
    743   1.9     lukem 	char *cp;
    744   1.1       cgd 
    745   1.1       cgd 	if (cond != CANY) {
    746  1.27  christos 		(void)printf("Illegal nested \"if\"\n");
    747   1.1       cgd 		return(1);
    748   1.1       cgd 	}
    749   1.1       cgd 	cond = CANY;
    750   1.1       cgd 	cp = argv[0];
    751   1.1       cgd 	switch (*cp) {
    752   1.1       cgd 	case 'r': case 'R':
    753   1.1       cgd 		cond = CRCV;
    754   1.1       cgd 		break;
    755   1.1       cgd 
    756   1.1       cgd 	case 's': case 'S':
    757   1.1       cgd 		cond = CSEND;
    758   1.1       cgd 		break;
    759   1.1       cgd 
    760   1.1       cgd 	default:
    761  1.27  christos 		(void)printf("Unrecognized if-keyword: \"%s\"\n", cp);
    762   1.1       cgd 		return(1);
    763   1.1       cgd 	}
    764   1.1       cgd 	return(0);
    765   1.1       cgd }
    766   1.1       cgd 
    767   1.1       cgd /*
    768   1.1       cgd  * Implement 'else'.  This is pretty simple -- we just
    769   1.1       cgd  * flip over the conditional flag.
    770   1.1       cgd  */
    771   1.4   deraadt int
    772  1.27  christos /*ARGSUSED*/
    773  1.12       wiz elsecmd(void *v)
    774   1.1       cgd {
    775   1.1       cgd 
    776   1.1       cgd 	switch (cond) {
    777   1.1       cgd 	case CANY:
    778  1.27  christos 		(void)printf("\"Else\" without matching \"if\"\n");
    779   1.1       cgd 		return(1);
    780   1.1       cgd 
    781   1.1       cgd 	case CSEND:
    782   1.1       cgd 		cond = CRCV;
    783   1.1       cgd 		break;
    784   1.1       cgd 
    785   1.1       cgd 	case CRCV:
    786   1.1       cgd 		cond = CSEND;
    787   1.1       cgd 		break;
    788   1.1       cgd 
    789   1.1       cgd 	default:
    790  1.27  christos 		(void)printf("Mail's idea of conditions is screwed up\n");
    791   1.1       cgd 		cond = CANY;
    792   1.1       cgd 		break;
    793   1.1       cgd 	}
    794   1.1       cgd 	return(0);
    795   1.1       cgd }
    796   1.1       cgd 
    797   1.1       cgd /*
    798   1.1       cgd  * End of if statement.  Just set cond back to anything.
    799   1.1       cgd  */
    800   1.4   deraadt int
    801  1.27  christos /*ARGSUSED*/
    802  1.12       wiz endifcmd(void *v)
    803   1.1       cgd {
    804   1.1       cgd 
    805   1.1       cgd 	if (cond == CANY) {
    806  1.27  christos 		(void)printf("\"Endif\" without matching \"if\"\n");
    807   1.1       cgd 		return(1);
    808   1.1       cgd 	}
    809   1.1       cgd 	cond = CANY;
    810   1.1       cgd 	return(0);
    811   1.1       cgd }
    812   1.1       cgd 
    813   1.1       cgd /*
    814   1.1       cgd  * Set the list of alternate names.
    815   1.1       cgd  */
    816   1.4   deraadt int
    817  1.12       wiz alternates(void *v)
    818   1.1       cgd {
    819   1.5  christos 	char **namelist = v;
    820   1.9     lukem 	int c;
    821   1.9     lukem 	char **ap, **ap2, *cp;
    822   1.1       cgd 
    823   1.1       cgd 	c = argcount(namelist) + 1;
    824   1.1       cgd 	if (c == 1) {
    825   1.1       cgd 		if (altnames == 0)
    826   1.1       cgd 			return(0);
    827   1.1       cgd 		for (ap = altnames; *ap; ap++)
    828  1.27  christos 			(void)printf("%s ", *ap);
    829  1.27  christos 		(void)printf("\n");
    830   1.1       cgd 		return(0);
    831   1.1       cgd 	}
    832   1.1       cgd 	if (altnames != 0)
    833  1.27  christos 		free(altnames);
    834   1.1       cgd 	altnames = (char **) calloc((unsigned) c, sizeof (char *));
    835   1.1       cgd 	for (ap = namelist, ap2 = altnames; *ap; ap++, ap2++) {
    836  1.27  christos 		cp = calloc((unsigned) strlen(*ap) + 1, sizeof (char));
    837  1.27  christos 		(void)strcpy(cp, *ap);
    838   1.1       cgd 		*ap2 = cp;
    839   1.1       cgd 	}
    840   1.1       cgd 	*ap2 = 0;
    841   1.1       cgd 	return(0);
    842   1.1       cgd }
    843