tests/rx/test_rx.c

/* [<][>]
[^][v][top][bottom][index][help] */

FUNCTIONS

This source file includes following functions.
  1. CO_get_query_logging
  2. CO_get_query_logfile
  3. check
  4. main


#include "../test_.c"   /* unit test template */


#include <defs.h> /* A_I6 */

// #define WITH_SQL

#include <rxroutines.h>
#include <iproutines.h>
#include <stdio.h>
#include <memwrap.h>
#include <numconv.h>

#include <properties.h>
#include <constants.h>

#ifndef ASCII_INPUT 
#include "mysql_driver.h"
#endif


/*
 * define ASCII_INPUT for old style database files,
 * or leave it undefined for (my)sql tables
 */
/*#define ASCII_INPUT */
#define MY_TREE_ATTR A_RT
#define LOAD_QUERY QUERY_RT
#define RIPE_REG (17)
#define MY_SPACE IP_V4

#define STR_L 1023


int Query_logging=0;
char *Query_logfile="stdout";

#if 0
int CO_get_query_logging() {
/* [<][>][^][v][top][bottom][index][help] */
  return Query_logging;
} /* CO_get_query_logging() */

const char *CO_get_query_logfile() {
/* [<][>][^][v][top][bottom][index][help] */
  return Query_logfile;
} /* CO_get_query_logfile() */
#endif

void check(rx_tree_t  *mytree) 
/* [<][>][^][v][top][bottom][index][help] */
{
  int cnt;
  er_ret_t err; 
  rx_treecheck_t errorfound;
  
  if( mytree->num_nodes > 0 ) {
    if( (err=RX_treecheck(mytree, 0, &errorfound)) != RX_OK ) {
      fprintf(stderr, "Nope! %d returned \n", err);
      die;
    } 
    else {
      // fprintf(stderr, "OK\n");
    }
  }
} 

int main(int argc, char **argv)
/* [<][>][^][v][top][bottom][index][help] */
{
  ip_prefix_t mypref;
  rx_tree_t  *mytree;
  ip_prefix_t *querypref;
  GList    *datlist, *qitem, *preflist;
  int maxline=0; 
  char buf[1024];
  er_path_t erlogstr;
  
  int i, par_a, par_b=0, search_mode;
  char pref[IP_PREFSTR_MAX];
  er_ret_t err;
  int cnt;
  char *key, *chp;
  
  sk_conn_st condat={ 1, 0,0,0,0, {0},{0}, "" }; 

  
  ER_init(argc, argv);
 
  PR_load("RIP.properties");
  fprintf(stderr,"%s\n", PR_to_string() );
  CO_set();
  fprintf(stderr,"%s\n", CO_to_string() );

  erlogstr.fdes = stderr;
  erlogstr.asp  = 0; /*xffffffff; / *ASP_RX_NODCRE_BOT;*/
  erlogstr.sev  = ER_SEV_W;
  erlogstr.mode = ER_M_SEVCHAR | ER_M_TEXTLONG ;

  ER_setpath(& erlogstr);  

  
  
#ifdef ASCII_INPUT

  if( argc<3 ) {
    fputs("arguments:  number_of_lines  filename", stderr);
    die;
  } else {
    dieif( rp_init_attr_tree( RIPE_REG, MY_TREE_ATTR ) != RP_OK );

    maxline = atoi(argv[1]);
    RP_asc_load(argv[2], maxline, RX_OPER_CRE, RIPE_REG);
  }
#else
  if( argc!=2 ) {
    fputs("noarguments: number_of_objects", stderr);
    die;
  } else {
    dieif( RP_init_trees( RIPE_REG ) != RP_OK );

    maxline = atoi(argv[1]);
    /* RP_sql_load_attr_space(maxline, RX_OPER_CRE, LOAD_QUERY, 
       MY_TREE_ATTR, MY_SPACE, RIPE_REG);*/
    dieif( RP_sql_load_reg(RIPE_REG) != RP_OK );
  }
#endif

    exit(0);

  //rx_space_list(&condat);
  // consistency check

  // change the loglevel
  erlogstr.asp  = ASP_RX_SRCH_BOT | ASP_RX_STKBLD_BOT;
  ER_setpath(& erlogstr); 

  // now delete.
  /* 
  fseek(fp,0,SEEK_SET);
  put_inetnums(mytree, fp, maxline, RX_OPER_DEL );
  // print
  rx_space_list(&condat);
  // consistency check
  check(mytree); 
  */
  // exit(0);
  //rx_tree_print( mytree );
  
  /*  check(mytree);*/

  // search
  while(!feof(stdin)) {
    printf("\nsearch> ");
    fgets(buf, 128, stdin);

    par_a=1;
    key = buf;
    
    
    // chop \n
    if( (chp = index(buf,'\n')) == NULL ) { //EOF
      break;
    }
    else {
      * chp = '\0';
    }

    if( *key == '\0' ) {
      continue;
    }

    if( strcmp(key, "exit") == 0 ) {
      exit(0);
    }
    
    if( strncmp(key, "show ", 5) == 0 ) {
      int depth;
      extern er_ret_t rx_walk_hook_printnode();
      
      key+=5;

      /* tree inspection // show tree <n>,  default n = 255 */
      if( strncmp(key, "tree", 4) == 0 ) {
        
        ip_space_t space = (RP_attr2spc(MY_TREE_ATTR, IP_V4)) 
                            ? IP_V4 : IP_V6;
        
        key+=4;
        if( sscanf(key, "%d", &depth) < 1 ) {
          depth = 255;
        }
        
        dieif(!NOERR(RP_tree_get(&mytree, RIPE_REG, space, MY_TREE_ATTR)));
        
        rx_walk_tree(mytree->top_ptr, rx_walk_hook_printnode, 
                     RX_WALK_CNTGLU,  // print also glue nodes
                     depth, 0, 0, &condat, &err);
        
        /* err will be handled below */
        printf("got status %x\n", err);
        ER_perror(FAC_RX, err);
        
        continue;
      }
    }
    else {

      /* ordinary query */

      /* take switches */
      if( *key=='-' ) {
        switch( *(++key) ) {
        case 'd':
          search_mode = RX_SRCH_EXLESS;     // default
          break;
        case 'e':
          search_mode = RX_SRCH_EXACT;
          break;
        case 'm':
          search_mode = RX_SRCH_MORE;
          key++;
          if(  ! isdigit( *key ) ) {
            puts("invalid syntax");
            continue;
          } else {
            sscanf( key, "%d", &par_a);
          }
          break;
        case 'l':
          search_mode = RX_SRCH_LESS;
          key++;
          if(  ! isdigit( *key ) ) {
            puts("invalid syntax");
            continue;
          } else {
            sscanf( key, "%d", &par_a);
          }
          break;
        case 'r':
          search_mode = RX_SRCH_RANG;
          key++;
          if( ! isdigit( *key ) ) {
            puts("invalid syntax");
            continue;
          }
          else {
            sscanf( key, "%d", &par_a);
          }
          break;
        case 's':
          search_mode = RX_SRCH_DBLS;
          key++;
          if(  ! isdigit( *key ) ) {
            puts("invalid syntax");
            continue;
          } else {
            sscanf( key, "%d", &par_a);
          }
          break;
        }
        
        if( (key=index(buf, ' ')) == NULL ) {
          // empty query
          continue;
        }
        else {
          key++;
        }
        
      } 
      else {
        search_mode = RX_SRCH_EXLESS;          // default search mode
      }
      
      // zero the datlist (the one for answers). 
      datlist = NULL;
      
      err = RP_asc_search(search_mode,  par_a, par_b, key, 
                          RIPE_REG, MY_TREE_ATTR, &datlist, RX_ANS_ALL);
    }

    printf("got status %x\n", err);
    
    ER_perror(FAC_RP, err);

    if( NOERR(err) ) {
    

      puts("### answers:");
      
      for(i=0; i<g_list_length(datlist); i++) {
        rx_datcpy_t *datcpy = g_list_nth_data(datlist, i);
        char rangstr[IP_RANGSTR_MAX];
        
        if( RP_attr2fam(MY_TREE_ATTR) == RX_FAM_IN ) {
          if(IP_rang_b2a( &(datcpy->leafcpy.iprange), rangstr, IP_RANGSTR_MAX) 
             != IP_OK ) {
            die;
          }
          if( datcpy->leafcpy.composed ) {
            strcat(rangstr, "  \tc");
          }
        } else {
          rangstr[0] = '\0';
        }

        printf("\n%d: [%u]\t%s\n%s",i, datcpy->leafcpy.data_key,
               rangstr, (char *) datcpy->leafcpy.data_ptr );
        wr_free(datcpy->leafcpy.data_ptr );
      }
      
      g_list_foreach(datlist, rx_free_list_element, NULL);
      g_list_free(datlist);
      
    }
    else {
      switch( err ) {
      case IP_INVARG:
        fputs("conversion error", stderr);
        break;
        
      case RX_NOTREE:
        fputs("this object type not loaded", stderr);
        break;
      default:
        die;
      }
    }
  }
  return 0;
}
 

/* [<][>][^][v][top][bottom][index][help] */