| 1 | /* Client process that communicates with GNU Emacs acting as server. |
| 2 | Copyright (C) 1986, 1987, 1994, 1999, 2000, 2001, 2002, 2003, 2004, |
| 3 | 2005 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GNU Emacs. |
| 6 | |
| 7 | GNU Emacs is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2, or (at your option) |
| 10 | any later version. |
| 11 | |
| 12 | GNU Emacs is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with GNU Emacs; see the file COPYING. If not, write to |
| 19 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 20 | Boston, MA 02110-1301, USA. */ |
| 21 | |
| 22 | |
| 23 | #define NO_SHORTNAMES |
| 24 | |
| 25 | #ifdef HAVE_CONFIG_H |
| 26 | #include <config.h> |
| 27 | #endif |
| 28 | |
| 29 | #undef signal |
| 30 | |
| 31 | #include <ctype.h> |
| 32 | #include <stdio.h> |
| 33 | #include <getopt.h> |
| 34 | #ifdef HAVE_UNISTD_H |
| 35 | #include <unistd.h> |
| 36 | #endif |
| 37 | |
| 38 | #ifdef VMS |
| 39 | # include "vms-pwd.h" |
| 40 | #else |
| 41 | # include <pwd.h> |
| 42 | #endif /* not VMS */ |
| 43 | |
| 44 | #include <signal.h> |
| 45 | #include <errno.h> |
| 46 | |
| 47 | \f |
| 48 | char *getenv (), *getwd (); |
| 49 | char *(getcwd) (); |
| 50 | |
| 51 | #ifndef VERSION |
| 52 | #define VERSION "unspecified" |
| 53 | #endif |
| 54 | \f |
| 55 | /* Name used to invoke this program. */ |
| 56 | char *progname; |
| 57 | |
| 58 | /* The first argument to main. */ |
| 59 | int main_argc; |
| 60 | |
| 61 | /* The second argument to main. */ |
| 62 | char **main_argv; |
| 63 | |
| 64 | /* Nonzero means don't wait for a response from Emacs. --no-wait. */ |
| 65 | int nowait = 0; |
| 66 | |
| 67 | /* Nonzero means args are expressions to be evaluated. --eval. */ |
| 68 | int eval = 0; |
| 69 | |
| 70 | /* Nonzero means don't open a new frame. --current-frame. */ |
| 71 | int current_frame = 0; |
| 72 | |
| 73 | /* Nonzero means open a new graphical frame. */ |
| 74 | int window_system = 0; |
| 75 | |
| 76 | /* The display on which Emacs should work. --display. */ |
| 77 | char *display = NULL; |
| 78 | |
| 79 | /* Nonzero means open a new Emacs frame on the current terminal. */ |
| 80 | int tty = 0; |
| 81 | |
| 82 | /* If non-NULL, the name of an editor to fallback to if the server |
| 83 | is not running. --alternate-editor. */ |
| 84 | const char * alternate_editor = NULL; |
| 85 | |
| 86 | /* If non-NULL, the filename of the UNIX socket. */ |
| 87 | char *socket_name = NULL; |
| 88 | |
| 89 | void print_help_and_exit (); |
| 90 | |
| 91 | struct option longopts[] = |
| 92 | { |
| 93 | { "no-wait", no_argument, NULL, 'n' }, |
| 94 | { "eval", no_argument, NULL, 'e' }, |
| 95 | { "help", no_argument, NULL, 'H' }, |
| 96 | { "version", no_argument, NULL, 'V' }, |
| 97 | { "tty", no_argument, NULL, 't' }, |
| 98 | { "current-frame", no_argument, NULL, 'c' }, |
| 99 | { "alternate-editor", required_argument, NULL, 'a' }, |
| 100 | { "socket-name", required_argument, NULL, 's' }, |
| 101 | { "display", required_argument, NULL, 'd' }, |
| 102 | { 0, 0, 0, 0 } |
| 103 | }; |
| 104 | |
| 105 | /* Decode the options from argv and argc. |
| 106 | The global variable `optind' will say how many arguments we used up. */ |
| 107 | |
| 108 | void |
| 109 | decode_options (argc, argv) |
| 110 | int argc; |
| 111 | char **argv; |
| 112 | { |
| 113 | alternate_editor = getenv ("ALTERNATE_EDITOR"); |
| 114 | display = getenv ("DISPLAY"); |
| 115 | if (display && strlen (display) == 0) |
| 116 | display = NULL; |
| 117 | |
| 118 | while (1) |
| 119 | { |
| 120 | int opt = getopt_long (argc, argv, |
| 121 | "VHnea:s:d:tc", longopts, 0); |
| 122 | |
| 123 | if (opt == EOF) |
| 124 | break; |
| 125 | |
| 126 | switch (opt) |
| 127 | { |
| 128 | case 0: |
| 129 | /* If getopt returns 0, then it has already processed a |
| 130 | long-named option. We should do nothing. */ |
| 131 | break; |
| 132 | |
| 133 | case 'a': |
| 134 | alternate_editor = optarg; |
| 135 | break; |
| 136 | |
| 137 | case 's': |
| 138 | socket_name = optarg; |
| 139 | break; |
| 140 | |
| 141 | case 'd': |
| 142 | display = optarg; |
| 143 | break; |
| 144 | |
| 145 | case 'n': |
| 146 | nowait = 1; |
| 147 | break; |
| 148 | |
| 149 | case 'e': |
| 150 | eval = 1; |
| 151 | break; |
| 152 | |
| 153 | case 'V': |
| 154 | printf ("emacsclient %s\n", VERSION); |
| 155 | exit (EXIT_SUCCESS); |
| 156 | break; |
| 157 | |
| 158 | case 't': |
| 159 | tty = 1; |
| 160 | break; |
| 161 | |
| 162 | case 'c': |
| 163 | current_frame = 1; |
| 164 | break; |
| 165 | |
| 166 | case 'H': |
| 167 | print_help_and_exit (); |
| 168 | break; |
| 169 | |
| 170 | default: |
| 171 | fprintf (stderr, "Try `%s --help' for more information\n", progname); |
| 172 | exit (EXIT_FAILURE); |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if (!tty && display) |
| 178 | window_system = 1; |
| 179 | else |
| 180 | tty = 1; |
| 181 | |
| 182 | /* `emacsclient --no-wait' should open a new permanent frame, then exit. |
| 183 | Otherwise, --no-wait always implies --current-frame. */ |
| 184 | if (nowait && argc - optind > 0) |
| 185 | current_frame = 1; |
| 186 | |
| 187 | if (current_frame) |
| 188 | { |
| 189 | tty = 0; |
| 190 | window_system = 0; |
| 191 | } |
| 192 | |
| 193 | if (tty) |
| 194 | window_system = 0; |
| 195 | } |
| 196 | |
| 197 | void |
| 198 | print_help_and_exit () |
| 199 | { |
| 200 | printf ( |
| 201 | "Usage: %s [OPTIONS] FILE...\n\ |
| 202 | Tell the Emacs server to visit the specified files.\n\ |
| 203 | Every FILE can be either just a FILENAME or [+LINE[:COLUMN]] FILENAME.\n\ |
| 204 | \n\ |
| 205 | The following OPTIONS are accepted:\n\ |
| 206 | -V, --version Just print a version info and return\n\ |
| 207 | -H, --help Print this usage information message\n\ |
| 208 | -t, --tty Open a new Emacs frame on the current terminal\n\ |
| 209 | -c, --current-frame Do not create a new frame; use the current Emacs frame\n\ |
| 210 | -n, --no-wait Don't wait for the server to return\n\ |
| 211 | -e, --eval Evaluate the FILE arguments as ELisp expressions\n\ |
| 212 | -d, --display=DISPLAY Visit the file in the given display\n\ |
| 213 | -s, --socket-name=FILENAME\n\ |
| 214 | Set the filename of the UNIX socket for communication\n\ |
| 215 | -a, --alternate-editor=EDITOR\n\ |
| 216 | Editor to fallback to if the server is not running\n\ |
| 217 | \n\ |
| 218 | Report bugs to bug-gnu-emacs@gnu.org.\n", progname); |
| 219 | exit (EXIT_SUCCESS); |
| 220 | } |
| 221 | |
| 222 | /* Like malloc but get fatal error if memory is exhausted. */ |
| 223 | |
| 224 | long * |
| 225 | xmalloc (size) |
| 226 | unsigned int size; |
| 227 | { |
| 228 | long *result = (long *) malloc (size); |
| 229 | if (result == NULL) |
| 230 | { |
| 231 | perror ("malloc"); |
| 232 | exit (EXIT_FAILURE); |
| 233 | } |
| 234 | return result; |
| 235 | } |
| 236 | |
| 237 | /* Like strdup but get a fatal error if memory is exhausted. */ |
| 238 | |
| 239 | char * |
| 240 | xstrdup (const char *s) |
| 241 | { |
| 242 | char *result = strdup (s); |
| 243 | if (result == NULL) |
| 244 | { |
| 245 | perror ("strdup"); |
| 246 | exit (EXIT_FAILURE); |
| 247 | } |
| 248 | return result; |
| 249 | } |
| 250 | |
| 251 | /* In STR, insert a & before each &, each space, each newline, and |
| 252 | any initial -. Change spaces to underscores, too, so that the |
| 253 | return value never contains a space. |
| 254 | |
| 255 | Does not change the string. Outputs the result to STREAM. */ |
| 256 | |
| 257 | void |
| 258 | quote_argument (str, stream) |
| 259 | char *str; |
| 260 | FILE *stream; |
| 261 | { |
| 262 | char *copy = (char *) xmalloc (strlen (str) * 2 + 1); |
| 263 | char *p, *q; |
| 264 | |
| 265 | p = str; |
| 266 | q = copy; |
| 267 | while (*p) |
| 268 | { |
| 269 | if (*p == ' ') |
| 270 | { |
| 271 | *q++ = '&'; |
| 272 | *q++ = '_'; |
| 273 | p++; |
| 274 | } |
| 275 | else if (*p == '\n') |
| 276 | { |
| 277 | *q++ = '&'; |
| 278 | *q++ = 'n'; |
| 279 | p++; |
| 280 | } |
| 281 | else |
| 282 | { |
| 283 | if (*p == '&' || (*p == '-' && p == str)) |
| 284 | *q++ = '&'; |
| 285 | *q++ = *p++; |
| 286 | } |
| 287 | } |
| 288 | *q++ = 0; |
| 289 | |
| 290 | fprintf (stream, "%s", copy); |
| 291 | |
| 292 | free (copy); |
| 293 | } |
| 294 | |
| 295 | |
| 296 | /* The inverse of quote_argument. Removes quoting in string STR by |
| 297 | modifying the string in place. Returns STR. */ |
| 298 | |
| 299 | char * |
| 300 | unquote_argument (str) |
| 301 | char *str; |
| 302 | { |
| 303 | char *p, *q; |
| 304 | |
| 305 | if (! str) |
| 306 | return str; |
| 307 | |
| 308 | p = str; |
| 309 | q = str; |
| 310 | while (*p) |
| 311 | { |
| 312 | if (*p == '&') |
| 313 | { |
| 314 | p++; |
| 315 | if (*p == '&') |
| 316 | *p = '&'; |
| 317 | else if (*p == '_') |
| 318 | *p = ' '; |
| 319 | else if (*p == 'n') |
| 320 | *p = '\n'; |
| 321 | else if (*p == '-') |
| 322 | *p = '-'; |
| 323 | } |
| 324 | *q++ = *p++; |
| 325 | } |
| 326 | *q = 0; |
| 327 | return str; |
| 328 | } |
| 329 | |
| 330 | \f |
| 331 | /* |
| 332 | Try to run a different command, or --if no alternate editor is |
| 333 | defined-- exit with an errorcode. |
| 334 | */ |
| 335 | void |
| 336 | fail (void) |
| 337 | { |
| 338 | if (alternate_editor) |
| 339 | { |
| 340 | int i = optind - 1; |
| 341 | execvp (alternate_editor, main_argv + i); |
| 342 | return; |
| 343 | } |
| 344 | else |
| 345 | { |
| 346 | exit (EXIT_FAILURE); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /* The process id of Emacs. */ |
| 351 | int emacs_pid; |
| 352 | |
| 353 | /* File handles for communicating with Emacs. */ |
| 354 | FILE *out, *in; |
| 355 | |
| 356 | /* A signal handler that passes the signal to the Emacs process. |
| 357 | Useful for SIGWINCH. */ |
| 358 | |
| 359 | SIGTYPE |
| 360 | pass_signal_to_emacs (int signalnum) |
| 361 | { |
| 362 | int old_errno = errno; |
| 363 | |
| 364 | if (emacs_pid) |
| 365 | kill (emacs_pid, signalnum); |
| 366 | |
| 367 | signal (signalnum, pass_signal_to_emacs); |
| 368 | errno = old_errno; |
| 369 | } |
| 370 | |
| 371 | /* Signal handler for SIGCONT; notify the Emacs process that it can |
| 372 | now resume our tty frame. */ |
| 373 | |
| 374 | SIGTYPE |
| 375 | handle_sigcont (int signalnum) |
| 376 | { |
| 377 | int old_errno = errno; |
| 378 | |
| 379 | if (tcgetpgrp (1) == getpgrp ()) |
| 380 | { |
| 381 | /* We are in the foreground. */ |
| 382 | fprintf (out, "-resume \n"); |
| 383 | fflush (out); |
| 384 | fsync (fileno (out)); |
| 385 | } |
| 386 | else |
| 387 | { |
| 388 | /* We are in the background; cancel the continue. */ |
| 389 | kill (getpid (), SIGSTOP); |
| 390 | } |
| 391 | |
| 392 | signal (signalnum, handle_sigcont); |
| 393 | errno = old_errno; |
| 394 | } |
| 395 | |
| 396 | /* Signal handler for SIGTSTP; notify the Emacs process that we are |
| 397 | going to sleep. Normally the suspend is initiated by Emacs via |
| 398 | server-handle-suspend-tty, but if the server gets out of sync with |
| 399 | reality, we may get a SIGTSTP on C-z. Handling this signal and |
| 400 | notifying Emacs about it should get things under control again. */ |
| 401 | |
| 402 | SIGTYPE |
| 403 | handle_sigtstp (int signalnum) |
| 404 | { |
| 405 | int old_errno = errno; |
| 406 | sigset_t set; |
| 407 | |
| 408 | if (out) |
| 409 | { |
| 410 | fprintf (out, "-suspend \n"); |
| 411 | fflush (out); |
| 412 | fsync (fileno (out)); |
| 413 | } |
| 414 | |
| 415 | /* Unblock this signal and call the default handler by temprarily |
| 416 | changing the handler and resignalling. */ |
| 417 | sigprocmask (SIG_BLOCK, NULL, &set); |
| 418 | sigdelset (&set, signalnum); |
| 419 | signal (signalnum, SIG_DFL); |
| 420 | kill (getpid (), signalnum); |
| 421 | sigprocmask (SIG_SETMASK, &set, NULL); /* Let's the above signal through. */ |
| 422 | signal (signalnum, handle_sigtstp); |
| 423 | |
| 424 | errno = old_errno; |
| 425 | } |
| 426 | |
| 427 | /* Set up signal handlers before opening a frame on the current tty. */ |
| 428 | |
| 429 | void |
| 430 | init_signals (void) |
| 431 | { |
| 432 | /* Set up signal handlers. */ |
| 433 | signal (SIGWINCH, pass_signal_to_emacs); |
| 434 | |
| 435 | /* Don't pass SIGINT and SIGQUIT to Emacs, because it has no way of |
| 436 | deciding which terminal the signal came from. C-g is now a |
| 437 | normal input event on secondary terminals. */ |
| 438 | #if 0 |
| 439 | signal (SIGINT, pass_signal_to_emacs); |
| 440 | signal (SIGQUIT, pass_signal_to_emacs); |
| 441 | #endif |
| 442 | |
| 443 | signal (SIGCONT, handle_sigcont); |
| 444 | signal (SIGTSTP, handle_sigtstp); |
| 445 | signal (SIGTTOU, handle_sigtstp); |
| 446 | } |
| 447 | |
| 448 | \f |
| 449 | #if !defined (HAVE_SOCKETS) || defined (NO_SOCKETS_IN_FILE_SYSTEM) |
| 450 | |
| 451 | int |
| 452 | main (argc, argv) |
| 453 | int argc; |
| 454 | char **argv; |
| 455 | { |
| 456 | fprintf (stderr, "%s: Sorry, the Emacs server is supported only\n", |
| 457 | argv[0]); |
| 458 | fprintf (stderr, "on systems with Berkeley sockets.\n"); |
| 459 | |
| 460 | fail (); |
| 461 | } |
| 462 | |
| 463 | #else /* HAVE_SOCKETS */ |
| 464 | |
| 465 | #include <sys/types.h> |
| 466 | #include <sys/socket.h> |
| 467 | #include <sys/un.h> |
| 468 | #include <sys/stat.h> |
| 469 | #include <errno.h> |
| 470 | |
| 471 | extern char *strerror (); |
| 472 | extern int errno; |
| 473 | |
| 474 | /* Three possibilities: |
| 475 | 2 - can't be `stat'ed (sets errno) |
| 476 | 1 - isn't owned by us |
| 477 | 0 - success: none of the above */ |
| 478 | |
| 479 | static int |
| 480 | socket_status (socket_name) |
| 481 | char *socket_name; |
| 482 | { |
| 483 | struct stat statbfr; |
| 484 | |
| 485 | if (stat (socket_name, &statbfr) == -1) |
| 486 | return 2; |
| 487 | |
| 488 | if (statbfr.st_uid != geteuid ()) |
| 489 | return 1; |
| 490 | |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | /* Returns 1 if PREFIX is a prefix of STRING. */ |
| 495 | static int |
| 496 | strprefix (char *prefix, char *string) |
| 497 | { |
| 498 | int i; |
| 499 | if (! prefix) |
| 500 | return 1; |
| 501 | |
| 502 | if (!string) |
| 503 | return 0; |
| 504 | |
| 505 | for (i = 0; prefix[i]; i++) |
| 506 | if (!string[i] || string[i] != prefix[i]) |
| 507 | return 0; |
| 508 | return 1; |
| 509 | } |
| 510 | |
| 511 | int |
| 512 | main (argc, argv) |
| 513 | int argc; |
| 514 | char **argv; |
| 515 | { |
| 516 | int s, i, needlf = 0; |
| 517 | struct sockaddr_un server; |
| 518 | char *cwd, *str; |
| 519 | char string[BUFSIZ]; |
| 520 | |
| 521 | main_argc = argc; |
| 522 | main_argv = argv; |
| 523 | progname = argv[0]; |
| 524 | |
| 525 | /* Process options. */ |
| 526 | decode_options (argc, argv); |
| 527 | |
| 528 | if ((argc - optind < 1) && !eval && !tty && !window_system) |
| 529 | { |
| 530 | fprintf (stderr, "%s: file name or argument required\n", progname); |
| 531 | fprintf (stderr, "Try `%s --help' for more information\n", progname); |
| 532 | exit (EXIT_FAILURE); |
| 533 | } |
| 534 | |
| 535 | /* |
| 536 | * Open up an AF_UNIX socket in this person's home directory |
| 537 | */ |
| 538 | |
| 539 | if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) |
| 540 | { |
| 541 | fprintf (stderr, "%s: ", argv[0]); |
| 542 | perror ("socket"); |
| 543 | fail (); |
| 544 | } |
| 545 | |
| 546 | server.sun_family = AF_UNIX; |
| 547 | |
| 548 | { |
| 549 | int sock_status = 0; |
| 550 | int default_sock = !socket_name; |
| 551 | int saved_errno = 0; |
| 552 | |
| 553 | char *server_name = "server"; |
| 554 | |
| 555 | if (socket_name && !index (socket_name, '/') && !index (socket_name, '\\')) |
| 556 | { /* socket_name is a file name component. */ |
| 557 | server_name = socket_name; |
| 558 | socket_name = NULL; |
| 559 | default_sock = 1; /* Try both UIDs. */ |
| 560 | } |
| 561 | |
| 562 | if (default_sock) |
| 563 | { |
| 564 | socket_name = alloca (100 + strlen (server_name)); |
| 565 | sprintf (socket_name, "/tmp/emacs%d/%s", |
| 566 | (int) geteuid (), server_name); |
| 567 | } |
| 568 | |
| 569 | if (strlen (socket_name) < sizeof (server.sun_path)) |
| 570 | strcpy (server.sun_path, socket_name); |
| 571 | else |
| 572 | { |
| 573 | fprintf (stderr, "%s: socket-name %s too long", |
| 574 | argv[0], socket_name); |
| 575 | fail (); |
| 576 | } |
| 577 | |
| 578 | /* See if the socket exists, and if it's owned by us. */ |
| 579 | sock_status = socket_status (server.sun_path); |
| 580 | saved_errno = errno; |
| 581 | if (sock_status && default_sock) |
| 582 | { |
| 583 | /* Failing that, see if LOGNAME or USER exist and differ from |
| 584 | our euid. If so, look for a socket based on the UID |
| 585 | associated with the name. This is reminiscent of the logic |
| 586 | that init_editfns uses to set the global Vuser_full_name. */ |
| 587 | |
| 588 | char *user_name = (char *) getenv ("LOGNAME"); |
| 589 | |
| 590 | if (!user_name) |
| 591 | user_name = (char *) getenv ("USER"); |
| 592 | |
| 593 | if (user_name) |
| 594 | { |
| 595 | struct passwd *pw = getpwnam (user_name); |
| 596 | |
| 597 | if (pw && (pw->pw_uid != geteuid ())) |
| 598 | { |
| 599 | /* We're running under su, apparently. */ |
| 600 | socket_name = alloca (100 + strlen (server_name)); |
| 601 | sprintf (socket_name, "/tmp/emacs%d/%s", |
| 602 | (int) pw->pw_uid, server_name); |
| 603 | |
| 604 | if (strlen (socket_name) < sizeof (server.sun_path)) |
| 605 | strcpy (server.sun_path, socket_name); |
| 606 | else |
| 607 | { |
| 608 | fprintf (stderr, "%s: socket-name %s too long", |
| 609 | argv[0], socket_name); |
| 610 | exit (EXIT_FAILURE); |
| 611 | } |
| 612 | |
| 613 | sock_status = socket_status (server.sun_path); |
| 614 | saved_errno = errno; |
| 615 | } |
| 616 | else |
| 617 | errno = saved_errno; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | switch (sock_status) |
| 622 | { |
| 623 | case 1: |
| 624 | /* There's a socket, but it isn't owned by us. This is OK if |
| 625 | we are root. */ |
| 626 | if (0 != geteuid ()) |
| 627 | { |
| 628 | fprintf (stderr, "%s: Invalid socket owner\n", argv[0]); |
| 629 | fail (); |
| 630 | } |
| 631 | break; |
| 632 | |
| 633 | case 2: |
| 634 | /* `stat' failed */ |
| 635 | if (saved_errno == ENOENT) |
| 636 | fprintf (stderr, |
| 637 | "%s: can't find socket; have you started the server?\n\ |
| 638 | To start the server in Emacs, type \"M-x server-start\".\n", |
| 639 | argv[0]); |
| 640 | else |
| 641 | fprintf (stderr, "%s: can't stat %s: %s\n", |
| 642 | argv[0], server.sun_path, strerror (saved_errno)); |
| 643 | fail (); |
| 644 | break; |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | if (connect (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2) |
| 649 | < 0) |
| 650 | { |
| 651 | fprintf (stderr, "%s: ", argv[0]); |
| 652 | perror ("connect"); |
| 653 | fail (); |
| 654 | } |
| 655 | |
| 656 | /* We use the stream OUT to send our commands to the server. */ |
| 657 | if ((out = fdopen (s, "r+")) == NULL) |
| 658 | { |
| 659 | fprintf (stderr, "%s: ", argv[0]); |
| 660 | perror ("fdopen"); |
| 661 | fail (); |
| 662 | } |
| 663 | |
| 664 | /* We use the stream IN to read the responses. |
| 665 | We used to use just one stream for both output and input |
| 666 | on the socket, but reversing direction works nonportably: |
| 667 | on some systems, the output appears as the first input; |
| 668 | on other systems it does not. */ |
| 669 | if ((in = fdopen (s, "r+")) == NULL) |
| 670 | { |
| 671 | fprintf (stderr, "%s: ", argv[0]); |
| 672 | perror ("fdopen"); |
| 673 | fail (); |
| 674 | } |
| 675 | |
| 676 | #ifdef HAVE_GETCWD |
| 677 | cwd = getcwd (string, sizeof string); |
| 678 | #else |
| 679 | cwd = getwd (string); |
| 680 | #endif |
| 681 | if (cwd == 0) |
| 682 | { |
| 683 | /* getwd puts message in STRING if it fails. */ |
| 684 | |
| 685 | #ifdef HAVE_GETCWD |
| 686 | fprintf (stderr, "%s: %s (%s)\n", argv[0], |
| 687 | "cannot get current working directory", strerror (errno)); |
| 688 | #else |
| 689 | fprintf (stderr, "%s: %s (%s)\n", argv[0], string, strerror (errno)); |
| 690 | #endif |
| 691 | fail (); |
| 692 | } |
| 693 | |
| 694 | /* First of all, send our version number for verification. */ |
| 695 | fprintf (out, "-version %s ", VERSION); |
| 696 | |
| 697 | /* Send over our environment. */ |
| 698 | { |
| 699 | extern char **environ; |
| 700 | int i; |
| 701 | for (i = 0; environ[i]; i++) |
| 702 | { |
| 703 | char *name = xstrdup (environ[i]); |
| 704 | char *value = strchr (name, '='); |
| 705 | if (value && strlen (value) > 1) |
| 706 | { |
| 707 | *value++ = 0; |
| 708 | fprintf (out, "-env "); |
| 709 | quote_argument (name, out); |
| 710 | fprintf (out, " "); |
| 711 | quote_argument (value, out); |
| 712 | fprintf (out, " "); |
| 713 | fflush (out); |
| 714 | } |
| 715 | free (name); |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | retry: |
| 720 | if (nowait) |
| 721 | fprintf (out, "-nowait "); |
| 722 | |
| 723 | if (current_frame) |
| 724 | fprintf (out, "-current-frame "); |
| 725 | |
| 726 | if (display) |
| 727 | { |
| 728 | fprintf (out, "-display "); |
| 729 | quote_argument (display, out); |
| 730 | fprintf (out, " "); |
| 731 | } |
| 732 | |
| 733 | if (tty) |
| 734 | { |
| 735 | char *tty_name = ttyname (fileno (stdin)); |
| 736 | char *type = getenv ("TERM"); |
| 737 | |
| 738 | if (! tty_name) |
| 739 | { |
| 740 | fprintf (stderr, "%s: could not get terminal name\n", progname); |
| 741 | fail (); |
| 742 | } |
| 743 | |
| 744 | if (! type) |
| 745 | { |
| 746 | fprintf (stderr, "%s: please set the TERM variable to your terminal type\n", |
| 747 | progname); |
| 748 | fail (); |
| 749 | } |
| 750 | |
| 751 | if (! strcmp (type, "eterm")) |
| 752 | { |
| 753 | /* This causes nasty, MULTI_KBOARD-related input lockouts. */ |
| 754 | fprintf (stderr, "%s: opening a frame in an Emacs term buffer" |
| 755 | " is not supported\n", progname); |
| 756 | fail (); |
| 757 | } |
| 758 | |
| 759 | init_signals (); |
| 760 | |
| 761 | fprintf (out, "-tty "); |
| 762 | quote_argument (tty_name, out); |
| 763 | fprintf (out, " "); |
| 764 | quote_argument (type, out); |
| 765 | fprintf (out, " "); |
| 766 | } |
| 767 | |
| 768 | if (window_system) |
| 769 | fprintf (out, "-window-system "); |
| 770 | |
| 771 | if ((argc - optind > 0)) |
| 772 | { |
| 773 | for (i = optind; i < argc; i++) |
| 774 | { |
| 775 | int relative = 0; |
| 776 | |
| 777 | if (eval) |
| 778 | { |
| 779 | /* Don't prepend any cwd or anything like that. */ |
| 780 | fprintf (out, "-eval "); |
| 781 | quote_argument (argv[i], out); |
| 782 | fprintf (out, " "); |
| 783 | continue; |
| 784 | } |
| 785 | |
| 786 | if (*argv[i] == '+') |
| 787 | { |
| 788 | char *p = argv[i] + 1; |
| 789 | while (isdigit ((unsigned char) *p) || *p == ':') p++; |
| 790 | if (*p == 0) |
| 791 | { |
| 792 | fprintf (out, "-position "); |
| 793 | quote_argument (argv[i], out); |
| 794 | fprintf (out, " "); |
| 795 | continue; |
| 796 | } |
| 797 | else |
| 798 | relative = 1; |
| 799 | } |
| 800 | else if (*argv[i] != '/') |
| 801 | relative = 1; |
| 802 | |
| 803 | fprintf (out, "-file "); |
| 804 | if (relative) |
| 805 | { |
| 806 | quote_argument (cwd, out); |
| 807 | fprintf (out, "/"); |
| 808 | } |
| 809 | quote_argument (argv[i], out); |
| 810 | fprintf (out, " "); |
| 811 | } |
| 812 | } |
| 813 | else |
| 814 | { |
| 815 | if (!tty && !window_system) |
| 816 | { |
| 817 | while ((str = fgets (string, BUFSIZ, stdin))) |
| 818 | { |
| 819 | if (eval) |
| 820 | fprintf (out, "-eval "); |
| 821 | else |
| 822 | fprintf (out, "-file "); |
| 823 | quote_argument (str, out); |
| 824 | } |
| 825 | fprintf (out, " "); |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | fprintf (out, "\n"); |
| 830 | fflush (out); |
| 831 | fsync (fileno (out)); |
| 832 | |
| 833 | /* Wait for an answer. */ |
| 834 | if (!eval && !tty && !nowait) |
| 835 | { |
| 836 | printf ("Waiting for Emacs..."); |
| 837 | needlf = 2; |
| 838 | } |
| 839 | fflush (stdout); |
| 840 | fsync (1); |
| 841 | |
| 842 | /* Now, wait for an answer and print any messages. */ |
| 843 | while ((str = fgets (string, BUFSIZ, in))) |
| 844 | { |
| 845 | char *p = str + strlen (str) - 1; |
| 846 | while (p > str && *p == '\n') |
| 847 | *p-- = 0; |
| 848 | |
| 849 | if (strprefix ("-good-version ", str)) |
| 850 | { |
| 851 | /* -good-version: The versions match. */ |
| 852 | } |
| 853 | else if (strprefix ("-emacs-pid ", str)) |
| 854 | { |
| 855 | /* -emacs-pid PID: The process id of the Emacs process. */ |
| 856 | emacs_pid = strtol (string + strlen ("-emacs-pid"), NULL, 10); |
| 857 | } |
| 858 | else if (strprefix ("-window-system-unsupported ", str)) |
| 859 | { |
| 860 | /* -window-system-unsupported: Emacs was compiled without X |
| 861 | support. Try again on the terminal. */ |
| 862 | window_system = 0; |
| 863 | nowait = 0; |
| 864 | tty = 1; |
| 865 | goto retry; |
| 866 | } |
| 867 | else if (strprefix ("-print ", str)) |
| 868 | { |
| 869 | /* -print STRING: Print STRING on the terminal. */ |
| 870 | str = unquote_argument (str + strlen ("-print ")); |
| 871 | if (needlf) |
| 872 | printf ("\n"); |
| 873 | printf ("%s", str); |
| 874 | needlf = str[0] == '\0' ? needlf : str[strlen (str) - 1] != '\n'; |
| 875 | } |
| 876 | else if (strprefix ("-error ", str)) |
| 877 | { |
| 878 | /* -error DESCRIPTION: Signal an error on the terminal. */ |
| 879 | str = unquote_argument (str + strlen ("-error ")); |
| 880 | if (needlf) |
| 881 | printf ("\n"); |
| 882 | printf ("*ERROR*: %s", str); |
| 883 | needlf = str[0] == '\0' ? needlf : str[strlen (str) - 1] != '\n'; |
| 884 | } |
| 885 | else if (strprefix ("-suspend ", str)) |
| 886 | { |
| 887 | /* -suspend: Suspend this terminal, i.e., stop the process. */ |
| 888 | if (needlf) |
| 889 | printf ("\n"); |
| 890 | needlf = 0; |
| 891 | kill (0, SIGSTOP); |
| 892 | } |
| 893 | else |
| 894 | { |
| 895 | /* Unknown command. */ |
| 896 | if (needlf) |
| 897 | printf ("\n"); |
| 898 | printf ("*ERROR*: Unknown message: %s", str); |
| 899 | needlf = str[0] == '\0' ? needlf : str[strlen (str) - 1] != '\n'; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | if (needlf) |
| 904 | printf ("\n"); |
| 905 | fflush (stdout); |
| 906 | fsync (1); |
| 907 | |
| 908 | return EXIT_SUCCESS; |
| 909 | } |
| 910 | |
| 911 | #endif /* HAVE_SOCKETS */ |
| 912 | \f |
| 913 | #ifndef HAVE_STRERROR |
| 914 | char * |
| 915 | strerror (errnum) |
| 916 | int errnum; |
| 917 | { |
| 918 | extern char *sys_errlist[]; |
| 919 | extern int sys_nerr; |
| 920 | |
| 921 | if (errnum >= 0 && errnum < sys_nerr) |
| 922 | return sys_errlist[errnum]; |
| 923 | return (char *) "Unknown error"; |
| 924 | } |
| 925 | |
| 926 | #endif /* ! HAVE_STRERROR */ |
| 927 | |
| 928 | /* arch-tag: f39bb9c4-73eb-477e-896d-50832e2ca9a7 |
| 929 | (do not change this comment) */ |
| 930 | |
| 931 | /* emacsclient.c ends here */ |