]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/parse_conf.c
4c2fb5d80ba21c84a662de9d3baedda271093d3d
[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             *(int *)(items[i].value) = items[i].default_value;
156          } else if (items[i].handler == store_int64) {
157             *(int64_t *)(items[i].value) = items[i].default_value;
158          } else if (items[i].handler == store_size ||
159                     items[i].handler == store_time) {
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;
590    btime_t value;
591    int  mod[]  = {'*', 's', 'm', 'h', 'd', 'w', 'o', 'q', 'y', 0};
592    int  mult[] = {1, 60, 60*60, 60*60*24, 60*60*24*7, 60*60*24*30, 
593                   60*60*24*91, 60*60*24*365};
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       /* Look for modifier */
608       ch = lc->str[lc->str_len - 1];
609       i = 0;
610       if (ISALPHA(ch)) {
611          if (ISUPPER(ch)) {
612             ch = tolower(ch);
613          }
614          while (mod[++i] != 0) {
615             if (ch == mod[i]) {
616                lc->str_len--;
617                lc->str[lc->str_len] = 0; /* strip modifier */
618                break;
619             }
620          }
621       }
622       if (mod[i] == 0 || !is_a_number(lc->str)) {
623          scan_err1(lc, "expected a time period, got: %s", lc->str);
624       }
625       value = (btime_t)strtod(lc->str, NULL);
626       if (errno != 0 || value < 0) {
627          scan_err1(lc, "expected a time period, got: %s", lc->str);
628       }
629       *(btime_t *)(item->value) = value * mult[i];
630       break;
631    default:
632       scan_err1(lc, "expected a time period, got: %s", lc->str);
633       break;
634    }
635    scan_to_eol(lc);
636    set_bit(index, res_all.hdr.item_present);
637 }
638
639
640 /* Store a yes/no in a bit field */
641 void store_yesno(LEX *lc, struct res_items *item, int index, int pass)
642 {
643    int token;
644
645    token = lex_get_token(lc);
646    lcase(lc->str);
647    if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
648       scan_err1(lc, "expected an identifier or string, got: %s", lc->str);
649    } else if (strcmp(lc->str, "yes") == 0) {
650       *(int *)(item->value) |= item->code;
651    } else if (strcmp(lc->str, "no") == 0) {
652       *(int *)(item->value) &= ~(item->code);
653    } else {
654       scan_err1(lc, "Expect a YES or NO, got: %s", lc->str);
655    }
656    scan_to_eol(lc);
657    set_bit(index, res_all.hdr.item_present);
658 }
659
660
661 /*
662  * Scan to "logical" end of line. I.e. end of line,
663  * or semicolon.
664  */
665 void scan_to_eol(LEX *lc)
666 {
667    int token;
668    Dmsg0(150, "start scan to eof\n");
669    while ((token = lex_get_token(lc)) != T_EOL) {
670    }
671    Dmsg0(150, "done scan to eof\n");
672 }
673
674    
675 /*
676  * Format a scanner error message 
677  */
678 void s_err(char *file, int line, LEX *lc, char *msg, ...)
679 {
680    va_list arg_ptr;
681    char buf[MAXSTRING];
682
683    va_start(arg_ptr, msg);
684    bvsnprintf(buf, sizeof(buf), msg, arg_ptr);
685    va_end(arg_ptr);
686      
687    e_msg(file, line, M_ABORT, 0, "Config error: %s,\n\
688             : Line %d, col %d of file %s\n%s\n",
689       buf, lc->line_no, lc->col_no, lc->fname, lc->line);
690 }
691
692 void LockRes()
693 {
694    P(res_mutex);
695    res_locked = 1;
696 }
697
698 void UnlockRes()
699 {
700    res_locked = 0;
701    V(res_mutex);
702 }
703
704 /*
705  * Return resource of type rcode that matches name
706  */
707 RES *
708 GetResWithName(int rcode, char *name)
709 {
710    RES *res;
711    int rindex = rcode - r_first;
712
713    LockRes();
714    res = resources[rindex].res_head;
715    while (res) {
716       if (strcmp(res->name, name) == 0) {
717          break;
718       }
719       res = res->next;
720    }
721    UnlockRes();
722    return res;
723    
724 }
725
726 /*
727  * Return next resource of type rcode. On first
728  * call second arg (res) is NULL, on subsequent
729  * calls, it is called with previous value.
730  */
731 RES *
732 GetNextRes(int rcode, RES *res)
733 {
734    RES *nres;
735    int rindex = rcode - r_first;
736        
737
738    if (!res_locked) {
739       Emsg0(M_ABORT, 0, "Resource chain not locked.\n");
740    }
741    if (res == NULL) {
742       nres = resources[rindex].res_head;
743    } else {
744       nres = res->next;
745    }
746    return nres;
747 }
748
749
750 /* Parser state */
751 enum parse_state {
752    p_none,
753    p_resource
754 };
755
756 /*********************************************************************
757  *
758  *      Parse configuration file
759  *
760  */
761 void 
762 parse_config(char *cf)
763 {
764    LEX *lc = NULL;
765    int token, i, res_type, pass;
766    enum parse_state state = p_none;
767    struct res_items *items;
768    int level = 0;
769
770    /* Make two passes. The first builds the name symbol table,
771     * and the second picks up the items. 
772     */
773    Dmsg0(200, "Enter parse_config()\n");
774    for (pass=1; pass<= 2; pass++) {
775       Dmsg1(200, "parse_config pass %d\n", pass);
776       lc = lex_open_file(lc, cf);
777       while ((token=lex_get_token(lc)) != T_EOF) {
778          Dmsg1(150, "parse got token=%s\n", lex_tok_to_str(token));
779          switch (state) {
780             case p_none:
781                if (token == T_EOL) {
782                   break;
783                }
784                if (token != T_IDENTIFIER) {
785                   scan_err1(lc, "Expected a Resource name identifier, got: %s", lc->str);
786                }
787                lcase(lc->str);
788                for (i=0; resources[i].name; i++)
789                   if (strcmp(resources[i].name, lc->str) == 0) {
790                      state = p_resource;
791                      items = resources[i].items;
792                      res_type = resources[i].rcode;
793                      init_resource(res_type, items);
794                      break;
795                   }
796                if (state == p_none) {
797                   scan_err1(lc, "expected resource name, got: %s", lc->str);
798                }
799                break;
800             case p_resource:
801                switch (token) {
802                   case T_BOB:
803                      level++;
804                      break;
805                   case T_IDENTIFIER:
806                      if (level != 1) {
807                         scan_err1(lc, "not in resource definition: %s", lc->str);
808                      }
809                      lcase(lc->str);
810                      for (i=0; items[i].name; i++) {
811                         if (strcmp(items[i].name, lc->str) == 0) {
812                            token = lex_get_token(lc);
813                            Dmsg1 (150, "in T_IDENT got token=%s\n", lex_tok_to_str(token));
814                            if (token != T_EQUALS) {
815                               scan_err1(lc, "expected an equals, got: %s", lc->str);
816                            }
817                            Dmsg1(150, "calling handler for %s\n", items[i].name);
818                            /* Call item handler */
819                            items[i].handler(lc, &items[i], i, pass);
820                            i = -1;
821                            break;
822                         }
823                      }
824                      if (i >= 0) {
825                         Dmsg2(150, "level=%d id=%s\n", level, lc->str);
826                         Dmsg1(150, "Keyword = %s\n", lc->str);
827                         scan_err1(lc, "Keyword %s not permitted in this resource", lc->str);
828                      }
829                      break;
830
831                   case T_EOB:
832                      level--;
833                      state = p_none;
834                      Dmsg0(150, "T_EOB => define new resource\n");
835                      save_resource(res_type, items, pass);  /* save resource */
836                      break;
837
838                   case T_EOL:
839                      break;
840
841                   default:
842                      scan_err2(lc, "unexpected token %d %s in resource definition",    
843                         token, lex_tok_to_str(token));
844                }
845                break;
846             default:
847                scan_err1(lc, "Unknown parser state %d\n", state);
848          }
849       }
850       if (debug_level > 50 && pass == 2) {
851          int i;
852          for (i=r_first; i<=r_last; i++) {
853             dump_resource(i, resources[i-r_first].res_head, prtmsg, NULL);
854          }
855       }
856       lc = lex_close_file(lc);
857    }
858    Dmsg0(200, "Leave parse_config()\n");
859 }
860
861 /*********************************************************************
862  *
863  *      Free configuration resources
864  *
865  */
866 void 
867 free_config_resources()
868 {
869    int i;
870    for (i=r_first; i<=r_last; i++) {
871       free_resource(i);
872    }
873 }