]>
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 /* In STR, insert a & before each &, each space, each newline, and
216 any initial -. Change spaces to underscores, too, so that the
217 return value never contains a space.
219 Does not change the string. Outputs the result to STREAM. */
222 quote_argument (str
, stream
)
226 char *copy
= (char *) malloc (strlen (str
) * 2 + 1);
247 if (*p
== '&' || (*p
== '-' && p
== str
))
254 fprintf (stream
, copy
);
260 /* The inverse of quote_argument. Removes quoting in string STR by
261 modifying the string in place. Returns STR. */
264 unquote_argument (str
)
294 /* Like malloc but get fatal error if memory is exhausted. */
300 long *result
= (long *) malloc (size
);
310 Try to run a different command, or --if no alternate editor is
311 defined-- exit with an errorcode.
316 if (alternate_editor
)
319 execvp (alternate_editor
, main_argv
+ i
);
328 /* The process id of Emacs. */
331 /* File handles for communicating with Emacs. */
334 /* A signal handler that passes the signal to the Emacs process.
335 Useful for SIGWINCH. */
338 pass_signal_to_emacs (int signalnum
)
340 int old_errno
= errno
;
343 kill (emacs_pid
, signalnum
);
345 signal (signalnum
, pass_signal_to_emacs
);
349 /* Signal handler for SIGCONT; notify the Emacs process that it can
350 now resume our tty frame. */
353 handle_sigcont (int signalnum
)
355 int old_errno
= errno
;
357 if (tcgetpgrp (1) == getpgrp ())
359 /* We are in the foreground. */
360 fprintf (out
, "-resume \n");
362 fsync (fileno (out
));
366 /* We are in the background; cancel the continue. */
367 kill (getpid (), SIGSTOP
);
372 /* Signal handler for SIGTSTP; notify the Emacs process that we are
373 going to sleep. Normally the suspend is initiated by Emacs via
374 server-handle-suspend-tty, but if the server gets out of sync with
375 reality, we may get a SIGTSTP on C-z. Handling this signal and
376 notifying Emacs about it should get things under control again. */
379 handle_sigtstp (int signalnum
)
381 int old_errno
= errno
;
386 fprintf (out
, "-suspend \n");
388 fsync (fileno (out
));
391 /* Unblock this signal and call the default handler by temprarily
392 changing the handler and resignalling. */
393 sigprocmask (SIG_BLOCK
, NULL
, &set
);
394 sigdelset (&set
, signalnum
);
395 signal (signalnum
, SIG_DFL
);
396 kill (getpid (), signalnum
);
397 sigprocmask (SIG_SETMASK
, &set
, NULL
); /* Let's the above signal through. */
398 signal (signalnum
, handle_sigtstp
);
403 /* Set up signal handlers before opening a frame on the current tty. */
408 /* Set up signal handlers. */
409 signal (SIGWINCH
, pass_signal_to_emacs
);
411 /* Don't pass SIGINT and SIGQUIT to Emacs, because it has no way of
412 deciding which terminal the signal came from. C-g is now a
413 normal input event on secondary terminals. */
415 signal (SIGINT
, pass_signal_to_emacs
);
416 signal (SIGQUIT
, pass_signal_to_emacs
);
419 signal (SIGCONT
, handle_sigcont
);
420 signal (SIGTSTP
, handle_sigtstp
);
421 signal (SIGTTOU
, handle_sigtstp
);
425 #if !defined (HAVE_SOCKETS) || defined (NO_SOCKETS_IN_FILE_SYSTEM)
432 fprintf (stderr
, "%s: Sorry, the Emacs server is supported only\n",
434 fprintf (stderr
, "on systems with Berkeley sockets.\n");
439 #else /* HAVE_SOCKETS */
441 #include <sys/types.h>
442 #include <sys/socket.h>
444 #include <sys/stat.h>
447 extern char *strerror ();
450 /* Three possibilities:
451 2 - can't be `stat'ed (sets errno)
452 1 - isn't owned by us
453 0 - success: none of the above */
456 socket_status (socket_name
)
461 if (stat (socket_name
, &statbfr
) == -1)
464 if (statbfr
.st_uid
!= geteuid ())
470 /* Returns 1 if PREFIX is a prefix of STRING. */
472 strprefix (char *prefix
, char *string
)
481 for (i
= 0; prefix
[i
]; i
++)
482 if (!string
[i
] || string
[i
] != prefix
[i
])
492 int s
, i
, needlf
= 0;
493 struct sockaddr_un server
;
501 /* Process options. */
502 decode_options (argc
, argv
);
504 if ((argc
- optind
< 1) && !eval
&& !tty
&& !window_system
)
506 fprintf (stderr
, "%s: file name or argument required\n", progname
);
507 fprintf (stderr
, "Try `%s --help' for more information\n", progname
);
512 * Open up an AF_UNIX socket in this person's home directory
515 if ((s
= socket (AF_UNIX
, SOCK_STREAM
, 0)) < 0)
517 fprintf (stderr
, "%s: ", argv
[0]);
522 server
.sun_family
= AF_UNIX
;
526 int default_sock
= !socket_name
;
529 char *server_name
= "server";
531 if (socket_name
&& !index (socket_name
, '/') && !index (socket_name
, '\\'))
532 { /* socket_name is a file name component. */
533 server_name
= socket_name
;
535 default_sock
= 1; /* Try both UIDs. */
540 socket_name
= alloca (100 + strlen (server_name
));
541 sprintf (socket_name
, "/tmp/emacs%d/%s",
542 (int) geteuid (), server_name
);
545 if (strlen (socket_name
) < sizeof (server
.sun_path
))
546 strcpy (server
.sun_path
, socket_name
);
549 fprintf (stderr
, "%s: socket-name %s too long",
550 argv
[0], socket_name
);
554 /* See if the socket exists, and if it's owned by us. */
555 sock_status
= socket_status (server
.sun_path
);
557 if (sock_status
&& default_sock
)
559 /* Failing that, see if LOGNAME or USER exist and differ from
560 our euid. If so, look for a socket based on the UID
561 associated with the name. This is reminiscent of the logic
562 that init_editfns uses to set the global Vuser_full_name. */
564 char *user_name
= (char *) getenv ("LOGNAME");
567 user_name
= (char *) getenv ("USER");
571 struct passwd
*pw
= getpwnam (user_name
);
573 if (pw
&& (pw
->pw_uid
!= geteuid ()))
575 /* We're running under su, apparently. */
576 socket_name
= alloca (100 + strlen (server_name
));
577 sprintf (socket_name
, "/tmp/emacs%d/%s",
578 (int) pw
->pw_uid
, server_name
);
580 if (strlen (socket_name
) < sizeof (server
.sun_path
))
581 strcpy (server
.sun_path
, socket_name
);
584 fprintf (stderr
, "%s: socket-name %s too long",
585 argv
[0], socket_name
);
589 sock_status
= socket_status (server
.sun_path
);
600 /* There's a socket, but it isn't owned by us. This is OK if
604 fprintf (stderr
, "%s: Invalid socket owner\n", argv
[0]);
611 if (saved_errno
== ENOENT
)
613 "%s: Can't find socket; have you started the server?\n\
614 To start the server in Emacs, type \"M-x server-start\".\n",
617 fprintf (stderr
, "%s: Can't stat %s: %s\n",
618 argv
[0], server
.sun_path
, strerror (saved_errno
));
624 if (connect (s
, (struct sockaddr
*) &server
, strlen (server
.sun_path
) + 2)
627 fprintf (stderr
, "%s: ", argv
[0]);
632 /* We use the stream OUT to send our command to the server. */
633 if ((out
= fdopen (s
, "r+")) == NULL
)
635 fprintf (stderr
, "%s: ", argv
[0]);
640 /* We use the stream IN to read the response.
641 We used to use just one stream for both output and input
642 on the socket, but reversing direction works nonportably:
643 on some systems, the output appears as the first input;
644 on other systems it does not. */
645 if ((in
= fdopen (s
, "r+")) == NULL
)
647 fprintf (stderr
, "%s: ", argv
[0]);
653 cwd
= getcwd (string
, sizeof string
);
655 cwd
= getwd (string
);
659 /* getwd puts message in STRING if it fails. */
662 fprintf (stderr
, "%s: %s (%s)\n", argv
[0],
663 "Cannot get current working directory", strerror (errno
));
665 fprintf (stderr
, "%s: %s (%s)\n", argv
[0], string
, strerror (errno
));
670 /* First of all, send our version number for verification. */
671 fprintf (out
, "-version %s ", VERSION
);
674 fprintf (out
, "-nowait ");
678 fprintf (out
, "-display ");
679 quote_argument (display
, out
);
685 char *tty_name
= ttyname (fileno (stdin
));
686 char *type
= getenv ("TERM");
690 fprintf (stderr
, "%s: could not get terminal name\n", progname
);
696 fprintf (stderr
, "%s: please set the TERM variable to your terminal type\n",
701 if (! strcmp (type
, "eterm"))
703 /* This causes nasty, MULTI_KBOARD-related input lockouts. */
704 fprintf (stderr
, "%s: opening a frame in an Emacs term buffer"
705 " is not supported\n", progname
);
711 fprintf (out
, "-tty ");
712 quote_argument (tty_name
, out
);
714 quote_argument (type
, out
);
719 fprintf (out
, "-window-system ");
721 if ((argc
- optind
> 0))
723 for (i
= optind
; i
< argc
; i
++)
729 /* Don't prepend any cwd or anything like that. */
730 fprintf (out
, "-eval ");
731 quote_argument (argv
[i
], out
);
738 char *p
= argv
[i
] + 1;
739 while (isdigit ((unsigned char) *p
) || *p
== ':') p
++;
742 fprintf (out
, "-position ");
743 quote_argument (argv
[i
], out
);
750 else if (*argv
[i
] != '/')
753 fprintf (out
, "-file ");
756 quote_argument (cwd
, out
);
759 quote_argument (argv
[i
], out
);
765 if (!tty
&& !window_system
)
767 while ((str
= fgets (string
, BUFSIZ
, stdin
)))
770 fprintf (out
, "-eval ");
772 fprintf (out
, "-file ");
773 quote_argument (str
, out
);
781 fsync (fileno (out
));
783 /* Maybe wait for an answer. */
791 printf ("Waiting for Emacs...");
797 /* Now, wait for an answer and print any messages. */
798 while ((str
= fgets (string
, BUFSIZ
, in
)))
800 char *p
= str
+ strlen (str
) - 1;
801 while (p
> str
&& *p
== '\n')
804 if (strprefix ("-good-version ", str
))
806 /* OK, we got the green light. */
808 else if (strprefix ("-emacs-pid ", str
))
810 emacs_pid
= strtol (string
+ strlen ("-emacs-pid"), NULL
, 10);
812 else if (strprefix ("-print ", str
))
814 str
= unquote_argument (str
+ strlen ("-print "));
818 needlf
= str
[0] == '\0' ? needlf
: str
[strlen (str
) - 1] != '\n';
820 else if (strprefix ("-error ", str
))
822 str
= unquote_argument (str
+ strlen ("-error "));
825 printf ("*ERROR*: %s", str
);
826 needlf
= str
[0] == '\0' ? needlf
: str
[strlen (str
) - 1] != '\n';
828 else if (strprefix ("-suspend ", str
))
839 printf ("*ERROR*: Unknown message: %s", str
);
840 needlf
= str
[0] == '\0' ? needlf
: str
[strlen (str
) - 1] != '\n';
852 #endif /* HAVE_SOCKETS */
854 #ifndef HAVE_STRERROR
859 extern char *sys_errlist
[];
862 if (errnum
>= 0 && errnum
< sys_nerr
)
863 return sys_errlist
[errnum
];
864 return (char *) "Unknown error";
867 #endif /* ! HAVE_STRERROR */
869 /* arch-tag: f39bb9c4-73eb-477e-896d-50832e2ca9a7
870 (do not change this comment) */