*** empty log message ***
[bpt/emacs.git] / lib-src / emacsclient.c
CommitLineData
efb859b4 1/* Client process that communicates with GNU Emacs acting as server.
5c9659d3 2 Copyright (C) 1986, 1987, 1994, 1999, 2000, 2001, 2003, 2004
de073ce3 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 44char *getenv (), *getwd ();
5b9562c3 45char *(getcwd) ();
8f9aaa0a 46
8f9aaa0a
RS
47#ifndef VERSION
48#define VERSION "unspecified"
49#endif
50\f
51/* Name used to invoke this program. */
52char *progname;
53
749ae770 54/* Nonzero means don't wait for a response from Emacs. --no-wait. */
8f9aaa0a
RS
55int nowait = 0;
56
30be2360
SM
57/* Nonzero means args are expressions to be evaluated. --eval. */
58int eval = 0;
59
60/* The display on which Emacs should work. --display. */
61char *display = NULL;
62
63/* If non-NULL, the name of an editor to fallback to if the server
64 is not running. --alternate-editor. */
65const char * alternate_editor = NULL;
66
3db926be 67/* If non-NULL, the filename of the UNIX socket. */
254107e4
RS
68char *socket_name = 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' },
254107e4 79 { "socket-name", required_argument, NULL, 's' },
30be2360
SM
80 { "display", required_argument, NULL, 'd' },
81 { 0, 0, 0, 0 }
8f9aaa0a
RS
82};
83
84/* Decode the options from argv and argc.
5212210c 85 The global variable `optind' will say how many arguments we used up. */
8f9aaa0a 86
5212210c 87void
8f9aaa0a
RS
88decode_options (argc, argv)
89 int argc;
90 char **argv;
91{
b6b6d6d2
SM
92 alternate_editor = getenv ("ALTERNATE_EDITOR");
93
8f9aaa0a
RS
94 while (1)
95 {
96 int opt = getopt_long (argc, argv,
254107e4 97 "VHnea:s:d:", longopts, 0);
8f9aaa0a
RS
98
99 if (opt == EOF)
100 break;
101
102 switch (opt)
103 {
104 case 0:
105 /* If getopt returns 0, then it has already processed a
106 long-named option. We should do nothing. */
107 break;
e69233c2 108
97e3214d
GM
109 case 'a':
110 alternate_editor = optarg;
111 break;
e69233c2 112
254107e4
RS
113 case 's':
114 socket_name = optarg;
115 break;
116
30be2360
SM
117 case 'd':
118 display = optarg;
119 break;
120
8f9aaa0a
RS
121 case 'n':
122 nowait = 1;
123 break;
124
30be2360
SM
125 case 'e':
126 eval = 1;
127 break;
128
8f9aaa0a 129 case 'V':
20c396e8 130 printf ("emacsclient %s\n", VERSION);
65396510 131 exit (EXIT_SUCCESS);
8f9aaa0a 132 break;
46cec291 133
8f9aaa0a 134 case 'H':
8f9aaa0a 135 print_help_and_exit ();
20c396e8
JB
136 break;
137
138 default:
139 fprintf (stderr, "Try `%s --help' for more information\n", progname);
65396510 140 exit (EXIT_FAILURE);
20c396e8 141 break;
8f9aaa0a
RS
142 }
143 }
8f9aaa0a
RS
144}
145
7f3bff3e 146void
8f9aaa0a
RS
147print_help_and_exit ()
148{
20c396e8
JB
149 printf (
150 "Usage: %s [OPTIONS] FILE...\n\
30be2360
SM
151Tell the Emacs server to visit the specified files.\n\
152Every FILE can be either just a FILENAME or [+LINE[:COLUMN]] FILENAME.\n\
20c396e8 153\n\
30be2360
SM
154The following OPTIONS are accepted:\n\
155-V, --version Just print a version info and return\n\
156-H, --help Print this usage information message\n\
157-n, --no-wait Don't wait for the server to return\n\
158-e, --eval Evaluate the FILE arguments as ELisp expressions\n\
159-d, --display=DISPLAY Visit the file in the given display\n\
254107e4
RS
160-s, --socket-name=FILENAME\n\
161 Set the filename of the UNIX socket for communication\n\
30be2360
SM
162-a, --alternate-editor=EDITOR\n\
163 Editor to fallback to if the server is not running\n\
20c396e8 164\n\
30be2360 165Report bugs to bug-gnu-emacs@gnu.org.\n", progname);
65396510 166 exit (EXIT_SUCCESS);
8f9aaa0a 167}
5212210c 168
a4cf2096
RS
169/* In NAME, insert a & before each &, each space, each newline, and
170 any initial -. Change spaces to underscores, too, so that the
5212210c
RS
171 return value never contains a space. */
172
87209357
EZ
173void
174quote_file_name (name, stream)
5212210c 175 char *name;
87209357 176 FILE *stream;
5212210c
RS
177{
178 char *copy = (char *) malloc (strlen (name) * 2 + 1);
179 char *p, *q;
180
181 p = name;
182 q = copy;
183 while (*p)
184 {
185 if (*p == ' ')
186 {
f1db6a73 187 *q++ = '&';
5212210c
RS
188 *q++ = '_';
189 p++;
190 }
3cf8c6aa
SM
191 else if (*p == '\n')
192 {
193 *q++ = '&';
194 *q++ = 'n';
195 p++;
196 }
5212210c
RS
197 else
198 {
f1db6a73
RS
199 if (*p == '&' || (*p == '-' && p == name))
200 *q++ = '&';
5212210c
RS
201 *q++ = *p++;
202 }
203 }
f1db6a73 204 *q++ = 0;
5212210c 205
cf0fad84 206 fprintf (stream, "%s", copy);
87209357
EZ
207
208 free (copy);
5212210c 209}
0c76956f 210
0c76956f
RS
211/* Like malloc but get fatal error if memory is exhausted. */
212
5497dfdb 213long *
0c76956f
RS
214xmalloc (size)
215 unsigned int size;
216{
5f32a712 217 long *result = (long *) malloc (size);
0c76956f
RS
218 if (result == NULL)
219 {
220 perror ("malloc");
65396510 221 exit (EXIT_FAILURE);
0c76956f
RS
222 }
223 return result;
224}
8f9aaa0a 225\f
97e3214d
GM
226/*
227 Try to run a different command, or --if no alternate editor is
228 defined-- exit with an errorcode.
229*/
de073ce3 230void
97e3214d
GM
231fail (argc, argv)
232 int argc;
233 char **argv;
234{
235 if (alternate_editor)
236 {
3cf8c6aa 237 int i = optind - 1;
97e3214d 238 execvp (alternate_editor, argv + i);
23431241 239 return;
97e3214d
GM
240 }
241 else
242 {
65396510 243 exit (EXIT_FAILURE);
97e3214d
GM
244 }
245}
246
247
97e3214d 248\f
30be2360 249#if !defined (HAVE_SOCKETS) || defined (NO_SOCKETS_IN_FILE_SYSTEM)
46cec291 250
de073ce3 251int
46cec291
RS
252main (argc, argv)
253 int argc;
254 char **argv;
255{
256 fprintf (stderr, "%s: Sorry, the Emacs server is supported only\n",
257 argv[0]);
30be2360 258 fprintf (stderr, "on systems with Berkeley sockets.\n");
97e3214d
GM
259
260 fail (argc, argv);
46cec291
RS
261}
262
30be2360 263#else /* HAVE_SOCKETS */
46cec291
RS
264
265#include <sys/types.h>
266#include <sys/socket.h>
267#include <sys/un.h>
efb859b4 268#include <sys/stat.h>
46cec291
RS
269#include <errno.h>
270
92af894f 271extern char *strerror ();
46cec291
RS
272extern int errno;
273
9f637eea
GM
274/* Three possibilities:
275 2 - can't be `stat'ed (sets errno)
276 1 - isn't owned by us
277 0 - success: none of the above */
278
279static int
280socket_status (socket_name)
281 char *socket_name;
282{
283 struct stat statbfr;
284
285 if (stat (socket_name, &statbfr) == -1)
286 return 2;
287
288 if (statbfr.st_uid != geteuid ())
289 return 1;
290
291 return 0;
292}
293
340ff9de 294int
46cec291
RS
295main (argc, argv)
296 int argc;
297 char **argv;
298{
3cf8c6aa 299 int s, i, needlf = 0;
23a7488d 300 FILE *out, *in;
46cec291 301 struct sockaddr_un server;
571512de 302 char *cwd, *str;
46cec291 303 char string[BUFSIZ];
8f9aaa0a
RS
304
305 progname = argv[0];
46cec291 306
8f9aaa0a 307 /* Process options. */
5212210c 308 decode_options (argc, argv);
46cec291 309
87209357 310 if ((argc - optind < 1) && !eval)
20c396e8
JB
311 {
312 fprintf (stderr, "%s: file name or argument required\n", progname);
313 fprintf (stderr, "Try `%s --help' for more information\n", progname);
65396510 314 exit (EXIT_FAILURE);
20c396e8 315 }
46cec291 316
e69233c2 317 /*
46cec291
RS
318 * Open up an AF_UNIX socket in this person's home directory
319 */
320
321 if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
322 {
323 fprintf (stderr, "%s: ", argv[0]);
324 perror ("socket");
97e3214d 325 fail (argc, argv);
46cec291 326 }
e69233c2 327
46cec291 328 server.sun_family = AF_UNIX;
c5fee545 329
c5fee545 330 {
9f637eea 331 int sock_status = 0;
5c9659d3 332 int default_sock = !socket_name;
152b6e83 333 int saved_errno;
0734b0d0
SM
334 char *server_name = "server";
335
336 if (socket_name && !index (socket_name, '/') && !index (socket_name, '\\'))
337 { /* socket_name is a file name component. */
338 server_name = socket_name;
339 socket_name = NULL;
340 default_sock = 1; /* Try both UIDs. */
341 }
efb859b4 342
5c9659d3 343 if (default_sock)
254107e4 344 {
0734b0d0
SM
345 socket_name = alloca (100 + strlen (server_name));
346 sprintf (socket_name, "/tmp/emacs%d/%s",
347 (int) geteuid (), server_name);
254107e4
RS
348 }
349
350 if (strlen (socket_name) < sizeof (server.sun_path))
351 strcpy (server.sun_path, socket_name);
352 else
5c9659d3
SM
353 {
354 fprintf (stderr, "%s: socket-name %s too long",
355 argv[0], socket_name);
65396510 356 exit (EXIT_FAILURE);
5c9659d3 357 }
efb859b4 358
9f637eea
GM
359 /* See if the socket exists, and if it's owned by us. */
360 sock_status = socket_status (server.sun_path);
152b6e83 361 saved_errno = errno;
5c9659d3 362 if (sock_status && default_sock)
efb859b4 363 {
9f637eea
GM
364 /* Failing that, see if LOGNAME or USER exist and differ from
365 our euid. If so, look for a socket based on the UID
366 associated with the name. This is reminiscent of the logic
367 that init_editfns uses to set the global Vuser_full_name. */
e69233c2 368
9f637eea 369 char *user_name = (char *) getenv ("LOGNAME");
293f9f2a 370
9f637eea
GM
371 if (!user_name)
372 user_name = (char *) getenv ("USER");
e69233c2 373
9f637eea
GM
374 if (user_name)
375 {
376 struct passwd *pw = getpwnam (user_name);
293f9f2a 377
9f637eea
GM
378 if (pw && (pw->pw_uid != geteuid ()))
379 {
380 /* We're running under su, apparently. */
0734b0d0
SM
381 socket_name = alloca (100 + strlen (server_name));
382 sprintf (socket_name, "/tmp/emacs%d/%s",
383 (int) pw->pw_uid, server_name);
5c9659d3
SM
384
385 if (strlen (socket_name) < sizeof (server.sun_path))
386 strcpy (server.sun_path, socket_name);
387 else
388 {
389 fprintf (stderr, "%s: socket-name %s too long",
390 argv[0], socket_name);
65396510 391 exit (EXIT_FAILURE);
5c9659d3
SM
392 }
393
9f637eea 394 sock_status = socket_status (server.sun_path);
152b6e83 395 saved_errno = errno;
9f637eea 396 }
293f9f2a
RS
397 else
398 errno = saved_errno;
9f637eea 399 }
efb859b4 400 }
e69233c2 401
9f637eea
GM
402 switch (sock_status)
403 {
404 case 1:
405 /* There's a socket, but it isn't owned by us. This is OK if
406 we are root. */
407 if (0 != geteuid ())
408 {
409 fprintf (stderr, "%s: Invalid socket owner\n", argv[0]);
410 fail (argc, argv);
411 }
412 break;
e69233c2 413
9f637eea
GM
414 case 2:
415 /* `stat' failed */
152b6e83 416 if (saved_errno == ENOENT)
9f637eea 417 fprintf (stderr,
45adde32
SE
418 "%s: can't find socket; have you started the server?\n\
419To start the server in Emacs, type \"M-x server-start\".\n",
9f637eea
GM
420 argv[0]);
421 else
422 fprintf (stderr, "%s: can't stat %s: %s\n",
152b6e83 423 argv[0], server.sun_path, strerror (saved_errno));
9f637eea
GM
424 fail (argc, argv);
425 break;
426 }
efb859b4 427 }
46cec291 428
4e23f2ba
JB
429 if (connect (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2)
430 < 0)
46cec291
RS
431 {
432 fprintf (stderr, "%s: ", argv[0]);
433 perror ("connect");
97e3214d 434 fail (argc, argv);
46cec291 435 }
23a7488d
RS
436
437 /* We use the stream OUT to send our command to the server. */
46cec291
RS
438 if ((out = fdopen (s, "r+")) == NULL)
439 {
440 fprintf (stderr, "%s: ", argv[0]);
441 perror ("fdopen");
97e3214d 442 fail (argc, argv);
46cec291
RS
443 }
444
23a7488d
RS
445 /* We use the stream IN to read the response.
446 We used to use just one stream for both output and input
447 on the socket, but reversing direction works nonportably:
448 on some systems, the output appears as the first input;
449 on other systems it does not. */
450 if ((in = fdopen (s, "r+")) == NULL)
451 {
452 fprintf (stderr, "%s: ", argv[0]);
453 perror ("fdopen");
97e3214d 454 fail (argc, argv);
23a7488d
RS
455 }
456
38732dba 457#ifdef HAVE_GETCWD
ee6a193c 458 cwd = getcwd (string, sizeof string);
38732dba
RS
459#else
460 cwd = getwd (string);
ee6a193c 461#endif
46cec291
RS
462 if (cwd == 0)
463 {
464 /* getwd puts message in STRING if it fails. */
bb5618fe 465
38732dba 466#ifdef HAVE_GETCWD
bb5618fe
JB
467 fprintf (stderr, "%s: %s (%s)\n", argv[0],
468 "Cannot get current working directory", strerror (errno));
38732dba 469#else
bb5618fe 470 fprintf (stderr, "%s: %s (%s)\n", argv[0], string, strerror (errno));
bd252662 471#endif
97e3214d 472 fail (argc, argv);
46cec291
RS
473 }
474
5212210c
RS
475 if (nowait)
476 fprintf (out, "-nowait ");
292d74a3 477
30be2360
SM
478 if (eval)
479 fprintf (out, "-eval ");
480
481 if (display)
87209357
EZ
482 {
483 fprintf (out, "-display ");
484 quote_file_name (display, out);
485 fprintf (out, " ");
486 }
30be2360 487
87209357 488 if ((argc - optind > 0))
5212210c 489 {
87209357 490 for (i = optind; i < argc; i++)
46cec291 491 {
87209357
EZ
492 if (eval)
493 ; /* Don't prepend any cwd or anything like that. */
494 else if (*argv[i] == '+')
495 {
496 char *p = argv[i] + 1;
497 while (isdigit ((unsigned char) *p) || *p == ':') p++;
498 if (*p != 0)
499 {
500 quote_file_name (cwd, out);
501 fprintf (out, "/");
502 }
503 }
504 else if (*argv[i] != '/')
505 {
506 quote_file_name (cwd, out);
507 fprintf (out, "/");
508 }
509
510 quote_file_name (argv[i], out);
511 fprintf (out, " ");
46cec291 512 }
46cec291 513 }
87209357
EZ
514 else
515 {
516 while ((str = fgets (string, BUFSIZ, stdin)))
517 {
518 quote_file_name (str, out);
519 }
520 fprintf (out, " ");
521 }
65396510 522
46cec291
RS
523 fprintf (out, "\n");
524 fflush (out);
525
292d74a3
RS
526 /* Maybe wait for an answer. */
527 if (nowait)
65396510 528 return EXIT_SUCCESS;
292d74a3 529
30be2360
SM
530 if (!eval)
531 {
532 printf ("Waiting for Emacs...");
533 needlf = 2;
534 }
46cec291
RS
535 fflush (stdout);
536
3cf8c6aa 537 /* Now, wait for an answer and print any messages. */
e69233c2 538 while ((str = fgets (string, BUFSIZ, in)))
3cf8c6aa
SM
539 {
540 if (needlf == 2)
541 printf ("\n");
542 printf ("%s", str);
543 needlf = str[0] == '\0' ? needlf : str[strlen (str) - 1] != '\n';
544 }
545
546 if (needlf)
547 printf ("\n");
548 fflush (stdout);
23a7488d 549
65396510 550 return EXIT_SUCCESS;
46cec291
RS
551}
552
30be2360 553#endif /* HAVE_SOCKETS */
27711600
RM
554\f
555#ifndef HAVE_STRERROR
556char *
557strerror (errnum)
558 int errnum;
559{
560 extern char *sys_errlist[];
561 extern int sys_nerr;
562
563 if (errnum >= 0 && errnum < sys_nerr)
564 return sys_errlist[errnum];
565 return (char *) "Unknown error";
566}
567
568#endif /* ! HAVE_STRERROR */
ab5796a9
MB
569
570/* arch-tag: f39bb9c4-73eb-477e-896d-50832e2ca9a7
571 (do not change this comment) */
65396510
TTN
572
573/* emacsclient.c ends here */