]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/scan.c
Doc + compiler fixes
[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 *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    f = fname + len - 1;
260    /* "strip" any trailing slashes */
261    while (slen > 1 && *f == '/') {
262       slen--;
263       f--;
264    }
265    /* Walk back to last slash -- begin of filename */
266    while (slen > 0 && *f != '/') {
267       slen--;
268       f--;
269    }
270    if (*f == '/') {                   /* did we find a slash? */
271       f++;                            /* yes, point to filename */
272    } else {                           /* no, whole thing must be path name */
273       f = fname;
274    }
275    Dmsg2(200, "after strip len=%d f=%s\n", len, f);
276    *fnl = fname - f + len;
277    if (*fnl > 0) {
278       *file = check_pool_memory_size(*file, *fnl+1);
279       memcpy(*file, f, *fnl);    /* copy filename */
280    }
281    (*file)[*fnl] = 0;
282
283    *pnl = f - fname;
284    if (*pnl > 0) {
285       *path = check_pool_memory_size(*path, *pnl+1);
286       memcpy(*path, fname, *pnl);
287    }
288    (*path)[*pnl] = 0;
289
290    Dmsg2(200, "pnl=%d fnl=%d\n", *pnl, *fnl);
291    Dmsg3(200, "split fname=%s path=%s file=%s\n", fname, *path, *file);
292 }
293
294 /*
295  * Extremely simple sscanf. Handles only %(u,d,ld,lu,lld,llu,c,nns) 
296  */
297 const int BIG = 1000;
298 int bsscanf(const char *buf, const char *fmt, ...)
299 {
300    va_list ap;
301    int count = 0;
302    void *vp;
303    char *cp;
304    int l = 0;
305    int max_len = BIG;
306    uint64_t value;
307    bool error = false;
308
309    va_start(ap, fmt);
310    while (*fmt && !error) {
311 //    Dmsg1(000, "fmt=%c\n", *fmt);
312       if (*fmt == '%') {
313          fmt++;
314 //       Dmsg1(000, "Got %% nxt=%c\n", *fmt);
315 switch_top:
316          switch (*fmt++) {
317          case 'u':
318          case 'd':
319             value = 0;
320             while (B_ISDIGIT(*buf)) {
321                value = B_TIMES10(value) + *buf++ - '0';
322             }
323             vp = (void *)va_arg(ap, void *);
324 //          Dmsg2(000, "val=%lld at 0x%lx\n", value, (long unsigned)vp);
325             if (l < 2) {
326                *((uint32_t *)vp) = (uint32_t)value;
327 //             Dmsg0(000, "Store 32 bit int\n");
328             } else {
329                *((uint64_t *)vp) = (uint64_t)value;
330 //             Dmsg0(000, "Store 64 bit int\n");
331             }
332             count++;
333             l = 0;
334             break;
335          case 'l':
336 //          Dmsg0(000, "got l\n");
337             l = 1;
338             if (*fmt == 'l') {
339                l++;
340                fmt++;
341             }
342             if (*fmt == 'd' || *fmt == 'u') {
343                goto switch_top;
344             }
345 //          Dmsg1(000, "fmt=%c !=d,u\n", *fmt);
346             error = true;
347             break;
348          case 'q':
349             l = 2;
350             if (*fmt == 'd' || *fmt == 'u') {
351                goto switch_top;
352             }
353 //          Dmsg1(000, "fmt=%c !=d,u\n", *fmt);
354             error = true;
355             break;
356          case 's':
357 //          Dmsg1(000, "Store string max_len=%d\n", max_len);
358             cp = (char *)va_arg(ap, char *);
359             while (*buf && !B_ISSPACE(*buf) && max_len-- > 0) {
360                *cp++ = *buf++;
361             }
362             *cp = 0;
363             count++;
364             max_len = BIG;
365             break;
366          case 'c':
367             cp = (char *)va_arg(ap, char *);
368             *cp = *buf++;
369             count++;
370             break;
371          case '%':
372             if (*buf++ != '%') {
373                error = true;
374             }
375             break;
376          default:
377             fmt--;
378             max_len = 0;
379             while (B_ISDIGIT(*fmt)) {
380                max_len = B_TIMES10(max_len) + *fmt++ - '0';
381             }
382 //          Dmsg1(000, "Default max_len=%d\n", max_len);
383             if (*fmt == 's') {
384                goto switch_top;
385             }
386 //          Dmsg1(000, "Default c=%c\n", *fmt);
387             error = true;
388             break;                    /* error: unknown format */
389          }
390          continue;
391
392       /* White space eats zero or more whitespace */
393       } else if (B_ISSPACE(*fmt)) {
394          fmt++;
395          while (B_ISSPACE(*buf)) {
396             buf++;
397          }
398       /* Plain text must match */
399       } else if (*buf++ != *fmt++) {
400 //       Dmsg2(000, "Mismatch buf=%c fmt=%c\n", *--buf, *--fmt);
401          error = true;
402       }
403    }
404    va_end(ap);
405 // Dmsg2(000, "Error=%d count=%d\n", error, count);
406    if (error) {
407       count = -1;
408    }
409    return count;
410 }
411
412 #ifdef TEST_PROGRAM
413 int main(int argc, char *argv[])
414 {
415    char buf[100];
416    uint32_t val32;
417    uint64_t val64;
418    uint32_t FirstIndex, LastIndex, StartFile, EndFile, StartBlock, EndBlock;
419    char Job[200];
420    int cnt;
421    char *helloreq= "Hello *UserAgent* calling\n";
422    char *hello = "Hello %127s calling\n";
423    char *catreq = 
424 "CatReq Job=NightlySave.2004-06-11_19.11.32 CreateJobMedia FirstIndex=1 LastIndex=114 StartFile=0 EndFile=0 StartBlock=208 EndBlock=2903248";
425 static char Create_job_media[] = "CatReq Job=%127s CreateJobMedia "
426   "FirstIndex=%u LastIndex=%u StartFile=%u EndFile=%u "
427   "StartBlock=%u EndBlock=%u\n";
428 static char OK_media[] = "1000 OK VolName=%127s VolJobs=%u VolFiles=%u"
429    " VolBlocks=%u VolBytes=%" lld " VolMounts=%u VolErrors=%u VolWrites=%u"
430    " MaxVolBytes=%" lld " VolCapacityBytes=%" lld " VolStatus=%20s"
431    " Slot=%d MaxVolJobs=%u MaxVolFiles=%u InChanger=%d"
432    " VolReadTime=%" lld " VolWriteTime=%" lld;
433    char *media =
434 "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";
435 struct VOLUME_CAT_INFO {
436    /* Media info for the current Volume */
437    uint32_t VolCatJobs;               /* number of jobs on this Volume */
438    uint32_t VolCatFiles;              /* Number of files */
439    uint32_t VolCatBlocks;             /* Number of blocks */
440    uint64_t VolCatBytes;              /* Number of bytes written */
441    uint32_t VolCatMounts;             /* Number of mounts this volume */
442    uint32_t VolCatErrors;             /* Number of errors this volume */
443    uint32_t VolCatWrites;             /* Number of writes this volume */
444    uint32_t VolCatReads;              /* Number of reads this volume */
445    uint64_t VolCatRBytes;             /* Number of bytes read */
446    uint32_t VolCatRecycles;           /* Number of recycles this volume */
447    int32_t  Slot;                     /* Slot in changer */
448    bool     InChanger;                /* Set if vol in current magazine */
449    uint32_t VolCatMaxJobs;            /* Maximum Jobs to write to volume */
450    uint32_t VolCatMaxFiles;           /* Maximum files to write to volume */
451    uint64_t VolCatMaxBytes;           /* Max bytes to write to volume */
452    uint64_t VolCatCapacityBytes;      /* capacity estimate */
453    uint64_t VolReadTime;              /* time spent reading */
454    uint64_t VolWriteTime;             /* time spent writing this Volume */
455    char VolCatStatus[20];             /* Volume status */
456    char VolCatName[MAX_NAME_LENGTH];  /* Desired volume to mount */
457 };                
458    struct VOLUME_CAT_INFO vol;
459
460 #ifdef xxx
461    bsscanf("Hello_world 123 1234", "%120s %ld %lld", buf, &val32, &val64);
462    printf("%s %d %lld\n", buf, val32, val64);
463
464    *Job=0;
465    cnt = bsscanf(catreq, Create_job_media, &Job,
466       &FirstIndex, &LastIndex, &StartFile, &EndFile,
467       &StartBlock, &EndBlock);
468    printf("cnt=%d Job=%s\n", cnt, Job);
469    cnt = bsscanf(helloreq, hello, &Job);
470    printf("cnt=%d Agent=%s\n", cnt, Job);
471 #endif
472    cnt = bsscanf(media, OK_media,
473                vol.VolCatName,
474                &vol.VolCatJobs, &vol.VolCatFiles,
475                &vol.VolCatBlocks, &vol.VolCatBytes,
476                &vol.VolCatMounts, &vol.VolCatErrors,
477                &vol.VolCatWrites, &vol.VolCatMaxBytes,
478                &vol.VolCatCapacityBytes, vol.VolCatStatus,
479                &vol.Slot, &vol.VolCatMaxJobs, &vol.VolCatMaxFiles,
480                &vol.InChanger, &vol.VolReadTime, &vol.VolWriteTime);
481    printf("cnt=%d Vol=%s\n", cnt, vol.VolCatName);
482
483 }
484
485 #endif