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) {
87 int find_arg(UAContext *ua, char *keyword)
89 for (int i=1; i<ua->argc; i++) {
90 if (strcasecmp(keyword, ua->argk[i]) == 0) {
98 * Given a single keyword, find it in the argument list, but
99 * it must have a value
100 * Returns: -1 if not found or no value
101 * list index (base 0) on success
103 int find_arg_with_value(UAContext *ua, char *keyword)
105 for (int i=1; i<ua->argc; i++) {
106 if (strcasecmp(keyword, ua->argk[i]) == 0) {
118 * Given a list of keywords, prompt the user
121 * Returns: -1 on failure
122 * index into list (base 0) on success
124 int do_keyword_prompt(UAContext *ua, char *msg, char **list)
127 start_prompt(ua, _("You have the following choices:\n"));
128 for (i=0; list[i]; i++) {
129 add_prompt(ua, list[i]);
131 return do_prompt(ua, "", msg, NULL, 0);
136 * Select a Storage resource from prompt list
138 STORE *select_storage_resource(UAContext *ua)
140 char name[MAX_NAME_LENGTH];
143 start_prompt(ua, _("The defined Storage resources are:\n"));
145 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
146 add_prompt(ua, store->hdr.name);
149 do_prompt(ua, _("Storage"), _("Select Storage resource"), name, sizeof(name));
150 store = (STORE *)GetResWithName(R_STORAGE, name);
155 * Select a FileSet resource from prompt list
157 FILESET *select_fileset_resource(UAContext *ua)
159 char name[MAX_NAME_LENGTH];
162 start_prompt(ua, _("The defined FileSet resources are:\n"));
164 while ((fs = (FILESET *)GetNextRes(R_FILESET, (RES *)fs))) {
165 add_prompt(ua, fs->hdr.name);
168 do_prompt(ua, _("FileSet"), _("Select FileSet resource"), name, sizeof(name));
169 fs = (FILESET *)GetResWithName(R_FILESET, name);
175 * Get a catalog resource from prompt list
177 CAT *get_catalog_resource(UAContext *ua)
179 char name[MAX_NAME_LENGTH];
183 for (i=1; i<ua->argc; i++) {
184 if (strcasecmp(ua->argk[i], _("catalog")) == 0 && ua->argv[i]) {
185 catalog = (CAT *)GetResWithName(R_CATALOG, ua->argv[i]);
190 start_prompt(ua, _("The defined Catalog resources are:\n"));
192 while ((catalog = (CAT *)GetNextRes(R_CATALOG, (RES *)catalog))) {
193 add_prompt(ua, catalog->hdr.name);
196 do_prompt(ua, _("Catalog"), _("Select Catalog resource"), name, sizeof(name));
197 catalog = (CAT *)GetResWithName(R_CATALOG, name);
204 * Select a Job resource from prompt list
206 JOB *select_job_resource(UAContext *ua)
208 char name[MAX_NAME_LENGTH];
211 start_prompt(ua, _("The defined Job resources are:\n"));
213 while ( (job = (JOB *)GetNextRes(R_JOB, (RES *)job)) ) {
214 add_prompt(ua, job->hdr.name);
217 do_prompt(ua, _("Job"), _("Select Job resource"), name, sizeof(name));
218 job = (JOB *)GetResWithName(R_JOB, name);
223 * Select a Restore Job resource from prompt list
225 JOB *select_restore_job_resource(UAContext *ua)
227 char name[MAX_NAME_LENGTH];
230 start_prompt(ua, _("The defined Restore Job resources are:\n"));
232 while ( (job = (JOB *)GetNextRes(R_JOB, (RES *)job)) ) {
233 if (job->JobType == JT_RESTORE) {
234 add_prompt(ua, job->hdr.name);
238 do_prompt(ua, _("Job"), _("Select Restore Job"), name, sizeof(name));
239 job = (JOB *)GetResWithName(R_JOB, name);
246 * Select a client resource from prompt list
248 CLIENT *select_client_resource(UAContext *ua)
250 char name[MAX_NAME_LENGTH];
251 CLIENT *client = NULL;
253 start_prompt(ua, _("The defined Client resources are:\n"));
255 while ( (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)) ) {
256 add_prompt(ua, client->hdr.name);
259 do_prompt(ua, _("Client"), _("Select Client (File daemon) resource"), name, sizeof(name));
260 client = (CLIENT *)GetResWithName(R_CLIENT, name);
265 * Get client resource, start by looking for
266 * client=<client-name>
267 * if we don't find the keyword, we prompt the user.
269 CLIENT *get_client_resource(UAContext *ua)
271 CLIENT *client = NULL;
274 for (i=1; i<ua->argc; i++) {
275 if (strcasecmp(ua->argk[i], _("client")) == 0 && ua->argv[i]) {
276 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
280 bsendmsg(ua, _("Error: Client resource %s does not exist.\n"), ua->argv[i]);
284 return select_client_resource(ua);
287 /* Scan what the user has entered looking for:
289 * client=<client-name>
291 * if error or not found, put up a list of client DBRs
294 * returns: 0 on error
295 * 1 on success and fills in CLIENT_DBR
297 int get_client_dbr(UAContext *ua, CLIENT_DBR *cr)
301 if (cr->Name[0]) { /* If name already supplied */
302 if (db_get_client_record(ua->jcr, ua->db, cr)) {
305 bsendmsg(ua, _("Could not find Client %s: ERR=%s"), cr->Name, db_strerror(ua->db));
307 for (i=1; i<ua->argc; i++) {
308 if (strcasecmp(ua->argk[i], _("client")) == 0 && ua->argv[i]) {
309 bstrncpy(cr->Name, ua->argv[i], sizeof(cr->Name));
310 if (!db_get_client_record(ua->jcr, ua->db, cr)) {
311 bsendmsg(ua, _("Could not find Client %s: ERR=%s"), ua->argv[i],
312 db_strerror(ua->db));
319 if (!select_client_dbr(ua, cr)) { /* try once more by proposing a list */
326 * Select a Client record from the catalog
327 * Returns 1 on success
330 int select_client_dbr(UAContext *ua, CLIENT_DBR *cr)
333 char name[MAX_NAME_LENGTH];
339 if (!db_get_client_ids(ua->jcr, ua->db, &num_clients, &ids)) {
340 bsendmsg(ua, _("Error obtaining client ids. ERR=%s\n"), db_strerror(ua->db));
343 if (num_clients <= 0) {
344 bsendmsg(ua, _("No clients defined. Run a job to create one.\n"));
348 start_prompt(ua, _("Defined Clients:\n"));
349 for (i=0; i < num_clients; i++) {
350 ocr.ClientId = ids[i];
351 if (!db_get_client_record(ua->jcr, ua->db, &ocr)) {
354 add_prompt(ua, ocr.Name);
357 if (do_prompt(ua, _("Client"), _("Select the Client"), name, sizeof(name)) < 0) {
360 memset(&ocr, 0, sizeof(ocr));
361 bstrncpy(ocr.Name, name, sizeof(ocr.Name));
363 if (!db_get_client_record(ua->jcr, ua->db, &ocr)) {
364 bsendmsg(ua, _("Could not find Client %s: ERR=%s"), name, db_strerror(ua->db));
367 memcpy(cr, &ocr, sizeof(ocr));
373 /* Scan what the user has entered looking for:
377 * if error or not found, put up a list of pool DBRs
380 * returns: 0 on error
381 * 1 on success and fills in POOL_DBR
383 int get_pool_dbr(UAContext *ua, POOL_DBR *pr)
385 if (pr->Name[0]) { /* If name already supplied */
386 if (db_get_pool_record(ua->jcr, ua->db, pr)) {
389 bsendmsg(ua, _("Could not find Pool %s: ERR=%s"), pr->Name, db_strerror(ua->db));
391 if (!select_pool_dbr(ua, pr)) { /* try once more */
398 * Select a Pool record from the catalog
400 int select_pool_dbr(UAContext *ua, POOL_DBR *pr)
403 char name[MAX_NAME_LENGTH];
407 for (i=1; i<ua->argc; i++) {
408 if (strcasecmp(ua->argk[i], _("pool")) == 0 && ua->argv[i]) {
409 bstrncpy(pr->Name, ua->argv[i], sizeof(pr->Name));
410 if (!db_get_pool_record(ua->jcr, ua->db, pr)) {
411 bsendmsg(ua, _("Could not find Pool %s: ERR=%s"), ua->argv[i],
412 db_strerror(ua->db));
421 if (!db_get_pool_ids(ua->jcr, ua->db, &num_pools, &ids)) {
422 bsendmsg(ua, _("Error obtaining pool ids. ERR=%s\n"), db_strerror(ua->db));
425 if (num_pools <= 0) {
426 bsendmsg(ua, _("No pools defined. Use the \"create\" command to create one.\n"));
430 start_prompt(ua, _("Defined Pools:\n"));
431 for (i=0; i < num_pools; i++) {
433 if (!db_get_pool_record(ua->jcr, ua->db, &opr)) {
436 add_prompt(ua, opr.Name);
439 if (do_prompt(ua, _("Pool"), _("Select the Pool"), name, sizeof(name)) < 0) {
442 memset(&opr, 0, sizeof(opr));
443 bstrncpy(opr.Name, name, sizeof(opr.Name));
445 if (!db_get_pool_record(ua->jcr, ua->db, &opr)) {
446 bsendmsg(ua, _("Could not find Pool %s: ERR=%s"), name, db_strerror(ua->db));
449 memcpy(pr, &opr, sizeof(opr));
454 * Select a Pool and a Media (Volume) record from the database
456 int select_pool_and_media_dbr(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr)
459 if (!select_media_dbr(ua, mr)) {
462 memset(pr, 0, sizeof(POOL_DBR));
463 pr->PoolId = mr->PoolId;
464 if (!db_get_pool_record(ua->jcr, ua->db, pr)) {
465 bsendmsg(ua, "%s", db_strerror(ua->db));
471 /* Select a Media (Volume) record from the database */
472 int select_media_dbr(UAContext *ua, MEDIA_DBR *mr)
476 memset(mr, 0, sizeof(MEDIA_DBR));
478 i = find_arg_with_value(ua, "volume");
480 bstrncpy(mr->VolumeName, ua->argv[i], sizeof(mr->VolumeName));
482 if (mr->VolumeName[0] == 0) {
484 memset(&pr, 0, sizeof(pr));
485 /* Get the pool from pool=<pool-name> */
486 if (!get_pool_dbr(ua, &pr)) {
489 mr->PoolId = pr.PoolId;
490 db_list_media_records(ua->jcr, ua->db, mr, prtit, ua, HORZ_LIST);
491 if (!get_cmd(ua, _("Enter MediaId or Volume name: "))) {
494 if (is_a_number(ua->cmd)) {
495 mr->MediaId = atoi(ua->cmd);
497 bstrncpy(mr->VolumeName, ua->cmd, sizeof(mr->VolumeName));
501 if (!db_get_media_record(ua->jcr, ua->db, mr)) {
502 bsendmsg(ua, "%s", db_strerror(ua->db));
510 * Select a pool resource from prompt list
512 POOL *select_pool_resource(UAContext *ua)
514 char name[MAX_NAME_LENGTH];
517 start_prompt(ua, _("The defined Pool resources are:\n"));
519 while ((pool = (POOL *)GetNextRes(R_POOL, (RES *)pool))) {
520 add_prompt(ua, pool->hdr.name);
523 do_prompt(ua, _("Pool"), _("Select Pool resource"), name, sizeof(name));
524 pool = (POOL *)GetResWithName(R_POOL, name);
530 * If you are thinking about using it, you
531 * probably want to use select_pool_dbr()
532 * or get_pool_dbr() above.
534 POOL *get_pool_resource(UAContext *ua)
539 i = find_arg_with_value(ua, "pool");
541 pool = (POOL *)GetResWithName(R_POOL, ua->argv[i]);
545 bsendmsg(ua, _("Error: Pool resource %s does not exist.\n"), ua->argv[i]);
547 return select_pool_resource(ua);
551 * List all jobs and ask user to select one
553 int select_job_dbr(UAContext *ua, JOB_DBR *jr)
555 db_list_job_records(ua->jcr, ua->db, jr, prtit, ua, HORZ_LIST);
556 if (!get_pint(ua, _("Enter the JobId to select: "))) {
559 jr->JobId = ua->pint32_val;
560 if (!db_get_job_record(ua->jcr, ua->db, jr)) {
561 bsendmsg(ua, "%s", db_strerror(ua->db));
569 /* Scan what the user has entered looking for:
573 * if error or not found, put up a list of Jobs
576 * returns: 0 on error
577 * JobId on success and fills in JOB_DBR
579 int get_job_dbr(UAContext *ua, JOB_DBR *jr)
583 for (i=1; i<ua->argc; i++) {
584 if (strcasecmp(ua->argk[i], _("job")) == 0 && ua->argv[i]) {
586 bstrncpy(jr->Job, ua->argv[i], sizeof(jr->Job));
587 } else if (strcasecmp(ua->argk[i], _("jobid")) == 0 && ua->argv[i]) {
588 jr->JobId = atoi(ua->argv[i]);
592 if (!db_get_job_record(ua->jcr, ua->db, jr)) {
593 bsendmsg(ua, _("Could not find Job %s: ERR=%s"), ua->argv[i],
594 db_strerror(ua->db));
601 if (!select_job_dbr(ua, jr)) { /* try once more */
608 * Implement unique set of prompts
610 void start_prompt(UAContext *ua, char *msg)
612 if (ua->max_prompts == 0) {
613 ua->max_prompts = 10;
614 ua->prompt = (char **)bmalloc(sizeof(char *) * ua->max_prompts);
617 ua->prompt[0] = bstrdup(msg);
621 * Add to prompts -- keeping them unique
623 void add_prompt(UAContext *ua, char *prompt)
626 if (ua->num_prompts == ua->max_prompts) {
627 ua->max_prompts *= 2;
628 ua->prompt = (char **)brealloc(ua->prompt, sizeof(char *) *
631 for (i=1; i < ua->num_prompts; i++) {
632 if (strcmp(ua->prompt[i], prompt) == 0) {
636 ua->prompt[ua->num_prompts++] = bstrdup(prompt);
640 * Display prompts and get user's choice
642 * Returns: -1 on error
643 * index base 0 on success, and choice
644 * is copied to prompt if not NULL
646 int do_prompt(UAContext *ua, char *automsg, char *msg, char *prompt, int max_prompt)
649 char pmsg[MAXSTRING];
651 if (ua->num_prompts == 2) {
654 bstrncpy(prompt, ua->prompt[1], max_prompt);
656 bsendmsg(ua, _("Automatically selected %s: %s\n"), automsg, ua->prompt[1]);
659 bsendmsg(ua, ua->prompt[0]);
660 for (i=1; i < ua->num_prompts; i++) {
661 bsendmsg(ua, "%6d: %s\n", i, ua->prompt[i]);
669 /* First item is the prompt string, not the items */
670 if (ua->num_prompts == 1) {
671 bsendmsg(ua, _("Selection is empty!\n"));
672 item = 0; /* list is empty ! */
675 if (ua->num_prompts == 2) {
677 bsendmsg(ua, _("Item 1 selected automatically.\n"));
679 bstrncpy(prompt, ua->prompt[1], max_prompt);
683 sprintf(pmsg, "%s (1-%d): ", msg, ua->num_prompts-1);
685 /* Either a . or an @ will get you out of the loop */
686 if (!get_pint(ua, pmsg)) {
687 item = -1; /* error */
688 bsendmsg(ua, _("Selection aborted, nothing done.\n"));
691 item = ua->pint32_val;
692 if (item < 1 || item >= ua->num_prompts) {
693 bsendmsg(ua, _("Please enter a number between 1 and %d\n"), ua->num_prompts-1);
697 bstrncpy(prompt, ua->prompt[item], max_prompt);
703 for (i=0; i < ua->num_prompts; i++) {
712 * We scan what the user has entered looking for
713 * storage=<storage-resource>
716 * ? (prompt him with storage list)
717 * <some-error> (prompt him with storage list)
719 * If use_default is set, we assume that any keyword without a value
720 * is the name of the Storage resource wanted.
722 STORE *get_storage_resource(UAContext *ua, int use_default)
724 char *store_name = NULL;
731 for (i=1; i<ua->argc; i++) {
732 if (use_default && !ua->argv[i]) {
733 /* Default argument is storage */
735 bsendmsg(ua, _("Storage name given twice.\n"));
738 /* Ignore barcode(s) keywords */
739 if (strncasecmp("barcode", ua->argk[i], 7) == 0) {
742 store_name = ua->argk[i];
743 if (*store_name == '?') {
748 if (strcasecmp(ua->argk[i], _("storage")) == 0) {
749 store_name = ua->argv[i];
752 } else if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
753 jobid = atoi(ua->argv[i]);
755 bsendmsg(ua, _("Expecting jobid=nn command, got: %s\n"), ua->argk[i]);
758 if (!(jcr=get_jcr_by_id(jobid))) {
759 bsendmsg(ua, _("JobId %d is not running.\n"), jobid);
766 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
767 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
768 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
778 if (!store && store_name) {
779 store = (STORE *)GetResWithName(R_STORAGE, store_name);
781 bsendmsg(ua, "Storage resource %s: not found\n", store_name);
784 /* No keywords found, so present a selection list */
786 store = select_storage_resource(ua);
793 * Scan looking for mediatype=
795 * if not found or error, put up selection list
797 * Returns: 0 on error
798 * 1 on success, MediaType is set
800 int get_media_type(UAContext *ua, char *MediaType, int max_media)
805 i = find_arg_with_value(ua, "mediatype");
807 bstrncpy(MediaType, ua->argv[i], max_media);
811 start_prompt(ua, _("Media Types defined in conf file:\n"));
813 for (store = NULL; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); ) {
814 add_prompt(ua, store->media_type);
817 return (do_prompt(ua, _("Media Type"), _("Select the Media Type"), MediaType, max_media) < 0) ? 0 : 1;