Subject: Re: bug#1298: allow 'emacsclient -a "emacs --daemon && emacsclient -c"'

From: Dan Nicolaescu <[email-masked]>
Date: Tue, 9 Dec 2008 10:58:32 -0800 (PST)

Stefan Monnier <[email-masked]> writes:

  > >> No need to worry about that: I've already explicitly rejected a tcp-port
  > >> option for server.el in the past, so I'll be happy to give other such
  > >> rejections in the future.
  > 
  > > Hmm, you said the reverse here:
  > > http://article.gmane.org/gmane.emacs.devel/103350
  > 
  > Indeed.  Consistency is the hgolm, isn't it?

hgolm?

Updated patch that uses -a ""  or --alternate-editor= or
ALTERNATE_EDITOR="":

Index: emacsclient.c
===================================================================
RCS file: /cvsroot/emacs/emacs/lib-src/emacsclient.c,v
retrieving revision 1.142
diff -u -3 -p -u -p -r1.142 emacsclient.c
--- emacsclient.c	3 Dec 2008 04:33:44 -0000	1.142
+++ emacsclient.c	9 Dec 2008 18:53:51 -0000
@@ -1294,7 +1294,7 @@ To start the server in Emacs, type \"M-x
 #endif /* ! NO_SOCKETS_IN_FILE_SYSTEM */
 
 HSOCKET
-set_socket ()
+set_socket (int no_exit_if_error)
 {
   HSOCKET s;
 
@@ -1305,7 +1305,7 @@ set_socket ()
   if (socket_name)
     {
       s = set_local_socket ();
-      if ((s != INVALID_SOCKET) || alternate_editor)
+      if ((s != INVALID_SOCKET) || no_exit_if_error)
 	return s;
       message (TRUE, "%s: error accessing socket \"%s\"\n",
 	       progname, socket_name);
@@ -1320,7 +1320,7 @@ set_socket ()
   if (server_file)
     {
       s = set_tcp_socket ();
-      if ((s != INVALID_SOCKET) || alternate_editor)
+      if ((s != INVALID_SOCKET) || no_exit_if_error)
 	return s;
 
       message (TRUE, "%s: error accessing server file \"%s\"\n",
@@ -1338,7 +1338,7 @@ set_socket ()
   /* Implicit server file.  */
   server_file = "server";
   s = set_tcp_socket ();
-  if ((s != INVALID_SOCKET) || alternate_editor)
+  if ((s != INVALID_SOCKET) || no_exit_if_error)
     return s;
 
   /* No implicit or explicit socket, and no alternate editor.  */
@@ -1416,6 +1416,7 @@ main (argc, argv)
   int i, rl, needlf = 0;
   char *cwd, *str;
   char string[BUFSIZ+1];
+  int null_socket_name, null_server_file, start_daemon_if_needed;
 
   main_argv = argv;
   progname = argv[0];
@@ -1431,9 +1432,68 @@ main (argc, argv)
       exit (EXIT_FAILURE);
     }
 
-  if ((emacs_socket = set_socket ()) == INVALID_SOCKET)
-    fail ();
-
+  start_daemon_if_needed = (alternate_editor
+			    && (alternate_editor[0] == '\0'));
+  if (start_daemon_if_needed)
+    {
+      /* set_socket changes the values for socket_name and
+	 server_file, we need to reset them, if they were NULL before
+	 for the second call to set_socket.  */
+      null_socket_name = (socket_name == NULL);
+      null_server_file = (server_file == NULL);
+    }
+
+  if ((emacs_socket = set_socket (alternate_editor
+				  || start_daemon_if_needed)) == INVALID_SOCKET)
+    if (start_daemon_if_needed)
+      {
+	pid_t dpid;
+	int status;
+	pid_t p;
+
+	/* Reset socket_name and server_file if they were NULL
+	   before the set_socket call.  */
+	if (null_socket_name)
+	  socket_name = NULL;
+	if (null_server_file)
+	  server_file = NULL;
+
+	dpid = fork ();
+
+	if (dpid > 0)
+	  {
+	    p = waitpid (dpid, &status, WUNTRACED | WCONTINUED);
+
+	    /* Try connecting again, the daemon should have started by
+	       now.  */
+	    message (TRUE, "daemon should have started, trying to connect again\n", dpid);
+	    if ((emacs_socket = set_socket (1)) == INVALID_SOCKET)
+	      message (TRUE, "Cannot connect even after starting the daemon\n");
+	  }
+	else if (dpid < 0)
+	  {
+	    fprintf (stderr, "Cannot fork!\n");
+	    exit (1);
+	  }
+	else
+	  {
+	    char *d_argv[] = {"emacs", "--daemon", 0 };
+	    if (!null_socket_name)
+	      {
+		/* Pass  --daemon=socket_name as argument.  */
+		char *deq = "--daemon=";
+		char *daemon_arg = alloca (strlen (deq)
+					   + strlen (socket_name) + 1);
+		strcpy (daemon_arg, deq);
+		strcat (daemon_arg, socket_name);
+		d_argv[1] = daemon_arg;
+	      }
+	    execvp ("emacs", d_argv);
+	    message (TRUE, "%s: error starting emacs daemon\n", progname);
+	  }
+      }
+    else
+      fail ();
 
   cwd = get_current_dir_name ();
   if (cwd == 0)