]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_input.c
This commit was manufactured by cvs2svn to create tag
[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    if (!sock) {                       /* No UA */
44       return 0;
45    }
46    bnet_fsend(sock, "%s", prompt);
47    bnet_sig(sock, BNET_PROMPT);       /* request more input */
48    for ( ;; ) {
49       if (bnet_recv(sock) < 0) {
50          return 0;
51       }
52       ua->cmd = (char *) check_pool_memory_size(ua->cmd, sock->msglen+1);
53       strcpy(ua->cmd, sock->msg);
54       ua->cmd[sock->msglen] = 0;
55       strip_trailing_junk(ua->cmd);
56       if (strcmp(ua->cmd, ".messages") == 0) {
57          qmessagescmd(ua, ua->cmd);
58       }
59       /* ****FIXME**** if .command, go off and do it. For now ignore it. */
60       if (ua->cmd[0] == '.' && ua->cmd[1] != 0) {
61          continue;                    /* dot command */
62       }
63       /* Lone dot => break or actual response */
64       break;
65    }
66    return 1;
67 }
68
69 /* 
70  * Return next argument from command line.  Note, this
71  * routine is destructive.
72  */
73 char *next_arg(char **s)
74 {
75    char *p, *n;
76
77    /* skip past spaces to next arg */
78    for (p=*s; *p && *p == ' '; p++)
79       {}
80    /* Determine start of argument */
81    if (*p == '"') {
82       n = p+1;                        /* skip leading quote */
83    } else {
84       n = p;
85    }
86    /* Scan argment and terminate it */
87    for ( ; *p && *p != ' '; p++) {
88       if (*p == '"') {
89          for (p++; *p && *p != '"'; p++) {
90             *(p-1) = *p;
91             *p = 0;
92          }
93          break;
94       }
95    }
96    if (*p) {                          /* if more arguments */
97       *p++ = 0;                       /* terminate this one */
98    }
99    *s = p;
100    return n;
101 }   
102
103 /*
104  * This routine parses the input command line.
105  * It makes a copy in args, then builds an
106  *  argc, argv like list where
107  *    
108  *  argc = count of arguments
109  *  argk[i] = argument keyword (part preceding =)
110  *  argv[i] = argument value (part after =)
111  *
112  *  example:  arg1 arg2=abc arg3=
113  *
114  *  argc = c
115  *  argk[0] = arg1
116  *  argv[0] = NULL
117  *  argk[1] = arg2
118  *  argv[1] = abc
119  *  argk[2] = arg3
120  *  argv[2] = 
121  */
122
123 void parse_command_args(UAContext *ua)
124 {
125    BSOCK *sock = ua->UA_sock;
126    char *p, *n;
127    int i;
128
129    ua->args = (char *) check_pool_memory_size(ua->args, sock->msglen+1);
130    strcpy(ua->args, sock->msg);
131    ua->args[sock->msglen] = 0;
132    strip_trailing_junk(ua->args);
133    ua->argc = 0;
134    p = ua->args;
135    /* Pick up all arguments */
136    while (ua->argc < MAX_ARGS) {
137       n = next_arg(&p);   
138       if (*n) {
139          ua->argk[ua->argc++] = n;
140       } else {
141          break;
142       }
143    }
144    /* Separate keyword and value */
145    for (i=0; i<ua->argc; i++) {
146       p = strchr(ua->argk[i], '=');
147       if (p) {
148          *p++ = 0;                    /* terminate keyword and point to value */
149          if (strlen(p) > MAX_NAME_LENGTH-1) {
150             p[MAX_NAME_LENGTH-1] = 0; /* truncate to max len */
151          }
152       }
153       ua->argv[i] = p;                /* save ptr to value or NULL */
154    }
155 #ifdef xxxxxxxxx
156    for (i=0; i<ua->argc; i++) {
157       Dmsg3(000, "Arg %d: kw=%s val=%s\n", i, 
158          ua->argk[i], ua->argv[i]?ua->argv[i]:"NULL");
159    }
160 #endif
161 }