2 Copyright (C) 2011-2011 Bacula Systems(R) SA
4 The main author of Bacula is Kern Sibbald, with contributions from
5 many others, a complete list can be found in the file AUTHORS.
6 This program is Free Software; you can modify it under the terms of
7 version three of the GNU Affero General Public License as published by the
8 Free Software Foundation, which is listed in the file LICENSE.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 Bacula® is a registered trademark of Kern Sibbald.
21 Bacula Systems(R) is a trademark of Bacula Systems SA.
22 Bacula Enterprise(TM) is a trademark of Bacula Systems SA.
24 The licensor of Bacula Enterprise(TM) is Bacula Systems(R) SA,
25 Rue Galilee 5, 1400 Yverdon-les-Bains, Switzerland.
32 * Plugin has a internal C structure that describes the configuration:
35 * The ConfigFile object can generate a text file that describes the C
36 * structure. This text format is saved as RestoreObject in the catalog.
38 * struct ini_items[] -> register_items() -> serialize() -> RestoreObject R1
40 * On the Director side, at the restore time, we can analyse this text to
41 * get the C structure.
43 * RestoreObject R1 -> write to disk -> unserialize() -> struct ini_items[]
45 * Once done, we can ask questions to the user at the restore time and fill
46 * the C struct with answers. The Director can send back as a RestoreObject
47 * the result of the questionnaire.
49 * struct ini_items[] -> UAContext -> dump_result() -> FD as RestoreObject R2
51 * On the Plugin side, it can get back the C structure and use it.
52 * RestoreObject R2 -> parse() -> struct ini_items[]
58 /* Used to store result */
61 char nameval[MAX_NAME_LENGTH];
68 /* These functions are used to convert a string to the appropriate value */
70 bool (INI_ITEM_HANDLER)(LEX *lc, ConfigFile *inifile,
71 struct ini_items *item);
73 /* If no items are registred at the scan time, we detect this list from
77 const char *name; /* keyword name */
78 INI_ITEM_HANDLER *handler; /* type accepted */
79 const char *comment; /* comment associated, used in prompt */
81 int required; /* optional required or not */
82 const char *default_value; /* optional default value */
84 const char *re_value; /* optional regexp associated */
85 const char *in_values; /* optional list of values */
87 bool found; /* if val is set */
88 item_value val; /* val contains the value */
91 /* When reading a ini file, we limit the number of items that we
94 #define MAX_INI_ITEMS 32
96 /* Special RestoreObject name used to get user input at restore time */
97 #define INI_RESTORE_OBJECT_NAME "RestoreOptions"
99 /* Can be used to set re_value, in_value, default_value, found and val to 0
100 * G++ looks to allow partial declaration, let see with an other compiler
102 #define ITEMS_DEFAULT NULL,NULL,NULL,0,{0}
105 * Handle simple configuration file such as "ini" files.
106 * key1 = val # comment
110 * For usage example, see ini.c TEST_PROGRAM
116 LEX *lc; /* Lex parser */
117 bool items_allocated;
120 JCR *jcr; /* JCR needed for Jmsg */
121 int version; /* Internal version check */
122 int sizeof_ini_items; /* Extra check when using dynamic loading */
123 bool unlink_temp_file; /* Unlink temp file when destroying object */
124 struct ini_items *items; /* Structure of the config file */
125 POOLMEM *out_fname; /* Can be used to dump config to disk */
126 POOLMEM *edit; /* Can be used to build result file */
127 char *plugin_name; /* Used to store owner of this ConfigFile */
137 items_allocated = false;
138 unlink_temp_file = true;
139 edit = get_pool_memory(PM_FNAME);
141 sizeof_ini_items = sizeof(struct ini_items);
149 free_pool_memory(edit);
152 if (unlink_temp_file) {
155 free_pool_memory(out_fname);
164 /* Dump a config string to out_fname */
165 bool dump_string(const char *buf, int32_t len);
167 void set_plugin_name(char *n) {
171 plugin_name = bstrdup(n);
174 /* JCR needed for Jmsg */
175 void set_jcr(JCR *ajcr) {
179 void set_unlink_temp_file(bool val) {
180 unlink_temp_file = val;
183 /* Free malloced items such as char* or alist or items */
186 /* Clear items member */
189 /* Dump the item table to a file (used on plugin side) */
190 bool serialize(const char *fname);
192 /* Dump the item table format to a buffer (used on plugin side)
193 * returns the length of the buffer, -1 if error
195 int serialize(POOLMEM **buf);
197 /* Dump the item table content to a buffer */
198 int dump_results(POOLMEM **buf);
200 /* Get item position in items list (useful when dynamic) */
201 int get_item(const char *name);
203 /* Register config file structure, if size doesn't match */
204 bool register_items(struct ini_items *aitems, int size) {
206 if (sizeof_ini_items == size) {
207 for (i = 0; aitems[i].name ; i++);
208 items = (struct ini_items*) malloc((i+1) * size); /* NULL terminated */
209 memcpy(items, aitems, (i+1) * size);
210 items_allocated = false; /* we copy only pointers, don't free them */
216 /* Parse a ini file with a item list previously registred (plugin side) */
217 bool parse(const char *filename);
219 /* Create a item list from a ini file (director side) */
220 bool unserialize(const char *filename);
222 /* Get Menu for an entry */
223 char *get_menu(int index, POOLMEM **dest);
225 /* Check if an entry is a part of a menu */
226 bool is_in_menu(int index, const char *menu);
230 * Standard global parsers defined in ini.c
231 * When called with lc=NULL, it converts the item value back in inifile->edit
234 bool ini_store_str(LEX *lc, ConfigFile *inifile, ini_items *item);
235 bool ini_store_name(LEX *lc, ConfigFile *inifile, ini_items *item);
236 bool ini_store_alist_str(LEX *lc, ConfigFile *inifile, ini_items *item);
237 bool ini_store_pint64(LEX *lc, ConfigFile *inifile, ini_items *item);
238 bool ini_store_int64(LEX *lc, ConfigFile *inifile, ini_items *item);
239 bool ini_store_pint32(LEX *lc, ConfigFile *inifile, ini_items *item);
240 bool ini_store_int32(LEX *lc, ConfigFile *inifile, ini_items *item);
241 bool ini_store_bool(LEX *lc, ConfigFile *inifile, ini_items *item);
243 /* Get handler code from handler @ */
244 const char *ini_get_store_code(INI_ITEM_HANDLER *handler);
246 /* Get handler function from handler name */
247 INI_ITEM_HANDLER *ini_get_store_handler(const char *);