LP1779158 Angular7 and ng-lint updates
[Evergreen.git] / Open-ILS / src / c-apps / dump_idl.c
1 /*
2 *  C Implementation: dump_idl
3 *
4 * Description: 
5 *
6 *
7 * Author: Scott McKellar <scott@esilibrary.com>, (C) 2009
8 *
9 * Copyright: See COPYING file that comes with this distribution
10 *
11 */
12
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <opensrf/string_array.h>
16 #include <opensrf/osrf_hash.h>
17 #include <openils/oils_idl.h>
18
19 static void dump_idl( osrfHash* IDLHash );
20 static void dump_class( osrfHash* class_hash, const char* class_name );
21 static void dump_fields( osrfHash* field_hash );
22 static void dump_one_field( osrfHash* field_hash, const char* field_name );
23 static void dump_links( osrfHash* links_hash );
24 static void dump_one_link( osrfHash* link_hash, const char* link_name );
25 static void dump_permacrud( osrfHash* pcrud_hash );
26 static void dump_action( osrfHash* action_hash, const char* action_name );
27 static void dump_foreign_context( osrfHash* fc_hash );
28 static void dump_fc_class( osrfHash* fc_class_hash, const char* class_name );
29 static void dump_string_array( 
30         osrfStringArray* sarr, const char* name, const char* indent );
31
32 int main( int argc, char* argv[] ) {
33         int rc = 0;
34         
35         // Suppress informational messages
36         osrfLogSetLevel( OSRF_LOG_WARNING );
37
38         // Get name of IDL file, if specified on command line
39         const char* IDL_filename = NULL;
40         int filename_expected = 0;              // boolean
41         int i;
42         for( i = 1; i < argc; ++i ) {
43                 const char* arg = argv[ i ];
44                 printf( "%s\n", arg );
45                 if( filename_expected ) {
46                         IDL_filename = arg;
47                         filename_expected = 0;
48                 } else {
49                         if( '-' == arg[ 0 ] && 'f' == arg[1] ) {
50                                 if( IDL_filename ) {
51                                         fprintf( stderr, "Only one IDL file may be specified\n" );
52                                         return 1;
53                                 } else {
54                                         if( arg[ 2 ] )
55                                                 IDL_filename = arg + 2;
56                                         else
57                                                 filename_expected = 1;
58                                 }
59                         }
60                         else
61                                 break;
62                 }
63         }
64         
65         if( filename_expected ) {
66                 fprintf( stderr, "IDL filename expected on command line, not found\n" );
67                 return 1;
68         }
69
70         // No filename?  Look in the environment
71         if( !IDL_filename )
72                 IDL_filename = getenv( "OILS_IDL_FILENAME" );
73
74         // Still no filename?  Apply a default
75         if( !IDL_filename )
76                 IDL_filename = "/openils/conf/fm_IDL.xml";
77         
78         printf( "IDL filename: %s\n", IDL_filename );
79         
80         osrfHash* IDL = oilsIDLInit( IDL_filename );
81         if( NULL == IDL ) {
82                 fputs( "Failed to build IDL\n", stderr );
83                 rc = 1;
84         }
85
86         if( i >= argc )
87                 // No classes specified?  Dump them all
88                 dump_idl( IDL );
89         else do {
90                 // Dump the requested classes
91                 dump_class( osrfHashGet( IDL, argv[ i ] ), argv[ i ] );
92                 ++i;
93         } while( i < argc );
94                 
95         return rc;
96 }
97
98 static void dump_idl( osrfHash* IDLHash ) {
99         if( NULL == IDLHash )
100                 return;
101
102         if( 0 == osrfHashGetCount( IDLHash ) )
103                 return;
104
105         osrfHashIterator* iter = osrfNewHashIterator( IDLHash );
106         osrfHash* class_hash = NULL;
107         
108         // Dump each class
109         for( ;; ) {
110                 class_hash = osrfHashIteratorNext( iter );
111                 if( class_hash )
112                         dump_class( class_hash, osrfHashIteratorKey( iter ) );
113                 else
114                         break;
115         }
116
117         osrfHashIteratorFree( iter );
118 }
119
120 static void dump_class( osrfHash* class_hash, const char* class_name )
121 {
122         if( !class_hash || !class_name )
123                 return;
124         
125         if( 0 == osrfHashGetCount( class_hash ) )
126                 return;
127
128         printf( "Class %s\n", class_name );
129         const char* indent = "    ";
130         
131         osrfHashIterator* iter = osrfNewHashIterator( class_hash );
132         
133         // Dump each attribute, etc. of the class hash
134         for( ;; ) {
135                 void* class_attr = osrfHashIteratorNext( iter );
136                 if( class_attr ) {
137                         const char* attr_name = osrfHashIteratorKey( iter );
138                         if( !strcmp( attr_name, "classname" ) )
139                                 printf( "%s%s: %s\n", indent, attr_name, (char*) class_attr );
140                         else if( !strcmp( attr_name, "fieldmapper" ) )
141                                 printf( "%s%s: %s\n", indent, attr_name, (char*) class_attr );
142                         else if( !strcmp( attr_name, "tablename" ) )
143                                 printf( "%s%s: %s\n", indent, attr_name, (char*) class_attr );
144                         else if( !strcmp( attr_name, "restrict_primary" ) )
145                                 printf( "%s%s: %s\n", indent, attr_name, (char*) class_attr );
146                         else if( !strcmp( attr_name, "virtual" ) )
147                                 printf( "%s%s: %s\n", indent, attr_name, (char*) class_attr );
148                         else if( !strcmp( attr_name, "controller" ) )
149                                 dump_string_array( (osrfStringArray*) class_attr, attr_name, indent );
150                         else if( !strcmp( attr_name, "fields" ) )
151                                 dump_fields( (osrfHash*) class_attr );
152                         else if( !strcmp( attr_name, "links" ) )
153                                 dump_links( (osrfHash*) class_attr );
154                         else if( !strcmp( attr_name, "primarykey" ) )
155                                 printf( "%s%s: %s\n", indent, attr_name, (char*) class_attr );
156                         else if( !strcmp( attr_name, "sequence" ) )
157                                 printf( "%s%s: %s\n", indent, attr_name, (char*) class_attr );
158                         else if( !strcmp( attr_name, "permacrud" ) )
159                                 dump_permacrud( (osrfHash*) class_attr );
160                         else if( !strcmp( attr_name, "source_definition" ) )
161                                 printf( "%s%s:\n%s\n", indent, attr_name, (char*) class_attr );
162                         else
163                                 printf( "%s%s (unknown)\n", indent, attr_name );
164                 } else
165                         break;
166         }
167 }
168
169 static void dump_fields( osrfHash* fields_hash ) {
170         if( NULL == fields_hash )
171                 return;
172
173         if( 0 == osrfHashGetCount( fields_hash ) )
174                 return;
175
176         fputs( "    fields\n", stdout );
177         
178         osrfHashIterator* iter = osrfNewHashIterator( fields_hash );
179         osrfHash* fields_attr = NULL;
180         
181         // Dump each field
182         for( ;; ) {
183                 fields_attr = osrfHashIteratorNext( iter );
184                 if( fields_attr )
185                         dump_one_field( fields_attr, osrfHashIteratorKey( iter ) );
186                 else
187                         break;
188         }
189
190         osrfHashIteratorFree( iter );
191 }
192
193 static void dump_one_field( osrfHash* field_hash, const char* field_name ) {
194         if( !field_hash || !field_name )
195                 return;
196         
197         if( 0 == osrfHashGetCount( field_hash ) )
198                 return;
199
200         printf( "        %s\n", field_name  );
201         
202         osrfHashIterator* iter = osrfNewHashIterator( field_hash );
203         const char* field_attr = NULL;
204         const char* indent = "            ";
205         
206         // Dump each field attribute
207         for( ;; ) {
208                 field_attr = osrfHashIteratorNext( iter );
209                 if( field_attr )
210                         printf( "%s%s: %s\n", indent, osrfHashIteratorKey( iter ), field_attr );
211                 else
212                         break;
213         }
214
215         osrfHashIteratorFree( iter );
216 }
217
218 static void dump_links( osrfHash* links_hash ) {
219         if( NULL == links_hash )
220                 return;
221
222         if( 0 == osrfHashGetCount( links_hash ) )
223                 return;
224
225         fputs( "    links\n", stdout );
226         
227         osrfHashIterator* iter = osrfNewHashIterator( links_hash );
228         osrfHash* links_attr = NULL;
229         
230         // Dump each link
231         for( ;; ) {
232                 links_attr = osrfHashIteratorNext( iter );
233                 if( links_attr )
234                         dump_one_link( links_attr, osrfHashIteratorKey( iter ) );
235                 else
236                         break;
237         }
238
239         osrfHashIteratorFree( iter );
240 }
241
242 static void dump_one_link( osrfHash* link_hash, const char* link_name ) {
243         if( !link_hash || !link_name )
244                 return;
245         
246         if( 0 == osrfHashGetCount( link_hash ) )
247                 return;
248
249         printf( "        %s\n", link_name  );
250         
251         osrfHashIterator* iter = osrfNewHashIterator( link_hash );
252         const void* link_attr = NULL;
253         const char* indent = "            ";
254         
255         // Dump each link attribute
256         for( ;; ) {
257                 link_attr = osrfHashIteratorNext( iter );
258                 if( link_attr ) {
259                         const char* link_attr_name = osrfHashIteratorKey( iter );
260                         if( !strcmp( link_attr_name, "reltype" ) )
261                                 printf( "%s%s: %s\n", indent, link_attr_name, (char*) link_attr );
262                         else if( !strcmp( link_attr_name, "key" ) )
263                                 printf( "%s%s: %s\n", indent, link_attr_name, (char*) link_attr );
264                         else if( !strcmp( link_attr_name, "class" ) )
265                                 printf( "%s%s: %s\n", indent, link_attr_name, (char*) link_attr );
266                         else if( !strcmp( link_attr_name, "map" ) ) 
267                                 dump_string_array( (osrfStringArray*) link_attr, link_attr_name, indent );
268                         else if( !strcmp( link_attr_name, "field" ) )
269                                 printf( "%s%s: %s\n", indent, link_attr_name, (char*) link_attr );
270                         else
271                                 printf( "%s%s (unknown)\n", indent, link_attr_name );
272                 } else
273                         break;
274         }
275
276         osrfHashIteratorFree( iter );
277 }
278
279 static void dump_permacrud( osrfHash* pcrud_hash ) {
280         if( NULL == pcrud_hash )
281                 return;
282
283         if( 0 == osrfHashGetCount( pcrud_hash ) )
284                 return;
285
286         fputs( "    permacrud\n", stdout );
287         
288         osrfHashIterator* iter = osrfNewHashIterator( pcrud_hash );
289         osrfHash* pcrud_attr = NULL;
290
291         // Dump each action
292         for( ;; ) {
293                 pcrud_attr = osrfHashIteratorNext( iter );
294                 if( pcrud_attr )
295                         dump_action( pcrud_attr, osrfHashIteratorKey( iter ) );
296                 else
297                         break;
298         }
299
300         osrfHashIteratorFree( iter );
301 }
302
303 static void dump_action( osrfHash* action_hash, const char* action_name ) {
304         if( !action_hash || !action_name )
305                 return;
306
307         if( 0 == osrfHashGetCount( action_hash ) )
308                 return;
309
310         printf( "        %s\n", action_name );
311
312         osrfHashIterator* iter = osrfNewHashIterator( action_hash );
313         void* action_attr = NULL;
314         const char* indent = "            ";
315
316         // Dump each attribute of the action
317         for( ;; ) {
318                 action_attr = osrfHashIteratorNext( iter );
319                 if( action_attr ) {
320                         const char* attr_name = osrfHashIteratorKey( iter );
321                         if( !strcmp( attr_name, "permission" ) )
322                                 dump_string_array( action_attr, attr_name, indent );
323                         else if( !strcmp( attr_name, "global_required" ) )
324                                 printf( "%s%s: %s\n", indent, attr_name, (char*) action_attr );
325                         else if( !strcmp( attr_name, "local_context" ) )
326                                 dump_string_array( action_attr, attr_name, indent );
327                         else if( !strcmp( attr_name, "foreign_context" ) )
328                                 dump_foreign_context( action_attr );
329                         else
330                                 printf( "%s%s (unknown)\n", indent, attr_name );
331                 } else
332                         break;
333         }
334
335         osrfHashIteratorFree( iter );
336 }
337
338 static void dump_foreign_context( osrfHash* fc_hash ) {
339         if( !fc_hash )
340                 return;
341         
342         if( 0 == osrfHashGetCount( fc_hash ) )
343                 return;
344
345         fputs( "            foreign_context\n", stdout );
346
347         osrfHashIterator* iter = osrfNewHashIterator( fc_hash );
348         osrfHash* fc_attr = NULL;
349
350         // Dump each foreign context attribute
351         for( ;; ) {
352                 fc_attr = osrfHashIteratorNext( iter );
353                 if( fc_attr )
354                         dump_fc_class( (osrfHash*) fc_attr, osrfHashIteratorKey( iter ) );
355                 else
356                         break;
357         }
358
359         osrfHashIteratorFree( iter );
360 }
361
362 static void dump_fc_class( osrfHash* fc_class_hash, const char* class_name )
363 {
364         if( ! fc_class_hash )
365                 return;
366         
367         if( 0 == osrfHashGetCount( fc_class_hash ) )
368                 return;
369         
370         printf( "                %s\n", class_name );
371
372         osrfHashIterator* iter = osrfNewHashIterator( fc_class_hash );
373         void* fc_class_attr = NULL;
374         const char* indent = "                    ";
375
376         // Dump each foreign context attribute
377         for( ;; ) {
378                 fc_class_attr = osrfHashIteratorNext( iter );
379                 if( fc_class_attr ) {
380                         const char* fc_class_attr_name = osrfHashIteratorKey( iter );
381                         if( !strcmp( fc_class_attr_name, "field" ) )
382                                 printf( "%s%s: %s\n", indent, fc_class_attr_name, (const char*) fc_class_attr );
383                         else if( !strcmp( fc_class_attr_name, "fkey" ) )
384                                 printf( "%s%s: %s\n", indent, fc_class_attr_name, (const char*) fc_class_attr );
385                         else if( !strcmp( fc_class_attr_name, "jump" ) )
386                                 dump_string_array( (osrfStringArray*) fc_class_attr, fc_class_attr_name, indent );
387                         else if( !strcmp( fc_class_attr_name, "context" ) )
388                                 dump_string_array( (osrfStringArray*) fc_class_attr, fc_class_attr_name, indent );
389                         else
390                                 printf( "%s%s\n", indent, fc_class_attr_name );
391                 } else
392                         break;
393         }
394
395         osrfHashIteratorFree( iter );
396 }
397
398 static void dump_string_array( 
399         osrfStringArray* sarr, const char* name, const char* indent ) {
400         if( !sarr || !name || !indent )
401                 return;
402
403         int size = sarr->size;
404
405         // Ignore an empty array
406         if( 0 == size )
407                 return;
408
409         printf( "%s%s (string array)\n", indent, name );
410
411         int i;
412         for( i = 0; i < size; ++i )
413                 printf( "%s\t%s\n", indent, osrfStringArrayGetString( sarr, i ) );
414 }