[daemon-fix] Registering starters: unwanted release_kdbus_name when no error was...
[platform/upstream/dbus.git] / tools / dbus-print-message.c
index f934c7d..75d00ac 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
  */
+
+#include <config.h>
 #include "dbus-print-message.h"
 
+#include <stdlib.h>
+#include "config.h"
+
 static const char*
 type_to_name (int message_type)
 {
@@ -49,11 +54,9 @@ indent (int depth)
 }
 
 static void
-print_ay (DBusMessageIter *iter, int depth)
+print_hex (unsigned char *bytes, unsigned int len, int depth)
 {
-  int current_type;
-  int n = 0;
-  int columns;
+  unsigned int i, columns;
 
   printf ("array of bytes [\n");
 
@@ -65,26 +68,19 @@ print_ay (DBusMessageIter *iter, int depth)
   if (columns < 8)
     columns = 8;
 
-  current_type = dbus_message_iter_get_arg_type (iter);
+  i = 0;
 
-  while (current_type != DBUS_TYPE_INVALID)
+  while (i < len)
     {
-      unsigned char val;
-      dbus_message_iter_get_basic (iter, &val);
-      printf ("%02x", val);
-
-      n++;
-
-      dbus_message_iter_next (iter);
-      current_type = dbus_message_iter_get_arg_type (iter);
+      printf ("%02x", bytes[i]);
+      i++;
 
-      if (current_type != DBUS_TYPE_INVALID)
+      if (i != len)
         {
-          if (n == columns)
+          if (i % columns == 0)
             {
               printf ("\n");
               indent (depth + 1);
-              n = 0;
             }
           else
             {
@@ -98,6 +94,54 @@ print_ay (DBusMessageIter *iter, int depth)
   printf ("]\n");
 }
 
+#define DEFAULT_SIZE 100
+
+static void
+print_ay (DBusMessageIter *iter, int depth)
+{
+  /* Not using DBusString because it's not public API. It's 2009, and I'm
+   * manually growing a string chunk by chunk.
+   */
+  unsigned char *bytes = malloc (DEFAULT_SIZE + 1);
+  unsigned int len = 0;
+  unsigned int max = DEFAULT_SIZE;
+  dbus_bool_t all_ascii = TRUE;
+  int current_type;
+
+  while ((current_type = dbus_message_iter_get_arg_type (iter))
+          != DBUS_TYPE_INVALID)
+    {
+      unsigned char val;
+
+      dbus_message_iter_get_basic (iter, &val);
+      bytes[len] = val;
+      len++;
+
+      if (val < 32 || val > 126)
+        all_ascii = FALSE;
+
+      if (len == max)
+        {
+          max *= 2;
+          bytes = realloc (bytes, max + 1);
+        }
+
+      dbus_message_iter_next (iter);
+    }
+
+  if (all_ascii)
+    {
+      bytes[len] = '\0';
+      printf ("array of bytes \"%s\"\n", bytes);
+    }
+  else
+    {
+      print_hex (bytes, len, depth);
+    }
+
+  free (bytes);
+}
+
 static void
 print_iter (DBusMessageIter *iter, dbus_bool_t literal, int depth)
 {
@@ -184,7 +228,11 @@ print_iter (DBusMessageIter *iter, dbus_bool_t literal, int depth)
          {
            dbus_int64_t val;
            dbus_message_iter_get_basic (iter, &val);
-           printf ("int64 %lld\n", val);
+#ifdef DBUS_INT64_PRINTF_MODIFIER
+        printf ("int64 %" DBUS_INT64_PRINTF_MODIFIER "d\n", val);
+#else
+        printf ("int64 (omitted)\n");
+#endif
            break;
          }
 
@@ -192,7 +240,11 @@ print_iter (DBusMessageIter *iter, dbus_bool_t literal, int depth)
          {
            dbus_uint64_t val;
            dbus_message_iter_get_basic (iter, &val);
-           printf ("uint64 %llu\n", val);
+#ifdef DBUS_INT64_PRINTF_MODIFIER
+        printf ("uint64 %" DBUS_INT64_PRINTF_MODIFIER "u\n", val);
+#else
+        printf ("uint64 (omitted)\n");
+#endif
            break;
          }