[patch] make dbus-launch export vars, usage bits

Colin Walters walters@debian.org
16 May 2003 19:08:45 -0400


--=-9N5adMgqmJQdYxjNE4dJ
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Fri, 2003-05-16 at 15:27, Havoc Pennington wrote:
> Hi,
> 
> Tangentially related, we should probably have the same default for 
> --session/--system for all the tools...

Ok, new patch attached.


--=-9N5adMgqmJQdYxjNE4dJ
Content-Disposition: attachment; filename=dbus.patch
Content-Transfer-Encoding: quoted-printable
Content-Type: text/x-patch; name=dbus.patch; charset=UTF-8

Index: ChangeLog
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /home/freedesktop/dbus/ChangeLog,v
retrieving revision 1.361
diff -u -d -r1.361 ChangeLog
--- ChangeLog	16 May 2003 20:09:24 -0000	1.361
+++ ChangeLog	16 May 2003 23:06:03 -0000
@@ -1,3 +1,25 @@
+2003-05-16  Colin Walters  <walters@verbum.org>
+
+	* tools/dbus-send.c: Don't exit with an error code if --help was
+	passed.  Default to using the session bus instead of the system
+	one.
+=09
+	* tools/dbus-launch.c: Ditto.=20
+
+	* tools/dbus-monitor.c: Ditto.
+
+	* tools/dbus-send.1: Update with new arguments.
+=09
+	* tools/dbus-launch.c: Emit code to export variables.  New
+	arguments -s and -c to specify shell syntax, and a bit of code to
+	autodetect syntax.  Also, allow specifying a program to run.
+=09
+	* tools/dbus-launch.1: Update with new arguments.
+=09
+	* tools/dbus-send.1: Ditto.
+
+	* tools/dbus-monitor.1: Ditto.
+=09
 2003-05-16  Havoc Pennington  <hp@redhat.com>
=20
 	* dbus/dbus-connection.c: disable verbose lock spew
Index: tools/dbus-send.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /home/freedesktop/dbus/tools/dbus-send.c,v
retrieving revision 1.3
diff -u -d -r1.3 dbus-send.c
--- tools/dbus-send.c	16 May 2003 20:09:25 -0000	1.3
+++ tools/dbus-send.c	16 May 2003 23:05:59 -0000
@@ -28,10 +28,10 @@
 #include "dbus-print-message.h"
=20
 static void
-usage (char *name)
+usage (char *name, int ecode)
 {
-  fprintf (stderr, "Usage: %s [--session] [--dest=3DSERVICE] [--print-repl=
y] <message type> [contents ...]\n", name);
-  exit (1);
+  fprintf (stderr, "Usage: %s [--help] [--session] [--dest=3DSERVICE] [--p=
rint-reply] <message type> [contents ...]\n", name);
+  exit (ecode);
 }
=20
 int
@@ -43,12 +43,12 @@
   int print_reply;
   DBusMessageIter iter;
   int i;
-  DBusBusType type =3D DBUS_BUS_SYSTEM;
+  DBusBusType type =3D DBUS_BUS_SESSION;
   char *dest =3D DBUS_SERVICE_BROADCAST;
   char *name =3D NULL;
=20
   if (argc < 2)
-    usage (argv[0]);
+    usage (argv[0], 1);
=20
   print_reply =3D FALSE;
  =20
@@ -56,20 +56,22 @@
     {
       char *arg =3D argv[i];
=20
-      if (strcmp (arg, "--session") =3D=3D 0)
-	type =3D DBUS_BUS_SESSION;
+      if (strcmp (arg, "--system") =3D=3D 0)
+	type =3D DBUS_BUS_SYSTEM;
       else if (strcmp (arg, "--print-reply") =3D=3D 0)
         print_reply =3D TRUE;
       else if (strstr (arg, "--dest=3D") =3D=3D arg)
 	dest =3D strchr (arg, '=3D') + 1;
+      else if (!strcmp(arg, "--help"))
+	usage (argv[0], 0);
       else if (arg[0] =3D=3D '-')
-	usage (argv[0]);
+	usage (argv[0], 1);
       else
 	name =3D arg;
     }
=20
   if (name =3D=3D NULL)
-    usage (argv[0]);
+    usage (argv[0], 1);
=20
   dbus_error_init (&error);
   connection =3D dbus_bus_get (type, &error);
Index: tools/dbus-send.1
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /home/freedesktop/dbus/tools/dbus-send.1,v
retrieving revision 1.2
diff -u -d -r1.2 dbus-send.1
--- tools/dbus-send.1	16 May 2003 20:09:25 -0000	1.2
+++ tools/dbus-send.1	16 May 2003 23:05:59 -0000
@@ -8,7 +8,7 @@
 .SH SYNOPSIS
 .PP
 .B dbus-send
-[\-\-session] [\-\-dest=3DSERVICE] [\-\-print-reply] <message name> [conte=
nts ...]
+[\-\-system] [\-\-dest=3DSERVICE] [\-\-print-reply] <message name> [conten=
ts ...]
=20
 .SH DESCRIPTION
=20
@@ -20,8 +20,8 @@
 There are two well-known message buses: the systemwide message bus=20
 (installed on many systems as the "messagebus" service) and the=20
 per-user-login-session message bus (started each time a user logs in).
-\fIdbus-send\fP sends messages to the system bus by default, and=20
-to the per-session bus if you specify \-\-session.
+\fIdbus-send\fP sends messages to the session bus by default, and=20
+to the system bus if you specify \-\-system.
=20
 .PP=20
 Nearly all uses of \fIdbus-send\fP must provide the \-\-dest=20
Index: tools/dbus-monitor.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /home/freedesktop/dbus/tools/dbus-monitor.c,v
retrieving revision 1.4
diff -u -d -r1.4 dbus-monitor.c
--- tools/dbus-monitor.c	16 May 2003 20:09:25 -0000	1.4
+++ tools/dbus-monitor.c	16 May 2003 23:05:59 -0000
@@ -46,7 +46,7 @@
 static void
 usage (char *name, int ecode)
 {
-  fprintf (stderr, "Usage: %s [--session]\n", name);
+  fprintf (stderr, "Usage: %s [--system]\n", name);
   exit (ecode);
 }
=20
@@ -55,7 +55,7 @@
 {
   DBusConnection *connection;
   DBusError error;
-  DBusBusType type =3D DBUS_BUS_SYSTEM;
+  DBusBusType type =3D DBUS_BUS_SESSION;
   DBusMessageHandler *handler;
   GMainLoop *loop;
   int i;
@@ -65,7 +65,7 @@
       char *arg =3D argv[i];
=20
       if (!strcmp (arg, "--session"))
-	type =3D DBUS_BUS_SESSION;
+	type =3D DBUS_BUS_SYSTEM;
       else if (!strcmp (arg, "--help"))
 	usage (argv[0], 0);
       else if (!strcmp (arg, "--"))
Index: tools/dbus-monitor.1
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /home/freedesktop/dbus/tools/dbus-monitor.1,v
retrieving revision 1.2
diff -u -d -r1.2 dbus-monitor.1
--- tools/dbus-monitor.1	16 May 2003 16:01:56 -0000	1.2
+++ tools/dbus-monitor.1	16 May 2003 23:05:59 -0000
@@ -8,7 +8,7 @@
 .SH SYNOPSIS
 .PP
 .B dbus-monitor
-[\-\-session]
+[\-\-system]
=20
 .SH DESCRIPTION
=20
@@ -21,8 +21,8 @@
 There are two well-known message buses: the systemwide message bus
 (installed on many systems as the "messagebus" service) and the
 per-user-login-session message bus (started each time a user logs in).
-\fIdbus-monitor\fP by default monitors the systemwide bus; to monitor the
-session bus, specify \-\-session.
+\fIdbus-monitor\fP by default monitors the session bus; to monitor the
+system bus, specify \-\-system.
=20
 .PP=20
 The message bus configuration may keep \fIdbus-monitor\fP from seeing
Index: tools/dbus-launch.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /home/freedesktop/dbus/tools/dbus-launch.c,v
retrieving revision 1.2
diff -u -d -r1.2 dbus-launch.c
--- tools/dbus-launch.c	4 May 2003 08:54:24 -0000	1.2
+++ tools/dbus-launch.c	16 May 2003 23:06:00 -0000
@@ -77,10 +77,10 @@
 }
=20
 static void
-usage (void)
+usage (int ecode)
 {
-  fprintf (stderr, "dbus-launch [--version] [--exit-with-session]\n");
-  exit (1);
+  fprintf (stderr, "dbus-launch [--version] [--help] [--sh-syntax] [--csh-=
syntax] [--auto-syntax] [--exit-with-session]\n");
+  exit (ecode);
 }
=20
 static void
@@ -543,7 +543,13 @@
 main (int argc, char **argv)
 {
   const char *prev_arg;
+  const char *shname;
+  char *runprog =3D NULL;
+  int remaining_args =3D 0;
   int exit_with_session;
+  int c_shell_syntax =3D FALSE;
+  int bourne_shell_syntax =3D FALSE;
+  int auto_shell_syntax =3D FALSE;
   int i; =20
   int ret;
   int bus_pid_to_launcher_pipe[2];
@@ -561,20 +567,48 @@
       if (strcmp (arg, "--help") =3D=3D 0 ||
           strcmp (arg, "-h") =3D=3D 0 ||
           strcmp (arg, "-?") =3D=3D 0)
-        usage ();
+        usage (0);
+      else if (strcmp (arg, "--auto-syntax") =3D=3D 0)
+        auto_shell_syntax =3D TRUE;
+      else if (strcmp (arg, "-c") =3D=3D 0 ||
+	       strcmp (arg, "--csh-syntax") =3D=3D 0)
+        c_shell_syntax =3D TRUE;
+      else if (strcmp (arg, "-s") =3D=3D 0 ||
+	       strcmp (arg, "--sh-syntax") =3D=3D 0)
+        bourne_shell_syntax =3D TRUE;
       else if (strcmp (arg, "--version") =3D=3D 0)
         version ();
       else if (strcmp (arg, "--exit-with-session") =3D=3D 0)
         exit_with_session =3D TRUE;
+      else if (runprog)
+	usage (1);
       else
-        usage ();
+	{
+	  runprog =3D strdup (arg);
+	  remaining_args =3D i+1;
+	  break;
+	}
      =20
       prev_arg =3D arg;
      =20
       ++i;
     }
=20
-  verbose ("--exit-with-session provided\n");
+  if (exit_with_session)
+    verbose ("--exit-with-session enabled\n");
+
+  if (auto_shell_syntax)
+    {
+      if ((shname =3D getenv ("SHELL")) !=3D NULL)
+       {
+         if (!strncmp (shname + strlen(shname) -3, "csh", 3))
+           c_shell_syntax =3D TRUE;
+         else
+           bourne_shell_syntax =3D TRUE;
+       }
+      else
+       bourne_shell_syntax =3D TRUE;
+    } =20
=20
   if (pipe (bus_pid_to_launcher_pipe) < 0 ||
       pipe (bus_address_to_launcher_pipe) < 0)
@@ -737,17 +771,56 @@
         }
=20
       close (bus_pid_to_launcher_pipe[READ_END]);
+     =20
+      if (runprog)
+	{
+	  char *envvar =3D malloc (strlen ("DBUS_SESSION_BUS_ADDRESS=3D") + FILEN=
AME_MAX + 1);
+	  char **args =3D malloc (sizeof (char *) * ((argc-remaining_args)+2));
+	  if (envvar =3D=3D NULL || args =3D=3D NULL)
+	    goto oom;
=20
-      printf ("DBUS_SESSION_BUS_ADDRESS=3D'%s'\n",
-              bus_address);
+	  args[0] =3D runprog;
+	  for (i =3D 1; i <=3D (argc-remaining_args); i++)
+	    {
+	      size_t len =3D strlen (argv[remaining_args+i-1])+1;
+	      args[i] =3D malloc (len);
+	      if (!args[i])
+		goto oom;
+	      strncpy (args[i], argv[remaining_args+i-1], len);
+	    }
+	  args[i] =3D NULL;
=20
-      printf ("DBUS_SESSION_BUS_PID=3D%ld\n",
-              (long) bus_pid);
+	  strcat (envvar, "DBUS_SESSION_BUS_ADDRESS=3D");
+	  strcat (envvar, bus_address);
+	  putenv (envvar);
=20
+	  execvp (runprog, args);
+	  fprintf (stderr, "Couldn't exec %s: %s\n", runprog, strerror (errno));
+	  exit (1);
+	}
+      else
+	{
+	  if (c_shell_syntax)
+	    printf ("setenv DBUS_SESSION_BUS_ADDRESS '%s'\n", bus_address);=09
+	  else
+	    {
+	      printf ("DBUS_SESSION_BUS_ADDRESS=3D'%s'\n", bus_address);
+	      if (bourne_shell_syntax)
+		printf ("export DBUS_SESSION_BUS_ADDRESS\n");
+	    }
+	  if (c_shell_syntax)
+	    printf ("set DBUS_SESSION_BUS_PID=3D%ld\n", (long) bus_pid);
+	  else
+	    printf ("DBUS_SESSION_BUS_PID=3D%ld\n", (long) bus_pid);
+	}
+	 =20
       verbose ("dbus-launch exiting\n");
      =20
       exit (0);
     }=20
  =20
   return 0;
+ oom:
+  fprintf (stderr, "Out of memory!");
+  exit (1);
 }
Index: tools/dbus-launch.1
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /home/freedesktop/dbus/tools/dbus-launch.1,v
retrieving revision 1.2
diff -u -d -r1.2 dbus-launch.1
--- tools/dbus-launch.1	4 May 2003 08:54:24 -0000	1.2
+++ tools/dbus-launch.1	16 May 2003 23:06:00 -0000
@@ -7,7 +7,7 @@
 dbus-launch \- Utility to start a message bus from a shell script
 .SH SYNOPSIS
 .PP
-.B dbus-launch [\-\-version] [\-\-exit-with-session]
+.B dbus-launch [\-\-version] [\-\-sh-syntax] [\-\-csh-syntax] [\-\-auto-sy=
ntax] [\-\-exit-with-session] [PROGRAM] [ARGS...]
=20
 .SH DESCRIPTION
=20
@@ -15,8 +15,25 @@
 from a shell script. It would normally be called from a user's login
 scripts. Unlike the daemon itself, \fIdbus-launch\fP exits, so
 backticks or the $() construct can be used to read information from
-\fIdbus-launch\fP. \fIdbus-launch\fP prints information about the
-launched daemon in KEY=3DVALUE format.
+\fIdbus-launch\fP.
+
+With no arguments, \fIdbus-launch\fP will simply print the values of
+DBUS_SESSION_BUS_ADDRESS and DBUS_SESSION_BUS_PID.
+
+You may specify a program to be run; in this case, \fIdbus-launch\fP
+will then set the appropriate environment variables and execute the
+specified program, with the specified arguments.  See below for
+examples.
+
+Finally, you may use the \-\-auto-syntax command to cause
+\fIdbus-launch\fP to emit shell code to set up the environment.  This
+is useful in shell scripts.  With this option, \fIdbus-launch\fP looks
+at the value of the SHELL environment variable to determine which
+shell syntax should be used.  If SHELL ends in "csh", then
+csh-compatible code is emitted; otherwise Bourne shell code is
+emitted.  Instead of passing \-\-auto-syntax, you may explicity
+specify a particular one by using \-\-sh-syntax for Bourne syntax, or
+\-\-csh-syntax for csh syntax.
=20
 .PP
 See http://www.freedesktop.org/software/dbus/ for more information
@@ -30,26 +47,48 @@
   ## test for an existing bus daemon, just to be safe
   if test -z "$DBUS_SESSION_BUS_ADDRESS" ; then
       ## if not found, launch a new one
-      eval `dbus-launch --exit-with-session`
+      eval `dbus-launch --auto-syntax --exit-with-session`
       echo "D-BUS per-session daemon address is: $DBUS_SESSION_BUS_ADDRESS=
"
-      export DBUS_SESSION_BUS_ADDRESS
   fi
=20
 .fi
 You might run something like that in your login scripts.
=20
+.PP
+Another way to use \fIdbus-launch\fP is to run your main session
+program, like so:
+.nf
+
+dbus-launch gnome-session
+
+.fi
+The above would likely be appropriate for ~/.xsession.
+
 .SH OPTIONS
 The following options are supported:
 .TP
+.I "--version"
+Print the version of dbus-launch
+
+.TP
+.I "--sh-syntax"
+Emit Bourne-shell compatible code.
+
+.TP
+.I "--csh-syntax"
+Emit csh compatible code.
+
+.TP
+.I "--auto-syntax"
+Attempt to detect the shell in use, and emit compatible code.
+
+.TP
 .I "--exit-with-session"
 If this option is provided, a persistent "babysitter" process will be=20
 created that watches stdin for HUP and tries to connect to the X
 server. If this process gets a HUP on stdin or loses its X connection,
 it kills the message bus daemon.
=20
-.TP
-.I "--version"
-Print the version of dbus-launch
=20
 .SH AUTHOR
 See http://www.freedesktop.org/software/dbus/doc/AUTHORS

--=-9N5adMgqmJQdYxjNE4dJ--