ytr boundery checking ... this is from the GD days ... we don't need this anymore
[rrdtool.git] / src / rrd_graph.c
1 /****************************************************************************
2  * RRDtool 1.3rc4  Copyright by Tobi Oetiker, 1997-2008
3  ****************************************************************************
4  * rrd__graph.c  produce graphs from data in rrdfiles
5  ****************************************************************************/
6
7
8 #include <sys/stat.h>
9
10 #ifdef WIN32
11 #include "strftime.h"
12 #endif
13 #include "rrd_tool.h"
14
15 #if defined(WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
16 #include <io.h>
17 #include <fcntl.h>
18 #endif
19
20 #ifdef HAVE_TIME_H
21 #include <time.h>
22 #endif
23
24 #ifdef HAVE_LOCALE_H
25 #include <locale.h>
26 #endif
27
28 #include "rrd_graph.h"
29
30 /* some constant definitions */
31
32
33
34 #ifndef RRD_DEFAULT_FONT
35 /* there is special code later to pick Cour.ttf when running on windows */
36 #define RRD_DEFAULT_FONT "DejaVu Sans Mono,Bitstream Vera Sans Mono,monospace,Courier"
37 #endif
38
39 text_prop_t text_prop[] = {
40     {8.0, RRD_DEFAULT_FONT}
41     ,                   /* default */
42     {9.0, RRD_DEFAULT_FONT}
43     ,                   /* title */
44     {7.0, RRD_DEFAULT_FONT}
45     ,                   /* axis */
46     {8.0, RRD_DEFAULT_FONT}
47     ,                   /* unit */
48     {8.0, RRD_DEFAULT_FONT} /* legend */
49 };
50
51 xlab_t    xlab[] = {
52     {0, 0, TMT_SECOND, 30, TMT_MINUTE, 5, TMT_MINUTE, 5, 0, "%H:%M"}
53     ,
54     {2, 0, TMT_MINUTE, 1, TMT_MINUTE, 5, TMT_MINUTE, 5, 0, "%H:%M"}
55     ,
56     {5, 0, TMT_MINUTE, 2, TMT_MINUTE, 10, TMT_MINUTE, 10, 0, "%H:%M"}
57     ,
58     {10, 0, TMT_MINUTE, 5, TMT_MINUTE, 20, TMT_MINUTE, 20, 0, "%H:%M"}
59     ,
60     {30, 0, TMT_MINUTE, 10, TMT_HOUR, 1, TMT_HOUR, 1, 0, "%H:%M"}
61     ,
62     {60, 0, TMT_MINUTE, 30, TMT_HOUR, 2, TMT_HOUR, 2, 0, "%H:%M"}
63     ,
64     {60, 24 * 3600, TMT_MINUTE, 30, TMT_HOUR, 2, TMT_HOUR, 6, 0, "%a %H:%M"}
65     ,
66     {180, 0, TMT_HOUR, 1, TMT_HOUR, 6, TMT_HOUR, 6, 0, "%H:%M"}
67     ,
68     {180, 24 * 3600, TMT_HOUR, 1, TMT_HOUR, 6, TMT_HOUR, 12, 0, "%a %H:%M"}
69     ,
70     /*{300,             0,   TMT_HOUR,3,    TMT_HOUR,12,   TMT_HOUR,12,    12*3600,"%a %p"},  this looks silly */
71     {600, 0, TMT_HOUR, 6, TMT_DAY, 1, TMT_DAY, 1, 24 * 3600, "%a"}
72     ,
73     {1200, 0, TMT_HOUR, 6, TMT_DAY, 1, TMT_DAY, 1, 24 * 3600, "%d"}
74     ,
75     {1800, 0, TMT_HOUR, 12, TMT_DAY, 1, TMT_DAY, 2, 24 * 3600, "%a %d"}
76     ,
77     {2400, 0, TMT_HOUR, 12, TMT_DAY, 1, TMT_DAY, 2, 24 * 3600, "%a"}
78     ,
79     {3600, 0, TMT_DAY, 1, TMT_WEEK, 1, TMT_WEEK, 1, 7 * 24 * 3600, "Week %V"}
80     ,
81     {3 * 3600, 0, TMT_WEEK, 1, TMT_MONTH, 1, TMT_WEEK, 2, 7 * 24 * 3600,
82      "Week %V"}
83     ,
84     {6 * 3600, 0, TMT_MONTH, 1, TMT_MONTH, 1, TMT_MONTH, 1, 30 * 24 * 3600,
85      "%b"}
86     ,
87     {48 * 3600, 0, TMT_MONTH, 1, TMT_MONTH, 3, TMT_MONTH, 3, 30 * 24 * 3600,
88      "%b"}
89     ,
90     {315360, 0, TMT_MONTH, 3, TMT_YEAR, 1, TMT_YEAR, 1, 365 * 24 * 3600, "%Y"}
91     ,
92     {10 * 24 * 3600, 0, TMT_YEAR, 1, TMT_YEAR, 1, TMT_YEAR, 1,
93      365 * 24 * 3600, "%y"}
94     ,
95     {-1, 0, TMT_MONTH, 0, TMT_MONTH, 0, TMT_MONTH, 0, 0, ""}
96 };
97
98 /* sensible y label intervals ...*/
99
100 ylab_t    ylab[] = {
101     {0.1, {1, 2, 5, 10}
102      }
103     ,
104     {0.2, {1, 5, 10, 20}
105      }
106     ,
107     {0.5, {1, 2, 4, 10}
108      }
109     ,
110     {1.0, {1, 2, 5, 10}
111      }
112     ,
113     {2.0, {1, 5, 10, 20}
114      }
115     ,
116     {5.0, {1, 2, 4, 10}
117      }
118     ,
119     {10.0, {1, 2, 5, 10}
120      }
121     ,
122     {20.0, {1, 5, 10, 20}
123      }
124     ,
125     {50.0, {1, 2, 4, 10}
126      }
127     ,
128     {100.0, {1, 2, 5, 10}
129      }
130     ,
131     {200.0, {1, 5, 10, 20}
132      }
133     ,
134     {500.0, {1, 2, 4, 10}
135      }
136     ,
137     {0.0, {0, 0, 0, 0}
138      }
139 };
140
141
142 gfx_color_t graph_col[] =   /* default colors */
143 {
144     {1.00, 1.00, 1.00, 1.00},   /* canvas     */
145     {0.95, 0.95, 0.95, 1.00},   /* background */
146     {0.81, 0.81, 0.81, 1.00},   /* shade A    */
147     {0.62, 0.62, 0.62, 1.00},   /* shade B    */
148     {0.56, 0.56, 0.56, 0.75},   /* grid       */
149     {0.87, 0.31, 0.31, 0.60},   /* major grid */
150     {0.00, 0.00, 0.00, 1.00},   /* font       */
151     {0.50, 0.12, 0.12, 1.00},   /* arrow      */
152     {0.12, 0.12, 0.12, 1.00},   /* axis       */
153     {0.00, 0.00, 0.00, 1.00}    /* frame      */
154 };
155
156
157 /* #define DEBUG */
158
159 #ifdef DEBUG
160 # define DPRINT(x)    (void)(printf x, printf("\n"))
161 #else
162 # define DPRINT(x)
163 #endif
164
165
166 /* initialize with xtr(im,0); */
167 int xtr(
168     image_desc_t *im,
169     time_t mytime)
170 {
171     static double pixie;
172
173     if (mytime == 0) {
174         pixie = (double) im->xsize / (double) (im->end - im->start);
175         return im->xorigin;
176     }
177     return (int) ((double) im->xorigin + pixie * (mytime - im->start));
178 }
179
180 /* translate data values into y coordinates */
181 double ytr(
182     image_desc_t *im,
183     double value)
184 {
185     static double pixie;
186     double    yval;
187
188     if (isnan(value)) {
189         if (!im->logarithmic)
190             pixie = (double) im->ysize / (im->maxval - im->minval);
191         else
192             pixie =
193                 (double) im->ysize / (log10(im->maxval) - log10(im->minval));
194         yval = im->yorigin;
195     } else if (!im->logarithmic) {
196         yval = im->yorigin - pixie * (value - im->minval);
197     } else {
198         if (value < im->minval) {
199             yval = im->yorigin;
200         } else {
201             yval = im->yorigin - pixie * (log10(value) - log10(im->minval));
202         }
203     }
204     return yval;
205 }
206
207
208
209 /* conversion function for symbolic entry names */
210
211
212 #define conv_if(VV,VVV) \
213    if (strcmp(#VV, string) == 0) return VVV ;
214
215 enum gf_en gf_conv(
216     char *string)
217 {
218
219     conv_if(PRINT, GF_PRINT);
220     conv_if(GPRINT, GF_GPRINT);
221     conv_if(COMMENT, GF_COMMENT);
222     conv_if(HRULE, GF_HRULE);
223     conv_if(VRULE, GF_VRULE);
224     conv_if(LINE, GF_LINE);
225     conv_if(AREA, GF_AREA);
226     conv_if(STACK, GF_STACK);
227     conv_if(TICK, GF_TICK);
228     conv_if(TEXTALIGN, GF_TEXTALIGN);
229     conv_if(DEF, GF_DEF);
230     conv_if(CDEF, GF_CDEF);
231     conv_if(VDEF, GF_VDEF);
232     conv_if(XPORT, GF_XPORT);
233     conv_if(SHIFT, GF_SHIFT);
234
235     return (-1);
236 }
237
238 enum gfx_if_en if_conv(
239     char *string)
240 {
241
242     conv_if(PNG, IF_PNG);
243     conv_if(SVG, IF_SVG);
244     conv_if(EPS, IF_EPS);
245     conv_if(PDF, IF_PDF);
246
247     return (-1);
248 }
249
250 enum tmt_en tmt_conv(
251     char *string)
252 {
253
254     conv_if(SECOND, TMT_SECOND);
255     conv_if(MINUTE, TMT_MINUTE);
256     conv_if(HOUR, TMT_HOUR);
257     conv_if(DAY, TMT_DAY);
258     conv_if(WEEK, TMT_WEEK);
259     conv_if(MONTH, TMT_MONTH);
260     conv_if(YEAR, TMT_YEAR);
261     return (-1);
262 }
263
264 enum grc_en grc_conv(
265     char *string)
266 {
267
268     conv_if(BACK, GRC_BACK);
269     conv_if(CANVAS, GRC_CANVAS);
270     conv_if(SHADEA, GRC_SHADEA);
271     conv_if(SHADEB, GRC_SHADEB);
272     conv_if(GRID, GRC_GRID);
273     conv_if(MGRID, GRC_MGRID);
274     conv_if(FONT, GRC_FONT);
275     conv_if(ARROW, GRC_ARROW);
276     conv_if(AXIS, GRC_AXIS);
277     conv_if(FRAME, GRC_FRAME);
278
279     return -1;
280 }
281
282 enum text_prop_en text_prop_conv(
283     char *string)
284 {
285
286     conv_if(DEFAULT, TEXT_PROP_DEFAULT);
287     conv_if(TITLE, TEXT_PROP_TITLE);
288     conv_if(AXIS, TEXT_PROP_AXIS);
289     conv_if(UNIT, TEXT_PROP_UNIT);
290     conv_if(LEGEND, TEXT_PROP_LEGEND);
291     return -1;
292 }
293
294
295 #undef conv_if
296
297 int im_free(
298     image_desc_t *im)
299 {
300     unsigned long i, ii;
301     cairo_status_t status = 0;
302
303     if (im == NULL)
304         return 0;
305     for (i = 0; i < (unsigned) im->gdes_c; i++) {
306         if (im->gdes[i].data_first) {
307             /* careful here, because a single pointer can occur several times */
308             free(im->gdes[i].data);
309             if (im->gdes[i].ds_namv) {
310                 for (ii = 0; ii < im->gdes[i].ds_cnt; ii++)
311                     free(im->gdes[i].ds_namv[ii]);
312                 free(im->gdes[i].ds_namv);
313             }
314         }
315         /* free allocated memory used for dashed lines */
316         if (im->gdes[i].p_dashes != NULL)
317             free(im->gdes[i].p_dashes);
318
319         free(im->gdes[i].p_data);
320         free(im->gdes[i].rpnp);
321     }
322     free(im->gdes);
323     if (im->font_options)
324         cairo_font_options_destroy(im->font_options);
325
326     if (im->cr) {
327         status = cairo_status(im->cr);
328         cairo_destroy(im->cr);
329     }
330     if (im->rendered_image) {
331         free(im->rendered_image);
332     }
333     if (im->surface)
334         cairo_surface_destroy(im->surface);
335     if (status)
336         fprintf(stderr, "OOPS: Cairo has issues it can't even die: %s\n",
337                 cairo_status_to_string(status));
338
339     return 0;
340 }
341
342 /* find SI magnitude symbol for the given number*/
343 void auto_scale(
344     image_desc_t *im,   /* image description */
345     double *value,
346     char **symb_ptr,
347     double *magfact)
348 {
349
350     char     *symbol[] = { "a", /* 10e-18 Atto */
351         "f",            /* 10e-15 Femto */
352         "p",            /* 10e-12 Pico */
353         "n",            /* 10e-9  Nano */
354         "u",            /* 10e-6  Micro */
355         "m",            /* 10e-3  Milli */
356         " ",            /* Base */
357         "k",            /* 10e3   Kilo */
358         "M",            /* 10e6   Mega */
359         "G",            /* 10e9   Giga */
360         "T",            /* 10e12  Tera */
361         "P",            /* 10e15  Peta */
362         "E"
363     };                  /* 10e18  Exa */
364
365     int       symbcenter = 6;
366     int       sindex;
367
368     if (*value == 0.0 || isnan(*value)) {
369         sindex = 0;
370         *magfact = 1.0;
371     } else {
372         sindex = floor(log(fabs(*value)) / log((double) im->base));
373         *magfact = pow((double) im->base, (double) sindex);
374         (*value) /= (*magfact);
375     }
376     if (sindex <= symbcenter && sindex >= -symbcenter) {
377         (*symb_ptr) = symbol[sindex + symbcenter];
378     } else {
379         (*symb_ptr) = "?";
380     }
381 }
382
383
384 static char si_symbol[] = {
385     'a',                /* 10e-18 Atto */
386     'f',                /* 10e-15 Femto */
387     'p',                /* 10e-12 Pico */
388     'n',                /* 10e-9  Nano */
389     'u',                /* 10e-6  Micro */
390     'm',                /* 10e-3  Milli */
391     ' ',                /* Base */
392     'k',                /* 10e3   Kilo */
393     'M',                /* 10e6   Mega */
394     'G',                /* 10e9   Giga */
395     'T',                /* 10e12  Tera */
396     'P',                /* 10e15  Peta */
397     'E',                /* 10e18  Exa */
398 };
399 static const int si_symbcenter = 6;
400
401 /* find SI magnitude symbol for the numbers on the y-axis*/
402 void si_unit(
403     image_desc_t *im    /* image description */
404     )
405 {
406
407     double    digits, viewdigits = 0;
408
409     digits =
410         floor(log(max(fabs(im->minval), fabs(im->maxval))) /
411               log((double) im->base));
412
413     if (im->unitsexponent != 9999) {
414         /* unitsexponent = 9, 6, 3, 0, -3, -6, -9, etc */
415         viewdigits = floor(im->unitsexponent / 3);
416     } else {
417         viewdigits = digits;
418     }
419
420     im->magfact = pow((double) im->base, digits);
421
422 #ifdef DEBUG
423     printf("digits %6.3f  im->magfact %6.3f\n", digits, im->magfact);
424 #endif
425
426     im->viewfactor = im->magfact / pow((double) im->base, viewdigits);
427
428     if (((viewdigits + si_symbcenter) < sizeof(si_symbol)) &&
429         ((viewdigits + si_symbcenter) >= 0))
430         im->symbol = si_symbol[(int) viewdigits + si_symbcenter];
431     else
432         im->symbol = '?';
433 }
434
435 /*  move min and max values around to become sensible */
436
437 void expand_range(
438     image_desc_t *im)
439 {
440     double    sensiblevalues[] = { 1000.0, 900.0, 800.0, 750.0, 700.0,
441         600.0, 500.0, 400.0, 300.0, 250.0,
442         200.0, 125.0, 100.0, 90.0, 80.0,
443         75.0, 70.0, 60.0, 50.0, 40.0, 30.0,
444         25.0, 20.0, 10.0, 9.0, 8.0,
445         7.0, 6.0, 5.0, 4.0, 3.5, 3.0,
446         2.5, 2.0, 1.8, 1.5, 1.2, 1.0,
447         0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0, -1
448     };
449
450     double    scaled_min, scaled_max;
451     double    adj;
452     int       i;
453
454
455
456 #ifdef DEBUG
457     printf("Min: %6.2f Max: %6.2f MagFactor: %6.2f\n",
458            im->minval, im->maxval, im->magfact);
459 #endif
460
461     if (isnan(im->ygridstep)) {
462         if (im->extra_flags & ALTAUTOSCALE) {
463             /* measure the amplitude of the function. Make sure that
464                graph boundaries are slightly higher then max/min vals
465                so we can see amplitude on the graph */
466             double    delt, fact;
467
468             delt = im->maxval - im->minval;
469             adj = delt * 0.1;
470             fact = 2.0 * pow(10.0,
471                              floor(log10
472                                    (max(fabs(im->minval), fabs(im->maxval)) /
473                                     im->magfact)) - 2);
474             if (delt < fact) {
475                 adj = (fact - delt) * 0.55;
476 #ifdef DEBUG
477                 printf
478                     ("Min: %6.2f Max: %6.2f delt: %6.2f fact: %6.2f adj: %6.2f\n",
479                      im->minval, im->maxval, delt, fact, adj);
480 #endif
481             }
482             im->minval -= adj;
483             im->maxval += adj;
484         } else if (im->extra_flags & ALTAUTOSCALE_MIN) {
485             /* measure the amplitude of the function. Make sure that
486                graph boundaries are slightly lower than min vals
487                so we can see amplitude on the graph */
488             adj = (im->maxval - im->minval) * 0.1;
489             im->minval -= adj;
490         } else if (im->extra_flags & ALTAUTOSCALE_MAX) {
491             /* measure the amplitude of the function. Make sure that
492                graph boundaries are slightly higher than max vals
493                so we can see amplitude on the graph */
494             adj = (im->maxval - im->minval) * 0.1;
495             im->maxval += adj;
496         } else {
497             scaled_min = im->minval / im->magfact;
498             scaled_max = im->maxval / im->magfact;
499
500             for (i = 1; sensiblevalues[i] > 0; i++) {
501                 if (sensiblevalues[i - 1] >= scaled_min &&
502                     sensiblevalues[i] <= scaled_min)
503                     im->minval = sensiblevalues[i] * (im->magfact);
504
505                 if (-sensiblevalues[i - 1] <= scaled_min &&
506                     -sensiblevalues[i] >= scaled_min)
507                     im->minval = -sensiblevalues[i - 1] * (im->magfact);
508
509                 if (sensiblevalues[i - 1] >= scaled_max &&
510                     sensiblevalues[i] <= scaled_max)
511                     im->maxval = sensiblevalues[i - 1] * (im->magfact);
512
513                 if (-sensiblevalues[i - 1] <= scaled_max &&
514                     -sensiblevalues[i] >= scaled_max)
515                     im->maxval = -sensiblevalues[i] * (im->magfact);
516             }
517         }
518     } else {
519         /* adjust min and max to the grid definition if there is one */
520         im->minval = (double) im->ylabfact * im->ygridstep *
521             floor(im->minval / ((double) im->ylabfact * im->ygridstep));
522         im->maxval = (double) im->ylabfact * im->ygridstep *
523             ceil(im->maxval / ((double) im->ylabfact * im->ygridstep));
524     }
525
526 #ifdef DEBUG
527     fprintf(stderr, "SCALED Min: %6.2f Max: %6.2f Factor: %6.2f\n",
528             im->minval, im->maxval, im->magfact);
529 #endif
530 }
531
532
533 void apply_gridfit(
534     image_desc_t *im)
535 {
536     if (isnan(im->minval) || isnan(im->maxval))
537         return;
538     ytr(im, DNAN);
539     if (im->logarithmic) {
540         double    ya, yb, ypix, ypixfrac;
541         double    log10_range = log10(im->maxval) - log10(im->minval);
542
543         ya = pow((double) 10, floor(log10(im->minval)));
544         while (ya < im->minval)
545             ya *= 10;
546         if (ya > im->maxval)
547             return;     /* don't have y=10^x gridline */
548         yb = ya * 10;
549         if (yb <= im->maxval) {
550             /* we have at least 2 y=10^x gridlines.
551                Make sure distance between them in pixels
552                are an integer by expanding im->maxval */
553             double    y_pixel_delta = ytr(im, ya) - ytr(im, yb);
554             double    factor = y_pixel_delta / floor(y_pixel_delta);
555             double    new_log10_range = factor * log10_range;
556             double    new_ymax_log10 = log10(im->minval) + new_log10_range;
557
558             im->maxval = pow(10, new_ymax_log10);
559             ytr(im, DNAN);  /* reset precalc */
560             log10_range = log10(im->maxval) - log10(im->minval);
561         }
562         /* make sure first y=10^x gridline is located on 
563            integer pixel position by moving scale slightly 
564            downwards (sub-pixel movement) */
565         ypix = ytr(im, ya) + im->ysize; /* add im->ysize so it always is positive */
566         ypixfrac = ypix - floor(ypix);
567         if (ypixfrac > 0 && ypixfrac < 1) {
568             double    yfrac = ypixfrac / im->ysize;
569
570             im->minval = pow(10, log10(im->minval) - yfrac * log10_range);
571             im->maxval = pow(10, log10(im->maxval) - yfrac * log10_range);
572             ytr(im, DNAN);  /* reset precalc */
573         }
574     } else {
575         /* Make sure we have an integer pixel distance between
576            each minor gridline */
577         double    ypos1 = ytr(im, im->minval);
578         double    ypos2 = ytr(im, im->minval + im->ygrid_scale.gridstep);
579         double    y_pixel_delta = ypos1 - ypos2;
580         double    factor = y_pixel_delta / floor(y_pixel_delta);
581         double    new_range = factor * (im->maxval - im->minval);
582         double    gridstep = im->ygrid_scale.gridstep;
583         double    minor_y, minor_y_px, minor_y_px_frac;
584
585         if (im->maxval > 0.0)
586             im->maxval = im->minval + new_range;
587         else
588             im->minval = im->maxval - new_range;
589         ytr(im, DNAN);  /* reset precalc */
590         /* make sure first minor gridline is on integer pixel y coord */
591         minor_y = gridstep * floor(im->minval / gridstep);
592         while (minor_y < im->minval)
593             minor_y += gridstep;
594         minor_y_px = ytr(im, minor_y) + im->ysize;  /* ensure > 0 by adding ysize */
595         minor_y_px_frac = minor_y_px - floor(minor_y_px);
596         if (minor_y_px_frac > 0 && minor_y_px_frac < 1) {
597             double    yfrac = minor_y_px_frac / im->ysize;
598             double    range = im->maxval - im->minval;
599
600             im->minval = im->minval - yfrac * range;
601             im->maxval = im->maxval - yfrac * range;
602             ytr(im, DNAN);  /* reset precalc */
603         }
604         calc_horizontal_grid(im);   /* recalc with changed im->maxval */
605     }
606 }
607
608 /* reduce data reimplementation by Alex */
609
610 void reduce_data(
611     enum cf_en cf,      /* which consolidation function ? */
612     unsigned long cur_step, /* step the data currently is in */
613     time_t *start,      /* start, end and step as requested ... */
614     time_t *end,        /* ... by the application will be   ... */
615     unsigned long *step,    /* ... adjusted to represent reality    */
616     unsigned long *ds_cnt,  /* number of data sources in file */
617     rrd_value_t **data)
618 {                       /* two dimensional array containing the data */
619     int       i, reduce_factor = ceil((double) (*step) / (double) cur_step);
620     unsigned long col, dst_row, row_cnt, start_offset, end_offset, skiprows =
621         0;
622     rrd_value_t *srcptr, *dstptr;
623
624     (*step) = cur_step * reduce_factor; /* set new step size for reduced data */
625     dstptr = *data;
626     srcptr = *data;
627     row_cnt = ((*end) - (*start)) / cur_step;
628
629 #ifdef DEBUG
630 #define DEBUG_REDUCE
631 #endif
632 #ifdef DEBUG_REDUCE
633     printf("Reducing %lu rows with factor %i time %lu to %lu, step %lu\n",
634            row_cnt, reduce_factor, *start, *end, cur_step);
635     for (col = 0; col < row_cnt; col++) {
636         printf("time %10lu: ", *start + (col + 1) * cur_step);
637         for (i = 0; i < *ds_cnt; i++)
638             printf(" %8.2e", srcptr[*ds_cnt * col + i]);
639         printf("\n");
640     }
641 #endif
642
643     /* We have to combine [reduce_factor] rows of the source
644      ** into one row for the destination.  Doing this we also
645      ** need to take care to combine the correct rows.  First
646      ** alter the start and end time so that they are multiples
647      ** of the new step time.  We cannot reduce the amount of
648      ** time so we have to move the end towards the future and
649      ** the start towards the past.
650      */
651     end_offset = (*end) % (*step);
652     start_offset = (*start) % (*step);
653
654     /* If there is a start offset (which cannot be more than
655      ** one destination row), skip the appropriate number of
656      ** source rows and one destination row.  The appropriate
657      ** number is what we do know (start_offset/cur_step) of
658      ** the new interval (*step/cur_step aka reduce_factor).
659      */
660 #ifdef DEBUG_REDUCE
661     printf("start_offset: %lu  end_offset: %lu\n", start_offset, end_offset);
662     printf("row_cnt before:  %lu\n", row_cnt);
663 #endif
664     if (start_offset) {
665         (*start) = (*start) - start_offset;
666         skiprows = reduce_factor - start_offset / cur_step;
667         srcptr += skiprows * *ds_cnt;
668         for (col = 0; col < (*ds_cnt); col++)
669             *dstptr++ = DNAN;
670         row_cnt -= skiprows;
671     }
672 #ifdef DEBUG_REDUCE
673     printf("row_cnt between: %lu\n", row_cnt);
674 #endif
675
676     /* At the end we have some rows that are not going to be
677      ** used, the amount is end_offset/cur_step
678      */
679     if (end_offset) {
680         (*end) = (*end) - end_offset + (*step);
681         skiprows = end_offset / cur_step;
682         row_cnt -= skiprows;
683     }
684 #ifdef DEBUG_REDUCE
685     printf("row_cnt after:   %lu\n", row_cnt);
686 #endif
687
688 /* Sanity check: row_cnt should be multiple of reduce_factor */
689 /* if this gets triggered, something is REALLY WRONG ... we die immediately */
690
691     if (row_cnt % reduce_factor) {
692         printf("SANITY CHECK: %lu rows cannot be reduced by %i \n",
693                row_cnt, reduce_factor);
694         printf("BUG in reduce_data()\n");
695         exit(1);
696     }
697
698     /* Now combine reduce_factor intervals at a time
699      ** into one interval for the destination.
700      */
701
702     for (dst_row = 0; (long int) row_cnt >= reduce_factor; dst_row++) {
703         for (col = 0; col < (*ds_cnt); col++) {
704             rrd_value_t newval = DNAN;
705             unsigned long validval = 0;
706
707             for (i = 0; i < reduce_factor; i++) {
708                 if (isnan(srcptr[i * (*ds_cnt) + col])) {
709                     continue;
710                 }
711                 validval++;
712                 if (isnan(newval))
713                     newval = srcptr[i * (*ds_cnt) + col];
714                 else {
715                     switch (cf) {
716                     case CF_HWPREDICT:
717                     case CF_MHWPREDICT:
718                     case CF_DEVSEASONAL:
719                     case CF_DEVPREDICT:
720                     case CF_SEASONAL:
721                     case CF_AVERAGE:
722                         newval += srcptr[i * (*ds_cnt) + col];
723                         break;
724                     case CF_MINIMUM:
725                         newval = min(newval, srcptr[i * (*ds_cnt) + col]);
726                         break;
727                     case CF_FAILURES:
728                         /* an interval contains a failure if any subintervals contained a failure */
729                     case CF_MAXIMUM:
730                         newval = max(newval, srcptr[i * (*ds_cnt) + col]);
731                         break;
732                     case CF_LAST:
733                         newval = srcptr[i * (*ds_cnt) + col];
734                         break;
735                     }
736                 }
737             }
738             if (validval == 0) {
739                 newval = DNAN;
740             } else {
741                 switch (cf) {
742                 case CF_HWPREDICT:
743                 case CF_MHWPREDICT:
744                 case CF_DEVSEASONAL:
745                 case CF_DEVPREDICT:
746                 case CF_SEASONAL:
747                 case CF_AVERAGE:
748                     newval /= validval;
749                     break;
750                 case CF_MINIMUM:
751                 case CF_FAILURES:
752                 case CF_MAXIMUM:
753                 case CF_LAST:
754                     break;
755                 }
756             }
757             *dstptr++ = newval;
758         }
759         srcptr += (*ds_cnt) * reduce_factor;
760         row_cnt -= reduce_factor;
761     }
762     /* If we had to alter the endtime, we didn't have enough
763      ** source rows to fill the last row. Fill it with NaN.
764      */
765     if (end_offset)
766         for (col = 0; col < (*ds_cnt); col++)
767             *dstptr++ = DNAN;
768 #ifdef DEBUG_REDUCE
769     row_cnt = ((*end) - (*start)) / *step;
770     srcptr = *data;
771     printf("Done reducing. Currently %lu rows, time %lu to %lu, step %lu\n",
772            row_cnt, *start, *end, *step);
773     for (col = 0; col < row_cnt; col++) {
774         printf("time %10lu: ", *start + (col + 1) * (*step));
775         for (i = 0; i < *ds_cnt; i++)
776             printf(" %8.2e", srcptr[*ds_cnt * col + i]);
777         printf("\n");
778     }
779 #endif
780 }
781
782
783 /* get the data required for the graphs from the 
784    relevant rrds ... */
785
786 int data_fetch(
787     image_desc_t *im)
788 {
789     int       i, ii;
790     int       skip;
791
792     /* pull the data from the rrd files ... */
793     for (i = 0; i < (int) im->gdes_c; i++) {
794         /* only GF_DEF elements fetch data */
795         if (im->gdes[i].gf != GF_DEF)
796             continue;
797
798         skip = 0;
799         /* do we have it already ? */
800         for (ii = 0; ii < i; ii++) {
801             if (im->gdes[ii].gf != GF_DEF)
802                 continue;
803             if ((strcmp(im->gdes[i].rrd, im->gdes[ii].rrd) == 0)
804                 && (im->gdes[i].cf == im->gdes[ii].cf)
805                 && (im->gdes[i].cf_reduce == im->gdes[ii].cf_reduce)
806                 && (im->gdes[i].start_orig == im->gdes[ii].start_orig)
807                 && (im->gdes[i].end_orig == im->gdes[ii].end_orig)
808                 && (im->gdes[i].step_orig == im->gdes[ii].step_orig)) {
809                 /* OK, the data is already there.
810                  ** Just copy the header portion
811                  */
812                 im->gdes[i].start = im->gdes[ii].start;
813                 im->gdes[i].end = im->gdes[ii].end;
814                 im->gdes[i].step = im->gdes[ii].step;
815                 im->gdes[i].ds_cnt = im->gdes[ii].ds_cnt;
816                 im->gdes[i].ds_namv = im->gdes[ii].ds_namv;
817                 im->gdes[i].data = im->gdes[ii].data;
818                 im->gdes[i].data_first = 0;
819                 skip = 1;
820             }
821             if (skip)
822                 break;
823         }
824         if (!skip) {
825             unsigned long ft_step = im->gdes[i].step;   /* ft_step will record what we got from fetch */
826
827             if ((rrd_fetch_fn(im->gdes[i].rrd,
828                               im->gdes[i].cf,
829                               &im->gdes[i].start,
830                               &im->gdes[i].end,
831                               &ft_step,
832                               &im->gdes[i].ds_cnt,
833                               &im->gdes[i].ds_namv,
834                               &im->gdes[i].data)) == -1) {
835                 return -1;
836             }
837             im->gdes[i].data_first = 1;
838
839             if (ft_step < im->gdes[i].step) {
840                 reduce_data(im->gdes[i].cf_reduce,
841                             ft_step,
842                             &im->gdes[i].start,
843                             &im->gdes[i].end,
844                             &im->gdes[i].step,
845                             &im->gdes[i].ds_cnt, &im->gdes[i].data);
846             } else {
847                 im->gdes[i].step = ft_step;
848             }
849         }
850
851         /* lets see if the required data source is really there */
852         for (ii = 0; ii < (int) im->gdes[i].ds_cnt; ii++) {
853             if (strcmp(im->gdes[i].ds_namv[ii], im->gdes[i].ds_nam) == 0) {
854                 im->gdes[i].ds = ii;
855             }
856         }
857         if (im->gdes[i].ds == -1) {
858             rrd_set_error("No DS called '%s' in '%s'",
859                           im->gdes[i].ds_nam, im->gdes[i].rrd);
860             return -1;
861         }
862
863     }
864     return 0;
865 }
866
867 /* evaluate the expressions in the CDEF functions */
868
869 /*************************************************************
870  * CDEF stuff 
871  *************************************************************/
872
873 long find_var_wrapper(
874     void *arg1,
875     char *key)
876 {
877     return find_var((image_desc_t *) arg1, key);
878 }
879
880 /* find gdes containing var*/
881 long find_var(
882     image_desc_t *im,
883     char *key)
884 {
885     long      ii;
886
887     for (ii = 0; ii < im->gdes_c - 1; ii++) {
888         if ((im->gdes[ii].gf == GF_DEF
889              || im->gdes[ii].gf == GF_VDEF || im->gdes[ii].gf == GF_CDEF)
890             && (strcmp(im->gdes[ii].vname, key) == 0)) {
891             return ii;
892         }
893     }
894     return -1;
895 }
896
897 /* find the largest common denominator for all the numbers
898    in the 0 terminated num array */
899 long lcd(
900     long *num)
901 {
902     long      rest;
903     int       i;
904
905     for (i = 0; num[i + 1] != 0; i++) {
906         do {
907             rest = num[i] % num[i + 1];
908             num[i] = num[i + 1];
909             num[i + 1] = rest;
910         } while (rest != 0);
911         num[i + 1] = num[i];
912     }
913 /*    return i==0?num[i]:num[i-1]; */
914     return num[i];
915 }
916
917 /* run the rpn calculator on all the VDEF and CDEF arguments */
918 int data_calc(
919     image_desc_t *im)
920 {
921
922     int       gdi;
923     int       dataidx;
924     long     *steparray, rpi;
925     int       stepcnt;
926     time_t    now;
927     rpnstack_t rpnstack;
928
929     rpnstack_init(&rpnstack);
930
931     for (gdi = 0; gdi < im->gdes_c; gdi++) {
932         /* Look for GF_VDEF and GF_CDEF in the same loop,
933          * so CDEFs can use VDEFs and vice versa
934          */
935         switch (im->gdes[gdi].gf) {
936         case GF_XPORT:
937             break;
938         case GF_SHIFT:{
939             graph_desc_t *vdp = &im->gdes[im->gdes[gdi].vidx];
940
941             /* remove current shift */
942             vdp->start -= vdp->shift;
943             vdp->end -= vdp->shift;
944
945             /* vdef */
946             if (im->gdes[gdi].shidx >= 0)
947                 vdp->shift = im->gdes[im->gdes[gdi].shidx].vf.val;
948             /* constant */
949             else
950                 vdp->shift = im->gdes[gdi].shval;
951
952             /* normalize shift to multiple of consolidated step */
953             vdp->shift = (vdp->shift / (long) vdp->step) * (long) vdp->step;
954
955             /* apply shift */
956             vdp->start += vdp->shift;
957             vdp->end += vdp->shift;
958             break;
959         }
960         case GF_VDEF:
961             /* A VDEF has no DS.  This also signals other parts
962              * of rrdtool that this is a VDEF value, not a CDEF.
963              */
964             im->gdes[gdi].ds_cnt = 0;
965             if (vdef_calc(im, gdi)) {
966                 rrd_set_error("Error processing VDEF '%s'",
967                               im->gdes[gdi].vname);
968                 rpnstack_free(&rpnstack);
969                 return -1;
970             }
971             break;
972         case GF_CDEF:
973             im->gdes[gdi].ds_cnt = 1;
974             im->gdes[gdi].ds = 0;
975             im->gdes[gdi].data_first = 1;
976             im->gdes[gdi].start = 0;
977             im->gdes[gdi].end = 0;
978             steparray = NULL;
979             stepcnt = 0;
980             dataidx = -1;
981
982             /* Find the variables in the expression.
983              * - VDEF variables are substituted by their values
984              *   and the opcode is changed into OP_NUMBER.
985              * - CDEF variables are analized for their step size,
986              *   the lowest common denominator of all the step
987              *   sizes of the data sources involved is calculated
988              *   and the resulting number is the step size for the
989              *   resulting data source.
990              */
991             for (rpi = 0; im->gdes[gdi].rpnp[rpi].op != OP_END; rpi++) {
992                 if (im->gdes[gdi].rpnp[rpi].op == OP_VARIABLE ||
993                     im->gdes[gdi].rpnp[rpi].op == OP_PREV_OTHER) {
994                     long      ptr = im->gdes[gdi].rpnp[rpi].ptr;
995
996                     if (im->gdes[ptr].ds_cnt == 0) {    /* this is a VDEF data source */
997 #if 0
998                         printf
999                             ("DEBUG: inside CDEF '%s' processing VDEF '%s'\n",
1000                              im->gdes[gdi].vname, im->gdes[ptr].vname);
1001                         printf("DEBUG: value from vdef is %f\n",
1002                                im->gdes[ptr].vf.val);
1003 #endif
1004                         im->gdes[gdi].rpnp[rpi].val = im->gdes[ptr].vf.val;
1005                         im->gdes[gdi].rpnp[rpi].op = OP_NUMBER;
1006                     } else {    /* normal variables and PREF(variables) */
1007
1008                         /* add one entry to the array that keeps track of the step sizes of the
1009                          * data sources going into the CDEF. */
1010                         if ((steparray =
1011                              rrd_realloc(steparray,
1012                                          (++stepcnt +
1013                                           1) * sizeof(*steparray))) == NULL) {
1014                             rrd_set_error("realloc steparray");
1015                             rpnstack_free(&rpnstack);
1016                             return -1;
1017                         };
1018
1019                         steparray[stepcnt - 1] = im->gdes[ptr].step;
1020
1021                         /* adjust start and end of cdef (gdi) so
1022                          * that it runs from the latest start point
1023                          * to the earliest endpoint of any of the
1024                          * rras involved (ptr)
1025                          */
1026
1027                         if (im->gdes[gdi].start < im->gdes[ptr].start)
1028                             im->gdes[gdi].start = im->gdes[ptr].start;
1029
1030                         if (im->gdes[gdi].end == 0 ||
1031                             im->gdes[gdi].end > im->gdes[ptr].end)
1032                             im->gdes[gdi].end = im->gdes[ptr].end;
1033
1034                         /* store pointer to the first element of
1035                          * the rra providing data for variable,
1036                          * further save step size and data source
1037                          * count of this rra
1038                          */
1039                         im->gdes[gdi].rpnp[rpi].data =
1040                             im->gdes[ptr].data + im->gdes[ptr].ds;
1041                         im->gdes[gdi].rpnp[rpi].step = im->gdes[ptr].step;
1042                         im->gdes[gdi].rpnp[rpi].ds_cnt = im->gdes[ptr].ds_cnt;
1043
1044                         /* backoff the *.data ptr; this is done so
1045                          * rpncalc() function doesn't have to treat
1046                          * the first case differently
1047                          */
1048                     }   /* if ds_cnt != 0 */
1049                 }       /* if OP_VARIABLE */
1050             }           /* loop through all rpi */
1051
1052             /* move the data pointers to the correct period */
1053             for (rpi = 0; im->gdes[gdi].rpnp[rpi].op != OP_END; rpi++) {
1054                 if (im->gdes[gdi].rpnp[rpi].op == OP_VARIABLE ||
1055                     im->gdes[gdi].rpnp[rpi].op == OP_PREV_OTHER) {
1056                     long      ptr = im->gdes[gdi].rpnp[rpi].ptr;
1057                     long      diff =
1058                         im->gdes[gdi].start - im->gdes[ptr].start;
1059
1060                     if (diff > 0)
1061                         im->gdes[gdi].rpnp[rpi].data +=
1062                             (diff / im->gdes[ptr].step) *
1063                             im->gdes[ptr].ds_cnt;
1064                 }
1065             }
1066
1067             if (steparray == NULL) {
1068                 rrd_set_error("rpn expressions without DEF"
1069                               " or CDEF variables are not supported");
1070                 rpnstack_free(&rpnstack);
1071                 return -1;
1072             }
1073             steparray[stepcnt] = 0;
1074             /* Now find the resulting step.  All steps in all
1075              * used RRAs have to be visited
1076              */
1077             im->gdes[gdi].step = lcd(steparray);
1078             free(steparray);
1079             if ((im->gdes[gdi].data = malloc(((im->gdes[gdi].end -
1080                                                im->gdes[gdi].start)
1081                                               / im->gdes[gdi].step)
1082                                              * sizeof(double))) == NULL) {
1083                 rrd_set_error("malloc im->gdes[gdi].data");
1084                 rpnstack_free(&rpnstack);
1085                 return -1;
1086             }
1087
1088             /* Step through the new cdef results array and
1089              * calculate the values
1090              */
1091             for (now = im->gdes[gdi].start + im->gdes[gdi].step;
1092                  now <= im->gdes[gdi].end; now += im->gdes[gdi].step) {
1093                 rpnp_t   *rpnp = im->gdes[gdi].rpnp;
1094
1095                 /* 3rd arg of rpn_calc is for OP_VARIABLE lookups;
1096                  * in this case we are advancing by timesteps;
1097                  * we use the fact that time_t is a synonym for long
1098                  */
1099                 if (rpn_calc(rpnp, &rpnstack, (long) now,
1100                              im->gdes[gdi].data, ++dataidx) == -1) {
1101                     /* rpn_calc sets the error string */
1102                     rpnstack_free(&rpnstack);
1103                     return -1;
1104                 }
1105             }           /* enumerate over time steps within a CDEF */
1106             break;
1107         default:
1108             continue;
1109         }
1110     }                   /* enumerate over CDEFs */
1111     rpnstack_free(&rpnstack);
1112     return 0;
1113 }
1114
1115 /* from http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm */
1116 /* yes we are loosing precision by doing tos with floats instead of doubles
1117    but it seems more stable this way. */
1118
1119 static int AlmostEqual2sComplement(
1120     float A,
1121     float B,
1122     int maxUlps)
1123 {
1124
1125     int       aInt = *(int *) &A;
1126     int       bInt = *(int *) &B;
1127     int       intDiff;
1128
1129     /* Make sure maxUlps is non-negative and small enough that the
1130        default NAN won't compare as equal to anything.  */
1131
1132     /* assert(maxUlps > 0 && maxUlps < 4 * 1024 * 1024); */
1133
1134     /* Make aInt lexicographically ordered as a twos-complement int */
1135
1136     if (aInt < 0)
1137         aInt = 0x80000000l - aInt;
1138
1139     /* Make bInt lexicographically ordered as a twos-complement int */
1140
1141     if (bInt < 0)
1142         bInt = 0x80000000l - bInt;
1143
1144     intDiff = abs(aInt - bInt);
1145
1146     if (intDiff <= maxUlps)
1147         return 1;
1148
1149     return 0;
1150 }
1151
1152 /* massage data so, that we get one value for each x coordinate in the graph */
1153 int data_proc(
1154     image_desc_t *im)
1155 {
1156     long      i, ii;
1157     double    pixstep = (double) (im->end - im->start)
1158         / (double) im->xsize;   /* how much time 
1159                                    passes in one pixel */
1160     double    paintval;
1161     double    minval = DNAN, maxval = DNAN;
1162
1163     unsigned long gr_time;
1164
1165     /* memory for the processed data */
1166     for (i = 0; i < im->gdes_c; i++) {
1167         if ((im->gdes[i].gf == GF_LINE) ||
1168             (im->gdes[i].gf == GF_AREA) || (im->gdes[i].gf == GF_TICK)) {
1169             if ((im->gdes[i].p_data = malloc((im->xsize + 1)
1170                                              * sizeof(rrd_value_t))) == NULL) {
1171                 rrd_set_error("malloc data_proc");
1172                 return -1;
1173             }
1174         }
1175     }
1176
1177     for (i = 0; i < im->xsize; i++) {   /* for each pixel */
1178         long      vidx;
1179
1180         gr_time = im->start + pixstep * i;  /* time of the current step */
1181         paintval = 0.0;
1182
1183         for (ii = 0; ii < im->gdes_c; ii++) {
1184             double    value;
1185
1186             switch (im->gdes[ii].gf) {
1187             case GF_LINE:
1188             case GF_AREA:
1189             case GF_TICK:
1190                 if (!im->gdes[ii].stack)
1191                     paintval = 0.0;
1192                 value = im->gdes[ii].yrule;
1193                 if (isnan(value) || (im->gdes[ii].gf == GF_TICK)) {
1194                     /* The time of the data doesn't necessarily match
1195                      ** the time of the graph. Beware.
1196                      */
1197                     vidx = im->gdes[ii].vidx;
1198                     if (im->gdes[vidx].gf == GF_VDEF) {
1199                         value = im->gdes[vidx].vf.val;
1200                     } else
1201                         if (((long int) gr_time >=
1202                              (long int) im->gdes[vidx].start)
1203                             && ((long int) gr_time <=
1204                                 (long int) im->gdes[vidx].end)) {
1205                         value = im->gdes[vidx].data[(unsigned long)
1206                                                     floor((double)
1207                                                           (gr_time -
1208                                                            im->gdes[vidx].
1209                                                            start)
1210                                                           /
1211                                                           im->gdes[vidx].step)
1212                                                     * im->gdes[vidx].ds_cnt +
1213                                                     im->gdes[vidx].ds];
1214                     } else {
1215                         value = DNAN;
1216                     }
1217                 };
1218
1219                 if (!isnan(value)) {
1220                     paintval += value;
1221                     im->gdes[ii].p_data[i] = paintval;
1222                     /* GF_TICK: the data values are not
1223                      ** relevant for min and max
1224                      */
1225                     if (finite(paintval) && im->gdes[ii].gf != GF_TICK) {
1226                         if ((isnan(minval) || paintval < minval) &&
1227                             !(im->logarithmic && paintval <= 0.0))
1228                             minval = paintval;
1229                         if (isnan(maxval) || paintval > maxval)
1230                             maxval = paintval;
1231                     }
1232                 } else {
1233                     im->gdes[ii].p_data[i] = DNAN;
1234                 }
1235                 break;
1236             case GF_STACK:
1237                 rrd_set_error
1238                     ("STACK should already be turned into LINE or AREA here");
1239                 return -1;
1240                 break;
1241             default:
1242                 break;
1243             }
1244         }
1245     }
1246
1247     /* if min or max have not been asigned a value this is because
1248        there was no data in the graph ... this is not good ...
1249        lets set these to dummy values then ... */
1250
1251     if (im->logarithmic) {
1252         if (isnan(minval) || isnan(maxval) || maxval <= 0) {
1253             minval = 0.0;   /* catching this right away below */
1254             maxval = 5.1;
1255         }
1256         /* in logarithm mode, where minval is smaller or equal 
1257            to 0 make the beast just way smaller than maxval */
1258         if (minval <= 0) {
1259             minval = maxval / 10e8;
1260         }
1261     } else {
1262         if (isnan(minval) || isnan(maxval)) {
1263             minval = 0.0;
1264             maxval = 1.0;
1265         }
1266     }
1267
1268     /* adjust min and max values given by the user */
1269     /* for logscale we add something on top */
1270     if (isnan(im->minval)
1271         || ((!im->rigid) && im->minval > minval)
1272         ) {
1273         if (im->logarithmic)
1274             im->minval = minval / 2.0;
1275         else
1276             im->minval = minval;
1277     }
1278     if (isnan(im->maxval)
1279         || (!im->rigid && im->maxval < maxval)
1280         ) {
1281         if (im->logarithmic)
1282             im->maxval = maxval * 2.0;
1283         else
1284             im->maxval = maxval;
1285     }
1286
1287     /* make sure min is smaller than max */
1288     if (im->minval > im->maxval) {
1289         if (im->minval > 0)
1290             im->minval = 0.99 * im->maxval;
1291         else
1292             im->minval = 1.01 * im->maxval;
1293     }
1294
1295     /* make sure min and max are not equal */
1296     if (AlmostEqual2sComplement(im->minval, im->maxval, 4)) {
1297         if (im->maxval > 0)
1298             im->maxval *= 1.01;
1299         else
1300             im->maxval *= 0.99;
1301
1302         /* make sure min and max are not both zero */
1303         if (AlmostEqual2sComplement(im->maxval, 0, 4)) {
1304             im->maxval = 1.0;
1305         }
1306     }
1307     return 0;
1308 }
1309
1310
1311
1312 /* identify the point where the first gridline, label ... gets placed */
1313
1314 time_t find_first_time(
1315     time_t start,       /* what is the initial time */
1316     enum tmt_en baseint,    /* what is the basic interval */
1317     long basestep       /* how many if these do we jump a time */
1318     )
1319 {
1320     struct tm tm;
1321
1322     localtime_r(&start, &tm);
1323
1324     switch (baseint) {
1325     case TMT_SECOND:
1326         tm.       tm_sec -= tm.tm_sec % basestep;
1327
1328         break;
1329     case TMT_MINUTE:
1330         tm.       tm_sec = 0;
1331         tm.       tm_min -= tm.tm_min % basestep;
1332
1333         break;
1334     case TMT_HOUR:
1335         tm.       tm_sec = 0;
1336         tm.       tm_min = 0;
1337         tm.       tm_hour -= tm.tm_hour % basestep;
1338
1339         break;
1340     case TMT_DAY:
1341         /* we do NOT look at the basestep for this ... */
1342         tm.       tm_sec = 0;
1343         tm.       tm_min = 0;
1344         tm.       tm_hour = 0;
1345
1346         break;
1347     case TMT_WEEK:
1348         /* we do NOT look at the basestep for this ... */
1349         tm.       tm_sec = 0;
1350         tm.       tm_min = 0;
1351         tm.       tm_hour = 0;
1352         tm.       tm_mday -= tm.tm_wday - 1;    /* -1 because we want the monday */
1353
1354         if (tm.tm_wday == 0)
1355             tm.       tm_mday -= 7; /* we want the *previous* monday */
1356
1357         break;
1358     case TMT_MONTH:
1359         tm.       tm_sec = 0;
1360         tm.       tm_min = 0;
1361         tm.       tm_hour = 0;
1362         tm.       tm_mday = 1;
1363         tm.       tm_mon -= tm.tm_mon % basestep;
1364
1365         break;
1366
1367     case TMT_YEAR:
1368         tm.       tm_sec = 0;
1369         tm.       tm_min = 0;
1370         tm.       tm_hour = 0;
1371         tm.       tm_mday = 1;
1372         tm.       tm_mon = 0;
1373         tm.       tm_year -= (
1374     tm.tm_year + 1900) %basestep;
1375
1376     }
1377     return mktime(&tm);
1378 }
1379
1380 /* identify the point where the next gridline, label ... gets placed */
1381 time_t find_next_time(
1382     time_t current,     /* what is the initial time */
1383     enum tmt_en baseint,    /* what is the basic interval */
1384     long basestep       /* how many if these do we jump a time */
1385     )
1386 {
1387     struct tm tm;
1388     time_t    madetime;
1389
1390     localtime_r(&current, &tm);
1391
1392     do {
1393         switch (baseint) {
1394         case TMT_SECOND:
1395             tm.       tm_sec += basestep;
1396
1397             break;
1398         case TMT_MINUTE:
1399             tm.       tm_min += basestep;
1400
1401             break;
1402         case TMT_HOUR:
1403             tm.       tm_hour += basestep;
1404
1405             break;
1406         case TMT_DAY:
1407             tm.       tm_mday += basestep;
1408
1409             break;
1410         case TMT_WEEK:
1411             tm.       tm_mday += 7 * basestep;
1412
1413             break;
1414         case TMT_MONTH:
1415             tm.       tm_mon += basestep;
1416
1417             break;
1418         case TMT_YEAR:
1419             tm.       tm_year += basestep;
1420         }
1421         madetime = mktime(&tm);
1422     } while (madetime == -1);   /* this is necessary to skip impssible times
1423                                    like the daylight saving time skips */
1424     return madetime;
1425
1426 }
1427
1428
1429 /* calculate values required for PRINT and GPRINT functions */
1430
1431 int print_calc(
1432     image_desc_t *im)
1433 {
1434     long      i, ii, validsteps;
1435     double    printval;
1436     struct tm tmvdef;
1437     int       graphelement = 0;
1438     long      vidx;
1439     int       max_ii;
1440     double    magfact = -1;
1441     char     *si_symb = "";
1442     char     *percent_s;
1443     int       prline_cnt = 0;
1444
1445     /* wow initializing tmvdef is quite a task :-) */
1446     time_t    now = time(NULL);
1447
1448     localtime_r(&now, &tmvdef);
1449     for (i = 0; i < im->gdes_c; i++) {
1450         vidx = im->gdes[i].vidx;
1451         switch (im->gdes[i].gf) {
1452         case GF_PRINT:
1453         case GF_GPRINT:
1454             /* PRINT and GPRINT can now print VDEF generated values.
1455              * There's no need to do any calculations on them as these
1456              * calculations were already made.
1457              */
1458             if (im->gdes[vidx].gf == GF_VDEF) { /* simply use vals */
1459                 printval = im->gdes[vidx].vf.val;
1460                 localtime_r(&im->gdes[vidx].vf.when, &tmvdef);
1461             } else {    /* need to calculate max,min,avg etcetera */
1462                 max_ii = ((im->gdes[vidx].end - im->gdes[vidx].start)
1463                           / im->gdes[vidx].step * im->gdes[vidx].ds_cnt);
1464                 printval = DNAN;
1465                 validsteps = 0;
1466                 for (ii = im->gdes[vidx].ds;
1467                      ii < max_ii; ii += im->gdes[vidx].ds_cnt) {
1468                     if (!finite(im->gdes[vidx].data[ii]))
1469                         continue;
1470                     if (isnan(printval)) {
1471                         printval = im->gdes[vidx].data[ii];
1472                         validsteps++;
1473                         continue;
1474                     }
1475
1476                     switch (im->gdes[i].cf) {
1477                     case CF_HWPREDICT:
1478                     case CF_MHWPREDICT:
1479                     case CF_DEVPREDICT:
1480                     case CF_DEVSEASONAL:
1481                     case CF_SEASONAL:
1482                     case CF_AVERAGE:
1483                         validsteps++;
1484                         printval += im->gdes[vidx].data[ii];
1485                         break;
1486                     case CF_MINIMUM:
1487                         printval = min(printval, im->gdes[vidx].data[ii]);
1488                         break;
1489                     case CF_FAILURES:
1490                     case CF_MAXIMUM:
1491                         printval = max(printval, im->gdes[vidx].data[ii]);
1492                         break;
1493                     case CF_LAST:
1494                         printval = im->gdes[vidx].data[ii];
1495                     }
1496                 }
1497                 if (im->gdes[i].cf == CF_AVERAGE || im->gdes[i].cf > CF_LAST) {
1498                     if (validsteps > 1) {
1499                         printval = (printval / validsteps);
1500                     }
1501                 }
1502             }           /* prepare printval */
1503
1504             if ((percent_s = strstr(im->gdes[i].format, "%S")) != NULL) {
1505                 /* Magfact is set to -1 upon entry to print_calc.  If it
1506                  * is still less than 0, then we need to run auto_scale.
1507                  * Otherwise, put the value into the correct units.  If
1508                  * the value is 0, then do not set the symbol or magnification
1509                  * so next the calculation will be performed again. */
1510                 if (magfact < 0.0) {
1511                     auto_scale(im, &printval, &si_symb, &magfact);
1512                     if (printval == 0.0)
1513                         magfact = -1.0;
1514                 } else {
1515                     printval /= magfact;
1516                 }
1517                 *(++percent_s) = 's';
1518             } else if (strstr(im->gdes[i].format, "%s") != NULL) {
1519                 auto_scale(im, &printval, &si_symb, &magfact);
1520             }
1521
1522             if (im->gdes[i].gf == GF_PRINT) {
1523                 infoval   prline;
1524
1525                 if (im->gdes[i].strftm) {
1526                     prline.u_str = malloc((FMT_LEG_LEN + 2) * sizeof(char));
1527                     strftime(prline.u_str,
1528                              FMT_LEG_LEN, im->gdes[i].format, &tmvdef);
1529                 } else if (bad_format(im->gdes[i].format)) {
1530                     rrd_set_error
1531                         ("bad format for PRINT in '%s'", im->gdes[i].format);
1532                     return -1;
1533                 } else {
1534                     prline.u_str =
1535                         sprintf_alloc(im->gdes[i].format, printval, si_symb);
1536                 }
1537                 grinfo_push(im,
1538                             sprintf_alloc
1539                             ("print[%ld]", prline_cnt++), RD_I_STR, prline);
1540                 free(prline.u_str);
1541             } else {
1542                 /* GF_GPRINT */
1543
1544                 if (im->gdes[i].strftm) {
1545                     strftime(im->gdes[i].legend,
1546                              FMT_LEG_LEN, im->gdes[i].format, &tmvdef);
1547                 } else {
1548                     if (bad_format(im->gdes[i].format)) {
1549                         rrd_set_error
1550                             ("bad format for GPRINT in '%s'",
1551                              im->gdes[i].format);
1552                         return -1;
1553                     }
1554 #ifdef HAVE_SNPRINTF
1555                     snprintf(im->gdes[i].legend,
1556                              FMT_LEG_LEN - 2,
1557                              im->gdes[i].format, printval, si_symb);
1558 #else
1559                     sprintf(im->gdes[i].legend,
1560                             im->gdes[i].format, printval, si_symb);
1561 #endif
1562                 }
1563                 graphelement = 1;
1564             }
1565             break;
1566         case GF_LINE:
1567         case GF_AREA:
1568         case GF_TICK:
1569             graphelement = 1;
1570             break;
1571         case GF_HRULE:
1572             if (isnan(im->gdes[i].yrule)) { /* we must set this here or the legend printer can not decide to print the legend */
1573                 im->gdes[i].yrule = im->gdes[vidx].vf.val;
1574             };
1575             graphelement = 1;
1576             break;
1577         case GF_VRULE:
1578             if (im->gdes[i].xrule == 0) {   /* again ... the legend printer needs it */
1579                 im->gdes[i].xrule = im->gdes[vidx].vf.when;
1580             };
1581             graphelement = 1;
1582             break;
1583         case GF_COMMENT:
1584         case GF_TEXTALIGN:
1585         case GF_DEF:
1586         case GF_CDEF:
1587         case GF_VDEF:
1588 #ifdef WITH_PIECHART
1589         case GF_PART:
1590 #endif
1591         case GF_SHIFT:
1592         case GF_XPORT:
1593             break;
1594         case GF_STACK:
1595             rrd_set_error
1596                 ("STACK should already be turned into LINE or AREA here");
1597             return -1;
1598             break;
1599         }
1600     }
1601     return graphelement;
1602 }
1603
1604
1605 /* place legends with color spots */
1606 int leg_place(
1607     image_desc_t *im,
1608     int *gY)
1609 {
1610     /* graph labels */
1611     int       interleg = im->text_prop[TEXT_PROP_LEGEND].size * 2.0;
1612     int       border = im->text_prop[TEXT_PROP_LEGEND].size * 2.0;
1613     int       fill = 0, fill_last;
1614     int       leg_c = 0;
1615     int       leg_x = border, leg_y = im->yimg;
1616     int       leg_y_prev = im->yimg;
1617     int       leg_cc;
1618     int       glue = 0;
1619     int       i, ii, mark = 0;
1620     char      prt_fctn; /*special printfunctions */
1621     char      default_txtalign = TXA_JUSTIFIED; /*default line orientation */
1622     int      *legspace;
1623
1624     if (!(im->extra_flags & NOLEGEND) & !(im->extra_flags & ONLY_GRAPH)) {
1625         if ((legspace = malloc(im->gdes_c * sizeof(int))) == NULL) {
1626             rrd_set_error("malloc for legspace");
1627             return -1;
1628         }
1629
1630         if (im->extra_flags & FULL_SIZE_MODE)
1631             leg_y = leg_y_prev =
1632                 leg_y - (int) (im->text_prop[TEXT_PROP_LEGEND].size * 1.8);
1633         for (i = 0; i < im->gdes_c; i++) {
1634             fill_last = fill;
1635             /* hide legends for rules which are not displayed */
1636             if (im->gdes[i].gf == GF_TEXTALIGN) {
1637                 default_txtalign = im->gdes[i].txtalign;
1638             }
1639
1640             if (!(im->extra_flags & FORCE_RULES_LEGEND)) {
1641                 if (im->gdes[i].gf == GF_HRULE
1642                     && (im->gdes[i].yrule <
1643                         im->minval || im->gdes[i].yrule > im->maxval))
1644                     im->gdes[i].legend[0] = '\0';
1645                 if (im->gdes[i].gf == GF_VRULE
1646                     && (im->gdes[i].xrule <
1647                         im->start || im->gdes[i].xrule > im->end))
1648                     im->gdes[i].legend[0] = '\0';
1649             }
1650
1651             leg_cc = strlen(im->gdes[i].legend);
1652             /* is there a controle code ant the end of the legend string ? */
1653             /* and it is not a tab \\t */
1654             if (leg_cc >= 2
1655                 && im->gdes[i].legend[leg_cc -
1656                                       2] == '\\'
1657                 && im->gdes[i].legend[leg_cc - 1] != 't') {
1658                 prt_fctn = im->gdes[i].legend[leg_cc - 1];
1659                 leg_cc -= 2;
1660                 im->gdes[i].legend[leg_cc] = '\0';
1661             } else {
1662                 prt_fctn = '\0';
1663             }
1664             /* only valid control codes */
1665             if (prt_fctn != 'l' && prt_fctn != 'n' &&   /* a synonym for l */
1666                 prt_fctn != 'r' &&
1667                 prt_fctn != 'j' &&
1668                 prt_fctn != 'c' &&
1669                 prt_fctn != 's' &&
1670                 prt_fctn != 't' && prt_fctn != '\0' && prt_fctn != 'g') {
1671                 free(legspace);
1672                 rrd_set_error
1673                     ("Unknown control code at the end of '%s\\%c'",
1674                      im->gdes[i].legend, prt_fctn);
1675                 return -1;
1676             }
1677             /* \n -> \l */
1678             if (prt_fctn == 'n') {
1679                 prt_fctn = 'l';
1680             }
1681
1682             /* remove exess space from the end of the legend for \g */
1683             while (prt_fctn == 'g' &&
1684                    leg_cc > 0 && im->gdes[i].legend[leg_cc - 1] == ' ') {
1685                 leg_cc--;
1686                 im->gdes[i].legend[leg_cc] = '\0';
1687             }
1688
1689             if (leg_cc != 0) {
1690
1691                 /* no interleg space if string ends in \g */
1692                 legspace[i] = (prt_fctn == 'g' ? 0 : interleg);
1693                 if (fill > 0) {
1694                     fill += legspace[i];
1695                 }
1696                 fill +=
1697                     gfx_get_text_width(im,
1698                                        fill + border,
1699                                        im->
1700                                        text_prop
1701                                        [TEXT_PROP_LEGEND].
1702                                        font,
1703                                        im->
1704                                        text_prop
1705                                        [TEXT_PROP_LEGEND].
1706                                        size,
1707                                        im->tabwidth, im->gdes[i].legend);
1708                 leg_c++;
1709             } else {
1710                 legspace[i] = 0;
1711             }
1712             /* who said there was a special tag ... ? */
1713             if (prt_fctn == 'g') {
1714                 prt_fctn = '\0';
1715             }
1716
1717             if (prt_fctn == '\0') {
1718                 if (i == im->gdes_c - 1 || fill > im->ximg - 2 * border) {
1719                     /* just one legend item is left right or center */
1720                     switch (default_txtalign) {
1721                     case TXA_RIGHT:
1722                         prt_fctn = 'r';
1723                         break;
1724                     case TXA_CENTER:
1725                         prt_fctn = 'c';
1726                         break;
1727                     case TXA_JUSTIFIED:
1728                         prt_fctn = 'j';
1729                         break;
1730                     default:
1731                         prt_fctn = 'l';
1732                         break;
1733                     }
1734                 }
1735                 /* is it time to place the legends ? */
1736                 if (fill > im->ximg - 2 * border) {
1737                     if (leg_c > 1) {
1738                         /* go back one */
1739                         i--;
1740                         fill = fill_last;
1741                         leg_c--;
1742                     }
1743                 }
1744                 if (leg_c == 1 && prt_fctn == 'j') {
1745                     prt_fctn = 'l';
1746                 }
1747             }
1748
1749
1750             if (prt_fctn != '\0') {
1751                 leg_x = border;
1752                 if (leg_c >= 2 && prt_fctn == 'j') {
1753                     glue = (im->ximg - fill - 2 * border) / (leg_c - 1);
1754                 } else {
1755                     glue = 0;
1756                 }
1757                 if (prt_fctn == 'c')
1758                     leg_x = (im->ximg - fill) / 2.0;
1759                 if (prt_fctn == 'r')
1760                     leg_x = im->ximg - fill - border;
1761                 for (ii = mark; ii <= i; ii++) {
1762                     if (im->gdes[ii].legend[0] == '\0')
1763                         continue;   /* skip empty legends */
1764                     im->gdes[ii].leg_x = leg_x;
1765                     im->gdes[ii].leg_y = leg_y;
1766                     leg_x +=
1767                         gfx_get_text_width(im, leg_x,
1768                                            im->
1769                                            text_prop
1770                                            [TEXT_PROP_LEGEND].
1771                                            font,
1772                                            im->
1773                                            text_prop
1774                                            [TEXT_PROP_LEGEND].
1775                                            size,
1776                                            im->tabwidth, im->gdes[ii].legend)
1777                         + legspace[ii]
1778                         + glue;
1779                 }
1780                 leg_y_prev = leg_y;
1781                 if (im->extra_flags & FULL_SIZE_MODE) {
1782                     /* only add y space if there was text on the line */
1783                     if (leg_x > border || prt_fctn == 's')
1784                         leg_y -= im->text_prop[TEXT_PROP_LEGEND].size * 1.8;
1785                     if (prt_fctn == 's')
1786                         leg_y += im->text_prop[TEXT_PROP_LEGEND].size;
1787                 } else {
1788                     if (leg_x > border || prt_fctn == 's')
1789                         leg_y += im->text_prop[TEXT_PROP_LEGEND].size * 1.8;
1790                     if (prt_fctn == 's')
1791                         leg_y -= im->text_prop[TEXT_PROP_LEGEND].size;
1792                 }
1793                 fill = 0;
1794                 leg_c = 0;
1795                 mark = ii;
1796             }
1797         }
1798
1799         if (im->extra_flags & FULL_SIZE_MODE) {
1800             if (leg_y != leg_y_prev) {
1801                 *gY = leg_y - im->text_prop[TEXT_PROP_LEGEND].size * 1.8;
1802                 im->yorigin =
1803                     leg_y - im->text_prop[TEXT_PROP_LEGEND].size * 1.8;
1804             }
1805         } else {
1806             im->yimg = leg_y_prev;
1807             /* if we did place some legends we have to add vertical space */
1808             if (leg_y != im->yimg)
1809                 im->yimg += im->text_prop[TEXT_PROP_LEGEND].size * 1.8;
1810         }
1811         free(legspace);
1812     }
1813     return 0;
1814 }
1815
1816 /* create a grid on the graph. it determines what to do
1817    from the values of xsize, start and end */
1818
1819 /* the xaxis labels are determined from the number of seconds per pixel
1820    in the requested graph */
1821
1822
1823
1824 int calc_horizontal_grid(
1825     image_desc_t
1826     *im)
1827 {
1828     double    range;
1829     double    scaledrange;
1830     int       pixel, i;
1831     int       gridind = 0;
1832     int       decimals, fractionals;
1833
1834     im->ygrid_scale.labfact = 2;
1835     range = im->maxval - im->minval;
1836     scaledrange = range / im->magfact;
1837     /* does the scale of this graph make it impossible to put lines
1838        on it? If so, give up. */
1839     if (isnan(scaledrange)) {
1840         return 0;
1841     }
1842
1843     /* find grid spaceing */
1844     pixel = 1;
1845     if (isnan(im->ygridstep)) {
1846         if (im->extra_flags & ALTYGRID) {
1847             /* find the value with max number of digits. Get number of digits */
1848             decimals =
1849                 ceil(log10
1850                      (max(fabs(im->maxval), fabs(im->minval)) *
1851                       im->viewfactor / im->magfact));
1852             if (decimals <= 0)  /* everything is small. make place for zero */
1853                 decimals = 1;
1854             im->ygrid_scale.gridstep =
1855                 pow((double) 10,
1856                     floor(log10(range * im->viewfactor / im->magfact))) /
1857                 im->viewfactor * im->magfact;
1858             if (im->ygrid_scale.gridstep == 0)  /* range is one -> 0.1 is reasonable scale */
1859                 im->ygrid_scale.gridstep = 0.1;
1860             /* should have at least 5 lines but no more then 15 */
1861             if (range / im->ygrid_scale.gridstep < 5)
1862                 im->ygrid_scale.gridstep /= 10;
1863             if (range / im->ygrid_scale.gridstep > 15)
1864                 im->ygrid_scale.gridstep *= 10;
1865             if (range / im->ygrid_scale.gridstep > 5) {
1866                 im->ygrid_scale.labfact = 1;
1867                 if (range / im->ygrid_scale.gridstep > 8)
1868                     im->ygrid_scale.labfact = 2;
1869             } else {
1870                 im->ygrid_scale.gridstep /= 5;
1871                 im->ygrid_scale.labfact = 5;
1872             }
1873             fractionals =
1874                 floor(log10
1875                       (im->ygrid_scale.gridstep *
1876                        (double) im->ygrid_scale.labfact * im->viewfactor /
1877                        im->magfact));
1878             if (fractionals < 0) {  /* small amplitude. */
1879                 int       len = decimals - fractionals + 1;
1880
1881                 if (im->unitslength < len + 2)
1882                     im->unitslength = len + 2;
1883                 sprintf(im->ygrid_scale.labfmt,
1884                         "%%%d.%df%s", len,
1885                         -fractionals, (im->symbol != ' ' ? " %c" : ""));
1886             } else {
1887                 int       len = decimals + 1;
1888
1889                 if (im->unitslength < len + 2)
1890                     im->unitslength = len + 2;
1891                 sprintf(im->ygrid_scale.labfmt,
1892                         "%%%d.0f%s", len, (im->symbol != ' ' ? " %c" : ""));
1893             }
1894         } else {
1895             for (i = 0; ylab[i].grid > 0; i++) {
1896                 pixel = im->ysize / (scaledrange / ylab[i].grid);
1897                 gridind = i;
1898                 if (pixel > 7)
1899                     break;
1900             }
1901
1902             for (i = 0; i < 4; i++) {
1903                 if (pixel * ylab[gridind].lfac[i] >=
1904                     2.5 * im->text_prop[TEXT_PROP_AXIS].size) {
1905                     im->ygrid_scale.labfact = ylab[gridind].lfac[i];
1906                     break;
1907                 }
1908             }
1909
1910             im->ygrid_scale.gridstep = ylab[gridind].grid * im->magfact;
1911         }
1912     } else {
1913         im->ygrid_scale.gridstep = im->ygridstep;
1914         im->ygrid_scale.labfact = im->ylabfact;
1915     }
1916     return 1;
1917 }
1918
1919 int draw_horizontal_grid(
1920     image_desc_t
1921     *im)
1922 {
1923     int       i;
1924     double    scaledstep;
1925     char      graph_label[100];
1926     int       nlabels = 0;
1927     double    X0 = im->xorigin;
1928     double    X1 = im->xorigin + im->xsize;
1929     int       sgrid = (int) (im->minval / im->ygrid_scale.gridstep - 1);
1930     int       egrid = (int) (im->maxval / im->ygrid_scale.gridstep + 1);
1931     double    MaxY;
1932
1933     scaledstep =
1934         im->ygrid_scale.gridstep /
1935         (double) im->magfact * (double) im->viewfactor;
1936     MaxY = scaledstep * (double) egrid;
1937     for (i = sgrid; i <= egrid; i++) {
1938         double    Y0 = ytr(im,
1939                            im->ygrid_scale.gridstep * i);
1940         double    YN = ytr(im,
1941                            im->ygrid_scale.gridstep * (i + 1));
1942
1943         if (floor(Y0 + 0.5) >=
1944             im->yorigin - im->ysize && floor(Y0 + 0.5) <= im->yorigin) {
1945             /* Make sure at least 2 grid labels are shown, even if it doesn't agree
1946                with the chosen settings. Add a label if required by settings, or if
1947                there is only one label so far and the next grid line is out of bounds. */
1948             if (i % im->ygrid_scale.labfact == 0
1949                 || (nlabels == 1
1950                     && (YN < im->yorigin - im->ysize || YN > im->yorigin))) {
1951                 if (im->symbol == ' ') {
1952                     if (im->extra_flags & ALTYGRID) {
1953                         sprintf(graph_label,
1954                                 im->ygrid_scale.labfmt,
1955                                 scaledstep * (double) i);
1956                     } else {
1957                         if (MaxY < 10) {
1958                             sprintf(graph_label, "%4.1f",
1959                                     scaledstep * (double) i);
1960                         } else {
1961                             sprintf(graph_label, "%4.0f",
1962                                     scaledstep * (double) i);
1963                         }
1964                     }
1965                 } else {
1966                     char      sisym = (i == 0 ? ' ' : im->symbol);
1967
1968                     if (im->extra_flags & ALTYGRID) {
1969                         sprintf(graph_label,
1970                                 im->ygrid_scale.labfmt,
1971                                 scaledstep * (double) i, sisym);
1972                     } else {
1973                         if (MaxY < 10) {
1974                             sprintf(graph_label, "%4.1f %c",
1975                                     scaledstep * (double) i, sisym);
1976                         } else {
1977                             sprintf(graph_label, "%4.0f %c",
1978                                     scaledstep * (double) i, sisym);
1979                         }
1980                     }
1981                 }
1982                 nlabels++;
1983                 gfx_text(im,
1984                          X0 -
1985                          im->
1986                          text_prop[TEXT_PROP_AXIS].
1987                          size, Y0,
1988                          im->graph_col[GRC_FONT],
1989                          im->
1990                          text_prop[TEXT_PROP_AXIS].
1991                          font,
1992                          im->
1993                          text_prop[TEXT_PROP_AXIS].
1994                          size, im->tabwidth, 0.0,
1995                          GFX_H_RIGHT, GFX_V_CENTER, graph_label);
1996                 gfx_line(im, X0 - 2, Y0, X0, Y0,
1997                          MGRIDWIDTH, im->graph_col[GRC_MGRID]);
1998                 gfx_line(im, X1, Y0, X1 + 2, Y0,
1999                          MGRIDWIDTH, im->graph_col[GRC_MGRID]);
2000                 gfx_dashed_line(im, X0 - 2, Y0,
2001                                 X1 + 2, Y0,
2002                                 MGRIDWIDTH,
2003                                 im->
2004                                 graph_col
2005                                 [GRC_MGRID],
2006                                 im->grid_dash_on, im->grid_dash_off);
2007             } else if (!(im->extra_flags & NOMINOR)) {
2008                 gfx_line(im,
2009                          X0 - 2, Y0,
2010                          X0, Y0, GRIDWIDTH, im->graph_col[GRC_GRID]);
2011                 gfx_line(im, X1, Y0, X1 + 2, Y0,
2012                          GRIDWIDTH, im->graph_col[GRC_GRID]);
2013                 gfx_dashed_line(im, X0 - 1, Y0,
2014                                 X1 + 1, Y0,
2015                                 GRIDWIDTH,
2016                                 im->
2017                                 graph_col[GRC_GRID],
2018                                 im->grid_dash_on, im->grid_dash_off);
2019             }
2020         }
2021     }
2022     return 1;
2023 }
2024
2025 /* this is frexp for base 10 */
2026 double    frexp10(
2027     double,
2028     double *);
2029 double frexp10(
2030     double x,
2031     double *e)
2032 {
2033     double    mnt;
2034     int       iexp;
2035
2036     iexp = floor(log(fabs(x)) / log(10));
2037     mnt = x / pow(10.0, iexp);
2038     if (mnt >= 10.0) {
2039         iexp++;
2040         mnt = x / pow(10.0, iexp);
2041     }
2042     *e = iexp;
2043     return mnt;
2044 }
2045
2046
2047 /* logaritmic horizontal grid */
2048 int horizontal_log_grid(
2049     image_desc_t
2050     *im)
2051 {
2052     double    yloglab[][10] = {
2053         {
2054          1.0, 10., 0.0, 0.0, 0.0, 0.0, 0.0,
2055          0.0, 0.0, 0.0}, {
2056                           1.0, 5.0, 10., 0.0, 0.0, 0.0, 0.0,
2057                           0.0, 0.0, 0.0}, {
2058                                            1.0, 2.0, 5.0, 7.0, 10., 0.0, 0.0,
2059                                            0.0, 0.0, 0.0}, {
2060                                                             1.0, 2.0, 4.0,
2061                                                             6.0, 8.0, 10.,
2062                                                             0.0,
2063                                                             0.0, 0.0, 0.0}, {
2064                                                                              1.0,
2065                                                                              2.0,
2066                                                                              3.0,
2067                                                                              4.0,
2068                                                                              5.0,
2069                                                                              6.0,
2070                                                                              7.0,
2071                                                                              8.0,
2072                                                                              9.0,
2073                                                                              10.},
2074         {
2075          0, 0, 0, 0, 0, 0, 0, 0, 0, 0}  /* last line */
2076     };
2077     int       i, j, val_exp, min_exp;
2078     double    nex;      /* number of decades in data */
2079     double    logscale; /* scale in logarithmic space */
2080     int       exfrac = 1;   /* decade spacing */
2081     int       mid = -1; /* row in yloglab for major grid */
2082     double    mspac;    /* smallest major grid spacing (pixels) */
2083     int       flab;     /* first value in yloglab to use */
2084     double    value, tmp, pre_value;
2085     double    X0, X1, Y0;
2086     char      graph_label[100];
2087
2088     nex = log10(im->maxval / im->minval);
2089     logscale = im->ysize / nex;
2090     /* major spacing for data with high dynamic range */
2091     while (logscale * exfrac < 3 * im->text_prop[TEXT_PROP_LEGEND].size) {
2092         if (exfrac == 1)
2093             exfrac = 3;
2094         else
2095             exfrac += 3;
2096     }
2097
2098     /* major spacing for less dynamic data */
2099     do {
2100         /* search best row in yloglab */
2101         mid++;
2102         for (i = 0; yloglab[mid][i + 1] < 10.0; i++);
2103         mspac = logscale * log10(10.0 / yloglab[mid][i]);
2104     }
2105     while (mspac >
2106            2 * im->text_prop[TEXT_PROP_LEGEND].size && yloglab[mid][0] > 0);
2107     if (mid)
2108         mid--;
2109     /* find first value in yloglab */
2110     for (flab = 0;
2111          yloglab[mid][flab] < 10
2112          && frexp10(im->minval, &tmp) > yloglab[mid][flab]; flab++);
2113     if (yloglab[mid][flab] == 10.0) {
2114         tmp += 1.0;
2115         flab = 0;
2116     }
2117     val_exp = tmp;
2118     if (val_exp % exfrac)
2119         val_exp += abs(-val_exp % exfrac);
2120     X0 = im->xorigin;
2121     X1 = im->xorigin + im->xsize;
2122     /* draw grid */
2123     pre_value = DNAN;
2124     while (1) {
2125
2126         value = yloglab[mid][flab] * pow(10.0, val_exp);
2127         if (AlmostEqual2sComplement(value, pre_value, 4))
2128             break;      /* it seems we are not converging */
2129         pre_value = value;
2130         Y0 = ytr(im, value);
2131         if (floor(Y0 + 0.5) <= im->yorigin - im->ysize)
2132             break;
2133         /* major grid line */
2134         gfx_line(im,
2135                  X0 - 2, Y0, X0, Y0, MGRIDWIDTH, im->graph_col[GRC_MGRID]);
2136         gfx_line(im, X1, Y0, X1 + 2, Y0,
2137                  MGRIDWIDTH, im->graph_col[GRC_MGRID]);
2138         gfx_dashed_line(im, X0 - 2, Y0,
2139                         X1 + 2, Y0,
2140                         MGRIDWIDTH,
2141                         im->
2142                         graph_col
2143                         [GRC_MGRID], im->grid_dash_on, im->grid_dash_off);
2144         /* label */
2145         if (im->extra_flags & FORCE_UNITS_SI) {
2146             int       scale;
2147             double    pvalue;
2148             char      symbol;
2149
2150             scale = floor(val_exp / 3.0);
2151             if (value >= 1.0)
2152                 pvalue = pow(10.0, val_exp % 3);
2153             else
2154                 pvalue = pow(10.0, ((val_exp + 1) % 3) + 2);
2155             pvalue *= yloglab[mid][flab];
2156             if (((scale + si_symbcenter) < (int) sizeof(si_symbol))
2157                 && ((scale + si_symbcenter) >= 0))
2158                 symbol = si_symbol[scale + si_symbcenter];
2159             else
2160                 symbol = '?';
2161             sprintf(graph_label, "%3.0f %c", pvalue, symbol);
2162         } else
2163             sprintf(graph_label, "%3.0e", value);
2164         gfx_text(im,
2165                  X0 -
2166                  im->
2167                  text_prop[TEXT_PROP_AXIS].
2168                  size, Y0,
2169                  im->graph_col[GRC_FONT],
2170                  im->
2171                  text_prop[TEXT_PROP_AXIS].
2172                  font,
2173                  im->
2174                  text_prop[TEXT_PROP_AXIS].
2175                  size, im->tabwidth, 0.0,
2176                  GFX_H_RIGHT, GFX_V_CENTER, graph_label);
2177         /* minor grid */
2178         if (mid < 4 && exfrac == 1) {
2179             /* find first and last minor line behind current major line
2180              * i is the first line and j tha last */
2181             if (flab == 0) {
2182                 min_exp = val_exp - 1;
2183                 for (i = 1; yloglab[mid][i] < 10.0; i++);
2184                 i = yloglab[mid][i - 1] + 1;
2185                 j = 10;
2186             } else {
2187                 min_exp = val_exp;
2188                 i = yloglab[mid][flab - 1] + 1;
2189                 j = yloglab[mid][flab];
2190             }
2191
2192             /* draw minor lines below current major line */
2193             for (; i < j; i++) {
2194
2195                 value = i * pow(10.0, min_exp);
2196                 if (value < im->minval)
2197                     continue;
2198                 Y0 = ytr(im, value);
2199                 if (floor(Y0 + 0.5) <= im->yorigin - im->ysize)
2200                     break;
2201                 /* draw lines */
2202                 gfx_line(im,
2203                          X0 - 2, Y0,
2204                          X0, Y0, GRIDWIDTH, im->graph_col[GRC_GRID]);
2205                 gfx_line(im, X1, Y0, X1 + 2, Y0,
2206                          GRIDWIDTH, im->graph_col[GRC_GRID]);
2207                 gfx_dashed_line(im, X0 - 1, Y0,
2208                                 X1 + 1, Y0,
2209                                 GRIDWIDTH,
2210                                 im->
2211                                 graph_col[GRC_GRID],
2212                                 im->grid_dash_on, im->grid_dash_off);
2213             }
2214         } else if (exfrac > 1) {
2215             for (i = val_exp - exfrac / 3 * 2; i < val_exp; i += exfrac / 3) {
2216                 value = pow(10.0, i);
2217                 if (value < im->minval)
2218                     continue;
2219                 Y0 = ytr(im, value);
2220                 if (floor(Y0 + 0.5) <= im->yorigin - im->ysize)
2221                     break;
2222                 /* draw lines */
2223                 gfx_line(im,
2224                          X0 - 2, Y0,
2225                          X0, Y0, GRIDWIDTH, im->graph_col[GRC_GRID]);
2226                 gfx_line(im, X1, Y0, X1 + 2, Y0,
2227                          GRIDWIDTH, im->graph_col[GRC_GRID]);
2228                 gfx_dashed_line(im, X0 - 1, Y0,
2229                                 X1 + 1, Y0,
2230                                 GRIDWIDTH,
2231                                 im->
2232                                 graph_col[GRC_GRID],
2233                                 im->grid_dash_on, im->grid_dash_off);
2234             }
2235         }
2236
2237         /* next decade */
2238         if (yloglab[mid][++flab] == 10.0) {
2239             flab = 0;
2240             val_exp += exfrac;
2241         }
2242     }
2243
2244     /* draw minor lines after highest major line */
2245     if (mid < 4 && exfrac == 1) {
2246         /* find first and last minor line below current major line
2247          * i is the first line and j tha last */
2248         if (flab == 0) {
2249             min_exp = val_exp - 1;
2250             for (i = 1; yloglab[mid][i] < 10.0; i++);
2251             i = yloglab[mid][i - 1] + 1;
2252             j = 10;
2253         } else {
2254             min_exp = val_exp;
2255             i = yloglab[mid][flab - 1] + 1;
2256             j = yloglab[mid][flab];
2257         }
2258
2259         /* draw minor lines below current major line */
2260         for (; i < j; i++) {
2261
2262             value = i * pow(10.0, min_exp);
2263             if (value < im->minval)
2264                 continue;
2265             Y0 = ytr(im, value);
2266             if (floor(Y0 + 0.5) <= im->yorigin - im->ysize)
2267                 break;
2268             /* draw lines */
2269             gfx_line(im,
2270                      X0 - 2, Y0, X0, Y0, GRIDWIDTH, im->graph_col[GRC_GRID]);
2271             gfx_line(im, X1, Y0, X1 + 2, Y0,
2272                      GRIDWIDTH, im->graph_col[GRC_GRID]);
2273             gfx_dashed_line(im, X0 - 1, Y0,
2274                             X1 + 1, Y0,
2275                             GRIDWIDTH,
2276                             im->
2277                             graph_col[GRC_GRID],
2278                             im->grid_dash_on, im->grid_dash_off);
2279         }
2280     }
2281     /* fancy minor gridlines */
2282     else if (exfrac > 1) {
2283         for (i = val_exp - exfrac / 3 * 2; i < val_exp; i += exfrac / 3) {
2284             value = pow(10.0, i);
2285             if (value < im->minval)
2286                 continue;
2287             Y0 = ytr(im, value);
2288             if (floor(Y0 + 0.5) <= im->yorigin - im->ysize)
2289                 break;
2290             /* draw lines */
2291             gfx_line(im,
2292                      X0 - 2, Y0, X0, Y0, GRIDWIDTH, im->graph_col[GRC_GRID]);
2293             gfx_line(im, X1, Y0, X1 + 2, Y0,
2294                      GRIDWIDTH, im->graph_col[GRC_GRID]);
2295             gfx_dashed_line(im, X0 - 1, Y0,
2296                             X1 + 1, Y0,
2297                             GRIDWIDTH,
2298                             im->
2299                             graph_col[GRC_GRID],
2300                             im->grid_dash_on, im->grid_dash_off);
2301         }
2302     }
2303
2304     return 1;
2305 }
2306
2307
2308 void vertical_grid(
2309     image_desc_t *im)
2310 {
2311     int       xlab_sel; /* which sort of label and grid ? */
2312     time_t    ti, tilab, timajor;
2313     long      factor;
2314     char      graph_label[100];
2315     double    X0, Y0, Y1;   /* points for filled graph and more */
2316     struct tm tm;
2317
2318     /* the type of time grid is determined by finding
2319        the number of seconds per pixel in the graph */
2320     if (im->xlab_user.minsec == -1) {
2321         factor = (im->end - im->start) / im->xsize;
2322         xlab_sel = 0;
2323         while (xlab[xlab_sel + 1].minsec !=
2324                -1 && xlab[xlab_sel + 1].minsec <= factor) {
2325             xlab_sel++;
2326         }               /* pick the last one */
2327         while (xlab[xlab_sel - 1].minsec ==
2328                xlab[xlab_sel].minsec
2329                && xlab[xlab_sel].length > (im->end - im->start)) {
2330             xlab_sel--;
2331         }               /* go back to the smallest size */
2332         im->xlab_user.gridtm = xlab[xlab_sel].gridtm;
2333         im->xlab_user.gridst = xlab[xlab_sel].gridst;
2334         im->xlab_user.mgridtm = xlab[xlab_sel].mgridtm;
2335         im->xlab_user.mgridst = xlab[xlab_sel].mgridst;
2336         im->xlab_user.labtm = xlab[xlab_sel].labtm;
2337         im->xlab_user.labst = xlab[xlab_sel].labst;
2338         im->xlab_user.precis = xlab[xlab_sel].precis;
2339         im->xlab_user.stst = xlab[xlab_sel].stst;
2340     }
2341
2342     /* y coords are the same for every line ... */
2343     Y0 = im->yorigin;
2344     Y1 = im->yorigin - im->ysize;
2345     /* paint the minor grid */
2346     if (!(im->extra_flags & NOMINOR)) {
2347         for (ti = find_first_time(im->start,
2348                                   im->
2349                                   xlab_user.
2350                                   gridtm,
2351                                   im->
2352                                   xlab_user.
2353                                   gridst),
2354              timajor =
2355              find_first_time(im->start,
2356                              im->xlab_user.
2357                              mgridtm,
2358                              im->xlab_user.
2359                              mgridst);
2360              ti < im->end;
2361              ti =
2362              find_next_time(ti, im->xlab_user.gridtm, im->xlab_user.gridst)
2363             ) {
2364             /* are we inside the graph ? */
2365             if (ti < im->start || ti > im->end)
2366                 continue;
2367             while (timajor < ti) {
2368                 timajor = find_next_time(timajor,
2369                                          im->
2370                                          xlab_user.
2371                                          mgridtm, im->xlab_user.mgridst);
2372             }
2373             if (ti == timajor)
2374                 continue;   /* skip as falls on major grid line */
2375             X0 = xtr(im, ti);
2376             gfx_line(im, X0, Y1 - 2, X0, Y1,
2377                      GRIDWIDTH, im->graph_col[GRC_GRID]);
2378             gfx_line(im, X0, Y0, X0, Y0 + 2,
2379                      GRIDWIDTH, im->graph_col[GRC_GRID]);
2380             gfx_dashed_line(im, X0, Y0 + 1, X0,
2381                             Y1 - 1, GRIDWIDTH,
2382                             im->
2383                             graph_col[GRC_GRID],
2384                             im->grid_dash_on, im->grid_dash_off);
2385         }
2386     }
2387
2388     /* paint the major grid */
2389     for (ti = find_first_time(im->start,
2390                               im->
2391                               xlab_user.
2392                               mgridtm,
2393                               im->
2394                               xlab_user.
2395                               mgridst);
2396          ti < im->end;
2397          ti = find_next_time(ti, im->xlab_user.mgridtm, im->xlab_user.mgridst)
2398         ) {
2399         /* are we inside the graph ? */
2400         if (ti < im->start || ti > im->end)
2401             continue;
2402         X0 = xtr(im, ti);
2403         gfx_line(im, X0, Y1 - 2, X0, Y1,
2404                  MGRIDWIDTH, im->graph_col[GRC_MGRID]);
2405         gfx_line(im, X0, Y0, X0, Y0 + 3,
2406                  MGRIDWIDTH, im->graph_col[GRC_MGRID]);
2407         gfx_dashed_line(im, X0, Y0 + 3, X0,
2408                         Y1 - 2, MGRIDWIDTH,
2409                         im->
2410                         graph_col
2411                         [GRC_MGRID], im->grid_dash_on, im->grid_dash_off);
2412     }
2413     /* paint the labels below the graph */
2414     for (ti =
2415          find_first_time(im->start -
2416                          im->xlab_user.
2417                          precis / 2,
2418                          im->xlab_user.
2419                          labtm,
2420                          im->xlab_user.
2421                          labst);
2422          ti <=
2423          im->end -
2424          im->xlab_user.precis / 2;
2425          ti = find_next_time(ti, im->xlab_user.labtm, im->xlab_user.labst)
2426         ) {
2427         tilab = ti + im->xlab_user.precis / 2;  /* correct time for the label */
2428         /* are we inside the graph ? */
2429         if (tilab < im->start || tilab > im->end)
2430             continue;
2431 #if HAVE_STRFTIME
2432         localtime_r(&tilab, &tm);
2433         strftime(graph_label, 99, im->xlab_user.stst, &tm);
2434 #else
2435 # error "your libc has no strftime I guess we'll abort the exercise here."
2436 #endif
2437         gfx_text(im,
2438                  xtr(im, tilab),
2439                  Y0 + 3,
2440                  im->graph_col[GRC_FONT],
2441                  im->
2442                  text_prop[TEXT_PROP_AXIS].
2443                  font,
2444                  im->
2445                  text_prop[TEXT_PROP_AXIS].
2446                  size, im->tabwidth, 0.0,
2447                  GFX_H_CENTER, GFX_V_TOP, graph_label);
2448     }
2449
2450 }
2451
2452
2453 void axis_paint(
2454     image_desc_t *im)
2455 {
2456     /* draw x and y axis */
2457     /* gfx_line ( im->canvas, im->xorigin+im->xsize,im->yorigin,
2458        im->xorigin+im->xsize,im->yorigin-im->ysize,
2459        GRIDWIDTH, im->graph_col[GRC_AXIS]);
2460
2461        gfx_line ( im->canvas, im->xorigin,im->yorigin-im->ysize,
2462        im->xorigin+im->xsize,im->yorigin-im->ysize,
2463        GRIDWIDTH, im->graph_col[GRC_AXIS]); */
2464
2465     gfx_line(im, im->xorigin - 4,
2466              im->yorigin,
2467              im->xorigin + im->xsize +
2468              4, im->yorigin, MGRIDWIDTH, im->graph_col[GRC_AXIS]);
2469     gfx_line(im, im->xorigin,
2470              im->yorigin + 4,
2471              im->xorigin,
2472              im->yorigin - im->ysize -
2473              4, MGRIDWIDTH, im->graph_col[GRC_AXIS]);
2474     /* arrow for X and Y axis direction */
2475     gfx_new_area(im, im->xorigin + im->xsize + 2, im->yorigin - 3, im->xorigin + im->xsize + 2, im->yorigin + 3, im->xorigin + im->xsize + 7, im->yorigin,  /* horyzontal */
2476                  im->graph_col[GRC_ARROW]);
2477     gfx_close_path(im);
2478     gfx_new_area(im, im->xorigin - 3, im->yorigin - im->ysize - 2, im->xorigin + 3, im->yorigin - im->ysize - 2, im->xorigin, im->yorigin - im->ysize - 7,  /* vertical */
2479                  im->graph_col[GRC_ARROW]);
2480     gfx_close_path(im);
2481 }
2482
2483 void grid_paint(
2484     image_desc_t *im)
2485 {
2486     long      i;
2487     int       res = 0;
2488     double    X0, Y0;   /* points for filled graph and more */
2489     struct gfx_color_t water_color;
2490
2491     /* draw 3d border */
2492     gfx_new_area(im, 0, im->yimg,
2493                  2, im->yimg - 2, 2, 2, im->graph_col[GRC_SHADEA]);
2494     gfx_add_point(im, im->ximg - 2, 2);
2495     gfx_add_point(im, im->ximg, 0);
2496     gfx_add_point(im, 0, 0);
2497     gfx_close_path(im);
2498     gfx_new_area(im, 2, im->yimg - 2,
2499                  im->ximg - 2,
2500                  im->yimg - 2, im->ximg - 2, 2, im->graph_col[GRC_SHADEB]);
2501     gfx_add_point(im, im->ximg, 0);
2502     gfx_add_point(im, im->ximg, im->yimg);
2503     gfx_add_point(im, 0, im->yimg);
2504     gfx_close_path(im);
2505     if (im->draw_x_grid == 1)
2506         vertical_grid(im);
2507     if (im->draw_y_grid == 1) {
2508         if (im->logarithmic) {
2509             res = horizontal_log_grid(im);
2510         } else {
2511             res = draw_horizontal_grid(im);
2512         }
2513
2514         /* dont draw horizontal grid if there is no min and max val */
2515         if (!res) {
2516             char     *nodata = "No Data found";
2517
2518             gfx_text(im, im->ximg / 2,
2519                      (2 * im->yorigin -
2520                       im->ysize) / 2,
2521                      im->graph_col[GRC_FONT],
2522                      im->
2523                      text_prop[TEXT_PROP_AXIS].
2524                      font,
2525                      im->
2526                      text_prop[TEXT_PROP_AXIS].
2527                      size, im->tabwidth, 0.0,
2528                      GFX_H_CENTER, GFX_V_CENTER, nodata);
2529         }
2530     }
2531
2532     /* yaxis unit description */
2533     gfx_text(im,
2534              10,
2535              (im->yorigin -
2536               im->ysize / 2),
2537              im->graph_col[GRC_FONT],
2538              im->
2539              text_prop[TEXT_PROP_UNIT].
2540              font,
2541              im->
2542              text_prop[TEXT_PROP_UNIT].
2543              size, im->tabwidth,
2544              RRDGRAPH_YLEGEND_ANGLE, GFX_H_CENTER, GFX_V_CENTER, im->ylegend);
2545     /* graph title */
2546     gfx_text(im,
2547              im->ximg / 2, 6,
2548              im->graph_col[GRC_FONT],
2549              im->
2550              text_prop[TEXT_PROP_TITLE].
2551              font,
2552              im->
2553              text_prop[TEXT_PROP_TITLE].
2554              size, im->tabwidth, 0.0, GFX_H_CENTER, GFX_V_TOP, im->title);
2555     /* rrdtool 'logo' */
2556     water_color = im->graph_col[GRC_FONT];
2557     water_color.alpha = 0.3;
2558     gfx_text(im, im->ximg - 4, 5,
2559              water_color,
2560              im->
2561              text_prop[TEXT_PROP_AXIS].
2562              font, 5.5, im->tabwidth,
2563              -90, GFX_H_LEFT, GFX_V_TOP, "RRDTOOL / TOBI OETIKER");
2564     /* graph watermark */
2565     if (im->watermark[0] != '\0') {
2566         gfx_text(im,
2567                  im->ximg / 2, im->yimg - 6,
2568                  water_color,
2569                  im->
2570                  text_prop[TEXT_PROP_AXIS].
2571                  font, 5.5, im->tabwidth, 0,
2572                  GFX_H_CENTER, GFX_V_BOTTOM, im->watermark);
2573     }
2574
2575     /* graph labels */
2576     if (!(im->extra_flags & NOLEGEND) & !(im->extra_flags & ONLY_GRAPH)) {
2577         for (i = 0; i < im->gdes_c; i++) {
2578             if (im->gdes[i].legend[0] == '\0')
2579                 continue;
2580             /* im->gdes[i].leg_y is the bottom of the legend */
2581             X0 = im->gdes[i].leg_x;
2582             Y0 = im->gdes[i].leg_y;
2583             gfx_text(im, X0, Y0,
2584                      im->graph_col[GRC_FONT],
2585                      im->
2586                      text_prop
2587                      [TEXT_PROP_LEGEND].font,
2588                      im->
2589                      text_prop
2590                      [TEXT_PROP_LEGEND].size,
2591                      im->tabwidth, 0.0,
2592                      GFX_H_LEFT, GFX_V_BOTTOM, im->gdes[i].legend);
2593             /* The legend for GRAPH items starts with "M " to have
2594                enough space for the box */
2595             if (im->gdes[i].gf != GF_PRINT &&
2596                 im->gdes[i].gf != GF_GPRINT && im->gdes[i].gf != GF_COMMENT) {
2597                 double    boxH, boxV;
2598                 double    X1, Y1;
2599
2600                 boxH = gfx_get_text_width(im, 0,
2601                                           im->
2602                                           text_prop
2603                                           [TEXT_PROP_LEGEND].
2604                                           font,
2605                                           im->
2606                                           text_prop
2607                                           [TEXT_PROP_LEGEND].
2608                                           size, im->tabwidth, "o") * 1.2;
2609                 boxV = boxH;
2610                 /* shift the box up a bit */
2611                 Y0 -= boxV * 0.4;
2612                 /* make sure transparent colors show up the same way as in the graph */
2613                 gfx_new_area(im,
2614                              X0, Y0 - boxV,
2615                              X0, Y0, X0 + boxH, Y0, im->graph_col[GRC_BACK]);
2616                 gfx_add_point(im, X0 + boxH, Y0 - boxV);
2617                 gfx_close_path(im);
2618                 gfx_new_area(im, X0, Y0 - boxV, X0,
2619                              Y0, X0 + boxH, Y0, im->gdes[i].col);
2620                 gfx_add_point(im, X0 + boxH, Y0 - boxV);
2621                 gfx_close_path(im);
2622                 cairo_save(im->cr);
2623                 cairo_new_path(im->cr);
2624                 cairo_set_line_width(im->cr, 1.0);
2625                 X1 = X0 + boxH;
2626                 Y1 = Y0 - boxV;
2627                 gfx_line_fit(im, &X0, &Y0);
2628                 gfx_line_fit(im, &X1, &Y1);
2629                 cairo_move_to(im->cr, X0, Y0);
2630                 cairo_line_to(im->cr, X1, Y0);
2631                 cairo_line_to(im->cr, X1, Y1);
2632                 cairo_line_to(im->cr, X0, Y1);
2633                 cairo_close_path(im->cr);
2634                 cairo_set_source_rgba(im->cr,
2635                                       im->
2636                                       graph_col
2637                                       [GRC_FRAME].
2638                                       red,
2639                                       im->
2640                                       graph_col
2641                                       [GRC_FRAME].
2642                                       green,
2643                                       im->
2644                                       graph_col
2645                                       [GRC_FRAME].
2646                                       blue, im->graph_col[GRC_FRAME].alpha);
2647                 if (im->gdes[i].dash) {
2648                     /* make box borders in legend dashed if the graph is dashed */
2649                     double    dashes[] = {
2650                         3.0
2651                     };
2652                     cairo_set_dash(im->cr, dashes, 1, 0.0);
2653                 }
2654                 cairo_stroke(im->cr);
2655                 cairo_restore(im->cr);
2656             }
2657         }
2658     }
2659 }
2660
2661
2662 /*****************************************************
2663  * lazy check make sure we rely need to create this graph
2664  *****************************************************/
2665
2666 int lazy_check(
2667     image_desc_t *im)
2668 {
2669     FILE     *fd = NULL;
2670     int       size = 1;
2671     struct stat imgstat;
2672
2673     if (im->lazy == 0)
2674         return 0;       /* no lazy option */
2675     if (strlen(im->graphfile) == 0)
2676         return 0;       /* inmemory option */
2677     if (stat(im->graphfile, &imgstat) != 0)
2678         return 0;       /* can't stat */
2679     /* one pixel in the existing graph is more then what we would
2680        change here ... */
2681     if (time(NULL) - imgstat.st_mtime > (im->end - im->start) / im->xsize)
2682         return 0;
2683     if ((fd = fopen(im->graphfile, "rb")) == NULL)
2684         return 0;       /* the file does not exist */
2685     switch (im->imgformat) {
2686     case IF_PNG:
2687         size = PngSize(fd, &(im->ximg), &(im->yimg));
2688         break;
2689     default:
2690         size = 1;
2691     }
2692     fclose(fd);
2693     return size;
2694 }
2695
2696
2697 int graph_size_location(
2698     image_desc_t
2699     *im,
2700     int elements)
2701 {
2702     /* The actual size of the image to draw is determined from
2703      ** several sources.  The size given on the command line is
2704      ** the graph area but we need more as we have to draw labels
2705      ** and other things outside the graph area
2706      */
2707
2708     int       Xvertical = 0, Ytitle =
2709         0, Xylabel = 0, Xmain = 0, Ymain =
2710         0, Yxlabel = 0, Xspacing = 15, Yspacing = 15, Ywatermark = 4;
2711
2712     if (im->extra_flags & ONLY_GRAPH) {
2713         im->xorigin = 0;
2714         im->ximg = im->xsize;
2715         im->yimg = im->ysize;
2716         im->yorigin = im->ysize;
2717         ytr(im, DNAN);
2718         return 0;
2719     }
2720
2721     /** +---+--------------------------------------------+
2722      ** | y |...............graph title..................|
2723      ** |   +---+-------------------------------+--------+
2724      ** | a | y |                               |        |
2725      ** | x |   |                               |        |
2726      ** | i | a |                               |  pie   |
2727      ** | s | x |       main graph area         | chart  |
2728      ** |   | i |                               |  area  |
2729      ** | t | s |                               |        |
2730      ** | i |   |                               |        |
2731      ** | t | l |                               |        |
2732      ** | l | b +-------------------------------+--------+
2733      ** | e | l |       x axis labels           |        |
2734      ** +---+---+-------------------------------+--------+
2735      ** |....................legends.....................|
2736      ** +------------------------------------------------+
2737      ** |                   watermark                    |
2738      ** +------------------------------------------------+
2739      */
2740
2741     if (im->ylegend[0] != '\0') {
2742         Xvertical = im->text_prop[TEXT_PROP_UNIT].size * 2;
2743     }
2744
2745     if (im->title[0] != '\0') {
2746         /* The title is placed "inbetween" two text lines so it
2747          ** automatically has some vertical spacing.  The horizontal
2748          ** spacing is added here, on each side.
2749          */
2750         /* if necessary, reduce the font size of the title until it fits the image width */
2751         Ytitle = im->text_prop[TEXT_PROP_TITLE].size * 2.6 + 10;
2752     }
2753
2754     if (elements) {
2755         if (im->draw_x_grid) {
2756             Yxlabel = im->text_prop[TEXT_PROP_AXIS].size * 2.5;
2757         }
2758         if (im->draw_y_grid || im->forceleftspace) {
2759             Xylabel =
2760                 gfx_get_text_width(im, 0,
2761                                    im->
2762                                    text_prop
2763                                    [TEXT_PROP_AXIS].
2764                                    font,
2765                                    im->
2766                                    text_prop
2767                                    [TEXT_PROP_AXIS].
2768                                    size, im->tabwidth, "0") * im->unitslength;
2769         }
2770     }
2771
2772     if (im->extra_flags & FULL_SIZE_MODE) {
2773         /* The actual size of the image to draw has been determined by the user.
2774          ** The graph area is the space remaining after accounting for the legend,
2775          ** the watermark, the pie chart, the axis labels, and the title.
2776          */
2777         im->xorigin = 0;
2778         im->ximg = im->xsize;
2779         im->yimg = im->ysize;
2780         im->yorigin = im->ysize;
2781         Xmain = im->ximg;
2782         Ymain = im->yimg;
2783         im->yorigin += Ytitle;
2784         /* Now calculate the total size.  Insert some spacing where
2785            desired.  im->xorigin and im->yorigin need to correspond
2786            with the lower left corner of the main graph area or, if
2787            this one is not set, the imaginary box surrounding the
2788            pie chart area. */
2789         /* Initial size calculation for the main graph area */
2790         Xmain = im->ximg - (Xylabel + 2 * Xspacing);
2791         if (Xmain)
2792             Xmain -= Xspacing;  /* put space between main graph area and right edge */
2793         im->xorigin = Xspacing + Xylabel;
2794         /* the length of the title should not influence with width of the graph
2795            if (Xtitle > im->ximg) im->ximg = Xtitle; */
2796         if (Xvertical) {    /* unit description */
2797             Xmain -= Xvertical;
2798             im->xorigin += Xvertical;
2799         }
2800         im->xsize = Xmain;
2801         xtr(im, 0);
2802         /* The vertical size of the image is known in advance.  The main graph area
2803          ** (Ymain) and im->yorigin must be set according to the space requirements
2804          ** of the legend and the axis labels.
2805          */
2806         if (im->extra_flags & NOLEGEND) {
2807             /* set dimensions correctly if using full size mode with no legend */
2808             im->yorigin =
2809                 im->yimg -
2810                 im->text_prop[TEXT_PROP_AXIS].size * 2.5 - Yspacing;
2811             Ymain = im->yorigin;
2812         } else {
2813             /* Determine where to place the legends onto the image.
2814              ** Set Ymain and adjust im->yorigin to match the space requirements.
2815              */
2816             if (leg_place(im, &Ymain) == -1)
2817                 return -1;
2818         }
2819
2820
2821         /* remove title space *or* some padding above the graph from the main graph area */
2822         if (Ytitle) {
2823             Ymain -= Ytitle;
2824         } else {
2825             Ymain -= 1.5 * Yspacing;
2826         }
2827
2828         /* watermark doesn't seem to effect the vertical size of the main graph area, oh well! */
2829         if (im->watermark[0] != '\0') {
2830             Ymain -= Ywatermark;
2831         }
2832
2833         im->ysize = Ymain;
2834     } else {            /* dimension options -width and -height refer to the dimensions of the main graph area */
2835
2836         /* The actual size of the image to draw is determined from
2837          ** several sources.  The size given on the command line is
2838          ** the graph area but we need more as we have to draw labels
2839          ** and other things outside the graph area.
2840          */
2841
2842         if (im->ylegend[0] != '\0') {
2843             Xvertical = im->text_prop[TEXT_PROP_UNIT].size * 2;
2844         }
2845
2846
2847         if (im->title[0] != '\0') {
2848             /* The title is placed "inbetween" two text lines so it
2849              ** automatically has some vertical spacing.  The horizontal
2850              ** spacing is added here, on each side.
2851              */
2852             /* don't care for the with of the title
2853                Xtitle = gfx_get_text_width(im->canvas, 0,
2854                im->text_prop[TEXT_PROP_TITLE].font,
2855                im->text_prop[TEXT_PROP_TITLE].size,
2856                im->tabwidth,
2857                im->title, 0) + 2*Xspacing; */
2858             Ytitle = im->text_prop[TEXT_PROP_TITLE].size * 2.6 + 10;
2859         }
2860
2861         if (elements) {
2862             Xmain = im->xsize;
2863             Ymain = im->ysize;
2864         }
2865         /* Now calculate the total size.  Insert some spacing where
2866            desired.  im->xorigin and im->yorigin need to correspond
2867            with the lower left corner of the main graph area or, if
2868            this one is not set, the imaginary box surrounding the
2869            pie chart area. */
2870
2871         /* The legend width cannot yet be determined, as a result we
2872          ** have problems adjusting the image to it.  For now, we just
2873          ** forget about it at all; the legend will have to fit in the
2874          ** size already allocated.
2875          */
2876         im->ximg = Xylabel + Xmain + 2 * Xspacing;
2877         if (Xmain)
2878             im->ximg += Xspacing;
2879         im->xorigin = Xspacing + Xylabel;
2880         /* the length of the title should not influence with width of the graph
2881            if (Xtitle > im->ximg) im->ximg = Xtitle; */
2882         if (Xvertical) {    /* unit description */
2883             im->ximg += Xvertical;
2884             im->xorigin += Xvertical;
2885         }
2886         xtr(im, 0);
2887         /* The vertical size is interesting... we need to compare
2888          ** the sum of {Ytitle, Ymain, Yxlabel, Ylegend, Ywatermark} with 
2889          ** Yvertical however we need to know {Ytitle+Ymain+Yxlabel}
2890          ** in order to start even thinking about Ylegend or Ywatermark.
2891          **
2892          ** Do it in three portions: First calculate the inner part,
2893          ** then do the legend, then adjust the total height of the img,
2894          ** adding space for a watermark if one exists;
2895          */
2896         /* reserve space for main and/or pie */
2897         im->yimg = Ymain + Yxlabel;
2898         im->yorigin = im->yimg - Yxlabel;
2899         /* reserve space for the title *or* some padding above the graph */
2900         if (Ytitle) {
2901             im->yimg += Ytitle;
2902             im->yorigin += Ytitle;
2903         } else {
2904             im->yimg += 1.5 * Yspacing;
2905             im->yorigin += 1.5 * Yspacing;
2906         }
2907         /* reserve space for padding below the graph */
2908         im->yimg += Yspacing;
2909         /* Determine where to place the legends onto the image.
2910          ** Adjust im->yimg to match the space requirements.
2911          */
2912         if (leg_place(im, 0) == -1)
2913             return -1;
2914         if (im->watermark[0] != '\0') {
2915             im->yimg += Ywatermark;
2916         }
2917     }
2918
2919     ytr(im, DNAN);
2920     return 0;
2921 }
2922
2923 static cairo_status_t cairo_output(
2924     void *closure,
2925     const unsigned char
2926     *data,
2927     unsigned int length)
2928 {
2929     image_desc_t *im = closure;
2930
2931     im->rendered_image =
2932         realloc(im->rendered_image, im->rendered_image_size + length);
2933     if (im->rendered_image == NULL)
2934         return CAIRO_STATUS_WRITE_ERROR;
2935     memcpy(im->rendered_image + im->rendered_image_size, data, length);
2936     im->rendered_image_size += length;
2937     return CAIRO_STATUS_SUCCESS;
2938 }
2939
2940 /* draw that picture thing ... */
2941 int graph_paint(
2942     image_desc_t *im)
2943 {
2944     int       i, ii;
2945     int       lazy = lazy_check(im);
2946     double    areazero = 0.0;
2947     graph_desc_t *lastgdes = NULL;
2948     infoval   info;
2949     PangoFontMap *font_map = pango_cairo_font_map_get_default();
2950
2951     /* if we are lazy and there is nothing to PRINT ... quit now */
2952     if (lazy && im->prt_c == 0)
2953         return 0;
2954     /* pull the data from the rrd files ... */
2955     if (data_fetch(im) == -1)
2956         return -1;
2957     /* evaluate VDEF and CDEF operations ... */
2958     if (data_calc(im) == -1)
2959         return -1;
2960     /* calculate and PRINT and GPRINT definitions. We have to do it at
2961      * this point because it will affect the length of the legends
2962      * if there are no graph elements we stop here ... 
2963      * if we are lazy, try to quit ... 
2964      */
2965     i = print_calc(im);
2966     if (i < 0)
2967         return -1;
2968     if ((i == 0) || lazy)
2969         return 0;
2970 /**************************************************************
2971  *** Calculating sizes and locations became a bit confusing ***
2972  *** so I moved this into a separate function.              ***
2973  **************************************************************/
2974     if (graph_size_location(im, i) == -1)
2975         return -1;
2976
2977     info.u_cnt = im->xorigin;
2978     grinfo_push(im, sprintf_alloc("graph_left"), RD_I_CNT, info);
2979     info.u_cnt = im->yorigin - im->ysize;
2980     grinfo_push(im, sprintf_alloc("graph_top"), RD_I_CNT, info);
2981     info.u_cnt = im->xsize;
2982     grinfo_push(im, sprintf_alloc("graph_width"), RD_I_CNT, info);
2983     info.u_cnt = im->ysize;
2984     grinfo_push(im, sprintf_alloc("graph_height"), RD_I_CNT, info);
2985     info.u_cnt = im->ximg;
2986     grinfo_push(im, sprintf_alloc("image_width"), RD_I_CNT, info);
2987     info.u_cnt = im->yimg;
2988     grinfo_push(im, sprintf_alloc("image_height"), RD_I_CNT, info);
2989
2990     /* get actual drawing data and find min and max values */
2991     if (data_proc(im) == -1)
2992         return -1;
2993     if (!im->logarithmic) {
2994         si_unit(im);
2995     }
2996
2997     /* identify si magnitude Kilo, Mega Giga ? */
2998     if (!im->rigid && !im->logarithmic)
2999         expand_range(im);   /* make sure the upper and lower limit are
3000                                sensible values */
3001
3002     info.u_val = im->minval;
3003     grinfo_push(im, sprintf_alloc("value_min"), RD_I_VAL, info);
3004     info.u_val = im->maxval;
3005     grinfo_push(im, sprintf_alloc("value_max"), RD_I_VAL, info);
3006
3007     if (!calc_horizontal_grid(im))
3008         return -1;
3009     /* reset precalc */
3010     ytr(im, DNAN);
3011 /*   if (im->gridfit)
3012      apply_gridfit(im); */
3013     /* the actual graph is created by going through the individual
3014        graph elements and then drawing them */
3015     cairo_surface_destroy(im->surface);
3016     switch (im->imgformat) {
3017     case IF_PNG:
3018         im->surface =
3019             cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
3020                                        im->ximg * im->zoom,
3021                                        im->yimg * im->zoom);
3022         break;
3023     case IF_PDF:
3024         im->gridfit = 0;
3025         im->surface = strlen(im->graphfile)
3026             ? cairo_pdf_surface_create(im->graphfile, im->ximg * im->zoom,
3027                                        im->yimg * im->zoom)
3028             : cairo_pdf_surface_create_for_stream
3029             (&cairo_output, im, im->ximg * im->zoom, im->yimg * im->zoom);
3030         break;
3031     case IF_EPS:
3032         im->gridfit = 0;
3033         im->surface = strlen(im->graphfile)
3034             ?
3035             cairo_ps_surface_create(im->graphfile, im->ximg * im->zoom,
3036                                     im->yimg * im->zoom)
3037             : cairo_ps_surface_create_for_stream
3038             (&cairo_output, im, im->ximg * im->zoom, im->yimg * im->zoom);
3039         break;
3040     case IF_SVG:
3041         im->gridfit = 0;
3042         im->surface = strlen(im->graphfile)
3043             ?
3044             cairo_svg_surface_create(im->
3045                                      graphfile,
3046                                      im->ximg * im->zoom, im->yimg * im->zoom)
3047             : cairo_svg_surface_create_for_stream
3048             (&cairo_output, im, im->ximg * im->zoom, im->yimg * im->zoom);
3049         cairo_svg_surface_restrict_to_version
3050             (im->surface, CAIRO_SVG_VERSION_1_1);
3051         break;
3052     };
3053     im->cr = cairo_create(im->surface);
3054     cairo_set_antialias(im->cr, im->graph_antialias);
3055     cairo_scale(im->cr, im->zoom, im->zoom);
3056     pango_cairo_font_map_set_resolution(PANGO_CAIRO_FONT_MAP(font_map), 100);
3057     gfx_new_area(im, 0, 0, 0, im->yimg,
3058                  im->ximg, im->yimg, im->graph_col[GRC_BACK]);
3059     gfx_add_point(im, im->ximg, 0);
3060     gfx_close_path(im);
3061     gfx_new_area(im, im->xorigin,
3062                  im->yorigin,
3063                  im->xorigin +
3064                  im->xsize, im->yorigin,
3065                  im->xorigin +
3066                  im->xsize,
3067                  im->yorigin - im->ysize, im->graph_col[GRC_CANVAS]);
3068     gfx_add_point(im, im->xorigin, im->yorigin - im->ysize);
3069     gfx_close_path(im);
3070     if (im->minval > 0.0)
3071         areazero = im->minval;
3072     if (im->maxval < 0.0)
3073         areazero = im->maxval;
3074     for (i = 0; i < im->gdes_c; i++) {
3075         switch (im->gdes[i].gf) {
3076         case GF_CDEF:
3077         case GF_VDEF:
3078         case GF_DEF:
3079         case GF_PRINT:
3080         case GF_GPRINT:
3081         case GF_COMMENT:
3082         case GF_TEXTALIGN:
3083         case GF_HRULE:
3084         case GF_VRULE:
3085         case GF_XPORT:
3086         case GF_SHIFT:
3087             break;
3088         case GF_TICK:
3089             for (ii = 0; ii < im->xsize; ii++) {
3090                 if (!isnan(im->gdes[i].p_data[ii])
3091                     && im->gdes[i].p_data[ii] != 0.0) {
3092                     if (im->gdes[i].yrule > 0) {
3093                         gfx_line(im,
3094                                  im->xorigin + ii,
3095                                  im->yorigin,
3096                                  im->xorigin + ii,
3097                                  im->yorigin -
3098                                  im->gdes[i].yrule *
3099                                  im->ysize, 1.0, im->gdes[i].col);
3100                     } else if (im->gdes[i].yrule < 0) {
3101                         gfx_line(im,
3102                                  im->xorigin + ii,
3103                                  im->yorigin - im->ysize,
3104                                  im->xorigin + ii,
3105                                  im->yorigin - (1 -
3106                                                 im->gdes[i].
3107                                                 yrule) *
3108                                  im->ysize, 1.0, im->gdes[i].col);
3109                     }
3110                 }
3111             }
3112             break;
3113         case GF_LINE:
3114         case GF_AREA:
3115             /* fix data points at oo and -oo */
3116             for (ii = 0; ii < im->xsize; ii++) {
3117                 if (isinf(im->gdes[i].p_data[ii])) {
3118                     if (im->gdes[i].p_data[ii] > 0) {
3119                         im->gdes[i].p_data[ii] = im->maxval;
3120                     } else {
3121                         im->gdes[i].p_data[ii] = im->minval;
3122                     }
3123
3124                 }
3125             }           /* for */
3126
3127             /* *******************************************************
3128                a           ___. (a,t) 
3129                |   |    ___
3130                ____|   |   |   |
3131                |       |___|
3132                -------|--t-1--t--------------------------------      
3133
3134                if we know the value at time t was a then 
3135                we draw a square from t-1 to t with the value a.
3136
3137                ********************************************************* */
3138             if (im->gdes[i].col.alpha != 0.0) {
3139                 /* GF_LINE and friend */
3140                 if (im->gdes[i].gf == GF_LINE) {
3141                     double    last_y = 0.0;
3142                     int       draw_on = 0;
3143
3144                     cairo_save(im->cr);
3145                     cairo_new_path(im->cr);
3146                     cairo_set_line_width(im->cr, im->gdes[i].linewidth);
3147                     if (im->gdes[i].dash) {
3148                         cairo_set_dash(im->cr,
3149                                        im->gdes[i].p_dashes,
3150                                        im->gdes[i].ndash, im->gdes[i].offset);
3151                     }
3152
3153                     for (ii = 1; ii < im->xsize; ii++) {
3154                         if (isnan(im->gdes[i].p_data[ii])
3155                             || (im->slopemode == 1
3156                                 && isnan(im->gdes[i].p_data[ii - 1]))) {
3157                             draw_on = 0;
3158                             continue;
3159                         }
3160                         if (draw_on == 0) {
3161                             last_y = ytr(im, im->gdes[i].p_data[ii]);
3162                             if (im->slopemode == 0) {
3163                                 double    x = ii - 1 + im->xorigin;
3164                                 double    y = last_y;
3165
3166                                 gfx_line_fit(im, &x, &y);
3167                                 cairo_move_to(im->cr, x, y);
3168                                 x = ii + im->xorigin;
3169                                 y = last_y;
3170                                 gfx_line_fit(im, &x, &y);
3171                                 cairo_line_to(im->cr, x, y);
3172                             } else {
3173                                 double    x = ii - 1 + im->xorigin;
3174                                 double    y =
3175                                     ytr(im, im->gdes[i].p_data[ii - 1]);
3176                                 gfx_line_fit(im, &x, &y);
3177                                 cairo_move_to(im->cr, x, y);
3178                                 x = ii + im->xorigin;
3179                                 y = last_y;
3180                                 gfx_line_fit(im, &x, &y);
3181                                 cairo_line_to(im->cr, x, y);
3182                             }
3183                             draw_on = 1;
3184                         } else {
3185                             double    x1 = ii + im->xorigin;
3186                             double    y1 = ytr(im, im->gdes[i].p_data[ii]);
3187
3188                             if (im->slopemode == 0
3189                                 && !AlmostEqual2sComplement(y1, last_y, 4)) {
3190                                 double    x = ii - 1 + im->xorigin;
3191                                 double    y = y1;
3192
3193                                 gfx_line_fit(im, &x, &y);
3194                                 cairo_line_to(im->cr, x, y);
3195                             };
3196                             last_y = y1;
3197                             gfx_line_fit(im, &x1, &y1);
3198                             cairo_line_to(im->cr, x1, y1);
3199                         };
3200                     }
3201                     cairo_set_source_rgba(im->cr,
3202                                           im->gdes[i].
3203                                           col.red,
3204                                           im->gdes[i].
3205                                           col.green,
3206                                           im->gdes[i].
3207                                           col.blue, im->gdes[i].col.alpha);
3208                     cairo_set_line_cap(im->cr, CAIRO_LINE_CAP_ROUND);
3209                     cairo_set_line_join(im->cr, CAIRO_LINE_JOIN_ROUND);
3210                     cairo_stroke(im->cr);
3211                     cairo_restore(im->cr);
3212                 } else {
3213                     int       idxI = -1;
3214                     double   *foreY =
3215                         (double *) malloc(sizeof(double) * im->xsize * 2);
3216                     double   *foreX =
3217                         (double *) malloc(sizeof(double) * im->xsize * 2);
3218                     double   *backY =
3219                         (double *) malloc(sizeof(double) * im->xsize * 2);
3220                     double   *backX =
3221                         (double *) malloc(sizeof(double) * im->xsize * 2);
3222                     int       drawem = 0;
3223
3224                     for (ii = 0; ii <= im->xsize; ii++) {
3225                         double    ybase, ytop;
3226
3227                         if (idxI > 0 && (drawem != 0 || ii == im->xsize)) {
3228                             int       cntI = 1;
3229                             int       lastI = 0;
3230
3231                             while (cntI < idxI
3232                                    &&
3233                                    AlmostEqual2sComplement(foreY
3234                                                            [lastI],
3235                                                            foreY[cntI], 4)
3236                                    &&
3237                                    AlmostEqual2sComplement(foreY
3238                                                            [lastI],
3239                                                            foreY
3240                                                            [cntI + 1], 4)) {
3241                                 cntI++;
3242                             }
3243                             gfx_new_area(im,
3244                                          backX[0], backY[0],
3245                                          foreX[0], foreY[0],
3246                                          foreX[cntI],
3247                                          foreY[cntI], im->gdes[i].col);
3248                             while (cntI < idxI) {
3249                                 lastI = cntI;
3250                                 cntI++;
3251                                 while (cntI < idxI
3252                                        &&
3253                                        AlmostEqual2sComplement(foreY
3254                                                                [lastI],
3255                                                                foreY[cntI], 4)
3256                                        &&
3257                                        AlmostEqual2sComplement(foreY
3258                                                                [lastI],
3259                                                                foreY
3260                                                                [cntI
3261                                                                 + 1], 4)) {
3262                                     cntI++;
3263                                 }
3264                                 gfx_add_point(im, foreX[cntI], foreY[cntI]);
3265                             }
3266                             gfx_add_point(im, backX[idxI], backY[idxI]);
3267                             while (idxI > 1) {
3268                                 lastI = idxI;
3269                                 idxI--;
3270                                 while (idxI > 1
3271                                        &&
3272                                        AlmostEqual2sComplement(backY
3273                                                                [lastI],
3274                                                                backY[idxI], 4)
3275                                        &&
3276                                        AlmostEqual2sComplement(backY
3277                                                                [lastI],
3278                                                                backY
3279                                                                [idxI
3280                                                                 - 1], 4)) {
3281                                     idxI--;
3282                                 }
3283                                 gfx_add_point(im, backX[idxI], backY[idxI]);
3284                             }
3285                             idxI = -1;
3286                             drawem = 0;
3287                             gfx_close_path(im);
3288                         }
3289                         if (drawem != 0) {
3290                             drawem = 0;
3291                             idxI = -1;
3292                         }
3293                         if (ii == im->xsize)
3294                             break;
3295                         if (im->slopemode == 0 && ii == 0) {
3296                             continue;
3297                         }
3298                         if (isnan(im->gdes[i].p_data[ii])) {
3299                             drawem = 1;
3300                             continue;
3301                         }
3302                         ytop = ytr(im, im->gdes[i].p_data[ii]);
3303                         if (lastgdes && im->gdes[i].stack) {
3304                             ybase = ytr(im, lastgdes->p_data[ii]);
3305                         } else {
3306                             ybase = ytr(im, areazero);
3307                         }
3308                         if (ybase == ytop) {
3309                             drawem = 1;
3310                             continue;
3311                         }
3312
3313                         if (ybase > ytop) {
3314                             double    extra = ytop;
3315
3316                             ytop = ybase;
3317                             ybase = extra;
3318                         }
3319                         if (im->slopemode == 0) {
3320                             backY[++idxI] = ybase - 0.2;
3321                             backX[idxI] = ii + im->xorigin - 1;
3322                             foreY[idxI] = ytop + 0.2;
3323                             foreX[idxI] = ii + im->xorigin - 1;
3324                         }
3325                         backY[++idxI] = ybase - 0.2;
3326                         backX[idxI] = ii + im->xorigin;
3327                         foreY[idxI] = ytop + 0.2;
3328                         foreX[idxI] = ii + im->xorigin;
3329                     }
3330                     /* close up any remaining area */
3331                     free(foreY);
3332                     free(foreX);
3333                     free(backY);
3334                     free(backX);
3335                 }       /* else GF_LINE */
3336             }
3337             /* if color != 0x0 */
3338             /* make sure we do not run into trouble when stacking on NaN */
3339             for (ii = 0; ii < im->xsize; ii++) {
3340                 if (isnan(im->gdes[i].p_data[ii])) {
3341                     if (lastgdes && (im->gdes[i].stack)) {
3342                         im->gdes[i].p_data[ii] = lastgdes->p_data[ii];
3343                     } else {
3344                         im->gdes[i].p_data[ii] = areazero;
3345                     }
3346                 }
3347             }
3348             lastgdes = &(im->gdes[i]);
3349             break;
3350         case GF_STACK:
3351             rrd_set_error
3352                 ("STACK should already be turned into LINE or AREA here");
3353             return -1;
3354             break;
3355         }               /* switch */
3356     }
3357
3358     /* grid_paint also does the text */
3359     if (!(im->extra_flags & ONLY_GRAPH))
3360         grid_paint(im);
3361     if (!(im->extra_flags & ONLY_GRAPH))
3362         axis_paint(im);
3363     /* the RULES are the last thing to paint ... */
3364     for (i = 0; i < im->gdes_c; i++) {
3365
3366         switch (im->gdes[i].gf) {
3367         case GF_HRULE:
3368             if (im->gdes[i].yrule >= im->minval
3369                 && im->gdes[i].yrule <= im->maxval) {
3370                 cairo_save(im->cr);
3371                 if (im->gdes[i].dash) {
3372                     cairo_set_dash(im->cr,
3373                                    im->gdes[i].p_dashes,
3374                                    im->gdes[i].ndash, im->gdes[i].offset);
3375                 }
3376                 gfx_line(im, im->xorigin,
3377                          ytr(im, im->gdes[i].yrule),
3378                          im->xorigin + im->xsize,
3379                          ytr(im, im->gdes[i].yrule), 1.0, im->gdes[i].col);
3380                 cairo_stroke(im->cr);
3381                 cairo_restore(im->cr);
3382             }
3383             break;
3384         case GF_VRULE:
3385             if (im->gdes[i].xrule >= im->start
3386                 && im->gdes[i].xrule <= im->end) {
3387                 cairo_save(im->cr);
3388                 if (im->gdes[i].dash) {
3389                     cairo_set_dash(im->cr,
3390                                    im->gdes[i].p_dashes,
3391                                    im->gdes[i].ndash, im->gdes[i].offset);
3392                 }
3393                 gfx_line(im,
3394                          xtr(im, im->gdes[i].xrule),
3395                          im->yorigin, xtr(im,
3396                                           im->
3397                                           gdes[i].
3398                                           xrule),
3399                          im->yorigin - im->ysize, 1.0, im->gdes[i].col);
3400                 cairo_stroke(im->cr);
3401                 cairo_restore(im->cr);
3402             }
3403             break;
3404         default:
3405             break;
3406         }
3407     }
3408
3409
3410     switch (im->imgformat) {
3411     case IF_PNG:
3412     {
3413         cairo_status_t status;
3414
3415         status = strlen(im->graphfile) ?
3416             cairo_surface_write_to_png(im->surface, im->graphfile)
3417             : cairo_surface_write_to_png_stream(im->surface, &cairo_output,
3418                                                 im);
3419
3420         if (status != CAIRO_STATUS_SUCCESS) {
3421             rrd_set_error("Could not save png to '%s'", im->graphfile);
3422             return 1;
3423         }
3424         break;
3425     }
3426     default:
3427         if (strlen(im->graphfile)) {
3428             cairo_show_page(im->cr);
3429         } else {
3430             cairo_surface_finish(im->surface);
3431         }
3432         break;
3433     }
3434
3435     return 0;
3436 }
3437
3438
3439 /*****************************************************
3440  * graph stuff 
3441  *****************************************************/
3442
3443 int gdes_alloc(
3444     image_desc_t *im)
3445 {
3446
3447     im->gdes_c++;
3448     if ((im->gdes = (graph_desc_t *)
3449          rrd_realloc(im->gdes, (im->gdes_c)
3450                      * sizeof(graph_desc_t))) == NULL) {
3451         rrd_set_error("realloc graph_descs");
3452         return -1;
3453     }
3454
3455
3456     im->gdes[im->gdes_c - 1].step = im->step;
3457     im->gdes[im->gdes_c - 1].step_orig = im->step;
3458     im->gdes[im->gdes_c - 1].stack = 0;
3459     im->gdes[im->gdes_c - 1].linewidth = 0;
3460     im->gdes[im->gdes_c - 1].debug = 0;
3461     im->gdes[im->gdes_c - 1].start = im->start;
3462     im->gdes[im->gdes_c - 1].start_orig = im->start;
3463     im->gdes[im->gdes_c - 1].end = im->end;
3464     im->gdes[im->gdes_c - 1].end_orig = im->end;
3465     im->gdes[im->gdes_c - 1].vname[0] = '\0';
3466     im->gdes[im->gdes_c - 1].data = NULL;
3467     im->gdes[im->gdes_c - 1].ds_namv = NULL;
3468     im->gdes[im->gdes_c - 1].data_first = 0;
3469     im->gdes[im->gdes_c - 1].p_data = NULL;
3470     im->gdes[im->gdes_c - 1].rpnp = NULL;
3471     im->gdes[im->gdes_c - 1].p_dashes = NULL;
3472     im->gdes[im->gdes_c - 1].shift = 0.0;
3473     im->gdes[im->gdes_c - 1].dash = 0;
3474     im->gdes[im->gdes_c - 1].ndash = 0;
3475     im->gdes[im->gdes_c - 1].offset = 0;
3476     im->gdes[im->gdes_c - 1].col.red = 0.0;
3477     im->gdes[im->gdes_c - 1].col.green = 0.0;
3478     im->gdes[im->gdes_c - 1].col.blue = 0.0;
3479     im->gdes[im->gdes_c - 1].col.alpha = 0.0;
3480     im->gdes[im->gdes_c - 1].legend[0] = '\0';
3481     im->gdes[im->gdes_c - 1].format[0] = '\0';
3482     im->gdes[im->gdes_c - 1].strftm = 0;
3483     im->gdes[im->gdes_c - 1].rrd[0] = '\0';
3484     im->gdes[im->gdes_c - 1].ds = -1;
3485     im->gdes[im->gdes_c - 1].cf_reduce = CF_AVERAGE;
3486     im->gdes[im->gdes_c - 1].cf = CF_AVERAGE;
3487     im->gdes[im->gdes_c - 1].yrule = DNAN;
3488     im->gdes[im->gdes_c - 1].xrule = 0;
3489     return 0;
3490 }
3491
3492 /* copies input untill the first unescaped colon is found
3493    or until input ends. backslashes have to be escaped as well */
3494 int scan_for_col(
3495     const char *const input,
3496     int len,
3497     char *const output)
3498 {
3499     int       inp, outp = 0;
3500
3501     for (inp = 0; inp < len && input[inp] != ':' && input[inp] != '\0'; inp++) {
3502         if (input[inp] == '\\'
3503             && input[inp + 1] != '\0'
3504             && (input[inp + 1] == '\\' || input[inp + 1] == ':')) {
3505             output[outp++] = input[++inp];
3506         } else {
3507             output[outp++] = input[inp];
3508         }
3509     }
3510     output[outp] = '\0';
3511     return inp;
3512 }
3513
3514 /* Now just a wrapper around rrd_graph_v */
3515 int rrd_graph(
3516     int argc,
3517     char **argv,
3518     char ***prdata,
3519     int *xsize,
3520     int *ysize,
3521     FILE * stream,
3522     double *ymin,
3523     double *ymax)
3524 {
3525     int       prlines = 0;
3526     info_t   *grinfo = NULL;
3527     info_t   *walker;
3528
3529     grinfo = rrd_graph_v(argc, argv);
3530     if (grinfo == NULL)
3531         return -1;
3532     walker = grinfo;
3533     (*prdata) = NULL;
3534     while (walker) {
3535         if (strcmp(walker->key, "image_info") == 0) {
3536             prlines++;
3537             if (((*prdata) =
3538                  rrd_realloc((*prdata),
3539                              (prlines + 1) * sizeof(char *))) == NULL) {
3540                 rrd_set_error("realloc prdata");
3541                 return 0;
3542             }
3543             /* imginfo goes to position 0 in the prdata array */
3544             (*prdata)[prlines - 1] = malloc((strlen(walker->value.u_str)
3545                                              + 2) * sizeof(char));
3546             strcpy((*prdata)[prlines - 1], walker->value.u_str);
3547             (*prdata)[prlines] = NULL;
3548         }
3549         /* skip anything else */
3550         walker = walker->next;
3551     }
3552     walker = grinfo;
3553     while (walker) {
3554         if (strcmp(walker->key, "image_width") == 0) {
3555             *xsize = walker->value.u_int;
3556         } else if (strcmp(walker->key, "image_height") == 0) {
3557             *ysize = walker->value.u_int;
3558         } else if (strcmp(walker->key, "value_min") == 0) {
3559             *ymin = walker->value.u_val;
3560         } else if (strcmp(walker->key, "value_max") == 0) {
3561             *ymax = walker->value.u_val;
3562         } else if (strncmp(walker->key, "print", 5) == 0) { /* keys are prdate[0..] */
3563             prlines++;
3564             if (((*prdata) =
3565                  rrd_realloc((*prdata),
3566                              (prlines + 1) * sizeof(char *))) == NULL) {
3567                 rrd_set_error("realloc prdata");
3568                 return 0;
3569             }
3570             (*prdata)[prlines - 1] = malloc((strlen(walker->value.u_str)
3571                                              + 2) * sizeof(char));
3572             (*prdata)[prlines] = NULL;
3573             strcpy((*prdata)[prlines - 1], walker->value.u_str);
3574         } else if (strcmp(walker->key, "image") == 0) {
3575             fwrite(walker->value.u_blo.ptr, walker->value.u_blo.size, 1,
3576                    (stream ? stream : stdout));
3577         }
3578         /* skip anything else */
3579         walker = walker->next;
3580     }
3581     info_free(grinfo);
3582     return 0;
3583 }
3584
3585
3586 /* Some surgery done on this function, it became ridiculously big.
3587 ** Things moved:
3588 ** - initializing     now in rrd_graph_init()
3589 ** - options parsing  now in rrd_graph_options()
3590 ** - script parsing   now in rrd_graph_script()
3591 */
3592 info_t   *rrd_graph_v(
3593     int argc,
3594     char **argv)
3595 {
3596     image_desc_t im;
3597     info_t   *grinfo;
3598
3599     rrd_graph_init(&im);
3600     /* a dummy surface so that we can measure text sizes for placements */
3601     im.surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 10, 10);
3602     im.cr = cairo_create(im.surface);
3603     rrd_graph_options(argc, argv, &im);
3604     if (rrd_test_error()) {
3605         info_free(im.grinfo);
3606         im_free(&im);
3607         return NULL;
3608     }
3609
3610     if (optind >= argc) {
3611         info_free(im.grinfo);
3612         im_free(&im);
3613         rrd_set_error("missing filename");
3614         return NULL;
3615     }
3616
3617     if (strlen(argv[optind]) >= MAXPATH) {
3618         rrd_set_error("filename (including path) too long");
3619         info_free(im.grinfo);
3620         im_free(&im);
3621         return NULL;
3622     }
3623
3624     strncpy(im.graphfile, argv[optind], MAXPATH - 1);
3625     im.graphfile[MAXPATH - 1] = '\0';
3626
3627     if (strcmp(im.graphfile, "-") == 0) {
3628         im.graphfile[0] = '\0';
3629     }
3630
3631     rrd_graph_script(argc, argv, &im, 1);
3632     if (rrd_test_error()) {
3633         info_free(im.grinfo);
3634         im_free(&im);
3635         return NULL;
3636     }
3637
3638     /* Everything is now read and the actual work can start */
3639
3640     if (graph_paint(&im) == -1) {
3641         info_free(im.grinfo);
3642         im_free(&im);
3643         return NULL;
3644     }
3645
3646
3647     /* The image is generated and needs to be output.
3648      ** Also, if needed, print a line with information about the image.
3649      */
3650
3651     if (im.imginfo) {
3652         infoval   info;
3653
3654         info.u_str =
3655             sprintf_alloc(im.imginfo,
3656                           im.graphfile,
3657                           (long) (im.zoom *
3658                                   im.ximg), (long) (im.zoom * im.yimg));
3659         grinfo_push(&im, sprintf_alloc("image_info"), RD_I_STR, info);
3660         free(info.u_str);
3661     }
3662     if (im.rendered_image) {
3663         infoval   img;
3664
3665         img.u_blo.size = im.rendered_image_size;
3666         img.u_blo.ptr = im.rendered_image;
3667         grinfo_push(&im, sprintf_alloc("image"), RD_I_BLO, img);
3668     }
3669     grinfo = im.grinfo;
3670     im_free(&im);
3671     return grinfo;
3672 }
3673
3674 void rrd_graph_init(
3675     image_desc_t
3676     *im)
3677 {
3678     unsigned int i;
3679
3680 #ifdef HAVE_TZSET
3681     tzset();
3682 #endif
3683 #ifdef HAVE_SETLOCALE
3684     setlocale(LC_TIME, "");
3685 #ifdef HAVE_MBSTOWCS
3686     setlocale(LC_CTYPE, "");
3687 #endif
3688 #endif
3689     im->base = 1000;
3690     im->cr = NULL;
3691     im->draw_x_grid = 1;
3692     im->draw_y_grid = 1;
3693     im->extra_flags = 0;
3694     im->font_options = cairo_font_options_create();
3695     im->forceleftspace = 0;
3696     im->gdes_c = 0;
3697     im->gdes = NULL;
3698     im->graph_antialias = CAIRO_ANTIALIAS_GRAY;
3699     im->grid_dash_off = 1;
3700     im->grid_dash_on = 1;
3701     im->gridfit = 1;
3702     im->grinfo = (info_t *) NULL;
3703     im->grinfo_current = (info_t *) NULL;
3704     im->imgformat = IF_PNG;
3705     im->imginfo = NULL;
3706     im->lazy = 0;
3707     im->logarithmic = 0;
3708     im->maxval = DNAN;
3709     im->minval = 0;
3710     im->minval = DNAN;
3711     im->prt_c = 0;
3712     im->rigid = 0;
3713     im->rendered_image_size = 0;
3714     im->rendered_image = NULL;
3715     im->slopemode = 0;
3716     im->step = 0;
3717     im->surface = NULL;
3718     im->symbol = ' ';
3719     im->tabwidth = 40.0;
3720     im->title[0] = '\0';
3721     im->unitsexponent = 9999;
3722     im->unitslength = 6;
3723     im->viewfactor = 1.0;
3724     im->watermark[0] = '\0';
3725     im->ximg = 0;
3726     im->xlab_user.minsec = -1;
3727     im->xorigin = 0;
3728     im->xsize = 400;
3729     im->ygridstep = DNAN;
3730     im->yimg = 0;
3731     im->ylegend[0] = '\0';
3732     im->yorigin = 0;
3733     im->ysize = 100;
3734     im->zoom = 1;
3735     cairo_font_options_set_hint_style
3736         (im->font_options, CAIRO_HINT_STYLE_FULL);
3737     cairo_font_options_set_hint_metrics
3738         (im->font_options, CAIRO_HINT_METRICS_ON);
3739     cairo_font_options_set_antialias(im->font_options, CAIRO_ANTIALIAS_GRAY);
3740     for (i = 0; i < DIM(graph_col); i++)
3741         im->graph_col[i] = graph_col[i];
3742 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
3743     {
3744         char     *windir;
3745         char      rrd_win_default_font[1000];
3746
3747         windir = getenv("windir");
3748         /* %windir% is something like D:\windows or C:\winnt */
3749         if (windir != NULL) {
3750             strncpy(rrd_win_default_font, windir, 500);
3751             rrd_win_default_font[500] = '\0';
3752             strcat(rrd_win_default_font, "\\fonts\\");
3753             strcat(rrd_win_default_font, RRD_DEFAULT_FONT);
3754             for (i = 0; i < DIM(text_prop); i++) {
3755                 strncpy(text_prop[i].font,
3756                         rrd_win_default_font, sizeof(text_prop[i].font) - 1);
3757                 text_prop[i].font[sizeof(text_prop[i].font) - 1] = '\0';
3758             }
3759         }
3760     }
3761 #endif
3762     {
3763         char     *deffont;
3764
3765         deffont = getenv("RRD_DEFAULT_FONT");
3766         if (deffont != NULL) {
3767             for (i = 0; i < DIM(text_prop); i++) {
3768                 strncpy(text_prop[i].font, deffont,
3769                         sizeof(text_prop[i].font) - 1);
3770                 text_prop[i].font[sizeof(text_prop[i].font) - 1] = '\0';
3771             }
3772         }
3773     }
3774     for (i = 0; i < DIM(text_prop); i++) {
3775         im->text_prop[i].size = text_prop[i].size;
3776         strcpy(im->text_prop[i].font, text_prop[i].font);
3777     }
3778 }
3779
3780 void rrd_graph_options(
3781     int argc,
3782     char *argv[],
3783     image_desc_t
3784     *im)
3785 {
3786     int       stroff;
3787     char     *parsetime_error = NULL;
3788     char      scan_gtm[12], scan_mtm[12], scan_ltm[12], col_nam[12];
3789     time_t    start_tmp = 0, end_tmp = 0;
3790     long      long_tmp;
3791     struct rrd_time_value start_tv, end_tv;
3792     long unsigned int color;
3793     char     *old_locale = "";
3794
3795     /* defines for long options without a short equivalent. should be bytes,
3796        and may not collide with (the ASCII value of) short options */
3797 #define LONGOPT_UNITS_SI 255
3798     struct option long_options[] = {
3799         {
3800          "start", required_argument, 0, 's'}, {
3801                                                "end", required_argument, 0,
3802                                                'e'}, {
3803                                                       "x-grid",
3804                                                       required_argument, 0,
3805                                                       'x'}, {
3806                                                              "y-grid",
3807                                                              required_argument,
3808                                                              0, 'y'}, {
3809                                                                        "vertical-label",
3810                                                                        required_argument,
3811                                                                        0,
3812                                                                        'v'}, {
3813                                                                               "width",
3814                                                                               required_argument,
3815                                                                               0,
3816                                                                               'w'},
3817         {
3818          "height", required_argument, 0, 'h'}, {
3819                                                 "full-size-mode", no_argument,
3820                                                 0, 'D'}, {
3821                                                           "interlaced",
3822                                                           no_argument, 0,
3823                                                           'i'}, {
3824                                                                  "upper-limit",
3825                                                                  required_argument,
3826                                                                  0,
3827                                                                  'u'}, {
3828                                                                         "lower-limit",
3829                                                                         required_argument,
3830                                                                         0,
3831                                                                         'l'}, {
3832                                                                                "rigid",
3833                                                                                no_argument,
3834                                                                                0,
3835                                                                                'r'},
3836         {
3837          "base", required_argument, 0, 'b'}, {
3838                                               "logarithmic", no_argument, 0,
3839                                               'o'}, {
3840                                                      "color",
3841                                                      required_argument, 0,
3842                                                      'c'}, {
3843                                                             "font",
3844                                                             required_argument,
3845                                                             0, 'n'}, {
3846                                                                       "title",
3847                                                                       required_argument,
3848                                                                       0, 't'},
3849         {
3850          "imginfo", required_argument, 0, 'f'}, {
3851                                                  "imgformat",
3852                                                  required_argument, 0, 'a'}, {
3853                                                                               "lazy",
3854                                                                               no_argument,
3855                                                                               0,
3856                                                                               'z'},
3857         {
3858          "zoom", required_argument, 0, 'm'}, {
3859                                               "no-legend", no_argument, 0,
3860                                               'g'}, {
3861                                                      "force-rules-legend",
3862                                                      no_argument,
3863                                                      0, 'F'}, {
3864                                                                "only-graph",
3865                                                                no_argument, 0,
3866                                                                'j'}, {
3867                                                                       "alt-y-grid",
3868                                                                       no_argument,
3869                                                                       0, 'Y'},
3870         {
3871          "no-minor", no_argument, 0, 'I'}, {
3872                                             "slope-mode", no_argument, 0,
3873                                             'E'}, {
3874                                                    "alt-autoscale",
3875                                                    no_argument, 0, 'A'}, {
3876                                                                           "alt-autoscale-min",
3877                                                                           no_argument,
3878                                                                           0,
3879                                                                           'J'}, {
3880                                                                                  "alt-autoscale-max",
3881                                                                                  no_argument,
3882                                                                                  0,
3883                                                                                  'M'}, {
3884                                                                                         "no-gridfit",
3885                                                                                         no_argument,
3886                                                                                         0,
3887                                                                                         'N'},
3888         {
3889          "units-exponent", required_argument,
3890          0, 'X'}, {
3891                    "units-length", required_argument,
3892                    0, 'L'}, {
3893                              "units", required_argument, 0,
3894                              LONGOPT_UNITS_SI}, {
3895                                                  "step", required_argument, 0,
3896                                                  'S'}, {
3897                                                         "tabwidth",
3898                                                         required_argument, 0,
3899                                                         'T'}, {
3900                                                                "font-render-mode",
3901                                                                required_argument,
3902                                                                0, 'R'}, {
3903                                                                          "graph-render-mode",
3904                                                                          required_argument,
3905                                                                          0,
3906                                                                          'G'},
3907         {
3908          "font-smoothing-threshold",
3909          required_argument, 0, 'B'}, {
3910                                       "watermark", required_argument, 0, 'W'}, {
3911                                                                                 "alt-y-mrtg", no_argument, 0, 1000},    /* this has no effect it is just here to save old apps from crashing when they use it */
3912         {
3913          0, 0, 0, 0}
3914     };
3915     optind = 0;
3916     opterr = 0;         /* initialize getopt */
3917     parsetime("end-24h", &start_tv);
3918     parsetime("now", &end_tv);
3919     while (1) {
3920         int       option_index = 0;
3921         int       opt;
3922         int       col_start, col_end;
3923
3924         opt = getopt_long(argc, argv,
3925                           "s:e:x:y:v:w:h:D:iu:l:rb:oc:n:m:t:f:a:I:zgjFYAMEX:L:S:T:NR:B:W:k",
3926                           long_options, &option_index);
3927         if (opt == EOF)
3928             break;
3929         switch (opt) {
3930         case 'I':
3931             im->extra_flags |= NOMINOR;
3932             break;
3933         case 'Y':
3934             im->extra_flags |= ALTYGRID;
3935             break;
3936         case 'A':
3937             im->extra_flags |= ALTAUTOSCALE;
3938             break;
3939         case 'J':
3940             im->extra_flags |= ALTAUTOSCALE_MIN;
3941             break;
3942         case 'M':
3943             im->extra_flags |= ALTAUTOSCALE_MAX;
3944             break;
3945         case 'j':
3946             im->extra_flags |= ONLY_GRAPH;
3947             break;
3948         case 'g':
3949             im->extra_flags |= NOLEGEND;
3950             break;
3951         case 'F':
3952             im->extra_flags |= FORCE_RULES_LEGEND;
3953             break;
3954         case LONGOPT_UNITS_SI:
3955             if (im->extra_flags & FORCE_UNITS) {
3956                 rrd_set_error("--units can only be used once!");
3957                 setlocale(LC_NUMERIC, old_locale);
3958                 return;
3959             }
3960             if (strcmp(optarg, "si") == 0)
3961                 im->extra_flags |= FORCE_UNITS_SI;
3962             else {
3963                 rrd_set_error("invalid argument for --units: %s", optarg);
3964                 return;
3965             }
3966             break;
3967         case 'X':
3968             im->unitsexponent = atoi(optarg);
3969             break;
3970         case 'L':
3971             im->unitslength = atoi(optarg);
3972             im->forceleftspace = 1;
3973             break;
3974         case 'T':
3975             old_locale = setlocale(LC_NUMERIC, "C");
3976             im->tabwidth = atof(optarg);
3977             setlocale(LC_NUMERIC, old_locale);
3978             break;
3979         case 'S':
3980             old_locale = setlocale(LC_NUMERIC, "C");
3981             im->step = atoi(optarg);
3982             setlocale(LC_NUMERIC, old_locale);
3983             break;
3984         case 'N':
3985             im->gridfit = 0;
3986             break;
3987         case 's':
3988             if ((parsetime_error = parsetime(optarg, &start_tv))) {
3989                 rrd_set_error("start time: %s", parsetime_error);
3990                 return;
3991             }
3992             break;
3993         case 'e':
3994             if ((parsetime_error = parsetime(optarg, &end_tv))) {
3995                 rrd_set_error("end time: %s", parsetime_error);
3996                 return;
3997             }
3998             break;
3999         case 'x':
4000             if (strcmp(optarg, "none") == 0) {
4001                 im->draw_x_grid = 0;
4002                 break;
4003             };
4004             if (sscanf(optarg,
4005                        "%10[A-Z]:%ld:%10[A-Z]:%ld:%10[A-Z]:%ld:%ld:%n",
4006                        scan_gtm,
4007                        &im->xlab_user.gridst,
4008                        scan_mtm,
4009                        &im->xlab_user.mgridst,
4010                        scan_ltm,
4011                        &im->xlab_user.labst,
4012                        &im->xlab_user.precis, &stroff) == 7 && stroff != 0) {
4013                 strncpy(im->xlab_form, optarg + stroff,
4014                         sizeof(im->xlab_form) - 1);
4015                 im->xlab_form[sizeof(im->xlab_form) - 1] = '\0';
4016                 if ((int)
4017                     (im->xlab_user.gridtm = tmt_conv(scan_gtm)) == -1) {
4018                     rrd_set_error("unknown keyword %s", scan_gtm);
4019                     return;
4020                 } else if ((int)
4021                            (im->xlab_user.mgridtm = tmt_conv(scan_mtm))
4022                            == -1) {
4023                     rrd_set_error("unknown keyword %s", scan_mtm);
4024                     return;
4025                 } else if ((int)
4026                            (im->xlab_user.labtm = tmt_conv(scan_ltm)) == -1) {
4027                     rrd_set_error("unknown keyword %s", scan_ltm);
4028                     return;
4029                 }
4030                 im->xlab_user.minsec = 1;
4031                 im->xlab_user.stst = im->xlab_form;
4032             } else {
4033                 rrd_set_error("invalid x-grid format");
4034                 return;
4035             }
4036             break;
4037         case 'y':
4038
4039             if (strcmp(optarg, "none") == 0) {
4040                 im->draw_y_grid = 0;
4041                 break;
4042             };
4043             old_locale = setlocale(LC_NUMERIC, "C");
4044             if (sscanf(optarg, "%lf:%d", &im->ygridstep, &im->ylabfact) == 2) {
4045                 setlocale(LC_NUMERIC, old_locale);
4046                 if (im->ygridstep <= 0) {
4047                     rrd_set_error("grid step must be > 0");
4048                     return;
4049                 } else if (im->ylabfact < 1) {
4050                     rrd_set_error("label factor must be > 0");
4051                     return;
4052                 }
4053             } else {
4054                 setlocale(LC_NUMERIC, old_locale);
4055                 rrd_set_error("invalid y-grid format");
4056                 return;
4057             }
4058             break;
4059         case 'v':
4060             strncpy(im->ylegend, optarg, 150);
4061             im->ylegend[150] = '\0';
4062             break;
4063         case 'u':
4064             old_locale = setlocale(LC_NUMERIC, "C");
4065             im->maxval = atof(optarg);
4066             setlocale(LC_NUMERIC, old_locale);
4067             break;
4068         case 'l':
4069             old_locale = setlocale(LC_NUMERIC, "C");
4070             im->minval = atof(optarg);
4071             setlocale(LC_NUMERIC, old_locale);
4072             break;
4073         case 'b':
4074             im->base = atol(optarg);
4075             if (im->base != 1024 && im->base != 1000) {
4076                 rrd_set_error
4077                     ("the only sensible value for base apart from 1000 is 1024");
4078                 return;
4079             }
4080             break;
4081         case 'w':
4082             long_tmp = atol(optarg);
4083             if (long_tmp < 10) {
4084                 rrd_set_error("width below 10 pixels");
4085                 return;
4086             }
4087             im->xsize = long_tmp;
4088             break;
4089         case 'h':
4090             long_tmp = atol(optarg);
4091             if (long_tmp < 10) {
4092                 rrd_set_error("height below 10 pixels");
4093                 return;
4094             }
4095             im->ysize = long_tmp;
4096             break;
4097         case 'D':
4098             im->extra_flags |= FULL_SIZE_MODE;
4099             break;
4100         case 'i':
4101             /* interlaced png not supported at the moment */
4102             break;
4103         case 'r':
4104             im->rigid = 1;
4105             break;
4106         case 'f':
4107             im->imginfo = optarg;
4108             break;
4109         case 'a':
4110             if ((int)
4111                 (im->imgformat = if_conv(optarg)) == -1) {
4112                 rrd_set_error("unsupported graphics format '%s'", optarg);
4113                 return;
4114             }
4115             break;
4116         case 'z':
4117             im->lazy = 1;
4118             break;
4119         case 'E':
4120             im->slopemode = 1;
4121             break;
4122         case 'o':
4123             im->logarithmic = 1;
4124             break;
4125         case 'c':
4126             if (sscanf(optarg,
4127                        "%10[A-Z]#%n%8lx%n",
4128                        col_nam, &col_start, &color, &col_end) == 2) {
4129                 int       ci;
4130                 int       col_len = col_end - col_start;
4131
4132                 switch (col_len) {
4133                 case 3:
4134                     color =
4135                         (((color & 0xF00) * 0x110000) | ((color & 0x0F0) *
4136                                                          0x011000) |
4137                          ((color & 0x00F)
4138                           * 0x001100)
4139                          | 0x000000FF);
4140                     break;
4141                 case 4:
4142                     color =
4143                         (((color & 0xF000) *
4144                           0x11000) | ((color & 0x0F00) *
4145                                       0x01100) | ((color &
4146                                                    0x00F0) *
4147                                                   0x00110) |
4148                          ((color & 0x000F) * 0x00011)
4149                         );
4150                     break;
4151                 case 6:
4152                     color = (color << 8) + 0xff /* shift left by 8 */ ;
4153                     break;
4154                 case 8:
4155                     break;
4156                 default:
4157                     rrd_set_error("the color format is #RRGGBB[AA]");
4158                     return;
4159                 }
4160                 if ((ci = grc_conv(col_nam)) != -1) {
4161                     im->graph_col[ci] = gfx_hex_to_col(color);
4162                 } else {
4163                     rrd_set_error("invalid color name '%s'", col_nam);
4164                     return;
4165                 }
4166             } else {
4167                 rrd_set_error("invalid color def format");
4168                 return;
4169             }
4170             break;
4171         case 'n':{
4172             char      prop[15];
4173             double    size = 1;
4174             int       end;
4175
4176             old_locale = setlocale(LC_NUMERIC, "C");
4177             if (sscanf(optarg, "%10[A-Z]:%lf%n", prop, &size, &end) >= 2) {
4178                 int       sindex, propidx;
4179
4180                 setlocale(LC_NUMERIC, old_locale);
4181                 if ((sindex = text_prop_conv(prop)) != -1) {
4182                     for (propidx = sindex;
4183                          propidx < TEXT_PROP_LAST; propidx++) {
4184                         if (size > 0) {
4185                             im->text_prop[propidx].size = size;
4186                         }
4187                         if ((int) strlen(prop) > end) {
4188                             if (prop[end] == ':') {
4189                                 strncpy(im->text_prop[propidx].font,
4190                                         prop + end + 1, 255);
4191                                 im->text_prop[propidx].font[255] = '\0';
4192                             } else {
4193                                 rrd_set_error
4194                                     ("expected after font size in '%s'",
4195                                      prop);
4196                                 return;
4197                             }
4198                         }
4199                         if (propidx == sindex && sindex != 0)
4200                             break;
4201                     }
4202                 } else {
4203                     rrd_set_error("invalid fonttag '%s'", prop);
4204                     return;
4205                 }
4206             } else {
4207                 setlocale(LC_NUMERIC, old_locale);
4208                 rrd_set_error("invalid text property format");
4209                 return;
4210             }
4211             break;
4212         }
4213         case 'm':
4214             old_locale = setlocale(LC_NUMERIC, "C");
4215             im->zoom = atof(optarg);
4216             setlocale(LC_NUMERIC, old_locale);
4217             if (im->zoom <= 0.0) {
4218                 rrd_set_error("zoom factor must be > 0");
4219                 return;
4220             }
4221             break;
4222         case 't':
4223             strncpy(im->title, optarg, 150);
4224             im->title[150] = '\0';
4225             break;
4226         case 'R':
4227             if (strcmp(optarg, "normal") == 0) {
4228                 cairo_font_options_set_antialias
4229                     (im->font_options, CAIRO_ANTIALIAS_GRAY);
4230                 cairo_font_options_set_hint_style
4231                     (im->font_options, CAIRO_HINT_STYLE_FULL);
4232             } else if (strcmp(optarg, "light") == 0) {
4233                 cairo_font_options_set_antialias
4234                     (im->font_options, CAIRO_ANTIALIAS_GRAY);
4235                 cairo_font_options_set_hint_style
4236                     (im->font_options, CAIRO_HINT_STYLE_SLIGHT);
4237             } else if (strcmp(optarg, "mono") == 0) {
4238                 cairo_font_options_set_antialias
4239                     (im->font_options, CAIRO_ANTIALIAS_NONE);
4240                 cairo_font_options_set_hint_style
4241                     (im->font_options, CAIRO_HINT_STYLE_FULL);
4242             } else {
4243                 rrd_set_error("unknown font-render-mode '%s'", optarg);
4244                 return;
4245             }
4246             break;
4247         case 'G':
4248             if (strcmp(optarg, "normal") == 0)
4249                 im->graph_antialias = CAIRO_ANTIALIAS_GRAY;
4250             else if (strcmp(optarg, "mono") == 0)
4251                 im->graph_antialias = CAIRO_ANTIALIAS_NONE;
4252             else {
4253                 rrd_set_error("unknown graph-render-mode '%s'", optarg);
4254                 return;
4255             }
4256             break;
4257         case 'B':
4258             /* not supported curently */
4259             break;
4260         case 'W':
4261             strncpy(im->watermark, optarg, 100);
4262             im->watermark[99] = '\0';
4263             break;
4264         case '?':
4265             if (optopt != 0)
4266                 rrd_set_error("unknown option '%c'", optopt);
4267             else
4268                 rrd_set_error("unknown option '%s'", argv[optind - 1]);
4269             return;
4270         }
4271     }
4272
4273     if (im->logarithmic && im->minval <= 0) {
4274         rrd_set_error
4275             ("for a logarithmic yaxis you must specify a lower-limit > 0");
4276         return;
4277     }
4278
4279     if (proc_start_end(&start_tv, &end_tv, &start_tmp, &end_tmp) == -1) {
4280         /* error string is set in parsetime.c */
4281         return;
4282     }
4283
4284     if (start_tmp < 3600 * 24 * 365 * 10) {
4285         rrd_set_error
4286             ("the first entry to fetch should be after 1980 (%ld)",
4287              start_tmp);
4288         return;
4289     }
4290
4291     if (end_tmp < start_tmp) {
4292         rrd_set_error
4293             ("start (%ld) should be less than end (%ld)", start_tmp, end_tmp);
4294         return;
4295     }
4296
4297     im->start = start_tmp;
4298     im->end = end_tmp;
4299     im->step = max((long) im->step, (im->end - im->start) / im->xsize);
4300 }
4301
4302 int rrd_graph_color(
4303     image_desc_t
4304     *im,
4305     char *var,
4306     char *err,
4307     int optional)
4308 {
4309     char     *color;
4310     graph_desc_t *gdp = &im->gdes[im->gdes_c - 1];
4311
4312     color = strstr(var, "#");
4313     if (color == NULL) {
4314         if (optional == 0) {
4315             rrd_set_error("Found no color in %s", err);
4316             return 0;
4317         }
4318         return 0;
4319     } else {
4320         int       n = 0;
4321         char     *rest;
4322         long unsigned int col;
4323
4324         rest = strstr(color, ":");
4325         if (rest != NULL)
4326             n = rest - color;
4327         else
4328             n = strlen(color);
4329         switch (n) {
4330         case 7:
4331             sscanf(color, "#%6lx%n", &col, &n);
4332             col = (col << 8) + 0xff /* shift left by 8 */ ;
4333             if (n != 7)
4334                 rrd_set_error("Color problem in %s", err);
4335             break;
4336         case 9:
4337             sscanf(color, "#%8lx%n", &col, &n);
4338             if (n == 9)
4339                 break;
4340         default:
4341             rrd_set_error("Color problem in %s", err);
4342         }
4343         if (rrd_test_error())
4344             return 0;
4345         gdp->col = gfx_hex_to_col(col);
4346         return n;
4347     }
4348 }
4349
4350
4351 int bad_format(
4352     char *fmt)
4353 {
4354     char     *ptr;
4355     int       n = 0;
4356
4357     ptr = fmt;
4358     while (*ptr != '\0')
4359         if (*ptr++ == '%') {
4360
4361             /* line cannot end with percent char */
4362             if (*ptr == '\0')
4363                 return 1;
4364             /* '%s', '%S' and '%%' are allowed */
4365             if (*ptr == 's' || *ptr == 'S' || *ptr == '%')
4366                 ptr++;
4367             /* %c is allowed (but use only with vdef!) */
4368             else if (*ptr == 'c') {
4369                 ptr++;
4370                 n = 1;
4371             }
4372
4373             /* or else '% 6.2lf' and such are allowed */
4374             else {
4375                 /* optional padding character */
4376                 if (*ptr == ' ' || *ptr == '+' || *ptr == '-')
4377                     ptr++;
4378                 /* This should take care of 'm.n' with all three optional */
4379                 while (*ptr >= '0' && *ptr <= '9')
4380                     ptr++;
4381                 if (*ptr == '.')
4382                     ptr++;
4383                 while (*ptr >= '0' && *ptr <= '9')
4384                     ptr++;
4385                 /* Either 'le', 'lf' or 'lg' must follow here */
4386                 if (*ptr++ != 'l')
4387                     return 1;
4388                 if (*ptr == 'e' || *ptr == 'f' || *ptr == 'g')
4389                     ptr++;
4390                 else
4391                     return 1;
4392                 n++;
4393             }
4394         }
4395
4396     return (n != 1);
4397 }
4398
4399
4400 int vdef_parse(
4401     struct graph_desc_t
4402     *gdes,
4403     const char *const str)
4404 {
4405     /* A VDEF currently is either "func" or "param,func"
4406      * so the parsing is rather simple.  Change if needed.
4407      */
4408     double    param;
4409     char      func[30];
4410     int       n;
4411     char     *old_locale;
4412
4413     n = 0;
4414     old_locale = setlocale(LC_NUMERIC, "C");
4415     sscanf(str, "%le,%29[A-Z]%n", &param, func, &n);
4416     setlocale(LC_NUMERIC, old_locale);
4417     if (n == (int) strlen(str)) {   /* matched */
4418         ;
4419     } else {
4420         n = 0;
4421         sscanf(str, "%29[A-Z]%n", func, &n);
4422         if (n == (int) strlen(str)) {   /* matched */
4423             param = DNAN;
4424         } else {
4425             rrd_set_error
4426                 ("Unknown function string '%s' in VDEF '%s'",
4427                  str, gdes->vname);
4428             return -1;
4429         }
4430     }
4431     if (!strcmp("PERCENT", func))
4432         gdes->vf.op = VDEF_PERCENT;
4433     else if (!strcmp("MAXIMUM", func))
4434         gdes->vf.op = VDEF_MAXIMUM;
4435     else if (!strcmp("AVERAGE", func))
4436         gdes->vf.op = VDEF_AVERAGE;
4437     else if (!strcmp("STDEV", func))
4438         gdes->vf.op = VDEF_STDEV;
4439     else if (!strcmp("MINIMUM", func))
4440         gdes->vf.op = VDEF_MINIMUM;
4441     else if (!strcmp("TOTAL", func))
4442         gdes->vf.op = VDEF_TOTAL;
4443     else if (!strcmp("FIRST", func))
4444         gdes->vf.op = VDEF_FIRST;
4445     else if (!strcmp("LAST", func))
4446         gdes->vf.op = VDEF_LAST;
4447     else if (!strcmp("LSLSLOPE", func))
4448         gdes->vf.op = VDEF_LSLSLOPE;
4449     else if (!strcmp("LSLINT", func))
4450         gdes->vf.op = VDEF_LSLINT;
4451     else if (!strcmp("LSLCORREL", func))
4452         gdes->vf.op = VDEF_LSLCORREL;
4453     else {
4454         rrd_set_error
4455             ("Unknown function '%s' in VDEF '%s'\n", func, gdes->vname);
4456         return -1;
4457     };
4458     switch (gdes->vf.op) {
4459     case VDEF_PERCENT:
4460         if (isnan(param)) { /* no parameter given */
4461             rrd_set_error
4462                 ("Function '%s' needs parameter in VDEF '%s'\n",
4463                  func, gdes->vname);
4464             return -1;
4465         };
4466         if (param >= 0.0 && param <= 100.0) {
4467             gdes->vf.param = param;
4468             gdes->vf.val = DNAN;    /* undefined */
4469             gdes->vf.when = 0;  /* undefined */
4470         } else {
4471             rrd_set_error
4472                 ("Parameter '%f' out of range in VDEF '%s'\n",
4473                  param, gdes->vname);
4474             return -1;
4475         };
4476         break;
4477     case VDEF_MAXIMUM:
4478     case VDEF_AVERAGE:
4479     case VDEF_STDEV:
4480     case VDEF_MINIMUM:
4481     case VDEF_TOTAL:
4482     case VDEF_FIRST:
4483     case VDEF_LAST:
4484     case VDEF_LSLSLOPE:
4485     case VDEF_LSLINT:
4486     case VDEF_LSLCORREL:
4487         if (isnan(param)) {
4488             gdes->vf.param = DNAN;
4489             gdes->vf.val = DNAN;
4490             gdes->vf.when = 0;
4491         } else {
4492             rrd_set_error
4493                 ("Function '%s' needs no parameter in VDEF '%s'\n",
4494                  func, gdes->vname);
4495             return -1;
4496         };
4497         break;
4498     };
4499     return 0;
4500 }
4501
4502
4503 int vdef_calc(
4504     image_desc_t *im,
4505     int gdi)
4506 {
4507     graph_desc_t *src, *dst;
4508     rrd_value_t *data;
4509     long      step, steps;
4510
4511     dst = &im->gdes[gdi];
4512     src = &im->gdes[dst->vidx];
4513     data = src->data + src->ds;
4514     steps = (src->end - src->start) / src->step;
4515 #if 0
4516     printf
4517         ("DEBUG: start == %lu, end == %lu, %lu steps\n",
4518          src->start, src->end, steps);
4519 #endif
4520     switch (dst->vf.op) {
4521     case VDEF_PERCENT:{
4522         rrd_value_t *array;
4523         int       field;
4524         if ((array = malloc(steps * sizeof(double))) == NULL) {
4525             rrd_set_error("malloc VDEV_PERCENT");
4526             return -1;
4527         }
4528         for (step = 0; step < steps; step++) {
4529             array[step] = data[step * src->ds_cnt];
4530         }
4531         qsort(array, step, sizeof(double), vdef_percent_compar);
4532         field = (steps - 1) * dst->vf.param / 100;
4533         dst->vf.val = array[field];
4534         dst->vf.when = 0;   /* no time component */
4535         free(array);
4536 #if 0
4537         for (step = 0; step < steps; step++)
4538             printf("DEBUG: %3li:%10.2f %c\n",
4539                    step, array[step], step == field ? '*' : ' ');
4540 #endif
4541     }
4542         break;
4543     case VDEF_MAXIMUM:
4544         step = 0;
4545         while (step != steps && isnan(data[step * src->ds_cnt]))
4546             step++;
4547         if (step == steps) {
4548             dst->vf.val = DNAN;
4549             dst->vf.when = 0;
4550         } else {
4551             dst->vf.val = data[step * src->ds_cnt];
4552             dst->vf.when = src->start + (step + 1) * src->step;
4553         }
4554         while (step != steps) {
4555             if (finite(data[step * src->ds_cnt])) {
4556                 if (data[step * src->ds_cnt] > dst->vf.val) {
4557                     dst->vf.val = data[step * src->ds_cnt];
4558                     dst->vf.when = src->start + (step + 1) * src->step;
4559                 }
4560             }
4561             step++;
4562         }
4563         break;
4564     case VDEF_TOTAL:
4565     case VDEF_STDEV:
4566     case VDEF_AVERAGE:{
4567         int       cnt = 0;
4568         double    sum = 0.0;
4569         double    average = 0.0;
4570
4571         for (step = 0; step < steps; step++) {
4572             if (finite(data[step * src->ds_cnt])) {
4573                 sum += data[step * src->ds_cnt];
4574                 cnt++;
4575             };
4576         }
4577         if (cnt) {
4578             if (dst->vf.op == VDEF_TOTAL) {
4579                 dst->vf.val = sum * src->step;
4580                 dst->vf.when = 0;   /* no time component */
4581             } else if (dst->vf.op == VDEF_AVERAGE) {
4582                 dst->vf.val = sum / cnt;
4583                 dst->vf.when = 0;   /* no time component */
4584             } else {
4585                 average = sum / cnt;
4586                 sum = 0.0;
4587                 for (step = 0; step < steps; step++) {
4588                     if (finite(data[step * src->ds_cnt])) {
4589                         sum += pow((data[step * src->ds_cnt] - average), 2.0);
4590                     };
4591                 }
4592                 dst->vf.val = pow(sum / cnt, 0.5);
4593                 dst->vf.when = 0;   /* no time component */
4594             };
4595         } else {
4596             dst->vf.val = DNAN;
4597             dst->vf.when = 0;
4598         }
4599     }
4600         break;
4601     case VDEF_MINIMUM:
4602         step = 0;
4603         while (step != steps && isnan(data[step * src->ds_cnt]))
4604             step++;
4605         if (step == steps) {
4606             dst->vf.val = DNAN;
4607             dst->vf.when = 0;
4608         } else {
4609             dst->vf.val = data[step * src->ds_cnt];
4610             dst->vf.when = src->start + (step + 1) * src->step;
4611         }
4612         while (step != steps) {
4613             if (finite(data[step * src->ds_cnt])) {
4614                 if (data[step * src->ds_cnt] < dst->vf.val) {
4615                     dst->vf.val = data[step * src->ds_cnt];
4616                     dst->vf.when = src->start + (step + 1) * src->step;
4617                 }
4618             }
4619             step++;
4620         }
4621         break;
4622     case VDEF_FIRST:
4623         /* The time value returned here is one step before the
4624          * actual time value.  This is the start of the first
4625          * non-NaN interval.
4626          */
4627         step = 0;
4628         while (step != steps && isnan(data[step * src->ds_cnt]))
4629             step++;
4630         if (step == steps) {    /* all entries were NaN */
4631             dst->vf.val = DNAN;
4632             dst->vf.when = 0;
4633         } else {
4634             dst->vf.val = data[step * src->ds_cnt];
4635             dst->vf.when = src->start + step * src->step;
4636         }
4637         break;
4638     case VDEF_LAST:
4639         /* The time value returned here is the
4640          * actual time value.  This is the end of the last
4641          * non-NaN interval.
4642          */
4643         step = steps - 1;
4644         while (step >= 0 && isnan(data[step * src->ds_cnt]))
4645             step--;
4646         if (step < 0) { /* all entries were NaN */
4647             dst->vf.val = DNAN;
4648             dst->vf.when = 0;
4649         } else {
4650             dst->vf.val = data[step * src->ds_cnt];
4651             dst->vf.when = src->start + (step + 1) * src->step;
4652         }
4653         break;
4654     case VDEF_LSLSLOPE:
4655     case VDEF_LSLINT:
4656     case VDEF_LSLCORREL:{
4657         /* Bestfit line by linear least squares method */
4658
4659         int       cnt = 0;
4660         double    SUMx, SUMy, SUMxy, SUMxx, SUMyy, slope, y_intercept, correl;
4661
4662         SUMx = 0;
4663         SUMy = 0;
4664         SUMxy = 0;
4665         SUMxx = 0;
4666         SUMyy = 0;
4667         for (step = 0; step < steps; step++) {
4668             if (finite(data[step * src->ds_cnt])) {
4669                 cnt++;
4670                 SUMx += step;
4671                 SUMxx += step * step;
4672                 SUMxy += step * data[step * src->ds_cnt];
4673                 SUMy += data[step * src->ds_cnt];
4674                 SUMyy += data[step * src->ds_cnt] * data[step * src->ds_cnt];
4675             };
4676         }
4677
4678         slope = (SUMx * SUMy - cnt * SUMxy) / (SUMx * SUMx - cnt * SUMxx);
4679         y_intercept = (SUMy - slope * SUMx) / cnt;
4680         correl =
4681             (SUMxy -
4682              (SUMx * SUMy) / cnt) /
4683             sqrt((SUMxx -
4684                   (SUMx * SUMx) / cnt) * (SUMyy - (SUMy * SUMy) / cnt));
4685         if (cnt) {
4686             if (dst->vf.op == VDEF_LSLSLOPE) {
4687                 dst->vf.val = slope;
4688                 dst->vf.when = 0;
4689             } else if (dst->vf.op == VDEF_LSLINT) {
4690                 dst->vf.val = y_intercept;
4691                 dst->vf.when = 0;
4692             } else if (dst->vf.op == VDEF_LSLCORREL) {
4693                 dst->vf.val = correl;
4694                 dst->vf.when = 0;
4695             };
4696         } else {
4697             dst->vf.val = DNAN;
4698             dst->vf.when = 0;
4699         }
4700     }
4701         break;
4702     }
4703     return 0;
4704 }
4705
4706 /* NaN < -INF < finite_values < INF */
4707 int vdef_percent_compar(
4708     const void
4709     *a,
4710     const void
4711     *b)
4712 {
4713     /* Equality is not returned; this doesn't hurt except
4714      * (maybe) for a little performance.
4715      */
4716
4717     /* First catch NaN values. They are smallest */
4718     if (isnan(*(double *) a))
4719         return -1;
4720     if (isnan(*(double *) b))
4721         return 1;
4722     /* NaN doesn't reach this part so INF and -INF are extremes.
4723      * The sign from isinf() is compatible with the sign we return
4724      */
4725     if (isinf(*(double *) a))
4726         return isinf(*(double *) a);
4727     if (isinf(*(double *) b))
4728         return isinf(*(double *) b);
4729     /* If we reach this, both values must be finite */
4730     if (*(double *) a < *(double *) b)
4731         return -1;
4732     else
4733         return 1;
4734 }
4735
4736 void grinfo_push(
4737     image_desc_t *im,
4738     char *key,
4739     enum info_type type,
4740     infoval value)
4741 {
4742     im->grinfo_current = info_push(im->grinfo_current, key, type, value);
4743     if (im->grinfo == NULL) {
4744         im->grinfo = im->grinfo_current;
4745     }
4746 }