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 */
38 /* Exported functions */
40 int do_prompt(UAContext *ua, char *msg, char *prompt);
41 void add_prompt(UAContext *ua, char *prompt);
42 void start_prompt(UAContext *ua, char *msg);
43 STORE *select_storage_resource(UAContext *ua);
44 JOB *select_job_resource(UAContext *ua);
47 * Confirm a retention period
49 int confirm_retention(UAContext *ua, btime_t *ret, char *msg)
54 bsendmsg(ua, _("The current %s retention period is: %s\n"),
55 msg, edit_btime(*ret, ed1));
56 if (!get_cmd(ua, _("Continue? (yes/mod/no): "))) {
59 if (strcasecmp(ua->cmd, _("mod")) == 0) {
60 if (!get_cmd(ua, _("Enter new retention period: "))) {
63 if (!string_to_btime(ua->cmd, ret)) {
64 bsendmsg(ua, _("Invalid period.\n"));
69 if (strcasecmp(ua->cmd, _("yes")) == 0) {
77 * Given a list of keywords, find the first one
78 * that is in the argument list.
79 * Returns: -1 if not found
80 * index into list (base 0) on success
82 int find_arg_keyword(UAContext *ua, char **list)
85 for (i=1; i<ua->argc; i++) {
86 for(j=0; list[j]; j++) {
87 if (strcasecmp(_(list[j]), ua->argk[i]) == 0) {
96 * Given a list of keywords, prompt the user
99 * Returns: -1 on failure
100 * index into list (base 0) on success
102 int do_keyword_prompt(UAContext *ua, char *msg, char **list)
105 start_prompt(ua, _("You have the following choices:\n"));
106 for (i=0; list[i]; i++) {
107 add_prompt(ua, list[i]);
109 return do_prompt(ua, msg, NULL);
114 * Select a Storage resource from prompt list
116 STORE *select_storage_resource(UAContext *ua)
118 char name[MAX_NAME_LENGTH];
121 start_prompt(ua, _("The defined Storage resources are:\n"));
123 while ((store = (STORE *)GetNextRes(R_STORAGE, (RES *)store))) {
124 add_prompt(ua, store->hdr.name);
127 do_prompt(ua, _("Select Storage resource"), name);
128 store = (STORE *)GetResWithName(R_STORAGE, name);
133 * Select a FileSet resource from prompt list
135 FILESET *select_fs_resource(UAContext *ua)
137 char name[MAX_NAME_LENGTH];
140 start_prompt(ua, _("The defined FileSet resources are:\n"));
142 while ((fs = (FILESET *)GetNextRes(R_FILESET, (RES *)fs))) {
143 add_prompt(ua, fs->hdr.name);
146 do_prompt(ua, _("Select FileSet resource"), name);
147 fs = (FILESET *)GetResWithName(R_FILESET, name);
153 * Get a catalog resource from prompt list
155 CAT *get_catalog_resource(UAContext *ua)
157 char name[MAX_NAME_LENGTH];
161 for (i=1; i<ua->argc; i++) {
162 if (strcasecmp(ua->argk[i], _("catalog")) == 0 && ua->argv[i]) {
163 catalog = (CAT *)GetResWithName(R_CATALOG, ua->argv[i]);
168 start_prompt(ua, _("The defined Catalog resources are:\n"));
170 while ((catalog = (CAT *)GetNextRes(R_CATALOG, (RES *)catalog))) {
171 add_prompt(ua, catalog->hdr.name);
174 do_prompt(ua, _("Select Catalog resource"), name);
175 catalog = (CAT *)GetResWithName(R_CATALOG, name);
182 * Select a Job resource from prompt list
184 JOB *select_job_resource(UAContext *ua)
186 char name[MAX_NAME_LENGTH];
189 start_prompt(ua, _("The defined Job resources are:\n"));
191 while ( (job = (JOB *)GetNextRes(R_JOB, (RES *)job)) ) {
192 add_prompt(ua, job->hdr.name);
195 do_prompt(ua, _("Select Job resource"), name);
196 job = (JOB *)GetResWithName(R_JOB, name);
202 * Select a client resource from prompt list
204 CLIENT *select_client_resource(UAContext *ua)
206 char name[MAX_NAME_LENGTH];
207 CLIENT *client = NULL;
209 start_prompt(ua, _("The defined Client resources are:\n"));
211 while ( (client = (CLIENT *)GetNextRes(R_CLIENT, (RES *)client)) ) {
212 add_prompt(ua, client->hdr.name);
215 do_prompt(ua, _("Select Client (File daemon) resource"), name);
216 client = (CLIENT *)GetResWithName(R_CLIENT, name);
221 * Get client resource, start by looking for
222 * client=<client-name>
223 * if we don't find the keyword, we prompt the user.
225 CLIENT *get_client_resource(UAContext *ua)
227 CLIENT *client = NULL;
230 for (i=1; i<ua->argc; i++) {
231 if (strcasecmp(ua->argk[i], _("client")) == 0 && ua->argv[i]) {
232 client = (CLIENT *)GetResWithName(R_CLIENT, ua->argv[i]);
236 bsendmsg(ua, _("Error: Client resource %s does not exist.\n"), ua->argv[i]);
240 return select_client_resource(ua);
243 /* Scan what the user has entered looking for:
247 * if error or not found, put up a list of pool DBRs
250 * returns: 0 on error
251 * poolid on success and fills in POOL_DBR
253 int get_pool_dbr(UAContext *ua, POOL_DBR *pr)
257 for (i=1; i<ua->argc; i++) {
258 if (strcasecmp(ua->argk[i], _("pool")) == 0 && ua->argv[i]) {
259 strcpy(pr->Name, ua->argv[i]);
260 if (!db_get_pool_record(ua->db, pr)) {
261 bsendmsg(ua, _("Could not find Pool %s: ERR=%s"), ua->argv[i],
262 db_strerror(ua->db));
269 if (!select_pool_dbr(ua, pr)) { /* try once more */
276 * Select a Pool record from the catalog
278 int select_pool_dbr(UAContext *ua, POOL_DBR *pr)
281 char name[MAX_NAME_LENGTH];
287 if (!db_get_pool_ids(ua->db, &num_pools, &ids)) {
288 bsendmsg(ua, _("Error obtaining pool ids. ERR=%s\n"), db_strerror(ua->db));
291 if (num_pools <= 0) {
292 bsendmsg(ua, _("No pools defined. Use the \"create\" command to create one.\n"));
296 start_prompt(ua, _("Defined Pools:\n"));
297 for (i=0; i < num_pools; i++) {
299 if (!db_get_pool_record(ua->db, &opr)) {
302 add_prompt(ua, opr.Name);
305 if (do_prompt(ua, _("Select the Pool"), name) < 0) {
308 memset(&opr, 0, sizeof(pr));
309 strcpy(opr.Name, name);
311 if (!db_get_pool_record(ua->db, &opr)) {
312 bsendmsg(ua, _("Could not find Pool %s: ERR=%s"), name, db_strerror(ua->db));
315 memcpy(pr, &opr, sizeof(opr));
320 * Select a Pool and a Media (Volume) record from the database
322 int select_pool_and_media_dbr(UAContext *ua, POOL_DBR *pr, MEDIA_DBR *mr)
327 memset(pr, 0, sizeof(POOL_DBR));
328 memset(mr, 0, sizeof(MEDIA_DBR));
330 /* Get the pool, possibly from pool=<pool-name> */
331 if (!get_pool_dbr(ua, pr)) {
334 mr->PoolId = pr->PoolId;
336 /* See if a volume name is specified as an argument */
337 for (i=1; i<ua->argc; i++) {
338 if (strcasecmp(ua->argk[i], _("volume")) == 0 && ua->argv[i]) {
344 strcpy(mr->VolumeName, ua->argv[i]);
346 db_list_media_records(ua->db, mr, prtit, ua);
347 if (!get_cmd(ua, _("Enter the Volume name to delete: "))) {
350 strcpy(mr->VolumeName, ua->cmd);
353 if (!db_get_media_record(ua->db, mr)) {
354 bsendmsg(ua, "%s", db_strerror(ua->db));
362 * This routine is ONLY used in the create command.
363 * If you are thinking about using it, you
364 * probably want to use select_pool_dbr()
365 * or get_pool_dbr() above.
367 POOL *get_pool_resource(UAContext *ua)
370 char name[MAX_NAME_LENGTH];
373 for (i=1; i<ua->argc; i++) {
374 if (strcasecmp(ua->argk[i], _("pool")) == 0 && ua->argv[i]) {
375 pool = (POOL *)GetResWithName(R_POOL, ua->argv[i]);
379 bsendmsg(ua, _("Error: Pool resource %s does not exist.\n"), ua->argv[i]);
383 start_prompt(ua, _("The defined Pool resources are:\n"));
385 while ((pool = (POOL *)GetNextRes(R_POOL, (RES *)pool))) {
386 add_prompt(ua, pool->hdr.name);
389 do_prompt(ua, _("Select Pool resource"), name);
390 pool = (POOL *)GetResWithName(R_POOL, name);
395 * List all jobs and ask user to select one
397 int select_job_dbr(UAContext *ua, JOB_DBR *jr)
399 db_list_job_records(ua->db, jr, prtit, ua);
400 if (!get_cmd(ua, _("Enter the JobId to select: "))) {
403 jr->JobId = atoi(ua->cmd);
404 if (!db_get_job_record(ua->db, jr)) {
405 bsendmsg(ua, "%s", db_strerror(ua->db));
413 /* Scan what the user has entered looking for:
417 * if error or not found, put up a list of Jobs
420 * returns: 0 on error
421 * JobId on success and fills in JOB_DBR
423 int get_job_dbr(UAContext *ua, JOB_DBR *jr)
427 for (i=1; i<ua->argc; i++) {
428 if (strcasecmp(ua->argk[i], _("job")) == 0 && ua->argv[i]) {
430 strcpy(jr->Job, ua->argv[i]);
431 } else if (strcasecmp(ua->argk[i], _("jobid")) == 0 && ua->argv[i]) {
432 jr->JobId = atoi(ua->argv[i]);
436 if (!db_get_job_record(ua->db, jr)) {
437 bsendmsg(ua, _("Could not find Job %s: ERR=%s"), ua->argv[i],
438 db_strerror(ua->db));
445 if (!select_job_dbr(ua, jr)) { /* try once more */
455 * Implement unique set of prompts
457 void start_prompt(UAContext *ua, char *msg)
459 if (ua->max_prompts == 0) {
460 ua->max_prompts = 10;
461 ua->prompt = (char **) bmalloc(sizeof(char *) * ua->max_prompts);
464 ua->prompt[0] = bstrdup(msg);
468 * Add to prompts -- keeping them unique
470 void add_prompt(UAContext *ua, char *prompt)
473 if (ua->num_prompts == ua->max_prompts) {
474 ua->max_prompts *= 2;
475 ua->prompt = (char **) brealloc(ua->prompt, sizeof(char *) *
478 for (i=1; i < ua->num_prompts; i++) {
479 if (strcmp(ua->prompt[i], prompt) == 0) {
483 ua->prompt[ua->num_prompts++] = bstrdup(prompt);
487 * Display prompts and get user's choice
489 * Returns: -1 on error
490 * index base 0 on success, and choice
491 * is copied to prompt if not NULL
493 int do_prompt(UAContext *ua, char *msg, char *prompt)
496 char pmsg[MAXSTRING];
498 bsendmsg(ua, ua->prompt[0]);
499 for (i=1; i < ua->num_prompts; i++) {
500 bsendmsg(ua, "%6d: %s\n", i, ua->prompt[i]);
508 if (ua->num_prompts == 2) {
510 bsendmsg(ua, _("Item 1 selected automatically.\n"));
512 strcpy(prompt, ua->prompt[1]);
516 sprintf(pmsg, "%s (1-%d): ", msg, ua->num_prompts-1);
518 /* Either a . or an @ will get you out of the loop */
519 if (!get_cmd(ua, pmsg) || *ua->cmd == '.' || *ua->cmd == '@') {
520 item = -1; /* error */
523 item = atoi(ua->cmd);
524 if (item < 1 || item >= ua->num_prompts) {
525 bsendmsg(ua, _("Please enter a number between 1 and %d\n"), ua->num_prompts-1);
529 strcpy(prompt, ua->prompt[item]);
534 for (i=0; i < ua->num_prompts; i++) {
543 * We scan what the user has entered looking for
545 * device=<device-name> ???? does this work ????
546 * storage=<storage-resource>
549 * ? (prompt him with storage list)
550 * <some-error> (prompt him with storage list)
552 STORE *get_storage_resource(UAContext *ua, char *cmd)
554 char *store_name, *device_name;
561 return select_storage_resource(ua);
567 for (i=1; i<ua->argc; i++) {
569 /* Default argument is storage */
571 bsendmsg(ua, _("Storage name given twice.\n"));
574 store_name = ua->argk[i];
575 if (*store_name == '?') {
576 return select_storage_resource(ua);
579 if (strcasecmp(ua->argk[i], _("device")) == 0) {
580 device_name = ua->argv[i];
582 } else if (strcasecmp(ua->argk[i], _("storage")) == 0) {
583 store_name = ua->argv[i];
585 } else if (strcasecmp(ua->argk[i], _("jobid")) == 0) {
586 jobid = atoi(ua->argv[i]);
588 bsendmsg(ua, _("Expecting jobid=nn command, got: %s\n"), ua->argk[i]);
591 if (!(jcr=get_jcr_by_id(jobid))) {
592 bsendmsg(ua, _("JobId %d is not running.\n"), jobid);
599 } else if (strcasecmp(ua->argk[i], _("job")) == 0) {
600 if (!(jcr=get_jcr_by_partial_name(ua->argv[i]))) {
601 bsendmsg(ua, _("Job %s is not running.\n"), ua->argv[i]);
609 bsendmsg(ua, _("Unknown keyword: %s\n"), ua->argk[i]);
616 bsendmsg(ua, _("A storage device name must be given.\n"));
619 store = (STORE *)GetResWithName(R_STORAGE, store_name);
621 bsendmsg(ua, "Storage resource %s: not found\n", store_name);
625 store = select_storage_resource(ua);
632 * Scan looking for mediatype=
634 * if not found or error, put up selection list
636 * Returns: 0 on error
637 * 1 on success, MediaType is set
639 int get_media_type(UAContext *ua, char *MediaType)
643 static char *keyword[] = {
647 i = find_arg_keyword(ua, keyword);
648 if (i >= 0 && ua->argv[i]) {
649 strcpy(MediaType, ua->argv[i]);
653 start_prompt(ua, _("Media Types defined in conf file:\n"));
655 for (store = NULL; (store = (STORE *)GetNextRes(R_STORAGE, (RES *)store)); ) {
656 add_prompt(ua, store->media_type);
659 return (do_prompt(ua, _("Select the Media Type"), MediaType) < 0) ? 0 : 1;