OpenVAS Libraries  9.0.3
prefs.c
Go to the documentation of this file.
1 /* openvas-libraries/misc
2  * $Id$
3  * Description: Implementation of API to handle globally stored preferences
4  *
5  * Authors:
6  * Jan-Oliver Wagner <jan-oliver.wagner@greenbone.net>
7  *
8  * Copyright:
9  * Copyright (C) 2014 Greenbone Networks GmbH
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25 
39 #include <string.h> /* for strlen() */
40 #include <stdlib.h> /* for atoi() */
41 #include <stdio.h> /* for printf() */
42 #include <glib.h> /* for gchar */
43 
44 #include "../base/settings.h" /* for init_settings_iterator_from_file */
45 #include "arglists.h" /* for struct arglist */
46 
47 static struct arglist *global_prefs = NULL;
48 
54 static void
55 prefs_init (void)
56 {
57  if (global_prefs)
58  arg_free (global_prefs);
59 
60  global_prefs = g_malloc0 (sizeof (struct arglist));
61 }
62 
67 struct arglist *
69 {
70  if (!global_prefs)
71  prefs_init ();
72 
73  return global_prefs;
74 }
75 
85 const gchar *
86 prefs_get (const gchar * key)
87 {
88  if (!global_prefs)
89  prefs_init ();
90 
91  if (arg_get_type (global_prefs, key) != ARG_STRING)
92  return NULL;
93 
94  return arg_get_value (global_prefs, key);
95 }
96 
108 int
109 prefs_get_bool (const gchar * key)
110 {
111  if (!global_prefs)
112  prefs_init ();
113 
114  if (arg_get_type (global_prefs, key) == ARG_STRING)
115  {
116  gchar *str = arg_get_value (global_prefs, key);
117  if (str && !strcmp (str, "yes"))
118  return 1;
119  }
120 
121  return 0;
122 }
123 
132 void
133 prefs_set (const gchar * key, const gchar * value)
134 {
135  if (!global_prefs)
136  prefs_init ();
137 
138  if (arg_get_value (global_prefs, key))
139  {
140  gchar *old = arg_get_value (global_prefs, key);
141  arg_set_value (global_prefs, key, g_strdup (value));
142  g_free (old);
143  return;
144  }
145 
146  arg_add_value (global_prefs, key, ARG_STRING, g_strdup (value));
147 }
148 
154 void
155 prefs_config (const char *config)
156 {
157  settings_iterator_t settings;
158 
159  if (!global_prefs)
160  prefs_init ();
161 
162  if (!init_settings_iterator_from_file (&settings, config, "Misc"))
163  {
164  while (settings_iterator_next (&settings))
165  prefs_set (settings_iterator_name (&settings),
166  settings_iterator_value (&settings));
167 
168  cleanup_settings_iterator (&settings);
169  }
170 
171  prefs_set ("config_file", config);
172 }
173 
177 void
179 {
180  struct arglist * prefs = global_prefs;
181 
182  while (prefs && prefs->next)
183  {
184  printf ("%s = %s\n", prefs->name, (char *) prefs->value);
185  prefs = prefs->next;
186  }
187 }
188 
197 int
198 prefs_nvt_timeout (const char *oid)
199 {
200  char *pref_name = g_strdup_printf ("timeout.%s", oid);
201  const char * val = prefs_get (pref_name);
202  int timeout = (val ? atoi (val) : 0);
203 
204  g_free (pref_name);
205 
206  return timeout;
207 }
const gchar * settings_iterator_name(settings_iterator_t *iterator)
Get the name from a settings iterator.
Definition: settings.c:184
gboolean settings_iterator_next(settings_iterator_t *iterator)
Increment an iterator.
Definition: settings.c:168
const char * val
Definition: nasl_init.c:525
int arg_set_value(struct arglist *arglst, const char *name, void *value)
Definition: arglists.c:225
void arg_free(struct arglist *arg)
Definition: arglists.c:322
const char * oid
const gchar * prefs_get(const gchar *key)
Get a string preference value via a key.
Definition: prefs.c:86
void cleanup_settings_iterator(settings_iterator_t *iterator)
Cleanup a settings iterator.
Definition: settings.c:154
void * value
Definition: arglists.h:32
void arg_add_value(struct arglist *arglst, const char *name, int type, void *value)
Definition: arglists.c:170
void prefs_set(const gchar *key, const gchar *value)
Set a string preference value via a key.
Definition: prefs.c:133
struct arglist * next
Definition: arglists.h:33
const gchar * settings_iterator_value(settings_iterator_t *iterator)
Get the value from a settings iterator.
Definition: settings.c:197
void prefs_config(const char *config)
Apply the configs from given file as preferences.
Definition: prefs.c:155
#define ARG_STRING
Definition: arglists.h:38
struct arglist * preferences_get(void)
Get the pointer to the global preferences structure. Eventually this function should not be used anyw...
Definition: prefs.c:68
int init_settings_iterator_from_file(settings_iterator_t *iterator, const gchar *filename, const gchar *group)
Initialise a settings iterator from a file.
Definition: settings.c:115
char * name
Definition: arglists.h:31
int arg_get_type(struct arglist *args, const char *name)
Definition: arglists.c:268
void * arg_get_value(struct arglist *args, const char *name)
Definition: arglists.c:252
int prefs_nvt_timeout(const char *oid)
Returns the timeout defined by the client or 0 if none was set.
Definition: prefs.c:198
int prefs_get_bool(const gchar *key)
Get a boolean expression of a preference value via a key.
Definition: prefs.c:109
void prefs_dump(void)
Dump the preferences to stdout.
Definition: prefs.c:178