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);
211 /* Scan what the user has entered looking for:
215 * if error or not found, put up a list of pool DBRs
218 * returns: 0 on error
219 * poolid on success and fills in POOL_DBR
221 int get_pool_dbr(UAContext *ua, POOL_DBR *pr)
225 for (i=1; i<ua->argc; i++) {
226 if (strcasecmp(ua->argk[i], _("pool")) == 0 && ua->argv[i]) {
227 strcpy(pr->Name, ua->argv[i]);
228 if (!db_get_pool_record(ua->db, pr)) {
229 bsendmsg(ua, _("Could not find Pool %s: ERR=%s"), ua->argv[i],
230 db_strerror(ua->db));
237 if (!select_pool_dbr(ua, pr)) { /* try once more */
244 * Select a Pool record from the catalog
246 int select_pool_dbr(UAContext *ua, POOL_DBR *pr)
249 char name[MAX_NAME_LENGTH];
255 if (!db_get_pool_ids(ua->db, &num_pools, &ids)) {
256 bsendmsg(ua, _("Error obtaining pool ids. ERR=%s\n"), db_strerror(ua->db));
259 if (num_pools <= 0) {
260 bsendmsg(ua, _("No pools defined. Use the \"create\" command to create one.\n"));
264 start_prompt(ua, _("Defined Pools:\n"));
265 for (i=0; i < num_pools; i++) {
267 if (!db_get_pool_record(ua->db, &opr)) {
270 add_prompt(ua, opr.Name);
273 if (do_prompt(ua, _("Select the Pool"), name) < 0) {
276 memset(&opr, 0, sizeof(pr));
277 strcpy(opr.Name, name);
279 if (!db_get_pool_record(ua->db, &opr)) {
280 bsendmsg(ua, _("Could not find Pool %s: ERR=%s"), name, db_strerror(ua->db));
283 memcpy(pr, &opr, sizeof(opr));
288 * Select a Pool and a Media (Volume) record from the database
290 int select_pool_and_media_dbr(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr)
295 memset(pr, 0, sizeof(POOL_DBR));
296 memset(mr, 0, sizeof(MEDIA_DBR));
298 /* Get the pool, possibly from pool=<pool-name> */
299 if (!get_pool_dbr(ua, pr)) {
302 mr->PoolId = pr->PoolId;
304 /* See if a volume name is specified as an argument */
305 for (i=1; i<ua->argc; i++) {
306 if (strcasecmp(ua->argk[i], _("volume")) == 0 && ua->argv[i]) {
312 strcpy(mr->VolumeName, ua->argv[i]);
314 db_list_media_records(ua->db, mr, prtit, ua);
315 if (!get_cmd(ua, _("Enter the Volume name to delete: "))) {
318 strcpy(mr->VolumeName, ua->cmd);
321 if (!db_get_media_record(ua->db, mr)) {
322 bsendmsg(ua, "%s", db_strerror(ua->db));
330 * This routine is ONLY used in the create command.
331 * If you are thinking about using it, you
332 * probably want to use select_pool_dbr()
333 * or get_pool_dbr() above.
335 POOL *get_pool_resource(UAContext *ua)
338 char name[MAX_NAME_LENGTH];
341 for (i=1; i<ua->argc; i++) {
342 if (strcasecmp(ua->argk[i], _("pool")) == 0 && ua->argv[i]) {
343 pool = (POOL *)GetResWithName(R_POOL, ua->argv[i]);
347 bsendmsg(ua, _("Error: Pool resource %s does not exist.\n"), ua->argv[i]);
351 start_prompt(ua, _("The defined Pool resources are:\n"));
353 while ((pool = (POOL *)GetNextRes(R_POOL, (RES *)pool))) {
354 add_prompt(ua, pool->hdr.name);
357 do_prompt(ua, _("Select Pool resource"), name);
358 pool = (POOL *)GetResWithName(R_POOL, name);
363 * List all jobs and ask user to select one
365 int select_job_dbr(UAContext *ua, JOB_DBR *jr)
367 db_list_job_records(ua->db, jr, prtit, ua);
368 if (!get_cmd(ua, _("Enter the JobId to select: "))) {
371 jr->JobId = atoi(ua->cmd);
372 if (!db_get_job_record(ua->db, jr)) {
373 bsendmsg(ua, "%s", db_strerror(ua->db));
381 /* Scan what the user has entered looking for:
385 * if error or not found, put up a list of Jobs
388 * returns: 0 on error
389 * JobId on success and fills in JOB_DBR
391 int get_job_dbr(UAContext *ua, JOB_DBR *jr)
395 for (i=1; i<ua->argc; i++) {
396 if (strcasecmp(ua->argk[i], _("job")) == 0 && ua->argv[i]) {
398 strcpy(jr->Job, ua->argv[i]);
399 } else if (strcasecmp(ua->argk[i], _("jobid")) == 0 && ua->argv[i]) {
400 jr->JobId = atoi(ua->argv[i]);
404 if (!db_get_job_record(ua->db, jr)) {
405 bsendmsg(ua, _("Could not find Job %s: ERR=%s"), ua->argv[i],
406 db_strerror(ua->db));
413 if (!select_job_dbr(ua, jr)) { /* try once more */
423 * Implement unique set of prompts
425 void start_prompt(UAContext *ua, char *msg)
427 if (ua->max_prompts == 0) {
428 ua->max_prompts = 10;
429 ua->prompt = (char **) bmalloc(sizeof(char *) * ua->max_prompts);
432 ua->prompt[0] = bstrdup(msg);
436 * Add to prompts -- keeping them unique
438 void add_prompt(UAContext *ua, char *prompt)
441 if (ua->num_prompts == ua->max_prompts) {
442 ua->max_prompts *= 2;
443 ua->prompt = (char **) brealloc(ua->prompt, sizeof(char *) *
446 for (i=1; i < ua->num_prompts; i++) {
447 if (strcmp(ua->prompt[i], prompt) == 0) {
451 ua->prompt[ua->num_prompts++] = bstrdup(prompt);
455 * Display prompts and get user's choice
457 * Returns: -1 on error
458 * index base 0 on success, and choice
459 * is copied to prompt if not NULL
461 int do_prompt(UAContext *ua, char *msg, char *prompt)
464 char pmsg[MAXSTRING];
466 bsendmsg(ua, ua->prompt[0]);
467 for (i=1; i < ua->num_prompts; i++) {
468 bsendmsg(ua, "%6d: %s\n", i, ua->prompt[i]);
476 if (ua->num_prompts == 2) {
478 bsendmsg(ua, _("Item 1 selected automatically.\n"));
480 strcpy(prompt, ua->prompt[1]);
484 sprintf(pmsg, "%s (1-%d): ", msg, ua->num_prompts-1);
486 /* Either a . or an @ will get you out of the loop */
487 if (!get_cmd(ua, pmsg) || *ua->cmd == '.' || *ua->cmd == '@') {
488 item = -1; /* error */
491 item = atoi(ua->cmd);
492 if (item < 1 || item >= ua->num_prompts) {
493 bsendmsg(ua, _("Please enter a number between 1 and %d\n"), ua->num_prompts-1);
497 strcpy(prompt, ua->prompt[item]);
502 for (i=0; i < ua->num_prompts; i++) {
511 * We scan what the user has entered looking for
513 * device=<device-name> ???? does this work ????
514 * storage=<storage-resource>
517 * ? (prompt him with storage list)
518 * <some-error> (prompt him with storage list)
520 STORE *get_storage_resource(UAContext *ua, char *cmd)
522 char *store_name, *device_name;
529 return select_storage_resource(ua);
535 for (i=1; i<ua->argc; i++) {
537 /* Default argument is storage */
539 bsendmsg(ua, _("Storage name given twice.\n"));
542 store_name = ua->argk[i];
543 if (*store_name == '?') {
544 return select_storage_resource(ua);
547 if (strcasecmp(ua->argk[i], _("device")) == 0) {
548 device_name = ua->argv[i];
550 } else if (strcasecmp(ua->argk[i], _("storage")) == 0) {
551 store_name = ua->argv[i];
553 } else if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
554 jobid = atoi(ua->argv[i]);
556 bsendmsg(ua, _("Expecting jobid=nn command, got: %s\n"), ua->argk[i]);
559 if (!(jcr=get_jcr_by_id(jobid))) {
560 bsendmsg(ua, _("JobId %d is not running.\n"), jobid);
567 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
568 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
569 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
577 bsendmsg(ua, _("Unknown keyword: %s\n"), ua->argk[i]);
584 bsendmsg(ua, _("A storage device name must be given.\n"));
587 store = (STORE *)GetResWithName(R_STORAGE, store_name);
589 bsendmsg(ua, "Storage resource %s: not found\n", store_name);
593 store = select_storage_resource(ua);
600 * Scan looking for mediatype=
602 * if not found or error, put up selection list
604 * Returns: 0 on error
605 * 1 on success, MediaType is set
607 int get_media_type(UAContext *ua, char *MediaType)
611 static char *keyword[] = {
615 i = find_arg_keyword(ua, keyword);
616 if (i >= 0 && ua->argv[i]) {
617 strcpy(MediaType, ua->argv[i]);
621 start_prompt(ua, _("Media Types defined in conf file:\n"));
623 for (store = NULL; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); ) {
624 add_prompt(ua, store->media_type);
627 return (do_prompt(ua, _("Select the Media Type"), MediaType) < 0) ? 0 : 1;