3 * Bacula Director -- User Agent Prompt and Selection code
5 * Kern Sibbald, October MMI
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,
33 /* Imported variables */
37 * Confirm a retention period
39 int confirm_retention(UAContext *ua, utime_t *ret, char *msg)
44 bsendmsg(ua, _("The current %s retention period is: %s\n"),
45 msg, edit_utime(*ret, ed1));
46 if (!get_cmd(ua, _("Continue? (yes/mod/no): "))) {
49 if (strcasecmp(ua->cmd, _("mod")) == 0) {
50 if (!get_cmd(ua, _("Enter new retention period: "))) {
53 if (!duration_to_utime(ua->cmd, ret)) {
54 bsendmsg(ua, _("Invalid period.\n"));
59 if (strcasecmp(ua->cmd, _("yes")) == 0) {
62 if (strcasecmp(ua->cmd, _("no")) == 0) {
70 * Given a list of keywords, find the first one
71 * that is in the argument list.
72 * Returns: -1 if not found
73 * index into list (base 0) on success
75 int find_arg_keyword(UAContext *ua, char **list)
77 for (int i=1; i<ua->argc; i++) {
78 for(int j=0; list[j]; j++) {
79 if (strcasecmp(_(list[j]), ua->argk[i]) == 0) {
88 * Given one keyword, find the first one that
89 * is in the argument list.
90 * Returns: argk index (always gt 0)
93 int find_arg(UAContext *ua, char *keyword)
95 for (int i=1; i<ua->argc; i++) {
96 if (strcasecmp(keyword, ua->argk[i]) == 0) {
104 * Given a single keyword, find it in the argument list, but
105 * it must have a value
106 * Returns: -1 if not found or no value
107 * list index (base 0) on success
109 int find_arg_with_value(UAContext *ua, char *keyword)
111 for (int i=1; i<ua->argc; i++) {
112 if (strcasecmp(keyword, ua->argk[i]) == 0) {
124 * Given a list of keywords, prompt the user
127 * Returns: -1 on failure
128 * index into list (base 0) on success
130 int do_keyword_prompt(UAContext *ua, char *msg, char **list)
133 start_prompt(ua, _("You have the following choices:\n"));
134 for (i=0; list[i]; i++) {
135 add_prompt(ua, list[i]);
137 return do_prompt(ua, "", msg, NULL, 0);
142 * Select a Storage resource from prompt list
144 STORE *select_storage_resource(UAContext *ua)
146 char name[MAX_NAME_LENGTH];
149 start_prompt(ua, _("The defined Storage resources are:\n"));
151 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
152 add_prompt(ua, store->hdr.name);
155 do_prompt(ua, _("Storage"), _("Select Storage resource"), name, sizeof(name));
156 store = (STORE *)GetResWithName(R_STORAGE, name);
161 * Select a FileSet resource from prompt list
163 FILESET *select_fileset_resource(UAContext *ua)
165 char name[MAX_NAME_LENGTH];
168 start_prompt(ua, _("The defined FileSet resources are:\n"));
170 while ((fs = (FILESET *)GetNextRes(R_FILESET, (RES *)fs))) {
171 add_prompt(ua, fs->hdr.name);
174 do_prompt(ua, _("FileSet"), _("Select FileSet resource"), name, sizeof(name));
175 fs = (FILESET *)GetResWithName(R_FILESET, name);
181 * Get a catalog resource from prompt list
183 CAT *get_catalog_resource(UAContext *ua)
185 char name[MAX_NAME_LENGTH];
189 for (i=1; i<ua->argc; i++) {
190 if (strcasecmp(ua->argk[i], _("catalog")) == 0 && ua->argv[i]) {
191 catalog = (CAT *)GetResWithName(R_CATALOG, ua->argv[i]);
196 start_prompt(ua, _("The defined Catalog resources are:\n"));
198 while ((catalog = (CAT *)GetNextRes(R_CATALOG, (RES *)catalog))) {
199 add_prompt(ua, catalog->hdr.name);
202 do_prompt(ua, _("Catalog"), _("Select Catalog resource"), name, sizeof(name));
203 catalog = (CAT *)GetResWithName(R_CATALOG, name);
210 * Select a Job resource from prompt list
212 JOB *select_job_resource(UAContext *ua)
214 char name[MAX_NAME_LENGTH];
217 start_prompt(ua, _("The defined Job resources are:\n"));
219 while ( (job = (JOB *)GetNextRes(R_JOB, (RES *)job)) ) {
220 add_prompt(ua, job->hdr.name);
223 do_prompt(ua, _("Job"), _("Select Job resource"), name, sizeof(name));
224 job = (JOB *)GetResWithName(R_JOB, name);
229 * Select a Restore Job resource from prompt list
231 JOB *select_restore_job_resource(UAContext *ua)
233 char name[MAX_NAME_LENGTH];
236 start_prompt(ua, _("The defined Restore Job resources are:\n"));
238 while ( (job = (JOB *)GetNextRes(R_JOB, (RES *)job)) ) {
239 if (job->JobType == JT_RESTORE) {
240 add_prompt(ua, job->hdr.name);
244 do_prompt(ua, _("Job"), _("Select Restore Job"), name, sizeof(name));
245 job = (JOB *)GetResWithName(R_JOB, name);
252 * Select a client resource from prompt list
254 CLIENT *select_client_resource(UAContext *ua)
256 char name[MAX_NAME_LENGTH];
257 CLIENT *client = NULL;
259 start_prompt(ua, _("The defined Client resources are:\n"));
261 while ( (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)) ) {
262 add_prompt(ua, client->hdr.name);
265 do_prompt(ua, _("Client"), _("Select Client (File daemon) resource"), name, sizeof(name));
266 client = (CLIENT *)GetResWithName(R_CLIENT, name);
271 * Get client resource, start by looking for
272 * client=<client-name>
273 * if we don't find the keyword, we prompt the user.
275 CLIENT *get_client_resource(UAContext *ua)
277 CLIENT *client = NULL;
280 for (i=1; i<ua->argc; i++) {
281 if ((strcasecmp(ua->argk[i], _("client")) == 0 ||
282 strcasecmp(ua->argk[i], _("fd")) == 0) && ua->argv[i]) {
283 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
287 bsendmsg(ua, _("Error: Client resource %s does not exist.\n"), ua->argv[i]);
291 return select_client_resource(ua);
294 /* Scan what the user has entered looking for:
296 * client=<client-name>
298 * if error or not found, put up a list of client DBRs
301 * returns: 0 on error
302 * 1 on success and fills in CLIENT_DBR
304 int get_client_dbr(UAContext *ua, CLIENT_DBR *cr)
308 if (cr->Name[0]) { /* If name already supplied */
309 if (db_get_client_record(ua->jcr, ua->db, cr)) {
312 bsendmsg(ua, _("Could not find Client %s: ERR=%s"), cr->Name, db_strerror(ua->db));
314 for (i=1; i<ua->argc; i++) {
315 if ((strcasecmp(ua->argk[i], _("client")) == 0 ||
316 strcasecmp(ua->argk[i], _("fd")) == 0) && ua->argv[i]) {
317 bstrncpy(cr->Name, ua->argv[i], sizeof(cr->Name));
318 if (!db_get_client_record(ua->jcr, ua->db, cr)) {
319 bsendmsg(ua, _("Could not find Client %s: ERR=%s"), ua->argv[i],
320 db_strerror(ua->db));
327 if (!select_client_dbr(ua, cr)) { /* try once more by proposing a list */
334 * Select a Client record from the catalog
335 * Returns 1 on success
338 int select_client_dbr(UAContext *ua, CLIENT_DBR *cr)
341 char name[MAX_NAME_LENGTH];
347 if (!db_get_client_ids(ua->jcr, ua->db, &num_clients, &ids)) {
348 bsendmsg(ua, _("Error obtaining client ids. ERR=%s\n"), db_strerror(ua->db));
351 if (num_clients <= 0) {
352 bsendmsg(ua, _("No clients defined. You must run a job before using this command.\n"));
356 start_prompt(ua, _("Defined Clients:\n"));
357 for (i=0; i < num_clients; i++) {
358 ocr.ClientId = ids[i];
359 if (!db_get_client_record(ua->jcr, ua->db, &ocr)) {
362 add_prompt(ua, ocr.Name);
365 if (do_prompt(ua, _("Client"), _("Select the Client"), name, sizeof(name)) < 0) {
368 memset(&ocr, 0, sizeof(ocr));
369 bstrncpy(ocr.Name, name, sizeof(ocr.Name));
371 if (!db_get_client_record(ua->jcr, ua->db, &ocr)) {
372 bsendmsg(ua, _("Could not find Client %s: ERR=%s"), name, db_strerror(ua->db));
375 memcpy(cr, &ocr, sizeof(ocr));
381 /* Scan what the user has entered looking for:
385 * if error or not found, put up a list of pool DBRs
388 * returns: 0 on error
389 * 1 on success and fills in POOL_DBR
391 int get_pool_dbr(UAContext *ua, POOL_DBR *pr)
393 if (pr->Name[0]) { /* If name already supplied */
394 if (db_get_pool_record(ua->jcr, ua->db, pr)) {
397 bsendmsg(ua, _("Could not find Pool %s: ERR=%s"), pr->Name, db_strerror(ua->db));
399 if (!select_pool_dbr(ua, pr)) { /* try once more */
406 * Select a Pool record from the catalog
408 int select_pool_dbr(UAContext *ua, POOL_DBR *pr)
411 char name[MAX_NAME_LENGTH];
415 for (i=1; i<ua->argc; i++) {
416 if (strcasecmp(ua->argk[i], _("pool")) == 0 && ua->argv[i]) {
417 bstrncpy(pr->Name, ua->argv[i], sizeof(pr->Name));
418 if (!db_get_pool_record(ua->jcr, ua->db, pr)) {
419 bsendmsg(ua, _("Could not find Pool %s: ERR=%s"), ua->argv[i],
420 db_strerror(ua->db));
429 if (!db_get_pool_ids(ua->jcr, ua->db, &num_pools, &ids)) {
430 bsendmsg(ua, _("Error obtaining pool ids. ERR=%s\n"), db_strerror(ua->db));
433 if (num_pools <= 0) {
434 bsendmsg(ua, _("No pools defined. Use the \"create\" command to create one.\n"));
438 start_prompt(ua, _("Defined Pools:\n"));
439 for (i=0; i < num_pools; i++) {
441 if (!db_get_pool_record(ua->jcr, ua->db, &opr)) {
444 add_prompt(ua, opr.Name);
447 if (do_prompt(ua, _("Pool"), _("Select the Pool"), name, sizeof(name)) < 0) {
450 memset(&opr, 0, sizeof(opr));
451 bstrncpy(opr.Name, name, sizeof(opr.Name));
453 if (!db_get_pool_record(ua->jcr, ua->db, &opr)) {
454 bsendmsg(ua, _("Could not find Pool %s: ERR=%s"), name, db_strerror(ua->db));
457 memcpy(pr, &opr, sizeof(opr));
462 * Select a Pool and a Media (Volume) record from the database
464 int select_pool_and_media_dbr(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr)
467 if (!select_media_dbr(ua, mr)) {
470 memset(pr, 0, sizeof(POOL_DBR));
471 pr->PoolId = mr->PoolId;
472 if (!db_get_pool_record(ua->jcr, ua->db, pr)) {
473 bsendmsg(ua, "%s", db_strerror(ua->db));
479 /* Select a Media (Volume) record from the database */
480 int select_media_dbr(UAContext *ua, MEDIA_DBR *mr)
484 memset(mr, 0, sizeof(MEDIA_DBR));
486 i = find_arg_with_value(ua, "volume");
488 bstrncpy(mr->VolumeName, ua->argv[i], sizeof(mr->VolumeName));
490 if (mr->VolumeName[0] == 0) {
492 memset(&pr, 0, sizeof(pr));
493 /* Get the pool from pool=<pool-name> */
494 if (!get_pool_dbr(ua, &pr)) {
497 mr->PoolId = pr.PoolId;
498 db_list_media_records(ua->jcr, ua->db, mr, prtit, ua, HORZ_LIST);
499 if (!get_cmd(ua, _("Enter MediaId or Volume name: "))) {
502 if (is_a_number(ua->cmd)) {
503 mr->MediaId = atoi(ua->cmd);
505 bstrncpy(mr->VolumeName, ua->cmd, sizeof(mr->VolumeName));
509 if (!db_get_media_record(ua->jcr, ua->db, mr)) {
510 bsendmsg(ua, "%s", db_strerror(ua->db));
518 * Select a pool resource from prompt list
520 POOL *select_pool_resource(UAContext *ua)
522 char name[MAX_NAME_LENGTH];
525 start_prompt(ua, _("The defined Pool resources are:\n"));
527 while ((pool = (POOL *)GetNextRes(R_POOL, (RES *)pool))) {
528 add_prompt(ua, pool->hdr.name);
531 do_prompt(ua, _("Pool"), _("Select Pool resource"), name, sizeof(name));
532 pool = (POOL *)GetResWithName(R_POOL, name);
538 * If you are thinking about using it, you
539 * probably want to use select_pool_dbr()
540 * or get_pool_dbr() above.
542 POOL *get_pool_resource(UAContext *ua)
547 i = find_arg_with_value(ua, "pool");
549 pool = (POOL *)GetResWithName(R_POOL, ua->argv[i]);
553 bsendmsg(ua, _("Error: Pool resource %s does not exist.\n"), ua->argv[i]);
555 return select_pool_resource(ua);
559 * List all jobs and ask user to select one
561 int select_job_dbr(UAContext *ua, JOB_DBR *jr)
563 db_list_job_records(ua->jcr, ua->db, jr, prtit, ua, HORZ_LIST);
564 if (!get_pint(ua, _("Enter the JobId to select: "))) {
567 jr->JobId = ua->pint32_val;
568 if (!db_get_job_record(ua->jcr, ua->db, jr)) {
569 bsendmsg(ua, "%s", db_strerror(ua->db));
577 /* Scan what the user has entered looking for:
581 * if error or not found, put up a list of Jobs
584 * returns: 0 on error
585 * JobId on success and fills in JOB_DBR
587 int get_job_dbr(UAContext *ua, JOB_DBR *jr)
591 for (i=1; i<ua->argc; i++) {
592 if (strcasecmp(ua->argk[i], _("job")) == 0 && ua->argv[i]) {
594 bstrncpy(jr->Job, ua->argv[i], sizeof(jr->Job));
595 } else if (strcasecmp(ua->argk[i], _("jobid")) == 0 && ua->argv[i]) {
596 jr->JobId = atoi(ua->argv[i]);
600 if (!db_get_job_record(ua->jcr, ua->db, jr)) {
601 bsendmsg(ua, _("Could not find Job %s: ERR=%s"), ua->argv[i],
602 db_strerror(ua->db));
609 if (!select_job_dbr(ua, jr)) { /* try once more */
616 * Implement unique set of prompts
618 void start_prompt(UAContext *ua, char *msg)
620 if (ua->max_prompts == 0) {
621 ua->max_prompts = 10;
622 ua->prompt = (char **)bmalloc(sizeof(char *) * ua->max_prompts);
625 ua->prompt[0] = bstrdup(msg);
629 * Add to prompts -- keeping them unique
631 void add_prompt(UAContext *ua, char *prompt)
634 if (ua->num_prompts == ua->max_prompts) {
635 ua->max_prompts *= 2;
636 ua->prompt = (char **)brealloc(ua->prompt, sizeof(char *) *
639 for (i=1; i < ua->num_prompts; i++) {
640 if (strcmp(ua->prompt[i], prompt) == 0) {
644 ua->prompt[ua->num_prompts++] = bstrdup(prompt);
648 * Display prompts and get user's choice
650 * Returns: -1 on error
651 * index base 0 on success, and choice
652 * is copied to prompt if not NULL
654 int do_prompt(UAContext *ua, char *automsg, char *msg, char *prompt, int max_prompt)
657 char pmsg[MAXSTRING];
659 if (ua->num_prompts == 2) {
662 bstrncpy(prompt, ua->prompt[1], max_prompt);
664 bsendmsg(ua, _("Automatically selected %s: %s\n"), automsg, ua->prompt[1]);
667 bsendmsg(ua, ua->prompt[0]);
668 for (i=1; i < ua->num_prompts; i++) {
669 bsendmsg(ua, "%6d: %s\n", i, ua->prompt[i]);
677 /* First item is the prompt string, not the items */
678 if (ua->num_prompts == 1) {
679 bsendmsg(ua, _("Selection is empty!\n"));
680 item = 0; /* list is empty ! */
683 if (ua->num_prompts == 2) {
685 bsendmsg(ua, _("Item 1 selected automatically.\n"));
687 bstrncpy(prompt, ua->prompt[1], max_prompt);
691 sprintf(pmsg, "%s (1-%d): ", msg, ua->num_prompts-1);
693 /* Either a . or an @ will get you out of the loop */
694 if (!get_pint(ua, pmsg)) {
695 item = -1; /* error */
696 bsendmsg(ua, _("Selection aborted, nothing done.\n"));
699 item = ua->pint32_val;
700 if (item < 1 || item >= ua->num_prompts) {
701 bsendmsg(ua, _("Please enter a number between 1 and %d\n"), ua->num_prompts-1);
705 bstrncpy(prompt, ua->prompt[item], max_prompt);
711 for (i=0; i < ua->num_prompts; i++) {
720 * We scan what the user has entered looking for
721 * storage=<storage-resource>
724 * ? (prompt him with storage list)
725 * <some-error> (prompt him with storage list)
727 * If use_default is set, we assume that any keyword without a value
728 * is the name of the Storage resource wanted.
730 STORE *get_storage_resource(UAContext *ua, int use_default)
732 char *store_name = NULL;
739 for (i=1; i<ua->argc; i++) {
740 if (use_default && !ua->argv[i]) {
741 /* Ignore scan and barcode(s) keywords */
742 if (strncasecmp("scan", ua->argk[i], 4) == 0 ||
743 strncasecmp("barcode", ua->argk[i], 7) == 0) {
746 /* Default argument is storage */
748 bsendmsg(ua, _("Storage name given twice.\n"));
751 store_name = ua->argk[i];
752 if (*store_name == '?') {
757 if (strcasecmp(ua->argk[i], _("storage")) == 0 ||
758 strcasecmp(ua->argk[i], _("sd")) == 0) {
759 store_name = ua->argv[i];
762 } else if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
763 jobid = atoi(ua->argv[i]);
765 bsendmsg(ua, _("Expecting jobid=nn command, got: %s\n"), ua->argk[i]);
768 if (!(jcr=get_jcr_by_id(jobid))) {
769 bsendmsg(ua, _("JobId %d is not running.\n"), jobid);
776 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
777 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
778 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
788 if (!store && store_name) {
789 store = (STORE *)GetResWithName(R_STORAGE, store_name);
791 bsendmsg(ua, "Storage resource %s: not found\n", store_name);
794 /* No keywords found, so present a selection list */
796 store = select_storage_resource(ua);
803 * Scan looking for mediatype=
805 * if not found or error, put up selection list
807 * Returns: 0 on error
808 * 1 on success, MediaType is set
810 int get_media_type(UAContext *ua, char *MediaType, int max_media)
815 i = find_arg_with_value(ua, "mediatype");
817 bstrncpy(MediaType, ua->argv[i], max_media);
821 start_prompt(ua, _("Media Types defined in conf file:\n"));
823 for (store = NULL; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); ) {
824 add_prompt(ua, store->media_type);
827 return (do_prompt(ua, _("Media Type"), _("Select the Media Type"), MediaType, max_media) < 0) ? 0 : 1;