Correct wording in 2006-11-15 entry.
[bpt/emacs.git] / lib-src / emacsclient.c
CommitLineData
efb859b4 1/* Client process that communicates with GNU Emacs acting as server.
92b47a4a 2 Copyright (C) 1986, 1987, 1994, 1999, 2000, 2001, 2002, 2003, 2004,
273dc16a 3 2005, 2006 Free Software Foundation, Inc.
46cec291
RS
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
92af894f 9the Free Software Foundation; either version 2, or (at your option)
46cec291
RS
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
364c38d3
LK
19the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20Boston, MA 02110-1301, USA. */
46cec291
RS
21
22
23#define NO_SHORTNAMES
e69233c2
PJ
24
25#ifdef HAVE_CONFIG_H
2f8fe2f4 26#include <config.h>
e69233c2
PJ
27#endif
28
aa0b6932 29#ifdef WINDOWSNT
aa0b6932 30
bc28de71
JB
31/* config.h defines these, which disables sockets altogether! */
32# undef _WINSOCKAPI_
33# undef _WINSOCK_H
34
411b80a5
JB
35# include <malloc.h>
36# include <stdlib.h>
37
411b80a5
JB
38# define NO_SOCKETS_IN_FILE_SYSTEM
39
aa0b6932
JB
40# define HSOCKET SOCKET
41# define CLOSE_SOCKET closesocket
aa0b6932 42# define INITIALIZE() (initialize_sockets ())
411b80a5
JB
43
44#else /* !WINDOWSNT */
45
ed4a3730
JB
46# include <sys/types.h>
47
1e7823d0
JB
48# ifdef HAVE_INET_SOCKETS
49# include <netinet/in.h>
50# endif
411b80a5 51
e35fc962 52# define INVALID_SOCKET -1
aa0b6932
JB
53# define HSOCKET int
54# define CLOSE_SOCKET close
aa0b6932 55# define INITIALIZE()
411b80a5
JB
56
57#endif /* !WINDOWSNT */
aa0b6932 58
4e23f2ba 59#undef signal
46cec291 60
e69233c2 61#include <ctype.h>
8f9aaa0a 62#include <stdio.h>
aa0b6932 63#include "getopt.h"
79f13bba
DL
64#ifdef HAVE_UNISTD_H
65#include <unistd.h>
66#endif
8f9aaa0a 67
9f637eea
GM
68#ifdef VMS
69# include "vms-pwd.h"
aa0b6932
JB
70#else /* not VMS */
71#ifdef WINDOWSNT
72# include <io.h>
73#else /* not WINDOWSNT */
9f637eea 74# include <pwd.h>
aa0b6932 75#endif /* not WINDOWSNT */
9f637eea
GM
76#endif /* not VMS */
77
8f9aaa0a 78char *getenv (), *getwd ();
5b9562c3 79char *(getcwd) ();
8f9aaa0a 80
8f9aaa0a
RS
81#ifndef VERSION
82#define VERSION "unspecified"
83#endif
84\f
aa0b6932
JB
85#define SEND_STRING(data) (send_to_emacs (s, (data)))
86#define SEND_QUOTED(data) (quote_file_name (s, (data)))
87
88#ifndef EXIT_SUCCESS
89#define EXIT_SUCCESS 0
90#endif
91
92#ifndef EXIT_FAILURE
93#define EXIT_FAILURE 1
94#endif
95
96#ifndef FALSE
97#define FALSE 0
98#endif
99
100#ifndef TRUE
101#define TRUE 1
102#endif
103
104#ifndef NO_RETURN
105#define NO_RETURN
106#endif
107\f
8f9aaa0a
RS
108/* Name used to invoke this program. */
109char *progname;
110
749ae770 111/* Nonzero means don't wait for a response from Emacs. --no-wait. */
8f9aaa0a
RS
112int nowait = 0;
113
30be2360
SM
114/* Nonzero means args are expressions to be evaluated. --eval. */
115int eval = 0;
116
117/* The display on which Emacs should work. --display. */
118char *display = NULL;
119
120/* If non-NULL, the name of an editor to fallback to if the server
121 is not running. --alternate-editor. */
b03d27bd 122const char *alternate_editor = NULL;
30be2360 123
3db926be 124/* If non-NULL, the filename of the UNIX socket. */
254107e4
RS
125char *socket_name = NULL;
126
aa0b6932
JB
127/* If non-NULL, the filename of the authentication file. */
128char *server_file = NULL;
129
2381d38d 130void print_help_and_exit () NO_RETURN;
7f3bff3e 131
8f9aaa0a
RS
132struct option longopts[] =
133{
749ae770 134 { "no-wait", no_argument, NULL, 'n' },
30be2360 135 { "eval", no_argument, NULL, 'e' },
8f9aaa0a
RS
136 { "help", no_argument, NULL, 'H' },
137 { "version", no_argument, NULL, 'V' },
3cf8c6aa 138 { "alternate-editor", required_argument, NULL, 'a' },
b03d27bd 139#ifndef NO_SOCKETS_IN_FILE_SYSTEM
254107e4 140 { "socket-name", required_argument, NULL, 's' },
b03d27bd 141#endif
aa0b6932 142 { "server-file", required_argument, NULL, 'f' },
30be2360
SM
143 { "display", required_argument, NULL, 'd' },
144 { 0, 0, 0, 0 }
8f9aaa0a
RS
145};
146
147/* Decode the options from argv and argc.
5212210c 148 The global variable `optind' will say how many arguments we used up. */
8f9aaa0a 149
5212210c 150void
8f9aaa0a
RS
151decode_options (argc, argv)
152 int argc;
153 char **argv;
154{
b6b6d6d2
SM
155 alternate_editor = getenv ("ALTERNATE_EDITOR");
156
8f9aaa0a
RS
157 while (1)
158 {
159 int opt = getopt_long (argc, argv,
b03d27bd
JB
160#ifndef NO_SOCKETS_IN_FILE_SYSTEM
161 "VHnea:s:f:d:",
162#else
163 "VHnea:f:d:",
164#endif
165 longopts, 0);
8f9aaa0a
RS
166
167 if (opt == EOF)
168 break;
169
170 switch (opt)
171 {
172 case 0:
173 /* If getopt returns 0, then it has already processed a
174 long-named option. We should do nothing. */
175 break;
e69233c2 176
97e3214d
GM
177 case 'a':
178 alternate_editor = optarg;
179 break;
e69233c2 180
b03d27bd 181#ifndef NO_SOCKETS_IN_FILE_SYSTEM
254107e4
RS
182 case 's':
183 socket_name = optarg;
184 break;
b03d27bd 185#endif
254107e4 186
aa0b6932
JB
187 case 'f':
188 server_file = optarg;
189 break;
190
30be2360
SM
191 case 'd':
192 display = optarg;
193 break;
194
8f9aaa0a
RS
195 case 'n':
196 nowait = 1;
197 break;
198
30be2360
SM
199 case 'e':
200 eval = 1;
201 break;
202
8f9aaa0a 203 case 'V':
20c396e8 204 printf ("emacsclient %s\n", VERSION);
65396510 205 exit (EXIT_SUCCESS);
8f9aaa0a 206 break;
46cec291 207
8f9aaa0a 208 case 'H':
8f9aaa0a 209 print_help_and_exit ();
20c396e8
JB
210 break;
211
212 default:
213 fprintf (stderr, "Try `%s --help' for more information\n", progname);
65396510 214 exit (EXIT_FAILURE);
20c396e8 215 break;
8f9aaa0a
RS
216 }
217 }
8f9aaa0a
RS
218}
219
7f3bff3e 220void
8f9aaa0a
RS
221print_help_and_exit ()
222{
20c396e8
JB
223 printf (
224 "Usage: %s [OPTIONS] FILE...\n\
30be2360
SM
225Tell the Emacs server to visit the specified files.\n\
226Every FILE can be either just a FILENAME or [+LINE[:COLUMN]] FILENAME.\n\
20c396e8 227\n\
30be2360
SM
228The following OPTIONS are accepted:\n\
229-V, --version Just print a version info and return\n\
230-H, --help Print this usage information message\n\
231-n, --no-wait Don't wait for the server to return\n\
232-e, --eval Evaluate the FILE arguments as ELisp expressions\n\
aa0b6932
JB
233-d, --display=DISPLAY Visit the file in the given display\n"
234#ifndef NO_SOCKETS_IN_FILE_SYSTEM
235"-s, --socket-name=FILENAME\n\
236 Set the filename of the UNIX socket for communication\n"
237#endif
238"-f, --server-file=FILENAME\n\
239 Set the filename of the TCP configuration file\n\
30be2360
SM
240-a, --alternate-editor=EDITOR\n\
241 Editor to fallback to if the server is not running\n\
20c396e8 242\n\
30be2360 243Report bugs to bug-gnu-emacs@gnu.org.\n", progname);
65396510 244 exit (EXIT_SUCCESS);
8f9aaa0a 245}
5212210c 246
aa0b6932
JB
247\f
248/*
249 Try to run a different command, or --if no alternate editor is
250 defined-- exit with an errorcode.
251*/
252void
253fail (argc, argv)
254 int argc;
255 char **argv;
256{
257 if (alternate_editor)
258 {
259 int i = optind - 1;
b73ea44b
JB
260#ifdef WINDOWSNT
261 argv[i] = (char *)alternate_editor;
262#endif
aa0b6932
JB
263 execvp (alternate_editor, argv + i);
264 fprintf (stderr, "%s: error executing alternate editor \"%s\"\n",
265 progname, alternate_editor);
266 }
aa0b6932
JB
267 exit (EXIT_FAILURE);
268}
269
270\f
1e7823d0 271#if !defined (HAVE_SOCKETS) || !defined (HAVE_INET_SOCKETS)
aa0b6932
JB
272
273int
274main (argc, argv)
275 int argc;
276 char **argv;
277{
278 fprintf (stderr, "%s: Sorry, the Emacs server is supported only\n",
279 argv[0]);
280 fprintf (stderr, "on systems with Berkeley sockets.\n");
281
282 fail (argc, argv);
283}
284
1e7823d0 285#else /* HAVE_SOCKETS && HAVE_INET_SOCKETS */
aa0b6932
JB
286
287#ifdef WINDOWSNT
288# include <winsock2.h>
289#else
290# include <sys/types.h>
291# include <sys/socket.h>
292# include <sys/un.h>
293# include <sys/stat.h>
294# include <errno.h>
295#endif
296
297#define AUTH_KEY_LENGTH 64
298#define SEND_BUFFER_SIZE 4096
299
300extern char *strerror ();
301extern int errno;
302
303/* Buffer to accumulate data to send in TCP connections. */
304char send_buffer[SEND_BUFFER_SIZE + 1];
305int sblen = 0; /* Fill pointer for the send buffer. */
306
307/* Let's send the data to Emacs when either
308 - the data ends in "\n", or
309 - the buffer is full (but this shouldn't happen)
310 Otherwise, we just accumulate it. */
b03d27bd
JB
311void
312send_to_emacs (s, data)
aa0b6932
JB
313 HSOCKET s;
314 char *data;
315{
316 while (data)
317 {
318 int dlen = strlen (data);
319 if (dlen + sblen >= SEND_BUFFER_SIZE)
320 {
321 int part = SEND_BUFFER_SIZE - sblen;
322 strncpy (&send_buffer[sblen], data, part);
323 data += part;
324 sblen = SEND_BUFFER_SIZE;
325 }
326 else if (dlen)
327 {
328 strcpy (&send_buffer[sblen], data);
329 data = NULL;
330 sblen += dlen;
331 }
332 else
333 break;
334
335 if (sblen == SEND_BUFFER_SIZE
336 || (sblen > 0 && send_buffer[sblen-1] == '\n'))
337 {
338 int sent = send (s, send_buffer, sblen, 0);
339 if (sent != sblen)
340 strcpy (send_buffer, &send_buffer[sent]);
341 sblen -= sent;
342 }
343 }
344}
345
a4cf2096
RS
346/* In NAME, insert a & before each &, each space, each newline, and
347 any initial -. Change spaces to underscores, too, so that the
5212210c 348 return value never contains a space. */
87209357 349void
aa0b6932
JB
350quote_file_name (s, name)
351 HSOCKET s;
5212210c
RS
352 char *name;
353{
354 char *copy = (char *) malloc (strlen (name) * 2 + 1);
355 char *p, *q;
356
357 p = name;
358 q = copy;
359 while (*p)
360 {
361 if (*p == ' ')
362 {
f1db6a73 363 *q++ = '&';
5212210c
RS
364 *q++ = '_';
365 p++;
366 }
3cf8c6aa
SM
367 else if (*p == '\n')
368 {
369 *q++ = '&';
370 *q++ = 'n';
371 p++;
372 }
5212210c
RS
373 else
374 {
f1db6a73
RS
375 if (*p == '&' || (*p == '-' && p == name))
376 *q++ = '&';
5212210c
RS
377 *q++ = *p++;
378 }
379 }
f1db6a73 380 *q++ = 0;
5212210c 381
aa0b6932 382 SEND_STRING (copy);
87209357
EZ
383
384 free (copy);
5212210c 385}
0c76956f 386
b03d27bd
JB
387int
388file_name_absolute_p (filename)
389 const unsigned char *filename;
390{
391 /* Sanity check, it shouldn't happen. */
392 if (! filename) return FALSE;
393
394 /* /xxx is always an absolute path. */
395 if (filename[0] == '/') return TRUE;
396
397 /* Empty filenames (which shouldn't happen) are relative. */
398 if (filename[0] == '\0') return FALSE;
399
400#ifdef WINDOWSNT
401 /* X:\xxx is always absolute; X:xxx is an error and will fail. */
402 if (islower (tolower (filename[0]))
403 && filename[1] == ':' && filename[2] == '\\')
404 return TRUE;
405
406 /* Both \xxx and \\xxx\yyy are absolute. */
407 if (filename[0] == '\\') return TRUE;
408#endif
409
410 return FALSE;
411}
412
aa0b6932
JB
413#ifdef WINDOWSNT
414/* Wrapper to make WSACleanup a cdecl, as required by atexit(). */
b03d27bd
JB
415void
416__cdecl close_winsock ()
aa0b6932
JB
417{
418 WSACleanup ();
419}
0c76956f 420
b03d27bd
JB
421/* Initialize the WinSock2 library. */
422void
423initialize_sockets ()
0c76956f 424{
aa0b6932
JB
425 WSADATA wsaData;
426
aa0b6932
JB
427 if (WSAStartup (MAKEWORD (2, 0), &wsaData))
428 {
429 fprintf (stderr, "%s: error initializing WinSock2", progname);
430 exit (EXIT_FAILURE);
431 }
432
433 atexit (close_winsock);
0c76956f 434}
e35fc962 435#endif /* WINDOWSNT */
8f9aaa0a 436\f
97e3214d 437/*
aa0b6932 438 * Read the information needed to set up a TCP comm channel with
434a6c5d 439 * the Emacs server: host, port, pid and authentication string.
97e3214d 440*/
b03d27bd
JB
441int
442get_server_config (server, authentication)
aa0b6932
JB
443 struct sockaddr_in *server;
444 char *authentication;
97e3214d 445{
aa0b6932
JB
446 char dotted[32];
447 char *port;
434a6c5d 448 char *pid;
b03d27bd 449 FILE *config = NULL;
aa0b6932 450
b03d27bd
JB
451 if (file_name_absolute_p (server_file))
452 config = fopen (server_file, "rb");
453 else
97e3214d 454 {
aa0b6932 455 char *home = getenv ("HOME");
88b46d84 456
aa0b6932
JB
457 if (home)
458 {
459 char *path = alloca (32 + strlen (home) + strlen (server_file));
460 sprintf (path, "%s/.emacs.d/server/%s", home, server_file);
461 config = fopen (path, "rb");
462 }
88b46d84
JB
463#ifdef WINDOWSNT
464 if (!config && (home = getenv ("APPDATA")))
465 {
466 char *path = alloca (32 + strlen (home) + strlen (server_file));
467 sprintf (path, "%s/.emacs.d/server/%s", home, server_file);
468 config = fopen (path, "rb");
469 }
470#endif
aa0b6932
JB
471 }
472
473 if (! config)
474 return FALSE;
475
476 if (fgets (dotted, sizeof dotted, config)
434a6c5d
JB
477 && (port = strchr (dotted, ':'))
478 && (pid = strchr (port, ' ')))
aa0b6932
JB
479 {
480 *port++ = '\0';
434a6c5d 481 *pid++ = '\0';
97e3214d
GM
482 }
483 else
484 {
aa0b6932 485 fprintf (stderr, "%s: invalid configuration info", progname);
65396510 486 exit (EXIT_FAILURE);
97e3214d 487 }
97e3214d 488
aa0b6932
JB
489 server->sin_family = AF_INET;
490 server->sin_addr.s_addr = inet_addr (dotted);
491 server->sin_port = htons (atoi (port));
97e3214d 492
aa0b6932
JB
493 if (! fread (authentication, AUTH_KEY_LENGTH, 1, config))
494 {
495 fprintf (stderr, "%s: cannot read authentication info", progname);
496 exit (EXIT_FAILURE);
497 }
46cec291 498
aa0b6932 499 fclose (config);
97e3214d 500
434a6c5d
JB
501#ifdef WINDOWSNT
502 /*
503 Modern Windows restrict which processes can set the foreground window.
504 So, for emacsclient to be able to force Emacs into the foreground, we
505 have to call AllowSetForegroundWindow(). Unfortunately, older Windows
506 (W95, W98 and NT) don't have this function, so we have to check first.
507
508 We're doing this here because it has to be done before sending info
509 to Emacs, and otherwise we'll need a global variable just to pass around
510 the pid, which is also inelegant.
511 */
512 {
513 HMODULE hUser32;
514
515 if (hUser32 = LoadLibrary ("user32.dll"))
516 {
2dc07a12 517 FARPROC set_fg;
434a6c5d
JB
518 if (set_fg = GetProcAddress (hUser32, "AllowSetForegroundWindow"))
519 set_fg (atoi (pid));
520 FreeLibrary (hUser32);
521 }
522 }
523#endif
524
aa0b6932 525 return TRUE;
46cec291
RS
526}
527
aa0b6932
JB
528HSOCKET
529set_tcp_socket ()
530{
531 HSOCKET s;
532 struct sockaddr_in server;
aa0b6932
JB
533 struct linger l_arg = {1, 1};
534 char auth_string[AUTH_KEY_LENGTH + 1];
46cec291 535
aa0b6932
JB
536 if (! get_server_config (&server, auth_string))
537 return INVALID_SOCKET;
538
b03d27bd
JB
539 if (server.sin_addr.s_addr != inet_addr ("127.0.0.1"))
540 fprintf (stderr, "%s: connected to remote socket at %s\n",
541 progname, inet_ntoa (server.sin_addr));
542
aa0b6932
JB
543 /*
544 * Open up an AF_INET socket
545 */
546 if ((s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
547 {
548 fprintf (stderr, "%s: ", progname);
549 perror ("socket");
550 return INVALID_SOCKET;
551 }
552
553 /*
554 * Set up the socket
555 */
556 if (connect (s, (struct sockaddr *) &server, sizeof server) < 0)
557 {
558 fprintf (stderr, "%s: ", progname);
559 perror ("connect");
560 return INVALID_SOCKET;
561 }
562
aa0b6932
JB
563 setsockopt (s, SOL_SOCKET, SO_LINGER, (char *) &l_arg, sizeof l_arg);
564
565 /*
566 * Send the authentication
567 */
568 auth_string[AUTH_KEY_LENGTH] = '\0';
569
570 SEND_STRING ("-auth ");
571 SEND_STRING (auth_string);
572 SEND_STRING ("\n");
573
574 return s;
575}
576
577#if !defined (NO_SOCKETS_IN_FILE_SYSTEM)
46cec291 578
9f637eea
GM
579/* Three possibilities:
580 2 - can't be `stat'ed (sets errno)
581 1 - isn't owned by us
582 0 - success: none of the above */
583
584static int
585socket_status (socket_name)
586 char *socket_name;
587{
588 struct stat statbfr;
589
590 if (stat (socket_name, &statbfr) == -1)
591 return 2;
592
593 if (statbfr.st_uid != geteuid ())
594 return 1;
595
596 return 0;
597}
598
aa0b6932
JB
599HSOCKET
600set_local_socket ()
46cec291 601{
aa0b6932 602 HSOCKET s;
46cec291 603 struct sockaddr_un server;
46cec291 604
e69233c2 605 /*
46cec291
RS
606 * Open up an AF_UNIX socket in this person's home directory
607 */
608
609 if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
610 {
aa0b6932 611 fprintf (stderr, "%s: ", progname);
46cec291 612 perror ("socket");
aa0b6932 613 return INVALID_SOCKET;
46cec291 614 }
e69233c2 615
46cec291 616 server.sun_family = AF_UNIX;
c5fee545 617
c5fee545 618 {
9f637eea 619 int sock_status = 0;
5c9659d3 620 int default_sock = !socket_name;
152b6e83 621 int saved_errno;
0734b0d0
SM
622 char *server_name = "server";
623
624 if (socket_name && !index (socket_name, '/') && !index (socket_name, '\\'))
625 { /* socket_name is a file name component. */
626 server_name = socket_name;
627 socket_name = NULL;
628 default_sock = 1; /* Try both UIDs. */
629 }
efb859b4 630
5c9659d3 631 if (default_sock)
254107e4 632 {
0734b0d0
SM
633 socket_name = alloca (100 + strlen (server_name));
634 sprintf (socket_name, "/tmp/emacs%d/%s",
635 (int) geteuid (), server_name);
254107e4
RS
636 }
637
638 if (strlen (socket_name) < sizeof (server.sun_path))
639 strcpy (server.sun_path, socket_name);
640 else
5c9659d3
SM
641 {
642 fprintf (stderr, "%s: socket-name %s too long",
aa0b6932 643 progname, socket_name);
65396510 644 exit (EXIT_FAILURE);
5c9659d3 645 }
efb859b4 646
9f637eea
GM
647 /* See if the socket exists, and if it's owned by us. */
648 sock_status = socket_status (server.sun_path);
152b6e83 649 saved_errno = errno;
5c9659d3 650 if (sock_status && default_sock)
efb859b4 651 {
9f637eea
GM
652 /* Failing that, see if LOGNAME or USER exist and differ from
653 our euid. If so, look for a socket based on the UID
654 associated with the name. This is reminiscent of the logic
655 that init_editfns uses to set the global Vuser_full_name. */
e69233c2 656
9f637eea 657 char *user_name = (char *) getenv ("LOGNAME");
293f9f2a 658
9f637eea
GM
659 if (!user_name)
660 user_name = (char *) getenv ("USER");
e69233c2 661
9f637eea
GM
662 if (user_name)
663 {
664 struct passwd *pw = getpwnam (user_name);
293f9f2a 665
9f637eea
GM
666 if (pw && (pw->pw_uid != geteuid ()))
667 {
668 /* We're running under su, apparently. */
0734b0d0
SM
669 socket_name = alloca (100 + strlen (server_name));
670 sprintf (socket_name, "/tmp/emacs%d/%s",
671 (int) pw->pw_uid, server_name);
5c9659d3
SM
672
673 if (strlen (socket_name) < sizeof (server.sun_path))
674 strcpy (server.sun_path, socket_name);
675 else
676 {
677 fprintf (stderr, "%s: socket-name %s too long",
aa0b6932 678 progname, socket_name);
65396510 679 exit (EXIT_FAILURE);
5c9659d3
SM
680 }
681
9f637eea 682 sock_status = socket_status (server.sun_path);
152b6e83 683 saved_errno = errno;
9f637eea 684 }
293f9f2a
RS
685 else
686 errno = saved_errno;
9f637eea 687 }
efb859b4 688 }
e69233c2 689
aa0b6932
JB
690 switch (sock_status)
691 {
692 case 1:
693 /* There's a socket, but it isn't owned by us. This is OK if
694 we are root. */
695 if (0 != geteuid ())
696 {
e35fc962 697 fprintf (stderr, "%s: Invalid socket owner\n", progname);
aa0b6932
JB
698 return INVALID_SOCKET;
699 }
700 break;
701
702 case 2:
703 /* `stat' failed */
704 if (saved_errno == ENOENT)
705 fprintf (stderr,
706 "%s: can't find socket; have you started the server?\n\
45adde32 707To start the server in Emacs, type \"M-x server-start\".\n",
aa0b6932
JB
708 progname);
709 else
710 fprintf (stderr, "%s: can't stat %s: %s\n",
711 progname, server.sun_path, strerror (saved_errno));
b03d27bd 712 return INVALID_SOCKET;
aa0b6932 713 }
efb859b4 714 }
46cec291 715
4e23f2ba
JB
716 if (connect (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2)
717 < 0)
46cec291 718 {
aa0b6932 719 fprintf (stderr, "%s: ", progname);
46cec291 720 perror ("connect");
aa0b6932 721 return INVALID_SOCKET;
46cec291 722 }
23a7488d 723
aa0b6932
JB
724 return s;
725}
726#endif /* ! NO_SOCKETS_IN_FILE_SYSTEM */
727
728HSOCKET
729set_socket ()
730{
b03d27bd
JB
731 HSOCKET s;
732
733 INITIALIZE ();
734
aa0b6932 735#ifndef NO_SOCKETS_IN_FILE_SYSTEM
b03d27bd
JB
736 /* Explicit --socket-name argument. */
737 if (socket_name)
738 {
739 s = set_local_socket ();
740 if ((s != INVALID_SOCKET) || alternate_editor)
741 return s;
742
743 fprintf (stderr, "%s: error accessing socket \"%s\"",
744 progname, socket_name);
745 exit (EXIT_FAILURE);
746 }
747#endif
748
749 /* Explicit --server-file arg or EMACS_SERVER_FILE variable. */
750 if (!server_file)
751 server_file = getenv ("EMACS_SERVER_FILE");
752
753 if (server_file)
46cec291 754 {
b03d27bd
JB
755 s = set_tcp_socket ();
756 if ((s != INVALID_SOCKET) || alternate_editor)
757 return s;
758
759 fprintf (stderr, "%s: error accessing server file \"%s\"",
760 progname, server_file);
761 exit (EXIT_FAILURE);
46cec291 762 }
b03d27bd
JB
763
764#ifndef NO_SOCKETS_IN_FILE_SYSTEM
765 /* Implicit local socket. */
766 s = set_local_socket ();
767 if (s != INVALID_SOCKET)
768 return s;
769#endif
770
771 /* Implicit server file. */
772 server_file = "server";
773 s = set_tcp_socket ();
774 if ((s != INVALID_SOCKET) || alternate_editor)
775 return s;
776
777 /* No implicit or explicit socket, and no alternate editor. */
778 fprintf (stderr, "%s: No socket or alternate editor. Please use:\n\n"
779#ifndef NO_SOCKETS_IN_FILE_SYSTEM
780"\t--socket-name\n"
aa0b6932 781#endif
b03d27bd
JB
782"\t--server-file (or environment variable EMACS_SERVER_FILE)\n\
783\t--alternate-editor (or environment variable ALTERNATE_EDITOR)\n",
784 progname);
785 exit (EXIT_FAILURE);
aa0b6932 786}
46cec291 787
aa0b6932
JB
788int
789main (argc, argv)
790 int argc;
791 char **argv;
792{
793 HSOCKET s;
794 int i, rl, needlf = 0;
795 char *cwd;
796 char string[BUFSIZ+1];
797
798 progname = argv[0];
799
800 /* Process options. */
801 decode_options (argc, argv);
802
803 if ((argc - optind < 1) && !eval)
23a7488d 804 {
aa0b6932
JB
805 fprintf (stderr, "%s: file name or argument required\n", progname);
806 fprintf (stderr, "Try `%s --help' for more information\n", progname);
807 exit (EXIT_FAILURE);
23a7488d
RS
808 }
809
aa0b6932
JB
810 if ((s = set_socket ()) == INVALID_SOCKET)
811 fail (argc, argv);
812
38732dba 813#ifdef HAVE_GETCWD
ee6a193c 814 cwd = getcwd (string, sizeof string);
38732dba
RS
815#else
816 cwd = getwd (string);
ee6a193c 817#endif
46cec291
RS
818 if (cwd == 0)
819 {
820 /* getwd puts message in STRING if it fails. */
38732dba 821#ifdef HAVE_GETCWD
aa0b6932 822 fprintf (stderr, "%s: %s (%s)\n", progname,
bb5618fe 823 "Cannot get current working directory", strerror (errno));
38732dba 824#else
aa0b6932 825 fprintf (stderr, "%s: %s (%s)\n", progname, string, strerror (errno));
bd252662 826#endif
97e3214d 827 fail (argc, argv);
46cec291
RS
828 }
829
5212210c 830 if (nowait)
aa0b6932 831 SEND_STRING ("-nowait ");
292d74a3 832
30be2360 833 if (eval)
aa0b6932 834 SEND_STRING ("-eval ");
30be2360
SM
835
836 if (display)
87209357 837 {
aa0b6932
JB
838 SEND_STRING ("-display ");
839 SEND_QUOTED (display);
840 SEND_STRING (" ");
87209357 841 }
30be2360 842
87209357 843 if ((argc - optind > 0))
5212210c 844 {
87209357 845 for (i = optind; i < argc; i++)
46cec291 846 {
87209357
EZ
847 if (eval)
848 ; /* Don't prepend any cwd or anything like that. */
849 else if (*argv[i] == '+')
850 {
851 char *p = argv[i] + 1;
852 while (isdigit ((unsigned char) *p) || *p == ':') p++;
853 if (*p != 0)
854 {
aa0b6932
JB
855 SEND_QUOTED (cwd);
856 SEND_STRING ("/");
87209357
EZ
857 }
858 }
b03d27bd 859 else if (! file_name_absolute_p (argv[i]))
87209357 860 {
aa0b6932
JB
861 SEND_QUOTED (cwd);
862 SEND_STRING ("/");
87209357
EZ
863 }
864
aa0b6932
JB
865 SEND_QUOTED (argv[i]);
866 SEND_STRING (" ");
46cec291 867 }
46cec291 868 }
87209357
EZ
869 else
870 {
aa0b6932 871 while (fgets (string, BUFSIZ, stdin))
87209357 872 {
aa0b6932 873 SEND_QUOTED (string);
87209357 874 }
aa0b6932 875 SEND_STRING (" ");
87209357 876 }
65396510 877
aa0b6932 878 SEND_STRING ("\n");
46cec291 879
292d74a3 880 /* Maybe wait for an answer. */
aa0b6932 881 if (!nowait)
30be2360 882 {
aa0b6932
JB
883 if (!eval)
884 {
885 printf ("Waiting for Emacs...");
886 needlf = 2;
887 }
888 fflush (stdout);
889
890 /* Now, wait for an answer and print any messages. */
891 while ((rl = recv (s, string, BUFSIZ, 0)) > 0)
892 {
893 string[rl] = '\0';
894 if (needlf == 2)
895 printf ("\n");
896 printf ("%s", string);
897 needlf = string[0] == '\0' ? needlf : string[strlen (string) - 1] != '\n';
898 }
899
900 if (needlf)
901 printf ("\n");
902 fflush (stdout);
30be2360 903 }
23a7488d 904
aa0b6932 905 CLOSE_SOCKET (s);
65396510 906 return EXIT_SUCCESS;
46cec291
RS
907}
908
1e7823d0 909#endif /* HAVE_SOCKETS && HAVE_INET_SOCKETS */
aa0b6932 910
27711600
RM
911#ifndef HAVE_STRERROR
912char *
913strerror (errnum)
914 int errnum;
915{
916 extern char *sys_errlist[];
917 extern int sys_nerr;
918
919 if (errnum >= 0 && errnum < sys_nerr)
920 return sys_errlist[errnum];
921 return (char *) "Unknown error";
922}
923
924#endif /* ! HAVE_STRERROR */
ab5796a9
MB
925
926/* arch-tag: f39bb9c4-73eb-477e-896d-50832e2ca9a7
927 (do not change this comment) */
65396510
TTN
928
929/* emacsclient.c ends here */