(w32_window_app): Init common controls when windowed.
[bpt/emacs.git] / lib-src / emacsclient.c
1 /* Client process that communicates with GNU Emacs acting as server.
2 Copyright (C) 1986, 1987, 1994, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21
22
23 #define NO_SHORTNAMES
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #ifdef WINDOWSNT
30
31 /* config.h defines these, which disables sockets altogether! */
32 # undef _WINSOCKAPI_
33 # undef _WINSOCK_H
34
35 # include <malloc.h>
36 # include <stdlib.h>
37 # include <windows.h>
38 # include <commctrl.h>
39
40 # define NO_SOCKETS_IN_FILE_SYSTEM
41
42 # define HSOCKET SOCKET
43 # define CLOSE_SOCKET closesocket
44 # define INITIALIZE() (initialize_sockets ())
45
46 #else /* !WINDOWSNT */
47
48 # include <sys/types.h>
49
50 # ifdef HAVE_INET_SOCKETS
51 # include <netinet/in.h>
52 # endif
53
54 # define INVALID_SOCKET -1
55 # define HSOCKET int
56 # define CLOSE_SOCKET close
57 # define INITIALIZE()
58
59 #endif /* !WINDOWSNT */
60
61 #undef signal
62
63 #include <stdarg.h>
64 #include <ctype.h>
65 #include <stdio.h>
66 #include "getopt.h"
67 #ifdef HAVE_UNISTD_H
68 #include <unistd.h>
69 #endif
70
71 #ifdef VMS
72 # include "vms-pwd.h"
73 #else /* not VMS */
74 #ifdef WINDOWSNT
75 # include <io.h>
76 #else /* not WINDOWSNT */
77 # include <pwd.h>
78 #endif /* not WINDOWSNT */
79 #endif /* not VMS */
80
81 char *getenv (), *getwd ();
82 char *(getcwd) ();
83
84 #ifndef VERSION
85 #define VERSION "unspecified"
86 #endif
87 \f
88 #define SEND_STRING(data) (send_to_emacs (s, (data)))
89 #define SEND_QUOTED(data) (quote_file_name (s, (data)))
90
91 #ifndef EXIT_SUCCESS
92 #define EXIT_SUCCESS 0
93 #endif
94
95 #ifndef EXIT_FAILURE
96 #define EXIT_FAILURE 1
97 #endif
98
99 #ifndef FALSE
100 #define FALSE 0
101 #endif
102
103 #ifndef TRUE
104 #define TRUE 1
105 #endif
106
107 #ifndef NO_RETURN
108 #define NO_RETURN
109 #endif
110 \f
111 /* Name used to invoke this program. */
112 char *progname;
113
114 /* Nonzero means don't wait for a response from Emacs. --no-wait. */
115 int nowait = 0;
116
117 /* Nonzero means args are expressions to be evaluated. --eval. */
118 int eval = 0;
119
120 /* The display on which Emacs should work. --display. */
121 char *display = NULL;
122
123 /* If non-NULL, the name of an editor to fallback to if the server
124 is not running. --alternate-editor. */
125 const char *alternate_editor = NULL;
126
127 /* If non-NULL, the filename of the UNIX socket. */
128 char *socket_name = NULL;
129
130 /* If non-NULL, the filename of the authentication file. */
131 char *server_file = NULL;
132
133 /* PID of the Emacs server process. */
134 int emacs_pid = 0;
135
136 void print_help_and_exit () NO_RETURN;
137
138 struct option longopts[] =
139 {
140 { "no-wait", no_argument, NULL, 'n' },
141 { "eval", no_argument, NULL, 'e' },
142 { "help", no_argument, NULL, 'H' },
143 { "version", no_argument, NULL, 'V' },
144 { "alternate-editor", required_argument, NULL, 'a' },
145 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
146 { "socket-name", required_argument, NULL, 's' },
147 #endif
148 { "server-file", required_argument, NULL, 'f' },
149 { "display", required_argument, NULL, 'd' },
150 { 0, 0, 0, 0 }
151 };
152
153 /* Message functions. */
154
155 #ifdef WINDOWSNT
156 int
157 w32_window_app ()
158 {
159 static int window_app = -1;
160 char szTitle[MAX_PATH];
161
162 if (window_app < 0)
163 {
164 /* Checking for STDOUT does not work; it's a valid handle also in
165 nonconsole apps. Testing for the console title seems to work. */
166 window_app = (GetConsoleTitleA (szTitle, MAX_PATH) == 0);
167 if (window_app)
168 InitCommonControls();
169 }
170
171 return window_app;
172 }
173 #endif
174
175 void
176 message (int is_error, char *message, ...)
177 {
178 char msg [2048];
179 va_list args;
180
181 va_start (args, message);
182 vsprintf (msg, message, args);
183 va_end (args);
184
185 #ifdef WINDOWSNT
186 if (w32_window_app ())
187 {
188 if (is_error)
189 MessageBox (NULL, msg, "Emacsclient ERROR", MB_ICONERROR);
190 else
191 MessageBox (NULL, msg, "Emacsclient", MB_ICONINFORMATION);
192 }
193 else
194 #endif
195 {
196 FILE *f = is_error ? stderr : stdout;
197
198 fputs (msg, f);
199 fflush (f);
200 }
201 }
202
203 /* Decode the options from argv and argc.
204 The global variable `optind' will say how many arguments we used up. */
205
206 void
207 decode_options (argc, argv)
208 int argc;
209 char **argv;
210 {
211 alternate_editor = getenv ("ALTERNATE_EDITOR");
212
213 while (1)
214 {
215 int opt = getopt_long (argc, argv,
216 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
217 "VHnea:s:f:d:",
218 #else
219 "VHnea:f:d:",
220 #endif
221 longopts, 0);
222
223 if (opt == EOF)
224 break;
225
226 switch (opt)
227 {
228 case 0:
229 /* If getopt returns 0, then it has already processed a
230 long-named option. We should do nothing. */
231 break;
232
233 case 'a':
234 alternate_editor = optarg;
235 break;
236
237 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
238 case 's':
239 socket_name = optarg;
240 break;
241 #endif
242
243 case 'f':
244 server_file = optarg;
245 break;
246
247 case 'd':
248 display = optarg;
249 break;
250
251 case 'n':
252 nowait = 1;
253 break;
254
255 case 'e':
256 eval = 1;
257 break;
258
259 case 'V':
260 message (FALSE, "emacsclient %s\n", VERSION);
261 exit (EXIT_SUCCESS);
262 break;
263
264 case 'H':
265 print_help_and_exit ();
266 break;
267
268 default:
269 message (TRUE, "Try `%s --help' for more information\n", progname);
270 exit (EXIT_FAILURE);
271 break;
272 }
273 }
274 }
275
276 void
277 print_help_and_exit ()
278 {
279 message (FALSE,
280 "Usage: %s [OPTIONS] FILE...\n\
281 Tell the Emacs server to visit the specified files.\n\
282 Every FILE can be either just a FILENAME or [+LINE[:COLUMN]] FILENAME.\n\
283 \n\
284 The following OPTIONS are accepted:\n\
285 \n\
286 -V, --version Just print version info and return\n\
287 -H, --help Print this usage information message\n\
288 -e, --eval Evaluate FILE arguments as Lisp expressions\n\
289 -n, --no-wait Don't wait for the server to return\n\
290 -d, --display=DISPLAY Visit the file in the given display\n"
291 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
292 "-s, --socket-name=FILENAME\n\
293 Set filename of the UNIX socket for communication\n"
294 #endif
295 "-f, --server-file=FILENAME\n\
296 Set filename of the TCP authentication file\n\
297 -a, --alternate-editor=EDITOR\n\
298 Editor to fallback to if server is not running\n\
299 \n\
300 Report bugs to bug-gnu-emacs@gnu.org.\n", progname);
301 exit (EXIT_SUCCESS);
302 }
303
304 \f
305 #ifdef WINDOWSNT
306
307 /*
308 execvp wrapper for Windows. Quotes arguments with embedded spaces.
309
310 This is necessary due to the broken implementation of exec* routines in
311 the Microsoft libraries: they concatenate the arguments together without
312 quoting special characters, and pass the result to CreateProcess, with
313 predictably bad results. By contrast, Posix execvp passes the arguments
314 directly into the argv array of the child process.
315 */
316 int
317 w32_execvp (path, argv)
318 char *path;
319 char **argv;
320 {
321 int i;
322
323 /* Required to allow a .BAT script as alternate editor. */
324 argv[0] = (char *) alternate_editor;
325
326 for (i = 0; argv[i]; i++)
327 if (strchr (argv[i], ' '))
328 {
329 char *quoted = alloca (strlen (argv[i]) + 3);
330 sprintf (quoted, "\"%s\"", argv[i]);
331 argv[i] = quoted;
332 }
333
334 return execvp (path, argv);
335 }
336
337 #undef execvp
338 #define execvp w32_execvp
339
340 #endif /* WINDOWSNT */
341
342 /*
343 Try to run a different command, or --if no alternate editor is
344 defined-- exit with an errorcode.
345 */
346 void
347 fail (argc, argv)
348 int argc;
349 char **argv;
350 {
351 if (alternate_editor)
352 {
353 int i = optind - 1;
354
355 execvp (alternate_editor, argv + i);
356 message (TRUE, "%s: error executing alternate editor \"%s\"\n",
357 progname, alternate_editor);
358 }
359 exit (EXIT_FAILURE);
360 }
361
362 \f
363 #if !defined (HAVE_SOCKETS) || !defined (HAVE_INET_SOCKETS)
364
365 int
366 main (argc, argv)
367 int argc;
368 char **argv;
369 {
370 message (TRUE, "%s: Sorry, the Emacs server is supported only\non systems with Berkely sockets.\n",
371 argv[0]);
372
373 fail (argc, argv);
374 }
375
376 #else /* HAVE_SOCKETS && HAVE_INET_SOCKETS */
377
378 #ifdef WINDOWSNT
379 # include <winsock2.h>
380 #else
381 # include <sys/types.h>
382 # include <sys/socket.h>
383 # include <sys/un.h>
384 # include <sys/stat.h>
385 # include <errno.h>
386 #endif
387
388 #define AUTH_KEY_LENGTH 64
389 #define SEND_BUFFER_SIZE 4096
390
391 extern char *strerror ();
392 extern int errno;
393
394 /* Buffer to accumulate data to send in TCP connections. */
395 char send_buffer[SEND_BUFFER_SIZE + 1];
396 int sblen = 0; /* Fill pointer for the send buffer. */
397
398 /* Let's send the data to Emacs when either
399 - the data ends in "\n", or
400 - the buffer is full (but this shouldn't happen)
401 Otherwise, we just accumulate it. */
402 void
403 send_to_emacs (s, data)
404 HSOCKET s;
405 char *data;
406 {
407 while (data)
408 {
409 int dlen = strlen (data);
410 if (dlen + sblen >= SEND_BUFFER_SIZE)
411 {
412 int part = SEND_BUFFER_SIZE - sblen;
413 strncpy (&send_buffer[sblen], data, part);
414 data += part;
415 sblen = SEND_BUFFER_SIZE;
416 }
417 else if (dlen)
418 {
419 strcpy (&send_buffer[sblen], data);
420 data = NULL;
421 sblen += dlen;
422 }
423 else
424 break;
425
426 if (sblen == SEND_BUFFER_SIZE
427 || (sblen > 0 && send_buffer[sblen-1] == '\n'))
428 {
429 int sent = send (s, send_buffer, sblen, 0);
430 if (sent != sblen)
431 strcpy (send_buffer, &send_buffer[sent]);
432 sblen -= sent;
433 }
434 }
435 }
436
437 /* In NAME, insert a & before each &, each space, each newline, and
438 any initial -. Change spaces to underscores, too, so that the
439 return value never contains a space. */
440 void
441 quote_file_name (s, name)
442 HSOCKET s;
443 char *name;
444 {
445 char *copy = (char *) malloc (strlen (name) * 2 + 1);
446 char *p, *q;
447
448 p = name;
449 q = copy;
450 while (*p)
451 {
452 if (*p == ' ')
453 {
454 *q++ = '&';
455 *q++ = '_';
456 p++;
457 }
458 else if (*p == '\n')
459 {
460 *q++ = '&';
461 *q++ = 'n';
462 p++;
463 }
464 else
465 {
466 if (*p == '&' || (*p == '-' && p == name))
467 *q++ = '&';
468 *q++ = *p++;
469 }
470 }
471 *q++ = 0;
472
473 SEND_STRING (copy);
474
475 free (copy);
476 }
477
478 int
479 file_name_absolute_p (filename)
480 const unsigned char *filename;
481 {
482 /* Sanity check, it shouldn't happen. */
483 if (! filename) return FALSE;
484
485 /* /xxx is always an absolute path. */
486 if (filename[0] == '/') return TRUE;
487
488 /* Empty filenames (which shouldn't happen) are relative. */
489 if (filename[0] == '\0') return FALSE;
490
491 #ifdef WINDOWSNT
492 /* X:\xxx is always absolute. */
493 if (isalpha (filename[0])
494 && filename[1] == ':' && (filename[2] == '\\' || filename[2] == '/'))
495 return TRUE;
496
497 /* Both \xxx and \\xxx\yyy are absolute. */
498 if (filename[0] == '\\') return TRUE;
499
500 /*
501 FIXME: There's a corner case not dealt with, "x:y", where:
502
503 1) x is a valid drive designation (usually a letter in the A-Z range)
504 and y is a path, relative to the current directory on drive x. This
505 is absolute, *after* fixing the y part to include the current
506 directory in x.
507
508 2) x is a relative file name, and y is an NTFS stream name. This is a
509 correct relative path, but it is very unusual.
510
511 The trouble is that first case items are also valid examples of the
512 second case, i.e., "c:test" can be understood as drive:path or as
513 file:stream.
514
515 The "right" fix would involve checking whether
516 - the current drive/partition is NTFS,
517 - x is a valid (and accesible) drive designator,
518 - x:y already exists as a file:stream in the current directory,
519 - y already exists on the current directory of drive x,
520 - the auspices are favorable,
521 and then taking an "informed decision" based on the above.
522
523 Whatever the result, Emacs currently does a very bad job of dealing
524 with NTFS file:streams: it cannot visit them, and the only way to
525 create one is by setting `buffer-file-name' to point to it (either
526 manually or with emacsclient). So perhaps resorting to 1) and ignoring
527 2) for now is the right thing to do.
528
529 Anyway, something to decide After the Release.
530 */
531 #endif
532
533 return FALSE;
534 }
535
536 #ifdef WINDOWSNT
537 /* Wrapper to make WSACleanup a cdecl, as required by atexit. */
538 void
539 __cdecl close_winsock ()
540 {
541 WSACleanup ();
542 }
543
544 /* Initialize the WinSock2 library. */
545 void
546 initialize_sockets ()
547 {
548 WSADATA wsaData;
549
550 if (WSAStartup (MAKEWORD (2, 0), &wsaData))
551 {
552 message (TRUE, "%s: error initializing WinSock2", progname);
553 exit (EXIT_FAILURE);
554 }
555
556 atexit (close_winsock);
557 }
558 #endif /* WINDOWSNT */
559 \f
560 /*
561 * Read the information needed to set up a TCP comm channel with
562 * the Emacs server: host, port, pid and authentication string.
563 */
564 int
565 get_server_config (server, authentication)
566 struct sockaddr_in *server;
567 char *authentication;
568 {
569 char dotted[32];
570 char *port;
571 char *pid;
572 FILE *config = NULL;
573
574 if (file_name_absolute_p (server_file))
575 config = fopen (server_file, "rb");
576 else
577 {
578 char *home = getenv ("HOME");
579
580 if (home)
581 {
582 char *path = alloca (32 + strlen (home) + strlen (server_file));
583 sprintf (path, "%s/.emacs.d/server/%s", home, server_file);
584 config = fopen (path, "rb");
585 }
586 #ifdef WINDOWSNT
587 if (!config && (home = getenv ("APPDATA")))
588 {
589 char *path = alloca (32 + strlen (home) + strlen (server_file));
590 sprintf (path, "%s/.emacs.d/server/%s", home, server_file);
591 config = fopen (path, "rb");
592 }
593 #endif
594 }
595
596 if (! config)
597 return FALSE;
598
599 if (fgets (dotted, sizeof dotted, config)
600 && (port = strchr (dotted, ':'))
601 && (pid = strchr (port, ' ')))
602 {
603 *port++ = '\0';
604 *pid++ = '\0';
605 }
606 else
607 {
608 message (TRUE, "%s: invalid configuration info", progname);
609 exit (EXIT_FAILURE);
610 }
611
612 server->sin_family = AF_INET;
613 server->sin_addr.s_addr = inet_addr (dotted);
614 server->sin_port = htons (atoi (port));
615
616 if (! fread (authentication, AUTH_KEY_LENGTH, 1, config))
617 {
618 message (TRUE, "%s: cannot read authentication info", progname);
619 exit (EXIT_FAILURE);
620 }
621
622 fclose (config);
623
624 emacs_pid = atoi (pid);
625
626 return TRUE;
627 }
628
629 HSOCKET
630 set_tcp_socket ()
631 {
632 HSOCKET s;
633 struct sockaddr_in server;
634 struct linger l_arg = {1, 1};
635 char auth_string[AUTH_KEY_LENGTH + 1];
636
637 if (! get_server_config (&server, auth_string))
638 return INVALID_SOCKET;
639
640 if (server.sin_addr.s_addr != inet_addr ("127.0.0.1"))
641 message (FALSE, "%s: connected to remote socket at %s\n",
642 progname, inet_ntoa (server.sin_addr));
643
644 /*
645 * Open up an AF_INET socket
646 */
647 if ((s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
648 {
649 message (TRUE, "%s: socket: %s\n", progname, strerror (errno));
650 return INVALID_SOCKET;
651 }
652
653 /*
654 * Set up the socket
655 */
656 if (connect (s, (struct sockaddr *) &server, sizeof server) < 0)
657 {
658 message (TRUE, "%s: connect: %s\n", progname, strerror (errno));
659 return INVALID_SOCKET;
660 }
661
662 setsockopt (s, SOL_SOCKET, SO_LINGER, (char *) &l_arg, sizeof l_arg);
663
664 /*
665 * Send the authentication
666 */
667 auth_string[AUTH_KEY_LENGTH] = '\0';
668
669 SEND_STRING ("-auth ");
670 SEND_STRING (auth_string);
671 SEND_STRING ("\n");
672
673 return s;
674 }
675
676 #if !defined (NO_SOCKETS_IN_FILE_SYSTEM)
677
678 /* Three possibilities:
679 2 - can't be `stat'ed (sets errno)
680 1 - isn't owned by us
681 0 - success: none of the above */
682
683 static int
684 socket_status (socket_name)
685 char *socket_name;
686 {
687 struct stat statbfr;
688
689 if (stat (socket_name, &statbfr) == -1)
690 return 2;
691
692 if (statbfr.st_uid != geteuid ())
693 return 1;
694
695 return 0;
696 }
697
698 HSOCKET
699 set_local_socket ()
700 {
701 HSOCKET s;
702 struct sockaddr_un server;
703
704 /*
705 * Open up an AF_UNIX socket in this person's home directory
706 */
707
708 if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
709 {
710 message (TRUE, "%s: socket: %s\n", progname, strerror (errno));
711 return INVALID_SOCKET;
712 }
713
714 server.sun_family = AF_UNIX;
715
716 {
717 int sock_status = 0;
718 int default_sock = !socket_name;
719 int saved_errno;
720 char *server_name = "server";
721
722 if (socket_name && !index (socket_name, '/') && !index (socket_name, '\\'))
723 { /* socket_name is a file name component. */
724 server_name = socket_name;
725 socket_name = NULL;
726 default_sock = 1; /* Try both UIDs. */
727 }
728
729 if (default_sock)
730 {
731 socket_name = alloca (100 + strlen (server_name));
732 sprintf (socket_name, "/tmp/emacs%d/%s",
733 (int) geteuid (), server_name);
734 }
735
736 if (strlen (socket_name) < sizeof (server.sun_path))
737 strcpy (server.sun_path, socket_name);
738 else
739 {
740 message (TRUE, "%s: socket-name %s too long",
741 progname, socket_name);
742 exit (EXIT_FAILURE);
743 }
744
745 /* See if the socket exists, and if it's owned by us. */
746 sock_status = socket_status (server.sun_path);
747 saved_errno = errno;
748 if (sock_status && default_sock)
749 {
750 /* Failing that, see if LOGNAME or USER exist and differ from
751 our euid. If so, look for a socket based on the UID
752 associated with the name. This is reminiscent of the logic
753 that init_editfns uses to set the global Vuser_full_name. */
754
755 char *user_name = (char *) getenv ("LOGNAME");
756
757 if (!user_name)
758 user_name = (char *) getenv ("USER");
759
760 if (user_name)
761 {
762 struct passwd *pw = getpwnam (user_name);
763
764 if (pw && (pw->pw_uid != geteuid ()))
765 {
766 /* We're running under su, apparently. */
767 socket_name = alloca (100 + strlen (server_name));
768 sprintf (socket_name, "/tmp/emacs%d/%s",
769 (int) pw->pw_uid, server_name);
770
771 if (strlen (socket_name) < sizeof (server.sun_path))
772 strcpy (server.sun_path, socket_name);
773 else
774 {
775 message (TRUE, "%s: socket-name %s too long",
776 progname, socket_name);
777 exit (EXIT_FAILURE);
778 }
779
780 sock_status = socket_status (server.sun_path);
781 saved_errno = errno;
782 }
783 else
784 errno = saved_errno;
785 }
786 }
787
788 switch (sock_status)
789 {
790 case 1:
791 /* There's a socket, but it isn't owned by us. This is OK if
792 we are root. */
793 if (0 != geteuid ())
794 {
795 message (TRUE, "%s: Invalid socket owner\n", progname);
796 return INVALID_SOCKET;
797 }
798 break;
799
800 case 2:
801 /* `stat' failed */
802 if (saved_errno == ENOENT)
803 message (TRUE,
804 "%s: can't find socket; have you started the server?\n\
805 To start the server in Emacs, type \"M-x server-start\".\n",
806 progname);
807 else
808 message (TRUE, "%s: can't stat %s: %s\n",
809 progname, server.sun_path, strerror (saved_errno));
810 return INVALID_SOCKET;
811 }
812 }
813
814 if (connect (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2)
815 < 0)
816 {
817 message (TRUE, "%s: connect: %s\n", progname, strerror (errno));
818 return INVALID_SOCKET;
819 }
820
821 return s;
822 }
823 #endif /* ! NO_SOCKETS_IN_FILE_SYSTEM */
824
825 HSOCKET
826 set_socket ()
827 {
828 HSOCKET s;
829
830 INITIALIZE ();
831
832 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
833 /* Explicit --socket-name argument. */
834 if (socket_name)
835 {
836 s = set_local_socket ();
837 if ((s != INVALID_SOCKET) || alternate_editor)
838 return s;
839
840 message (TRUE, "%s: error accessing socket \"%s\"",
841 progname, socket_name);
842 exit (EXIT_FAILURE);
843 }
844 #endif
845
846 /* Explicit --server-file arg or EMACS_SERVER_FILE variable. */
847 if (!server_file)
848 server_file = getenv ("EMACS_SERVER_FILE");
849
850 if (server_file)
851 {
852 s = set_tcp_socket ();
853 if ((s != INVALID_SOCKET) || alternate_editor)
854 return s;
855
856 message (TRUE, "%s: error accessing server file \"%s\"",
857 progname, server_file);
858 exit (EXIT_FAILURE);
859 }
860
861 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
862 /* Implicit local socket. */
863 s = set_local_socket ();
864 if (s != INVALID_SOCKET)
865 return s;
866 #endif
867
868 /* Implicit server file. */
869 server_file = "server";
870 s = set_tcp_socket ();
871 if ((s != INVALID_SOCKET) || alternate_editor)
872 return s;
873
874 /* No implicit or explicit socket, and no alternate editor. */
875 message (TRUE, "%s: No socket or alternate editor. Please use:\n\n"
876 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
877 "\t--socket-name\n"
878 #endif
879 "\t--server-file (or environment variable EMACS_SERVER_FILE)\n\
880 \t--alternate-editor (or environment variable ALTERNATE_EDITOR)\n",
881 progname);
882 exit (EXIT_FAILURE);
883 }
884
885 #ifdef WINDOWSNT
886 FARPROC set_fg; /* Pointer to AllowSetForegroundWindow. */
887 FARPROC get_wc; /* Pointer to RealGetWindowClassA. */
888
889 BOOL CALLBACK
890 w32_find_emacs_process (hWnd, lParam)
891 HWND hWnd;
892 LPARAM lParam;
893 {
894 DWORD pid;
895 char class[6];
896
897 /* Reject any window not of class "Emacs". */
898 if (! get_wc (hWnd, class, sizeof (class))
899 || strcmp (class, "Emacs"))
900 return TRUE;
901
902 /* We only need the process id, not the thread id. */
903 (void) GetWindowThreadProcessId (hWnd, &pid);
904
905 /* Not the one we're looking for. */
906 if (pid != (DWORD) emacs_pid) return TRUE;
907
908 /* OK, let's raise it. */
909 set_fg (emacs_pid);
910
911 /* Stop enumeration. */
912 return FALSE;
913 }
914
915 /*
916 * Search for a window of class "Emacs" and owned by a process with
917 * process id = emacs_pid. If found, allow it to grab the focus.
918 */
919 void
920 w32_give_focus ()
921 {
922 HMODULE hUser32;
923
924 /* It shouldn't happen when dealing with TCP sockets. */
925 if (!emacs_pid) return;
926
927 if (!(hUser32 = LoadLibrary ("user32.dll"))) return;
928
929 /* Modern Windows restrict which processes can set the foreground window.
930 emacsclient can allow Emacs to grab the focus by calling the function
931 AllowSetForegroundWindow. Unfortunately, older Windows (W95, W98 and
932 NT) lack this function, so we have to check its availability. */
933 if ((set_fg = GetProcAddress (hUser32, "AllowSetForegroundWindow"))
934 && (get_wc = GetProcAddress (hUser32, "RealGetWindowClassA")))
935 EnumWindows (w32_find_emacs_process, (LPARAM) 0);
936
937 FreeLibrary (hUser32);
938 }
939 #endif
940
941 int
942 main (argc, argv)
943 int argc;
944 char **argv;
945 {
946 HSOCKET s;
947 int i, rl, needlf = 0;
948 char *cwd;
949 char string[BUFSIZ+1];
950
951 progname = argv[0];
952
953 /* Process options. */
954 decode_options (argc, argv);
955
956 if ((argc - optind < 1) && !eval)
957 {
958 message (TRUE, "%s: file name or argument required\nTry `%s --help' for more information\n",
959 progname, progname);
960 exit (EXIT_FAILURE);
961 }
962
963 if ((s = set_socket ()) == INVALID_SOCKET)
964 fail (argc, argv);
965
966 #ifdef HAVE_GETCWD
967 cwd = getcwd (string, sizeof string);
968 #else
969 cwd = getwd (string);
970 #endif
971 if (cwd == 0)
972 {
973 /* getwd puts message in STRING if it fails. */
974 message (TRUE, "%s: %s (%s)\n", progname,
975 #ifdef HAVE_GETCWD
976 "Cannot get current working directory",
977 #else
978 string,
979 #endif
980 strerror (errno));
981 fail (argc, argv);
982 }
983
984 #ifdef WINDOWSNT
985 w32_give_focus ();
986 #endif
987
988 if (nowait)
989 SEND_STRING ("-nowait ");
990
991 if (eval)
992 SEND_STRING ("-eval ");
993
994 if (display)
995 {
996 SEND_STRING ("-display ");
997 SEND_QUOTED (display);
998 SEND_STRING (" ");
999 }
1000
1001 if ((argc - optind > 0))
1002 {
1003 for (i = optind; i < argc; i++)
1004 {
1005 if (eval)
1006 ; /* Don't prepend any cwd or anything like that. */
1007 else if (*argv[i] == '+')
1008 {
1009 char *p = argv[i] + 1;
1010 while (isdigit ((unsigned char) *p) || *p == ':') p++;
1011 if (*p != 0)
1012 {
1013 SEND_QUOTED (cwd);
1014 SEND_STRING ("/");
1015 }
1016 }
1017 else if (! file_name_absolute_p (argv[i]))
1018 {
1019 SEND_QUOTED (cwd);
1020 SEND_STRING ("/");
1021 }
1022
1023 SEND_QUOTED (argv[i]);
1024 SEND_STRING (" ");
1025 }
1026 }
1027 else
1028 {
1029 while (fgets (string, BUFSIZ, stdin))
1030 {
1031 SEND_QUOTED (string);
1032 }
1033 SEND_STRING (" ");
1034 }
1035
1036 SEND_STRING ("\n");
1037
1038 /* Maybe wait for an answer. */
1039 if (!nowait)
1040 {
1041 if (!eval)
1042 {
1043 printf ("Waiting for Emacs...");
1044 needlf = 2;
1045 }
1046 fflush (stdout);
1047
1048 /* Now, wait for an answer and print any messages. */
1049 while ((rl = recv (s, string, BUFSIZ, 0)) > 0)
1050 {
1051 string[rl] = '\0';
1052 if (needlf == 2)
1053 printf ("\n");
1054 printf ("%s", string);
1055 needlf = string[0] == '\0' ? needlf : string[strlen (string) - 1] != '\n';
1056 }
1057
1058 if (needlf)
1059 printf ("\n");
1060 fflush (stdout);
1061 }
1062
1063 CLOSE_SOCKET (s);
1064 return EXIT_SUCCESS;
1065 }
1066
1067 #endif /* HAVE_SOCKETS && HAVE_INET_SOCKETS */
1068
1069 #ifndef HAVE_STRERROR
1070 char *
1071 strerror (errnum)
1072 int errnum;
1073 {
1074 extern char *sys_errlist[];
1075 extern int sys_nerr;
1076
1077 if (errnum >= 0 && errnum < sys_nerr)
1078 return sys_errlist[errnum];
1079 return (char *) "Unknown error";
1080 }
1081
1082 #endif /* ! HAVE_STRERROR */
1083
1084 /* arch-tag: f39bb9c4-73eb-477e-896d-50832e2ca9a7
1085 (do not change this comment) */
1086
1087 /* emacsclient.c ends here */