*** empty log message ***
[bpt/emacs.git] / lib-src / emacsclient.c
CommitLineData
efb859b4 1/* Client process that communicates with GNU Emacs acting as server.
de073ce3
GM
2 Copyright (C) 1986, 1987, 1994, 1999, 2000, 2001
3 Free Software Foundation, Inc.
46cec291
RS
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
92af894f 9the Free Software Foundation; either version 2, or (at your option)
46cec291
RS
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
46cec291
RS
21
22
23#define NO_SHORTNAMES
e69233c2
PJ
24
25#ifdef HAVE_CONFIG_H
2f8fe2f4 26#include <config.h>
e69233c2
PJ
27#endif
28
4e23f2ba 29#undef signal
46cec291 30
e69233c2 31#include <ctype.h>
8f9aaa0a
RS
32#include <stdio.h>
33#include <getopt.h>
79f13bba
DL
34#ifdef HAVE_UNISTD_H
35#include <unistd.h>
36#endif
8f9aaa0a 37
9f637eea
GM
38#ifdef VMS
39# include "vms-pwd.h"
40#else
41# include <pwd.h>
42#endif /* not VMS */
43
8f9aaa0a
RS
44char *getenv (), *getwd ();
45char *getcwd ();
8f9aaa0a
RS
46
47/* This is defined with -D from the compilation command,
48 which extracts it from ../lisp/version.el. */
49
50#ifndef VERSION
51#define VERSION "unspecified"
52#endif
53\f
54/* Name used to invoke this program. */
55char *progname;
56
749ae770 57/* Nonzero means don't wait for a response from Emacs. --no-wait. */
8f9aaa0a
RS
58int nowait = 0;
59
30be2360
SM
60/* Nonzero means args are expressions to be evaluated. --eval. */
61int eval = 0;
62
63/* The display on which Emacs should work. --display. */
64char *display = NULL;
65
66/* If non-NULL, the name of an editor to fallback to if the server
67 is not running. --alternate-editor. */
68const char * alternate_editor = NULL;
69
7f3bff3e
AS
70void print_help_and_exit ();
71
8f9aaa0a
RS
72struct option longopts[] =
73{
749ae770 74 { "no-wait", no_argument, NULL, 'n' },
30be2360 75 { "eval", no_argument, NULL, 'e' },
8f9aaa0a
RS
76 { "help", no_argument, NULL, 'H' },
77 { "version", no_argument, NULL, 'V' },
3cf8c6aa 78 { "alternate-editor", required_argument, NULL, 'a' },
30be2360
SM
79 { "display", required_argument, NULL, 'd' },
80 { 0, 0, 0, 0 }
8f9aaa0a
RS
81};
82
83/* Decode the options from argv and argc.
5212210c 84 The global variable `optind' will say how many arguments we used up. */
8f9aaa0a 85
5212210c 86void
8f9aaa0a
RS
87decode_options (argc, argv)
88 int argc;
89 char **argv;
90{
91 while (1)
92 {
93 int opt = getopt_long (argc, argv,
30be2360 94 "VHnea:d:", longopts, 0);
8f9aaa0a
RS
95
96 if (opt == EOF)
97 break;
98
97e3214d 99 alternate_editor = getenv ("ALTERNATE_EDITOR");
e69233c2 100
8f9aaa0a
RS
101 switch (opt)
102 {
103 case 0:
104 /* If getopt returns 0, then it has already processed a
105 long-named option. We should do nothing. */
106 break;
e69233c2 107
97e3214d
GM
108 case 'a':
109 alternate_editor = optarg;
110 break;
e69233c2 111
30be2360
SM
112 case 'd':
113 display = optarg;
114 break;
115
8f9aaa0a
RS
116 case 'n':
117 nowait = 1;
118 break;
119
30be2360
SM
120 case 'e':
121 eval = 1;
122 break;
123
8f9aaa0a 124 case 'V':
b4e94f42 125 fprintf (stderr, "emacsclient %s\n", VERSION);
8f9aaa0a
RS
126 exit (1);
127 break;
46cec291 128
8f9aaa0a
RS
129 case 'H':
130 default:
131 print_help_and_exit ();
132 }
133 }
8f9aaa0a
RS
134}
135
7f3bff3e 136void
8f9aaa0a
RS
137print_help_and_exit ()
138{
139 fprintf (stderr,
30be2360
SM
140 "Usage: %s [OPTIONS] FILE...\n\
141Tell the Emacs server to visit the specified files.\n\
142Every FILE can be either just a FILENAME or [+LINE[:COLUMN]] FILENAME.\n\
143The following OPTIONS are accepted:\n\
144-V, --version Just print a version info and return\n\
145-H, --help Print this usage information message\n\
146-n, --no-wait Don't wait for the server to return\n\
147-e, --eval Evaluate the FILE arguments as ELisp expressions\n\
148-d, --display=DISPLAY Visit the file in the given display\n\
149-a, --alternate-editor=EDITOR\n\
150 Editor to fallback to if the server is not running\n\
151Report bugs to bug-gnu-emacs@gnu.org.\n", progname);
8f9aaa0a
RS
152 exit (1);
153}
5212210c 154
f1db6a73 155/* Return a copy of NAME, inserting a &
3cf8c6aa 156 before each &, each space, each newline, and any initial -.
5212210c
RS
157 Change spaces to underscores, too, so that the
158 return value never contains a space. */
159
160char *
161quote_file_name (name)
162 char *name;
163{
164 char *copy = (char *) malloc (strlen (name) * 2 + 1);
165 char *p, *q;
166
167 p = name;
168 q = copy;
169 while (*p)
170 {
171 if (*p == ' ')
172 {
f1db6a73 173 *q++ = '&';
5212210c
RS
174 *q++ = '_';
175 p++;
176 }
3cf8c6aa
SM
177 else if (*p == '\n')
178 {
179 *q++ = '&';
180 *q++ = 'n';
181 p++;
182 }
5212210c
RS
183 else
184 {
f1db6a73
RS
185 if (*p == '&' || (*p == '-' && p == name))
186 *q++ = '&';
5212210c
RS
187 *q++ = *p++;
188 }
189 }
f1db6a73 190 *q++ = 0;
5212210c
RS
191
192 return copy;
193}
0c76956f 194
0c76956f
RS
195/* Like malloc but get fatal error if memory is exhausted. */
196
5497dfdb 197long *
0c76956f
RS
198xmalloc (size)
199 unsigned int size;
200{
5f32a712 201 long *result = (long *) malloc (size);
0c76956f
RS
202 if (result == NULL)
203 {
204 perror ("malloc");
205 exit (1);
206 }
207 return result;
208}
8f9aaa0a 209\f
97e3214d
GM
210/*
211 Try to run a different command, or --if no alternate editor is
212 defined-- exit with an errorcode.
213*/
de073ce3 214void
97e3214d
GM
215fail (argc, argv)
216 int argc;
217 char **argv;
218{
219 if (alternate_editor)
220 {
3cf8c6aa 221 int i = optind - 1;
97e3214d 222 execvp (alternate_editor, argv + i);
23431241 223 return;
97e3214d
GM
224 }
225 else
226 {
227 exit (1);
228 }
229}
230
231
97e3214d 232\f
30be2360 233#if !defined (HAVE_SOCKETS) || defined (NO_SOCKETS_IN_FILE_SYSTEM)
46cec291 234
de073ce3 235int
46cec291
RS
236main (argc, argv)
237 int argc;
238 char **argv;
239{
240 fprintf (stderr, "%s: Sorry, the Emacs server is supported only\n",
241 argv[0]);
30be2360 242 fprintf (stderr, "on systems with Berkeley sockets.\n");
97e3214d
GM
243
244 fail (argc, argv);
46cec291
RS
245}
246
30be2360 247#else /* HAVE_SOCKETS */
46cec291
RS
248
249#include <sys/types.h>
250#include <sys/socket.h>
251#include <sys/un.h>
efb859b4 252#include <sys/stat.h>
46cec291
RS
253#include <errno.h>
254
92af894f 255extern char *strerror ();
46cec291
RS
256extern int errno;
257
9f637eea
GM
258/* Three possibilities:
259 2 - can't be `stat'ed (sets errno)
260 1 - isn't owned by us
261 0 - success: none of the above */
262
263static int
264socket_status (socket_name)
265 char *socket_name;
266{
267 struct stat statbfr;
268
269 if (stat (socket_name, &statbfr) == -1)
270 return 2;
271
272 if (statbfr.st_uid != geteuid ())
273 return 1;
274
275 return 0;
276}
277
340ff9de 278int
46cec291
RS
279main (argc, argv)
280 int argc;
281 char **argv;
282{
a2b3f2b5
RS
283 char *system_name;
284 int system_name_length;
3cf8c6aa 285 int s, i, needlf = 0;
23a7488d 286 FILE *out, *in;
46cec291 287 struct sockaddr_un server;
571512de 288 char *cwd, *str;
46cec291 289 char string[BUFSIZ];
8f9aaa0a
RS
290
291 progname = argv[0];
46cec291 292
8f9aaa0a 293 /* Process options. */
5212210c 294 decode_options (argc, argv);
46cec291 295
5212210c 296 if (argc - optind < 1)
8f9aaa0a 297 print_help_and_exit ();
46cec291 298
e69233c2 299 /*
46cec291
RS
300 * Open up an AF_UNIX socket in this person's home directory
301 */
302
303 if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
304 {
305 fprintf (stderr, "%s: ", argv[0]);
306 perror ("socket");
97e3214d 307 fail (argc, argv);
46cec291 308 }
e69233c2 309
46cec291 310 server.sun_family = AF_UNIX;
c5fee545 311
efb859b4 312 {
3ecdcd59 313 char *dot;
a2b3f2b5
RS
314 system_name_length = 32;
315
316 while (1)
317 {
318 system_name = (char *) xmalloc (system_name_length + 1);
319
320 /* system_name must be null-terminated string. */
321 system_name[system_name_length] = '\0';
322
323 if (gethostname (system_name, system_name_length) == 0)
324 break;
325
326 free (system_name);
327 system_name_length *= 2;
328 }
3ecdcd59
SM
329
330 /* We always use the non-dotted host name, for simplicity. */
331 dot = index (system_name, '.');
332 if (dot)
333 *dot = '\0';
c5fee545
KH
334 }
335
c5fee545 336 {
9f637eea 337 int sock_status = 0;
efb859b4 338
23431241 339 sprintf (server.sun_path, "/tmp/esrv%d-%s", (int) geteuid (), system_name);
efb859b4 340
9f637eea
GM
341 /* See if the socket exists, and if it's owned by us. */
342 sock_status = socket_status (server.sun_path);
343 if (sock_status)
efb859b4 344 {
9f637eea
GM
345 /* Failing that, see if LOGNAME or USER exist and differ from
346 our euid. If so, look for a socket based on the UID
347 associated with the name. This is reminiscent of the logic
348 that init_editfns uses to set the global Vuser_full_name. */
e69233c2 349
9f637eea
GM
350 char *user_name = (char *) getenv ("LOGNAME");
351 if (!user_name)
352 user_name = (char *) getenv ("USER");
e69233c2 353
9f637eea
GM
354 if (user_name)
355 {
356 struct passwd *pw = getpwnam (user_name);
357 if (pw && (pw->pw_uid != geteuid ()))
358 {
359 /* We're running under su, apparently. */
360 sprintf (server.sun_path, "/tmp/esrv%d-%s",
23431241 361 (int) pw->pw_uid, system_name);
9f637eea
GM
362 sock_status = socket_status (server.sun_path);
363 }
364 }
efb859b4 365 }
e69233c2 366
9f637eea
GM
367 switch (sock_status)
368 {
369 case 1:
370 /* There's a socket, but it isn't owned by us. This is OK if
371 we are root. */
372 if (0 != geteuid ())
373 {
374 fprintf (stderr, "%s: Invalid socket owner\n", argv[0]);
375 fail (argc, argv);
376 }
377 break;
e69233c2 378
9f637eea
GM
379 case 2:
380 /* `stat' failed */
381 if (errno == ENOENT)
382 fprintf (stderr,
45adde32
SE
383 "%s: can't find socket; have you started the server?\n\
384To start the server in Emacs, type \"M-x server-start\".\n",
9f637eea
GM
385 argv[0]);
386 else
387 fprintf (stderr, "%s: can't stat %s: %s\n",
388 argv[0], server.sun_path, strerror (errno));
389 fail (argc, argv);
390 break;
391 }
efb859b4 392 }
46cec291 393
4e23f2ba
JB
394 if (connect (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2)
395 < 0)
46cec291
RS
396 {
397 fprintf (stderr, "%s: ", argv[0]);
398 perror ("connect");
97e3214d 399 fail (argc, argv);
46cec291 400 }
23a7488d
RS
401
402 /* We use the stream OUT to send our command to the server. */
46cec291
RS
403 if ((out = fdopen (s, "r+")) == NULL)
404 {
405 fprintf (stderr, "%s: ", argv[0]);
406 perror ("fdopen");
97e3214d 407 fail (argc, argv);
46cec291
RS
408 }
409
23a7488d
RS
410 /* We use the stream IN to read the response.
411 We used to use just one stream for both output and input
412 on the socket, but reversing direction works nonportably:
413 on some systems, the output appears as the first input;
414 on other systems it does not. */
415 if ((in = fdopen (s, "r+")) == NULL)
416 {
417 fprintf (stderr, "%s: ", argv[0]);
418 perror ("fdopen");
97e3214d 419 fail (argc, argv);
23a7488d
RS
420 }
421
38732dba 422#ifdef HAVE_GETCWD
ee6a193c 423 cwd = getcwd (string, sizeof string);
38732dba
RS
424#else
425 cwd = getwd (string);
ee6a193c 426#endif
46cec291
RS
427 if (cwd == 0)
428 {
429 /* getwd puts message in STRING if it fails. */
bd252662 430 fprintf (stderr, "%s: %s (%s)\n", argv[0],
38732dba 431#ifdef HAVE_GETCWD
bd252662 432 "Cannot get current working directory",
38732dba
RS
433#else
434 string,
bd252662
RS
435#endif
436 strerror (errno));
97e3214d 437 fail (argc, argv);
46cec291
RS
438 }
439
5212210c
RS
440 if (nowait)
441 fprintf (out, "-nowait ");
292d74a3 442
30be2360
SM
443 if (eval)
444 fprintf (out, "-eval ");
445
446 if (display)
447 fprintf (out, "-display %s ", quote_file_name (display));
448
5212210c
RS
449 for (i = optind; i < argc; i++)
450 {
30be2360
SM
451 if (eval)
452 ; /* Don't prepend any cwd or anything like that. */
453 else if (*argv[i] == '+')
46cec291
RS
454 {
455 char *p = argv[i] + 1;
cb1cdedd 456 while (isdigit ((unsigned char) *p) || *p == ':') p++;
46cec291 457 if (*p != 0)
0343a017 458 fprintf (out, "%s/", quote_file_name (cwd));
46cec291
RS
459 }
460 else if (*argv[i] != '/')
0343a017 461 fprintf (out, "%s/", quote_file_name (cwd));
5212210c
RS
462
463 fprintf (out, "%s ", quote_file_name (argv[i]));
46cec291
RS
464 }
465 fprintf (out, "\n");
466 fflush (out);
467
292d74a3
RS
468 /* Maybe wait for an answer. */
469 if (nowait)
470 return 0;
471
30be2360
SM
472 if (!eval)
473 {
474 printf ("Waiting for Emacs...");
475 needlf = 2;
476 }
46cec291
RS
477 fflush (stdout);
478
3cf8c6aa 479 /* Now, wait for an answer and print any messages. */
e69233c2 480 while ((str = fgets (string, BUFSIZ, in)))
3cf8c6aa
SM
481 {
482 if (needlf == 2)
483 printf ("\n");
484 printf ("%s", str);
485 needlf = str[0] == '\0' ? needlf : str[strlen (str) - 1] != '\n';
486 }
487
488 if (needlf)
489 printf ("\n");
490 fflush (stdout);
23a7488d 491
340ff9de 492 return 0;
46cec291
RS
493}
494
30be2360 495#endif /* HAVE_SOCKETS */
27711600
RM
496\f
497#ifndef HAVE_STRERROR
498char *
499strerror (errnum)
500 int errnum;
501{
502 extern char *sys_errlist[];
503 extern int sys_nerr;
504
505 if (errnum >= 0 && errnum < sys_nerr)
506 return sys_errlist[errnum];
507 return (char *) "Unknown error";
508}
509
510#endif /* ! HAVE_STRERROR */