]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/run_conf.c
add conio.c + fix bsendmsg seg fault some pm_strcpy() cleanups
[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_wpos,                            /* 1st, 2nd, ...*/
51 };  
52
53 struct s_keyw {
54   char *name;                         /* keyword */
55   enum e_state state;                 /* parser state */
56   int code;                           /* state value */
57 };
58
59 /* Keywords understood by parser */
60 static struct s_keyw keyw[] = {
61   {N_("on"),         s_none,    0},
62   {N_("at"),         s_at,      0},
63
64   {N_("sun"),        s_wday,    0},
65   {N_("mon"),        s_wday,    1},
66   {N_("tue"),        s_wday,    2},
67   {N_("wed"),        s_wday,    3},
68   {N_("thu"),        s_wday,    4},
69   {N_("fri"),        s_wday,    5},
70   {N_("sat"),        s_wday,    6},
71   {N_("jan"),        s_month,   0},
72   {N_("feb"),        s_month,   1},
73   {N_("mar"),        s_month,   2},
74   {N_("apr"),        s_month,   3},
75   {N_("may"),        s_month,   4},
76   {N_("jun"),        s_month,   5},
77   {N_("jul"),        s_month,   6},
78   {N_("aug"),        s_month,   7},
79   {N_("sep"),        s_month,   8},
80   {N_("oct"),        s_month,   9},
81   {N_("nov"),        s_month,  10},
82   {N_("dec"),        s_month,  11},
83
84   {N_("sunday"),     s_wday,    0},
85   {N_("monday"),     s_wday,    1},
86   {N_("tuesday"),    s_wday,    2},
87   {N_("wednesday"),  s_wday,    3},
88   {N_("thursday"),   s_wday,    4},
89   {N_("friday"),     s_wday,    5},
90   {N_("saturday"),   s_wday,    6},
91   {N_("january"),    s_month,   0},
92   {N_("february"),   s_month,   1},
93   {N_("march"),      s_month,   2},
94   {N_("april"),      s_month,   3},
95   {N_("june"),       s_month,   5},
96   {N_("july"),       s_month,   6},
97   {N_("august"),     s_month,   7},
98   {N_("september"),  s_month,   8},
99   {N_("october"),    s_month,   9},
100   {N_("november"),   s_month,  10},
101   {N_("december"),   s_month,  11},
102
103   {N_("daily"),      s_daily,   0},
104   {N_("weekly"),     s_weekly,  0},
105   {N_("monthly"),    s_monthly, 0},
106   {N_("hourly"),     s_hourly,  0},
107
108   {N_("1st"),        s_wpos,    0},
109   {N_("2nd"),        s_wpos,    1},
110   {N_("3rd"),        s_wpos,    2},
111   {N_("4th"),        s_wpos,    3},
112   {N_("5th"),        s_wpos,    4},
113
114   {N_("first"),      s_wpos,    0},
115   {N_("second"),     s_wpos,    1},
116   {N_("third"),      s_wpos,    2},
117   {N_("fourth"),     s_wpos,    3},
118   {N_("fifth"),      s_wpos,    4},
119   {NULL,         s_none,    0}
120 };
121
122 static int have_hour, have_mday, have_wday, have_month, have_wpos;
123 static int have_at;
124 static RUN lrun;
125
126 static void clear_defaults()
127 {
128    have_hour = have_mday = have_wday = have_month = have_wpos = TRUE;
129    clear_bit(0,lrun.hour);
130    clear_bits(0, 30, lrun.mday);
131    clear_bits(0, 6, lrun.wday);
132    clear_bits(0, 11, lrun.month);
133    clear_bits(0, 4, lrun.wpos);
134 }
135
136 static void set_defaults()
137 {
138    have_hour = have_mday = have_wday = have_month = have_wpos = FALSE;
139    have_at = FALSE;
140    set_bit(0,lrun.hour);
141    set_bits(0, 30, lrun.mday);
142    set_bits(0, 6, lrun.wday);
143    set_bits(0, 11, lrun.month);
144    set_bits(0, 4, lrun.wpos);
145 }
146
147
148 /* Keywords (RHS) permitted in Run records */
149 static struct s_kw RunFields[] = {
150    {"pool",     'P'},
151    {"level",    'L'},
152    {"storage",  'S'},
153    {"messages", 'M'},
154    {"priority", 'p'},
155    {NULL,        0}
156 };
157
158 /* 
159  * Store Schedule Run information   
160  * 
161  * Parse Run statement:
162  *
163  *  Run <keyword=value ...> [on] 2 january at 23:45
164  *
165  *   Default Run time is daily at 0:0
166  *  
167  *   There can be multiple run statements, they are simply chained
168  *   together.
169  *
170  */
171 void store_run(LEX *lc, struct res_items *item, int index, int pass)
172 {
173    int i, j, found;
174    int token, state, state2 = 0, code = 0, code2 = 0;
175    int options = lc->options;
176    RUN **run = (RUN **)(item->value);   
177    RUN *trun;
178    char *p;
179    RES *res;
180
181
182    lc->options |= LOPT_NO_IDENT;      /* want only "strings" */
183
184    /* clear local copy of run record */
185    memset(&lrun, 0, sizeof(RUN));
186
187    /* scan for Job level "full", "incremental", ... */
188    for (found=TRUE; found; ) {
189       found = FALSE;
190       token = lex_get_token(lc, T_NAME);
191       for (i=0; RunFields[i].name; i++) {
192          if (strcasecmp(lc->str, RunFields[i].name) == 0) {
193             found = TRUE;
194             if (lex_get_token(lc, T_ALL) != T_EQUALS) {
195                scan_err1(lc, "Expected an equals, got: %s", lc->str);
196                /* NOT REACHED */ 
197             }
198             switch (RunFields[i].token) {
199             case 'L':                 /* level */
200                token = lex_get_token(lc, T_NAME);
201                for (j=0; joblevels[j].level_name; j++) {
202                   if (strcasecmp(lc->str, joblevels[j].level_name) == 0) {
203                      lrun.level = joblevels[j].level;
204                      lrun.job_type = joblevels[j].job_type;
205                      j = 0;
206                      break;
207                   }
208                }
209                if (j != 0) {
210                   scan_err1(lc, _("Job level field: %s not found in run record"), lc->str);
211                   /* NOT REACHED */
212                }
213                break;
214             case 'p':                 /* Priority */
215                token = lex_get_token(lc, T_PINT32);
216                if (pass == 2) {
217                   lrun.Priority = lc->pint32_val;
218                }
219                break;
220             case 'P':                 /* Pool */
221                token = lex_get_token(lc, T_NAME);
222                if (pass == 2) {
223                   res = GetResWithName(R_POOL, lc->str);
224                   if (res == NULL) {
225                      scan_err1(lc, "Could not find specified Pool Resource: %s",
226                                 lc->str);
227                      /* NOT REACHED */
228                   }
229                   lrun.pool = (POOL *)res;
230                }
231                break;
232             case 'S':                 /* storage */
233                token = lex_get_token(lc, T_NAME);
234                if (pass == 2) {
235                   res = GetResWithName(R_STORAGE, lc->str);
236                   if (res == NULL) {
237                      scan_err1(lc, "Could not find specified Storage Resource: %s",
238                                 lc->str);
239                      /* NOT REACHED */
240                   }
241                   lrun.storage = (STORE *)res;
242                }
243                break;
244             case 'M':                 /* messages */
245                token = lex_get_token(lc, T_NAME);
246                if (pass == 2) {
247                   res = GetResWithName(R_MSGS, lc->str);
248                   if (res == NULL) {
249                      scan_err1(lc, "Could not find specified Messages Resource: %s",
250                                 lc->str);
251                      /* NOT REACHED */
252                   }
253                   lrun.msgs = (MSGS *)res;
254                }
255                break;
256             default:
257                scan_err1(lc, "Expected a keyword name, got: %s", lc->str);
258                /* NOT REACHED */
259                break;
260             } /* end switch */     
261          } /* end if strcasecmp */
262       } /* end for RunFields */
263
264       /* At this point, it is not a keyword. Check for old syle
265        * Job Levels without keyword. This form is depreciated!!!
266        */
267       for (j=0; joblevels[j].level_name; j++) {
268          if (strcasecmp(lc->str, joblevels[j].level_name) == 0) {
269             lrun.level = joblevels[j].level;
270             lrun.job_type = joblevels[j].job_type;
271             found = TRUE;
272             break;
273          }
274       }
275    } /* end for found */
276
277
278    /*
279     * Scan schedule times.
280     * Default is: daily at 0:0
281     */
282    state = s_none;
283    set_defaults();
284
285    for ( ; token != T_EOL; (token = lex_get_token(lc, T_ALL))) {
286       int len, pm = 0;
287       switch (token) {
288       case T_NUMBER:
289          state = s_mday;
290          code = atoi(lc->str) - 1;
291          if (code < 0 || code > 30) {
292             scan_err0(lc, _("Day number out of range (1-31)"));
293          }
294          break;
295       case T_NAME:                 /* this handles drop through from keyword */
296       case T_UNQUOTED_STRING:
297          if (strchr(lc->str, (int)'-')) {
298             state = s_range;
299             break;
300          }
301          if (strchr(lc->str, (int)':')) {
302             state = s_time;
303             break;
304          }
305          /* everything else must be a keyword */
306          for (i=0; keyw[i].name; i++) {
307             if (strcasecmp(lc->str, keyw[i].name) == 0) {
308                state = keyw[i].state;
309                code   = keyw[i].code;
310                i = 0;
311                break;
312             }
313          }
314          if (i != 0) {
315             scan_err1(lc, _("Job type field: %s in run record not found"), lc->str);
316             /* NOT REACHED */
317          }
318          break;
319       case T_COMMA:
320          continue;
321       default:
322          scan_err2(lc, _("Unexpected token: %d:%s"), token, lc->str);
323          /* NOT REACHED */
324          break;
325       }
326       switch (state) {
327       case s_none:
328          continue;
329       case s_mday:                 /* day of month */
330          if (!have_mday) {
331             clear_bits(0, 30, lrun.mday);
332             clear_bits(0, 6, lrun.wday);
333             have_mday = TRUE;
334          }
335          set_bit(code, lrun.mday);
336          break;
337       case s_month:                /* month of year */
338          if (!have_month) {
339             clear_bits(0, 11, lrun.month);
340             have_month = TRUE;
341          }
342          set_bit(code, lrun.month);
343          break;
344       case s_wday:                 /* week day */
345          if (!have_wday) {
346             clear_bits(0, 6, lrun.wday);
347             clear_bits(0, 30, lrun.mday);
348             have_wday = TRUE;
349          }
350          set_bit(code, lrun.wday);
351          break;
352       case s_wpos:                 /* Week position 1st, ... */
353          if (!have_wpos) {
354             clear_bits(0, 4, lrun.wpos);
355             have_wpos = TRUE;
356          }
357          set_bit(code, lrun.wpos);
358          break;
359       case s_time:                 /* time */
360          if (!have_at) {
361             scan_err0(lc, _("Time must be preceded by keyword AT."));
362             /* NOT REACHED */
363          }
364          if (!have_hour) {
365             clear_bit(0, lrun.hour);
366          }
367          p = strchr(lc->str, ':');
368          if (!p)  {
369             scan_err0(lc, _("Time logic error.\n"));
370             /* NOT REACHED */
371          }
372          *p++ = 0;                 /* separate two halves */
373          code = atoi(lc->str);
374          len = strlen(p);
375          if (len > 2 && p[len-1] == 'm') {
376             if (p[len-2] == 'a') {
377                pm = 0;
378             } else if (p[len-2] == 'p') {
379                pm = 1;
380             } else {
381                scan_err0(lc, _("Bad time specification."));
382                /* NOT REACHED */
383             }
384          } else {
385             pm = 0;
386          }
387          code2 = atoi(p);
388          if (pm) {
389             code += 12;
390          }
391          if (code < 0 || code > 23 || code2 < 0 || code2 > 59) {
392             scan_err0(lc, _("Bad time specification."));
393             /* NOT REACHED */
394          }
395          set_bit(code, lrun.hour);
396          lrun.minute = code2;
397          have_hour = TRUE;
398          break;
399       case s_at:
400          have_at = TRUE;
401          break;
402       case s_range:
403          p = strchr(lc->str, '-');
404          if (!p) {
405             scan_err0(lc, _("Range logic error.\n"));
406          }
407          *p++ = 0;                 /* separate two halves */
408
409          /* Check for day range */
410          if (is_an_integer(lc->str) && is_an_integer(p)) {
411             code = atoi(lc->str) - 1;
412             code2 = atoi(p) - 1;
413             if (code < 0 || code > 30 || code2 < 0 || code2 > 30) {
414                scan_err0(lc, _("Bad day range specification."));
415             }
416             if (!have_mday) {
417                clear_bits(0, 30, lrun.mday);
418                clear_bits(0, 6, lrun.wday);
419                have_mday = TRUE;
420             }
421             if (code < code2) {
422                set_bits(code, code2, lrun.mday);
423             } else {
424                set_bits(code, 30, lrun.mday);
425                set_bits(0, code2, lrun.mday);
426             }
427             break;
428          }
429
430          /* lookup first half of keyword range (week days or months) */
431          lcase(lc->str);
432          for (i=0; keyw[i].name; i++) {
433             if (strcmp(lc->str, keyw[i].name) == 0) {
434                state = keyw[i].state;
435                code   = keyw[i].code;
436                i = 0;
437                break;
438             }
439          }
440          if (i != 0 || (state != s_month && state != s_wday && state != s_wpos)) {
441             scan_err0(lc, _("Invalid month, week or position day range"));
442             /* NOT REACHED */
443          }
444
445          /* Lookup end of range */
446          lcase(p);
447          for (i=0; keyw[i].name; i++) {
448             if (strcmp(p, keyw[i].name) == 0) {
449                state2  = keyw[i].state;
450                code2   = keyw[i].code;
451                i = 0;
452                break;
453             }
454          }
455          if (i != 0 || state != state2 || code == code2) {
456             scan_err0(lc, _("Invalid month, weekday or position range"));
457             /* NOT REACHED */
458          }
459          if (state == s_wday) {
460             if (!have_wday) {
461                clear_bits(0, 6, lrun.wday);
462                clear_bits(0, 30, lrun.mday);
463                have_wday = TRUE;
464             }
465             if (code < code2) {
466                set_bits(code, code2, lrun.wday);
467             } else {
468                set_bits(code, 6, lrun.wday);
469                set_bits(0, code2, lrun.wday);
470             }
471          } else if (state == s_month) {
472             if (!have_month) {
473                clear_bits(0, 30, lrun.month);
474                have_month = TRUE;
475             }
476             if (code < code2) {
477                set_bits(code, code2, lrun.month);
478             } else {
479                /* this is a bit odd, but we accept it anyway */
480                set_bits(code, 30, lrun.month);
481                set_bits(0, code2, lrun.month);
482             }
483          } else {
484             /* Must be position */
485             if (!have_wpos) {
486                clear_bits(0, 4, lrun.wpos);
487                have_wpos = TRUE;
488             }
489             if (code < code2) {
490                set_bits(code, code2, lrun.wpos);
491             } else {
492                set_bits(code, 4, lrun.wpos);
493                set_bits(0, code2, lrun.wpos);
494             }
495          }                      
496          break;
497       case s_hourly:
498          clear_defaults();
499          set_bits(0, 23, lrun.hour);
500          set_bits(0, 30, lrun.mday);
501          set_bits(0, 11, lrun.month);
502          set_bits(0, 4, lrun.wpos);
503          break;
504       case s_weekly:
505          clear_defaults();
506          set_bit(0, lrun.wday);
507          set_bits(0, 11, lrun.month);
508          set_bits(0, 4, lrun.wpos);
509          break;
510       case s_daily:
511          clear_defaults();
512          set_bits(0, 30, lrun.mday);
513          set_bits(0, 11, lrun.month);
514          set_bits(0, 4,  lrun.wpos);
515          break;
516       case s_monthly:
517          clear_defaults();
518          set_bits(0, 11, lrun.month);
519          set_bits(0, 4,  lrun.wpos);
520          break;
521       default:
522          scan_err0(lc, _("Unexpected run state\n"));
523          /* NOT REACHED */
524          break;
525       }
526    }
527
528    /* Allocate run record, copy new stuff into it,
529     * and link it into the list of run records 
530     * in the schedule resource.
531     */
532    if (pass == 2) {
533       trun = (RUN *)malloc(sizeof(RUN));
534       memcpy(trun, &lrun, sizeof(RUN));
535       if (*run) {
536          trun->next = *run;
537       }
538       *run = trun;
539    }
540
541    lc->options = options;             /* restore scanner options */
542    set_bit(index, res_all.res_sch.hdr.item_present);
543 }