1    | /***************************************
2    |   $Revision: 1.16 $
3    | 
4    |   Radix tree (rx).  rx_print.c - functions to print a forest/tree/node
5    |                                  (mainly for debugging purposes)
6    | 
7    |   Status: NOT REVUED, TESTED, INCOMPLETE
8    | 
9    |   Design and implementation by: Marek Bukowy
10   | 
11   |   ******************/ /******************
12   |   Copyright (c) 1999,2000,2001,2002               RIPE NCC
13   |  
14   |   All Rights Reserved
15   |   
16   |   Permission to use, copy, modify, and distribute this software and its
17   |   documentation for any purpose and without fee is hereby granted,
18   |   provided that the above copyright notice appear in all copies and that
19   |   both that copyright notice and this permission notice appear in
20   |   supporting documentation, and that the name of the author not be
21   |   used in advertising or publicity pertaining to distribution of the
22   |   software without specific, written prior permission.
23   |   
24   |   THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
25   |   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
26   |   AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
27   |   DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
28   |   AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
29   |   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30   |   ***************************************/
31   | 
32   | #define RX_IMPL
33   | #define RX_IMPL_PRINT
34   | #include "rip.h"
35   | 
36   | const char *
37   | RX_text_srch_mode(rx_srch_mt mode) 
38   | {
39   |   return rx_srch_mode_text[mode];
40   | }
41   | 
42   | er_ret_t
43   | rx_walk_hook_printnode(rx_node_t *node, int level, int nodecounter, void *con)
44   | {
45   |   sk_conn_st *condat = (sk_conn_st *) con;
46   |   char line[200]="", buf[1024];
47   | 
48   |   int i;
49   | 
50   |   /* indent*/
51   |   for(i=0;i<level;i++) strcat(line,"  ");
52   | 
53   |   sprintf( line+strlen(line) ,"** level %d ** ", level);
54   |   /* @ %p; parent %p, child[0]=%p, child[1]=%p\n", */
55   |   /*  node, node->parent_ptr, node->child_ptr[0], node->child_ptr[1] );*/
56   | 
57   |   rx_nod_print(node, buf, 1024);
58   |   
59   |   SK_cd_puts(condat, line);
60   |   SK_cd_puts(condat, buf);
61   |   SK_cd_puts(condat, "\n");
62   |   return RX_OK;
63   | }
64   | 
65   | /***************************************************************************/
66   | 
67   | er_ret_t
68   | rx_tree_print( sk_conn_st *condat, rx_tree_t *tree ) 
69   | {
70   | int cnt;
71   | er_ret_t err;
72   | char line[200]="";
73   | 
74   |  if( tree->top_ptr != NULL ) {
75   |    cnt = rx_walk_tree(tree->top_ptr, rx_walk_hook_printnode, 
76   | 		      RX_WALK_CNTGLU,  /* print also glue nodes*/
77   | 		      255, 0, 0, condat, &err);
78   |    sprintf(line,"Traversed %d nodes\n", cnt);
79   |    SK_cd_puts(condat,line);
80   |  }
81   |  else {
82   |    SK_cd_puts(condat,"The tree is empty!\n");
83   |  }
84   | 
85   |  return err;
86   | }
87   | 
88   | 
89   | /***************************************************************************/
90   | 
91   | #if 0
92   | 
93   | This function is never used, and may potentially cause a buffer overflow.
94   | If you need it, check that %s and add it back in.   Shane
95   | 
96   | void
97   | rx_space_printone(void *voptr, void *condat)
98   | {
99   |   rx_tree_t *ptr = voptr;
100  |   char aout[1024];
101  |   char prstr[IP_PREFSTR_MAX];
102  |   
103  |   *aout=0;
104  | 
105  |   sprintf(aout+strlen(aout), "%50s:%d\n", "space",    ptr->space );
106  |   sprintf(aout+strlen(aout), "%50s:%d\n", "family",   ptr->family );
107  |   sprintf(aout+strlen(aout), "%50s:%d\n", "subtrees", ptr->subtrees);
108  |   sprintf(aout+strlen(aout), "%50s:%d\n", "mem_mode", ptr->mem_mode);
109  |   sprintf(aout+strlen(aout), "%50s:%d\n", "num_nodes",ptr->num_nodes);
110  |   sprintf(aout+strlen(aout), "%50s:%08x\n", "top_ptr", (int) ptr->top_ptr);
111  |   sprintf(aout+strlen(aout), "%50s:%d\n", "maxbits",  ptr->maxbits);
112  | 
113  |   if( IP_pref_b2a(  &(ptr->prefix), prstr, IP_PREFSTR_MAX) != IP_OK )
114  |     die; /* program error.*/
115  | 
116  |   sprintf(aout+strlen(aout), "%50s:%s\n", "prefix", prstr);
117  |   SK_cd_puts( (sk_conn_st *)condat,aout);
118  | }
119  | 
120  | #endif /* 0 */
121  | 
122  | /***************************************************************************/
123  | 
124  | void
125  | rx_nod_print( rx_node_t *node, char *buf, unsigned maxchar )
126  | {
127  |   char pref[IP_PREFSTR_MAX];
128  |   
129  |   if( IP_pref_b2a(  &(node->prefix), pref, IP_PREFSTR_MAX) != IP_OK ) {
130  |     die;
131  |   }
132  |   
133  |   snprintf(buf, maxchar, "%s%s", 
134  | 	   ( node->glue ) ? "++glue++" : "", pref);
135  | }
136  | /***************************************************************************/
137  | 
138  | void 
139  | rx_stk_print( rx_nodcpy_t   stack[],         /* stack==array of node_copies*/
140  | 	      int           stackdepth )
141  | {
142  |   int i;
143  |   rx_node_t *node;
144  |   char buf[1024];
145  | 
146  |   ER_dbg_va(FAC_RX, ASP_RX_STKBLD_DET, 
147  | 	    "stack dump: %d elements", stackdepth);
148  | 
149  |   for(i = 0; i < stackdepth; i++) {
150  |     node = & stack[i].cpy;
151  | 
152  |     rx_nod_print(node, buf, 1024);
153  | 
154  |     ER_dbg_va(FAC_RX, ASP_RX_STKBLD_DET, "position %d: %s", i, buf);
155  |   }
156  | }