maint: build via make CFLAGS='-DGNULIB_POSIXCHECK=1'; address warnings
[iwhd.git] / setup.h
blobb0b68821f35e8f7b170c463a307911c30f168ccf
1 /* Copyright (C) 2010-2011 Red Hat, Inc.
3 This program is free software: you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation, either version 3 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>. */
16 #if !defined(_SETUP_H)
17 #define _SETUP_H
19 #include <glib.h>
20 #include <curl/curl.h> /* needed by stuff in state_defs.h (from backend.h) */
21 #include <microhttpd.h> /* ditto */
22 #include <assert.h>
23 #include "backend.h"
24 #include "hash.h"
25 #include "hash-pjw.h"
27 typedef struct _provider {
28 const char *name;
29 const char *type;
30 const char *host;
31 int port;
32 int deleted;
33 const char *username;
34 const char *password;
35 const char *path;
36 backend_func_tbl *func_tbl;
37 Hash_table *attrs;
38 char *token;
39 } provider_t;
41 provider_t *g_master_prov;
43 const char *parse_config (char *);
44 provider_t *get_provider (const char *name);
45 void update_provider (const char *provname,
46 const char *username,
47 const char *password);
48 const char *get_provider_value (const provider_t *prov,
49 const char *fname);
51 const char *auto_config (void);
52 int validate_provider (Hash_table *h);
53 provider_t *find_provider (const char *name);
54 int add_provider (Hash_table *h);
55 provider_t *get_main_provider (void);
56 void set_main_provider (provider_t *prov);
58 typedef int (*prov_iterator_fn) (provider_t *, void *);
59 int prov_do_for_each (prov_iterator_fn fn, void *client_data);
60 provider_t **hash_get_prov_list (size_t *n);
62 struct kv_pair
64 char *key;
65 char *val;
68 #define STREQ(a, b) (strcmp (a, b) == 0)
70 enum { SMALL_PRIME = 13 };
72 static inline size_t
73 kv_hash (void const *x, size_t table_size)
75 struct kv_pair const *p = x;
76 return hash_pjw (p->key, table_size);
79 static inline bool
80 kv_compare (void const *x, void const *y)
82 struct kv_pair const *u = x;
83 struct kv_pair const *v = y;
84 return STREQ (u->key, v->key) ? true : false;
87 static inline int
88 kv_hash_insert_new (Hash_table *ht, char *k, char *v)
90 struct kv_pair *kv = malloc (sizeof *kv);
91 if (!kv)
92 return 0;
93 kv->key = k;
94 kv->val = v;
95 void *e = hash_insert (ht, kv);
96 assert (e == kv);
97 return 1;
100 /* Given a hash table and key K, return the value
101 corresponding to K. The caller must not free K. */
102 static char *
103 kv_hash_lookup (Hash_table const *ht, char const *k)
105 struct kv_pair kv;
106 kv.key = (char *) k;
107 struct kv_pair *p = hash_lookup (ht, &kv);
108 return p ? p->val : NULL;
111 static void
112 kv_hash_delete (Hash_table *ht, char const *k)
114 struct kv_pair kv;
115 kv.key = (char *) k;
116 struct kv_pair *p = hash_delete (ht, &kv);
117 if (p) {
118 free (p->key);
119 free (p->val);
120 free (p);
124 /* Determine whether a key/value pair exists for which PRED_FN returns true.
125 If so, return a pointer to that kv_pair. Otherwise, return NULL. */
126 static struct kv_pair *
127 kv_find_val (Hash_table *ht, Hash_processor pred_fn, void *ctx)
129 void *found_kv = NULL;
130 hash_do_for_each (ht, pred_fn, &found_kv);
131 return found_kv;
134 #endif