]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_input.c
OK, ANSI labels seem to be working now
[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 /*
11    Copyright (C) 2000-2004 Kern Sibbald and John Walker
12
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.
17
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.
22
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,
26    MA 02111-1307, USA.
27
28  */
29
30 #include "bacula.h"
31 #include "dird.h"
32
33
34 /* Imported variables */
35
36
37 /* Exported functions */
38
39 int get_cmd(UAContext *ua, const char *prompt)
40 {
41    BSOCK *sock = ua->UA_sock;
42    int stat;
43
44    ua->cmd[0] = 0;
45    if (!sock) {                       /* No UA */
46       return 0;
47    }
48    bnet_fsend(sock, "%s", prompt);
49    bnet_sig(sock, BNET_PROMPT);       /* request more input */
50    for ( ;; ) {
51       stat = bnet_recv(sock);
52       if (stat == BNET_SIGNAL) {
53          continue;                    /* ignore signals */
54       }
55       if (is_bnet_stop(sock)) {
56          return 0;                    /* error or terminate */
57       }
58       pm_strcpy(ua->cmd, sock->msg);
59       strip_trailing_junk(ua->cmd);
60       if (strcmp(ua->cmd, ".messages") == 0) {
61          qmessagescmd(ua, ua->cmd);
62       }
63       /* Lone dot => break */
64       if (ua->cmd[0] == '.' && ua->cmd[1] == 0) {
65          return 0;
66       }
67       break;
68    }
69    return 1;
70 }
71
72 /*
73  * Get a positive integer
74  *  Returns:  false if failure
75  *            true  if success => value in ua->pint32_val
76  */
77 bool get_pint(UAContext *ua, const char *prompt)
78 {
79    double dval;
80    ua->pint32_val = 0;
81    for (;;) {
82       ua->cmd[0] = 0;
83       if (!get_cmd(ua, prompt)) {
84          return false;
85       }
86       /* Kludge for slots blank line => 0 */
87       if (ua->cmd[0] == 0 && strncmp(prompt, "Enter slot", 10) == 0) {
88          ua->pint32_val = 0;
89          return true;
90       }
91       if (!is_a_number(ua->cmd)) {
92          bsendmsg(ua, "Expected a positive integer, got: %s\n", ua->cmd);
93          continue;
94       }
95       errno = 0;
96       dval = strtod(ua->cmd, NULL);
97       if (errno != 0 || dval < 0) {
98          bsendmsg(ua, "Expected a positive integer, got: %s\n", ua->cmd);
99          continue;
100       }
101       ua->pint32_val = (uint32_t)dval;
102       return true;
103    }
104 }
105
106 /*
107  * Gets a yes or no response
108  *  Returns:  false if failure
109  *            true  if success => ua->pint32_val == 1 for yes
110  *                                ua->pint32_val == 0 for no
111  */
112 bool get_yesno(UAContext *ua, const char *prompt)
113 {
114    int len;
115
116    ua->pint32_val = 0;
117    for (;;) {
118       if (!get_cmd(ua, prompt)) {
119          return false;
120       }
121       len = strlen(ua->cmd);
122       if (len < 1 || len > 3) {
123          continue;
124       }
125       if (strncasecmp(ua->cmd, _("yes"), len) == 0) {
126          ua->pint32_val = 1;
127          return true;
128       }
129       if (strncasecmp(ua->cmd, _("no"), len) == 0) {
130          return true;
131       }
132       bsendmsg(ua, _("Invalid response. You must answer yes or no.\n"));
133    }
134 }
135
136
137 void parse_ua_args(UAContext *ua)
138 {
139    parse_args(ua->cmd, &ua->args, &ua->argc, ua->argk, ua->argv, MAX_CMD_ARGS);
140 }