+#include <getopt.h>
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#ifdef VMS
+# include "vms-pwd.h"
+#else
+# include <pwd.h>
+#endif /* not VMS */
+
+char *getenv (), *getwd ();
+char *getcwd ();
+
+#ifndef VERSION
+#define VERSION "unspecified"
+#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;
+
+void print_help_and_exit ();
+
+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' },
+ { "socket-name", required_argument, NULL, 's' },
+ { "display", required_argument, NULL, 'd' },
+ { 0, 0, 0, 0 }
+};
+
+/* 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,
+ "VHnea:s:d:", 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;
+
+ case 's':
+ socket_name = optarg;
+ break;
+
+ case 'd':
+ display = optarg;
+ break;
+
+ case 'n':
+ nowait = 1;
+ break;
+
+ case 'e':
+ eval = 1;
+ break;
+
+ case 'V':
+ printf ("emacsclient %s\n", VERSION);
+ exit (EXIT_SUCCESS);
+ break;
+
+ case 'H':
+ print_help_and_exit ();
+ break;
+
+ default:
+ fprintf (stderr, "Try `%s --help' for more information\n", progname);
+ exit (EXIT_FAILURE);
+ break;
+ }
+ }
+}
+
+void
+print_help_and_exit ()
+{
+ printf (
+ "Usage: %s [OPTIONS] FILE...\n\
+Tell the Emacs server to visit the specified files.\n\
+Every FILE can be either just a FILENAME or [+LINE[:COLUMN]] FILENAME.\n\
+\n\
+The following OPTIONS are accepted:\n\
+-V, --version Just print a version info and return\n\
+-H, --help Print this usage information message\n\
+-n, --no-wait Don't wait for the server to return\n\
+-e, --eval Evaluate the FILE arguments as ELisp expressions\n\
+-d, --display=DISPLAY Visit the file in the given display\n\
+-s, --socket-name=FILENAME\n\
+ Set the filename of the UNIX socket for communication\n\
+-a, --alternate-editor=EDITOR\n\
+ Editor to fallback to if the server is not running\n\
+\n\
+Report bugs to bug-gnu-emacs@gnu.org.\n", progname);
+ exit (EXIT_SUCCESS);
+}
+
+/* In NAME, insert a & before each &, each space, each newline, and
+ any initial -. Change spaces to underscores, too, so that the
+ return value never contains a space. */
+
+void
+quote_file_name (name, stream)
+ char *name;
+ FILE *stream;
+{
+ char *copy = (char *) malloc (strlen (name) * 2 + 1);
+ char *p, *q;
+
+ p = name;
+ q = copy;
+ while (*p)
+ {
+ if (*p == ' ')
+ {
+ *q++ = '&';
+ *q++ = '_';
+ p++;
+ }
+ else if (*p == '\n')
+ {
+ *q++ = '&';
+ *q++ = 'n';
+ p++;
+ }
+ else
+ {
+ if (*p == '&' || (*p == '-' && p == name))
+ *q++ = '&';
+ *q++ = *p++;
+ }
+ }
+ *q++ = 0;
+
+ fprintf (stream, "%s", copy);
+
+ free (copy);
+}
+
+/* Like malloc but get fatal error if memory is exhausted. */