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, 2006 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., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
31 /* config.h defines these, which disables sockets altogether! */
39 # define NO_SOCKETS_IN_FILE_SYSTEM
41 # define HSOCKET SOCKET
42 # define CLOSE_SOCKET closesocket
43 # define INITIALIZE() (initialize_sockets ())
45 #else /* !WINDOWSNT */
47 # include <sys/types.h>
49 # ifdef HAVE_INET_SOCKETS
50 # include <netinet/in.h>
53 # define INVALID_SOCKET -1
55 # define CLOSE_SOCKET close
58 #endif /* !WINDOWSNT */
75 #else /* not WINDOWSNT */
77 #endif /* not WINDOWSNT */
86 #define DIRECTORY_SEP '/'
88 #ifndef IS_DIRECTORY_SEP
89 #define IS_DIRECTORY_SEP(_c_) ((_c_) == DIRECTORY_SEP)
93 #define IS_DEVICE_SEP(_c_) 0
95 #define IS_DEVICE_SEP(_c_) ((_c_) == DEVICE_SEP)
99 #define IS_ANY_SEP(_c_) (IS_DIRECTORY_SEP (_c_))
104 char *getenv (), *getwd ();
108 #define VERSION "unspecified"
111 #define SEND_STRING(data) (send_to_emacs (s, (data)))
112 #define SEND_QUOTED(data) (quote_argument (s, (data)))
115 #define EXIT_SUCCESS 0
119 #define EXIT_FAILURE 1
134 /* Name used to invoke this program. */
137 /* The first argument to main. */
140 /* The second argument to main. */
143 /* Nonzero means don't wait for a response from Emacs. --no-wait. */
146 /* Nonzero means args are expressions to be evaluated. --eval. */
149 /* Nonzero means don't open a new frame. --current-frame. */
150 int current_frame
= 0;
152 /* Nonzero means open a new graphical frame. */
153 int window_system
= 0;
155 /* The display on which Emacs should work. --display. */
156 char *display
= NULL
;
158 /* Nonzero means open a new Emacs frame on the current terminal. */
161 /* If non-NULL, the name of an editor to fallback to if the server
162 is not running. --alternate-editor. */
163 const char *alternate_editor
= NULL
;
165 /* If non-NULL, the filename of the UNIX socket. */
166 char *socket_name
= NULL
;
168 /* If non-NULL, the filename of the authentication file. */
169 char *server_file
= NULL
;
171 /* PID of the Emacs server process. */
174 /* File handles for communicating with Emacs. */
177 void print_help_and_exit () NO_RETURN
;
179 struct option longopts
[] =
181 { "no-wait", no_argument
, NULL
, 'n' },
182 { "eval", no_argument
, NULL
, 'e' },
183 { "help", no_argument
, NULL
, 'H' },
184 { "version", no_argument
, NULL
, 'V' },
185 { "tty", no_argument
, NULL
, 't' },
186 { "current-frame", no_argument
, NULL
, 'c' },
187 { "alternate-editor", required_argument
, NULL
, 'a' },
188 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
189 { "socket-name", required_argument
, NULL
, 's' },
191 { "server-file", required_argument
, NULL
, 'f' },
192 { "display", required_argument
, NULL
, 'd' },
197 /* Like malloc but get fatal error if memory is exhausted. */
203 long *result
= (long *) malloc (size
);
212 /* Like strdup but get a fatal error if memory is exhausted. */
215 xstrdup (const char *s
)
217 char *result
= strdup (s
);
227 #if !defined (HAVE_GET_CURRENT_DIR_NAME) || defined (BROKEN_GET_CURRENT_DIR_NAME)
229 /* Return the current working directory. Returns NULL on errors.
230 Any other returned value must be freed with free. This is used
231 only when get_current_dir_name is not defined on the system. */
233 get_current_dir_name ()
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. */
241 if ((pwd
= getenv ("PWD")) != 0
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
248 && strlen (pwd
) < MAXPATHLEN
252 buf
= (char *) xmalloc (strlen (pwd
) + 1);
260 size_t buf_size
= 1024;
261 buf
= (char *) xmalloc (buf_size
);
266 if (getcwd (buf
, buf_size
) == buf
)
270 int tmp_errno
= errno
;
276 buf
= (char *) realloc (buf
, buf_size
);
284 /* We need MAXPATHLEN here. */
285 buf
= (char *) xmalloc (MAXPATHLEN
+ 1);
288 if (getwd (buf
) == NULL
)
290 int tmp_errno
= errno
;
301 /* Message functions. */
304 /* I first tried to check for STDOUT. The check did not work,
305 I get a valid handle also in nonconsole apps.
306 Instead I test for console title, which seems to work. */
310 static int window_app
= -1;
311 char szTitle
[MAX_PATH
];
314 window_app
= (GetConsoleTitleA (szTitle
, MAX_PATH
) == 0);
321 message (int is_error
, char *message
, ...)
326 va_start (args
, message
);
327 vsprintf (msg
, message
, args
);
331 if (w32_window_app ())
334 MessageBox (NULL
, msg
, "Emacsclient ERROR", MB_ICONERROR
);
336 MessageBox (NULL
, msg
, "Emacsclient", MB_ICONINFORMATION
);
341 FILE *f
= is_error
? stderr
: stdout
;
348 /* Decode the options from argv and argc.
349 The global variable `optind' will say how many arguments we used up. */
352 decode_options (argc
, argv
)
356 alternate_editor
= getenv ("ALTERNATE_EDITOR");
357 display
= getenv ("DISPLAY");
358 if (display
&& strlen (display
) == 0)
363 int opt
= getopt_long (argc
, argv
,
364 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
377 /* If getopt returns 0, then it has already processed a
378 long-named option. We should do nothing. */
382 alternate_editor
= optarg
;
385 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
387 socket_name
= optarg
;
392 server_file
= optarg
;
408 message (FALSE
, "emacsclient %s\n", VERSION
);
421 print_help_and_exit ();
425 message (TRUE
, "Try `%s --help' for more information\n", progname
);
436 /* --no-wait implies --current-frame on ttys when there are file
437 arguments or expressions given. */
438 if (nowait
&& tty
&& argc
- optind
> 0)
453 print_help_and_exit ()
456 "Usage: %s [OPTIONS] FILE...\n\
457 Tell the Emacs server to visit the specified files.\n\
458 Every FILE can be either just a FILENAME or [+LINE[:COLUMN]] FILENAME.\n\
460 The following OPTIONS are accepted:\n\
461 -V, --version Just print version info and return\n\
462 -H, --help Print this usage information message\n\
463 -t, --tty Open a new Emacs frame on the current terminal\n\
464 -c, --current-frame Do not create a new frame; use the current Emacs frame\n\
465 -e, --eval Evaluate the FILE arguments as ELisp expressions\n\
466 -n, --no-wait Don't wait for the server to return\n\
467 -d, --display=DISPLAY Visit the file in the given display\n"
468 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
469 "-s, --socket-name=FILENAME\n\
470 Set filename of the UNIX socket for communication\n"
472 "-f, --server-file=FILENAME\n\
473 Set filename of the TCP authentication file\n\
474 -a, --alternate-editor=EDITOR\n\
475 Editor to fallback to if the server is not running\n\
477 Report bugs to bug-gnu-emacs@gnu.org.\n", progname
);
482 Try to run a different command, or --if no alternate editor is
483 defined-- exit with an errorcode.
488 if (alternate_editor
)
492 main_argv
[i
] = (char *)alternate_editor
;
494 execvp (alternate_editor
, main_argv
+ i
);
495 message (TRUE
, "%s: error executing alternate editor \"%s\"\n",
496 progname
, alternate_editor
);
502 #if !defined (HAVE_SOCKETS) || !defined (HAVE_INET_SOCKETS)
512 message (TRUE
, "%s: Sorry, the Emacs server is supported only\n"
513 "on systems with Berkeley sockets.\n",
518 #else /* HAVE_SOCKETS && HAVE_INET_SOCKETS */
521 # include <winsock2.h>
523 # include <sys/types.h>
524 # include <sys/socket.h>
526 # include <sys/stat.h>
530 #define AUTH_KEY_LENGTH 64
531 #define SEND_BUFFER_SIZE 4096
533 extern char *strerror ();
536 /* Buffer to accumulate data to send in TCP connections. */
537 char send_buffer
[SEND_BUFFER_SIZE
+ 1];
538 int sblen
= 0; /* Fill pointer for the send buffer. */
540 /* Let's send the data to Emacs when either
541 - the data ends in "\n", or
542 - the buffer is full (but this shouldn't happen)
543 Otherwise, we just accumulate it. */
545 send_to_emacs (s
, data
)
551 int dlen
= strlen (data
);
552 if (dlen
+ sblen
>= SEND_BUFFER_SIZE
)
554 int part
= SEND_BUFFER_SIZE
- sblen
;
555 strncpy (&send_buffer
[sblen
], data
, part
);
557 sblen
= SEND_BUFFER_SIZE
;
561 strcpy (&send_buffer
[sblen
], data
);
568 if (sblen
== SEND_BUFFER_SIZE
569 || (sblen
> 0 && send_buffer
[sblen
-1] == '\n'))
571 int sent
= send (s
, send_buffer
, sblen
, 0);
573 strcpy (send_buffer
, &send_buffer
[sent
]);
580 /* In STR, insert a & before each &, each space, each newline, and
581 any initial -. Change spaces to underscores, too, so that the
582 return value never contains a space.
584 Does not change the string. Outputs the result to STREAM. */
586 quote_argument (s
, str
)
590 char *copy
= (char *) xmalloc (strlen (str
) * 2 + 1);
611 if (*p
== '&' || (*p
== '-' && p
== str
))
624 /* The inverse of quote_argument. Removes quoting in string STR by
625 modifying the string in place. Returns STR. */
628 unquote_argument (str
)
660 file_name_absolute_p (filename
)
661 const unsigned char *filename
;
663 /* Sanity check, it shouldn't happen. */
664 if (! filename
) return FALSE
;
666 /* /xxx is always an absolute path. */
667 if (filename
[0] == '/') return TRUE
;
669 /* Empty filenames (which shouldn't happen) are relative. */
670 if (filename
[0] == '\0') return FALSE
;
673 /* X:\xxx is always absolute; X:xxx is an error and will fail. */
674 if (isalpha (filename
[0])
675 && filename
[1] == ':' && (filename
[2] == '\\' || filename
[2] == '/'))
678 /* Both \xxx and \\xxx\yyy are absolute. */
679 if (filename
[0] == '\\') return TRUE
;
686 /* Wrapper to make WSACleanup a cdecl, as required by atexit(). */
688 __cdecl
close_winsock ()
693 /* Initialize the WinSock2 library. */
695 initialize_sockets ()
699 if (WSAStartup (MAKEWORD (2, 0), &wsaData
))
701 message (TRUE
, "%s: error initializing WinSock2", progname
);
705 atexit (close_winsock
);
707 #endif /* WINDOWSNT */
711 * Read the information needed to set up a TCP comm channel with
712 * the Emacs server: host, port, pid and authentication string.
715 get_server_config (server
, authentication
)
716 struct sockaddr_in
*server
;
717 char *authentication
;
724 if (file_name_absolute_p (server_file
))
725 config
= fopen (server_file
, "rb");
728 char *home
= getenv ("HOME");
732 char *path
= alloca (32 + strlen (home
) + strlen (server_file
));
733 sprintf (path
, "%s/.emacs.d/server/%s", home
, server_file
);
734 config
= fopen (path
, "rb");
737 if (!config
&& (home
= getenv ("APPDATA")))
739 char *path
= alloca (32 + strlen (home
) + strlen (server_file
));
740 sprintf (path
, "%s/.emacs.d/server/%s", home
, server_file
);
741 config
= fopen (path
, "rb");
749 if (fgets (dotted
, sizeof dotted
, config
)
750 && (port
= strchr (dotted
, ':'))
751 && (pid
= strchr (port
, ' ')))
758 message (TRUE
, "%s: invalid configuration info", progname
);
762 server
->sin_family
= AF_INET
;
763 server
->sin_addr
.s_addr
= inet_addr (dotted
);
764 server
->sin_port
= htons (atoi (port
));
766 if (! fread (authentication
, AUTH_KEY_LENGTH
, 1, config
))
768 message (TRUE
, "%s: cannot read authentication info", progname
);
774 emacs_pid
= atoi (pid
);
783 struct sockaddr_in server
;
784 struct linger l_arg
= {1, 1};
785 char auth_string
[AUTH_KEY_LENGTH
+ 1];
787 if (! get_server_config (&server
, auth_string
))
788 return INVALID_SOCKET
;
790 if (server
.sin_addr
.s_addr
!= inet_addr ("127.0.0.1"))
791 message (FALSE
, "%s: connected to remote socket at %s\n",
792 progname
, inet_ntoa (server
.sin_addr
));
795 * Open up an AF_INET socket
797 if ((s
= socket (AF_INET
, SOCK_STREAM
, IPPROTO_TCP
)) < 0)
799 message (TRUE
, "%s: socket: %s\n", progname
, strerror (errno
));
800 return INVALID_SOCKET
;
806 if (connect (s
, (struct sockaddr
*) &server
, sizeof server
) < 0)
808 message (TRUE
, "%s: connect: %s\n", progname
, strerror (errno
));
809 return INVALID_SOCKET
;
812 setsockopt (s
, SOL_SOCKET
, SO_LINGER
, (char *) &l_arg
, sizeof l_arg
);
815 * Send the authentication
817 auth_string
[AUTH_KEY_LENGTH
] = '\0';
819 SEND_STRING ("-auth ");
820 SEND_STRING (auth_string
);
826 #if !defined (NO_SOCKETS_IN_FILE_SYSTEM)
828 /* Three possibilities:
829 2 - can't be `stat'ed (sets errno)
830 1 - isn't owned by us
831 0 - success: none of the above */
834 socket_status (socket_name
)
839 if (stat (socket_name
, &statbfr
) == -1)
842 if (statbfr
.st_uid
!= geteuid ())
849 /* A signal handler that passes the signal to the Emacs process.
850 Useful for SIGWINCH. */
853 pass_signal_to_emacs (int signalnum
)
855 int old_errno
= errno
;
858 kill (emacs_pid
, signalnum
);
860 signal (signalnum
, pass_signal_to_emacs
);
864 /* Signal handler for SIGCONT; notify the Emacs process that it can
865 now resume our tty frame. */
868 handle_sigcont (int signalnum
)
870 int old_errno
= errno
;
872 if (tcgetpgrp (1) == getpgrp ())
874 /* We are in the foreground. */
875 fprintf (out
, "-resume \n");
877 fsync (fileno (out
));
881 /* We are in the background; cancel the continue. */
882 kill (getpid (), SIGSTOP
);
885 signal (signalnum
, handle_sigcont
);
889 /* Signal handler for SIGTSTP; notify the Emacs process that we are
890 going to sleep. Normally the suspend is initiated by Emacs via
891 server-handle-suspend-tty, but if the server gets out of sync with
892 reality, we may get a SIGTSTP on C-z. Handling this signal and
893 notifying Emacs about it should get things under control again. */
896 handle_sigtstp (int signalnum
)
898 int old_errno
= errno
;
903 fprintf (out
, "-suspend \n");
905 fsync (fileno (out
));
908 /* Unblock this signal and call the default handler by temprarily
909 changing the handler and resignalling. */
910 sigprocmask (SIG_BLOCK
, NULL
, &set
);
911 sigdelset (&set
, signalnum
);
912 signal (signalnum
, SIG_DFL
);
913 kill (getpid (), signalnum
);
914 sigprocmask (SIG_SETMASK
, &set
, NULL
); /* Let's the above signal through. */
915 signal (signalnum
, handle_sigtstp
);
920 /* Set up signal handlers before opening a frame on the current tty. */
925 /* Set up signal handlers. */
926 signal (SIGWINCH
, pass_signal_to_emacs
);
928 /* Don't pass SIGINT and SIGQUIT to Emacs, because it has no way of
929 deciding which terminal the signal came from. C-g is now a
930 normal input event on secondary terminals. */
932 signal (SIGINT
, pass_signal_to_emacs
);
933 signal (SIGQUIT
, pass_signal_to_emacs
);
936 signal (SIGCONT
, handle_sigcont
);
937 signal (SIGTSTP
, handle_sigtstp
);
938 signal (SIGTTOU
, handle_sigtstp
);
943 /* Returns 1 if PREFIX is a prefix of STRING. */
945 strprefix (char *prefix
, char *string
)
954 for (i
= 0; prefix
[i
]; i
++)
955 if (!string
[i
] || string
[i
] != prefix
[i
])
965 struct sockaddr_un server
;
968 * Open up an AF_UNIX socket in this person's home directory
971 if ((s
= socket (AF_UNIX
, SOCK_STREAM
, 0)) < 0)
973 message (TRUE
, "%s: socket: %s\n", progname
, strerror (errno
));
974 return INVALID_SOCKET
;
977 server
.sun_family
= AF_UNIX
;
981 int default_sock
= !socket_name
;
984 char *server_name
= "server";
986 if (socket_name
&& !index (socket_name
, '/') && !index (socket_name
, '\\'))
987 { /* socket_name is a file name component. */
988 server_name
= socket_name
;
990 default_sock
= 1; /* Try both UIDs. */
995 socket_name
= alloca (100 + strlen (server_name
));
996 sprintf (socket_name
, "/tmp/emacs%d/%s",
997 (int) geteuid (), server_name
);
1000 if (strlen (socket_name
) < sizeof (server
.sun_path
))
1001 strcpy (server
.sun_path
, socket_name
);
1004 message (TRUE
, "%s: socket-name %s too long",
1005 progname
, socket_name
);
1009 /* See if the socket exists, and if it's owned by us. */
1010 sock_status
= socket_status (server
.sun_path
);
1011 saved_errno
= errno
;
1012 if (sock_status
&& default_sock
)
1014 /* Failing that, see if LOGNAME or USER exist and differ from
1015 our euid. If so, look for a socket based on the UID
1016 associated with the name. This is reminiscent of the logic
1017 that init_editfns uses to set the global Vuser_full_name. */
1019 char *user_name
= (char *) getenv ("LOGNAME");
1022 user_name
= (char *) getenv ("USER");
1026 struct passwd
*pw
= getpwnam (user_name
);
1028 if (pw
&& (pw
->pw_uid
!= geteuid ()))
1030 /* We're running under su, apparently. */
1031 socket_name
= alloca (100 + strlen (server_name
));
1032 sprintf (socket_name
, "/tmp/emacs%d/%s",
1033 (int) pw
->pw_uid
, server_name
);
1035 if (strlen (socket_name
) < sizeof (server
.sun_path
))
1036 strcpy (server
.sun_path
, socket_name
);
1039 message (TRUE
, "%s: socket-name %s too long",
1040 progname
, socket_name
);
1041 exit (EXIT_FAILURE
);
1044 sock_status
= socket_status (server
.sun_path
);
1045 saved_errno
= errno
;
1048 errno
= saved_errno
;
1052 switch (sock_status
)
1055 /* There's a socket, but it isn't owned by us. This is OK if
1057 if (0 != geteuid ())
1059 message (TRUE
, "%s: Invalid socket owner\n", progname
);
1060 return INVALID_SOCKET
;
1066 if (saved_errno
== ENOENT
)
1068 "%s: can't find socket; have you started the server?\n\
1069 To start the server in Emacs, type \"M-x server-start\".\n",
1072 message (TRUE
, "%s: can't stat %s: %s\n",
1073 progname
, server
.sun_path
, strerror (saved_errno
));
1074 return INVALID_SOCKET
;
1078 if (connect (s
, (struct sockaddr
*) &server
, strlen (server
.sun_path
) + 2)
1081 message (TRUE
, "%s: connect: %s\n", progname
, strerror (errno
));
1082 return INVALID_SOCKET
;
1087 #endif /* ! NO_SOCKETS_IN_FILE_SYSTEM */
1096 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
1097 /* Explicit --socket-name argument. */
1100 s
= set_local_socket ();
1101 if ((s
!= INVALID_SOCKET
) || alternate_editor
)
1103 message (TRUE
, "%s: error accessing socket \"%s\"",
1104 progname
, socket_name
);
1105 exit (EXIT_FAILURE
);
1109 /* Explicit --server-file arg or EMACS_SERVER_FILE variable. */
1111 server_file
= getenv ("EMACS_SERVER_FILE");
1115 s
= set_tcp_socket ();
1116 if ((s
!= INVALID_SOCKET
) || alternate_editor
)
1119 message (TRUE
, "%s: error accessing server file \"%s\"",
1120 progname
, server_file
);
1121 exit (EXIT_FAILURE
);
1124 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
1125 /* Implicit local socket. */
1126 s
= set_local_socket ();
1127 if (s
!= INVALID_SOCKET
)
1131 /* Implicit server file. */
1132 server_file
= "server";
1133 s
= set_tcp_socket ();
1134 if ((s
!= INVALID_SOCKET
) || alternate_editor
)
1137 /* No implicit or explicit socket, and no alternate editor. */
1138 message (TRUE
, "%s: No socket or alternate editor. Please use:\n\n"
1139 #ifndef NO_SOCKETS_IN_FILE_SYSTEM
1142 "\t--server-file (or environment variable EMACS_SERVER_FILE)\n\
1143 \t--alternate-editor (or environment variable ALTERNATE_EDITOR)\n",
1145 exit (EXIT_FAILURE
);
1154 int i
, rl
, needlf
= 0;
1156 char string
[BUFSIZ
+1];
1162 /* Process options. */
1163 decode_options (argc
, argv
);
1165 if ((argc
- optind
< 1) && !eval
&& !tty
&& !window_system
)
1167 message (TRUE
, "%s: file name or argument required\n"
1168 "Try `%s --help' for more information\n",
1169 progname
, progname
);
1170 exit (EXIT_FAILURE
);
1173 if ((s
= set_socket ()) == INVALID_SOCKET
)
1177 cwd
= get_current_dir_name ();
1180 /* getwd puts message in STRING if it fails. */
1181 message (TRUE
, "%s: %s\n", progname
,
1182 "Cannot get current working directory");
1188 Modern Windows restrict which processes can set the foreground window.
1189 emacsclient can allow Emacs to grab the focus by calling the function
1190 AllowSetForegroundWindow(). Unfortunately, older Windows (W95, W98
1191 and NT) lack this function, so we have to check its availability.
1197 if (hUser32
= LoadLibrary ("user32.dll"))
1200 if (set_fg
= GetProcAddress (hUser32
, "AllowSetForegroundWindow"))
1202 FreeLibrary (hUser32
);
1207 /* First of all, send our version number for verification. */
1208 SEND_STRING ("-version ");
1209 SEND_STRING (VERSION
);
1212 /* Send over our environment. */
1215 extern char **environ
;
1217 for (i
= 0; environ
[i
]; i
++)
1219 char *name
= xstrdup (environ
[i
]);
1220 char *value
= strchr (name
, '=');
1221 SEND_STRING ("-env ");
1222 SEND_QUOTED (environ
[i
]);
1227 /* Send over our current directory. */
1230 SEND_STRING ("-dir ");
1238 SEND_STRING ("-nowait ");
1241 SEND_STRING ("-current-frame ");
1245 SEND_STRING ("-display ");
1246 SEND_QUOTED (display
);
1252 char *tty_name
= ttyname (fileno (stdin
));
1253 char *type
= getenv ("TERM");
1257 message (TRUE
, "%s: could not get terminal name\n", progname
);
1263 message (TRUE
, "%s: please set the TERM variable to your terminal type\n",
1268 if (! strcmp (type
, "eterm"))
1270 /* This causes nasty, MULTI_KBOARD-related input lockouts. */
1271 message (TRUE
, "%s: opening a frame in an Emacs term buffer"
1272 " is not supported\n", progname
);
1278 SEND_STRING ("-tty ");
1279 SEND_QUOTED (tty_name
);
1286 SEND_STRING ("-window-system ");
1288 if ((argc
- optind
> 0))
1290 for (i
= optind
; i
< argc
; i
++)
1296 /* Don't prepend cwd or anything like that. */
1297 SEND_STRING ("-eval ");
1298 SEND_QUOTED (argv
[i
]);
1303 if (*argv
[i
] == '+')
1305 char *p
= argv
[i
] + 1;
1306 while (isdigit ((unsigned char) *p
) || *p
== ':') p
++;
1309 SEND_STRING ("-position ");
1310 SEND_QUOTED (argv
[i
]);
1317 else if (! file_name_absolute_p (argv
[i
]))
1320 SEND_STRING ("-file ");
1326 SEND_QUOTED (argv
[i
]);
1332 if (!tty
&& !window_system
)
1334 while ((str
= fgets (string
, BUFSIZ
, stdin
)))
1337 SEND_STRING ("-eval ");
1339 SEND_STRING ("-file ");
1348 /* Wait for an answer. */
1349 if (!eval
&& !tty
&& !nowait
)
1351 printf ("Waiting for Emacs...");
1357 /* Now, wait for an answer and print any messages. */
1358 while ((rl
= recv (s
, string
, BUFSIZ
, 0)) > 0)
1363 p
= string
+ strlen (string
) - 1;
1364 while (p
> string
&& *p
== '\n')
1367 if (strprefix ("-good-version ", string
))
1369 /* -good-version: The versions match. */
1371 else if (strprefix ("-emacs-pid ", string
))
1373 /* -emacs-pid PID: The process id of the Emacs process. */
1374 emacs_pid
= strtol (string
+ strlen ("-emacs-pid"), NULL
, 10);
1376 else if (strprefix ("-window-system-unsupported ", string
))
1378 /* -window-system-unsupported: Emacs was compiled without X
1379 support. Try again on the terminal. */
1385 else if (strprefix ("-print ", string
))
1387 /* -print STRING: Print STRING on the terminal. */
1388 str
= unquote_argument (string
+ strlen ("-print "));
1392 needlf
= str
[0] == '\0' ? needlf
: str
[strlen (str
) - 1] != '\n';
1394 else if (strprefix ("-error ", string
))
1396 /* -error DESCRIPTION: Signal an error on the terminal. */
1397 str
= unquote_argument (string
+ strlen ("-error "));
1400 fprintf (stderr
, "*ERROR*: %s", str
);
1401 needlf
= str
[0] == '\0' ? needlf
: str
[strlen (str
) - 1] != '\n';
1403 else if (strprefix ("-suspend ", str
))
1405 /* -suspend: Suspend this terminal, i.e., stop the process. */
1413 /* Unknown command. */
1416 printf ("*ERROR*: Unknown message: %s", string
);
1417 needlf
= string
[0] == '\0' ? needlf
: string
[strlen (string
) - 1] != '\n';
1427 return EXIT_SUCCESS
;
1430 #endif /* HAVE_SOCKETS && HAVE_INET_SOCKETS */
1433 #ifndef HAVE_STRERROR
1438 extern char *sys_errlist
[];
1439 extern int sys_nerr
;
1441 if (errnum
>= 0 && errnum
< sys_nerr
)
1442 return sys_errlist
[errnum
];
1443 return (char *) "Unknown error";
1446 #endif /* ! HAVE_STRERROR */
1448 /* arch-tag: f39bb9c4-73eb-477e-896d-50832e2ca9a7
1449 (do not change this comment) */
1451 /* emacsclient.c ends here */