]> code.delx.au - gnu-emacs/blob - lib-src/emacsclient.c
Remove SYSV support.
[gnu-emacs] / lib-src / emacsclient.c
1 /* Client process that communicates with GNU Emacs acting as server.
2 Copyright (C) 1986, 1987, 1994, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
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.
11
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.
16
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. */
21
22
23 #define NO_SHORTNAMES
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #undef signal
30
31 #include <ctype.h>
32 #include <stdio.h>
33 #include <getopt.h>
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 #ifdef VMS
39 # include "vms-pwd.h"
40 #else
41 # include <pwd.h>
42 #endif /* not VMS */
43
44 char *getenv (), *getwd ();
45 char *getcwd ();
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. */
55 char *progname;
56
57 /* Nonzero means don't wait for a response from Emacs. --no-wait. */
58 int nowait = 0;
59
60 /* Nonzero means args are expressions to be evaluated. --eval. */
61 int eval = 0;
62
63 /* The display on which Emacs should work. --display. */
64 char *display = NULL;
65
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;
69
70 void print_help_and_exit ();
71
72 struct option longopts[] =
73 {
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' },
80 { 0, 0, 0, 0 }
81 };
82
83 /* Decode the options from argv and argc.
84 The global variable `optind' will say how many arguments we used up. */
85
86 void
87 decode_options (argc, argv)
88 int argc;
89 char **argv;
90 {
91 while (1)
92 {
93 int opt = getopt_long (argc, argv,
94 "VHnea:d:", longopts, 0);
95
96 if (opt == EOF)
97 break;
98
99 alternate_editor = getenv ("ALTERNATE_EDITOR");
100
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;
107
108 case 'a':
109 alternate_editor = optarg;
110 break;
111
112 case 'd':
113 display = optarg;
114 break;
115
116 case 'n':
117 nowait = 1;
118 break;
119
120 case 'e':
121 eval = 1;
122 break;
123
124 case 'V':
125 fprintf (stderr, "emacsclient %s\n", VERSION);
126 exit (1);
127 break;
128
129 case 'H':
130 default:
131 print_help_and_exit ();
132 }
133 }
134 }
135
136 void
137 print_help_and_exit ()
138 {
139 fprintf (stderr,
140 "Usage: %s [OPTIONS] FILE...\n\
141 Tell the Emacs server to visit the specified files.\n\
142 Every FILE can be either just a FILENAME or [+LINE[:COLUMN]] FILENAME.\n\
143 The following OPTIONS are accepted:\n\
144 -V, --version Just print a version info and return\n\
145 -H, --help Print this usage information message\n\
146 -n, --no-wait Don't wait for the server to return\n\
147 -e, --eval Evaluate the FILE arguments as ELisp expressions\n\
148 -d, --display=DISPLAY Visit the file in the given display\n\
149 -a, --alternate-editor=EDITOR\n\
150 Editor to fallback to if the server is not running\n\
151 Report bugs to bug-gnu-emacs@gnu.org.\n", progname);
152 exit (1);
153 }
154
155 /* Return a copy of NAME, inserting a &
156 before each &, each space, each newline, and any initial -.
157 Change spaces to underscores, too, so that the
158 return value never contains a space. */
159
160 char *
161 quote_file_name (name)
162 char *name;
163 {
164 char *copy = (char *) malloc (strlen (name) * 2 + 1);
165 char *p, *q;
166
167 p = name;
168 q = copy;
169 while (*p)
170 {
171 if (*p == ' ')
172 {
173 *q++ = '&';
174 *q++ = '_';
175 p++;
176 }
177 else if (*p == '\n')
178 {
179 *q++ = '&';
180 *q++ = 'n';
181 p++;
182 }
183 else
184 {
185 if (*p == '&' || (*p == '-' && p == name))
186 *q++ = '&';
187 *q++ = *p++;
188 }
189 }
190 *q++ = 0;
191
192 return copy;
193 }
194
195 /* Like malloc but get fatal error if memory is exhausted. */
196
197 long *
198 xmalloc (size)
199 unsigned int size;
200 {
201 long *result = (long *) malloc (size);
202 if (result == NULL)
203 {
204 perror ("malloc");
205 exit (1);
206 }
207 return result;
208 }
209 \f
210 /*
211 Try to run a different command, or --if no alternate editor is
212 defined-- exit with an errorcode.
213 */
214 void
215 fail (argc, argv)
216 int argc;
217 char **argv;
218 {
219 if (alternate_editor)
220 {
221 int i = optind - 1;
222 execvp (alternate_editor, argv + i);
223 return;
224 }
225 else
226 {
227 exit (1);
228 }
229 }
230
231
232 \f
233 #if !defined (HAVE_SOCKETS) || defined (NO_SOCKETS_IN_FILE_SYSTEM)
234
235 int
236 main (argc, argv)
237 int argc;
238 char **argv;
239 {
240 fprintf (stderr, "%s: Sorry, the Emacs server is supported only\n",
241 argv[0]);
242 fprintf (stderr, "on systems with Berkeley sockets.\n");
243
244 fail (argc, argv);
245 }
246
247 #else /* HAVE_SOCKETS */
248
249 #include <sys/types.h>
250 #include <sys/socket.h>
251 #include <sys/un.h>
252 #include <sys/stat.h>
253 #include <errno.h>
254
255 extern char *strerror ();
256 extern int errno;
257
258 /* Three possibilities:
259 2 - can't be `stat'ed (sets errno)
260 1 - isn't owned by us
261 0 - success: none of the above */
262
263 static int
264 socket_status (socket_name)
265 char *socket_name;
266 {
267 struct stat statbfr;
268
269 if (stat (socket_name, &statbfr) == -1)
270 return 2;
271
272 if (statbfr.st_uid != geteuid ())
273 return 1;
274
275 return 0;
276 }
277
278 int
279 main (argc, argv)
280 int argc;
281 char **argv;
282 {
283 char *system_name;
284 int system_name_length;
285 int s, i, needlf = 0;
286 FILE *out, *in;
287 struct sockaddr_un server;
288 #ifdef SERVER_HOME_DIR
289 char *homedir;
290 #endif
291 char *cwd, *str;
292 char string[BUFSIZ];
293
294 progname = argv[0];
295
296 /* Process options. */
297 decode_options (argc, argv);
298
299 if (argc - optind < 1)
300 print_help_and_exit ();
301
302 /*
303 * Open up an AF_UNIX socket in this person's home directory
304 */
305
306 if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
307 {
308 fprintf (stderr, "%s: ", argv[0]);
309 perror ("socket");
310 fail (argc, argv);
311 }
312
313 server.sun_family = AF_UNIX;
314
315 {
316 system_name_length = 32;
317
318 while (1)
319 {
320 system_name = (char *) xmalloc (system_name_length + 1);
321
322 /* system_name must be null-terminated string. */
323 system_name[system_name_length] = '\0';
324
325 if (gethostname (system_name, system_name_length) == 0)
326 break;
327
328 free (system_name);
329 system_name_length *= 2;
330 }
331 }
332
333 #ifndef SERVER_HOME_DIR
334 {
335 int sock_status = 0;
336
337 sprintf (server.sun_path, "/tmp/esrv%d-%s", (int) geteuid (), system_name);
338
339 /* See if the socket exists, and if it's owned by us. */
340 sock_status = socket_status (server.sun_path);
341 if (sock_status)
342 {
343 /* Failing that, see if LOGNAME or USER exist and differ from
344 our euid. If so, look for a socket based on the UID
345 associated with the name. This is reminiscent of the logic
346 that init_editfns uses to set the global Vuser_full_name. */
347
348 char *user_name = (char *) getenv ("LOGNAME");
349 if (!user_name)
350 user_name = (char *) getenv ("USER");
351
352 if (user_name)
353 {
354 struct passwd *pw = getpwnam (user_name);
355 if (pw && (pw->pw_uid != geteuid ()))
356 {
357 /* We're running under su, apparently. */
358 sprintf (server.sun_path, "/tmp/esrv%d-%s",
359 (int) pw->pw_uid, system_name);
360 sock_status = socket_status (server.sun_path);
361 }
362 }
363 }
364
365 switch (sock_status)
366 {
367 case 1:
368 /* There's a socket, but it isn't owned by us. This is OK if
369 we are root. */
370 if (0 != geteuid ())
371 {
372 fprintf (stderr, "%s: Invalid socket owner\n", argv[0]);
373 fail (argc, argv);
374 }
375 break;
376
377 case 2:
378 /* `stat' failed */
379 if (errno == ENOENT)
380 fprintf (stderr,
381 "%s: can't find socket; have you started the server?\n",
382 argv[0]);
383 else
384 fprintf (stderr, "%s: can't stat %s: %s\n",
385 argv[0], server.sun_path, strerror (errno));
386 fail (argc, argv);
387 break;
388 }
389 }
390 #else
391 if ((homedir = getenv ("HOME")) == NULL)
392 {
393 fprintf (stderr, "%s: No home directory\n", argv[0]);
394 fail (argc, argv);
395 }
396 strcpy (server.sun_path, homedir);
397 strcat (server.sun_path, "/.emacs-server-");
398 strcat (server.sun_path, system_name);
399 #endif
400
401 if (connect (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2)
402 < 0)
403 {
404 fprintf (stderr, "%s: ", argv[0]);
405 perror ("connect");
406 fail (argc, argv);
407 }
408
409 /* We use the stream OUT to send our command to the server. */
410 if ((out = fdopen (s, "r+")) == NULL)
411 {
412 fprintf (stderr, "%s: ", argv[0]);
413 perror ("fdopen");
414 fail (argc, argv);
415 }
416
417 /* We use the stream IN to read the response.
418 We used to use just one stream for both output and input
419 on the socket, but reversing direction works nonportably:
420 on some systems, the output appears as the first input;
421 on other systems it does not. */
422 if ((in = fdopen (s, "r+")) == NULL)
423 {
424 fprintf (stderr, "%s: ", argv[0]);
425 perror ("fdopen");
426 fail (argc, argv);
427 }
428
429 #ifdef BSD_SYSTEM
430 cwd = getwd (string);
431 #else
432 cwd = getcwd (string, sizeof string);
433 #endif
434 if (cwd == 0)
435 {
436 /* getwd puts message in STRING if it fails. */
437 fprintf (stderr, "%s: %s (%s)\n", argv[0],
438 #ifdef BSD_SYSTEM
439 string,
440 #else
441 "Cannot get current working directory",
442 #endif
443 strerror (errno));
444 fail (argc, argv);
445 }
446
447 if (nowait)
448 fprintf (out, "-nowait ");
449
450 if (eval)
451 fprintf (out, "-eval ");
452
453 if (display)
454 fprintf (out, "-display %s ", quote_file_name (display));
455
456 for (i = optind; i < argc; i++)
457 {
458 if (eval)
459 ; /* Don't prepend any cwd or anything like that. */
460 else if (*argv[i] == '+')
461 {
462 char *p = argv[i] + 1;
463 while (isdigit ((unsigned char) *p) || *p == ':') p++;
464 if (*p != 0)
465 fprintf (out, "%s/", quote_file_name (cwd));
466 }
467 else if (*argv[i] != '/')
468 fprintf (out, "%s/", quote_file_name (cwd));
469
470 fprintf (out, "%s ", quote_file_name (argv[i]));
471 }
472 fprintf (out, "\n");
473 fflush (out);
474
475 /* Maybe wait for an answer. */
476 if (nowait)
477 return 0;
478
479 if (!eval)
480 {
481 printf ("Waiting for Emacs...");
482 needlf = 2;
483 }
484 fflush (stdout);
485
486 /* Now, wait for an answer and print any messages. */
487 while ((str = fgets (string, BUFSIZ, in)))
488 {
489 if (needlf == 2)
490 printf ("\n");
491 printf ("%s", str);
492 needlf = str[0] == '\0' ? needlf : str[strlen (str) - 1] != '\n';
493 }
494
495 if (needlf)
496 printf ("\n");
497 fflush (stdout);
498
499 return 0;
500 }
501
502 #endif /* HAVE_SOCKETS */
503 \f
504 #ifndef HAVE_STRERROR
505 char *
506 strerror (errnum)
507 int errnum;
508 {
509 extern char *sys_errlist[];
510 extern int sys_nerr;
511
512 if (errnum >= 0 && errnum < sys_nerr)
513 return sys_errlist[errnum];
514 return (char *) "Unknown error";
515 }
516
517 #endif /* ! HAVE_STRERROR */