]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_input.c
11Apr06
[bacula/bacula] / bacula / src / dird / ua_input.c
1 /*
2  *
3  *   Bacula Director -- User Agent Input and scanning code
4  *
5  *     Kern Sibbald, October MMI
6  *
7  *   Version $Id$
8  */
9 /*
10    Copyright (C) 2001-2005 Kern Sibbald
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License
14    version 2 as amended with additional clauses defined in the
15    file LICENSE in the main source directory.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
20    the file LICENSE for additional details.
21
22  */
23
24 #include "bacula.h"
25 #include "dird.h"
26
27
28 /* Imported variables */
29
30
31 /* Exported functions */
32
33 int get_cmd(UAContext *ua, const char *prompt)
34 {
35    BSOCK *sock = ua->UA_sock;
36    int stat;
37
38    ua->cmd[0] = 0;
39    if (!sock) {                       /* No UA */
40       return 0;
41    }
42    bnet_fsend(sock, "%s", prompt);
43    bnet_sig(sock, BNET_PROMPT);       /* request more input */
44    for ( ;; ) {
45       stat = bnet_recv(sock);
46       if (stat == BNET_SIGNAL) {
47          continue;                    /* ignore signals */
48       }
49       if (is_bnet_stop(sock)) {
50          return 0;                    /* error or terminate */
51       }
52       pm_strcpy(ua->cmd, sock->msg);
53       strip_trailing_junk(ua->cmd);
54       if (strcmp(ua->cmd, ".messages") == 0) {
55          qmessagescmd(ua, ua->cmd);
56       }
57       /* Lone dot => break */
58       if (ua->cmd[0] == '.' && ua->cmd[1] == 0) {
59          return 0;
60       }
61       break;
62    }
63    return 1;
64 }
65
66 /*
67  * Get a positive integer
68  *  Returns:  false if failure
69  *            true  if success => value in ua->pint32_val
70  */
71 bool get_pint(UAContext *ua, const char *prompt)
72 {
73    double dval;
74    ua->pint32_val = 0;
75    ua->int64_val = 0;
76    for (;;) {
77       ua->cmd[0] = 0;
78       if (!get_cmd(ua, prompt)) {
79          return false;
80       }
81       /* Kludge for slots blank line => 0 */
82       if (ua->cmd[0] == 0 && strncmp(prompt, _("Enter slot"), strlen(_("Enter slot"))) == 0) {
83          return true;
84       }
85       if (!is_a_number(ua->cmd)) {
86          bsendmsg(ua, _("Expected a positive integer, got: %s\n"), ua->cmd);
87          continue;
88       }
89       errno = 0;
90       dval = strtod(ua->cmd, NULL);
91       if (errno != 0 || dval < 0) {
92          bsendmsg(ua, _("Expected a positive integer, got: %s\n"), ua->cmd);
93          continue;
94       }
95       ua->pint32_val = (uint32_t)dval;
96       ua->int64_val = (int64_t)dval;
97       return true;
98    }
99 }
100
101 /*
102  * Gets a yes or no response
103  *  Returns:  false if failure
104  *            true  if success => ua->pint32_val == 1 for yes
105  *                                ua->pint32_val == 0 for no
106  */
107 bool get_yesno(UAContext *ua, const char *prompt)
108 {
109    int len;
110
111    ua->pint32_val = 0;
112    for (;;) {
113       if (!get_cmd(ua, prompt)) {
114          return false;
115       }
116       len = strlen(ua->cmd);
117       if (len < 1 || len > 3) {
118          continue;
119       }
120       if (strncasecmp(ua->cmd, _("yes"), len) == 0) {
121          ua->pint32_val = 1;
122          return true;
123       }
124       if (strncasecmp(ua->cmd, _("no"), len) == 0) {
125          return true;
126       }
127       bsendmsg(ua, _("Invalid response. You must answer yes or no.\n"));
128    }
129 }
130
131
132 void parse_ua_args(UAContext *ua)
133 {
134    parse_args(ua->cmd, &ua->args, &ua->argc, ua->argk, ua->argv, MAX_CMD_ARGS);
135 }