]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/parse_conf.c
aabfa3328c6bf88610c2fc26b440f39d956d4e19
[bacula/bacula] / bacula / src / lib / parse_conf.c
1 /*
2  *   Master Configuration routines.
3  *  
4  *   This file contains the common parts of the Bacula
5  *   configuration routines.
6  *
7  *   Note, the configuration file parser consists of three parts
8  *
9  *   1. The generic lexical scanner in lib/lex.c and lib/lex.h
10  *
11  *   2. The generic config  scanner in lib/parse_conf.c and 
12  *      lib/parse_conf.h.
13  *      These files contain the parser code, some utility
14  *      routines, and the common store routines (name, int,
15  *      string).
16  *
17  *   3. The daemon specific file, which contains the Resource
18  *      definitions as well as any specific store routines
19  *      for the resource records.
20  *
21  *     Kern Sibbald, January MM
22  *
23  *   Version $Id$
24  */
25
26 /*
27    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
28
29    This program is free software; you can redistribute it and/or
30    modify it under the terms of the GNU General Public License as
31    published by the Free Software Foundation; either version 2 of
32    the License, or (at your option) any later version.
33
34    This program is distributed in the hope that it will be useful,
35    but WITHOUT ANY WARRANTY; without even the implied warranty of
36    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
37    General Public License for more details.
38
39    You should have received a copy of the GNU General Public
40    License along with this program; if not, write to the Free
41    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
42    MA 02111-1307, USA.
43
44  */
45
46
47 #include "bacula.h"
48
49 extern int debug_level;
50
51 /* Each daemon has a slightly different set of 
52  * resources, so it will define the following
53  * global values.
54  */
55 extern int r_first;
56 extern int r_last;
57 extern pthread_mutex_t res_mutex;
58 extern struct s_res resources[];
59 extern CURES res_all;
60 extern int res_all_size;
61
62 static int res_locked = 0;             /* set when resource chains locked */
63
64 /* Forward referenced subroutines */
65 static void scan_types(LEX *lc, MSGS *msg, int dest, char *where, char *cmd);
66
67
68 /* Common Resource definitions */
69
70 /* Message resource directives
71  *  name         handler      value       code   flags  default_value
72  */
73 struct res_items msgs_items[] = {
74    {"name",        store_name,    ITEM(res_msgs.hdr.name),  0, 0, 0},
75    {"description", store_str,     ITEM(res_msgs.hdr.desc),  0, 0, 0},
76    {"mailcommand", store_str,     ITEM(res_msgs.mail_cmd),  0, 0, 0},
77    {"operatorcommand", store_str, ITEM(res_msgs.operator_cmd), 0, 0, 0},
78    {"syslog",      store_msgs, ITEM(res_msgs), MD_SYSLOG,   0, 0}, 
79    {"mail",        store_msgs, ITEM(res_msgs), MD_MAIL,     0, 0},
80    {"mailonerror", store_msgs, ITEM(res_msgs), MD_MAIL_ON_ERROR, 0, 0},
81    {"file",        store_msgs, ITEM(res_msgs), MD_FILE,     0, 0},
82    {"append",      store_msgs, ITEM(res_msgs), MD_APPEND,   0, 0},
83    {"stdout",      store_msgs, ITEM(res_msgs), MD_STDOUT,   0, 0},
84    {"stderr",      store_msgs, ITEM(res_msgs), MD_STDERR,   0, 0},
85    {"director",    store_msgs, ITEM(res_msgs), MD_DIRECTOR, 0, 0},
86    {"console",     store_msgs, ITEM(res_msgs), MD_CONSOLE,  0, 0},   
87    {"operator",    store_msgs, ITEM(res_msgs), MD_OPERATOR, 0, 0},
88    {NULL, NULL,    NULL, 0}
89 };
90
91 struct s_mtypes {       
92    char *name;
93    int token;   
94 };
95 /* Various message types */
96 static struct s_mtypes msg_types[] = {
97    {"debug",         M_DEBUG},
98    {"abort",         M_ABORT},
99    {"fatal",         M_FATAL},
100    {"error",         M_ERROR},
101    {"warning",       M_WARNING},
102    {"info",          M_INFO},
103    {"saved",         M_SAVED},
104    {"notsaved",      M_NOTSAVED},
105    {"skipped",       M_SKIPPED},
106    {"mount",         M_MOUNT},
107    {"terminate",     M_TERM},
108    {"all",           M_MAX+1},
109    {NULL,            0}
110 };
111
112
113 /* Simply print a message */
114 static void prtmsg(void *sock, char *fmt, ...)
115 {
116    va_list arg_ptr;
117
118    va_start(arg_ptr, fmt);
119    vfprintf(stdout, fmt, arg_ptr);
120    va_end(arg_ptr);
121 }
122
123 #ifdef DEBUG
124 char *res_to_str(int rcode)
125 {
126    if (rcode < r_first || rcode > r_last) {
127       return "***UNKNOWN***";
128    } else {
129       return resources[rcode-r_first].name;
130    }
131 }
132 #endif /* DEBUG */
133
134
135 /* 
136  * Initialize the static structure to zeros, then
137  *  apply all the default values.
138  */
139 void init_resource(int type, struct res_items *items)
140 {
141    int i;
142    int rindex = type - r_first;
143
144    memset(&res_all, 0, res_all_size);
145    res_all.hdr.rcode = type;
146    res_all.hdr.refcnt = 1;
147
148    for (i=0; items[i].name; i++) {
149       Dmsg3(300, "Item=%s def=%s defval=%d\n", items[i].name,
150             (items[i].flags & ITEM_DEFAULT) ? "yes" : "no",      
151             items[i].default_value);
152       if (items[i].flags & ITEM_DEFAULT && items[i].default_value != 0) {
153          if (items[i].handler == store_yesno) {
154             *(int *)(items[i].value) |= items[i].code;
155          } else if (items[i].handler == store_pint || 
156                     items[i].handler == store_int) {
157             *(int *)(items[i].value) = items[i].default_value;
158          } else if (items[i].handler == store_int64) {
159             *(int64_t *)(items[i].value) = items[i].default_value;
160          } else if (items[i].handler == store_size ||
161                     items[i].handler == store_time) {
162             *(uint64_t *)(items[i].value) = items[i].default_value;
163          }
164       }
165       /* If this triggers, take a look at lib/parse_conf.h */
166       if (i >= MAX_RES_ITEMS) {
167          Emsg1(M_ABORT, 0, _("Too many items in %s resource\n"), resources[rindex]);
168       }
169    }
170 }
171
172
173 /* Store Messages Destination information */
174 void store_msgs(LEX *lc, struct res_items *item, int index, int pass)
175 {
176    int token;
177    char *cmd;
178    POOLMEM *dest;
179    int dest_len;
180
181    Dmsg2(200, "store_msgs pass=%d code=%d\n", pass, item->code);
182    if (pass == 1) {
183       switch (item->code) {
184          case MD_STDOUT:
185          case MD_STDERR:
186          case MD_SYSLOG:              /* syslog */
187          case MD_CONSOLE:
188             scan_types(lc, (MSGS *)(item->value), item->code, NULL, NULL);
189             break;
190          case MD_OPERATOR:            /* send to operator */
191          case MD_DIRECTOR:            /* send to Director */
192          case MD_MAIL:                /* mail */
193          case MD_MAIL_ON_ERROR:       /* mail if Job errors */
194             if (item->code == MD_OPERATOR) {
195                cmd = res_all.res_msgs.operator_cmd;
196             } else {
197                cmd = res_all.res_msgs.mail_cmd;
198             }
199             dest = get_pool_memory(PM_MESSAGE);
200             dest_len = 0;
201             dest[0] = 0;
202             /* Pick up comma separated list of destinations */
203             for ( ;; ) {
204                token = lex_get_token(lc);    /* scan destination */
205                if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
206                   scan_err1(lc, "expected a message destination, got: %s", lc->str);
207                }
208                dest = (char *) check_pool_memory_size(dest, dest_len + lc->str_len + 2);
209                if (dest[0] != 0) {
210                   strcat(dest, " ");  /* separate multiple destinations with space */
211                   dest_len++;
212                }
213                strcat(dest, lc->str);
214                dest_len += lc->str_len;
215                Dmsg2(100, "store_msgs newdest=%s: dest=%s:\n", lc->str, dest);
216                token = lex_get_token(lc);
217                if (token == T_COMMA) { 
218                   continue;           /* get another destination */
219                }
220                if (token != T_EQUALS) {
221                   scan_err1(lc, "expected an =, got: %s", lc->str); 
222                }
223                break;
224             }
225             Dmsg1(200, "mail_cmd=%s\n", cmd);
226             scan_types(lc, (MSGS *)(item->value), item->code, dest, cmd);
227             free_pool_memory(dest);
228             Dmsg0(200, "done with dest codes\n");
229             break;
230          case MD_FILE:                /* file */
231          case MD_APPEND:              /* append */
232             dest = get_pool_memory(PM_MESSAGE);
233             /* Pick up a single destination */
234             token = lex_get_token(lc);    /* scan destination */
235             if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
236                scan_err1(lc, "expected a message destination, got: %s", lc->str);
237             }
238             dest = check_pool_memory_size(dest, dest_len + lc->str_len + 2);
239             strcpy(dest, lc->str);
240             dest_len = lc->str_len;
241             token = lex_get_token(lc);
242             Dmsg1(200, "store_msgs dest=%s:\n", dest);
243             if (token != T_EQUALS) {
244                scan_err1(lc, "expected an =, got: %s", lc->str); 
245             }
246             scan_types(lc, (MSGS *)(item->value), item->code, dest, NULL);
247             free_pool_memory(dest);
248             Dmsg0(200, "done with dest codes\n");
249             break;
250
251          default:
252             scan_err1(lc, "Unknown item code: %d\n", item->code);
253             break;
254       }
255    }
256    scan_to_eol(lc);
257    set_bit(index, res_all.hdr.item_present);
258    Dmsg0(200, "Done store_msgs\n");
259 }
260
261 /* 
262  * Scan for message types and add them to the message
263  * destination. The basic job here is to connect message types
264  *  (WARNING, ERROR, FATAL, INFO, ...) with an appropriate
265  *  destination (MAIL, FILE, OPERATOR, ...)
266  */
267 static void scan_types(LEX *lc, MSGS *msg, int dest_code, char *where, char *cmd)
268 {
269    int token, i, found, quit, is_not;
270    int msg_type;
271    char *str;
272
273    for (quit=0; !quit;) {
274       token = lex_get_token(lc);             /* expect at least one type */       
275       if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
276          scan_err1(lc, "expected a message type, got: %s", lc->str);
277       }
278       found = FALSE;
279       if (lc->str[0] == '!') {
280          is_not = TRUE;
281          str = &lc->str[1];
282       } else {
283          is_not = FALSE;
284          str = &lc->str[0];
285       }
286       for (i=0; msg_types[i].name; i++) {
287          if (strcmp(str, msg_types[i].name) == 0) {
288             msg_type = msg_types[i].token;
289             found = TRUE;
290             break;
291          }
292       }
293       if (!found) {
294          scan_err1(lc, "message type: %s not found", str);
295       }
296
297       if (msg_type == M_MAX+1) {         /* all? */
298          for (i=1; i<=M_MAX; i++) {      /* yes set all types */
299             add_msg_dest(msg, dest_code, i, where, cmd);
300          }
301       } else {
302          if (is_not) {
303             rem_msg_dest(msg, dest_code, msg_type, where);
304          } else {
305             add_msg_dest(msg, dest_code, msg_type, where, cmd);
306          }
307       }
308       if (lc->ch != ',') {
309          break;
310       }
311       Dmsg0(200, "call lex_get_token() to eat comma\n");
312       token = lex_get_token(lc);          /* eat comma */
313    }
314    Dmsg0(200, "Done scan_types()\n");
315 }
316
317
318 /* 
319  * This routine is ONLY for resource names
320  *  Store a name at specified address.
321  */
322 void store_name(LEX *lc, struct res_items *item, int index, int pass)
323 {
324    int token;
325
326    token = lex_get_token(lc);
327    if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
328       scan_err1(lc, "expected an identifier or string, got: %s", lc->str);
329    } else if (lc->str_len > MAX_RES_NAME_LENGTH) {
330       scan_err3(lc, "name %s length %d too long, max is %d\n", lc->str, 
331          lc->str_len, MAX_RES_NAME_LENGTH);
332    } else {
333       /* Store the name both pass 1 and pass 2 */
334       *(item->value) = bstrdup(lc->str);
335    }
336    scan_to_eol(lc);
337    set_bit(index, res_all.hdr.item_present);
338 }
339
340
341 /*
342  * Store a name string at specified address
343  * A name string is limited to MAX_RES_NAME_LENGTH
344  */
345 void store_strname(LEX *lc, struct res_items *item, int index, int pass)
346 {
347    int token;
348
349    token = lex_get_token(lc);
350    if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
351       scan_err1(lc, "expected an identifier or string, got: %s", lc->str);
352    } else if (lc->str_len > MAX_RES_NAME_LENGTH) {
353       scan_err3(lc, "name %s length %d too long, max is %d\n", lc->str, 
354          lc->str_len, MAX_RES_NAME_LENGTH);
355    } else {
356       /* Store the name */
357       if (pass == 1)
358          *(item->value) = bstrdup(lc->str);
359    }
360    scan_to_eol(lc);
361    set_bit(index, res_all.hdr.item_present);
362 }
363
364
365
366 /* Store a string at specified address */
367 void store_str(LEX *lc, struct res_items *item, int index, int pass)
368 {
369    int token;
370
371    token = lex_get_token(lc);
372    if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
373       scan_err1(lc, "expected an identifier or string, got: %s", lc->str);
374    } else {
375       if (pass == 1) {
376          *(item->value) = bstrdup(lc->str);
377       }
378    }
379    scan_to_eol(lc);
380    set_bit(index, res_all.hdr.item_present);
381 }
382
383 /* Store a directory name at specified address */
384 void store_dir(LEX *lc, struct res_items *item, int index, int pass)
385 {
386    int token;
387
388    token = lex_get_token(lc);
389    if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
390       scan_err1(lc, "expected an identifier or string, got: %s", lc->str);
391    } else {
392       if (pass == 1) {
393          do_shell_expansion(lc->str);
394          *(item->value) = bstrdup(lc->str);
395       }
396    }
397    scan_to_eol(lc);
398    set_bit(index, res_all.hdr.item_present);
399 }
400
401
402 /* Store a password specified address in MD5 coding */
403 void store_password(LEX *lc, struct res_items *item, int index, int pass)
404 {
405    unsigned int token, i, j;
406    struct MD5Context md5c;
407    unsigned char signature[16];
408    char sig[100];
409
410
411    token = lex_get_token(lc);
412    if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
413       scan_err1(lc, "expected an identifier or string, got: %s\n", lc->str);
414    } else {
415       if (pass == 1) {
416          MD5Init(&md5c);
417          MD5Update(&md5c, (unsigned char *) (lc->str), lc->str_len);
418          MD5Final(signature, &md5c);
419          for (i = j = 0; i < sizeof(signature); i++) {
420             sprintf(&sig[j], "%02x", signature[i]); 
421             j += 2;
422          }
423          *(item->value) = bstrdup(sig);
424       }
425    }
426    scan_to_eol(lc);
427    set_bit(index, res_all.hdr.item_present);
428 }
429
430
431 /* Store a resource at specified address.
432  * If we are in pass 2, do a lookup of the 
433  * resource.
434  */
435 void store_res(LEX *lc, struct res_items *item, int index, int pass)
436 {
437    int token;
438    RES *res;
439
440    token = lex_get_token(lc);
441    if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
442       scan_err1(lc, "expected a Resource name, got: %s", lc->str);
443    }
444    if (pass == 2) {
445      res = GetResWithName(item->code, lc->str);
446      if (res == NULL) {
447         scan_err3(lc, "Could not find Resource %s referenced on line %d : %s\n", 
448            lc->str, lc->line_no, lc->line);
449      }
450      *(item->value) = (char *)res;
451    }
452    scan_to_eol(lc);
453    set_bit(index, res_all.hdr.item_present);
454 }
455
456
457 /* Store an integer at specified address */
458 void store_int(LEX *lc, struct res_items *item, int index, int pass)
459 {
460    int token;
461
462    token = lex_get_token(lc);
463    if (token != T_NUMBER || !is_a_number(lc->str)) {
464       scan_err1(lc, "expected an integer number, got: %s", lc->str);
465    } else {
466       errno = 0;
467       *(int *)(item->value) = (int)strtod(lc->str, NULL);
468       if (errno != 0) {
469          scan_err1(lc, "expected an integer number, got: %s", lc->str);
470       }
471    }
472    scan_to_eol(lc);
473    set_bit(index, res_all.hdr.item_present);
474 }
475
476 /* Store a positive integer at specified address */
477 void store_pint(LEX *lc, struct res_items *item, int index, int pass)
478 {
479    int token;
480
481    token = lex_get_token(lc);
482    if (token != T_NUMBER || !is_a_number(lc->str)) {
483       scan_err1(lc, "expected a positive integer number, got: %s", lc->str);
484    } else {
485       errno = 0;
486       token = (int)strtod(lc->str, NULL);
487       if (errno != 0 || token < 0) {
488          scan_err1(lc, "expected a postive integer number, got: %s", lc->str);
489       }
490       *(int *)(item->value) = token;
491    }
492    scan_to_eol(lc);
493    set_bit(index, res_all.hdr.item_present);
494 }
495
496
497 /* Store an 64 bit integer at specified address */
498 void store_int64(LEX *lc, struct res_items *item, int index, int pass)
499 {
500    int token;
501
502    token = lex_get_token(lc);
503    Dmsg2(400, "int64=:%s: %f\n", lc->str, strtod(lc->str, NULL)); 
504    if (token != T_NUMBER || !is_a_number(lc->str)) {
505       scan_err1(lc, "expected an integer number, got: %s", lc->str);
506    } else {
507       errno = 0;
508       *(int64_t *)(item->value) = (int64_t)strtod(lc->str, NULL);
509       if (errno != 0)
510          scan_err1(lc, "expected an integer number, got: %s", lc->str);
511    }
512    scan_to_eol(lc);
513    set_bit(index, res_all.hdr.item_present);
514 }
515
516 /* Store a size in bytes */
517 void store_size(LEX *lc, struct res_items *item, int index, int pass)
518 {
519    int token, i, ch;
520    uint64_t value;
521    int mod[]  = {'*', 'k', 'm', 'g', 0}; /* first item * not used */
522    uint64_t mult[] = {1,             /* byte */
523                       1024,          /* kilobyte */
524                       1048576,       /* megabyte */
525                       1073741824};   /* gigabyte */
526
527 #ifdef we_have_a_compiler_that_works
528    int mod[]  = {'*', 'k', 'm', 'g', 't', 0};
529    uint64_t mult[] = {1,             /* byte */
530                       1024,          /* kilobyte */
531                       1048576,       /* megabyte */
532                       1073741824,    /* gigabyte */
533                       1099511627776};/* terabyte */
534 #endif
535
536    Dmsg0(400, "Enter store_size\n");
537    token = lex_get_token(lc);
538    errno = 0;
539    switch (token) {
540    case T_NUMBER:
541       Dmsg2(400, "size num=:%s: %f\n", lc->str, strtod(lc->str, NULL)); 
542       value = (uint64_t)strtod(lc->str, NULL);
543       if (errno != 0 || token < 0) {
544          scan_err1(lc, "expected a size number, got: %s", lc->str);
545       }
546       *(uint64_t *)(item->value) = value;
547       break;
548    case T_IDENTIFIER:
549    case T_STRING:
550       /* Look for modifier */
551       ch = lc->str[lc->str_len - 1];
552       i = 0;
553       if (ISALPHA(ch)) {
554          if (ISUPPER(ch)) {
555             ch = tolower(ch);
556          }
557          while (mod[++i] != 0) {
558             if (ch == mod[i]) {
559                lc->str_len--;
560                lc->str[lc->str_len] = 0; /* strip modifier */
561                break;
562             }
563          }
564       }
565       if (mod[i] == 0 || !is_a_number(lc->str)) {
566          scan_err1(lc, "expected a size number, got: %s", lc->str);
567       }
568       Dmsg3(400, "size str=:%s: %f i=%d\n", lc->str, strtod(lc->str, NULL), i);
569
570       value = (uint64_t)strtod(lc->str, NULL);
571       Dmsg1(400, "Int value = %d\n", (int)value);
572       if (errno != 0 || value < 0) {
573          scan_err1(lc, "expected a size number, got: %s", lc->str);
574       }
575       *(uint64_t *)(item->value) = value * mult[i];
576       Dmsg2(400, "Full value = %f %" lld "\n", strtod(lc->str, NULL) * mult[i],
577           value *mult[i]);
578       break;
579    default:
580       scan_err1(lc, "expected a size, got: %s", lc->str);
581       break;
582    }
583    scan_to_eol(lc);
584    set_bit(index, res_all.hdr.item_present);
585    Dmsg0(400, "Leave store_size\n");
586 }
587
588
589 /* Store a time period in seconds */
590 void store_time(LEX *lc, struct res_items *item, int index, int pass)
591 {
592    int token; 
593    btime_t value;
594
595    token = lex_get_token(lc);
596    errno = 0;
597    switch (token) {
598    case T_NUMBER:
599       value = (btime_t)strtod(lc->str, NULL);
600       if (errno != 0 || value < 0) {
601          scan_err1(lc, "expected a time period, got: %s", lc->str);
602       }
603       *(btime_t *)(item->value) = value;
604       break;
605    case T_IDENTIFIER:
606    case T_STRING:
607       if (!string_to_btime(lc->str, &value)) {
608          scan_err1(lc, "expected a time period, got: %s", lc->str);
609       }
610       *(btime_t *)(item->value) = value;
611       break;
612    default:
613       scan_err1(lc, "expected a time period, got: %s", lc->str);
614       break;
615    }
616    scan_to_eol(lc);
617    set_bit(index, res_all.hdr.item_present);
618 }
619
620
621 /* Store a yes/no in a bit field */
622 void store_yesno(LEX *lc, struct res_items *item, int index, int pass)
623 {
624    int token;
625
626    token = lex_get_token(lc);
627    lcase(lc->str);
628    if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
629       scan_err1(lc, "expected an identifier or string, got: %s", lc->str);
630    } else if (strcmp(lc->str, "yes") == 0) {
631       *(int *)(item->value) |= item->code;
632    } else if (strcmp(lc->str, "no") == 0) {
633       *(int *)(item->value) &= ~(item->code);
634    } else {
635       scan_err1(lc, "Expect a YES or NO, got: %s", lc->str);
636    }
637    scan_to_eol(lc);
638    set_bit(index, res_all.hdr.item_present);
639 }
640
641
642 /*
643  * Scan to "logical" end of line. I.e. end of line,
644  * or semicolon.
645  */
646 void scan_to_eol(LEX *lc)
647 {
648    int token;
649    Dmsg0(150, "start scan to eof\n");
650    while ((token = lex_get_token(lc)) != T_EOL) {
651    }
652    Dmsg0(150, "done scan to eof\n");
653 }
654
655    
656 /*
657  * Format a scanner error message 
658  */
659 void s_err(char *file, int line, LEX *lc, char *msg, ...)
660 {
661    va_list arg_ptr;
662    char buf[MAXSTRING];
663
664    va_start(arg_ptr, msg);
665    bvsnprintf(buf, sizeof(buf), msg, arg_ptr);
666    va_end(arg_ptr);
667      
668    e_msg(file, line, M_ABORT, 0, "Config error: %s,\n\
669             : Line %d, col %d of file %s\n%s\n",
670       buf, lc->line_no, lc->col_no, lc->fname, lc->line);
671 }
672
673 void LockRes()
674 {
675    P(res_mutex);
676    res_locked = 1;
677 }
678
679 void UnlockRes()
680 {
681    res_locked = 0;
682    V(res_mutex);
683 }
684
685 /*
686  * Return resource of type rcode that matches name
687  */
688 RES *
689 GetResWithName(int rcode, char *name)
690 {
691    RES *res;
692    int rindex = rcode - r_first;
693
694    LockRes();
695    res = resources[rindex].res_head;
696    while (res) {
697       if (strcmp(res->name, name) == 0) {
698          break;
699       }
700       res = res->next;
701    }
702    UnlockRes();
703    return res;
704    
705 }
706
707 /*
708  * Return next resource of type rcode. On first
709  * call second arg (res) is NULL, on subsequent
710  * calls, it is called with previous value.
711  */
712 RES *
713 GetNextRes(int rcode, RES *res)
714 {
715    RES *nres;
716    int rindex = rcode - r_first;
717        
718
719    if (!res_locked) {
720       Emsg0(M_ABORT, 0, "Resource chain not locked.\n");
721    }
722    if (res == NULL) {
723       nres = resources[rindex].res_head;
724    } else {
725       nres = res->next;
726    }
727    return nres;
728 }
729
730
731 /* Parser state */
732 enum parse_state {
733    p_none,
734    p_resource
735 };
736
737 /*********************************************************************
738  *
739  *      Parse configuration file
740  *
741  */
742 void 
743 parse_config(char *cf)
744 {
745    LEX *lc = NULL;
746    int token, i, res_type, pass;
747    enum parse_state state = p_none;
748    struct res_items *items;
749    int level = 0;
750
751    /* Make two passes. The first builds the name symbol table,
752     * and the second picks up the items. 
753     */
754    Dmsg0(200, "Enter parse_config()\n");
755    for (pass=1; pass<= 2; pass++) {
756       Dmsg1(200, "parse_config pass %d\n", pass);
757       lc = lex_open_file(lc, cf);
758       while ((token=lex_get_token(lc)) != T_EOF) {
759          Dmsg1(150, "parse got token=%s\n", lex_tok_to_str(token));
760          switch (state) {
761             case p_none:
762                if (token == T_EOL) {
763                   break;
764                }
765                if (token != T_IDENTIFIER) {
766                   scan_err1(lc, "Expected a Resource name identifier, got: %s", lc->str);
767                }
768                lcase(lc->str);
769                for (i=0; resources[i].name; i++)
770                   if (strcmp(resources[i].name, lc->str) == 0) {
771                      state = p_resource;
772                      items = resources[i].items;
773                      res_type = resources[i].rcode;
774                      init_resource(res_type, items);
775                      break;
776                   }
777                if (state == p_none) {
778                   scan_err1(lc, "expected resource name, got: %s", lc->str);
779                }
780                break;
781             case p_resource:
782                switch (token) {
783                   case T_BOB:
784                      level++;
785                      break;
786                   case T_IDENTIFIER:
787                      if (level != 1) {
788                         scan_err1(lc, "not in resource definition: %s", lc->str);
789                      }
790                      lcase(lc->str);
791                      for (i=0; items[i].name; i++) {
792                         if (strcmp(items[i].name, lc->str) == 0) {
793                            token = lex_get_token(lc);
794                            Dmsg1 (150, "in T_IDENT got token=%s\n", lex_tok_to_str(token));
795                            if (token != T_EQUALS) {
796                               scan_err1(lc, "expected an equals, got: %s", lc->str);
797                            }
798                            Dmsg1(150, "calling handler for %s\n", items[i].name);
799                            /* Call item handler */
800                            items[i].handler(lc, &items[i], i, pass);
801                            i = -1;
802                            break;
803                         }
804                      }
805                      if (i >= 0) {
806                         Dmsg2(150, "level=%d id=%s\n", level, lc->str);
807                         Dmsg1(150, "Keyword = %s\n", lc->str);
808                         scan_err1(lc, "Keyword %s not permitted in this resource", lc->str);
809                      }
810                      break;
811
812                   case T_EOB:
813                      level--;
814                      state = p_none;
815                      Dmsg0(150, "T_EOB => define new resource\n");
816                      save_resource(res_type, items, pass);  /* save resource */
817                      break;
818
819                   case T_EOL:
820                      break;
821
822                   default:
823                      scan_err2(lc, "unexpected token %d %s in resource definition",    
824                         token, lex_tok_to_str(token));
825                }
826                break;
827             default:
828                scan_err1(lc, "Unknown parser state %d\n", state);
829          }
830       }
831       if (debug_level > 50 && pass == 2) {
832          int i;
833          for (i=r_first; i<=r_last; i++) {
834             dump_resource(i, resources[i-r_first].res_head, prtmsg, NULL);
835          }
836       }
837       lc = lex_close_file(lc);
838    }
839    Dmsg0(200, "Leave parse_config()\n");
840 }
841
842 /*********************************************************************
843  *
844  *      Free configuration resources
845  *
846  */
847 void 
848 free_config_resources()
849 {
850    int i;
851    for (i=r_first; i<=r_last; i++) {
852       free_resource(i);
853    }
854 }