1 /****************************************************************************
2 * RRDtool 1.3.7 Copyright by Tobi Oetiker, 1997-2009
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(
52 const char *const,
53 unsigned int *const,
54 graph_desc_t *const);
56 int rrd_parse_legend(
57 const char *const,
58 unsigned int *const,
59 graph_desc_t *const);
61 int rrd_parse_color(
62 const char *const,
63 graph_desc_t *const);
65 int rrd_parse_textalign(
66 const char *const,
67 unsigned int *const,
68 graph_desc_t *const);
71 int rrd_parse_CF(
72 const char *const,
73 unsigned int *const,
74 graph_desc_t *const,
75 enum cf_en *const);
77 int rrd_parse_print(
78 const char *const,
79 unsigned int *const,
80 graph_desc_t *const,
81 image_desc_t *const);
83 int rrd_parse_shift(
84 const char *const,
85 unsigned int *const,
86 graph_desc_t *const,
87 image_desc_t *const);
89 int rrd_parse_xport(
90 const char *const,
91 unsigned int *const,
92 graph_desc_t *const,
93 image_desc_t *const);
95 int rrd_parse_PVHLAST(
96 const char *const,
97 unsigned int *const,
98 graph_desc_t *const,
99 image_desc_t *const);
101 int rrd_parse_make_vname(
102 const char *const,
103 unsigned int *const,
104 graph_desc_t *const,
105 image_desc_t *const);
107 int rrd_parse_find_vname(
108 const char *const,
109 unsigned int *const,
110 graph_desc_t *const,
111 image_desc_t *const);
113 int rrd_parse_def(
114 const char *const,
115 unsigned int *const,
116 graph_desc_t *const,
117 image_desc_t *const);
119 int rrd_parse_vdef(
120 const char *const,
121 unsigned int *const,
122 graph_desc_t *const,
123 image_desc_t *const);
125 int rrd_parse_cdef(
126 const char *const,
127 unsigned int *const,
128 graph_desc_t *const,
129 image_desc_t *const);
131 int rrd_parse_find_gf(
132 const char *const line,
133 unsigned int *const eaten,
134 graph_desc_t *const gdp)
135 {
136 char funcname[11], c1 = 0;
137 int i = 0;
139 /* start an argument with DEBUG to be able to see how it is parsed */
140 sscanf(&line[*eaten], "DEBUG%n", &i);
141 if (i) {
142 gdp->debug = 1;
143 (*eaten) += i;
144 i = 0;
145 dprintf("Scanning line '%s'\n", &line[*eaten]);
146 }
147 i = 0;
148 c1 = '\0';
149 sscanf(&line[*eaten], "%10[A-Z]%n%c", funcname, &i, &c1);
150 if (!i) {
151 rrd_set_error("Could not make sense out of '%s'", line);
152 return 1;
153 }
154 (*eaten) += i;
155 if ((int) (gdp->gf = gf_conv(funcname)) == -1) {
156 rrd_set_error("'%s' is not a valid function name", funcname);
157 return 1;
158 } else {
159 dprintf("- found function name '%s'\n", funcname);
160 }
162 if (c1 == '\0') {
163 rrd_set_error("Function %s needs parameters. Line: %s\n", funcname,
164 line);
165 return 1;
166 }
167 if (c1 == ':')
168 (*eaten)++;
170 /* Some commands have a parameter before the colon
171 * (currently only LINE)
172 */
173 switch (gdp->gf) {
174 case GF_LINE:
175 if (c1 == ':') {
176 gdp->linewidth = 1;
177 dprintf("- using default width of 1\n");
178 } else {
179 i = 0;
180 sscanf(&line[*eaten], "%lf:%n", &gdp->linewidth, &i);
181 if (!i) {
182 rrd_set_error("Cannot parse line width '%s' in line '%s'\n",
183 &line[*eaten], line);
184 return 1;
185 } else {
186 dprintf("- scanned width %f\n", gdp->linewidth);
187 if (isnan(gdp->linewidth)) {
188 rrd_set_error
189 ("LINE width '%s' is not a number in line '%s'\n",
190 &line[*eaten], line);
191 return 1;
192 }
193 if (isinf(gdp->linewidth)) {
194 rrd_set_error
195 ("LINE width '%s' is out of range in line '%s'\n",
196 &line[*eaten], line);
197 return 1;
198 }
199 if (gdp->linewidth < 0) {
200 rrd_set_error
201 ("LINE width '%s' is less than 0 in line '%s'\n",
202 &line[*eaten], line);
203 return 1;
204 }
205 }
206 (*eaten) += i;
207 }
208 break;
209 default:
210 if (c1 == ':')
211 break;
212 rrd_set_error("Malformed '%s' command in line '%s'\n", &line[*eaten],
213 line);
214 return 1;
215 }
216 if (line[*eaten] == '\0') {
217 rrd_set_error("Expected some arguments after '%s'\n", line);
218 return 1;
219 }
220 return 0;
221 }
223 int rrd_parse_legend(
224 const char *const line,
225 unsigned int *const eaten,
226 graph_desc_t *const gdp)
227 {
228 int i;
230 if (line[*eaten] == '\0' || line[*eaten] == ':') {
231 dprintf("- no (or: empty) legend found\n");
232 return 0;
233 }
235 i = scan_for_col(&line[*eaten], FMT_LEG_LEN, gdp->legend);
237 (*eaten) += i;
239 if (line[*eaten] != '\0' && line[*eaten] != ':') {
240 rrd_set_error("Legend too long");
241 return 1;
242 } else {
243 return 0;
244 }
245 }
247 int rrd_parse_color(
248 const char *const string,
249 graph_desc_t *const gdp)
250 {
251 unsigned int r = 0, g = 0, b = 0, a = 0, i;
253 /* matches the following formats:
254 ** RGB
255 ** RGBA
256 ** RRGGBB
257 ** RRGGBBAA
258 */
260 i = 0;
261 while (string[i] && isxdigit((unsigned int) string[i]))
262 i++;
263 if (string[i] != '\0')
264 return 1; /* garbage follows hexdigits */
265 switch (i) {
266 case 3:
267 case 4:
268 sscanf(string, "%1x%1x%1x%1x", &r, &g, &b, &a);
269 r *= 0x11;
270 g *= 0x11;
271 b *= 0x11;
272 a *= 0x11;
273 if (i == 3)
274 a = 0xFF;
275 break;
276 case 6:
277 case 8:
278 sscanf(string, "%02x%02x%02x%02x", &r, &g, &b, &a);
279 if (i == 6)
280 a = 0xFF;
281 break;
282 default:
283 return 1; /* wrong number of digits */
284 }
285 gdp->col = gfx_hex_to_col(r << 24 | g << 16 | b << 8 | a);
286 return 0;
287 }
289 int rrd_parse_CF(
290 const char *const line,
291 unsigned int *const eaten,
292 graph_desc_t *const gdp,
293 enum cf_en *cf)
294 {
295 char symname[CF_NAM_SIZE];
296 int i = 0;
298 sscanf(&line[*eaten], CF_NAM_FMT "%n", symname, &i);
299 if ((!i) || ((line[(*eaten) + i] != '\0') && (line[(*eaten) + i] != ':'))) {
300 rrd_set_error("Cannot parse CF in '%s'", line);
301 return 1;
302 }
303 (*eaten) += i;
304 dprintf("- using CF '%s'\n", symname);
306 if ((int) (*cf = cf_conv(symname)) == -1) {
307 rrd_set_error("Unknown CF '%s' in '%s'", symname, line);
308 return 1;
309 }
311 if (line[*eaten] != '\0')
312 (*eaten)++;
313 return 0;
314 }
316 /* Try to match next token as a vname.
317 *
318 * Returns:
319 * -1 an error occured and the error string is set
320 * other the vname index number
321 *
322 * *eaten is incremented only when a vname is found.
323 */
324 int rrd_parse_find_vname(
325 const char *const line,
326 unsigned int *const eaten,
327 graph_desc_t *const gdp,
328 image_desc_t *const im)
329 {
330 char tmpstr[MAX_VNAME_LEN + 1];
331 int i;
332 long vidx;
334 i = 0;
335 sscanf(&line[*eaten], DEF_NAM_FMT "%n", tmpstr, &i);
336 if (!i) {
337 rrd_set_error("Could not parse line '%s'", line);
338 return -1;
339 }
340 if (line[*eaten + i] != ':' && line[*eaten + i] != '\0') {
341 rrd_set_error("Could not parse line '%s'", line);
342 return -1;
343 }
344 dprintf("- Considering '%s'\n", tmpstr);
346 if ((vidx = find_var(im, tmpstr)) < 0) {
347 dprintf("- Not a vname\n");
348 rrd_set_error("Not a valid vname: %s in line %s", tmpstr, line);
349 return -1;
350 }
351 dprintf("- Found vname '%s' vidx '%li'\n", tmpstr, gdp->vidx);
352 if (line[*eaten + i] == ':')
353 i++;
354 (*eaten) += i;
355 return vidx;
356 }
358 /* Parsing old-style xPRINT and new-style xPRINT */
359 int rrd_parse_print(
360 const char *const line,
361 unsigned int *const eaten,
362 graph_desc_t *const gdp,
363 image_desc_t *const im)
364 {
365 /* vname:CF:format in case of DEF-based vname
366 ** vname:CF:format in case of CDEF-based vname
367 ** vname:format[:strftime] in case of VDEF-based vname
368 */
369 if ((gdp->vidx = rrd_parse_find_vname(line, eaten, gdp, im)) < 0)
370 return 1;
372 switch (im->gdes[gdp->vidx].gf) {
373 case GF_DEF:
374 case GF_CDEF:
375 dprintf("- vname is of type DEF or CDEF, looking for CF\n");
376 if (rrd_parse_CF(line, eaten, gdp, &gdp->cf))
377 return 1;
378 break;
379 case GF_VDEF:
380 dprintf("- vname is of type VDEF\n");
381 break;
382 default:
383 rrd_set_error("Encountered unknown type variable '%s'",
384 im->gdes[gdp->vidx].vname);
385 return 1;
386 }
388 if (rrd_parse_legend(line, eaten, gdp))
389 return 1;
390 /* for *PRINT the legend itself gets rendered later. We only
391 get the format at this juncture */
392 strcpy(gdp->format, gdp->legend);
393 gdp->legend[0] = '\0';
394 /* this is a very crud test, parsing :style flags should be in a function */
395 if (im->gdes[gdp->vidx].gf == GF_VDEF
396 && strcmp(line + (*eaten), ":strftime") == 0) {
397 gdp->strftm = 1;
398 (*eaten) += strlen(":strftime");
399 }
400 return 0;
401 }
403 /* SHIFT:_def_or_cdef:_vdef_or_number_
404 */
405 int rrd_parse_shift(
406 const char *const line,
407 unsigned int *const eaten,
408 graph_desc_t *const gdp,
409 image_desc_t *const im)
410 {
411 int i;
413 if ((gdp->vidx = rrd_parse_find_vname(line, eaten, gdp, im)) < 0)
414 return 1;
416 switch (im->gdes[gdp->vidx].gf) {
417 case GF_DEF:
418 case GF_CDEF:
419 dprintf("- vname is of type DEF or CDEF, OK\n");
420 break;
421 case GF_VDEF:
422 rrd_set_error("Cannot shift a VDEF: '%s' in line '%s'\n",
423 im->gdes[gdp->vidx].vname, line);
424 return 1;
425 default:
426 rrd_set_error("Encountered unknown type variable '%s' in line '%s'",
427 im->gdes[gdp->vidx].vname, line);
428 return 1;
429 }
431 if ((gdp->shidx = rrd_parse_find_vname(line, eaten, gdp, im)) >= 0) {
432 switch (im->gdes[gdp->shidx].gf) {
433 case GF_DEF:
434 case GF_CDEF:
435 rrd_set_error("Offset cannot be a (C)DEF: '%s' in line '%s'\n",
436 im->gdes[gdp->shidx].vname, line);
437 return 1;
438 case GF_VDEF:
439 dprintf("- vname is of type VDEF, OK\n");
440 break;
441 default:
442 rrd_set_error
443 ("Encountered unknown type variable '%s' in line '%s'",
444 im->gdes[gdp->vidx].vname, line);
445 return 1;
446 }
447 } else {
448 long time_tmp = 0;
450 rrd_clear_error();
451 i = 0;
452 sscanf(&line[*eaten], "%li%n", &time_tmp, &i);
453 gdp->shval = time_tmp;
454 if (i != (int) strlen(&line[*eaten])) {
455 rrd_set_error("Not a valid offset: %s in line %s", &line[*eaten],
456 line);
457 return 1;
458 }
459 (*eaten) += i;
460 dprintf("- offset is number %li\n", gdp->shval);
461 gdp->shidx = -1;
462 }
463 return 0;
464 }
466 /* XPORT:_def_or_cdef[:legend]
467 */
468 int rrd_parse_xport(
469 const char *const line,
470 unsigned int *const eaten,
471 graph_desc_t *const gdp,
472 image_desc_t *const im)
473 {
474 if ((gdp->vidx = rrd_parse_find_vname(line, eaten, gdp, im)) < 0)
475 return 1;
477 switch (im->gdes[gdp->vidx].gf) {
478 case GF_DEF:
479 case GF_CDEF:
480 dprintf("- vname is of type DEF or CDEF, OK\n");
481 break;
482 case GF_VDEF:
483 rrd_set_error("Cannot xport a VDEF: '%s' in line '%s'\n",
484 im->gdes[gdp->vidx].vname, line);
485 return 1;
486 default:
487 rrd_set_error("Encountered unknown type variable '%s' in line '%s'",
488 im->gdes[gdp->vidx].vname, line);
489 return 1;
490 }
491 dprintf("- looking for legend in '%s'\n", &line[*eaten]);
492 if (rrd_parse_legend(line, eaten, gdp))
493 return 1;
494 return 0;
495 }
497 int rrd_parse_textalign(
498 const char *const line,
499 unsigned int *const eaten,
500 graph_desc_t *const gdp)
501 {
502 if (strcmp(&line[*eaten], "left") == 0) {
503 gdp->txtalign = TXA_LEFT;
504 } else if (strcmp(&line[*eaten], "right") == 0) {
505 gdp->txtalign = TXA_RIGHT;
506 } else if (strcmp(&line[*eaten], "justified") == 0) {
507 gdp->txtalign = TXA_JUSTIFIED;
508 } else if (strcmp(&line[*eaten], "center") == 0) {
509 gdp->txtalign = TXA_CENTER;
510 } else {
511 rrd_set_error("Unknown alignement type '%s'", &line[*eaten]);
512 return 1;
513 }
514 *eaten += strlen(&line[*eaten]);
515 return 0;
516 }
519 /* Parsing of VRULE, HRULE, LINE, AREA, STACK and TICK
520 ** is done in one function.
521 **
522 ** Stacking VRULE, HRULE or TICK is not allowed.
523 **
524 ** If a number (which is valid to enter) is more than a
525 ** certain amount of characters, it is caught as an error.
526 ** While this is arguable, so is entering fixed numbers
527 ** with more than MAX_VNAME_LEN significant digits.
528 */
529 int rrd_parse_PVHLAST(
530 const char *const line,
531 unsigned int *const eaten,
532 graph_desc_t *const gdp,
533 image_desc_t *const im)
534 {
535 int i, j, k;
536 int colorfound = 0;
537 char tmpstr[MAX_VNAME_LEN + 10]; /* vname#RRGGBBAA\0 */
538 static int spacecnt = 0;
540 if (spacecnt == 0) {
541 float one_space = gfx_get_text_width(im, 0,
542 im->
543 text_prop[TEXT_PROP_LEGEND].
544 font_desc,
545 im->tabwidth, " ") / 4.0;
546 float target_space = gfx_get_text_width(im, 0,
547 im->
548 text_prop
549 [TEXT_PROP_LEGEND].font_desc,
550 im->tabwidth, "oo");
552 spacecnt = target_space / one_space;
553 dprintf("- spacecnt: %i onespace: %f targspace: %f\n", spacecnt,
554 one_space, target_space);
555 }
558 dprintf("- parsing '%s'\n", &line[*eaten]);
560 /* have simpler code in the drawing section */
561 if (gdp->gf == GF_STACK) {
562 gdp->stack = 1;
563 }
565 i = scan_for_col(&line[*eaten], MAX_VNAME_LEN + 9, tmpstr);
566 if (line[*eaten + i] != '\0' && line[*eaten + i] != ':') {
567 rrd_set_error("Cannot parse line '%s'", line);
568 return 1;
569 }
571 j = i;
572 while (j > 0 && tmpstr[j] != '#')
573 j--;
575 if (j) {
576 tmpstr[j] = '\0';
577 }
578 /* We now have:
579 * tmpstr[0] containing vname
580 * tmpstr[j] if j!=0 then containing color
581 * i size of vname + color
582 * j if j!=0 then size of vname
583 */
585 /* Number or vname ?
586 * If it is an existing vname, that's OK, provided that it is a
587 * valid type (need time for VRULE, not a float)
588 * Else see if it parses as a number.
589 */
590 dprintf("- examining string '%s'\n", tmpstr);
591 if ((gdp->vidx = find_var(im, tmpstr)) >= 0) {
592 dprintf("- found vname: '%s' vidx %li\n", tmpstr, gdp->vidx);
593 switch (gdp->gf) {
594 case GF_VRULE:
595 case GF_HRULE:
596 if (im->gdes[gdp->vidx].gf != GF_VDEF) {
597 rrd_set_error("Using vname %s of wrong type in line %s\n",
598 im->gdes[gdp->gf].vname, line);
599 return 1;
600 }
601 break;
602 default:;
603 }
604 } else {
605 long time_tmp = 0;
607 dprintf("- it is not an existing vname\n");
608 switch (gdp->gf) {
609 case GF_VRULE:
610 k = 0;
611 sscanf(tmpstr, "%li%n", &time_tmp, &k);
612 gdp->xrule = time_tmp;
613 if (((j != 0) && (k == j)) || ((j == 0) && (k == i))) {
614 dprintf("- found time: %li\n", gdp->xrule);
615 } else {
616 dprintf("- is is not a valid number: %li\n", gdp->xrule);
617 rrd_set_error
618 ("parameter '%s' does not represent time in line %s\n",
619 tmpstr, line);
620 return 1;
621 }
622 default:
623 k = 0;
624 sscanf(tmpstr, "%lf%n", &gdp->yrule, &k);
625 if (((j != 0) && (k == j)) || ((j == 0) && (k == i))) {
626 dprintf("- found number: %lf\n", gdp->yrule);
627 } else {
628 dprintf("- is is not a valid number: %lf\n", gdp->yrule);
629 rrd_set_error
630 ("parameter '%s' does not represent a number in line %s\n",
631 tmpstr, line);
632 return 1;
633 }
634 }
635 }
637 if (j) {
638 j++;
639 dprintf("- examining color '%s'\n", &tmpstr[j]);
640 if (rrd_parse_color(&tmpstr[j], gdp)) {
641 rrd_set_error("Could not parse color in '%s'", &tmpstr[j]);
642 return 1;
643 }
644 dprintf("- parsed color %0.0f,%0.0f,%0.0f,%0.0f\n", gdp->col.red,
645 gdp->col.green, gdp->col.blue, gdp->col.alpha);
646 colorfound = 1;
647 } else {
648 dprintf("- no color present in '%s'\n", tmpstr);
649 }
651 (*eaten) += i; /* after vname#color */
652 if (line[*eaten] != '\0') {
653 (*eaten)++; /* after colon */
654 }
656 if (gdp->gf == GF_TICK) {
657 dprintf("- parsing '%s'\n", &line[*eaten]);
658 dprintf("- looking for optional TICK number\n");
659 j = 0;
660 sscanf(&line[*eaten], "%lf%n", &gdp->yrule, &j);
661 if (j) {
662 if (line[*eaten + j] != '\0' && line[*eaten + j] != ':') {
663 rrd_set_error("Cannot parse TICK fraction '%s'", line);
664 return 1;
665 }
666 dprintf("- found number %f\n", gdp->yrule);
667 if (gdp->yrule > 1.0 || gdp->yrule < -1.0) {
668 rrd_set_error("Tick factor should be <= 1.0");
669 return 1;
670 }
671 (*eaten) += j;
672 } else {
673 dprintf("- not found, defaulting to 0.1\n");
674 gdp->yrule = 0.1;
675 }
676 if (line[*eaten] == '\0') {
677 dprintf("- done parsing line\n");
678 return 0;
679 } else {
680 if (line[*eaten] == ':') {
681 (*eaten)++;
682 } else {
683 rrd_set_error("Can't make sense of that TICK line");
684 return 1;
685 }
686 }
687 }
689 dprintf("- parsing '%s'\n", &line[*eaten]);
691 /* Legend is next. A legend without a color is an error.
692 ** Stacking an item without having a legend is OK however
693 ** then an empty legend should be specified.
694 ** LINE:val#color:STACK means legend is string "STACK"
695 ** LINE:val#color::STACK means no legend, and do STACK
696 ** LINE:val:STACK is an error (legend but no color)
697 ** LINE:val::STACK means no legend, and do STACK
698 */
699 if (colorfound) {
700 int err = 0;
701 char *linecp = strdup(line);
703 dprintf("- looking for optional legend\n");
705 dprintf("- examining '%s'\n", &line[*eaten]);
706 if (linecp[*eaten] != '\0' && linecp[*eaten] != ':') {
707 int spi;
709 /* If the legend is not empty, it has to be prefixed with spacecnt ' ' characters. This then gets
710 * replaced by the color box later on. */
711 for (spi = 0; spi < spacecnt && (*eaten) > 1; spi++) {
712 linecp[--(*eaten)] = ' ';
713 }
714 }
716 if (rrd_parse_legend(linecp, eaten, gdp))
717 err = 1;
718 free(linecp);
719 if (err)
720 return 1;
722 dprintf("- found legend '%s'\n", &gdp->legend[2]);
723 } else {
724 dprintf("- skipping empty legend\n");
725 if (line[*eaten] != '\0' && line[*eaten] != ':') {
726 rrd_set_error("Legend set but no color: %s", &line[*eaten]);
727 return 1;
728 }
729 }
730 if (line[*eaten] == '\0') {
731 dprintf("- done parsing line\n");
732 return 0;
733 }
734 (*eaten)++; /* after colon */
736 /* HRULE, VRULE and TICK cannot be stacked. */
737 if ((gdp->gf != GF_HRULE)
738 && (gdp->gf != GF_VRULE)
739 && (gdp->gf != GF_TICK)) {
741 dprintf("- parsing '%s', looking for STACK\n", &line[*eaten]);
742 j = scan_for_col(&line[*eaten], 5, tmpstr);
743 if (!strcmp("STACK", tmpstr)) {
744 dprintf("- found STACK\n");
745 gdp->stack = 1;
746 (*eaten) += j;
747 if (line[*eaten] == ':') {
748 (*eaten) += 1;
749 } else if (line[*eaten] == '\0') {
750 dprintf("- done parsing line\n");
751 return 0;
752 } else {
753 dprintf("- found %s instead of just STACK\n", &line[*eaten]);
754 rrd_set_error("STACK expected but %s found", &line[*eaten]);
755 return 1;
756 }
757 } else
758 dprintf("- not STACKing\n");
759 }
761 dprintf("- still more, should be dashes[=...]\n");
762 dprintf("- parsing '%s'\n", &line[*eaten]);
763 if (line[*eaten] != '\0') {
764 /* parse dash arguments here. Possible options:
765 - dashes
766 - dashes=n_on[,n_off[,n_on,n_off]]
767 - dashes[=n_on[,n_off[,n_on,n_off]]]:dash-offset=offset
768 allowing 64 characters for definition of dash style */
769 j = scan_for_col(&line[*eaten], 64, tmpstr);
770 /* start with dashes */
771 if (strcmp(tmpstr, "dashes") == 0) {
772 /* if line was "dashes" or "dashes:dash-offset=xdashes="
773 tmpstr will be "dashes" */
774 dprintf("- found %s\n", tmpstr);
775 /* initialise all required variables we need for dashed lines
776 using default dash length of 5 pixels */
777 gdp->dash = 1;
778 gdp->p_dashes = (double *) malloc(sizeof(double));
779 gdp->p_dashes[0] = 5;
780 gdp->ndash = 1;
781 gdp->offset = 0;
782 (*eaten) += j;
783 } else if (sscanf(tmpstr, "dashes=%s", tmpstr)) {
784 /* dashes=n_on[,n_off[,n_on,n_off]] */
785 char csv[64];
786 char *pch;
787 float dsh;
788 int count = 0;
789 char *saveptr;
791 strcpy(csv, tmpstr);
793 pch = strtok_r(tmpstr, ",", &saveptr);
794 while (pch != NULL) {
795 pch = strtok_r(NULL, ",", &saveptr);
796 count++;
797 }
798 dprintf("- %d dash value(s) found: ", count);
799 if (count > 0) {
800 gdp->dash = 1;
801 gdp->ndash = count;
802 gdp->p_dashes = (double *) malloc(sizeof(double) * count);
803 pch = strtok_r(csv, ",", &saveptr);
804 count = 0;
805 while (pch != NULL) {
806 if (sscanf(pch, "%f", &dsh)) {
807 gdp->p_dashes[count] = (double) dsh;
808 dprintf("%.1f ", gdp->p_dashes[count]);
809 count++;
810 }
811 pch = strtok_r(NULL, ",", &saveptr);
812 }
813 dprintf("\n");
814 } else
815 dprintf("- syntax error. No dash lengths found!\n");
816 (*eaten) += j;
817 } else
818 dprintf("- error: expected dashes[=...], found %s\n", tmpstr);
819 if (line[*eaten] == ':') {
820 (*eaten) += 1;
821 } else if (line[*eaten] == '\0') {
822 dprintf("- done parsing line\n");
823 return 0;
824 }
825 /* dashes[=n_on[,n_off[,n_on,n_off]]]:dash-offset=offset
826 allowing 16 characters for dash-offset=....
827 => 4 characters for the offset value */
828 j = scan_for_col(&line[*eaten], 16, tmpstr);
829 if (sscanf(tmpstr, "dash-offset=%lf", &gdp->offset)) {
830 dprintf("- found dash-offset=%.1f\n", gdp->offset);
831 gdp->dash = 1;
832 (*eaten) += j;
833 if (line[*eaten] == ':')
834 (*eaten) += 1;
835 }
836 if (line[*eaten] == '\0') {
837 dprintf("- done parsing line\n");
838 return 0;
839 }
840 }
841 if (line[*eaten] == '\0') {
842 dprintf("- done parsing line\n");
843 return 0;
844 }
845 (*eaten)++;
846 dprintf("- parsing '%s'\n", &line[*eaten]);
848 return 0;
849 }
851 int rrd_parse_make_vname(
852 const char *const line,
853 unsigned int *const eaten,
854 graph_desc_t *const gdp,
855 image_desc_t *const im)
856 {
857 char tmpstr[MAX_VNAME_LEN + 10];
858 int i = 0;
860 sscanf(&line[*eaten], DEF_NAM_FMT "=%n", tmpstr, &i);
861 if (!i) {
862 rrd_set_error("Cannot parse vname from '%s'", line);
863 return 1;
864 }
865 if (line[*eaten+i] == '\0') {
866 rrd_set_error("String ends after the = sign on '%s'", line);
867 return 1;
868 }
869 dprintf("- found candidate '%s'\n", tmpstr);
871 if ((gdp->vidx = find_var(im, tmpstr)) >= 0) {
872 rrd_set_error("Attempting to reuse '%s'", im->gdes[gdp->vidx].vname);
873 return 1;
874 }
875 strcpy(gdp->vname, tmpstr);
876 dprintf("- created vname '%s' vidx %lu\n", gdp->vname, im->gdes_c - 1);
877 (*eaten) += i;
878 return 0;
879 }
881 int rrd_parse_def(
882 const char *const line,
883 unsigned int *const eaten,
884 graph_desc_t *const gdp,
885 image_desc_t *const im)
886 {
887 int i = 0;
888 char command[7]; /* step, start, end, reduce */
889 char tmpstr[256];
890 rrd_time_value_t start_tv, end_tv;
891 time_t start_tmp = 0, end_tmp = 0;
892 char *parsetime_error = NULL;
894 start_tv.type = end_tv.type = ABSOLUTE_TIME;
895 start_tv.offset = end_tv.offset = 0;
896 localtime_r(&gdp->start, &start_tv.tm);
897 localtime_r(&gdp->end, &end_tv.tm);
899 dprintf("- parsing '%s'\n", &line[*eaten]);
900 dprintf("- from line '%s'\n", line);
902 if (rrd_parse_make_vname(line, eaten, gdp, im))
903 return 1;
904 i = scan_for_col(&line[*eaten], sizeof(gdp->rrd) - 1, gdp->rrd);
905 if (line[*eaten + i] != ':') {
906 rrd_set_error("Problems reading database name");
907 return 1;
908 }
909 (*eaten) += ++i;
910 dprintf("- using file '%s'\n", gdp->rrd);
912 i = 0;
913 sscanf(&line[*eaten], DS_NAM_FMT ":%n", gdp->ds_nam, &i);
914 if (!i) {
915 rrd_set_error("Cannot parse DS in '%s'", line);
916 return 1;
917 }
918 (*eaten) += i;
919 dprintf("- using DS '%s'\n", gdp->ds_nam);
921 if (rrd_parse_CF(line, eaten, gdp, &gdp->cf))
922 return 1;
923 gdp->cf_reduce = gdp->cf;
925 if (line[*eaten] == '\0')
926 return 0;
928 while (1) {
929 dprintf("- optional parameter follows: %s\n", &line[*eaten]);
930 i = 0;
931 sscanf(&line[*eaten], "%6[a-z]=%n", command, &i);
932 if (!i) {
933 rrd_set_error("Parse error in '%s'", line);
934 return 1;
935 }
936 (*eaten) += i;
937 dprintf("- processing '%s'\n", command);
938 if (!strcmp("reduce", command)) {
939 if (rrd_parse_CF(line, eaten, gdp, &gdp->cf_reduce))
940 return 1;
941 if (line[*eaten] != '\0')
942 (*eaten)--;
943 } else if (!strcmp("step", command)) {
944 i = 0;
945 sscanf(&line[*eaten], "%lu%n", &gdp->step, &i);
946 gdp->step_orig = gdp->step;
947 (*eaten) += i;
948 dprintf("- using step %lu\n", gdp->step);
949 } else if (!strcmp("start", command)) {
950 i = scan_for_col(&line[*eaten], 255, tmpstr);
951 (*eaten) += i;
952 if ((parsetime_error = rrd_parsetime(tmpstr, &start_tv))) {
953 rrd_set_error("start time: %s", parsetime_error);
954 return 1;
955 }
956 dprintf("- done parsing: '%s'\n", &line[*eaten]);
957 } else if (!strcmp("end", command)) {
958 i = scan_for_col(&line[*eaten], 255, tmpstr);
959 (*eaten) += i;
960 if ((parsetime_error = rrd_parsetime(tmpstr, &end_tv))) {
961 rrd_set_error("end time: %s", parsetime_error);
962 return 1;
963 }
964 dprintf("- done parsing: '%s'\n", &line[*eaten]);
965 } else {
966 rrd_set_error("Parse error in '%s'", line);
967 return 1;
968 }
969 if (line[*eaten] == '\0')
970 break;
971 if (line[*eaten] != ':') {
972 dprintf("- Expected to see end of string but got '%s'\n",
973 &line[*eaten]);
974 rrd_set_error("Parse error in '%s'", line);
975 return 1;
976 }
977 (*eaten)++;
978 }
979 if (rrd_proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) {
980 /* error string is set in rrd_parsetime.c */
981 return 1;
982 }
983 if (start_tmp < 3600 * 24 * 365 * 10) {
984 rrd_set_error("the first entry to fetch should be "
985 "after 1980 (%ld)", start_tmp);
986 return 1;
987 }
989 if (end_tmp < start_tmp) {
990 rrd_set_error("start (%ld) should be less than end (%ld)",
991 start_tmp, end_tmp);
992 return 1;
993 }
995 gdp->start = start_tmp;
996 gdp->end = end_tmp;
997 gdp->start_orig = start_tmp;
998 gdp->end_orig = end_tmp;
1000 dprintf("- start time %lu\n", gdp->start);
1001 dprintf("- end time %lu\n", gdp->end);
1003 return 0;
1004 }
1006 int rrd_parse_vdef(
1007 const char *const line,
1008 unsigned int *const eaten,
1009 graph_desc_t *const gdp,
1010 image_desc_t *const im)
1011 {
1012 char tmpstr[MAX_VNAME_LEN + 1]; /* vname\0 */
1013 int i = 0;
1015 dprintf("- parsing '%s'\n", &line[*eaten]);
1016 if (rrd_parse_make_vname(line, eaten, gdp, im))
1017 return 1;
1019 sscanf(&line[*eaten], DEF_NAM_FMT ",%n", tmpstr, &i);
1020 if (!i) {
1021 rrd_set_error("Cannot parse line '%s'", line);
1022 return 1;
1023 }
1024 if ((gdp->vidx = find_var(im, tmpstr)) < 0) {
1025 rrd_set_error("Not a valid vname: %s in line %s", tmpstr, line);
1026 return 1;
1027 }
1028 if (im->gdes[gdp->vidx].gf != GF_DEF && im->gdes[gdp->vidx].gf != GF_CDEF) {
1029 rrd_set_error("variable '%s' not DEF nor "
1030 "CDEF in VDEF '%s'", tmpstr, gdp->vname);
1031 return 1;
1032 }
1033 dprintf("- found vname: '%s' vidx %li\n", tmpstr, gdp->vidx);
1034 (*eaten) += i;
1036 dprintf("- calling vdef_parse with param '%s'\n", &line[*eaten]);
1037 vdef_parse(gdp, &line[*eaten]);
1038 while (line[*eaten] != '\0' && line[*eaten] != ':')
1039 (*eaten)++;
1041 return 0;
1042 }
1044 int rrd_parse_cdef(
1045 const char *const line,
1046 unsigned int *const eaten,
1047 graph_desc_t *const gdp,
1048 image_desc_t *const im)
1049 {
1050 dprintf("- parsing '%s'\n", &line[*eaten]);
1051 if (rrd_parse_make_vname(line, eaten, gdp, im))
1052 return 1;
1053 if ((gdp->rpnp = rpn_parse((void *) im, &line[*eaten], &find_var_wrapper)
1054 ) == NULL) {
1055 rrd_set_error("invalid rpn expression in: %s", &line[*eaten]);
1056 return 1;
1057 };
1058 while (line[*eaten] != '\0' && line[*eaten] != ':')
1059 (*eaten)++;
1060 return 0;
1061 }
1063 void rrd_graph_script(
1064 int argc,
1065 char *argv[],
1066 image_desc_t *const im,
1067 int optno)
1068 {
1069 int i;
1071 /* save state for STACK backward compat function */
1072 enum gf_en last_gf = GF_PRINT;
1073 float last_linewidth = 0.0;
1075 for (i = optind + optno; i < argc; i++) {
1076 graph_desc_t *gdp;
1077 unsigned int eaten = 0;
1079 if (gdes_alloc(im))
1080 return; /* the error string is already set */
1081 gdp = &im->gdes[im->gdes_c - 1];
1082 #ifdef DEBUG
1083 gdp->debug = 1;
1084 #endif
1086 if (rrd_parse_find_gf(argv[i], &eaten, gdp))
1087 return;
1089 switch (gdp->gf) {
1090 case GF_SHIFT: /* vname:value */
1091 if (rrd_parse_shift(argv[i], &eaten, gdp, im))
1092 return;
1093 break;
1094 case GF_TEXTALIGN: /* left|right|center|justified */
1095 if (rrd_parse_textalign(argv[i], &eaten, gdp))
1096 return;
1097 break;
1098 case GF_XPORT:
1099 if (rrd_parse_xport(argv[i], &eaten, gdp, im))
1100 return;
1101 break;
1102 case GF_PRINT: /* vname:CF:format -or- vname:format */
1103 im->prt_c++;
1104 case GF_GPRINT: /* vname:CF:format -or- vname:format */
1105 if (rrd_parse_print(argv[i], &eaten, gdp, im))
1106 return;
1107 break;
1108 case GF_COMMENT: /* text */
1109 if (rrd_parse_legend(argv[i], &eaten, gdp))
1110 return;
1111 break;
1112 case GF_VRULE: /* value#color[:legend] */
1113 case GF_HRULE: /* value#color[:legend] */
1114 case GF_LINE: /* vname-or-value[#color[:legend]][:STACK] */
1115 case GF_AREA: /* vname-or-value[#color[:legend]][:STACK] */
1116 case GF_TICK: /* vname#color[:num[:legend]] */
1117 if (rrd_parse_PVHLAST(argv[i], &eaten, gdp, im))
1118 return;
1119 last_gf = gdp->gf;
1120 last_linewidth = gdp->linewidth;
1121 break;
1122 case GF_STACK: /* vname-or-value[#color[:legend]] */
1123 if (rrd_parse_PVHLAST(argv[i], &eaten, gdp, im))
1124 return;
1125 if (last_gf == GF_LINE || last_gf == GF_AREA) {
1126 gdp->gf = last_gf;
1127 gdp->linewidth = last_linewidth;
1128 } else {
1129 rrd_set_error("STACK must follow LINE or AREA! command:\n%s",
1130 &argv[i][eaten], argv[i]);
1131 return;
1132 }
1133 break;
1134 /* data acquisition */
1135 case GF_DEF: /* vname=x:DS:CF:[:step=#][:start=#][:end=#] */
1136 if (rrd_parse_def(argv[i], &eaten, gdp, im))
1137 return;
1138 break;
1139 case GF_CDEF: /* vname=rpn-expression */
1140 if (rrd_parse_cdef(argv[i], &eaten, gdp, im))
1141 return;
1142 break;
1143 case GF_VDEF: /* vname=rpn-expression */
1144 if (rrd_parse_vdef(argv[i], &eaten, gdp, im))
1145 return;
1146 break;
1147 }
1148 if (gdp->debug) {
1149 dprintf("used %i out of %zi chars\n", eaten, strlen(argv[i]));
1150 dprintf("parsed line: '%s'\n", argv[i]);
1151 dprintf("remaining: '%s'\n", &argv[i][eaten]);
1152 if (eaten >= strlen(argv[i]))
1153 dprintf("Command finished successfully\n");
1154 }
1155 if (eaten < strlen(argv[i])) {
1156 rrd_set_error("I don't understand '%s' in command: '%s'.",
1157 &argv[i][eaten], argv[i]);
1158 return;
1159 }
1160 }
1161 }