]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_input.c
restore cmd + misc -- see kes04Aug02
[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, 2001, 2002 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 #include "ua.h"
33
34
35 /* Imported variables */
36
37
38 /* Exported functions */
39
40 int get_cmd(UAContext *ua, char *prompt)
41 {
42    BSOCK *sock = ua->UA_sock;
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       if (bnet_recv(sock) < 0) {
52          return 0;
53       }
54       ua->cmd = (char *) check_pool_memory_size(ua->cmd, sock->msglen+1);
55       bstrncpy(ua->cmd, sock->msg, sock->msglen+1);
56       strip_trailing_junk(ua->cmd);
57       if (strcmp(ua->cmd, ".messages") == 0) {
58          qmessagescmd(ua, ua->cmd);
59       }
60       /* ****FIXME**** if .command, go off and do it. For now ignore it. */
61       if (ua->cmd[0] == '.' && ua->cmd[1] != 0) {
62          continue;                    /* dot command */
63       }
64       /* Lone dot => break or actual response */
65       break;
66    }
67    return 1;
68 }
69
70 /* 
71  * Return next argument from command line.  Note, this
72  * routine is destructive.
73  */
74 char *next_arg(char **s)
75 {
76    char *p, *q, *n;
77
78    Dmsg1(400, "Next arg=%s\n", *s);
79    /* skip past spaces to next arg */
80    for (p=*s; *p && *p == ' '; ) {
81       p++;
82    }    
83    /* Determine start of argument */
84    if (*p == '"') {
85       Dmsg0(400, "Start with quote.\n");
86       for (n = q = ++p; *p && *p != '"'; ) {
87          if (*p == '\\') {
88             p++;
89          }
90          *q++ = *p++;
91       }
92       p++;                            /* skip terminating quote */
93       for ( ; *p && *p != ' '; ) {
94          *q++ = *p++;
95       }
96       *q = 0;
97    } else {
98       /* Scan argment and terminate it */
99       n = p;
100       for ( ; *p && *p != ' '; ) {
101          p++;
102       }
103       if (*p == ' ') {
104          *p++ = 0;
105       }
106    }
107    *s = p;
108    Dmsg2(400, "End arg=%s next=%s\n", n, p);
109    return n;
110 }   
111
112 /*
113  * This routine parses the input command line.
114  * It makes a copy in args, then builds an
115  *  argc, argv like list where
116  *    
117  *  argc = count of arguments
118  *  argk[i] = argument keyword (part preceding =)
119  *  argv[i] = argument value (part after =)
120  *
121  *  example:  arg1 arg2=abc arg3=
122  *
123  *  argc = c
124  *  argk[0] = arg1
125  *  argv[0] = NULL
126  *  argk[1] = arg2
127  *  argv[1] = abc
128  *  argk[2] = arg3
129  *  argv[2] = 
130  */
131
132 void parse_command_args(UAContext *ua)
133 {
134    BSOCK *sock = ua->UA_sock;
135    char *p, *n;
136    int i;
137
138    ua->args = (char *) check_pool_memory_size(ua->args, sock->msglen+1);
139    bstrncpy(ua->args, sock->msg, sock->msglen+1);
140    strip_trailing_junk(ua->args);
141    ua->argc = 0;
142    p = ua->args;
143    /* Pick up all arguments */
144    while (ua->argc < MAX_ARGS) {
145       n = next_arg(&p);   
146       if (*n) {
147          ua->argk[ua->argc++] = n;
148       } else {
149          break;
150       }
151    }
152    /* Separate keyword and value */
153    for (i=0; i<ua->argc; i++) {
154       p = strchr(ua->argk[i], '=');
155       if (p) {
156          *p++ = 0;                    /* terminate keyword and point to value */
157          if (strlen(p) > MAX_NAME_LENGTH-1) {
158             p[MAX_NAME_LENGTH-1] = 0; /* truncate to max len */
159          }
160       }
161       ua->argv[i] = p;                /* save ptr to value or NULL */
162    }
163 #ifdef xxxx
164    for (i=0; i<ua->argc; i++) {
165       Dmsg3(000, "Arg %d: kw=%s val=%s\n", i, 
166          ua->argk[i], ua->argv[i]?ua->argv[i]:"NULL");
167    }
168 #endif
169 }