]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_input.c
bd88cceb2b5c6d14c10a1bd91a5a82193c89736b
[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
8 /*
9    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
10
11    This program is free software; you can redistribute it and/or
12    modify it under the terms of the GNU General Public License as
13    published by the Free Software Foundation; either version 2 of
14    the License, or (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19    General Public License for more details.
20
21    You should have received a copy of the GNU General Public
22    License along with this program; if not, write to the Free
23    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24    MA 02111-1307, USA.
25
26  */
27
28 #include "bacula.h"
29 #include "dird.h"
30 #include "ua.h"
31
32
33 /* Imported variables */
34
35
36 /* Exported functions */
37
38 int get_cmd(UAContext *ua, char *prompt)
39 {
40    BSOCK *sock = ua->UA_sock;
41
42    ua->cmd[0] = 0;
43    bnet_fsend(sock, "%s", prompt);
44    bnet_sig(sock, BNET_PROMPT);       /* request more input */
45    for ( ;; ) {
46       if (bnet_recv(sock) < 0) {
47          return 0;
48       }
49       ua->cmd = (char *) check_pool_memory_size(ua->cmd, sock->msglen+1);
50       strcpy(ua->cmd, sock->msg);
51       ua->cmd[sock->msglen] = 0;
52       strip_trailing_junk(ua->cmd);
53       if (strcmp(ua->cmd, ".messages") == 0) {
54          qmessagescmd(ua, ua->cmd);
55       }
56       /* ****FIXME**** if .command, go off and do it. For now ignore it. */
57       if (ua->cmd[0] == '.' && ua->cmd[1] != 0) {
58          continue;                    /* dot command */
59       }
60       /* Lone dot => break or actual response */
61       break;
62    }
63    return 1;
64 }
65
66 /* 
67  * Return next argument from command line.  Note, this
68  * routine is destructive.
69  */
70 char *next_arg(char **s)
71 {
72    char *p, *n;
73
74    /* skip past spaces to next arg */
75    for (p=*s; *p && *p == ' '; p++)
76       {}
77    /* Determine start of argument */
78    if (*p == '"') {
79       n = p+1;                        /* skip leading quote */
80    } else {
81       n = p;
82    }
83    /* Scan argment and terminate it */
84    for ( ; *p && *p != ' '; p++) {
85       if (*p == '"') {
86          for (p++; *p && *p != '"'; p++) {
87             *(p-1) = *p;
88             *p = 0;
89          }
90          break;
91       }
92    }
93    if (*p) {                          /* if more arguments */
94       *p++ = 0;                       /* terminate this one */
95    }
96    *s = p;
97    return n;
98 }   
99
100 /*
101  * This routine parses the input command line.
102  * It makes a copy in args, then builds an
103  *  argc, argv like list where
104  *    
105  *  argc = count of arguments
106  *  argk[i] = argument keyword (part preceding =)
107  *  argv[i] = argument value (part after =)
108  *
109  *  example:  arg1 arg2=abc arg3=
110  *
111  *  argc = c
112  *  argk[0] = arg1
113  *  argv[0] = NULL
114  *  argk[1] = arg2
115  *  argv[1] = abc
116  *  argk[2] = arg3
117  *  argv[2] = 
118  */
119
120 void parse_command_args(UAContext *ua)
121 {
122    BSOCK *sock = ua->UA_sock;
123    char *p, *n;
124    int i;
125
126    ua->args = (char *) check_pool_memory_size(ua->args, sock->msglen+1);
127    strcpy(ua->args, sock->msg);
128    ua->args[sock->msglen] = 0;
129    strip_trailing_junk(ua->args);
130    ua->argc = 0;
131    p = ua->args;
132    /* Pick up all arguments */
133    while (ua->argc < MAX_ARGS) {
134       n = next_arg(&p);   
135       if (*n) {
136          ua->argk[ua->argc++] = n;
137       } else {
138          break;
139       }
140    }
141    /* Separate keyword and value */
142    for (i=0; i<ua->argc; i++) {
143       p = strchr(ua->argk[i], '=');
144       if (p) {
145          *p++ = 0;                    /* terminate keyword and point to value */
146          if (strlen(p) > MAX_NAME_LENGTH-1) {
147             p[MAX_NAME_LENGTH-1] = 0; /* truncate to max len */
148          }
149       }
150       ua->argv[i] = p;                /* save ptr to value or NULL */
151    }
152 #ifdef xxxxxxxxx
153    for (i=0; i<ua->argc; i++) {
154       Dmsg3(000, "Arg %d: kw=%s val=%s\n", i, 
155          ua->argk[i], ua->argv[i]?ua->argv[i]:"NULL");
156    }
157 #endif
158 }