+ 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;
+ (*eaten) += i;
+ i = 0;
+ dprintf("Scanning line '%s'\n", &line[*eaten]);
+ }
+ i = 0;
+ c1 = '\0';
+ sscanf(&line[*eaten], "%10[A-Z]%n%c", funcname, &i, &c1);
+ if (!i) {
+ rrd_set_error("Could not make sense out of '%s'", line);
+ return 1;
+ }
+ (*eaten) += i;
+ 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 (c1 == '\0') {
+ rrd_set_error("Function %s needs parameters. Line: %s\n", funcname,
+ line);
+ return 1;
+ }
+ if (c1 == ':')
+ (*eaten)++;
+
+ /* Some commands have a parameter before the colon
+ * (currently only LINE)
+ */
+ switch (gdp->gf) {
+ case GF_LINE:
+ if (c1 == ':') {
+ gdp->linewidth = 1;
+ dprintf("- using default width of 1\n");
+ } else {
+ i = 0;
+ sscanf(&line[*eaten], "%lf:%n", &gdp->linewidth, &i);
+ if (!i) {
+ rrd_set_error("Cannot parse line width '%s' in line '%s'\n",
+ &line[*eaten], line);
+ return 1;
+ } else {
+ dprintf("- scanned width %f\n", gdp->linewidth);
+ if (isnan(gdp->linewidth)) {
+ rrd_set_error
+ ("LINE width '%s' is not a number in line '%s'\n",
+ &line[*eaten], line);
+ return 1;
+ }
+ if (isinf(gdp->linewidth)) {
+ rrd_set_error
+ ("LINE width '%s' is out of range in line '%s'\n",
+ &line[*eaten], line);
+ return 1;
+ }
+ if (gdp->linewidth < 0) {
+ rrd_set_error
+ ("LINE width '%s' is less than 0 in line '%s'\n",
+ &line[*eaten], line);
+ return 1;
+ }
+ }
+ (*eaten) += i;
+ }
+ break;
+ default:
+ if (c1 == ':')
+ break;
+ rrd_set_error("Malformed '%s' command in line '%s'\n", &line[*eaten],
+ line);
+ return 1;
+ }
+ if (line[*eaten] == '\0') {
+ rrd_set_error("Expected some arguments after '%s'\n", line);
+ return 1;
+ }
+ return 0;
+}
+
+int rrd_parse_legend(
+ const char *const line,
+ unsigned int *const eaten,
+ graph_desc_t *const gdp)
+{
+ int i;
+
+ 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;
+
+ if (line[*eaten] != '\0' && line[*eaten] != ':') {
+ rrd_set_error("Legend too long");
+ return 1;
+ } else {
+ return 0;
+ }
+}
+
+int rrd_parse_color(
+ const char *const string,
+ graph_desc_t *const 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((unsigned int) 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 = gfx_hex_to_col(r << 24 | g << 16 | b << 8 | a);
+ return 0;
+}
+
+int rrd_parse_CF(
+ const char *const line,
+ unsigned int *const eaten,
+ graph_desc_t *const 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;
+}
+
+/* Try to match next token as a vname.
+ *
+ * Returns:
+ * -1 an error occured and the error string is set
+ * other the vname index number
+ *
+ * *eaten is incremented only when a vname is found.
+ */
+int rrd_parse_find_vname(
+ const char *const line,
+ unsigned int *const eaten,
+ graph_desc_t *const gdp,
+ image_desc_t *const im)
+{
+ char tmpstr[MAX_VNAME_LEN + 1];
+ int i;
+ long vidx;
+
+ i = 0;
+ sscanf(&line[*eaten], DEF_NAM_FMT "%n", tmpstr, &i);
+ if (!i) {
+ rrd_set_error("Could not parse line '%s'", line);
+ return -1;
+ }
+ if (line[*eaten + i] != ':' && line[*eaten + i] != '\0') {
+ rrd_set_error("Could not parse line '%s'", line);
+ return -1;
+ }
+ dprintf("- Considering '%s'\n", tmpstr);
+
+ if ((vidx = find_var(im, tmpstr)) < 0) {
+ dprintf("- Not a vname\n");
+ 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 (line[*eaten + i] == ':')
+ i++;
+ (*eaten) += i;
+ return vidx;
+}
+
+/* Parsing old-style xPRINT and new-style xPRINT */
+int rrd_parse_print(
+ const char *const line,
+ unsigned int *const eaten,
+ graph_desc_t *const gdp,
+ image_desc_t *const im)
+{
+ /* vname:CF:format in case of DEF-based vname
+ ** vname:CF:format in case of CDEF-based vname
+ ** vname:format[:strftime] in case of VDEF-based vname
+ */
+ if ((gdp->vidx = rrd_parse_find_vname(line, eaten, gdp, im)) < 0)
+ 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'",
+ im->gdes[gdp->vidx].vname);
+ 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';
+ /* this is a very crud test, parsing :style flags should be in a function */
+ if (im->gdes[gdp->vidx].gf == GF_VDEF
+ && strcmp(line + (*eaten), ":strftime") == 0) {
+ gdp->strftm = 1;
+ (*eaten) += strlen(":strftime");
+ }
+ return 0;
+}
+
+/* SHIFT:_def_or_cdef:_vdef_or_number_
+ */
+int rrd_parse_shift(
+ const char *const line,
+ unsigned int *const eaten,
+ graph_desc_t *const gdp,
+ image_desc_t *const im)
+{
+ int i;
+
+ if ((gdp->vidx = rrd_parse_find_vname(line, eaten, gdp, im)) < 0)
+ return 1;
+
+ switch (im->gdes[gdp->vidx].gf) {
+ case GF_DEF:
+ case GF_CDEF:
+ dprintf("- vname is of type DEF or CDEF, OK\n");
+ break;
+ case GF_VDEF:
+ rrd_set_error("Cannot shift a VDEF: '%s' in line '%s'\n",
+ im->gdes[gdp->vidx].vname, line);
+ return 1;
+ default:
+ rrd_set_error("Encountered unknown type variable '%s' in line '%s'",
+ im->gdes[gdp->vidx].vname, line);
+ return 1;
+ }
+
+ if ((gdp->shidx = rrd_parse_find_vname(line, eaten, gdp, im)) >= 0) {
+ switch (im->gdes[gdp->shidx].gf) {
+ case GF_DEF:
+ case GF_CDEF:
+ rrd_set_error("Offset cannot be a (C)DEF: '%s' in line '%s'\n",
+ im->gdes[gdp->shidx].vname, line);
+ return 1;
+ case GF_VDEF:
+ dprintf("- vname is of type VDEF, OK\n");
+ break;
+ default:
+ rrd_set_error
+ ("Encountered unknown type variable '%s' in line '%s'",
+ im->gdes[gdp->vidx].vname, line);
+ return 1;
+ }
+ } else {
+ long time_tmp = 0;
+
+ rrd_clear_error();
+ i = 0;
+ sscanf(&line[*eaten], "%li%n", &time_tmp, &i);
+ gdp->shval = time_tmp;
+ if (i != (int) strlen(&line[*eaten])) {
+ rrd_set_error("Not a valid offset: %s in line %s", &line[*eaten],
+ line);
+ return 1;
+ }
+ (*eaten) += i;
+ dprintf("- offset is number %li\n", gdp->shval);
+ gdp->shidx = -1;
+ }
+ return 0;
+}
+
+/* XPORT:_def_or_cdef[:legend]
+ */
+int rrd_parse_xport(
+ const char *const line,
+ unsigned int *const eaten,
+ graph_desc_t *const gdp,
+ image_desc_t *const im)
+{
+ if ((gdp->vidx = rrd_parse_find_vname(line, eaten, gdp, im)) < 0)
+ return 1;
+
+ switch (im->gdes[gdp->vidx].gf) {
+ case GF_DEF:
+ case GF_CDEF:
+ dprintf("- vname is of type DEF or CDEF, OK\n");
+ break;
+ case GF_VDEF:
+ rrd_set_error("Cannot xport a VDEF: '%s' in line '%s'\n",
+ im->gdes[gdp->vidx].vname, line);
+ return 1;
+ default:
+ rrd_set_error("Encountered unknown type variable '%s' in line '%s'",
+ im->gdes[gdp->vidx].vname, line);
+ return 1;
+ }
+ dprintf("- looking for legend in '%s'\n", &line[*eaten]);
+ if (rrd_parse_legend(line, eaten, gdp))
+ return 1;
+ return 0;
+}
+
+int rrd_parse_textalign(
+ const char *const line,
+ unsigned int *const eaten,
+ graph_desc_t *const gdp)
+{
+ if (strcmp(&line[*eaten], "left") == 0) {
+ gdp->txtalign = TXA_LEFT;
+ } else if (strcmp(&line[*eaten], "right") == 0) {
+ gdp->txtalign = TXA_RIGHT;
+ } else if (strcmp(&line[*eaten], "justified") == 0) {
+ gdp->txtalign = TXA_JUSTIFIED;
+ } else if (strcmp(&line[*eaten], "center") == 0) {
+ gdp->txtalign = TXA_CENTER;
+ } else {
+ rrd_set_error("Unknown alignement type '%s'", &line[*eaten]);
+ return 1;
+ }
+ *eaten += strlen(&line[*eaten]);
+ return 0;
+}
+
+
+/* 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(
+ const char *const line,
+ unsigned int *const eaten,
+ graph_desc_t *const gdp,
+ image_desc_t *const im)
+{
+ int i, j, k;
+ int colorfound = 0;
+ char tmpstr[MAX_VNAME_LEN + 10]; /* vname#RRGGBBAA\0 */
+ static int spacecnt = 0;
+
+ if (spacecnt == 0) {
+ float one_space = gfx_get_text_width(im, 0,
+ im->
+ text_prop[TEXT_PROP_LEGEND].
+ font,
+ im->
+ text_prop[TEXT_PROP_LEGEND].
+ size,
+ im->tabwidth, " ") / 4.0;
+ float target_space = gfx_get_text_width(im, 0,
+ im->
+ text_prop
+ [TEXT_PROP_LEGEND].font,
+ im->
+ text_prop
+ [TEXT_PROP_LEGEND].size,
+ im->tabwidth, "oo");
+
+ spacecnt = target_space / one_space;
+ dprintf("- spacecnt: %i onespace: %f targspace: %f\n", spacecnt,
+ one_space, target_space);
+ }