]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/scan.c
Add Peter Eriksson's const code + turn bsscanf code
[bacula/bacula] / bacula / src / lib / scan.c
1 /*
2  *   scan.c -- scanning routines for Bacula
3  * 
4  *    Kern Sibbald, MM  separated from util.c MMIII
5  *
6  *   Version $Id$
7  */
8
9 /*
10    Copyright (C) 2000-2004 Kern Sibbald and John Walker
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 as
14    published by the Free Software Foundation; either version 2 of
15    the License, or (at your option) any later version.
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 GNU
20    General Public License for more details.
21
22    You should have received a copy of the GNU General Public
23    License along with this program; if not, write to the Free
24    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"
30 #include "jcr.h"
31 #include "findlib/find.h"
32
33
34 /* Strip any trailing junk from the command */
35 void strip_trailing_junk(char *cmd)
36 {
37    char *p;
38    p = cmd + strlen(cmd) - 1;
39
40    /* strip trailing junk from command */
41    while ((p >= cmd) && (*p == '\n' || *p == '\r' || *p == ' '))
42       *p-- = 0;
43 }
44
45 /* Strip any trailing slashes from a directory path */
46 void strip_trailing_slashes(char *dir)
47 {
48    char *p;
49    p = dir + strlen(dir) - 1;
50
51    /* strip trailing slashes */
52    while ((p >= dir) && (*p == '/'))
53       *p-- = 0;
54 }
55
56 /*
57  * Skip spaces
58  *  Returns: 0 on failure (EOF)             
59  *           1 on success
60  *           new address in passed parameter 
61  */
62 bool skip_spaces(char **msg)
63 {
64    char *p = *msg;
65    if (!p) {
66       return false;
67    }
68    while (*p && B_ISSPACE(*p)) {
69       p++;
70    }
71    *msg = p;
72    return *p ? true : false;
73 }
74
75 /*
76  * Skip nonspaces
77  *  Returns: 0 on failure (EOF)             
78  *           1 on success
79  *           new address in passed parameter 
80  */
81 bool skip_nonspaces(char **msg)
82 {
83    char *p = *msg;
84
85    if (!p) {
86       return false;
87    }
88    while (*p && !B_ISSPACE(*p)) {
89       p++;
90    }
91    *msg = p;
92    return *p ? true : false;
93 }
94
95 /* folded search for string - case insensitive */
96 int
97 fstrsch(const char *a, const char *b)   /* folded case search */
98 {
99    const char *s1,*s2;
100    char c1, c2;
101
102    s1=a;
103    s2=b;
104    while (*s1) {                      /* do it the fast way */
105       if ((*s1++ | 0x20) != (*s2++ | 0x20))
106          return 0;                    /* failed */
107    }
108    while (*a) {                       /* do it over the correct slow way */
109       if (B_ISUPPER(c1 = *a)) {
110          c1 = tolower((int)c1);
111       }
112       if (B_ISUPPER(c2 = *b)) {
113          c2 = tolower((int)c2);
114       }
115       if (c1 != c2) {
116          return 0;
117       }
118       a++;
119       b++;
120    }
121    return 1;
122 }
123
124
125 /* 
126  * Return next argument from command line.  Note, this
127  * routine is destructive.
128  */
129 char *next_arg(char **s)
130 {
131    char *p, *q, *n;
132    bool in_quote = false;
133
134    /* skip past spaces to next arg */
135    for (p=*s; *p && B_ISSPACE(*p); ) {
136       p++;
137    }    
138    Dmsg1(400, "Next arg=%s\n", p);
139    for (n = q = p; *p ; ) {
140       if (*p == '\\') {
141          p++;
142          if (*p) {
143             *q++ = *p++;
144          } else {
145             *q++ = *p;
146          }
147          continue;
148       }
149       if (*p == '"') {                  /* start or end of quote */
150          if (in_quote) {
151             p++;                        /* skip quote */
152             in_quote = false;
153             continue;
154          }
155          in_quote = true;
156          p++;
157          continue;
158       }
159       if (!in_quote && B_ISSPACE(*p)) {     /* end of field */
160          p++;
161          break;
162       }
163       *q++ = *p++;
164    }
165    *q = 0;
166    *s = p;
167    Dmsg2(400, "End arg=%s next=%s\n", n, p);
168    return n;
169 }   
170
171 /*
172  * This routine parses the input command line.
173  * It makes a copy in args, then builds an
174  *  argc, argv like list where
175  *    
176  *  argc = count of arguments
177  *  argk[i] = argument keyword (part preceding =)
178  *  argv[i] = argument value (part after =)
179  *
180  *  example:  arg1 arg2=abc arg3=
181  *
182  *  argc = c
183  *  argk[0] = arg1
184  *  argv[0] = NULL
185  *  argk[1] = arg2
186  *  argv[1] = abc
187  *  argk[2] = arg3
188  *  argv[2] = 
189  */
190
191 int parse_args(POOLMEM *cmd, POOLMEM **args, int *argc, 
192                char **argk, char **argv, int max_args) 
193 {
194    char *p, *q, *n;
195
196    pm_strcpy(args, cmd);
197    strip_trailing_junk(*args);
198    p = *args;
199    *argc = 0;
200    /* Pick up all arguments */
201    while (*argc < max_args) {
202       n = next_arg(&p);   
203       if (*n) {
204          argk[*argc] = n;
205          argv[(*argc)++] = NULL;
206       } else {
207          break;
208       }
209    }
210    /* Separate keyword and value */
211    for (int i=0; i < *argc; i++) {
212       p = strchr(argk[i], '=');
213       if (p) {
214          *p++ = 0;                    /* terminate keyword and point to value */
215          /* Unquote quoted values */
216          if (*p == '"') {
217             for (n = q = ++p; *p && *p != '"'; ) {
218                if (*p == '\\') {
219                   p++;
220                }
221                *q++ = *p++;
222             }
223             *q = 0;                   /* terminate string */
224             p = n;                    /* point to string */
225          }
226          if (strlen(p) > MAX_NAME_LENGTH-1) {
227             p[MAX_NAME_LENGTH-1] = 0; /* truncate to max len */
228          }
229       }
230       argv[i] = p;                    /* save ptr to value or NULL */
231    }
232 #ifdef xxxx
233    for (int i=0; i < *argc; i++) {
234       Pmsg3(000, "Arg %d: kw=%s val=%s\n", i, argk[i], argv[i]?argv[i]:"NULL");
235    }
236 #endif
237    return 1;
238 }
239
240 /*
241  * Given a full filename, split it into its path
242  *  and filename parts. They are returned in pool memory
243  *  in the arguments provided.
244  */
245 void split_path_and_filename(const char *fname, POOLMEM **path, int *pnl,
246         POOLMEM **file, int *fnl)
247 {
248    const char *p, *f;
249    int slen;
250    int len = slen = strlen(fname);
251
252    /*
253     * Find path without the filename.  
254     * I.e. everything after the last / is a "filename".
255     * OK, maybe it is a directory name, but we treat it like
256     * a filename. If we don't find a / then the whole name
257     * must be a path name (e.g. c:).
258     */
259    p = fname;
260    f = fname + len - 1;
261    /* "strip" any trailing slashes */
262    while (slen > 1 && *f == '/') {
263       slen--;
264       f--;
265    }
266    /* Walk back to last slash -- begin of filename */
267    while (slen > 0 && *f != '/') {
268       slen--;
269       f--;
270    }
271    if (*f == '/') {                   /* did we find a slash? */
272       f++;                            /* yes, point to filename */
273    } else {                           /* no, whole thing must be path name */
274       f = fname;
275    }
276    Dmsg2(200, "after strip len=%d f=%s\n", len, f);
277    *fnl = fname - f + len;
278    if (*fnl > 0) {
279       *file = check_pool_memory_size(*file, *fnl+1);
280       memcpy(*file, f, *fnl);    /* copy filename */
281    }
282    (*file)[*fnl] = 0;
283
284    *pnl = f - fname;
285    if (*pnl > 0) {
286       *path = check_pool_memory_size(*path, *pnl+1);
287       memcpy(*path, fname, *pnl);
288    }
289    (*path)[*pnl] = 0;
290
291    Dmsg2(200, "pnl=%d fnl=%d\n", *pnl, *fnl);
292    Dmsg3(200, "split fname=%s path=%s file=%s\n", fname, *path, *file);
293 }
294
295 /*
296  * Extremely simple sscanf. Handles only %(u,d,ld,lu,lld,llu,c,nns) 
297  */
298 const int BIG = 1000;
299 int bsscanf(const char *buf, const char *fmt, ...)
300 {
301    va_list ap;
302    int count = 0;
303    void *vp;
304    char *cp;
305    int l = 0;
306    int max_len = BIG;
307    uint64_t value;
308    bool error = false;
309
310    va_start(ap, fmt);
311    while (*fmt && !error) {
312 //    Dmsg1(000, "fmt=%c\n", *fmt);
313       if (*fmt == '%') {
314          fmt++;
315 //       Dmsg1(000, "Got %% nxt=%c\n", *fmt);
316 switch_top:
317          switch (*fmt++) {
318          case 'u':
319          case 'd':
320             value = 0;
321             while (B_ISDIGIT(*buf)) {
322                value = B_TIMES10(value) + *buf++ - '0';
323             }
324             vp = (void *)va_arg(ap, void *);
325 //          Dmsg2(000, "val=%lld at 0x%lx\n", value, (long unsigned)vp);
326             if (l < 2) {
327                *((uint32_t *)vp) = (uint32_t)value;
328 //             Dmsg0(000, "Store 32 bit int\n");
329             } else {
330                *((uint64_t *)vp) = (uint64_t)value;
331 //             Dmsg0(000, "Store 64 bit int\n");
332             }
333             count++;
334             l = 0;
335             break;
336          case 'l':
337 //          Dmsg0(000, "got l\n");
338             l = 1;
339             if (*fmt == 'l') {
340                l++;
341                fmt++;
342             }
343             if (*fmt == 'd' || *fmt == 'u') {
344                goto switch_top;
345             }
346 //          Dmsg1(000, "fmt=%c !=d,u\n", *fmt);
347             error = true;
348             break;
349          case 'q':
350             l = 2;
351             if (*fmt == 'd' || *fmt == 'u') {
352                goto switch_top;
353             }
354 //          Dmsg1(000, "fmt=%c !=d,u\n", *fmt);
355             error = true;
356             break;
357          case 's':
358 //          Dmsg1(000, "Store string max_len=%d\n", max_len);
359             cp = (char *)va_arg(ap, char *);
360             while (*buf && !B_ISSPACE(*buf) && max_len-- > 0) {
361                *cp++ = *buf++;
362             }
363             *cp = 0;
364             count++;
365             max_len = BIG;
366             break;
367          case 'c':
368             cp = (char *)va_arg(ap, char *);
369             *cp = *buf++;
370             count++;
371             break;
372          case '%':
373             if (*buf++ != '%') {
374                error = true;
375             }
376             break;
377          default:
378             fmt--;
379             max_len = 0;
380             while (B_ISDIGIT(*fmt)) {
381                max_len = B_TIMES10(max_len) + *fmt++ - '0';
382             }
383 //          Dmsg1(000, "Default max_len=%d\n", max_len);
384             if (*fmt == 's') {
385                goto switch_top;
386             }
387 //          Dmsg1(000, "Default c=%c\n", *fmt);
388             error = true;
389             break;                    /* error: unknown format */
390          }
391          continue;
392
393       /* White space eats zero or more whitespace */
394       } else if (B_ISSPACE(*fmt)) {
395          fmt++;
396          while (B_ISSPACE(*buf)) {
397             buf++;
398          }
399       /* Plain text must match */
400       } else if (*buf++ != *fmt++) {
401 //       Dmsg2(000, "Mismatch buf=%c fmt=%c\n", *--buf, *--fmt);
402          error = true;
403       }
404    }
405    va_end(ap);
406 // Dmsg2(000, "Error=%d count=%d\n", error, count);
407    if (error) {
408       count = -1;
409    }
410    return count;
411 }
412
413 #ifdef TEST_PROGRAM
414 int main(int argc, char *argv[])
415 {
416    char buf[100];
417    uint32_t val32;
418    uint64_t val64;
419    uint32_t FirstIndex, LastIndex, StartFile, EndFile, StartBlock, EndBlock;
420    char Job[200];
421    int cnt;
422    char *helloreq= "Hello *UserAgent* calling\n";
423    char *hello = "Hello %127s calling\n";
424    char *catreq = 
425 "CatReq Job=NightlySave.2004-06-11_19.11.32 CreateJobMedia FirstIndex=1 LastIndex=114 StartFile=0 EndFile=0 StartBlock=208 EndBlock=2903248";
426 static char Create_job_media[] = "CatReq Job=%127s CreateJobMedia "
427   "FirstIndex=%u LastIndex=%u StartFile=%u EndFile=%u "
428   "StartBlock=%u EndBlock=%u\n";
429 static char OK_media[] = "1000 OK VolName=%127s VolJobs=%u VolFiles=%u"
430    " VolBlocks=%u VolBytes=%" lld " VolMounts=%u VolErrors=%u VolWrites=%u"
431    " MaxVolBytes=%" lld " VolCapacityBytes=%" lld " VolStatus=%20s"
432    " Slot=%d MaxVolJobs=%u MaxVolFiles=%u InChanger=%d"
433    " VolReadTime=%" lld " VolWriteTime=%" lld;
434    char *media =
435 "1000 OK VolName=TestVolume001 VolJobs=0 VolFiles=0 VolBlocks=0 VolBytes=1 VolMounts=0 VolErrors=0 VolWrites=0 MaxVolBytes=0 VolCapacityBytes=0 VolStatus=Append Slot=0 MaxVolJobs=0 MaxVolFiles=0 InChanger=1 VolReadTime=0 VolWriteTime=0";
436 struct VOLUME_CAT_INFO {
437    /* Media info for the current Volume */
438    uint32_t VolCatJobs;               /* number of jobs on this Volume */
439    uint32_t VolCatFiles;              /* Number of files */
440    uint32_t VolCatBlocks;             /* Number of blocks */
441    uint64_t VolCatBytes;              /* Number of bytes written */
442    uint32_t VolCatMounts;             /* Number of mounts this volume */
443    uint32_t VolCatErrors;             /* Number of errors this volume */
444    uint32_t VolCatWrites;             /* Number of writes this volume */
445    uint32_t VolCatReads;              /* Number of reads this volume */
446    uint64_t VolCatRBytes;             /* Number of bytes read */
447    uint32_t VolCatRecycles;           /* Number of recycles this volume */
448    int32_t  Slot;                     /* Slot in changer */
449    bool     InChanger;                /* Set if vol in current magazine */
450    uint32_t VolCatMaxJobs;            /* Maximum Jobs to write to volume */
451    uint32_t VolCatMaxFiles;           /* Maximum files to write to volume */
452    uint64_t VolCatMaxBytes;           /* Max bytes to write to volume */
453    uint64_t VolCatCapacityBytes;      /* capacity estimate */
454    uint64_t VolReadTime;              /* time spent reading */
455    uint64_t VolWriteTime;             /* time spent writing this Volume */
456    char VolCatStatus[20];             /* Volume status */
457    char VolCatName[MAX_NAME_LENGTH];  /* Desired volume to mount */
458 };                
459    struct VOLUME_CAT_INFO vol;
460
461 #ifdef xxx
462    bsscanf("Hello_world 123 1234", "%120s %ld %lld", buf, &val32, &val64);
463    printf("%s %d %lld\n", buf, val32, val64);
464
465    *Job=0;
466    cnt = bsscanf(catreq, Create_job_media, &Job,
467       &FirstIndex, &LastIndex, &StartFile, &EndFile,
468       &StartBlock, &EndBlock);
469    printf("cnt=%d Job=%s\n", cnt, Job);
470    cnt = bsscanf(helloreq, hello, &Job);
471    printf("cnt=%d Agent=%s\n", cnt, Job);
472 #endif
473    cnt = bsscanf(media, OK_media,
474                vol.VolCatName,
475                &vol.VolCatJobs, &vol.VolCatFiles,
476                &vol.VolCatBlocks, &vol.VolCatBytes,
477                &vol.VolCatMounts, &vol.VolCatErrors,
478                &vol.VolCatWrites, &vol.VolCatMaxBytes,
479                &vol.VolCatCapacityBytes, vol.VolCatStatus,
480                &vol.Slot, &vol.VolCatMaxJobs, &vol.VolCatMaxFiles,
481                &vol.InChanger, &vol.VolReadTime, &vol.VolWriteTime);
482    printf("cnt=%d Vol=%s\n", cnt, vol.VolCatName);
483
484 }
485
486 #endif