1 /* Client process that communicates with GNU Emacs acting as server.
2 Copyright (C) 1986, 1987, 1994, 1999, 2000, 2001, 2003
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 void print_help_and_exit ();
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 { "display", required_argument
, NULL
, 'd' },
83 /* Decode the options from argv and argc.
84 The global variable `optind' will say how many arguments we used up. */
87 decode_options (argc
, argv
)
93 int opt
= getopt_long (argc
, argv
,
94 "VHnea:d:", longopts
, 0);
99 alternate_editor
= getenv ("ALTERNATE_EDITOR");
104 /* If getopt returns 0, then it has already processed a
105 long-named option. We should do nothing. */
109 alternate_editor
= optarg
;
125 printf ("emacsclient %s\n", VERSION
);
130 print_help_and_exit ();
134 fprintf (stderr
, "Try `%s --help' for more information\n", progname
);
142 print_help_and_exit ()
145 "Usage: %s [OPTIONS] FILE...\n\
146 Tell the Emacs server to visit the specified files.\n\
147 Every FILE can be either just a FILENAME or [+LINE[:COLUMN]] FILENAME.\n\
149 The 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\
158 Report bugs to bug-gnu-emacs@gnu.org.\n", progname
);
162 /* Return a copy of NAME, inserting a &
163 before each &, each space, each newline, and any initial -.
164 Change spaces to underscores, too, so that the
165 return value never contains a space. */
168 quote_file_name (name
)
171 char *copy
= (char *) malloc (strlen (name
) * 2 + 1);
192 if (*p
== '&' || (*p
== '-' && p
== name
))
202 /* Like malloc but get fatal error if memory is exhausted. */
208 long *result
= (long *) malloc (size
);
218 Try to run a different command, or --if no alternate editor is
219 defined-- exit with an errorcode.
226 if (alternate_editor
)
229 execvp (alternate_editor
, argv
+ i
);
240 #if !defined (HAVE_SOCKETS) || defined (NO_SOCKETS_IN_FILE_SYSTEM)
247 fprintf (stderr
, "%s: Sorry, the Emacs server is supported only\n",
249 fprintf (stderr
, "on systems with Berkeley sockets.\n");
254 #else /* HAVE_SOCKETS */
256 #include <sys/types.h>
257 #include <sys/socket.h>
259 #include <sys/stat.h>
262 extern char *strerror ();
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 */
271 socket_status (socket_name
)
276 if (stat (socket_name
, &statbfr
) == -1)
279 if (statbfr
.st_uid
!= geteuid ())
291 int system_name_length
;
292 int s
, i
, needlf
= 0;
294 struct sockaddr_un server
;
300 /* Process options. */
301 decode_options (argc
, argv
);
303 if (argc
- optind
< 1)
305 fprintf (stderr
, "%s: file name or argument required\n", progname
);
306 fprintf (stderr
, "Try `%s --help' for more information\n", progname
);
311 * Open up an AF_UNIX socket in this person's home directory
314 if ((s
= socket (AF_UNIX
, SOCK_STREAM
, 0)) < 0)
316 fprintf (stderr
, "%s: ", argv
[0]);
321 server
.sun_family
= AF_UNIX
;
325 system_name_length
= 32;
329 system_name
= (char *) xmalloc (system_name_length
+ 1);
331 /* system_name must be null-terminated string. */
332 system_name
[system_name_length
] = '\0';
334 if (gethostname (system_name
, system_name_length
) == 0)
338 system_name_length
*= 2;
341 /* We always use the non-dotted host name, for simplicity. */
342 dot
= index (system_name
, '.');
350 sprintf (server
.sun_path
, "/tmp/emacs%d-%s/server", (int) geteuid (), system_name
);
352 /* See if the socket exists, and if it's owned by us. */
353 sock_status
= socket_status (server
.sun_path
);
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. */
361 char *user_name
= (char *) getenv ("LOGNAME");
363 user_name
= (char *) getenv ("USER");
367 struct passwd
*pw
= getpwnam (user_name
);
368 if (pw
&& (pw
->pw_uid
!= geteuid ()))
370 /* We're running under su, apparently. */
371 sprintf (server
.sun_path
, "/tmp/esrv%d-%s",
372 (int) pw
->pw_uid
, system_name
);
373 sock_status
= socket_status (server
.sun_path
);
381 /* There's a socket, but it isn't owned by us. This is OK if
385 fprintf (stderr
, "%s: Invalid socket owner\n", argv
[0]);
394 "%s: can't find socket; have you started the server?\n\
395 To start the server in Emacs, type \"M-x server-start\".\n",
398 fprintf (stderr
, "%s: can't stat %s: %s\n",
399 argv
[0], server
.sun_path
, strerror (errno
));
405 if (connect (s
, (struct sockaddr
*) &server
, strlen (server
.sun_path
) + 2)
408 fprintf (stderr
, "%s: ", argv
[0]);
413 /* We use the stream OUT to send our command to the server. */
414 if ((out
= fdopen (s
, "r+")) == NULL
)
416 fprintf (stderr
, "%s: ", argv
[0]);
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
)
428 fprintf (stderr
, "%s: ", argv
[0]);
434 cwd
= getcwd (string
, sizeof string
);
436 cwd
= getwd (string
);
440 /* getwd puts message in STRING if it fails. */
441 fprintf (stderr
, "%s: %s (%s)\n", argv
[0],
443 "Cannot get current working directory",
452 fprintf (out
, "-nowait ");
455 fprintf (out
, "-eval ");
458 fprintf (out
, "-display %s ", quote_file_name (display
));
460 for (i
= optind
; i
< argc
; i
++)
463 ; /* Don't prepend any cwd or anything like that. */
464 else if (*argv
[i
] == '+')
466 char *p
= argv
[i
] + 1;
467 while (isdigit ((unsigned char) *p
) || *p
== ':') p
++;
469 fprintf (out
, "%s/", quote_file_name (cwd
));
471 else if (*argv
[i
] != '/')
472 fprintf (out
, "%s/", quote_file_name (cwd
));
474 fprintf (out
, "%s ", quote_file_name (argv
[i
]));
479 /* Maybe wait for an answer. */
485 printf ("Waiting for Emacs...");
490 /* Now, wait for an answer and print any messages. */
491 while ((str
= fgets (string
, BUFSIZ
, in
)))
496 needlf
= str
[0] == '\0' ? needlf
: str
[strlen (str
) - 1] != '\n';
506 #endif /* HAVE_SOCKETS */
508 #ifndef HAVE_STRERROR
513 extern char *sys_errlist
[];
516 if (errnum
>= 0 && errnum
< sys_nerr
)
517 return sys_errlist
[errnum
];
518 return (char *) "Unknown error";
521 #endif /* ! HAVE_STRERROR */