3 #define dprintf if (gdp->debug) printf
6 rrd_parse_find_gf(char *line, unsigned int *eaten, graph_desc_t *gdp) {
7 char funcname[11],c1=0,c2=0;
10 sscanf(&line[*eaten], "DEBUG%n", &i);
15 dprintf("Scanning line '%s'\n",&line[*eaten]);
17 sscanf(&line[*eaten], "%10[A-Z]%n%c%c", funcname, &i, &c1, &c2);
19 rrd_set_error("Could not make sense out of '%s'",line);
22 if ((int)(gdp->gf=gf_conv(funcname)) == -1) {
23 rrd_set_error("'%s' is not a valid function name", funcname);
26 if (gdp->gf == GF_LINE) {
27 if (c1 < '1' || c1 > '3' || c2 != ':') {
28 rrd_set_error("Malformed LINE command: %s",line);
31 gdp->linewidth=c1-'0';
35 rrd_set_error("Malformed %s command: %s",funcname,line);
44 rrd_parse_legend(char *line, unsigned int *eaten, graph_desc_t *gdp) {
47 dprintf("- examining '%s'\n",&line[*eaten]);
49 i=scan_for_col(&line[*eaten],FMT_LEG_LEN,gdp->legend);
52 if (line[*eaten]!='\0' && line[*eaten]!=':') {
53 rrd_set_error("Legend too long");
56 dprintf("- found legend '%s'\n", gdp->legend);
62 rrd_parse_color(char *string, graph_desc_t *gdp) {
63 unsigned int r=0,g=0,b=0,a=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 */
72 if (!i1) return 1; /* no color after '#' */
73 gdp->col = r<<24|g<<16|b<<8|a;
78 rrd_parse_CF(char *line, unsigned int *eaten, graph_desc_t *gdp) {
79 char symname[CF_NAM_SIZE];
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);
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);
95 if (line[*eaten]!='\0') (*eaten)++;
99 /* Parsing old-style xPRINT and new-style xPRINT */
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
106 char tmpstr[MAX_VNAME_LEN+1];
109 sscanf(&line[*eaten], DEF_NAM_FMT ":%n", tmpstr,&i);
111 rrd_set_error("Could not parse line '%s'",line);
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);
121 switch (im->gdes[gdp->vidx].gf) {
124 dprintf("- vname is of type DEF or CDEF, looking for CF\n");
125 if (rrd_parse_CF(line,eaten,gdp)) return 1;
128 dprintf("- vname is of type VDEF\n");
131 rrd_set_error("Encountered unknown type variable '%s'",tmpstr);
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);
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.
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.
156 rrd_parse_PVHLAST(char *line, unsigned int *eaten, graph_desc_t *gdp, image_desc_t *im) {
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);
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]);
178 dprintf("- parsed color 0x%08x\n",(unsigned int)gdp->col);
182 dprintf("- examining '%s'\n",tmpstr);
184 if (gdp->gf == GF_VRULE) {
185 sscanf(tmpstr,"%li%n",&gdp->xrule,&j);
186 if (j) dprintf("- found time: %li\n",gdp->xrule);
188 sscanf(tmpstr,"%lf%n",&gdp->yrule,&j);
189 if (j) dprintf("- found number: %f\n",gdp->yrule);
192 if ((gdp->vidx=find_var(im,tmpstr))<0) {
193 rrd_set_error("Not a valid vname: %s in line %s",tmpstr,line);
196 dprintf("- found vname: '%s' vidx %li\n",tmpstr,gdp->vidx);
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.
202 if (line[*eaten+i]=='\0') {
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
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);
218 dprintf("- found number %f\n",gdp->yrule);
220 if (gdp->yrule > 1.0 || gdp->yrule < -1.0) {
221 rrd_set_error("Tick factor should be <= 1.0");
225 dprintf("- not found, defaulting to 0.1\n");
230 dprintf("- looking for optional legend\n");
231 dprintf("- in '%s'\n",&line[*eaten]);
232 if (rrd_parse_legend(line, eaten, gdp)) return 1;
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)
242 if (line[*eaten]!='\0') {
243 dprintf("- still more, should be STACK\n");
245 j=scan_for_col(&line[*eaten],5,tmpstr);
246 if (line[*eaten+j]!='\0') {
247 rrd_set_error("Garbage found where STACK expected");
250 if (!strcmp("STACK",tmpstr)) {
251 dprintf("- found STACK\n");
255 rrd_set_error("Garbage found where STACK expected");
264 rrd_parse_vname(char *line, unsigned int *eaten, graph_desc_t *gdp, image_desc_t *im) {
265 char tmpstr[MAX_VNAME_LEN + 10];
268 sscanf(&line[*eaten], DEF_NAM_FMT "=%n", tmpstr,&i);
270 rrd_set_error("Cannot parse vname from '%s'",line);
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);
279 strcpy(gdp->vname,tmpstr);
280 dprintf("- created vname '%s' vidx %lu\n", gdp->vname,im->gdes_c-1);
286 rrd_parse_def(char *line, unsigned int *eaten, graph_desc_t *gdp, image_desc_t *im) {
288 char command[6]; /* step, start, end */
290 struct 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 memcpy(&start_tv.tm, localtime(&gdp->start) , sizeof(struct tm) );
297 memcpy(&end_tv.tm, localtime(&gdp->end) , sizeof(struct 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");
309 dprintf("- using file '%s'\n",gdp->rrd);
312 sscanf(&line[*eaten], DS_NAM_FMT ":%n", gdp->ds_nam,&i);
314 rrd_set_error("Cannot parse DS in '%s'",line);
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;
325 dprintf("- optional parameter follows: %s\n", &line[*eaten]);
327 sscanf(&line[*eaten], "%5[a-z]=%n", command, &i);
329 rrd_set_error("Parse error in '%s'",line);
333 dprintf("- processing '%s'\n",command);
334 if (!strcmp("step",command)) {
336 sscanf(&line[*eaten],"%lu%n",&gdp->step,&i);
338 dprintf("- using step %lu\n",gdp->step);
339 } else if (!strcmp("start",command)) {
340 i=scan_for_col(&line[*eaten],255,tmpstr);
342 if ((parsetime_error = parsetime(tmpstr, &start_tv))) {
343 rrd_set_error( "start time: %s", parsetime_error );
346 dprintf("- done parsing: '%s'\n",&line[*eaten]);
347 } else if (!strcmp("end",command)) {
348 i=scan_for_col(&line[*eaten],255,tmpstr);
350 if ((parsetime_error = parsetime(tmpstr, &end_tv))) {
351 rrd_set_error( "end time: %s", parsetime_error );
354 dprintf("- done parsing: '%s'\n",&line[*eaten]);
356 rrd_set_error("Parse error in '%s'",line);
359 if (line[*eaten]=='\0') break;
360 if (line[*eaten]!=':') {
361 dprintf("- Expected to see end of string but got '%s'\n",\
363 rrd_set_error("Parse error in '%s'",line);
368 if (proc_start_end(&start_tv,&end_tv,&start_tmp,&end_tmp) == -1){
369 /* error string is set in parsetime.c */
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);
378 if (end_tmp < start_tmp) {
379 rrd_set_error("start (%ld) should be less than end (%ld)",
384 gdp->start = start_tmp;
387 dprintf("- start time %lu\n",gdp->start);
388 dprintf("- end time %lu\n",gdp->end);
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 */
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);
403 rrd_set_error("Cannot parse line '%s'",line);
406 if ((gdp->vidx=find_var(im,tmpstr))<0) {
407 rrd_set_error("Not a valid vname: %s in line %s",tmpstr,line);
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);
416 dprintf("- found vname: '%s' vidx %li\n",tmpstr,gdp->vidx);
419 dprintf("- calling vdef_parse with param '%s'\n",&line[*eaten]);
420 vdef_parse(gdp,&line[*eaten]);
421 while (line[*eaten]!='\0'&&line[*eaten]!=':')
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(
436 rrd_set_error("invalid rpn expression in: %s",&line[*eaten]);
439 while (line[*eaten]!='\0'&&line[*eaten]!=':')
445 rrd_graph_script(int argc, char *argv[], image_desc_t *im) {
448 for (i=optind+1;i<argc;i++) {
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];
458 if (rrd_parse_find_gf(argv[i],&eaten,gdp)) return;
463 case GF_SHIFT: vname:value
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;
471 case GF_COMMENT: /* text */
472 if (rrd_parse_legend(argv[i],&eaten,gdp)) return;
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;
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;
487 case GF_CDEF: /* vname=rpn-expression */
488 if (rrd_parse_cdef(argv[i],&eaten,gdp,im)) return;
490 case GF_VDEF: /* vname=rpn-expression */
491 if (rrd_parse_vdef(argv[i],&eaten,gdp,im)) return;
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");
501 if (eaten < strlen(argv[i])) {
502 rrd_set_error("Garbage '%s' after command:\n%s",
503 &argv[i][eaten],argv[i]);