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

   1 
   2 #include "../test_.c"   /* unit test template */
   3 
   4 
   5 #include <defs.h> /* A_I6 */
   6 
   7 // #define WITH_SQL
   8 
   9 #include <rxroutines.h>
  10 #include <iproutines.h>
  11 #include <stdio.h>
  12 #include <memwrap.h>
  13 #include <numconv.h>
  14 
  15 #include <properties.h>
  16 #include <constants.h>
  17 
  18 #ifndef ASCII_INPUT 
  19 #include "mysql_driver.h"
  20 #endif
  21 
  22 
  23 /*
  24  * define ASCII_INPUT for old style database files,
  25  * or leave it undefined for (my)sql tables
  26  */
  27 /*#define ASCII_INPUT */
  28 #define MY_TREE_ATTR A_RT
  29 #define LOAD_QUERY QUERY_RT
  30 #define RIPE_REG (17)
  31 #define MY_SPACE IP_V4
  32 
  33 #define STR_L 1023
  34 
  35 
  36 int Query_logging=0;
  37 char *Query_logfile="stdout";
  38 
  39 #if 0
  40 int CO_get_query_logging() {
     /* [<][>][^][v][top][bottom][index][help] */
  41   return Query_logging;
  42 } /* CO_get_query_logging() */
  43 
  44 const char *CO_get_query_logfile() {
     /* [<][>][^][v][top][bottom][index][help] */
  45   return Query_logfile;
  46 } /* CO_get_query_logfile() */
  47 #endif
  48 
  49 void check(rx_tree_t  *mytree) 
     /* [<][>][^][v][top][bottom][index][help] */
  50 {
  51   int cnt;
  52   er_ret_t err; 
  53   rx_treecheck_t errorfound;
  54   
  55   if( mytree->num_nodes > 0 ) {
  56     if( (err=RX_treecheck(mytree, 0, &errorfound)) != RX_OK ) {
  57       fprintf(stderr, "Nope! %d returned \n", err);
  58       die;
  59     } 
  60     else {
  61       // fprintf(stderr, "OK\n");
  62     }
  63   }
  64 } 
  65 
  66 int main(int argc, char **argv)
     /* [<][>][^][v][top][bottom][index][help] */
  67 {
  68   ip_prefix_t mypref;
  69   rx_tree_t  *mytree;
  70   ip_prefix_t *querypref;
  71   GList    *datlist, *qitem, *preflist;
  72   int maxline=0; 
  73   char buf[1024];
  74   er_path_t erlogstr;
  75   
  76   int i, par_a, par_b=0, search_mode;
  77   char pref[IP_PREFSTR_MAX];
  78   er_ret_t err;
  79   int cnt;
  80   char *key, *chp;
  81   
  82   sk_conn_st condat={ 1, 0,0,0,0, {0},{0}, "" }; 
  83 
  84   
  85   ER_init(argc, argv);
  86  
  87   PR_load("RIP.properties");
  88   fprintf(stderr,"%s\n", PR_to_string() );
  89   CO_set();
  90   fprintf(stderr,"%s\n", CO_to_string() );
  91 
  92   erlogstr.fdes = stderr;
  93   erlogstr.asp  = 0; /*xffffffff; / *ASP_RX_NODCRE_BOT;*/
  94   erlogstr.sev  = ER_SEV_W;
  95   erlogstr.mode = ER_M_SEVCHAR | ER_M_TEXTLONG ;
  96 
  97   ER_setpath(& erlogstr);  
  98 
  99   
 100   
 101 #ifdef ASCII_INPUT
 102 
 103   if( argc<3 ) {
 104     fputs("arguments:  number_of_lines  filename", stderr);
 105     die;
 106   } else {
 107     dieif( rp_init_attr_tree( RIPE_REG, MY_TREE_ATTR ) != RP_OK );
 108 
 109     maxline = atoi(argv[1]);
 110     RP_asc_load(argv[2], maxline, RX_OPER_CRE, RIPE_REG);
 111   }
 112 #else
 113   if( argc!=2 ) {
 114     fputs("noarguments: number_of_objects", stderr);
 115     die;
 116   } else {
 117     dieif( RP_init_trees( RIPE_REG ) != RP_OK );
 118 
 119     maxline = atoi(argv[1]);
 120     /* RP_sql_load_attr_space(maxline, RX_OPER_CRE, LOAD_QUERY, 
 121        MY_TREE_ATTR, MY_SPACE, RIPE_REG);*/
 122     dieif( RP_sql_load_reg(RIPE_REG) != RP_OK );
 123   }
 124 #endif
 125 
 126     exit(0);
 127 
 128   //rx_space_list(&condat);
 129   // consistency check
 130 
 131   // change the loglevel
 132   erlogstr.asp  = ASP_RX_SRCH_BOT | ASP_RX_STKBLD_BOT;
 133   ER_setpath(& erlogstr); 
 134 
 135   // now delete.
 136   /* 
 137   fseek(fp,0,SEEK_SET);
 138   put_inetnums(mytree, fp, maxline, RX_OPER_DEL );
 139   // print
 140   rx_space_list(&condat);
 141   // consistency check
 142   check(mytree); 
 143   */
 144   // exit(0);
 145   //rx_tree_print( mytree );
 146   
 147   /*  check(mytree);*/
 148 
 149   // search
 150   while(!feof(stdin)) {
 151     printf("\nsearch> ");
 152     fgets(buf, 128, stdin);
 153 
 154     par_a=1;
 155     key = buf;
 156     
 157     
 158     // chop \n
 159     if( (chp = index(buf,'\n')) == NULL ) { //EOF
 160       break;
 161     }
 162     else {
 163       * chp = '\0';
 164     }
 165 
 166     if( *key == '\0' ) {
 167       continue;
 168     }
 169 
 170     if( strcmp(key, "exit") == 0 ) {
 171       exit(0);
 172     }
 173     
 174     if( strncmp(key, "show ", 5) == 0 ) {
 175       int depth;
 176       extern er_ret_t rx_walk_hook_printnode();
 177       
 178       key+=5;
 179 
 180       /* tree inspection // show tree <n>,  default n = 255 */
 181       if( strncmp(key, "tree", 4) == 0 ) {
 182         
 183         ip_space_t space = (RP_attr2spc(MY_TREE_ATTR, IP_V4)) 
 184                             ? IP_V4 : IP_V6;
 185         
 186         key+=4;
 187         if( sscanf(key, "%d", &depth) < 1 ) {
 188           depth = 255;
 189         }
 190         
 191         dieif(!NOERR(RP_tree_get(&mytree, RIPE_REG, space, MY_TREE_ATTR)));
 192         
 193         rx_walk_tree(mytree->top_ptr, rx_walk_hook_printnode, 
 194                      RX_WALK_CNTGLU,  // print also glue nodes
 195                      depth, 0, 0, &condat, &err);
 196         
 197         /* err will be handled below */
 198         printf("got status %x\n", err);
 199         ER_perror(FAC_RX, err);
 200         
 201         continue;
 202       }
 203     }
 204     else {
 205 
 206       /* ordinary query */
 207 
 208       /* take switches */
 209       if( *key=='-' ) {
 210         switch( *(++key) ) {
 211         case 'd':
 212           search_mode = RX_SRCH_EXLESS;     // default
 213           break;
 214         case 'e':
 215           search_mode = RX_SRCH_EXACT;
 216           break;
 217         case 'm':
 218           search_mode = RX_SRCH_MORE;
 219           key++;
 220           if(  ! isdigit( *key ) ) {
 221             puts("invalid syntax");
 222             continue;
 223           } else {
 224             sscanf( key, "%d", &par_a);
 225           }
 226           break;
 227         case 'l':
 228           search_mode = RX_SRCH_LESS;
 229           key++;
 230           if(  ! isdigit( *key ) ) {
 231             puts("invalid syntax");
 232             continue;
 233           } else {
 234             sscanf( key, "%d", &par_a);
 235           }
 236           break;
 237         case 'r':
 238           search_mode = RX_SRCH_RANG;
 239           key++;
 240           if( ! isdigit( *key ) ) {
 241             puts("invalid syntax");
 242             continue;
 243           }
 244           else {
 245             sscanf( key, "%d", &par_a);
 246           }
 247           break;
 248         case 's':
 249           search_mode = RX_SRCH_DBLS;
 250           key++;
 251           if(  ! isdigit( *key ) ) {
 252             puts("invalid syntax");
 253             continue;
 254           } else {
 255             sscanf( key, "%d", &par_a);
 256           }
 257           break;
 258         }
 259         
 260         if( (key=index(buf, ' ')) == NULL ) {
 261           // empty query
 262           continue;
 263         }
 264         else {
 265           key++;
 266         }
 267         
 268       } 
 269       else {
 270         search_mode = RX_SRCH_EXLESS;          // default search mode
 271       }
 272       
 273       // zero the datlist (the one for answers). 
 274       datlist = NULL;
 275       
 276       err = RP_asc_search(search_mode,  par_a, par_b, key, 
 277                           RIPE_REG, MY_TREE_ATTR, &datlist, RX_ANS_ALL);
 278     }
 279 
 280     printf("got status %x\n", err);
 281     
 282     ER_perror(FAC_RP, err);
 283 
 284     if( NOERR(err) ) {
 285     
 286 
 287       puts("### answers:");
 288       
 289       for(i=0; i<g_list_length(datlist); i++) {
 290         rx_datcpy_t *datcpy = g_list_nth_data(datlist, i);
 291         char rangstr[IP_RANGSTR_MAX];
 292         
 293         if( RP_attr2fam(MY_TREE_ATTR) == RX_FAM_IN ) {
 294           if(IP_rang_b2a( &(datcpy->leafcpy.iprange), rangstr, IP_RANGSTR_MAX) 
 295              != IP_OK ) {
 296             die;
 297           }
 298           if( datcpy->leafcpy.composed ) {
 299             strcat(rangstr, "  \tc");
 300           }
 301         } else {
 302           rangstr[0] = '\0';
 303         }
 304 
 305         printf("\n%d: [%u]\t%s\n%s",i, datcpy->leafcpy.data_key,
 306                rangstr, (char *) datcpy->leafcpy.data_ptr );
 307         wr_free(datcpy->leafcpy.data_ptr );
 308       }
 309       
 310       g_list_foreach(datlist, rx_free_list_element, NULL);
 311       g_list_free(datlist);
 312       
 313     }
 314     else {
 315       switch( err ) {
 316       case IP_INVARG:
 317         fputs("conversion error", stderr);
 318         break;
 319         
 320       case RX_NOTREE:
 321         fputs("this object type not loaded", stderr);
 322         break;
 323       default:
 324         die;
 325       }
 326     }
 327   }
 328   return 0;
 329 }
 330  

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