600127f0 |
1 | /* -*-c-*- |
600127f0 |
2 | * |
3 | * Framework for checking consistency of keys |
4 | * |
5 | * (c) 2001 Straylight/Edgeware |
6 | */ |
7 | |
45c0fd36 |
8 | /*----- Licensing notice --------------------------------------------------* |
600127f0 |
9 | * |
10 | * This file is part of Catacomb. |
11 | * |
12 | * Catacomb is free software; you can redistribute it and/or modify |
13 | * it under the terms of the GNU Library General Public License as |
14 | * published by the Free Software Foundation; either version 2 of the |
15 | * License, or (at your option) any later version. |
45c0fd36 |
16 | * |
600127f0 |
17 | * Catacomb is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 | * GNU Library General Public License for more details. |
45c0fd36 |
21 | * |
600127f0 |
22 | * You should have received a copy of the GNU Library General Public |
23 | * License along with Catacomb; if not, write to the Free |
24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
25 | * MA 02111-1307, USA. |
26 | */ |
27 | |
600127f0 |
28 | /*----- Header files ------------------------------------------------------*/ |
29 | |
30 | #include <stdarg.h> |
31 | |
32 | #include <mLib/dstr.h> |
33 | |
34 | #include "keycheck.h" |
35 | |
36 | /*----- Main code ---------------------------------------------------------*/ |
37 | |
38 | /* --- @keycheck_report@ --- * |
39 | * |
40 | * Arguments: @keycheck *kc@ = keycheck state |
41 | * @unsigned sev@ = severity of this report |
42 | * @const char *msg@ = message to send along |
43 | * @...@ = things to fill the message in with |
44 | * |
45 | * Returns: Zero to continue, or nonzero to stop and give up. |
46 | * |
47 | * Use: Reports a message to the user function. |
48 | */ |
49 | |
50 | int keycheck_report(keycheck *kc, unsigned sev, const char *msg, ...) |
51 | { |
52 | int rc; |
53 | va_list ap; |
54 | dstr d = DSTR_INIT; |
55 | |
56 | kc->sev[sev]++; |
57 | va_start(ap, msg); |
8ca05294 |
58 | dstr_vputf(&d, msg, &ap); |
600127f0 |
59 | va_end(ap); |
60 | rc = kc->func ? kc->func(sev, d.buf, kc->p) : 0; |
61 | dstr_destroy(&d); |
45c0fd36 |
62 | return (rc); |
600127f0 |
63 | } |
64 | |
65 | /* --- @keycheck_init@ --- * |
66 | * |
67 | * Arguments: @keycheck *kc@ = pointer to block to initialize |
68 | * @int (*func)(unsigned sev, const char *msg, void *p)@ = |
69 | * handler function for problems |
70 | * @void *p@ = pointer to give to handler |
71 | * |
72 | * Returns: --- |
73 | * |
74 | * Use: Initializes a key checking context. |
75 | */ |
76 | |
77 | void keycheck_init(keycheck *kc, |
78 | int (*func)(unsigned /*sev*/, |
79 | const char */*msg*/, |
80 | void */*p*/), |
81 | void *p) |
82 | { |
83 | unsigned i; |
84 | kc->func = func; |
85 | kc->p = p; |
86 | for (i = 0; i < KCSEV_MAX; i++) |
87 | kc->sev[i] = 0; |
88 | } |
89 | |
90 | /* --- @keycheck_allclear@ --- * |
91 | * |
92 | * Arguments: @keycheck *kc@ = pointer to keycheck context |
93 | * @unsigned sev@ = minimum severity to care about |
94 | * |
95 | * Returns: Nonzero if no problems of @sev@ or above were noticed. |
96 | */ |
97 | |
98 | int keycheck_allclear(keycheck *kc, unsigned sev) |
99 | { |
100 | while (sev < KCSEV_MAX) { |
101 | if (kc->sev[sev]) |
102 | return (0); |
103 | sev++; |
104 | } |
105 | return (1); |
106 | } |
107 | |
108 | /*----- That's all, folks -------------------------------------------------*/ |