dbus/tools dbus-send.c,1.13,1.14 dbus-print-message.c,1.5,1.6

Colin Walters walters at freedesktop.org
Wed May 11 11:48:26 PDT 2005


Update of /cvs/dbus/dbus/tools
In directory gabe:/tmp/cvs-serv19610/tools

Modified Files:
	dbus-send.c dbus-print-message.c 
Log Message:
2005-05-11  Colin Walters  <walters at 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.


Index: dbus-send.c
===================================================================
RCS file: /cvs/dbus/dbus/tools/dbus-send.c,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -d -r1.13 -r1.14
--- dbus-send.c	18 Jan 2005 20:42:15 -0000	1.13
+++ dbus-send.c	11 May 2005 18:48:24 -0000	1.14
@@ -27,13 +27,92 @@
 
 #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 @@
   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 @@
       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 @@
       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 @@
 
       *(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 @@
 	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;
+	  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;
 
-	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;
+      if (container_type == DBUS_TYPE_ARRAY)
+	{
+	  append_array (target_iter, type, c);
+	}
+      else
+	append_arg (target_iter, type, c);
 
-	default:
-	  fprintf (stderr, "%s: Unsupported data type\n", argv[0]);
-	  exit (1);
+      if (container_type != DBUS_TYPE_INVALID)
+	{
+	  dbus_message_iter_close_container (&iter,
+					     &container_iter);
 	}
     }
 

Index: dbus-print-message.c
===================================================================
RCS file: /cvs/dbus/dbus/tools/dbus-print-message.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- dbus-print-message.c	30 Jan 2005 07:44:08 -0000	1.5
+++ dbus-print-message.c	11 May 2005 18:48:24 -0000	1.6
@@ -39,6 +39,94 @@
     }
 }
 
+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 @@
   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 @@
     }
 
   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));
 }
 



More information about the dbus-commit mailing list