00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <config.h>
00025 #include "dbus-gidl.h"
00026 #include "dbus-gparser.h"
00027 #include "dbus-gutils.h"
00028 #include "dbus-glib-tool.h"
00029 #include "dbus-binding-tool-glib.h"
00030 #include <locale.h>
00031 #include <libintl.h>
00032 #define _(x) dgettext (GETTEXT_PACKAGE, x)
00033 #define N_(x) x
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036 #include <errno.h>
00037 #include <sys/stat.h>
00038 #include <string.h>
00039 #include <time.h>
00040
00041 #ifdef DBUS_BUILD_TESTS
00042 static void run_all_tests (const char *test_data_dir);
00043 #endif
00044
00045 typedef enum {
00046 DBUS_BINDING_OUTPUT_NONE,
00047 DBUS_BINDING_OUTPUT_PRETTY,
00048 DBUS_BINDING_OUTPUT_GLIB_SERVER,
00049 DBUS_BINDING_OUTPUT_GLIB_CLIENT
00050 } DBusBindingOutputMode;
00051
00052 static void
00053 indent (int depth)
00054 {
00055 depth *= 2;
00056
00057 while (depth > 0)
00058 {
00059 putc (' ', stdout);
00060 --depth;
00061 }
00062 }
00063
00064 static void pretty_print (BaseInfo *base,
00065 int depth);
00066
00067 static void
00068 pretty_print_list (GSList *list,
00069 int depth)
00070 {
00071 GSList *tmp;
00072
00073 tmp = list;
00074 while (tmp != NULL)
00075 {
00076 pretty_print (tmp->data, depth);
00077 tmp = tmp->next;
00078 }
00079 }
00080
00081 static void
00082 pretty_print (BaseInfo *base,
00083 int depth)
00084 {
00085 InfoType t;
00086 const char *name;
00087
00088 t = base_info_get_type (base);
00089 name = base_info_get_name (base);
00090
00091 indent (depth);
00092
00093 switch (t)
00094 {
00095 case INFO_TYPE_NODE:
00096 {
00097 NodeInfo *n = (NodeInfo*) base;
00098
00099 if (name == NULL)
00100 printf (_("<anonymous node> {\n"));
00101 else
00102 printf (_("node \"%s\" {\n"), name);
00103
00104 pretty_print_list (node_info_get_interfaces (n), depth + 1);
00105 pretty_print_list (node_info_get_nodes (n), depth + 1);
00106
00107 indent (depth);
00108 printf ("}\n");
00109 }
00110 break;
00111 case INFO_TYPE_INTERFACE:
00112 {
00113 InterfaceInfo *i = (InterfaceInfo*) base;
00114 GSList *annotations, *elt;
00115
00116 g_assert (name != NULL);
00117
00118 printf (_("interface \"%s\" {\n"), name);
00119
00120 annotations = interface_info_get_annotations (i);
00121 for (elt = annotations; elt; elt = elt->next)
00122 {
00123 const char *name = elt->data;
00124 const char *value = interface_info_get_annotation (i, name);
00125
00126 printf (_(" (binding \"%s\": \"%s\") "),
00127 name, value);
00128 }
00129 g_slist_free (annotations);
00130
00131 pretty_print_list (interface_info_get_methods (i), depth + 1);
00132 pretty_print_list (interface_info_get_signals (i), depth + 1);
00133 pretty_print_list (interface_info_get_properties (i), depth + 1);
00134
00135 indent (depth);
00136 printf ("}\n");
00137 }
00138 break;
00139 case INFO_TYPE_METHOD:
00140 {
00141 MethodInfo *m = (MethodInfo*) base;
00142 GSList *annotations, *elt;
00143
00144 g_assert (name != NULL);
00145
00146 annotations = method_info_get_annotations (m);
00147 printf (_("method \"%s\""), name);
00148 for (elt = annotations; elt; elt = elt->next)
00149 {
00150 const char *name = elt->data;
00151 const char *value = method_info_get_annotation (m, name);
00152
00153 printf (_(" (annotation \"%s\": \"%s\") "),
00154 name, value);
00155 }
00156 g_slist_free (annotations);
00157
00158 pretty_print_list (method_info_get_args (m), depth + 1);
00159
00160 indent (depth);
00161 printf (")\n");
00162 }
00163 break;
00164 case INFO_TYPE_SIGNAL:
00165 {
00166 SignalInfo *s = (SignalInfo*) base;
00167
00168 g_assert (name != NULL);
00169
00170 printf (_("signal \"%s\" (\n"), name);
00171
00172 pretty_print_list (signal_info_get_args (s), depth + 1);
00173
00174 indent (depth);
00175 printf (")\n");
00176 }
00177 break;
00178 case INFO_TYPE_PROPERTY:
00179 {
00180 PropertyInfo *a = (PropertyInfo*) base;
00181 const char *pt = property_info_get_type (a);
00182 PropertyAccessFlags acc = property_info_get_access (a);
00183
00184 printf ("%s%s %s",
00185 acc & PROPERTY_READ ? "read" : "",
00186 acc & PROPERTY_WRITE ? "write" : "",
00187 pt);
00188 if (name)
00189 printf (" %s\n", name);
00190 else
00191 printf ("\n");
00192 }
00193 break;
00194 case INFO_TYPE_ARG:
00195 {
00196 ArgInfo *a = (ArgInfo*) base;
00197 const char *at = arg_info_get_type (a);
00198 ArgDirection d = arg_info_get_direction (a);
00199
00200 printf ("%s %s",
00201 d == ARG_IN ? "in" : "out",
00202 at);
00203 if (name)
00204 printf (" %s\n", name);
00205 else
00206 printf ("\n");
00207 }
00208 break;
00209 }
00210 }
00211
00212 GQuark
00213 dbus_binding_tool_error_quark (void)
00214 {
00215 static GQuark quark = 0;
00216 if (!quark)
00217 quark = g_quark_from_static_string ("dbus_binding_tool_error");
00218
00219 return quark;
00220 }
00221
00222 static void lose (const char *fmt, ...) G_GNUC_NORETURN G_GNUC_PRINTF (1, 2);
00223 static void lose_gerror (const char *prefix, GError *error) G_GNUC_NORETURN;
00224
00225 static void
00226 lose (const char *str, ...)
00227 {
00228 va_list args;
00229
00230 va_start (args, str);
00231
00232 vfprintf (stderr, str, args);
00233 fputc ('\n', stderr);
00234
00235 va_end (args);
00236
00237 exit (1);
00238 }
00239
00240 static void
00241 lose_gerror (const char *prefix, GError *error)
00242 {
00243 lose ("%s: %s", prefix, error->message);
00244 }
00245
00246 static void
00247 usage (int ecode)
00248 {
00249 fprintf (stderr, "dbus-binding-tool [--version] [--help]\n");
00250 fprintf (stderr, "dbus-binding-tool ---mode=[pretty|glib-server|glib-client] -prefix=SYMBOL_PREFIX [--ignore-unsupported] [--force] [--output=FILE] [\n");
00251 exit (ecode);
00252 }
00253
00254 static void
00255 version (void)
00256 {
00257 printf ("D-BUS Binding Tool %s\n"
00258 "Copyright (C) 2003-2005 Red Hat, Inc.\n"
00259 "This is free software; see the source for copying conditions.\n"
00260 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
00261 VERSION);
00262 exit (0);
00263 }
00264
00265 int
00266 main (int argc, char **argv)
00267 {
00268 const char *output_file;
00269 const char *prefix;
00270 char *output_file_tmp;
00271 int i;
00272 GSList *files;
00273 DBusBindingOutputMode outputmode;
00274 gboolean end_of_args;
00275 GSList *tmp;
00276 GIOChannel *channel;
00277 GError *error;
00278 time_t newest_src;
00279 struct stat srcbuf;
00280 struct stat targetbuf;
00281 gboolean force;
00282 gboolean ignore_unsupported;
00283 gboolean has_prefix = FALSE;
00284
00285 setlocale (LC_ALL, "");
00286 bindtextdomain (GETTEXT_PACKAGE, DBUS_LOCALEDIR);
00287 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
00288 textdomain (GETTEXT_PACKAGE);
00289
00290 g_type_init ();
00291
00292 outputmode = DBUS_BINDING_OUTPUT_NONE;
00293 end_of_args = FALSE;
00294 files = NULL;
00295 output_file = NULL;
00296 prefix = "";
00297 ignore_unsupported = FALSE;
00298 force = FALSE;
00299 i = 1;
00300 while (i < argc)
00301 {
00302 const char *arg = argv[i];
00303
00304 if (!end_of_args)
00305 {
00306 if (strcmp (arg, "--help") == 0 ||
00307 strcmp (arg, "-h") == 0 ||
00308 strcmp (arg, "-?") == 0)
00309 usage (0);
00310 else if (strcmp (arg, "--version") == 0)
00311 version ();
00312 else if (strcmp (arg, "--force") == 0)
00313 force = TRUE;
00314 #ifdef DBUS_BUILD_TESTS
00315 else if (strcmp (arg, "--self-test") == 0)
00316 run_all_tests (NULL);
00317 #endif
00318 else if (strncmp (arg, "--mode=", 7) == 0)
00319 {
00320 const char *mode = arg + 7;
00321 if (!strcmp (mode, "pretty"))
00322 outputmode = DBUS_BINDING_OUTPUT_PRETTY;
00323 else if (!strcmp (mode, "glib-server"))
00324 outputmode = DBUS_BINDING_OUTPUT_GLIB_SERVER;
00325 else if (!strcmp (mode, "glib-client"))
00326 outputmode = DBUS_BINDING_OUTPUT_GLIB_CLIENT;
00327 else
00328 usage (1);
00329 }
00330 else if (strcmp (arg, "--ignore-unsupported") == 0)
00331 ignore_unsupported = TRUE;
00332 else if (strncmp (arg, "--output=", 9) == 0)
00333 {
00334 output_file = arg + 9;
00335 }
00336 else if (strncmp (arg, "--prefix=", 9) == 0)
00337 {
00338 has_prefix = TRUE;
00339 prefix = arg + 9;
00340 }
00341 else if (arg[0] == '-' &&
00342 arg[1] == '-' &&
00343 arg[2] == '\0')
00344 end_of_args = TRUE;
00345 else if (arg[0] == '-')
00346 {
00347 usage (1);
00348 }
00349 else
00350 {
00351 files = g_slist_prepend (files, (char*) arg);
00352 }
00353 }
00354 else
00355 files = g_slist_prepend (files, (char*) arg);
00356
00357 ++i;
00358 }
00359
00360 if (!has_prefix)
00361 usage (1);
00362
00363 error = NULL;
00364
00365 files = g_slist_reverse (files);
00366
00367 if (output_file && !force)
00368 {
00369 newest_src = 0;
00370 for (tmp = files; tmp != NULL; tmp = tmp->next)
00371 {
00372 const char *filename;
00373
00374 filename = tmp->data;
00375 if (stat (filename, &srcbuf) < 0)
00376 lose ("Couldn't stat %s: %s", filename, g_strerror (errno));
00377
00378 if (srcbuf.st_mtime > newest_src)
00379 newest_src = srcbuf.st_mtime;
00380 }
00381
00382 if (stat (output_file, &targetbuf) > 0
00383 && targetbuf.st_mtime >= newest_src)
00384 exit (0);
00385 }
00386
00387 if (output_file)
00388 {
00389 output_file_tmp = g_strconcat (output_file, ".tmp", NULL);
00390
00391 if (!(channel = g_io_channel_new_file (output_file_tmp, "w", &error)))
00392 lose_gerror (_("Couldn't open temporary file"), error);
00393 }
00394 else
00395 {
00396 channel = g_io_channel_unix_new (fileno (stdout));
00397 output_file_tmp = NULL;
00398 }
00399 if (!g_io_channel_set_encoding (channel, NULL, &error))
00400 lose_gerror (_("Couldn't set channel encoding to NULL"), error);
00401
00402
00403 for (tmp = files; tmp != NULL; tmp = tmp->next)
00404 {
00405 NodeInfo *node;
00406 GError *error;
00407 const char *filename;
00408
00409 filename = tmp->data;
00410
00411 error = NULL;
00412 node = description_load_from_file (filename,
00413 &error);
00414 if (node == NULL)
00415 {
00416 lose_gerror (_("Unable to load \"%s\""), error);
00417 }
00418 else
00419 {
00420 switch (outputmode)
00421 {
00422 case DBUS_BINDING_OUTPUT_PRETTY:
00423 pretty_print ((BaseInfo*) node, 0);
00424 break;
00425 case DBUS_BINDING_OUTPUT_GLIB_SERVER:
00426 if (!dbus_binding_tool_output_glib_server ((BaseInfo *) node, channel, prefix, &error))
00427 lose_gerror (_("Compilation failed"), error);
00428 break;
00429 case DBUS_BINDING_OUTPUT_GLIB_CLIENT:
00430 if (!dbus_binding_tool_output_glib_client ((BaseInfo *) node, channel, ignore_unsupported, &error))
00431 lose_gerror (_("Compilation failed"), error);
00432 break;
00433 case DBUS_BINDING_OUTPUT_NONE:
00434 break;
00435 }
00436 }
00437
00438 if (node)
00439 node_info_unref (node);
00440 }
00441
00442 if (g_io_channel_shutdown (channel, TRUE, &error) != G_IO_STATUS_NORMAL)
00443 lose_gerror (_("Failed to shutdown IO channel"), error);
00444 g_io_channel_unref (channel);
00445
00446 if (output_file)
00447 {
00448 if (rename (output_file_tmp, output_file) < 0)
00449 lose ("Failed to rename %s to %s: %s", output_file_tmp, output_file,
00450 g_strerror (errno));
00451 g_free (output_file_tmp);
00452 }
00453
00454 return 0;
00455 }
00456
00457
00458 #ifdef DBUS_BUILD_TESTS
00459 static void
00460 test_die (const char *failure)
00461 {
00462 lose ("Unit test failed: %s", failure);
00463 }
00464
00470 static gboolean
00471 _dbus_gtool_test (const char *test_data_dir)
00472 {
00473
00474 return TRUE;
00475 }
00476
00477 static void
00478 run_all_tests (const char *test_data_dir)
00479 {
00480 if (test_data_dir == NULL)
00481 test_data_dir = g_getenv ("DBUS_TEST_DATA");
00482
00483 if (test_data_dir != NULL)
00484 printf ("Test data in %s\n", test_data_dir);
00485 else
00486 printf ("No test data!\n");
00487
00488 printf ("%s: running binding tests\n", "dbus-binding-tool");
00489 if (!_dbus_gtool_test (test_data_dir))
00490 test_die ("gtool");
00491
00492 printf ("%s: completed successfully\n", "dbus-binding-tool");
00493 }
00494
00495 #endif