1 /* Client process that communicates with GNU Emacs acting as server.
2 Copyright (C) 1986, 1987, 1994, 1999, 2000, 2001, 2003
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
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)
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.
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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
48 char *getenv (), *getwd ();
51 /* This is defined with -D from the compilation command,
52 which extracts it from ../lisp/version.el. */
55 #define VERSION "unspecified"
58 /* Name used to invoke this program. */
61 /* The first argument to main. */
64 /* The second argument to main. */
67 /* Nonzero means don't wait for a response from Emacs. --no-wait. */
70 /* Nonzero means args are expressions to be evaluated. --eval. */
73 /* The display on which Emacs should work. --display. */
76 /* Nonzero means open a new Emacs frame on the current terminal. */
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
;
83 /* If non-NULL, the filename of the UNIX socket. */
84 char *socket_name
= NULL
;
86 void print_help_and_exit ();
88 struct option longopts
[] =
90 { "no-wait", no_argument
, NULL
, 'n' },
91 { "eval", no_argument
, NULL
, 'e' },
92 { "help", no_argument
, NULL
, 'H' },
93 { "version", no_argument
, NULL
, 'V' },
94 { "tty", no_argument
, NULL
, 't' },
95 { "alternate-editor", required_argument
, NULL
, 'a' },
96 { "socket-name", required_argument
, NULL
, 's' },
97 { "display", required_argument
, NULL
, 'd' },
101 /* Decode the options from argv and argc.
102 The global variable `optind' will say how many arguments we used up. */
105 decode_options (argc
, argv
)
111 int opt
= getopt_long (argc
, argv
,
112 "VHnea:s:d:t", longopts
, 0);
117 alternate_editor
= getenv ("ALTERNATE_EDITOR");
122 /* If getopt returns 0, then it has already processed a
123 long-named option. We should do nothing. */
127 alternate_editor
= optarg
;
131 socket_name
= optarg
;
147 printf ("emacsclient %s\n", VERSION
);
156 print_help_and_exit ();
160 fprintf (stderr
, "Try `%s --help' for more information\n", progname
);
174 print_help_and_exit ()
177 "Usage: %s [OPTIONS] FILE...\n\
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\
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\
184 -t, --tty Open a new Emacs frame on the current terminal\n\
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\
188 -s, --socket-name=FILENAME\n\
189 Set the filename of the UNIX socket for communication\n\
190 -a, --alternate-editor=EDITOR\n\
191 Editor to fallback to if the server is not running\n\
193 Report bugs to bug-gnu-emacs@gnu.org.\n", progname
);
197 /* In NAME, insert a & before each &, each space, each newline, and
198 any initial -. Change spaces to underscores, too, so that the
199 return value never contains a space. */
202 quote_file_name (name
, stream
)
206 char *copy
= (char *) malloc (strlen (name
) * 2 + 1);
227 if (*p
== '&' || (*p
== '-' && p
== name
))
234 fprintf (stream
, copy
);
239 /* Like malloc but get fatal error if memory is exhausted. */
245 long *result
= (long *) malloc (size
);
255 Try to run a different command, or --if no alternate editor is
256 defined-- exit with an errorcode.
261 if (alternate_editor
)
264 execvp (alternate_editor
, main_argv
+ i
);
276 extern char *_sobuf
;
278 #if defined (USG) || defined (DGUX)
279 unsigned char _sobuf
[BUFSIZ
+8];
286 pass_signal_to_emacs (int signalnum
)
288 int old_errno
= errno
;
291 kill (emacs_pid
, signalnum
);
293 signal (signalnum
, pass_signal_to_emacs
);
300 /* Set up signal handlers. */
301 signal (SIGWINCH
, pass_signal_to_emacs
);
303 /* Don't pass SIGINT and SIGQUIT to Emacs, because it has no way of
304 deciding which terminal the signal came from. C-g is now a
305 normal input event on secondary terminals. */
307 signal (SIGINT
, pass_signal_to_emacs
);
308 signal (SIGQUIT
, pass_signal_to_emacs
);
313 #if !defined (HAVE_SOCKETS) || defined (NO_SOCKETS_IN_FILE_SYSTEM)
320 fprintf (stderr
, "%s: Sorry, the Emacs server is supported only\n",
322 fprintf (stderr
, "on systems with Berkeley sockets.\n");
327 #else /* HAVE_SOCKETS */
329 #include <sys/types.h>
330 #include <sys/socket.h>
332 #include <sys/stat.h>
335 extern char *strerror ();
338 /* Three possibilities:
339 2 - can't be `stat'ed (sets errno)
340 1 - isn't owned by us
341 0 - success: none of the above */
344 socket_status (socket_name
)
349 if (stat (socket_name
, &statbfr
) == -1)
352 if (statbfr
.st_uid
!= geteuid ())
358 /* Returns 1 if PREFIX is a prefix of STRING. */
360 strprefix (char *prefix
, char *string
)
369 for (i
= 0; prefix
[i
]; i
++)
370 if (!string
[i
] || string
[i
] != prefix
[i
])
381 int system_name_length
;
382 int s
, i
, needlf
= 0;
384 struct sockaddr_un server
;
392 /* Process options. */
393 decode_options (argc
, argv
);
395 if ((argc
- optind
< 1) && !eval
&& !frame
)
397 fprintf (stderr
, "%s: file name or argument required\n", progname
);
398 fprintf (stderr
, "Try `%s --help' for more information\n", progname
);
403 * Open up an AF_UNIX socket in this person's home directory
406 if ((s
= socket (AF_UNIX
, SOCK_STREAM
, 0)) < 0)
408 fprintf (stderr
, "%s: ", argv
[0]);
413 server
.sun_family
= AF_UNIX
;
417 system_name_length
= 32;
421 system_name
= (char *) xmalloc (system_name_length
+ 1);
423 /* system_name must be null-terminated string. */
424 system_name
[system_name_length
] = '\0';
426 if (gethostname (system_name
, system_name_length
) == 0)
430 system_name_length
*= 2;
433 /* We always use the non-dotted host name, for simplicity. */
434 dot
= index (system_name
, '.');
445 socket_name
= alloca (system_name_length
+ 100);
446 sprintf (socket_name
, "/tmp/emacs%d-%s/server",
447 (int) geteuid (), system_name
);
450 if (strlen (socket_name
) < sizeof (server
.sun_path
))
451 strcpy (server
.sun_path
, socket_name
);
454 fprintf (stderr
, "%s: socket-name %s too long",
455 argv
[0], socket_name
);
459 /* See if the socket exists, and if it's owned by us. */
460 sock_status
= socket_status (server
.sun_path
);
464 /* Failing that, see if LOGNAME or USER exist and differ from
465 our euid. If so, look for a socket based on the UID
466 associated with the name. This is reminiscent of the logic
467 that init_editfns uses to set the global Vuser_full_name. */
469 char *user_name
= (char *) getenv ("LOGNAME");
471 user_name
= (char *) getenv ("USER");
475 struct passwd
*pw
= getpwnam (user_name
);
476 if (pw
&& (pw
->pw_uid
!= geteuid ()))
478 /* We're running under su, apparently. */
479 sprintf (server
.sun_path
, "/tmp/emacs%d-%s/server",
480 (int) pw
->pw_uid
, system_name
);
481 sock_status
= socket_status (server
.sun_path
);
490 /* There's a socket, but it isn't owned by us. This is OK if
494 fprintf (stderr
, "%s: Invalid socket owner\n", argv
[0]);
501 if (saved_errno
== ENOENT
)
503 "%s: Can't find socket; have you started the server?\n\
504 To start the server in Emacs, type \"M-x server-start\".\n",
507 fprintf (stderr
, "%s: Can't stat %s: %s\n",
508 argv
[0], server
.sun_path
, strerror (saved_errno
));
514 if (connect (s
, (struct sockaddr
*) &server
, strlen (server
.sun_path
) + 2)
517 fprintf (stderr
, "%s: ", argv
[0]);
522 /* We use the stream OUT to send our command to the server. */
523 if ((out
= fdopen (s
, "r+")) == NULL
)
525 fprintf (stderr
, "%s: ", argv
[0]);
530 /* We use the stream IN to read the response.
531 We used to use just one stream for both output and input
532 on the socket, but reversing direction works nonportably:
533 on some systems, the output appears as the first input;
534 on other systems it does not. */
535 if ((in
= fdopen (s
, "r+")) == NULL
)
537 fprintf (stderr
, "%s: ", argv
[0]);
543 cwd
= getcwd (string
, sizeof string
);
545 cwd
= getwd (string
);
549 /* getwd puts message in STRING if it fails. */
552 fprintf (stderr
, "%s: %s (%s)\n", argv
[0],
553 "Cannot get current working directory", strerror (errno
));
555 fprintf (stderr
, "%s: %s (%s)\n", argv
[0], string
, strerror (errno
));
561 fprintf (out
, "-nowait ");
564 fprintf (out
, "-eval ");
568 fprintf (out
, "-display ");
569 quote_file_name (display
, out
);
575 char *tty_name
= ttyname (fileno (stdin
));
581 fprintf (out
, "-tty ");
582 quote_file_name (tty_name
, out
);
584 quote_file_name (getenv("TERM"), out
);
588 if ((argc
- optind
> 0))
590 for (i
= optind
; i
< argc
; i
++)
593 ; /* Don't prepend any cwd or anything like that. */
594 else if (*argv
[i
] == '+')
596 char *p
= argv
[i
] + 1;
597 while (isdigit ((unsigned char) *p
) || *p
== ':') p
++;
600 quote_file_name (cwd
, out
);
604 else if (*argv
[i
] != '/')
606 quote_file_name (cwd
, out
);
610 quote_file_name (argv
[i
], out
);
618 while ((str
= fgets (string
, BUFSIZ
, stdin
)))
620 quote_file_name (str
, out
);
629 /* Maybe wait for an answer. */
637 printf ("Waiting for Emacs...");
642 /* Now, wait for an answer and print any messages. */
643 while ((str
= fgets (string
, BUFSIZ
, in
)))
647 if (strprefix ("emacs-pid ", str
))
649 emacs_pid
= strtol (string
+ strlen ("emacs-pid"), NULL
, 10);
657 needlf
= str
[0] == '\0' ? needlf
: str
[strlen (str
) - 1] != '\n';
668 #endif /* HAVE_SOCKETS */
670 #ifndef HAVE_STRERROR
675 extern char *sys_errlist
[];
678 if (errnum
>= 0 && errnum
< sys_nerr
)
679 return sys_errlist
[errnum
];
680 return (char *) "Unknown error";
683 #endif /* ! HAVE_STRERROR */
685 /* arch-tag: f39bb9c4-73eb-477e-896d-50832e2ca9a7
686 (do not change this comment) */