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