Commit | Line | Data |
---|---|---|
efb859b4 | 1 | /* Client process that communicates with GNU Emacs acting as server. |
92b47a4a | 2 | Copyright (C) 1986, 1987, 1994, 1999, 2000, 2001, 2002, 2003, 2004, |
273dc16a | 3 | 2005, 2006 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 | |
364c38d3 LK |
19 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | Boston, MA 02110-1301, 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 | ||
aa0b6932 | 29 | #ifdef WINDOWSNT |
aa0b6932 | 30 | |
411b80a5 JB |
31 | # include <malloc.h> |
32 | # include <stdlib.h> | |
33 | ||
34 | # define HAVE_SOCKETS | |
1e7823d0 | 35 | # define HAVE_INET_SOCKETS |
411b80a5 JB |
36 | # define NO_SOCKETS_IN_FILE_SYSTEM |
37 | ||
aa0b6932 JB |
38 | # define HSOCKET SOCKET |
39 | # define CLOSE_SOCKET closesocket | |
aa0b6932 | 40 | # define INITIALIZE() (initialize_sockets ()) |
411b80a5 JB |
41 | |
42 | #else /* !WINDOWSNT */ | |
43 | ||
ed4a3730 JB |
44 | # include <sys/types.h> |
45 | ||
1e7823d0 JB |
46 | # ifdef HAVE_INET_SOCKETS |
47 | # include <netinet/in.h> | |
48 | # endif | |
411b80a5 | 49 | |
e35fc962 | 50 | # define INVALID_SOCKET -1 |
aa0b6932 JB |
51 | # define HSOCKET int |
52 | # define CLOSE_SOCKET close | |
aa0b6932 | 53 | # define INITIALIZE() |
411b80a5 JB |
54 | |
55 | #endif /* !WINDOWSNT */ | |
aa0b6932 | 56 | |
4e23f2ba | 57 | #undef signal |
46cec291 | 58 | |
e69233c2 | 59 | #include <ctype.h> |
8f9aaa0a | 60 | #include <stdio.h> |
aa0b6932 | 61 | #include "getopt.h" |
79f13bba DL |
62 | #ifdef HAVE_UNISTD_H |
63 | #include <unistd.h> | |
64 | #endif | |
8f9aaa0a | 65 | |
9f637eea GM |
66 | #ifdef VMS |
67 | # include "vms-pwd.h" | |
aa0b6932 JB |
68 | #else /* not VMS */ |
69 | #ifdef WINDOWSNT | |
70 | # include <io.h> | |
71 | #else /* not WINDOWSNT */ | |
9f637eea | 72 | # include <pwd.h> |
aa0b6932 | 73 | #endif /* not WINDOWSNT */ |
9f637eea GM |
74 | #endif /* not VMS */ |
75 | ||
8f9aaa0a | 76 | char *getenv (), *getwd (); |
5b9562c3 | 77 | char *(getcwd) (); |
8f9aaa0a | 78 | |
8f9aaa0a RS |
79 | #ifndef VERSION |
80 | #define VERSION "unspecified" | |
81 | #endif | |
82 | \f | |
aa0b6932 JB |
83 | #define SEND_STRING(data) (send_to_emacs (s, (data))) |
84 | #define SEND_QUOTED(data) (quote_file_name (s, (data))) | |
85 | ||
86 | #ifndef EXIT_SUCCESS | |
87 | #define EXIT_SUCCESS 0 | |
88 | #endif | |
89 | ||
90 | #ifndef EXIT_FAILURE | |
91 | #define EXIT_FAILURE 1 | |
92 | #endif | |
93 | ||
94 | #ifndef FALSE | |
95 | #define FALSE 0 | |
96 | #endif | |
97 | ||
98 | #ifndef TRUE | |
99 | #define TRUE 1 | |
100 | #endif | |
101 | ||
102 | #ifndef NO_RETURN | |
103 | #define NO_RETURN | |
104 | #endif | |
105 | \f | |
8f9aaa0a RS |
106 | /* Name used to invoke this program. */ |
107 | char *progname; | |
108 | ||
749ae770 | 109 | /* Nonzero means don't wait for a response from Emacs. --no-wait. */ |
8f9aaa0a RS |
110 | int nowait = 0; |
111 | ||
30be2360 SM |
112 | /* Nonzero means args are expressions to be evaluated. --eval. */ |
113 | int eval = 0; | |
114 | ||
115 | /* The display on which Emacs should work. --display. */ | |
116 | char *display = NULL; | |
117 | ||
118 | /* If non-NULL, the name of an editor to fallback to if the server | |
119 | is not running. --alternate-editor. */ | |
b03d27bd | 120 | const char *alternate_editor = NULL; |
30be2360 | 121 | |
3db926be | 122 | /* If non-NULL, the filename of the UNIX socket. */ |
254107e4 RS |
123 | char *socket_name = NULL; |
124 | ||
aa0b6932 JB |
125 | /* If non-NULL, the filename of the authentication file. */ |
126 | char *server_file = NULL; | |
127 | ||
2381d38d | 128 | void print_help_and_exit () NO_RETURN; |
7f3bff3e | 129 | |
8f9aaa0a RS |
130 | struct option longopts[] = |
131 | { | |
749ae770 | 132 | { "no-wait", no_argument, NULL, 'n' }, |
30be2360 | 133 | { "eval", no_argument, NULL, 'e' }, |
8f9aaa0a RS |
134 | { "help", no_argument, NULL, 'H' }, |
135 | { "version", no_argument, NULL, 'V' }, | |
3cf8c6aa | 136 | { "alternate-editor", required_argument, NULL, 'a' }, |
b03d27bd | 137 | #ifndef NO_SOCKETS_IN_FILE_SYSTEM |
254107e4 | 138 | { "socket-name", required_argument, NULL, 's' }, |
b03d27bd | 139 | #endif |
aa0b6932 | 140 | { "server-file", required_argument, NULL, 'f' }, |
30be2360 SM |
141 | { "display", required_argument, NULL, 'd' }, |
142 | { 0, 0, 0, 0 } | |
8f9aaa0a RS |
143 | }; |
144 | ||
145 | /* Decode the options from argv and argc. | |
5212210c | 146 | The global variable `optind' will say how many arguments we used up. */ |
8f9aaa0a | 147 | |
5212210c | 148 | void |
8f9aaa0a RS |
149 | decode_options (argc, argv) |
150 | int argc; | |
151 | char **argv; | |
152 | { | |
b6b6d6d2 SM |
153 | alternate_editor = getenv ("ALTERNATE_EDITOR"); |
154 | ||
8f9aaa0a RS |
155 | while (1) |
156 | { | |
157 | int opt = getopt_long (argc, argv, | |
b03d27bd JB |
158 | #ifndef NO_SOCKETS_IN_FILE_SYSTEM |
159 | "VHnea:s:f:d:", | |
160 | #else | |
161 | "VHnea:f:d:", | |
162 | #endif | |
163 | longopts, 0); | |
8f9aaa0a RS |
164 | |
165 | if (opt == EOF) | |
166 | break; | |
167 | ||
168 | switch (opt) | |
169 | { | |
170 | case 0: | |
171 | /* If getopt returns 0, then it has already processed a | |
172 | long-named option. We should do nothing. */ | |
173 | break; | |
e69233c2 | 174 | |
97e3214d GM |
175 | case 'a': |
176 | alternate_editor = optarg; | |
177 | break; | |
e69233c2 | 178 | |
b03d27bd | 179 | #ifndef NO_SOCKETS_IN_FILE_SYSTEM |
254107e4 RS |
180 | case 's': |
181 | socket_name = optarg; | |
182 | break; | |
b03d27bd | 183 | #endif |
254107e4 | 184 | |
aa0b6932 JB |
185 | case 'f': |
186 | server_file = optarg; | |
187 | break; | |
188 | ||
30be2360 SM |
189 | case 'd': |
190 | display = optarg; | |
191 | break; | |
192 | ||
8f9aaa0a RS |
193 | case 'n': |
194 | nowait = 1; | |
195 | break; | |
196 | ||
30be2360 SM |
197 | case 'e': |
198 | eval = 1; | |
199 | break; | |
200 | ||
8f9aaa0a | 201 | case 'V': |
20c396e8 | 202 | printf ("emacsclient %s\n", VERSION); |
65396510 | 203 | exit (EXIT_SUCCESS); |
8f9aaa0a | 204 | break; |
46cec291 | 205 | |
8f9aaa0a | 206 | case 'H': |
8f9aaa0a | 207 | print_help_and_exit (); |
20c396e8 JB |
208 | break; |
209 | ||
210 | default: | |
211 | fprintf (stderr, "Try `%s --help' for more information\n", progname); | |
65396510 | 212 | exit (EXIT_FAILURE); |
20c396e8 | 213 | break; |
8f9aaa0a RS |
214 | } |
215 | } | |
8f9aaa0a RS |
216 | } |
217 | ||
7f3bff3e | 218 | void |
8f9aaa0a RS |
219 | print_help_and_exit () |
220 | { | |
20c396e8 JB |
221 | printf ( |
222 | "Usage: %s [OPTIONS] FILE...\n\ | |
30be2360 SM |
223 | Tell the Emacs server to visit the specified files.\n\ |
224 | Every FILE can be either just a FILENAME or [+LINE[:COLUMN]] FILENAME.\n\ | |
20c396e8 | 225 | \n\ |
30be2360 SM |
226 | The following OPTIONS are accepted:\n\ |
227 | -V, --version Just print a version info and return\n\ | |
228 | -H, --help Print this usage information message\n\ | |
229 | -n, --no-wait Don't wait for the server to return\n\ | |
230 | -e, --eval Evaluate the FILE arguments as ELisp expressions\n\ | |
aa0b6932 JB |
231 | -d, --display=DISPLAY Visit the file in the given display\n" |
232 | #ifndef NO_SOCKETS_IN_FILE_SYSTEM | |
233 | "-s, --socket-name=FILENAME\n\ | |
234 | Set the filename of the UNIX socket for communication\n" | |
235 | #endif | |
236 | "-f, --server-file=FILENAME\n\ | |
237 | Set the filename of the TCP configuration file\n\ | |
30be2360 SM |
238 | -a, --alternate-editor=EDITOR\n\ |
239 | Editor to fallback to if the server is not running\n\ | |
20c396e8 | 240 | \n\ |
30be2360 | 241 | Report bugs to bug-gnu-emacs@gnu.org.\n", progname); |
65396510 | 242 | exit (EXIT_SUCCESS); |
8f9aaa0a | 243 | } |
5212210c | 244 | |
aa0b6932 JB |
245 | \f |
246 | /* | |
247 | Try to run a different command, or --if no alternate editor is | |
248 | defined-- exit with an errorcode. | |
249 | */ | |
250 | void | |
251 | fail (argc, argv) | |
252 | int argc; | |
253 | char **argv; | |
254 | { | |
255 | if (alternate_editor) | |
256 | { | |
257 | int i = optind - 1; | |
b73ea44b JB |
258 | #ifdef WINDOWSNT |
259 | argv[i] = (char *)alternate_editor; | |
260 | #endif | |
aa0b6932 JB |
261 | execvp (alternate_editor, argv + i); |
262 | fprintf (stderr, "%s: error executing alternate editor \"%s\"\n", | |
263 | progname, alternate_editor); | |
264 | } | |
aa0b6932 JB |
265 | exit (EXIT_FAILURE); |
266 | } | |
267 | ||
268 | \f | |
1e7823d0 | 269 | #if !defined (HAVE_SOCKETS) || !defined (HAVE_INET_SOCKETS) |
aa0b6932 JB |
270 | |
271 | int | |
272 | main (argc, argv) | |
273 | int argc; | |
274 | char **argv; | |
275 | { | |
276 | fprintf (stderr, "%s: Sorry, the Emacs server is supported only\n", | |
277 | argv[0]); | |
278 | fprintf (stderr, "on systems with Berkeley sockets.\n"); | |
279 | ||
280 | fail (argc, argv); | |
281 | } | |
282 | ||
1e7823d0 | 283 | #else /* HAVE_SOCKETS && HAVE_INET_SOCKETS */ |
aa0b6932 JB |
284 | |
285 | #ifdef WINDOWSNT | |
286 | # include <winsock2.h> | |
287 | #else | |
288 | # include <sys/types.h> | |
289 | # include <sys/socket.h> | |
290 | # include <sys/un.h> | |
291 | # include <sys/stat.h> | |
292 | # include <errno.h> | |
293 | #endif | |
294 | ||
295 | #define AUTH_KEY_LENGTH 64 | |
296 | #define SEND_BUFFER_SIZE 4096 | |
297 | ||
298 | extern char *strerror (); | |
299 | extern int errno; | |
300 | ||
301 | /* Buffer to accumulate data to send in TCP connections. */ | |
302 | char send_buffer[SEND_BUFFER_SIZE + 1]; | |
303 | int sblen = 0; /* Fill pointer for the send buffer. */ | |
304 | ||
305 | /* Let's send the data to Emacs when either | |
306 | - the data ends in "\n", or | |
307 | - the buffer is full (but this shouldn't happen) | |
308 | Otherwise, we just accumulate it. */ | |
b03d27bd JB |
309 | void |
310 | send_to_emacs (s, data) | |
aa0b6932 JB |
311 | HSOCKET s; |
312 | char *data; | |
313 | { | |
314 | while (data) | |
315 | { | |
316 | int dlen = strlen (data); | |
317 | if (dlen + sblen >= SEND_BUFFER_SIZE) | |
318 | { | |
319 | int part = SEND_BUFFER_SIZE - sblen; | |
320 | strncpy (&send_buffer[sblen], data, part); | |
321 | data += part; | |
322 | sblen = SEND_BUFFER_SIZE; | |
323 | } | |
324 | else if (dlen) | |
325 | { | |
326 | strcpy (&send_buffer[sblen], data); | |
327 | data = NULL; | |
328 | sblen += dlen; | |
329 | } | |
330 | else | |
331 | break; | |
332 | ||
333 | if (sblen == SEND_BUFFER_SIZE | |
334 | || (sblen > 0 && send_buffer[sblen-1] == '\n')) | |
335 | { | |
336 | int sent = send (s, send_buffer, sblen, 0); | |
337 | if (sent != sblen) | |
338 | strcpy (send_buffer, &send_buffer[sent]); | |
339 | sblen -= sent; | |
340 | } | |
341 | } | |
342 | } | |
343 | ||
a4cf2096 RS |
344 | /* In NAME, insert a & before each &, each space, each newline, and |
345 | any initial -. Change spaces to underscores, too, so that the | |
5212210c | 346 | return value never contains a space. */ |
87209357 | 347 | void |
aa0b6932 JB |
348 | quote_file_name (s, name) |
349 | HSOCKET s; | |
5212210c RS |
350 | char *name; |
351 | { | |
352 | char *copy = (char *) malloc (strlen (name) * 2 + 1); | |
353 | char *p, *q; | |
354 | ||
355 | p = name; | |
356 | q = copy; | |
357 | while (*p) | |
358 | { | |
359 | if (*p == ' ') | |
360 | { | |
f1db6a73 | 361 | *q++ = '&'; |
5212210c RS |
362 | *q++ = '_'; |
363 | p++; | |
364 | } | |
3cf8c6aa SM |
365 | else if (*p == '\n') |
366 | { | |
367 | *q++ = '&'; | |
368 | *q++ = 'n'; | |
369 | p++; | |
370 | } | |
5212210c RS |
371 | else |
372 | { | |
f1db6a73 RS |
373 | if (*p == '&' || (*p == '-' && p == name)) |
374 | *q++ = '&'; | |
5212210c RS |
375 | *q++ = *p++; |
376 | } | |
377 | } | |
f1db6a73 | 378 | *q++ = 0; |
5212210c | 379 | |
aa0b6932 | 380 | SEND_STRING (copy); |
87209357 EZ |
381 | |
382 | free (copy); | |
5212210c | 383 | } |
0c76956f | 384 | |
b03d27bd JB |
385 | int |
386 | file_name_absolute_p (filename) | |
387 | const unsigned char *filename; | |
388 | { | |
389 | /* Sanity check, it shouldn't happen. */ | |
390 | if (! filename) return FALSE; | |
391 | ||
392 | /* /xxx is always an absolute path. */ | |
393 | if (filename[0] == '/') return TRUE; | |
394 | ||
395 | /* Empty filenames (which shouldn't happen) are relative. */ | |
396 | if (filename[0] == '\0') return FALSE; | |
397 | ||
398 | #ifdef WINDOWSNT | |
399 | /* X:\xxx is always absolute; X:xxx is an error and will fail. */ | |
400 | if (islower (tolower (filename[0])) | |
401 | && filename[1] == ':' && filename[2] == '\\') | |
402 | return TRUE; | |
403 | ||
404 | /* Both \xxx and \\xxx\yyy are absolute. */ | |
405 | if (filename[0] == '\\') return TRUE; | |
406 | #endif | |
407 | ||
408 | return FALSE; | |
409 | } | |
410 | ||
aa0b6932 JB |
411 | #ifdef WINDOWSNT |
412 | /* Wrapper to make WSACleanup a cdecl, as required by atexit(). */ | |
b03d27bd JB |
413 | void |
414 | __cdecl close_winsock () | |
aa0b6932 JB |
415 | { |
416 | WSACleanup (); | |
417 | } | |
0c76956f | 418 | |
b03d27bd JB |
419 | /* Initialize the WinSock2 library. */ |
420 | void | |
421 | initialize_sockets () | |
0c76956f | 422 | { |
aa0b6932 JB |
423 | WSADATA wsaData; |
424 | ||
aa0b6932 JB |
425 | if (WSAStartup (MAKEWORD (2, 0), &wsaData)) |
426 | { | |
427 | fprintf (stderr, "%s: error initializing WinSock2", progname); | |
428 | exit (EXIT_FAILURE); | |
429 | } | |
430 | ||
431 | atexit (close_winsock); | |
0c76956f | 432 | } |
e35fc962 | 433 | #endif /* WINDOWSNT */ |
8f9aaa0a | 434 | \f |
97e3214d | 435 | /* |
aa0b6932 | 436 | * Read the information needed to set up a TCP comm channel with |
434a6c5d | 437 | * the Emacs server: host, port, pid and authentication string. |
97e3214d | 438 | */ |
b03d27bd JB |
439 | int |
440 | get_server_config (server, authentication) | |
aa0b6932 JB |
441 | struct sockaddr_in *server; |
442 | char *authentication; | |
97e3214d | 443 | { |
aa0b6932 JB |
444 | char dotted[32]; |
445 | char *port; | |
434a6c5d | 446 | char *pid; |
b03d27bd | 447 | FILE *config = NULL; |
aa0b6932 | 448 | |
b03d27bd JB |
449 | if (file_name_absolute_p (server_file)) |
450 | config = fopen (server_file, "rb"); | |
451 | else | |
97e3214d | 452 | { |
aa0b6932 | 453 | char *home = getenv ("HOME"); |
88b46d84 | 454 | |
aa0b6932 JB |
455 | if (home) |
456 | { | |
457 | char *path = alloca (32 + strlen (home) + strlen (server_file)); | |
458 | sprintf (path, "%s/.emacs.d/server/%s", home, server_file); | |
459 | config = fopen (path, "rb"); | |
460 | } | |
88b46d84 JB |
461 | #ifdef WINDOWSNT |
462 | if (!config && (home = getenv ("APPDATA"))) | |
463 | { | |
464 | char *path = alloca (32 + strlen (home) + strlen (server_file)); | |
465 | sprintf (path, "%s/.emacs.d/server/%s", home, server_file); | |
466 | config = fopen (path, "rb"); | |
467 | } | |
468 | #endif | |
aa0b6932 JB |
469 | } |
470 | ||
471 | if (! config) | |
472 | return FALSE; | |
473 | ||
474 | if (fgets (dotted, sizeof dotted, config) | |
434a6c5d JB |
475 | && (port = strchr (dotted, ':')) |
476 | && (pid = strchr (port, ' '))) | |
aa0b6932 JB |
477 | { |
478 | *port++ = '\0'; | |
434a6c5d | 479 | *pid++ = '\0'; |
97e3214d GM |
480 | } |
481 | else | |
482 | { | |
aa0b6932 | 483 | fprintf (stderr, "%s: invalid configuration info", progname); |
65396510 | 484 | exit (EXIT_FAILURE); |
97e3214d | 485 | } |
97e3214d | 486 | |
aa0b6932 JB |
487 | server->sin_family = AF_INET; |
488 | server->sin_addr.s_addr = inet_addr (dotted); | |
489 | server->sin_port = htons (atoi (port)); | |
97e3214d | 490 | |
aa0b6932 JB |
491 | if (! fread (authentication, AUTH_KEY_LENGTH, 1, config)) |
492 | { | |
493 | fprintf (stderr, "%s: cannot read authentication info", progname); | |
494 | exit (EXIT_FAILURE); | |
495 | } | |
46cec291 | 496 | |
aa0b6932 | 497 | fclose (config); |
97e3214d | 498 | |
434a6c5d JB |
499 | #ifdef WINDOWSNT |
500 | /* | |
501 | Modern Windows restrict which processes can set the foreground window. | |
502 | So, for emacsclient to be able to force Emacs into the foreground, we | |
503 | have to call AllowSetForegroundWindow(). Unfortunately, older Windows | |
504 | (W95, W98 and NT) don't have this function, so we have to check first. | |
505 | ||
506 | We're doing this here because it has to be done before sending info | |
507 | to Emacs, and otherwise we'll need a global variable just to pass around | |
508 | the pid, which is also inelegant. | |
509 | */ | |
510 | { | |
511 | HMODULE hUser32; | |
512 | ||
513 | if (hUser32 = LoadLibrary ("user32.dll")) | |
514 | { | |
2dc07a12 | 515 | FARPROC set_fg; |
434a6c5d JB |
516 | if (set_fg = GetProcAddress (hUser32, "AllowSetForegroundWindow")) |
517 | set_fg (atoi (pid)); | |
518 | FreeLibrary (hUser32); | |
519 | } | |
520 | } | |
521 | #endif | |
522 | ||
aa0b6932 | 523 | return TRUE; |
46cec291 RS |
524 | } |
525 | ||
aa0b6932 JB |
526 | HSOCKET |
527 | set_tcp_socket () | |
528 | { | |
529 | HSOCKET s; | |
530 | struct sockaddr_in server; | |
aa0b6932 JB |
531 | struct linger l_arg = {1, 1}; |
532 | char auth_string[AUTH_KEY_LENGTH + 1]; | |
46cec291 | 533 | |
aa0b6932 JB |
534 | if (! get_server_config (&server, auth_string)) |
535 | return INVALID_SOCKET; | |
536 | ||
b03d27bd JB |
537 | if (server.sin_addr.s_addr != inet_addr ("127.0.0.1")) |
538 | fprintf (stderr, "%s: connected to remote socket at %s\n", | |
539 | progname, inet_ntoa (server.sin_addr)); | |
540 | ||
aa0b6932 JB |
541 | /* |
542 | * Open up an AF_INET socket | |
543 | */ | |
544 | if ((s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) | |
545 | { | |
546 | fprintf (stderr, "%s: ", progname); | |
547 | perror ("socket"); | |
548 | return INVALID_SOCKET; | |
549 | } | |
550 | ||
551 | /* | |
552 | * Set up the socket | |
553 | */ | |
554 | if (connect (s, (struct sockaddr *) &server, sizeof server) < 0) | |
555 | { | |
556 | fprintf (stderr, "%s: ", progname); | |
557 | perror ("connect"); | |
558 | return INVALID_SOCKET; | |
559 | } | |
560 | ||
aa0b6932 JB |
561 | setsockopt (s, SOL_SOCKET, SO_LINGER, (char *) &l_arg, sizeof l_arg); |
562 | ||
563 | /* | |
564 | * Send the authentication | |
565 | */ | |
566 | auth_string[AUTH_KEY_LENGTH] = '\0'; | |
567 | ||
568 | SEND_STRING ("-auth "); | |
569 | SEND_STRING (auth_string); | |
570 | SEND_STRING ("\n"); | |
571 | ||
572 | return s; | |
573 | } | |
574 | ||
575 | #if !defined (NO_SOCKETS_IN_FILE_SYSTEM) | |
46cec291 | 576 | |
9f637eea GM |
577 | /* Three possibilities: |
578 | 2 - can't be `stat'ed (sets errno) | |
579 | 1 - isn't owned by us | |
580 | 0 - success: none of the above */ | |
581 | ||
582 | static int | |
583 | socket_status (socket_name) | |
584 | char *socket_name; | |
585 | { | |
586 | struct stat statbfr; | |
587 | ||
588 | if (stat (socket_name, &statbfr) == -1) | |
589 | return 2; | |
590 | ||
591 | if (statbfr.st_uid != geteuid ()) | |
592 | return 1; | |
593 | ||
594 | return 0; | |
595 | } | |
596 | ||
aa0b6932 JB |
597 | HSOCKET |
598 | set_local_socket () | |
46cec291 | 599 | { |
aa0b6932 | 600 | HSOCKET s; |
46cec291 | 601 | struct sockaddr_un server; |
46cec291 | 602 | |
e69233c2 | 603 | /* |
46cec291 RS |
604 | * Open up an AF_UNIX socket in this person's home directory |
605 | */ | |
606 | ||
607 | if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) | |
608 | { | |
aa0b6932 | 609 | fprintf (stderr, "%s: ", progname); |
46cec291 | 610 | perror ("socket"); |
aa0b6932 | 611 | return INVALID_SOCKET; |
46cec291 | 612 | } |
e69233c2 | 613 | |
46cec291 | 614 | server.sun_family = AF_UNIX; |
c5fee545 | 615 | |
c5fee545 | 616 | { |
9f637eea | 617 | int sock_status = 0; |
5c9659d3 | 618 | int default_sock = !socket_name; |
152b6e83 | 619 | int saved_errno; |
0734b0d0 SM |
620 | char *server_name = "server"; |
621 | ||
622 | if (socket_name && !index (socket_name, '/') && !index (socket_name, '\\')) | |
623 | { /* socket_name is a file name component. */ | |
624 | server_name = socket_name; | |
625 | socket_name = NULL; | |
626 | default_sock = 1; /* Try both UIDs. */ | |
627 | } | |
efb859b4 | 628 | |
5c9659d3 | 629 | if (default_sock) |
254107e4 | 630 | { |
0734b0d0 SM |
631 | socket_name = alloca (100 + strlen (server_name)); |
632 | sprintf (socket_name, "/tmp/emacs%d/%s", | |
633 | (int) geteuid (), server_name); | |
254107e4 RS |
634 | } |
635 | ||
636 | if (strlen (socket_name) < sizeof (server.sun_path)) | |
637 | strcpy (server.sun_path, socket_name); | |
638 | else | |
5c9659d3 SM |
639 | { |
640 | fprintf (stderr, "%s: socket-name %s too long", | |
aa0b6932 | 641 | progname, socket_name); |
65396510 | 642 | exit (EXIT_FAILURE); |
5c9659d3 | 643 | } |
efb859b4 | 644 | |
9f637eea GM |
645 | /* See if the socket exists, and if it's owned by us. */ |
646 | sock_status = socket_status (server.sun_path); | |
152b6e83 | 647 | saved_errno = errno; |
5c9659d3 | 648 | if (sock_status && default_sock) |
efb859b4 | 649 | { |
9f637eea GM |
650 | /* Failing that, see if LOGNAME or USER exist and differ from |
651 | our euid. If so, look for a socket based on the UID | |
652 | associated with the name. This is reminiscent of the logic | |
653 | that init_editfns uses to set the global Vuser_full_name. */ | |
e69233c2 | 654 | |
9f637eea | 655 | char *user_name = (char *) getenv ("LOGNAME"); |
293f9f2a | 656 | |
9f637eea GM |
657 | if (!user_name) |
658 | user_name = (char *) getenv ("USER"); | |
e69233c2 | 659 | |
9f637eea GM |
660 | if (user_name) |
661 | { | |
662 | struct passwd *pw = getpwnam (user_name); | |
293f9f2a | 663 | |
9f637eea GM |
664 | if (pw && (pw->pw_uid != geteuid ())) |
665 | { | |
666 | /* We're running under su, apparently. */ | |
0734b0d0 SM |
667 | socket_name = alloca (100 + strlen (server_name)); |
668 | sprintf (socket_name, "/tmp/emacs%d/%s", | |
669 | (int) pw->pw_uid, server_name); | |
5c9659d3 SM |
670 | |
671 | if (strlen (socket_name) < sizeof (server.sun_path)) | |
672 | strcpy (server.sun_path, socket_name); | |
673 | else | |
674 | { | |
675 | fprintf (stderr, "%s: socket-name %s too long", | |
aa0b6932 | 676 | progname, socket_name); |
65396510 | 677 | exit (EXIT_FAILURE); |
5c9659d3 SM |
678 | } |
679 | ||
9f637eea | 680 | sock_status = socket_status (server.sun_path); |
152b6e83 | 681 | saved_errno = errno; |
9f637eea | 682 | } |
293f9f2a RS |
683 | else |
684 | errno = saved_errno; | |
9f637eea | 685 | } |
efb859b4 | 686 | } |
e69233c2 | 687 | |
aa0b6932 JB |
688 | switch (sock_status) |
689 | { | |
690 | case 1: | |
691 | /* There's a socket, but it isn't owned by us. This is OK if | |
692 | we are root. */ | |
693 | if (0 != geteuid ()) | |
694 | { | |
e35fc962 | 695 | fprintf (stderr, "%s: Invalid socket owner\n", progname); |
aa0b6932 JB |
696 | return INVALID_SOCKET; |
697 | } | |
698 | break; | |
699 | ||
700 | case 2: | |
701 | /* `stat' failed */ | |
702 | if (saved_errno == ENOENT) | |
703 | fprintf (stderr, | |
704 | "%s: can't find socket; have you started the server?\n\ | |
45adde32 | 705 | To start the server in Emacs, type \"M-x server-start\".\n", |
aa0b6932 JB |
706 | progname); |
707 | else | |
708 | fprintf (stderr, "%s: can't stat %s: %s\n", | |
709 | progname, server.sun_path, strerror (saved_errno)); | |
b03d27bd | 710 | return INVALID_SOCKET; |
aa0b6932 | 711 | } |
efb859b4 | 712 | } |
46cec291 | 713 | |
4e23f2ba JB |
714 | if (connect (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2) |
715 | < 0) | |
46cec291 | 716 | { |
aa0b6932 | 717 | fprintf (stderr, "%s: ", progname); |
46cec291 | 718 | perror ("connect"); |
aa0b6932 | 719 | return INVALID_SOCKET; |
46cec291 | 720 | } |
23a7488d | 721 | |
aa0b6932 JB |
722 | return s; |
723 | } | |
724 | #endif /* ! NO_SOCKETS_IN_FILE_SYSTEM */ | |
725 | ||
726 | HSOCKET | |
727 | set_socket () | |
728 | { | |
b03d27bd JB |
729 | HSOCKET s; |
730 | ||
731 | INITIALIZE (); | |
732 | ||
aa0b6932 | 733 | #ifndef NO_SOCKETS_IN_FILE_SYSTEM |
b03d27bd JB |
734 | /* Explicit --socket-name argument. */ |
735 | if (socket_name) | |
736 | { | |
737 | s = set_local_socket (); | |
738 | if ((s != INVALID_SOCKET) || alternate_editor) | |
739 | return s; | |
740 | ||
741 | fprintf (stderr, "%s: error accessing socket \"%s\"", | |
742 | progname, socket_name); | |
743 | exit (EXIT_FAILURE); | |
744 | } | |
745 | #endif | |
746 | ||
747 | /* Explicit --server-file arg or EMACS_SERVER_FILE variable. */ | |
748 | if (!server_file) | |
749 | server_file = getenv ("EMACS_SERVER_FILE"); | |
750 | ||
751 | if (server_file) | |
46cec291 | 752 | { |
b03d27bd JB |
753 | s = set_tcp_socket (); |
754 | if ((s != INVALID_SOCKET) || alternate_editor) | |
755 | return s; | |
756 | ||
757 | fprintf (stderr, "%s: error accessing server file \"%s\"", | |
758 | progname, server_file); | |
759 | exit (EXIT_FAILURE); | |
46cec291 | 760 | } |
b03d27bd JB |
761 | |
762 | #ifndef NO_SOCKETS_IN_FILE_SYSTEM | |
763 | /* Implicit local socket. */ | |
764 | s = set_local_socket (); | |
765 | if (s != INVALID_SOCKET) | |
766 | return s; | |
767 | #endif | |
768 | ||
769 | /* Implicit server file. */ | |
770 | server_file = "server"; | |
771 | s = set_tcp_socket (); | |
772 | if ((s != INVALID_SOCKET) || alternate_editor) | |
773 | return s; | |
774 | ||
775 | /* No implicit or explicit socket, and no alternate editor. */ | |
776 | fprintf (stderr, "%s: No socket or alternate editor. Please use:\n\n" | |
777 | #ifndef NO_SOCKETS_IN_FILE_SYSTEM | |
778 | "\t--socket-name\n" | |
aa0b6932 | 779 | #endif |
b03d27bd JB |
780 | "\t--server-file (or environment variable EMACS_SERVER_FILE)\n\ |
781 | \t--alternate-editor (or environment variable ALTERNATE_EDITOR)\n", | |
782 | progname); | |
783 | exit (EXIT_FAILURE); | |
aa0b6932 | 784 | } |
46cec291 | 785 | |
aa0b6932 JB |
786 | int |
787 | main (argc, argv) | |
788 | int argc; | |
789 | char **argv; | |
790 | { | |
791 | HSOCKET s; | |
792 | int i, rl, needlf = 0; | |
793 | char *cwd; | |
794 | char string[BUFSIZ+1]; | |
795 | ||
796 | progname = argv[0]; | |
797 | ||
798 | /* Process options. */ | |
799 | decode_options (argc, argv); | |
800 | ||
801 | if ((argc - optind < 1) && !eval) | |
23a7488d | 802 | { |
aa0b6932 JB |
803 | fprintf (stderr, "%s: file name or argument required\n", progname); |
804 | fprintf (stderr, "Try `%s --help' for more information\n", progname); | |
805 | exit (EXIT_FAILURE); | |
23a7488d RS |
806 | } |
807 | ||
aa0b6932 JB |
808 | if ((s = set_socket ()) == INVALID_SOCKET) |
809 | fail (argc, argv); | |
810 | ||
38732dba | 811 | #ifdef HAVE_GETCWD |
ee6a193c | 812 | cwd = getcwd (string, sizeof string); |
38732dba RS |
813 | #else |
814 | cwd = getwd (string); | |
ee6a193c | 815 | #endif |
46cec291 RS |
816 | if (cwd == 0) |
817 | { | |
818 | /* getwd puts message in STRING if it fails. */ | |
38732dba | 819 | #ifdef HAVE_GETCWD |
aa0b6932 | 820 | fprintf (stderr, "%s: %s (%s)\n", progname, |
bb5618fe | 821 | "Cannot get current working directory", strerror (errno)); |
38732dba | 822 | #else |
aa0b6932 | 823 | fprintf (stderr, "%s: %s (%s)\n", progname, string, strerror (errno)); |
bd252662 | 824 | #endif |
97e3214d | 825 | fail (argc, argv); |
46cec291 RS |
826 | } |
827 | ||
5212210c | 828 | if (nowait) |
aa0b6932 | 829 | SEND_STRING ("-nowait "); |
292d74a3 | 830 | |
30be2360 | 831 | if (eval) |
aa0b6932 | 832 | SEND_STRING ("-eval "); |
30be2360 SM |
833 | |
834 | if (display) | |
87209357 | 835 | { |
aa0b6932 JB |
836 | SEND_STRING ("-display "); |
837 | SEND_QUOTED (display); | |
838 | SEND_STRING (" "); | |
87209357 | 839 | } |
30be2360 | 840 | |
87209357 | 841 | if ((argc - optind > 0)) |
5212210c | 842 | { |
87209357 | 843 | for (i = optind; i < argc; i++) |
46cec291 | 844 | { |
87209357 EZ |
845 | if (eval) |
846 | ; /* Don't prepend any cwd or anything like that. */ | |
847 | else if (*argv[i] == '+') | |
848 | { | |
849 | char *p = argv[i] + 1; | |
850 | while (isdigit ((unsigned char) *p) || *p == ':') p++; | |
851 | if (*p != 0) | |
852 | { | |
aa0b6932 JB |
853 | SEND_QUOTED (cwd); |
854 | SEND_STRING ("/"); | |
87209357 EZ |
855 | } |
856 | } | |
b03d27bd | 857 | else if (! file_name_absolute_p (argv[i])) |
87209357 | 858 | { |
aa0b6932 JB |
859 | SEND_QUOTED (cwd); |
860 | SEND_STRING ("/"); | |
87209357 EZ |
861 | } |
862 | ||
aa0b6932 JB |
863 | SEND_QUOTED (argv[i]); |
864 | SEND_STRING (" "); | |
46cec291 | 865 | } |
46cec291 | 866 | } |
87209357 EZ |
867 | else |
868 | { | |
aa0b6932 | 869 | while (fgets (string, BUFSIZ, stdin)) |
87209357 | 870 | { |
aa0b6932 | 871 | SEND_QUOTED (string); |
87209357 | 872 | } |
aa0b6932 | 873 | SEND_STRING (" "); |
87209357 | 874 | } |
65396510 | 875 | |
aa0b6932 | 876 | SEND_STRING ("\n"); |
46cec291 | 877 | |
292d74a3 | 878 | /* Maybe wait for an answer. */ |
aa0b6932 | 879 | if (!nowait) |
30be2360 | 880 | { |
aa0b6932 JB |
881 | if (!eval) |
882 | { | |
883 | printf ("Waiting for Emacs..."); | |
884 | needlf = 2; | |
885 | } | |
886 | fflush (stdout); | |
887 | ||
888 | /* Now, wait for an answer and print any messages. */ | |
889 | while ((rl = recv (s, string, BUFSIZ, 0)) > 0) | |
890 | { | |
891 | string[rl] = '\0'; | |
892 | if (needlf == 2) | |
893 | printf ("\n"); | |
894 | printf ("%s", string); | |
895 | needlf = string[0] == '\0' ? needlf : string[strlen (string) - 1] != '\n'; | |
896 | } | |
897 | ||
898 | if (needlf) | |
899 | printf ("\n"); | |
900 | fflush (stdout); | |
30be2360 | 901 | } |
23a7488d | 902 | |
aa0b6932 | 903 | CLOSE_SOCKET (s); |
65396510 | 904 | return EXIT_SUCCESS; |
46cec291 RS |
905 | } |
906 | ||
1e7823d0 | 907 | #endif /* HAVE_SOCKETS && HAVE_INET_SOCKETS */ |
aa0b6932 | 908 | |
27711600 RM |
909 | #ifndef HAVE_STRERROR |
910 | char * | |
911 | strerror (errnum) | |
912 | int errnum; | |
913 | { | |
914 | extern char *sys_errlist[]; | |
915 | extern int sys_nerr; | |
916 | ||
917 | if (errnum >= 0 && errnum < sys_nerr) | |
918 | return sys_errlist[errnum]; | |
919 | return (char *) "Unknown error"; | |
920 | } | |
921 | ||
922 | #endif /* ! HAVE_STRERROR */ | |
ab5796a9 MB |
923 | |
924 | /* arch-tag: f39bb9c4-73eb-477e-896d-50832e2ca9a7 | |
925 | (do not change this comment) */ | |
65396510 TTN |
926 | |
927 | /* emacsclient.c ends here */ |