1    | /***************************************
2    |   $Revision: 1.5 $
3    | 
4    |   Error reporting (er) er_print.c - routines to print the currently registered
5    |                                     paths and filters in a syntax compliant 
6    | 				    to the one of the interpreter.
7    | 
8    |   Status: NOT REVUED, PARTLY TESTED
9    | 
10   |   Design and implementation by: Marek Bukowy
11   | 
12   |   ******************/ /******************
13   |   Copyright (c) 1999,2000,2001,2002                   RIPE NCC
14   |  
15   |   All Rights Reserved
16   |   
17   |   Permission to use, copy, modify, and distribute this software and its
18   |   documentation for any purpose and without fee is hereby granted,
19   |   provided that the above copyright notice appear in all copies and that
20   |   both that copyright notice and this permission notice appear in
21   |   supporting documentation, and that the name of the author not be
22   |   used in advertising or publicity pertaining to distribution of the
23   |   software without specific, written prior permission.
24   |   
25   |   THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
26   |   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
27   |   AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
28   |   DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
29   |   AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
30   |   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31   |   ***************************************/
32   | 
33   | #include "rip.h"
34   | 
35   | /**************** PRINTING PATHS ********************************************/
36   | static
37   | void er_print_format(int format, GString *g_reply )
38   | {
39   |   int i;
40   | 
41   |   for(i=0;  er_formarr[i].n != NULL; i++) {
42   |     if( format & er_formarr[i].v ) {
43   |       g_string_sprintfa(g_reply, "%s|",er_formarr[i].n);
44   |     }
45   |   }
46   |   /* cut the last "|" */
47   |   g_string_truncate(g_reply, (int) strlen(g_reply->str)-1);
48   | }
49   | 
50   | 
51   | static
52   | void er_print_one_path_descr(er_path_t *pathptr, GString *g_reply )
53   | {
54   |   er_path_descr_t *d = &(pathptr->descr);
55   | 
56   |   switch(pathptr->type) {
57   |   case  ER_PATH_NAME:
58   |     g_string_sprintfa(g_reply, "NAME %s%s", d->name.filename, 
59   | 		      d->name.date ? " DATE" : ""
60   | 		      );
61   |     break;
62   |   case  ER_PATH_SOCK:
63   |     g_string_sprintfa(g_reply, "SOCK %d", d->sock.fd );
64   | 
65   |     break;
66   | 
67   |   case  ER_PATH_EXEC:
68   |     g_string_sprintfa(g_reply, "EXEC ");
69   |     if( d->exec.usepath ) {
70   |       g_string_sprintfa(g_reply, "PATH ");
71   |     }
72   |     {
73   |       char **argv = d->exec.argv;
74   |       int len=0;
75   |   
76   |       if( argv != NULL ) {
77   | 	while( argv[len] != NULL ) {
78   | 	  g_string_sprintfa(g_reply, "%s ", argv[len]);
79   | 	  len++;
80   | 	}
81   |       }
82   |     }
83   |     break;
84   | 
85   |   default:
86   |     /* XXX other path descriptions missing */
87   |     break;
88   |   }
89   | }
90   | 
91   | static
92   | void er_print_aspmask(mask_t facmask, unsigned aspmask, GString *g_reply)
93   | {
94   |   int i = 31;
95   | 
96   |   while(i >= 0) {
97   |     if( aspmask & (1<<i) ) {
98   |       er_getaspsym(facmask, 1<<i, g_reply);
99   |       g_string_append(g_reply, "|");
100  |     }
101  |     
102  |     i--;
103  |   }
104  |   /* cut the last "|" */
105  |   g_string_truncate(g_reply, (int) strlen(g_reply->str)-1);
106  | }
107  | 
108  | static
109  | void er_print_facmask(mask_t facmask, GString *g_reply)
110  | {
111  |   int i = FAC_NONE;
112  |   
113  |   while( ++i != FAC_LAST ) {
114  |     if( MA_isset(facmask, er_fac_err[i].code) ) {
115  |       g_string_sprintfa(g_reply, "%s|", er_fac_err[i].name);
116  |     }
117  |   }
118  |   /* cut the last "|" */
119  |   g_string_truncate(g_reply, (int) strlen(g_reply->str)-1);
120  | 
121  | }
122  | 
123  | static
124  | void er_print_one_filter(er_filter_t *filtptr, GString *g_reply )
125  | { 
126  |   g_string_sprintfa(g_reply, "( FAC ");
127  |   er_print_facmask( filtptr->fac_mask, g_reply);
128  |   
129  |   if( filtptr->asp_mask != 0 ) {
130  |     g_string_sprintfa(g_reply, "  ASP ");
131  |     er_print_aspmask(  filtptr->fac_mask, filtptr->asp_mask, g_reply);
132  |   }
133  | 
134  |   g_string_sprintfa(g_reply, "  SEV %s-%s ",
135  | 		    er_getsevsym( filtptr->sev_min, ER_M_SEVCHAR),
136  | 		    er_getsevsym( filtptr->sev_max, ER_M_SEVCHAR)
137  | 		    );
138  |   if( filtptr->thr_id != 0 ) {
139  |     g_string_sprintfa(g_reply, "   THR %lu ", (long unsigned)filtptr->thr_id);
140  |   }
141  |   g_string_sprintfa(g_reply, " )" );
142  | }
143  | 
144  | static
145  | void er_print_one_path(er_path_t *pathptr, GString *g_reply )
146  | {
147  |   GList   *qitem;
148  |   int f=1;
149  |   
150  |   g_string_sprintfa(g_reply,"%s { ", pathptr->name );
151  |   g_string_sprintfa(g_reply," FORMAT ");
152  |   er_print_format(pathptr->format, g_reply ); 
153  |   g_string_sprintfa(g_reply,"  ");
154  | 
155  |   er_print_one_path_descr(pathptr, g_reply);
156  |   g_string_sprintfa(g_reply," }\n");
157  | 
158  |   for(qitem = g_list_first(pathptr->filters);
159  |       qitem != NULL;
160  |       qitem = g_list_next(qitem)) {
161  |     er_filter_t *filtptr = (er_filter_t *) qitem -> data;
162  |     
163  |     g_string_sprintfa(g_reply,"\t");
164  |     er_print_one_filter(filtptr, g_reply) ;
165  |     g_string_sprintfa(g_reply,"\n");
166  |     f++;
167  |   }
168  | 
169  | }
170  | 
171  | void er_print_paths(char **retbuf)
172  | {
173  |   GList   *qitem;
174  |   GString *g_reply = g_string_sized_new(2048); /* initial size */
175  | 
176  |   for( qitem = g_list_first(er_pathlist);
177  |        qitem != NULL;
178  |        qitem = g_list_next(qitem)) {
179  |     er_path_t *pathptr = qitem -> data;
180  | 
181  |     /*   g_string_sprintfa(g_reply, "path type %d (%s) with %d filters\n",
182  | 	   pathptr->type, er_pathtypes[pathptr->type],
183  | 	   g_list_length(pathptr->filters));
184  |     */
185  |     er_print_one_path(pathptr, g_reply);
186  | 
187  |   }
188  | 
189  |   *retbuf = g_reply->str;  
190  | 
191  |   g_string_free( g_reply, /* CONSTCOND */ FALSE);
192  | }