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