4847812c36186ebfa7815e72b7df5546dfd357dc
1 /****************************************************************************
2 * RRDtool 1.2.x Copyright Tobias Oetiker, 1997 - 2005
3 ****************************************************************************
4 * rrd_graph_helper.c commandline parser functions
5 * this code was probably writtenn by Alex van den Bogaerdt
6 ****************************************************************************/
8 #include "rrd_graph.h"
10 #define dprintf if (gdp->debug) printf
12 /* Define prototypes for the parsing methods.
13 Inputs:
14 char *line - pointer to base of input source
15 unsigned int eaten - index to next input character (INPUT/OUTPUT)
16 graph_desc_t *gdp - pointer to a graph description
17 image_desc_t *im - pointer to an image description
18 */
20 int rrd_parse_find_gf (char *, unsigned int *, graph_desc_t *);
21 int rrd_parse_legend (char *, unsigned int *, graph_desc_t *);
22 int rrd_parse_color (char *, graph_desc_t *);
23 int rrd_parse_CF (char *, unsigned int *, graph_desc_t *, enum cf_en *);
24 int rrd_parse_print (char *, unsigned int *, graph_desc_t *, image_desc_t *);
25 int rrd_parse_shift (char *, unsigned int *, graph_desc_t *, image_desc_t *);
26 int rrd_parse_xport (char *, unsigned int *, graph_desc_t *, image_desc_t *);
27 int rrd_parse_PVHLAST (char *, unsigned int *, graph_desc_t *, image_desc_t *);
28 int rrd_parse_vname (char *, unsigned int *, graph_desc_t *, image_desc_t *);
29 int rrd_parse_def (char *, unsigned int *, graph_desc_t *, image_desc_t *);
30 int rrd_parse_vdef (char *, unsigned int *, graph_desc_t *, image_desc_t *);
31 int rrd_parse_cdef (char *, unsigned int *, graph_desc_t *, image_desc_t *);
35 int
36 rrd_parse_find_gf(char *line, unsigned int *eaten, graph_desc_t *gdp) {
37 char funcname[11],c1=0,c2=0;
38 int i=0;
40 sscanf(&line[*eaten], "DEBUG%n", &i);
41 if (i) {
42 gdp->debug=1;
43 (*eaten)+=i;
44 i=0;
45 dprintf("Scanning line '%s'\n",&line[*eaten]);
46 }
47 sscanf(&line[*eaten], "%10[A-Z]%n%c%c", funcname, &i, &c1, &c2);
48 if (!i) {
49 rrd_set_error("Could not make sense out of '%s'",line);
50 return 1;
51 }
52 if ((int)(gdp->gf=gf_conv(funcname)) == -1) {
53 rrd_set_error("'%s' is not a valid function name", funcname);
54 return 1;
55 }
56 if (gdp->gf == GF_LINE) {
57 if (c1 < '1' || c1 > '3' || c2 != ':') {
58 rrd_set_error("Malformed LINE command: %s",line);
59 return 1;
60 }
61 gdp->linewidth=c1-'0';
62 i++;
63 } else {
64 if (c1 != ':') {
65 rrd_set_error("Malformed %s command: %s",funcname,line);
66 return 1;
67 }
68 }
69 *eaten+=++i;
70 return 0;
71 }
73 int
74 rrd_parse_legend(char *line, unsigned int *eaten, graph_desc_t *gdp) {
75 int i;
77 dprintf("- examining '%s'\n",&line[*eaten]);
79 i=scan_for_col(&line[*eaten],FMT_LEG_LEN,gdp->legend);
81 *eaten += i;
82 if (line[*eaten]!='\0' && line[*eaten]!=':') {
83 rrd_set_error("Legend too long");
84 return 1;
85 } else {
86 dprintf("- found legend '%s'\n", gdp->legend);
87 return 0;
88 }
89 }
91 int
92 rrd_parse_color(char *string, graph_desc_t *gdp) {
93 unsigned int r=0,g=0,b=0,a=0;
94 int i1=0,i2=0,i3=0;
96 if (string[0] != '#') return 1;
97 sscanf(string, "#%02x%02x%02x%n%02x%n%*s%n",
98 &r,&g,&b,&i1,&a,&i2,&i3);
100 if (i3) return 1; /* garbage after color */
101 if (!i2) a=0xFF;
102 if (!i1) return 1; /* no color after '#' */
103 gdp->col = r<<24|g<<16|b<<8|a;
104 return 0;
105 }
107 int
108 rrd_parse_CF(char *line, unsigned int *eaten, graph_desc_t *gdp, enum cf_en *cf) {
109 char symname[CF_NAM_SIZE];
110 int i=0;
112 sscanf(&line[*eaten], CF_NAM_FMT "%n", symname,&i);
113 if ((!i)||((line[*eaten+i]!='\0')&&(line[*eaten+i]!=':'))) {
114 rrd_set_error("Cannot parse CF in '%s'",line);
115 return 1;
116 }
117 (*eaten)+=i;
118 dprintf("- using CF '%s'\n",symname);
120 if ((int)(*cf = cf_conv(symname))==-1) {
121 rrd_set_error("Unknown CF '%s' in '%s'",symname,line);
122 return 1;
123 }
125 if (line[*eaten]!='\0') (*eaten)++;
126 return 0;
127 }
129 /* Parsing old-style xPRINT and new-style xPRINT */
130 int
131 rrd_parse_print(char *line, unsigned int *eaten, graph_desc_t *gdp, image_desc_t *im) {
132 /* vname:CF:format in case of DEF-based vname
133 ** vname:CF:format in case of CDEF-based vname
134 ** vname:format in case of VDEF-based vname
135 */
136 char tmpstr[MAX_VNAME_LEN+1];
137 int i=0;
139 sscanf(&line[*eaten], DEF_NAM_FMT ":%n", tmpstr,&i);
140 if (!i) {
141 rrd_set_error("Could not parse line '%s'",line);
142 return 1;
143 }
144 (*eaten)+=i;
145 dprintf("- Found candidate vname '%s'\n",tmpstr);
147 if ((gdp->vidx=find_var(im,tmpstr))<0) {
148 rrd_set_error("Not a valid vname: %s in line %s",tmpstr,line);
149 return 1;
150 }
151 switch (im->gdes[gdp->vidx].gf) {
152 case GF_DEF:
153 case GF_CDEF:
154 dprintf("- vname is of type DEF or CDEF, looking for CF\n");
155 if (rrd_parse_CF(line,eaten,gdp,&gdp->cf)) return 1;
156 break;
157 case GF_VDEF:
158 dprintf("- vname is of type VDEF\n");
159 break;
160 default:
161 rrd_set_error("Encountered unknown type variable '%s'",tmpstr);
162 return 1;
163 }
165 if (rrd_parse_legend(line,eaten,gdp)) return 1;
167 /* Why is there a separate structure member "format" ??? */
168 strcpy(gdp->format,gdp->legend);
170 return 0;
171 }
173 int
174 rrd_parse_shift(char *line, unsigned int *eaten, graph_desc_t *gdp, image_desc_t *im) {
175 char *l = strdup(line + *eaten), *p;
176 int rc = 1;
178 p = strchr(l, ',');
179 if (p == NULL) {
180 rrd_set_error("Invalid SHIFT syntax");
181 goto out;
182 }
183 *p++ = '\0';
185 if ((gdp->vidx=find_var(im,l))<0) {
186 rrd_set_error("Not a valid vname: %s in line %s",l,line);
187 goto out;
188 }
190 /* constant will parse; otherwise, must be VDEF reference */
191 if (sscanf(p, "%ld", &gdp->shval) != 1) {
192 graph_desc_t *vdp;
194 if ((gdp->shidx=find_var(im, p))<0) {
195 rrd_set_error("invalid offset vname: %s", p);
196 goto out;
197 }
199 vdp = &im->gdes[gdp->shidx];
200 if (vdp->gf != GF_VDEF) {
201 rrd_set_error("offset must specify value or VDEF");
202 goto out;
203 }
204 } else {
205 gdp->shidx = -1;
206 }
208 *eaten = strlen(line);
209 rc = 0;
211 out:
212 free(l);
213 return rc;
214 }
216 int
217 rrd_parse_xport(char *line, unsigned int *eaten, graph_desc_t *gdp, image_desc_t *im) {
218 char *l = strdup(line + *eaten), *p;
219 int rc = 1;
221 p = strchr(l, ':');
222 if (p != NULL)
223 *p++ = '\0';
224 else
225 p = "";
227 if ((gdp->vidx=find_var(im, l))==-1){
228 rrd_set_error("unknown variable '%s'",l);
229 goto out;
230 }
232 if (strlen(p) >= FMT_LEG_LEN)
233 *(p + FMT_LEG_LEN) = '\0';
235 strcpy(gdp->legend, p);
236 *eaten = strlen(line);
237 rc = 0;
239 out:
240 free(l);
241 return rc;
242 }
244 /* Parsing of PART, VRULE, HRULE, LINE, AREA, STACK and TICK
245 ** is done in one function. Stacking STACK is silently ignored
246 ** as it is redundant. Stacking PART, VRULE, HRULE or TICK is
247 ** not allowed. The check for color doesn't need to be so strict
248 ** anymore, the user can specify the color '#00000000' and
249 ** effectively circumvent this check, so why bother.
250 **
251 ** If a number (which is valid to enter) is more than a
252 ** certain amount of characters, it is caught as an error.
253 ** While this is arguable, so is entering fixed numbers
254 ** with more than MAX_VNAME_LEN significant digits.
255 */
256 int
257 rrd_parse_PVHLAST(char *line, unsigned int *eaten, graph_desc_t *gdp, image_desc_t *im) {
258 int i,j;
259 int colorfound=0;
260 char tmpstr[MAX_VNAME_LEN + 10]; /* vname#RRGGBBAA\0 */
262 dprintf("- parsing '%s'\n",&line[*eaten]);
263 dprintf("- from line '%s'\n",line);
265 i=scan_for_col(&line[*eaten],MAX_VNAME_LEN+9,tmpstr);
266 if (line[*eaten+i]!='\0' && line[*eaten+i]!=':') {
267 rrd_set_error("Cannot parse line '%s'",line);
268 return 1;
269 }
271 j=i; while (j>0 && tmpstr[j]!='#') j--;
273 if (tmpstr[j]=='#') {
274 if (rrd_parse_color(&tmpstr[j],gdp)) {
275 rrd_set_error("Could not parse color in '%s'",tmpstr[j]);
276 return 1;
277 }
278 tmpstr[j]='\0';
279 dprintf("- parsed color 0x%08x\n",(unsigned int)gdp->col);
280 colorfound=1;
281 }
283 dprintf("- examining '%s'\n",tmpstr);
284 j=0;
285 if (gdp->gf == GF_VRULE) {
286 sscanf(tmpstr,"%li%n",&gdp->xrule,&j);
287 if (j) dprintf("- found time: %li\n",gdp->xrule);
288 } else {
289 sscanf(tmpstr,"%lf%n",&gdp->yrule,&j);
290 if (j) dprintf("- found number: %f\n",gdp->yrule);
291 }
292 if (!j) {
293 if ((gdp->vidx=find_var(im,tmpstr))<0) {
294 rrd_set_error("Not a valid vname: %s in line %s",tmpstr,line);
295 return 1;
296 }
297 dprintf("- found vname: '%s' vidx %li\n",tmpstr,gdp->vidx);
298 }
299 /* "*eaten" is still pointing to the original location,
300 ** "*eaten +i" is pointing to the character after the color
301 ** or to the terminating '\0' in which case we're finished.
302 */
303 if (line[*eaten+i]=='\0') {
304 *eaten+=i;
305 return 0;
306 }
307 *eaten+=++i;
309 /* If a color is specified and the only remaining part is
310 ** ":STACK" then it is assumed to be the legend. An empty
311 ** legend can be specified as expected. This means the
312 ** following can be done: LINE1:x#FF0000FF::STACK
313 */
314 if (colorfound) { /* no legend if no color */
315 if (gdp->gf == GF_TICK) {
316 dprintf("- looking for optional number\n");
317 sscanf(&line[*eaten],"%lf%n",&gdp->yrule,&j);
318 if (j) {
319 dprintf("- found number %f\n",gdp->yrule);
320 (*eaten)+=j;
321 if (gdp->yrule > 1.0 || gdp->yrule < -1.0) {
322 rrd_set_error("Tick factor should be <= 1.0");
323 return 1;
324 }
325 if (line[*eaten] == ':')
326 (*eaten)++;
327 } else {
328 dprintf("- not found, defaulting to 0.1\n");
329 gdp->yrule=0.1;
330 return 0;
331 }
332 }
333 dprintf("- looking for optional legend\n");
334 dprintf("- in '%s'\n",&line[*eaten]);
335 /* the legend for a graph item must start with "m " the first
336 m will then be over drawn with a color box. Since there
337 is ample space I overwrite the first few characters of the line
338 with the material that I want to see in the legend */
339 if (line[*eaten] != '\0' && line[*eaten] != ':'){
340 *eaten = *eaten - 2;
341 line[*eaten] = 'm';
342 line[*eaten+1] = ' ';
343 }
344 if (rrd_parse_legend(line, eaten, gdp)) return 1;
345 }
347 /* PART, HRULE, VRULE and TICK cannot be stacked. We're finished */
348 if ( (gdp->gf == GF_HRULE)
349 || (gdp->gf == GF_VRULE)
350 || (gdp->gf == GF_PART)
351 || (gdp->gf == GF_TICK)
352 ) return 0;
354 if (line[*eaten]!='\0') {
355 dprintf("- still more, should be STACK\n");
356 (*eaten)++;
357 j=scan_for_col(&line[*eaten],5,tmpstr);
358 if (line[*eaten+j]!='\0') {
359 rrd_set_error("Garbage found where STACK expected");
360 return 1;
361 }
362 if (!strcmp("STACK",tmpstr)) {
363 dprintf("- found STACK\n");
364 gdp->stack=1;
365 (*eaten)+=5;
366 } else {
367 rrd_set_error("Garbage found where STACK expected");
368 return 1;
369 }
370 }
371 return 0;
372 }
374 int
375 rrd_parse_vname(char *line, unsigned int *eaten, graph_desc_t *gdp, image_desc_t *im) {
376 char tmpstr[MAX_VNAME_LEN + 10];
377 int i=0;
379 sscanf(&line[*eaten], DEF_NAM_FMT "=%n", tmpstr,&i);
380 if (!i) {
381 rrd_set_error("Cannot parse vname from '%s'",line);
382 return 1;
383 }
384 dprintf("- found candidate '%s'\n",tmpstr);
386 if ((gdp->vidx=find_var(im,tmpstr))>=0) {
387 rrd_set_error("Attempting to reuse '%s'",im->gdes[gdp->vidx].vname);
388 return 1;
389 }
390 strcpy(gdp->vname,tmpstr);
391 dprintf("- created vname '%s' vidx %lu\n", gdp->vname,im->gdes_c-1);
392 (*eaten)+=i;
393 return 0;
394 }
396 int
397 rrd_parse_def(char *line, unsigned int *eaten, graph_desc_t *gdp, image_desc_t *im) {
398 int i=0;
399 char command[7]; /* step, start, end, reduce */
400 char tmpstr[256];
401 struct rrd_time_value start_tv,end_tv;
402 time_t start_tmp=0,end_tmp=0;
403 char *parsetime_error=NULL;
405 start_tv.type = end_tv.type=ABSOLUTE_TIME;
406 start_tv.offset = end_tv.offset=0;
407 localtime_r(&gdp->start, &start_tv.tm);
408 localtime_r(&gdp->end, &end_tv.tm);
410 dprintf("- parsing '%s'\n",&line[*eaten]);
411 dprintf("- from line '%s'\n",line);
413 if (rrd_parse_vname(line,eaten,gdp,im)) return 1;
414 i=scan_for_col(&line[*eaten],254,gdp->rrd);
415 if (line[*eaten+i]!=':') {
416 rrd_set_error("Problems reading database name");
417 return 1;
418 }
419 (*eaten)+=++i;
420 dprintf("- using file '%s'\n",gdp->rrd);
422 i=0;
423 sscanf(&line[*eaten], DS_NAM_FMT ":%n", gdp->ds_nam,&i);
424 if (!i) {
425 rrd_set_error("Cannot parse DS in '%s'",line);
426 return 1;
427 }
428 (*eaten)+=i;
429 dprintf("- using DS '%s'\n",gdp->ds_nam);
431 if (rrd_parse_CF(line,eaten,gdp,&gdp->cf)) return 1;
432 gdp->cf_reduce = gdp->cf;
434 if (line[*eaten]=='\0') return 0;
436 while (1) {
437 dprintf("- optional parameter follows: %s\n", &line[*eaten]);
438 i=0;
439 sscanf(&line[*eaten], "%6[a-z]=%n", command, &i);
440 if (!i) {
441 rrd_set_error("Parse error in '%s'",line);
442 return 1;
443 }
444 (*eaten)+=i;
445 dprintf("- processing '%s'\n",command);
446 if (!strcmp("reduce",command)) {
447 if (rrd_parse_CF(line,eaten,gdp,&gdp->cf_reduce)) return 1;
448 if (line[*eaten] != '\0')
449 (*eaten)--;
450 } else if (!strcmp("step",command)) {
451 i=0;
452 sscanf(&line[*eaten],"%lu%n",&gdp->step,&i);
453 (*eaten)+=i;
454 dprintf("- using step %lu\n",gdp->step);
455 } else if (!strcmp("start",command)) {
456 i=scan_for_col(&line[*eaten],255,tmpstr);
457 (*eaten)+=i;
458 if ((parsetime_error = parsetime(tmpstr, &start_tv))) {
459 rrd_set_error( "start time: %s", parsetime_error );
460 return 1;
461 }
462 dprintf("- done parsing: '%s'\n",&line[*eaten]);
463 } else if (!strcmp("end",command)) {
464 i=scan_for_col(&line[*eaten],255,tmpstr);
465 (*eaten)+=i;
466 if ((parsetime_error = parsetime(tmpstr, &end_tv))) {
467 rrd_set_error( "end time: %s", parsetime_error );
468 return 1;
469 }
470 dprintf("- done parsing: '%s'\n",&line[*eaten]);
471 } else {
472 rrd_set_error("Parse error in '%s'",line);
473 return 1;
474 }
475 if (line[*eaten]=='\0') break;
476 if (line[*eaten]!=':') {
477 dprintf("- Expected to see end of string but got '%s'\n",\
478 &line[*eaten]);
479 rrd_set_error("Parse error in '%s'",line);
480 return 1;
481 }
482 (*eaten)++;
483 }
484 if (proc_start_end(&start_tv,&end_tv,&start_tmp,&end_tmp) == -1){
485 /* error string is set in parsetime.c */
486 return 1;
487 }
488 if (start_tmp < 3600*24*365*10) {
489 rrd_set_error("the first entry to fetch should be "
490 "after 1980 (%ld)",start_tmp);
491 return 1;
492 }
494 if (end_tmp < start_tmp) {
495 rrd_set_error("start (%ld) should be less than end (%ld)",
496 start_tmp, end_tmp);
497 return 1;
498 }
500 gdp->start = start_tmp;
501 gdp->end = end_tmp;
503 dprintf("- start time %lu\n",gdp->start);
504 dprintf("- end time %lu\n",gdp->end);
506 return 0;
507 }
509 int
510 rrd_parse_vdef(char *line, unsigned int *eaten, graph_desc_t *gdp, image_desc_t *im) {
511 char tmpstr[MAX_VNAME_LEN+1]; /* vname\0 */
512 int i=0;
514 dprintf("- parsing '%s'\n",&line[*eaten]);
515 if (rrd_parse_vname(line,eaten,gdp,im)) return 1;
517 sscanf(&line[*eaten], DEF_NAM_FMT ",%n", tmpstr,&i);
518 if (!i) {
519 rrd_set_error("Cannot parse line '%s'",line);
520 return 1;
521 }
522 if ((gdp->vidx=find_var(im,tmpstr))<0) {
523 rrd_set_error("Not a valid vname: %s in line %s",tmpstr,line);
524 return 1;
525 }
526 if ( im->gdes[gdp->vidx].gf != GF_DEF
527 && im->gdes[gdp->vidx].gf != GF_CDEF) {
528 rrd_set_error("variable '%s' not DEF nor "
529 "CDEF in VDEF '%s'", tmpstr,gdp->vname);
530 return 1;
531 }
532 dprintf("- found vname: '%s' vidx %li\n",tmpstr,gdp->vidx);
533 (*eaten)+=i;
535 dprintf("- calling vdef_parse with param '%s'\n",&line[*eaten]);
536 vdef_parse(gdp,&line[*eaten]);
537 while (line[*eaten]!='\0'&&line[*eaten]!=':')
538 (*eaten)++;
540 return 0;
541 }
543 int
544 rrd_parse_cdef(char *line, unsigned int *eaten, graph_desc_t *gdp, image_desc_t *im) {
545 dprintf("- parsing '%s'\n",&line[*eaten]);
546 if (rrd_parse_vname(line,eaten,gdp,im)) return 1;
547 if ((gdp->rpnp = rpn_parse(
548 (void *)im,
549 &line[*eaten],
550 &find_var_wrapper)
551 )==NULL) {
552 rrd_set_error("invalid rpn expression in: %s",&line[*eaten]);
553 return 1;
554 };
555 while (line[*eaten]!='\0'&&line[*eaten]!=':')
556 (*eaten)++;
557 return 0;
558 }
560 void
561 rrd_graph_script(int argc, char *argv[], image_desc_t *im, int optno) {
562 int i;
564 for (i=optind+optno;i<argc;i++) {
565 graph_desc_t *gdp;
566 unsigned int eaten=0;
568 if (gdes_alloc(im)) return; /* the error string is already set */
569 gdp = &im->gdes[im->gdes_c-1];
570 #ifdef DEBUG
571 gdp->debug = 1;
572 #endif
574 if (rrd_parse_find_gf(argv[i],&eaten,gdp)) return;
576 switch (gdp->gf) {
577 case GF_SHIFT: /* vname:value */
578 if (rrd_parse_shift(argv[i],&eaten,gdp,im)) return;
579 break;
580 case GF_XPORT:
581 if (rrd_parse_xport(argv[i],&eaten,gdp,im)) return;
582 break;
583 case GF_PRINT: /* vname:CF:format -or- vname:format */
584 case GF_GPRINT: /* vname:CF:format -or- vname:format */
585 if (rrd_parse_print(argv[i],&eaten,gdp,im)) return;
586 break;
587 case GF_COMMENT: /* text */
588 if (rrd_parse_legend(argv[i],&eaten,gdp)) return;
589 break;
590 case GF_PART: /* value[#color[:legend]] */
591 case GF_VRULE: /* value#color[:legend] */
592 case GF_HRULE: /* value#color[:legend] */
593 case GF_LINE: /* vname-or-value[#color[:legend]][:STACK] */
594 case GF_AREA: /* vname-or-value[#color[:legend]][:STACK] */
595 case GF_STACK: /* vname-or-value[#color[:legend]] */
596 case GF_TICK: /* vname#color[:num[:legend]] */
597 if (rrd_parse_PVHLAST(argv[i],&eaten,gdp,im)) return;
598 break;
599 /* data acquisition */
600 case GF_DEF: /* vname=x:DS:CF:[:step=#][:start=#][:end=#] */
601 if (rrd_parse_def(argv[i],&eaten,gdp,im)) return;
602 break;
603 case GF_CDEF: /* vname=rpn-expression */
604 if (rrd_parse_cdef(argv[i],&eaten,gdp,im)) return;
605 break;
606 case GF_VDEF: /* vname=rpn-expression */
607 if (rrd_parse_vdef(argv[i],&eaten,gdp,im)) return;
608 break;
609 }
610 if (gdp->debug) {
611 dprintf("used %i out of %i chars\n",eaten,strlen(argv[i]));
612 dprintf("parsed line: '%s'\n",argv[i]);
613 dprintf("remaining: '%s'\n",&argv[i][eaten]);
614 if (eaten >= strlen(argv[i]))
615 dprintf("Command finished successfully\n");
616 }
617 if (eaten < strlen(argv[i])) {
618 rrd_set_error("Garbage '%s' after command:\n%s",
619 &argv[i][eaten],argv[i]);
620 return;
621 }
622 }
623 }