+void msdos_downcase_filename (unsigned char *);
+
+/* Destructively turn backslashes into slashes. */
+
+void
+dostounix_filename (p)
+ register char *p;
+{
+ msdos_downcase_filename (p);
+
+ while (*p)
+ {
+ if (*p == '\\')
+ *p = '/';
+ p++;
+ }
+}
+
+/* Destructively turn slashes into backslashes. */
+
+void
+unixtodos_filename (p)
+ register char *p;
+{
+ if (p[1] == ':' && *p >= 'A' && *p <= 'Z')
+ {
+ *p += 'a' - 'A';
+ p += 2;
+ }
+
+ while (*p)
+ {
+ if (*p == '/')
+ *p = '\\';
+ p++;
+ }
+}
+
+/* Get the default directory for a given drive. 0=def, 1=A, 2=B, ... */
+
+int
+getdefdir (drive, dst)
+ int drive;
+ char *dst;
+{
+ char in_path[4], *p = in_path;
+ int e = errno;
+
+ /* Generate "X:." (when drive is X) or "." (when drive is 0). */
+ if (drive != 0)
+ {
+ *p++ = drive + 'A' - 1;
+ *p++ = ':';
+ }
+
+ *p++ = '.';
+ *p = '\0';
+ errno = 0;
+ _fixpath (in_path, dst);
+ if (errno)
+ return 0;
+
+ msdos_downcase_filename (dst);
+
+ errno = e;
+ return 1;
+}
+
+/* Remove all CR's that are followed by a LF. */
+
+int
+crlf_to_lf (n, buf)
+ register int n;
+ register unsigned char *buf;
+{
+ unsigned char *np = buf;
+ unsigned char *startp = buf;
+ unsigned char *endp = buf + n;
+
+ if (n == 0)
+ return n;
+ while (buf < endp - 1)
+ {
+ if (*buf == 0x0d)
+ {
+ if (*(++buf) != 0x0a)
+ *np++ = 0x0d;
+ }
+ else
+ *np++ = *buf++;
+ }
+ if (buf < endp)
+ *np++ = *buf++;
+ return np - startp;
+}
+
+#if defined(__DJGPP__) && __DJGPP__ == 2 && __DJGPP_MINOR__ == 0
+
+/* In DJGPP v2.0, library `write' can call `malloc', which might
+ cause relocation of the buffer whose address we get in ADDR.
+ Here is a version of `write' that avoids calling `malloc',
+ to serve us until such time as the library is fixed.
+ Actually, what we define here is called `__write', because
+ `write' is a stub that just jmp's to `__write' (to be
+ POSIXLY-correct with respect to the global name-space). */
+
+#include <io.h> /* for _write */
+#include <libc/dosio.h> /* for __file_handle_modes[] */
+
+static char xbuf[64 * 1024]; /* DOS cannot write more in one chunk */
+
+#define XBUF_END (xbuf + sizeof (xbuf) - 1)
+
+int
+__write (int handle, const void *buffer, size_t count)
+{
+ if (count == 0)
+ return 0;
+
+ if(__file_handle_modes[handle] & O_BINARY)
+ return _write (handle, buffer, count);
+ else
+ {
+ char *xbp = xbuf;
+ const char *bp = buffer;
+ int total_written = 0;
+ int nmoved = 0, ncr = 0;
+
+ while (count)
+ {
+ /* The next test makes sure there's space for at least 2 more
+ characters in xbuf[], so both CR and LF can be put there. */
+ if (xbp < XBUF_END)
+ {
+ if (*bp == '\n')
+ {
+ ncr++;
+ *xbp++ = '\r';
+ }
+ *xbp++ = *bp++;
+ nmoved++;
+ count--;
+ }
+ if (xbp >= XBUF_END || !count)
+ {
+ size_t to_write = nmoved + ncr;
+ int written = _write (handle, xbuf, to_write);
+
+ if (written == -1)
+ return -1;
+ else
+ total_written += nmoved; /* CRs aren't counted in ret value */
+
+ /* If some, but not all were written (disk full?), return
+ an estimate of the total written bytes not counting CRs. */
+ if (written < to_write)
+ return total_written - (to_write - written) * nmoved/to_write;
+
+ nmoved = 0;
+ ncr = 0;
+ xbp = xbuf;
+ }
+ }
+ return total_written;
+ }
+}
+
+/* A low-level file-renaming function which works around Windows 95 bug.
+ This is pulled directly out of DJGPP v2.01 library sources, and only
+ used when you compile with DJGPP v2.0. */
+
+#include <io.h>
+
+int _rename(const char *old, const char *new)
+{
+ __dpmi_regs r;
+ int olen = strlen(old) + 1;
+ int i;
+ int use_lfn = _USE_LFN;
+ char tempfile[FILENAME_MAX];
+ const char *orig = old;
+ int lfn_fd = -1;
+
+ r.x.dx = __tb_offset;
+ r.x.di = __tb_offset + olen;
+ r.x.ds = r.x.es = __tb_segment;
+
+ if (use_lfn)
+ {
+ /* Windows 95 bug: for some filenames, when you rename
+ file -> file~ (as in Emacs, to leave a backup), the
+ short 8+3 alias doesn't change, which effectively
+ makes OLD and NEW the same file. We must rename
+ through a temporary file to work around this. */
+
+ char *pbase = 0, *p;
+ static char try_char[] = "abcdefghijklmnopqrstuvwxyz012345789";
+ int idx = sizeof(try_char) - 1;
+
+ /* Generate a temporary name. Can't use `tmpnam', since $TMPDIR
+ might point to another drive, which will fail the DOS call. */
+ strcpy(tempfile, old);
+ for (p = tempfile; *p; p++) /* ensure temporary is on the same drive */
+ if (*p == '/' || *p == '\\' || *p == ':')
+ pbase = p;
+ if (pbase)
+ pbase++;
+ else
+ pbase = tempfile;
+ strcpy(pbase, "X$$djren$$.$$temp$$");
+
+ do
+ {
+ if (idx <= 0)
+ return -1;
+ *pbase = try_char[--idx];
+ } while (_chmod(tempfile, 0) != -1);
+
+ r.x.ax = 0x7156;
+ _put_path2(tempfile, olen);
+ _put_path(old);
+ __dpmi_int(0x21, &r);
+ if (r.x.flags & 1)
+ {
+ errno = __doserr_to_errno(r.x.ax);
+ return -1;
+ }
+
+ /* Now create a file with the original name. This will
+ ensure that NEW will always have a 8+3 alias
+ different from that of OLD. (Seems to be required
+ when NameNumericTail in the Registry is set to 0.) */
+ lfn_fd = _creat(old, 0);
+
+ olen = strlen(tempfile) + 1;
+ old = tempfile;
+ r.x.di = __tb_offset + olen;
+ }
+
+ for (i=0; i<2; i++)
+ {
+ if(use_lfn)
+ r.x.ax = 0x7156;
+ else
+ r.h.ah = 0x56;
+ _put_path2(new, olen);
+ _put_path(old);
+ __dpmi_int(0x21, &r);
+ if(r.x.flags & 1)
+ {
+ if (r.x.ax == 5 && i == 0) /* access denied */
+ remove(new); /* and try again */
+ else
+ {
+ errno = __doserr_to_errno(r.x.ax);
+
+ /* Restore to original name if we renamed it to temporary. */
+ if (use_lfn)
+ {
+ if (lfn_fd != -1)
+ {
+ _close (lfn_fd);
+ remove (orig);
+ }
+ _put_path2(orig, olen);
+ _put_path(tempfile);
+ r.x.ax = 0x7156;
+ __dpmi_int(0x21, &r);
+ }
+ return -1;
+ }
+ }
+ else
+ break;
+ }
+
+ /* Success. Delete the file possibly created to work
+ around the Windows 95 bug. */
+ if (lfn_fd != -1)
+ return (_close (lfn_fd) == 0) ? remove (orig) : -1;
+ return 0;
+}
+
+#endif /* __DJGPP__ == 2 && __DJGPP_MINOR__ == 0 */
+
+DEFUN ("msdos-long-file-names", Fmsdos_long_file_names, Smsdos_long_file_names,
+ 0, 0, 0,
+ "Return non-nil if long file names are supported on MSDOS.")
+ ()
+{
+ return (_USE_LFN ? Qt : Qnil);
+}
+
+/* Convert alphabetic characters in a filename to lower-case. */
+
+void
+msdos_downcase_filename (p)
+ register unsigned char *p;
+{
+ /* Always lower-case drive letters a-z, even if the filesystem
+ preserves case in filenames.
+ This is so MSDOS filenames could be compared by string comparison
+ functions that are case-sensitive. Even case-preserving filesystems
+ do not distinguish case in drive letters. */
+ if (p[1] == ':' && *p >= 'A' && *p <= 'Z')
+ {
+ *p += 'a' - 'A';
+ p += 2;
+ }
+
+ /* Under LFN we expect to get pathnames in their true case. */
+ if (NILP (Fmsdos_long_file_names ()))
+ for ( ; *p; p++)
+ if (*p >= 'A' && *p <= 'Z')
+ *p += 'a' - 'A';
+}
+
+DEFUN ("msdos-downcase-filename", Fmsdos_downcase_filename, Smsdos_downcase_filename,
+ 1, 1, 0,
+ "Convert alphabetic characters in FILENAME to lower case and return that.\n\
+When long filenames are supported, doesn't change FILENAME.\n\
+If FILENAME is not a string, returns nil.\n\
+The argument object is never altered--the value is a copy.")
+ (filename)
+ Lisp_Object filename;
+{
+ Lisp_Object tem;
+
+ if (! STRINGP (filename))
+ return Qnil;
+
+ tem = Fcopy_sequence (filename);
+ msdos_downcase_filename (XSTRING (tem)->data);
+ return tem;
+}
+\f
+/* The Emacs root directory as determined by init_environment. */
+
+static char emacsroot[MAXPATHLEN];
+
+char *
+rootrelativepath (rel)
+ char *rel;
+{
+ static char result[MAXPATHLEN + 10];
+
+ strcpy (result, emacsroot);
+ strcat (result, "/");
+ strcat (result, rel);
+ return result;
+}
+
+/* Define a lot of environment variables if not already defined. Don't
+ remove anything unless you know what you're doing -- lots of code will
+ break if one or more of these are missing. */
+
+void
+init_environment (argc, argv, skip_args)
+ int argc;
+ char **argv;
+ int skip_args;
+{
+ char *s, *t, *root;
+ int len;
+ static const char * const tempdirs[] = {
+ "$TMPDIR", "$TEMP", "$TMP", "c:/"
+ };
+ int i;
+ const int imax = sizeof (tempdirs) / sizeof (tempdirs[0]);
+
+ /* Make sure they have a usable $TMPDIR. Many Emacs functions use
+ temporary files and assume "/tmp" if $TMPDIR is unset, which
+ will break on DOS/Windows. Refuse to work if we cannot find
+ a directory, not even "c:/", usable for that purpose. */
+ for (i = 0; i < imax ; i++)
+ {
+ const char *tmp = tempdirs[i];
+
+ if (*tmp == '$')
+ tmp = getenv (tmp + 1);
+ /* Note that `access' can lie to us if the directory resides on a
+ read-only filesystem, like CD-ROM or a write-protected floppy.
+ The only way to be really sure is to actually create a file and
+ see if it succeeds. But I think that's too much to ask. */
+ if (tmp && access (tmp, D_OK) == 0)
+ {
+ setenv ("TMPDIR", tmp, 1);
+ break;
+ }
+ }
+ if (i >= imax)
+ cmd_error_internal
+ (Fcons (Qerror,
+ Fcons (build_string ("no usable temporary directories found!!"),
+ Qnil)),
+ "While setting TMPDIR: ");
+
+ /* Find our root from argv[0]. Assuming argv[0] is, say,
+ "c:/emacs/bin/emacs.exe" our root will be "c:/emacs". */
+ root = alloca (MAXPATHLEN + 20);
+ _fixpath (argv[0], root);
+ msdos_downcase_filename (root);
+ len = strlen (root);
+ while (len > 0 && root[len] != '/' && root[len] != ':')
+ len--;
+ root[len] = '\0';
+ if (len > 4
+ && (strcmp (root + len - 4, "/bin") == 0
+ || strcmp (root + len - 4, "/src") == 0)) /* under a debugger */
+ root[len - 4] = '\0';
+ else
+ strcpy (root, "c:/emacs"); /* let's be defensive */
+ len = strlen (root);
+ strcpy (emacsroot, root);
+
+ /* We default HOME to our root. */
+ setenv ("HOME", root, 0);
+
+ /* We default EMACSPATH to root + "/bin". */
+ strcpy (root + len, "/bin");
+ setenv ("EMACSPATH", root, 0);
+
+ /* I don't expect anybody to ever use other terminals so the internal
+ terminal is the default. */
+ setenv ("TERM", "internal", 0);
+
+#ifdef HAVE_X_WINDOWS
+ /* Emacs expects DISPLAY to be set. */
+ setenv ("DISPLAY", "unix:0.0", 0);
+#endif
+
+ /* SHELL is a bit tricky -- COMSPEC is the closest we come, but we must
+ downcase it and mirror the backslashes. */
+ s = getenv ("COMSPEC");
+ if (!s) s = "c:/command.com";
+ t = alloca (strlen (s) + 1);
+ strcpy (t, s);
+ dostounix_filename (t);
+ setenv ("SHELL", t, 0);
+
+ /* PATH is also downcased and backslashes mirrored. */
+ s = getenv ("PATH");
+ if (!s) s = "";
+ t = alloca (strlen (s) + 3);
+ /* Current directory is always considered part of MsDos's path but it is
+ not normally mentioned. Now it is. */
+ strcat (strcpy (t, ".;"), s);
+ dostounix_filename (t); /* Not a single file name, but this should work. */
+ setenv ("PATH", t, 1);
+
+ /* In some sense all dos users have root privileges, so... */
+ setenv ("USER", "root", 0);
+ setenv ("NAME", getenv ("USER"), 0);
+
+ /* Time zone determined from country code. To make this possible, the
+ country code may not span more than one time zone. In other words,
+ in the USA, you lose. */
+ if (!getenv ("TZ"))
+ switch (dos_country_code)
+ {
+ case 31: /* Belgium */
+ case 32: /* The Netherlands */
+ case 33: /* France */
+ case 34: /* Spain */
+ case 36: /* Hungary */
+ case 38: /* Yugoslavia (or what's left of it?) */
+ case 39: /* Italy */
+ case 41: /* Switzerland */
+ case 42: /* Tjekia */
+ case 45: /* Denmark */
+ case 46: /* Sweden */
+ case 47: /* Norway */
+ case 48: /* Poland */
+ case 49: /* Germany */
+ /* Daylight saving from last Sunday in March to last Sunday in
+ September, both at 2AM. */
+ setenv ("TZ", "MET-01METDST-02,M3.5.0/02:00,M9.5.0/02:00", 0);
+ break;
+ case 44: /* United Kingdom */
+ case 351: /* Portugal */
+ case 354: /* Iceland */
+ setenv ("TZ", "GMT+00", 0);
+ break;
+ case 81: /* Japan */
+ case 82: /* Korea */
+ setenv ("TZ", "JST-09", 0);
+ break;
+ case 90: /* Turkey */
+ case 358: /* Finland */
+ setenv ("TZ", "EET-02", 0);
+ break;
+ case 972: /* Israel */
+ /* This is an approximation. (For exact rules, use the
+ `zoneinfo/israel' file which comes with DJGPP, but you need
+ to install it in `/usr/share/zoneinfo/' directory first.) */
+ setenv ("TZ", "IST-02IDT-03,M4.1.6/00:00,M9.5.6/01:00", 0);
+ break;
+ }
+ tzset ();
+}
+
+\f
+
+static int break_stat; /* BREAK check mode status. */
+static int stdin_stat; /* stdin IOCTL status. */
+
+#if __DJGPP__ < 2
+
+/* These must be global. */
+static _go32_dpmi_seginfo ctrl_break_vector;
+static _go32_dpmi_registers ctrl_break_regs;
+static int ctrlbreakinstalled = 0;
+
+/* Interrupt level detection of Ctrl-Break. Don't do anything fancy here! */
+
+void
+ctrl_break_func (regs)
+ _go32_dpmi_registers *regs;
+{
+ Vquit_flag = Qt;
+}
+
+void
+install_ctrl_break_check ()
+{
+ if (!ctrlbreakinstalled)
+ {
+ /* Don't press Ctrl-Break if you don't have either DPMI or Emacs
+ was compiler with Djgpp 1.11 maintenance level 5 or later! */
+ ctrlbreakinstalled = 1;
+ ctrl_break_vector.pm_offset = (int) ctrl_break_func;
+ _go32_dpmi_allocate_real_mode_callback_iret (&ctrl_break_vector,
+ &ctrl_break_regs);
+ _go32_dpmi_set_real_mode_interrupt_vector (0x1b, &ctrl_break_vector);
+ }
+}
+
+#endif /* __DJGPP__ < 2 */
+
+/* Turn off Dos' Ctrl-C checking and inhibit interpretation of
+ control chars by DOS. Determine the keyboard type. */
+
+int
+dos_ttraw ()
+{
+ union REGS inregs, outregs;
+ static int first_time = 1;
+
+ break_stat = getcbrk ();
+ setcbrk (0);
+#if __DJGPP__ < 2
+ install_ctrl_break_check ();
+#endif
+
+ if (first_time)
+ {
+ inregs.h.ah = 0xc0;
+ int86 (0x15, &inregs, &outregs);
+ extended_kbd = (!outregs.x.cflag) && (outregs.h.ah == 0);
+
+ have_mouse = 0;
+
+ if (internal_terminal
+#ifdef HAVE_X_WINDOWS
+ && inhibit_window_system
+#endif
+ )
+ {
+ inregs.x.ax = 0x0021;
+ int86 (0x33, &inregs, &outregs);
+ have_mouse = (outregs.x.ax & 0xffff) == 0xffff;
+ if (!have_mouse)
+ {
+ /* Reportedly, the above doesn't work for some mouse drivers. There
+ is an additional detection method that should work, but might be
+ a little slower. Use that as an alternative. */
+ inregs.x.ax = 0x0000;
+ int86 (0x33, &inregs, &outregs);
+ have_mouse = (outregs.x.ax & 0xffff) == 0xffff;
+ }
+
+ if (have_mouse)
+ {
+ have_mouse = 1; /* enable mouse */
+ mouse_visible = 0;
+
+ if (outregs.x.bx == 3)
+ {
+ mouse_button_count = 3;
+ mouse_button_translate[0] = 0; /* Left */
+ mouse_button_translate[1] = 2; /* Middle */
+ mouse_button_translate[2] = 1; /* Right */
+ }
+ else
+ {
+ mouse_button_count = 2;
+ mouse_button_translate[0] = 0;
+ mouse_button_translate[1] = 1;
+ }
+ mouse_position_hook = &mouse_get_pos;
+ mouse_init ();
+ }
+ }
+
+ first_time = 0;
+
+#if __DJGPP__ >= 2
+
+ stdin_stat = setmode (fileno (stdin), O_BINARY);
+ return (stdin_stat != -1);
+ }
+ else
+ return (setmode (fileno (stdin), O_BINARY) != -1);
+
+#else /* __DJGPP__ < 2 */
+
+ }
+
+ /* I think it is wrong to overwrite `stdin_stat' every time
+ but the first one this function is called, but I don't
+ want to change the way it used to work in v1.x.--EZ */
+
+ inregs.x.ax = 0x4400; /* Get IOCTL status. */
+ inregs.x.bx = 0x00; /* 0 = stdin. */
+ intdos (&inregs, &outregs);
+ stdin_stat = outregs.h.dl;
+
+ inregs.x.dx = stdin_stat | 0x0020; /* raw mode */
+ inregs.x.ax = 0x4401; /* Set IOCTL status */
+ intdos (&inregs, &outregs);
+ return !outregs.x.cflag;
+
+#endif /* __DJGPP__ < 2 */
+}
+
+/* Restore status of standard input and Ctrl-C checking. */
+
+int
+dos_ttcooked ()
+{
+ union REGS inregs, outregs;
+
+ setcbrk (break_stat);
+ mouse_off ();
+
+#if __DJGPP__ >= 2
+
+ return (setmode (fileno (stdin), stdin_stat) != -1);
+
+#else /* not __DJGPP__ >= 2 */
+
+ inregs.x.ax = 0x4401; /* Set IOCTL status. */
+ inregs.x.bx = 0x00; /* 0 = stdin. */
+ inregs.x.dx = stdin_stat;
+ intdos (&inregs, &outregs);
+ return !outregs.x.cflag;
+
+#endif /* not __DJGPP__ >= 2 */
+}
+
+\f
+/* Run command as specified by ARGV in directory DIR.
+ The command is run with input from TEMPIN, output to
+ file TEMPOUT and stderr to TEMPERR. */
+
+int
+run_msdos_command (argv, working_dir, tempin, tempout, temperr, envv)
+ unsigned char **argv;
+ const char *working_dir;
+ int tempin, tempout, temperr;
+ char **envv;
+{
+ char *saveargv1, *saveargv2, *lowcase_argv0, *pa, *pl;
+ char oldwd[MAXPATHLEN + 1]; /* Fixed size is safe on MSDOS. */
+ int msshell, result = -1;
+ int inbak, outbak, errbak;
+ int x, y;
+ Lisp_Object cmd;
+
+ /* Get current directory as MSDOS cwd is not per-process. */
+ getwd (oldwd);
+
+ /* If argv[0] is the shell, it might come in any lettercase.
+ Since `Fmember' is case-sensitive, we need to downcase
+ argv[0], even if we are on case-preserving filesystems. */
+ lowcase_argv0 = alloca (strlen (argv[0]) + 1);
+ for (pa = argv[0], pl = lowcase_argv0; *pa; pl++)
+ {
+ *pl = *pa++;
+ if (*pl >= 'A' && *pl <= 'Z')
+ *pl += 'a' - 'A';
+ }
+ *pl = '\0';
+
+ cmd = Ffile_name_nondirectory (build_string (lowcase_argv0));
+ msshell = !NILP (Fmember (cmd, Fsymbol_value (intern ("msdos-shells"))))
+ && !strcmp ("-c", argv[1]);
+ if (msshell)
+ {
+ saveargv1 = argv[1];
+ saveargv2 = argv[2];
+ argv[1] = "/c";
+ if (argv[2])
+ {
+ char *p = alloca (strlen (argv[2]) + 1);
+
+ strcpy (argv[2] = p, saveargv2);
+ while (*p && isspace (*p))
+ p++;
+ while (*p && !isspace (*p))
+ if (*p == '/')
+ *p++ = '\\';
+ else
+ p++;
+ }
+ }
+
+ chdir (working_dir);
+ inbak = dup (0);
+ outbak = dup (1);
+ errbak = dup (2);
+ if (inbak < 0 || outbak < 0 || errbak < 0)
+ goto done; /* Allocation might fail due to lack of descriptors. */
+
+ if (have_mouse > 0)
+ mouse_get_xy (&x, &y);
+
+ dos_ttcooked (); /* do it here while 0 = stdin */
+
+ dup2 (tempin, 0);
+ dup2 (tempout, 1);
+ dup2 (temperr, 2);
+
+#if __DJGPP__ > 1
+
+ if (msshell && !argv[3])
+ {
+ /* MS-DOS native shells are too restrictive. For starters, they
+ cannot grok commands longer than 126 characters. In DJGPP v2
+ and later, `system' is much smarter, so we'll call it instead. */
+
+ const char *cmnd;
+
+ /* A shell gets a single argument--its full command
+ line--whose original was saved in `saveargv2'. */
+
+ /* Don't let them pass empty command lines to `system', since
+ with some shells it will try to invoke an interactive shell,
+ which will hang Emacs. */
+ for (cmnd = saveargv2; *cmnd && isspace (*cmnd); cmnd++)
+ ;
+ if (*cmnd)
+ {
+ extern char **environ;
+ int save_system_flags = __system_flags;
+
+ /* Request the most powerful version of `system'. We need
+ all the help we can get to avoid calling stock DOS shells. */
+ __system_flags = (__system_redirect
+ | __system_use_shell
+ | __system_allow_multiple_cmds
+ | __system_allow_long_cmds
+ | __system_handle_null_commands
+ | __system_emulate_chdir);
+
+ environ = envv;
+ result = system (cmnd);
+ __system_flags = save_system_flags;
+ }
+ else
+ result = 0; /* emulate Unixy shell behavior with empty cmd line */
+ }
+ else
+
+#endif /* __DJGPP__ > 1 */
+
+ result = spawnve (P_WAIT, argv[0], argv, envv);
+
+ dup2 (inbak, 0);
+ dup2 (outbak, 1);
+ dup2 (errbak, 2);
+ close (inbak);
+ close (outbak);
+ close (errbak);
+
+ dos_ttraw ();
+ if (have_mouse > 0)
+ {
+ mouse_init ();
+ mouse_moveto (x, y);
+ }
+
+ /* Some programs might change the meaning of the highest bit of the
+ text attribute byte, so we get blinking characters instead of the
+ bright background colors. Restore that. */
+ bright_bg ();
+
+ done:
+ chdir (oldwd);
+ if (msshell)
+ {
+ argv[1] = saveargv1;
+ argv[2] = saveargv2;
+ }
+ return result;
+}
+
+croak (badfunc)
+ char *badfunc;
+{
+ fprintf (stderr, "%s not yet implemented\r\n", badfunc);
+ reset_sys_modes ();
+ exit (1);
+}
+\f
+#if __DJGPP__ < 2
+
+/* ------------------------- Compatibility functions -------------------
+ * gethostname
+ * gettimeofday
+ */
+
+/* Hostnames for a pc are not really funny,
+ but they are used in change log so we emulate the best we can. */
+
+gethostname (p, size)
+ char *p;
+ int size;
+{
+ char *q = egetenv ("HOSTNAME");
+
+ if (!q) q = "pc";
+ strcpy (p, q);
+ return 0;
+}
+
+/* When time zones are set from Ms-Dos too many C-libraries are playing
+ tricks with time values. We solve this by defining our own version
+ of `gettimeofday' bypassing GO32. Our version needs to be initialized
+ once and after each call to `tzset' with TZ changed. That is
+ accomplished by aliasing tzset to init_gettimeofday. */
+
+static struct tm time_rec;
+
+int
+gettimeofday (struct timeval *tp, struct timezone *tzp)
+{
+ if (tp)
+ {
+ struct time t;
+ struct tm tm;
+
+ gettime (&t);
+ if (t.ti_hour < time_rec.tm_hour) /* midnight wrap */
+ {
+ struct date d;
+ getdate (&d);
+ time_rec.tm_year = d.da_year - 1900;
+ time_rec.tm_mon = d.da_mon - 1;
+ time_rec.tm_mday = d.da_day;
+ }
+
+ time_rec.tm_hour = t.ti_hour;
+ time_rec.tm_min = t.ti_min;
+ time_rec.tm_sec = t.ti_sec;
+
+ tm = time_rec;
+ tm.tm_gmtoff = dos_timezone_offset;
+
+ tp->tv_sec = mktime (&tm); /* may modify tm */
+ tp->tv_usec = t.ti_hund * (1000000 / 100);
+ }
+ /* Ignore tzp; it's obsolescent. */
+ return 0;
+}
+
+#endif /* __DJGPP__ < 2 */
+
+/*
+ * A list of unimplemented functions that we silently ignore.
+ */
+
+#if __DJGPP__ < 2
+unsigned alarm (s) unsigned s; {}
+fork () { return 0; }
+int kill (x, y) int x, y; { return -1; }
+nice (p) int p; {}
+void volatile pause () {}
+sigsetmask (x) int x; { return 0; }
+sigblock (mask) int mask; { return 0; }
+#endif
+
+void request_sigio (void) {}
+setpgrp () {return 0; }
+setpriority (x,y,z) int x,y,z; { return 0; }
+void unrequest_sigio (void) {}
+
+#if __DJGPP__ > 1
+
+#ifdef POSIX_SIGNALS
+
+/* Augment DJGPP library POSIX signal functions. This is needed
+ as of DJGPP v2.01, but might be in the library in later releases. */
+
+#include <libc/bss.h>
+
+/* A counter to know when to re-initialize the static sets. */
+static int sigprocmask_count = -1;
+
+/* Which signals are currently blocked (initially none). */
+static sigset_t current_mask;
+
+/* Which signals are pending (initially none). */
+static sigset_t pending_signals;
+
+/* Previous handlers to restore when the blocked signals are unblocked. */
+typedef void (*sighandler_t)(int);
+static sighandler_t prev_handlers[320];
+
+/* A signal handler which just records that a signal occured
+ (it will be raised later, if and when the signal is unblocked). */
+static void
+sig_suspender (signo)
+ int signo;
+{
+ sigaddset (&pending_signals, signo);
+}
+
+int
+sigprocmask (how, new_set, old_set)
+ int how;
+ const sigset_t *new_set;
+ sigset_t *old_set;
+{
+ int signo;
+ sigset_t new_mask;
+
+ /* If called for the first time, initialize. */
+ if (sigprocmask_count != __bss_count)
+ {
+ sigprocmask_count = __bss_count;
+ sigemptyset (&pending_signals);
+ sigemptyset (¤t_mask);
+ for (signo = 0; signo < 320; signo++)
+ prev_handlers[signo] = SIG_ERR;
+ }
+
+ if (old_set)
+ *old_set = current_mask;
+
+ if (new_set == 0)
+ return 0;
+
+ if (how != SIG_BLOCK && how != SIG_UNBLOCK && how != SIG_SETMASK)
+ {
+ errno = EINVAL;
+ return -1;
+ }
+
+ sigemptyset (&new_mask);
+
+ /* DJGPP supports upto 320 signals. */
+ for (signo = 0; signo < 320; signo++)
+ {
+ if (sigismember (¤t_mask, signo))
+ sigaddset (&new_mask, signo);
+ else if (sigismember (new_set, signo) && how != SIG_UNBLOCK)
+ {
+ sigaddset (&new_mask, signo);
+
+ /* SIGKILL is silently ignored, as on other platforms. */
+ if (signo != SIGKILL && prev_handlers[signo] == SIG_ERR)
+ prev_handlers[signo] = signal (signo, sig_suspender);
+ }
+ if (( how == SIG_UNBLOCK
+ && sigismember (&new_mask, signo)
+ && sigismember (new_set, signo))
+ || (how == SIG_SETMASK
+ && sigismember (&new_mask, signo)
+ && !sigismember (new_set, signo)))
+ {
+ sigdelset (&new_mask, signo);
+ if (prev_handlers[signo] != SIG_ERR)
+ {
+ signal (signo, prev_handlers[signo]);
+ prev_handlers[signo] = SIG_ERR;
+ }
+ if (sigismember (&pending_signals, signo))
+ {
+ sigdelset (&pending_signals, signo);
+ raise (signo);
+ }
+ }
+ }
+ current_mask = new_mask;
+ return 0;
+}
+
+#else /* not POSIX_SIGNALS */
+
+sigsetmask (x) int x; { return 0; }
+sigblock (mask) int mask; { return 0; }
+
+#endif /* not POSIX_SIGNALS */
+#endif /* __DJGPP__ > 1 */
+
+#ifndef HAVE_SELECT
+#include "sysselect.h"
+
+#ifndef EMACS_TIME_ZERO_OR_NEG_P
+#define EMACS_TIME_ZERO_OR_NEG_P(time) \
+ ((long)(time).tv_sec < 0 \
+ || ((time).tv_sec == 0 \
+ && (long)(time).tv_usec <= 0))
+#endif
+
+/* This yields the rest of the current time slice to the task manager.
+ It should be called by any code which knows that it has nothing
+ useful to do except idle.
+
+ I don't use __dpmi_yield here, since versions of library before 2.02
+ called Int 2Fh/AX=1680h there in a way that would wedge the DOS box
+ on some versions of Windows 9X. */
+
+void
+dos_yield_time_slice (void)
+{
+ _go32_dpmi_registers r;
+
+ r.x.ax = 0x1680;
+ r.x.ss = r.x.sp = r.x.flags = 0;
+ _go32_dpmi_simulate_int (0x2f, &r);
+ if (r.h.al == 0x80)
+ errno = ENOSYS;
+}
+
+/* Only event queue is checked. */
+/* We don't have to call timer_check here
+ because wait_reading_process_input takes care of that. */
+int
+sys_select (nfds, rfds, wfds, efds, timeout)
+ int nfds;
+ SELECT_TYPE *rfds, *wfds, *efds;
+ EMACS_TIME *timeout;
+{
+ int check_input;
+ struct time t;
+
+ check_input = 0;
+ if (rfds)
+ {
+ check_input = FD_ISSET (0, rfds);
+ FD_ZERO (rfds);
+ }
+ if (wfds)
+ FD_ZERO (wfds);
+ if (efds)
+ FD_ZERO (efds);
+
+ if (nfds != 1)
+ abort ();
+
+ /* If we are looking only for the terminal, with no timeout,
+ just read it and wait -- that's more efficient. */
+ if (!timeout)
+ {
+ while (!detect_input_pending ())
+ {
+ dos_yield_time_slice ();
+ }
+ }
+ else
+ {
+ EMACS_TIME clnow, cllast, cldiff;
+
+ gettime (&t);
+ EMACS_SET_SECS_USECS (cllast, t.ti_sec, t.ti_hund * 10000L);
+
+ while (!check_input || !detect_input_pending ())
+ {
+ gettime (&t);
+ EMACS_SET_SECS_USECS (clnow, t.ti_sec, t.ti_hund * 10000L);
+ EMACS_SUB_TIME (cldiff, clnow, cllast);
+
+ /* When seconds wrap around, we assume that no more than
+ 1 minute passed since last `gettime'. */
+ if (EMACS_TIME_NEG_P (cldiff))
+ EMACS_SET_SECS (cldiff, EMACS_SECS (cldiff) + 60);
+ EMACS_SUB_TIME (*timeout, *timeout, cldiff);
+
+ /* Stop when timeout value crosses zero. */
+ if (EMACS_TIME_ZERO_OR_NEG_P (*timeout))
+ return 0;
+ cllast = clnow;
+ dos_yield_time_slice ();
+ }
+ }
+
+ FD_SET (0, rfds);
+ return 1;
+}
+#endif
+
+/*
+ * Define overlaid functions:
+ *
+ * chdir -> sys_chdir
+ * tzset -> init_gettimeofday
+ * abort -> dos_abort
+ */
+
+#ifdef chdir
+#undef chdir
+extern int chdir ();
+
+int
+sys_chdir (path)
+ const char* path;
+{
+ int len = strlen (path);
+ char *tmp = (char *)path;
+
+ if (*tmp && tmp[1] == ':')
+ {
+ if (getdisk () != tolower (tmp[0]) - 'a')
+ setdisk (tolower (tmp[0]) - 'a');
+ tmp += 2; /* strip drive: KFS 1995-07-06 */
+ len -= 2;
+ }
+
+ if (len > 1 && (tmp[len - 1] == '/'))
+ {
+ char *tmp1 = (char *) alloca (len + 1);
+ strcpy (tmp1, tmp);
+ tmp1[len - 1] = 0;
+ tmp = tmp1;
+ }
+ return chdir (tmp);
+}
+#endif
+
+#ifdef tzset
+#undef tzset
+extern void tzset (void);
+
+void
+init_gettimeofday ()
+{
+ time_t ltm, gtm;
+ struct tm *lstm;
+
+ tzset ();
+ ltm = gtm = time (NULL);
+ ltm = mktime (lstm = localtime (<m));
+ gtm = mktime (gmtime (>m));
+ time_rec.tm_hour = 99; /* force gettimeofday to get date */
+ time_rec.tm_isdst = lstm->tm_isdst;
+ dos_timezone_offset = time_rec.tm_gmtoff = (int)(gtm - ltm) / 60;
+}
+#endif
+
+#ifdef abort
+#undef abort
+void
+dos_abort (file, line)
+ char *file;
+ int line;
+{
+ char buffer1[200], buffer2[400];
+ int i, j;
+
+ sprintf (buffer1, "<EMACS FATAL ERROR IN %s LINE %d>", file, line);
+ for (i = j = 0; buffer1[i]; i++) {
+ buffer2[j++] = buffer1[i];
+ buffer2[j++] = 0x70;
+ }
+ dosmemput (buffer2, j, (int)ScreenPrimary);
+ ScreenSetCursor (2, 0);
+ abort ();
+}
+#else
+void
+abort ()
+{
+ dos_ttcooked ();
+ ScreenSetCursor (10, 0);
+ cputs ("\r\n\nEmacs aborted!\r\n");
+#if __DJGPP__ > 1
+#if __DJGPP__ == 2 && __DJGPP_MINOR__ < 2
+ if (screen_virtual_segment)
+ dosv_refresh_virtual_screen (2 * 10 * screen_size_X, 4 * screen_size_X);
+#endif /* __DJGPP_MINOR__ < 2 */
+ /* Generate traceback, so we could tell whodunit. */
+ signal (SIGINT, SIG_DFL);
+ __asm__ __volatile__ ("movb $0x1b,%al;call ___djgpp_hw_exception");
+#endif
+ exit (2);
+}
+#endif
+
+/* The following two are required so that customization feature
+ won't complain about unbound variables. */
+#ifndef HAVE_X_WINDOWS
+/* Search path for bitmap files (xfns.c). */
+Lisp_Object Vx_bitmap_file_path;
+#endif
+#ifndef subprocesses
+/* Nonzero means delete a process right away if it exits (process.c). */
+static int delete_exited_processes;
+#endif
+
+syms_of_msdos ()
+{
+ recent_doskeys = Fmake_vector (make_number (NUM_RECENT_DOSKEYS), Qnil);
+ staticpro (&recent_doskeys);
+#ifndef HAVE_X_WINDOWS
+ DEFVAR_LISP ("x-bitmap-file-path", &Vx_bitmap_file_path,
+ "List of directories to search for bitmap files for X.");
+ Vx_bitmap_file_path = decode_env_path ((char *) 0, ".");
+
+ /* The following two are from xfns.c: */
+ Qbackground_color = intern ("background-color");
+ staticpro (&Qbackground_color);
+ Qforeground_color = intern ("foreground-color");
+ staticpro (&Qforeground_color);
+#endif
+#ifndef subprocesses
+ DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes,
+ "*Non-nil means delete processes immediately when they exit.\n\
+nil means don't delete them until `list-processes' is run.");
+ delete_exited_processes = 0;
+#endif
+
+ defsubr (&Srecent_doskeys);
+ defsubr (&Smsdos_long_file_names);
+ defsubr (&Smsdos_downcase_filename);
+}
+
+#endif /* MSDOS */
+