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