]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/run_conf.c
e4a54ae41109f4ccd3a852d1881b6e84136c38b6
[bacula/bacula] / bacula / src / dird / run_conf.c
1 /*
2  *
3  *  Configuration parser for Director Run Configuration
4  *   directives, which are part of the Schedule Resource
5  *
6  *     Kern Sibbald, May MM
7  *
8  *     Version $Id$
9  */
10 /*
11    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
12
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation; either version 2 of
16    the License, or (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21    General Public License for more details.
22
23    You should have received a copy of the GNU General Public
24    License along with this program; if not, write to the Free
25    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26    MA 02111-1307, USA.
27
28  */
29
30 #include "bacula.h"
31 #include "dird.h"
32
33 extern URES res_all;
34 extern struct s_jl joblevels[];
35
36 /* Forward referenced subroutines */
37
38 enum e_state {
39    s_none = 0,
40    s_range,
41    s_mday,
42    s_month,
43    s_time,
44    s_at,
45    s_wday,
46    s_daily,
47    s_weekly,
48    s_monthly,
49    s_hourly,
50    s_wom,                           /* 1st, 2nd, ...*/
51    s_woy,                           /* week of year w00 - w53 */
52 };  
53
54 struct s_keyw {
55   char *name;                         /* keyword */
56   enum e_state state;                 /* parser state */
57   int code;                           /* state value */
58 };
59
60 /* Keywords understood by parser */
61 static struct s_keyw keyw[] = {
62   {N_("on"),         s_none,    0},
63   {N_("at"),         s_at,      0},
64
65   {N_("sun"),        s_wday,    0},
66   {N_("mon"),        s_wday,    1},
67   {N_("tue"),        s_wday,    2},
68   {N_("wed"),        s_wday,    3},
69   {N_("thu"),        s_wday,    4},
70   {N_("fri"),        s_wday,    5},
71   {N_("sat"),        s_wday,    6},
72   {N_("jan"),        s_month,   0},
73   {N_("feb"),        s_month,   1},
74   {N_("mar"),        s_month,   2},
75   {N_("apr"),        s_month,   3},
76   {N_("may"),        s_month,   4},
77   {N_("jun"),        s_month,   5},
78   {N_("jul"),        s_month,   6},
79   {N_("aug"),        s_month,   7},
80   {N_("sep"),        s_month,   8},
81   {N_("oct"),        s_month,   9},
82   {N_("nov"),        s_month,  10},
83   {N_("dec"),        s_month,  11},
84
85   {N_("sunday"),     s_wday,    0},
86   {N_("monday"),     s_wday,    1},
87   {N_("tuesday"),    s_wday,    2},
88   {N_("wednesday"),  s_wday,    3},
89   {N_("thursday"),   s_wday,    4},
90   {N_("friday"),     s_wday,    5},
91   {N_("saturday"),   s_wday,    6},
92   {N_("january"),    s_month,   0},
93   {N_("february"),   s_month,   1},
94   {N_("march"),      s_month,   2},
95   {N_("april"),      s_month,   3},
96   {N_("june"),       s_month,   5},
97   {N_("july"),       s_month,   6},
98   {N_("august"),     s_month,   7},
99   {N_("september"),  s_month,   8},
100   {N_("october"),    s_month,   9},
101   {N_("november"),   s_month,  10},
102   {N_("december"),   s_month,  11},
103
104   {N_("daily"),      s_daily,   0},
105   {N_("weekly"),     s_weekly,  0},
106   {N_("monthly"),    s_monthly, 0},
107   {N_("hourly"),     s_hourly,  0},
108
109   {N_("1st"),        s_wom,     0},
110   {N_("2nd"),        s_wom,     1},
111   {N_("3rd"),        s_wom,     2},
112   {N_("4th"),        s_wom,     3},
113   {N_("5th"),        s_wom,     4},
114
115   {N_("first"),      s_wom,     0},
116   {N_("second"),     s_wom,     1},
117   {N_("third"),      s_wom,     2},
118   {N_("fourth"),     s_wom,     3},
119   {N_("fifth"),      s_wom,     4},
120   {NULL,         s_none,    0}
121 };
122
123 static bool have_hour, have_mday, have_wday, have_month, have_wom;
124 static bool have_at, have_woy;
125 static RUN lrun;
126
127 static void clear_defaults()
128 {
129    have_hour = have_mday = have_wday = have_month = have_wom = have_woy = true;
130    clear_bit(0,lrun.hour);
131    clear_bits(0, 30, lrun.mday);
132    clear_bits(0, 6, lrun.wday);
133    clear_bits(0, 11, lrun.month);
134    clear_bits(0, 4, lrun.wom);
135    clear_bits(0, 53, lrun.woy);
136 }
137
138 static void set_defaults()
139 {
140    have_hour = have_mday = have_wday = have_month = have_wom = have_woy = false;
141    have_at = false;
142    set_bit(0,lrun.hour);
143    set_bits(0, 30, lrun.mday);
144    set_bits(0, 6, lrun.wday);
145    set_bits(0, 11, lrun.month);
146    set_bits(0, 4, lrun.wom);
147    set_bits(0, 53, lrun.woy);
148 }
149
150
151 /* Keywords (RHS) permitted in Run records */
152 static struct s_kw RunFields[] = {
153    {"pool",             'P'},
154    {"fullpool",         'f'},
155    {"incrementalpool",  'i'},
156    {"differentialpool", 'd'},
157    {"level",            'L'},
158    {"storage",          'S'},
159    {"messages",         'M'},
160    {"priority",         'p'},
161    {NULL,                 0}
162 };
163
164 /* 
165  * Store Schedule Run information   
166  * 
167  * Parse Run statement:
168  *
169  *  Run <keyword=value ...> [on] 2 january at 23:45
170  *
171  *   Default Run time is daily at 0:0
172  *  
173  *   There can be multiple run statements, they are simply chained
174  *   together.
175  *
176  */
177 void store_run(LEX *lc, struct res_items *item, int index, int pass)
178 {
179    int i, j;
180    bool found;
181    int token, state, state2 = 0, code = 0, code2 = 0;
182    int options = lc->options;
183    RUN **run = (RUN **)(item->value);   
184    RUN *trun;
185    char *p;
186    RES *res;
187
188
189    lc->options |= LOPT_NO_IDENT;      /* want only "strings" */
190
191    /* clear local copy of run record */
192    memset(&lrun, 0, sizeof(RUN));
193
194    /* scan for Job level "full", "incremental", ... */
195    for (found=true; found; ) {
196       found = false;
197       token = lex_get_token(lc, T_NAME);
198       for (i=0; RunFields[i].name; i++) {
199          if (strcasecmp(lc->str, RunFields[i].name) == 0) {
200             found = true;
201             if (lex_get_token(lc, T_ALL) != T_EQUALS) {
202                scan_err1(lc, "Expected an equals, got: %s", lc->str);
203                /* NOT REACHED */ 
204             }
205             switch (RunFields[i].token) {
206             case 'L':                 /* level */
207                token = lex_get_token(lc, T_NAME);
208                for (j=0; joblevels[j].level_name; j++) {
209                   if (strcasecmp(lc->str, joblevels[j].level_name) == 0) {
210                      lrun.level = joblevels[j].level;
211                      lrun.job_type = joblevels[j].job_type;
212                      j = 0;
213                      break;
214                   }
215                }
216                if (j != 0) {
217                   scan_err1(lc, _("Job level field: %s not found in run record"), lc->str);
218                   /* NOT REACHED */
219                }
220                break;
221             case 'p':                 /* Priority */
222                token = lex_get_token(lc, T_PINT32);
223                if (pass == 2) {
224                   lrun.Priority = lc->pint32_val;
225                }
226                break;
227             case 'P':                 /* Pool */
228             case 'f':                 /* FullPool */
229             case 'i':                 /* IncPool */
230             case 'd':                 /* DifPool */
231                token = lex_get_token(lc, T_NAME);
232                if (pass == 2) {
233                   res = GetResWithName(R_POOL, lc->str);
234                   if (res == NULL) {
235                      scan_err1(lc, "Could not find specified Pool Resource: %s",
236                                 lc->str);
237                      /* NOT REACHED */
238                   }
239                   switch(RunFields[i].token) {
240                   case 'P':
241                      lrun.pool = (POOL *)res;
242                      break;
243                   case 'f':
244                      lrun.full_pool = (POOL *)res;
245                      break;
246                   case 'i':
247                      lrun.inc_pool = (POOL *)res;
248                      break;
249                   case 'd':
250                      lrun.dif_pool = (POOL *)res;
251                      break;
252                   }
253                }
254                break;
255             case 'S':                 /* storage */
256                token = lex_get_token(lc, T_NAME);
257                if (pass == 2) {
258                   res = GetResWithName(R_STORAGE, lc->str);
259                   if (res == NULL) {
260                      scan_err1(lc, "Could not find specified Storage Resource: %s",
261                                 lc->str);
262                      /* NOT REACHED */
263                   }
264                   lrun.storage = (STORE *)res;
265                }
266                break;
267             case 'M':                 /* messages */
268                token = lex_get_token(lc, T_NAME);
269                if (pass == 2) {
270                   res = GetResWithName(R_MSGS, lc->str);
271                   if (res == NULL) {
272                      scan_err1(lc, "Could not find specified Messages Resource: %s",
273                                 lc->str);
274                      /* NOT REACHED */
275                   }
276                   lrun.msgs = (MSGS *)res;
277                }
278                break;
279             default:
280                scan_err1(lc, "Expected a keyword name, got: %s", lc->str);
281                /* NOT REACHED */
282                break;
283             } /* end switch */     
284          } /* end if strcasecmp */
285       } /* end for RunFields */
286
287       /* At this point, it is not a keyword. Check for old syle
288        * Job Levels without keyword. This form is depreciated!!!
289        */
290       for (j=0; joblevels[j].level_name; j++) {
291          if (strcasecmp(lc->str, joblevels[j].level_name) == 0) {
292             lrun.level = joblevels[j].level;
293             lrun.job_type = joblevels[j].job_type;
294             found = true;
295             break;
296          }
297       }
298    } /* end for found */
299
300
301    /*
302     * Scan schedule times.
303     * Default is: daily at 0:0
304     */
305    state = s_none;
306    set_defaults();
307
308    for ( ; token != T_EOL; (token = lex_get_token(lc, T_ALL))) {
309       int len, pm = 0;
310       switch (token) {
311       case T_NUMBER:
312          state = s_mday;
313          code = atoi(lc->str) - 1;
314          if (code < 0 || code > 30) {
315             scan_err0(lc, _("Day number out of range (1-31)"));
316          }
317          break;
318       case T_NAME:                 /* this handles drop through from keyword */
319       case T_UNQUOTED_STRING:
320          if (strchr(lc->str, (int)'-')) {
321             state = s_range;
322             break;
323          }
324          if (strchr(lc->str, (int)':')) {
325             state = s_time;
326             break;
327          }
328          if (lc->str_len == 3 && (lc->str[0] == 'w' || lc->str[0] == 'W') &&
329              is_an_integer(lc->str+1)) {
330             code = atoi(lc->str+1);
331             if (code < 0 || code > 53) {
332                scan_err0(lc, _("Week number out of range (0-53)"));
333             }
334             state = s_woy;            /* week of year */
335             break;
336          }
337          /* everything else must be a keyword */
338          for (i=0; keyw[i].name; i++) {
339             if (strcasecmp(lc->str, keyw[i].name) == 0) {
340                state = keyw[i].state;
341                code   = keyw[i].code;
342                i = 0;
343                break;
344             }
345          }
346          if (i != 0) {
347             scan_err1(lc, _("Job type field: %s in run record not found"), lc->str);
348             /* NOT REACHED */
349          }
350          break;
351       case T_COMMA:
352          continue;
353       default:
354          scan_err2(lc, _("Unexpected token: %d:%s"), token, lc->str);
355          /* NOT REACHED */
356          break;
357       }
358       switch (state) {
359       case s_none:
360          continue;
361       case s_mday:                 /* day of month */
362          if (!have_mday) {
363             clear_bits(0, 30, lrun.mday);
364             clear_bits(0, 6, lrun.wday);
365             have_mday = true;
366          }
367          set_bit(code, lrun.mday);
368          break;
369       case s_month:                /* month of year */
370          if (!have_month) {
371             clear_bits(0, 11, lrun.month);
372             have_month = true;
373          }
374          set_bit(code, lrun.month);
375          break;
376       case s_wday:                 /* week day */
377          if (!have_wday) {
378             clear_bits(0, 6, lrun.wday);
379             clear_bits(0, 30, lrun.mday);
380             have_wday = true;
381          }
382          set_bit(code, lrun.wday);
383          break;
384       case s_wom:                  /* Week of month 1st, ... */
385          if (!have_wom) {
386             clear_bits(0, 4, lrun.wom);
387             have_wom = true;
388          }
389          set_bit(code, lrun.wom);
390          break;
391       case s_woy:
392          if (!have_woy) {
393             clear_bits(0, 53, lrun.woy);
394             have_woy = true;
395          }
396          set_bit(code, lrun.woy);
397          break;
398       case s_time:                 /* time */
399          if (!have_at) {
400             scan_err0(lc, _("Time must be preceded by keyword AT."));
401             /* NOT REACHED */
402          }
403          if (!have_hour) {
404             clear_bit(0, lrun.hour);
405          }
406          p = strchr(lc->str, ':');
407          if (!p)  {
408             scan_err0(lc, _("Time logic error.\n"));
409             /* NOT REACHED */
410          }
411          *p++ = 0;                 /* separate two halves */
412          code = atoi(lc->str);
413          len = strlen(p);
414          if (len > 2 && p[len-1] == 'm') {
415             if (p[len-2] == 'a') {
416                pm = 0;
417             } else if (p[len-2] == 'p') {
418                pm = 1;
419             } else {
420                scan_err0(lc, _("Bad time specification."));
421                /* NOT REACHED */
422             }
423          } else {
424             pm = 0;
425          }
426          code2 = atoi(p);
427          if (pm) {
428             code += 12;
429          }
430          if (code < 0 || code > 23 || code2 < 0 || code2 > 59) {
431             scan_err0(lc, _("Bad time specification."));
432             /* NOT REACHED */
433          }
434          /****FIXME**** convert to UTC */
435          set_bit(code, lrun.hour);
436          lrun.minute = code2;
437          have_hour = true;
438          break;
439       case s_at:
440          have_at = true;
441          break;
442       case s_range:
443          p = strchr(lc->str, '-');
444          if (!p) {
445             scan_err0(lc, _("Range logic error.\n"));
446          }
447          *p++ = 0;                 /* separate two halves */
448
449          /* Check for day range */
450          if (is_an_integer(lc->str) && is_an_integer(p)) {
451             code = atoi(lc->str) - 1;
452             code2 = atoi(p) - 1;
453             if (code < 0 || code > 30 || code2 < 0 || code2 > 30) {
454                scan_err0(lc, _("Bad day range specification."));
455             }
456             if (!have_mday) {
457                clear_bits(0, 30, lrun.mday);
458                clear_bits(0, 6, lrun.wday);
459                have_mday = true;
460             }
461             if (code < code2) {
462                set_bits(code, code2, lrun.mday);
463             } else {
464                set_bits(code, 30, lrun.mday);
465                set_bits(0, code2, lrun.mday);
466             }
467             break;
468          }
469          /* Check for week of year range */
470          if (strlen(lc->str) == 3 && strlen(p) == 3 &&
471              (lc->str[0] == 'w' || lc->str[0] == 'W') &&
472              (p[0] == 'w' || p[0] == 'W') &&
473              is_an_integer(lc->str+1) && is_an_integer(p+1)) {
474             code = atoi(lc->str+1);
475             code2 = atoi(p+1);
476             if (code < 0 || code > 53 || code2 < 0 || code2 > 53) {
477                scan_err0(lc, _("Week number out of range (0-53)"));
478             }
479             if (!have_woy) {
480                clear_bits(0, 53, lrun.woy);
481                have_woy = true;
482             }
483             if (code < code2) {
484                set_bits(code, code2, lrun.woy);
485             } else {
486                set_bits(code, 53, lrun.woy);
487                set_bits(0, code2, lrun.woy);
488             }
489             break;
490          }
491          /* lookup first half of keyword range (week days or months) */
492          lcase(lc->str);
493          for (i=0; keyw[i].name; i++) {
494             if (strcmp(lc->str, keyw[i].name) == 0) {
495                state = keyw[i].state;
496                code   = keyw[i].code;
497                i = 0;
498                break;
499             }
500          }
501          if (i != 0 || (state != s_month && state != s_wday && state != s_wom)) {
502             scan_err0(lc, _("Invalid month, week or position day range"));
503             /* NOT REACHED */
504          }
505
506          /* Lookup end of range */
507          lcase(p);
508          for (i=0; keyw[i].name; i++) {
509             if (strcmp(p, keyw[i].name) == 0) {
510                state2  = keyw[i].state;
511                code2   = keyw[i].code;
512                i = 0;
513                break;
514             }
515          }
516          if (i != 0 || state != state2 || code == code2) {
517             scan_err0(lc, _("Invalid month, weekday or position range"));
518             /* NOT REACHED */
519          }
520          if (state == s_wday) {
521             if (!have_wday) {
522                clear_bits(0, 6, lrun.wday);
523                clear_bits(0, 30, lrun.mday);
524                have_wday = true;
525             }
526             if (code < code2) {
527                set_bits(code, code2, lrun.wday);
528             } else {
529                set_bits(code, 6, lrun.wday);
530                set_bits(0, code2, lrun.wday);
531             }
532          } else if (state == s_month) {
533             if (!have_month) {
534                clear_bits(0, 30, lrun.month);
535                have_month = true;
536             }
537             if (code < code2) {
538                set_bits(code, code2, lrun.month);
539             } else {
540                /* this is a bit odd, but we accept it anyway */
541                set_bits(code, 30, lrun.month);
542                set_bits(0, code2, lrun.month);
543             }
544          } else {
545             /* Must be position */
546             if (!have_wom) {
547                clear_bits(0, 4, lrun.wom);
548                have_wom = true;
549             }
550             if (code < code2) {
551                set_bits(code, code2, lrun.wom);
552             } else {
553                set_bits(code, 4, lrun.wom);
554                set_bits(0, code2, lrun.wom);
555             }
556          }                      
557          break;
558       case s_hourly:
559          clear_defaults();
560          set_bits(0, 23, lrun.hour);
561          set_bits(0, 30, lrun.mday);
562          set_bits(0, 11, lrun.month);
563          set_bits(0, 4, lrun.wom);
564          set_bits(0, 53, lrun.woy);
565          break;
566       case s_weekly:
567          clear_defaults();
568          set_bit(0, lrun.wday);
569          set_bits(0, 11, lrun.month);
570          set_bits(0, 4, lrun.wom);
571          set_bits(0, 53, lrun.woy);
572          break;
573       case s_daily:
574          clear_defaults();
575          set_bits(0, 30, lrun.mday);
576          set_bits(0, 11, lrun.month);
577          set_bits(0, 4,  lrun.wom);
578          set_bits(0, 53, lrun.woy);
579          break;
580       case s_monthly:
581          clear_defaults();
582          set_bits(0, 11, lrun.month);
583          set_bits(0, 4,  lrun.wom);
584          set_bits(0, 53, lrun.woy);
585          break;
586       default:
587          scan_err0(lc, _("Unexpected run state\n"));
588          /* NOT REACHED */
589          break;
590       }
591    }
592
593    /* Allocate run record, copy new stuff into it,
594     * and link it into the list of run records 
595     * in the schedule resource.
596     */
597    if (pass == 2) {
598       trun = (RUN *)malloc(sizeof(RUN));
599       memcpy(trun, &lrun, sizeof(RUN));
600       if (*run) {
601          trun->next = *run;
602       }
603       *run = trun;
604    }
605
606    lc->options = options;             /* restore scanner options */
607    set_bit(index, res_all.res_sch.hdr.item_present);
608 }