* alloc.c: Do not define struct catchtag.
[bpt/emacs.git] / src / dosfns.c
CommitLineData
1b94449f
RS
1/* MS-DOS specific Lisp utilities. Coded by Manabu Higashida, 1991.
2 Major changes May-July 1993 Morten Welinder (only 10% original code left)
0b5538bd 3 Copyright (C) 1991, 1993, 1996, 1997, 1998, 2001, 2002, 2003, 2004,
e5c9f94c 4 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
1b94449f
RS
5
6This file is part of GNU Emacs.
7
9ec0b715 8GNU Emacs is free software: you can redistribute it and/or modify
1b94449f 9it under the terms of the GNU General Public License as published by
9ec0b715
GM
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
1b94449f
RS
12
13GNU Emacs is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
9ec0b715 19along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
1b94449f 20
48984716 21#include <config.h>
1b94449f
RS
22
23#ifdef MSDOS
24/* The entire file is within this conditional */
25
26#include <stdio.h>
e4441df0 27#include <string.h>
1b94449f 28#include <dos.h>
d7306fe6 29#include <setjmp.h>
1b94449f
RS
30#include "lisp.h"
31#include "buffer.h"
32#include "termchar.h"
1b94449f 33#include "frame.h"
c06981c0 34#include "termhooks.h"
838a94b8
EZ
35#include "blockinput.h"
36#include "window.h"
1b94449f
RS
37#include "dosfns.h"
38#include "msdos.h"
e4441df0 39#include "dispextern.h"
83be827a 40#include "character.h"
76a76a57 41#include "coding.h"
e5c9f94c 42#include "process.h"
838a94b8 43#include <dpmi.h>
5f08dc78 44#include <go32.h>
50666766 45#include <dirent.h>
76a76a57 46#include <sys/vfs.h>
e5c9f94c
EZ
47#include <unistd.h>
48#include <grp.h>
49#include <crt0.h>
1b94449f 50
838a94b8
EZ
51#ifndef __DJGPP_MINOR__
52# define __tb _go32_info_block.linear_address_of_transfer_buffer;
53#endif
54
1b94449f 55DEFUN ("int86", Fint86, Sint86, 2, 2, 0,
8fc29035 56 doc: /* Call specific MS-DOS interrupt number INTERRUPT with REGISTERS.
7363986a
PJ
57Return the updated REGISTER vector.
58
59INTERRUPT should be an integer in the range 0 to 255.
60REGISTERS should be a vector produced by `make-register' and
61`set-register-value'. */)
62 (interrupt, registers)
63 Lisp_Object interrupt, registers;
1b94449f
RS
64{
65 register int i;
66 int no;
67 union REGS inregs, outregs;
68 Lisp_Object val;
69
b7826503 70 CHECK_NUMBER (interrupt);
e20104ba 71 no = (unsigned long) XINT (interrupt);
b7826503 72 CHECK_VECTOR (registers);
b058cb36 73 if (no < 0 || no > 0xff || XVECTOR (registers)-> size != 8)
1b94449f
RS
74 return Qnil;
75 for (i = 0; i < 8; i++)
b7826503 76 CHECK_NUMBER (XVECTOR (registers)->contents[i]);
1b94449f 77
e20104ba
EN
78 inregs.x.ax = (unsigned long) XFASTINT (XVECTOR (registers)->contents[0]);
79 inregs.x.bx = (unsigned long) XFASTINT (XVECTOR (registers)->contents[1]);
80 inregs.x.cx = (unsigned long) XFASTINT (XVECTOR (registers)->contents[2]);
81 inregs.x.dx = (unsigned long) XFASTINT (XVECTOR (registers)->contents[3]);
82 inregs.x.si = (unsigned long) XFASTINT (XVECTOR (registers)->contents[4]);
83 inregs.x.di = (unsigned long) XFASTINT (XVECTOR (registers)->contents[5]);
84 inregs.x.cflag = (unsigned long) XFASTINT (XVECTOR (registers)->contents[6]);
85 inregs.x.flags = (unsigned long) XFASTINT (XVECTOR (registers)->contents[7]);
1b94449f
RS
86
87 int86 (no, &inregs, &outregs);
88
e20104ba
EN
89 XVECTOR (registers)->contents[0] = make_number (outregs.x.ax);
90 XVECTOR (registers)->contents[1] = make_number (outregs.x.bx);
91 XVECTOR (registers)->contents[2] = make_number (outregs.x.cx);
92 XVECTOR (registers)->contents[3] = make_number (outregs.x.dx);
93 XVECTOR (registers)->contents[4] = make_number (outregs.x.si);
94 XVECTOR (registers)->contents[5] = make_number (outregs.x.di);
95 XVECTOR (registers)->contents[6] = make_number (outregs.x.cflag);
96 XVECTOR (registers)->contents[7] = make_number (outregs.x.flags);
1b94449f 97
e20104ba 98 return registers;
1b94449f
RS
99}
100
5f08dc78 101DEFUN ("msdos-memget", Fdos_memget, Sdos_memget, 2, 2, 0,
7363986a
PJ
102 doc: /* Read DOS memory at offset ADDRESS into VECTOR.
103Return the updated VECTOR. */)
104 (address, vector)
105 Lisp_Object address, vector;
5f08dc78
KS
106{
107 register int i;
108 int offs, len;
109 char *buf;
110 Lisp_Object val;
111
b7826503 112 CHECK_NUMBER (address);
e20104ba 113 offs = (unsigned long) XINT (address);
b7826503 114 CHECK_VECTOR (vector);
e20104ba 115 len = XVECTOR (vector)-> size;
238a44a8 116 if (len < 1 || len > 2048 || offs < 0 || offs > 0xfffff - len)
5f08dc78
KS
117 return Qnil;
118 buf = alloca (len);
119 dosmemget (offs, len, buf);
b058cb36 120
5f08dc78 121 for (i = 0; i < len; i++)
e20104ba 122 XVECTOR (vector)->contents[i] = make_number (buf[i]);
5f08dc78 123
e20104ba 124 return vector;
5f08dc78
KS
125}
126
127DEFUN ("msdos-memput", Fdos_memput, Sdos_memput, 2, 2, 0,
7363986a
PJ
128 doc: /* Write DOS memory at offset ADDRESS from VECTOR. */)
129 (address, vector)
130 Lisp_Object address, vector;
5f08dc78
KS
131{
132 register int i;
133 int offs, len;
134 char *buf;
135 Lisp_Object val;
136
b7826503 137 CHECK_NUMBER (address);
e20104ba 138 offs = (unsigned long) XINT (address);
b7826503 139 CHECK_VECTOR (vector);
e20104ba 140 len = XVECTOR (vector)-> size;
238a44a8 141 if (len < 1 || len > 2048 || offs < 0 || offs > 0xfffff - len)
5f08dc78
KS
142 return Qnil;
143 buf = alloca (len);
144
145 for (i = 0; i < len; i++)
146 {
b7826503 147 CHECK_NUMBER (XVECTOR (vector)->contents[i]);
e20104ba 148 buf[i] = (unsigned char) XFASTINT (XVECTOR (vector)->contents[i]) & 0xFF;
5f08dc78
KS
149 }
150
151 dosmemput (buf, len, offs);
152 return Qt;
153}
154
155DEFUN ("msdos-set-keyboard", Fmsdos_set_keyboard, Smsdos_set_keyboard, 1, 2, 0,
7363986a
PJ
156 doc: /* Set keyboard layout according to COUNTRY-CODE.
157If the optional argument ALLKEYS is non-nil, the keyboard is mapped for
158all keys; otherwise it is only used when the ALT key is pressed.
159The current keyboard layout is available in dos-keyboard-code. */)
160 (country_code, allkeys)
238a44a8 161 Lisp_Object country_code, allkeys;
5f08dc78 162{
b7826503 163 CHECK_NUMBER (country_code);
5f08dc78
KS
164 if (!dos_set_keyboard (XINT (country_code), !NILP (allkeys)))
165 return Qnil;
166 return Qt;
167}
168
87485d6f
MW
169#ifndef HAVE_X_WINDOWS
170/* Later we might want to control the mouse interface with this function,
171 e.g., with respect to non-80 column screen modes. */
172
7363986a
PJ
173DEFUN ("msdos-mouse-p", Fmsdos_mouse_p, Smsdos_mouse_p, 0, 0, 0,
174 doc: /* Report whether a mouse is present. */)
87485d6f
MW
175 ()
176{
177 if (have_mouse)
178 return Qt;
179 else
180 return Qnil;
181}
87485d6f
MW
182#endif
183
ac3b0279 184DEFUN ("msdos-mouse-init", Fmsdos_mouse_init, Smsdos_mouse_init, 0, 0, "",
7363986a
PJ
185 doc: /* Initialize and enable mouse if available. */)
186 ()
ac3b0279 187{
5f08dc78
KS
188 if (have_mouse)
189 {
190 have_mouse = 1;
191 mouse_init ();
192 return Qt;
193 }
ac3b0279
RS
194 return Qnil;
195}
196
197DEFUN ("msdos-mouse-enable", Fmsdos_mouse_enable, Smsdos_mouse_enable, 0, 0, "",
7363986a
PJ
198 doc: /* Enable mouse if available. */)
199 ()
ac3b0279
RS
200{
201 if (have_mouse)
202 {
5f08dc78
KS
203 have_mouse = 1;
204 mouse_on ();
ac3b0279
RS
205 }
206 return have_mouse ? Qt : Qnil;
207}
208
209DEFUN ("msdos-mouse-disable", Fmsdos_mouse_disable, Smsdos_mouse_disable, 0, 0, "",
7363986a
PJ
210 doc: /* Disable mouse if available. */)
211 ()
ac3b0279
RS
212{
213 mouse_off ();
214 if (have_mouse) have_mouse = -1;
215 return Qnil;
216}
217
7363986a 218DEFUN ("insert-startup-screen", Finsert_startup_screen, Sinsert_startup_screen, 0, 0, "",
cd53fb50 219 doc: /* Insert copy of screen contents prior to starting Emacs.
7363986a
PJ
220Return nil if startup screen is not available. */)
221 ()
5f08dc78
KS
222{
223 char *s;
b058cb36
EZ
224 int rows, cols, i, j;
225
5f08dc78
KS
226 if (!dos_get_saved_screen (&s, &rows, &cols))
227 return Qnil;
b058cb36 228
5f08dc78
KS
229 for (i = 0; i < rows; i++)
230 {
231 for (j = 0; j < cols; j++)
232 {
65788bc2 233 insert_char (*s);
5f08dc78
KS
234 s += 2;
235 }
65788bc2 236 insert_char ('\n');
5f08dc78
KS
237 }
238
239 return Qt;
240}
87485d6f 241\f
5f08dc78 242/* country info */
31ade731
SM
243EMACS_INT dos_country_code;
244EMACS_INT dos_codepage;
245EMACS_INT dos_timezone_offset;
246EMACS_INT dos_decimal_point;
247EMACS_INT dos_keyboard_layout;
5f08dc78 248unsigned char dos_country_info[DOS_COUNTRY_INFO];
e089dc62
RS
249static unsigned char usa_country_info[DOS_COUNTRY_INFO] = {
250 0, 0, /* date format */
251 '$', 0, 0, 0, 0, /* currency string */
252 ',', 0, /* thousands separator */
253 '.', 0, /* decimal separator */
254 '/', 0, /* date separator */
255 ':', 0, /* time separator */
256 0, /* currency format */
257 2, /* digits after decimal in currency */
258 0, /* time format */
259 0, 0, 0, 0, /* address of case map routine, GPF if used */
260 ' ', 0, /* data-list separator (?) */
261 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* reserved */
262};
5f08dc78 263
31ade731
SM
264EMACS_INT dos_hyper_key;
265EMACS_INT dos_super_key;
266EMACS_INT dos_keypad_mode;
1b94449f 267
5f08dc78
KS
268Lisp_Object Vdos_version;
269Lisp_Object Vdos_display_scancodes;
838a94b8
EZ
270
271#ifndef HAVE_X_WINDOWS
272static unsigned dos_windows_version;
273Lisp_Object Vdos_windows_version;
274
275char parent_vm_title[50]; /* Ralf Brown says 30 is enough */
276int w95_set_virtual_machine_title (const char *);
277
278void
279restore_parent_vm_title (void)
280{
281 if (NILP (Vdos_windows_version))
282 return;
283 if ((dos_windows_version & 0xff) >= 4 && parent_vm_title[0])
284 w95_set_virtual_machine_title (parent_vm_title);
285 delay (50);
286}
287#endif /* !HAVE_X_WINDOWS */
b058cb36 288
1b94449f
RS
289void
290init_dosfns ()
291{
292 union REGS regs;
1b94449f 293 _go32_dpmi_registers dpmiregs;
e089dc62 294 unsigned long xbuf = _go32_info_block.linear_address_of_transfer_buffer;
1b94449f 295
87485d6f 296#ifndef SYSTEM_MALLOC
1b94449f 297 get_lim_data (); /* why the hell isn't this called elsewhere? */
87485d6f 298#endif
1b94449f
RS
299
300 regs.x.ax = 0x3000;
301 intdos (&regs, &regs);
302 Vdos_version = Fcons (make_number (regs.h.al), make_number (regs.h.ah));
303
e089dc62
RS
304 /* Obtain the country code via DPMI, use DJGPP transfer buffer. */
305 dpmiregs.x.ax = 0x3800;
7a2fd369 306 dpmiregs.x.ds = xbuf >> 4;
e089dc62
RS
307 dpmiregs.x.dx = 0;
308 dpmiregs.x.ss = dpmiregs.x.sp = dpmiregs.x.flags = 0;
309 _go32_dpmi_simulate_int (0x21, &dpmiregs);
310 if (dpmiregs.x.flags & 1)
311 {
312 dos_country_code = 1; /* assume USA if 213800 failed */
313 memcpy (dos_country_info, usa_country_info, DOS_COUNTRY_INFO);
314 }
1b94449f
RS
315 else
316 {
1b94449f 317 dos_country_code = dpmiregs.x.bx;
e089dc62 318 dosmemget (xbuf, DOS_COUNTRY_INFO, dos_country_info);
1b94449f 319 }
e089dc62 320
5f08dc78 321 dos_set_keyboard (dos_country_code, 0);
1b94449f
RS
322
323 regs.x.ax = 0x6601;
324 intdos (&regs, &regs);
325 if (regs.x.cflag)
326 /* Estimate code page from country code */
b058cb36 327 switch (dos_country_code)
1b94449f
RS
328 {
329 case 45: /* Denmark */
330 case 47: /* Norway */
331 dos_codepage = 865;
332 break;
333 default:
334 /* US */
335 dos_codepage = 437;
336 }
337 else
338 dos_codepage = regs.x.bx & 0xffff;
50666766 339
838a94b8
EZ
340#ifndef HAVE_X_WINDOWS
341 parent_vm_title[0] = '\0';
342
343 /* If we are running from DOS box on MS-Windows, get Windows version. */
344 dpmiregs.x.ax = 0x1600; /* enhanced mode installation check */
345 dpmiregs.x.ss = dpmiregs.x.sp = dpmiregs.x.flags = 0;
346 _go32_dpmi_simulate_int (0x2f, &dpmiregs);
347 /* We only support Windows-specific features when we run on Windows 9X
348 or on Windows 3.X/enhanced mode.
349
350 Int 2Fh/AX=1600h returns:
351
352 AL = 00: no Windows at all;
353 AL = 01: Windows/386 2.x;
354 AL = 80h: Windows 3.x in mode other than enhanced;
355 AL = FFh: Windows/386 2.x
356
357 We also check AH > 0 (Windows 3.1 or later), in case AL tricks us. */
358 if (dpmiregs.h.al > 2 && dpmiregs.h.al != 0x80 && dpmiregs.h.al != 0xff
359 && (dpmiregs.h.al > 3 || dpmiregs.h.ah > 0))
360 {
361 dos_windows_version = dpmiregs.x.ax;
362 Vdos_windows_version =
363 Fcons (make_number (dpmiregs.h.al), make_number (dpmiregs.h.ah));
364
365 /* Save the current title of this virtual machine, so we can restore
366 it before exiting. Otherwise, Windows 95 will continue to use
367 the title we set even after we are history, stupido... */
368 if (dpmiregs.h.al >= 4)
369 {
370 dpmiregs.x.ax = 0x168e;
371 dpmiregs.x.dx = 3; /* get VM title */
372 dpmiregs.x.cx = sizeof(parent_vm_title) - 1;
373 dpmiregs.x.es = __tb >> 4;
374 dpmiregs.x.di = __tb & 15;
375 dpmiregs.x.sp = dpmiregs.x.ss = dpmiregs.x.flags = 0;
376 _go32_dpmi_simulate_int (0x2f, &dpmiregs);
377 if (dpmiregs.x.ax == 1)
378 dosmemget (__tb, sizeof(parent_vm_title), parent_vm_title);
379 }
380 }
381 else
382 {
383 dos_windows_version = 0;
384 Vdos_windows_version = Qnil;
385 }
386#endif /* !HAVE_X_WINDOWS */
387
50666766
RS
388#if __DJGPP__ >= 2
389
459f4042
RS
390 /* Without this, we never see hidden files.
391 Don't OR it with the previous value, so the value recorded at dump
392 time, possibly with `preserve-case' flags set, won't get through. */
393 __opendir_flags = __OPENDIR_FIND_HIDDEN;
394
395#if __DJGPP_MINOR__ == 0
396 /* Under LFN, preserve the case of files as recorded in the directory
397 (in DJGPP 2.01 and later this is automagically done by the library). */
50666766
RS
398 if (!NILP (Fmsdos_long_file_names ()))
399 __opendir_flags |= __OPENDIR_PRESERVE_CASE;
459f4042
RS
400#endif /* __DJGPP_MINOR__ == 0 */
401#endif /* __DJGPP__ >= 2 */
1b94449f
RS
402}
403\f
838a94b8 404#ifndef HAVE_X_WINDOWS
e4441df0
EZ
405
406/* Emulation of some X window features from xfns.c and xfaces.c. */
407
408/* Standard VGA colors, in the order of their standard numbering
409 in the default VGA palette. */
410static char *vga_colors[16] = {
411 "black", "blue", "green", "cyan", "red", "magenta", "brown",
412 "lightgray", "darkgray", "lightblue", "lightgreen", "lightcyan",
413 "lightred", "lightmagenta", "yellow", "white"
414};
415
416/* Given a color name, return its index, or -1 if not found. Note
417 that this only performs case-insensitive comparison against the
418 standard names. For anything more sophisticated, like matching
419 "gray" with "grey" or translating X color names into their MSDOS
2d764c78
EZ
420 equivalents, call the Lisp function Qtty_color_desc (defined
421 on lisp/term/tty-colors.el). */
e4441df0
EZ
422int
423msdos_stdcolor_idx (const char *name)
424{
425 int i;
426
427 for (i = 0; i < sizeof (vga_colors) / sizeof (vga_colors[0]); i++)
05131107 428 if (xstrcasecmp (name, vga_colors[i]) == 0)
e4441df0
EZ
429 return i;
430
f9d2fdc4 431 return
24480d5b
EZ
432 strcmp (name, unspecified_fg) == 0 ? FACE_TTY_DEFAULT_FG_COLOR
433 : strcmp (name, unspecified_bg) == 0 ? FACE_TTY_DEFAULT_BG_COLOR
f9d2fdc4 434 : FACE_TTY_DEFAULT_COLOR;
e4441df0
EZ
435}
436
437/* Given a color index, return its standard name. */
2d764c78 438Lisp_Object
e4441df0
EZ
439msdos_stdcolor_name (int idx)
440{
24480d5b
EZ
441 extern Lisp_Object Qunspecified;
442
443 if (idx == FACE_TTY_DEFAULT_FG_COLOR)
444 return build_string (unspecified_fg);
445 else if (idx == FACE_TTY_DEFAULT_BG_COLOR)
446 return build_string (unspecified_bg);
447 else if (idx >= 0 && idx < sizeof (vga_colors) / sizeof (vga_colors[0]))
448 return build_string (vga_colors[idx]);
449 else
450 return Qunspecified; /* meaning the default */
e4441df0
EZ
451}
452
838a94b8
EZ
453/* Support for features that are available when we run in a DOS box
454 on MS-Windows. */
455int
456ms_windows_version (void)
457{
458 return dos_windows_version;
459}
460
461/* Set the title of the current virtual machine, to appear on
462 the caption bar of that machine's window. */
463
464int
465w95_set_virtual_machine_title (const char *title_string)
466{
467 /* Only Windows 9X (version 4 and higher) support this function. */
468 if (!NILP (Vdos_windows_version)
469 && (dos_windows_version & 0xff) >= 4)
470 {
471 _go32_dpmi_registers regs;
472 dosmemput (title_string, strlen (title_string) + 1, __tb);
473 regs.x.ax = 0x168e;
474 regs.x.dx = 1;
475 regs.x.es = __tb >> 4;
476 regs.x.di = __tb & 15;
477 regs.x.sp = regs.x.ss = regs.x.flags = 0;
478 _go32_dpmi_simulate_int (0x2f, &regs);
479 return regs.x.ax == 1;
480 }
481 return 0;
482}
483
484/* Change the title of frame F to NAME.
485 If NAME is nil, use the frame name as the title.
486
487 If Emacs is not run from a DOS box on Windows 9X, this only
488 sets the name in the frame struct, but has no other effects. */
489
490void
491x_set_title (f, name)
492 struct frame *f;
493 Lisp_Object name;
494{
495 /* Don't change the title if it's already NAME. */
496 if (EQ (name, f->title))
497 return;
498
499 update_mode_lines = 1;
500
501 f->title = name;
502
503 if (NILP (name))
504 name = f->name;
505
506 if (FRAME_MSDOS_P (f))
507 {
508 BLOCK_INPUT;
d5db4077 509 w95_set_virtual_machine_title (SDATA (name));
838a94b8
EZ
510 UNBLOCK_INPUT;
511 }
512}
513#endif /* !HAVE_X_WINDOWS */
514\f
76a76a57 515DEFUN ("file-system-info", Ffile_system_info, Sfile_system_info, 1, 1, 0,
7363986a
PJ
516 doc: /* Return storage information about the file system FILENAME is on.
517Value is a list of floats (TOTAL FREE AVAIL), where TOTAL is the total
518storage of the file system, FREE is the free storage, and AVAIL is the
519storage available to a non-superuser. All 3 numbers are in bytes.
520If the underlying system call fails, value is nil. */)
521 (filename)
522 Lisp_Object filename;
76a76a57
EZ
523{
524 struct statfs stfs;
525 Lisp_Object encoded, value;
526
b7826503 527 CHECK_STRING (filename);
76a76a57
EZ
528 filename = Fexpand_file_name (filename, Qnil);
529 encoded = ENCODE_FILE (filename);
530
d5db4077 531 if (statfs (SDATA (encoded), &stfs))
76a76a57
EZ
532 value = Qnil;
533 else
534 value = list3 (make_float ((double) stfs.f_bsize * stfs.f_blocks),
535 make_float ((double) stfs.f_bsize * stfs.f_bfree),
536 make_float ((double) stfs.f_bsize * stfs.f_bavail));
537
538 return value;
539}
540\f
e5c9f94c
EZ
541/* System depended enumeration of and access to system processes a-la
542 ps(1). Here, we only return info about the running Emacs process.
543 (There are no other processes on DOS, right?) */
544
545Lisp_Object
546list_system_processes ()
547{
548 Lisp_Object proclist = Qnil;
549
550 proclist = Fcons (make_fixnum_or_float (getpid ()), proclist);
551
552 return proclist;
553}
554
555Lisp_Object
556system_process_attributes (Lisp_Object pid)
557{
558 int proc_id;
559 Lisp_Object attrs = Qnil;
560
561 CHECK_NUMBER_OR_FLOAT (pid);
562 proc_id = FLOATP (pid) ? XFLOAT_DATA (pid) : XINT (pid);
563
564 if (proc_id == getpid ())
565 {
566 EMACS_INT uid, gid;
567 char *usr;
568 struct group *gr;
569 char cmd[FILENAME_MAX];
570 char *cmdline = NULL, *p, *q;
571 size_t cmdline_size = 0;
572 int i;
573 Lisp_Object cmd_str, decoded_cmd, tem;
574 double pmem;
693a2698 575#ifndef SYSTEM_MALLOC
e5c9f94c 576 extern unsigned long ret_lim_data ();
693a2698 577#endif
e5c9f94c
EZ
578
579 uid = getuid ();
580 attrs = Fcons (Fcons (Qeuid, make_fixnum_or_float (uid)), attrs);
581 usr = getlogin ();
582 if (usr)
583 attrs = Fcons (Fcons (Quser, build_string (usr)), attrs);
584 gid = getgid ();
585 attrs = Fcons (Fcons (Qegid, make_fixnum_or_float (gid)), attrs);
586 gr = getgrgid (gid);
587 if (gr)
588 attrs = Fcons (Fcons (Qgroup, build_string (gr->gr_name)), attrs);
589 strcpy (cmd, basename (__crt0_argv[0]));
590 /* Command name is encoded in locale-coding-system; decode it. */
591 cmd_str = make_unibyte_string (cmd, strlen (cmd));
592 decoded_cmd = code_convert_string_norecord (cmd_str,
593 Vlocale_coding_system, 0);
594 attrs = Fcons (Fcons (Qcomm, decoded_cmd), attrs);
595 /* Pretend we have 0 as PPID. */
596 attrs = Fcons (Fcons (Qppid, make_number (0)), attrs);
597 attrs = Fcons (Fcons (Qpgrp, pid), attrs);
598 attrs = Fcons (Fcons (Qttname, build_string ("/dev/tty")), attrs);
599 /* We are never idle! */
600 tem = Fget_internal_run_time ();
601 attrs = Fcons (Fcons (Qtime, tem), attrs);
602 attrs = Fcons (Fcons (Qthcount, make_number (1)), attrs);
603 attrs = Fcons (Fcons (Qstart,
604 Fsymbol_value (intern ("before-init-time"))),
605 attrs);
606 attrs = Fcons (Fcons (Qvsize,
607 make_fixnum_or_float ((unsigned long)sbrk(0)/1024)),
608 attrs);
609 attrs = Fcons (Fcons (Qetime, tem), attrs);
693a2698
EZ
610#ifndef SYSTEM_MALLOC
611 /* ret_lim_data is on vm-limit.c, which is not compiled in under
612 SYSTEM_MALLOC. */
e5c9f94c
EZ
613 pmem = (double)((unsigned long) sbrk (0)) / ret_lim_data () * 100.0;
614 if (pmem > 100)
693a2698 615#endif
e5c9f94c
EZ
616 pmem = 100;
617 attrs = Fcons (Fcons (Qpmem, make_float (pmem)), attrs);
618 /* Pass 1: Count how much storage we need. */
619 for (i = 0; i < __crt0_argc; i++)
620 {
621 cmdline_size += strlen (__crt0_argv[i]) + 1; /* +1 for blank delim */
622 if (strpbrk (__crt0_argv[i], " \t\n\r\v\f"))
623 {
624 cmdline_size += 2;
625 for (p = __crt0_argv[i]; *p; p++)
626 {
627 if (*p == '"')
628 cmdline_size++;
629 }
630 }
631 }
632 /* Pass 2: Allocate storage and concatenate argv[]. */
633 cmdline = xmalloc (cmdline_size + 1);
634 for (i = 0, q = cmdline; i < __crt0_argc; i++)
635 {
636 if (strpbrk (__crt0_argv[i], " \t\n\r\v\f"))
637 {
638 *q++ = '"';
639 for (p = __crt0_argv[i]; *p; p++)
640 {
641 if (*p == '\"')
642 *q++ = '\\';
643 *q++ = *p;
644 }
645 *q++ = '"';
646 }
647 else
648 {
649 strcpy (q, __crt0_argv[i]);
650 q += strlen (__crt0_argv[i]);
651 }
652 *q++ = ' ';
653 }
654 /* Remove the trailing blank. */
655 if (q > cmdline)
656 q[-1] = '\0';
657
658 /* Command line is encoded in locale-coding-system; decode it. */
659 cmd_str = make_unibyte_string (cmdline, strlen (cmdline));
660 decoded_cmd = code_convert_string_norecord (cmd_str,
661 Vlocale_coding_system, 0);
662 xfree (cmdline);
663 attrs = Fcons (Fcons (Qargs, decoded_cmd), attrs);
664 }
665
666 return attrs;
667}
668\f
838a94b8
EZ
669void
670dos_cleanup (void)
671{
c06981c0
EZ
672 struct tty_display_info *tty;
673
838a94b8
EZ
674#ifndef HAVE_X_WINDOWS
675 restore_parent_vm_title ();
676#endif
6f855d64
EZ
677 /* Make sure the termscript file is committed, in case we are
678 crashing and some vital info was written there. */
c06981c0 679 if (FRAMEP (selected_frame))
6f855d64 680 {
c06981c0
EZ
681 struct frame *sf = XFRAME (selected_frame);
682
683 if (FRAME_LIVE_P (sf)
684 && (FRAME_MSDOS_P (sf) || FRAME_TERMCAP_P (sf)))
685 {
686 tty = CURTTY ();
687 if (tty->termscript)
688 {
689 fflush (tty->termscript);
690 fsync (fileno (tty->termscript));
691 }
692 }
6f855d64 693 }
838a94b8
EZ
694}
695
1b94449f
RS
696/*
697 * Define everything
698 */
699syms_of_dosfns ()
700{
1b94449f 701 defsubr (&Sint86);
5f08dc78
KS
702 defsubr (&Sdos_memget);
703 defsubr (&Sdos_memput);
ac3b0279
RS
704 defsubr (&Smsdos_mouse_init);
705 defsubr (&Smsdos_mouse_enable);
5f08dc78
KS
706 defsubr (&Smsdos_set_keyboard);
707 defsubr (&Sinsert_startup_screen);
ac3b0279 708 defsubr (&Smsdos_mouse_disable);
76a76a57 709 defsubr (&Sfile_system_info);
87485d6f
MW
710#ifndef HAVE_X_WINDOWS
711 defsubr (&Smsdos_mouse_p);
87485d6f 712#endif
1b94449f
RS
713
714 DEFVAR_INT ("dos-country-code", &dos_country_code,
7363986a
PJ
715 doc: /* The country code returned by Dos when Emacs was started.
716Usually this is the international telephone prefix. */);
1b94449f
RS
717
718 DEFVAR_INT ("dos-codepage", &dos_codepage,
7363986a
PJ
719 doc: /* The codepage active when Emacs was started.
720The following are known:
721 437 United States
722 850 Multilingual (Latin I)
723 852 Slavic (Latin II)
724 857 Turkish
725 860 Portugal
726 861 Iceland
727 863 Canada (French)
728 865 Norway/Denmark */);
1b94449f 729
5f08dc78 730 DEFVAR_INT ("dos-timezone-offset", &dos_timezone_offset,
7363986a
PJ
731 doc: /* The current timezone offset to UTC in minutes.
732Implicitly modified when the TZ variable is changed. */);
b058cb36 733
1b94449f 734 DEFVAR_LISP ("dos-version", &Vdos_version,
7363986a 735 doc: /* The (MAJOR . MINOR) Dos version (subject to modification with setver). */);
5f08dc78 736
838a94b8
EZ
737#ifndef HAVE_X_WINDOWS
738 DEFVAR_LISP ("dos-windows-version", &Vdos_windows_version,
7363986a 739 doc: /* The (MAJOR . MINOR) Windows version for DOS session on MS-Windows. */);
838a94b8
EZ
740#endif
741
5f08dc78 742 DEFVAR_LISP ("dos-display-scancodes", &Vdos_display_scancodes,
7363986a
PJ
743 doc: /* *Controls whether DOS raw keyboard events are displayed as you type.
744When non-nil, the keyboard scan-codes are displayed at the bottom right
745corner of the display (typically at the end of the mode line).
746The output format is: scan code:char code*modifiers. */);
747
5f08dc78 748 Vdos_display_scancodes = Qnil;
b058cb36 749
5f08dc78 750 DEFVAR_INT ("dos-hyper-key", &dos_hyper_key,
7363986a
PJ
751 doc: /* *If set to 1, use right ALT key as hyper key.
752If set to 2, use right CTRL key as hyper key. */);
5f08dc78 753 dos_hyper_key = 0;
b058cb36 754
5f08dc78 755 DEFVAR_INT ("dos-super-key", &dos_super_key,
7363986a
PJ
756 doc: /* *If set to 1, use right ALT key as super key.
757If set to 2, use right CTRL key as super key. */);
5f08dc78 758 dos_super_key = 0;
b058cb36 759
5f08dc78 760 DEFVAR_INT ("dos-keypad-mode", &dos_keypad_mode,
7363986a
PJ
761 doc: /* *Controls what key code is returned by a key in the numeric keypad.
762The `numlock ON' action is only taken if no modifier keys are pressed.
763The value is an integer constructed by adding the following bits together:
764
765 0x00 Digit key returns digit (if numlock ON)
766 0x01 Digit key returns kp-digit (if numlock ON)
767 0x02 Digit key returns M-digit (if numlock ON)
768 0x03 Digit key returns edit key (if numlock ON)
769
770 0x00 Grey key returns char (if numlock ON)
771 0x04 Grey key returns kp-key (if numlock ON)
772
773 0x00 Digit key returns digit (if numlock OFF)
774 0x10 Digit key returns kp-digit (if numlock OFF)
775 0x20 Digit key returns M-digit (if numlock OFF)
776 0x30 Digit key returns edit key (if numlock OFF)
777
778 0x00 Grey key returns char (if numlock OFF)
779 0x40 Grey key returns kp-key (if numlock OFF)
780
781 0x200 ALT-0..ALT-9 in top-row produces shifted codes. */);
b3d5621c 782 dos_keypad_mode = 0x75;
b058cb36 783
5f08dc78 784 DEFVAR_INT ("dos-keyboard-layout", &dos_keyboard_layout,
7363986a
PJ
785 doc: /* Contains the country code for the current keyboard layout.
786Use msdos-set-keyboard to select another keyboard layout. */);
5f08dc78 787 dos_keyboard_layout = 1; /* US */
b058cb36 788
5f08dc78 789 DEFVAR_INT ("dos-decimal-point", &dos_decimal_point,
7363986a
PJ
790 doc: /* The character to produce when kp-decimal key is pressed.
791If non-zero, this variable contains the character to be returned when the
792decimal point key in the numeric keypad is pressed when Num Lock is on.
793If zero, the decimal point key returns the country code specific value. */);
5f08dc78 794 dos_decimal_point = 0;
1b94449f
RS
795}
796#endif /* MSDOS */
6b61353c
KH
797
798/* arch-tag: f5ea8847-a014-42c9-83f5-7738ad640b17
799 (do not change this comment) */