aboutsummaryrefslogtreecommitdiffstats
path: root/lib-src
diff options
context:
space:
mode:
authorDan Nicolaescu2008-12-10 07:56:51 +0000
committerDan Nicolaescu2008-12-10 07:56:51 +0000
commit636b507bcc99120c6e9479541aa99fb737855de0 (patch)
tree9969e6e2ecc3b9392378139d2f11442ad6f09dee /lib-src
parente54be9a0323e16b3074983b9c28fb55452e196e3 (diff)
downloademacs-636b507bcc99120c6e9479541aa99fb737855de0.tar.gz
emacs-636b507bcc99120c6e9479541aa99fb737855de0.zip
* misc.texi (emacsclient Options): Describe what an empty string
argument does for --alternate-editor. * emacsclient.1: Describe what an empty string argument does for --alternate-editor. * emacsclient.c (print_help_and_exit): Describe what an empty string argument does for --alternate-editor. (set_socket): Make it possible to not exit in case of an error. (start_daemon_and_retry_set_socket): New function. (main): Use it. Restore the NULL value for socket_name and server_file after the set_socket call.
Diffstat (limited to 'lib-src')
-rw-r--r--lib-src/ChangeLog9
-rw-r--r--lib-src/emacsclient.c86
2 files changed, 89 insertions, 6 deletions
diff --git a/lib-src/ChangeLog b/lib-src/ChangeLog
index acd9ef9f3cf..7cb4cda2013 100644
--- a/lib-src/ChangeLog
+++ b/lib-src/ChangeLog
@@ -1,3 +1,12 @@
12008-12-10 Dan Nicolaescu <dann@ics.uci.edu>
2
3 * emacsclient.c (print_help_and_exit): Describe what an empty
4 string argument does for --alternate-editor.
5 (set_socket): Make it possible to not exit in case of an error.
6 (start_daemon_and_retry_set_socket): New function.
7 (main): Use it. Restore the NULL value for socket_name and
8 server_file after the set_socket call.
9
12008-12-03 Dan Nicolaescu <dann@ics.uci.edu> 102008-12-03 Dan Nicolaescu <dann@ics.uci.edu>
2 11
3 * emacsclient.c: Include <arpa/inet.h>. 12 * emacsclient.c: Include <arpa/inet.h>.
diff --git a/lib-src/emacsclient.c b/lib-src/emacsclient.c
index 2a5b23faeaf..e96cf4ef358 100644
--- a/lib-src/emacsclient.c
+++ b/lib-src/emacsclient.c
@@ -616,6 +616,8 @@ The following OPTIONS are accepted:\n\
616 Set filename of the TCP authentication file\n\ 616 Set filename of the TCP authentication file\n\
617-a, --alternate-editor=EDITOR\n\ 617-a, --alternate-editor=EDITOR\n\
618 Editor to fallback to if the server is not running\n\ 618 Editor to fallback to if the server is not running\n\
619 If EDITOR is the empty string, start Emacs in daemon\n\
620 mode and try connecting again
619\n\ 621\n\
620Report bugs to bug-gnu-emacs@gnu.org.\n", progname); 622Report bugs to bug-gnu-emacs@gnu.org.\n", progname);
621 exit (EXIT_SUCCESS); 623 exit (EXIT_SUCCESS);
@@ -1294,7 +1296,7 @@ To start the server in Emacs, type \"M-x server-start\".\n",
1294#endif /* ! NO_SOCKETS_IN_FILE_SYSTEM */ 1296#endif /* ! NO_SOCKETS_IN_FILE_SYSTEM */
1295 1297
1296HSOCKET 1298HSOCKET
1297set_socket () 1299set_socket (int no_exit_if_error)
1298{ 1300{
1299 HSOCKET s; 1301 HSOCKET s;
1300 1302
@@ -1305,7 +1307,7 @@ set_socket ()
1305 if (socket_name) 1307 if (socket_name)
1306 { 1308 {
1307 s = set_local_socket (); 1309 s = set_local_socket ();
1308 if ((s != INVALID_SOCKET) || alternate_editor) 1310 if ((s != INVALID_SOCKET) || no_exit_if_error)
1309 return s; 1311 return s;
1310 message (TRUE, "%s: error accessing socket \"%s\"\n", 1312 message (TRUE, "%s: error accessing socket \"%s\"\n",
1311 progname, socket_name); 1313 progname, socket_name);
@@ -1320,7 +1322,7 @@ set_socket ()
1320 if (server_file) 1322 if (server_file)
1321 { 1323 {
1322 s = set_tcp_socket (); 1324 s = set_tcp_socket ();
1323 if ((s != INVALID_SOCKET) || alternate_editor) 1325 if ((s != INVALID_SOCKET) || no_exit_if_error)
1324 return s; 1326 return s;
1325 1327
1326 message (TRUE, "%s: error accessing server file \"%s\"\n", 1328 message (TRUE, "%s: error accessing server file \"%s\"\n",
@@ -1338,7 +1340,7 @@ set_socket ()
1338 /* Implicit server file. */ 1340 /* Implicit server file. */
1339 server_file = "server"; 1341 server_file = "server";
1340 s = set_tcp_socket (); 1342 s = set_tcp_socket ();
1341 if ((s != INVALID_SOCKET) || alternate_editor) 1343 if ((s != INVALID_SOCKET) || no_exit_if_error)
1342 return s; 1344 return s;
1343 1345
1344 /* No implicit or explicit socket, and no alternate editor. */ 1346 /* No implicit or explicit socket, and no alternate editor. */
@@ -1408,6 +1410,52 @@ w32_give_focus ()
1408} 1410}
1409#endif 1411#endif
1410 1412
1413
1414/* Start the emacs daemon and try to connect to it. */
1415
1416void
1417start_daemon_and_retry_set_socket (void)
1418{
1419 pid_t dpid;
1420 int status;
1421 pid_t p;
1422
1423 dpid = fork ();
1424
1425 if (dpid > 0)
1426 {
1427 p = waitpid (dpid, &status, WUNTRACED | WCONTINUED);
1428
1429 /* Try connecting again, the daemon should have started by
1430 now. */
1431 message (TRUE, "daemon should have started, trying to connect again\n", dpid);
1432 if ((emacs_socket = set_socket (1)) == INVALID_SOCKET)
1433 message (TRUE, "Cannot connect even after starting the daemon\n");
1434 }
1435 else if (dpid < 0)
1436 {
1437 fprintf (stderr, "Cannot fork!\n");
1438 exit (1);
1439 }
1440 else
1441 {
1442 char *d_argv[] = {"emacs", "--daemon", 0 };
1443 if (socket_name != NULL)
1444 {
1445 /* Pass --daemon=socket_name as argument. */
1446 char *deq = "--daemon=";
1447 char *daemon_arg = alloca (strlen (deq)
1448 + strlen (socket_name) + 1);
1449 strcpy (daemon_arg, deq);
1450 strcat (daemon_arg, socket_name);
1451 d_argv[1] = daemon_arg;
1452 }
1453 execvp ("emacs", d_argv);
1454 message (TRUE, "%s: error starting emacs daemon\n", progname);
1455 }
1456}
1457
1458
1411int 1459int
1412main (argc, argv) 1460main (argc, argv)
1413 int argc; 1461 int argc;
@@ -1416,6 +1464,7 @@ main (argc, argv)
1416 int i, rl, needlf = 0; 1464 int i, rl, needlf = 0;
1417 char *cwd, *str; 1465 char *cwd, *str;
1418 char string[BUFSIZ+1]; 1466 char string[BUFSIZ+1];
1467 int null_socket_name, null_server_file, start_daemon_if_needed;
1419 1468
1420 main_argv = argv; 1469 main_argv = argv;
1421 progname = argv[0]; 1470 progname = argv[0];
@@ -1431,9 +1480,34 @@ main (argc, argv)
1431 exit (EXIT_FAILURE); 1480 exit (EXIT_FAILURE);
1432 } 1481 }
1433 1482
1434 if ((emacs_socket = set_socket ()) == INVALID_SOCKET) 1483 /* If alternate_editor is the empty string, start the emacs daemon
1435 fail (); 1484 in case of failure to connect. */
1485 start_daemon_if_needed = (alternate_editor
1486 && (alternate_editor[0] == '\0'));
1487 if (start_daemon_if_needed)
1488 {
1489 /* set_socket changes the values for socket_name and
1490 server_file, we need to reset them, if they were NULL before
1491 for the second call to set_socket. */
1492 null_socket_name = (socket_name == NULL);
1493 null_server_file = (server_file == NULL);
1494 }
1436 1495
1496 if ((emacs_socket = set_socket (alternate_editor
1497 || start_daemon_if_needed)) == INVALID_SOCKET)
1498 if (start_daemon_if_needed)
1499 {
1500 /* Reset socket_name and server_file if they were NULL
1501 before the set_socket call. */
1502 if (null_socket_name)
1503 socket_name = NULL;
1504 if (null_server_file)
1505 server_file = NULL;
1506
1507 start_daemon_and_retry_set_socket ();
1508 }
1509 else
1510 fail ();
1437 1511
1438 cwd = get_current_dir_name (); 1512 cwd = get_current_dir_name ();
1439 if (cwd == 0) 1513 if (cwd == 0)