(main): If using the current frame, send tty information to Emacs in
[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, 2008 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 of the License, or
10 (at your option) 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. If not, see <http://www.gnu.org/licenses/>. */
19
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #ifdef WINDOWSNT
26
27 /* config.h defines these, which disables sockets altogether! */
28 # undef _WINSOCKAPI_
29 # undef _WINSOCK_H
30
31 # include <malloc.h>
32 # include <stdlib.h>
33 # include <windows.h>
34 # include <commctrl.h>
35
36 # define NO_SOCKETS_IN_FILE_SYSTEM
37
38 # define HSOCKET SOCKET
39 # define CLOSE_SOCKET closesocket
40 # define INITIALIZE() (initialize_sockets ())
41
42 #else /* !WINDOWSNT */
43
44 # include <sys/types.h>
45
46 # ifdef HAVE_INET_SOCKETS
47 # include <netinet/in.h>
48 # endif
49
50 # define INVALID_SOCKET -1
51 # define HSOCKET int
52 # define CLOSE_SOCKET close
53 # define INITIALIZE()
54
55 #endif /* !WINDOWSNT */
56
57 #undef signal
58
59 #include <stdarg.h>
60 #include <ctype.h>
61 #include <stdio.h>
62 #include "getopt.h"
63 #ifdef HAVE_UNISTD_H
64 #include <unistd.h>
65 #endif
66
67 #ifdef WINDOWSNT
68 # include <io.h>
69 #else /* not WINDOWSNT */
70 # include <pwd.h>
71 #endif /* not WINDOWSNT */
72 #include <sys/stat.h>
73
74 #include <signal.h>
75 #include <errno.h>
76
77 \f
78 char *getenv (), *getwd ();
79 char *(getcwd) ();
80
81 #ifdef WINDOWSNT
82 char *w32_getenv ();
83 #define egetenv(VAR) w32_getenv(VAR)
84 #else
85 #define egetenv(VAR) getenv(VAR)
86 #endif
87
88 #ifndef VERSION
89 #define VERSION "unspecified"
90 #endif
91 \f
92
93 #ifndef EXIT_SUCCESS
94 #define EXIT_SUCCESS 0
95 #endif
96
97 #ifndef EXIT_FAILURE
98 #define EXIT_FAILURE 1
99 #endif
100
101 #ifndef FALSE
102 #define FALSE 0
103 #endif
104
105 #ifndef TRUE
106 #define TRUE 1
107 #endif
108
109 #ifndef NO_RETURN
110 #define NO_RETURN
111 #endif
112
113 /* Additional space when allocating buffers for filenames, etc. */
114 #define EXTRA_SPACE 100
115
116 \f
117 /* Name used to invoke this program. */
118 char *progname;
119
120 /* The second argument to main. */
121 char **main_argv;
122
123 /* Nonzero means don't wait for a response from Emacs. --no-wait. */
124 int nowait = 0;
125
126 /* Nonzero means args are expressions to be evaluated. --eval. */
127 int eval = 0;
128
129 /* Nonzero means don't open a new frame. Inverse of --create-frame. */
130 int current_frame = 1;
131
132 /* Nonzero means open a new graphical frame. */
133 int window_system = 0;
134
135 /* The display on which Emacs should work. --display. */
136 char *display = NULL;
137
138 /* Nonzero means open a new Emacs frame on the current terminal. */
139 int tty = 0;
140
141 /* If non-NULL, the name of an editor to fallback to if the server
142 is not running. --alternate-editor. */
143 const char *alternate_editor = NULL;
144
145 /* If non-NULL, the filename of the UNIX socket. */
146 char *socket_name = NULL;
147
148 /* If non-NULL, the filename of the authentication file. */
149 char *server_file = NULL;
150
151 /* PID of the Emacs server process. */
152 int emacs_pid = 0;
153
154 void print_help_and_exit () NO_RETURN;
155
156 struct option longopts[] =
157 {
158 { "no-wait", no_argument, NULL, 'n' },
159 { "eval", no_argument, NULL, 'e' },
160 { "help", no_argument, NULL, 'H' },
161 { "version", no_argument, NULL, 'V' },
162 { "tty", no_argument, NULL, 't' },
163 { "nw", no_argument, NULL, 't' },
164 { "create-frame", no_argument, NULL, 'c' },
165 { "alternate-editor", required_argument, NULL, 'a' },
166 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
167 { "socket-name", required_argument, NULL, 's' },
168 #endif
169 { "server-file", required_argument, NULL, 'f' },
170 #ifndef WINDOWSNT
171 { "display", required_argument, NULL, 'd' },
172 #endif
173 { 0, 0, 0, 0 }
174 };
175
176 \f
177 /* Like malloc but get fatal error if memory is exhausted. */
178
179 long *
180 xmalloc (size)
181 unsigned int size;
182 {
183 long *result = (long *) malloc (size);
184 if (result == NULL)
185 {
186 perror ("malloc");
187 exit (EXIT_FAILURE);
188 }
189 return result;
190 }
191
192 /* Like strdup but get a fatal error if memory is exhausted. */
193
194 char *
195 xstrdup (const char *s)
196 {
197 char *result = strdup (s);
198 if (result == NULL)
199 {
200 perror ("strdup");
201 exit (EXIT_FAILURE);
202 }
203 return result;
204 }
205
206 /* From sysdep.c */
207 #if !defined (HAVE_GET_CURRENT_DIR_NAME) || defined (BROKEN_GET_CURRENT_DIR_NAME)
208
209 /* From lisp.h */
210 #ifndef DIRECTORY_SEP
211 #define DIRECTORY_SEP '/'
212 #endif
213 #ifndef IS_DIRECTORY_SEP
214 #define IS_DIRECTORY_SEP(_c_) ((_c_) == DIRECTORY_SEP)
215 #endif
216 #ifndef IS_DEVICE_SEP
217 #ifndef DEVICE_SEP
218 #define IS_DEVICE_SEP(_c_) 0
219 #else
220 #define IS_DEVICE_SEP(_c_) ((_c_) == DEVICE_SEP)
221 #endif
222 #endif
223 #ifndef IS_ANY_SEP
224 #define IS_ANY_SEP(_c_) (IS_DIRECTORY_SEP (_c_))
225 #endif
226
227
228 /* Return the current working directory. Returns NULL on errors.
229 Any other returned value must be freed with free. This is used
230 only when get_current_dir_name is not defined on the system. */
231 char*
232 get_current_dir_name ()
233 {
234 char *buf;
235 char *pwd;
236 struct stat dotstat, pwdstat;
237 /* If PWD is accurate, use it instead of calling getwd. PWD is
238 sometimes a nicer name, and using it may avoid a fatal error if a
239 parent directory is searchable but not readable. */
240 if ((pwd = egetenv ("PWD")) != 0
241 && (IS_DIRECTORY_SEP (*pwd) || (*pwd && IS_DEVICE_SEP (pwd[1])))
242 && stat (pwd, &pwdstat) == 0
243 && stat (".", &dotstat) == 0
244 && dotstat.st_ino == pwdstat.st_ino
245 && dotstat.st_dev == pwdstat.st_dev
246 #ifdef MAXPATHLEN
247 && strlen (pwd) < MAXPATHLEN
248 #endif
249 )
250 {
251 buf = (char *) xmalloc (strlen (pwd) + 1);
252 if (!buf)
253 return NULL;
254 strcpy (buf, pwd);
255 }
256 #ifdef HAVE_GETCWD
257 else
258 {
259 size_t buf_size = 1024;
260 buf = (char *) xmalloc (buf_size);
261 if (!buf)
262 return NULL;
263 for (;;)
264 {
265 if (getcwd (buf, buf_size) == buf)
266 break;
267 if (errno != ERANGE)
268 {
269 int tmp_errno = errno;
270 free (buf);
271 errno = tmp_errno;
272 return NULL;
273 }
274 buf_size *= 2;
275 buf = (char *) realloc (buf, buf_size);
276 if (!buf)
277 return NULL;
278 }
279 }
280 #else
281 else
282 {
283 /* We need MAXPATHLEN here. */
284 buf = (char *) xmalloc (MAXPATHLEN + 1);
285 if (!buf)
286 return NULL;
287 if (getwd (buf) == NULL)
288 {
289 int tmp_errno = errno;
290 free (buf);
291 errno = tmp_errno;
292 return NULL;
293 }
294 }
295 #endif
296 return buf;
297 }
298 #endif
299
300 #ifdef WINDOWSNT
301
302 #define REG_ROOT "SOFTWARE\\GNU\\Emacs"
303
304 /* Retrieve an environment variable from the Emacs subkeys of the registry.
305 Return NULL if the variable was not found, or it was empty.
306 This code is based on w32_get_resource (w32.c). */
307 char *
308 w32_get_resource (predefined, key, type)
309 HKEY predefined;
310 char *key;
311 LPDWORD type;
312 {
313 HKEY hrootkey = NULL;
314 char *result = NULL;
315 DWORD cbData;
316
317 if (RegOpenKeyEx (predefined, REG_ROOT, 0, KEY_READ, &hrootkey) == ERROR_SUCCESS)
318 {
319 if (RegQueryValueEx (hrootkey, key, NULL, NULL, NULL, &cbData) == ERROR_SUCCESS)
320 {
321 result = (char *) xmalloc (cbData);
322
323 if ((RegQueryValueEx (hrootkey, key, NULL, type, result, &cbData) != ERROR_SUCCESS)
324 || (*result == 0))
325 {
326 free (result);
327 result = NULL;
328 }
329 }
330
331 RegCloseKey (hrootkey);
332 }
333
334 return result;
335 }
336
337 /*
338 getenv wrapper for Windows
339
340 This is needed to duplicate Emacs's behavior, which is to look for enviroment
341 variables in the registry if they don't appear in the environment.
342 */
343 char *
344 w32_getenv (envvar)
345 char *envvar;
346 {
347 char *value;
348 DWORD dwType;
349
350 if (value = getenv (envvar))
351 /* Found in the environment. */
352 return value;
353
354 if (! (value = w32_get_resource (HKEY_CURRENT_USER, envvar, &dwType)) &&
355 ! (value = w32_get_resource (HKEY_LOCAL_MACHINE, envvar, &dwType)))
356 /* Not found in the registry. */
357 return NULL;
358
359 if (dwType == REG_SZ)
360 /* Registry; no need to expand. */
361 return value;
362
363 if (dwType == REG_EXPAND_SZ)
364 {
365 DWORD size;
366
367 if (size = ExpandEnvironmentStrings (value, NULL, 0))
368 {
369 char *buffer = (char *) xmalloc (size);
370 if (ExpandEnvironmentStrings (value, buffer, size))
371 {
372 /* Found and expanded. */
373 free (value);
374 return buffer;
375 }
376
377 /* Error expanding. */
378 free (buffer);
379 }
380 }
381
382 /* Not the right type, or not correctly expanded. */
383 free (value);
384 return NULL;
385 }
386
387 int
388 w32_window_app ()
389 {
390 static int window_app = -1;
391 char szTitle[MAX_PATH];
392
393 if (window_app < 0)
394 {
395 /* Checking for STDOUT does not work; it's a valid handle also in
396 nonconsole apps. Testing for the console title seems to work. */
397 window_app = (GetConsoleTitleA (szTitle, MAX_PATH) == 0);
398 if (window_app)
399 InitCommonControls();
400 }
401
402 return window_app;
403 }
404
405 /*
406 execvp wrapper for Windows. Quotes arguments with embedded spaces.
407
408 This is necessary due to the broken implementation of exec* routines in
409 the Microsoft libraries: they concatenate the arguments together without
410 quoting special characters, and pass the result to CreateProcess, with
411 predictably bad results. By contrast, Posix execvp passes the arguments
412 directly into the argv array of the child process.
413 */
414 int
415 w32_execvp (path, argv)
416 char *path;
417 char **argv;
418 {
419 int i;
420
421 /* Required to allow a .BAT script as alternate editor. */
422 argv[0] = (char *) alternate_editor;
423
424 for (i = 0; argv[i]; i++)
425 if (strchr (argv[i], ' '))
426 {
427 char *quoted = alloca (strlen (argv[i]) + 3);
428 sprintf (quoted, "\"%s\"", argv[i]);
429 argv[i] = quoted;
430 }
431
432 return execvp (path, argv);
433 }
434
435 #undef execvp
436 #define execvp w32_execvp
437
438 #endif /* WINDOWSNT */
439
440 /* Display a normal or error message.
441 On Windows, use a message box if compiled as a Windows app. */
442 void
443 message (int is_error, char *message, ...)
444 {
445 char msg [2048];
446 va_list args;
447
448 va_start (args, message);
449 vsprintf (msg, message, args);
450 va_end (args);
451
452 #ifdef WINDOWSNT
453 if (w32_window_app ())
454 {
455 if (is_error)
456 MessageBox (NULL, msg, "Emacsclient ERROR", MB_ICONERROR);
457 else
458 MessageBox (NULL, msg, "Emacsclient", MB_ICONINFORMATION);
459 }
460 else
461 #endif
462 {
463 FILE *f = is_error ? stderr : stdout;
464
465 fputs (msg, f);
466 fflush (f);
467 }
468 }
469
470 /* Decode the options from argv and argc.
471 The global variable `optind' will say how many arguments we used up. */
472
473 void
474 decode_options (argc, argv)
475 int argc;
476 char **argv;
477 {
478 alternate_editor = egetenv ("ALTERNATE_EDITOR");
479
480 while (1)
481 {
482 int opt = getopt_long_only (argc, argv,
483 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
484 "VHnea:s:f:d:tc",
485 #else
486 "VHnea:f:d:tc",
487 #endif
488 longopts, 0);
489
490 if (opt == EOF)
491 break;
492
493 switch (opt)
494 {
495 case 0:
496 /* If getopt returns 0, then it has already processed a
497 long-named option. We should do nothing. */
498 break;
499
500 case 'a':
501 alternate_editor = optarg;
502 break;
503
504 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
505 case 's':
506 socket_name = optarg;
507 break;
508 #endif
509
510 case 'f':
511 server_file = optarg;
512 break;
513
514 /* We used to disallow this argument in w32, but it seems better
515 to allow it, for the occasional case where the user is
516 connecting with a w32 client to a server compiled with X11
517 support. */
518 #if 1 /* !defined WINDOWS */
519 case 'd':
520 display = optarg;
521 break;
522 #endif
523
524 case 'n':
525 nowait = 1;
526 break;
527
528 case 'e':
529 eval = 1;
530 break;
531
532 case 'V':
533 message (FALSE, "emacsclient %s\n", VERSION);
534 exit (EXIT_SUCCESS);
535 break;
536
537 case 't':
538 tty = 1;
539 current_frame = 0;
540 break;
541
542 case 'c':
543 current_frame = 0;
544 break;
545
546 case 'H':
547 print_help_and_exit ();
548 break;
549
550 default:
551 message (TRUE, "Try `%s --help' for more information\n", progname);
552 exit (EXIT_FAILURE);
553 break;
554 }
555 }
556
557 /* We used to set `display' to $DISPLAY by default, but this changed the
558 default behavior and is sometimes inconvenient. So instead of forcing
559 users to say "--display ''" when they want to use Emacs's existing tty
560 or display connection, we force them to use "--display $DISPLAY" if
561 they want Emacs to connect to their current display.
562 -c still implicitly passes --display $DISPLAY unless -t was specified
563 so as to try and mimick the behavior of `emacs' which either uses
564 the current tty or the current $DISPLAY. */
565 if (!current_frame && !tty && !display)
566 display = egetenv ("DISPLAY");
567
568 if (display && strlen (display) == 0)
569 display = NULL;
570
571 if (!tty && display)
572 window_system = 1;
573 #if !defined (WINDOWSNT)
574 else if (!current_frame)
575 tty = 1;
576 #endif
577
578 /* --no-wait implies --current-frame on ttys when there are file
579 arguments or expressions given. */
580 if (nowait && tty && argc - optind > 0)
581 current_frame = 1;
582
583 if (current_frame)
584 {
585 tty = 0;
586 window_system = 0;
587 }
588
589 if (tty)
590 window_system = 0;
591 }
592
593 \f
594 void
595 print_help_and_exit ()
596 {
597 /* Spaces and tabs are significant in this message; they're chosen so the
598 message aligns properly both in a tty and in a Windows message box.
599 Please try to preserve them; otherwise the output is very hard to read
600 when using emacsclientw. */
601 message (FALSE,
602 "Usage: %s [OPTIONS] FILE...\n\
603 Tell the Emacs server to visit the specified files.\n\
604 Every FILE can be either just a FILENAME or [+LINE[:COLUMN]] FILENAME.\n\
605 \n\
606 The following OPTIONS are accepted:\n\
607 -V, --version Just print version info and return\n\
608 -H, --help Print this usage information message\n\
609 -nw, -t, --tty Open a new Emacs frame on the current terminal\n\
610 -c, --create-frame Create a new frame instead of trying to\n\
611 use the current Emacs frame\n\
612 -e, --eval Evaluate the FILE arguments as ELisp expressions\n\
613 -n, --no-wait Don't wait for the server to return\n\
614 -d, --display=DISPLAY Visit the file in the given display\n"
615 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
616 "-s, --socket-name=FILENAME\n\
617 Set filename of the UNIX socket for communication\n"
618 #endif
619 "-f, --server-file=FILENAME\n\
620 Set filename of the TCP authentication file\n\
621 -a, --alternate-editor=EDITOR\n\
622 Editor to fallback to if the server is not running\n\
623 \n\
624 Report bugs to bug-gnu-emacs@gnu.org.\n", progname);
625 exit (EXIT_SUCCESS);
626 }
627
628 /*
629 Try to run a different command, or --if no alternate editor is
630 defined-- exit with an errorcode.
631 Uses argv, but gets it from the global variable main_argv.
632 */
633 void
634 fail (void)
635 {
636 if (alternate_editor)
637 {
638 int i = optind - 1;
639
640 execvp (alternate_editor, main_argv + i);
641 message (TRUE, "%s: error executing alternate editor \"%s\"\n",
642 progname, alternate_editor);
643 }
644 exit (EXIT_FAILURE);
645 }
646
647 \f
648 #if !defined (HAVE_SOCKETS) || !defined (HAVE_INET_SOCKETS)
649
650 int
651 main (argc, argv)
652 int argc;
653 char **argv;
654 {
655 main_argv = argv;
656 progname = argv[0];
657 message (TRUE, "%s: Sorry, the Emacs server is supported only\n"
658 "on systems with Berkeley sockets.\n",
659 argv[0]);
660 fail ();
661 }
662
663 #else /* HAVE_SOCKETS && HAVE_INET_SOCKETS */
664
665 #ifdef WINDOWSNT
666 # include <winsock2.h>
667 #else
668 # include <sys/types.h>
669 # include <sys/socket.h>
670 # include <sys/un.h>
671 #endif
672
673 #define AUTH_KEY_LENGTH 64
674 #define SEND_BUFFER_SIZE 4096
675
676 extern char *strerror ();
677 extern int errno;
678
679 /* Buffer to accumulate data to send in TCP connections. */
680 char send_buffer[SEND_BUFFER_SIZE + 1];
681 int sblen = 0; /* Fill pointer for the send buffer. */
682 /* Socket used to communicate with the Emacs server process. */
683 HSOCKET emacs_socket = 0;
684
685 /* On Windows, the socket library was historically separate from the standard
686 C library, so errors are handled differently. */
687 void
688 sock_err_message (function_name)
689 char *function_name;
690 {
691 #ifdef WINDOWSNT
692 char* msg = NULL;
693
694 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
695 | FORMAT_MESSAGE_ALLOCATE_BUFFER
696 | FORMAT_MESSAGE_ARGUMENT_ARRAY,
697 NULL, WSAGetLastError (), 0, (LPTSTR)&msg, 0, NULL);
698
699 message (TRUE, "%s: %s: %s\n", progname, function_name, msg);
700
701 LocalFree (msg);
702 #else
703 message (TRUE, "%s: %s: %s\n", progname, function_name, strerror (errno));
704 #endif
705 }
706
707
708 /* Let's send the data to Emacs when either
709 - the data ends in "\n", or
710 - the buffer is full (but this shouldn't happen)
711 Otherwise, we just accumulate it. */
712 void
713 send_to_emacs (s, data)
714 HSOCKET s;
715 char *data;
716 {
717 while (data)
718 {
719 int dlen = strlen (data);
720 if (dlen + sblen >= SEND_BUFFER_SIZE)
721 {
722 int part = SEND_BUFFER_SIZE - sblen;
723 strncpy (&send_buffer[sblen], data, part);
724 data += part;
725 sblen = SEND_BUFFER_SIZE;
726 }
727 else if (dlen)
728 {
729 strcpy (&send_buffer[sblen], data);
730 data = NULL;
731 sblen += dlen;
732 }
733 else
734 break;
735
736 if (sblen == SEND_BUFFER_SIZE
737 || (sblen > 0 && send_buffer[sblen-1] == '\n'))
738 {
739 int sent = send (s, send_buffer, sblen, 0);
740 if (sent != sblen)
741 strcpy (send_buffer, &send_buffer[sent]);
742 sblen -= sent;
743 }
744 }
745 }
746
747 \f
748 /* In STR, insert a & before each &, each space, each newline, and
749 any initial -. Change spaces to underscores, too, so that the
750 return value never contains a space.
751
752 Does not change the string. Outputs the result to STREAM. */
753 void
754 quote_argument (s, str)
755 HSOCKET s;
756 char *str;
757 {
758 char *copy = (char *) xmalloc (strlen (str) * 2 + 1);
759 char *p, *q;
760
761 p = str;
762 q = copy;
763 while (*p)
764 {
765 if (*p == ' ')
766 {
767 *q++ = '&';
768 *q++ = '_';
769 p++;
770 }
771 else if (*p == '\n')
772 {
773 *q++ = '&';
774 *q++ = 'n';
775 p++;
776 }
777 else
778 {
779 if (*p == '&' || (*p == '-' && p == str))
780 *q++ = '&';
781 *q++ = *p++;
782 }
783 }
784 *q++ = 0;
785
786 send_to_emacs (s, copy);
787
788 free (copy);
789 }
790
791
792 /* The inverse of quote_argument. Removes quoting in string STR by
793 modifying the string in place. Returns STR. */
794
795 char *
796 unquote_argument (str)
797 char *str;
798 {
799 char *p, *q;
800
801 if (! str)
802 return str;
803
804 p = str;
805 q = str;
806 while (*p)
807 {
808 if (*p == '&')
809 {
810 p++;
811 if (*p == '&')
812 *p = '&';
813 else if (*p == '_')
814 *p = ' ';
815 else if (*p == 'n')
816 *p = '\n';
817 else if (*p == '-')
818 *p = '-';
819 }
820 *q++ = *p++;
821 }
822 *q = 0;
823 return str;
824 }
825
826 \f
827 int
828 file_name_absolute_p (filename)
829 const unsigned char *filename;
830 {
831 /* Sanity check, it shouldn't happen. */
832 if (! filename) return FALSE;
833
834 /* /xxx is always an absolute path. */
835 if (filename[0] == '/') return TRUE;
836
837 /* Empty filenames (which shouldn't happen) are relative. */
838 if (filename[0] == '\0') return FALSE;
839
840 #ifdef WINDOWSNT
841 /* X:\xxx is always absolute. */
842 if (isalpha (filename[0])
843 && filename[1] == ':' && (filename[2] == '\\' || filename[2] == '/'))
844 return TRUE;
845
846 /* Both \xxx and \\xxx\yyy are absolute. */
847 if (filename[0] == '\\') return TRUE;
848 #endif
849
850 return FALSE;
851 }
852
853 #ifdef WINDOWSNT
854 /* Wrapper to make WSACleanup a cdecl, as required by atexit. */
855 void
856 __cdecl close_winsock ()
857 {
858 WSACleanup ();
859 }
860
861 /* Initialize the WinSock2 library. */
862 void
863 initialize_sockets ()
864 {
865 WSADATA wsaData;
866
867 if (WSAStartup (MAKEWORD (2, 0), &wsaData))
868 {
869 message (TRUE, "%s: error initializing WinSock2\n", progname);
870 exit (EXIT_FAILURE);
871 }
872
873 atexit (close_winsock);
874 }
875 #endif /* WINDOWSNT */
876
877 \f
878 /*
879 * Read the information needed to set up a TCP comm channel with
880 * the Emacs server: host, port, pid and authentication string.
881 */
882 int
883 get_server_config (server, authentication)
884 struct sockaddr_in *server;
885 char *authentication;
886 {
887 char dotted[32];
888 char *port;
889 char *pid;
890 FILE *config = NULL;
891
892 if (file_name_absolute_p (server_file))
893 config = fopen (server_file, "rb");
894 else
895 {
896 char *home = egetenv ("HOME");
897
898 if (home)
899 {
900 char *path = alloca (strlen (home) + strlen (server_file)
901 + EXTRA_SPACE);
902 sprintf (path, "%s/.emacs.d/server/%s", home, server_file);
903 config = fopen (path, "rb");
904 }
905 #ifdef WINDOWSNT
906 if (!config && (home = egetenv ("APPDATA")))
907 {
908 char *path = alloca (strlen (home) + strlen (server_file)
909 + EXTRA_SPACE);
910 sprintf (path, "%s/.emacs.d/server/%s", home, server_file);
911 config = fopen (path, "rb");
912 }
913 #endif
914 }
915
916 if (! config)
917 return FALSE;
918
919 if (fgets (dotted, sizeof dotted, config)
920 && (port = strchr (dotted, ':'))
921 && (pid = strchr (port, ' ')))
922 {
923 *port++ = '\0';
924 *pid++ = '\0';
925 }
926 else
927 {
928 message (TRUE, "%s: invalid configuration info\n", progname);
929 exit (EXIT_FAILURE);
930 }
931
932 server->sin_family = AF_INET;
933 server->sin_addr.s_addr = inet_addr (dotted);
934 server->sin_port = htons (atoi (port));
935
936 if (! fread (authentication, AUTH_KEY_LENGTH, 1, config))
937 {
938 message (TRUE, "%s: cannot read authentication info\n", progname);
939 exit (EXIT_FAILURE);
940 }
941
942 fclose (config);
943
944 emacs_pid = atoi (pid);
945
946 return TRUE;
947 }
948
949 HSOCKET
950 set_tcp_socket ()
951 {
952 HSOCKET s;
953 struct sockaddr_in server;
954 struct linger l_arg = {1, 1};
955 char auth_string[AUTH_KEY_LENGTH + 1];
956
957 if (! get_server_config (&server, auth_string))
958 return INVALID_SOCKET;
959
960 if (server.sin_addr.s_addr != inet_addr ("127.0.0.1"))
961 message (FALSE, "%s: connected to remote socket at %s\n",
962 progname, inet_ntoa (server.sin_addr));
963
964 /*
965 * Open up an AF_INET socket
966 */
967 if ((s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
968 {
969 sock_err_message ("socket");
970 return INVALID_SOCKET;
971 }
972
973 /*
974 * Set up the socket
975 */
976 if (connect (s, (struct sockaddr *) &server, sizeof server) < 0)
977 {
978 sock_err_message ("connect");
979 return INVALID_SOCKET;
980 }
981
982 setsockopt (s, SOL_SOCKET, SO_LINGER, (char *) &l_arg, sizeof l_arg);
983
984 /*
985 * Send the authentication
986 */
987 auth_string[AUTH_KEY_LENGTH] = '\0';
988
989 send_to_emacs (s, "-auth ");
990 send_to_emacs (s, auth_string);
991 send_to_emacs (s, " ");
992
993 return s;
994 }
995
996
997 /* Returns 1 if PREFIX is a prefix of STRING. */
998 static int
999 strprefix (char *prefix, char *string)
1000 {
1001 return !strncmp (prefix, string, strlen (prefix));
1002 }
1003
1004
1005 #if !defined (NO_SOCKETS_IN_FILE_SYSTEM)
1006
1007 /* Three possibilities:
1008 2 - can't be `stat'ed (sets errno)
1009 1 - isn't owned by us
1010 0 - success: none of the above */
1011
1012 static int
1013 socket_status (socket_name)
1014 char *socket_name;
1015 {
1016 struct stat statbfr;
1017
1018 if (stat (socket_name, &statbfr) == -1)
1019 return 2;
1020
1021 if (statbfr.st_uid != geteuid ())
1022 return 1;
1023
1024 return 0;
1025 }
1026
1027 \f
1028 /* A signal handler that passes the signal to the Emacs process.
1029 Useful for SIGWINCH. */
1030
1031 SIGTYPE
1032 pass_signal_to_emacs (int signalnum)
1033 {
1034 int old_errno = errno;
1035
1036 if (emacs_pid)
1037 kill (emacs_pid, signalnum);
1038
1039 signal (signalnum, pass_signal_to_emacs);
1040 errno = old_errno;
1041 }
1042
1043 /* Signal handler for SIGCONT; notify the Emacs process that it can
1044 now resume our tty frame. */
1045
1046 SIGTYPE
1047 handle_sigcont (int signalnum)
1048 {
1049 int old_errno = errno;
1050
1051 if (tcgetpgrp (1) == getpgrp ())
1052 {
1053 /* We are in the foreground. */
1054 send_to_emacs (emacs_socket, "-resume \n");
1055 }
1056 else
1057 {
1058 /* We are in the background; cancel the continue. */
1059 kill (getpid (), SIGSTOP);
1060 }
1061
1062 signal (signalnum, handle_sigcont);
1063 errno = old_errno;
1064 }
1065
1066 /* Signal handler for SIGTSTP; notify the Emacs process that we are
1067 going to sleep. Normally the suspend is initiated by Emacs via
1068 server-handle-suspend-tty, but if the server gets out of sync with
1069 reality, we may get a SIGTSTP on C-z. Handling this signal and
1070 notifying Emacs about it should get things under control again. */
1071
1072 SIGTYPE
1073 handle_sigtstp (int signalnum)
1074 {
1075 int old_errno = errno;
1076 sigset_t set;
1077
1078 if (emacs_socket)
1079 send_to_emacs (emacs_socket, "-suspend \n");
1080
1081 /* Unblock this signal and call the default handler by temporarily
1082 changing the handler and resignalling. */
1083 sigprocmask (SIG_BLOCK, NULL, &set);
1084 sigdelset (&set, signalnum);
1085 signal (signalnum, SIG_DFL);
1086 kill (getpid (), signalnum);
1087 sigprocmask (SIG_SETMASK, &set, NULL); /* Let's the above signal through. */
1088 signal (signalnum, handle_sigtstp);
1089
1090 errno = old_errno;
1091 }
1092 /* Set up signal handlers before opening a frame on the current tty. */
1093
1094 void
1095 init_signals (void)
1096 {
1097 /* Set up signal handlers. */
1098 signal (SIGWINCH, pass_signal_to_emacs);
1099
1100 /* Don't pass SIGINT and SIGQUIT to Emacs, because it has no way of
1101 deciding which terminal the signal came from. C-g is now a
1102 normal input event on secondary terminals. */
1103 #if 0
1104 signal (SIGINT, pass_signal_to_emacs);
1105 signal (SIGQUIT, pass_signal_to_emacs);
1106 #endif
1107
1108 signal (SIGCONT, handle_sigcont);
1109 signal (SIGTSTP, handle_sigtstp);
1110 signal (SIGTTOU, handle_sigtstp);
1111 }
1112
1113
1114 HSOCKET
1115 set_local_socket ()
1116 {
1117 HSOCKET s;
1118 struct sockaddr_un server;
1119
1120 /*
1121 * Open up an AF_UNIX socket in this person's home directory
1122 */
1123
1124 if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
1125 {
1126 message (TRUE, "%s: socket: %s\n", progname, strerror (errno));
1127 return INVALID_SOCKET;
1128 }
1129
1130 server.sun_family = AF_UNIX;
1131
1132 {
1133 int sock_status = 0;
1134 int default_sock = !socket_name;
1135 int saved_errno = 0;
1136 char *server_name = "server";
1137 char *tmpdir;
1138
1139 if (socket_name && !index (socket_name, '/') && !index (socket_name, '\\'))
1140 { /* socket_name is a file name component. */
1141 server_name = socket_name;
1142 socket_name = NULL;
1143 default_sock = 1; /* Try both UIDs. */
1144 }
1145
1146 if (default_sock)
1147 {
1148 tmpdir = egetenv ("TMPDIR");
1149 if (!tmpdir)
1150 tmpdir = "/tmp";
1151 socket_name = alloca (strlen (tmpdir) + strlen (server_name)
1152 + EXTRA_SPACE);
1153 sprintf (socket_name, "%s/emacs%d/%s",
1154 tmpdir, (int) geteuid (), server_name);
1155 }
1156
1157 if (strlen (socket_name) < sizeof (server.sun_path))
1158 strcpy (server.sun_path, socket_name);
1159 else
1160 {
1161 message (TRUE, "%s: socket-name %s too long\n",
1162 progname, socket_name);
1163 fail ();
1164 }
1165
1166 /* See if the socket exists, and if it's owned by us. */
1167 sock_status = socket_status (server.sun_path);
1168 saved_errno = errno;
1169 if (sock_status && default_sock)
1170 {
1171 /* Failing that, see if LOGNAME or USER exist and differ from
1172 our euid. If so, look for a socket based on the UID
1173 associated with the name. This is reminiscent of the logic
1174 that init_editfns uses to set the global Vuser_full_name. */
1175
1176 char *user_name = (char *) egetenv ("LOGNAME");
1177
1178 if (!user_name)
1179 user_name = (char *) egetenv ("USER");
1180
1181 if (user_name)
1182 {
1183 struct passwd *pw = getpwnam (user_name);
1184
1185 if (pw && (pw->pw_uid != geteuid ()))
1186 {
1187 /* We're running under su, apparently. */
1188 socket_name = alloca (strlen (tmpdir) + strlen (server_name)
1189 + EXTRA_SPACE);
1190 sprintf (socket_name, "%s/emacs%d/%s",
1191 tmpdir, (int) pw->pw_uid, server_name);
1192
1193 if (strlen (socket_name) < sizeof (server.sun_path))
1194 strcpy (server.sun_path, socket_name);
1195 else
1196 {
1197 message (TRUE, "%s: socket-name %s too long\n",
1198 progname, socket_name);
1199 exit (EXIT_FAILURE);
1200 }
1201
1202 sock_status = socket_status (server.sun_path);
1203 saved_errno = errno;
1204 }
1205 else
1206 errno = saved_errno;
1207 }
1208 }
1209
1210 switch (sock_status)
1211 {
1212 case 1:
1213 /* There's a socket, but it isn't owned by us. This is OK if
1214 we are root. */
1215 if (0 != geteuid ())
1216 {
1217 message (TRUE, "%s: Invalid socket owner\n", progname);
1218 return INVALID_SOCKET;
1219 }
1220 break;
1221
1222 case 2:
1223 /* `stat' failed */
1224 if (saved_errno == ENOENT)
1225 message (TRUE,
1226 "%s: can't find socket; have you started the server?\n\
1227 To start the server in Emacs, type \"M-x server-start\".\n",
1228 progname);
1229 else
1230 message (TRUE, "%s: can't stat %s: %s\n",
1231 progname, server.sun_path, strerror (saved_errno));
1232 return INVALID_SOCKET;
1233 }
1234 }
1235
1236 if (connect (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2)
1237 < 0)
1238 {
1239 message (TRUE, "%s: connect: %s\n", progname, strerror (errno));
1240 return INVALID_SOCKET;
1241 }
1242
1243 return s;
1244 }
1245 #endif /* ! NO_SOCKETS_IN_FILE_SYSTEM */
1246
1247 HSOCKET
1248 set_socket ()
1249 {
1250 HSOCKET s;
1251
1252 INITIALIZE ();
1253
1254 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
1255 /* Explicit --socket-name argument. */
1256 if (socket_name)
1257 {
1258 s = set_local_socket ();
1259 if ((s != INVALID_SOCKET) || alternate_editor)
1260 return s;
1261 message (TRUE, "%s: error accessing socket \"%s\"\n",
1262 progname, socket_name);
1263 exit (EXIT_FAILURE);
1264 }
1265 #endif
1266
1267 /* Explicit --server-file arg or EMACS_SERVER_FILE variable. */
1268 if (!server_file)
1269 server_file = egetenv ("EMACS_SERVER_FILE");
1270
1271 if (server_file)
1272 {
1273 s = set_tcp_socket ();
1274 if ((s != INVALID_SOCKET) || alternate_editor)
1275 return s;
1276
1277 message (TRUE, "%s: error accessing server file \"%s\"\n",
1278 progname, server_file);
1279 exit (EXIT_FAILURE);
1280 }
1281
1282 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
1283 /* Implicit local socket. */
1284 s = set_local_socket ();
1285 if (s != INVALID_SOCKET)
1286 return s;
1287 #endif
1288
1289 /* Implicit server file. */
1290 server_file = "server";
1291 s = set_tcp_socket ();
1292 if ((s != INVALID_SOCKET) || alternate_editor)
1293 return s;
1294
1295 /* No implicit or explicit socket, and no alternate editor. */
1296 message (TRUE, "%s: No socket or alternate editor. Please use:\n\n"
1297 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
1298 "\t--socket-name\n"
1299 #endif
1300 "\t--server-file (or environment variable EMACS_SERVER_FILE)\n\
1301 \t--alternate-editor (or environment variable ALTERNATE_EDITOR)\n",
1302 progname);
1303 exit (EXIT_FAILURE);
1304 }
1305
1306 #ifdef WINDOWSNT
1307 FARPROC set_fg; /* Pointer to AllowSetForegroundWindow. */
1308 FARPROC get_wc; /* Pointer to RealGetWindowClassA. */
1309
1310 BOOL CALLBACK
1311 w32_find_emacs_process (hWnd, lParam)
1312 HWND hWnd;
1313 LPARAM lParam;
1314 {
1315 DWORD pid;
1316 char class[6];
1317
1318 /* Reject any window not of class "Emacs". */
1319 if (! get_wc (hWnd, class, sizeof (class))
1320 || strcmp (class, "Emacs"))
1321 return TRUE;
1322
1323 /* We only need the process id, not the thread id. */
1324 (void) GetWindowThreadProcessId (hWnd, &pid);
1325
1326 /* Not the one we're looking for. */
1327 if (pid != (DWORD) emacs_pid) return TRUE;
1328
1329 /* OK, let's raise it. */
1330 set_fg (emacs_pid);
1331
1332 /* Stop enumeration. */
1333 return FALSE;
1334 }
1335
1336 /*
1337 * Search for a window of class "Emacs" and owned by a process with
1338 * process id = emacs_pid. If found, allow it to grab the focus.
1339 */
1340 void
1341 w32_give_focus ()
1342 {
1343 HMODULE hUser32;
1344
1345 /* It shouldn't happen when dealing with TCP sockets. */
1346 if (!emacs_pid) return;
1347
1348 if (!(hUser32 = LoadLibrary ("user32.dll"))) return;
1349
1350 /* Modern Windows restrict which processes can set the foreground window.
1351 emacsclient can allow Emacs to grab the focus by calling the function
1352 AllowSetForegroundWindow. Unfortunately, older Windows (W95, W98 and
1353 NT) lack this function, so we have to check its availability. */
1354 if ((set_fg = GetProcAddress (hUser32, "AllowSetForegroundWindow"))
1355 && (get_wc = GetProcAddress (hUser32, "RealGetWindowClassA")))
1356 EnumWindows (w32_find_emacs_process, (LPARAM) 0);
1357
1358 FreeLibrary (hUser32);
1359 }
1360 #endif
1361
1362 int
1363 main (argc, argv)
1364 int argc;
1365 char **argv;
1366 {
1367 int i, rl, needlf = 0;
1368 char *cwd, *str;
1369 char string[BUFSIZ+1];
1370
1371 main_argv = argv;
1372 progname = argv[0];
1373
1374 /* Process options. */
1375 decode_options (argc, argv);
1376
1377 if ((argc - optind < 1) && !eval && !tty && !window_system)
1378 {
1379 message (TRUE, "%s: file name or argument required\n"
1380 "Try `%s --help' for more information\n",
1381 progname, progname);
1382 exit (EXIT_FAILURE);
1383 }
1384
1385 if ((emacs_socket = set_socket ()) == INVALID_SOCKET)
1386 fail ();
1387
1388
1389 cwd = get_current_dir_name ();
1390 if (cwd == 0)
1391 {
1392 /* getwd puts message in STRING if it fails. */
1393 message (TRUE, "%s: %s\n", progname,
1394 "Cannot get current working directory");
1395 fail ();
1396 }
1397
1398 #ifdef WINDOWSNT
1399 w32_give_focus ();
1400 #endif
1401
1402 /* Send over our environment. */
1403 if (!current_frame)
1404 {
1405 extern char **environ;
1406 int i;
1407 for (i = 0; environ[i]; i++)
1408 {
1409 char *name = xstrdup (environ[i]);
1410 char *value = strchr (name, '=');
1411 send_to_emacs (emacs_socket, "-env ");
1412 quote_argument (emacs_socket, environ[i]);
1413 send_to_emacs (emacs_socket, " ");
1414 }
1415 }
1416
1417 /* Send over our current directory. */
1418 if (!current_frame)
1419 {
1420 send_to_emacs (emacs_socket, "-dir ");
1421 quote_argument (emacs_socket, cwd);
1422 send_to_emacs (emacs_socket, "/");
1423 send_to_emacs (emacs_socket, " ");
1424 }
1425
1426 retry:
1427 if (nowait)
1428 send_to_emacs (emacs_socket, "-nowait ");
1429
1430 if (current_frame)
1431 send_to_emacs (emacs_socket, "-current-frame ");
1432
1433 if (display)
1434 {
1435 send_to_emacs (emacs_socket, "-display ");
1436 quote_argument (emacs_socket, display);
1437 send_to_emacs (emacs_socket, " ");
1438 }
1439
1440 /* If using the current frame, send tty information to Emacs anyway.
1441 In daemon mode, Emacs may need to occupy this tty if no other
1442 frame is available. */
1443 if (tty || current_frame)
1444 {
1445 char *type = egetenv ("TERM");
1446 char *tty_name = NULL;
1447 #ifndef WINDOWSNT
1448 tty_name = ttyname (fileno (stdout));
1449 #endif
1450
1451 if (! tty_name)
1452 {
1453 message (TRUE, "%s: could not get terminal name\n", progname);
1454 fail ();
1455 }
1456
1457 if (! type)
1458 {
1459 message (TRUE, "%s: please set the TERM variable to your terminal type\n",
1460 progname);
1461 fail ();
1462 }
1463
1464 if (! strcmp (type, "eterm"))
1465 {
1466 /* This causes nasty, MULTI_KBOARD-related input lockouts. */
1467 message (TRUE, "%s: opening a frame in an Emacs term buffer"
1468 " is not supported\n", progname);
1469 fail ();
1470 }
1471 #if !defined (NO_SOCKETS_IN_FILE_SYSTEM)
1472 init_signals ();
1473 #endif
1474
1475 send_to_emacs (emacs_socket, "-tty ");
1476 quote_argument (emacs_socket, tty_name);
1477 send_to_emacs (emacs_socket, " ");
1478 quote_argument (emacs_socket, type);
1479 send_to_emacs (emacs_socket, " ");
1480 }
1481
1482 if (window_system)
1483 send_to_emacs (emacs_socket, "-window-system ");
1484
1485 if ((argc - optind > 0))
1486 {
1487 for (i = optind; i < argc; i++)
1488 {
1489 int relative = 0;
1490
1491 if (eval)
1492 {
1493 /* Don't prepend cwd or anything like that. */
1494 send_to_emacs (emacs_socket, "-eval ");
1495 quote_argument (emacs_socket, argv[i]);
1496 send_to_emacs (emacs_socket, " ");
1497 continue;
1498 }
1499
1500 if (*argv[i] == '+')
1501 {
1502 char *p = argv[i] + 1;
1503 while (isdigit ((unsigned char) *p) || *p == ':') p++;
1504 if (*p == 0)
1505 {
1506 send_to_emacs (emacs_socket, "-position ");
1507 quote_argument (emacs_socket, argv[i]);
1508 send_to_emacs (emacs_socket, " ");
1509 continue;
1510 }
1511 else
1512 relative = 1;
1513 }
1514 else if (! file_name_absolute_p (argv[i]))
1515 #ifndef WINDOWSNT
1516 relative = 1;
1517 #else
1518 /* Call GetFullPathName so filenames of the form X:Y, where X is
1519 a valid drive designator, are interpreted as drive:path, not
1520 file:stream, and treated as absolute.
1521 The user can still pass a file:stream if desired (for example,
1522 .\X:Y), but it is not very useful, as Emacs currently does a
1523 very bad job of dealing with NTFS streams. */
1524 {
1525 char *filename = (char *) xmalloc (MAX_PATH);
1526 DWORD size;
1527
1528 size = GetFullPathName (argv[i], MAX_PATH, filename, NULL);
1529 if (size > 0 && size < MAX_PATH)
1530 argv[i] = filename;
1531 else
1532 {
1533 relative = 1;
1534 free (filename);
1535 }
1536 }
1537 #endif
1538
1539 send_to_emacs (emacs_socket, "-file ");
1540 if (relative)
1541 {
1542 quote_argument (emacs_socket, cwd);
1543 send_to_emacs (emacs_socket, "/");
1544 }
1545 quote_argument (emacs_socket, argv[i]);
1546 send_to_emacs (emacs_socket, " ");
1547 }
1548 }
1549 else
1550 {
1551 if (!tty && !window_system)
1552 {
1553 while ((str = fgets (string, BUFSIZ, stdin)))
1554 {
1555 if (eval)
1556 send_to_emacs (emacs_socket, "-eval ");
1557 else
1558 send_to_emacs (emacs_socket, "-file ");
1559 quote_argument (emacs_socket, str);
1560 }
1561 send_to_emacs (emacs_socket, " ");
1562 }
1563 }
1564
1565 send_to_emacs (emacs_socket, "\n");
1566
1567 /* Wait for an answer. */
1568 if (!eval && !tty && !nowait)
1569 {
1570 printf ("Waiting for Emacs...");
1571 needlf = 2;
1572 }
1573 fflush (stdout);
1574 fsync (1);
1575
1576 /* Now, wait for an answer and print any messages. */
1577 while ((rl = recv (emacs_socket, string, BUFSIZ, 0)) > 0)
1578 {
1579 char *p;
1580 string[rl] = '\0';
1581
1582 p = string + strlen (string) - 1;
1583 while (p > string && *p == '\n')
1584 *p-- = 0;
1585
1586 if (strprefix ("-emacs-pid ", string))
1587 {
1588 /* -emacs-pid PID: The process id of the Emacs process. */
1589 emacs_pid = strtol (string + strlen ("-emacs-pid"), NULL, 10);
1590 }
1591 else if (strprefix ("-window-system-unsupported ", string))
1592 {
1593 /* -window-system-unsupported: Emacs was compiled without X
1594 support. Try again on the terminal. */
1595 window_system = 0;
1596 nowait = 0;
1597 tty = 1;
1598 goto retry;
1599 }
1600 else if (strprefix ("-print ", string))
1601 {
1602 /* -print STRING: Print STRING on the terminal. */
1603 str = unquote_argument (string + strlen ("-print "));
1604 if (needlf)
1605 printf ("\n");
1606 printf ("%s", str);
1607 needlf = str[0] == '\0' ? needlf : str[strlen (str) - 1] != '\n';
1608 }
1609 else if (strprefix ("-error ", string))
1610 {
1611 /* -error DESCRIPTION: Signal an error on the terminal. */
1612 str = unquote_argument (string + strlen ("-error "));
1613 if (needlf)
1614 printf ("\n");
1615 fprintf (stderr, "*ERROR*: %s", str);
1616 needlf = str[0] == '\0' ? needlf : str[strlen (str) - 1] != '\n';
1617 }
1618 #ifdef SIGSTOP
1619 else if (strprefix ("-suspend ", string))
1620 {
1621 /* -suspend: Suspend this terminal, i.e., stop the process. */
1622 if (needlf)
1623 printf ("\n");
1624 needlf = 0;
1625 kill (0, SIGSTOP);
1626 }
1627 #endif
1628 else
1629 {
1630 /* Unknown command. */
1631 if (needlf)
1632 printf ("\n");
1633 printf ("*ERROR*: Unknown message: %s", string);
1634 needlf = string[0] == '\0' ? needlf : string[strlen (string) - 1] != '\n';
1635 }
1636 }
1637
1638 if (needlf)
1639 printf ("\n");
1640 fflush (stdout);
1641 fsync (1);
1642
1643 CLOSE_SOCKET (emacs_socket);
1644 return EXIT_SUCCESS;
1645 }
1646
1647 #endif /* HAVE_SOCKETS && HAVE_INET_SOCKETS */
1648
1649 \f
1650 #ifndef HAVE_STRERROR
1651 char *
1652 strerror (errnum)
1653 int errnum;
1654 {
1655 extern char *sys_errlist[];
1656 extern int sys_nerr;
1657
1658 if (errnum >= 0 && errnum < sys_nerr)
1659 return sys_errlist[errnum];
1660 return (char *) "Unknown error";
1661 }
1662
1663 #endif /* ! HAVE_STRERROR */
1664
1665 /* arch-tag: f39bb9c4-73eb-477e-896d-50832e2ca9a7
1666 (do not change this comment) */
1667
1668 /* emacsclient.c ends here */