3 * Bacula Director -- User Agent Prompt and Selection code
5 * Kern Sibbald, October MMI
9 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of
14 the License, or (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public
22 License along with this program; if not, write to the Free
23 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
33 /* Imported variables */
36 /* Exported functions */
38 int do_prompt(UAContext *ua, char *msg, char *prompt);
39 void add_prompt(UAContext *ua, char *prompt);
40 void start_prompt(UAContext *ua, char *msg);
41 STORE *select_storage_resource(UAContext *ua);
42 JOB *select_job_resource(UAContext *ua);
45 * Given a list of keywords, find the first one
46 * that is in the argument list.
47 * Returns: -1 if not found
48 * index into list (base 0) on success
50 int find_arg_keyword(UAContext *ua, char **list)
53 for (i=1; i<ua->argc; i++) {
54 for(j=0; list[j]; j++) {
55 if (strcasecmp(_(list[j]), ua->argk[i]) == 0) {
64 * Given a list of keywords, prompt the user
67 * Returns: -1 on failure
68 * index into list (base 0) on success
70 int do_keyword_prompt(UAContext *ua, char *msg, char **list)
73 start_prompt(ua, _("You have the following choices:\n"));
74 for (i=0; list[i]; i++) {
75 add_prompt(ua, list[i]);
77 return do_prompt(ua, msg, NULL);
82 * Select a Storage resource from prompt list
84 STORE *select_storage_resource(UAContext *ua)
86 char name[MAX_NAME_LENGTH];
89 start_prompt(ua, _("The defined Storage resources are:\n"));
91 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
92 add_prompt(ua, store->hdr.name);
95 do_prompt(ua, _("Select Storage resource"), name);
96 store = (STORE *)GetResWithName(R_STORAGE, name);
101 * Select a FileSet resource from prompt list
103 FILESET *select_fs_resource(UAContext *ua)
105 char name[MAX_NAME_LENGTH];
108 start_prompt(ua, _("The defined FileSet resources are:\n"));
110 while ((fs = (FILESET *)GetNextRes(R_FILESET, (RES *)fs))) {
111 add_prompt(ua, fs->hdr.name);
114 do_prompt(ua, _("Select FileSet resource"), name);
115 fs = (FILESET *)GetResWithName(R_FILESET, name);
121 * Get a catalog resource from prompt list
123 CAT *get_catalog_resource(UAContext *ua)
125 char name[MAX_NAME_LENGTH];
129 for (i=1; i<ua->argc; i++) {
130 if (strcasecmp(ua->argk[i], _("catalog")) == 0 && ua->argv[i]) {
131 catalog = (CAT *)GetResWithName(R_CATALOG, ua->argv[i]);
136 start_prompt(ua, _("The defined Catalog resources are:\n"));
138 while ((catalog = (CAT *)GetNextRes(R_CATALOG, (RES *)catalog))) {
139 add_prompt(ua, catalog->hdr.name);
142 do_prompt(ua, _("Select Catalog resource"), name);
143 catalog = (CAT *)GetResWithName(R_CATALOG, name);
150 * Select a Job resource from prompt list
152 JOB *select_job_resource(UAContext *ua)
154 char name[MAX_NAME_LENGTH];
157 start_prompt(ua, _("The defined Job resources are:\n"));
159 while ( (job = (JOB *)GetNextRes(R_JOB, (RES *)job)) ) {
160 add_prompt(ua, job->hdr.name);
163 do_prompt(ua, _("Select Job resource"), name);
164 job = (JOB *)GetResWithName(R_JOB, name);
170 * Select a client resource from prompt list
172 CLIENT *select_client_resource(UAContext *ua)
174 char name[MAX_NAME_LENGTH];
175 CLIENT *client = NULL;
177 start_prompt(ua, _("The defined Client resources are:\n"));
179 while ( (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)) ) {
180 add_prompt(ua, client->hdr.name);
183 do_prompt(ua, _("Select Client (File daemon) resource"), name);
184 client = (CLIENT *)GetResWithName(R_CLIENT, name);
189 * Get client resource, start by looking for
190 * client=<client-name>
191 * if we don't find the keyword, we prompt the user.
193 CLIENT *get_client_resource(UAContext *ua)
195 CLIENT *client = NULL;
198 for (i=1; i<ua->argc; i++) {
199 if (strcasecmp(ua->argk[i], _("client")) == 0 && ua->argv[i]) {
200 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
204 bsendmsg(ua, _("Error: Client resource %s does not exist.\n"), ua->argv[i]);
208 return select_client_resource(ua);
214 /* Scan what the user has entered looking for:
218 * if error or not found, put up a list of pool DBRs
221 * returns: 0 on error
222 * poolid on success and fills in POOL_DBR
224 int get_pool_dbr(UAContext *ua, POOL_DBR *pr)
228 for (i=1; i<ua->argc; i++) {
229 if (strcasecmp(ua->argk[i], _("pool")) == 0 && ua->argv[i]) {
230 strcpy(pr->Name, ua->argv[i]);
231 if (!db_get_pool_record(ua->db, pr)) {
232 bsendmsg(ua, _("Could not find Pool %s: ERR=%s"), ua->argv[i],
233 db_strerror(ua->db));
240 if (!select_pool_dbr(ua, pr)) { /* try once more */
247 * Select a Pool record from the catalog
249 int select_pool_dbr(UAContext *ua, POOL_DBR *pr)
252 char name[MAX_NAME_LENGTH];
258 if (!db_get_pool_ids(ua->db, &num_pools, &ids)) {
259 bsendmsg(ua, _("Error obtaining pool ids. ERR=%s\n"), db_strerror(ua->db));
262 if (num_pools <= 0) {
263 bsendmsg(ua, _("No pools defined.\n"));
267 start_prompt(ua, _("Defined Pools:\n"));
268 for (i=0; i < num_pools; i++) {
270 if (!db_get_pool_record(ua->db, &opr)) {
273 add_prompt(ua, opr.Name);
276 if (do_prompt(ua, _("Select the Pool"), name) < 0) {
279 memset(&opr, 0, sizeof(pr));
280 strcpy(opr.Name, name);
282 if (!db_get_pool_record(ua->db, &opr)) {
283 bsendmsg(ua, _("Could not find Pool %s: ERR=%s"), name, db_strerror(ua->db));
286 memcpy(pr, &opr, sizeof(opr));
292 * This routine is ONLY used in the create command.
293 * If you are thinking about using it, you
294 * probably want to use select_pool_dbr()
295 * or get_pool_dbr() above.
297 POOL *get_pool_resource(UAContext *ua)
300 char name[MAX_NAME_LENGTH];
303 for (i=1; i<ua->argc; i++) {
304 if (strcasecmp(ua->argk[i], _("pool")) == 0 && ua->argv[i]) {
305 pool = (POOL *)GetResWithName(R_POOL, ua->argv[i]);
309 bsendmsg(ua, _("Error: Pool resource %s does not exist.\n"), ua->argv[i]);
313 start_prompt(ua, _("The defined Pool resources are:\n"));
315 while ((pool = (POOL *)GetNextRes(R_POOL, (RES *)pool))) {
316 add_prompt(ua, pool->hdr.name);
319 do_prompt(ua, _("Select Pool resource"), name);
320 pool = (POOL *)GetResWithName(R_POOL, name);
328 * Implement unique set of prompts
330 void start_prompt(UAContext *ua, char *msg)
332 if (ua->max_prompts == 0) {
333 ua->max_prompts = 10;
334 ua->prompt = (char **) bmalloc(sizeof(char *) * ua->max_prompts);
337 ua->prompt[0] = bstrdup(msg);
341 * Add to prompts -- keeping them unique
343 void add_prompt(UAContext *ua, char *prompt)
346 if (ua->num_prompts == ua->max_prompts) {
347 ua->max_prompts *= 2;
348 ua->prompt = (char **) brealloc(ua->prompt, sizeof(char *) *
351 for (i=1; i < ua->num_prompts; i++) {
352 if (strcmp(ua->prompt[i], prompt) == 0) {
356 ua->prompt[ua->num_prompts++] = bstrdup(prompt);
360 * Display prompts and get user's choice
362 * Returns: -1 on error
363 * index base 0 on success, and choice
364 * is copied to prompt if not NULL
366 int do_prompt(UAContext *ua, char *msg, char *prompt)
369 char pmsg[MAXSTRING];
371 bsendmsg(ua, ua->prompt[0]);
372 for (i=1; i < ua->num_prompts; i++) {
373 bsendmsg(ua, "%6d: %s\n", i, ua->prompt[i]);
381 if (ua->num_prompts == 2) {
383 bsendmsg(ua, _("Item 1 selected automatically.\n"));
385 strcpy(prompt, ua->prompt[1]);
389 sprintf(pmsg, "%s (1-%d): ", msg, ua->num_prompts-1);
391 if (!get_cmd(ua, pmsg) || *ua->cmd == '.') {
392 item = -1; /* error */
395 item = atoi(ua->cmd);
396 if (item < 1 || item >= ua->num_prompts) {
397 bsendmsg(ua, _("Please enter a number between 1 and %d\n"), ua->num_prompts-1);
401 strcpy(prompt, ua->prompt[item]);
406 for (i=0; i < ua->num_prompts; i++) {
415 * We scan what the user has entered looking for
417 * device=<device-name> ???? does this work ????
418 * storage=<storage-resource>
421 * ? (prompt him with storage list)
422 * <some-error> (prompt him with storage list)
424 STORE *get_storage_resource(UAContext *ua, char *cmd)
426 char *store_name, *device_name;
433 return select_storage_resource(ua);
439 for (i=1; i<ua->argc; i++) {
441 /* Default argument is storage */
443 bsendmsg(ua, _("Storage name given twice.\n"));
446 store_name = ua->argk[i];
447 if (*store_name == '?') {
448 return select_storage_resource(ua);
451 if (strcasecmp(ua->argk[i], _("device")) == 0) {
452 device_name = ua->argv[i];
454 } else if (strcasecmp(ua->argk[i], _("storage")) == 0) {
455 store_name = ua->argv[i];
457 } else if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
458 jobid = atoi(ua->argv[i]);
460 bsendmsg(ua, _("Expecting jobid=nn command, got: %s\n"), ua->argk[i]);
463 if (!(jcr=get_jcr_by_id(jobid))) {
464 bsendmsg(ua, _("JobId %d is not running.\n"), jobid);
471 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
472 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
473 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
481 bsendmsg(ua, _("Unknown keyword: %s\n"), ua->argk[i]);
488 bsendmsg(ua, _("A storage device name must be given.\n"));
491 store = (STORE *)GetResWithName(R_STORAGE, store_name);
493 bsendmsg(ua, "Storage resource %s: not found\n", store_name);
497 store = select_storage_resource(ua);
504 * Scan looking for mediatype=
506 * if not found or error, put up selection list
508 * Returns: 0 on error
509 * 1 on success, MediaType is set
511 int get_media_type(UAContext *ua, char *MediaType)
515 static char *keyword[] = {
519 i = find_arg_keyword(ua, keyword);
520 if (i >= 0 && ua->argv[i]) {
521 strcpy(MediaType, ua->argv[i]);
525 start_prompt(ua, _("Media Types defined in conf file:\n"));
527 for (store = NULL; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); ) {
528 add_prompt(ua, store->media_type);
531 return (do_prompt(ua, _("Select the Media Type"), MediaType) < 0) ? 0 : 1;