]>
git.proxmox.com Git - mirror_frr.git/blob - lib/defaults.c
2 * FRR switchable defaults.
3 * Copyright (c) 2017-2019 David Lamparter, for NetDEF, Inc.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 static char df_version
[128] = FRR_VER_SHORT
, df_profile
[128] = DFLT_NAME
;
24 static struct frr_default
*dflt_first
= NULL
, **dflt_next
= &dflt_first
;
26 /* these are global for all FRR daemons. they have to be, since we write an
27 * integrated config with the same value for all daemons.
29 const char *frr_defaults_profiles
[] = {
35 static int version_value(int ch
)
37 /* non-ASCII shouldn't happen */
38 if (ch
< 0 || ch
>= 128)
41 /* ~foo sorts older than nothing */
47 return 0x100 + tolower(ch
);
49 /* punctuation and digits (and everything else) */
53 int frr_version_cmp(const char *aa
, const char *bb
)
55 const char *apos
= aa
, *bpos
= bb
;
57 /* || is correct, we won't scan past the end of a string since that
58 * doesn't compare equal to anything else */
59 while (apos
[0] || bpos
[0]) {
60 if (isdigit((unsigned char)apos
[0]) &&
61 isdigit((unsigned char)bpos
[0])) {
63 char *aend
= NULL
, *bend
= NULL
;
65 av
= strtoul(apos
, &aend
, 10);
66 bv
= strtoul(bpos
, &bend
, 10);
77 int a
= version_value(*apos
++);
78 int b
= version_value(*bpos
++);
88 static void frr_default_apply_one(struct frr_default
*dflt
, bool check
);
90 void frr_default_add(struct frr_default
*dflt
)
94 dflt_next
= &dflt
->next
;
96 frr_default_apply_one(dflt
, true);
99 static bool frr_match_version(const char *name
, const char *vspec
,
100 const char *version
, bool check
)
117 /* NULL = all versions */
120 for (s
= specs
; s
->str
; s
++)
121 if (!strncmp(s
->str
, vspec
, strlen(s
->str
)))
125 fprintf(stderr
, "invalid version specifier for %s: %s",
127 /* invalid version spec, never matches */
131 vspec
+= strlen(s
->str
);
132 while (isspace((unsigned char)*vspec
))
135 cmp
= frr_version_cmp(version
, vspec
);
136 if (cmp
== s
->dir
|| (s
->eq
&& cmp
== 0))
142 static void frr_default_apply_one(struct frr_default
*dflt
, bool check
)
144 struct frr_default_entry
*entry
= dflt
->entries
;
145 struct frr_default_entry
*dfltentry
= NULL
, *saveentry
= NULL
;
147 for (; entry
->match_version
|| entry
->match_profile
; entry
++) {
148 if (entry
->match_profile
149 && strcmp(entry
->match_profile
, df_profile
))
152 if (!dfltentry
&& frr_match_version(dflt
->name
,
153 entry
->match_version
, df_version
, check
))
155 if (!saveentry
&& frr_match_version(dflt
->name
,
156 entry
->match_version
, FRR_VER_SHORT
, check
))
159 if (dfltentry
&& saveentry
&& !check
)
162 /* found default or arrived at last entry that has NULL,NULL spec */
170 *dflt
->dflt_bool
= dfltentry
->val_bool
;
172 *dflt
->dflt_str
= dfltentry
->val_str
;
174 *dflt
->dflt_long
= dfltentry
->val_long
;
175 if (dflt
->dflt_ulong
)
176 *dflt
->dflt_ulong
= dfltentry
->val_ulong
;
177 if (dflt
->dflt_float
)
178 *dflt
->dflt_float
= dfltentry
->val_float
;
180 *dflt
->save_bool
= saveentry
->val_bool
;
182 *dflt
->save_str
= saveentry
->val_str
;
184 *dflt
->save_long
= saveentry
->val_long
;
185 if (dflt
->save_ulong
)
186 *dflt
->save_ulong
= saveentry
->val_ulong
;
187 if (dflt
->save_float
)
188 *dflt
->save_float
= saveentry
->val_float
;
191 void frr_defaults_apply(void)
193 struct frr_default
*dflt
;
195 for (dflt
= dflt_first
; dflt
; dflt
= dflt
->next
)
196 frr_default_apply_one(dflt
, false);
199 bool frr_defaults_profile_valid(const char *profile
)
203 for (p
= frr_defaults_profiles
; *p
; p
++)
204 if (!strcmp(profile
, *p
))
209 const char *frr_defaults_version(void)
214 const char *frr_defaults_profile(void)
219 void frr_defaults_version_set(const char *version
)
221 strlcpy(df_version
, version
, sizeof(df_version
));
222 frr_defaults_apply();
225 void frr_defaults_profile_set(const char *profile
)
227 strlcpy(df_profile
, profile
, sizeof(df_profile
));
228 frr_defaults_apply();