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. */
44 char *getenv (), *getwd ();
47 /* This is defined with -D from the compilation command,
48 which extracts it from ../lisp/version.el. */
51 #define VERSION "unspecified"
54 /* Name used to invoke this program. */
57 /* Nonzero means don't wait for a response from Emacs. --no-wait. */
60 /* Nonzero means args are expressions to be evaluated. --eval. */
63 /* The display on which Emacs should work. --display. */
66 /* If non-NULL, the name of an editor to fallback to if the server
67 is not running. --alternate-editor. */
68 const char * alternate_editor
= NULL
;
70 /* If non-NULL, the filename of the UNIX socket. */
71 char *socket_name
= NULL
;
73 void print_help_and_exit ();
75 struct option longopts
[] =
77 { "no-wait", no_argument
, NULL
, 'n' },
78 { "eval", no_argument
, NULL
, 'e' },
79 { "help", no_argument
, NULL
, 'H' },
80 { "version", no_argument
, NULL
, 'V' },
81 { "alternate-editor", required_argument
, NULL
, 'a' },
82 { "socket-name", required_argument
, NULL
, 's' },
83 { "display", required_argument
, NULL
, 'd' },
87 /* Decode the options from argv and argc.
88 The global variable `optind' will say how many arguments we used up. */
91 decode_options (argc
, argv
)
95 alternate_editor
= getenv ("ALTERNATE_EDITOR");
99 int opt
= getopt_long (argc
, argv
,
100 "VHnea:s:d:", longopts
, 0);
108 /* If getopt returns 0, then it has already processed a
109 long-named option. We should do nothing. */
113 alternate_editor
= optarg
;
117 socket_name
= optarg
;
133 printf ("emacsclient %s\n", VERSION
);
138 print_help_and_exit ();
142 fprintf (stderr
, "Try `%s --help' for more information\n", progname
);
150 print_help_and_exit ()
153 "Usage: %s [OPTIONS] FILE...\n\
154 Tell the Emacs server to visit the specified files.\n\
155 Every FILE can be either just a FILENAME or [+LINE[:COLUMN]] FILENAME.\n\
157 The following OPTIONS are accepted:\n\
158 -V, --version Just print a version info and return\n\
159 -H, --help Print this usage information message\n\
160 -n, --no-wait Don't wait for the server to return\n\
161 -e, --eval Evaluate the FILE arguments as ELisp expressions\n\
162 -d, --display=DISPLAY Visit the file in the given display\n\
163 -s, --socket-name=FILENAME\n\
164 Set the filename of the UNIX socket for communication\n\
165 -a, --alternate-editor=EDITOR\n\
166 Editor to fallback to if the server is not running\n\
168 Report bugs to bug-gnu-emacs@gnu.org.\n", progname
);
172 /* In NAME, insert a & before each &, each space, each newline, and
173 any initial -. Change spaces to underscores, too, so that the
174 return value never contains a space. */
177 quote_file_name (name
, stream
)
181 char *copy
= (char *) malloc (strlen (name
) * 2 + 1);
202 if (*p
== '&' || (*p
== '-' && p
== name
))
209 fprintf (stream
, copy
);
214 /* Like malloc but get fatal error if memory is exhausted. */
220 long *result
= (long *) malloc (size
);
230 Try to run a different command, or --if no alternate editor is
231 defined-- exit with an errorcode.
238 if (alternate_editor
)
241 execvp (alternate_editor
, argv
+ i
);
252 #if !defined (HAVE_SOCKETS) || defined (NO_SOCKETS_IN_FILE_SYSTEM)
259 fprintf (stderr
, "%s: Sorry, the Emacs server is supported only\n",
261 fprintf (stderr
, "on systems with Berkeley sockets.\n");
266 #else /* HAVE_SOCKETS */
268 #include <sys/types.h>
269 #include <sys/socket.h>
271 #include <sys/stat.h>
274 extern char *strerror ();
277 /* Three possibilities:
278 2 - can't be `stat'ed (sets errno)
279 1 - isn't owned by us
280 0 - success: none of the above */
283 socket_status (socket_name
)
288 if (stat (socket_name
, &statbfr
) == -1)
291 if (statbfr
.st_uid
!= geteuid ())
302 int s
, i
, needlf
= 0;
304 struct sockaddr_un server
;
310 /* Process options. */
311 decode_options (argc
, argv
);
313 if ((argc
- optind
< 1) && !eval
)
315 fprintf (stderr
, "%s: file name or argument required\n", progname
);
316 fprintf (stderr
, "Try `%s --help' for more information\n", progname
);
321 * Open up an AF_UNIX socket in this person's home directory
324 if ((s
= socket (AF_UNIX
, SOCK_STREAM
, 0)) < 0)
326 fprintf (stderr
, "%s: ", argv
[0]);
331 server
.sun_family
= AF_UNIX
;
335 int default_sock
= !socket_name
;
337 char *server_name
= "server";
339 if (socket_name
&& !index (socket_name
, '/') && !index (socket_name
, '\\'))
340 { /* socket_name is a file name component. */
341 server_name
= socket_name
;
343 default_sock
= 1; /* Try both UIDs. */
348 socket_name
= alloca (100 + strlen (server_name
));
349 sprintf (socket_name
, "/tmp/emacs%d/%s",
350 (int) geteuid (), server_name
);
353 if (strlen (socket_name
) < sizeof (server
.sun_path
))
354 strcpy (server
.sun_path
, socket_name
);
357 fprintf (stderr
, "%s: socket-name %s too long",
358 argv
[0], socket_name
);
362 /* See if the socket exists, and if it's owned by us. */
363 sock_status
= socket_status (server
.sun_path
);
365 if (sock_status
&& default_sock
)
367 /* Failing that, see if LOGNAME or USER exist and differ from
368 our euid. If so, look for a socket based on the UID
369 associated with the name. This is reminiscent of the logic
370 that init_editfns uses to set the global Vuser_full_name. */
372 char *user_name
= (char *) getenv ("LOGNAME");
375 user_name
= (char *) getenv ("USER");
379 struct passwd
*pw
= getpwnam (user_name
);
381 if (pw
&& (pw
->pw_uid
!= geteuid ()))
383 /* We're running under su, apparently. */
384 socket_name
= alloca (100 + strlen (server_name
));
385 sprintf (socket_name
, "/tmp/emacs%d/%s",
386 (int) pw
->pw_uid
, server_name
);
388 if (strlen (socket_name
) < sizeof (server
.sun_path
))
389 strcpy (server
.sun_path
, socket_name
);
392 fprintf (stderr
, "%s: socket-name %s too long",
393 argv
[0], socket_name
);
397 sock_status
= socket_status (server
.sun_path
);
408 /* There's a socket, but it isn't owned by us. This is OK if
412 fprintf (stderr
, "%s: Invalid socket owner\n", argv
[0]);
419 if (saved_errno
== ENOENT
)
421 "%s: can't find socket; have you started the server?\n\
422 To start the server in Emacs, type \"M-x server-start\".\n",
425 fprintf (stderr
, "%s: can't stat %s: %s\n",
426 argv
[0], server
.sun_path
, strerror (saved_errno
));
432 if (connect (s
, (struct sockaddr
*) &server
, strlen (server
.sun_path
) + 2)
435 fprintf (stderr
, "%s: ", argv
[0]);
440 /* We use the stream OUT to send our command to the server. */
441 if ((out
= fdopen (s
, "r+")) == NULL
)
443 fprintf (stderr
, "%s: ", argv
[0]);
448 /* We use the stream IN to read the response.
449 We used to use just one stream for both output and input
450 on the socket, but reversing direction works nonportably:
451 on some systems, the output appears as the first input;
452 on other systems it does not. */
453 if ((in
= fdopen (s
, "r+")) == NULL
)
455 fprintf (stderr
, "%s: ", argv
[0]);
461 cwd
= getcwd (string
, sizeof string
);
463 cwd
= getwd (string
);
467 /* getwd puts message in STRING if it fails. */
470 fprintf (stderr
, "%s: %s (%s)\n", argv
[0],
471 "Cannot get current working directory", strerror (errno
));
473 fprintf (stderr
, "%s: %s (%s)\n", argv
[0], string
, strerror (errno
));
479 fprintf (out
, "-nowait ");
482 fprintf (out
, "-eval ");
486 fprintf (out
, "-display ");
487 quote_file_name (display
, out
);
491 if ((argc
- optind
> 0))
493 for (i
= optind
; i
< argc
; i
++)
496 ; /* Don't prepend any cwd or anything like that. */
497 else if (*argv
[i
] == '+')
499 char *p
= argv
[i
] + 1;
500 while (isdigit ((unsigned char) *p
) || *p
== ':') p
++;
503 quote_file_name (cwd
, out
);
507 else if (*argv
[i
] != '/')
509 quote_file_name (cwd
, out
);
513 quote_file_name (argv
[i
], out
);
519 while ((str
= fgets (string
, BUFSIZ
, stdin
)))
521 quote_file_name (str
, out
);
529 /* Maybe wait for an answer. */
535 printf ("Waiting for Emacs...");
540 /* Now, wait for an answer and print any messages. */
541 while ((str
= fgets (string
, BUFSIZ
, in
)))
546 needlf
= str
[0] == '\0' ? needlf
: str
[strlen (str
) - 1] != '\n';
556 #endif /* HAVE_SOCKETS */
558 #ifndef HAVE_STRERROR
563 extern char *sys_errlist
[];
566 if (errnum
>= 0 && errnum
< sys_nerr
)
567 return sys_errlist
[errnum
];
568 return (char *) "Unknown error";
571 #endif /* ! HAVE_STRERROR */
573 /* arch-tag: f39bb9c4-73eb-477e-896d-50832e2ca9a7
574 (do not change this comment) */