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 { "window-system", no_argument
, NULL
, 'w' },
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");
118 int opt
= getopt_long (argc
, argv
,
119 "VHnea:s:d:tw", longopts
, 0);
127 /* If getopt returns 0, then it has already processed a
128 long-named option. We should do nothing. */
132 alternate_editor
= optarg
;
136 socket_name
= optarg
;
152 printf ("emacsclient %s\n", VERSION
);
167 print_help_and_exit ();
171 fprintf (stderr
, "Try `%s --help' for more information\n", progname
);
184 print_help_and_exit ()
187 "Usage: %s [OPTIONS] FILE...\n\
188 Tell the Emacs server to visit the specified files.\n\
189 Every FILE can be either just a FILENAME or [+LINE[:COLUMN]] FILENAME.\n\
191 The following OPTIONS are accepted:\n\
192 -V, --version Just print a version info and return\n\
193 -H, --help Print this usage information message\n\
194 -t, --tty Open a new Emacs frame on the current terminal\n\
195 -w, --window-system Open a new graphical Emacs frame\n\
196 -n, --no-wait Don't wait for the server to return\n\
197 -e, --eval Evaluate the FILE arguments as ELisp expressions\n\
198 -d, --display=DISPLAY Visit the file in the given display\n\
199 -s, --socket-name=FILENAME\n\
200 Set the filename of the UNIX socket for communication\n\
201 -a, --alternate-editor=EDITOR\n\
202 Editor to fallback to if the server is not running\n\
204 Report bugs to bug-gnu-emacs@gnu.org.\n", progname
);
208 /* In NAME, insert a & before each &, each space, each newline, and
209 any initial -. Change spaces to underscores, too, so that the
210 return value never contains a space. */
213 quote_file_name (name
, stream
)
217 char *copy
= (char *) malloc (strlen (name
) * 2 + 1);
238 if (*p
== '&' || (*p
== '-' && p
== name
))
245 fprintf (stream
, copy
);
250 /* Like malloc but get fatal error if memory is exhausted. */
256 long *result
= (long *) malloc (size
);
266 Try to run a different command, or --if no alternate editor is
267 defined-- exit with an errorcode.
272 if (alternate_editor
)
275 execvp (alternate_editor
, main_argv
+ i
);
286 /* A signal handler that passes the signal to the Emacs process.
287 Useful for SIGWINCH. */
290 pass_signal_to_emacs (int signalnum
)
292 int old_errno
= errno
;
295 kill (emacs_pid
, signalnum
);
297 signal (signalnum
, pass_signal_to_emacs
);
301 /* Set up signal handlers before opening a frame on the current tty. */
306 /* Set up signal handlers. */
307 signal (SIGWINCH
, pass_signal_to_emacs
);
309 /* Don't pass SIGINT and SIGQUIT to Emacs, because it has no way of
310 deciding which terminal the signal came from. C-g is now a
311 normal input event on secondary terminals. */
313 signal (SIGINT
, pass_signal_to_emacs
);
314 signal (SIGQUIT
, pass_signal_to_emacs
);
319 #if !defined (HAVE_SOCKETS) || defined (NO_SOCKETS_IN_FILE_SYSTEM)
326 fprintf (stderr
, "%s: Sorry, the Emacs server is supported only\n",
328 fprintf (stderr
, "on systems with Berkeley sockets.\n");
333 #else /* HAVE_SOCKETS */
335 #include <sys/types.h>
336 #include <sys/socket.h>
338 #include <sys/stat.h>
341 extern char *strerror ();
344 /* Three possibilities:
345 2 - can't be `stat'ed (sets errno)
346 1 - isn't owned by us
347 0 - success: none of the above */
350 socket_status (socket_name
)
355 if (stat (socket_name
, &statbfr
) == -1)
358 if (statbfr
.st_uid
!= geteuid ())
364 /* Returns 1 if PREFIX is a prefix of STRING. */
366 strprefix (char *prefix
, char *string
)
375 for (i
= 0; prefix
[i
]; i
++)
376 if (!string
[i
] || string
[i
] != prefix
[i
])
386 int s
, i
, needlf
= 0;
388 struct sockaddr_un server
;
396 /* Process options. */
397 decode_options (argc
, argv
);
399 if ((argc
- optind
< 1) && !eval
&& !tty
&& !window_system
)
401 fprintf (stderr
, "%s: file name or argument required\n", progname
);
402 fprintf (stderr
, "Try `%s --help' for more information\n", progname
);
407 * Open up an AF_UNIX socket in this person's home directory
410 if ((s
= socket (AF_UNIX
, SOCK_STREAM
, 0)) < 0)
412 fprintf (stderr
, "%s: ", argv
[0]);
417 server
.sun_family
= AF_UNIX
;
421 int default_sock
= !socket_name
;
424 char *server_name
= "server";
426 if (socket_name
&& !index (socket_name
, '/') && !index (socket_name
, '\\'))
427 { /* socket_name is a file name component. */
428 server_name
= socket_name
;
430 default_sock
= 1; /* Try both UIDs. */
435 socket_name
= alloca (100 + strlen (server_name
));
436 sprintf (socket_name
, "/tmp/emacs%d/%s",
437 (int) geteuid (), server_name
);
440 if (strlen (socket_name
) < sizeof (server
.sun_path
))
441 strcpy (server
.sun_path
, socket_name
);
444 fprintf (stderr
, "%s: socket-name %s too long",
445 argv
[0], socket_name
);
449 /* See if the socket exists, and if it's owned by us. */
450 sock_status
= socket_status (server
.sun_path
);
452 if (sock_status
&& default_sock
)
454 /* Failing that, see if LOGNAME or USER exist and differ from
455 our euid. If so, look for a socket based on the UID
456 associated with the name. This is reminiscent of the logic
457 that init_editfns uses to set the global Vuser_full_name. */
459 char *user_name
= (char *) getenv ("LOGNAME");
462 user_name
= (char *) getenv ("USER");
466 struct passwd
*pw
= getpwnam (user_name
);
468 if (pw
&& (pw
->pw_uid
!= geteuid ()))
470 /* We're running under su, apparently. */
471 socket_name
= alloca (100 + strlen (server_name
));
472 sprintf (socket_name
, "/tmp/emacs%d/%s",
473 (int) pw
->pw_uid
, server_name
);
475 if (strlen (socket_name
) < sizeof (server
.sun_path
))
476 strcpy (server
.sun_path
, socket_name
);
479 fprintf (stderr
, "%s: socket-name %s too long",
480 argv
[0], socket_name
);
484 sock_status
= socket_status (server
.sun_path
);
495 /* There's a socket, but it isn't owned by us. This is OK if
499 fprintf (stderr
, "%s: Invalid socket owner\n", argv
[0]);
506 if (saved_errno
== ENOENT
)
508 "%s: Can't find socket; have you started the server?\n\
509 To start the server in Emacs, type \"M-x server-start\".\n",
512 fprintf (stderr
, "%s: Can't stat %s: %s\n",
513 argv
[0], server
.sun_path
, strerror (saved_errno
));
519 if (connect (s
, (struct sockaddr
*) &server
, strlen (server
.sun_path
) + 2)
522 fprintf (stderr
, "%s: ", argv
[0]);
527 /* We use the stream OUT to send our command to the server. */
528 if ((out
= fdopen (s
, "r+")) == NULL
)
530 fprintf (stderr
, "%s: ", argv
[0]);
535 /* We use the stream IN to read the response.
536 We used to use just one stream for both output and input
537 on the socket, but reversing direction works nonportably:
538 on some systems, the output appears as the first input;
539 on other systems it does not. */
540 if ((in
= fdopen (s
, "r+")) == NULL
)
542 fprintf (stderr
, "%s: ", argv
[0]);
548 cwd
= getcwd (string
, sizeof string
);
550 cwd
= getwd (string
);
554 /* getwd puts message in STRING if it fails. */
557 fprintf (stderr
, "%s: %s (%s)\n", argv
[0],
558 "Cannot get current working directory", strerror (errno
));
560 fprintf (stderr
, "%s: %s (%s)\n", argv
[0], string
, strerror (errno
));
566 fprintf (out
, "-nowait ");
569 fprintf (out
, "-eval ");
573 fprintf (out
, "-display ");
574 quote_file_name (display
, out
);
580 char *tty_name
= ttyname (fileno (stdin
));
586 fprintf (out
, "-tty ");
587 quote_file_name (tty_name
, out
);
589 quote_file_name (getenv("TERM"), out
);
594 fprintf (out
, "-window-system ");
596 if ((argc
- optind
> 0))
598 for (i
= optind
; i
< argc
; i
++)
601 ; /* Don't prepend any cwd or anything like that. */
602 else if (*argv
[i
] == '+')
604 char *p
= argv
[i
] + 1;
605 while (isdigit ((unsigned char) *p
) || *p
== ':') p
++;
608 quote_file_name (cwd
, out
);
612 else if (*argv
[i
] != '/')
614 quote_file_name (cwd
, out
);
618 quote_file_name (argv
[i
], out
);
624 if (!tty
&& !window_system
)
626 while ((str
= fgets (string
, BUFSIZ
, stdin
)))
628 quote_file_name (str
, out
);
637 /* Maybe wait for an answer. */
645 printf ("Waiting for Emacs...");
650 /* Now, wait for an answer and print any messages. */
651 while ((str
= fgets (string
, BUFSIZ
, in
)))
653 if (strprefix ("-emacs-pid ", str
))
655 emacs_pid
= strtol (string
+ strlen ("-emacs-pid"), NULL
, 10);
657 else if (strprefix ("-print ", str
))
661 printf ("%s", str
+ strlen ("-print "));
662 needlf
= str
[0] == '\0' ? needlf
: str
[strlen (str
) - 1] != '\n';
664 else if (strprefix ("-error ", str
))
668 printf ("*ERROR*: %s", str
+ strlen ("-print "));
669 needlf
= str
[0] == '\0' ? needlf
: str
[strlen (str
) - 1] != '\n';
675 printf ("*ERROR*: Unknown message: %s", str
);
676 needlf
= str
[0] == '\0' ? needlf
: str
[strlen (str
) - 1] != '\n';
687 #endif /* HAVE_SOCKETS */
689 #ifndef HAVE_STRERROR
694 extern char *sys_errlist
[];
697 if (errnum
>= 0 && errnum
< sys_nerr
)
698 return sys_errlist
[errnum
];
699 return (char *) "Unknown error";
702 #endif /* ! HAVE_STRERROR */
704 /* arch-tag: f39bb9c4-73eb-477e-896d-50832e2ca9a7
705 (do not change this comment) */