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