793163c94751af16a946b05e854d5a75dc23c841
1 /****************************************************************************
2 * RRDtool 1.2.15 Copyright by Tobi Oetiker, 1997-2006
3 ****************************************************************************
4 * rrd_graph_helper.c commandline parser functions
5 * this code initially written by Alex van den Bogaerdt
6 ****************************************************************************/
8 #include "rrd_graph.h"
10 #define dprintf if (gdp->debug) printf
12 /* NOTE ON PARSING:
13 *
14 * we use the following:
15 *
16 * i=0; sscanf(&line[*eaten], "what to find%n", variables, &i)
17 *
18 * Usually you want to find a separator as well. Example:
19 * i=0; sscanf(&line[*eaten], "%li:%n", &someint, &i)
20 *
21 * When the separator is not found, i is not set and thus remains zero.
22 * Another way would be to compare strlen() to i
23 *
24 * Why is this important? Because 12345abc should not be matched as
25 * integer 12345 ...
26 */
28 /* NOTE ON VNAMES:
29 *
30 * "if ((gdp->vidx=find_var(im, l))!=-1)" is not good enough, at least
31 * not by itself.
32 *
33 * A vname as a result of a VDEF is quite different from a vname
34 * resulting of a DEF or CDEF.
35 */
37 /* NOTE ON VNAMES:
38 *
39 * A vname called "123" is not to be parsed as the number 123
40 */
43 /* Define prototypes for the parsing methods.
44 Inputs:
45 const char *const line - a fixed pointer to a fixed string
46 unsigned int *const eaten - a fixed pointer to a changing index in that line
47 graph_desc_t *const gdp - a fixed pointer to a changing graph description
48 image_desc_t *const im - a fixed pointer to a changing image description
49 */
51 int rrd_parse_find_gf (const char * const, unsigned int *const, graph_desc_t *const);
52 int rrd_parse_legend (const char * const, unsigned int *const, graph_desc_t *const);
53 int rrd_parse_color (const char * const, graph_desc_t *const);
54 int rrd_parse_CF (const char * const, unsigned int *const, graph_desc_t *const, enum cf_en *const);
55 int rrd_parse_print (const char * const, unsigned int *const, graph_desc_t *const, image_desc_t *const);
56 int rrd_parse_shift (const char * const, unsigned int *const, graph_desc_t *const, image_desc_t *const);
57 int rrd_parse_xport (const char * const, unsigned int *const, graph_desc_t *const, image_desc_t *const);
58 int rrd_parse_PVHLAST (const char * const, unsigned int *const, graph_desc_t *const, image_desc_t *const);
59 int rrd_parse_make_vname (const char * const, unsigned int *const, graph_desc_t *const, image_desc_t *const);
60 int rrd_parse_find_vname (const char * const, unsigned int *const, graph_desc_t *const, image_desc_t *const);
61 int rrd_parse_def (const char * const, unsigned int *const, graph_desc_t *const, image_desc_t *const);
62 int rrd_parse_vdef (const char * const, unsigned int *const, graph_desc_t *const, image_desc_t *const);
63 int rrd_parse_cdef (const char * const, unsigned int *const, graph_desc_t *const, image_desc_t *const);
67 int
68 rrd_parse_find_gf(const char *const line, unsigned int *const eaten, graph_desc_t *const gdp) {
69 char funcname[11],c1=0;
70 int i=0;
72 /* start an argument with DEBUG to be able to see how it is parsed */
73 sscanf(&line[*eaten], "DEBUG%n", &i);
74 if (i) {
75 gdp->debug=1;
76 (*eaten)+=i;
77 i=0;
78 dprintf("Scanning line '%s'\n",&line[*eaten]);
79 }
80 i=0;c1='\0';
81 sscanf(&line[*eaten], "%10[A-Z]%n%c", funcname, &i, &c1);
82 if (!i) {
83 rrd_set_error("Could not make sense out of '%s'",line);
84 return 1;
85 }
86 (*eaten)+=i;
87 if ((int)(gdp->gf=gf_conv(funcname)) == -1) {
88 rrd_set_error("'%s' is not a valid function name", funcname);
89 return 1;
90 } else {
91 dprintf("- found function name '%s'\n",funcname);
92 }
94 if (c1 == '\0') {
95 rrd_set_error("Function %s needs parameters. Line: %s\n",funcname,line);
96 return 1;
97 }
98 if (c1 == ':') (*eaten)++;
100 /* Some commands have a parameter before the colon
101 * (currently only LINE)
102 */
103 switch (gdp->gf) {
104 case GF_LINE:
105 if (c1 == ':') {
106 gdp->linewidth=1;
107 dprintf("- - using default width of 1\n");
108 } else {
109 i=0;sscanf(&line[*eaten],"%lf:%n",&gdp->linewidth,&i);
110 if (!i) {
111 rrd_set_error("Cannot parse line width '%s' in line '%s'\n",&line[*eaten],line);
112 return 1;
113 } else {
114 dprintf("- - scanned width %f\n",gdp->linewidth);
115 if (isnan(gdp->linewidth)) {
116 rrd_set_error("LINE width '%s' is not a number in line '%s'\n",&line[*eaten],line);
117 return 1;
118 }
119 if (isinf(gdp->linewidth)) {
120 rrd_set_error("LINE width '%s' is out of range in line '%s'\n",&line[*eaten],line);
121 return 1;
122 }
123 if (gdp->linewidth<0) {
124 rrd_set_error("LINE width '%s' is less than 0 in line '%s'\n",&line[*eaten],line);
125 return 1;
126 }
127 }
128 (*eaten)+=i;
129 }
130 break;
131 default:
132 if (c1 == ':') break;
133 rrd_set_error("Malformed '%s' command in line '%s'\n",&line[*eaten],line);
134 return 1;
135 }
136 if (line[*eaten] == '\0') {
137 rrd_set_error("Expected some arguments after '%s'\n",line);
138 return 1;
139 }
140 return 0;
141 }
143 int
144 rrd_parse_legend(const char *const line, unsigned int *const eaten, graph_desc_t *const gdp) {
145 int i;
147 if (line[*eaten]=='\0' || line[*eaten]==':') {
148 dprintf("- no (or: empty) legend found\n");
149 return 0;
150 }
152 i=scan_for_col(&line[*eaten],FMT_LEG_LEN,gdp->legend);
154 (*eaten)+=i;
156 if (line[*eaten]!='\0' && line[*eaten]!=':') {
157 rrd_set_error("Legend too long");
158 return 1;
159 } else {
160 return 0;
161 }
162 }
164 int
165 rrd_parse_color(const char *const string, graph_desc_t *const gdp) {
166 unsigned int r=0,g=0,b=0,a=0,i;
168 /* matches the following formats:
169 ** RGB
170 ** RGBA
171 ** RRGGBB
172 ** RRGGBBAA
173 */
175 i=0;
176 while (string[i] && isxdigit((unsigned int)string[i])) i++;
177 if (string[i] != '\0') return 1; /* garbage follows hexdigits */
178 switch (i) {
179 case 3:
180 case 4:
181 sscanf(string, "%1x%1x%1x%1x",&r,&g,&b,&a);
182 r *= 0x11;
183 g *= 0x11;
184 b *= 0x11;
185 a *= 0x11;
186 if (i==3) a=0xFF;
187 break;
188 case 6:
189 case 8:
190 sscanf(string, "%02x%02x%02x%02x",&r,&g,&b,&a);
191 if (i==6) a=0xFF;
192 break;
193 default:
194 return 1; /* wrong number of digits */
195 }
196 gdp->col = r<<24|g<<16|b<<8|a;
197 return 0;
198 }
200 int
201 rrd_parse_CF(const char *const line, unsigned int *const eaten, graph_desc_t *const gdp, enum cf_en *cf) {
202 char symname[CF_NAM_SIZE];
203 int i=0;
205 sscanf(&line[*eaten], CF_NAM_FMT "%n", symname,&i);
206 if ((!i)||((line[(*eaten)+i]!='\0')&&(line[(*eaten)+i]!=':'))) {
207 rrd_set_error("Cannot parse CF in '%s'",line);
208 return 1;
209 }
210 (*eaten)+=i;
211 dprintf("- using CF '%s'\n",symname);
213 if ((int)(*cf = cf_conv(symname))==-1) {
214 rrd_set_error("Unknown CF '%s' in '%s'",symname,line);
215 return 1;
216 }
218 if (line[*eaten]!='\0') (*eaten)++;
219 return 0;
220 }
222 /* Try to match next token as a vname.
223 *
224 * Returns:
225 * -1 an error occured and the error string is set
226 * other the vname index number
227 *
228 * *eaten is incremented only when a vname is found.
229 */
230 int
231 rrd_parse_find_vname(const char *const line, unsigned int *const eaten, graph_desc_t *const gdp, image_desc_t *const im) {
232 char tmpstr[MAX_VNAME_LEN+1];
233 int i;
234 long vidx;
236 i=0;sscanf(&line[*eaten], DEF_NAM_FMT "%n", tmpstr,&i);
237 if (!i) {
238 rrd_set_error("Could not parse line '%s'",line);
239 return -1;
240 }
241 if (line[*eaten+i]!=':' && line[*eaten+i]!='\0') {
242 rrd_set_error("Could not parse line '%s'",line);
243 return -1;
244 }
245 dprintf("- Considering '%s'\n",tmpstr);
247 if ((vidx=find_var(im,tmpstr))<0) {
248 dprintf("- Not a vname\n");
249 rrd_set_error("Not a valid vname: %s in line %s",tmpstr,line);
250 return -1;
251 }
252 dprintf("- Found vname '%s' vidx '%li'\n",tmpstr,gdp->vidx);
253 if (line[*eaten+i]==':') i++;
254 (*eaten)+=i;
255 return vidx;
256 }
258 /* Parsing old-style xPRINT and new-style xPRINT */
259 int
260 rrd_parse_print(const char *const line, unsigned int *const eaten, graph_desc_t *const gdp, image_desc_t *const im) {
261 /* vname:CF:format in case of DEF-based vname
262 ** vname:CF:format in case of CDEF-based vname
263 ** vname:format[:strftime] in case of VDEF-based vname
264 */
265 if ((gdp->vidx=rrd_parse_find_vname(line,eaten,gdp,im))<0) return 1;
267 switch (im->gdes[gdp->vidx].gf) {
268 case GF_DEF:
269 case GF_CDEF:
270 dprintf("- vname is of type DEF or CDEF, looking for CF\n");
271 if (rrd_parse_CF(line,eaten,gdp,&gdp->cf)) return 1;
272 break;
273 case GF_VDEF:
274 dprintf("- vname is of type VDEF\n");
275 break;
276 default:
277 rrd_set_error("Encountered unknown type variable '%s'",im->gdes[gdp->vidx].vname);
278 return 1;
279 }
281 if (rrd_parse_legend(line,eaten,gdp)) return 1;
282 /* for *PRINT the legend itself gets rendered later. We only
283 get the format at this juncture */
284 strcpy(gdp->format,gdp->legend);
285 gdp->legend[0]='\0';
286 /* this is a very crud test, parsing :style flags should be in a function */
287 if (im->gdes[gdp->vidx].gf == GF_VDEF && strcmp(line+(*eaten),":strftime")==0){
288 gdp->strftm = 1;
289 (*eaten)+=strlen(":strftime");
290 }
291 return 0;
292 }
294 /* SHIFT:_def_or_cdef:_vdef_or_number_
295 */
296 int
297 rrd_parse_shift(const char *const line, unsigned int *const eaten, graph_desc_t *const gdp, image_desc_t *const im) {
298 int i;
300 if ((gdp->vidx=rrd_parse_find_vname(line,eaten,gdp,im))<0) return 1;
302 switch (im->gdes[gdp->vidx].gf) {
303 case GF_DEF:
304 case GF_CDEF:
305 dprintf("- vname is of type DEF or CDEF, OK\n");
306 break;
307 case GF_VDEF:
308 rrd_set_error("Cannot shift a VDEF: '%s' in line '%s'\n",im->gdes[gdp->vidx].vname,line);
309 return 1;
310 default:
311 rrd_set_error("Encountered unknown type variable '%s' in line '%s'",im->gdes[gdp->vidx].vname,line);
312 return 1;
313 }
315 if ((gdp->shidx=rrd_parse_find_vname(line,eaten,gdp,im))>=0) {
316 switch (im->gdes[gdp->shidx].gf) {
317 case GF_DEF:
318 case GF_CDEF:
319 rrd_set_error("Offset cannot be a (C)DEF: '%s' in line '%s'\n",im->gdes[gdp->shidx].vname,line);
320 return 1;
321 case GF_VDEF:
322 dprintf("- vname is of type VDEF, OK\n");
323 break;
324 default:
325 rrd_set_error("Encountered unknown type variable '%s' in line '%s'",im->gdes[gdp->vidx].vname,line);
326 return 1;
327 }
328 } else {
329 rrd_clear_error();
330 i=0; sscanf(&line[*eaten],"%li%n",&gdp->shval,&i);
331 if (i!=(int)strlen(&line[*eaten])) {
332 rrd_set_error("Not a valid offset: %s in line %s",&line[*eaten],line);
333 return 1;
334 }
335 (*eaten)+=i;
336 dprintf("- offset is number %li\n",gdp->shval);
337 gdp->shidx = -1;
338 }
339 return 0;
340 }
342 /* XPORT:_def_or_cdef[:legend]
343 */
344 int
345 rrd_parse_xport(const char *const line, unsigned int *const eaten, graph_desc_t *const gdp, image_desc_t *const im) {
346 if ((gdp->vidx=rrd_parse_find_vname(line,eaten,gdp,im))<0) return 1;
348 switch (im->gdes[gdp->vidx].gf) {
349 case GF_DEF:
350 case GF_CDEF:
351 dprintf("- vname is of type DEF or CDEF, OK\n");
352 break;
353 case GF_VDEF:
354 rrd_set_error("Cannot xport a VDEF: '%s' in line '%s'\n",im->gdes[gdp->vidx].vname,line);
355 return 1;
356 default:
357 rrd_set_error("Encountered unknown type variable '%s' in line '%s'",im->gdes[gdp->vidx].vname,line);
358 return 1;
359 }
360 dprintf("- looking for legend in '%s'\n",&line[*eaten]);
361 if (rrd_parse_legend(line,eaten,gdp)) return 1;
362 return 0;
363 }
365 /* Parsing of PART, VRULE, HRULE, LINE, AREA, STACK and TICK
366 ** is done in one function.
367 **
368 ** Stacking PART, VRULE, HRULE or TICK is not allowed.
369 **
370 ** If a number (which is valid to enter) is more than a
371 ** certain amount of characters, it is caught as an error.
372 ** While this is arguable, so is entering fixed numbers
373 ** with more than MAX_VNAME_LEN significant digits.
374 */
375 int
376 rrd_parse_PVHLAST(const char *const line, unsigned int *const eaten, graph_desc_t *const gdp, image_desc_t *const im) {
377 int i,j,k;
378 int colorfound=0;
379 char tmpstr[MAX_VNAME_LEN + 10]; /* vname#RRGGBBAA\0 */
380 static int spacecnt = 0;
382 if (spacecnt == 0) {
383 float one_space = gfx_get_text_width(im->canvas, 0,
384 im->text_prop[TEXT_PROP_LEGEND].font,
385 im->text_prop[TEXT_PROP_LEGEND].size,
386 im->tabwidth," ", 0) / 4.0;
387 float target_space = gfx_get_text_width(im->canvas, 0,
388 im->text_prop[TEXT_PROP_LEGEND].font,
389 im->text_prop[TEXT_PROP_LEGEND].size,
390 im->tabwidth,"oo", 0);
391 spacecnt = target_space / one_space;
392 dprintf("- spacecnt: %i onespace: %f targspace: %f\n",spacecnt,one_space,target_space);
393 }
396 dprintf("- parsing '%s'\n",&line[*eaten]);
398 /* have simpler code in the drawing section */
399 if ( gdp->gf == GF_STACK ){
400 gdp->stack=1;
401 }
403 i=scan_for_col(&line[*eaten],MAX_VNAME_LEN+9,tmpstr);
404 if (line[*eaten+i]!='\0' && line[*eaten+i]!=':') {
405 rrd_set_error("Cannot parse line '%s'",line);
406 return 1;
407 }
409 j=i; while (j>0 && tmpstr[j]!='#') j--;
411 if (j) {
412 tmpstr[j]='\0';
413 }
414 /* We now have:
415 * tmpstr[0] containing vname
416 * tmpstr[j] if j!=0 then containing color
417 * i size of vname + color
418 * j if j!=0 then size of vname
419 */
421 /* Number or vname ?
422 * If it is an existing vname, that's OK, provided that it is a
423 * valid type (need time for VRULE, not a float)
424 * Else see if it parses as a number.
425 */
426 dprintf("- examining string '%s'\n",tmpstr);
427 if ((gdp->vidx=find_var(im,tmpstr))>=0) {
428 dprintf("- found vname: '%s' vidx %li\n",tmpstr,gdp->vidx);
429 switch (gdp->gf) {
430 #ifdef WITH_PIECHART
431 case GF_PART:
432 #endif
433 case GF_VRULE:
434 case GF_HRULE:
435 if (im->gdes[gdp->vidx].gf != GF_VDEF) {
436 rrd_set_error("Using vname %s of wrong type in line %s\n",im->gdes[gdp->gf].vname,line);
437 return 1;
438 }
439 break;
440 default:;
441 }
442 } else {
443 dprintf("- it is not an existing vname\n");
444 switch (gdp->gf) {
445 case GF_VRULE:
446 k=0;sscanf(tmpstr,"%li%n",&gdp->xrule,&k);
447 if (((j!=0)&&(k==j))||((j==0)&&(k==i))) {
448 dprintf("- found time: %li\n",gdp->xrule);
449 } else {
450 dprintf("- is is not a valid number: %li\n",gdp->xrule);
451 rrd_set_error("parameter '%s' does not represent time in line %s\n",tmpstr,line);
452 return 1;
453 }
454 default:
455 k=0;sscanf(tmpstr,"%lf%n",&gdp->yrule,&k);
456 if (((j!=0)&&(k==j))||((j==0)&&(k==i))) {
457 dprintf("- found number: %f\n",gdp->yrule);
458 } else {
459 dprintf("- is is not a valid number: %li\n",gdp->xrule);
460 rrd_set_error("parameter '%s' does not represent a number in line %s\n",tmpstr,line);
461 return 1;
462 }
463 }
464 }
466 if (j) {
467 j++;
468 dprintf("- examining color '%s'\n",&tmpstr[j]);
469 if (rrd_parse_color(&tmpstr[j],gdp)) {
470 rrd_set_error("Could not parse color in '%s'",&tmpstr[j]);
471 return 1;
472 }
473 dprintf("- parsed color 0x%08x\n",(unsigned int)gdp->col);
474 colorfound=1;
475 } else {
476 dprintf("- no color present in '%s'\n",tmpstr);
477 }
479 (*eaten) += i; /* after vname#color */
480 if (line[*eaten]!='\0') {
481 (*eaten)++; /* after colon */
482 }
484 if (gdp->gf == GF_TICK) {
485 dprintf("- parsing '%s'\n",&line[*eaten]);
486 dprintf("- looking for optional TICK number\n");
487 j=0;
488 sscanf(&line[*eaten],"%lf%n",&gdp->yrule,&j);
489 if (j) {
490 if (line[*eaten+j]!='\0' && line[*eaten+j]!=':') {
491 rrd_set_error("Cannot parse TICK fraction '%s'",line);
492 return 1;
493 }
494 dprintf("- found number %f\n",gdp->yrule);
495 if (gdp->yrule > 1.0 || gdp->yrule < -1.0) {
496 rrd_set_error("Tick factor should be <= 1.0");
497 return 1;
498 }
499 (*eaten)+=j;
500 } else {
501 dprintf("- not found, defaulting to 0.1\n");
502 gdp->yrule=0.1;
503 }
504 if (line[*eaten] == '\0') {
505 dprintf("- done parsing line\n");
506 return 0;
507 } else { if (line[*eaten] == ':') {
508 (*eaten)++;
509 } else {
510 rrd_set_error("Can't make sense of that TICK line");
511 return 1;
512 }
513 }
514 }
516 dprintf("- parsing '%s'\n",&line[*eaten]);
518 /* Legend is next. A legend without a color is an error.
519 ** Stacking an item without having a legend is OK however
520 ** then an empty legend should be specified.
521 ** LINE:val#color:STACK means legend is string "STACK"
522 ** LINE:val#color::STACK means no legend, and do STACK
523 ** LINE:val:STACK is an error (legend but no color)
524 ** LINE:val::STACK means no legend, and do STACK
525 */
526 if (colorfound) {
527 int err=0;
528 char *linecp = strdup(line);
529 dprintf("- looking for optional legend\n");
531 dprintf("- examining '%s'\n",&line[*eaten]);
532 if (linecp[*eaten] != '\0' && linecp[*eaten] != ':') {
533 int spi;
534 /* If the legend is not empty, it has to be prefixed with spacecnt ' ' characters. This then gets
535 * replaced by the color box later on. */
536 for (spi=0;spi<spacecnt && (*eaten) > 1;spi++){
537 linecp[--(*eaten)]=' ';
538 }
539 }
541 if (rrd_parse_legend(linecp, eaten, gdp)) err=1;
542 free(linecp);
543 if (err) return 1;
545 dprintf("- found legend '%s'\n", &gdp->legend[2]);
546 } else {
547 dprintf("- skipping empty legend\n");
548 if (line[*eaten] != '\0' && line[*eaten] != ':') {
549 rrd_set_error("Legend set but no color: %s",&line[*eaten]);
550 return 1;
551 }
552 }
553 if (line[*eaten]=='\0') {
554 dprintf("- done parsing line\n");
555 return 0;
556 }
557 (*eaten)++; /* after colon */
559 /* PART, HRULE, VRULE and TICK cannot be stacked. */
560 if ( (gdp->gf == GF_HRULE)
561 || (gdp->gf == GF_VRULE)
562 #ifdef WITH_PIECHART
563 || (gdp->gf == GF_PART)
564 #endif
565 || (gdp->gf == GF_TICK)
566 ) return 0;
568 dprintf("- parsing '%s'\n",&line[*eaten]);
569 if (line[*eaten]!='\0') {
570 dprintf("- still more, should be STACK\n");
571 j=scan_for_col(&line[*eaten],5,tmpstr);
572 if (line[*eaten+j]!='\0' && line[*eaten+j]!=':') {
573 /* not 5 chars */
574 rrd_set_error("Garbage found where STACK expected");
575 return 1;
576 }
577 if (!strcmp("STACK",tmpstr)) {
578 dprintf("- found STACK\n");
579 gdp->stack=1;
580 (*eaten)+=j;
581 } else {
582 rrd_set_error("Garbage found where STACK expected");
583 return 1;
584 }
585 }
586 if (line[*eaten]=='\0') {
587 dprintf("- done parsing line\n");
588 return 0;
589 }
590 (*eaten)++;
591 dprintf("- parsing '%s'\n",&line[*eaten]);
593 return 0;
594 }
596 int
597 rrd_parse_make_vname(const char *const line, unsigned int *const eaten, graph_desc_t *const gdp, image_desc_t *const im) {
598 char tmpstr[MAX_VNAME_LEN + 10];
599 int i=0;
601 sscanf(&line[*eaten], DEF_NAM_FMT "=%n", tmpstr,&i);
602 if (!i) {
603 rrd_set_error("Cannot parse vname from '%s'",line);
604 return 1;
605 }
606 dprintf("- found candidate '%s'\n",tmpstr);
608 if ((gdp->vidx=find_var(im,tmpstr))>=0) {
609 rrd_set_error("Attempting to reuse '%s'",im->gdes[gdp->vidx].vname);
610 return 1;
611 }
612 strcpy(gdp->vname,tmpstr);
613 dprintf("- created vname '%s' vidx %lu\n", gdp->vname,im->gdes_c-1);
614 (*eaten)+=i;
615 return 0;
616 }
618 int
619 rrd_parse_def(const char *const line, unsigned int *const eaten, graph_desc_t *const gdp, image_desc_t *const im) {
620 int i=0;
621 char command[7]; /* step, start, end, reduce */
622 char tmpstr[256];
623 struct rrd_time_value start_tv,end_tv;
624 time_t start_tmp=0,end_tmp=0;
625 char *parsetime_error=NULL;
627 start_tv.type = end_tv.type=ABSOLUTE_TIME;
628 start_tv.offset = end_tv.offset=0;
629 localtime_r(&gdp->start, &start_tv.tm);
630 localtime_r(&gdp->end, &end_tv.tm);
632 dprintf("- parsing '%s'\n",&line[*eaten]);
633 dprintf("- from line '%s'\n",line);
635 if (rrd_parse_make_vname(line,eaten,gdp,im)) return 1;
636 i=scan_for_col(&line[*eaten],254,gdp->rrd);
637 if (line[*eaten+i]!=':') {
638 rrd_set_error("Problems reading database name");
639 return 1;
640 }
641 (*eaten)+=++i;
642 dprintf("- using file '%s'\n",gdp->rrd);
644 i=0;
645 sscanf(&line[*eaten], DS_NAM_FMT ":%n", gdp->ds_nam,&i);
646 if (!i) {
647 rrd_set_error("Cannot parse DS in '%s'",line);
648 return 1;
649 }
650 (*eaten)+=i;
651 dprintf("- using DS '%s'\n",gdp->ds_nam);
653 if (rrd_parse_CF(line,eaten,gdp,&gdp->cf)) return 1;
654 gdp->cf_reduce = gdp->cf;
656 if (line[*eaten]=='\0') return 0;
658 while (1) {
659 dprintf("- optional parameter follows: %s\n", &line[*eaten]);
660 i=0;
661 sscanf(&line[*eaten], "%6[a-z]=%n", command, &i);
662 if (!i) {
663 rrd_set_error("Parse error in '%s'",line);
664 return 1;
665 }
666 (*eaten)+=i;
667 dprintf("- processing '%s'\n",command);
668 if (!strcmp("reduce",command)) {
669 if (rrd_parse_CF(line,eaten,gdp,&gdp->cf_reduce)) return 1;
670 if (line[*eaten] != '\0')
671 (*eaten)--;
672 } else if (!strcmp("step",command)) {
673 i=0;
674 sscanf(&line[*eaten],"%lu%n",&gdp->step,&i);
675 gdp->step_orig = gdp->step;
676 (*eaten)+=i;
677 dprintf("- using step %lu\n",gdp->step);
678 } else if (!strcmp("start",command)) {
679 i=scan_for_col(&line[*eaten],255,tmpstr);
680 (*eaten)+=i;
681 if ((parsetime_error = parsetime(tmpstr, &start_tv))) {
682 rrd_set_error( "start time: %s", parsetime_error );
683 return 1;
684 }
685 dprintf("- done parsing: '%s'\n",&line[*eaten]);
686 } else if (!strcmp("end",command)) {
687 i=scan_for_col(&line[*eaten],255,tmpstr);
688 (*eaten)+=i;
689 if ((parsetime_error = parsetime(tmpstr, &end_tv))) {
690 rrd_set_error( "end time: %s", parsetime_error );
691 return 1;
692 }
693 dprintf("- done parsing: '%s'\n",&line[*eaten]);
694 } else {
695 rrd_set_error("Parse error in '%s'",line);
696 return 1;
697 }
698 if (line[*eaten]=='\0') break;
699 if (line[*eaten]!=':') {
700 dprintf("- Expected to see end of string but got '%s'\n",\
701 &line[*eaten]);
702 rrd_set_error("Parse error in '%s'",line);
703 return 1;
704 }
705 (*eaten)++;
706 }
707 if (proc_start_end(&start_tv,&end_tv,&start_tmp,&end_tmp) == -1){
708 /* error string is set in parsetime.c */
709 return 1;
710 }
711 if (start_tmp < 3600*24*365*10) {
712 rrd_set_error("the first entry to fetch should be "
713 "after 1980 (%ld)",start_tmp);
714 return 1;
715 }
717 if (end_tmp < start_tmp) {
718 rrd_set_error("start (%ld) should be less than end (%ld)",
719 start_tmp, end_tmp);
720 return 1;
721 }
723 gdp->start = start_tmp;
724 gdp->end = end_tmp;
725 gdp->start_orig = start_tmp;
726 gdp->end_orig = end_tmp;
728 dprintf("- start time %lu\n",gdp->start);
729 dprintf("- end time %lu\n",gdp->end);
731 return 0;
732 }
734 int
735 rrd_parse_vdef(const char *const line, unsigned int *const eaten, graph_desc_t *const gdp, image_desc_t *const im) {
736 char tmpstr[MAX_VNAME_LEN+1]; /* vname\0 */
737 int i=0;
739 dprintf("- parsing '%s'\n",&line[*eaten]);
740 if (rrd_parse_make_vname(line,eaten,gdp,im)) return 1;
742 sscanf(&line[*eaten], DEF_NAM_FMT ",%n", tmpstr,&i);
743 if (!i) {
744 rrd_set_error("Cannot parse line '%s'",line);
745 return 1;
746 }
747 if ((gdp->vidx=find_var(im,tmpstr))<0) {
748 rrd_set_error("Not a valid vname: %s in line %s",tmpstr,line);
749 return 1;
750 }
751 if ( im->gdes[gdp->vidx].gf != GF_DEF
752 && im->gdes[gdp->vidx].gf != GF_CDEF) {
753 rrd_set_error("variable '%s' not DEF nor "
754 "CDEF in VDEF '%s'", tmpstr,gdp->vname);
755 return 1;
756 }
757 dprintf("- found vname: '%s' vidx %li\n",tmpstr,gdp->vidx);
758 (*eaten)+=i;
760 dprintf("- calling vdef_parse with param '%s'\n",&line[*eaten]);
761 vdef_parse(gdp,&line[*eaten]);
762 while (line[*eaten]!='\0'&&line[*eaten]!=':')
763 (*eaten)++;
765 return 0;
766 }
768 int
769 rrd_parse_cdef(const char *const line, unsigned int *const eaten, graph_desc_t *const gdp, image_desc_t *const im) {
770 dprintf("- parsing '%s'\n",&line[*eaten]);
771 if (rrd_parse_make_vname(line,eaten,gdp,im)) return 1;
772 if ((gdp->rpnp = rpn_parse(
773 (void *)im,
774 &line[*eaten],
775 &find_var_wrapper)
776 )==NULL) {
777 rrd_set_error("invalid rpn expression in: %s",&line[*eaten]);
778 return 1;
779 };
780 while (line[*eaten]!='\0'&&line[*eaten]!=':')
781 (*eaten)++;
782 return 0;
783 }
785 void
786 rrd_graph_script(int argc, char *argv[], image_desc_t *const im, int optno) {
787 int i;
788 /* save state for STACK backward compat function */
789 enum gf_en last_gf=GF_PRINT;
790 float last_linewidth=0.0;
792 for (i=optind+optno;i<argc;i++) {
793 graph_desc_t *gdp;
794 unsigned int eaten=0;
796 if (gdes_alloc(im)) return; /* the error string is already set */
797 gdp = &im->gdes[im->gdes_c-1];
798 #ifdef DEBUG
799 gdp->debug = 1;
800 #endif
802 if (rrd_parse_find_gf(argv[i],&eaten,gdp)) return;
804 switch (gdp->gf) {
805 case GF_SHIFT: /* vname:value */
806 if (rrd_parse_shift(argv[i],&eaten,gdp,im)) return;
807 break;
808 case GF_XPORT:
809 if (rrd_parse_xport(argv[i],&eaten,gdp,im)) return;
810 break;
811 case GF_PRINT: /* vname:CF:format -or- vname:format */
812 im->prt_c++;
813 case GF_GPRINT: /* vname:CF:format -or- vname:format */
814 if (rrd_parse_print(argv[i],&eaten,gdp,im)) return;
815 break;
816 case GF_COMMENT: /* text */
817 if (rrd_parse_legend(argv[i],&eaten,gdp)) return;
818 break;
819 #ifdef WITH_PIECHART
820 case GF_PART: /* value[#color[:legend]] */
821 #endif
822 case GF_VRULE: /* value#color[:legend] */
823 case GF_HRULE: /* value#color[:legend] */
824 case GF_LINE: /* vname-or-value[#color[:legend]][:STACK] */
825 case GF_AREA: /* vname-or-value[#color[:legend]][:STACK] */
826 case GF_TICK: /* vname#color[:num[:legend]] */
827 if (rrd_parse_PVHLAST(argv[i],&eaten,gdp,im))return;
828 last_gf = gdp->gf;
829 last_linewidth = gdp->linewidth;
830 break;
831 case GF_STACK: /* vname-or-value[#color[:legend]] */
832 if (rrd_parse_PVHLAST(argv[i],&eaten,gdp,im))return;
833 if (last_gf == GF_LINE || last_gf == GF_AREA){
834 gdp->gf = last_gf;
835 gdp->linewidth = last_linewidth;
836 } else {
837 rrd_set_error("STACK must follow LINE or AREA! command:\n%s",
838 &argv[i][eaten],argv[i]);
839 return;
840 }
841 break;
842 /* data acquisition */
843 case GF_DEF: /* vname=x:DS:CF:[:step=#][:start=#][:end=#] */
844 if (rrd_parse_def(argv[i],&eaten,gdp,im)) return;
845 break;
846 case GF_CDEF: /* vname=rpn-expression */
847 if (rrd_parse_cdef(argv[i],&eaten,gdp,im)) return;
848 break;
849 case GF_VDEF: /* vname=rpn-expression */
850 if (rrd_parse_vdef(argv[i],&eaten,gdp,im)) return;
851 break;
852 }
853 if (gdp->debug) {
854 dprintf("used %i out of %i chars\n",eaten,strlen(argv[i]));
855 dprintf("parsed line: '%s'\n",argv[i]);
856 dprintf("remaining: '%s'\n",&argv[i][eaten]);
857 if (eaten >= strlen(argv[i]))
858 dprintf("Command finished successfully\n");
859 }
860 if (eaten < strlen(argv[i])) {
861 rrd_set_error("Garbage '%s' after command:\n%s",
862 &argv[i][eaten],argv[i]);
863 return;
864 }
865 }
866 }