(print_help_and_exit): Print to stdout. Exit successfully. Added some blank
[bpt/emacs.git] / lib-src / emacsclient.c
CommitLineData
efb859b4 1/* Client process that communicates with GNU Emacs acting as server.
20c396e8 2 Copyright (C) 1986, 1987, 1994, 1999, 2000, 2001, 2003
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
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':
20c396e8
JB
125 printf ("emacsclient %s\n", VERSION);
126 exit (0);
8f9aaa0a 127 break;
46cec291 128
8f9aaa0a 129 case 'H':
8f9aaa0a 130 print_help_and_exit ();
20c396e8
JB
131 break;
132
133 default:
134 fprintf (stderr, "Try `%s --help' for more information\n", progname);
135 exit (1);
136 break;
8f9aaa0a
RS
137 }
138 }
8f9aaa0a
RS
139}
140
7f3bff3e 141void
8f9aaa0a
RS
142print_help_and_exit ()
143{
20c396e8
JB
144 printf (
145 "Usage: %s [OPTIONS] FILE...\n\
30be2360
SM
146Tell the Emacs server to visit the specified files.\n\
147Every FILE can be either just a FILENAME or [+LINE[:COLUMN]] FILENAME.\n\
20c396e8 148\n\
30be2360
SM
149The following OPTIONS are accepted:\n\
150-V, --version Just print a version info and return\n\
151-H, --help Print this usage information message\n\
152-n, --no-wait Don't wait for the server to return\n\
153-e, --eval Evaluate the FILE arguments as ELisp expressions\n\
154-d, --display=DISPLAY Visit the file in the given display\n\
155-a, --alternate-editor=EDITOR\n\
156 Editor to fallback to if the server is not running\n\
20c396e8 157\n\
30be2360 158Report bugs to bug-gnu-emacs@gnu.org.\n", progname);
20c396e8 159 exit (0);
8f9aaa0a 160}
5212210c 161
f1db6a73 162/* Return a copy of NAME, inserting a &
3cf8c6aa 163 before each &, each space, each newline, and any initial -.
5212210c
RS
164 Change spaces to underscores, too, so that the
165 return value never contains a space. */
166
167char *
168quote_file_name (name)
169 char *name;
170{
171 char *copy = (char *) malloc (strlen (name) * 2 + 1);
172 char *p, *q;
173
174 p = name;
175 q = copy;
176 while (*p)
177 {
178 if (*p == ' ')
179 {
f1db6a73 180 *q++ = '&';
5212210c
RS
181 *q++ = '_';
182 p++;
183 }
3cf8c6aa
SM
184 else if (*p == '\n')
185 {
186 *q++ = '&';
187 *q++ = 'n';
188 p++;
189 }
5212210c
RS
190 else
191 {
f1db6a73
RS
192 if (*p == '&' || (*p == '-' && p == name))
193 *q++ = '&';
5212210c
RS
194 *q++ = *p++;
195 }
196 }
f1db6a73 197 *q++ = 0;
5212210c
RS
198
199 return copy;
200}
0c76956f 201
0c76956f
RS
202/* Like malloc but get fatal error if memory is exhausted. */
203
5497dfdb 204long *
0c76956f
RS
205xmalloc (size)
206 unsigned int size;
207{
5f32a712 208 long *result = (long *) malloc (size);
0c76956f
RS
209 if (result == NULL)
210 {
211 perror ("malloc");
212 exit (1);
213 }
214 return result;
215}
8f9aaa0a 216\f
97e3214d
GM
217/*
218 Try to run a different command, or --if no alternate editor is
219 defined-- exit with an errorcode.
220*/
de073ce3 221void
97e3214d
GM
222fail (argc, argv)
223 int argc;
224 char **argv;
225{
226 if (alternate_editor)
227 {
3cf8c6aa 228 int i = optind - 1;
97e3214d 229 execvp (alternate_editor, argv + i);
23431241 230 return;
97e3214d
GM
231 }
232 else
233 {
234 exit (1);
235 }
236}
237
238
97e3214d 239\f
30be2360 240#if !defined (HAVE_SOCKETS) || defined (NO_SOCKETS_IN_FILE_SYSTEM)
46cec291 241
de073ce3 242int
46cec291
RS
243main (argc, argv)
244 int argc;
245 char **argv;
246{
247 fprintf (stderr, "%s: Sorry, the Emacs server is supported only\n",
248 argv[0]);
30be2360 249 fprintf (stderr, "on systems with Berkeley sockets.\n");
97e3214d
GM
250
251 fail (argc, argv);
46cec291
RS
252}
253
30be2360 254#else /* HAVE_SOCKETS */
46cec291
RS
255
256#include <sys/types.h>
257#include <sys/socket.h>
258#include <sys/un.h>
efb859b4 259#include <sys/stat.h>
46cec291
RS
260#include <errno.h>
261
92af894f 262extern char *strerror ();
46cec291
RS
263extern int errno;
264
9f637eea
GM
265/* Three possibilities:
266 2 - can't be `stat'ed (sets errno)
267 1 - isn't owned by us
268 0 - success: none of the above */
269
270static int
271socket_status (socket_name)
272 char *socket_name;
273{
274 struct stat statbfr;
275
276 if (stat (socket_name, &statbfr) == -1)
277 return 2;
278
279 if (statbfr.st_uid != geteuid ())
280 return 1;
281
282 return 0;
283}
284
340ff9de 285int
46cec291
RS
286main (argc, argv)
287 int argc;
288 char **argv;
289{
a2b3f2b5
RS
290 char *system_name;
291 int system_name_length;
3cf8c6aa 292 int s, i, needlf = 0;
23a7488d 293 FILE *out, *in;
46cec291 294 struct sockaddr_un server;
571512de 295 char *cwd, *str;
46cec291 296 char string[BUFSIZ];
8f9aaa0a
RS
297
298 progname = argv[0];
46cec291 299
8f9aaa0a 300 /* Process options. */
5212210c 301 decode_options (argc, argv);
46cec291 302
5212210c 303 if (argc - optind < 1)
20c396e8
JB
304 {
305 fprintf (stderr, "%s: file name or argument required\n", progname);
306 fprintf (stderr, "Try `%s --help' for more information\n", progname);
307 exit (1);
308 }
46cec291 309
e69233c2 310 /*
46cec291
RS
311 * Open up an AF_UNIX socket in this person's home directory
312 */
313
314 if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
315 {
316 fprintf (stderr, "%s: ", argv[0]);
317 perror ("socket");
97e3214d 318 fail (argc, argv);
46cec291 319 }
e69233c2 320
46cec291 321 server.sun_family = AF_UNIX;
c5fee545 322
efb859b4 323 {
3ecdcd59 324 char *dot;
a2b3f2b5
RS
325 system_name_length = 32;
326
327 while (1)
328 {
329 system_name = (char *) xmalloc (system_name_length + 1);
330
331 /* system_name must be null-terminated string. */
332 system_name[system_name_length] = '\0';
333
334 if (gethostname (system_name, system_name_length) == 0)
335 break;
336
337 free (system_name);
338 system_name_length *= 2;
339 }
3ecdcd59
SM
340
341 /* We always use the non-dotted host name, for simplicity. */
342 dot = index (system_name, '.');
343 if (dot)
344 *dot = '\0';
c5fee545
KH
345 }
346
c5fee545 347 {
9f637eea 348 int sock_status = 0;
efb859b4 349
23431241 350 sprintf (server.sun_path, "/tmp/esrv%d-%s", (int) geteuid (), system_name);
efb859b4 351
9f637eea
GM
352 /* See if the socket exists, and if it's owned by us. */
353 sock_status = socket_status (server.sun_path);
354 if (sock_status)
efb859b4 355 {
9f637eea
GM
356 /* Failing that, see if LOGNAME or USER exist and differ from
357 our euid. If so, look for a socket based on the UID
358 associated with the name. This is reminiscent of the logic
359 that init_editfns uses to set the global Vuser_full_name. */
e69233c2 360
9f637eea
GM
361 char *user_name = (char *) getenv ("LOGNAME");
362 if (!user_name)
363 user_name = (char *) getenv ("USER");
e69233c2 364
9f637eea
GM
365 if (user_name)
366 {
367 struct passwd *pw = getpwnam (user_name);
368 if (pw && (pw->pw_uid != geteuid ()))
369 {
370 /* We're running under su, apparently. */
371 sprintf (server.sun_path, "/tmp/esrv%d-%s",
23431241 372 (int) pw->pw_uid, system_name);
9f637eea
GM
373 sock_status = socket_status (server.sun_path);
374 }
375 }
efb859b4 376 }
e69233c2 377
9f637eea
GM
378 switch (sock_status)
379 {
380 case 1:
381 /* There's a socket, but it isn't owned by us. This is OK if
382 we are root. */
383 if (0 != geteuid ())
384 {
385 fprintf (stderr, "%s: Invalid socket owner\n", argv[0]);
386 fail (argc, argv);
387 }
388 break;
e69233c2 389
9f637eea
GM
390 case 2:
391 /* `stat' failed */
392 if (errno == ENOENT)
393 fprintf (stderr,
45adde32
SE
394 "%s: can't find socket; have you started the server?\n\
395To start the server in Emacs, type \"M-x server-start\".\n",
9f637eea
GM
396 argv[0]);
397 else
398 fprintf (stderr, "%s: can't stat %s: %s\n",
399 argv[0], server.sun_path, strerror (errno));
400 fail (argc, argv);
401 break;
402 }
efb859b4 403 }
46cec291 404
4e23f2ba
JB
405 if (connect (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2)
406 < 0)
46cec291
RS
407 {
408 fprintf (stderr, "%s: ", argv[0]);
409 perror ("connect");
97e3214d 410 fail (argc, argv);
46cec291 411 }
23a7488d
RS
412
413 /* We use the stream OUT to send our command to the server. */
46cec291
RS
414 if ((out = fdopen (s, "r+")) == NULL)
415 {
416 fprintf (stderr, "%s: ", argv[0]);
417 perror ("fdopen");
97e3214d 418 fail (argc, argv);
46cec291
RS
419 }
420
23a7488d
RS
421 /* We use the stream IN to read the response.
422 We used to use just one stream for both output and input
423 on the socket, but reversing direction works nonportably:
424 on some systems, the output appears as the first input;
425 on other systems it does not. */
426 if ((in = fdopen (s, "r+")) == NULL)
427 {
428 fprintf (stderr, "%s: ", argv[0]);
429 perror ("fdopen");
97e3214d 430 fail (argc, argv);
23a7488d
RS
431 }
432
38732dba 433#ifdef HAVE_GETCWD
ee6a193c 434 cwd = getcwd (string, sizeof string);
38732dba
RS
435#else
436 cwd = getwd (string);
ee6a193c 437#endif
46cec291
RS
438 if (cwd == 0)
439 {
440 /* getwd puts message in STRING if it fails. */
bd252662 441 fprintf (stderr, "%s: %s (%s)\n", argv[0],
38732dba 442#ifdef HAVE_GETCWD
bd252662 443 "Cannot get current working directory",
38732dba
RS
444#else
445 string,
bd252662
RS
446#endif
447 strerror (errno));
97e3214d 448 fail (argc, argv);
46cec291
RS
449 }
450
5212210c
RS
451 if (nowait)
452 fprintf (out, "-nowait ");
292d74a3 453
30be2360
SM
454 if (eval)
455 fprintf (out, "-eval ");
456
457 if (display)
458 fprintf (out, "-display %s ", quote_file_name (display));
459
5212210c
RS
460 for (i = optind; i < argc; i++)
461 {
30be2360
SM
462 if (eval)
463 ; /* Don't prepend any cwd or anything like that. */
464 else if (*argv[i] == '+')
46cec291
RS
465 {
466 char *p = argv[i] + 1;
cb1cdedd 467 while (isdigit ((unsigned char) *p) || *p == ':') p++;
46cec291 468 if (*p != 0)
0343a017 469 fprintf (out, "%s/", quote_file_name (cwd));
46cec291
RS
470 }
471 else if (*argv[i] != '/')
0343a017 472 fprintf (out, "%s/", quote_file_name (cwd));
5212210c
RS
473
474 fprintf (out, "%s ", quote_file_name (argv[i]));
46cec291
RS
475 }
476 fprintf (out, "\n");
477 fflush (out);
478
292d74a3
RS
479 /* Maybe wait for an answer. */
480 if (nowait)
481 return 0;
482
30be2360
SM
483 if (!eval)
484 {
485 printf ("Waiting for Emacs...");
486 needlf = 2;
487 }
46cec291
RS
488 fflush (stdout);
489
3cf8c6aa 490 /* Now, wait for an answer and print any messages. */
e69233c2 491 while ((str = fgets (string, BUFSIZ, in)))
3cf8c6aa
SM
492 {
493 if (needlf == 2)
494 printf ("\n");
495 printf ("%s", str);
496 needlf = str[0] == '\0' ? needlf : str[strlen (str) - 1] != '\n';
497 }
498
499 if (needlf)
500 printf ("\n");
501 fflush (stdout);
23a7488d 502
340ff9de 503 return 0;
46cec291
RS
504}
505
30be2360 506#endif /* HAVE_SOCKETS */
27711600
RM
507\f
508#ifndef HAVE_STRERROR
509char *
510strerror (errnum)
511 int errnum;
512{
513 extern char *sys_errlist[];
514 extern int sys_nerr;
515
516 if (errnum >= 0 && errnum < sys_nerr)
517 return sys_errlist[errnum];
518 return (char *) "Unknown error";
519}
520
521#endif /* ! HAVE_STRERROR */