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