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