cmd4.c revision 1.1 1 1.1 christos /* $NetBSD: cmd4.c,v 1.1 2006/10/31 22:37:19 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.1 2006/10/31 22:37:19 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.1 christos */
58 1.1 christos
59 1.1 christos #ifdef SMOPTS_CMD
60 1.1 christos
61 1.1 christos #if 0 /* XXX - debugging stuff - to be removed */
62 1.1 christos void showname(struct name *);
63 1.1 christos void
64 1.1 christos showname(struct name *np)
65 1.1 christos {
66 1.1 christos for (/*EMPTY*/; np; np = np->n_flink)
67 1.1 christos (void)printf("np: %p np->n_type: %d np->n_name: '%s' (%p)\n",
68 1.1 christos np, np->n_type, np->n_name, np->n_name);
69 1.1 christos }
70 1.1 christos
71 1.1 christos __unused
72 1.1 christos static void
73 1.1 christos showsmopts(struct smopts_s *sp)
74 1.1 christos {
75 1.1 christos (void)printf("%s (%p)\n", sp->s_name, sp);
76 1.1 christos showname(sp->s_smopts);
77 1.1 christos }
78 1.1 christos #endif /* XXX - debugging stuff - to be removed */
79 1.1 christos
80 1.1 christos
81 1.1 christos static int
82 1.1 christos hashcase(const char *key)
83 1.1 christos {
84 1.1 christos char *lckey;
85 1.1 christos lckey = salloc(strlen(key) + 1);
86 1.1 christos istrcpy(lckey, key);
87 1.1 christos return hash(lckey);
88 1.1 christos }
89 1.1 christos
90 1.1 christos static struct smopts_s *
91 1.1 christos findsmopts_core(const char *name)
92 1.1 christos {
93 1.1 christos struct smopts_s *sh;
94 1.1 christos
95 1.1 christos for (sh = smoptstbl[hashcase(name)]; sh; sh = sh->s_link)
96 1.1 christos if (strcasecmp(sh->s_name, name) == 0)
97 1.1 christos return(sh);
98 1.1 christos return NULL;
99 1.1 christos }
100 1.1 christos
101 1.1 christos struct smopts_s *
102 1.1 christos findsmopts(const char *name, int top_only)
103 1.1 christos {
104 1.1 christos const char *cp;
105 1.1 christos struct smopts_s *sh;
106 1.1 christos
107 1.1 christos if ((sh = findsmopts_core(name)) != NULL)
108 1.1 christos return sh;
109 1.1 christos
110 1.1 christos if (top_only)
111 1.1 christos return NULL;
112 1.1 christos
113 1.1 christos for (cp = strchr(name, '@'); cp; cp = strchr(cp + 1, '.'))
114 1.1 christos if ((sh = findsmopts_core(cp)) != NULL)
115 1.1 christos return sh;
116 1.1 christos
117 1.1 christos return findsmopts_core(".");
118 1.1 christos }
119 1.1 christos
120 1.1 christos /****************************************
121 1.1 christos *
122 1.1 christos */
123 1.1 christos static void
124 1.1 christos printsmopts(char *name)
125 1.1 christos {
126 1.1 christos struct smopts_s *sp;
127 1.1 christos
128 1.1 christos if ((sp = findsmopts(name, 1)) == NULL) {
129 1.1 christos (void)printf("%s:\n", name);
130 1.1 christos return;
131 1.1 christos }
132 1.1 christos (void)printf("%s:\t%s\n", sp->s_name, detract(sp->s_smopts, GSMOPTS));
133 1.1 christos }
134 1.1 christos
135 1.1 christos static void
136 1.1 christos printsmoptstbl(void)
137 1.1 christos {
138 1.1 christos struct smopts_s *sp;
139 1.1 christos char **argv, **ap;
140 1.1 christos int h;
141 1.1 christos int cnt;
142 1.1 christos
143 1.1 christos cnt = 1;
144 1.1 christos for (h = 0; h < sizeofarray(smoptstbl); h++ )
145 1.1 christos for (sp = smoptstbl[h]; sp && sp->s_name != NULL; sp = sp->s_link)
146 1.1 christos cnt++;
147 1.1 christos
148 1.1 christos argv = salloc(cnt * sizeof(*argv));
149 1.1 christos ap = argv;
150 1.1 christos for (h = 0; h < sizeofarray(smoptstbl); h++ )
151 1.1 christos for (sp = smoptstbl[h]; sp && sp->s_name != NULL; sp = sp->s_link)
152 1.1 christos *ap++ = sp->s_name;
153 1.1 christos *ap = NULL;
154 1.1 christos sort(argv);
155 1.1 christos for (ap = argv; *ap != NULL; ap++)
156 1.1 christos printsmopts(*ap);
157 1.1 christos }
158 1.1 christos
159 1.1 christos static struct name *
160 1.1 christos name_expand(char *sname, int ntype)
161 1.1 christos {
162 1.1 christos struct grouphead *gh;
163 1.1 christos struct name *np;
164 1.1 christos
165 1.1 christos if ((gh = findgroup(sname)) != NULL) {
166 1.1 christos np = gexpand(NULL, gh, 0, ntype);
167 1.1 christos }
168 1.1 christos else {
169 1.1 christos np = csalloc(1, sizeof(*np));
170 1.1 christos np->n_name = sname;
171 1.1 christos np->n_type = ntype;
172 1.1 christos }
173 1.1 christos return np;
174 1.1 christos }
175 1.1 christos
176 1.1 christos static struct name *
177 1.1 christos ncalloc(char *str, int ntype)
178 1.1 christos {
179 1.1 christos struct name *np;
180 1.1 christos
181 1.1 christos np = ecalloc(1, sizeof *np);
182 1.1 christos np->n_type = ntype;
183 1.1 christos np->n_name = vcopy(str);
184 1.1 christos return(np);
185 1.1 christos }
186 1.1 christos
187 1.1 christos static void
188 1.1 christos smopts_core(const char *sname, char **argv)
189 1.1 christos {
190 1.1 christos struct smopts_s *sp;
191 1.1 christos struct name *np, *t;
192 1.1 christos int h;
193 1.1 christos char **ap;
194 1.1 christos
195 1.1 christos if ((sp = findsmopts(sname, 1)) != NULL) {
196 1.1 christos char *cp;
197 1.1 christos cp = detract(sp->s_smopts, GSMOPTS);
198 1.1 christos (void)printf("%s already defined as: %s\n", sname, cp);
199 1.1 christos return;
200 1.1 christos }
201 1.1 christos h = hashcase(sname);
202 1.1 christos sp = ecalloc(1, sizeof *sp);
203 1.1 christos sp->s_name = vcopy(sname);
204 1.1 christos if (smoptstbl[h])
205 1.1 christos sp->s_link = smoptstbl[h];
206 1.1 christos smoptstbl[h] = sp;
207 1.1 christos
208 1.1 christos np = NULL;
209 1.1 christos for (ap = argv + 1; *ap != NULL; ap++) {
210 1.1 christos t = ncalloc(*ap, GSMOPTS);
211 1.1 christos if (sp->s_smopts == NULL)
212 1.1 christos sp->s_smopts = t;
213 1.1 christos else
214 1.1 christos np->n_flink = t;
215 1.1 christos t->n_blink = np;
216 1.1 christos np = t;
217 1.1 christos }
218 1.1 christos }
219 1.1 christos
220 1.1 christos int
221 1.1 christos smoptscmd(void *v)
222 1.1 christos {
223 1.1 christos struct name *np;
224 1.1 christos char **argv = v;
225 1.1 christos
226 1.1 christos if (*argv == NULL) {
227 1.1 christos printsmoptstbl();
228 1.1 christos return 0;
229 1.1 christos }
230 1.1 christos np = name_expand(argv[0], GTO);
231 1.1 christos
232 1.1 christos if (argv[1] == NULL) {
233 1.1 christos for (/*EMPTY*/; np; np = np->n_flink)
234 1.1 christos printsmopts(np->n_name);
235 1.1 christos return 0;
236 1.1 christos }
237 1.1 christos for (/*EMPTY*/; np; np = np->n_flink)
238 1.1 christos smopts_core(np->n_name, argv);
239 1.1 christos
240 1.1 christos return 0;
241 1.1 christos }
242 1.1 christos
243 1.1 christos static void
244 1.1 christos free_name(struct name *np)
245 1.1 christos {
246 1.1 christos struct name *next_np;
247 1.1 christos for (/*EMPTY*/; np; np = next_np) {
248 1.1 christos next_np = np->n_flink;
249 1.1 christos free(next_np);
250 1.1 christos }
251 1.1 christos }
252 1.1 christos
253 1.1 christos static void
254 1.1 christos delsmopts(char *name)
255 1.1 christos {
256 1.1 christos struct smopts_s *sp;
257 1.1 christos struct smopts_s **last_link;
258 1.1 christos
259 1.1 christos last_link = &smoptstbl[hashcase(name)];
260 1.1 christos for (sp = *last_link; sp; sp = sp->s_link) {
261 1.1 christos if (strcasecmp(sp->s_name, name) == 0) {
262 1.1 christos *last_link = sp->s_link;
263 1.1 christos free_name(sp->s_smopts);
264 1.1 christos free(sp);
265 1.1 christos }
266 1.1 christos }
267 1.1 christos }
268 1.1 christos
269 1.1 christos int
270 1.1 christos unsmopts(void *v)
271 1.1 christos {
272 1.1 christos struct name *np;
273 1.1 christos char **argv, **ap;
274 1.1 christos
275 1.1 christos argv = v;
276 1.1 christos for (ap = argv; *ap != NULL; ap++)
277 1.1 christos for (np = name_expand(*ap, GTO); np; np = np->n_flink)
278 1.1 christos delsmopts(np->n_name);
279 1.1 christos return 0;
280 1.1 christos }
281 1.1 christos #endif /* SMOPTS_CMD */
282