2 * scan.c -- scanning routines for Bacula
4 * Kern Sibbald, MM separated from util.c MMIII
9 Bacula® - The Network Backup Solution
11 Copyright (C) 2000-2006 Free Software Foundation Europe e.V.
13 The main author of Bacula is Kern Sibbald, with contributions from
14 many others, a complete list can be found in the file AUTHORS.
15 This program is Free Software; you can redistribute it and/or
16 modify it under the terms of version two of the GNU General Public
17 License as published by the Free Software Foundation plus additions
18 that are listed in the file LICENSE.
20 This program is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
30 Bacula® is a registered trademark of John Walker.
31 The licensor of Bacula is the Free Software Foundation Europe
32 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
33 Switzerland, email:ftf@fsfeurope.org.
39 #include "findlib/find.h"
41 /* Strip leading space from command line arguments */
42 void strip_leading_space(char *str)
45 while (B_ISSPACE(*p)) {
54 /* Strip any trailing junk from the command */
55 void strip_trailing_junk(char *cmd)
58 p = cmd + strlen(cmd) - 1;
60 /* strip trailing junk from command */
61 while ((p >= cmd) && (*p == '\n' || *p == '\r' || *p == ' '))
65 /* Strip any trailing newline characters from the string */
66 void strip_trailing_newline(char *cmd)
69 p = cmd + strlen(cmd) - 1;
71 while ((p >= cmd) && (*p == '\n' || *p == '\r'))
75 /* Strip any trailing slashes from a directory path */
76 void strip_trailing_slashes(char *dir)
79 p = dir + strlen(dir) - 1;
81 /* strip trailing slashes */
82 while (p >= dir && IsPathSeparator(*p))
88 * Returns: 0 on failure (EOF)
90 * new address in passed parameter
92 bool skip_spaces(char **msg)
98 while (*p && B_ISSPACE(*p)) {
102 return *p ? true : false;
107 * Returns: 0 on failure (EOF)
109 * new address in passed parameter
111 bool skip_nonspaces(char **msg)
118 while (*p && !B_ISSPACE(*p)) {
122 return *p ? true : false;
125 /* folded search for string - case insensitive */
127 fstrsch(const char *a, const char *b) /* folded case search */
134 while (*s1) { /* do it the fast way */
135 if ((*s1++ | 0x20) != (*s2++ | 0x20))
136 return 0; /* failed */
138 while (*a) { /* do it over the correct slow way */
139 if (B_ISUPPER(c1 = *a)) {
140 c1 = tolower((int)c1);
142 if (B_ISUPPER(c2 = *b)) {
143 c2 = tolower((int)c2);
156 * Return next argument from command line. Note, this
157 * routine is destructive.
159 char *next_arg(char **s)
162 bool in_quote = false;
164 /* skip past spaces to next arg */
165 for (p=*s; *p && B_ISSPACE(*p); ) {
168 Dmsg1(900, "Next arg=%s\n", p);
169 for (n = q = p; *p ; ) {
179 if (*p == '"') { /* start or end of quote */
181 p++; /* skip quote */
189 if (!in_quote && B_ISSPACE(*p)) { /* end of field */
197 Dmsg2(900, "End arg=%s next=%s\n", n, p);
202 * This routine parses the input command line.
203 * It makes a copy in args, then builds an
204 * argc, argv like list where
206 * argc = count of arguments
207 * argk[i] = argument keyword (part preceding =)
208 * argv[i] = argument value (part after =)
210 * example: arg1 arg2=abc arg3=
221 int parse_args(POOLMEM *cmd, POOLMEM **args, int *argc,
222 char **argk, char **argv, int max_args)
226 pm_strcpy(args, cmd);
227 strip_trailing_junk(*args);
230 /* Pick up all arguments */
231 while (*argc < max_args) {
235 argv[(*argc)++] = NULL;
240 /* Separate keyword and value */
241 for (int i=0; i < *argc; i++) {
242 p = strchr(argk[i], '=');
244 *p++ = 0; /* terminate keyword and point to value */
245 /* Unquote quoted values */
247 for (n = q = ++p; *p && *p != '"'; ) {
253 *q = 0; /* terminate string */
254 p = n; /* point to string */
256 if (strlen(p) > MAX_NAME_LENGTH-1) {
257 p[MAX_NAME_LENGTH-1] = 0; /* truncate to max len */
260 argv[i] = p; /* save ptr to value or NULL */
263 for (int i=0; i < *argc; i++) {
264 Pmsg3(000, "Arg %d: kw=%s val=%s\n", i, argk[i], argv[i]?argv[i]:"NULL");
271 * Given a full filename, split it into its path
272 * and filename parts. They are returned in pool memory
273 * in the arguments provided.
275 void split_path_and_filename(const char *fname, POOLMEM **path, int *pnl,
276 POOLMEM **file, int *fnl)
280 int len = slen = strlen(fname);
283 * Find path without the filename.
284 * I.e. everything after the last / is a "filename".
285 * OK, maybe it is a directory name, but we treat it like
286 * a filename. If we don't find a / then the whole name
287 * must be a path name (e.g. c:).
290 /* "strip" any trailing slashes */
291 while (slen > 1 && IsPathSeparator(*f)) {
295 /* Walk back to last slash -- begin of filename */
296 while (slen > 0 && !IsPathSeparator(*f)) {
300 if (IsPathSeparator(*f)) { /* did we find a slash? */
301 f++; /* yes, point to filename */
302 } else { /* no, whole thing must be path name */
305 Dmsg2(200, "after strip len=%d f=%s\n", len, f);
306 *fnl = fname - f + len;
308 *file = check_pool_memory_size(*file, *fnl+1);
309 memcpy(*file, f, *fnl); /* copy filename */
315 *path = check_pool_memory_size(*path, *pnl+1);
316 memcpy(*path, fname, *pnl);
320 Dmsg2(200, "pnl=%d fnl=%d\n", *pnl, *fnl);
321 Dmsg3(200, "split fname=%s path=%s file=%s\n", fname, *path, *file);
325 * Extremely simple sscanf. Handles only %(u,d,ld,qd,qu,lu,lld,llu,c,nns)
327 const int BIG = 1000;
328 int bsscanf(const char *buf, const char *fmt, ...)
340 while (*fmt && !error) {
341 // Dmsg1(000, "fmt=%c\n", *fmt);
344 // Dmsg1(000, "Got %% nxt=%c\n", *fmt);
350 while (B_ISDIGIT(*buf)) {
351 value = B_TIMES10(value) + *buf++ - '0';
353 vp = (void *)va_arg(ap, void *);
354 // Dmsg2(000, "val=%lld at 0x%lx\n", value, (long unsigned)vp);
356 *((int *)vp) = (int)value;
358 *((uint32_t *)vp) = (uint32_t)value;
359 // Dmsg0(000, "Store 32 bit int\n");
361 *((uint64_t *)vp) = (uint64_t)value;
362 // Dmsg0(000, "Store 64 bit int\n");
368 // Dmsg0(000, "got l\n");
374 if (*fmt == 'd' || *fmt == 'u') {
377 // Dmsg1(000, "fmt=%c !=d,u\n", *fmt);
382 if (*fmt == 'd' || *fmt == 'u') {
385 // Dmsg1(000, "fmt=%c !=d,u\n", *fmt);
389 // Dmsg1(000, "Store string max_len=%d\n", max_len);
390 cp = (char *)va_arg(ap, char *);
391 while (*buf && !B_ISSPACE(*buf) && max_len-- > 0) {
399 cp = (char *)va_arg(ap, char *);
411 while (B_ISDIGIT(*fmt)) {
412 max_len = B_TIMES10(max_len) + *fmt++ - '0';
414 // Dmsg1(000, "Default max_len=%d\n", max_len);
418 // Dmsg1(000, "Default c=%c\n", *fmt);
420 break; /* error: unknown format */
424 /* White space eats zero or more whitespace */
425 } else if (B_ISSPACE(*fmt)) {
427 while (B_ISSPACE(*buf)) {
430 /* Plain text must match */
431 } else if (*buf++ != *fmt++) {
432 // Dmsg2(000, "Mismatch buf=%c fmt=%c\n", *--buf, *--fmt);
438 // Dmsg2(000, "Error=%d count=%d\n", error, count);
446 int main(int argc, char *argv[])
451 uint32_t FirstIndex, LastIndex, StartFile, EndFile, StartBlock, EndBlock;
454 char *helloreq= "Hello *UserAgent* calling\n";
455 char *hello = "Hello %127s calling\n";
457 "CatReq Job=NightlySave.2004-06-11_19.11.32 CreateJobMedia FirstIndex=1 LastIndex=114 StartFile=0 EndFile=0 StartBlock=208 EndBlock=2903248";
458 static char Create_job_media[] = "CatReq Job=%127s CreateJobMedia "
459 "FirstIndex=%u LastIndex=%u StartFile=%u EndFile=%u "
460 "StartBlock=%u EndBlock=%u\n";
461 static char OK_media[] = "1000 OK VolName=%127s VolJobs=%u VolFiles=%u"
462 " VolBlocks=%u VolBytes=%" lld " VolMounts=%u VolErrors=%u VolWrites=%u"
463 " MaxVolBytes=%" lld " VolCapacityBytes=%" lld " VolStatus=%20s"
464 " Slot=%d MaxVolJobs=%u MaxVolFiles=%u InChanger=%d"
465 " VolReadTime=%" lld " VolWriteTime=%" lld;
467 "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";
468 struct VOLUME_CAT_INFO {
469 /* Media info for the current Volume */
470 uint32_t VolCatJobs; /* number of jobs on this Volume */
471 uint32_t VolCatFiles; /* Number of files */
472 uint32_t VolCatBlocks; /* Number of blocks */
473 uint64_t VolCatBytes; /* Number of bytes written */
474 uint32_t VolCatMounts; /* Number of mounts this volume */
475 uint32_t VolCatErrors; /* Number of errors this volume */
476 uint32_t VolCatWrites; /* Number of writes this volume */
477 uint32_t VolCatReads; /* Number of reads this volume */
478 uint64_t VolCatRBytes; /* Number of bytes read */
479 uint32_t VolCatRecycles; /* Number of recycles this volume */
480 int32_t Slot; /* Slot in changer */
481 bool InChanger; /* Set if vol in current magazine */
482 uint32_t VolCatMaxJobs; /* Maximum Jobs to write to volume */
483 uint32_t VolCatMaxFiles; /* Maximum files to write to volume */
484 uint64_t VolCatMaxBytes; /* Max bytes to write to volume */
485 uint64_t VolCatCapacityBytes; /* capacity estimate */
486 uint64_t VolReadTime; /* time spent reading */
487 uint64_t VolWriteTime; /* time spent writing this Volume */
488 char VolCatStatus[20]; /* Volume status */
489 char VolCatName[MAX_NAME_LENGTH]; /* Desired volume to mount */
491 struct VOLUME_CAT_INFO vol;
494 bsscanf("Hello_world 123 1234", "%120s %ld %lld", buf, &val32, &val64);
495 printf("%s %d %lld\n", buf, val32, val64);
498 cnt = bsscanf(catreq, Create_job_media, &Job,
499 &FirstIndex, &LastIndex, &StartFile, &EndFile,
500 &StartBlock, &EndBlock);
501 printf("cnt=%d Job=%s\n", cnt, Job);
502 cnt = bsscanf(helloreq, hello, &Job);
503 printf("cnt=%d Agent=%s\n", cnt, Job);
505 cnt = bsscanf(media, OK_media,
507 &vol.VolCatJobs, &vol.VolCatFiles,
508 &vol.VolCatBlocks, &vol.VolCatBytes,
509 &vol.VolCatMounts, &vol.VolCatErrors,
510 &vol.VolCatWrites, &vol.VolCatMaxBytes,
511 &vol.VolCatCapacityBytes, vol.VolCatStatus,
512 &vol.Slot, &vol.VolCatMaxJobs, &vol.VolCatMaxFiles,
513 &vol.InChanger, &vol.VolReadTime, &vol.VolWriteTime);
514 printf("cnt=%d Vol=%s\n", cnt, vol.VolCatName);