+#include <stdio.h>
+#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 ();
+
+/* This is defined with -D from the compilation command,
+ which extracts it from ../lisp/version.el. */
+
+#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;
+
+void print_help_and_exit ();
+
+struct option longopts[] =
+{
+ { "no-wait", no_argument, NULL, 'n' },
+ { "help", no_argument, NULL, 'H' },
+ { "version", no_argument, NULL, 'V' },
+ { "alternate-editor",required_argument, NULL, 'a' },
+ { 0 }
+};
+
+
+const char * alternate_editor = NULL;
+
+/* 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;
+{
+ while (1)
+ {
+ int opt = getopt_long (argc, argv,
+ "VHna:", longopts, 0);
+
+ if (opt == EOF)
+ break;
+
+ alternate_editor = getenv ("ALTERNATE_EDITOR");
+
+ 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 'n':
+ nowait = 1;
+ break;
+
+ case 'V':
+ fprintf (stderr, "emacsclient %s\n", VERSION);
+ exit (1);
+ break;
+
+ case 'H':
+ default:
+ print_help_and_exit ();
+ }
+ }
+}
+
+void
+print_help_and_exit ()
+{
+ fprintf (stderr,
+ "Usage: %s [-a ALTERNATE-EDITOR] [-n] [--no-wait] [+LINENUMBER] FILENAME\n",
+ progname);
+ fprintf (stderr,
+ "Or %s --version\n",
+ progname);
+ fprintf (stderr,
+ "Report bugs to bug-gnu-emacs@gnu.org.\n");
+ exit (1);
+}
+
+/* Return a copy of NAME, inserting a &
+ before each &, each space, and any initial -.
+ Change spaces to underscores, too, so that the
+ return value never contains a space. */