From: oetiker Date: Fri, 6 May 2005 15:44:11 +0000 (+0000) Subject: * allow for 3(4) letter color names X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=dede4bb63bc4f4e856c636eb39632c13acb4189d;p=rrdtool.git * allow for 3(4) letter color names * better debugging code * fix tick, line, stack parsing -- Alex van den Bogaerdt git-svn-id: svn://svn.oetiker.ch/rrdtool/branches/1.2/program@489 a5681a0c-68f1-0310-ab6d-d61299d08faa --- diff --git a/src/rrd_graph_helper.c b/src/rrd_graph_helper.c index 595486a..1f84f7d 100644 --- a/src/rrd_graph_helper.c +++ b/src/rrd_graph_helper.c @@ -37,6 +37,7 @@ rrd_parse_find_gf(char *line, unsigned int *eaten, graph_desc_t *gdp) { char funcname[11],c1=0; int i=0; + /* start an argument with DEBUG to be able to see how it is parsed */ sscanf(&line[*eaten], "DEBUG%n", &i); if (i) { gdp->debug=1; @@ -52,23 +53,28 @@ rrd_parse_find_gf(char *line, unsigned int *eaten, graph_desc_t *gdp) { if ((int)(gdp->gf=gf_conv(funcname)) == -1) { rrd_set_error("'%s' is not a valid function name", funcname); return 1; + } else { + dprintf("- found function name '%s'\n",funcname); } + if (gdp->gf == GF_LINE) { - if (c1 == ':'){ - gdp->linewidth=1; + if (c1 == ':') { + gdp->linewidth=1; + dprintf("- - using default width of 1\n"); } else { - double width; - *eaten+=i; - if (sscanf(&line[*eaten],"%lf%n:",&width,&i)){ - if (width < 0 || isnan(width) || isinf(width) ){ - rrd_set_error("LINE width is %lf. It must be finite and >= 0 though",width); - return 1; - } - gdp->linewidth=width; - } else { - rrd_set_error("LINE width: %s",line); - return 1; + double width; + (*eaten)+=i; + if (sscanf(&line[*eaten],"%lf%n:",&width,&i)) { + if (width < 0 || isnan(width) || isinf(width) ) { + rrd_set_error("LINE width is %lf. It must be finite and >= 0 though",width); + return 1; } + gdp->linewidth=width; + dprintf("- - using width %f\n",width); + } else { + rrd_set_error("LINE width: %s",line); + return 1; + } } } else { if (c1 != ':') { @@ -76,7 +82,7 @@ rrd_parse_find_gf(char *line, unsigned int *eaten, graph_desc_t *gdp) { return 1; } } - *eaten+=++i; + (*eaten)+=++i; return 0; } @@ -84,32 +90,55 @@ int rrd_parse_legend(char *line, unsigned int *eaten, graph_desc_t *gdp) { int i; - dprintf("- examining '%s'\n",&line[*eaten]); + if (line[*eaten]=='\0' || line[*eaten]==':') { + dprintf("- no (or: empty) legend found\n"); + return 0; + } i=scan_for_col(&line[*eaten],FMT_LEG_LEN,gdp->legend); - *eaten += i; + (*eaten)+=i; + if (line[*eaten]!='\0' && line[*eaten]!=':') { rrd_set_error("Legend too long"); return 1; } else { - dprintf("- found legend '%s'\n", gdp->legend); return 0; } } int rrd_parse_color(char *string, graph_desc_t *gdp) { - unsigned int r=0,g=0,b=0,a=0; - int i1=0,i2=0,i3=0; + unsigned int r=0,g=0,b=0,a=0,i; - if (string[0] != '#') return 1; - sscanf(string, "#%02x%02x%02x%n%02x%n%*s%n", - &r,&g,&b,&i1,&a,&i2,&i3); + /* matches the following formats: + ** RGB + ** RGBA + ** RRGGBB + ** RRGGBBAA + */ - if (i3) return 1; /* garbage after color */ - if (!i2) a=0xFF; /* default no transparency */ - if (!i1) return 1; /* no color after '#' */ + i=0; + while (string[i] && isxdigit(string[i])) i++; + if (string[i] != '\0') return 1; /* garbage follows hexdigits */ + switch (i) { + case 3: + case 4: + sscanf(string, "%1x%1x%1x%1x",&r,&g,&b,&a); + r *= 0x11; + g *= 0x11; + b *= 0x11; + a *= 0x11; + if (i==3) a=0xFF; + break; + case 6: + case 8: + sscanf(string, "%02x%02x%02x%02x",&r,&g,&b,&a); + if (i==6) a=0xFF; + break; + default: + return 1; /* wrong number of digits */ + } gdp->col = r<<24|g<<16|b<<8|a; return 0; } @@ -120,7 +149,7 @@ rrd_parse_CF(char *line, unsigned int *eaten, graph_desc_t *gdp, enum cf_en *cf) int i=0; sscanf(&line[*eaten], CF_NAM_FMT "%n", symname,&i); - if ((!i)||((line[*eaten+i]!='\0')&&(line[*eaten+i]!=':'))) { + if ((!i)||((line[(*eaten)+i]!='\0')&&(line[(*eaten)+i]!=':'))) { rrd_set_error("Cannot parse CF in '%s'",line); return 1; } @@ -252,11 +281,9 @@ rrd_parse_xport(char *line, unsigned int *eaten, graph_desc_t *gdp, image_desc_t } /* Parsing of PART, VRULE, HRULE, LINE, AREA, STACK and TICK -** is done in one function. Stacking STACK is silently ignored -** as it is redundant. Stacking PART, VRULE, HRULE or TICK is -** not allowed. The check for color doesn't need to be so strict -** anymore, the user can specify the color '#00000000' and -** effectively circumvent this check, so why bother. +** is done in one function. +** +** Stacking PART, VRULE, HRULE or TICK is not allowed. ** ** If a number (which is valid to enter) is more than a ** certain amount of characters, it is caught as an error. @@ -265,12 +292,11 @@ rrd_parse_xport(char *line, unsigned int *eaten, graph_desc_t *gdp, image_desc_t */ int rrd_parse_PVHLAST(char *line, unsigned int *eaten, graph_desc_t *gdp, image_desc_t *im) { - int i,j; + int i,j,k; int colorfound=0; char tmpstr[MAX_VNAME_LEN + 10]; /* vname#RRGGBBAA\0 */ dprintf("- parsing '%s'\n",&line[*eaten]); - dprintf("- from line '%s'\n",line); i=scan_for_col(&line[*eaten],MAX_VNAME_LEN+9,tmpstr); if (line[*eaten+i]!='\0' && line[*eaten+i]!=':') { @@ -280,81 +306,123 @@ rrd_parse_PVHLAST(char *line, unsigned int *eaten, graph_desc_t *gdp, image_desc j=i; while (j>0 && tmpstr[j]!='#') j--; - if (tmpstr[j]=='#') { - if (rrd_parse_color(&tmpstr[j],gdp)) { - rrd_set_error("Could not parse color in '%s'",tmpstr[j]); - return 1; - } + if (j) { tmpstr[j]='\0'; - dprintf("- parsed color 0x%08x\n",(unsigned int)gdp->col); - colorfound=1; } - dprintf("- examining '%s'\n",tmpstr); - j=0; + dprintf("- examining value '%s'\n",tmpstr); + k=0; if (gdp->gf == GF_VRULE) { - sscanf(tmpstr,"%li%n",&gdp->xrule,&j); - if (j) dprintf("- found time: %li\n",gdp->xrule); + sscanf(tmpstr,"%li%n",&gdp->xrule,&k); + if (k) dprintf("- found time: %li\n",gdp->xrule); } else { - sscanf(tmpstr,"%lf%n",&gdp->yrule,&j); - if (j) dprintf("- found number: %f\n",gdp->yrule); + sscanf(tmpstr,"%lf%n",&gdp->yrule,&k); + if (k) dprintf("- found number: %f\n",gdp->yrule); } - if (!j) { + if (!k) { if ((gdp->vidx=find_var(im,tmpstr))<0) { rrd_set_error("Not a valid vname: %s in line %s",tmpstr,line); return 1; } dprintf("- found vname: '%s' vidx %li\n",tmpstr,gdp->vidx); } - /* "*eaten" is still pointing to the original location, - ** "*eaten +i" is pointing to the character after the color - ** or to the terminating '\0' in which case we're finished. - */ - if (line[*eaten+i]=='\0') { - *eaten+=i; - return 0; + + if (j) { + j++; + dprintf("- examining color '%s'\n",&tmpstr[j]); + if (rrd_parse_color(&tmpstr[j],gdp)) { + rrd_set_error("Could not parse color in '%s'",&tmpstr[j]); + return 1; + } + dprintf("- parsed color 0x%08x\n",(unsigned int)gdp->col); + colorfound=1; + } else { + dprintf("- no color present in '%s'\n",tmpstr); } - *eaten+=++i; - /* If a color is specified and the only remaining part is - ** ":STACK" then it is assumed to be the legend. An empty - ** legend can be specified as expected. This means the - ** following can be done: LINE1:x#FF0000FF::STACK - */ - if (colorfound) { /* no legend if no color */ - if (gdp->gf == GF_TICK) { - dprintf("- looking for optional number\n"); - sscanf(&line[*eaten],"%lf%n",&gdp->yrule,&j); - if (j) { - dprintf("- found number %f\n",gdp->yrule); - (*eaten)+=j; - if (gdp->yrule > 1.0 || gdp->yrule < -1.0) { - rrd_set_error("Tick factor should be <= 1.0"); - return 1; - } - if (line[*eaten] == ':') - (*eaten)++; - } else { - dprintf("- not found, defaulting to 0.1\n"); - gdp->yrule=0.1; - return 0; + (*eaten) += i; /* after vname#color */ + if (line[*eaten]!='\0') { + (*eaten)++; /* after colon */ + } + + if (gdp->gf == GF_TICK) { + dprintf("- parsing '%s'\n",&line[*eaten]); + dprintf("- looking for optional TICK number\n"); + j=0; + sscanf(&line[*eaten],"%lf%n",&gdp->yrule,&j); + if (j) { + if (line[*eaten+j]!='\0' && line[*eaten+j]!=':') { + rrd_set_error("Cannot parse TICK fraction '%s'",line); + return 1; + } + dprintf("- found number %f\n",gdp->yrule); + if (gdp->yrule > 1.0 || gdp->yrule < -1.0) { + rrd_set_error("Tick factor should be <= 1.0"); + return 1; } + (*eaten)+=j; + } else { + dprintf("- not found, defaulting to 0.1\n"); + gdp->yrule=0.1; } + if (line[*eaten] == '\0') { + dprintf("- done parsing line\n"); + return 0; + } else { if (line[*eaten] == ':') { + (*eaten)++; + } else { + rrd_set_error("Can't make sense of that TICK line"); + return 1; + } + } + } + + dprintf("- parsing '%s'\n",&line[*eaten]); + + /* Legend is next. A legend without a color is an error. + ** Stacking an item without having a legend is OK however + ** then an empty legend should be specified. + ** LINE:val#color:STACK means legend is string "STACK" + ** LINE:val#color::STACK means no legend, and do STACK + ** LINE:val:STACK is an error (legend but no color) + ** LINE:val::STACK means no legend, and do STACK + */ + if (colorfound) { + char c1,c2; + int err=0; + char *linecp = strdup(line); dprintf("- looking for optional legend\n"); - dprintf("- in '%s'\n",&line[*eaten]); - /* the legend for a graph item must start with "m " the first - m will then be over drawn with a color box. Since there - is ample space I overwrite the first few characters of the line - with the material that I want to see in the legend */ - if (line[*eaten] != '\0' && line[*eaten] != ':'){ - *eaten = *eaten - 2; - line[*eaten] = 'm'; - line[*eaten+1] = ' '; - } - if (rrd_parse_legend(line, eaten, gdp)) return 1; - } - - /* PART, HRULE, VRULE and TICK cannot be stacked. We're finished */ + + /* The legend needs to be prefixed with "m ". This then gets + ** replaced by the color box. */ + + dprintf("- examining '%s'\n",&line[*eaten]); + + (*eaten)--; + linecp[*eaten]=' '; + (*eaten)--; + linecp[*eaten]='m'; + + if (rrd_parse_legend(linecp, eaten, gdp)) err=1; + + free(linecp); + if (err) return 1; + + dprintf("- found legend '%s'\n", &gdp->legend[2]); + } else { + dprintf("- skipping empty legend\n"); + if (line[*eaten] != '\0' && line[*eaten] != ':') { + rrd_set_error("Legend set but no color: %s",&line[*eaten]); + return 1; + } + } + if (line[*eaten]=='\0') { + dprintf("- done parsing line\n"); + return 0; + } + (*eaten)++; /* after colon */ + + /* PART, HRULE, VRULE and TICK cannot be stacked. */ if ( (gdp->gf == GF_HRULE) || (gdp->gf == GF_VRULE) #ifdef WITH_PIECHART @@ -363,23 +431,31 @@ rrd_parse_PVHLAST(char *line, unsigned int *eaten, graph_desc_t *gdp, image_desc || (gdp->gf == GF_TICK) ) return 0; + dprintf("- parsing '%s'\n",&line[*eaten]); if (line[*eaten]!='\0') { dprintf("- still more, should be STACK\n"); - (*eaten)++; j=scan_for_col(&line[*eaten],5,tmpstr); - if (line[*eaten+j]!='\0') { + if (line[*eaten+j]!='\0' && line[*eaten+j]!=':') { + /* not 5 chars */ rrd_set_error("Garbage found where STACK expected"); return 1; } if (!strcmp("STACK",tmpstr)) { dprintf("- found STACK\n"); gdp->stack=1; - (*eaten)+=5; + (*eaten)+=j; } else { rrd_set_error("Garbage found where STACK expected"); return 1; } } + if (line[*eaten]=='\0') { + dprintf("- done parsing line\n"); + return 0; + } + (*eaten)++; + dprintf("- parsing '%s'\n",&line[*eaten]); + /* have simpler code in the drawing section */ if ( gdp->gf == GF_STACK ){ gdp->stack=1;