Commit | Line | Data |
---|---|---|
efb859b4 | 1 | /* Client process that communicates with GNU Emacs acting as server. |
73b0cd50 | 2 | Copyright (C) 1986-1987, 1994, 1999-2011 Free Software Foundation, Inc. |
46cec291 RS |
3 | |
4 | This file is part of GNU Emacs. | |
5 | ||
294981c7 | 6 | GNU Emacs is free software: you can redistribute it and/or modify |
46cec291 | 7 | it under the terms of the GNU General Public License as published by |
294981c7 GM |
8 | the Free Software Foundation, either version 3 of the License, or |
9 | (at your option) any later version. | |
46cec291 RS |
10 | |
11 | GNU Emacs is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
294981c7 | 17 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
46cec291 RS |
18 | |
19 | ||
2f8fe2f4 | 20 | #include <config.h> |
e69233c2 | 21 | |
aa0b6932 | 22 | #ifdef WINDOWSNT |
aa0b6932 | 23 | |
bc28de71 JB |
24 | /* config.h defines these, which disables sockets altogether! */ |
25 | # undef _WINSOCKAPI_ | |
26 | # undef _WINSOCK_H | |
27 | ||
411b80a5 JB |
28 | # include <malloc.h> |
29 | # include <stdlib.h> | |
42073bfb | 30 | # include <windows.h> |
dbf60b07 | 31 | # include <commctrl.h> |
0613f5d5 DN |
32 | # include <io.h> |
33 | # include <winsock2.h> | |
411b80a5 | 34 | |
411b80a5 JB |
35 | # define NO_SOCKETS_IN_FILE_SYSTEM |
36 | ||
aa0b6932 JB |
37 | # define HSOCKET SOCKET |
38 | # define CLOSE_SOCKET closesocket | |
aa0b6932 | 39 | # define INITIALIZE() (initialize_sockets ()) |
411b80a5 | 40 | |
728a982d DN |
41 | char *w32_getenv (char *); |
42 | #define egetenv(VAR) w32_getenv(VAR) | |
43 | ||
411b80a5 JB |
44 | #else /* !WINDOWSNT */ |
45 | ||
67c1df01 | 46 | # include "syswait.h" |
ed4a3730 | 47 | |
1e7823d0 JB |
48 | # ifdef HAVE_INET_SOCKETS |
49 | # include <netinet/in.h> | |
0613f5d5 DN |
50 | # ifdef HAVE_SOCKETS |
51 | # include <sys/types.h> | |
52 | # include <sys/socket.h> | |
53 | # include <sys/un.h> | |
54 | # endif /* HAVE_SOCKETS */ | |
1e7823d0 | 55 | # endif |
18d2ad29 | 56 | # include <arpa/inet.h> |
448d4085 | 57 | |
e35fc962 | 58 | # define INVALID_SOCKET -1 |
aa0b6932 JB |
59 | # define HSOCKET int |
60 | # define CLOSE_SOCKET close | |
aa0b6932 | 61 | # define INITIALIZE() |
411b80a5 | 62 | |
5445ab06 DN |
63 | # ifndef WCONTINUED |
64 | # define WCONTINUED 8 | |
65 | # endif | |
66 | ||
728a982d DN |
67 | #define egetenv(VAR) getenv(VAR) |
68 | ||
411b80a5 | 69 | #endif /* !WINDOWSNT */ |
aa0b6932 | 70 | |
4e23f2ba | 71 | #undef signal |
46cec291 | 72 | |
42073bfb | 73 | #include <stdarg.h> |
e69233c2 | 74 | #include <ctype.h> |
8f9aaa0a | 75 | #include <stdio.h> |
613f7bda | 76 | #include <getopt.h> |
4004364e | 77 | #include <unistd.h> |
8f9aaa0a | 78 | |
0613f5d5 | 79 | #include <pwd.h> |
dc4a4a14 | 80 | #include <sys/stat.h> |
9628b887 | 81 | #include <signal.h> |
4d553a13 | 82 | #include <errno.h> |
9628b887 | 83 | |
0613f5d5 | 84 | |
9628b887 | 85 | \f |
873fbd0b | 86 | char *getenv (const char *), *getwd (char *); |
35a7804d JB |
87 | #ifdef HAVE_GETCWD |
88 | char *(getcwd) (char *, size_t); | |
89 | #endif | |
8f9aaa0a | 90 | |
8f9aaa0a RS |
91 | #ifndef VERSION |
92 | #define VERSION "unspecified" | |
93 | #endif | |
94 | \f | |
aa0b6932 JB |
95 | |
96 | #ifndef EXIT_SUCCESS | |
97 | #define EXIT_SUCCESS 0 | |
98 | #endif | |
99 | ||
100 | #ifndef EXIT_FAILURE | |
101 | #define EXIT_FAILURE 1 | |
102 | #endif | |
103 | ||
104 | #ifndef FALSE | |
105 | #define FALSE 0 | |
106 | #endif | |
107 | ||
108 | #ifndef TRUE | |
109 | #define TRUE 1 | |
110 | #endif | |
111 | ||
bc558f3e JB |
112 | /* Additional space when allocating buffers for filenames, etc. */ |
113 | #define EXTRA_SPACE 100 | |
114 | ||
4be3dfc5 PE |
115 | /* Use this to suppress gcc's `...may be used before initialized' warnings. */ |
116 | #ifdef lint | |
117 | # define IF_LINT(Code) Code | |
118 | #else | |
119 | # define IF_LINT(Code) /* empty */ | |
120 | #endif | |
121 | ||
aa0b6932 | 122 | \f |
8f9aaa0a | 123 | /* Name used to invoke this program. */ |
728a982d | 124 | const char *progname; |
8f9aaa0a | 125 | |
0a125897 KL |
126 | /* The second argument to main. */ |
127 | char **main_argv; | |
128 | ||
749ae770 | 129 | /* Nonzero means don't wait for a response from Emacs. --no-wait. */ |
8f9aaa0a RS |
130 | int nowait = 0; |
131 | ||
0b973fc5 DW |
132 | /* Nonzero means don't print messages for successful operations. --quiet. */ |
133 | int quiet = 0; | |
134 | ||
30be2360 SM |
135 | /* Nonzero means args are expressions to be evaluated. --eval. */ |
136 | int eval = 0; | |
137 | ||
31fa6595 SM |
138 | /* Nonzero means don't open a new frame. Inverse of --create-frame. */ |
139 | int current_frame = 1; | |
92071250 | 140 | |
30be2360 | 141 | /* The display on which Emacs should work. --display. */ |
988e88ab | 142 | const char *display = NULL; |
30be2360 | 143 | |
0191e222 CY |
144 | /* The parent window ID, if we are opening a frame via XEmbed. */ |
145 | char *parent_id = NULL; | |
146 | ||
9628b887 | 147 | /* Nonzero means open a new Emacs frame on the current terminal. */ |
77134727 | 148 | int tty = 0; |
9628b887 | 149 | |
30be2360 SM |
150 | /* If non-NULL, the name of an editor to fallback to if the server |
151 | is not running. --alternate-editor. */ | |
b03d27bd | 152 | const char *alternate_editor = NULL; |
30be2360 | 153 | |
3db926be | 154 | /* If non-NULL, the filename of the UNIX socket. */ |
254107e4 RS |
155 | char *socket_name = NULL; |
156 | ||
aa0b6932 | 157 | /* If non-NULL, the filename of the authentication file. */ |
988e88ab | 158 | const char *server_file = NULL; |
aa0b6932 | 159 | |
c66648e0 JB |
160 | /* PID of the Emacs server process. */ |
161 | int emacs_pid = 0; | |
162 | ||
18a4ce5e AR |
163 | /* If non-NULL, a string that should form a frame parameter alist to |
164 | be used for the new frame */ | |
165 | const char *frame_parameters = NULL; | |
166 | ||
b23b5a5b PE |
167 | static void print_help_and_exit (void) NO_RETURN; |
168 | static void fail (void) NO_RETURN; | |
68441b90 | 169 | |
7f3bff3e | 170 | |
8f9aaa0a RS |
171 | struct option longopts[] = |
172 | { | |
749ae770 | 173 | { "no-wait", no_argument, NULL, 'n' }, |
0b973fc5 | 174 | { "quiet", no_argument, NULL, 'q' }, |
30be2360 | 175 | { "eval", no_argument, NULL, 'e' }, |
8f9aaa0a RS |
176 | { "help", no_argument, NULL, 'H' }, |
177 | { "version", no_argument, NULL, 'V' }, | |
b28c910d | 178 | { "tty", no_argument, NULL, 't' }, |
dc1cd5f7 | 179 | { "nw", no_argument, NULL, 't' }, |
31fa6595 | 180 | { "create-frame", no_argument, NULL, 'c' }, |
3cf8c6aa | 181 | { "alternate-editor", required_argument, NULL, 'a' }, |
18a4ce5e | 182 | { "frame-parameters", required_argument, NULL, 'F' }, |
b03d27bd | 183 | #ifndef NO_SOCKETS_IN_FILE_SYSTEM |
254107e4 | 184 | { "socket-name", required_argument, NULL, 's' }, |
b03d27bd | 185 | #endif |
aa0b6932 | 186 | { "server-file", required_argument, NULL, 'f' }, |
0ebec7d3 | 187 | #ifndef WINDOWSNT |
30be2360 | 188 | { "display", required_argument, NULL, 'd' }, |
0ebec7d3 | 189 | #endif |
0191e222 | 190 | { "parent-id", required_argument, NULL, 'p' }, |
30be2360 | 191 | { 0, 0, 0, 0 } |
8f9aaa0a RS |
192 | }; |
193 | ||
974b73e8 KL |
194 | \f |
195 | /* Like malloc but get fatal error if memory is exhausted. */ | |
196 | ||
b23b5a5b | 197 | static long * |
873fbd0b | 198 | xmalloc (unsigned int size) |
974b73e8 KL |
199 | { |
200 | long *result = (long *) malloc (size); | |
201 | if (result == NULL) | |
202 | { | |
203 | perror ("malloc"); | |
204 | exit (EXIT_FAILURE); | |
205 | } | |
206 | return result; | |
207 | } | |
208 | ||
974b73e8 KL |
209 | /* From sysdep.c */ |
210 | #if !defined (HAVE_GET_CURRENT_DIR_NAME) || defined (BROKEN_GET_CURRENT_DIR_NAME) | |
211 | ||
cb06b8dc SM |
212 | /* From lisp.h */ |
213 | #ifndef DIRECTORY_SEP | |
214 | #define DIRECTORY_SEP '/' | |
215 | #endif | |
216 | #ifndef IS_DIRECTORY_SEP | |
217 | #define IS_DIRECTORY_SEP(_c_) ((_c_) == DIRECTORY_SEP) | |
218 | #endif | |
219 | #ifndef IS_DEVICE_SEP | |
220 | #ifndef DEVICE_SEP | |
221 | #define IS_DEVICE_SEP(_c_) 0 | |
222 | #else | |
223 | #define IS_DEVICE_SEP(_c_) ((_c_) == DEVICE_SEP) | |
224 | #endif | |
225 | #endif | |
cb06b8dc | 226 | |
c5443aa5 | 227 | char *get_current_dir_name (void); |
cb06b8dc | 228 | |
974b73e8 | 229 | /* Return the current working directory. Returns NULL on errors. |
5ab73228 | 230 | Any other returned value must be freed with free. This is used |
974b73e8 KL |
231 | only when get_current_dir_name is not defined on the system. */ |
232 | char* | |
7c3320d8 | 233 | get_current_dir_name (void) |
974b73e8 KL |
234 | { |
235 | char *buf; | |
b9170155 | 236 | const char *pwd; |
974b73e8 KL |
237 | struct stat dotstat, pwdstat; |
238 | /* If PWD is accurate, use it instead of calling getwd. PWD is | |
239 | sometimes a nicer name, and using it may avoid a fatal error if a | |
240 | parent directory is searchable but not readable. */ | |
7ce8671d | 241 | if ((pwd = egetenv ("PWD")) != 0 |
974b73e8 KL |
242 | && (IS_DIRECTORY_SEP (*pwd) || (*pwd && IS_DEVICE_SEP (pwd[1]))) |
243 | && stat (pwd, &pwdstat) == 0 | |
244 | && stat (".", &dotstat) == 0 | |
245 | && dotstat.st_ino == pwdstat.st_ino | |
246 | && dotstat.st_dev == pwdstat.st_dev | |
247 | #ifdef MAXPATHLEN | |
248 | && strlen (pwd) < MAXPATHLEN | |
249 | #endif | |
250 | ) | |
251 | { | |
252 | buf = (char *) xmalloc (strlen (pwd) + 1); | |
253 | if (!buf) | |
254 | return NULL; | |
255 | strcpy (buf, pwd); | |
256 | } | |
257 | #ifdef HAVE_GETCWD | |
258 | else | |
259 | { | |
260 | size_t buf_size = 1024; | |
261 | buf = (char *) xmalloc (buf_size); | |
262 | if (!buf) | |
263 | return NULL; | |
264 | for (;;) | |
265 | { | |
266 | if (getcwd (buf, buf_size) == buf) | |
267 | break; | |
268 | if (errno != ERANGE) | |
269 | { | |
270 | int tmp_errno = errno; | |
271 | free (buf); | |
272 | errno = tmp_errno; | |
273 | return NULL; | |
274 | } | |
275 | buf_size *= 2; | |
276 | buf = (char *) realloc (buf, buf_size); | |
277 | if (!buf) | |
278 | return NULL; | |
279 | } | |
280 | } | |
281 | #else | |
282 | else | |
283 | { | |
284 | /* We need MAXPATHLEN here. */ | |
285 | buf = (char *) xmalloc (MAXPATHLEN + 1); | |
286 | if (!buf) | |
287 | return NULL; | |
288 | if (getwd (buf) == NULL) | |
289 | { | |
290 | int tmp_errno = errno; | |
291 | free (buf); | |
292 | errno = tmp_errno; | |
293 | return NULL; | |
294 | } | |
295 | } | |
296 | #endif | |
297 | return buf; | |
298 | } | |
299 | #endif | |
300 | ||
42073bfb | 301 | #ifdef WINDOWSNT |
7ce8671d | 302 | |
a54af40e EZ |
303 | /* Like strdup but get a fatal error if memory is exhausted. */ |
304 | ||
305 | char * | |
306 | xstrdup (const char *s) | |
307 | { | |
308 | char *result = strdup (s); | |
309 | if (result == NULL) | |
310 | { | |
311 | perror ("strdup"); | |
312 | exit (EXIT_FAILURE); | |
313 | } | |
314 | return result; | |
315 | } | |
316 | ||
7ce8671d JB |
317 | #define REG_ROOT "SOFTWARE\\GNU\\Emacs" |
318 | ||
319 | /* Retrieve an environment variable from the Emacs subkeys of the registry. | |
320 | Return NULL if the variable was not found, or it was empty. | |
321 | This code is based on w32_get_resource (w32.c). */ | |
322 | char * | |
7c3320d8 | 323 | w32_get_resource (HKEY predefined, char *key, LPDWORD type) |
7ce8671d JB |
324 | { |
325 | HKEY hrootkey = NULL; | |
326 | char *result = NULL; | |
327 | DWORD cbData; | |
328 | ||
329 | if (RegOpenKeyEx (predefined, REG_ROOT, 0, KEY_READ, &hrootkey) == ERROR_SUCCESS) | |
330 | { | |
331 | if (RegQueryValueEx (hrootkey, key, NULL, NULL, NULL, &cbData) == ERROR_SUCCESS) | |
332 | { | |
333 | result = (char *) xmalloc (cbData); | |
334 | ||
bc558f3e JB |
335 | if ((RegQueryValueEx (hrootkey, key, NULL, type, result, &cbData) != ERROR_SUCCESS) |
336 | || (*result == 0)) | |
7ce8671d JB |
337 | { |
338 | free (result); | |
339 | result = NULL; | |
340 | } | |
341 | } | |
342 | ||
343 | RegCloseKey (hrootkey); | |
344 | } | |
345 | ||
346 | return result; | |
347 | } | |
348 | ||
349 | /* | |
350 | getenv wrapper for Windows | |
351 | ||
a54af40e EZ |
352 | Value is allocated on the heap, and can be free'd. |
353 | ||
354 | This is needed to duplicate Emacs's behavior, which is to look for | |
355 | environment variables in the registry if they don't appear in the | |
356 | environment. */ | |
7ce8671d | 357 | char * |
7c3320d8 | 358 | w32_getenv (char *envvar) |
7ce8671d JB |
359 | { |
360 | char *value; | |
361 | DWORD dwType; | |
362 | ||
363 | if (value = getenv (envvar)) | |
a54af40e EZ |
364 | /* Found in the environment. strdup it, because values returned |
365 | by getenv cannot be free'd. */ | |
366 | return xstrdup (value); | |
7ce8671d JB |
367 | |
368 | if (! (value = w32_get_resource (HKEY_CURRENT_USER, envvar, &dwType)) && | |
369 | ! (value = w32_get_resource (HKEY_LOCAL_MACHINE, envvar, &dwType))) | |
d41784ee EZ |
370 | { |
371 | /* "w32console" is what Emacs on Windows uses for tty-type under -nw. */ | |
372 | if (strcmp (envvar, "TERM") == 0) | |
a54af40e | 373 | return xstrdup ("w32console"); |
d41784ee EZ |
374 | /* Found neither in the environment nor in the registry. */ |
375 | return NULL; | |
376 | } | |
7ce8671d JB |
377 | |
378 | if (dwType == REG_SZ) | |
379 | /* Registry; no need to expand. */ | |
380 | return value; | |
381 | ||
382 | if (dwType == REG_EXPAND_SZ) | |
383 | { | |
384 | DWORD size; | |
385 | ||
386 | if (size = ExpandEnvironmentStrings (value, NULL, 0)) | |
387 | { | |
388 | char *buffer = (char *) xmalloc (size); | |
389 | if (ExpandEnvironmentStrings (value, buffer, size)) | |
390 | { | |
391 | /* Found and expanded. */ | |
392 | free (value); | |
393 | return buffer; | |
394 | } | |
395 | ||
396 | /* Error expanding. */ | |
397 | free (buffer); | |
398 | } | |
399 | } | |
400 | ||
401 | /* Not the right type, or not correctly expanded. */ | |
402 | free (value); | |
403 | return NULL; | |
404 | } | |
405 | ||
ff90fbde | 406 | void |
7c3320d8 | 407 | w32_set_user_model_id (void) |
ff90fbde JR |
408 | { |
409 | HMODULE shell; | |
0a3472c7 | 410 | HRESULT (WINAPI * set_user_model) (wchar_t * id); |
ff90fbde JR |
411 | |
412 | /* On Windows 7 and later, we need to set the user model ID | |
413 | to associate emacsclient launched files with Emacs frames | |
414 | in the UI. */ | |
361358ea | 415 | shell = LoadLibrary ("shell32.dll"); |
ff90fbde JR |
416 | if (shell) |
417 | { | |
418 | set_user_model | |
419 | = (void *) GetProcAddress (shell, | |
420 | "SetCurrentProcessExplicitAppUserModelID"); | |
421 | /* If the function is defined, then we are running on Windows 7 | |
422 | or newer, and the UI uses this to group related windows | |
423 | together. Since emacs, runemacs, emacsclient are related, we | |
424 | want them grouped even though the executables are different, | |
425 | so we need to set a consistent ID between them. */ | |
426 | if (set_user_model) | |
427 | set_user_model (L"GNU.Emacs"); | |
428 | ||
429 | FreeLibrary (shell); | |
430 | } | |
431 | } | |
432 | ||
42073bfb | 433 | int |
7c3320d8 | 434 | w32_window_app (void) |
42073bfb JB |
435 | { |
436 | static int window_app = -1; | |
437 | char szTitle[MAX_PATH]; | |
438 | ||
439 | if (window_app < 0) | |
dbf60b07 JR |
440 | { |
441 | /* Checking for STDOUT does not work; it's a valid handle also in | |
442 | nonconsole apps. Testing for the console title seems to work. */ | |
443 | window_app = (GetConsoleTitleA (szTitle, MAX_PATH) == 0); | |
444 | if (window_app) | |
361358ea | 445 | InitCommonControls (); |
dbf60b07 | 446 | } |
42073bfb JB |
447 | |
448 | return window_app; | |
449 | } | |
105faa84 DN |
450 | |
451 | /* | |
cb06b8dc | 452 | execvp wrapper for Windows. Quotes arguments with embedded spaces. |
105faa84 DN |
453 | |
454 | This is necessary due to the broken implementation of exec* routines in | |
455 | the Microsoft libraries: they concatenate the arguments together without | |
456 | quoting special characters, and pass the result to CreateProcess, with | |
7c3320d8 | 457 | predictably bad results. By contrast, POSIX execvp passes the arguments |
105faa84 DN |
458 | directly into the argv array of the child process. |
459 | */ | |
460 | int | |
7c3320d8 | 461 | w32_execvp (const char *path, char **argv) |
105faa84 DN |
462 | { |
463 | int i; | |
464 | ||
465 | /* Required to allow a .BAT script as alternate editor. */ | |
466 | argv[0] = (char *) alternate_editor; | |
467 | ||
468 | for (i = 0; argv[i]; i++) | |
469 | if (strchr (argv[i], ' ')) | |
470 | { | |
471 | char *quoted = alloca (strlen (argv[i]) + 3); | |
472 | sprintf (quoted, "\"%s\"", argv[i]); | |
473 | argv[i] = quoted; | |
474 | } | |
475 | ||
476 | return execvp (path, argv); | |
477 | } | |
478 | ||
479 | #undef execvp | |
480 | #define execvp w32_execvp | |
481 | ||
d41784ee EZ |
482 | /* Emulation of ttyname for Windows. */ |
483 | char * | |
484 | ttyname (int fd) | |
485 | { | |
486 | return "CONOUT$"; | |
487 | } | |
488 | ||
105faa84 | 489 | #endif /* WINDOWSNT */ |
42073bfb | 490 | |
f387c1ee JB |
491 | /* Display a normal or error message. |
492 | On Windows, use a message box if compiled as a Windows app. */ | |
72b04a8a | 493 | static void message (int, const char *, ...) ATTRIBUTE_FORMAT_PRINTF (2, 3); |
b23b5a5b | 494 | static void |
b0bbc07d | 495 | message (int is_error, const char *format, ...) |
42073bfb | 496 | { |
361358ea | 497 | char msg[2048]; |
42073bfb JB |
498 | va_list args; |
499 | ||
b0bbc07d PE |
500 | va_start (args, format); |
501 | vsprintf (msg, format, args); | |
42073bfb JB |
502 | va_end (args); |
503 | ||
504 | #ifdef WINDOWSNT | |
505 | if (w32_window_app ()) | |
506 | { | |
507 | if (is_error) | |
508 | MessageBox (NULL, msg, "Emacsclient ERROR", MB_ICONERROR); | |
509 | else | |
510 | MessageBox (NULL, msg, "Emacsclient", MB_ICONINFORMATION); | |
511 | } | |
512 | else | |
513 | #endif | |
9219db75 JB |
514 | { |
515 | FILE *f = is_error ? stderr : stdout; | |
516 | ||
517 | fputs (msg, f); | |
518 | fflush (f); | |
519 | } | |
42073bfb JB |
520 | } |
521 | ||
8f9aaa0a | 522 | /* Decode the options from argv and argc. |
5212210c | 523 | The global variable `optind' will say how many arguments we used up. */ |
8f9aaa0a | 524 | |
b23b5a5b | 525 | static void |
873fbd0b | 526 | decode_options (int argc, char **argv) |
8f9aaa0a | 527 | { |
7ce8671d | 528 | alternate_editor = egetenv ("ALTERNATE_EDITOR"); |
0ea5797a | 529 | |
8f9aaa0a RS |
530 | while (1) |
531 | { | |
dc1cd5f7 | 532 | int opt = getopt_long_only (argc, argv, |
b03d27bd | 533 | #ifndef NO_SOCKETS_IN_FILE_SYSTEM |
3794a2d3 | 534 | "VHneqa:s:f:d:F:tc", |
b03d27bd | 535 | #else |
3794a2d3 | 536 | "VHneqa:f:d:F:tc", |
b03d27bd | 537 | #endif |
974b73e8 | 538 | longopts, 0); |
8f9aaa0a RS |
539 | |
540 | if (opt == EOF) | |
541 | break; | |
542 | ||
543 | switch (opt) | |
544 | { | |
545 | case 0: | |
546 | /* If getopt returns 0, then it has already processed a | |
547 | long-named option. We should do nothing. */ | |
548 | break; | |
e69233c2 | 549 | |
97e3214d GM |
550 | case 'a': |
551 | alternate_editor = optarg; | |
552 | break; | |
e69233c2 | 553 | |
b03d27bd | 554 | #ifndef NO_SOCKETS_IN_FILE_SYSTEM |
254107e4 RS |
555 | case 's': |
556 | socket_name = optarg; | |
557 | break; | |
b03d27bd | 558 | #endif |
254107e4 | 559 | |
aa0b6932 JB |
560 | case 'f': |
561 | server_file = optarg; | |
562 | break; | |
254107e4 | 563 | |
0ea5797a SM |
564 | /* We used to disallow this argument in w32, but it seems better |
565 | to allow it, for the occasional case where the user is | |
566 | connecting with a w32 client to a server compiled with X11 | |
567 | support. */ | |
30be2360 SM |
568 | case 'd': |
569 | display = optarg; | |
570 | break; | |
571 | ||
8f9aaa0a RS |
572 | case 'n': |
573 | nowait = 1; | |
574 | break; | |
575 | ||
30be2360 SM |
576 | case 'e': |
577 | eval = 1; | |
578 | break; | |
579 | ||
0b973fc5 DW |
580 | case 'q': |
581 | quiet = 1; | |
582 | break; | |
583 | ||
8f9aaa0a | 584 | case 'V': |
42073bfb | 585 | message (FALSE, "emacsclient %s\n", VERSION); |
65396510 | 586 | exit (EXIT_SUCCESS); |
8f9aaa0a | 587 | break; |
46cec291 | 588 | |
819b8f00 | 589 | case 't': |
77134727 | 590 | tty = 1; |
c1b8e896 | 591 | current_frame = 0; |
77134727 KL |
592 | break; |
593 | ||
e5299d8d | 594 | case 'c': |
31fa6595 | 595 | current_frame = 0; |
9628b887 | 596 | break; |
0b0d3e0b | 597 | |
0191e222 CY |
598 | case 'p': |
599 | parent_id = optarg; | |
600 | current_frame = 0; | |
601 | break; | |
602 | ||
8f9aaa0a | 603 | case 'H': |
8f9aaa0a | 604 | print_help_and_exit (); |
20c396e8 JB |
605 | break; |
606 | ||
18a4ce5e AR |
607 | case 'F': |
608 | frame_parameters = optarg; | |
609 | break; | |
610 | ||
20c396e8 | 611 | default: |
42073bfb | 612 | message (TRUE, "Try `%s --help' for more information\n", progname); |
65396510 | 613 | exit (EXIT_FAILURE); |
20c396e8 | 614 | break; |
8f9aaa0a RS |
615 | } |
616 | } | |
9628b887 | 617 | |
273b9028 CY |
618 | /* If the -c option is used (without -t) and no --display argument |
619 | is provided, try $DISPLAY. | |
620 | Without the -c option, we used to set `display' to $DISPLAY by | |
621 | default, but this changed the default behavior and is sometimes | |
622 | inconvenient. So we force users to use "--display $DISPLAY" if | |
623 | they want Emacs to connect to their current display. */ | |
09317bf4 | 624 | if (!current_frame && !tty && !display) |
e7534fc4 AR |
625 | { |
626 | display = egetenv ("DISPLAY"); | |
627 | #ifdef NS_IMPL_COCOA | |
628 | /* Under Cocoa, we don't really use displays the same way as in X, | |
629 | so provide a dummy. */ | |
630 | if (!display || strlen (display) == 0) | |
631 | display = "ns"; | |
632 | #endif | |
633 | } | |
9997dc15 | 634 | |
273b9028 | 635 | /* A null-string display is invalid. */ |
0ea5797a SM |
636 | if (display && strlen (display) == 0) |
637 | display = NULL; | |
638 | ||
273b9028 CY |
639 | /* If no display is available, new frames are tty frames. */ |
640 | if (!current_frame && !display) | |
92071250 | 641 | tty = 1; |
c0f342ab | 642 | |
b8ccaf6f | 643 | /* --no-wait implies --current-frame on ttys when there are file |
273b9028 | 644 | arguments or expressions given. */ |
b8ccaf6f | 645 | if (nowait && tty && argc - optind > 0) |
92071250 | 646 | current_frame = 1; |
802bdb3c DN |
647 | |
648 | #ifdef WINDOWSNT | |
69157c99 | 649 | if (alternate_editor && alternate_editor[0] == '\0') |
802bdb3c DN |
650 | { |
651 | message (TRUE, "--alternate-editor argument or ALTERNATE_EDITOR variable cannot be\n\ | |
652 | an empty string"); | |
653 | exit (EXIT_FAILURE); | |
654 | } | |
655 | #endif /* WINDOWSNT */ | |
8f9aaa0a RS |
656 | } |
657 | ||
974b73e8 | 658 | \f |
b23b5a5b | 659 | static void |
873fbd0b | 660 | print_help_and_exit (void) |
8f9aaa0a | 661 | { |
c0f342ab JB |
662 | /* Spaces and tabs are significant in this message; they're chosen so the |
663 | message aligns properly both in a tty and in a Windows message box. | |
664 | Please try to preserve them; otherwise the output is very hard to read | |
665 | when using emacsclientw. */ | |
42073bfb | 666 | message (FALSE, |
974b73e8 | 667 | "Usage: %s [OPTIONS] FILE...\n\ |
30be2360 SM |
668 | Tell the Emacs server to visit the specified files.\n\ |
669 | Every FILE can be either just a FILENAME or [+LINE[:COLUMN]] FILENAME.\n\ | |
20c396e8 | 670 | \n\ |
30be2360 | 671 | The following OPTIONS are accepted:\n\ |
c0f342ab JB |
672 | -V, --version Just print version info and return\n\ |
673 | -H, --help Print this usage information message\n\ | |
dc1cd5f7 | 674 | -nw, -t, --tty Open a new Emacs frame on the current terminal\n\ |
c4b858e3 | 675 | -c, --create-frame Create a new frame instead of trying to\n\ |
9dfda22f | 676 | use the current Emacs frame\n\ |
3794a2d3 | 677 | -F, --frame-parameters Set the parameters of a new frame\n\ |
c0f342ab | 678 | -e, --eval Evaluate the FILE arguments as ELisp expressions\n\ |
d07529f3 | 679 | -n, --no-wait Don't wait for the server to return\n\ |
0b973fc5 | 680 | -q, --quiet Don't display messages on success\n\ |
24d5010d | 681 | -d DISPLAY, --display=DISPLAY\n\ |
0191e222 CY |
682 | Visit the file in the given display\n\ |
683 | --parent-id=ID Open in parent window ID, via XEmbed\n" | |
aa0b6932 | 684 | #ifndef NO_SOCKETS_IN_FILE_SYSTEM |
24d5010d | 685 | "-s SOCKET, --socket-name=SOCKET\n\ |
9dfda22f | 686 | Set filename of the UNIX socket for communication\n" |
aa0b6932 | 687 | #endif |
24d5010d | 688 | "-f SERVER, --server-file=SERVER\n\ |
9dfda22f | 689 | Set filename of the TCP authentication file\n\ |
24d5010d | 690 | -a EDITOR, --alternate-editor=EDITOR\n\ |
9dfda22f | 691 | Editor to fallback to if the server is not running\n" |
6133a1a9 | 692 | #ifndef WINDOWSNT |
c3f995a2 JB |
693 | " If EDITOR is the empty string, start Emacs in daemon\n\ |
694 | mode and try connecting again\n" | |
6133a1a9 | 695 | #endif /* not WINDOWSNT */ |
c3f995a2 | 696 | "\n\ |
5f54cae6 | 697 | Report bugs with M-x report-emacs-bug.\n", progname); |
65396510 | 698 | exit (EXIT_SUCCESS); |
8f9aaa0a | 699 | } |
5212210c | 700 | |
aa0b6932 JB |
701 | /* |
702 | Try to run a different command, or --if no alternate editor is | |
703 | defined-- exit with an errorcode. | |
cb06b8dc | 704 | Uses argv, but gets it from the global variable main_argv. |
aa0b6932 | 705 | */ |
b23b5a5b | 706 | static void |
974b73e8 | 707 | fail (void) |
9002956f | 708 | { |
aa0b6932 | 709 | if (alternate_editor) |
9002956f | 710 | { |
aa0b6932 | 711 | int i = optind - 1; |
4472aef4 | 712 | |
974b73e8 | 713 | execvp (alternate_editor, main_argv + i); |
42073bfb | 714 | message (TRUE, "%s: error executing alternate editor \"%s\"\n", |
974b73e8 | 715 | progname, alternate_editor); |
9002956f | 716 | } |
aa0b6932 | 717 | exit (EXIT_FAILURE); |
9002956f KL |
718 | } |
719 | ||
aa0b6932 | 720 | \f |
1e7823d0 | 721 | #if !defined (HAVE_SOCKETS) || !defined (HAVE_INET_SOCKETS) |
9002956f | 722 | |
aa0b6932 | 723 | int |
0613f5d5 | 724 | main (int argc, char **argv) |
9002956f | 725 | { |
974b73e8 KL |
726 | main_argv = argv; |
727 | progname = argv[0]; | |
728 | message (TRUE, "%s: Sorry, the Emacs server is supported only\n" | |
c0f342ab | 729 | "on systems with Berkeley sockets.\n", |
aa0b6932 | 730 | argv[0]); |
974b73e8 | 731 | fail (); |
9002956f KL |
732 | } |
733 | ||
1e7823d0 | 734 | #else /* HAVE_SOCKETS && HAVE_INET_SOCKETS */ |
2828d5f9 | 735 | |
aa0b6932 JB |
736 | #define AUTH_KEY_LENGTH 64 |
737 | #define SEND_BUFFER_SIZE 4096 | |
738 | ||
873fbd0b | 739 | extern char *strerror (int); |
aa0b6932 JB |
740 | |
741 | /* Buffer to accumulate data to send in TCP connections. */ | |
742 | char send_buffer[SEND_BUFFER_SIZE + 1]; | |
743 | int sblen = 0; /* Fill pointer for the send buffer. */ | |
4b7b77f6 JR |
744 | /* Socket used to communicate with the Emacs server process. */ |
745 | HSOCKET emacs_socket = 0; | |
aa0b6932 | 746 | |
d22b00e5 JR |
747 | /* On Windows, the socket library was historically separate from the standard |
748 | C library, so errors are handled differently. */ | |
b23b5a5b | 749 | static void |
988e88ab | 750 | sock_err_message (const char *function_name) |
d22b00e5 JR |
751 | { |
752 | #ifdef WINDOWSNT | |
753 | char* msg = NULL; | |
754 | ||
755 | FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | |
756 | | FORMAT_MESSAGE_ALLOCATE_BUFFER | |
757 | | FORMAT_MESSAGE_ARGUMENT_ARRAY, | |
758 | NULL, WSAGetLastError (), 0, (LPTSTR)&msg, 0, NULL); | |
759 | ||
760 | message (TRUE, "%s: %s: %s\n", progname, function_name, msg); | |
761 | ||
762 | LocalFree (msg); | |
763 | #else | |
764 | message (TRUE, "%s: %s: %s\n", progname, function_name, strerror (errno)); | |
765 | #endif | |
766 | } | |
767 | ||
768 | ||
aa0b6932 JB |
769 | /* Let's send the data to Emacs when either |
770 | - the data ends in "\n", or | |
771 | - the buffer is full (but this shouldn't happen) | |
772 | Otherwise, we just accumulate it. */ | |
b23b5a5b | 773 | static void |
988e88ab | 774 | send_to_emacs (HSOCKET s, const char *data) |
aa0b6932 JB |
775 | { |
776 | while (data) | |
2828d5f9 | 777 | { |
728a982d | 778 | size_t dlen = strlen (data); |
aa0b6932 JB |
779 | if (dlen + sblen >= SEND_BUFFER_SIZE) |
780 | { | |
781 | int part = SEND_BUFFER_SIZE - sblen; | |
782 | strncpy (&send_buffer[sblen], data, part); | |
783 | data += part; | |
784 | sblen = SEND_BUFFER_SIZE; | |
785 | } | |
786 | else if (dlen) | |
787 | { | |
788 | strcpy (&send_buffer[sblen], data); | |
789 | data = NULL; | |
790 | sblen += dlen; | |
791 | } | |
792 | else | |
793 | break; | |
794 | ||
795 | if (sblen == SEND_BUFFER_SIZE | |
796 | || (sblen > 0 && send_buffer[sblen-1] == '\n')) | |
797 | { | |
798 | int sent = send (s, send_buffer, sblen, 0); | |
799 | if (sent != sblen) | |
800 | strcpy (send_buffer, &send_buffer[sent]); | |
801 | sblen -= sent; | |
802 | } | |
2828d5f9 | 803 | } |
2828d5f9 | 804 | } |
2828d5f9 KL |
805 | |
806 | \f | |
0b0d3e0b | 807 | /* In STR, insert a & before each &, each space, each newline, and |
a4cf2096 | 808 | any initial -. Change spaces to underscores, too, so that the |
0b0d3e0b KL |
809 | return value never contains a space. |
810 | ||
3a35a84c | 811 | Does not change the string. Outputs the result to S. */ |
b23b5a5b | 812 | static void |
988e88ab | 813 | quote_argument (HSOCKET s, const char *str) |
5212210c | 814 | { |
9002956f | 815 | char *copy = (char *) xmalloc (strlen (str) * 2 + 1); |
988e88ab J |
816 | const char *p; |
817 | char *q; | |
5212210c | 818 | |
0b0d3e0b | 819 | p = str; |
5212210c RS |
820 | q = copy; |
821 | while (*p) | |
822 | { | |
823 | if (*p == ' ') | |
824 | { | |
f1db6a73 | 825 | *q++ = '&'; |
5212210c RS |
826 | *q++ = '_'; |
827 | p++; | |
828 | } | |
3cf8c6aa SM |
829 | else if (*p == '\n') |
830 | { | |
831 | *q++ = '&'; | |
832 | *q++ = 'n'; | |
833 | p++; | |
834 | } | |
5212210c RS |
835 | else |
836 | { | |
0b0d3e0b | 837 | if (*p == '&' || (*p == '-' && p == str)) |
f1db6a73 | 838 | *q++ = '&'; |
5212210c RS |
839 | *q++ = *p++; |
840 | } | |
841 | } | |
f1db6a73 | 842 | *q++ = 0; |
5212210c | 843 | |
486ba65f | 844 | send_to_emacs (s, copy); |
87209357 EZ |
845 | |
846 | free (copy); | |
5212210c | 847 | } |
0c76956f | 848 | |
0b0d3e0b KL |
849 | |
850 | /* The inverse of quote_argument. Removes quoting in string STR by | |
851 | modifying the string in place. Returns STR. */ | |
852 | ||
b23b5a5b | 853 | static char * |
873fbd0b | 854 | unquote_argument (char *str) |
0b0d3e0b KL |
855 | { |
856 | char *p, *q; | |
857 | ||
858 | if (! str) | |
859 | return str; | |
860 | ||
861 | p = str; | |
862 | q = str; | |
863 | while (*p) | |
864 | { | |
865 | if (*p == '&') | |
866 | { | |
867 | p++; | |
868 | if (*p == '&') | |
869 | *p = '&'; | |
870 | else if (*p == '_') | |
871 | *p = ' '; | |
872 | else if (*p == 'n') | |
873 | *p = '\n'; | |
874 | else if (*p == '-') | |
875 | *p = '-'; | |
876 | } | |
877 | *q++ = *p++; | |
878 | } | |
879 | *q = 0; | |
880 | return str; | |
881 | } | |
882 | ||
8f9aaa0a | 883 | \f |
b23b5a5b | 884 | static int |
03fc768b | 885 | file_name_absolute_p (const char *filename) |
b03d27bd JB |
886 | { |
887 | /* Sanity check, it shouldn't happen. */ | |
888 | if (! filename) return FALSE; | |
889 | ||
890 | /* /xxx is always an absolute path. */ | |
891 | if (filename[0] == '/') return TRUE; | |
892 | ||
893 | /* Empty filenames (which shouldn't happen) are relative. */ | |
894 | if (filename[0] == '\0') return FALSE; | |
895 | ||
896 | #ifdef WINDOWSNT | |
71b8f735 | 897 | /* X:\xxx is always absolute. */ |
03fc768b | 898 | if (isalpha ((unsigned char) filename[0]) |
cb0297bb | 899 | && filename[1] == ':' && (filename[2] == '\\' || filename[2] == '/')) |
b03d27bd JB |
900 | return TRUE; |
901 | ||
902 | /* Both \xxx and \\xxx\yyy are absolute. */ | |
903 | if (filename[0] == '\\') return TRUE; | |
904 | #endif | |
905 | ||
906 | return FALSE; | |
907 | } | |
908 | ||
aa0b6932 | 909 | #ifdef WINDOWSNT |
f0384499 | 910 | /* Wrapper to make WSACleanup a cdecl, as required by atexit. */ |
7c3320d8 JB |
911 | void __cdecl |
912 | close_winsock (void) | |
aa0b6932 JB |
913 | { |
914 | WSACleanup (); | |
915 | } | |
0c76956f | 916 | |
b03d27bd JB |
917 | /* Initialize the WinSock2 library. */ |
918 | void | |
7c3320d8 | 919 | initialize_sockets (void) |
0c76956f | 920 | { |
aa0b6932 JB |
921 | WSADATA wsaData; |
922 | ||
aa0b6932 JB |
923 | if (WSAStartup (MAKEWORD (2, 0), &wsaData)) |
924 | { | |
78da39c6 | 925 | message (TRUE, "%s: error initializing WinSock2\n", progname); |
aa0b6932 JB |
926 | exit (EXIT_FAILURE); |
927 | } | |
928 | ||
929 | atexit (close_winsock); | |
0c76956f | 930 | } |
e35fc962 | 931 | #endif /* WINDOWSNT */ |
974b73e8 | 932 | |
8f9aaa0a | 933 | \f |
97e3214d | 934 | /* |
aa0b6932 | 935 | * Read the information needed to set up a TCP comm channel with |
968ef9b4 | 936 | * the Emacs server: host, port, and authentication string. |
0e0dced5 | 937 | */ |
b23b5a5b | 938 | static int |
873fbd0b | 939 | get_server_config (struct sockaddr_in *server, char *authentication) |
97e3214d | 940 | { |
aa0b6932 JB |
941 | char dotted[32]; |
942 | char *port; | |
b03d27bd | 943 | FILE *config = NULL; |
aa0b6932 | 944 | |
b03d27bd JB |
945 | if (file_name_absolute_p (server_file)) |
946 | config = fopen (server_file, "rb"); | |
947 | else | |
97e3214d | 948 | { |
b9170155 | 949 | const char *home = egetenv ("HOME"); |
88b46d84 | 950 | |
aa0b6932 JB |
951 | if (home) |
952 | { | |
bc558f3e JB |
953 | char *path = alloca (strlen (home) + strlen (server_file) |
954 | + EXTRA_SPACE); | |
aa0b6932 JB |
955 | sprintf (path, "%s/.emacs.d/server/%s", home, server_file); |
956 | config = fopen (path, "rb"); | |
957 | } | |
88b46d84 | 958 | #ifdef WINDOWSNT |
7ce8671d | 959 | if (!config && (home = egetenv ("APPDATA"))) |
88b46d84 | 960 | { |
bc558f3e JB |
961 | char *path = alloca (strlen (home) + strlen (server_file) |
962 | + EXTRA_SPACE); | |
88b46d84 JB |
963 | sprintf (path, "%s/.emacs.d/server/%s", home, server_file); |
964 | config = fopen (path, "rb"); | |
965 | } | |
966 | #endif | |
aa0b6932 JB |
967 | } |
968 | ||
969 | if (! config) | |
970 | return FALSE; | |
971 | ||
972 | if (fgets (dotted, sizeof dotted, config) | |
968ef9b4 JB |
973 | && (port = strchr (dotted, ':'))) |
974 | *port++ = '\0'; | |
97e3214d GM |
975 | else |
976 | { | |
78da39c6 | 977 | message (TRUE, "%s: invalid configuration info\n", progname); |
65396510 | 978 | exit (EXIT_FAILURE); |
97e3214d | 979 | } |
97e3214d | 980 | |
aa0b6932 JB |
981 | server->sin_family = AF_INET; |
982 | server->sin_addr.s_addr = inet_addr (dotted); | |
983 | server->sin_port = htons (atoi (port)); | |
97e3214d | 984 | |
aa0b6932 JB |
985 | if (! fread (authentication, AUTH_KEY_LENGTH, 1, config)) |
986 | { | |
78da39c6 | 987 | message (TRUE, "%s: cannot read authentication info\n", progname); |
aa0b6932 JB |
988 | exit (EXIT_FAILURE); |
989 | } | |
46cec291 | 990 | |
aa0b6932 | 991 | fclose (config); |
97e3214d | 992 | |
aa0b6932 | 993 | return TRUE; |
97e3214d GM |
994 | } |
995 | ||
b23b5a5b | 996 | static HSOCKET |
873fbd0b | 997 | set_tcp_socket (void) |
aa0b6932 JB |
998 | { |
999 | HSOCKET s; | |
1000 | struct sockaddr_in server; | |
aa0b6932 JB |
1001 | struct linger l_arg = {1, 1}; |
1002 | char auth_string[AUTH_KEY_LENGTH + 1]; | |
9628b887 | 1003 | |
aa0b6932 JB |
1004 | if (! get_server_config (&server, auth_string)) |
1005 | return INVALID_SOCKET; | |
1006 | ||
0b973fc5 | 1007 | if (server.sin_addr.s_addr != inet_addr ("127.0.0.1") && !quiet) |
9219db75 | 1008 | message (FALSE, "%s: connected to remote socket at %s\n", |
b03d27bd JB |
1009 | progname, inet_ntoa (server.sin_addr)); |
1010 | ||
aa0b6932 JB |
1011 | /* |
1012 | * Open up an AF_INET socket | |
1013 | */ | |
1014 | if ((s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) | |
1015 | { | |
d22b00e5 | 1016 | sock_err_message ("socket"); |
aa0b6932 JB |
1017 | return INVALID_SOCKET; |
1018 | } | |
1019 | ||
1020 | /* | |
1021 | * Set up the socket | |
1022 | */ | |
1023 | if (connect (s, (struct sockaddr *) &server, sizeof server) < 0) | |
1024 | { | |
d22b00e5 | 1025 | sock_err_message ("connect"); |
aa0b6932 JB |
1026 | return INVALID_SOCKET; |
1027 | } | |
1028 | ||
aa0b6932 JB |
1029 | setsockopt (s, SOL_SOCKET, SO_LINGER, (char *) &l_arg, sizeof l_arg); |
1030 | ||
1031 | /* | |
1032 | * Send the authentication | |
1033 | */ | |
1034 | auth_string[AUTH_KEY_LENGTH] = '\0'; | |
1035 | ||
486ba65f JR |
1036 | send_to_emacs (s, "-auth "); |
1037 | send_to_emacs (s, auth_string); | |
5ab73228 | 1038 | send_to_emacs (s, " "); |
aa0b6932 JB |
1039 | |
1040 | return s; | |
1041 | } | |
1042 | ||
b2ff54a0 JR |
1043 | |
1044 | /* Returns 1 if PREFIX is a prefix of STRING. */ | |
1045 | static int | |
988e88ab | 1046 | strprefix (const char *prefix, const char *string) |
b2ff54a0 | 1047 | { |
cb06b8dc | 1048 | return !strncmp (prefix, string, strlen (prefix)); |
b2ff54a0 JR |
1049 | } |
1050 | ||
a336ee5b CY |
1051 | /* Get tty name and type. If successful, return the type in TTY_TYPE |
1052 | and the name in TTY_NAME, and return 1. Otherwise, fail if NOABORT | |
1053 | is zero, or return 0 if NOABORT is non-zero. */ | |
1054 | ||
b23b5a5b | 1055 | static int |
b9170155 | 1056 | find_tty (const char **tty_type, const char **tty_name, int noabort) |
a336ee5b | 1057 | { |
b9170155 PE |
1058 | const char *type = egetenv ("TERM"); |
1059 | const char *name = ttyname (fileno (stdout)); | |
a336ee5b CY |
1060 | |
1061 | if (!name) | |
1062 | { | |
1063 | if (noabort) | |
1064 | return 0; | |
1065 | else | |
1066 | { | |
1067 | message (TRUE, "%s: could not get terminal name\n", progname); | |
1068 | fail (); | |
1069 | } | |
1070 | } | |
1071 | ||
1072 | if (!type) | |
1073 | { | |
1074 | if (noabort) | |
1075 | return 0; | |
1076 | else | |
1077 | { | |
1078 | message (TRUE, "%s: please set the TERM variable to your terminal type\n", | |
1079 | progname); | |
1080 | fail (); | |
1081 | } | |
1082 | } | |
1083 | ||
1084 | if (strcmp (type, "eterm") == 0) | |
1085 | { | |
1086 | if (noabort) | |
1087 | return 0; | |
1088 | else | |
1089 | { | |
1090 | /* This causes nasty, MULTI_KBOARD-related input lockouts. */ | |
1091 | message (TRUE, "%s: opening a frame in an Emacs term buffer" | |
1092 | " is not supported\n", progname); | |
1093 | fail (); | |
1094 | } | |
1095 | } | |
1096 | ||
1097 | *tty_name = name; | |
1098 | *tty_type = type; | |
1099 | return 1; | |
1100 | } | |
1101 | ||
b2ff54a0 | 1102 | |
aa0b6932 | 1103 | #if !defined (NO_SOCKETS_IN_FILE_SYSTEM) |
46cec291 | 1104 | |
9f637eea GM |
1105 | /* Three possibilities: |
1106 | 2 - can't be `stat'ed (sets errno) | |
1107 | 1 - isn't owned by us | |
1108 | 0 - success: none of the above */ | |
1109 | ||
1110 | static int | |
fe91f5a0 | 1111 | socket_status (const char *name) |
9f637eea GM |
1112 | { |
1113 | struct stat statbfr; | |
1114 | ||
b0bbc07d | 1115 | if (stat (name, &statbfr) == -1) |
9f637eea GM |
1116 | return 2; |
1117 | ||
1118 | if (statbfr.st_uid != geteuid ()) | |
1119 | return 1; | |
0b0d3e0b | 1120 | |
9f637eea GM |
1121 | return 0; |
1122 | } | |
1123 | ||
974b73e8 | 1124 | \f |
da8e1115 KL |
1125 | /* A signal handler that passes the signal to the Emacs process. |
1126 | Useful for SIGWINCH. */ | |
1127 | ||
9af30bdf | 1128 | static void |
428a555e | 1129 | pass_signal_to_emacs (int signalnum) |
9628b887 KL |
1130 | { |
1131 | int old_errno = errno; | |
1132 | ||
9f729af5 | 1133 | if (emacs_pid) |
428a555e | 1134 | kill (emacs_pid, signalnum); |
9f729af5 | 1135 | |
428a555e | 1136 | signal (signalnum, pass_signal_to_emacs); |
9f729af5 KL |
1137 | errno = old_errno; |
1138 | } | |
1139 | ||
0b0d3e0b KL |
1140 | /* Signal handler for SIGCONT; notify the Emacs process that it can |
1141 | now resume our tty frame. */ | |
1142 | ||
9af30bdf | 1143 | static void |
0b0d3e0b KL |
1144 | handle_sigcont (int signalnum) |
1145 | { | |
1146 | int old_errno = errno; | |
1147 | ||
1148 | if (tcgetpgrp (1) == getpgrp ()) | |
1149 | { | |
1150 | /* We are in the foreground. */ | |
486ba65f | 1151 | send_to_emacs (emacs_socket, "-resume \n"); |
0b0d3e0b KL |
1152 | } |
1153 | else | |
1154 | { | |
1155 | /* We are in the background; cancel the continue. */ | |
1156 | kill (getpid (), SIGSTOP); | |
1157 | } | |
c6c53c3e KL |
1158 | |
1159 | signal (signalnum, handle_sigcont); | |
0b0d3e0b KL |
1160 | errno = old_errno; |
1161 | } | |
1162 | ||
1163 | /* Signal handler for SIGTSTP; notify the Emacs process that we are | |
1164 | going to sleep. Normally the suspend is initiated by Emacs via | |
1165 | server-handle-suspend-tty, but if the server gets out of sync with | |
1166 | reality, we may get a SIGTSTP on C-z. Handling this signal and | |
1167 | notifying Emacs about it should get things under control again. */ | |
1168 | ||
9af30bdf | 1169 | static void |
0b0d3e0b KL |
1170 | handle_sigtstp (int signalnum) |
1171 | { | |
1172 | int old_errno = errno; | |
1173 | sigset_t set; | |
c0f342ab | 1174 | |
90843190 | 1175 | if (emacs_socket) |
486ba65f | 1176 | send_to_emacs (emacs_socket, "-suspend \n"); |
0b0d3e0b | 1177 | |
5ab73228 | 1178 | /* Unblock this signal and call the default handler by temporarily |
0b0d3e0b KL |
1179 | changing the handler and resignalling. */ |
1180 | sigprocmask (SIG_BLOCK, NULL, &set); | |
1181 | sigdelset (&set, signalnum); | |
1182 | signal (signalnum, SIG_DFL); | |
1183 | kill (getpid (), signalnum); | |
1184 | sigprocmask (SIG_SETMASK, &set, NULL); /* Let's the above signal through. */ | |
1185 | signal (signalnum, handle_sigtstp); | |
1186 | ||
1187 | errno = old_errno; | |
1188 | } | |
273b9028 CY |
1189 | |
1190 | ||
da8e1115 | 1191 | /* Set up signal handlers before opening a frame on the current tty. */ |
0b0d3e0b | 1192 | |
b23b5a5b | 1193 | static void |
4d553a13 | 1194 | init_signals (void) |
9628b887 KL |
1195 | { |
1196 | /* Set up signal handlers. */ | |
428a555e | 1197 | signal (SIGWINCH, pass_signal_to_emacs); |
4ca927b4 KL |
1198 | |
1199 | /* Don't pass SIGINT and SIGQUIT to Emacs, because it has no way of | |
1200 | deciding which terminal the signal came from. C-g is now a | |
1201 | normal input event on secondary terminals. */ | |
1202 | #if 0 | |
428a555e KL |
1203 | signal (SIGINT, pass_signal_to_emacs); |
1204 | signal (SIGQUIT, pass_signal_to_emacs); | |
4ca927b4 | 1205 | #endif |
0b0d3e0b KL |
1206 | |
1207 | signal (SIGCONT, handle_sigcont); | |
1208 | signal (SIGTSTP, handle_sigtstp); | |
1209 | signal (SIGTTOU, handle_sigtstp); | |
9628b887 KL |
1210 | } |
1211 | ||
46cec291 | 1212 | |
b23b5a5b | 1213 | static HSOCKET |
873fbd0b | 1214 | set_local_socket (void) |
46cec291 | 1215 | { |
aa0b6932 | 1216 | HSOCKET s; |
46cec291 | 1217 | struct sockaddr_un server; |
46cec291 | 1218 | |
e69233c2 | 1219 | /* |
46cec291 RS |
1220 | * Open up an AF_UNIX socket in this person's home directory |
1221 | */ | |
1222 | ||
1223 | if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) | |
1224 | { | |
42073bfb | 1225 | message (TRUE, "%s: socket: %s\n", progname, strerror (errno)); |
aa0b6932 | 1226 | return INVALID_SOCKET; |
46cec291 | 1227 | } |
e69233c2 | 1228 | |
46cec291 | 1229 | server.sun_family = AF_UNIX; |
c5fee545 | 1230 | |
c5fee545 | 1231 | { |
9f637eea | 1232 | int sock_status = 0; |
5c9659d3 | 1233 | int default_sock = !socket_name; |
b80bf66e | 1234 | int saved_errno = 0; |
988e88ab | 1235 | const char *server_name = "server"; |
4be3dfc5 | 1236 | const char *tmpdir IF_LINT ( = NULL); |
0b0d3e0b | 1237 | |
8966b757 AS |
1238 | if (socket_name && !strchr (socket_name, '/') |
1239 | && !strchr (socket_name, '\\')) | |
1240 | { | |
1241 | /* socket_name is a file name component. */ | |
d3a6748c KL |
1242 | server_name = socket_name; |
1243 | socket_name = NULL; | |
1244 | default_sock = 1; /* Try both UIDs. */ | |
0734b0d0 | 1245 | } |
efb859b4 | 1246 | |
5c9659d3 | 1247 | if (default_sock) |
254107e4 | 1248 | { |
f77b11a0 JB |
1249 | tmpdir = egetenv ("TMPDIR"); |
1250 | if (!tmpdir) | |
b336bfcd GM |
1251 | { |
1252 | #ifdef DARWIN_OS | |
e6068ab3 YM |
1253 | #ifndef _CS_DARWIN_USER_TEMP_DIR |
1254 | #define _CS_DARWIN_USER_TEMP_DIR 65537 | |
1255 | #endif | |
b336bfcd GM |
1256 | size_t n = confstr (_CS_DARWIN_USER_TEMP_DIR, NULL, (size_t) 0); |
1257 | if (n > 0) | |
1258 | { | |
1259 | tmpdir = alloca (n); | |
1260 | confstr (_CS_DARWIN_USER_TEMP_DIR, tmpdir, n); | |
1261 | } | |
1262 | else | |
1263 | #endif | |
1264 | tmpdir = "/tmp"; | |
1265 | } | |
bc558f3e JB |
1266 | socket_name = alloca (strlen (tmpdir) + strlen (server_name) |
1267 | + EXTRA_SPACE); | |
f77b11a0 JB |
1268 | sprintf (socket_name, "%s/emacs%d/%s", |
1269 | tmpdir, (int) geteuid (), server_name); | |
254107e4 RS |
1270 | } |
1271 | ||
1272 | if (strlen (socket_name) < sizeof (server.sun_path)) | |
1273 | strcpy (server.sun_path, socket_name); | |
1274 | else | |
819b8f00 | 1275 | { |
78da39c6 | 1276 | message (TRUE, "%s: socket-name %s too long\n", |
974b73e8 | 1277 | progname, socket_name); |
819b8f00 KL |
1278 | fail (); |
1279 | } | |
efb859b4 | 1280 | |
9f637eea GM |
1281 | /* See if the socket exists, and if it's owned by us. */ |
1282 | sock_status = socket_status (server.sun_path); | |
152b6e83 | 1283 | saved_errno = errno; |
5c9659d3 | 1284 | if (sock_status && default_sock) |
efb859b4 | 1285 | { |
9f637eea GM |
1286 | /* Failing that, see if LOGNAME or USER exist and differ from |
1287 | our euid. If so, look for a socket based on the UID | |
1288 | associated with the name. This is reminiscent of the logic | |
1289 | that init_editfns uses to set the global Vuser_full_name. */ | |
e69233c2 | 1290 | |
b9170155 | 1291 | const char *user_name = egetenv ("LOGNAME"); |
293f9f2a | 1292 | |
9f637eea | 1293 | if (!user_name) |
b9170155 | 1294 | user_name = egetenv ("USER"); |
e69233c2 | 1295 | |
9f637eea GM |
1296 | if (user_name) |
1297 | { | |
1298 | struct passwd *pw = getpwnam (user_name); | |
293f9f2a | 1299 | |
9f637eea GM |
1300 | if (pw && (pw->pw_uid != geteuid ())) |
1301 | { | |
1302 | /* We're running under su, apparently. */ | |
bc558f3e JB |
1303 | socket_name = alloca (strlen (tmpdir) + strlen (server_name) |
1304 | + EXTRA_SPACE); | |
f77b11a0 JB |
1305 | sprintf (socket_name, "%s/emacs%d/%s", |
1306 | tmpdir, (int) pw->pw_uid, server_name); | |
5c9659d3 SM |
1307 | |
1308 | if (strlen (socket_name) < sizeof (server.sun_path)) | |
1309 | strcpy (server.sun_path, socket_name); | |
1310 | else | |
1311 | { | |
78da39c6 | 1312 | message (TRUE, "%s: socket-name %s too long\n", |
aa0b6932 | 1313 | progname, socket_name); |
65396510 | 1314 | exit (EXIT_FAILURE); |
5c9659d3 SM |
1315 | } |
1316 | ||
9f637eea | 1317 | sock_status = socket_status (server.sun_path); |
b80bf66e | 1318 | saved_errno = errno; |
9f637eea | 1319 | } |
293f9f2a RS |
1320 | else |
1321 | errno = saved_errno; | |
9f637eea | 1322 | } |
efb859b4 | 1323 | } |
e69233c2 | 1324 | |
aa0b6932 JB |
1325 | switch (sock_status) |
1326 | { | |
1327 | case 1: | |
974b73e8 KL |
1328 | /* There's a socket, but it isn't owned by us. This is OK if |
1329 | we are root. */ | |
1330 | if (0 != geteuid ()) | |
1331 | { | |
1332 | message (TRUE, "%s: Invalid socket owner\n", progname); | |
aa0b6932 | 1333 | return INVALID_SOCKET; |
974b73e8 KL |
1334 | } |
1335 | break; | |
aa0b6932 JB |
1336 | |
1337 | case 2: | |
974b73e8 KL |
1338 | /* `stat' failed */ |
1339 | if (saved_errno == ENOENT) | |
1340 | message (TRUE, | |
1341 | "%s: can't find socket; have you started the server?\n\ | |
45adde32 | 1342 | To start the server in Emacs, type \"M-x server-start\".\n", |
aa0b6932 | 1343 | progname); |
974b73e8 KL |
1344 | else |
1345 | message (TRUE, "%s: can't stat %s: %s\n", | |
aa0b6932 | 1346 | progname, server.sun_path, strerror (saved_errno)); |
974b73e8 | 1347 | return INVALID_SOCKET; |
aa0b6932 | 1348 | } |
efb859b4 | 1349 | } |
46cec291 | 1350 | |
4e23f2ba JB |
1351 | if (connect (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2) |
1352 | < 0) | |
46cec291 | 1353 | { |
42073bfb | 1354 | message (TRUE, "%s: connect: %s\n", progname, strerror (errno)); |
aa0b6932 | 1355 | return INVALID_SOCKET; |
46cec291 | 1356 | } |
23a7488d | 1357 | |
aa0b6932 JB |
1358 | return s; |
1359 | } | |
1360 | #endif /* ! NO_SOCKETS_IN_FILE_SYSTEM */ | |
1361 | ||
b23b5a5b | 1362 | static HSOCKET |
636b507b | 1363 | set_socket (int no_exit_if_error) |
aa0b6932 | 1364 | { |
b03d27bd | 1365 | HSOCKET s; |
c0f342ab | 1366 | |
b03d27bd | 1367 | INITIALIZE (); |
c0f342ab | 1368 | |
aa0b6932 | 1369 | #ifndef NO_SOCKETS_IN_FILE_SYSTEM |
b03d27bd JB |
1370 | /* Explicit --socket-name argument. */ |
1371 | if (socket_name) | |
46cec291 | 1372 | { |
b03d27bd | 1373 | s = set_local_socket (); |
636b507b | 1374 | if ((s != INVALID_SOCKET) || no_exit_if_error) |
974b73e8 | 1375 | return s; |
355a326e | 1376 | message (TRUE, "%s: error accessing socket \"%s\"\n", |
974b73e8 | 1377 | progname, socket_name); |
b03d27bd | 1378 | exit (EXIT_FAILURE); |
46cec291 | 1379 | } |
b03d27bd JB |
1380 | #endif |
1381 | ||
1382 | /* Explicit --server-file arg or EMACS_SERVER_FILE variable. */ | |
1383 | if (!server_file) | |
7ce8671d | 1384 | server_file = egetenv ("EMACS_SERVER_FILE"); |
46cec291 | 1385 | |
b03d27bd | 1386 | if (server_file) |
23a7488d | 1387 | { |
b03d27bd | 1388 | s = set_tcp_socket (); |
636b507b | 1389 | if ((s != INVALID_SOCKET) || no_exit_if_error) |
974b73e8 | 1390 | return s; |
c0f342ab | 1391 | |
78da39c6 | 1392 | message (TRUE, "%s: error accessing server file \"%s\"\n", |
974b73e8 | 1393 | progname, server_file); |
b03d27bd | 1394 | exit (EXIT_FAILURE); |
23a7488d | 1395 | } |
c0f342ab | 1396 | |
b03d27bd JB |
1397 | #ifndef NO_SOCKETS_IN_FILE_SYSTEM |
1398 | /* Implicit local socket. */ | |
1399 | s = set_local_socket (); | |
1400 | if (s != INVALID_SOCKET) | |
1401 | return s; | |
1402 | #endif | |
23a7488d | 1403 | |
b03d27bd JB |
1404 | /* Implicit server file. */ |
1405 | server_file = "server"; | |
1406 | s = set_tcp_socket (); | |
636b507b | 1407 | if ((s != INVALID_SOCKET) || no_exit_if_error) |
b03d27bd JB |
1408 | return s; |
1409 | ||
1410 | /* No implicit or explicit socket, and no alternate editor. */ | |
42073bfb | 1411 | message (TRUE, "%s: No socket or alternate editor. Please use:\n\n" |
b03d27bd JB |
1412 | #ifndef NO_SOCKETS_IN_FILE_SYSTEM |
1413 | "\t--socket-name\n" | |
ee6a193c | 1414 | #endif |
b03d27bd JB |
1415 | "\t--server-file (or environment variable EMACS_SERVER_FILE)\n\ |
1416 | \t--alternate-editor (or environment variable ALTERNATE_EDITOR)\n", | |
1417 | progname); | |
1418 | exit (EXIT_FAILURE); | |
aa0b6932 | 1419 | } |
46cec291 | 1420 | |
0e0dced5 JB |
1421 | #ifdef WINDOWSNT |
1422 | FARPROC set_fg; /* Pointer to AllowSetForegroundWindow. */ | |
1423 | FARPROC get_wc; /* Pointer to RealGetWindowClassA. */ | |
1424 | ||
1425 | BOOL CALLBACK | |
7c3320d8 | 1426 | w32_find_emacs_process (HWND hWnd, LPARAM lParam) |
0e0dced5 JB |
1427 | { |
1428 | DWORD pid; | |
1429 | char class[6]; | |
1430 | ||
1431 | /* Reject any window not of class "Emacs". */ | |
1432 | if (! get_wc (hWnd, class, sizeof (class)) | |
1433 | || strcmp (class, "Emacs")) | |
1434 | return TRUE; | |
1435 | ||
1436 | /* We only need the process id, not the thread id. */ | |
1437 | (void) GetWindowThreadProcessId (hWnd, &pid); | |
1438 | ||
1439 | /* Not the one we're looking for. */ | |
1440 | if (pid != (DWORD) emacs_pid) return TRUE; | |
1441 | ||
1442 | /* OK, let's raise it. */ | |
1443 | set_fg (emacs_pid); | |
1444 | ||
1445 | /* Stop enumeration. */ | |
1446 | return FALSE; | |
1447 | } | |
1448 | ||
1449 | /* | |
1450 | * Search for a window of class "Emacs" and owned by a process with | |
1451 | * process id = emacs_pid. If found, allow it to grab the focus. | |
1452 | */ | |
1453 | void | |
7c3320d8 | 1454 | w32_give_focus (void) |
0e0dced5 | 1455 | { |
ff90fbde | 1456 | HANDLE user32; |
0e0dced5 | 1457 | |
71b8f735 | 1458 | /* It shouldn't happen when dealing with TCP sockets. */ |
0e0dced5 JB |
1459 | if (!emacs_pid) return; |
1460 | ||
ff90fbde JR |
1461 | user32 = GetModuleHandle ("user32.dll"); |
1462 | ||
1463 | if (!user32) | |
1464 | return; | |
0e0dced5 JB |
1465 | |
1466 | /* Modern Windows restrict which processes can set the foreground window. | |
1467 | emacsclient can allow Emacs to grab the focus by calling the function | |
1468 | AllowSetForegroundWindow. Unfortunately, older Windows (W95, W98 and | |
1469 | NT) lack this function, so we have to check its availability. */ | |
ff90fbde JR |
1470 | if ((set_fg = GetProcAddress (user32, "AllowSetForegroundWindow")) |
1471 | && (get_wc = GetProcAddress (user32, "RealGetWindowClassA"))) | |
0e0dced5 | 1472 | EnumWindows (w32_find_emacs_process, (LPARAM) 0); |
0e0dced5 JB |
1473 | } |
1474 | #endif | |
1475 | ||
636b507b DN |
1476 | /* Start the emacs daemon and try to connect to it. */ |
1477 | ||
b23b5a5b | 1478 | static void |
636b507b DN |
1479 | start_daemon_and_retry_set_socket (void) |
1480 | { | |
802bdb3c | 1481 | #ifndef WINDOWSNT |
636b507b DN |
1482 | pid_t dpid; |
1483 | int status; | |
636b507b DN |
1484 | |
1485 | dpid = fork (); | |
1486 | ||
1487 | if (dpid > 0) | |
1488 | { | |
fd95644b DN |
1489 | pid_t w; |
1490 | w = waitpid (dpid, &status, WUNTRACED | WCONTINUED); | |
636b507b | 1491 | |
361358ea | 1492 | if ((w == -1) || !WIFEXITED (status) || WEXITSTATUS (status)) |
fd95644b DN |
1493 | { |
1494 | message (TRUE, "Error: Could not start the Emacs daemon\n"); | |
1495 | exit (EXIT_FAILURE); | |
1496 | } | |
1497 | ||
1498 | /* Try connecting, the daemon should have started by now. */ | |
1499 | message (TRUE, "Emacs daemon should have started, trying to connect again\n"); | |
636b507b | 1500 | if ((emacs_socket = set_socket (1)) == INVALID_SOCKET) |
fd95644b DN |
1501 | { |
1502 | message (TRUE, "Error: Cannot connect even after starting the Emacs daemon\n"); | |
1503 | exit (EXIT_FAILURE); | |
1504 | } | |
636b507b DN |
1505 | } |
1506 | else if (dpid < 0) | |
1507 | { | |
fd95644b | 1508 | fprintf (stderr, "Error: Cannot fork!\n"); |
6ab88e02 | 1509 | exit (EXIT_FAILURE); |
636b507b DN |
1510 | } |
1511 | else | |
1512 | { | |
988e88ab | 1513 | char emacs[] = "emacs"; |
b0bbc07d PE |
1514 | char daemon_option[] = "--daemon"; |
1515 | char *d_argv[] = {emacs, daemon_option, 0 }; | |
636b507b DN |
1516 | if (socket_name != NULL) |
1517 | { | |
1518 | /* Pass --daemon=socket_name as argument. */ | |
988e88ab | 1519 | const char *deq = "--daemon="; |
636b507b DN |
1520 | char *daemon_arg = alloca (strlen (deq) |
1521 | + strlen (socket_name) + 1); | |
1522 | strcpy (daemon_arg, deq); | |
1523 | strcat (daemon_arg, socket_name); | |
1524 | d_argv[1] = daemon_arg; | |
1525 | } | |
1526 | execvp ("emacs", d_argv); | |
1527 | message (TRUE, "%s: error starting emacs daemon\n", progname); | |
1528 | } | |
802bdb3c | 1529 | #endif /* WINDOWSNT */ |
636b507b DN |
1530 | } |
1531 | ||
aa0b6932 | 1532 | int |
873fbd0b | 1533 | main (int argc, char **argv) |
aa0b6932 | 1534 | { |
c5443aa5 | 1535 | int rl = 0, needlf = 0; |
974b73e8 | 1536 | char *cwd, *str; |
aa0b6932 | 1537 | char string[BUFSIZ+1]; |
4be3dfc5 PE |
1538 | int null_socket_name IF_LINT ( = 0); |
1539 | int null_server_file IF_LINT ( = 0); | |
1540 | int start_daemon_if_needed; | |
3ecb8d93 | 1541 | int exit_status = EXIT_SUCCESS; |
aa0b6932 | 1542 | |
974b73e8 | 1543 | main_argv = argv; |
aa0b6932 JB |
1544 | progname = argv[0]; |
1545 | ||
ff90fbde JR |
1546 | #ifdef WINDOWSNT |
1547 | /* On Windows 7 and later, we need to explicitly associate emacsclient | |
1548 | with emacs so the UI behaves sensibly. */ | |
1549 | w32_set_user_model_id (); | |
1550 | #endif | |
1551 | ||
aa0b6932 JB |
1552 | /* Process options. */ |
1553 | decode_options (argc, argv); | |
1554 | ||
273b9028 | 1555 | if ((argc - optind < 1) && !eval && current_frame) |
23a7488d | 1556 | { |
974b73e8 KL |
1557 | message (TRUE, "%s: file name or argument required\n" |
1558 | "Try `%s --help' for more information\n", | |
1559 | progname, progname); | |
aa0b6932 | 1560 | exit (EXIT_FAILURE); |
23a7488d RS |
1561 | } |
1562 | ||
636b507b DN |
1563 | /* If alternate_editor is the empty string, start the emacs daemon |
1564 | in case of failure to connect. */ | |
1565 | start_daemon_if_needed = (alternate_editor | |
1566 | && (alternate_editor[0] == '\0')); | |
1567 | if (start_daemon_if_needed) | |
1568 | { | |
1569 | /* set_socket changes the values for socket_name and | |
1570 | server_file, we need to reset them, if they were NULL before | |
1571 | for the second call to set_socket. */ | |
1572 | null_socket_name = (socket_name == NULL); | |
1573 | null_server_file = (server_file == NULL); | |
1574 | } | |
aa0b6932 | 1575 | |
7e6bb4c9 PE |
1576 | emacs_socket = set_socket (alternate_editor || start_daemon_if_needed); |
1577 | if (emacs_socket == INVALID_SOCKET) | |
1578 | { | |
1579 | if (! start_daemon_if_needed) | |
1580 | fail (); | |
1581 | ||
1582 | /* Reset socket_name and server_file if they were NULL | |
1583 | before the set_socket call. */ | |
1584 | if (null_socket_name) | |
1585 | socket_name = NULL; | |
1586 | if (null_server_file) | |
1587 | server_file = NULL; | |
1588 | ||
1589 | start_daemon_and_retry_set_socket (); | |
1590 | } | |
974b73e8 KL |
1591 | |
1592 | cwd = get_current_dir_name (); | |
46cec291 RS |
1593 | if (cwd == 0) |
1594 | { | |
1595 | /* getwd puts message in STRING if it fails. */ | |
974b73e8 KL |
1596 | message (TRUE, "%s: %s\n", progname, |
1597 | "Cannot get current working directory"); | |
819b8f00 | 1598 | fail (); |
46cec291 RS |
1599 | } |
1600 | ||
c66648e0 | 1601 | #ifdef WINDOWSNT |
0e0dced5 | 1602 | w32_give_focus (); |
c66648e0 JB |
1603 | #endif |
1604 | ||
273b9028 | 1605 | /* Send over our environment and current directory. */ |
59e085e0 KL |
1606 | if (!current_frame) |
1607 | { | |
1608 | extern char **environ; | |
1609 | int i; | |
1610 | for (i = 0; environ[i]; i++) | |
1611 | { | |
486ba65f JR |
1612 | send_to_emacs (emacs_socket, "-env "); |
1613 | quote_argument (emacs_socket, environ[i]); | |
1614 | send_to_emacs (emacs_socket, " "); | |
59e085e0 | 1615 | } |
2828d5f9 | 1616 | } |
ba528748 SM |
1617 | send_to_emacs (emacs_socket, "-dir "); |
1618 | quote_argument (emacs_socket, cwd); | |
1619 | send_to_emacs (emacs_socket, "/"); | |
1620 | send_to_emacs (emacs_socket, " "); | |
2828d5f9 | 1621 | |
6afdd335 | 1622 | retry: |
5212210c | 1623 | if (nowait) |
486ba65f | 1624 | send_to_emacs (emacs_socket, "-nowait "); |
292d74a3 | 1625 | |
92071250 | 1626 | if (current_frame) |
486ba65f | 1627 | send_to_emacs (emacs_socket, "-current-frame "); |
c0f342ab | 1628 | |
30be2360 | 1629 | if (display) |
87209357 | 1630 | { |
486ba65f JR |
1631 | send_to_emacs (emacs_socket, "-display "); |
1632 | quote_argument (emacs_socket, display); | |
1633 | send_to_emacs (emacs_socket, " "); | |
87209357 | 1634 | } |
30be2360 | 1635 | |
0191e222 CY |
1636 | if (parent_id) |
1637 | { | |
1638 | send_to_emacs (emacs_socket, "-parent-id "); | |
1639 | quote_argument (emacs_socket, parent_id); | |
1640 | send_to_emacs (emacs_socket, " "); | |
1641 | } | |
1642 | ||
18a4ce5e AR |
1643 | if (frame_parameters && !current_frame) |
1644 | { | |
1645 | send_to_emacs (emacs_socket, "-frame-parameters "); | |
1646 | quote_argument (emacs_socket, frame_parameters); | |
1647 | send_to_emacs (emacs_socket, " "); | |
1648 | } | |
1649 | ||
8536c0f5 CY |
1650 | /* If using the current frame, send tty information to Emacs anyway. |
1651 | In daemon mode, Emacs may need to occupy this tty if no other | |
1652 | frame is available. */ | |
322ca650 | 1653 | if (tty || (current_frame && !eval)) |
9628b887 | 1654 | { |
b9170155 | 1655 | const char *tty_type, *tty_name; |
0b0d3e0b | 1656 | |
273b9028 CY |
1657 | if (find_tty (&tty_type, &tty_name, !tty)) |
1658 | { | |
b2ff54a0 | 1659 | #if !defined (NO_SOCKETS_IN_FILE_SYSTEM) |
273b9028 | 1660 | init_signals (); |
b2ff54a0 | 1661 | #endif |
273b9028 CY |
1662 | send_to_emacs (emacs_socket, "-tty "); |
1663 | quote_argument (emacs_socket, tty_name); | |
1664 | send_to_emacs (emacs_socket, " "); | |
1665 | quote_argument (emacs_socket, tty_type); | |
1666 | send_to_emacs (emacs_socket, " "); | |
1667 | } | |
9628b887 | 1668 | } |
77134727 | 1669 | |
273b9028 | 1670 | if (!current_frame && !tty) |
486ba65f | 1671 | send_to_emacs (emacs_socket, "-window-system "); |
0b0d3e0b | 1672 | |
87209357 | 1673 | if ((argc - optind > 0)) |
5212210c | 1674 | { |
b0bbc07d | 1675 | int i; |
87209357 | 1676 | for (i = optind; i < argc; i++) |
46cec291 | 1677 | { |
0b0d3e0b | 1678 | |
87209357 | 1679 | if (eval) |
0b0d3e0b | 1680 | { |
974b73e8 | 1681 | /* Don't prepend cwd or anything like that. */ |
486ba65f JR |
1682 | send_to_emacs (emacs_socket, "-eval "); |
1683 | quote_argument (emacs_socket, argv[i]); | |
1684 | send_to_emacs (emacs_socket, " "); | |
0b0d3e0b KL |
1685 | continue; |
1686 | } | |
1687 | ||
1688 | if (*argv[i] == '+') | |
1689 | { | |
87209357 EZ |
1690 | char *p = argv[i] + 1; |
1691 | while (isdigit ((unsigned char) *p) || *p == ':') p++; | |
0b0d3e0b KL |
1692 | if (*p == 0) |
1693 | { | |
486ba65f JR |
1694 | send_to_emacs (emacs_socket, "-position "); |
1695 | quote_argument (emacs_socket, argv[i]); | |
1696 | send_to_emacs (emacs_socket, " "); | |
0b0d3e0b KL |
1697 | continue; |
1698 | } | |
0b0d3e0b | 1699 | } |
6cde1b21 JB |
1700 | #ifdef WINDOWSNT |
1701 | else if (! file_name_absolute_p (argv[i]) | |
1702 | && (isalpha (argv[i][0]) && argv[i][1] == ':')) | |
1703 | /* Windows can have a different default directory for each | |
1704 | drive, so the cwd passed via "-dir" is not sufficient | |
1705 | to account for that. | |
1706 | If the user uses <drive>:<relpath>, we hence need to be | |
1707 | careful to expand <relpath> with the default directory | |
1708 | corresponding to <drive>. */ | |
1709 | { | |
1710 | char *filename = (char *) xmalloc (MAX_PATH); | |
1711 | DWORD size; | |
1712 | ||
1713 | size = GetFullPathName (argv[i], MAX_PATH, filename, NULL); | |
1714 | if (size > 0 && size < MAX_PATH) | |
1715 | argv[i] = filename; | |
1716 | else | |
1717 | free (filename); | |
1718 | } | |
1719 | #endif | |
0b0d3e0b | 1720 | |
486ba65f | 1721 | send_to_emacs (emacs_socket, "-file "); |
486ba65f JR |
1722 | quote_argument (emacs_socket, argv[i]); |
1723 | send_to_emacs (emacs_socket, " "); | |
0b0d3e0b | 1724 | } |
46cec291 | 1725 | } |
273b9028 | 1726 | else if (eval) |
87209357 | 1727 | { |
273b9028 CY |
1728 | /* Read expressions interactively. */ |
1729 | while ((str = fgets (string, BUFSIZ, stdin))) | |
1730 | { | |
1731 | send_to_emacs (emacs_socket, "-eval "); | |
1732 | quote_argument (emacs_socket, str); | |
1733 | } | |
1734 | send_to_emacs (emacs_socket, " "); | |
87209357 | 1735 | } |
0b0d3e0b | 1736 | |
486ba65f | 1737 | send_to_emacs (emacs_socket, "\n"); |
46cec291 | 1738 | |
fc2040c0 | 1739 | /* Wait for an answer. */ |
0b973fc5 | 1740 | if (!eval && !tty && !nowait && !quiet) |
30be2360 SM |
1741 | { |
1742 | printf ("Waiting for Emacs..."); | |
1743 | needlf = 2; | |
1744 | } | |
46cec291 | 1745 | fflush (stdout); |
0b0d3e0b | 1746 | fsync (1); |
46cec291 | 1747 | |
3cf8c6aa | 1748 | /* Now, wait for an answer and print any messages. */ |
b166dcd8 | 1749 | while (exit_status == EXIT_SUCCESS) |
3cf8c6aa | 1750 | { |
974b73e8 | 1751 | char *p; |
b166dcd8 KC |
1752 | do |
1753 | { | |
1754 | errno = 0; | |
1755 | rl = recv (emacs_socket, string, BUFSIZ, 0); | |
1756 | } | |
1757 | /* If we receive a signal (e.g. SIGWINCH, which we pass | |
1758 | through to Emacs), on some OSes we get EINTR and must retry. */ | |
1759 | while (rl < 0 && errno == EINTR); | |
1760 | ||
1761 | if (rl <= 0) | |
1762 | break; | |
b23b5a5b | 1763 | |
974b73e8 KL |
1764 | string[rl] = '\0'; |
1765 | ||
1766 | p = string + strlen (string) - 1; | |
1767 | while (p > string && *p == '\n') | |
0b0d3e0b KL |
1768 | *p-- = 0; |
1769 | ||
31fa6595 | 1770 | if (strprefix ("-emacs-pid ", string)) |
4d553a13 | 1771 | { |
6afdd335 | 1772 | /* -emacs-pid PID: The process id of the Emacs process. */ |
77134727 KL |
1773 | emacs_pid = strtol (string + strlen ("-emacs-pid"), NULL, 10); |
1774 | } | |
974b73e8 | 1775 | else if (strprefix ("-window-system-unsupported ", string)) |
6afdd335 KL |
1776 | { |
1777 | /* -window-system-unsupported: Emacs was compiled without X | |
1778 | support. Try again on the terminal. */ | |
6afdd335 KL |
1779 | nowait = 0; |
1780 | tty = 1; | |
1781 | goto retry; | |
1782 | } | |
974b73e8 | 1783 | else if (strprefix ("-print ", string)) |
77134727 | 1784 | { |
6afdd335 | 1785 | /* -print STRING: Print STRING on the terminal. */ |
974b73e8 | 1786 | str = unquote_argument (string + strlen ("-print ")); |
0b0d3e0b | 1787 | if (needlf) |
77134727 | 1788 | printf ("\n"); |
0b0d3e0b KL |
1789 | printf ("%s", str); |
1790 | needlf = str[0] == '\0' ? needlf : str[strlen (str) - 1] != '\n'; | |
77134727 | 1791 | } |
974b73e8 | 1792 | else if (strprefix ("-error ", string)) |
77134727 | 1793 | { |
6afdd335 | 1794 | /* -error DESCRIPTION: Signal an error on the terminal. */ |
974b73e8 | 1795 | str = unquote_argument (string + strlen ("-error ")); |
0b0d3e0b KL |
1796 | if (needlf) |
1797 | printf ("\n"); | |
974b73e8 | 1798 | fprintf (stderr, "*ERROR*: %s", str); |
0b0d3e0b | 1799 | needlf = str[0] == '\0' ? needlf : str[strlen (str) - 1] != '\n'; |
3ecb8d93 | 1800 | exit_status = EXIT_FAILURE; |
0b0d3e0b | 1801 | } |
c801ad51 | 1802 | #ifdef SIGSTOP |
382707ec | 1803 | else if (strprefix ("-suspend ", string)) |
0b0d3e0b | 1804 | { |
6afdd335 | 1805 | /* -suspend: Suspend this terminal, i.e., stop the process. */ |
0b0d3e0b | 1806 | if (needlf) |
77134727 | 1807 | printf ("\n"); |
0b0d3e0b KL |
1808 | needlf = 0; |
1809 | kill (0, SIGSTOP); | |
4d553a13 | 1810 | } |
b2ff54a0 | 1811 | #endif |
4d553a13 KL |
1812 | else |
1813 | { | |
6afdd335 | 1814 | /* Unknown command. */ |
0b0d3e0b | 1815 | if (needlf) |
4d553a13 | 1816 | printf ("\n"); |
974b73e8 | 1817 | printf ("*ERROR*: Unknown message: %s", string); |
3ecb8d93 WS |
1818 | needlf = string[0] |
1819 | == '\0' ? needlf : string[strlen (string) - 1] != '\n'; | |
4d553a13 | 1820 | } |
3cf8c6aa SM |
1821 | } |
1822 | ||
1823 | if (needlf) | |
1824 | printf ("\n"); | |
1825 | fflush (stdout); | |
0b0d3e0b | 1826 | fsync (1); |
23a7488d | 1827 | |
3ecb8d93 WS |
1828 | if (rl < 0) |
1829 | exit_status = EXIT_FAILURE; | |
1830 | ||
4b7b77f6 | 1831 | CLOSE_SOCKET (emacs_socket); |
3ecb8d93 | 1832 | return exit_status; |
46cec291 RS |
1833 | } |
1834 | ||
1e7823d0 | 1835 | #endif /* HAVE_SOCKETS && HAVE_INET_SOCKETS */ |
aa0b6932 | 1836 | |
27711600 RM |
1837 | \f |
1838 | #ifndef HAVE_STRERROR | |
1839 | char * | |
1840 | strerror (errnum) | |
1841 | int errnum; | |
1842 | { | |
1843 | extern char *sys_errlist[]; | |
1844 | extern int sys_nerr; | |
1845 | ||
1846 | if (errnum >= 0 && errnum < sys_nerr) | |
1847 | return sys_errlist[errnum]; | |
1848 | return (char *) "Unknown error"; | |
1849 | } | |
1850 | ||
1851 | #endif /* ! HAVE_STRERROR */ | |
ab5796a9 | 1852 | |
65396510 TTN |
1853 | |
1854 | /* emacsclient.c ends here */ |