Commit | Line | Data |
---|---|---|
efb859b4 | 1 | /* Client process that communicates with GNU Emacs acting as server. |
5c9659d3 | 2 | Copyright (C) 1986, 1987, 1994, 1999, 2000, 2001, 2003, 2004 |
de073ce3 | 3 | Free Software Foundation, Inc. |
46cec291 RS |
4 | |
5 | This file is part of GNU Emacs. | |
6 | ||
7 | GNU Emacs is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
92af894f | 9 | the Free Software Foundation; either version 2, or (at your option) |
46cec291 RS |
10 | any later version. |
11 | ||
12 | GNU Emacs is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GNU Emacs; see the file COPYING. If not, write to | |
3b7ad313 EN |
19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
20 | Boston, MA 02111-1307, USA. */ | |
46cec291 RS |
21 | |
22 | ||
23 | #define NO_SHORTNAMES | |
e69233c2 PJ |
24 | |
25 | #ifdef HAVE_CONFIG_H | |
2f8fe2f4 | 26 | #include <config.h> |
e69233c2 PJ |
27 | #endif |
28 | ||
4e23f2ba | 29 | #undef signal |
46cec291 | 30 | |
e69233c2 | 31 | #include <ctype.h> |
8f9aaa0a RS |
32 | #include <stdio.h> |
33 | #include <getopt.h> | |
79f13bba DL |
34 | #ifdef HAVE_UNISTD_H |
35 | #include <unistd.h> | |
36 | #endif | |
8f9aaa0a | 37 | |
9f637eea GM |
38 | #ifdef VMS |
39 | # include "vms-pwd.h" | |
40 | #else | |
41 | # include <pwd.h> | |
42 | #endif /* not VMS */ | |
43 | ||
9628b887 | 44 | #include <signal.h> |
4d553a13 | 45 | #include <errno.h> |
9628b887 KL |
46 | |
47 | \f | |
8f9aaa0a RS |
48 | char *getenv (), *getwd (); |
49 | char *getcwd (); | |
8f9aaa0a RS |
50 | |
51 | /* This is defined with -D from the compilation command, | |
52 | which extracts it from ../lisp/version.el. */ | |
53 | ||
54 | #ifndef VERSION | |
55 | #define VERSION "unspecified" | |
56 | #endif | |
57 | \f | |
58 | /* Name used to invoke this program. */ | |
59 | char *progname; | |
60 | ||
0a125897 KL |
61 | /* The first argument to main. */ |
62 | int main_argc; | |
63 | ||
64 | /* The second argument to main. */ | |
65 | char **main_argv; | |
66 | ||
749ae770 | 67 | /* Nonzero means don't wait for a response from Emacs. --no-wait. */ |
8f9aaa0a RS |
68 | int nowait = 0; |
69 | ||
30be2360 SM |
70 | /* Nonzero means args are expressions to be evaluated. --eval. */ |
71 | int eval = 0; | |
72 | ||
73 | /* The display on which Emacs should work. --display. */ | |
74 | char *display = NULL; | |
75 | ||
9628b887 | 76 | /* Nonzero means open a new Emacs frame on the current terminal. */ |
16c290d8 | 77 | int frame = 0; |
9628b887 | 78 | |
30be2360 SM |
79 | /* If non-NULL, the name of an editor to fallback to if the server |
80 | is not running. --alternate-editor. */ | |
81 | const char * alternate_editor = NULL; | |
82 | ||
3db926be | 83 | /* If non-NULL, the filename of the UNIX socket. */ |
254107e4 RS |
84 | char *socket_name = NULL; |
85 | ||
7f3bff3e AS |
86 | void print_help_and_exit (); |
87 | ||
8f9aaa0a RS |
88 | struct option longopts[] = |
89 | { | |
749ae770 | 90 | { "no-wait", no_argument, NULL, 'n' }, |
30be2360 | 91 | { "eval", no_argument, NULL, 'e' }, |
8f9aaa0a RS |
92 | { "help", no_argument, NULL, 'H' }, |
93 | { "version", no_argument, NULL, 'V' }, | |
b28c910d | 94 | { "tty", no_argument, NULL, 't' }, |
3cf8c6aa | 95 | { "alternate-editor", required_argument, NULL, 'a' }, |
254107e4 | 96 | { "socket-name", required_argument, NULL, 's' }, |
30be2360 SM |
97 | { "display", required_argument, NULL, 'd' }, |
98 | { 0, 0, 0, 0 } | |
8f9aaa0a RS |
99 | }; |
100 | ||
101 | /* Decode the options from argv and argc. | |
5212210c | 102 | The global variable `optind' will say how many arguments we used up. */ |
8f9aaa0a | 103 | |
5212210c | 104 | void |
8f9aaa0a RS |
105 | decode_options (argc, argv) |
106 | int argc; | |
107 | char **argv; | |
108 | { | |
109 | while (1) | |
110 | { | |
111 | int opt = getopt_long (argc, argv, | |
819b8f00 | 112 | "VHnea:s:d:t", longopts, 0); |
8f9aaa0a RS |
113 | |
114 | if (opt == EOF) | |
115 | break; | |
116 | ||
97e3214d | 117 | alternate_editor = getenv ("ALTERNATE_EDITOR"); |
e69233c2 | 118 | |
8f9aaa0a RS |
119 | switch (opt) |
120 | { | |
121 | case 0: | |
122 | /* If getopt returns 0, then it has already processed a | |
123 | long-named option. We should do nothing. */ | |
124 | break; | |
e69233c2 | 125 | |
97e3214d GM |
126 | case 'a': |
127 | alternate_editor = optarg; | |
128 | break; | |
e69233c2 | 129 | |
254107e4 RS |
130 | case 's': |
131 | socket_name = optarg; | |
132 | break; | |
133 | ||
30be2360 SM |
134 | case 'd': |
135 | display = optarg; | |
136 | break; | |
137 | ||
8f9aaa0a RS |
138 | case 'n': |
139 | nowait = 1; | |
140 | break; | |
141 | ||
30be2360 SM |
142 | case 'e': |
143 | eval = 1; | |
144 | break; | |
145 | ||
8f9aaa0a | 146 | case 'V': |
20c396e8 JB |
147 | printf ("emacsclient %s\n", VERSION); |
148 | exit (0); | |
8f9aaa0a | 149 | break; |
46cec291 | 150 | |
819b8f00 | 151 | case 't': |
16c290d8 | 152 | frame = 1; |
9628b887 KL |
153 | break; |
154 | ||
8f9aaa0a | 155 | case 'H': |
8f9aaa0a | 156 | print_help_and_exit (); |
20c396e8 JB |
157 | break; |
158 | ||
159 | default: | |
160 | fprintf (stderr, "Try `%s --help' for more information\n", progname); | |
161 | exit (1); | |
162 | break; | |
8f9aaa0a RS |
163 | } |
164 | } | |
9628b887 | 165 | |
16c290d8 | 166 | if (frame) { |
9628b887 KL |
167 | nowait = 0; |
168 | display = 0; | |
169 | } | |
170 | ||
8f9aaa0a RS |
171 | } |
172 | ||
7f3bff3e | 173 | void |
8f9aaa0a RS |
174 | print_help_and_exit () |
175 | { | |
20c396e8 JB |
176 | printf ( |
177 | "Usage: %s [OPTIONS] FILE...\n\ | |
30be2360 SM |
178 | Tell the Emacs server to visit the specified files.\n\ |
179 | Every FILE can be either just a FILENAME or [+LINE[:COLUMN]] FILENAME.\n\ | |
20c396e8 | 180 | \n\ |
30be2360 SM |
181 | The following OPTIONS are accepted:\n\ |
182 | -V, --version Just print a version info and return\n\ | |
183 | -H, --help Print this usage information message\n\ | |
819b8f00 | 184 | -t, --tty Open a new Emacs frame on the current terminal\n\ |
30be2360 SM |
185 | -n, --no-wait Don't wait for the server to return\n\ |
186 | -e, --eval Evaluate the FILE arguments as ELisp expressions\n\ | |
187 | -d, --display=DISPLAY Visit the file in the given display\n\ | |
254107e4 RS |
188 | -s, --socket-name=FILENAME\n\ |
189 | Set the filename of the UNIX socket for communication\n\ | |
30be2360 SM |
190 | -a, --alternate-editor=EDITOR\n\ |
191 | Editor to fallback to if the server is not running\n\ | |
20c396e8 | 192 | \n\ |
30be2360 | 193 | Report bugs to bug-gnu-emacs@gnu.org.\n", progname); |
20c396e8 | 194 | exit (0); |
8f9aaa0a | 195 | } |
5212210c | 196 | |
a4cf2096 RS |
197 | /* In NAME, insert a & before each &, each space, each newline, and |
198 | any initial -. Change spaces to underscores, too, so that the | |
5212210c RS |
199 | return value never contains a space. */ |
200 | ||
87209357 EZ |
201 | void |
202 | quote_file_name (name, stream) | |
5212210c | 203 | char *name; |
87209357 | 204 | FILE *stream; |
5212210c RS |
205 | { |
206 | char *copy = (char *) malloc (strlen (name) * 2 + 1); | |
207 | char *p, *q; | |
208 | ||
209 | p = name; | |
210 | q = copy; | |
211 | while (*p) | |
212 | { | |
213 | if (*p == ' ') | |
214 | { | |
f1db6a73 | 215 | *q++ = '&'; |
5212210c RS |
216 | *q++ = '_'; |
217 | p++; | |
218 | } | |
3cf8c6aa SM |
219 | else if (*p == '\n') |
220 | { | |
221 | *q++ = '&'; | |
222 | *q++ = 'n'; | |
223 | p++; | |
224 | } | |
5212210c RS |
225 | else |
226 | { | |
f1db6a73 RS |
227 | if (*p == '&' || (*p == '-' && p == name)) |
228 | *q++ = '&'; | |
5212210c RS |
229 | *q++ = *p++; |
230 | } | |
231 | } | |
f1db6a73 | 232 | *q++ = 0; |
5212210c | 233 | |
87209357 EZ |
234 | fprintf (stream, copy); |
235 | ||
236 | free (copy); | |
5212210c | 237 | } |
0c76956f | 238 | |
0c76956f RS |
239 | /* Like malloc but get fatal error if memory is exhausted. */ |
240 | ||
5497dfdb | 241 | long * |
0c76956f RS |
242 | xmalloc (size) |
243 | unsigned int size; | |
244 | { | |
5f32a712 | 245 | long *result = (long *) malloc (size); |
0c76956f RS |
246 | if (result == NULL) |
247 | { | |
248 | perror ("malloc"); | |
249 | exit (1); | |
250 | } | |
251 | return result; | |
252 | } | |
8f9aaa0a | 253 | \f |
97e3214d GM |
254 | /* |
255 | Try to run a different command, or --if no alternate editor is | |
256 | defined-- exit with an errorcode. | |
257 | */ | |
de073ce3 | 258 | void |
819b8f00 | 259 | fail (void) |
97e3214d GM |
260 | { |
261 | if (alternate_editor) | |
262 | { | |
3cf8c6aa | 263 | int i = optind - 1; |
0a125897 | 264 | execvp (alternate_editor, main_argv + i); |
23431241 | 265 | return; |
97e3214d GM |
266 | } |
267 | else | |
268 | { | |
269 | exit (1); | |
270 | } | |
271 | } | |
272 | ||
6548cf00 | 273 | int emacs_pid; |
9628b887 | 274 | |
4d553a13 KL |
275 | #ifdef nec_ews_svr4 |
276 | extern char *_sobuf ; | |
9628b887 | 277 | #else |
4d553a13 KL |
278 | #if defined (USG) || defined (DGUX) |
279 | unsigned char _sobuf[BUFSIZ+8]; | |
9628b887 | 280 | #else |
4d553a13 KL |
281 | char _sobuf[BUFSIZ]; |
282 | #endif | |
9628b887 | 283 | #endif |
9628b887 | 284 | |
da8e1115 KL |
285 | /* A signal handler that passes the signal to the Emacs process. |
286 | Useful for SIGWINCH. */ | |
287 | ||
9628b887 | 288 | SIGTYPE |
428a555e | 289 | pass_signal_to_emacs (int signalnum) |
9628b887 KL |
290 | { |
291 | int old_errno = errno; | |
292 | ||
9f729af5 | 293 | if (emacs_pid) |
428a555e | 294 | kill (emacs_pid, signalnum); |
9f729af5 | 295 | |
428a555e | 296 | signal (signalnum, pass_signal_to_emacs); |
9f729af5 KL |
297 | errno = old_errno; |
298 | } | |
299 | ||
da8e1115 KL |
300 | /* Set up signal handlers before opening a frame on the current tty. */ |
301 | ||
4d553a13 KL |
302 | void |
303 | init_signals (void) | |
9628b887 KL |
304 | { |
305 | /* Set up signal handlers. */ | |
428a555e | 306 | signal (SIGWINCH, pass_signal_to_emacs); |
4ca927b4 KL |
307 | |
308 | /* Don't pass SIGINT and SIGQUIT to Emacs, because it has no way of | |
309 | deciding which terminal the signal came from. C-g is now a | |
310 | normal input event on secondary terminals. */ | |
311 | #if 0 | |
428a555e KL |
312 | signal (SIGINT, pass_signal_to_emacs); |
313 | signal (SIGQUIT, pass_signal_to_emacs); | |
4ca927b4 | 314 | #endif |
9628b887 KL |
315 | } |
316 | ||
97e3214d | 317 | \f |
30be2360 | 318 | #if !defined (HAVE_SOCKETS) || defined (NO_SOCKETS_IN_FILE_SYSTEM) |
46cec291 | 319 | |
de073ce3 | 320 | int |
46cec291 RS |
321 | main (argc, argv) |
322 | int argc; | |
323 | char **argv; | |
324 | { | |
325 | fprintf (stderr, "%s: Sorry, the Emacs server is supported only\n", | |
326 | argv[0]); | |
30be2360 | 327 | fprintf (stderr, "on systems with Berkeley sockets.\n"); |
97e3214d | 328 | |
819b8f00 | 329 | fail (); |
46cec291 RS |
330 | } |
331 | ||
30be2360 | 332 | #else /* HAVE_SOCKETS */ |
46cec291 RS |
333 | |
334 | #include <sys/types.h> | |
335 | #include <sys/socket.h> | |
336 | #include <sys/un.h> | |
efb859b4 | 337 | #include <sys/stat.h> |
46cec291 RS |
338 | #include <errno.h> |
339 | ||
92af894f | 340 | extern char *strerror (); |
46cec291 RS |
341 | extern int errno; |
342 | ||
9f637eea GM |
343 | /* Three possibilities: |
344 | 2 - can't be `stat'ed (sets errno) | |
345 | 1 - isn't owned by us | |
346 | 0 - success: none of the above */ | |
347 | ||
348 | static int | |
349 | socket_status (socket_name) | |
350 | char *socket_name; | |
351 | { | |
352 | struct stat statbfr; | |
353 | ||
354 | if (stat (socket_name, &statbfr) == -1) | |
355 | return 2; | |
356 | ||
357 | if (statbfr.st_uid != geteuid ()) | |
358 | return 1; | |
359 | ||
360 | return 0; | |
361 | } | |
362 | ||
4d553a13 KL |
363 | /* Returns 1 if PREFIX is a prefix of STRING. */ |
364 | static int | |
365 | strprefix (char *prefix, char *string) | |
366 | { | |
367 | int i; | |
368 | if (! prefix) | |
369 | return 1; | |
370 | ||
371 | if (!string) | |
372 | return 0; | |
373 | ||
374 | for (i = 0; prefix[i]; i++) | |
375 | if (!string[i] || string[i] != prefix[i]) | |
376 | return 0; | |
377 | return 1; | |
378 | } | |
379 | ||
340ff9de | 380 | int |
46cec291 RS |
381 | main (argc, argv) |
382 | int argc; | |
383 | char **argv; | |
384 | { | |
a2b3f2b5 RS |
385 | char *system_name; |
386 | int system_name_length; | |
3cf8c6aa | 387 | int s, i, needlf = 0; |
23a7488d | 388 | FILE *out, *in; |
46cec291 | 389 | struct sockaddr_un server; |
571512de | 390 | char *cwd, *str; |
46cec291 | 391 | char string[BUFSIZ]; |
8f9aaa0a | 392 | |
0a125897 KL |
393 | main_argc = argc; |
394 | main_argv = argv; | |
8f9aaa0a | 395 | progname = argv[0]; |
46cec291 | 396 | |
8f9aaa0a | 397 | /* Process options. */ |
5212210c | 398 | decode_options (argc, argv); |
46cec291 | 399 | |
16c290d8 | 400 | if ((argc - optind < 1) && !eval && !frame) |
20c396e8 JB |
401 | { |
402 | fprintf (stderr, "%s: file name or argument required\n", progname); | |
403 | fprintf (stderr, "Try `%s --help' for more information\n", progname); | |
404 | exit (1); | |
405 | } | |
46cec291 | 406 | |
e69233c2 | 407 | /* |
46cec291 RS |
408 | * Open up an AF_UNIX socket in this person's home directory |
409 | */ | |
410 | ||
411 | if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) | |
412 | { | |
413 | fprintf (stderr, "%s: ", argv[0]); | |
414 | perror ("socket"); | |
819b8f00 | 415 | fail (); |
46cec291 | 416 | } |
e69233c2 | 417 | |
46cec291 | 418 | server.sun_family = AF_UNIX; |
c5fee545 | 419 | |
efb859b4 | 420 | { |
3ecdcd59 | 421 | char *dot; |
a2b3f2b5 RS |
422 | system_name_length = 32; |
423 | ||
424 | while (1) | |
425 | { | |
426 | system_name = (char *) xmalloc (system_name_length + 1); | |
427 | ||
428 | /* system_name must be null-terminated string. */ | |
429 | system_name[system_name_length] = '\0'; | |
430 | ||
431 | if (gethostname (system_name, system_name_length) == 0) | |
432 | break; | |
433 | ||
434 | free (system_name); | |
435 | system_name_length *= 2; | |
436 | } | |
3ecdcd59 SM |
437 | |
438 | /* We always use the non-dotted host name, for simplicity. */ | |
439 | dot = index (system_name, '.'); | |
440 | if (dot) | |
441 | *dot = '\0'; | |
c5fee545 KH |
442 | } |
443 | ||
c5fee545 | 444 | { |
9f637eea | 445 | int sock_status = 0; |
5c9659d3 | 446 | int default_sock = !socket_name; |
b80bf66e | 447 | int saved_errno = 0; |
819b8f00 | 448 | |
5c9659d3 | 449 | if (default_sock) |
254107e4 RS |
450 | { |
451 | socket_name = alloca (system_name_length + 100); | |
452 | sprintf (socket_name, "/tmp/emacs%d-%s/server", | |
453 | (int) geteuid (), system_name); | |
454 | } | |
455 | ||
456 | if (strlen (socket_name) < sizeof (server.sun_path)) | |
457 | strcpy (server.sun_path, socket_name); | |
458 | else | |
819b8f00 KL |
459 | { |
460 | fprintf (stderr, "%s: socket-name %s too long", | |
461 | argv[0], socket_name); | |
462 | fail (); | |
463 | } | |
efb859b4 | 464 | |
9f637eea GM |
465 | /* See if the socket exists, and if it's owned by us. */ |
466 | sock_status = socket_status (server.sun_path); | |
152b6e83 | 467 | saved_errno = errno; |
5c9659d3 | 468 | if (sock_status && default_sock) |
efb859b4 | 469 | { |
9f637eea GM |
470 | /* Failing that, see if LOGNAME or USER exist and differ from |
471 | our euid. If so, look for a socket based on the UID | |
472 | associated with the name. This is reminiscent of the logic | |
473 | that init_editfns uses to set the global Vuser_full_name. */ | |
e69233c2 | 474 | |
9f637eea | 475 | char *user_name = (char *) getenv ("LOGNAME"); |
293f9f2a | 476 | |
9f637eea GM |
477 | if (!user_name) |
478 | user_name = (char *) getenv ("USER"); | |
e69233c2 | 479 | |
9f637eea GM |
480 | if (user_name) |
481 | { | |
482 | struct passwd *pw = getpwnam (user_name); | |
293f9f2a | 483 | |
9f637eea GM |
484 | if (pw && (pw->pw_uid != geteuid ())) |
485 | { | |
486 | /* We're running under su, apparently. */ | |
5c9659d3 | 487 | sprintf (socket_name, "/tmp/emacs%d-%s/server", |
23431241 | 488 | (int) pw->pw_uid, system_name); |
5c9659d3 SM |
489 | |
490 | if (strlen (socket_name) < sizeof (server.sun_path)) | |
491 | strcpy (server.sun_path, socket_name); | |
492 | else | |
493 | { | |
494 | fprintf (stderr, "%s: socket-name %s too long", | |
495 | argv[0], socket_name); | |
496 | exit (1); | |
497 | } | |
498 | ||
9f637eea | 499 | sock_status = socket_status (server.sun_path); |
b80bf66e | 500 | saved_errno = errno; |
9f637eea | 501 | } |
293f9f2a RS |
502 | else |
503 | errno = saved_errno; | |
9f637eea | 504 | } |
efb859b4 | 505 | } |
e69233c2 | 506 | |
9f637eea GM |
507 | switch (sock_status) |
508 | { | |
509 | case 1: | |
510 | /* There's a socket, but it isn't owned by us. This is OK if | |
511 | we are root. */ | |
512 | if (0 != geteuid ()) | |
513 | { | |
514 | fprintf (stderr, "%s: Invalid socket owner\n", argv[0]); | |
819b8f00 | 515 | fail (); |
9f637eea GM |
516 | } |
517 | break; | |
e69233c2 | 518 | |
9f637eea GM |
519 | case 2: |
520 | /* `stat' failed */ | |
152b6e83 | 521 | if (saved_errno == ENOENT) |
9f637eea | 522 | fprintf (stderr, |
819b8f00 | 523 | "%s: Can't find socket; have you started the server?\n\ |
45adde32 | 524 | To start the server in Emacs, type \"M-x server-start\".\n", |
9f637eea GM |
525 | argv[0]); |
526 | else | |
819b8f00 | 527 | fprintf (stderr, "%s: Can't stat %s: %s\n", |
152b6e83 | 528 | argv[0], server.sun_path, strerror (saved_errno)); |
819b8f00 | 529 | fail (); |
9f637eea GM |
530 | break; |
531 | } | |
efb859b4 | 532 | } |
46cec291 | 533 | |
4e23f2ba JB |
534 | if (connect (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2) |
535 | < 0) | |
46cec291 RS |
536 | { |
537 | fprintf (stderr, "%s: ", argv[0]); | |
538 | perror ("connect"); | |
819b8f00 | 539 | fail (); |
46cec291 | 540 | } |
23a7488d RS |
541 | |
542 | /* We use the stream OUT to send our command to the server. */ | |
46cec291 RS |
543 | if ((out = fdopen (s, "r+")) == NULL) |
544 | { | |
545 | fprintf (stderr, "%s: ", argv[0]); | |
546 | perror ("fdopen"); | |
819b8f00 | 547 | fail (); |
46cec291 RS |
548 | } |
549 | ||
23a7488d RS |
550 | /* We use the stream IN to read the response. |
551 | We used to use just one stream for both output and input | |
552 | on the socket, but reversing direction works nonportably: | |
553 | on some systems, the output appears as the first input; | |
554 | on other systems it does not. */ | |
555 | if ((in = fdopen (s, "r+")) == NULL) | |
556 | { | |
557 | fprintf (stderr, "%s: ", argv[0]); | |
558 | perror ("fdopen"); | |
819b8f00 | 559 | fail (); |
23a7488d RS |
560 | } |
561 | ||
38732dba | 562 | #ifdef HAVE_GETCWD |
ee6a193c | 563 | cwd = getcwd (string, sizeof string); |
38732dba RS |
564 | #else |
565 | cwd = getwd (string); | |
ee6a193c | 566 | #endif |
46cec291 RS |
567 | if (cwd == 0) |
568 | { | |
569 | /* getwd puts message in STRING if it fails. */ | |
bb5618fe | 570 | |
38732dba | 571 | #ifdef HAVE_GETCWD |
bb5618fe JB |
572 | fprintf (stderr, "%s: %s (%s)\n", argv[0], |
573 | "Cannot get current working directory", strerror (errno)); | |
38732dba | 574 | #else |
bb5618fe | 575 | fprintf (stderr, "%s: %s (%s)\n", argv[0], string, strerror (errno)); |
bd252662 | 576 | #endif |
819b8f00 | 577 | fail (); |
46cec291 RS |
578 | } |
579 | ||
5212210c RS |
580 | if (nowait) |
581 | fprintf (out, "-nowait "); | |
292d74a3 | 582 | |
30be2360 SM |
583 | if (eval) |
584 | fprintf (out, "-eval "); | |
585 | ||
586 | if (display) | |
87209357 EZ |
587 | { |
588 | fprintf (out, "-display "); | |
589 | quote_file_name (display, out); | |
590 | fprintf (out, " "); | |
591 | } | |
30be2360 | 592 | |
16c290d8 | 593 | if (frame) |
9628b887 | 594 | { |
4d553a13 KL |
595 | char *tty_name = ttyname (fileno (stdin)); |
596 | if (! tty_name) | |
597 | fail (); | |
9628b887 | 598 | |
4d553a13 | 599 | init_signals (); |
9628b887 | 600 | |
819b8f00 | 601 | fprintf (out, "-tty "); |
4d553a13 | 602 | quote_file_name (tty_name, out); |
9628b887 KL |
603 | fprintf (out, " "); |
604 | quote_file_name (getenv("TERM"), out); | |
605 | fprintf (out, " "); | |
606 | } | |
607 | ||
87209357 | 608 | if ((argc - optind > 0)) |
5212210c | 609 | { |
87209357 | 610 | for (i = optind; i < argc; i++) |
46cec291 | 611 | { |
87209357 EZ |
612 | if (eval) |
613 | ; /* Don't prepend any cwd or anything like that. */ | |
614 | else if (*argv[i] == '+') | |
615 | { | |
616 | char *p = argv[i] + 1; | |
617 | while (isdigit ((unsigned char) *p) || *p == ':') p++; | |
618 | if (*p != 0) | |
619 | { | |
620 | quote_file_name (cwd, out); | |
621 | fprintf (out, "/"); | |
622 | } | |
623 | } | |
624 | else if (*argv[i] != '/') | |
625 | { | |
626 | quote_file_name (cwd, out); | |
627 | fprintf (out, "/"); | |
628 | } | |
629 | ||
630 | quote_file_name (argv[i], out); | |
631 | fprintf (out, " "); | |
46cec291 | 632 | } |
46cec291 | 633 | } |
87209357 EZ |
634 | else |
635 | { | |
16c290d8 | 636 | if (!frame) |
9628b887 KL |
637 | { |
638 | while ((str = fgets (string, BUFSIZ, stdin))) | |
639 | { | |
640 | quote_file_name (str, out); | |
641 | } | |
642 | fprintf (out, " "); | |
643 | } | |
87209357 EZ |
644 | } |
645 | ||
46cec291 RS |
646 | fprintf (out, "\n"); |
647 | fflush (out); | |
648 | ||
292d74a3 RS |
649 | /* Maybe wait for an answer. */ |
650 | if (nowait) | |
9628b887 | 651 | { |
9628b887 KL |
652 | return 0; |
653 | } | |
292d74a3 | 654 | |
4d553a13 | 655 | if (!eval && !frame) |
30be2360 SM |
656 | { |
657 | printf ("Waiting for Emacs..."); | |
658 | needlf = 2; | |
659 | } | |
46cec291 RS |
660 | fflush (stdout); |
661 | ||
3cf8c6aa | 662 | /* Now, wait for an answer and print any messages. */ |
e69233c2 | 663 | while ((str = fgets (string, BUFSIZ, in))) |
3cf8c6aa | 664 | { |
4d553a13 KL |
665 | if (frame) |
666 | { | |
667 | if (strprefix ("emacs-pid ", str)) | |
668 | { | |
669 | emacs_pid = strtol (string + strlen ("emacs-pid"), NULL, 10); | |
670 | } | |
671 | } | |
672 | else | |
673 | { | |
674 | if (needlf == 2) | |
675 | printf ("\n"); | |
676 | printf ("%s", str); | |
677 | needlf = str[0] == '\0' ? needlf : str[strlen (str) - 1] != '\n'; | |
678 | } | |
3cf8c6aa SM |
679 | } |
680 | ||
681 | if (needlf) | |
682 | printf ("\n"); | |
683 | fflush (stdout); | |
23a7488d | 684 | |
340ff9de | 685 | return 0; |
46cec291 RS |
686 | } |
687 | ||
30be2360 | 688 | #endif /* HAVE_SOCKETS */ |
27711600 RM |
689 | \f |
690 | #ifndef HAVE_STRERROR | |
691 | char * | |
692 | strerror (errnum) | |
693 | int errnum; | |
694 | { | |
695 | extern char *sys_errlist[]; | |
696 | extern int sys_nerr; | |
697 | ||
698 | if (errnum >= 0 && errnum < sys_nerr) | |
699 | return sys_errlist[errnum]; | |
700 | return (char *) "Unknown error"; | |
701 | } | |
702 | ||
703 | #endif /* ! HAVE_STRERROR */ | |
ab5796a9 MB |
704 | |
705 | /* arch-tag: f39bb9c4-73eb-477e-896d-50832e2ca9a7 | |
706 | (do not change this comment) */ |