+#include "getopt.h"
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#ifdef VMS
+# include "vms-pwd.h"
+#else /* not VMS */
+#ifdef WINDOWSNT
+# include <io.h>
+#else /* not WINDOWSNT */
+# include <pwd.h>
+#endif /* not WINDOWSNT */
+#endif /* not VMS */
+
+char *getenv (), *getwd ();
+char *(getcwd) ();
+
+#ifndef VERSION
+#define VERSION "unspecified"
+#endif
+\f
+#define SEND_STRING(data) (send_to_emacs (s, (data)))
+#define SEND_QUOTED(data) (quote_file_name (s, (data)))
+
+#ifndef EXIT_SUCCESS
+#define EXIT_SUCCESS 0
+#endif
+
+#ifndef EXIT_FAILURE
+#define EXIT_FAILURE 1
+#endif
+
+#ifndef FALSE
+#define FALSE 0
+#endif
+
+#ifndef TRUE
+#define TRUE 1
+#endif
+
+#ifndef NO_RETURN
+#define NO_RETURN
+#endif
+\f
+/* Name used to invoke this program. */
+char *progname;
+
+/* Nonzero means don't wait for a response from Emacs. --no-wait. */
+int nowait = 0;
+
+/* Nonzero means args are expressions to be evaluated. --eval. */
+int eval = 0;
+
+/* The display on which Emacs should work. --display. */
+char *display = NULL;
+
+/* If non-NULL, the name of an editor to fallback to if the server
+ is not running. --alternate-editor. */
+const char *alternate_editor = NULL;
+
+/* If non-NULL, the filename of the UNIX socket. */
+char *socket_name = NULL;
+
+/* If non-NULL, the filename of the authentication file. */
+char *server_file = NULL;
+
+/* PID of the Emacs server process. */
+int emacs_pid = 0;
+
+void print_help_and_exit () NO_RETURN;
+
+struct option longopts[] =
+{
+ { "no-wait", no_argument, NULL, 'n' },
+ { "eval", no_argument, NULL, 'e' },
+ { "help", no_argument, NULL, 'H' },
+ { "version", no_argument, NULL, 'V' },
+ { "alternate-editor", required_argument, NULL, 'a' },
+#ifndef NO_SOCKETS_IN_FILE_SYSTEM
+ { "socket-name", required_argument, NULL, 's' },
+#endif
+ { "server-file", required_argument, NULL, 'f' },
+ { "display", required_argument, NULL, 'd' },
+ { 0, 0, 0, 0 }
+};
+
+/* Message functions. */
+
+#ifdef WINDOWSNT
+int
+w32_window_app ()
+{
+ static int window_app = -1;
+ char szTitle[MAX_PATH];
+
+ if (window_app < 0)
+ /* Checking for STDOUT does not work; it's a valid handle also in
+ nonconsole apps. Testing for the console title seems to work. */
+ window_app = (GetConsoleTitleA (szTitle, MAX_PATH) == 0);
+
+ return window_app;
+}
+#endif
+
+void
+message (int is_error, char *message, ...)
+{
+ char msg [2048];
+ va_list args;
+
+ va_start (args, message);
+ vsprintf (msg, message, args);
+ va_end (args);
+
+#ifdef WINDOWSNT
+ if (w32_window_app ())
+ {
+ if (is_error)
+ MessageBox (NULL, msg, "Emacsclient ERROR", MB_ICONERROR);
+ else
+ MessageBox (NULL, msg, "Emacsclient", MB_ICONINFORMATION);
+ }
+ else
+#endif
+ {
+ FILE *f = is_error ? stderr : stdout;
+
+ fputs (msg, f);
+ fflush (f);
+ }
+}
+
+/* Decode the options from argv and argc.
+ The global variable `optind' will say how many arguments we used up. */
+
+void
+decode_options (argc, argv)
+ int argc;
+ char **argv;
+{
+ alternate_editor = getenv ("ALTERNATE_EDITOR");
+
+ while (1)
+ {
+ int opt = getopt_long (argc, argv,
+#ifndef NO_SOCKETS_IN_FILE_SYSTEM
+ "VHnea:s:f:d:",
+#else
+ "VHnea:f:d:",
+#endif
+ longopts, 0);
+
+ if (opt == EOF)
+ break;
+
+ switch (opt)
+ {
+ case 0:
+ /* If getopt returns 0, then it has already processed a
+ long-named option. We should do nothing. */
+ break;
+
+ case 'a':
+ alternate_editor = optarg;
+ break;
+
+#ifndef NO_SOCKETS_IN_FILE_SYSTEM
+ case 's':
+ socket_name = optarg;
+ break;
+#endif
+
+ case 'f':
+ server_file = optarg;
+ break;
+
+ case 'd':
+ display = optarg;
+ break;
+
+ case 'n':
+ nowait = 1;
+ break;
+
+ case 'e':
+ eval = 1;
+ break;