2005-05-11 Colin Walters <walters@verbum.org>
authorColin Walters <walters@verbum.org>
Wed, 11 May 2005 18:48:24 +0000 (18:48 +0000)
committerColin Walters <walters@verbum.org>
Wed, 11 May 2005 18:48:24 +0000 (18:48 +0000)
* tools/dbus-send.c (append_array): New function.
(append_arg): Broken out from main.
(main): Add cheesy hack to send arrays and variants.
(usage): Update.
* tools/dbus-print-message.c (print_iter): Broken out
from main.

ChangeLog
tools/dbus-print-message.c
tools/dbus-send.c

index 1f3bea1..4e519ad 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,31 @@
 2005-05-11  Colin Walters  <walters@verbum.org>
 
+       * tools/dbus-send.c (append_array): New function.
+       (append_arg): Broken out from main.
+       (main): Add cheesy hack to send arrays and variants.
+       (usage): Update.
+       * tools/dbus-print-message.c (print_iter): Broken out
+       from main.
+
+2005-05-11  Colin Walters  <walters@verbum.org>
+
+       * dbus/dbus-signature.c (dbus_signature_iter_get_signature):
+       New function, returns signature string for signature iter.
+       * dbus/dbus-signature.h: Prototype it.
+       * dbus/dbus-message.c (dbus_message_iter_get_signature):
+       New function, returns signature string for message iter.
+       (dbus_message_iter_get_array_len): New function, returns
+       length of array.
+       (dbus_message_iter_get_fixed_array): Fix assertion; this
+       function should be used when the iter is pointing to the
+       contents of an array
+       * dbus/dbus-message.h: Prototypes.
+       * dbus/dbus-marshal-recursive.c (_dbus_type_reader_get_array_length):
+       New function; returns length of an array.
+       * dbus/dbus-marshal-recursive.h: Prototype it.
+       
+2005-05-11  Colin Walters  <walters@verbum.org>
+
        * dbus/dbus-sysdeps-util.c <!HAVE_POSIX_GETPWNAM_R>: Fix
        compilation error.
        
index b355925..7fcdec7 100644 (file)
@@ -39,6 +39,94 @@ type_to_name (int message_type)
     }
 }
 
+static void
+print_iter (DBusMessageIter *iter, int depth)
+{
+  do
+    {
+      int type = dbus_message_iter_get_arg_type (iter);
+      const char *str;
+      dbus_uint32_t uint32;
+      dbus_int32_t int32;
+      double d;
+      unsigned char byte;
+      dbus_bool_t boolean;
+
+      if (type == DBUS_TYPE_INVALID)
+       break;
+
+      while (depth-- > 0)
+       putc (' ', stdout);
+
+      switch (type)
+       {
+       case DBUS_TYPE_STRING:
+          dbus_message_iter_get_basic (iter, &str);
+         printf ("string \"%s\"\n", str);
+         break;
+
+       case DBUS_TYPE_INT32:
+          dbus_message_iter_get_basic (iter, &int32);
+         printf ("int32 %d\n", int32);
+         break;
+
+       case DBUS_TYPE_UINT32:
+          dbus_message_iter_get_basic (iter, &uint32);
+         printf ("uint32 %u\n", uint32);
+         break;
+
+       case DBUS_TYPE_DOUBLE:
+         dbus_message_iter_get_basic (iter, &d);
+         printf ("double %g\n", d);
+         break;
+
+       case DBUS_TYPE_BYTE:
+         dbus_message_iter_get_basic (iter, &byte);
+         printf ("byte %d\n", byte);
+         break;
+
+       case DBUS_TYPE_BOOLEAN:
+          dbus_message_iter_get_basic (iter, &boolean);
+         printf ("boolean %s\n", boolean ? "true" : "false");
+         break;
+
+       case DBUS_TYPE_VARIANT:
+         {
+           DBusMessageIter subiter;
+
+           dbus_message_iter_recurse (iter, &subiter);
+
+           printf ("variant:");
+           print_iter (&subiter, depth);
+           break;
+         }
+       case DBUS_TYPE_ARRAY:
+         {
+           int current_type;
+           DBusMessageIter subiter;
+
+           dbus_message_iter_recurse (iter, &subiter);
+
+           printf("[");
+           while ((current_type = dbus_message_iter_get_arg_type (&subiter)) != DBUS_TYPE_INVALID)
+             {
+               print_iter (&subiter, depth);
+               dbus_message_iter_next (&subiter);
+               if (dbus_message_iter_get_arg_type (&subiter) != DBUS_TYPE_INVALID)
+                 printf (",");
+             }
+           printf("]");
+           break;
+         }
+           
+       default:
+         printf (" (dbus-monitor too dumb to decipher arg type '%c')\n", type);
+         break;
+       }
+      
+    } while (dbus_message_iter_next (iter));
+}
+
 void
 print_message (DBusMessage *message)
 {
@@ -46,7 +134,6 @@ print_message (DBusMessage *message)
   const char *sender;
   const char *destination;
   int message_type;
-  int count;
 
   message_type = dbus_message_get_type (message);
   sender = dbus_message_get_sender (message);
@@ -81,59 +168,7 @@ print_message (DBusMessage *message)
     }
 
   dbus_message_iter_init (message, &iter);
-  count = 0;
+  print_iter (&iter, 1);
   
-  do
-    {
-      int type = dbus_message_iter_get_arg_type (&iter);
-      const char *str;
-      dbus_uint32_t uint32;
-      dbus_int32_t int32;
-      double d;
-      unsigned char byte;
-      dbus_bool_t boolean;
-
-      if (type == DBUS_TYPE_INVALID)
-       break;
-
-      switch (type)
-       {
-       case DBUS_TYPE_STRING:
-          dbus_message_iter_get_basic (&iter, &str);
-         printf (" %d string \"%s\"\n", count, str);
-         break;
-
-       case DBUS_TYPE_INT32:
-          dbus_message_iter_get_basic (&iter, &int32);
-         printf (" %d int32 %d\n", count, int32);
-         break;
-
-       case DBUS_TYPE_UINT32:
-          dbus_message_iter_get_basic (&iter, &uint32);
-         printf (" %d uint32 %u\n", count, uint32);
-         break;
-
-       case DBUS_TYPE_DOUBLE:
-         dbus_message_iter_get_basic (&iter, &d);
-         printf (" %d double %g\n", count, d);
-         break;
-
-       case DBUS_TYPE_BYTE:
-         dbus_message_iter_get_basic (&iter, &byte);
-         printf (" %d byte %d\n", count, byte);
-         break;
-
-       case DBUS_TYPE_BOOLEAN:
-          dbus_message_iter_get_basic (&iter, &boolean);
-         printf (" %d boolean %s\n", count, boolean ? "true" : "false");
-         break;
-
-       default:
-         printf (" (dbus-monitor too dumb to decipher arg type '%c')\n", type);
-         break;
-       }
-      
-      count += 1;
-    } while (dbus_message_iter_next (&iter));
 }
 
index ed513fb..6a77c75 100644 (file)
 
 #include "dbus-print-message.h"
 
+static const char *appname;
+
 static void
-usage (char *name, int ecode)
+usage (int ecode)
 {
-  fprintf (stderr, "Usage: %s [--help] [--system | --session] [--dest=NAME] [--type=TYPE] [--print-reply] [--reply-timeout=MSEC] <destination object path> <message name> [contents ...]\n", name);
+  fprintf (stderr, "Usage: %s [--help] [--system | --session] [--dest=NAME] [--type=TYPE] [--print-reply] [--reply-timeout=MSEC] <destination object path> <message name> [contents ...]\n", appname);
   exit (ecode);
 }
 
+static void
+append_arg (DBusMessageIter *iter, int type, const char *value)
+{
+  dbus_uint32_t uint32;
+  dbus_int32_t int32;
+  double d;
+  unsigned char byte;
+  dbus_bool_t v_BOOLEAN;
+  
+  /* FIXME - we are ignoring OOM returns on all these functions */
+  switch (type)
+    {
+    case DBUS_TYPE_BYTE:
+      byte = strtoul (value, NULL, 0);
+      dbus_message_iter_append_basic (iter, DBUS_TYPE_BYTE, &byte);
+      break;
+
+    case DBUS_TYPE_DOUBLE:
+      d = strtod (value, NULL);
+      dbus_message_iter_append_basic (iter, DBUS_TYPE_DOUBLE, &d);
+      break;
+
+    case DBUS_TYPE_INT32:
+      int32 = strtol (value, NULL, 0);
+      dbus_message_iter_append_basic (iter, DBUS_TYPE_INT32, &int32);
+      break;
+
+    case DBUS_TYPE_UINT32:
+      uint32 = strtoul (value, NULL, 0);
+      dbus_message_iter_append_basic (iter, DBUS_TYPE_UINT32, &uint32);
+      break;
+
+    case DBUS_TYPE_STRING:
+      dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &value);
+      break;
+
+    case DBUS_TYPE_OBJECT_PATH:
+      dbus_message_iter_append_basic (iter, DBUS_TYPE_OBJECT_PATH, &value);
+      break;
+
+    case DBUS_TYPE_BOOLEAN:
+      if (strcmp (value, "true") == 0)
+       {
+         v_BOOLEAN = TRUE;
+         dbus_message_iter_append_basic (iter, DBUS_TYPE_BOOLEAN, &v_BOOLEAN);
+       }
+      else if (strcmp (value, "false") == 0)
+       {
+         v_BOOLEAN = FALSE;
+         dbus_message_iter_append_basic (iter, DBUS_TYPE_BOOLEAN, &v_BOOLEAN);
+       }
+      else
+       {
+         fprintf (stderr, "%s: Expected \"true\" or \"false\" instead of \"%s\"\n", appname, value);
+         exit (1);
+       }
+      break;
+
+    default:
+      fprintf (stderr, "%s: Unsupported data type %c\n", appname, (char) type);
+      exit (1);
+    }
+}
+
+static void
+append_array (DBusMessageIter *iter, int type, const char *value)
+{
+  const char *c;
+
+  append_arg (iter, type, value);
+  c = value;
+  while ((c = strchr (c + 1, ',')) != NULL)
+    {
+      append_arg (iter, type, c);
+    }
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -50,9 +129,11 @@ main (int argc, char *argv[])
   const char *path = NULL;
   int message_type = DBUS_MESSAGE_TYPE_SIGNAL;
   const char *type_str = NULL;
+
+  appname = argv[0];
   
   if (argc < 3)
-    usage (argv[0], 1);
+    usage (1);
 
   print_reply = FALSE;
   reply_timeout = -1;
@@ -80,19 +161,19 @@ main (int argc, char *argv[])
       else if (strstr (arg, "--type=") == arg)
        type_str = strchr (arg, '=') + 1;
       else if (!strcmp(arg, "--help"))
-       usage (argv[0], 0);
+       usage (0);
       else if (arg[0] == '-')
-       usage (argv[0], 1);
+       usage (1);
       else if (path == NULL)
         path = arg;
       else if (name == NULL)
         name = arg;
       else
-        usage (argv[0], 1);
+        usage (1);
     }
 
   if (name == NULL)
-    usage (argv[0], 1);
+    usage (1);
 
   if (type_str != NULL)
     {
@@ -175,11 +256,9 @@ main (int argc, char *argv[])
       char *arg;
       char *c;
       int type;
-      dbus_uint32_t uint32;
-      dbus_int32_t int32;
-      double d;
-      unsigned char byte;
-      dbus_bool_t v_BOOLEAN;
+      int container_type;
+      DBusMessageIter *target_iter;
+      DBusMessageIter container_iter;
 
       type = DBUS_TYPE_INVALID;
       arg = argv[i++];
@@ -193,6 +272,25 @@ main (int argc, char *argv[])
 
       *(c++) = 0;
 
+      container_type = DBUS_TYPE_INVALID;
+
+      if (strcmp (arg, "variant") == 0)
+       container_type = DBUS_TYPE_VARIANT;
+      else if (strcmp (arg, "array") == 0)
+       container_type = DBUS_TYPE_ARRAY;
+
+      if (container_type != DBUS_TYPE_INVALID)
+       {
+         arg = c;
+         c = strchr (arg, ':');
+         if (c == NULL)
+           {
+             fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
+             exit (1);
+           }
+         *(c++) = 0;
+       }
+
       if (arg[0] == 0 || !strcmp (arg, "string"))
        type = DBUS_TYPE_STRING;
       else if (!strcmp (arg, "int32"))
@@ -205,60 +303,39 @@ main (int argc, char *argv[])
        type = DBUS_TYPE_BYTE;
       else if (!strcmp (arg, "boolean"))
        type = DBUS_TYPE_BOOLEAN;
+      else if (!strcmp (arg, "objpath"))
+       type = DBUS_TYPE_OBJECT_PATH;
       else
        {
-         fprintf (stderr, "%s: Unknown type \"%s\"\n", argv[0], arg);
+         fprintf (stderr, "%s: Unknown type \"%s\"\n", appname, arg);
          exit (1);
        }
 
-      /* FIXME - we are ignoring OOM returns on all these functions */
-      switch (type)
+      if (container_type != DBUS_TYPE_INVALID)
        {
-       case DBUS_TYPE_BYTE:
-         byte = strtoul (c, NULL, 0);
-         dbus_message_iter_append_basic (&iter, DBUS_TYPE_BYTE, &byte);
-         break;
-
-       case DBUS_TYPE_DOUBLE:
-         d = strtod (c, NULL);
-         dbus_message_iter_append_basic (&iter, DBUS_TYPE_DOUBLE, &d);
-         break;
-
-       case DBUS_TYPE_INT32:
-         int32 = strtol (c, NULL, 0);
-         dbus_message_iter_append_basic (&iter, DBUS_TYPE_INT32, &int32);
-         break;
-
-       case DBUS_TYPE_UINT32:
-         uint32 = strtoul (c, NULL, 0);
-         dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &uint32);
-         break;
-
-       case DBUS_TYPE_STRING:
-         dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &c);
-         break;
-
-       case DBUS_TYPE_BOOLEAN:
-          if (strcmp(c, "true") == 0)
-            {
-              v_BOOLEAN = TRUE;
-              dbus_message_iter_append_basic (&iter, DBUS_TYPE_BOOLEAN, &v_BOOLEAN);
-            }
-         else if (strcmp(c, "false") == 0)
-            {
-              v_BOOLEAN = FALSE;
-              dbus_message_iter_append_basic (&iter, DBUS_TYPE_BOOLEAN, &v_BOOLEAN);
-            }
-         else
-           {
-             fprintf (stderr, "%s: Expected \"true\" or \"false\" instead of \"%s\"\n", argv[0], c);
-             exit (1);
-           }
-         break;
+         char sig[2];
+         sig[0] = type;
+         sig[1] = '\0';
+         dbus_message_iter_open_container (&iter,
+                                           container_type,
+                                           sig,
+                                           &container_iter);
+         target_iter = &container_iter;
+       }
+      else
+       target_iter = &iter;
 
-       default:
-         fprintf (stderr, "%s: Unsupported data type\n", argv[0]);
-         exit (1);
+      if (container_type == DBUS_TYPE_ARRAY)
+       {
+         append_array (target_iter, type, c);
+       }
+      else
+       append_arg (target_iter, type, c);
+
+      if (container_type != DBUS_TYPE_INVALID)
+       {
+         dbus_message_iter_close_container (&iter,
+                                            &container_iter);
        }
     }