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