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