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