[win32] Protect usage of SIGHUP with #ifdef
[platform/upstream/dbus.git] / tools / dbus-send.c
index 37b7206..81a9c37 100644 (file)
@@ -1,4 +1,4 @@
-/* -*- mode: C; c-file-style: "gnu" -*- */
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
 /* dbus-send.c  Utility program to send messages from the command line
  *
  * Copyright (C) 2003 Philip Blundell <philb@gnu.org>
@@ -19,6 +19,7 @@
  *
  */
 
+#include <config.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -32,15 +33,19 @@ static const char *appname;
 static void
 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", appname);
+  fprintf (stderr, "Usage: %s [--help] [--system | --session | --address=ADDRESS] [--dest=NAME] [--type=TYPE] [--print-reply=(literal)] [--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_uint16_t uint16;
+  dbus_int16_t int16;
   dbus_uint32_t uint32;
   dbus_int32_t int32;
+  dbus_uint64_t uint64;
+  dbus_int64_t int64;
   double d;
   unsigned char byte;
   dbus_bool_t v_BOOLEAN;
@@ -58,6 +63,16 @@ append_arg (DBusMessageIter *iter, int type, const char *value)
       dbus_message_iter_append_basic (iter, DBUS_TYPE_DOUBLE, &d);
       break;
 
+    case DBUS_TYPE_INT16:
+      int16 = strtol (value, NULL, 0);
+      dbus_message_iter_append_basic (iter, DBUS_TYPE_INT16, &int16);
+      break;
+
+    case DBUS_TYPE_UINT16:
+      uint16 = strtoul (value, NULL, 0);
+      dbus_message_iter_append_basic (iter, DBUS_TYPE_UINT16, &uint16);
+      break;
+
     case DBUS_TYPE_INT32:
       int32 = strtol (value, NULL, 0);
       dbus_message_iter_append_basic (iter, DBUS_TYPE_INT32, &int32);
@@ -68,6 +83,16 @@ append_arg (DBusMessageIter *iter, int type, const char *value)
       dbus_message_iter_append_basic (iter, DBUS_TYPE_UINT32, &uint32);
       break;
 
+    case DBUS_TYPE_INT64:
+      int64 = strtoll (value, NULL, 0);
+      dbus_message_iter_append_basic (iter, DBUS_TYPE_INT64, &int64);
+      break;
+
+    case DBUS_TYPE_UINT64:
+      uint64 = strtoull (value, NULL, 0);
+      dbus_message_iter_append_basic (iter, DBUS_TYPE_UINT64, &uint64);
+      break;
+
     case DBUS_TYPE_STRING:
       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &value);
       break;
@@ -125,14 +150,10 @@ append_dict (DBusMessageIter *iter, int keytype, int valtype, const char *value)
   while (val != NULL)
     {
       DBusMessageIter subiter;
-      char sig[3];
-      sig[0] = keytype;
-      sig[1] = valtype;
-      sig[2] = '\0';
       
       dbus_message_iter_open_container (iter,
                                        DBUS_TYPE_DICT_ENTRY,
-                                       sig,
+                                       NULL,
                                        &subiter);
 
       append_arg (&subiter, keytype, val);
@@ -156,10 +177,18 @@ type_from_name (const char *arg)
   int type;
   if (!strcmp (arg, "string"))
     type = DBUS_TYPE_STRING;
+  else if (!strcmp (arg, "int16"))
+    type = DBUS_TYPE_INT16;
+  else if (!strcmp (arg, "uint16"))
+    type = DBUS_TYPE_UINT16;
   else if (!strcmp (arg, "int32"))
     type = DBUS_TYPE_INT32;
   else if (!strcmp (arg, "uint32"))
     type = DBUS_TYPE_UINT32;
+  else if (!strcmp (arg, "int64"))
+    type = DBUS_TYPE_INT64;
+  else if (!strcmp (arg, "uint64"))
+    type = DBUS_TYPE_UINT64;
   else if (!strcmp (arg, "double"))
     type = DBUS_TYPE_DOUBLE;
   else if (!strcmp (arg, "byte"))
@@ -183,6 +212,7 @@ main (int argc, char *argv[])
   DBusError error;
   DBusMessage *message;
   int print_reply;
+  int print_reply_literal;
   int reply_timeout;
   DBusMessageIter iter;
   int i;
@@ -192,6 +222,8 @@ main (int argc, char *argv[])
   const char *path = NULL;
   int message_type = DBUS_MESSAGE_TYPE_SIGNAL;
   const char *type_str = NULL;
+  const char *address = NULL;
+  int session_or_system = FALSE;
 
   appname = argv[0];
   
@@ -199,6 +231,7 @@ main (int argc, char *argv[])
     usage (1);
 
   print_reply = FALSE;
+  print_reply_literal = FALSE;
   reply_timeout = -1;
   
   for (i = 1; i < argc && name == NULL; i++)
@@ -206,13 +239,35 @@ main (int argc, char *argv[])
       char *arg = argv[i];
 
       if (strcmp (arg, "--system") == 0)
-       type = DBUS_BUS_SYSTEM;
+        {
+         type = DBUS_BUS_SYSTEM;
+          session_or_system = TRUE;
+        }
       else if (strcmp (arg, "--session") == 0)
-       type = DBUS_BUS_SESSION;
-      else if (strcmp (arg, "--print-reply") == 0)
+        {
+         type = DBUS_BUS_SESSION;
+          session_or_system = TRUE;
+        }
+      else if (strstr (arg, "--address") == arg)
+        {
+          address = strchr (arg, '=');
+
+          if (address == NULL) 
+            {
+              fprintf (stderr, "\"--address=\" requires an ADDRESS\n");
+              usage (1);
+            }
+          else
+            {
+              address = address + 1;
+            }
+        }
+      else if (strncmp (arg, "--print-reply", 13) == 0)
        {
          print_reply = TRUE;
          message_type = DBUS_MESSAGE_TYPE_METHOD_CALL;
+         if (*(arg + 13) != '\0')
+           print_reply_literal = TRUE;
        }
       else if (strstr (arg, "--reply-timeout=") == arg)
        {
@@ -238,6 +293,13 @@ main (int argc, char *argv[])
   if (name == NULL)
     usage (1);
 
+  if (session_or_system &&
+      (address != NULL))
+    {
+      fprintf (stderr, "\"--address\" may not be used with \"--system\" or \"--session\"\n");
+      usage (1);
+    }
+
   if (type_str != NULL)
     {
       message_type = dbus_message_type_from_string (type_str);
@@ -251,11 +313,21 @@ main (int argc, char *argv[])
     }
   
   dbus_error_init (&error);
-  connection = dbus_bus_get (type, &error);
+
+  if (address != NULL)
+    {
+      connection = dbus_connection_open (address, &error);
+    }
+  else
+    {
+      connection = dbus_bus_get (type, &error);
+    }
+
   if (connection == NULL)
     {
-      fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
-              (type == DBUS_BUS_SYSTEM) ? "system" : "session",
+      fprintf (stderr, "Failed to open connection to \"%s\" message bus: %s\n",
+               (address != NULL) ? address :
+                 ((type == DBUS_BUS_SYSTEM) ? "system" : "session"),
                error.message);
       dbus_error_free (&error);
       exit (1);
@@ -303,7 +375,7 @@ main (int argc, char *argv[])
 
   if (message == NULL)
     {
-      fprintf (stderr, "Couldn't allocate D-BUS message\n");
+      fprintf (stderr, "Couldn't allocate D-Bus message\n");
       exit (1);
     }
 
@@ -365,6 +437,7 @@ main (int argc, char *argv[])
 
       if (container_type == DBUS_TYPE_DICT_ENTRY)
        {
+         char sig[5];
          arg = c;
          c = strchr (c, ':');
          if (c == NULL)
@@ -374,7 +447,6 @@ main (int argc, char *argv[])
            }
          *(c++) = 0;
          secondary_type = type_from_name (arg);
-         char sig[5];
          sig[0] = DBUS_DICT_ENTRY_BEGIN_CHAR;
          sig[1] = type;
          sig[2] = secondary_type;
@@ -436,7 +508,7 @@ main (int argc, char *argv[])
 
       if (reply)
         {
-          print_message (reply);
+          print_message (reply, print_reply_literal);
           dbus_message_unref (reply);
         }
     }
@@ -448,7 +520,7 @@ main (int argc, char *argv[])
 
   dbus_message_unref (message);
 
-  dbus_connection_close (connection);
+  dbus_connection_unref (connection);
 
   exit (0);
 }