2 * Configuration file parser for new and old Include and
5 * Kern Sibbald, March MMIII
10 Copyright (C) 2000-2003 Kern Sibbald and John Walker
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of
15 the License, or (at your option) any later version.
17 This program 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 GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write to the Free
24 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
32 /* Forward referenced subroutines */
34 void store_inc(LEX *lc, struct res_items *item, int index, int pass);
36 static void store_newinc(LEX *lc, struct res_items *item, int index, int pass);
37 static void store_match(LEX *lc, struct res_items *item, int index, int pass);
38 static void store_opts(LEX *lc, struct res_items *item, int index, int pass);
39 static void store_fname(LEX *lc, struct res_items *item, int index, int pass);
40 static void store_base(LEX *lc, struct res_items *item, int index, int pass);
41 static void setup_current_opts(void);
44 /* We build the current resource here as we are
45 * scanning the resource configuration definition,
46 * then move it to allocated memory when the resource
50 extern int res_all_size;
52 /* We build the current new Include and Exclude items here */
53 static INCEXE res_incexe;
56 * new Include/Exclude items
57 * name handler value code flags default_value
59 static struct res_items newinc_items[] = {
60 {"compression", store_opts, NULL, 0, 0, 0},
61 {"signature", store_opts, NULL, 0, 0, 0},
62 {"verify", store_opts, NULL, 0, 0, 0},
63 {"onefs", store_opts, NULL, 0, 0, 0},
64 {"recurse", store_opts, NULL, 0, 0, 0},
65 {"sparse", store_opts, NULL, 0, 0, 0},
66 {"readfifo", store_opts, NULL, 0, 0, 0},
67 {"replace", store_opts, NULL, 0, 0, 0},
68 {"portable", store_opts, NULL, 0, 0, 0},
69 {"match", store_match, NULL, 0, 0, 0},
70 {"file", store_fname, NULL, 0, 0, 0},
71 {"base", store_base, NULL, 0, 0, 0},
72 {NULL, NULL, NULL, 0, 0, 0}
75 /* Define FileSet KeyWord values */
78 #define INC_KW_COMPRESSION 1
79 #define INC_KW_SIGNATURE 2
80 #define INC_KW_ENCRYPTION 3
81 #define INC_KW_VERIFY 4
82 #define INC_KW_ONEFS 5
83 #define INC_KW_RECURSE 6
84 #define INC_KW_SPARSE 7
85 #define INC_KW_REPLACE 8 /* restore options */
86 #define INC_KW_READFIFO 9 /* Causes fifo data to be read */
87 #define INC_KW_PORTABLE 10
89 /* Include keywords -- these are keywords that can appear
90 * in the options lists of an old include ( Include = compression= ...)
92 static struct s_kw FS_option_kw[] = {
93 {"compression", INC_KW_COMPRESSION},
94 {"signature", INC_KW_SIGNATURE},
95 {"encryption", INC_KW_ENCRYPTION},
96 {"verify", INC_KW_VERIFY},
97 {"onefs", INC_KW_ONEFS},
98 {"recurse", INC_KW_RECURSE},
99 {"sparse", INC_KW_SPARSE},
100 {"replace", INC_KW_REPLACE},
101 {"readfifo", INC_KW_READFIFO},
102 {"portable", INC_KW_PORTABLE},
106 /* Options for FileSet keywords */
115 * Options permitted for each keyword and resulting value.
116 * The output goes into opts, which are then transmitted to
117 * the FD for application as options to the following list of
120 static struct s_fs_opt FS_options[] = {
121 {"md5", INC_KW_SIGNATURE, "M"},
122 {"sha1", INC_KW_SIGNATURE, "S"},
123 {"gzip", INC_KW_COMPRESSION, "Z6"},
124 {"gzip1", INC_KW_COMPRESSION, "Z1"},
125 {"gzip2", INC_KW_COMPRESSION, "Z2"},
126 {"gzip3", INC_KW_COMPRESSION, "Z3"},
127 {"gzip4", INC_KW_COMPRESSION, "Z4"},
128 {"gzip5", INC_KW_COMPRESSION, "Z5"},
129 {"gzip6", INC_KW_COMPRESSION, "Z6"},
130 {"gzip7", INC_KW_COMPRESSION, "Z7"},
131 {"gzip8", INC_KW_COMPRESSION, "Z8"},
132 {"gzip9", INC_KW_COMPRESSION, "Z9"},
133 {"blowfish", INC_KW_ENCRYPTION, "B"}, /* ***FIXME*** not implemented */
134 {"3des", INC_KW_ENCRYPTION, "3"}, /* ***FIXME*** not implemented */
135 {"yes", INC_KW_ONEFS, "0"},
136 {"no", INC_KW_ONEFS, "f"},
137 {"yes", INC_KW_RECURSE, "0"},
138 {"no", INC_KW_RECURSE, "h"},
139 {"yes", INC_KW_SPARSE, "s"},
140 {"no", INC_KW_SPARSE, "0"},
141 {"always", INC_KW_REPLACE, "a"},
142 {"ifnewer", INC_KW_REPLACE, "w"},
143 {"never", INC_KW_REPLACE, "n"},
144 {"yes", INC_KW_READFIFO, "r"},
145 {"no", INC_KW_READFIFO, "0"},
146 {"yes", INC_KW_PORTABLE, "p"},
147 {"no", INC_KW_PORTABLE, "0"},
154 * Scan for old Include options (keyword=option) is converted into one or
155 * two characters. Verifyopts=xxxx is Vxxxx:
157 static void scan_include_options(LEX *lc, int keyword, char *opts, int optlen)
162 option[0] = 0; /* default option = none */
163 option[2] = 0; /* terminate options */
164 token = lex_get_token(lc, T_NAME); /* expect at least one option */
165 if (keyword == INC_KW_VERIFY) { /* special case */
166 /* ***FIXME**** ensure these are in permitted set */
167 bstrncat(opts, "V", optlen); /* indicate Verify */
168 bstrncat(opts, lc->str, optlen);
169 bstrncat(opts, ":", optlen); /* terminate it */
172 * Standard keyword options for Include/Exclude
175 for (i=0; FS_options[i].name; i++) {
176 if (strcasecmp(lc->str, FS_options[i].name) == 0 && FS_options[i].keyword == keyword) {
177 /* NOTE! maximum 2 letters here or increase option[3] */
178 option[0] = FS_options[i].option[0];
179 option[1] = FS_options[i].option[1];
185 scan_err1(lc, "Expected a FileSet option keyword, got:%s:", lc->str);
186 } else { /* add option */
187 bstrncat(opts, option, optlen);
188 Dmsg3(200, "Catopts=%s option=%s optlen=%d\n", opts, option,optlen);
192 /* If option terminated by comma, eat it */
194 token = lex_get_token(lc, T_ALL); /* yes, eat comma */
198 /* Store FileSet Include/Exclude info */
199 void store_inc(LEX *lc, struct res_items *item, int index, int pass)
202 int options = lc->options;
208 * Decide if we are doing a new Include or an old include. The
209 * new Include is followed immediately by {, whereas the
210 * old include has options following the Include.
212 token = lex_get_token(lc, T_ALL);
213 if (token == T_BOB) {
214 store_newinc(lc, item, index, pass);
217 if (token != T_EQUALS) {
218 scan_err1(lc, _("Expecting an equals sign, got: %s\n"), lc->str);
220 lc->options |= LOPT_NO_IDENT; /* make spaces significant */
221 memset(&res_incexe, 0, sizeof(INCEXE));
223 /* Get include options */
225 while ((token=lex_get_token(lc, T_ALL)) != T_BOB) {
227 keyword = INC_KW_NONE;
228 for (i=0; FS_option_kw[i].name; i++) {
229 if (strcasecmp(lc->str, FS_option_kw[i].name) == 0) {
230 keyword = FS_option_kw[i].token;
234 if (keyword == INC_KW_NONE) {
235 scan_err1(lc, _("Expected a FileSet keyword, got: %s"), lc->str);
237 /* Option keyword should be following by = <option> */
238 if ((token=lex_get_token(lc, T_ALL)) != T_EQUALS) {
239 scan_err1(lc, _("expected an = following keyword, got: %s"), lc->str);
241 scan_include_options(lc, keyword, inc_opts, sizeof(inc_opts));
243 if (token == T_BOB) {
249 strcat(inc_opts, "0"); /* set no options */
251 inc_opts_len = strlen(inc_opts);
255 if (!res_all.res_fs.have_MD5) {
256 MD5Init(&res_all.res_fs.md5c);
257 res_all.res_fs.have_MD5 = TRUE;
259 setup_current_opts();
260 bstrncpy(res_incexe.current_opts->opts, inc_opts, MAX_FOPTS);
261 Dmsg1(200, "incexe opts=%s\n", res_incexe.current_opts->opts);
263 /* Create incexe structure */
264 Dmsg0(200, "Create INCEXE structure\n");
265 incexe = (INCEXE *)malloc(sizeof(INCEXE));
266 memcpy(incexe, &res_incexe, sizeof(INCEXE));
267 memset(&res_incexe, 0, sizeof(INCEXE));
268 if (item->code == 0) { /* include */
269 if (res_all.res_fs.num_includes == 0) {
270 res_all.res_fs.include_items = (INCEXE **)malloc(sizeof(INCEXE *));
272 res_all.res_fs.include_items = (INCEXE **)realloc(res_all.res_fs.include_items,
273 sizeof(INCEXE *) * (res_all.res_fs.num_includes + 1));
275 res_all.res_fs.include_items[res_all.res_fs.num_includes++] = incexe;
276 Dmsg1(200, "num_includes=%d\n", res_all.res_fs.num_includes);
277 } else { /* exclude */
278 if (res_all.res_fs.num_excludes == 0) {
279 res_all.res_fs.exclude_items = (INCEXE **)malloc(sizeof(INCEXE *));
281 res_all.res_fs.exclude_items = (INCEXE **)realloc(res_all.res_fs.exclude_items,
282 sizeof(INCEXE *) * (res_all.res_fs.num_excludes + 1));
284 res_all.res_fs.exclude_items[res_all.res_fs.num_excludes++] = incexe;
285 Dmsg1(200, "num_excludes=%d\n", res_all.res_fs.num_excludes);
288 /* Pickup include/exclude names. They are stored in INCEXE
289 * structures which contains the options and the name.
291 while ((token = lex_get_token(lc, T_ALL)) != T_EOB) {
298 case T_UNQUOTED_STRING:
299 case T_QUOTED_STRING:
300 if (res_all.res_fs.have_MD5) {
301 MD5Update(&res_all.res_fs.md5c, (unsigned char *)lc->str, lc->str_len);
303 if (incexe->name_list.size() == 0) {
304 incexe->name_list.init(10, true);
306 incexe->name_list.append(bstrdup(lc->str));
307 Dmsg1(200, "Add to name_list %s\n", lc->str);
310 scan_err1(lc, "Expected a filename, got: %s", lc->str);
313 /* Note, MD5Final is done in backup.c */
314 } else { /* pass 2 */
315 while (lex_get_token(lc, T_ALL) != T_EOB)
319 lc->options = options;
320 set_bit(index, res_all.hdr.item_present);
325 * Store FileSet FInclude/FExclude info
326 * Note, when this routine is called, we are inside a FileSet
327 * resource. We treat the Finclude/Fexeclude like a sort of
328 * mini-resource within the FileSet resource.
330 static void store_newinc(LEX *lc, struct res_items *item, int index, int pass)
335 if (!res_all.res_fs.have_MD5) {
336 MD5Init(&res_all.res_fs.md5c);
337 res_all.res_fs.have_MD5 = TRUE;
339 memset(&res_incexe, 0, sizeof(INCEXE));
340 res_all.res_fs.new_include = TRUE;
341 while ((token = lex_get_token(lc, T_ALL)) != T_EOF) {
342 if (token == T_EOL) {
345 if (token == T_EOB) {
348 if (token != T_IDENTIFIER) {
349 scan_err1(lc, _("Expecting keyword, got: %s\n"), lc->str);
351 for (i=0; newinc_items[i].name; i++) {
352 if (strcasecmp(newinc_items[i].name, lc->str) == 0) {
353 token = lex_get_token(lc, T_ALL);
354 if (token != T_EQUALS) {
355 scan_err1(lc, "expected an equals, got: %s", lc->str);
357 /* Call item handler */
358 newinc_items[i].handler(lc, &newinc_items[i], i, pass);
364 scan_err1(lc, "Keyword %s not permitted in this resource", lc->str);
368 incexe = (INCEXE *)malloc(sizeof(INCEXE));
369 memcpy(incexe, &res_incexe, sizeof(INCEXE));
370 memset(&res_incexe, 0, sizeof(INCEXE));
371 if (item->code == 0) { /* include */
372 if (res_all.res_fs.num_includes == 0) {
373 res_all.res_fs.include_items = (INCEXE **)malloc(sizeof(INCEXE *));
375 res_all.res_fs.include_items = (INCEXE **)realloc(res_all.res_fs.include_items,
376 sizeof(INCEXE *) * (res_all.res_fs.num_includes + 1));
378 res_all.res_fs.include_items[res_all.res_fs.num_includes++] = incexe;
379 Dmsg1(200, "num_includes=%d\n", res_all.res_fs.num_includes);
380 } else { /* exclude */
381 if (res_all.res_fs.num_excludes == 0) {
382 res_all.res_fs.exclude_items = (INCEXE **)malloc(sizeof(INCEXE *));
384 res_all.res_fs.exclude_items = (INCEXE **)realloc(res_all.res_fs.exclude_items,
385 sizeof(INCEXE *) * (res_all.res_fs.num_excludes + 1));
387 res_all.res_fs.exclude_items[res_all.res_fs.num_excludes++] = incexe;
388 Dmsg1(200, "num_excludes=%d\n", res_all.res_fs.num_excludes);
392 set_bit(index, res_all.hdr.item_present);
396 /* Store Match info */
397 static void store_match(LEX *lc, struct res_items *item, int index, int pass)
402 /* Pickup Match string
404 token = lex_get_token(lc, T_ALL);
407 case T_UNQUOTED_STRING:
408 case T_QUOTED_STRING:
409 setup_current_opts();
410 res_incexe.current_opts->match.append(bstrdup(lc->str));
413 scan_err1(lc, _("Expected a filename, got: %s\n"), lc->str);
415 } else { /* pass 2 */
416 lex_get_token(lc, T_ALL);
421 /* Store Base info */
422 static void store_base(LEX *lc, struct res_items *item, int index, int pass)
427 setup_current_opts();
429 * Pickup Base Job Name
431 token = lex_get_token(lc, T_NAME);
432 res_incexe.current_opts->base_list.append(bstrdup(lc->str));
433 } else { /* pass 2 */
434 lex_get_token(lc, T_ALL);
439 * Store Filename info. Note, for minor efficiency reasons, we
440 * always increase the name buffer by 10 items because we expect
441 * to add more entries.
443 static void store_fname(LEX *lc, struct res_items *item, int index, int pass)
449 /* Pickup Filename string
451 token = lex_get_token(lc, T_ALL);
454 case T_UNQUOTED_STRING:
455 case T_QUOTED_STRING:
456 if (res_all.res_fs.have_MD5) {
457 MD5Update(&res_all.res_fs.md5c, (unsigned char *)lc->str, lc->str_len);
459 incexe = &res_incexe;
460 if (incexe->name_list.size() == 0) {
461 incexe->name_list.init(10, true);
463 incexe->name_list.append(bstrdup(lc->str));
464 Dmsg1(200, "Add to name_list %s\n", lc->str);
467 scan_err1(lc, _("Expected a filename, got: %s"), lc->str);
469 } else { /* pass 2 */
470 lex_get_token(lc, T_ALL);
476 static void store_opts(LEX *lc, struct res_items *item, int index, int pass)
483 keyword = INC_KW_NONE;
484 for (i=0; FS_option_kw[i].name; i++) {
485 if (strcasecmp(item->name, FS_option_kw[i].name) == 0) {
486 keyword = FS_option_kw[i].token;
490 if (keyword == INC_KW_NONE) {
491 scan_err1(lc, "Expected a FileSet keyword, got: %s", lc->str);
493 Dmsg2(200, "keyword=%d %s\n", keyword, FS_option_kw[keyword].name);
494 scan_include_options(lc, keyword, inc_opts, sizeof(inc_opts));
497 setup_current_opts();
498 bstrncat(res_incexe.current_opts->opts, inc_opts, MAX_FOPTS);
506 /* If current_opts not defined, create first entry */
507 static void setup_current_opts(void)
509 if (res_incexe.current_opts == NULL) {
510 res_incexe.current_opts = (FOPTS *)malloc(sizeof(FOPTS));
511 memset(res_incexe.current_opts, 0, sizeof(FOPTS));
512 res_incexe.current_opts->match.init(1, true);
513 res_incexe.current_opts->base_list.init(1, true);
514 res_incexe.num_opts = 1;
515 res_incexe.opts_list = (FOPTS **)malloc(sizeof(FOPTS *));
516 res_incexe.opts_list[0] = res_incexe.current_opts;