

#include<stdio.h>
#include<netinet/in.h>
#include<string.h>
#include<stdlib.h>
#include<pthread.h>

#include"sitar.h"
#include"sitar_scan.h"
#include<almemsys/almemsys.h>
#include"jpreturn.h"





// char * cal_oid(char *, char *, int *, int *, char *, char *, int *, int);


char * cal_oid(char * oid, char * backdata, int * stat, int * ret, char * port, char * host, int *chk, int timeout)
{ 
int max, damax, co, count, sta, sto, lim, kaz;
char calchar = '+';
char sw[1024];
char dd[1024];
char * swap = sw;
char * da = dd;

memset(swap, 0x00, 1024);
memset(da, 0x00, 1024);

co = 0;
sta = 0;
sto = 0;
lim = 0;
kaz = 0;
damax = 0;

max = strlen(oid);
* stat = 0;
* ret = 0;


if(5 > max || 1024 <= max){
   * stat = -1;
   backdata = safe_sprintf(backdata, BUF_MAX,"%s\n", SNMP_1);
   return backdata;
   }

co = char_count(oid, '[');

if(0 < (co % 2)){
   * stat = -1;
   backdata = safe_sprintf(backdata, BUF_MAX,"%s\n", SNMP_2);
   return backdata;
   }


// 先頭にCALが含まれる
if(0 == memcmp(oid, "cal", 3)){
   for(count = 0; count < co; count++){

      delim_count(oid, '[', count, &sta, &sto);

      memcount_cpm(da, oid, sta, sto, max);
      damax = (int)strlen(da);
      if(damax > 0){
         if(1 == damax && 0 == ch_en(da[0])){
            calchar = da[0];
            backdata = safe_sprintf(backdata, BUF_MAX,"%s (%c)\n", SNMP_3, calchar);
            #ifdef SNMP_DEBUG
            printf("SNMP_DEBUG: cal_oid(1): +-*/Da = %c\n", calchar);
            #endif
            }
         else{
            lim = char_count(da, '.');
            if(lim == 0 && 0 < (int)strlen(da) && 0 == ch_int(da)){
               kaz = strtol(da, (char **)NULL, 10);
               backdata = safe_sprintf(backdata, BUF_MAX,"%s (%d)\n", SNMP_22, kaz);
               #ifdef SNMP_DEBUG
               printf("SNMP_DEBUG: cal_oid(2): 1234Da = %d\n", kaz);
               #endif
               * ret = cal4(* ret, kaz, calchar);
               kaz = 0;
               }
            else{
               if(0 != memcmp(da, "cal", 3)){
                  backdata = snmp_test(backdata, port, host, da, stat, 2, chk, timeout);
                  #ifdef SNMP_DEBUG
                  printf("SNMP_DEBUG: cal_oid(3): M->OID Da = %s %d %s %d\n", da, *chk, backdata, *stat);
                  #endif
                  if(* stat >= 0 && * chk < 4){
                     kaz = strtol(backdata, (char **)NULL, 10);
                     backdata = safe_sprintf(backdata, BUF_MAX,
                     "%s OID (%s) (%d)\n", SNMP_7, da, kaz);
                     * ret = cal4(* ret, kaz, calchar);
                     kaz = 0;
                     }
                  else{
                     * stat = -1;
                     * ret = 0;
                     #ifdef SNMP_DEBUG
                     printf("SNMP_DEBUG: cal_oid(4): <%s> %s OID<%s>\n", backdata, SNMP_5, da);
                     #endif
                     self_memcpy(swap, backdata, 1024);
                     backdata = safe_sprintf(backdata, BUF_MAX, "%s OID<%s>\n", swap, da);
                     return backdata;
                     }
                  }
               else{
                  #ifdef SNMP_DEBUG
                  printf("SNMP_DEBUG: cal_oid(5): head string is 'cal' Da = %s\n", da);
                  #endif
                  }
              }
            lim = 0;
            }
         }
      else{
         fprintf(stderr,"cal_oid(): Da size = 0\n");
         }      
      }
   }

// 先頭にCALが含まれる
else{ 
   lim = char_count(oid, '.');
   if(lim == 0 && 0 < (int)strlen(oid) && 0 == ch_int(oid)){
      kaz = strtol(oid, (char **)NULL, 10);
      backdata = safe_sprintf(backdata, BUF_MAX,
      "%s (%s) stat(%d)\n", SNMP_22, oid, kaz);
      * ret = cal4(* ret, kaz, calchar);
      kaz = 0;
      }
   else{
      backdata = snmp_test(backdata, port, host, oid, stat, 2, chk, timeout);
      #ifdef SNMP_DEBUG
      printf("SNMP_DEBUG: cal_oid(6): S->OID Da = %s %d %s\n", oid, * stat, backdata);
      #endif
      if(* stat >= 0 && * chk < 4){
         kaz = strtol(backdata, (char **)NULL, 10);
         backdata = safe_sprintf(backdata, BUF_MAX,
         "%s OID (%s) (%d)\n", SNMP_7, oid, kaz);
         * ret = cal4(* ret, kaz, calchar);
         kaz = 0;
         }
      else{
         * stat = -1;
         * ret = 0;
         #ifdef SNMP_DEBUG
         printf("SNMP_DEBUG: cal_oid(7): <%s> %s OID<%s> stat<%d>\n", backdata, SNMP_5, oid, * stat);
         #endif
         self_memcpy(swap, backdata, 1024);
         backdata = safe_sprintf(backdata, BUF_MAX, "%s OID<%s> stat<%d>\n", swap, oid, * stat);
         return backdata;
         }
      }
   }

if(* stat >= 0){
   backdata = safe_sprintf(backdata, BUF_MAX,
   "%s ret (%d)\n", SNMP_9, * ret);
   }


return backdata;
}



