]>
code.delx.au - gnu-emacs/blob - lib-src/emacsclient.c
1 /* Client process that communicates with GNU Emacs acting as server.
2 Copyright (C) 1986, 1987, 1994, 1999, 2000, 2001, 2003, 2004
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
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)
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.
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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
48 char *getenv (), *getwd ();
51 /* This is defined with -D from the compilation command,
52 which extracts it from ../lisp/version.el. */
55 #define VERSION "unspecified"
58 /* Name used to invoke this program. */
61 /* The first argument to main. */
64 /* The second argument to main. */
67 /* Nonzero means don't wait for a response from Emacs. --no-wait. */
70 /* Nonzero means args are expressions to be evaluated. --eval. */
73 /* Nonzero means open a new graphical frame. */
74 int window_system
= 0;
76 /* The display on which Emacs should work. --display. */
79 /* Nonzero means open a new Emacs frame on the current terminal. */
82 /* If non-NULL, the name of an editor to fallback to if the server
83 is not running. --alternate-editor. */
84 const char * alternate_editor
= NULL
;
86 /* If non-NULL, the filename of the UNIX socket. */
87 char *socket_name
= NULL
;
89 void print_help_and_exit ();
91 struct option longopts
[] =
93 { "no-wait", no_argument
, NULL
, 'n' },
94 { "eval", no_argument
, NULL
, 'e' },
95 { "help", no_argument
, NULL
, 'H' },
96 { "version", no_argument
, NULL
, 'V' },
97 { "tty", no_argument
, NULL
, 't' },
98 { "current-frame", no_argument
, NULL
, 'c' },
99 { "alternate-editor", required_argument
, NULL
, 'a' },
100 { "socket-name", required_argument
, NULL
, 's' },
101 { "display", required_argument
, NULL
, 'd' },
105 /* Decode the options from argv and argc.
106 The global variable `optind' will say how many arguments we used up. */
109 decode_options (argc
, argv
)
113 alternate_editor
= getenv ("ALTERNATE_EDITOR");
114 display
= getenv ("DISPLAY");
115 if (display
&& strlen (display
) == 0)
125 int opt
= getopt_long (argc
, argv
,
126 "VHnea:s:d:tc", longopts
, 0);
134 /* If getopt returns 0, then it has already processed a
135 long-named option. We should do nothing. */
139 alternate_editor
= optarg
;
143 socket_name
= optarg
;
159 printf ("emacsclient %s\n", VERSION
);
174 print_help_and_exit ();
178 fprintf (stderr
, "Try `%s --help' for more information\n", progname
);
191 print_help_and_exit ()
194 "Usage: %s [OPTIONS] FILE...\n\
195 Tell the Emacs server to visit the specified files.\n\
196 Every FILE can be either just a FILENAME or [+LINE[:COLUMN]] FILENAME.\n\
198 The following OPTIONS are accepted:\n\
199 -V, --version Just print a version info and return\n\
200 -H, --help Print this usage information message\n\
201 -t, --tty Open a new Emacs frame on the current terminal\n\
202 -c, --current-frame Do not create a new frame; use the current Emacs frame\n\
203 -n, --no-wait Don't wait for the server to return\n\
204 -e, --eval Evaluate the FILE arguments as ELisp expressions\n\
205 -d, --display=DISPLAY Visit the file in the given display\n\
206 -s, --socket-name=FILENAME\n\
207 Set the filename of the UNIX socket for communication\n\
208 -a, --alternate-editor=EDITOR\n\
209 Editor to fallback to if the server is not running\n\
211 Report bugs to bug-gnu-emacs@gnu.org.\n", progname
);
215 /* Like malloc but get fatal error if memory is exhausted. */
221 long *result
= (long *) malloc (size
);
230 /* Like strdup but get a fatal error if memory is exhausted. */
233 xstrdup (const char *s
)
235 char *result
= strdup (s
);
244 /* In STR, insert a & before each &, each space, each newline, and
245 any initial -. Change spaces to underscores, too, so that the
246 return value never contains a space.
248 Does not change the string. Outputs the result to STREAM. */
251 quote_argument (str
, stream
)
255 char *copy
= (char *) xmalloc (strlen (str
) * 2 + 1);
276 if (*p
== '&' || (*p
== '-' && p
== str
))
283 fprintf (stream
, "%s", copy
);
289 /* The inverse of quote_argument. Removes quoting in string STR by
290 modifying the string in place. Returns STR. */
293 unquote_argument (str
)
325 Try to run a different command, or --if no alternate editor is
326 defined-- exit with an errorcode.
331 if (alternate_editor
)
334 execvp (alternate_editor
, main_argv
+ i
);
343 /* The process id of Emacs. */
346 /* File handles for communicating with Emacs. */
349 /* A signal handler that passes the signal to the Emacs process.
350 Useful for SIGWINCH. */
353 pass_signal_to_emacs (int signalnum
)
355 int old_errno
= errno
;
358 kill (emacs_pid
, signalnum
);
360 signal (signalnum
, pass_signal_to_emacs
);
364 /* Signal handler for SIGCONT; notify the Emacs process that it can
365 now resume our tty frame. */
368 handle_sigcont (int signalnum
)
370 int old_errno
= errno
;
372 if (tcgetpgrp (1) == getpgrp ())
374 /* We are in the foreground. */
375 fprintf (out
, "-resume \n");
377 fsync (fileno (out
));
381 /* We are in the background; cancel the continue. */
382 kill (getpid (), SIGSTOP
);
387 /* Signal handler for SIGTSTP; notify the Emacs process that we are
388 going to sleep. Normally the suspend is initiated by Emacs via
389 server-handle-suspend-tty, but if the server gets out of sync with
390 reality, we may get a SIGTSTP on C-z. Handling this signal and
391 notifying Emacs about it should get things under control again. */
394 handle_sigtstp (int signalnum
)
396 int old_errno
= errno
;
401 fprintf (out
, "-suspend \n");
403 fsync (fileno (out
));
406 /* Unblock this signal and call the default handler by temprarily
407 changing the handler and resignalling. */
408 sigprocmask (SIG_BLOCK
, NULL
, &set
);
409 sigdelset (&set
, signalnum
);
410 signal (signalnum
, SIG_DFL
);
411 kill (getpid (), signalnum
);
412 sigprocmask (SIG_SETMASK
, &set
, NULL
); /* Let's the above signal through. */
413 signal (signalnum
, handle_sigtstp
);
418 /* Set up signal handlers before opening a frame on the current tty. */
423 /* Set up signal handlers. */
424 signal (SIGWINCH
, pass_signal_to_emacs
);
426 /* Don't pass SIGINT and SIGQUIT to Emacs, because it has no way of
427 deciding which terminal the signal came from. C-g is now a
428 normal input event on secondary terminals. */
430 signal (SIGINT
, pass_signal_to_emacs
);
431 signal (SIGQUIT
, pass_signal_to_emacs
);
434 signal (SIGCONT
, handle_sigcont
);
435 signal (SIGTSTP
, handle_sigtstp
);
436 signal (SIGTTOU
, handle_sigtstp
);
440 #if !defined (HAVE_SOCKETS) || defined (NO_SOCKETS_IN_FILE_SYSTEM)
447 fprintf (stderr
, "%s: Sorry, the Emacs server is supported only\n",
449 fprintf (stderr
, "on systems with Berkeley sockets.\n");
454 #else /* HAVE_SOCKETS */
456 #include <sys/types.h>
457 #include <sys/socket.h>
459 #include <sys/stat.h>
462 extern char *strerror ();
465 /* Three possibilities:
466 2 - can't be `stat'ed (sets errno)
467 1 - isn't owned by us
468 0 - success: none of the above */
471 socket_status (socket_name
)
476 if (stat (socket_name
, &statbfr
) == -1)
479 if (statbfr
.st_uid
!= geteuid ())
485 /* Returns 1 if PREFIX is a prefix of STRING. */
487 strprefix (char *prefix
, char *string
)
496 for (i
= 0; prefix
[i
]; i
++)
497 if (!string
[i
] || string
[i
] != prefix
[i
])
507 int s
, i
, needlf
= 0;
508 struct sockaddr_un server
;
516 /* Process options. */
517 decode_options (argc
, argv
);
519 if ((argc
- optind
< 1) && !eval
&& !tty
&& !window_system
)
521 fprintf (stderr
, "%s: file name or argument required\n", progname
);
522 fprintf (stderr
, "Try `%s --help' for more information\n", progname
);
527 * Open up an AF_UNIX socket in this person's home directory
530 if ((s
= socket (AF_UNIX
, SOCK_STREAM
, 0)) < 0)
532 fprintf (stderr
, "%s: ", argv
[0]);
537 server
.sun_family
= AF_UNIX
;
541 int default_sock
= !socket_name
;
544 char *server_name
= "server";
546 if (socket_name
&& !index (socket_name
, '/') && !index (socket_name
, '\\'))
547 { /* socket_name is a file name component. */
548 server_name
= socket_name
;
550 default_sock
= 1; /* Try both UIDs. */
555 socket_name
= alloca (100 + strlen (server_name
));
556 sprintf (socket_name
, "/tmp/emacs%d/%s",
557 (int) geteuid (), server_name
);
560 if (strlen (socket_name
) < sizeof (server
.sun_path
))
561 strcpy (server
.sun_path
, socket_name
);
564 fprintf (stderr
, "%s: socket-name %s too long",
565 argv
[0], socket_name
);
569 /* See if the socket exists, and if it's owned by us. */
570 sock_status
= socket_status (server
.sun_path
);
572 if (sock_status
&& default_sock
)
574 /* Failing that, see if LOGNAME or USER exist and differ from
575 our euid. If so, look for a socket based on the UID
576 associated with the name. This is reminiscent of the logic
577 that init_editfns uses to set the global Vuser_full_name. */
579 char *user_name
= (char *) getenv ("LOGNAME");
582 user_name
= (char *) getenv ("USER");
586 struct passwd
*pw
= getpwnam (user_name
);
588 if (pw
&& (pw
->pw_uid
!= geteuid ()))
590 /* We're running under su, apparently. */
591 socket_name
= alloca (100 + strlen (server_name
));
592 sprintf (socket_name
, "/tmp/emacs%d/%s",
593 (int) pw
->pw_uid
, server_name
);
595 if (strlen (socket_name
) < sizeof (server
.sun_path
))
596 strcpy (server
.sun_path
, socket_name
);
599 fprintf (stderr
, "%s: socket-name %s too long",
600 argv
[0], socket_name
);
604 sock_status
= socket_status (server
.sun_path
);
615 /* There's a socket, but it isn't owned by us. This is OK if
619 fprintf (stderr
, "%s: Invalid socket owner\n", argv
[0]);
626 if (saved_errno
== ENOENT
)
628 "%s: can't find socket; have you started the server?\n\
629 To start the server in Emacs, type \"M-x server-start\".\n",
632 fprintf (stderr
, "%s: can't stat %s: %s\n",
633 argv
[0], server
.sun_path
, strerror (saved_errno
));
639 if (connect (s
, (struct sockaddr
*) &server
, strlen (server
.sun_path
) + 2)
642 fprintf (stderr
, "%s: ", argv
[0]);
647 /* We use the stream OUT to send our commands to the server. */
648 if ((out
= fdopen (s
, "r+")) == NULL
)
650 fprintf (stderr
, "%s: ", argv
[0]);
655 /* We use the stream IN to read the responses.
656 We used to use just one stream for both output and input
657 on the socket, but reversing direction works nonportably:
658 on some systems, the output appears as the first input;
659 on other systems it does not. */
660 if ((in
= fdopen (s
, "r+")) == NULL
)
662 fprintf (stderr
, "%s: ", argv
[0]);
668 cwd
= getcwd (string
, sizeof string
);
670 cwd
= getwd (string
);
674 /* getwd puts message in STRING if it fails. */
677 fprintf (stderr
, "%s: %s (%s)\n", argv
[0],
678 "cannot get current working directory", strerror (errno
));
680 fprintf (stderr
, "%s: %s (%s)\n", argv
[0], string
, strerror (errno
));
685 /* First of all, send our version number for verification. */
686 fprintf (out
, "-version %s ", VERSION
);
688 /* Send over our environment. */
690 extern char **environ
;
692 for (i
= 0; environ
[i
]; i
++)
694 char *name
= xstrdup (environ
[i
]);
695 char *value
= strchr (name
, '=');
696 if (value
&& strlen (value
) > 1)
699 fprintf (out
, "-env ");
700 quote_argument (name
, out
);
702 quote_argument (value
, out
);
711 fprintf (out
, "-nowait ");
715 fprintf (out
, "-display ");
716 quote_argument (display
, out
);
722 char *tty_name
= ttyname (fileno (stdin
));
723 char *type
= getenv ("TERM");
727 fprintf (stderr
, "%s: could not get terminal name\n", progname
);
733 fprintf (stderr
, "%s: please set the TERM variable to your terminal type\n",
738 if (! strcmp (type
, "eterm"))
740 /* This causes nasty, MULTI_KBOARD-related input lockouts. */
741 fprintf (stderr
, "%s: opening a frame in an Emacs term buffer"
742 " is not supported\n", progname
);
748 fprintf (out
, "-tty ");
749 quote_argument (tty_name
, out
);
751 quote_argument (type
, out
);
756 fprintf (out
, "-window-system ");
758 if ((argc
- optind
> 0))
760 for (i
= optind
; i
< argc
; i
++)
766 /* Don't prepend any cwd or anything like that. */
767 fprintf (out
, "-eval ");
768 quote_argument (argv
[i
], out
);
775 char *p
= argv
[i
] + 1;
776 while (isdigit ((unsigned char) *p
) || *p
== ':') p
++;
779 fprintf (out
, "-position ");
780 quote_argument (argv
[i
], out
);
787 else if (*argv
[i
] != '/')
790 fprintf (out
, "-file ");
793 quote_argument (cwd
, out
);
796 quote_argument (argv
[i
], out
);
802 if (!tty
&& !window_system
)
804 while ((str
= fgets (string
, BUFSIZ
, stdin
)))
807 fprintf (out
, "-eval ");
809 fprintf (out
, "-file ");
810 quote_argument (str
, out
);
818 fsync (fileno (out
));
820 /* Maybe wait for an answer. */
828 printf ("Waiting for Emacs...");
834 /* Now, wait for an answer and print any messages. */
835 while ((str
= fgets (string
, BUFSIZ
, in
)))
837 char *p
= str
+ strlen (str
) - 1;
838 while (p
> str
&& *p
== '\n')
841 if (strprefix ("-good-version ", str
))
843 /* OK, we got the green light. */
845 else if (strprefix ("-emacs-pid ", str
))
847 emacs_pid
= strtol (string
+ strlen ("-emacs-pid"), NULL
, 10);
849 else if (strprefix ("-print ", str
))
851 str
= unquote_argument (str
+ strlen ("-print "));
855 needlf
= str
[0] == '\0' ? needlf
: str
[strlen (str
) - 1] != '\n';
857 else if (strprefix ("-error ", str
))
859 str
= unquote_argument (str
+ strlen ("-error "));
862 printf ("*ERROR*: %s", str
);
863 needlf
= str
[0] == '\0' ? needlf
: str
[strlen (str
) - 1] != '\n';
865 else if (strprefix ("-suspend ", str
))
876 printf ("*ERROR*: Unknown message: %s", str
);
877 needlf
= str
[0] == '\0' ? needlf
: str
[strlen (str
) - 1] != '\n';
889 #endif /* HAVE_SOCKETS */
891 #ifndef HAVE_STRERROR
896 extern char *sys_errlist
[];
899 if (errnum
>= 0 && errnum
< sys_nerr
)
900 return sys_errlist
[errnum
];
901 return (char *) "Unknown error";
904 #endif /* ! HAVE_STRERROR */
906 /* arch-tag: f39bb9c4-73eb-477e-896d-50832e2ca9a7
907 (do not change this comment) */