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