Code

fix use of setlocale all over the place ...
[rrdtool-all.git] / program / src / rrd_rpncalc.c
1 /****************************************************************************
2  * RRDtool 1.3.9  Copyright by Tobi Oetiker, 1997-2009
3  ****************************************************************************
4  * rrd_rpncalc.c  RPN calculator functions
5  ****************************************************************************/
7 #include "rrd_tool.h"
8 #include "rrd_rpncalc.h"
9 // #include "rrd_graph.h"
10 #include <limits.h>
11 #include <locale.h>
13 #ifdef WIN32
14 #include <stdlib.h>
15 #endif
17 short     addop2str(
18     enum op_en op,
19     enum op_en op_type,
20     char *op_str,
21     char **result_str,
22     unsigned short *offset);
23 int       tzoffset(
24     time_t);            /* used to implement LTIME */
26 short rpn_compact(
27     rpnp_t *rpnp,
28     rpn_cdefds_t **rpnc,
29     short *count)
30 {
31     short     i;
33     *count = 0;
34     /* count the number of rpn nodes */
35     while (rpnp[*count].op != OP_END)
36         (*count)++;
37     if (++(*count) > DS_CDEF_MAX_RPN_NODES) {
38         rrd_set_error("Maximum %d RPN nodes permitted",
39                       DS_CDEF_MAX_RPN_NODES);
40         return -1;
41     }
43     /* allocate memory */
44     *rpnc = (rpn_cdefds_t *) calloc(*count, sizeof(rpn_cdefds_t));
45     for (i = 0; rpnp[i].op != OP_END; i++) {
46         (*rpnc)[i].op = (char) rpnp[i].op;
47         if (rpnp[i].op == OP_NUMBER) {
48             /* rpnp.val is a double, rpnc.val is a short */
49             double    temp = floor(rpnp[i].val);
51             if (temp < SHRT_MIN || temp > SHRT_MAX) {
52                 rrd_set_error
53                     ("constants must be integers in the interval (%d, %d)",
54                      SHRT_MIN, SHRT_MAX);
55                 free(*rpnc);
56                 return -1;
57             }
58             (*rpnc)[i].val = (short) temp;
59         } else if (rpnp[i].op == OP_VARIABLE || rpnp[i].op == OP_PREV_OTHER) {
60             (*rpnc)[i].val = (short) rpnp[i].ptr;
61         }
62     }
63     /* terminate the sequence */
64     (*rpnc)[(*count) - 1].op = OP_END;
65     return 0;
66 }
68 rpnp_t   *rpn_expand(
69     rpn_cdefds_t *rpnc)
70 {
71     short     i;
72     rpnp_t   *rpnp;
74     /* DS_CDEF_MAX_RPN_NODES is small, so at the expense of some wasted
75      * memory we avoid any reallocs */
76     rpnp = (rpnp_t *) calloc(DS_CDEF_MAX_RPN_NODES, sizeof(rpnp_t));
77     if (rpnp == NULL) {
78         rrd_set_error("failed allocating rpnp array");
79         return NULL;
80     }
81     for (i = 0; rpnc[i].op != OP_END; ++i) {
82         rpnp[i].op = (enum op_en) rpnc[i].op;
83         if (rpnp[i].op == OP_NUMBER) {
84             rpnp[i].val = (double) rpnc[i].val;
85         } else if (rpnp[i].op == OP_VARIABLE || rpnp[i].op == OP_PREV_OTHER) {
86             rpnp[i].ptr = (long) rpnc[i].val;
87         }
88     }
89     /* terminate the sequence */
90     rpnp[i].op = OP_END;
91     return rpnp;
92 }
94 /* rpn_compact2str: convert a compact sequence of RPN operator nodes back
95  * into a CDEF string. This function is used by rrd_dump.
96  * arguments:
97  *  rpnc: an array of compact RPN operator nodes
98  *  ds_def: a pointer to the data source definition section of an RRD header
99  *   for lookup of data source names by index
100  *  str: out string, memory is allocated by the function, must be freed by the
101  *   the caller */
102 void rpn_compact2str(
103     rpn_cdefds_t *rpnc,
104     ds_def_t *ds_def,
105     char **str)
107     unsigned short i, offset = 0;
108     char      buffer[7];    /* short as a string */
110     for (i = 0; rpnc[i].op != OP_END; i++) {
111         if (i > 0)
112             (*str)[offset++] = ',';
114 #define add_op(VV,VVV) \
115           if (addop2str((enum op_en)(rpnc[i].op), VV, VVV, str, &offset) == 1) continue;
117         if (rpnc[i].op == OP_NUMBER) {
118             /* convert a short into a string */
119 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
120             _itoa(rpnc[i].val, buffer, 10);
121 #else
122             sprintf(buffer, "%d", rpnc[i].val);
123 #endif
124             add_op(OP_NUMBER, buffer)
125         }
127         if (rpnc[i].op == OP_VARIABLE) {
128             char     *ds_name = ds_def[rpnc[i].val].ds_nam;
130             add_op(OP_VARIABLE, ds_name)
131         }
133         if (rpnc[i].op == OP_PREV_OTHER) {
134             char     *ds_name = ds_def[rpnc[i].val].ds_nam;
136             add_op(OP_VARIABLE, ds_name)
137         }
138 #undef add_op
140 #define add_op(VV,VVV) \
141           if (addop2str((enum op_en)rpnc[i].op, VV, #VVV, str, &offset) == 1) continue;
143         add_op(OP_ADD, +)
144             add_op(OP_SUB, -)
145             add_op(OP_MUL, *)
146             add_op(OP_DIV, /)
147             add_op(OP_MOD, %)
148             add_op(OP_SIN, SIN)
149             add_op(OP_COS, COS)
150             add_op(OP_LOG, LOG)
151             add_op(OP_FLOOR, FLOOR)
152             add_op(OP_CEIL, CEIL)
153             add_op(OP_EXP, EXP)
154             add_op(OP_DUP, DUP)
155             add_op(OP_EXC, EXC)
156             add_op(OP_POP, POP)
157             add_op(OP_LT, LT)
158             add_op(OP_LE, LE)
159             add_op(OP_GT, GT)
160             add_op(OP_GE, GE)
161             add_op(OP_EQ, EQ)
162             add_op(OP_IF, IF)
163             add_op(OP_MIN, MIN)
164             add_op(OP_MAX, MAX)
165             add_op(OP_LIMIT, LIMIT)
166             add_op(OP_UNKN, UNKN)
167             add_op(OP_UN, UN)
168             add_op(OP_NEGINF, NEGINF)
169             add_op(OP_NE, NE)
170             add_op(OP_PREV, PREV)
171             add_op(OP_INF, INF)
172             add_op(OP_ISINF, ISINF)
173             add_op(OP_NOW, NOW)
174             add_op(OP_LTIME, LTIME)
175             add_op(OP_TIME, TIME)
176             add_op(OP_ATAN2, ATAN2)
177             add_op(OP_ATAN, ATAN)
178             add_op(OP_SQRT, SQRT)
179             add_op(OP_SORT, SORT)
180             add_op(OP_REV, REV)
181             add_op(OP_TREND, TREND)
182             add_op(OP_TRENDNAN, TRENDNAN)
183             add_op(OP_RAD2DEG, RAD2DEG)
184             add_op(OP_DEG2RAD, DEG2RAD)
185             add_op(OP_AVG, AVG)
186             add_op(OP_ABS, ABS)
187             add_op(OP_ADDNAN, ADDNAN)
188 #undef add_op
189     }
190     (*str)[offset] = '\0';
194 short addop2str(
195     enum op_en op,
196     enum op_en op_type,
197     char *op_str,
198     char **result_str,
199     unsigned short *offset)
201     if (op == op_type) {
202         short     op_len;
204         op_len = strlen(op_str);
205         *result_str = (char *) rrd_realloc(*result_str,
206                                            (op_len + 1 +
207                                             *offset) * sizeof(char));
208         if (*result_str == NULL) {
209             rrd_set_error("failed to alloc memory in addop2str");
210             return -1;
211         }
212         strncpy(&((*result_str)[*offset]), op_str, op_len);
213         *offset += op_len;
214         return 1;
215     }
216     return 0;
219 void parseCDEF_DS(
220     const char *def,
221     rrd_t *rrd,
222     int ds_idx)
224     rpnp_t   *rpnp = NULL;
225     rpn_cdefds_t *rpnc = NULL;
226     short     count, i;
228     rpnp = rpn_parse((void *) rrd, def, &lookup_DS);
229     if (rpnp == NULL) {
230         rrd_set_error("failed to parse computed data source");
231         return;
232     }
233     /* Check for OP nodes not permitted in COMPUTE DS.
234      * Moved this check from within rpn_compact() because it really is
235      * COMPUTE DS specific. This is less efficient, but creation doesn't
236      * occur too often. */
237     for (i = 0; rpnp[i].op != OP_END; i++) {
238         if (rpnp[i].op == OP_TIME || rpnp[i].op == OP_LTIME ||
239             rpnp[i].op == OP_PREV || rpnp[i].op == OP_COUNT) {
240             rrd_set_error
241                 ("operators time, ltime, prev and count not supported with DS COMPUTE");
242             free(rpnp);
243             return;
244         }
245     }
246     if (rpn_compact(rpnp, &rpnc, &count) == -1) {
247         free(rpnp);
248         return;
249     }
250     /* copy the compact rpn representation over the ds_def par array */
251     memcpy((void *) &(rrd->ds_def[ds_idx].par[DS_cdef]),
252            (void *) rpnc, count * sizeof(rpn_cdefds_t));
253     free(rpnp);
254     free(rpnc);
257 /* lookup a data source name in the rrd struct and return the index,
258  * should use ds_match() here except:
259  * (1) need a void * pointer to the rrd
260  * (2) error handling is left to the caller
261  */
262 long lookup_DS(
263     void *rrd_vptr,
264     char *ds_name)
266     unsigned int i;
267     rrd_t    *rrd;
269     rrd = (rrd_t *) rrd_vptr;
271     for (i = 0; i < rrd->stat_head->ds_cnt; ++i) {
272         if (strcmp(ds_name, rrd->ds_def[i].ds_nam) == 0)
273             return i;
274     }
275     /* the caller handles a bad data source name in the rpn string */
276     return -1;
279 /* rpn_parse : parse a string and generate a rpnp array; modified
280  * str2rpn() originally included in rrd_graph.c
281  * arguments:
282  * key_hash: a transparent argument passed to lookup(); conceptually this
283  *    is a hash object for lookup of a numeric key given a variable name
284  * expr: the string RPN expression, including variable names
285  * lookup(): a function that retrieves a numeric key given a variable name
286  */
287 rpnp_t   *rpn_parse(
288     void *key_hash,
289     const char *const expr_const,
290     long      (*lookup) (void *,
291                          char *))
293     int       pos = 0;
294     char     *expr;
295     long      steps = -1;
296     rpnp_t   *rpnp;
297     char      vname[MAX_VNAME_LEN + 10];
298     char     *old_locale;
300     old_locale = setlocale(LC_NUMERIC, NULL);
301     setlocale(LC_NUMERIC, "C");
303     rpnp = NULL;
304     expr = (char *) expr_const;
306     while (*expr) {
307         if ((rpnp = (rpnp_t *) rrd_realloc(rpnp, (++steps + 2) *
308                                            sizeof(rpnp_t))) == NULL) {
309             setlocale(LC_NUMERIC, old_locale);
310             return NULL;
311         }
313         else if ((sscanf(expr, "%lf%n", &rpnp[steps].val, &pos) == 1)
314                  && (expr[pos] == ',')) {
315             rpnp[steps].op = OP_NUMBER;
316             expr += pos;
317         }
318 #define match_op(VV,VVV) \
319         else if (strncmp(expr, #VVV, strlen(#VVV))==0 && ( expr[strlen(#VVV)] == ',' || expr[strlen(#VVV)] == '\0' )){ \
320             rpnp[steps].op = VV; \
321             expr+=strlen(#VVV); \
322         }
324 #define match_op_param(VV,VVV) \
325         else if (sscanf(expr, #VVV "(" DEF_NAM_FMT ")",vname) == 1) { \
326           int length = 0; \
327           if ((length = strlen(#VVV)+strlen(vname)+2, \
328               expr[length] == ',' || expr[length] == '\0') ) { \
329              rpnp[steps].op = VV; \
330              rpnp[steps].ptr = (*lookup)(key_hash,vname); \
331              if (rpnp[steps].ptr < 0) { \
332                            free(rpnp); \
333                            return NULL; \
334                          } else expr+=length; \
335           } \
336         }
338         match_op(OP_ADD, +)
339             match_op(OP_SUB, -)
340             match_op(OP_MUL, *)
341             match_op(OP_DIV, /)
342             match_op(OP_MOD, %)
343             match_op(OP_SIN, SIN)
344             match_op(OP_COS, COS)
345             match_op(OP_LOG, LOG)
346             match_op(OP_FLOOR, FLOOR)
347             match_op(OP_CEIL, CEIL)
348             match_op(OP_EXP, EXP)
349             match_op(OP_DUP, DUP)
350             match_op(OP_EXC, EXC)
351             match_op(OP_POP, POP)
352             match_op(OP_LTIME, LTIME)
353             match_op(OP_LT, LT)
354             match_op(OP_LE, LE)
355             match_op(OP_GT, GT)
356             match_op(OP_GE, GE)
357             match_op(OP_EQ, EQ)
358             match_op(OP_IF, IF)
359             match_op(OP_MIN, MIN)
360             match_op(OP_MAX, MAX)
361             match_op(OP_LIMIT, LIMIT)
362             /* order is important here ! .. match longest first */
363             match_op(OP_UNKN, UNKN)
364             match_op(OP_UN, UN)
365             match_op(OP_NEGINF, NEGINF)
366             match_op(OP_NE, NE)
367             match_op(OP_COUNT, COUNT)
368             match_op_param(OP_PREV_OTHER, PREV)
369             match_op(OP_PREV, PREV)
370             match_op(OP_INF, INF)
371             match_op(OP_ISINF, ISINF)
372             match_op(OP_NOW, NOW)
373             match_op(OP_TIME, TIME)
374             match_op(OP_ATAN2, ATAN2)
375             match_op(OP_ATAN, ATAN)
376             match_op(OP_SQRT, SQRT)
377             match_op(OP_SORT, SORT)
378             match_op(OP_REV, REV)
379             match_op(OP_TREND, TREND)
380             match_op(OP_TRENDNAN, TRENDNAN)
381             match_op(OP_RAD2DEG, RAD2DEG)
382             match_op(OP_DEG2RAD, DEG2RAD)
383             match_op(OP_AVG, AVG)
384             match_op(OP_ABS, ABS)
385             match_op(OP_ADDNAN, ADDNAN)
386 #undef match_op
387             else if ((sscanf(expr, DEF_NAM_FMT "%n", vname, &pos) == 1)
388                      && ((rpnp[steps].ptr = (*lookup) (key_hash, vname)) !=
389                          -1)) {
390             rpnp[steps].op = OP_VARIABLE;
391             expr += pos;
392         }
394         else {
395             setlocale(LC_NUMERIC, old_locale);
396             free(rpnp);
397             return NULL;
398         }
400         if (*expr == 0)
401             break;
402         if (*expr == ',')
403             expr++;
404         else {
405             setlocale(LC_NUMERIC, old_locale);
406             free(rpnp);
407             return NULL;
408         }
409     }
410     rpnp[steps + 1].op = OP_END;
411     setlocale(LC_NUMERIC, old_locale);
412     return rpnp;
415 void rpnstack_init(
416     rpnstack_t *rpnstack)
418     rpnstack->s = NULL;
419     rpnstack->dc_stacksize = 0;
420     rpnstack->dc_stackblock = 100;
423 void rpnstack_free(
424     rpnstack_t *rpnstack)
426     if (rpnstack->s != NULL)
427         free(rpnstack->s);
428     rpnstack->dc_stacksize = 0;
431 static int rpn_compare_double(
432     const void *x,
433     const void *y)
435     double    diff = *((const double *) x) - *((const double *) y);
437     return (diff < 0) ? -1 : (diff > 0) ? 1 : 0;
440 /* rpn_calc: run the RPN calculator; also performs variable substitution;
441  * moved and modified from data_calc() originally included in rrd_graph.c 
442  * arguments:
443  * rpnp : an array of RPN operators (including variable references)
444  * rpnstack : the initialized stack
445  * data_idx : when data_idx is a multiple of rpnp.step, the rpnp.data pointer
446  *   is advanced by rpnp.ds_cnt; used only for variable substitution
447  * output : an array of output values; OP_PREV assumes this array contains
448  *   the "previous" value at index position output_idx-1; the definition of
449  *   "previous" depends on the calling environment
450  * output_idx : an index into the output array in which to store the output
451  *   of the RPN calculator
452  * returns: -1 if the computation failed (also calls rrd_set_error)
453  *           0 on success
454  */
455 short rpn_calc(
456     rpnp_t *rpnp,
457     rpnstack_t *rpnstack,
458     long data_idx,
459     rrd_value_t *output,
460     int output_idx)
462     int       rpi;
463     long      stptr = -1;
465     /* process each op from the rpn in turn */
466     for (rpi = 0; rpnp[rpi].op != OP_END; rpi++) {
467         /* allocate or grow the stack */
468         if (stptr + 5 > rpnstack->dc_stacksize) {
469             /* could move this to a separate function */
470             rpnstack->dc_stacksize += rpnstack->dc_stackblock;
471             rpnstack->s = (double*)(rrd_realloc(rpnstack->s,
472                                       (rpnstack->dc_stacksize) *
473                                                             sizeof(*(rpnstack->s))));
474             if (rpnstack->s == NULL) {
475                 rrd_set_error("RPN stack overflow");
476                 return -1;
477             }
478         }
479 #define stackunderflow(MINSIZE)                         \
480         if(stptr<MINSIZE){                              \
481             rrd_set_error("RPN stack underflow");       \
482             return -1;                                  \
483         }
485         switch (rpnp[rpi].op) {
486         case OP_NUMBER:
487             rpnstack->s[++stptr] = rpnp[rpi].val;
488             break;
489         case OP_VARIABLE:
490         case OP_PREV_OTHER:
491             /* Sanity check: VDEFs shouldn't make it here */
492             if (rpnp[rpi].ds_cnt == 0) {
493                 rrd_set_error("VDEF made it into rpn_calc... aborting");
494                 return -1;
495             } else {
496                 /* make sure we pull the correct value from
497                  * the *.data array. Adjust the pointer into
498                  * the array acordingly. Advance the ptr one
499                  * row in the rra (skip over non-relevant
500                  * data sources)
501                  */
502                 if (rpnp[rpi].op == OP_VARIABLE) {
503                     rpnstack->s[++stptr] = *(rpnp[rpi].data);
504                 } else {
505                     if ((output_idx) <= 0) {
506                         rpnstack->s[++stptr] = DNAN;
507                     } else {
508                         rpnstack->s[++stptr] =
509                             *(rpnp[rpi].data - rpnp[rpi].ds_cnt);
510                     }
512                 }
513                 if (data_idx % rpnp[rpi].step == 0) {
514                     rpnp[rpi].data += rpnp[rpi].ds_cnt;
515                 }
516             }
517             break;
518         case OP_COUNT:
519             rpnstack->s[++stptr] = (output_idx + 1);    /* Note: Counter starts at 1 */
520             break;
521         case OP_PREV:
522             if ((output_idx) <= 0) {
523                 rpnstack->s[++stptr] = DNAN;
524             } else {
525                 rpnstack->s[++stptr] = output[output_idx - 1];
526             }
527             break;
528         case OP_UNKN:
529             rpnstack->s[++stptr] = DNAN;
530             break;
531         case OP_INF:
532             rpnstack->s[++stptr] = DINF;
533             break;
534         case OP_NEGINF:
535             rpnstack->s[++stptr] = -DINF;
536             break;
537         case OP_NOW:
538             rpnstack->s[++stptr] = (double) time(NULL);
539             break;
540         case OP_TIME:
541             /* HACK: this relies on the data_idx being the time,
542              ** which the within-function scope is unaware of */
543             rpnstack->s[++stptr] = (double) data_idx;
544             break;
545         case OP_LTIME:
546             rpnstack->s[++stptr] =
547                 (double) tzoffset(data_idx) + (double) data_idx;
548             break;
549         case OP_ADD:
550             stackunderflow(1);
551             rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
552                 + rpnstack->s[stptr];
553             stptr--;
554             break;
555         case OP_ADDNAN:
556             stackunderflow(1);
557             if (isnan(rpnstack->s[stptr - 1])) {
558                 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
559             } else if (isnan(rpnstack->s[stptr])) {
560                 /* NOOP */
561                 /* rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]; */
562             } else {
563                 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
564                     + rpnstack->s[stptr];
565             }
567             stptr--;
568             break;
569         case OP_SUB:
570             stackunderflow(1);
571             rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
572                 - rpnstack->s[stptr];
573             stptr--;
574             break;
575         case OP_MUL:
576             stackunderflow(1);
577             rpnstack->s[stptr - 1] = (rpnstack->s[stptr - 1])
578                 * (rpnstack->s[stptr]);
579             stptr--;
580             break;
581         case OP_DIV:
582             stackunderflow(1);
583             rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
584                 / rpnstack->s[stptr];
585             stptr--;
586             break;
587         case OP_MOD:
588             stackunderflow(1);
589             rpnstack->s[stptr - 1] = fmod(rpnstack->s[stptr - 1]
590                                           , rpnstack->s[stptr]);
591             stptr--;
592             break;
593         case OP_SIN:
594             stackunderflow(0);
595             rpnstack->s[stptr] = sin(rpnstack->s[stptr]);
596             break;
597         case OP_ATAN:
598             stackunderflow(0);
599             rpnstack->s[stptr] = atan(rpnstack->s[stptr]);
600             break;
601         case OP_RAD2DEG:
602             stackunderflow(0);
603             rpnstack->s[stptr] = 57.29577951 * rpnstack->s[stptr];
604             break;
605         case OP_DEG2RAD:
606             stackunderflow(0);
607             rpnstack->s[stptr] = 0.0174532952 * rpnstack->s[stptr];
608             break;
609         case OP_ATAN2:
610             stackunderflow(1);
611             rpnstack->s[stptr - 1] = atan2(rpnstack->s[stptr - 1],
612                                            rpnstack->s[stptr]);
613             stptr--;
614             break;
615         case OP_COS:
616             stackunderflow(0);
617             rpnstack->s[stptr] = cos(rpnstack->s[stptr]);
618             break;
619         case OP_CEIL:
620             stackunderflow(0);
621             rpnstack->s[stptr] = ceil(rpnstack->s[stptr]);
622             break;
623         case OP_FLOOR:
624             stackunderflow(0);
625             rpnstack->s[stptr] = floor(rpnstack->s[stptr]);
626             break;
627         case OP_LOG:
628             stackunderflow(0);
629             rpnstack->s[stptr] = log(rpnstack->s[stptr]);
630             break;
631         case OP_DUP:
632             stackunderflow(0);
633             rpnstack->s[stptr + 1] = rpnstack->s[stptr];
634             stptr++;
635             break;
636         case OP_POP:
637             stackunderflow(0);
638             stptr--;
639             break;
640         case OP_EXC:
641             stackunderflow(1);
642             {
643                 double    dummy;
645                 dummy = rpnstack->s[stptr];
646                 rpnstack->s[stptr] = rpnstack->s[stptr - 1];
647                 rpnstack->s[stptr - 1] = dummy;
648             }
649             break;
650         case OP_EXP:
651             stackunderflow(0);
652             rpnstack->s[stptr] = exp(rpnstack->s[stptr]);
653             break;
654         case OP_LT:
655             stackunderflow(1);
656             if (isnan(rpnstack->s[stptr - 1]));
657             else if (isnan(rpnstack->s[stptr]))
658                 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
659             else
660                 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] <
661                     rpnstack->s[stptr] ? 1.0 : 0.0;
662             stptr--;
663             break;
664         case OP_LE:
665             stackunderflow(1);
666             if (isnan(rpnstack->s[stptr - 1]));
667             else if (isnan(rpnstack->s[stptr]))
668                 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
669             else
670                 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] <=
671                     rpnstack->s[stptr] ? 1.0 : 0.0;
672             stptr--;
673             break;
674         case OP_GT:
675             stackunderflow(1);
676             if (isnan(rpnstack->s[stptr - 1]));
677             else if (isnan(rpnstack->s[stptr]))
678                 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
679             else
680                 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] >
681                     rpnstack->s[stptr] ? 1.0 : 0.0;
682             stptr--;
683             break;
684         case OP_GE:
685             stackunderflow(1);
686             if (isnan(rpnstack->s[stptr - 1]));
687             else if (isnan(rpnstack->s[stptr]))
688                 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
689             else
690                 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] >=
691                     rpnstack->s[stptr] ? 1.0 : 0.0;
692             stptr--;
693             break;
694         case OP_NE:
695             stackunderflow(1);
696             if (isnan(rpnstack->s[stptr - 1]));
697             else if (isnan(rpnstack->s[stptr]))
698                 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
699             else
700                 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] ==
701                     rpnstack->s[stptr] ? 0.0 : 1.0;
702             stptr--;
703             break;
704         case OP_EQ:
705             stackunderflow(1);
706             if (isnan(rpnstack->s[stptr - 1]));
707             else if (isnan(rpnstack->s[stptr]))
708                 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
709             else
710                 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] ==
711                     rpnstack->s[stptr] ? 1.0 : 0.0;
712             stptr--;
713             break;
714         case OP_IF:
715             stackunderflow(2);
716             rpnstack->s[stptr - 2] = (isnan(rpnstack->s[stptr - 2])
717                                       || rpnstack->s[stptr - 2] ==
718                                       0.0) ? rpnstack->s[stptr] : rpnstack->
719                 s[stptr - 1];
720             stptr--;
721             stptr--;
722             break;
723         case OP_MIN:
724             stackunderflow(1);
725             if (isnan(rpnstack->s[stptr - 1]));
726             else if (isnan(rpnstack->s[stptr]))
727                 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
728             else if (rpnstack->s[stptr - 1] > rpnstack->s[stptr])
729                 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
730             stptr--;
731             break;
732         case OP_MAX:
733             stackunderflow(1);
734             if (isnan(rpnstack->s[stptr - 1]));
735             else if (isnan(rpnstack->s[stptr]))
736                 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
737             else if (rpnstack->s[stptr - 1] < rpnstack->s[stptr])
738                 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
739             stptr--;
740             break;
741         case OP_LIMIT:
742             stackunderflow(2);
743             if (isnan(rpnstack->s[stptr - 2]));
744             else if (isnan(rpnstack->s[stptr - 1]))
745                 rpnstack->s[stptr - 2] = rpnstack->s[stptr - 1];
746             else if (isnan(rpnstack->s[stptr]))
747                 rpnstack->s[stptr - 2] = rpnstack->s[stptr];
748             else if (rpnstack->s[stptr - 2] < rpnstack->s[stptr - 1])
749                 rpnstack->s[stptr - 2] = DNAN;
750             else if (rpnstack->s[stptr - 2] > rpnstack->s[stptr])
751                 rpnstack->s[stptr - 2] = DNAN;
752             stptr -= 2;
753             break;
754         case OP_UN:
755             stackunderflow(0);
756             rpnstack->s[stptr] = isnan(rpnstack->s[stptr]) ? 1.0 : 0.0;
757             break;
758         case OP_ISINF:
759             stackunderflow(0);
760             rpnstack->s[stptr] = isinf(rpnstack->s[stptr]) ? 1.0 : 0.0;
761             break;
762         case OP_SQRT:
763             stackunderflow(0);
764             rpnstack->s[stptr] = sqrt(rpnstack->s[stptr]);
765             break;
766         case OP_SORT:
767             stackunderflow(0);
768             {
769                 int       spn = (int) rpnstack->s[stptr--];
771                 stackunderflow(spn - 1);
772                 qsort(rpnstack->s + stptr - spn + 1, spn, sizeof(double),
773                       rpn_compare_double);
774             }
775             break;
776         case OP_REV:
777             stackunderflow(0);
778             {
779                 int       spn = (int) rpnstack->s[stptr--];
780                 double   *p, *q;
782                 stackunderflow(spn - 1);
784                 p = rpnstack->s + stptr - spn + 1;
785                 q = rpnstack->s + stptr;
786                 while (p < q) {
787                     double    x = *q;
789                     *q-- = *p;
790                     *p++ = x;
791                 }
792             }
793             break;
794         case OP_TREND:
795         case OP_TRENDNAN:
796             stackunderflow(1);
797             if ((rpi < 2) || (rpnp[rpi - 2].op != OP_VARIABLE)) {
798                 rrd_set_error("malformed trend arguments");
799                 return -1;
800             } else {
801                 time_t    dur = (time_t) rpnstack->s[stptr];
802                 time_t    step = (time_t) rpnp[rpi - 2].step;
804                 if (output_idx > (int) ceil((float) dur / (float) step)) {
805                     int       ignorenan = (rpnp[rpi].op == OP_TREND);
806                     double    accum = 0.0;
807                     int       i = 0;
808                     int       count = 0;
810                     do {
811                         double    val =
812                             rpnp[rpi - 2].data[rpnp[rpi - 2].ds_cnt * i--];
813                         if (ignorenan || !isnan(val)) {
814                             accum += val;
815                             ++count;
816                         }
818                         dur -= step;
819                     } while (dur > 0);
821                     rpnstack->s[--stptr] =
822                         (count == 0) ? DNAN : (accum / count);
823                 } else
824                     rpnstack->s[--stptr] = DNAN;
825             }
826             break;
827         case OP_AVG:
828             stackunderflow(0);
829             {
830                 int       i = (int) rpnstack->s[stptr--];
831                 double    sum = 0;
832                 int       count = 0;
834                 stackunderflow(i - 1);
835                 while (i > 0) {
836                     double    val = rpnstack->s[stptr--];
838                     i--;
839                     if (isnan(val)) {
840                         continue;
841                     }
842                     count++;
843                     sum += val;
844                 }
845                 /* now push the result back on stack */
846                 if (count > 0) {
847                     rpnstack->s[++stptr] = sum / count;
848                 } else {
849                     rpnstack->s[++stptr] = DNAN;
850                 }
851             }
852             break;
853         case OP_ABS:
854             stackunderflow(0);
855             rpnstack->s[stptr] = fabs(rpnstack->s[stptr]);
856             break;
857         case OP_END:
858             break;
859         }
860 #undef stackunderflow
861     }
862     if (stptr != 0) {
863         rrd_set_error("RPN final stack size != 1");
864         return -1;
865     }
867     output[output_idx] = rpnstack->s[0];
868     return 0;
871 /* figure out what the local timezone offset for any point in
872    time was. Return it in seconds */
873 int tzoffset(
874     time_t now)
876     int       gm_sec, gm_min, gm_hour, gm_yday, gm_year,
877         l_sec, l_min, l_hour, l_yday, l_year;
878     struct tm t;
879     int       off;
881     gmtime_r(&now, &t);
882     gm_sec = t.tm_sec;
883     gm_min = t.tm_min;
884     gm_hour = t.tm_hour;
885     gm_yday = t.tm_yday;
886     gm_year = t.tm_year;
887     localtime_r(&now, &t);
888     l_sec = t.tm_sec;
889     l_min = t.tm_min;
890     l_hour = t.tm_hour;
891     l_yday = t.tm_yday;
892     l_year = t.tm_year;
893     off =
894         (l_sec - gm_sec) + (l_min - gm_min) * 60 + (l_hour - gm_hour) * 3600;
895     if (l_yday > gm_yday || l_year > gm_year) {
896         off += 24 * 3600;
897     } else if (l_yday < gm_yday || l_year < gm_year) {
898         off -= 24 * 3600;
899     }
900     return off;