+ i=scan_for_col(&line[*eaten],FMT_LEG_LEN,gdp->legend);
+
+ (*eaten)+=i;
+
+ if (line[*eaten]!='\0' && line[*eaten]!=':') {
+ rrd_set_error("Legend too long");
+ return 1;
+ } else {
+ return 0;
+ }
+}
+
+int
+rrd_parse_color(char *string, graph_desc_t *gdp) {
+ unsigned int r=0,g=0,b=0,a=0,i;
+
+ /* matches the following formats:
+ ** RGB
+ ** RGBA
+ ** RRGGBB
+ ** RRGGBBAA
+ */
+
+ 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;
+}
+
+int
+rrd_parse_CF(char *line, unsigned int *eaten, graph_desc_t *gdp, enum cf_en *cf) {
+ char symname[CF_NAM_SIZE];
+ int i=0;
+
+ sscanf(&line[*eaten], CF_NAM_FMT "%n", symname,&i);
+ if ((!i)||((line[(*eaten)+i]!='\0')&&(line[(*eaten)+i]!=':'))) {
+ rrd_set_error("Cannot parse CF in '%s'",line);
+ return 1;
+ }
+ (*eaten)+=i;
+ dprintf("- using CF '%s'\n",symname);
+
+ if ((int)(*cf = cf_conv(symname))==-1) {
+ rrd_set_error("Unknown CF '%s' in '%s'",symname,line);
+ return 1;
+ }
+
+ if (line[*eaten]!='\0') (*eaten)++;
+ return 0;
+}
+
+/* Parsing old-style xPRINT and new-style xPRINT */
+int
+rrd_parse_print(char *line, unsigned int *eaten, graph_desc_t *gdp, image_desc_t *im) {
+ /* vname:CF:format in case of DEF-based vname
+ ** vname:CF:format in case of CDEF-based vname
+ ** vname:format in case of VDEF-based vname
+ */
+ char tmpstr[MAX_VNAME_LEN+1];
+ int i=0;
+
+ sscanf(&line[*eaten], DEF_NAM_FMT ":%n", tmpstr,&i);
+ if (!i) {
+ rrd_set_error("Could not parse line '%s'",line);
+ return 1;
+ }
+ (*eaten)+=i;
+ dprintf("- Found candidate vname '%s'\n",tmpstr);
+
+ if ((gdp->vidx=find_var(im,tmpstr))<0) {
+ rrd_set_error("Not a valid vname: %s in line %s",tmpstr,line);
+ return 1;
+ }
+ switch (im->gdes[gdp->vidx].gf) {
+ case GF_DEF:
+ case GF_CDEF:
+ dprintf("- vname is of type DEF or CDEF, looking for CF\n");
+ if (rrd_parse_CF(line,eaten,gdp,&gdp->cf)) return 1;
+ break;
+ case GF_VDEF:
+ dprintf("- vname is of type VDEF\n");
+ break;
+ default:
+ rrd_set_error("Encountered unknown type variable '%s'",tmpstr);
+ return 1;
+ }
+
+ if (rrd_parse_legend(line,eaten,gdp)) return 1;
+ /* for *PRINT the legend itself gets rendered later. We only
+ get the format at this juncture */
+ strcpy(gdp->format,gdp->legend);
+ gdp->legend[0]='\0';
+ return 0;
+}
+
+int
+rrd_parse_shift(char *line, unsigned int *eaten, graph_desc_t *gdp, image_desc_t *im) {
+ char *l = strdup(line + *eaten), *p;
+ int rc = 1;
+
+ p = strchr(l, ':');
+ if (p == NULL) {
+ rrd_set_error("Invalid SHIFT syntax");
+ goto out;
+ }
+ *p++ = '\0';
+
+ if ((gdp->vidx=find_var(im,l))<0) {
+ rrd_set_error("Not a valid vname: %s in line %s",l,line);
+ goto out;
+ }
+
+ /* constant will parse; otherwise, must be VDEF reference */
+ if (sscanf(p, "%ld", &gdp->shval) != 1) {
+ graph_desc_t *vdp;
+
+ if ((gdp->shidx=find_var(im, p))<0) {
+ rrd_set_error("invalid offset vname: %s", p);
+ goto out;
+ }
+
+ vdp = &im->gdes[gdp->shidx];
+ if (vdp->gf != GF_VDEF) {
+ rrd_set_error("offset must specify value or VDEF");
+ goto out;
+ }
+ } else {
+ gdp->shidx = -1;
+ }
+
+ *eaten = strlen(line);
+ rc = 0;
+
+ out:
+ free(l);
+ return rc;
+}
+
+int
+rrd_parse_xport(char *line, unsigned int *eaten, graph_desc_t *gdp, image_desc_t *im) {
+ char *l = strdup(line + *eaten), *p;
+ int rc = 1;
+
+ p = strchr(l, ':');
+ if (p != NULL)
+ *p++ = '\0';
+ else
+ p = "";
+
+ if ((gdp->vidx=find_var(im, l))==-1){
+ rrd_set_error("unknown variable '%s'",l);
+ goto out;
+ }
+
+ if (strlen(p) >= FMT_LEG_LEN)
+ *(p + FMT_LEG_LEN) = '\0';
+
+ strcpy(gdp->legend, p);
+ *eaten = strlen(line);
+ rc = 0;
+
+ out:
+ free(l);
+ return rc;
+}
+
+/* Parsing of PART, VRULE, HRULE, LINE, AREA, STACK and TICK
+** 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.
+** While this is arguable, so is entering fixed numbers
+** with more than MAX_VNAME_LEN significant digits.
+*/
+int
+rrd_parse_PVHLAST(char *line, unsigned int *eaten, graph_desc_t *gdp, image_desc_t *im) {
+ int i,j,k;
+ int colorfound=0;
+ char tmpstr[MAX_VNAME_LEN + 10]; /* vname#RRGGBBAA\0 */
+
+ dprintf("- parsing '%s'\n",&line[*eaten]);
+
+ /* have simpler code in the drawing section */
+ if ( gdp->gf == GF_STACK ){
+ gdp->stack=1;
+ }
+
+ i=scan_for_col(&line[*eaten],MAX_VNAME_LEN+9,tmpstr);
+ if (line[*eaten+i]!='\0' && line[*eaten+i]!=':') {
+ rrd_set_error("Cannot parse line '%s'",line);
+ return 1;
+ }
+
+ j=i; while (j>0 && tmpstr[j]!='#') j--;
+
+ if (j) {
+ tmpstr[j]='\0';
+ }
+
+ dprintf("- examining value '%s'\n",tmpstr);
+ k=0;
+ if (gdp->gf == GF_VRULE) {
+ sscanf(tmpstr,"%li%n",&gdp->xrule,&k);
+ if (k) dprintf("- found time: %li\n",gdp->xrule);
+ } else {
+ sscanf(tmpstr,"%lf%n",&gdp->yrule,&k);
+ if (k) dprintf("- found number: %f\n",gdp->yrule);
+ }
+ 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);
+ }
+
+ 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; /* 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) {
+ int err=0;
+ char *linecp = strdup(line);
+ dprintf("- looking for optional legend\n");
+
+ dprintf("- examining '%s'\n",&line[*eaten]);
+ if (linecp[*eaten] != '\0' && linecp[*eaten] != ':') {
+ /* If the legend is not empty, it has to be prefixed with "m ". This then gets
+ * replaced by the color box later on. */
+ (*eaten)--;
+ linecp[*eaten]='o';
+ (*eaten)--;
+ linecp[*eaten]='o';
+ }
+
+ 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");