+/*
+ execvp wrapper for Windows. Quotes arguments with embedded spaces.
+
+ This is necessary due to the broken implementation of exec* routines in
+ the Microsoft libraries: they concatenate the arguments together without
+ quoting special characters, and pass the result to CreateProcess, with
+ predictably bad results. By contrast, Posix execvp passes the arguments
+ directly into the argv array of the child process.
+*/
+int
+w32_execvp (path, argv)
+ char *path;
+ char **argv;
+{
+ int i;
+
+ /* Required to allow a .BAT script as alternate editor. */
+ argv[0] = (char *) alternate_editor;
+
+ for (i = 0; argv[i]; i++)
+ if (strchr (argv[i], ' '))
+ {
+ char *quoted = alloca (strlen (argv[i]) + 3);
+ sprintf (quoted, "\"%s\"", argv[i]);
+ argv[i] = quoted;
+ }
+
+ return execvp (path, argv);
+}
+
+#undef execvp
+#define execvp w32_execvp
+
+#endif /* WINDOWSNT */
+
+/*
+ Try to run a different command, or --if no alternate editor is
+ defined-- exit with an errorcode.
+*/
+void
+fail (argc, argv)
+ int argc;
+ char **argv;
+{
+ if (alternate_editor)
+ {
+ int i = optind - 1;
+
+ execvp (alternate_editor, argv + i);
+ message (TRUE, "%s: error executing alternate editor \"%s\"\n",
+ progname, alternate_editor);
+ }
+ exit (EXIT_FAILURE);
+}
+
+\f
+#if !defined (HAVE_SOCKETS) || !defined (HAVE_INET_SOCKETS)
+
+int
+main (argc, argv)
+ int argc;
+ char **argv;
+{
+ message (TRUE, "%s: Sorry, the Emacs server is supported only\non systems with Berkely sockets.\n",
+ argv[0]);
+
+ fail (argc, argv);
+}
+
+#else /* HAVE_SOCKETS && HAVE_INET_SOCKETS */
+
+#ifdef WINDOWSNT
+# include <winsock2.h>
+#else
+# include <sys/types.h>
+# include <sys/socket.h>
+# include <sys/un.h>
+# include <sys/stat.h>
+# include <errno.h>
+#endif
+
+#define AUTH_KEY_LENGTH 64
+#define SEND_BUFFER_SIZE 4096
+
+extern char *strerror ();
+extern int errno;
+
+/* Buffer to accumulate data to send in TCP connections. */
+char send_buffer[SEND_BUFFER_SIZE + 1];
+int sblen = 0; /* Fill pointer for the send buffer. */
+
+/* Let's send the data to Emacs when either
+ - the data ends in "\n", or
+ - the buffer is full (but this shouldn't happen)
+ Otherwise, we just accumulate it. */
+void
+send_to_emacs (s, data)
+ HSOCKET s;
+ char *data;
+{
+ while (data)
+ {
+ int dlen = strlen (data);
+ if (dlen + sblen >= SEND_BUFFER_SIZE)
+ {
+ int part = SEND_BUFFER_SIZE - sblen;
+ strncpy (&send_buffer[sblen], data, part);
+ data += part;
+ sblen = SEND_BUFFER_SIZE;
+ }
+ else if (dlen)
+ {
+ strcpy (&send_buffer[sblen], data);
+ data = NULL;
+ sblen += dlen;
+ }
+ else
+ break;
+
+ if (sblen == SEND_BUFFER_SIZE
+ || (sblen > 0 && send_buffer[sblen-1] == '\n'))
+ {
+ int sent = send (s, send_buffer, sblen, 0);
+ if (sent != sblen)
+ strcpy (send_buffer, &send_buffer[sent]);
+ sblen -= sent;
+ }
+ }
+}
+
+/* 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 (s, name)
+ HSOCKET s;