The expression's head was first checking for LT, and then for LTIME,
[rrdtool.git] / src / rrd_rpncalc.c
index 8e64d8b..94796d1 100644 (file)
@@ -153,6 +153,7 @@ void rpn_compact2str(rpn_cdefds_t *rpnc,ds_def_t *ds_def,char **str)
          add_op(OP_NOW,NOW)
          add_op(OP_LTIME,LTIME)
          add_op(OP_TIME,TIME)
+         add_op(OP_ATAN,ATAN)
 
 #undef add_op
               }
@@ -196,10 +197,10 @@ void parseCDEF_DS(char *def,rrd_t *rrd, int ds_idx)
      * occur too often. */
     for (i = 0; rpnp[i].op != OP_END; i++) {
         if (rpnp[i].op == OP_TIME || rpnp[i].op == OP_LTIME || 
-            rpnp[i].op == OP_PREV)
+            rpnp[i].op == OP_PREV || rpnp[i].op == OP_COUNT)
         {
             rrd_set_error(
-                "operators time, ltime and prev not supported with DS COMPUTE");
+                "operators time, ltime, prev and count not supported with DS COMPUTE");
             free(rpnp);
             return;
         }
@@ -222,7 +223,7 @@ void parseCDEF_DS(char *def,rrd_t *rrd, int ds_idx)
  */
 long lookup_DS(void *rrd_vptr,char *ds_name)
 {
-    int i;
+    unsigned int i;
     rrd_t *rrd; 
     
     rrd = (rrd_t *) rrd_vptr;
@@ -299,6 +300,7 @@ rpn_parse(void *key_hash,char *expr,long (*lookup)(void *,char*)){
        match_op(OP_DUP,DUP)
        match_op(OP_EXC,EXC)
        match_op(OP_POP,POP)
+       match_op(OP_LTIME,LTIME)
        match_op(OP_LT,LT)
        match_op(OP_LE,LE)
        match_op(OP_GT,GT)
@@ -313,13 +315,14 @@ rpn_parse(void *key_hash,char *expr,long (*lookup)(void *,char*)){
        match_op(OP_UN,UN)
        match_op(OP_NEGINF,NEGINF)
        match_op(OP_NE,NE)
+       match_op(OP_COUNT,COUNT)
          match_op_param(OP_PREV_OTHER,PREV)
        match_op(OP_PREV,PREV)
        match_op(OP_INF,INF)
        match_op(OP_ISINF,ISINF)
        match_op(OP_NOW,NOW)
-       match_op(OP_LTIME,LTIME)
        match_op(OP_TIME,TIME)
+       match_op(OP_ATAN,ATAN)
 
 #undef match_op
 
@@ -428,15 +431,18 @@ rpn_calc(rpnp_t *rpnp, rpnstack_t *rpnstack, long data_idx,
                    }
                }
                break;
+           case OP_COUNT:
+               rpnstack -> s[++stptr] = (output_idx+1); /* Note: Counter starts at 1 */
+               break;
            case OP_PREV:
-               if ((output_idx-1) <= 0) {
+               if ((output_idx) <= 0) {
                    rpnstack -> s[++stptr] = DNAN;
                } else {
                    rpnstack -> s[++stptr] = output[output_idx-1];
                }
                break;
        case OP_PREV_OTHER:
-         if ((output_idx-1) <= 0) {
+         if ((output_idx) <= 0) {
                rpnstack -> s[++stptr] = DNAN;
          } else {
                rpnstack -> s[++stptr] = rpnp[rpnp[rpi].ptr].data[output_idx-1];
@@ -497,6 +503,10 @@ rpn_calc(rpnp_t *rpnp, rpnstack_t *rpnstack, long data_idx,
                stackunderflow(0);
                rpnstack -> s[stptr] = sin(rpnstack -> s[stptr]);
                break;
+           case OP_ATAN:
+               stackunderflow(0);
+               rpnstack -> s[stptr] = atan(rpnstack -> s[stptr]);
+               break;
            case OP_COS:
                stackunderflow(0);
                rpnstack -> s[stptr] = cos(rpnstack -> s[stptr]);
@@ -670,20 +680,20 @@ int
 tzoffset( time_t now ){
     int gm_sec, gm_min, gm_hour, gm_yday, gm_year,
         l_sec, l_min, l_hour, l_yday, l_year;
-    struct tm *t;
+    struct tm t;
     int off;
-    t = gmtime(&now);
-    gm_sec = t->tm_sec;
-    gm_min = t->tm_min;
-    gm_hour = t->tm_hour;
-    gm_yday = t->tm_yday;
-    gm_year = t->tm_year;
-    t = localtime(&now);
-    l_sec = t->tm_sec;
-    l_min = t->tm_min;
-    l_hour = t->tm_hour;
-    l_yday = t->tm_yday;
-    l_year = t->tm_year;
+    gmtime_r(&now, &t);
+    gm_sec = t.tm_sec;
+    gm_min = t.tm_min;
+    gm_hour = t.tm_hour;
+    gm_yday = t.tm_yday;
+    gm_year = t.tm_year;
+    localtime_r(&now, &t);
+    l_sec = t.tm_sec;
+    l_min = t.tm_min;
+    l_hour = t.tm_hour;
+    l_yday = t.tm_yday;
+    l_year = t.tm_year;
     off = (l_sec-gm_sec)+(l_min-gm_min)*60+(l_hour-gm_hour)*3600; 
     if ( l_yday > gm_yday || l_year > gm_year){
         off += 24*3600;