Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / libbind / dist / irs / gen_ng.c
blobc2f0b2ab6eeb9e77e2743e2b1500cffe8bbb8be1
1 /* $NetBSD$ */
3 /*
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996,1999 by Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #if !defined(LINT) && !defined(CODECENTER)
21 static const char rcsid[] = "Id: gen_ng.c,v 1.3 2005/04/27 04:56:23 sra Exp";
22 #endif
24 /* Imports */
26 #include "port_before.h"
28 #include <sys/types.h>
30 #include <netinet/in.h>
31 #include <arpa/nameser.h>
32 #include <resolv.h>
34 #include <errno.h>
35 #include <stdlib.h>
36 #include <string.h>
38 #include <isc/memcluster.h>
39 #include <irs.h>
41 #include "port_after.h"
43 #include "irs_p.h"
44 #include "gen_p.h"
46 /* Types */
48 struct pvt {
49 struct irs_rule * rules;
50 struct irs_rule * rule;
51 char * curgroup;
54 /* Forward */
56 static void ng_close(struct irs_ng *);
57 static int ng_next(struct irs_ng *, const char **,
58 const char **, const char **);
59 static int ng_test(struct irs_ng *, const char *,
60 const char *, const char *,
61 const char *);
62 static void ng_rewind(struct irs_ng *, const char *);
63 static void ng_minimize(struct irs_ng *);
65 /* Public */
67 struct irs_ng *
68 irs_gen_ng(struct irs_acc *this) {
69 struct gen_p *accpvt = (struct gen_p *)this->private;
70 struct irs_ng *ng;
71 struct pvt *pvt;
73 if (!(ng = memget(sizeof *ng))) {
74 errno = ENOMEM;
75 return (NULL);
77 memset(ng, 0x5e, sizeof *ng);
78 if (!(pvt = memget(sizeof *pvt))) {
79 memput(ng, sizeof *ng);
80 errno = ENOMEM;
81 return (NULL);
83 memset(pvt, 0, sizeof *pvt);
84 pvt->rules = accpvt->map_rules[irs_ng];
85 pvt->rule = pvt->rules;
86 ng->private = pvt;
87 ng->close = ng_close;
88 ng->next = ng_next;
89 ng->test = ng_test;
90 ng->rewind = ng_rewind;
91 ng->minimize = ng_minimize;
92 return (ng);
95 /* Methods */
97 static void
98 ng_close(struct irs_ng *this) {
99 struct pvt *pvt = (struct pvt *)this->private;
101 ng_minimize(this);
102 if (pvt->curgroup)
103 free(pvt->curgroup);
104 memput(pvt, sizeof *pvt);
105 memput(this, sizeof *this);
108 static int
109 ng_next(struct irs_ng *this, const char **host, const char **user,
110 const char **domain)
112 struct pvt *pvt = (struct pvt *)this->private;
113 struct irs_ng *ng;
115 while (pvt->rule) {
116 ng = pvt->rule->inst->ng;
117 if ((*ng->next)(ng, host, user, domain) == 1)
118 return (1);
119 if (!(pvt->rule->flags & IRS_CONTINUE))
120 break;
121 pvt->rule = pvt->rule->next;
122 if (pvt->rule) {
123 ng = pvt->rule->inst->ng;
124 (*ng->rewind)(ng, pvt->curgroup);
127 return (0);
130 static int
131 ng_test(struct irs_ng *this, const char *name,
132 const char *user, const char *host, const char *domain)
134 struct pvt *pvt = (struct pvt *)this->private;
135 struct irs_rule *rule;
136 struct irs_ng *ng;
137 int rval;
139 rval = 0;
140 for (rule = pvt->rules; rule; rule = rule->next) {
141 ng = rule->inst->ng;
142 rval = (*ng->test)(ng, name, user, host, domain);
143 if (rval || !(rule->flags & IRS_CONTINUE))
144 break;
146 return (rval);
149 static void
150 ng_rewind(struct irs_ng *this, const char *group) {
151 struct pvt *pvt = (struct pvt *)this->private;
152 struct irs_ng *ng;
154 pvt->rule = pvt->rules;
155 if (pvt->rule) {
156 if (pvt->curgroup)
157 free(pvt->curgroup);
158 pvt->curgroup = strdup(group);
159 ng = pvt->rule->inst->ng;
160 (*ng->rewind)(ng, pvt->curgroup);
164 static void
165 ng_minimize(struct irs_ng *this) {
166 struct pvt *pvt = (struct pvt *)this->private;
167 struct irs_rule *rule;
169 for (rule = pvt->rules; rule != NULL; rule = rule->next) {
170 struct irs_ng *ng = rule->inst->ng;
172 (*ng->minimize)(ng);
176 /*! \file */