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