3 * Bacula Director -- User Agent Prompt and Selection code
5 * Kern Sibbald, October MMI
11 Copyright (C) 2000, 2001, 2002 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 /* Imported variables */
39 * Confirm a retention period
41 int confirm_retention(UAContext *ua, utime_t *ret, char *msg)
46 bsendmsg(ua, _("The current %s retention period is: %s\n"),
47 msg, edit_utime(*ret, ed1));
48 if (!get_cmd(ua, _("Continue? (yes/mod/no): "))) {
51 if (strcasecmp(ua->cmd, _("mod")) == 0) {
52 if (!get_cmd(ua, _("Enter new retention period: "))) {
55 if (!duration_to_utime(ua->cmd, ret)) {
56 bsendmsg(ua, _("Invalid period.\n"));
61 if (strcasecmp(ua->cmd, _("yes")) == 0) {
69 * Given a list of keywords, find the first one
70 * that is in the argument list.
71 * Returns: -1 if not found
72 * index into list (base 0) on success
74 int find_arg_keyword(UAContext *ua, char **list)
77 for (i=1; i<ua->argc; i++) {
78 for(j=0; list[j]; j++) {
79 if (strcasecmp(_(list[j]), ua->argk[i]) == 0) {
88 * Given a list of keywords, prompt the user
91 * Returns: -1 on failure
92 * index into list (base 0) on success
94 int do_keyword_prompt(UAContext *ua, char *msg, char **list)
97 start_prompt(ua, _("You have the following choices:\n"));
98 for (i=0; list[i]; i++) {
99 add_prompt(ua, list[i]);
101 return do_prompt(ua, msg, NULL, 0);
106 * Select a Storage resource from prompt list
108 STORE *select_storage_resource(UAContext *ua)
110 char name[MAX_NAME_LENGTH];
113 start_prompt(ua, _("The defined Storage resources are:\n"));
115 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
116 add_prompt(ua, store->hdr.name);
119 do_prompt(ua, _("Select Storage resource"), name, sizeof(name));
120 store = (STORE *)GetResWithName(R_STORAGE, name);
125 * Select a FileSet resource from prompt list
127 FILESET *select_fileset_resource(UAContext *ua)
129 char name[MAX_NAME_LENGTH];
132 start_prompt(ua, _("The defined FileSet resources are:\n"));
134 while ((fs = (FILESET *)GetNextRes(R_FILESET, (RES *)fs))) {
135 add_prompt(ua, fs->hdr.name);
138 do_prompt(ua, _("Select FileSet resource"), name, sizeof(name));
139 fs = (FILESET *)GetResWithName(R_FILESET, name);
145 * Get a catalog resource from prompt list
147 CAT *get_catalog_resource(UAContext *ua)
149 char name[MAX_NAME_LENGTH];
153 for (i=1; i<ua->argc; i++) {
154 if (strcasecmp(ua->argk[i], _("catalog")) == 0 && ua->argv[i]) {
155 catalog = (CAT *)GetResWithName(R_CATALOG, ua->argv[i]);
160 start_prompt(ua, _("The defined Catalog resources are:\n"));
162 while ((catalog = (CAT *)GetNextRes(R_CATALOG, (RES *)catalog))) {
163 add_prompt(ua, catalog->hdr.name);
166 do_prompt(ua, _("Select Catalog resource"), name, sizeof(name));
167 catalog = (CAT *)GetResWithName(R_CATALOG, name);
174 * Select a Job resource from prompt list
176 JOB *select_job_resource(UAContext *ua)
178 char name[MAX_NAME_LENGTH];
181 start_prompt(ua, _("The defined Job resources are:\n"));
183 while ( (job = (JOB *)GetNextRes(R_JOB, (RES *)job)) ) {
184 add_prompt(ua, job->hdr.name);
187 do_prompt(ua, _("Select Job resource"), name, sizeof(name));
188 job = (JOB *)GetResWithName(R_JOB, name);
193 * Select a Restore Job resource from prompt list
195 JOB *select_restore_job_resource(UAContext *ua)
197 char name[MAX_NAME_LENGTH];
200 start_prompt(ua, _("The defined Restore Job resources are:\n"));
202 while ( (job = (JOB *)GetNextRes(R_JOB, (RES *)job)) ) {
203 if (job->JobType == JT_RESTORE) {
204 add_prompt(ua, job->hdr.name);
208 do_prompt(ua, _("Select Restore Job"), name, sizeof(name));
209 job = (JOB *)GetResWithName(R_JOB, name);
216 * Select a client resource from prompt list
218 CLIENT *select_client_resource(UAContext *ua)
220 char name[MAX_NAME_LENGTH];
221 CLIENT *client = NULL;
223 start_prompt(ua, _("The defined Client resources are:\n"));
225 while ( (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)) ) {
226 add_prompt(ua, client->hdr.name);
229 do_prompt(ua, _("Select Client (File daemon) resource"), name, sizeof(name));
230 client = (CLIENT *)GetResWithName(R_CLIENT, name);
235 * Get client resource, start by looking for
236 * client=<client-name>
237 * if we don't find the keyword, we prompt the user.
239 CLIENT *get_client_resource(UAContext *ua)
241 CLIENT *client = NULL;
244 for (i=1; i<ua->argc; i++) {
245 if (strcasecmp(ua->argk[i], _("client")) == 0 && ua->argv[i]) {
246 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
250 bsendmsg(ua, _("Error: Client resource %s does not exist.\n"), ua->argv[i]);
254 return select_client_resource(ua);
257 /* Scan what the user has entered looking for:
259 * client=<client-name>
261 * if error or not found, put up a list of client DBRs
264 * returns: 0 on error
265 * 1 on success and fills in CLIENT_DBR
267 int get_client_dbr(UAContext *ua, CLIENT_DBR *cr)
271 if (cr->Name[0]) { /* If name already supplied */
272 if (db_get_client_record(ua->db, cr)) {
275 bsendmsg(ua, _("Could not find Client %s: ERR=%s"), cr->Name, db_strerror(ua->db));
277 for (i=1; i<ua->argc; i++) {
278 if (strcasecmp(ua->argk[i], _("client")) == 0 && ua->argv[i]) {
279 bstrncpy(cr->Name, ua->argv[i], sizeof(cr->Name));
280 if (!db_get_client_record(ua->db, cr)) {
281 bsendmsg(ua, _("Could not find Client %s: ERR=%s"), ua->argv[i],
282 db_strerror(ua->db));
289 if (!select_client_dbr(ua, cr)) { /* try once more by proposing a list */
296 * Select a Client record from the catalog
297 * Returns 1 on success
300 int select_client_dbr(UAContext *ua, CLIENT_DBR *cr)
303 char name[MAX_NAME_LENGTH];
309 if (!db_get_client_ids(ua->db, &num_clients, &ids)) {
310 bsendmsg(ua, _("Error obtaining client ids. ERR=%s\n"), db_strerror(ua->db));
313 if (num_clients <= 0) {
314 bsendmsg(ua, _("No clients defined. Run a job to create one.\n"));
318 start_prompt(ua, _("Defined Clients:\n"));
319 for (i=0; i < num_clients; i++) {
320 ocr.ClientId = ids[i];
321 if (!db_get_client_record(ua->db, &ocr)) {
324 add_prompt(ua, ocr.Name);
327 if (do_prompt(ua, _("Select the Client"), name, sizeof(name)) < 0) {
330 memset(&ocr, 0, sizeof(ocr));
331 bstrncpy(ocr.Name, name, sizeof(ocr.Name));
333 if (!db_get_client_record(ua->db, &ocr)) {
334 bsendmsg(ua, _("Could not find Client %s: ERR=%s"), name, db_strerror(ua->db));
337 memcpy(cr, &ocr, sizeof(ocr));
343 /* Scan what the user has entered looking for:
347 * if error or not found, put up a list of pool DBRs
350 * returns: 0 on error
351 * 1 on success and fills in POOL_DBR
353 int get_pool_dbr(UAContext *ua, POOL_DBR *pr)
357 if (pr->Name[0]) { /* If name already supplied */
358 if (db_get_pool_record(ua->db, pr)) {
361 bsendmsg(ua, _("Could not find Pool %s: ERR=%s"), pr->Name, db_strerror(ua->db));
363 for (i=1; i<ua->argc; i++) {
364 if (strcasecmp(ua->argk[i], _("pool")) == 0 && ua->argv[i]) {
365 bstrncpy(pr->Name, ua->argv[i], sizeof(pr->Name));
366 if (!db_get_pool_record(ua->db, pr)) {
367 bsendmsg(ua, _("Could not find Pool %s: ERR=%s"), ua->argv[i],
368 db_strerror(ua->db));
375 if (!select_pool_dbr(ua, pr)) { /* try once more */
382 * Select a Pool record from the catalog
384 int select_pool_dbr(UAContext *ua, POOL_DBR *pr)
387 char name[MAX_NAME_LENGTH];
393 if (!db_get_pool_ids(ua->db, &num_pools, &ids)) {
394 bsendmsg(ua, _("Error obtaining pool ids. ERR=%s\n"), db_strerror(ua->db));
397 if (num_pools <= 0) {
398 bsendmsg(ua, _("No pools defined. Use the \"create\" command to create one.\n"));
402 start_prompt(ua, _("Defined Pools:\n"));
403 for (i=0; i < num_pools; i++) {
405 if (!db_get_pool_record(ua->db, &opr)) {
408 add_prompt(ua, opr.Name);
411 if (do_prompt(ua, _("Select the Pool"), name, sizeof(name)) < 0) {
414 memset(&opr, 0, sizeof(opr));
415 bstrncpy(opr.Name, name, sizeof(opr.Name));
417 if (!db_get_pool_record(ua->db, &opr)) {
418 bsendmsg(ua, _("Could not find Pool %s: ERR=%s"), name, db_strerror(ua->db));
421 memcpy(pr, &opr, sizeof(opr));
426 * Select a Pool and a Media (Volume) record from the database
428 int select_pool_and_media_dbr(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr)
431 static char *kw[] = {
435 memset(pr, 0, sizeof(POOL_DBR));
436 memset(mr, 0, sizeof(MEDIA_DBR));
438 /* Get the pool, possibly from pool=<pool-name> */
439 if (!get_pool_dbr(ua, pr)) {
442 mr->PoolId = pr->PoolId;
444 i = find_arg_keyword(ua, kw);
445 if (i == 0 && ua->argv[i]) {
446 bstrncpy(mr->VolumeName, ua->argv[i], sizeof(mr->VolumeName));
448 if (mr->VolumeName[0] == 0) {
449 db_list_media_records(ua->db, mr, prtit, ua);
450 if (!get_cmd(ua, _("Enter MediaId or Volume name: "))) {
453 if (is_a_number(ua->cmd)) {
454 mr->MediaId = atoi(ua->cmd);
456 bstrncpy(mr->VolumeName, ua->cmd, sizeof(mr->VolumeName));
460 if (!db_get_media_record(ua->db, mr)) {
461 bsendmsg(ua, "%s", db_strerror(ua->db));
469 * This routine is ONLY used in the create command.
470 * If you are thinking about using it, you
471 * probably want to use select_pool_dbr()
472 * or get_pool_dbr() above.
474 POOL *get_pool_resource(UAContext *ua)
477 char name[MAX_NAME_LENGTH];
480 for (i=1; i<ua->argc; i++) {
481 if (strcasecmp(ua->argk[i], _("pool")) == 0 && ua->argv[i]) {
482 pool = (POOL *)GetResWithName(R_POOL, ua->argv[i]);
486 bsendmsg(ua, _("Error: Pool resource %s does not exist.\n"), ua->argv[i]);
490 start_prompt(ua, _("The defined Pool resources are:\n"));
492 while ((pool = (POOL *)GetNextRes(R_POOL, (RES *)pool))) {
493 add_prompt(ua, pool->hdr.name);
496 do_prompt(ua, _("Select Pool resource"), name, sizeof(name));
497 pool = (POOL *)GetResWithName(R_POOL, name);
502 * List all jobs and ask user to select one
504 int select_job_dbr(UAContext *ua, JOB_DBR *jr)
506 db_list_job_records(ua->db, jr, prtit, ua);
507 if (!get_cmd(ua, _("Enter the JobId to select: "))) {
510 jr->JobId = atoi(ua->cmd);
511 if (!db_get_job_record(ua->db, jr)) {
512 bsendmsg(ua, "%s", db_strerror(ua->db));
520 /* Scan what the user has entered looking for:
524 * if error or not found, put up a list of Jobs
527 * returns: 0 on error
528 * JobId on success and fills in JOB_DBR
530 int get_job_dbr(UAContext *ua, JOB_DBR *jr)
534 for (i=1; i<ua->argc; i++) {
535 if (strcasecmp(ua->argk[i], _("job")) == 0 && ua->argv[i]) {
537 bstrncpy(jr->Job, ua->argv[i], sizeof(jr->Job));
538 } else if (strcasecmp(ua->argk[i], _("jobid")) == 0 && ua->argv[i]) {
539 jr->JobId = atoi(ua->argv[i]);
543 if (!db_get_job_record(ua->db, jr)) {
544 bsendmsg(ua, _("Could not find Job %s: ERR=%s"), ua->argv[i],
545 db_strerror(ua->db));
552 if (!select_job_dbr(ua, jr)) { /* try once more */
559 * Implement unique set of prompts
561 void start_prompt(UAContext *ua, char *msg)
563 if (ua->max_prompts == 0) {
564 ua->max_prompts = 10;
565 ua->prompt = (char **)bmalloc(sizeof(char *) * ua->max_prompts);
568 ua->prompt[0] = bstrdup(msg);
572 * Add to prompts -- keeping them unique
574 void add_prompt(UAContext *ua, char *prompt)
577 if (ua->num_prompts == ua->max_prompts) {
578 ua->max_prompts *= 2;
579 ua->prompt = (char **)brealloc(ua->prompt, sizeof(char *) *
582 for (i=1; i < ua->num_prompts; i++) {
583 if (strcmp(ua->prompt[i], prompt) == 0) {
587 ua->prompt[ua->num_prompts++] = bstrdup(prompt);
591 * Display prompts and get user's choice
593 * Returns: -1 on error
594 * index base 0 on success, and choice
595 * is copied to prompt if not NULL
597 int do_prompt(UAContext *ua, char *msg, char *prompt, int max_prompt)
600 char pmsg[MAXSTRING];
602 bsendmsg(ua, ua->prompt[0]);
603 for (i=1; i < ua->num_prompts; i++) {
604 bsendmsg(ua, "%6d: %s\n", i, ua->prompt[i]);
612 /* First item is the prompt string, not the items */
613 if (ua->num_prompts == 1) {
614 item = 0; /* list is empty ! */
617 if (ua->num_prompts == 2) {
619 bsendmsg(ua, _("Item 1 selected automatically.\n"));
621 bstrncpy(prompt, ua->prompt[1], max_prompt);
625 sprintf(pmsg, "%s (1-%d): ", msg, ua->num_prompts-1);
627 /* Either a . or an @ will get you out of the loop */
628 if (!get_cmd(ua, pmsg) || *ua->cmd == '.' || *ua->cmd == '@') {
629 item = -1; /* error */
630 bsendmsg(ua, _("Selection aborted, nothing done.\n"));
633 item = atoi(ua->cmd);
634 if (item < 1 || item >= ua->num_prompts) {
635 bsendmsg(ua, _("Please enter a number between 1 and %d\n"), ua->num_prompts-1);
639 bstrncpy(prompt, ua->prompt[item], max_prompt);
644 for (i=0; i < ua->num_prompts; i++) {
653 * We scan what the user has entered looking for
655 * device=<device-name> ???? does this work ????
656 * storage=<storage-resource>
659 * ? (prompt him with storage list)
660 * <some-error> (prompt him with storage list)
662 STORE *get_storage_resource(UAContext *ua, char *cmd)
664 char *store_name, *device_name;
671 return select_storage_resource(ua);
677 for (i=1; i<ua->argc; i++) {
679 /* Default argument is storage */
681 bsendmsg(ua, _("Storage name given twice.\n"));
684 store_name = ua->argk[i];
685 if (*store_name == '?') {
686 return select_storage_resource(ua);
689 if (strcasecmp(ua->argk[i], _("device")) == 0) {
690 device_name = ua->argv[i];
692 } else if (strcasecmp(ua->argk[i], _("storage")) == 0) {
693 store_name = ua->argv[i];
695 } else if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
696 jobid = atoi(ua->argv[i]);
698 bsendmsg(ua, _("Expecting jobid=nn command, got: %s\n"), ua->argk[i]);
701 if (!(jcr=get_jcr_by_id(jobid))) {
702 bsendmsg(ua, _("JobId %d is not running.\n"), jobid);
709 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
710 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
711 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
719 bsendmsg(ua, _("Unknown keyword: %s\n"), ua->argk[i]);
726 bsendmsg(ua, _("A storage device name must be given.\n"));
729 store = (STORE *)GetResWithName(R_STORAGE, store_name);
731 bsendmsg(ua, "Storage resource %s: not found\n", store_name);
735 store = select_storage_resource(ua);
742 * Scan looking for mediatype=
744 * if not found or error, put up selection list
746 * Returns: 0 on error
747 * 1 on success, MediaType is set
749 int get_media_type(UAContext *ua, char *MediaType, int max_media)
753 static char *keyword[] = {
757 i = find_arg_keyword(ua, keyword);
758 if (i >= 0 && ua->argv[i]) {
759 bstrncpy(MediaType, ua->argv[i], max_media);
763 start_prompt(ua, _("Media Types defined in conf file:\n"));
765 for (store = NULL; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); ) {
766 add_prompt(ua, store->media_type);
769 return (do_prompt(ua, _("Select the Media Type"), MediaType, max_media) < 0) ? 0 : 1;