1 | /* |
---|
2 | * |
---|
3 | * Some crude tests for libIDL. |
---|
4 | * |
---|
5 | * Usage: tstidl <filename> [flags] |
---|
6 | * |
---|
7 | * if given, flags is read as (output_flags << 24) | parse_flags |
---|
8 | * |
---|
9 | * gcc `libIDL-config --cflags --libs` tstidl.c -o tstidl |
---|
10 | * |
---|
11 | */ |
---|
12 | #ifdef G_LOG_DOMAIN |
---|
13 | # undef G_LOG_DOMAIN |
---|
14 | #endif |
---|
15 | #define G_LOG_DOMAIN "tstidl" |
---|
16 | #include <stdio.h> |
---|
17 | #include <stdlib.h> |
---|
18 | #include <string.h> |
---|
19 | #include <ctype.h> |
---|
20 | #ifdef IDL_LIBRARY |
---|
21 | # include "IDL.h" |
---|
22 | #else |
---|
23 | # include <libIDL/IDL.h> |
---|
24 | #endif |
---|
25 | |
---|
26 | #define IDLFP_IDENT_VISITED (1UL << 0) |
---|
27 | |
---|
28 | typedef struct { |
---|
29 | IDL_tree tree; |
---|
30 | IDL_ns ns; |
---|
31 | } WalkData; |
---|
32 | |
---|
33 | static gboolean |
---|
34 | print_repo_id (IDL_tree_func_data *tfd, WalkData *data) |
---|
35 | { |
---|
36 | char *repo_id = NULL; |
---|
37 | IDL_tree p; |
---|
38 | |
---|
39 | p = tfd->tree; |
---|
40 | |
---|
41 | if (IDL_NODE_TYPE (p) == IDLN_INTERFACE) { |
---|
42 | repo_id = IDL_IDENT_REPO_ID (IDL_INTERFACE (p).ident); |
---|
43 | } else if (IDL_NODE_TYPE (p) == IDLN_IDENT && |
---|
44 | IDL_NODE_UP (p) != NULL && |
---|
45 | IDL_NODE_UP (IDL_NODE_UP (p)) != NULL && |
---|
46 | IDL_NODE_TYPE (IDL_NODE_UP (IDL_NODE_UP (p))) == IDLN_ATTR_DCL) |
---|
47 | repo_id = IDL_IDENT_REPO_ID (p); |
---|
48 | if (repo_id) |
---|
49 | printf ("%s\n", repo_id); |
---|
50 | |
---|
51 | return TRUE; |
---|
52 | } |
---|
53 | |
---|
54 | static gboolean |
---|
55 | print_xpidl_exts (IDL_tree_func_data *tfd, WalkData *data) |
---|
56 | { |
---|
57 | IDL_tree p; |
---|
58 | |
---|
59 | p = tfd->tree; |
---|
60 | |
---|
61 | if (IDL_NODE_TYPE (p) == IDLN_IDENT && |
---|
62 | IDL_NODE_TYPE (IDL_NODE_UP (p)) == IDLN_INTERFACE) { |
---|
63 | const char *val; |
---|
64 | |
---|
65 | val = IDL_tree_property_get (p, "IID"); |
---|
66 | if (val) printf ("\tinterface `%s' XPIDL IID:\"%s\"\n", |
---|
67 | IDL_IDENT (IDL_INTERFACE (IDL_NODE_UP (p)).ident).str, |
---|
68 | val); |
---|
69 | } |
---|
70 | |
---|
71 | if (IDL_NODE_TYPE (p) == IDLN_NATIVE && |
---|
72 | IDL_NATIVE (p).user_type) |
---|
73 | g_message ("XPIDL native type: \"%s\"", IDL_NATIVE (p).user_type); |
---|
74 | |
---|
75 | if (IDL_NODE_TYPE (p) == IDLN_CODEFRAG) { |
---|
76 | GSList *slist = IDL_CODEFRAG (p).lines; |
---|
77 | |
---|
78 | g_message ("XPIDL code fragment desc.: \"%s\"", IDL_CODEFRAG (p).desc); |
---|
79 | for (; slist; slist = slist->next) |
---|
80 | g_message ("XPIDL code fragment line.: \"%s\"", (char *) slist->data); |
---|
81 | } |
---|
82 | |
---|
83 | return TRUE; |
---|
84 | } |
---|
85 | |
---|
86 | static gboolean |
---|
87 | print_ident_comments (IDL_tree_func_data *tfd, WalkData *data) |
---|
88 | { |
---|
89 | GSList *list; |
---|
90 | IDL_tree p; |
---|
91 | |
---|
92 | p = tfd->tree; |
---|
93 | |
---|
94 | if (IDL_NODE_TYPE (p) == IDLN_IDENT) { |
---|
95 | if (!(p->flags & IDLFP_IDENT_VISITED)) { |
---|
96 | printf ("Identifier: %s\n", IDL_IDENT (p).str); |
---|
97 | for (list = IDL_IDENT (p).comments; list; |
---|
98 | list = g_slist_next (list)) { |
---|
99 | char *comment = list->data; |
---|
100 | printf ("%s\n", comment); |
---|
101 | } |
---|
102 | p->flags |= IDLFP_IDENT_VISITED; |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | return TRUE; |
---|
107 | } |
---|
108 | |
---|
109 | static gboolean |
---|
110 | print_const_dcls (IDL_tree_func_data *tfd, WalkData *data) |
---|
111 | { |
---|
112 | IDL_tree p; |
---|
113 | |
---|
114 | p = tfd->tree; |
---|
115 | |
---|
116 | if (IDL_NODE_TYPE (p) == IDLN_CONST_DCL) { |
---|
117 | GString *s; |
---|
118 | |
---|
119 | s = IDL_tree_to_IDL_string (p, NULL, IDLF_OUTPUT_NO_NEWLINES); |
---|
120 | puts (s->str); |
---|
121 | g_string_free (s, TRUE); |
---|
122 | |
---|
123 | return FALSE; |
---|
124 | } |
---|
125 | |
---|
126 | return TRUE; |
---|
127 | } |
---|
128 | |
---|
129 | /* Example input method... simply reads in from some file and passes it along as |
---|
130 | * is--warning, no actual C preprocessing performed here! Standard C preprocessor |
---|
131 | * indicators should also be passed to libIDL, including # <line> "filename" (double |
---|
132 | * quotes expected), etcetera (do not confuse this with passing #defines to libIDL, this |
---|
133 | * should not be done). */ |
---|
134 | |
---|
135 | /* #define TEST_INPUT_CB */ |
---|
136 | |
---|
137 | #ifdef _WIN32 |
---|
138 | # ifndef TEST_INPUT_CB |
---|
139 | # define TEST_INPUT_CB |
---|
140 | # endif |
---|
141 | #endif |
---|
142 | |
---|
143 | #ifdef TEST_INPUT_CB |
---|
144 | typedef struct { |
---|
145 | FILE *in; |
---|
146 | } InputData; |
---|
147 | |
---|
148 | static int |
---|
149 | my_input_cb (IDL_input_reason reason, union IDL_input_data *cb_data, gpointer user_data) |
---|
150 | { |
---|
151 | InputData *my_data = user_data; |
---|
152 | int rv; |
---|
153 | static int linecount; |
---|
154 | |
---|
155 | switch (reason) { |
---|
156 | case IDL_INPUT_REASON_INIT: |
---|
157 | g_message ("my_input_cb: filename: %s", cb_data->init.filename); |
---|
158 | my_data->in = fopen (cb_data->init.filename, "r"); |
---|
159 | /* If failed, should know that it is implied to libIDL that errno is set |
---|
160 | * appropriately by a C library function or otherwise. Return 0 upon |
---|
161 | * success. */ |
---|
162 | linecount = 1; |
---|
163 | IDL_file_set (cb_data->init.filename, 1); |
---|
164 | return my_data->in ? 0 : -1; |
---|
165 | |
---|
166 | case IDL_INPUT_REASON_FILL: |
---|
167 | /* Fill the buffer here... return number of bytes read (maximum of |
---|
168 | cb_data->fill.max_size), 0 for EOF, negative value upon error. */ |
---|
169 | rv = fread (cb_data->fill.buffer, 1, cb_data->fill.max_size, my_data->in); |
---|
170 | IDL_queue_new_ident_comment ("Queue some comment..."); |
---|
171 | g_message ("my_input_cb: fill, max size %d, got %d", |
---|
172 | cb_data->fill.max_size, rv); |
---|
173 | if (rv == 0 && ferror (my_data->in)) |
---|
174 | return -1; |
---|
175 | IDL_file_set (NULL, ++linecount); |
---|
176 | return rv; |
---|
177 | |
---|
178 | case IDL_INPUT_REASON_ABORT: |
---|
179 | case IDL_INPUT_REASON_FINISH: |
---|
180 | /* Called after parsing to indicate success or failure */ |
---|
181 | g_message ("my_input_cb: abort or finish"); |
---|
182 | fclose (my_data->in); |
---|
183 | break; |
---|
184 | } |
---|
185 | |
---|
186 | return 0; |
---|
187 | } |
---|
188 | #endif |
---|
189 | |
---|
190 | int |
---|
191 | main (int argc, char *argv[]) |
---|
192 | { |
---|
193 | int rv; |
---|
194 | IDL_tree tree; |
---|
195 | IDL_ns ns; |
---|
196 | char *fn; |
---|
197 | WalkData data; |
---|
198 | unsigned long parse_flags = 0; |
---|
199 | |
---|
200 | #ifndef _WIN32 |
---|
201 | { extern int __IDL_debug; |
---|
202 | __IDL_debug = argc >= 4 ? TRUE : FALSE; } |
---|
203 | #endif |
---|
204 | |
---|
205 | IDL_check_cast_enable (TRUE); |
---|
206 | |
---|
207 | g_message ("libIDL version %s", IDL_get_libver_string ()); |
---|
208 | |
---|
209 | if (argc < 2) { |
---|
210 | fprintf (stderr, "usage: tstidl <filename> [parse flags, hex]\n"); |
---|
211 | exit (1); |
---|
212 | } |
---|
213 | |
---|
214 | fn = argv[1]; |
---|
215 | if (argc >= 3) |
---|
216 | sscanf (argv[2], "%lx", &parse_flags); |
---|
217 | |
---|
218 | #ifdef TEST_INPUT_CB |
---|
219 | { InputData input_cb_data; |
---|
220 | g_message ("IDL_parse_filename_with_input"); |
---|
221 | rv = IDL_parse_filename_with_input ( |
---|
222 | fn, my_input_cb, &input_cb_data, |
---|
223 | NULL, &tree, &ns, parse_flags, |
---|
224 | IDL_WARNING1); |
---|
225 | } |
---|
226 | #else |
---|
227 | g_message ("IDL_parse_filename"); |
---|
228 | rv = IDL_parse_filename ( |
---|
229 | fn, NULL, NULL, &tree, &ns, |
---|
230 | parse_flags, IDL_WARNING1); |
---|
231 | #endif |
---|
232 | |
---|
233 | if (rv == IDL_ERROR) { |
---|
234 | g_message ("IDL_ERROR"); |
---|
235 | exit (1); |
---|
236 | } else if (rv < 0) { |
---|
237 | perror (fn); |
---|
238 | exit (1); |
---|
239 | } |
---|
240 | |
---|
241 | /* rv == IDL_SUCCESS */ |
---|
242 | |
---|
243 | data.tree = tree; |
---|
244 | data.ns = ns; |
---|
245 | |
---|
246 | g_print ("--------------------------------------\n"); |
---|
247 | g_message ("Repository IDs"); |
---|
248 | g_print ("--------------------------------------\n"); |
---|
249 | IDL_tree_walk_in_order (tree, (IDL_tree_func) print_repo_id, &data); |
---|
250 | g_print ("\n--------------------------------------\n"); |
---|
251 | g_message ("XPIDL extensions"); |
---|
252 | g_print ("--------------------------------------\n"); |
---|
253 | IDL_tree_walk_in_order (tree, (IDL_tree_func) print_xpidl_exts, &data); |
---|
254 | g_print ("\n--------------------------------------\n"); |
---|
255 | g_message ("Constant Declarations"); |
---|
256 | g_print ("--------------------------------------\n"); |
---|
257 | IDL_tree_walk_in_order (tree, (IDL_tree_func) print_const_dcls, &data); |
---|
258 | g_print ("\n--------------------------------------\n"); |
---|
259 | g_message ("Identifiers"); |
---|
260 | g_print ("--------------------------------------\n"); |
---|
261 | IDL_tree_walk_in_order (tree, (IDL_tree_func) print_ident_comments, &data); |
---|
262 | g_print ("\n--------------------------------------\n"); |
---|
263 | g_message ("IDL_tree_to_IDL"); |
---|
264 | g_print ("--------------------------------------\n"); |
---|
265 | IDL_tree_to_IDL (tree, ns, stdout, parse_flags >> 24); |
---|
266 | IDL_ns_free (ns); |
---|
267 | IDL_tree_free (tree); |
---|
268 | |
---|
269 | return 0; |
---|
270 | } |
---|