{GPL, other}: Relicense to MIT license.
[collectd.git] / src / configfile.h
1 /**
2  * collectd - src/configfile.h
3  * Copyright (C) 2005-2011  Florian octo Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  **/
26
27 #ifndef CONFIGFILE_H
28 #define CONFIGFILE_H
29
30 #include "collectd.h"
31 #include "utils_time.h"
32 #include "liboconfig/oconfig.h"
33
34 /*
35  * DESCRIPTION
36  *  Remove a registered plugin from the internal data structures.
37  * 
38  * PARAMETERS
39  *  `type'      Name of the plugin (must be the same as passed to
40  *              `plugin_register'
41  */
42 void cf_unregister (const char *type);
43 void cf_unregister_complex (const char *type);
44
45 /*
46  * DESCRIPTION
47  *  `cf_register' is called by plugins that wish to receive config keys. The
48  *  plugin will then receive all keys it registered for if they're found in a
49  *  `<Plugin $type>' section.
50  *
51  * PARAMETERS
52  *  `type'      Name of the plugin (must be the same as passed to
53  *              `plugin_register'
54  *  `callback'  Pointer to the callback function. The callback must return zero
55  *              upon success, a value smaller than zero if it doesn't know how
56  *              to handle the `key' passed to it (the first argument) or a
57  *              value greater than zero if it knows how to handle the key but
58  *              failed.
59  *  `keys'      Array of key values this plugin wished to receive. The last
60  *              element must be a NULL-pointer.
61  *  `keys_num'  Number of elements in the array (not counting the last NULL-
62  *              pointer.
63  *
64  * NOTES
65  *  `cf_unregister' will be called for `type' to make sure only one record
66  *  exists for each `type' at any time. This means that `cf_register' may be
67  *  called multiple times, but only the last call will have an effect.
68  */
69 void cf_register (const char *type,
70                 int (*callback) (const char *, const char *),
71                 const char **keys, int keys_num);
72
73 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *));
74
75 /*
76  * DESCRIPTION
77  *  `cf_read' reads the config file `filename' and dispatches the read
78  *  information to functions/variables. Most important: Is calls `plugin_load'
79  *  to load specific plugins, depending on the current mode of operation.
80  *
81  * PARAMETERS
82  *  `filename'  An additional filename to look for. This function calls
83  *              `lc_process' which already searches many standard locations..
84  *              If set to NULL will use the `CONFIGFILE' define.
85  *
86  * RETURN VALUE
87  *  Returns zero upon success and non-zero otherwise. A error-message will have
88  *  been printed in this case.
89  */
90 int cf_read (char *filename);
91
92 int global_option_set (const char *option, const char *value);
93 const char *global_option_get (const char *option);
94 long global_option_get_long (const char *option, long default_value);
95 long global_option_get_long_in_range (const char *option, long default_value, long min, long max);
96
97 cdtime_t cf_get_default_interval (void);
98
99 /* Assures the config option is a string, duplicates it and returns the copy in
100  * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
101  * success. */
102 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string);
103
104 /* Assures the config option is a string and copies it to the provided buffer.
105  * Assures null-termination. */
106 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer,
107                 size_t buffer_size);
108
109 /* Assures the config option is a number and returns it as an int. */
110 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value);
111
112 /* Assures the config option is a number and returns it as a double. */
113 int cf_util_get_double (const oconfig_item_t *ci, double *ret_value);
114
115 /* Assures the config option is a boolean and assignes it to `ret_bool'.
116  * Otherwise, `ret_bool' is not changed and non-zero is returned. */
117 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool);
118
119 /* Assures the config option is a boolean and set or unset the given flag in
120  * `ret_value' as appropriate. Returns non-zero on error. */
121 int cf_util_get_flag (const oconfig_item_t *ci,
122                 unsigned int *ret_value, unsigned int flag);
123
124 /* Assures that the config option is a string or a number if the correct range
125  * of 1-65535. The string is then converted to a port number using
126  * `service_name_to_port_number' and returned.
127  * Returns the port number in the range [1-65535] or less than zero upon
128  * failure. */
129 int cf_util_get_port_number (const oconfig_item_t *ci);
130
131 /* Assures that the config option is either a service name (a string) or a port
132  * number (an integer in the appropriate range) and returns a newly allocated
133  * string. If ret_string points to a non-NULL pointer, it is freed before
134  * assigning a new value. */
135 int cf_util_get_service (const oconfig_item_t *ci, char **ret_string);
136
137 int cf_util_get_cdtime (const oconfig_item_t *ci, cdtime_t *ret_value);
138
139 #endif /* defined(CONFIGFILE_H) */