(url-http-head-file-attributes): Add device "info".
[emacs.git] / lib-src / emacsclient.c
blobb0bf50c2c780e8ac21f3be48baa221f3817ac262
1 /* Client process that communicates with GNU Emacs acting as server.
2 Copyright (C) 1986, 1987, 1994, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006 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)
10 any later version.
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., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
23 #define NO_SHORTNAMES
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
29 #undef signal
31 #include <ctype.h>
32 #include <stdio.h>
33 #include <getopt.h>
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
38 #ifdef VMS
39 # include "vms-pwd.h"
40 #else
41 # include <pwd.h>
42 #endif /* not VMS */
44 char *getenv (), *getwd ();
45 char *(getcwd) ();
47 #ifndef VERSION
48 #define VERSION "unspecified"
49 #endif
51 /* Name used to invoke this program. */
52 char *progname;
54 /* Nonzero means don't wait for a response from Emacs. --no-wait. */
55 int nowait = 0;
57 /* Nonzero means args are expressions to be evaluated. --eval. */
58 int eval = 0;
60 /* The display on which Emacs should work. --display. */
61 char *display = NULL;
63 /* If non-NULL, the name of an editor to fallback to if the server
64 is not running. --alternate-editor. */
65 const char * alternate_editor = NULL;
67 /* If non-NULL, the filename of the UNIX socket. */
68 char *socket_name = NULL;
70 void print_help_and_exit () NO_RETURN;
72 struct option longopts[] =
74 { "no-wait", no_argument, NULL, 'n' },
75 { "eval", no_argument, NULL, 'e' },
76 { "help", no_argument, NULL, 'H' },
77 { "version", no_argument, NULL, 'V' },
78 { "alternate-editor", required_argument, NULL, 'a' },
79 { "socket-name", required_argument, NULL, 's' },
80 { "display", required_argument, NULL, 'd' },
81 { 0, 0, 0, 0 }
84 /* Decode the options from argv and argc.
85 The global variable `optind' will say how many arguments we used up. */
87 void
88 decode_options (argc, argv)
89 int argc;
90 char **argv;
92 alternate_editor = getenv ("ALTERNATE_EDITOR");
94 while (1)
96 int opt = getopt_long (argc, argv,
97 "VHnea:s:d:", longopts, 0);
99 if (opt == EOF)
100 break;
102 switch (opt)
104 case 0:
105 /* If getopt returns 0, then it has already processed a
106 long-named option. We should do nothing. */
107 break;
109 case 'a':
110 alternate_editor = optarg;
111 break;
113 case 's':
114 socket_name = optarg;
115 break;
117 case 'd':
118 display = optarg;
119 break;
121 case 'n':
122 nowait = 1;
123 break;
125 case 'e':
126 eval = 1;
127 break;
129 case 'V':
130 printf ("emacsclient %s\n", VERSION);
131 exit (EXIT_SUCCESS);
132 break;
134 case 'H':
135 print_help_and_exit ();
136 break;
138 default:
139 fprintf (stderr, "Try `%s --help' for more information\n", progname);
140 exit (EXIT_FAILURE);
141 break;
146 void
147 print_help_and_exit ()
149 printf (
150 "Usage: %s [OPTIONS] FILE...\n\
151 Tell the Emacs server to visit the specified files.\n\
152 Every FILE can be either just a FILENAME or [+LINE[:COLUMN]] FILENAME.\n\
154 The 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\
160 -s, --socket-name=FILENAME\n\
161 Set the filename of the UNIX socket for communication\n\
162 -a, --alternate-editor=EDITOR\n\
163 Editor to fallback to if the server is not running\n\
165 Report bugs to bug-gnu-emacs@gnu.org.\n", progname);
166 exit (EXIT_SUCCESS);
169 /* In NAME, insert a & before each &, each space, each newline, and
170 any initial -. Change spaces to underscores, too, so that the
171 return value never contains a space. */
173 void
174 quote_file_name (name, stream)
175 char *name;
176 FILE *stream;
178 char *copy = (char *) malloc (strlen (name) * 2 + 1);
179 char *p, *q;
181 p = name;
182 q = copy;
183 while (*p)
185 if (*p == ' ')
187 *q++ = '&';
188 *q++ = '_';
189 p++;
191 else if (*p == '\n')
193 *q++ = '&';
194 *q++ = 'n';
195 p++;
197 else
199 if (*p == '&' || (*p == '-' && p == name))
200 *q++ = '&';
201 *q++ = *p++;
204 *q++ = 0;
206 fprintf (stream, "%s", copy);
208 free (copy);
211 /* Like malloc but get fatal error if memory is exhausted. */
213 long *
214 xmalloc (size)
215 unsigned int size;
217 long *result = (long *) malloc (size);
218 if (result == NULL)
220 perror ("malloc");
221 exit (EXIT_FAILURE);
223 return result;
227 Try to run a different command, or --if no alternate editor is
228 defined-- exit with an errorcode.
230 void
231 fail (argc, argv)
232 int argc;
233 char **argv;
235 if (alternate_editor)
237 int i = optind - 1;
238 execvp (alternate_editor, argv + i);
239 return;
241 else
243 exit (EXIT_FAILURE);
249 #if !defined (HAVE_SOCKETS) || defined (NO_SOCKETS_IN_FILE_SYSTEM)
252 main (argc, argv)
253 int argc;
254 char **argv;
256 fprintf (stderr, "%s: Sorry, the Emacs server is supported only\n",
257 argv[0]);
258 fprintf (stderr, "on systems with Berkeley sockets.\n");
260 fail (argc, argv);
263 #else /* HAVE_SOCKETS */
265 #include <sys/types.h>
266 #include <sys/socket.h>
267 #include <sys/un.h>
268 #include <sys/stat.h>
269 #include <errno.h>
271 extern char *strerror ();
272 extern int errno;
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 */
279 static int
280 socket_status (socket_name)
281 char *socket_name;
283 struct stat statbfr;
285 if (stat (socket_name, &statbfr) == -1)
286 return 2;
288 if (statbfr.st_uid != geteuid ())
289 return 1;
291 return 0;
295 main (argc, argv)
296 int argc;
297 char **argv;
299 int s, i, needlf = 0;
300 FILE *out, *in;
301 struct sockaddr_un server;
302 char *cwd, *str;
303 char string[BUFSIZ];
305 progname = argv[0];
307 /* Process options. */
308 decode_options (argc, argv);
310 if ((argc - optind < 1) && !eval)
312 fprintf (stderr, "%s: file name or argument required\n", progname);
313 fprintf (stderr, "Try `%s --help' for more information\n", progname);
314 exit (EXIT_FAILURE);
318 * Open up an AF_UNIX socket in this person's home directory
321 if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
323 fprintf (stderr, "%s: ", argv[0]);
324 perror ("socket");
325 fail (argc, argv);
328 server.sun_family = AF_UNIX;
331 int sock_status = 0;
332 int default_sock = !socket_name;
333 int saved_errno;
334 char *server_name = "server";
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. */
343 if (default_sock)
345 socket_name = alloca (100 + strlen (server_name));
346 sprintf (socket_name, "/tmp/emacs%d/%s",
347 (int) geteuid (), server_name);
350 if (strlen (socket_name) < sizeof (server.sun_path))
351 strcpy (server.sun_path, socket_name);
352 else
354 fprintf (stderr, "%s: socket-name %s too long",
355 argv[0], socket_name);
356 exit (EXIT_FAILURE);
359 /* See if the socket exists, and if it's owned by us. */
360 sock_status = socket_status (server.sun_path);
361 saved_errno = errno;
362 if (sock_status && default_sock)
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. */
369 char *user_name = (char *) getenv ("LOGNAME");
371 if (!user_name)
372 user_name = (char *) getenv ("USER");
374 if (user_name)
376 struct passwd *pw = getpwnam (user_name);
378 if (pw && (pw->pw_uid != geteuid ()))
380 /* We're running under su, apparently. */
381 socket_name = alloca (100 + strlen (server_name));
382 sprintf (socket_name, "/tmp/emacs%d/%s",
383 (int) pw->pw_uid, server_name);
385 if (strlen (socket_name) < sizeof (server.sun_path))
386 strcpy (server.sun_path, socket_name);
387 else
389 fprintf (stderr, "%s: socket-name %s too long",
390 argv[0], socket_name);
391 exit (EXIT_FAILURE);
394 sock_status = socket_status (server.sun_path);
395 saved_errno = errno;
397 else
398 errno = saved_errno;
402 switch (sock_status)
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 ())
409 fprintf (stderr, "%s: Invalid socket owner\n", argv[0]);
410 fail (argc, argv);
412 break;
414 case 2:
415 /* `stat' failed */
416 if (saved_errno == ENOENT)
417 fprintf (stderr,
418 "%s: can't find socket; have you started the server?\n\
419 To start the server in Emacs, type \"M-x server-start\".\n",
420 argv[0]);
421 else
422 fprintf (stderr, "%s: can't stat %s: %s\n",
423 argv[0], server.sun_path, strerror (saved_errno));
424 fail (argc, argv);
425 break;
429 if (connect (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2)
430 < 0)
432 fprintf (stderr, "%s: ", argv[0]);
433 perror ("connect");
434 fail (argc, argv);
437 /* We use the stream OUT to send our command to the server. */
438 if ((out = fdopen (s, "r+")) == NULL)
440 fprintf (stderr, "%s: ", argv[0]);
441 perror ("fdopen");
442 fail (argc, argv);
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)
452 fprintf (stderr, "%s: ", argv[0]);
453 perror ("fdopen");
454 fail (argc, argv);
457 #ifdef HAVE_GETCWD
458 cwd = getcwd (string, sizeof string);
459 #else
460 cwd = getwd (string);
461 #endif
462 if (cwd == 0)
464 /* getwd puts message in STRING if it fails. */
466 #ifdef HAVE_GETCWD
467 fprintf (stderr, "%s: %s (%s)\n", argv[0],
468 "Cannot get current working directory", strerror (errno));
469 #else
470 fprintf (stderr, "%s: %s (%s)\n", argv[0], string, strerror (errno));
471 #endif
472 fail (argc, argv);
475 if (nowait)
476 fprintf (out, "-nowait ");
478 if (eval)
479 fprintf (out, "-eval ");
481 if (display)
483 fprintf (out, "-display ");
484 quote_file_name (display, out);
485 fprintf (out, " ");
488 if ((argc - optind > 0))
490 for (i = optind; i < argc; i++)
492 if (eval)
493 ; /* Don't prepend any cwd or anything like that. */
494 else if (*argv[i] == '+')
496 char *p = argv[i] + 1;
497 while (isdigit ((unsigned char) *p) || *p == ':') p++;
498 if (*p != 0)
500 quote_file_name (cwd, out);
501 fprintf (out, "/");
504 else if (*argv[i] != '/')
506 quote_file_name (cwd, out);
507 fprintf (out, "/");
510 quote_file_name (argv[i], out);
511 fprintf (out, " ");
514 else
516 while ((str = fgets (string, BUFSIZ, stdin)))
518 quote_file_name (str, out);
520 fprintf (out, " ");
523 fprintf (out, "\n");
524 fflush (out);
526 /* Maybe wait for an answer. */
527 if (nowait)
528 return EXIT_SUCCESS;
530 if (!eval)
532 printf ("Waiting for Emacs...");
533 needlf = 2;
535 fflush (stdout);
537 /* Now, wait for an answer and print any messages. */
538 while ((str = fgets (string, BUFSIZ, in)))
540 if (needlf == 2)
541 printf ("\n");
542 printf ("%s", str);
543 needlf = str[0] == '\0' ? needlf : str[strlen (str) - 1] != '\n';
546 if (needlf)
547 printf ("\n");
548 fflush (stdout);
550 return EXIT_SUCCESS;
553 #endif /* HAVE_SOCKETS */
555 #ifndef HAVE_STRERROR
556 char *
557 strerror (errnum)
558 int errnum;
560 extern char *sys_errlist[];
561 extern int sys_nerr;
563 if (errnum >= 0 && errnum < sys_nerr)
564 return sys_errlist[errnum];
565 return (char *) "Unknown error";
568 #endif /* ! HAVE_STRERROR */
570 /* arch-tag: f39bb9c4-73eb-477e-896d-50832e2ca9a7
571 (do not change this comment) */
573 /* emacsclient.c ends here */