*** empty log message ***
[emacs.git] / src / dosfns.c
blobdd958041662bcd21df1e3a46a58f23f02c141cfd
1 /* MS-DOS specific Lisp utilities. Coded by Manabu Higashida, 1991.
2 Major changes May-July 1993 Morten Welinder (only 10% original code left)
3 Copyright (C) 1991, 1993, 1996, 1997 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include <config.h>
25 #ifdef MSDOS
26 /* The entire file is within this conditional */
28 #include <stdio.h>
29 #include <string.h>
30 #include <dos.h>
31 #include "lisp.h"
32 #include "buffer.h"
33 #include "termchar.h"
34 #include "termhooks.h"
35 #include "frame.h"
36 #include "blockinput.h"
37 #include "window.h"
38 #include "dosfns.h"
39 #include "msdos.h"
40 #include "dispextern.h"
41 #include "charset.h"
42 #include "coding.h"
43 #include <dpmi.h>
44 #include <go32.h>
45 #include <dirent.h>
46 #include <sys/vfs.h>
48 #ifndef __DJGPP_MINOR__
49 # define __tb _go32_info_block.linear_address_of_transfer_buffer;
50 #endif
52 DEFUN ("int86", Fint86, Sint86, 2, 2, 0,
53 "Call specific MSDOS interrupt number INTERRUPT with REGISTERS.\n\
54 Return the updated REGISTER vector.\n\
55 \n\
56 INTERRUPT should be an integer in the range 0 to 255.\n\
57 REGISTERS should be a vector produced by `make-register' and\n\
58 `set-register-value'.")
59 (interrupt, registers)
60 Lisp_Object interrupt, registers;
62 register int i;
63 int no;
64 union REGS inregs, outregs;
65 Lisp_Object val;
67 CHECK_NUMBER (interrupt, 0);
68 no = (unsigned long) XINT (interrupt);
69 CHECK_VECTOR (registers, 1);
70 if (no < 0 || no > 0xff || XVECTOR (registers)-> size != 8)
71 return Qnil;
72 for (i = 0; i < 8; i++)
73 CHECK_NUMBER (XVECTOR (registers)->contents[i], 1);
75 inregs.x.ax = (unsigned long) XFASTINT (XVECTOR (registers)->contents[0]);
76 inregs.x.bx = (unsigned long) XFASTINT (XVECTOR (registers)->contents[1]);
77 inregs.x.cx = (unsigned long) XFASTINT (XVECTOR (registers)->contents[2]);
78 inregs.x.dx = (unsigned long) XFASTINT (XVECTOR (registers)->contents[3]);
79 inregs.x.si = (unsigned long) XFASTINT (XVECTOR (registers)->contents[4]);
80 inregs.x.di = (unsigned long) XFASTINT (XVECTOR (registers)->contents[5]);
81 inregs.x.cflag = (unsigned long) XFASTINT (XVECTOR (registers)->contents[6]);
82 inregs.x.flags = (unsigned long) XFASTINT (XVECTOR (registers)->contents[7]);
84 int86 (no, &inregs, &outregs);
86 XVECTOR (registers)->contents[0] = make_number (outregs.x.ax);
87 XVECTOR (registers)->contents[1] = make_number (outregs.x.bx);
88 XVECTOR (registers)->contents[2] = make_number (outregs.x.cx);
89 XVECTOR (registers)->contents[3] = make_number (outregs.x.dx);
90 XVECTOR (registers)->contents[4] = make_number (outregs.x.si);
91 XVECTOR (registers)->contents[5] = make_number (outregs.x.di);
92 XVECTOR (registers)->contents[6] = make_number (outregs.x.cflag);
93 XVECTOR (registers)->contents[7] = make_number (outregs.x.flags);
95 return registers;
98 DEFUN ("msdos-memget", Fdos_memget, Sdos_memget, 2, 2, 0,
99 "Read DOS memory at offset ADDRESS into VECTOR.\n\
100 Return the updated VECTOR.")
101 (address, vector)
102 Lisp_Object address, vector;
104 register int i;
105 int offs, len;
106 char *buf;
107 Lisp_Object val;
109 CHECK_NUMBER (address, 0);
110 offs = (unsigned long) XINT (address);
111 CHECK_VECTOR (vector, 1);
112 len = XVECTOR (vector)-> size;
113 if (len < 1 || len > 2048 || address < 0 || address > 0xfffff - len)
114 return Qnil;
115 buf = alloca (len);
116 dosmemget (offs, len, buf);
118 for (i = 0; i < len; i++)
119 XVECTOR (vector)->contents[i] = make_number (buf[i]);
121 return vector;
124 DEFUN ("msdos-memput", Fdos_memput, Sdos_memput, 2, 2, 0,
125 "Write DOS memory at offset ADDRESS from VECTOR.")
126 (address, vector)
127 Lisp_Object address, vector;
129 register int i;
130 int offs, len;
131 char *buf;
132 Lisp_Object val;
134 CHECK_NUMBER (address, 0);
135 offs = (unsigned long) XINT (address);
136 CHECK_VECTOR (vector, 1);
137 len = XVECTOR (vector)-> size;
138 if (len < 1 || len > 2048 || address < 0 || address > 0xfffff - len)
139 return Qnil;
140 buf = alloca (len);
142 for (i = 0; i < len; i++)
144 CHECK_NUMBER (XVECTOR (vector)->contents[i], 1);
145 buf[i] = (unsigned char) XFASTINT (XVECTOR (vector)->contents[i]) & 0xFF;
148 dosmemput (buf, len, offs);
149 return Qt;
152 DEFUN ("msdos-set-keyboard", Fmsdos_set_keyboard, Smsdos_set_keyboard, 1, 2, 0,
153 "Set keyboard layout according to COUNTRY-CODE.\n\
154 If the optional argument ALLKEYS is non-nil, the keyboard is mapped for\n\
155 all keys; otherwise it is only used when the ALT key is pressed.\n\
156 The current keyboard layout is available in dos-keyboard-code.")
157 (country_code, allkeys)
158 Lisp_Object country_code;
160 CHECK_NUMBER (country_code, 0);
161 if (!dos_set_keyboard (XINT (country_code), !NILP (allkeys)))
162 return Qnil;
163 return Qt;
166 #ifndef HAVE_X_WINDOWS
167 /* Later we might want to control the mouse interface with this function,
168 e.g., with respect to non-80 column screen modes. */
170 DEFUN ("msdos-mouse-p", Fmsdos_mouse_p, Smsdos_mouse_p, 0, 0, 0, "\
171 Report whether a mouse is present.")
174 if (have_mouse)
175 return Qt;
176 else
177 return Qnil;
179 #endif
182 DEFUN ("msdos-mouse-init", Fmsdos_mouse_init, Smsdos_mouse_init, 0, 0, "",
183 "Initialize and enable mouse if available.")
186 if (have_mouse)
188 have_mouse = 1;
189 mouse_init ();
190 return Qt;
192 return Qnil;
195 DEFUN ("msdos-mouse-enable", Fmsdos_mouse_enable, Smsdos_mouse_enable, 0, 0, "",
196 "Enable mouse if available.")
199 if (have_mouse)
201 have_mouse = 1;
202 mouse_on ();
204 return have_mouse ? Qt : Qnil;
207 DEFUN ("msdos-mouse-disable", Fmsdos_mouse_disable, Smsdos_mouse_disable, 0, 0, "",
208 "Disable mouse if available.")
211 mouse_off ();
212 if (have_mouse) have_mouse = -1;
213 return Qnil;
216 DEFUN ("insert-startup-screen", Finsert_startup_screen, Sinsert_startup_screen, 0, 0, "", "\
217 Insert copy of screen contents prior to starting emacs.\n\
218 Return nil if startup screen is not available.")
221 char *s;
222 int rows, cols;
223 int i, j;
225 if (!dos_get_saved_screen (&s, &rows, &cols))
226 return Qnil;
228 for (i = 0; i < rows; i++)
230 for (j = 0; j < cols; j++)
232 insert_char (*s);
233 s += 2;
235 insert_char ('\n');
238 return Qt;
241 /* country info */
242 int dos_country_code;
243 int dos_codepage;
244 int dos_timezone_offset;
245 int dos_decimal_point;
246 int dos_keyboard_layout;
247 unsigned char dos_country_info[DOS_COUNTRY_INFO];
248 static unsigned char usa_country_info[DOS_COUNTRY_INFO] = {
249 0, 0, /* date format */
250 '$', 0, 0, 0, 0, /* currency string */
251 ',', 0, /* thousands separator */
252 '.', 0, /* decimal separator */
253 '/', 0, /* date separator */
254 ':', 0, /* time separator */
255 0, /* currency format */
256 2, /* digits after decimal in currency */
257 0, /* time format */
258 0, 0, 0, 0, /* address of case map routine, GPF if used */
259 ' ', 0, /* data-list separator (?) */
260 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* reserved */
263 int dos_hyper_key;
264 int dos_super_key;
265 int dos_keypad_mode;
267 Lisp_Object Vdos_version;
268 Lisp_Object Vdos_display_scancodes;
270 #ifndef HAVE_X_WINDOWS
271 static unsigned dos_windows_version;
272 Lisp_Object Vdos_windows_version;
274 char parent_vm_title[50]; /* Ralf Brown says 30 is enough */
275 int w95_set_virtual_machine_title (const char *);
277 void
278 restore_parent_vm_title (void)
280 if (NILP (Vdos_windows_version))
281 return;
282 if ((dos_windows_version & 0xff) >= 4 && parent_vm_title[0])
283 w95_set_virtual_machine_title (parent_vm_title);
284 delay (50);
286 #endif /* !HAVE_X_WINDOWS */
288 void
289 init_dosfns ()
291 union REGS regs;
292 _go32_dpmi_registers dpmiregs;
293 unsigned long xbuf = _go32_info_block.linear_address_of_transfer_buffer;
295 #ifndef SYSTEM_MALLOC
296 get_lim_data (); /* why the hell isn't this called elsewhere? */
297 #endif
299 regs.x.ax = 0x3000;
300 intdos (&regs, &regs);
301 Vdos_version = Fcons (make_number (regs.h.al), make_number (regs.h.ah));
303 /* Obtain the country code via DPMI, use DJGPP transfer buffer. */
304 dpmiregs.x.ax = 0x3800;
305 dpmiregs.x.ds = xbuf >> 4;
306 dpmiregs.x.dx = 0;
307 dpmiregs.x.ss = dpmiregs.x.sp = dpmiregs.x.flags = 0;
308 _go32_dpmi_simulate_int (0x21, &dpmiregs);
309 if (dpmiregs.x.flags & 1)
311 dos_country_code = 1; /* assume USA if 213800 failed */
312 memcpy (dos_country_info, usa_country_info, DOS_COUNTRY_INFO);
314 else
316 dos_country_code = dpmiregs.x.bx;
317 dosmemget (xbuf, DOS_COUNTRY_INFO, dos_country_info);
320 dos_set_keyboard (dos_country_code, 0);
322 regs.x.ax = 0x6601;
323 intdos (&regs, &regs);
324 if (regs.x.cflag)
325 /* Estimate code page from country code */
326 switch (dos_country_code)
328 case 45: /* Denmark */
329 case 47: /* Norway */
330 dos_codepage = 865;
331 break;
332 default:
333 /* US */
334 dos_codepage = 437;
336 else
337 dos_codepage = regs.x.bx & 0xffff;
339 #ifndef HAVE_X_WINDOWS
340 parent_vm_title[0] = '\0';
342 /* If we are running from DOS box on MS-Windows, get Windows version. */
343 dpmiregs.x.ax = 0x1600; /* enhanced mode installation check */
344 dpmiregs.x.ss = dpmiregs.x.sp = dpmiregs.x.flags = 0;
345 _go32_dpmi_simulate_int (0x2f, &dpmiregs);
346 /* We only support Windows-specific features when we run on Windows 9X
347 or on Windows 3.X/enhanced mode.
349 Int 2Fh/AX=1600h returns:
351 AL = 00: no Windows at all;
352 AL = 01: Windows/386 2.x;
353 AL = 80h: Windows 3.x in mode other than enhanced;
354 AL = FFh: Windows/386 2.x
356 We also check AH > 0 (Windows 3.1 or later), in case AL tricks us. */
357 if (dpmiregs.h.al > 2 && dpmiregs.h.al != 0x80 && dpmiregs.h.al != 0xff
358 && (dpmiregs.h.al > 3 || dpmiregs.h.ah > 0))
360 dos_windows_version = dpmiregs.x.ax;
361 Vdos_windows_version =
362 Fcons (make_number (dpmiregs.h.al), make_number (dpmiregs.h.ah));
364 /* Save the current title of this virtual machine, so we can restore
365 it before exiting. Otherwise, Windows 95 will continue to use
366 the title we set even after we are history, stupido... */
367 if (dpmiregs.h.al >= 4)
369 dpmiregs.x.ax = 0x168e;
370 dpmiregs.x.dx = 3; /* get VM title */
371 dpmiregs.x.cx = sizeof(parent_vm_title) - 1;
372 dpmiregs.x.es = __tb >> 4;
373 dpmiregs.x.di = __tb & 15;
374 dpmiregs.x.sp = dpmiregs.x.ss = dpmiregs.x.flags = 0;
375 _go32_dpmi_simulate_int (0x2f, &dpmiregs);
376 if (dpmiregs.x.ax == 1)
377 dosmemget (__tb, sizeof(parent_vm_title), parent_vm_title);
380 else
382 dos_windows_version = 0;
383 Vdos_windows_version = Qnil;
385 #endif /* !HAVE_X_WINDOWS */
387 #if __DJGPP__ >= 2
389 /* Without this, we never see hidden files.
390 Don't OR it with the previous value, so the value recorded at dump
391 time, possibly with `preserve-case' flags set, won't get through. */
392 __opendir_flags = __OPENDIR_FIND_HIDDEN;
394 #if __DJGPP_MINOR__ == 0
395 /* Under LFN, preserve the case of files as recorded in the directory
396 (in DJGPP 2.01 and later this is automagically done by the library). */
397 if (!NILP (Fmsdos_long_file_names ()))
398 __opendir_flags |= __OPENDIR_PRESERVE_CASE;
399 #endif /* __DJGPP_MINOR__ == 0 */
400 #endif /* __DJGPP__ >= 2 */
403 #ifndef HAVE_X_WINDOWS
405 /* Emulation of some X window features from xfns.c and xfaces.c. */
407 /* Standard VGA colors, in the order of their standard numbering
408 in the default VGA palette. */
409 static char *vga_colors[16] = {
410 "black", "blue", "green", "cyan", "red", "magenta", "brown",
411 "lightgray", "darkgray", "lightblue", "lightgreen", "lightcyan",
412 "lightred", "lightmagenta", "yellow", "white"
415 /* Given a color name, return its index, or -1 if not found. Note
416 that this only performs case-insensitive comparison against the
417 standard names. For anything more sophisticated, like matching
418 "gray" with "grey" or translating X color names into their MSDOS
419 equivalents, call the Lisp function Qtty_color_desc (defined
420 on lisp/term/tty-colors.el). */
422 msdos_stdcolor_idx (const char *name)
424 int i;
426 for (i = 0; i < sizeof (vga_colors) / sizeof (vga_colors[0]); i++)
427 if (strcasecmp (name, vga_colors[i]) == 0)
428 return i;
430 return
431 strcmp (name, unspecified_fg) == 0 ? FACE_TTY_DEFAULT_FG_COLOR
432 : strcmp (name, unspecified_bg) == 0 ? FACE_TTY_DEFAULT_BG_COLOR
433 : FACE_TTY_DEFAULT_COLOR;
436 /* Given a color index, return its standard name. */
437 Lisp_Object
438 msdos_stdcolor_name (int idx)
440 extern Lisp_Object Qunspecified;
442 if (idx == FACE_TTY_DEFAULT_FG_COLOR)
443 return build_string (unspecified_fg);
444 else if (idx == FACE_TTY_DEFAULT_BG_COLOR)
445 return build_string (unspecified_bg);
446 else if (idx >= 0 && idx < sizeof (vga_colors) / sizeof (vga_colors[0]))
447 return build_string (vga_colors[idx]);
448 else
449 return Qunspecified; /* meaning the default */
452 /* Support for features that are available when we run in a DOS box
453 on MS-Windows. */
455 ms_windows_version (void)
457 return dos_windows_version;
460 /* Set the title of the current virtual machine, to appear on
461 the caption bar of that machine's window. */
464 w95_set_virtual_machine_title (const char *title_string)
466 /* Only Windows 9X (version 4 and higher) support this function. */
467 if (!NILP (Vdos_windows_version)
468 && (dos_windows_version & 0xff) >= 4)
470 _go32_dpmi_registers regs;
471 dosmemput (title_string, strlen (title_string) + 1, __tb);
472 regs.x.ax = 0x168e;
473 regs.x.dx = 1;
474 regs.x.es = __tb >> 4;
475 regs.x.di = __tb & 15;
476 regs.x.sp = regs.x.ss = regs.x.flags = 0;
477 _go32_dpmi_simulate_int (0x2f, &regs);
478 return regs.x.ax == 1;
480 return 0;
483 /* Change the title of frame F to NAME.
484 If NAME is nil, use the frame name as the title.
486 If Emacs is not run from a DOS box on Windows 9X, this only
487 sets the name in the frame struct, but has no other effects. */
489 void
490 x_set_title (f, name)
491 struct frame *f;
492 Lisp_Object name;
494 /* Don't change the title if it's already NAME. */
495 if (EQ (name, f->title))
496 return;
498 update_mode_lines = 1;
500 f->title = name;
502 if (NILP (name))
503 name = f->name;
505 if (FRAME_MSDOS_P (f))
507 BLOCK_INPUT;
508 w95_set_virtual_machine_title (XSTRING (name)->data);
509 UNBLOCK_INPUT;
512 #endif /* !HAVE_X_WINDOWS */
514 DEFUN ("file-system-info", Ffile_system_info, Sfile_system_info, 1, 1, 0,
515 "Return storage information about the file system FILENAME is on.\n\
516 Value is a list of floats (TOTAL FREE AVAIL), where TOTAL is the total\n\
517 storage of the file system, FREE is the free storage, and AVAIL is the\n\
518 storage available to a non-superuser. All 3 numbers are in bytes.\n\
519 If the underlying system call fails, value is nil.")
520 (filename)
521 Lisp_Object filename;
523 struct statfs stfs;
524 Lisp_Object encoded, value;
526 CHECK_STRING (filename, 0);
527 filename = Fexpand_file_name (filename, Qnil);
528 encoded = ENCODE_FILE (filename);
530 if (statfs (XSTRING (encoded)->data, &stfs))
531 value = Qnil;
532 else
533 value = list3 (make_float ((double) stfs.f_bsize * stfs.f_blocks),
534 make_float ((double) stfs.f_bsize * stfs.f_bfree),
535 make_float ((double) stfs.f_bsize * stfs.f_bavail));
537 return value;
540 void
541 dos_cleanup (void)
543 #ifndef HAVE_X_WINDOWS
544 restore_parent_vm_title ();
545 #endif
546 /* Make sure the termscript file is committed, in case we are
547 crashing and some vital info was written there. */
548 if (termscript)
550 fflush (termscript);
551 fsync (fileno (termscript));
556 * Define everything
558 syms_of_dosfns ()
560 defsubr (&Sint86);
561 defsubr (&Sdos_memget);
562 defsubr (&Sdos_memput);
563 defsubr (&Smsdos_mouse_init);
564 defsubr (&Smsdos_mouse_enable);
565 defsubr (&Smsdos_set_keyboard);
566 defsubr (&Sinsert_startup_screen);
567 defsubr (&Smsdos_mouse_disable);
568 defsubr (&Sfile_system_info);
569 #ifndef HAVE_X_WINDOWS
570 defsubr (&Smsdos_mouse_p);
571 #endif
573 DEFVAR_INT ("dos-country-code", &dos_country_code,
574 "The country code returned by Dos when Emacs was started.\n\
575 Usually this is the international telephone prefix.");
577 DEFVAR_INT ("dos-codepage", &dos_codepage,
578 "The codepage active when Emacs was started.\n\
579 The following are known:\n\
580 437 United States\n\
581 850 Multilingual (Latin I)\n\
582 852 Slavic (Latin II)\n\
583 857 Turkish\n\
584 860 Portugal\n\
585 861 Iceland\n\
586 863 Canada (French)\n\
587 865 Norway/Denmark");
589 DEFVAR_INT ("dos-timezone-offset", &dos_timezone_offset,
590 "The current timezone offset to UTC in minutes.
591 Implicitly modified when the TZ variable is changed.");
593 DEFVAR_LISP ("dos-version", &Vdos_version,
594 "The (MAJOR . MINOR) Dos version (subject to modification with setver).");
596 #ifndef HAVE_X_WINDOWS
597 DEFVAR_LISP ("dos-windows-version", &Vdos_windows_version,
598 "The (MAJOR . MINOR) Windows version for DOS session on MS-Windows.");
599 #endif
601 DEFVAR_LISP ("dos-display-scancodes", &Vdos_display_scancodes,
602 "*When non-nil, the keyboard scan-codes are displayed at the bottom right\n\
603 corner of the display (typically at the end of the mode line).\n\
604 The output format is: scan code:char code*modifiers.");
605 Vdos_display_scancodes = Qnil;
607 DEFVAR_INT ("dos-hyper-key", &dos_hyper_key,
608 "*If set to 1, use right ALT key as hyper key.\n\
609 If set to 2, use right CTRL key as hyper key.");
610 dos_hyper_key = 0;
612 DEFVAR_INT ("dos-super-key", &dos_super_key,
613 "*If set to 1, use right ALT key as super key.\n\
614 If set to 2, use right CTRL key as super key.");
615 dos_super_key = 0;
617 DEFVAR_INT ("dos-keypad-mode", &dos_keypad_mode,
618 "*Controls what key code is returned by a key in the numeric keypad.\n\
619 The `numlock ON' action is only taken if no modifier keys are pressed.\n\
620 The value is an integer constructed by adding the following bits together:\n\
622 0x00 Digit key returns digit (if numlock ON)\n\
623 0x01 Digit key returns kp-digit (if numlock ON)\n\
624 0x02 Digit key returns M-digit (if numlock ON)\n\
625 0x03 Digit key returns edit key (if numlock ON)\n\
627 0x00 Grey key returns char (if numlock ON)\n\
628 0x04 Grey key returns kp-key (if numlock ON)\n\
630 0x00 Digit key returns digit (if numlock OFF)\n\
631 0x10 Digit key returns kp-digit (if numlock OFF)\n\
632 0x20 Digit key returns M-digit (if numlock OFF)\n\
633 0x30 Digit key returns edit key (if numlock OFF)\n\
635 0x00 Grey key returns char (if numlock OFF)\n\
636 0x40 Grey key returns kp-key (if numlock OFF)\n\
638 0x200 ALT-0..ALT-9 in top-row produces shifted codes.");
639 dos_keypad_mode = 0x75;
641 DEFVAR_INT ("dos-keyboard-layout", &dos_keyboard_layout,
642 "Contains the country code for the current keyboard layout.\n\
643 Use msdos-set-keyboard to select another keyboard layout.");
644 dos_keyboard_layout = 1; /* US */
646 DEFVAR_INT ("dos-decimal-point", &dos_decimal_point,
647 "If non-zero, it contains the character to be returned when the\n\
648 decimal point key in the numeric keypad is pressed when Num Lock is on.\n\
649 If zero, the decimal point key returns the country code specific value.");
650 dos_decimal_point = 0;
652 #endif /* MSDOS */