1 #include "rrd_graph.h"
3 #define dprintf if (gdp->debug) printf
5 int
6 rrd_parse_find_gf(char *line, unsigned int *eaten, graph_desc_t *gdp) {
7 char funcname[11],c1=0,c2=0;
8 int i=0;
10 sscanf(&line[*eaten], "DEBUG%n", &i);
11 if (i) {
12 gdp->debug=1;
13 (*eaten)+=i;
14 i=0;
15 dprintf("Scanning line '%s'\n",&line[*eaten]);
16 }
17 sscanf(&line[*eaten], "%10[A-Z]%n%c%c", funcname, &i, &c1, &c2);
18 if (!i) {
19 rrd_set_error("Could not make sense out of '%s'",line);
20 return 1;
21 }
22 if ((int)(gdp->gf=gf_conv(funcname)) == -1) {
23 rrd_set_error("'%s' is not a valid function name", funcname);
24 return 1;
25 }
26 if (gdp->gf == GF_LINE) {
27 if (c1 < '1' || c1 > '3' || c2 != ':') {
28 rrd_set_error("Malformed LINE command: %s",line);
29 return 1;
30 }
31 gdp->linewidth=c1-'0';
32 i++;
33 } else {
34 if (c1 != ':') {
35 rrd_set_error("Malformed %s command: %s",funcname,line);
36 return 1;
37 }
38 }
39 *eaten+=++i;
40 return 0;
41 }
43 int
44 rrd_parse_legend(char *line, unsigned int *eaten, graph_desc_t *gdp) {
45 int i;
47 dprintf("- examining '%s'\n",&line[*eaten]);
49 i=scan_for_col(&line[*eaten],FMT_LEG_LEN,gdp->legend);
51 *eaten += i;
52 if (line[*eaten]!='\0' && line[*eaten]!=':') {
53 rrd_set_error("Legend too long");
54 return 1;
55 } else {
56 dprintf("- found legend '%s'\n", gdp->legend);
57 return 0;
58 }
59 }
61 int
62 rrd_parse_color(char *string, graph_desc_t *gdp) {
63 unsigned int r=0,g=0,b=0,a=0;
64 int i1=0,i2=0,i3=0;
66 if (string[0] != '#') return 1;
67 sscanf(string, "#%02x%02x%02x%n%02x%n%*s%n",
68 &r,&g,&b,&i1,&a,&i2,&i3);
70 if (i3) return 1; /* garbage after color */
71 if (!i2) a=0xFF;
72 if (!i1) return 1; /* no color after '#' */
73 gdp->col = r<<24|g<<16|b<<8|a;
74 return 0;
75 }
77 int
78 rrd_parse_CF(char *line, unsigned int *eaten, graph_desc_t *gdp) {
79 char symname[CF_NAM_SIZE];
80 int i=0;
82 sscanf(&line[*eaten], CF_NAM_FMT "%n", symname,&i);
83 if ((!i)||((line[*eaten+i]!='\0')&&(line[*eaten+i]!=':'))) {
84 rrd_set_error("Cannot parse CF in '%s'",line);
85 return 1;
86 }
87 (*eaten)+=i;
88 dprintf("- using CF '%s'\n",symname);
90 if ((int)(gdp->cf = cf_conv(symname))==-1) {
91 rrd_set_error("Unknown CF '%s' in '%s'",symname,line);
92 return 1;
93 }
95 if (line[*eaten]!='\0') (*eaten)++;
96 return 0;
97 }
99 /* Parsing old-style xPRINT and new-style xPRINT */
100 int
101 rrd_parse_print(char *line, unsigned int *eaten, graph_desc_t *gdp, image_desc_t *im) {
102 /* vname:CF:format in case of DEF-based vname
103 ** vname:CF:format in case of CDEF-based vname
104 ** vname:format in case of VDEF-based vname
105 */
106 char tmpstr[MAX_VNAME_LEN+1];
107 int i=0;
109 sscanf(&line[*eaten], DEF_NAM_FMT ":%n", tmpstr,&i);
110 if (!i) {
111 rrd_set_error("Could not parse line '%s'",line);
112 return 1;
113 }
114 (*eaten)+=i;
115 dprintf("- Found candidate vname '%s'\n",tmpstr);
117 if ((gdp->vidx=find_var(im,tmpstr))<0) {
118 rrd_set_error("Not a valid vname: %s in line %s",tmpstr,line);
119 return 1;
120 }
121 switch (im->gdes[gdp->vidx].gf) {
122 case GF_DEF:
123 case GF_CDEF:
124 dprintf("- vname is of type DEF or CDEF, looking for CF\n");
125 if (rrd_parse_CF(line,eaten,gdp)) return 1;
126 break;
127 case GF_VDEF:
128 dprintf("- vname is of type VDEF\n");
129 break;
130 default:
131 rrd_set_error("Encountered unknown type variable '%s'",tmpstr);
132 return 1;
133 }
135 if (rrd_parse_legend(line,eaten,gdp)) return 1;
137 /* Why is there a separate structure member "format" ??? */
138 strcpy(gdp->format,gdp->legend);
140 return 0;
141 }
143 /* Parsing of PART, VRULE, HRULE, LINE, AREA, STACK and TICK
144 ** is done in one function. Stacking STACK is silently ignored
145 ** as it is redundant. Stacking PART, VRULE, HRULE or TICK is
146 ** not allowed. The check for color doesn't need to be so strict
147 ** anymore, the user can specify the color '#00000000' and
148 ** effectively circumvent this check, so why bother.
149 **
150 ** If a number (which is valid to enter) is more than a
151 ** certain amount of characters, it is caught as an error.
152 ** While this is arguable, so is entering fixed numbers
153 ** with more than MAX_VNAME_LEN significant digits.
154 */
155 int
156 rrd_parse_PVHLAST(char *line, unsigned int *eaten, graph_desc_t *gdp, image_desc_t *im) {
157 int i,j;
158 int colorfound=0;
159 char tmpstr[MAX_VNAME_LEN + 10]; /* vname#RRGGBBAA\0 */
161 dprintf("- parsing '%s'\n",&line[*eaten]);
162 dprintf("- from line '%s'\n",line);
164 i=scan_for_col(&line[*eaten],MAX_VNAME_LEN+9,tmpstr);
165 if (line[*eaten+i]!='\0' && line[*eaten+i]!=':') {
166 rrd_set_error("Cannot parse line '%s'",line);
167 return 1;
168 }
170 j=i; while (j>0 && tmpstr[j]!='#') j--;
172 if (tmpstr[j]=='#') {
173 if (rrd_parse_color(&tmpstr[j],gdp)) {
174 rrd_set_error("Could not parse color in '%s'",tmpstr[j]);
175 return 1;
176 }
177 tmpstr[j]='\0';
178 dprintf("- parsed color 0x%08x\n",(unsigned int)gdp->col);
179 colorfound=1;
180 }
182 dprintf("- examining '%s'\n",tmpstr);
183 j=0;
184 if (gdp->gf == GF_VRULE) {
185 sscanf(tmpstr,"%li%n",&gdp->xrule,&j);
186 if (j) dprintf("- found time: %li\n",gdp->xrule);
187 } else {
188 sscanf(tmpstr,"%lf%n",&gdp->yrule,&j);
189 if (j) dprintf("- found number: %f\n",gdp->yrule);
190 }
191 if (!j) {
192 if ((gdp->vidx=find_var(im,tmpstr))<0) {
193 rrd_set_error("Not a valid vname: %s in line %s",tmpstr,line);
194 return 1;
195 }
196 dprintf("- found vname: '%s' vidx %li\n",tmpstr,gdp->vidx);
197 }
198 /* "*eaten" is still pointing to the original location,
199 ** "*eaten +i" is pointing to the character after the color
200 ** or to the terminating '\0' in which case we're finished.
201 */
202 if (line[*eaten+i]=='\0') {
203 *eaten+=i;
204 return 0;
205 }
206 *eaten+=++i;
208 /* If a color is specified and the only remaining part is
209 ** ":STACK" then it is assumed to be the legend. An empty
210 ** legend can be specified as expected. This means the
211 ** following can be done: LINE1:x#FF0000FF::STACK
212 */
213 if (colorfound) { /* no legend if no color */
214 if (gdp->gf == GF_TICK) {
215 dprintf("- looking for optional number\n");
216 sscanf(&line[*eaten],"%lf:%n",&gdp->yrule,&j);
217 if (j) {
218 dprintf("- found number %f\n",gdp->yrule);
219 (*eaten)+=j;
220 if (gdp->yrule > 1.0 || gdp->yrule < -1.0) {
221 rrd_set_error("Tick factor should be <= 1.0");
222 return 1;
223 }
224 } else {
225 dprintf("- not found, defaulting to 0.1\n");
226 gdp->yrule=0.1;
227 return 0;
228 }
229 }
230 dprintf("- looking for optional legend\n");
231 dprintf("- in '%s'\n",&line[*eaten]);
232 if (rrd_parse_legend(line, eaten, gdp)) return 1;
233 }
235 /* PART, HRULE, VRULE and TICK cannot be stacked. We're finished */
236 if ( (gdp->gf == GF_HRULE)
237 || (gdp->gf == GF_VRULE)
238 || (gdp->gf == GF_PART)
239 || (gdp->gf == GF_TICK)
240 ) return 0;
242 if (line[*eaten]!='\0') {
243 dprintf("- still more, should be STACK\n");
244 (*eaten)++;
245 j=scan_for_col(&line[*eaten],5,tmpstr);
246 if (line[*eaten+j]!='\0') {
247 rrd_set_error("Garbage found where STACK expected");
248 return 1;
249 }
250 if (!strcmp("STACK",tmpstr)) {
251 dprintf("- found STACK\n");
252 gdp->stack=1;
253 (*eaten)+=5;
254 } else {
255 rrd_set_error("Garbage found where STACK expected");
256 return 1;
257 }
258 }
260 return 0;
261 }
263 int
264 rrd_parse_vname(char *line, unsigned int *eaten, graph_desc_t *gdp, image_desc_t *im) {
265 char tmpstr[MAX_VNAME_LEN + 10];
266 int i=0;
268 sscanf(&line[*eaten], DEF_NAM_FMT "=%n", tmpstr,&i);
269 if (!i) {
270 rrd_set_error("Cannot parse vname from '%s'",line);
271 return 1;
272 }
273 dprintf("- found candidate '%s'\n",tmpstr);
275 if ((gdp->vidx=find_var(im,tmpstr))>=0) {
276 rrd_set_error("Attempting to reuse '%s'",im->gdes[gdp->vidx].vname);
277 return 1;
278 }
279 strcpy(gdp->vname,tmpstr);
280 dprintf("- created vname '%s' vidx %lu\n", gdp->vname,im->gdes_c-1);
281 (*eaten)+=i;
282 return 0;
283 }
285 int
286 rrd_parse_def(char *line, unsigned int *eaten, graph_desc_t *gdp, image_desc_t *im) {
287 int i=0;
288 char command[6]; /* step, start, end */
289 char tmpstr[256];
290 struct rrd_time_value start_tv,end_tv;
291 time_t start_tmp=0,end_tmp=0;
292 char *parsetime_error=NULL;
294 start_tv.type = end_tv.type=ABSOLUTE_TIME;
295 start_tv.offset = end_tv.offset=0;
296 localtime_r(&gdp->start, &start_tv.tm);
297 localtime_r(&gdp->end, &end_tv.tm);
299 dprintf("- parsing '%s'\n",&line[*eaten]);
300 dprintf("- from line '%s'\n",line);
302 if (rrd_parse_vname(line,eaten,gdp,im)) return 1;
303 i=scan_for_col(&line[*eaten],254,gdp->rrd);
304 if (line[*eaten+i]!=':') {
305 rrd_set_error("Problems reading database name");
306 return 1;
307 }
308 (*eaten)+=++i;
309 dprintf("- using file '%s'\n",gdp->rrd);
311 i=0;
312 sscanf(&line[*eaten], DS_NAM_FMT ":%n", gdp->ds_nam,&i);
313 if (!i) {
314 rrd_set_error("Cannot parse DS in '%s'",line);
315 return 1;
316 }
317 (*eaten)+=i;
318 dprintf("- using DS '%s'\n",gdp->ds_nam);
320 if (rrd_parse_CF(line,eaten,gdp)) return 1;
322 if (line[*eaten]=='\0') return 0;
324 while (1) {
325 dprintf("- optional parameter follows: %s\n", &line[*eaten]);
326 i=0;
327 sscanf(&line[*eaten], "%5[a-z]=%n", command, &i);
328 if (!i) {
329 rrd_set_error("Parse error in '%s'",line);
330 return 1;
331 }
332 (*eaten)+=i;
333 dprintf("- processing '%s'\n",command);
334 if (!strcmp("step",command)) {
335 i=0;
336 sscanf(&line[*eaten],"%lu%n",&gdp->step,&i);
337 (*eaten)+=i;
338 dprintf("- using step %lu\n",gdp->step);
339 } else if (!strcmp("start",command)) {
340 i=scan_for_col(&line[*eaten],255,tmpstr);
341 (*eaten)+=i;
342 if ((parsetime_error = parsetime(tmpstr, &start_tv))) {
343 rrd_set_error( "start time: %s", parsetime_error );
344 return 1;
345 }
346 dprintf("- done parsing: '%s'\n",&line[*eaten]);
347 } else if (!strcmp("end",command)) {
348 i=scan_for_col(&line[*eaten],255,tmpstr);
349 (*eaten)+=i;
350 if ((parsetime_error = parsetime(tmpstr, &end_tv))) {
351 rrd_set_error( "end time: %s", parsetime_error );
352 return 1;
353 }
354 dprintf("- done parsing: '%s'\n",&line[*eaten]);
355 } else {
356 rrd_set_error("Parse error in '%s'",line);
357 return 1;
358 }
359 if (line[*eaten]=='\0') break;
360 if (line[*eaten]!=':') {
361 dprintf("- Expected to see end of string but got '%s'\n",\
362 &line[*eaten]);
363 rrd_set_error("Parse error in '%s'",line);
364 return 1;
365 }
366 (*eaten)++;
367 }
368 if (proc_start_end(&start_tv,&end_tv,&start_tmp,&end_tmp) == -1){
369 /* error string is set in parsetime.c */
370 return 1;
371 }
372 if (start_tmp < 3600*24*365*10) {
373 rrd_set_error("the first entry to fetch should be "
374 "after 1980 (%ld)",start_tmp);
375 return 1;
376 }
378 if (end_tmp < start_tmp) {
379 rrd_set_error("start (%ld) should be less than end (%ld)",
380 start_tmp, end_tmp);
381 return 1;
382 }
384 gdp->start = start_tmp;
385 gdp->end = end_tmp;
387 dprintf("- start time %lu\n",gdp->start);
388 dprintf("- end time %lu\n",gdp->end);
390 return 0;
391 }
393 int
394 rrd_parse_vdef(char *line, unsigned int *eaten, graph_desc_t *gdp, image_desc_t *im) {
395 char tmpstr[MAX_VNAME_LEN+1]; /* vname\0 */
396 int i=0;
398 dprintf("- parsing '%s'\n",&line[*eaten]);
399 if (rrd_parse_vname(line,eaten,gdp,im)) return 1;
401 sscanf(&line[*eaten], DEF_NAM_FMT ",%n", tmpstr,&i);
402 if (!i) {
403 rrd_set_error("Cannot parse line '%s'",line);
404 return 1;
405 }
406 if ((gdp->vidx=find_var(im,tmpstr))<0) {
407 rrd_set_error("Not a valid vname: %s in line %s",tmpstr,line);
408 return 1;
409 }
410 if ( im->gdes[gdp->vidx].gf != GF_DEF
411 && im->gdes[gdp->vidx].gf != GF_CDEF) {
412 rrd_set_error("variable '%s' not DEF nor "
413 "CDEF in VDEF '%s'", tmpstr,gdp->vname);
414 return 1;
415 }
416 dprintf("- found vname: '%s' vidx %li\n",tmpstr,gdp->vidx);
417 (*eaten)+=i;
419 dprintf("- calling vdef_parse with param '%s'\n",&line[*eaten]);
420 vdef_parse(gdp,&line[*eaten]);
421 while (line[*eaten]!='\0'&&line[*eaten]!=':')
422 (*eaten)++;
424 return 0;
425 }
427 int
428 rrd_parse_cdef(char *line, unsigned int *eaten, graph_desc_t *gdp, image_desc_t *im) {
429 dprintf("- parsing '%s'\n",&line[*eaten]);
430 if (rrd_parse_vname(line,eaten,gdp,im)) return 1;
431 if ((gdp->rpnp = rpn_parse(
432 (void *)im,
433 &line[*eaten],
434 &find_var_wrapper)
435 )==NULL) {
436 rrd_set_error("invalid rpn expression in: %s",&line[*eaten]);
437 return 1;
438 };
439 while (line[*eaten]!='\0'&&line[*eaten]!=':')
440 (*eaten)++;
441 return 0;
442 }
444 void
445 rrd_graph_script(int argc, char *argv[], image_desc_t *im) {
446 int i;
448 for (i=optind+1;i<argc;i++) {
449 graph_desc_t *gdp;
450 unsigned int eaten=0;
452 if (gdes_alloc(im)) return; /* the error string is already set */
453 gdp = &im->gdes[im->gdes_c-1];
454 #ifdef DEBUG
455 gdp->debug = 1;
456 #endif
458 if (rrd_parse_find_gf(argv[i],&eaten,gdp)) return;
460 switch (gdp->gf) {
461 #if 0
462 /* future command */
463 case GF_SHIFT: vname:value
464 #endif
465 case GF_XPORT:
466 break;
467 case GF_PRINT: /* vname:CF:format -or- vname:format */
468 case GF_GPRINT: /* vname:CF:format -or- vname:format */
469 if (rrd_parse_print(argv[i],&eaten,gdp,im)) return;
470 break;
471 case GF_COMMENT: /* text */
472 if (rrd_parse_legend(argv[i],&eaten,gdp)) return;
473 break;
474 case GF_PART: /* value[#color[:legend]] */
475 case GF_VRULE: /* value#color[:legend] */
476 case GF_HRULE: /* value#color[:legend] */
477 case GF_LINE: /* vname-or-value[#color[:legend]][:STACK] */
478 case GF_AREA: /* vname-or-value[#color[:legend]][:STACK] */
479 case GF_STACK: /* vname-or-value[#color[:legend]] */
480 case GF_TICK: /* vname#color[:num[:legend]] */
481 if (rrd_parse_PVHLAST(argv[i],&eaten,gdp,im)) return;
482 break;
483 /* data acquisition */
484 case GF_DEF: /* vname=x:DS:CF:[:step=#][:start=#][:end=#] */
485 if (rrd_parse_def(argv[i],&eaten,gdp,im)) return;
486 break;
487 case GF_CDEF: /* vname=rpn-expression */
488 if (rrd_parse_cdef(argv[i],&eaten,gdp,im)) return;
489 break;
490 case GF_VDEF: /* vname=rpn-expression */
491 if (rrd_parse_vdef(argv[i],&eaten,gdp,im)) return;
492 break;
493 }
494 if (gdp->debug) {
495 dprintf("used %i out of %i chars\n",eaten,strlen(argv[i]));
496 dprintf("parsed line: '%s'\n",argv[i]);
497 dprintf("remaining: '%s'\n",&argv[i][eaten]);
498 if (eaten >= strlen(argv[i]))
499 dprintf("Command finished successfully\n");
500 }
501 if (eaten < strlen(argv[i])) {
502 rrd_set_error("Garbage '%s' after command:\n%s",
503 &argv[i][eaten],argv[i]);
504 return;
505 }
506 }
507 }