+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. */
+
+char *
+quote_file_name (name)
+ char *name;
+{
+ 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 == '&' || (*p == '-' && p == name))
+ *q++ = '&';
+ *q++ = *p++;
+ }
+ }
+ *q++ = 0;
+
+
+ return copy;
+}
+
+/* Like malloc but get fatal error if memory is exhausted. */
+
+long *
+xmalloc (size)
+ unsigned int size;
+{
+ long *result = (long *) malloc (size);
+ if (result == NULL)
+ {
+ perror ("malloc");
+ exit (1);
+ }
+ return result;
+}
+\f
+/*
+ Try to run a different command, or --if no alternate editor is
+ defined-- exit with an errorcode.
+*/
+fail (argc, argv)
+ int argc;
+ char **argv;
+{
+ if (alternate_editor)
+ {
+ int i = optind -1 ;
+ execvp (alternate_editor, argv + i);
+ }
+ else
+ {
+ exit (1);
+ }
+}
+
+
+
+\f
+#if !defined (HAVE_SOCKETS) && !defined (HAVE_SYSVIPC)