]> code.delx.au - gnu-emacs/blob - lib-src/emacsclient.c
Merged from emacs@sv.gnu.org
[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 #undef signal
30
31 #include <ctype.h>
32 #include <stdio.h>
33 #include <getopt.h>
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 #ifdef VMS
39 # include "vms-pwd.h"
40 #else
41 # include <pwd.h>
42 #endif /* not VMS */
43 #include <sys/stat.h>
44
45 #include <signal.h>
46 #include <errno.h>
47
48 /* From lisp.h */
49 #ifndef DIRECTORY_SEP
50 #define DIRECTORY_SEP '/'
51 #endif
52 #ifndef IS_DIRECTORY_SEP
53 #define IS_DIRECTORY_SEP(_c_) ((_c_) == DIRECTORY_SEP)
54 #endif
55 #ifndef IS_DEVICE_SEP
56 #ifndef DEVICE_SEP
57 #define IS_DEVICE_SEP(_c_) 0
58 #else
59 #define IS_DEVICE_SEP(_c_) ((_c_) == DEVICE_SEP)
60 #endif
61 #endif
62 #ifndef IS_ANY_SEP
63 #define IS_ANY_SEP(_c_) (IS_DIRECTORY_SEP (_c_))
64 #endif
65
66
67 \f
68 char *getenv (), *getwd ();
69 char *(getcwd) ();
70
71 #ifndef VERSION
72 #define VERSION "unspecified"
73 #endif
74 \f
75 /* Name used to invoke this program. */
76 char *progname;
77
78 /* The first argument to main. */
79 int main_argc;
80
81 /* The second argument to main. */
82 char **main_argv;
83
84 /* Nonzero means don't wait for a response from Emacs. --no-wait. */
85 int nowait = 0;
86
87 /* Nonzero means args are expressions to be evaluated. --eval. */
88 int eval = 0;
89
90 /* Nonzero means don't open a new frame. --current-frame. */
91 int current_frame = 0;
92
93 /* Nonzero means open a new graphical frame. */
94 int window_system = 0;
95
96 /* The display on which Emacs should work. --display. */
97 char *display = NULL;
98
99 /* Nonzero means open a new Emacs frame on the current terminal. */
100 int tty = 0;
101
102 /* If non-NULL, the name of an editor to fallback to if the server
103 is not running. --alternate-editor. */
104 const char * alternate_editor = NULL;
105
106 /* If non-NULL, the filename of the UNIX socket. */
107 char *socket_name = NULL;
108
109 void print_help_and_exit () NO_RETURN;
110
111 struct option longopts[] =
112 {
113 { "no-wait", no_argument, NULL, 'n' },
114 { "eval", no_argument, NULL, 'e' },
115 { "help", no_argument, NULL, 'H' },
116 { "version", no_argument, NULL, 'V' },
117 { "tty", no_argument, NULL, 't' },
118 { "current-frame", no_argument, NULL, 'c' },
119 { "alternate-editor", required_argument, NULL, 'a' },
120 { "socket-name", required_argument, NULL, 's' },
121 { "display", required_argument, NULL, 'd' },
122 { 0, 0, 0, 0 }
123 };
124
125 /* Decode the options from argv and argc.
126 The global variable `optind' will say how many arguments we used up. */
127
128 void
129 decode_options (argc, argv)
130 int argc;
131 char **argv;
132 {
133 alternate_editor = getenv ("ALTERNATE_EDITOR");
134 display = getenv ("DISPLAY");
135 if (display && strlen (display) == 0)
136 display = NULL;
137
138 while (1)
139 {
140 int opt = getopt_long (argc, argv,
141 "VHnea:s:d:tc", longopts, 0);
142
143 if (opt == EOF)
144 break;
145
146 switch (opt)
147 {
148 case 0:
149 /* If getopt returns 0, then it has already processed a
150 long-named option. We should do nothing. */
151 break;
152
153 case 'a':
154 alternate_editor = optarg;
155 break;
156
157 case 's':
158 socket_name = optarg;
159 break;
160
161 case 'd':
162 display = optarg;
163 break;
164
165 case 'n':
166 nowait = 1;
167 break;
168
169 case 'e':
170 eval = 1;
171 break;
172
173 case 'V':
174 printf ("emacsclient %s\n", VERSION);
175 exit (EXIT_SUCCESS);
176 break;
177
178 case 't':
179 tty = 1;
180 break;
181
182 case 'c':
183 current_frame = 1;
184 break;
185
186 case 'H':
187 print_help_and_exit ();
188 break;
189
190 default:
191 fprintf (stderr, "Try `%s --help' for more information\n", progname);
192 exit (EXIT_FAILURE);
193 break;
194 }
195 }
196
197 if (!tty && display)
198 window_system = 1;
199 else
200 tty = 1;
201
202 /* --no-wait implies --current-frame on ttys when there are file
203 arguments or expressions given. */
204 if (nowait && tty && argc - optind > 0)
205 current_frame = 1;
206
207 if (current_frame)
208 {
209 tty = 0;
210 window_system = 0;
211 }
212
213 if (tty)
214 window_system = 0;
215 }
216
217 void
218 print_help_and_exit ()
219 {
220 printf (
221 "Usage: %s [OPTIONS] FILE...\n\
222 Tell the Emacs server to visit the specified files.\n\
223 Every FILE can be either just a FILENAME or [+LINE[:COLUMN]] FILENAME.\n\
224 \n\
225 The following OPTIONS are accepted:\n\
226 -V, --version Just print a version info and return\n\
227 -H, --help Print this usage information message\n\
228 -t, --tty Open a new Emacs frame on the current terminal\n\
229 -c, --current-frame Do not create a new frame; use the current Emacs frame\n\
230 -n, --no-wait Don't wait for the server to return\n\
231 -e, --eval Evaluate the FILE arguments as ELisp expressions\n\
232 -d, --display=DISPLAY Visit the file in the given display\n\
233 -s, --socket-name=FILENAME\n\
234 Set the filename of the UNIX socket for communication\n\
235 -a, --alternate-editor=EDITOR\n\
236 Editor to fallback to if the server is not running\n\
237 \n\
238 Report bugs to bug-gnu-emacs@gnu.org.\n", progname);
239 exit (EXIT_SUCCESS);
240 }
241
242 /* Like malloc but get fatal error if memory is exhausted. */
243
244 long *
245 xmalloc (size)
246 unsigned int size;
247 {
248 long *result = (long *) malloc (size);
249 if (result == NULL)
250 {
251 perror ("malloc");
252 exit (EXIT_FAILURE);
253 }
254 return result;
255 }
256
257 /* Like strdup but get a fatal error if memory is exhausted. */
258
259 char *
260 xstrdup (const char *s)
261 {
262 char *result = strdup (s);
263 if (result == NULL)
264 {
265 perror ("strdup");
266 exit (EXIT_FAILURE);
267 }
268 return result;
269 }
270
271 /* From sysdep.c */
272 #if !defined (HAVE_GET_CURRENT_DIR_NAME) || defined (BROKEN_GET_CURRENT_DIR_NAME)
273
274 /* Return the current working directory. Returns NULL on errors.
275 Any other returned value must be freed with free. This is used
276 only when get_current_dir_name is not defined on the system. */
277 char*
278 get_current_dir_name ()
279 {
280 char *buf;
281 char *pwd;
282 struct stat dotstat, pwdstat;
283 /* If PWD is accurate, use it instead of calling getwd. PWD is
284 sometimes a nicer name, and using it may avoid a fatal error if a
285 parent directory is searchable but not readable. */
286 if ((pwd = getenv ("PWD")) != 0
287 && (IS_DIRECTORY_SEP (*pwd) || (*pwd && IS_DEVICE_SEP (pwd[1])))
288 && stat (pwd, &pwdstat) == 0
289 && stat (".", &dotstat) == 0
290 && dotstat.st_ino == pwdstat.st_ino
291 && dotstat.st_dev == pwdstat.st_dev
292 #ifdef MAXPATHLEN
293 && strlen (pwd) < MAXPATHLEN
294 #endif
295 )
296 {
297 buf = (char *) malloc (strlen (pwd) + 1);
298 if (!buf)
299 return NULL;
300 strcpy (buf, pwd);
301 }
302 #ifdef HAVE_GETCWD
303 else
304 {
305 size_t buf_size = 1024;
306 buf = (char *) malloc (buf_size);
307 if (!buf)
308 return NULL;
309 for (;;)
310 {
311 if (getcwd (buf, buf_size) == buf)
312 break;
313 if (errno != ERANGE)
314 {
315 int tmp_errno = errno;
316 free (buf);
317 errno = tmp_errno;
318 return NULL;
319 }
320 buf_size *= 2;
321 buf = (char *) realloc (buf, buf_size);
322 if (!buf)
323 return NULL;
324 }
325 }
326 #else
327 else
328 {
329 /* We need MAXPATHLEN here. */
330 buf = (char *) malloc (MAXPATHLEN + 1);
331 if (!buf)
332 return NULL;
333 if (getwd (buf) == NULL)
334 {
335 int tmp_errno = errno;
336 free (buf);
337 errno = tmp_errno;
338 return NULL;
339 }
340 }
341 #endif
342 return buf;
343 }
344 #endif
345
346
347 \f
348 /* In STR, insert a & before each &, each space, each newline, and
349 any initial -. Change spaces to underscores, too, so that the
350 return value never contains a space.
351
352 Does not change the string. Outputs the result to STREAM. */
353
354 void
355 quote_argument (str, stream)
356 char *str;
357 FILE *stream;
358 {
359 char *copy = (char *) xmalloc (strlen (str) * 2 + 1);
360 char *p, *q;
361
362 p = str;
363 q = copy;
364 while (*p)
365 {
366 if (*p == ' ')
367 {
368 *q++ = '&';
369 *q++ = '_';
370 p++;
371 }
372 else if (*p == '\n')
373 {
374 *q++ = '&';
375 *q++ = 'n';
376 p++;
377 }
378 else
379 {
380 if (*p == '&' || (*p == '-' && p == str))
381 *q++ = '&';
382 *q++ = *p++;
383 }
384 }
385 *q++ = 0;
386
387 fprintf (stream, "%s", copy);
388
389 free (copy);
390 }
391
392
393 /* The inverse of quote_argument. Removes quoting in string STR by
394 modifying the string in place. Returns STR. */
395
396 char *
397 unquote_argument (str)
398 char *str;
399 {
400 char *p, *q;
401
402 if (! str)
403 return str;
404
405 p = str;
406 q = str;
407 while (*p)
408 {
409 if (*p == '&')
410 {
411 p++;
412 if (*p == '&')
413 *p = '&';
414 else if (*p == '_')
415 *p = ' ';
416 else if (*p == 'n')
417 *p = '\n';
418 else if (*p == '-')
419 *p = '-';
420 }
421 *q++ = *p++;
422 }
423 *q = 0;
424 return str;
425 }
426
427 \f
428 /*
429 Try to run a different command, or --if no alternate editor is
430 defined-- exit with an errorcode.
431 */
432 void
433 fail (void)
434 {
435 if (alternate_editor)
436 {
437 int i = optind - 1;
438 execvp (alternate_editor, main_argv + i);
439 return;
440 }
441 else
442 {
443 exit (EXIT_FAILURE);
444 }
445 }
446
447 /* The process id of Emacs. */
448 int emacs_pid;
449
450 /* File handles for communicating with Emacs. */
451 FILE *out, *in;
452
453 /* A signal handler that passes the signal to the Emacs process.
454 Useful for SIGWINCH. */
455
456 SIGTYPE
457 pass_signal_to_emacs (int signalnum)
458 {
459 int old_errno = errno;
460
461 if (emacs_pid)
462 kill (emacs_pid, signalnum);
463
464 signal (signalnum, pass_signal_to_emacs);
465 errno = old_errno;
466 }
467
468 /* Signal handler for SIGCONT; notify the Emacs process that it can
469 now resume our tty frame. */
470
471 SIGTYPE
472 handle_sigcont (int signalnum)
473 {
474 int old_errno = errno;
475
476 if (tcgetpgrp (1) == getpgrp ())
477 {
478 /* We are in the foreground. */
479 fprintf (out, "-resume \n");
480 fflush (out);
481 fsync (fileno (out));
482 }
483 else
484 {
485 /* We are in the background; cancel the continue. */
486 kill (getpid (), SIGSTOP);
487 }
488
489 signal (signalnum, handle_sigcont);
490 errno = old_errno;
491 }
492
493 /* Signal handler for SIGTSTP; notify the Emacs process that we are
494 going to sleep. Normally the suspend is initiated by Emacs via
495 server-handle-suspend-tty, but if the server gets out of sync with
496 reality, we may get a SIGTSTP on C-z. Handling this signal and
497 notifying Emacs about it should get things under control again. */
498
499 SIGTYPE
500 handle_sigtstp (int signalnum)
501 {
502 int old_errno = errno;
503 sigset_t set;
504
505 if (out)
506 {
507 fprintf (out, "-suspend \n");
508 fflush (out);
509 fsync (fileno (out));
510 }
511
512 /* Unblock this signal and call the default handler by temprarily
513 changing the handler and resignalling. */
514 sigprocmask (SIG_BLOCK, NULL, &set);
515 sigdelset (&set, signalnum);
516 signal (signalnum, SIG_DFL);
517 kill (getpid (), signalnum);
518 sigprocmask (SIG_SETMASK, &set, NULL); /* Let's the above signal through. */
519 signal (signalnum, handle_sigtstp);
520
521 errno = old_errno;
522 }
523
524 /* Set up signal handlers before opening a frame on the current tty. */
525
526 void
527 init_signals (void)
528 {
529 /* Set up signal handlers. */
530 signal (SIGWINCH, pass_signal_to_emacs);
531
532 /* Don't pass SIGINT and SIGQUIT to Emacs, because it has no way of
533 deciding which terminal the signal came from. C-g is now a
534 normal input event on secondary terminals. */
535 #if 0
536 signal (SIGINT, pass_signal_to_emacs);
537 signal (SIGQUIT, pass_signal_to_emacs);
538 #endif
539
540 signal (SIGCONT, handle_sigcont);
541 signal (SIGTSTP, handle_sigtstp);
542 signal (SIGTTOU, handle_sigtstp);
543 }
544
545 \f
546 #if !defined (HAVE_SOCKETS) || defined (NO_SOCKETS_IN_FILE_SYSTEM)
547
548 int
549 main (argc, argv)
550 int argc;
551 char **argv;
552 {
553 fprintf (stderr, "%s: Sorry, the Emacs server is supported only\n",
554 argv[0]);
555 fprintf (stderr, "on systems with Berkeley sockets.\n");
556
557 fail ();
558 }
559
560 #else /* HAVE_SOCKETS */
561
562 #include <sys/types.h>
563 #include <sys/socket.h>
564 #include <sys/un.h>
565 #include <sys/stat.h>
566 #include <errno.h>
567
568 extern char *strerror ();
569 extern int errno;
570
571 /* Three possibilities:
572 2 - can't be `stat'ed (sets errno)
573 1 - isn't owned by us
574 0 - success: none of the above */
575
576 static int
577 socket_status (socket_name)
578 char *socket_name;
579 {
580 struct stat statbfr;
581
582 if (stat (socket_name, &statbfr) == -1)
583 return 2;
584
585 if (statbfr.st_uid != geteuid ())
586 return 1;
587
588 return 0;
589 }
590
591 /* Returns 1 if PREFIX is a prefix of STRING. */
592 static int
593 strprefix (char *prefix, char *string)
594 {
595 int i;
596 if (! prefix)
597 return 1;
598
599 if (!string)
600 return 0;
601
602 for (i = 0; prefix[i]; i++)
603 if (!string[i] || string[i] != prefix[i])
604 return 0;
605 return 1;
606 }
607
608 int
609 main (argc, argv)
610 int argc;
611 char **argv;
612 {
613 int s, i, needlf = 0;
614 struct sockaddr_un server;
615 char *cwd, *str;
616 char string[BUFSIZ];
617
618 main_argc = argc;
619 main_argv = argv;
620 progname = argv[0];
621
622 /* Process options. */
623 decode_options (argc, argv);
624
625 if ((argc - optind < 1) && !eval && !tty && !window_system)
626 {
627 fprintf (stderr, "%s: file name or argument required\n", progname);
628 fprintf (stderr, "Try `%s --help' for more information\n", progname);
629 exit (EXIT_FAILURE);
630 }
631
632 /*
633 * Open up an AF_UNIX socket in this person's home directory
634 */
635
636 if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
637 {
638 fprintf (stderr, "%s: ", argv[0]);
639 perror ("socket");
640 fail ();
641 }
642
643 server.sun_family = AF_UNIX;
644
645 {
646 int sock_status = 0;
647 int default_sock = !socket_name;
648 int saved_errno = 0;
649
650 char *server_name = "server";
651
652 if (socket_name && !index (socket_name, '/') && !index (socket_name, '\\'))
653 { /* socket_name is a file name component. */
654 server_name = socket_name;
655 socket_name = NULL;
656 default_sock = 1; /* Try both UIDs. */
657 }
658
659 if (default_sock)
660 {
661 socket_name = alloca (100 + strlen (server_name));
662 sprintf (socket_name, "/tmp/emacs%d/%s",
663 (int) geteuid (), server_name);
664 }
665
666 if (strlen (socket_name) < sizeof (server.sun_path))
667 strcpy (server.sun_path, socket_name);
668 else
669 {
670 fprintf (stderr, "%s: socket-name %s too long",
671 argv[0], socket_name);
672 fail ();
673 }
674
675 /* See if the socket exists, and if it's owned by us. */
676 sock_status = socket_status (server.sun_path);
677 saved_errno = errno;
678 if (sock_status && default_sock)
679 {
680 /* Failing that, see if LOGNAME or USER exist and differ from
681 our euid. If so, look for a socket based on the UID
682 associated with the name. This is reminiscent of the logic
683 that init_editfns uses to set the global Vuser_full_name. */
684
685 char *user_name = (char *) getenv ("LOGNAME");
686
687 if (!user_name)
688 user_name = (char *) getenv ("USER");
689
690 if (user_name)
691 {
692 struct passwd *pw = getpwnam (user_name);
693
694 if (pw && (pw->pw_uid != geteuid ()))
695 {
696 /* We're running under su, apparently. */
697 socket_name = alloca (100 + strlen (server_name));
698 sprintf (socket_name, "/tmp/emacs%d/%s",
699 (int) pw->pw_uid, server_name);
700
701 if (strlen (socket_name) < sizeof (server.sun_path))
702 strcpy (server.sun_path, socket_name);
703 else
704 {
705 fprintf (stderr, "%s: socket-name %s too long",
706 argv[0], socket_name);
707 exit (EXIT_FAILURE);
708 }
709
710 sock_status = socket_status (server.sun_path);
711 saved_errno = errno;
712 }
713 else
714 errno = saved_errno;
715 }
716 }
717
718 switch (sock_status)
719 {
720 case 1:
721 /* There's a socket, but it isn't owned by us. This is OK if
722 we are root. */
723 if (0 != geteuid ())
724 {
725 fprintf (stderr, "%s: Invalid socket owner\n", argv[0]);
726 fail ();
727 }
728 break;
729
730 case 2:
731 /* `stat' failed */
732 if (saved_errno == ENOENT)
733 fprintf (stderr,
734 "%s: can't find socket; have you started the server?\n\
735 To start the server in Emacs, type \"M-x server-start\".\n",
736 argv[0]);
737 else
738 fprintf (stderr, "%s: can't stat %s: %s\n",
739 argv[0], server.sun_path, strerror (saved_errno));
740 fail ();
741 break;
742 }
743 }
744
745 if (connect (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2)
746 < 0)
747 {
748 fprintf (stderr, "%s: ", argv[0]);
749 perror ("connect");
750 fail ();
751 }
752
753 /* We use the stream OUT to send our commands to the server. */
754 if ((out = fdopen (s, "r+")) == NULL)
755 {
756 fprintf (stderr, "%s: ", argv[0]);
757 perror ("fdopen");
758 fail ();
759 }
760
761 /* We use the stream IN to read the responses.
762 We used to use just one stream for both output and input
763 on the socket, but reversing direction works nonportably:
764 on some systems, the output appears as the first input;
765 on other systems it does not. */
766 if ((in = fdopen (s, "r+")) == NULL)
767 {
768 fprintf (stderr, "%s: ", argv[0]);
769 perror ("fdopen");
770 fail ();
771 }
772
773 #ifdef HAVE_GETCWD
774 cwd = getcwd (string, sizeof string);
775 #else
776 cwd = getwd (string);
777 #endif
778 if (cwd == 0)
779 {
780 /* getwd puts message in STRING if it fails. */
781
782 #ifdef HAVE_GETCWD
783 fprintf (stderr, "%s: %s (%s)\n", argv[0],
784 "cannot get current working directory", strerror (errno));
785 #else
786 fprintf (stderr, "%s: %s (%s)\n", argv[0], string, strerror (errno));
787 #endif
788 fail ();
789 }
790
791 /* First of all, send our version number for verification. */
792 fprintf (out, "-version %s ", VERSION);
793
794 /* Send over our environment. */
795 if (!current_frame)
796 {
797 extern char **environ;
798 int i;
799 for (i = 0; environ[i]; i++)
800 {
801 char *name = xstrdup (environ[i]);
802 char *value = strchr (name, '=');
803 fprintf (out, "-env ");
804 quote_argument (environ[i], out);
805 fprintf (out, " ");
806 }
807 }
808
809 /* Send over our current directory. */
810 if (!current_frame)
811 {
812 char *dir = get_current_dir_name ();
813 if (dir)
814 {
815 fprintf (out, "-dir ");
816 quote_argument (dir, out);
817 fprintf (out, "/");
818 fprintf (out, " ");
819 free (dir);
820 }
821 }
822
823 retry:
824 if (nowait)
825 fprintf (out, "-nowait ");
826
827 if (current_frame)
828 fprintf (out, "-current-frame ");
829
830 if (display)
831 {
832 fprintf (out, "-display ");
833 quote_argument (display, out);
834 fprintf (out, " ");
835 }
836
837 if (tty)
838 {
839 char *tty_name = ttyname (fileno (stdin));
840 char *type = getenv ("TERM");
841
842 if (! tty_name)
843 {
844 fprintf (stderr, "%s: could not get terminal name\n", progname);
845 fail ();
846 }
847
848 if (! type)
849 {
850 fprintf (stderr, "%s: please set the TERM variable to your terminal type\n",
851 progname);
852 fail ();
853 }
854
855 if (! strcmp (type, "eterm"))
856 {
857 /* This causes nasty, MULTI_KBOARD-related input lockouts. */
858 fprintf (stderr, "%s: opening a frame in an Emacs term buffer"
859 " is not supported\n", progname);
860 fail ();
861 }
862
863 init_signals ();
864
865 fprintf (out, "-tty ");
866 quote_argument (tty_name, out);
867 fprintf (out, " ");
868 quote_argument (type, out);
869 fprintf (out, " ");
870 }
871
872 if (window_system)
873 fprintf (out, "-window-system ");
874
875 if ((argc - optind > 0))
876 {
877 for (i = optind; i < argc; i++)
878 {
879 int relative = 0;
880
881 if (eval)
882 {
883 /* Don't prepend any cwd or anything like that. */
884 fprintf (out, "-eval ");
885 quote_argument (argv[i], out);
886 fprintf (out, " ");
887 continue;
888 }
889
890 if (*argv[i] == '+')
891 {
892 char *p = argv[i] + 1;
893 while (isdigit ((unsigned char) *p) || *p == ':') p++;
894 if (*p == 0)
895 {
896 fprintf (out, "-position ");
897 quote_argument (argv[i], out);
898 fprintf (out, " ");
899 continue;
900 }
901 else
902 relative = 1;
903 }
904 else if (*argv[i] != '/')
905 relative = 1;
906
907 fprintf (out, "-file ");
908 if (relative)
909 {
910 quote_argument (cwd, out);
911 fprintf (out, "/");
912 }
913 quote_argument (argv[i], out);
914 fprintf (out, " ");
915 }
916 }
917 else
918 {
919 if (!tty && !window_system)
920 {
921 while ((str = fgets (string, BUFSIZ, stdin)))
922 {
923 if (eval)
924 fprintf (out, "-eval ");
925 else
926 fprintf (out, "-file ");
927 quote_argument (str, out);
928 }
929 fprintf (out, " ");
930 }
931 }
932
933 fprintf (out, "\n");
934 fflush (out);
935 fsync (fileno (out));
936
937 /* Wait for an answer. */
938 if (!eval && !tty && !nowait)
939 {
940 printf ("Waiting for Emacs...");
941 needlf = 2;
942 }
943 fflush (stdout);
944 fsync (1);
945
946 /* Now, wait for an answer and print any messages. */
947 while ((str = fgets (string, BUFSIZ, in)))
948 {
949 char *p = str + strlen (str) - 1;
950 while (p > str && *p == '\n')
951 *p-- = 0;
952
953 if (strprefix ("-good-version ", str))
954 {
955 /* -good-version: The versions match. */
956 }
957 else if (strprefix ("-emacs-pid ", str))
958 {
959 /* -emacs-pid PID: The process id of the Emacs process. */
960 emacs_pid = strtol (string + strlen ("-emacs-pid"), NULL, 10);
961 }
962 else if (strprefix ("-window-system-unsupported ", str))
963 {
964 /* -window-system-unsupported: Emacs was compiled without X
965 support. Try again on the terminal. */
966 window_system = 0;
967 nowait = 0;
968 tty = 1;
969 goto retry;
970 }
971 else if (strprefix ("-print ", str))
972 {
973 /* -print STRING: Print STRING on the terminal. */
974 str = unquote_argument (str + strlen ("-print "));
975 if (needlf)
976 printf ("\n");
977 printf ("%s", str);
978 needlf = str[0] == '\0' ? needlf : str[strlen (str) - 1] != '\n';
979 }
980 else if (strprefix ("-error ", str))
981 {
982 /* -error DESCRIPTION: Signal an error on the terminal. */
983 str = unquote_argument (str + strlen ("-error "));
984 if (needlf)
985 printf ("\n");
986 printf ("*ERROR*: %s", str);
987 needlf = str[0] == '\0' ? needlf : str[strlen (str) - 1] != '\n';
988 }
989 else if (strprefix ("-suspend ", str))
990 {
991 /* -suspend: Suspend this terminal, i.e., stop the process. */
992 if (needlf)
993 printf ("\n");
994 needlf = 0;
995 kill (0, SIGSTOP);
996 }
997 else
998 {
999 /* Unknown command. */
1000 if (needlf)
1001 printf ("\n");
1002 printf ("*ERROR*: Unknown message: %s", str);
1003 needlf = str[0] == '\0' ? needlf : str[strlen (str) - 1] != '\n';
1004 }
1005 }
1006
1007 if (needlf)
1008 printf ("\n");
1009 fflush (stdout);
1010 fsync (1);
1011
1012 return EXIT_SUCCESS;
1013 }
1014
1015 #endif /* HAVE_SOCKETS */
1016 \f
1017 #ifndef HAVE_STRERROR
1018 char *
1019 strerror (errnum)
1020 int errnum;
1021 {
1022 extern char *sys_errlist[];
1023 extern int sys_nerr;
1024
1025 if (errnum >= 0 && errnum < sys_nerr)
1026 return sys_errlist[errnum];
1027 return (char *) "Unknown error";
1028 }
1029
1030 #endif /* ! HAVE_STRERROR */
1031
1032 /* arch-tag: f39bb9c4-73eb-477e-896d-50832e2ca9a7
1033 (do not change this comment) */
1034
1035 /* emacsclient.c ends here */