unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Dan Nicolaescu <dann@ics.uci.edu>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: 1298@emacsbugs.donarmstrong.com
Subject: bug#1298: allow 'emacsclient -a "emacs --daemon && emacsclient -c"'
Date: Sun, 7 Dec 2008 23:54:33 -0800 (PST)	[thread overview]
Message-ID: <200812080754.mB87sXSV016391@mothra.ics.uci.edu> (raw)
In-Reply-To: <jwvskposbip.fsf-monnier+emacsbugreports@gnu.org> (Stefan Monnier's message of "Tue, 18 Nov 2008 21:21:29 -0500")

Stefan Monnier <monnier@iro.umontreal.ca> writes:

  > > How about something like:
  > > emacsclient --start-daemon 
  > 
  > You mean "emacsclient --start-daemon-if-needed FILENAME" ?
  > Yes, probably something like that.  It could potentially be folded into
  > the alternate-editor (e.g. if alternate-editor has some special value
  > such as the empty string).

How does the patch below look like?
There's one refinement possible: make --start-daemon-if-needed take an
argument --start-daemon-if-needed=ARG and use it like this: emacs --daemon=ARG

Index: emacsclient.c
===================================================================
RCS file: /cvsroot/emacs/emacs/lib-src/emacsclient.c,v
retrieving revision 1.142
diff -u -3 -p -c -r1.142 emacsclient.c
cvs diff: conflicting specifications of output style
--- emacsclient.c	3 Dec 2008 04:33:44 -0000	1.142
+++ emacsclient.c	8 Dec 2008 07:52:45 -0000
@@ -150,6 +150,10 @@
 /* PID of the Emacs server process.  */
 int emacs_pid = 0;
 
+/* In case connecting to the server fails, try connecting again after
+   starting the emacs daemon.  */
+int start_daemon_if_needed = 0;
+
 void print_help_and_exit () NO_RETURN;
 
 struct option longopts[] =
@@ -162,6 +166,7 @@
   { "nw",	no_argument,       NULL, 't' },
   { "create-frame", no_argument,   NULL, 'c' },
   { "alternate-editor", required_argument, NULL, 'a' },
+  { "start-daemon-if-needed", no_argument, NULL, 'D' },
 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
   { "socket-name",	required_argument, NULL, 's' },
 #endif
@@ -492,7 +497,7 @@
     {
       int opt = getopt_long_only (argc, argv,
 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
-			     "VHnea:s:f:d:tc",
+			     "VHneDa:s:f:d:tc",
 #else
 			     "VHnea:f:d:tc",
 #endif
@@ -552,6 +557,10 @@
           current_frame = 0;
           break;
 
+        case 'D':
+          start_daemon_if_needed = 1;
+          break;
+
 	case 'H':
 	  print_help_and_exit ();
 	  break;
@@ -563,6 +572,12 @@
 	}
     }
 
+  if (start_daemon_if_needed && alternate_editor != NULL)
+    {
+      message (TRUE, "--alternate-editor cannot be used when using --start_daemon_if_needed\n");
+      exit (EXIT_FAILURE);
+    }
+
   /* If the -c option is used (without -t) and no --display argument
      is provided, try $DISPLAY.
      Without the -c option, we used to set `display' to $DISPLAY by
@@ -1294,7 +1309,7 @@
 #endif /* ! NO_SOCKETS_IN_FILE_SYSTEM */
 
 HSOCKET
-set_socket ()
+set_socket (int no_exit_if_error)
 {
   HSOCKET s;
 
@@ -1305,7 +1320,7 @@
   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 +1335,7 @@
   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 +1353,7 @@
   /* 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.  */
@@ -1353,7 +1368,7 @@
 }
 
 #ifdef WINDOWSNT
-FARPROC set_fg;  /* Pointer to AllowSetForegroundWindow.  */
+FARPROC se t_fg;  /* Pointer to AllowSetForegroundWindow.  */
 FARPROC get_wc;  /* Pointer to RealGetWindowClassA.  */
 
 BOOL CALLBACK
@@ -1416,6 +1431,8 @@
   int i, rl, needlf = 0;
   char *cwd, *str;
   char string[BUFSIZ+1];
+  int null_socket_name;
+  int null_server_file;
 
   main_argv = argv;
   progname = argv[0];
@@ -1431,9 +1448,57 @@
       exit (EXIT_FAILURE);
     }
 
-  if ((emacs_socket = set_socket ()) == INVALID_SOCKET)
-    fail ();
+  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;
+
+	dpid = fork ();
 
+	if (dpid > 0)
+	  {
+	    p = waitpid (dpid, &status, WUNTRACED | WCONTINUED);
+
+	    /* 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;
+
+	    /* 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 *const d_argv[] = {"emacs", "--daemon", 0 };
+
+	    /* Start the daemon. */
+	    execvp ("emacs", d_argv);
+	    message (TRUE, "%s: error starting emacs daemon\n", progname);
+	  }
+      }
+    else
+      fail ();
 
   cwd = get_current_dir_name ();
   if (cwd == 0)






  reply	other threads:[~2008-12-08  7:54 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <200812112123.mBBLNGpI001493@mothra.ics.uci.edu>
2008-11-02 18:18 ` bug#1298: allow 'emacsclient -a "emacs --daemon && emacsclient -c"' Dan Nicolaescu
2008-11-18  8:22   ` Dan Nicolaescu
2008-11-18 16:36     ` Stefan Monnier
2008-11-18 18:17       ` Dan Nicolaescu
2008-11-18 21:34         ` Stefan Monnier
2008-11-18 22:19           ` Dan Nicolaescu
2008-11-18 23:01             ` Stefan Monnier
2008-11-18 23:27               ` Dan Nicolaescu
2008-11-19  2:21                 ` Stefan Monnier
2008-12-08  7:54                   ` Dan Nicolaescu [this message]
2008-12-08 16:44                     ` Stefan Monnier
2008-12-08 17:09                       ` Dan Nicolaescu
2008-12-08 20:07                         ` Stefan Monnier
2008-12-08 20:20                           ` Dan Nicolaescu
2008-12-08 21:48                             ` Stefan Monnier
2008-12-09 18:58                               ` Dan Nicolaescu
2008-12-09 19:30                                 ` Juanma Barranquero
2008-12-09 20:07                                 ` Stefan Monnier
2008-12-10  7:58                                   ` Dan Nicolaescu
2008-12-10  9:49                                     ` Juanma Barranquero
2008-12-10 15:03                                       ` Dan Nicolaescu
2008-12-10 15:10                                         ` Juanma Barranquero
2008-12-10 15:29                                           ` Dan Nicolaescu
2008-12-10 15:32                                             ` Juanma Barranquero
2008-12-10 15:43                                               ` Dan Nicolaescu
2008-12-10 15:55                                                 ` Juanma Barranquero
2008-12-11 16:37                                         ` Stefan Monnier
2008-12-11 17:08                                           ` Dan Nicolaescu
2008-12-11 19:05                                             ` Juanma Barranquero
2008-12-11 19:06                                             ` Stefan Monnier
2008-12-11 19:08                                               ` Juanma Barranquero
2008-12-11 19:28                                               ` Dan Nicolaescu
2008-12-11 19:58                                                 ` Juanma Barranquero
2008-12-11 20:39                                                   ` Dan Nicolaescu
2008-12-11 20:48                                                     ` Juanma Barranquero
2008-12-11 20:58                                                 ` Stefan Monnier
2008-11-18 23:01             ` Andreas Schwab
2008-11-18 19:43     ` Eli Zaretskii
2008-12-11 21:25   ` bug#1298: marked as done (allow 'emacsclient -a "emacs --daemon && emacsclient -c"') Emacs bug Tracking System

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200812080754.mB87sXSV016391@mothra.ics.uci.edu \
    --to=dann@ics.uci.edu \
    --cc=1298@emacsbugs.donarmstrong.com \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).