3 * Bacula Director -- User Agent Input and scanning 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 get_cmd(UAContext *ua, char *prompt)
42 BSOCK *sock = ua->UA_sock;
46 if (!sock) { /* No UA */
49 bnet_fsend(sock, "%s", prompt);
50 bnet_sig(sock, BNET_PROMPT); /* request more input */
52 stat = bnet_recv(sock);
53 if (stat == BNET_SIGNAL) {
54 continue; /* ignore signals */
56 if (is_bnet_stop(sock)) {
57 return 0; /* error or terminate */
59 ua->cmd = check_pool_memory_size(ua->cmd, sock->msglen+1);
60 bstrncpy(ua->cmd, sock->msg, sock->msglen+1);
61 strip_trailing_junk(ua->cmd);
62 if (strcmp(ua->cmd, ".messages") == 0) {
63 qmessagescmd(ua, ua->cmd);
65 /* ****FIXME**** if .command, go off and do it. For now ignore it. */
66 if (ua->cmd[0] == '.' && ua->cmd[1] != 0) {
67 continue; /* dot command */
69 /* Lone dot => break or actual response */
76 * Return next argument from command line. Note, this
77 * routine is destructive.
79 char *next_arg(char **s)
84 /* skip past spaces to next arg */
85 for (p=*s; *p && *p == ' '; ) {
88 Dmsg1(400, "Next arg=%s\n", p);
89 for (n = q = p; *p ; ) {
99 if (*p == '"') { /* start or end of quote */
101 p++; /* skip quote */
109 if (!in_quote && *p == ' ') { /* end of field */
117 Dmsg2(400, "End arg=%s next=%s\n", n, p);
122 * This routine parses the input command line.
123 * It makes a copy in args, then builds an
124 * argc, argv like list where
126 * argc = count of arguments
127 * argk[i] = argument keyword (part preceding =)
128 * argv[i] = argument value (part after =)
130 * example: arg1 arg2=abc arg3=
141 void parse_command_args(UAContext *ua)
146 len = strlen(ua->cmd) + 1;
147 ua->args = check_pool_memory_size(ua->args, len);
148 bstrncpy(ua->args, ua->cmd, len);
149 strip_trailing_junk(ua->args);
152 /* Pick up all arguments */
153 while (ua->argc < MAX_ARGS) {
156 ua->argk[ua->argc++] = n;
161 /* Separate keyword and value */
162 for (i=0; i<ua->argc; i++) {
163 p = strchr(ua->argk[i], '=');
165 *p++ = 0; /* terminate keyword and point to value */
166 /* Unquote quoted values */
168 Dmsg1(000, "Start with quote: %s\n", p);
169 for (n = q = ++p; *p && *p != '"'; ) {
175 *q = 0; /* terminate string */
176 p = n; /* point to string */
178 if (strlen(p) > MAX_NAME_LENGTH-1) {
179 p[MAX_NAME_LENGTH-1] = 0; /* truncate to max len */
182 ua->argv[i] = p; /* save ptr to value or NULL */
185 for (i=0; i<ua->argc; i++) {
186 Dmsg3(000, "Arg %d: kw=%s val=%s\n", i,
187 ua->argk[i], ua->argv[i]?ua->argv[i]:"NULL");