]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/parse_conf.c
Start of Recycle Code
[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 || !is_a_number(lc->str)) {
461       scan_err1(lc, "expected an integer number, got: %s", lc->str);
462    } else {
463       errno = 0;
464       *(int *)(item->value) = (int)strtod(lc->str, NULL);
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 || !is_a_number(lc->str)) {
480       scan_err1(lc, "expected a positive integer number, got: %s", lc->str);
481    } else {
482       errno = 0;
483       token = (int)strtod(lc->str, NULL);
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    Dmsg2(400, "int64=:%s: %f\n", lc->str, strtod(lc->str, NULL)); 
501    if (token != T_NUMBER || !is_a_number(lc->str)) {
502       scan_err1(lc, "expected an integer number, got: %s", lc->str);
503    } else {
504       errno = 0;
505       *(int64_t *)(item->value) = (int64_t)strtod(lc->str, NULL);
506       if (errno != 0)
507          scan_err1(lc, "expected an integer number, got: %s", lc->str);
508    }
509    scan_to_eol(lc);
510    set_bit(index, res_all.hdr.item_present);
511 }
512
513 /* Store a size in bytes */
514 void store_size(LEX *lc, struct res_items *item, int index, int pass)
515 {
516    int token, i, ch;
517    uint64_t value;
518    int mod[]  = {'*', 'k', 'm', 'g', 0}; /* first item * not used */
519    uint64_t mult[] = {1,             /* byte */
520                       1024,          /* kilobyte */
521                       1048576,       /* megabyte */
522                       1073741824};   /* gigabyte */
523
524 #ifdef we_have_a_compiler_that_works
525    int mod[]  = {'*', 'k', 'm', 'g', 't', 0};
526    uint64_t mult[] = {1,             /* byte */
527                       1024,          /* kilobyte */
528                       1048576,       /* megabyte */
529                       1073741824,    /* gigabyte */
530                       1099511627776};/* terabyte */
531 #endif
532
533    Dmsg0(400, "Enter store_size\n");
534    token = lex_get_token(lc);
535    errno = 0;
536    switch (token) {
537    case T_NUMBER:
538       Dmsg2(400, "size num=:%s: %f\n", lc->str, strtod(lc->str, NULL)); 
539       value = (uint64_t)strtod(lc->str, NULL);
540       if (errno != 0 || token < 0) {
541          scan_err1(lc, "expected a size number, got: %s", lc->str);
542       }
543       *(uint64_t *)(item->value) = value;
544       break;
545    case T_IDENTIFIER:
546    case T_STRING:
547       /* Look for modifier */
548       ch = lc->str[lc->str_len - 1];
549       i = 0;
550       if (ISALPHA(ch)) {
551          if (ISUPPER(ch)) {
552             ch = tolower(ch);
553          }
554          while (mod[++i] != 0) {
555             if (ch == mod[i]) {
556                lc->str_len--;
557                lc->str[lc->str_len] = 0; /* strip modifier */
558                break;
559             }
560          }
561       }
562       if (mod[i] == 0 || !is_a_number(lc->str)) {
563          scan_err1(lc, "expected a size number, got: %s", lc->str);
564       }
565       Dmsg3(400, "size str=:%s: %f i=%d\n", lc->str, strtod(lc->str, NULL), i);
566
567       value = (uint64_t)strtod(lc->str, NULL);
568       Dmsg1(400, "Int value = %d\n", (int)value);
569       if (errno != 0 || value < 0) {
570          scan_err1(lc, "expected a size number, got: %s", lc->str);
571       }
572       *(uint64_t *)(item->value) = value * mult[i];
573       Dmsg2(400, "Full value = %f %" lld "\n", strtod(lc->str, NULL) * mult[i],
574           value *mult[i]);
575       break;
576    default:
577       scan_err1(lc, "expected a size, got: %s", lc->str);
578       break;
579    }
580    scan_to_eol(lc);
581    set_bit(index, res_all.hdr.item_present);
582    Dmsg0(400, "Leave store_size\n");
583 }
584
585
586 /* Store a time period in seconds */
587 void store_time(LEX *lc, struct res_items *item, int index, int pass)
588 {
589    int token, i, ch, value;
590    int  mod[]  = {'*', 's', 'm', 'h', 'd', 'w', 'o', 'q', 'y', 0};
591    int  mult[] = {1, 60, 60*60, 60*60*24, 60*60*24*7, 60*60*24*30, 
592                   60*60*24*91, 60*60*24*365};
593
594    token = lex_get_token(lc);
595    errno = 0;
596    switch (token) {
597    case T_NUMBER:
598       token = (int)strtod(lc->str, NULL);
599       if (errno != 0 || token < 0) {
600          scan_err1(lc, "expected a time period, got: %s", lc->str);
601       }
602       *(int *)(item->value) = token;
603       break;
604    case T_IDENTIFIER:
605    case T_STRING:
606       /* Look for modifier */
607       ch = lc->str[lc->str_len - 1];
608       i = 0;
609       if (ISALPHA(ch)) {
610          if (ISUPPER(ch)) {
611             ch = tolower(ch);
612          }
613          while (mod[++i] != 0) {
614             if (ch == mod[i]) {
615                lc->str_len--;
616                lc->str[lc->str_len] = 0; /* strip modifier */
617                break;
618             }
619          }
620       }
621       if (mod[i] == 0 || !is_a_number(lc->str)) {
622          scan_err1(lc, "expected a time period, got: %s", lc->str);
623       }
624       value = (int)strtod(lc->str, NULL);
625       if (errno != 0 || value < 0) {
626          scan_err1(lc, "expected a time period, got: %s", lc->str);
627       }
628       *(int *)(item->value) = value * mult[i];
629       break;
630    default:
631       scan_err1(lc, "expected a time period, got: %s", lc->str);
632       break;
633    }
634    scan_to_eol(lc);
635    set_bit(index, res_all.hdr.item_present);
636 }
637
638
639 /* Store a yes/no in a bit field */
640 void store_yesno(LEX *lc, struct res_items *item, int index, int pass)
641 {
642    int token;
643
644    token = lex_get_token(lc);
645    lcase(lc->str);
646    if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
647       scan_err1(lc, "expected an identifier or string, got: %s", lc->str);
648    } else if (strcmp(lc->str, "yes") == 0) {
649       *(int *)(item->value) |= item->code;
650    } else if (strcmp(lc->str, "no") == 0) {
651       *(int *)(item->value) &= ~(item->code);
652    } else {
653       scan_err1(lc, "Expect a YES or NO, got: %s", lc->str);
654    }
655    scan_to_eol(lc);
656    set_bit(index, res_all.hdr.item_present);
657 }
658
659
660 /*
661  * Scan to "logical" end of line. I.e. end of line,
662  * or semicolon.
663  */
664 void scan_to_eol(LEX *lc)
665 {
666    int token;
667    Dmsg0(150, "start scan to eof\n");
668    while ((token = lex_get_token(lc)) != T_EOL) {
669    }
670    Dmsg0(150, "done scan to eof\n");
671 }
672
673    
674 /*
675  * Format a scanner error message 
676  */
677 void s_err(char *file, int line, LEX *lc, char *msg, ...)
678 {
679    va_list arg_ptr;
680    char buf[MAXSTRING];
681
682    va_start(arg_ptr, msg);
683    bvsnprintf(buf, sizeof(buf), msg, arg_ptr);
684    va_end(arg_ptr);
685      
686    e_msg(file, line, M_ABORT, 0, "Config error: %s,\n\
687             : Line %d, col %d of file %s\n%s\n",
688       buf, lc->line_no, lc->col_no, lc->fname, lc->line);
689 }
690
691 void LockRes()
692 {
693    P(res_mutex);
694    res_locked = 1;
695 }
696
697 void UnlockRes()
698 {
699    res_locked = 0;
700    V(res_mutex);
701 }
702
703 /*
704  * Return resource of type rcode that matches name
705  */
706 RES *
707 GetResWithName(int rcode, char *name)
708 {
709    RES *res;
710    int rindex = rcode - r_first;
711
712    LockRes();
713    res = resources[rindex].res_head;
714    while (res) {
715       if (strcmp(res->name, name) == 0) {
716          break;
717       }
718       res = res->next;
719    }
720    UnlockRes();
721    return res;
722    
723 }
724
725 /*
726  * Return next resource of type rcode. On first
727  * call second arg (res) is NULL, on subsequent
728  * calls, it is called with previous value.
729  */
730 RES *
731 GetNextRes(int rcode, RES *res)
732 {
733    RES *nres;
734    int rindex = rcode - r_first;
735        
736
737    if (!res_locked) {
738       Emsg0(M_ABORT, 0, "Resource chain not locked.\n");
739    }
740    if (res == NULL) {
741       nres = resources[rindex].res_head;
742    } else {
743       nres = res->next;
744    }
745    return nres;
746 }
747
748
749 /* Parser state */
750 enum parse_state {
751    p_none,
752    p_resource
753 };
754
755 /*********************************************************************
756  *
757  *      Parse configuration file
758  *
759  */
760 void 
761 parse_config(char *cf)
762 {
763    LEX *lc = NULL;
764    int token, i, res_type, pass;
765    enum parse_state state = p_none;
766    struct res_items *items;
767    int level = 0;
768
769    /* Make two passes. The first builds the name symbol table,
770     * and the second picks up the items. 
771     */
772    Dmsg0(200, "Enter parse_config()\n");
773    for (pass=1; pass<= 2; pass++) {
774       Dmsg1(200, "parse_config pass %d\n", pass);
775       lc = lex_open_file(lc, cf);
776       while ((token=lex_get_token(lc)) != T_EOF) {
777          Dmsg1(150, "parse got token=%s\n", lex_tok_to_str(token));
778          switch (state) {
779             case p_none:
780                if (token == T_EOL) {
781                   break;
782                }
783                if (token != T_IDENTIFIER) {
784                   scan_err1(lc, "Expected a Resource name identifier, got: %s", lc->str);
785                }
786                lcase(lc->str);
787                for (i=0; resources[i].name; i++)
788                   if (strcmp(resources[i].name, lc->str) == 0) {
789                      state = p_resource;
790                      items = resources[i].items;
791                      res_type = resources[i].rcode;
792                      init_resource(res_type, items);
793                      break;
794                   }
795                if (state == p_none) {
796                   scan_err1(lc, "expected resource name, got: %s", lc->str);
797                }
798                break;
799             case p_resource:
800                switch (token) {
801                   case T_BOB:
802                      level++;
803                      break;
804                   case T_IDENTIFIER:
805                      if (level != 1) {
806                         scan_err1(lc, "not in resource definition: %s", lc->str);
807                      }
808                      lcase(lc->str);
809                      for (i=0; items[i].name; i++) {
810                         if (strcmp(items[i].name, lc->str) == 0) {
811                            token = lex_get_token(lc);
812                            Dmsg1 (150, "in T_IDENT got token=%s\n", lex_tok_to_str(token));
813                            if (token != T_EQUALS) {
814                               scan_err1(lc, "expected an equals, got: %s", lc->str);
815                            }
816                            Dmsg1(150, "calling handler for %s\n", items[i].name);
817                            /* Call item handler */
818                            items[i].handler(lc, &items[i], i, pass);
819                            i = -1;
820                            break;
821                         }
822                      }
823                      if (i >= 0) {
824                         Dmsg2(150, "level=%d id=%s\n", level, lc->str);
825                         Dmsg1(150, "Keyword = %s\n", lc->str);
826                         scan_err1(lc, "Keyword %s not permitted in this resource", lc->str);
827                      }
828                      break;
829
830                   case T_EOB:
831                      level--;
832                      state = p_none;
833                      Dmsg0(150, "T_EOB => define new resource\n");
834                      save_resource(res_type, items, pass);  /* save resource */
835                      break;
836
837                   case T_EOL:
838                      break;
839
840                   default:
841                      scan_err2(lc, "unexpected token %d %s in resource definition",    
842                         token, lex_tok_to_str(token));
843                }
844                break;
845             default:
846                scan_err1(lc, "Unknown parser state %d\n", state);
847          }
848       }
849       if (debug_level > 50 && pass == 2) {
850          int i;
851          for (i=r_first; i<=r_last; i++) {
852             dump_resource(i, resources[i-r_first].res_head, prtmsg, NULL);
853          }
854       }
855       lc = lex_close_file(lc);
856    }
857    Dmsg0(200, "Leave parse_config()\n");
858 }
859
860 /*********************************************************************
861  *
862  *      Free configuration resources
863  *
864  */
865 void 
866 free_config_resources()
867 {
868    int i;
869    for (i=r_first; i<=r_last; i++) {
870       free_resource(i);
871    }
872 }