]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_input.c
o add new is_yesno() function. This function test argument against NT_("yes") and...
[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  * Test a yes or no response
103  *  Returns:  false if failure
104  *            true  if success => ret == 1 for yes
105  *                                ret == 0 for no
106  */
107 bool is_yesno(char *val, int *ret)
108 {
109    *ret = 0;
110    if ((strcasecmp(val,   _("yes")) == 0) ||
111        (strcasecmp(val, NT_("yes")) == 0))
112    {
113       *ret = 1;
114    } else if ((strcasecmp(val,   _("no")) == 0) ||
115               (strcasecmp(val, NT_("no")) == 0))
116    {
117       *ret = 0;
118    } else {
119       return false;
120    }
121
122    return true;
123 }
124
125 /*
126  * Gets a yes or no response
127  *  Returns:  false if failure
128  *            true  if success => ua->pint32_val == 1 for yes
129  *                                ua->pint32_val == 0 for no
130  */
131 bool get_yesno(UAContext *ua, const char *prompt)
132 {
133    int len;
134    int ret;
135    ua->pint32_val = 0;
136    for (;;) {
137       if (!get_cmd(ua, prompt)) {
138          return false;
139       }
140       len = strlen(ua->cmd);
141       if (len < 1 || len > 3) {
142          continue;
143       }
144       if (is_yesno(ua->cmd, &ret)) {
145          ua->pint32_val = ret;
146          return true;
147       }
148       bsendmsg(ua, _("Invalid response. You must answer yes or no.\n"));
149    }
150 }
151
152 void parse_ua_args(UAContext *ua)
153 {
154    parse_args(ua->cmd, &ua->args, &ua->argc, ua->argk, ua->argv, MAX_CMD_ARGS);
155 }