+/* We build RunScripts items here */
+static RUNSCRIPT res_runscript;
+
+/* Store a runscript->when in a bit field */
+static void store_runscript_when(LEX *lc, RES_ITEM *item, int index, int pass)
+{
+ lex_get_token(lc, T_NAME);
+
+ if (strcasecmp(lc->str, "before") == 0) {
+ *(int *)(item->value) = SCRIPT_Before ;
+ } else if (strcasecmp(lc->str, "after") == 0) {
+ *(int *)(item->value) = SCRIPT_After;
+ } else if (strcasecmp(lc->str, "always") == 0) {
+ *(int *)(item->value) = SCRIPT_Any;
+ } else {
+ scan_err2(lc, _("Expect %s, got: %s"), "Before, After or Always", lc->str);
+ }
+ scan_to_eol(lc);
+}
+
+/* Store a runscript->target
+ *
+ */
+static void store_runscript_target(LEX *lc, RES_ITEM *item, int index, int pass)
+{
+ lex_get_token(lc, T_STRING);
+
+ if (pass == 2) {
+ if (strcmp(lc->str, "%c") == 0) {
+ ((RUNSCRIPT*) item->value)->set_target(lc->str);
+ } else if (strcasecmp(lc->str, "yes") == 0) {
+ ((RUNSCRIPT*) item->value)->set_target("%c");
+ } else if (strcasecmp(lc->str, "no") == 0) {
+ ((RUNSCRIPT*) item->value)->set_target("");
+ } else {
+ RES *res = GetResWithName(R_CLIENT, lc->str);
+ if (res == NULL) {
+ scan_err3(lc, _("Could not find config Resource %s referenced on line %d : %s\n"),
+ lc->str, lc->line_no, lc->line);
+ }
+
+ ((RUNSCRIPT*) item->value)->set_target(lc->str);
+ }
+ }
+ scan_to_eol(lc);
+}
+
+/*
+ * Store a runscript->command as a string
+ */
+static void store_runscript_cmd(LEX *lc, RES_ITEM *item, int index, int pass)
+{
+ lex_get_token(lc, T_STRING);
+
+ if (pass == 2) {
+ ((RUNSCRIPT*)item->value)->set_command(lc->str, item->code);
+ }
+ scan_to_eol(lc);
+}
+
+static void store_short_runscript(LEX *lc, RES_ITEM *item, int index, int pass)
+{
+ lex_get_token(lc, T_STRING);
+ alist **runscripts = (alist **)(item->value) ;
+
+ if (pass == 2) {
+ RUNSCRIPT *script = new_runscript();
+ script->set_job_code_callback(job_code_callback_filesetname);
+
+ script->set_command(lc->str);
+
+ /* TODO: remove all script->old_proto with bacula 1.42 */
+
+ if (strcmp(item->name, "runbeforejob") == 0) {
+ script->when = SCRIPT_Before;
+ script->fail_on_error = true;
+ script->set_target("");
+
+ } else if (strcmp(item->name, "runafterjob") == 0) {
+ script->when = SCRIPT_After;
+ script->on_success = true;
+ script->on_failure = false;
+ script->set_target("");
+
+ } else if (strcmp(item->name, "clientrunafterjob") == 0) {
+ script->old_proto = true;
+ script->when = SCRIPT_After;
+ script->set_target("%c");
+ script->on_success = true;
+ script->on_failure = false;
+
+ } else if (strcmp(item->name, "clientrunbeforejob") == 0) {
+ script->old_proto = true;
+ script->when = SCRIPT_Before;
+ script->set_target("%c");
+ script->fail_on_error = true;
+
+ } else if (strcmp(item->name, "runafterfailedjob") == 0) {
+ script->when = SCRIPT_After;
+ script->on_failure = true;
+ script->on_success = false;
+ script->set_target("");
+ }
+
+ if (*runscripts == NULL) {
+ *runscripts = New(alist(10, not_owned_by_alist));
+ }
+
+ (*runscripts)->append(script);
+ script->debug();
+ }
+
+ scan_to_eol(lc);
+}
+
+/* Store a bool in a bit field without modifing res_all.hdr
+ * We can also add an option to store_bool to skip res_all.hdr
+ */
+void store_runscript_bool(LEX *lc, RES_ITEM *item, int index, int pass)
+{
+ lex_get_token(lc, T_NAME);
+ if (strcasecmp(lc->str, "yes") == 0 || strcasecmp(lc->str, "true") == 0) {
+ *(bool *)(item->value) = true;
+ } else if (strcasecmp(lc->str, "no") == 0 || strcasecmp(lc->str, "false") == 0) {
+ *(bool *)(item->value) = false;
+ } else {
+ scan_err2(lc, _("Expect %s, got: %s"), "YES, NO, TRUE, or FALSE", lc->str); /* YES and NO must not be translated */
+ }
+ scan_to_eol(lc);
+}
+
+/*
+ * new RunScript items
+ * name handler value code flags default_value
+ */
+static RES_ITEM runscript_items[] = {
+ {"command", store_runscript_cmd, {(char **)&res_runscript}, SHELL_CMD, 0, 0},
+ {"console", store_runscript_cmd, {(char **)&res_runscript}, CONSOLE_CMD, 0, 0},
+ {"target", store_runscript_target,{(char **)&res_runscript}, 0, 0, 0},
+ {"runsonsuccess", store_runscript_bool, {(char **)&res_runscript.on_success},0, 0, 0},
+ {"runsonfailure", store_runscript_bool, {(char **)&res_runscript.on_failure},0, 0, 0},
+ {"failjobonerror",store_runscript_bool, {(char **)&res_runscript.fail_on_error},0, 0, 0},
+ {"abortjobonerror",store_runscript_bool, {(char **)&res_runscript.fail_on_error},0, 0, 0},
+ {"runswhen", store_runscript_when, {(char **)&res_runscript.when}, 0, 0, 0},
+ {"runsonclient", store_runscript_target,{(char **)&res_runscript}, 0, 0, 0}, /* TODO */
+ {NULL, NULL, {0}, 0, 0, 0}
+};
+
+/*
+ * Store RunScript info
+ *
+ * Note, when this routine is called, we are inside a Job
+ * resource. We treat the RunScript like a sort of
+ * mini-resource within the Job resource.
+ */
+static void store_runscript(LEX *lc, RES_ITEM *item, int index, int pass)
+{
+ int token, i;
+ alist **runscripts = (alist **)(item->value) ;
+
+ Dmsg1(200, "store_runscript: begin store_runscript pass=%i\n", pass);
+
+ res_runscript.reset_default(); /* setting on_success, on_failure, fail_on_error */
+
+ token = lex_get_token(lc, T_SKIP_EOL);
+
+ if (token != T_BOB) {
+ scan_err1(lc, _("Expecting open brace. Got %s"), lc->str);
+ }
+
+ while ((token = lex_get_token(lc, T_SKIP_EOL)) != T_EOF) {
+ if (token == T_EOB) {
+ break;
+ }
+ if (token != T_IDENTIFIER) {
+ scan_err1(lc, _("Expecting keyword, got: %s\n"), lc->str);
+ }
+ for (i=0; runscript_items[i].name; i++) {
+ if (strcasecmp(runscript_items[i].name, lc->str) == 0) {
+ token = lex_get_token(lc, T_SKIP_EOL);
+ if (token != T_EQUALS) {
+ scan_err1(lc, _("expected an equals, got: %s"), lc->str);
+ }
+
+ /* Call item handler */
+ runscript_items[i].handler(lc, &runscript_items[i], i, pass);
+ i = -1;
+ break;
+ }
+ }
+
+ if (i >=0) {
+ scan_err1(lc, _("Keyword %s not permitted in this resource"), lc->str);
+ }
+ }
+
+ if (pass == 2) {
+ if (res_runscript.command == NULL) {
+ scan_err2(lc, _("%s item is required in %s resource, but not found.\n"),
+ "command", "runscript");
+ }
+
+ /* run on client by default */
+ if (res_runscript.target == NULL) {
+ res_runscript.set_target("%c");
+ }
+
+ RUNSCRIPT *script = new_runscript();
+ memcpy(script, &res_runscript, sizeof(RUNSCRIPT));
+ script->set_job_code_callback(job_code_callback_filesetname);
+
+ if (*runscripts == NULL) {
+ *runscripts = New(alist(10, not_owned_by_alist));
+ }
+
+ (*runscripts)->append(script);
+ script->debug();
+ }
+
+ scan_to_eol(lc);
+ set_bit(index, res_all.hdr.item_present);
+}
+
+/* callback function for edit_job_codes */
+extern "C" char *job_code_callback_filesetname(JCR *jcr, const char* param)
+{
+ if (param[0] == 'f') {
+ return jcr->fileset->name();
+ } else {
+ return NULL;
+ }
+}