Main Page | Modules | Data Structures | Directories | File List | Data Fields | Related Pages

dbus-glib-tool.c

00001 /* -*- mode: C; c-file-style: "gnu" -*- */
00002 /* dbus-glib-tool.c Tool used by apps using glib bindings
00003  *
00004  * Copyright (C) 2003, 2004 Red Hat, Inc.
00005  *
00006  * Licensed under the Academic Free License version 2.1
00007  *
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  */
00023 
00024 #include <config.h>
00025 #include "dbus-gidl.h"
00026 #include "dbus-gparser.h"
00027 #include "dbus-gutils.h"
00028 #include <locale.h>
00029 #include <libintl.h>
00030 #define _(x) dgettext (GETTEXT_PACKAGE, x)
00031 #define N_(x) x
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035 
00036 #ifdef DBUS_BUILD_TESTS
00037 static void run_all_tests (const char *test_data_dir);
00038 #endif
00039 
00040 static void
00041 indent (int depth)
00042 {
00043   depth *= 2; /* 2-space indent */
00044   
00045   while (depth > 0)
00046     {
00047       putc (' ', stdout);
00048       --depth;
00049     }
00050 }
00051 
00052 static void pretty_print (BaseInfo *base,
00053                           int       depth);
00054 
00055 static void
00056 pretty_print_list (GSList *list,
00057                    int     depth)
00058 {
00059   GSList *tmp;
00060 
00061   tmp = list;
00062   while (tmp != NULL)
00063     {
00064       pretty_print (tmp->data, depth);
00065       tmp = tmp->next;
00066     }
00067 }
00068 
00069 static void
00070 pretty_print (BaseInfo *base,
00071               int       depth)
00072 {
00073   InfoType t;
00074   const char *name;
00075 
00076   t = base_info_get_type (base);
00077   name = base_info_get_name (base);
00078 
00079   indent (depth);
00080   
00081   switch (t)
00082     {
00083     case INFO_TYPE_NODE:
00084       {
00085         NodeInfo *n = (NodeInfo*) base;
00086         
00087         if (name == NULL)
00088           printf (_("<anonymous node> {\n"));
00089         else
00090           printf (_("node \"%s\" {\n"), name);
00091 
00092         pretty_print_list (node_info_get_interfaces (n), depth + 1);
00093         pretty_print_list (node_info_get_nodes (n), depth + 1);
00094 
00095         indent (depth);
00096         printf ("}\n");
00097       }
00098       break;
00099     case INFO_TYPE_INTERFACE:
00100       {
00101         InterfaceInfo *i = (InterfaceInfo*) base;
00102 
00103         g_assert (name != NULL);
00104 
00105         printf (_("interface \"%s\" {\n"), name);
00106 
00107         pretty_print_list (interface_info_get_methods (i), depth + 1);
00108         pretty_print_list (interface_info_get_signals (i), depth + 1);
00109 
00110         indent (depth);
00111         printf ("}\n");
00112       }
00113       break;
00114     case INFO_TYPE_METHOD:
00115       {
00116         MethodInfo *m = (MethodInfo*) base;
00117 
00118         g_assert (name != NULL);
00119 
00120         printf (_("method \"%s\" (\n"), name);
00121 
00122         pretty_print_list (method_info_get_args (m), depth + 1);
00123 
00124         indent (depth);
00125         printf (")\n");
00126       }
00127       break;
00128     case INFO_TYPE_SIGNAL:
00129       {
00130         SignalInfo *s = (SignalInfo*) base;
00131 
00132         g_assert (name != NULL);
00133 
00134         printf (_("signal \"%s\" (\n"), name);
00135 
00136         pretty_print_list (signal_info_get_args (s), depth + 1);
00137 
00138         indent (depth);
00139         printf (")\n");
00140       }
00141       break;
00142     case INFO_TYPE_ARG:
00143       {
00144         ArgInfo *a = (ArgInfo*) base;
00145         int at = arg_info_get_type (a);
00146         ArgDirection d = arg_info_get_direction (a);
00147 
00148         printf ("%s %s",
00149                 d == ARG_IN ? "in" : "out",
00150                 _dbus_gutils_type_to_string (at));
00151         if (name)
00152           printf (" %s\n", name);
00153         else
00154           printf ("\n");
00155       }
00156       break;
00157     }
00158 }
00159 
00160 static void
00161 usage (int ecode)
00162 {
00163   fprintf (stderr, "dbus-glib-tool [--version] [--help]\n");
00164   exit (ecode);
00165 }
00166 
00167 static void
00168 version (void)
00169 {
00170   printf ("D-BUS GLib Tool %s\n"
00171           "Copyright (C) 2003, 2004 Red Hat, Inc.\n"
00172           "This is free software; see the source for copying conditions.\n"
00173           "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
00174           VERSION);
00175   exit (0);
00176 }
00177 
00178 int
00179 main (int argc, char **argv)
00180 {
00181   const char *prev_arg;
00182   int i;
00183   GSList *files;
00184   gboolean end_of_args;
00185   GSList *tmp;
00186   gboolean just_pretty_print;
00187 
00188   setlocale (LC_ALL, "");
00189   bindtextdomain (GETTEXT_PACKAGE, DBUS_LOCALEDIR);
00190   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
00191   textdomain (GETTEXT_PACKAGE); 
00192 
00193   just_pretty_print = FALSE;
00194   end_of_args = FALSE;
00195   files = NULL;
00196   prev_arg = NULL;
00197   i = 1;
00198   while (i < argc)
00199     {
00200       const char *arg = argv[i];
00201 
00202       if (!end_of_args)
00203         {
00204           if (strcmp (arg, "--help") == 0 ||
00205               strcmp (arg, "-h") == 0 ||
00206               strcmp (arg, "-?") == 0)
00207             usage (0);
00208           else if (strcmp (arg, "--version") == 0)
00209             version ();
00210 #ifdef DBUS_BUILD_TESTS
00211           else if (strcmp (arg, "--self-test") == 0)
00212             run_all_tests (NULL);
00213 #endif /* DBUS_BUILD_TESTS */
00214           else if (strcmp (arg, "--pretty-print") == 0)
00215             just_pretty_print = TRUE;
00216           else if (arg[0] == '-' &&
00217                    arg[1] == '-' &&
00218                    arg[2] == '\0')
00219             end_of_args = TRUE;
00220           else if (arg[0] == '-')
00221             {
00222               usage (1);
00223             }
00224           else
00225             {
00226               files = g_slist_prepend (files, (char*) arg);
00227             }
00228         }
00229       else
00230         files = g_slist_prepend (files, (char*) arg);
00231       
00232       prev_arg = arg;
00233       
00234       ++i;
00235     }
00236 
00237   files = g_slist_reverse (files);
00238 
00239   tmp = files;
00240   while (tmp != NULL)
00241     {
00242       NodeInfo *node;
00243       GError *error;
00244       const char *filename;
00245 
00246       filename = tmp->data;
00247 
00248       error = NULL;
00249       node = description_load_from_file (filename,
00250                                          &error);
00251       if (node == NULL)
00252         {
00253           g_assert (error != NULL);
00254           fprintf (stderr, _("Unable to load \"%s\": %s\n"),
00255                    filename, error->message);
00256           g_error_free (error);
00257           exit (1);
00258         }
00259       else if (just_pretty_print)
00260         {
00261           pretty_print ((BaseInfo*) node, 0);
00262         }
00263       else
00264         {
00265           /* FIXME process the file to generate metadata variable
00266            * definition rather than just printing it.
00267            * i.e. we want to create DBusGObjectInfo.
00268            * This probably requires extending the introspection XML format to
00269            * allow a "native function name":
00270            *  <method name="Frobate" native="my_object_frobate">
00271            */
00272           pretty_print ((BaseInfo*) node, 0);
00273         }
00274 
00275       if (node)
00276         node_info_unref (node);
00277       
00278       tmp = tmp->next;
00279     }
00280   
00281   return 0;
00282 }
00283 
00284 
00285 #ifdef DBUS_BUILD_TESTS
00286 static void
00287 test_die (const char *failure)
00288 {
00289   fprintf (stderr, "Unit test failed: %s\n", failure);
00290   exit (1);
00291 }
00292 
00298 static gboolean
00299 _dbus_gtool_test (const char *test_data_dir)
00300 {
00301 
00302   return TRUE;
00303 }
00304 
00305 static void
00306 run_all_tests (const char *test_data_dir)
00307 {
00308   if (test_data_dir == NULL)
00309     test_data_dir = g_getenv ("DBUS_TEST_DATA");
00310 
00311   if (test_data_dir != NULL)
00312     printf ("Test data in %s\n", test_data_dir);
00313   else
00314     printf ("No test data!\n");
00315 
00316   printf ("%s: running gtool tests\n", "dbus-glib-tool");
00317   if (!_dbus_gtool_test (test_data_dir))
00318     test_die ("gtool");
00319 
00320   printf ("%s: completed successfully\n", "dbus-glib-tool");
00321 }
00322 
00323 #endif /* DBUS_BUILD_TESTS */

Generated on Thu Aug 11 21:11:12 2005 for D-BUS by  doxygen 1.4.0