Home | History | Annotate | Line # | Download | only in mail
cmd4.c revision 1.2
      1  1.1  christos /*	$NetBSD: cmd4.c,v 1.2 2006/11/28 18:45:32 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  christos  * by Anon Ymous.
      9  1.1  christos  *
     10  1.1  christos  * Redistribution and use in source and binary forms, with or without
     11  1.1  christos  * modification, are permitted provided that the following conditions
     12  1.1  christos  * are met:
     13  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  christos  *    documentation and/or other materials provided with the distribution.
     18  1.1  christos  * 3. All advertising materials mentioning features or use of this software
     19  1.1  christos  *    must display the following acknowledgement:
     20  1.1  christos  *        This product includes software developed by the NetBSD
     21  1.1  christos  *        Foundation, Inc. and its contributors.
     22  1.1  christos  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  christos  *    contributors may be used to endorse or promote products derived
     24  1.1  christos  *    from this software without specific prior written permission.
     25  1.1  christos  *
     26  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  christos  */
     38  1.1  christos 
     39  1.1  christos 
     40  1.1  christos #include <sys/cdefs.h>
     41  1.1  christos #ifndef lint
     42  1.1  christos #if 0
     43  1.1  christos static char sccsid[] = "@(#)cmd3.c	8.2 (Berkeley) 4/20/95";
     44  1.1  christos #else
     45  1.1  christos __RCSID("$NetBSD: cmd4.c,v 1.2 2006/11/28 18:45:32 christos Exp $");
     46  1.1  christos #endif
     47  1.1  christos #endif /* not lint */
     48  1.1  christos 
     49  1.1  christos #include "rcv.h"
     50  1.1  christos #include <util.h>
     51  1.1  christos #include "extern.h"
     52  1.1  christos 
     53  1.1  christos /*
     54  1.1  christos  * Mail -- a mail program
     55  1.1  christos  *
     56  1.1  christos  * Still more user commands.
     57  1.2  christos  * XXX - should this be renamed smopts.c?
     58  1.1  christos  */
     59  1.1  christos 
     60  1.1  christos #if 0	/* XXX - debugging stuff - to be removed */
     61  1.1  christos void showname(struct name *);
     62  1.1  christos void
     63  1.1  christos showname(struct name *np)
     64  1.1  christos {
     65  1.1  christos 	for (/*EMPTY*/; np; np = np->n_flink)
     66  1.1  christos 		(void)printf("np: %p  np->n_type: %d  np->n_name: '%s' (%p)\n",
     67  1.1  christos 		    np, np->n_type, np->n_name, np->n_name);
     68  1.1  christos }
     69  1.1  christos 
     70  1.1  christos __unused
     71  1.1  christos static void
     72  1.1  christos showsmopts(struct smopts_s *sp)
     73  1.1  christos {
     74  1.1  christos 	(void)printf("%s (%p)\n", sp->s_name, sp);
     75  1.1  christos 	showname(sp->s_smopts);
     76  1.1  christos }
     77  1.1  christos #endif	/* XXX - debugging stuff - to be removed */
     78  1.1  christos 
     79  1.1  christos 
     80  1.1  christos static int
     81  1.1  christos hashcase(const char *key)
     82  1.1  christos {
     83  1.1  christos 	char *lckey;
     84  1.1  christos 	lckey = salloc(strlen(key) + 1);
     85  1.1  christos 	istrcpy(lckey, key);
     86  1.1  christos 	return hash(lckey);
     87  1.1  christos }
     88  1.1  christos 
     89  1.1  christos static struct smopts_s *
     90  1.1  christos findsmopts_core(const char *name)
     91  1.1  christos {
     92  1.1  christos 	struct smopts_s *sh;
     93  1.1  christos 
     94  1.1  christos 	for (sh = smoptstbl[hashcase(name)]; sh; sh = sh->s_link)
     95  1.1  christos 		if (strcasecmp(sh->s_name, name) == 0)
     96  1.2  christos 			return sh;
     97  1.1  christos 	return NULL;
     98  1.1  christos }
     99  1.1  christos 
    100  1.2  christos /*
    101  1.2  christos  * The exported smopts lookup routine.
    102  1.2  christos  */
    103  1.2  christos PUBLIC struct smopts_s *
    104  1.1  christos findsmopts(const char *name, int top_only)
    105  1.1  christos {
    106  1.1  christos 	const char *cp;
    107  1.1  christos 	struct smopts_s *sh;
    108  1.1  christos 
    109  1.1  christos 	if ((sh = findsmopts_core(name)) != NULL)
    110  1.1  christos 		return sh;
    111  1.1  christos 
    112  1.1  christos 	if (top_only)
    113  1.1  christos 		return NULL;
    114  1.1  christos 
    115  1.1  christos 	for (cp = strchr(name, '@'); cp; cp = strchr(cp + 1, '.'))
    116  1.1  christos 		if ((sh = findsmopts_core(cp)) != NULL)
    117  1.1  christos 			return sh;
    118  1.1  christos 
    119  1.1  christos 	return findsmopts_core(".");
    120  1.1  christos }
    121  1.1  christos 
    122  1.1  christos static void
    123  1.2  christos printsmopts(const char *name)
    124  1.1  christos {
    125  1.1  christos 	struct smopts_s *sp;
    126  1.1  christos 
    127  1.1  christos 	if ((sp = findsmopts(name, 1)) == NULL) {
    128  1.1  christos 		(void)printf("%s:\n", name);
    129  1.1  christos 		return;
    130  1.1  christos 	}
    131  1.1  christos 	(void)printf("%s:\t%s\n", sp->s_name, detract(sp->s_smopts, GSMOPTS));
    132  1.1  christos }
    133  1.1  christos 
    134  1.1  christos static void
    135  1.1  christos printsmoptstbl(void)
    136  1.1  christos {
    137  1.1  christos 	struct smopts_s *sp;
    138  1.2  christos 	const char **argv, **ap;
    139  1.1  christos 	int h;
    140  1.1  christos 	int cnt;
    141  1.1  christos 
    142  1.1  christos 	cnt = 1;
    143  1.2  christos 	for (h = 0; h < (int)sizeofarray(smoptstbl); h++ )
    144  1.1  christos 		for (sp = smoptstbl[h]; sp && sp->s_name != NULL; sp = sp->s_link)
    145  1.1  christos 			cnt++;
    146  1.1  christos 
    147  1.1  christos 	argv = salloc(cnt * sizeof(*argv));
    148  1.1  christos 	ap = argv;
    149  1.2  christos 	for (h = 0; h < (int)sizeofarray(smoptstbl); h++ )
    150  1.1  christos 		for (sp = smoptstbl[h]; sp && sp->s_name != NULL; sp = sp->s_link)
    151  1.1  christos 			*ap++ = sp->s_name;
    152  1.1  christos 	*ap = NULL;
    153  1.1  christos 	sort(argv);
    154  1.1  christos 	for (ap = argv; *ap != NULL; ap++)
    155  1.1  christos 		printsmopts(*ap);
    156  1.1  christos }
    157  1.1  christos 
    158  1.1  christos static struct name *
    159  1.1  christos name_expand(char *sname, int ntype)
    160  1.1  christos {
    161  1.1  christos 	struct grouphead *gh;
    162  1.1  christos 	struct name *np;
    163  1.1  christos 
    164  1.1  christos 	if ((gh = findgroup(sname)) != NULL) {
    165  1.1  christos 		np = gexpand(NULL, gh, 0, ntype);
    166  1.1  christos 	}
    167  1.1  christos 	else {
    168  1.1  christos 		np = csalloc(1, sizeof(*np));
    169  1.1  christos 		np->n_name = sname;
    170  1.1  christos 		np->n_type = ntype;
    171  1.1  christos 	}
    172  1.1  christos 	return np;
    173  1.1  christos }
    174  1.1  christos 
    175  1.1  christos static struct name *
    176  1.1  christos ncalloc(char *str, int ntype)
    177  1.1  christos {
    178  1.1  christos 	struct name *np;
    179  1.1  christos 
    180  1.1  christos 	np = ecalloc(1, sizeof *np);
    181  1.1  christos 	np->n_type = ntype;
    182  1.1  christos 	np->n_name = vcopy(str);
    183  1.2  christos 	return np;
    184  1.1  christos }
    185  1.1  christos 
    186  1.1  christos static void
    187  1.1  christos smopts_core(const char *sname, char **argv)
    188  1.1  christos {
    189  1.1  christos 	struct smopts_s *sp;
    190  1.1  christos 	struct name *np, *t;
    191  1.1  christos 	int h;
    192  1.1  christos 	char **ap;
    193  1.1  christos 
    194  1.1  christos 	if ((sp = findsmopts(sname, 1)) != NULL) {
    195  1.1  christos 		char *cp;
    196  1.1  christos 		cp = detract(sp->s_smopts, GSMOPTS);
    197  1.1  christos 		(void)printf("%s already defined as: %s\n", sname, cp);
    198  1.1  christos 		return;
    199  1.1  christos 	}
    200  1.1  christos 	h = hashcase(sname);
    201  1.1  christos 	sp = ecalloc(1, sizeof *sp);
    202  1.1  christos 	sp->s_name = vcopy(sname);
    203  1.1  christos 	if (smoptstbl[h])
    204  1.1  christos 		sp->s_link = smoptstbl[h];
    205  1.1  christos 	smoptstbl[h] = sp;
    206  1.1  christos 
    207  1.1  christos 	np = NULL;
    208  1.1  christos 	for (ap = argv + 1; *ap != NULL; ap++) {
    209  1.1  christos 		t = ncalloc(*ap, GSMOPTS);
    210  1.1  christos 		if (sp->s_smopts == NULL)
    211  1.1  christos 			sp->s_smopts = t;
    212  1.1  christos 		else
    213  1.1  christos 			np->n_flink = t;
    214  1.1  christos 		t->n_blink = np;
    215  1.1  christos 		np = t;
    216  1.1  christos 	}
    217  1.1  christos }
    218  1.1  christos 
    219  1.2  christos /*
    220  1.2  christos  * Takes a list of entries, expands them, and adds the results to the
    221  1.2  christos  * smopts table.
    222  1.2  christos  */
    223  1.2  christos PUBLIC int
    224  1.1  christos smoptscmd(void *v)
    225  1.1  christos {
    226  1.1  christos 	struct name *np;
    227  1.1  christos 	char **argv = v;
    228  1.1  christos 
    229  1.1  christos 	if (*argv == NULL) {
    230  1.1  christos 		printsmoptstbl();
    231  1.1  christos 		return 0;
    232  1.1  christos 	}
    233  1.1  christos 	np = name_expand(argv[0], GTO);
    234  1.1  christos 
    235  1.1  christos 	if (argv[1] == NULL) {
    236  1.1  christos 		for (/*EMPTY*/; np; np = np->n_flink)
    237  1.1  christos 			printsmopts(np->n_name);
    238  1.1  christos 		return 0;
    239  1.1  christos 	}
    240  1.1  christos 	for (/*EMPTY*/; np; np = np->n_flink)
    241  1.1  christos 		smopts_core(np->n_name, argv);
    242  1.1  christos 
    243  1.1  christos 	return 0;
    244  1.1  christos }
    245  1.1  christos 
    246  1.1  christos static void
    247  1.1  christos free_name(struct name *np)
    248  1.1  christos {
    249  1.1  christos 	struct name *next_np;
    250  1.1  christos 	for (/*EMPTY*/; np; np = next_np) {
    251  1.1  christos 		next_np = np->n_flink;
    252  1.1  christos 		free(next_np);
    253  1.1  christos 	}
    254  1.1  christos }
    255  1.1  christos 
    256  1.1  christos static void
    257  1.1  christos delsmopts(char *name)
    258  1.1  christos {
    259  1.1  christos 	struct smopts_s *sp;
    260  1.1  christos 	struct smopts_s **last_link;
    261  1.1  christos 
    262  1.1  christos 	last_link = &smoptstbl[hashcase(name)];
    263  1.1  christos 	for (sp = *last_link; sp; sp = sp->s_link) {
    264  1.1  christos 		if (strcasecmp(sp->s_name, name) == 0) {
    265  1.1  christos 			*last_link = sp->s_link;
    266  1.1  christos 			free_name(sp->s_smopts);
    267  1.1  christos 			free(sp);
    268  1.1  christos 		}
    269  1.1  christos 	}
    270  1.1  christos }
    271  1.1  christos 
    272  1.2  christos /*
    273  1.2  christos  * Takes a list of entries and removes them from the smoptstbl.
    274  1.2  christos  */
    275  1.2  christos PUBLIC int
    276  1.2  christos unsmoptscmd(void *v)
    277  1.1  christos {
    278  1.1  christos 	struct name *np;
    279  1.1  christos 	char **argv, **ap;
    280  1.1  christos 
    281  1.1  christos 	argv = v;
    282  1.1  christos 	for (ap = argv; *ap != NULL; ap++)
    283  1.1  christos 		for (np = name_expand(*ap, GTO); np; np = np->n_flink)
    284  1.1  christos 			delsmopts(np->n_name);
    285  1.1  christos 	return 0;
    286  1.1  christos }
    287