Commit | Line | Data |
---|---|---|
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 | |
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 | |
92af894f | 9 | the Free Software Foundation; either version 2, or (at your option) |
46cec291 RS |
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 | |
3b7ad313 EN |
19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
20 | Boston, 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 |
44 | char *getenv (), *getwd (); |
45 | char *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. */ | |
55 | char *progname; | |
56 | ||
749ae770 | 57 | /* Nonzero means don't wait for a response from Emacs. --no-wait. */ |
8f9aaa0a RS |
58 | int nowait = 0; |
59 | ||
30be2360 SM |
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 | ||
3db926be | 70 | /* If non-NULL, the filename of the UNIX socket. */ |
254107e4 RS |
71 | char *socket_name = NULL; |
72 | ||
7f3bff3e AS |
73 | void print_help_and_exit (); |
74 | ||
8f9aaa0a RS |
75 | struct option longopts[] = |
76 | { | |
749ae770 | 77 | { "no-wait", no_argument, NULL, 'n' }, |
30be2360 | 78 | { "eval", no_argument, NULL, 'e' }, |
8f9aaa0a RS |
79 | { "help", no_argument, NULL, 'H' }, |
80 | { "version", no_argument, NULL, 'V' }, | |
3cf8c6aa | 81 | { "alternate-editor", required_argument, NULL, 'a' }, |
254107e4 | 82 | { "socket-name", required_argument, NULL, 's' }, |
30be2360 SM |
83 | { "display", required_argument, NULL, 'd' }, |
84 | { 0, 0, 0, 0 } | |
8f9aaa0a RS |
85 | }; |
86 | ||
87 | /* Decode the options from argv and argc. | |
5212210c | 88 | The global variable `optind' will say how many arguments we used up. */ |
8f9aaa0a | 89 | |
5212210c | 90 | void |
8f9aaa0a RS |
91 | decode_options (argc, argv) |
92 | int argc; | |
93 | char **argv; | |
94 | { | |
95 | while (1) | |
96 | { | |
97 | int opt = getopt_long (argc, argv, | |
254107e4 | 98 | "VHnea:s:d:", longopts, 0); |
8f9aaa0a RS |
99 | |
100 | if (opt == EOF) | |
101 | break; | |
102 | ||
97e3214d | 103 | alternate_editor = getenv ("ALTERNATE_EDITOR"); |
e69233c2 | 104 | |
8f9aaa0a RS |
105 | switch (opt) |
106 | { | |
107 | case 0: | |
108 | /* If getopt returns 0, then it has already processed a | |
109 | long-named option. We should do nothing. */ | |
110 | break; | |
e69233c2 | 111 | |
97e3214d GM |
112 | case 'a': |
113 | alternate_editor = optarg; | |
114 | break; | |
e69233c2 | 115 | |
254107e4 RS |
116 | case 's': |
117 | socket_name = optarg; | |
118 | break; | |
119 | ||
30be2360 SM |
120 | case 'd': |
121 | display = optarg; | |
122 | break; | |
123 | ||
8f9aaa0a RS |
124 | case 'n': |
125 | nowait = 1; | |
126 | break; | |
127 | ||
30be2360 SM |
128 | case 'e': |
129 | eval = 1; | |
130 | break; | |
131 | ||
8f9aaa0a | 132 | case 'V': |
20c396e8 JB |
133 | printf ("emacsclient %s\n", VERSION); |
134 | exit (0); | |
8f9aaa0a | 135 | break; |
46cec291 | 136 | |
8f9aaa0a | 137 | case 'H': |
8f9aaa0a | 138 | print_help_and_exit (); |
20c396e8 JB |
139 | break; |
140 | ||
141 | default: | |
142 | fprintf (stderr, "Try `%s --help' for more information\n", progname); | |
143 | exit (1); | |
144 | break; | |
8f9aaa0a RS |
145 | } |
146 | } | |
8f9aaa0a RS |
147 | } |
148 | ||
7f3bff3e | 149 | void |
8f9aaa0a RS |
150 | print_help_and_exit () |
151 | { | |
20c396e8 JB |
152 | printf ( |
153 | "Usage: %s [OPTIONS] FILE...\n\ | |
30be2360 SM |
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\ | |
20c396e8 | 156 | \n\ |
30be2360 SM |
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\ | |
254107e4 RS |
163 | -s, --socket-name=FILENAME\n\ |
164 | Set the filename of the UNIX socket for communication\n\ | |
30be2360 SM |
165 | -a, --alternate-editor=EDITOR\n\ |
166 | Editor to fallback to if the server is not running\n\ | |
20c396e8 | 167 | \n\ |
30be2360 | 168 | Report bugs to bug-gnu-emacs@gnu.org.\n", progname); |
20c396e8 | 169 | exit (0); |
8f9aaa0a | 170 | } |
5212210c | 171 | |
a4cf2096 RS |
172 | /* In NAME, insert a & before each &, each space, each newline, and |
173 | any initial -. Change spaces to underscores, too, so that the | |
5212210c RS |
174 | return value never contains a space. */ |
175 | ||
87209357 EZ |
176 | void |
177 | quote_file_name (name, stream) | |
5212210c | 178 | char *name; |
87209357 | 179 | FILE *stream; |
5212210c RS |
180 | { |
181 | char *copy = (char *) malloc (strlen (name) * 2 + 1); | |
182 | char *p, *q; | |
183 | ||
184 | p = name; | |
185 | q = copy; | |
186 | while (*p) | |
187 | { | |
188 | if (*p == ' ') | |
189 | { | |
f1db6a73 | 190 | *q++ = '&'; |
5212210c RS |
191 | *q++ = '_'; |
192 | p++; | |
193 | } | |
3cf8c6aa SM |
194 | else if (*p == '\n') |
195 | { | |
196 | *q++ = '&'; | |
197 | *q++ = 'n'; | |
198 | p++; | |
199 | } | |
5212210c RS |
200 | else |
201 | { | |
f1db6a73 RS |
202 | if (*p == '&' || (*p == '-' && p == name)) |
203 | *q++ = '&'; | |
5212210c RS |
204 | *q++ = *p++; |
205 | } | |
206 | } | |
f1db6a73 | 207 | *q++ = 0; |
5212210c | 208 | |
87209357 EZ |
209 | fprintf (stream, copy); |
210 | ||
211 | free (copy); | |
5212210c | 212 | } |
0c76956f | 213 | |
0c76956f RS |
214 | /* Like malloc but get fatal error if memory is exhausted. */ |
215 | ||
5497dfdb | 216 | long * |
0c76956f RS |
217 | xmalloc (size) |
218 | unsigned int size; | |
219 | { | |
5f32a712 | 220 | long *result = (long *) malloc (size); |
0c76956f RS |
221 | if (result == NULL) |
222 | { | |
223 | perror ("malloc"); | |
224 | exit (1); | |
225 | } | |
226 | return result; | |
227 | } | |
8f9aaa0a | 228 | \f |
97e3214d GM |
229 | /* |
230 | Try to run a different command, or --if no alternate editor is | |
231 | defined-- exit with an errorcode. | |
232 | */ | |
de073ce3 | 233 | void |
97e3214d GM |
234 | fail (argc, argv) |
235 | int argc; | |
236 | char **argv; | |
237 | { | |
238 | if (alternate_editor) | |
239 | { | |
3cf8c6aa | 240 | int i = optind - 1; |
97e3214d | 241 | execvp (alternate_editor, argv + i); |
23431241 | 242 | return; |
97e3214d GM |
243 | } |
244 | else | |
245 | { | |
246 | exit (1); | |
247 | } | |
248 | } | |
249 | ||
250 | ||
97e3214d | 251 | \f |
30be2360 | 252 | #if !defined (HAVE_SOCKETS) || defined (NO_SOCKETS_IN_FILE_SYSTEM) |
46cec291 | 253 | |
de073ce3 | 254 | int |
46cec291 RS |
255 | main (argc, argv) |
256 | int argc; | |
257 | char **argv; | |
258 | { | |
259 | fprintf (stderr, "%s: Sorry, the Emacs server is supported only\n", | |
260 | argv[0]); | |
30be2360 | 261 | fprintf (stderr, "on systems with Berkeley sockets.\n"); |
97e3214d GM |
262 | |
263 | fail (argc, argv); | |
46cec291 RS |
264 | } |
265 | ||
30be2360 | 266 | #else /* HAVE_SOCKETS */ |
46cec291 RS |
267 | |
268 | #include <sys/types.h> | |
269 | #include <sys/socket.h> | |
270 | #include <sys/un.h> | |
efb859b4 | 271 | #include <sys/stat.h> |
46cec291 RS |
272 | #include <errno.h> |
273 | ||
92af894f | 274 | extern char *strerror (); |
46cec291 RS |
275 | extern int errno; |
276 | ||
9f637eea GM |
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 */ | |
281 | ||
282 | static int | |
283 | socket_status (socket_name) | |
284 | char *socket_name; | |
285 | { | |
286 | struct stat statbfr; | |
287 | ||
288 | if (stat (socket_name, &statbfr) == -1) | |
289 | return 2; | |
290 | ||
291 | if (statbfr.st_uid != geteuid ()) | |
292 | return 1; | |
293 | ||
294 | return 0; | |
295 | } | |
296 | ||
340ff9de | 297 | int |
46cec291 RS |
298 | main (argc, argv) |
299 | int argc; | |
300 | char **argv; | |
301 | { | |
a2b3f2b5 RS |
302 | char *system_name; |
303 | int system_name_length; | |
3cf8c6aa | 304 | int s, i, needlf = 0; |
23a7488d | 305 | FILE *out, *in; |
46cec291 | 306 | struct sockaddr_un server; |
571512de | 307 | char *cwd, *str; |
46cec291 | 308 | char string[BUFSIZ]; |
8f9aaa0a RS |
309 | |
310 | progname = argv[0]; | |
46cec291 | 311 | |
8f9aaa0a | 312 | /* Process options. */ |
5212210c | 313 | decode_options (argc, argv); |
46cec291 | 314 | |
87209357 | 315 | if ((argc - optind < 1) && !eval) |
20c396e8 JB |
316 | { |
317 | fprintf (stderr, "%s: file name or argument required\n", progname); | |
318 | fprintf (stderr, "Try `%s --help' for more information\n", progname); | |
319 | exit (1); | |
320 | } | |
46cec291 | 321 | |
e69233c2 | 322 | /* |
46cec291 RS |
323 | * Open up an AF_UNIX socket in this person's home directory |
324 | */ | |
325 | ||
326 | if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) | |
327 | { | |
328 | fprintf (stderr, "%s: ", argv[0]); | |
329 | perror ("socket"); | |
97e3214d | 330 | fail (argc, argv); |
46cec291 | 331 | } |
e69233c2 | 332 | |
46cec291 | 333 | server.sun_family = AF_UNIX; |
c5fee545 | 334 | |
efb859b4 | 335 | { |
3ecdcd59 | 336 | char *dot; |
a2b3f2b5 RS |
337 | system_name_length = 32; |
338 | ||
339 | while (1) | |
340 | { | |
341 | system_name = (char *) xmalloc (system_name_length + 1); | |
342 | ||
343 | /* system_name must be null-terminated string. */ | |
344 | system_name[system_name_length] = '\0'; | |
345 | ||
346 | if (gethostname (system_name, system_name_length) == 0) | |
347 | break; | |
348 | ||
349 | free (system_name); | |
350 | system_name_length *= 2; | |
351 | } | |
3ecdcd59 SM |
352 | |
353 | /* We always use the non-dotted host name, for simplicity. */ | |
354 | dot = index (system_name, '.'); | |
355 | if (dot) | |
356 | *dot = '\0'; | |
c5fee545 KH |
357 | } |
358 | ||
c5fee545 | 359 | { |
9f637eea | 360 | int sock_status = 0; |
efb859b4 | 361 | |
254107e4 RS |
362 | if (! socket_name) |
363 | { | |
364 | socket_name = alloca (system_name_length + 100); | |
365 | sprintf (socket_name, "/tmp/emacs%d-%s/server", | |
366 | (int) geteuid (), system_name); | |
367 | } | |
368 | ||
369 | if (strlen (socket_name) < sizeof (server.sun_path)) | |
370 | strcpy (server.sun_path, socket_name); | |
371 | else | |
372 | fprintf (stderr, "%s: socket-name %s too long", | |
373 | argv[0], socket_name); | |
efb859b4 | 374 | |
9f637eea GM |
375 | /* See if the socket exists, and if it's owned by us. */ |
376 | sock_status = socket_status (server.sun_path); | |
377 | if (sock_status) | |
efb859b4 | 378 | { |
9f637eea GM |
379 | /* Failing that, see if LOGNAME or USER exist and differ from |
380 | our euid. If so, look for a socket based on the UID | |
381 | associated with the name. This is reminiscent of the logic | |
382 | that init_editfns uses to set the global Vuser_full_name. */ | |
e69233c2 | 383 | |
9f637eea GM |
384 | char *user_name = (char *) getenv ("LOGNAME"); |
385 | if (!user_name) | |
386 | user_name = (char *) getenv ("USER"); | |
e69233c2 | 387 | |
9f637eea GM |
388 | if (user_name) |
389 | { | |
390 | struct passwd *pw = getpwnam (user_name); | |
391 | if (pw && (pw->pw_uid != geteuid ())) | |
392 | { | |
393 | /* We're running under su, apparently. */ | |
e8228824 | 394 | sprintf (server.sun_path, "/tmp/emacs%d-%s/server", |
23431241 | 395 | (int) pw->pw_uid, system_name); |
9f637eea GM |
396 | sock_status = socket_status (server.sun_path); |
397 | } | |
398 | } | |
efb859b4 | 399 | } |
e69233c2 | 400 | |
9f637eea GM |
401 | switch (sock_status) |
402 | { | |
403 | case 1: | |
404 | /* There's a socket, but it isn't owned by us. This is OK if | |
405 | we are root. */ | |
406 | if (0 != geteuid ()) | |
407 | { | |
408 | fprintf (stderr, "%s: Invalid socket owner\n", argv[0]); | |
409 | fail (argc, argv); | |
410 | } | |
411 | break; | |
e69233c2 | 412 | |
9f637eea GM |
413 | case 2: |
414 | /* `stat' failed */ | |
415 | if (errno == ENOENT) | |
416 | fprintf (stderr, | |
45adde32 SE |
417 | "%s: can't find socket; have you started the server?\n\ |
418 | To start the server in Emacs, type \"M-x server-start\".\n", | |
9f637eea GM |
419 | argv[0]); |
420 | else | |
421 | fprintf (stderr, "%s: can't stat %s: %s\n", | |
422 | argv[0], server.sun_path, strerror (errno)); | |
423 | fail (argc, argv); | |
424 | break; | |
425 | } | |
efb859b4 | 426 | } |
46cec291 | 427 | |
4e23f2ba JB |
428 | if (connect (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2) |
429 | < 0) | |
46cec291 RS |
430 | { |
431 | fprintf (stderr, "%s: ", argv[0]); | |
432 | perror ("connect"); | |
97e3214d | 433 | fail (argc, argv); |
46cec291 | 434 | } |
23a7488d RS |
435 | |
436 | /* We use the stream OUT to send our command to the server. */ | |
46cec291 RS |
437 | if ((out = fdopen (s, "r+")) == NULL) |
438 | { | |
439 | fprintf (stderr, "%s: ", argv[0]); | |
440 | perror ("fdopen"); | |
97e3214d | 441 | fail (argc, argv); |
46cec291 RS |
442 | } |
443 | ||
23a7488d RS |
444 | /* We use the stream IN to read the response. |
445 | We used to use just one stream for both output and input | |
446 | on the socket, but reversing direction works nonportably: | |
447 | on some systems, the output appears as the first input; | |
448 | on other systems it does not. */ | |
449 | if ((in = fdopen (s, "r+")) == NULL) | |
450 | { | |
451 | fprintf (stderr, "%s: ", argv[0]); | |
452 | perror ("fdopen"); | |
97e3214d | 453 | fail (argc, argv); |
23a7488d RS |
454 | } |
455 | ||
38732dba | 456 | #ifdef HAVE_GETCWD |
ee6a193c | 457 | cwd = getcwd (string, sizeof string); |
38732dba RS |
458 | #else |
459 | cwd = getwd (string); | |
ee6a193c | 460 | #endif |
46cec291 RS |
461 | if (cwd == 0) |
462 | { | |
463 | /* getwd puts message in STRING if it fails. */ | |
bb5618fe | 464 | |
38732dba | 465 | #ifdef HAVE_GETCWD |
bb5618fe JB |
466 | fprintf (stderr, "%s: %s (%s)\n", argv[0], |
467 | "Cannot get current working directory", strerror (errno)); | |
38732dba | 468 | #else |
bb5618fe | 469 | fprintf (stderr, "%s: %s (%s)\n", argv[0], string, strerror (errno)); |
bd252662 | 470 | #endif |
97e3214d | 471 | fail (argc, argv); |
46cec291 RS |
472 | } |
473 | ||
5212210c RS |
474 | if (nowait) |
475 | fprintf (out, "-nowait "); | |
292d74a3 | 476 | |
30be2360 SM |
477 | if (eval) |
478 | fprintf (out, "-eval "); | |
479 | ||
480 | if (display) | |
87209357 EZ |
481 | { |
482 | fprintf (out, "-display "); | |
483 | quote_file_name (display, out); | |
484 | fprintf (out, " "); | |
485 | } | |
30be2360 | 486 | |
87209357 | 487 | if ((argc - optind > 0)) |
5212210c | 488 | { |
87209357 | 489 | for (i = optind; i < argc; i++) |
46cec291 | 490 | { |
87209357 EZ |
491 | if (eval) |
492 | ; /* Don't prepend any cwd or anything like that. */ | |
493 | else if (*argv[i] == '+') | |
494 | { | |
495 | char *p = argv[i] + 1; | |
496 | while (isdigit ((unsigned char) *p) || *p == ':') p++; | |
497 | if (*p != 0) | |
498 | { | |
499 | quote_file_name (cwd, out); | |
500 | fprintf (out, "/"); | |
501 | } | |
502 | } | |
503 | else if (*argv[i] != '/') | |
504 | { | |
505 | quote_file_name (cwd, out); | |
506 | fprintf (out, "/"); | |
507 | } | |
508 | ||
509 | quote_file_name (argv[i], out); | |
510 | fprintf (out, " "); | |
46cec291 | 511 | } |
46cec291 | 512 | } |
87209357 EZ |
513 | else |
514 | { | |
515 | while ((str = fgets (string, BUFSIZ, stdin))) | |
516 | { | |
517 | quote_file_name (str, out); | |
518 | } | |
519 | fprintf (out, " "); | |
520 | } | |
521 | ||
46cec291 RS |
522 | fprintf (out, "\n"); |
523 | fflush (out); | |
524 | ||
292d74a3 RS |
525 | /* Maybe wait for an answer. */ |
526 | if (nowait) | |
527 | return 0; | |
528 | ||
30be2360 SM |
529 | if (!eval) |
530 | { | |
531 | printf ("Waiting for Emacs..."); | |
532 | needlf = 2; | |
533 | } | |
46cec291 RS |
534 | fflush (stdout); |
535 | ||
3cf8c6aa | 536 | /* Now, wait for an answer and print any messages. */ |
e69233c2 | 537 | while ((str = fgets (string, BUFSIZ, in))) |
3cf8c6aa SM |
538 | { |
539 | if (needlf == 2) | |
540 | printf ("\n"); | |
541 | printf ("%s", str); | |
542 | needlf = str[0] == '\0' ? needlf : str[strlen (str) - 1] != '\n'; | |
543 | } | |
544 | ||
545 | if (needlf) | |
546 | printf ("\n"); | |
547 | fflush (stdout); | |
23a7488d | 548 | |
340ff9de | 549 | return 0; |
46cec291 RS |
550 | } |
551 | ||
30be2360 | 552 | #endif /* HAVE_SOCKETS */ |
27711600 RM |
553 | \f |
554 | #ifndef HAVE_STRERROR | |
555 | char * | |
556 | strerror (errnum) | |
557 | int errnum; | |
558 | { | |
559 | extern char *sys_errlist[]; | |
560 | extern int sys_nerr; | |
561 | ||
562 | if (errnum >= 0 && errnum < sys_nerr) | |
563 | return sys_errlist[errnum]; | |
564 | return (char *) "Unknown error"; | |
565 | } | |
566 | ||
567 | #endif /* ! HAVE_STRERROR */ | |
ab5796a9 MB |
568 | |
569 | /* arch-tag: f39bb9c4-73eb-477e-896d-50832e2ca9a7 | |
570 | (do not change this comment) */ |