3 * Bacula Director -- expand.c -- does variable expansion
4 * in particular for the LabelFormat specification.
6 * Kern Sibbald, June MMIII
11 Copyright (C) 2000-2003 Kern Sibbald and John Walker
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.
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.
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,
35 static int date_item(JCR *jcr, int code,
36 const char **val_ptr, int *val_len, int *val_size)
39 time_t now = time(NULL);
40 localtime_r(&now, &tm);
46 val = tm.tm_year + 1900;
63 case 7: /* Week day */
67 bsnprintf(buf, sizeof(buf), "%d", val);
68 *val_ptr = bstrdup(buf);
69 *val_len = strlen(buf);
74 static int job_item(JCR *jcr, int code,
75 const char **val_ptr, int *val_len, int *val_size)
82 str = jcr->job->hdr.name;
84 case 2: /* Director's name */
88 str = job_level_to_str(jcr->JobLevel);
91 str = job_type_to_str(jcr->JobType);
94 bsnprintf(buf, sizeof(buf), "%d", jcr->JobId);
98 str = jcr->client->hdr.name;
103 case 7: /* NumVols */
104 bsnprintf(buf, sizeof(buf), "%d", jcr->NumVols);
108 str = jcr->pool->hdr.name;
110 case 9: /* Storage */
111 str = jcr->store->hdr.name;
113 case 10: /* Catalog */
114 str = jcr->catalog->hdr.name;
116 case 11: /* MediaType */
117 str = jcr->store->media_type;
119 case 12: /* JobName */
123 *val_ptr = bstrdup(str);
124 *val_len = strlen(str);
125 *val_size = *val_len;
130 struct s_built_in_vars {char *var_name; int code; int (*func)(JCR *jcr, int code,
131 const char **val_ptr, int *val_len, int *val_size);};
133 static struct s_built_in_vars built_in_vars[] = {
134 { N_("Year"), 1, date_item},
135 { N_("Month"), 2, date_item},
136 { N_("Day"), 3, date_item},
137 { N_("Hour"), 4, date_item},
138 { N_("Minute"), 5, date_item},
139 { N_("Second"), 6, date_item},
140 { N_("WeekDay"), 7, date_item},
142 { N_("Job"), 1, job_item},
143 { N_("Dir"), 2, job_item},
144 { N_("Level"), 3, job_item},
145 { N_("Type"), 4, job_item},
146 { N_("JobId"), 5, job_item},
147 { N_("Client"), 6, job_item},
148 { N_("NumVols"), 7, job_item},
149 { N_("Pool"), 8, job_item},
150 { N_("Storage"), 9, job_item},
151 { N_("Catalog"), 10, job_item},
152 { N_("MediaType"), 11, job_item},
153 { N_("JobName"), 12, job_item},
159 static var_rc_t lookup_built_in_var(var_t *ctx, void *my_ctx,
160 const char *var_ptr, int var_len, int var_index,
161 const char **val_ptr, int *val_len, int *val_size)
163 JCR *jcr = (JCR *)my_ctx;
166 for (int i=0; _(built_in_vars[i].var_name); i++) {
167 if (strncmp(_(built_in_vars[i].var_name), var_ptr, var_len) == 0) {
168 stat = (*built_in_vars[i].func)(jcr, built_in_vars[i].code,
169 val_ptr, val_len, val_size);
176 return VAR_ERR_UNDEFINED_VARIABLE;
181 * Search counter variables
183 static var_rc_t lookup_counter_var(var_t *ctx, void *my_ctx,
184 const char *var_ptr, int var_len, int var_inc, int var_index,
185 const char **val_ptr, int *val_len, int *val_size)
188 var_rc_t stat = VAR_ERR_UNDEFINED_VARIABLE;
190 if (var_len > (int)sizeof(buf) - 1) {
191 return VAR_ERR_OUT_OF_MEMORY;
193 memcpy(buf, var_ptr, var_len);
196 for (COUNTER *counter=NULL; (counter = (COUNTER *)GetNextRes(R_COUNTER, (RES *)counter)); ) {
197 if (strcmp(counter->hdr.name, buf) == 0) {
198 Dmsg2(100, "Counter=%s val=%d\n", buf, counter->CurrentValue);
199 bsnprintf(buf, sizeof(buf), "%d", counter->CurrentValue);
200 *val_ptr = bstrdup(buf);
201 *val_len = strlen(buf);
202 *val_size = *val_len;
203 if (var_inc && counter->Catalog) {
205 JCR *jcr = (JCR *)my_ctx;
206 memset(&cr, 0, sizeof(cr));
207 bstrncpy(cr.Counter, counter->hdr.name, sizeof(cr.Counter));
208 cr.MinValue = counter->MinValue;
209 cr.MaxValue = counter->MaxValue;
210 if (counter->CurrentValue == counter->MaxValue) {
211 counter->CurrentValue = counter->MinValue;
213 counter->CurrentValue++;
215 cr.CurrentValue = counter->CurrentValue;
216 Dmsg1(100, "New value=%d\n", cr.CurrentValue);
217 if (counter->WrapCounter) {
218 bstrncpy(cr.WrapCounter, counter->WrapCounter->hdr.name, sizeof(cr.WrapCounter));
220 cr.WrapCounter[0] = 0;
222 if (!db_update_counter_record(jcr, jcr->db, &cr)) {
223 Jmsg(jcr, M_ERROR, 0, _("Count not update counter %s: ERR=%s\n"),
224 counter->hdr.name, db_strerror(jcr->db));
237 * Called here to look up a variable
239 static var_rc_t lookup_var(var_t *ctx, void *my_ctx,
240 const char *var_ptr, int var_len, int var_inc, int var_index,
241 const char **val_ptr, int *val_len, int *val_size)
243 char buf[MAXSTRING], *val, *p, *v;
247 if ((stat = lookup_built_in_var(ctx, my_ctx, var_ptr, var_len, var_index,
248 val_ptr, val_len, val_size)) == VAR_OK) {
252 if ((stat = lookup_counter_var(ctx, my_ctx, var_ptr, var_len, var_inc, var_index,
253 val_ptr, val_len, val_size)) == VAR_OK) {
257 /* Look in environment */
258 if (var_len > (int)sizeof(buf) - 1) {
259 return VAR_ERR_OUT_OF_MEMORY;
261 memcpy(buf, var_ptr, var_len + 1);
263 Dmsg1(100, "Var=%s\n", buf);
265 if ((val = getenv(buf)) == NULL) {
266 return VAR_ERR_UNDEFINED_VARIABLE;
268 if (var_index == 0) {
270 *val_len = strlen(val);
274 /* He wants to index the "array" */
276 /* Find the size of the "array"
277 * each element is separated by a |
279 for (p = val; *p; p++) {
285 Dmsg3(100, "For %s, reqest index=%d have=%d\n",
286 buf, var_index, count);
287 if (var_index < 0 || var_index > count) {
288 return VAR_ERR_SUBMATCH_OUT_OF_RANGE;
290 /* Now find the particular item (var_index) he wants */
294 if (count < var_index) {
303 if (p-val > (int)sizeof(buf) - 1) {
304 return VAR_ERR_OUT_OF_MEMORY;
306 Dmsg2(100, "val=%s len=%d\n", val, p-val);
307 /* Make a copy of item, and pass it back */
308 v = (char *)malloc(p-val+1);
309 memcpy(v, val, p-val);
314 Dmsg1(100, "v=%s\n", v);
319 * Called here to do a special operation on a variable
320 * op_ptr points to the special operation code (not EOS terminated)
321 * arg_ptr points to argument to special op code
322 * val_ptr points to the value string
323 * out_ptr points to string to be returned
325 static var_rc_t operate_var(var_t *var, void *my_ctx,
326 const char *op_ptr, int op_len,
327 const char *arg_ptr, int arg_len,
328 const char *val_ptr, int val_len,
329 char **out_ptr, int *out_len, int *out_size)
331 var_rc_t stat = VAR_ERR_UNDEFINED_OPERATION;
332 Dmsg0(100, "Enter operate_var\n");
337 if (op_len == 3 && strncmp(op_ptr, "inc", 3) == 0) {
339 if (val_len > (int)sizeof(buf) - 1) {
340 return VAR_ERR_OUT_OF_MEMORY;
342 memcpy(buf, arg_ptr, arg_len);
344 Dmsg1(100, "Arg=%s\n", buf);
345 memcpy(buf, val_ptr, val_len);
347 Dmsg1(100, "Val=%s\n", buf);
349 for (COUNTER *counter=NULL; (counter = (COUNTER *)GetNextRes(R_COUNTER, (RES *)counter)); ) {
350 if (strcmp(counter->hdr.name, buf) == 0) {
351 Dmsg2(100, "counter=%s val=%s\n", counter->hdr.name, buf);
364 * Expand an input line and return it.
366 * Returns: 0 on failure
367 * 1 on success and exp has expanded input
369 int variable_expansion(JCR *jcr, char *inp, POOLMEM **exp)
377 in_len = strlen(inp);
382 if ((stat = var_create(&var_ctx)) != VAR_OK) {
383 Jmsg(jcr, M_ERROR, 0, _("Cannot create var context: ERR=%s\n"), var_strerror(var_ctx, stat));
386 /* define callback */
387 if ((stat = var_config(var_ctx, VAR_CONFIG_CB_VALUE, lookup_var, (void *)jcr)) != VAR_OK) {
388 Jmsg(jcr, M_ERROR, 0, _("Cannot set var callback: ERR=%s\n"), var_strerror(var_ctx, stat));
392 /* define special operations */
393 if ((stat = var_config(var_ctx, VAR_CONFIG_CB_OPERATION, operate_var, (void *)jcr)) != VAR_OK) {
394 Jmsg(jcr, M_ERROR, 0, _("Cannot set var operate: ERR=%s\n"), var_strerror(var_ctx, stat));
398 /* unescape in place */
399 if ((stat = var_unescape(var_ctx, inp, in_len, inp, in_len+1, 0)) != VAR_OK) {
400 Jmsg(jcr, M_ERROR, 0, _("Cannot unescape string: ERR=%s\n"), var_strerror(var_ctx, stat));
404 in_len = strlen(inp);
406 /* expand variables */
407 if ((stat = var_expand(var_ctx, inp, in_len, &outp, &out_len, 1)) != VAR_OK) {
408 Jmsg(jcr, M_ERROR, 0, _("Cannot expand LabelFormat \"%s\": ERR=%s\n"),
409 inp, var_strerror(var_ctx, stat));
413 /* unescape once more in place */
414 if ((stat = var_unescape(var_ctx, outp, out_len, outp, out_len+1, 1)) != VAR_OK) {
415 Jmsg(jcr, M_ERROR, 0, _("Cannot unescape string: ERR=%s\n"), var_strerror(var_ctx, stat));
419 pm_strcpy(exp, outp);
424 /* destroy expansion context */
425 if ((stat = var_destroy(var_ctx)) != VAR_OK) {
426 Jmsg(jcr, M_ERROR, 0, _("Cannot destroy var context: ERR=%s\n"), var_strerror(var_ctx, stat));