Debian Codegraph @

terps/tads/tads2/msdos/osdos.c

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/TADS2/msdos/Osdos.c,v 1.3 1999/07/11 00:46:37 MJRoberts Exp $";
4 #endif
5
6 /*
7 * Copyright (c) 1987, 2002 by Michael J. Roberts. All Rights Reserved.
8 *
9 * Please see the accompanying license file, LICENSE.TXT, for information
10 * on using and copying this software.
11 */
12 /*
13 Name
14 osdos - Operating System dependent definitions for MS-DOS and OS/2
15 Function
16 os_printz() - acts like fputs() to stdout
17 os_print() - prints a counted-length (not null-terminated) string
18 os_waitc() - waits for a keystroke at the keyboard
19 os_flush() - forces flush of standard output without newline
20 os_getc() - return a single character from the keyboard
21 os_defext() - adds a default extension to a filename
22 os_remext() - removes the extension from a filename
23 os_init() - os-dependent initialization
24 os_term() - os-dependent termination
25 os_expause()- pause on exit (from ti) if this is a wacky OS (Mac, etc.)
26 os_askfile()- prompt the user for a filename
27 os_score() - informs OS layer of current, maximum score (for status line)
28 os_status() - sets output status: 0=normal, 1=status line, 2=room ldesc
29 os_exeseek()- returns file pointer set up to read datafile from executable
30 os_exfil() - loads a resource from a file of the same name
31 os_exfld() - loads external function from an open file
32 os_excall() - calls an external function
33 Notes
34 The operating system and compiler dependent routines have been isolated
35 to this module. The appropriate pre-processor symbol for your compiler
36 should be defined when you compile this module. Currently recognized
37 compilers are:
38
39 CHIPMUNK - Bogus Caltech Chipmunk C compiler*
40 MWC - Mark Williams C (Atari ST/TOS)
41 LATTICE - Lattice C (IBM PC/MSDOS)
42 MACAZTEC - Aztec C68k (Macintosh)
43 MACLSC - LightspeedC (Macintosh)*
44 __TURBOC__ - Borland Turbo C (MSDOS)
45 MICROSOFT - Microsoft C (IBM PC/MSDOS/OS2)
46 UNIX5 - Unix System 5 Portable C compiler*
47 VMS - VAX/VMS
48 SUN_UNIX - Sun version of Unix
49
50 (Note: those marked with an asterisk (*) are not yet fully implemented.)
51
52 In addition, any global data items that are required for your compiler
53 should be placed in this module. Note in particular that many compilers
54 set a larger-than-default stack size (which is usually required due to the
55 heavy use of the stack in this program) with a global defined in this
56 module.
57 Returns
58 Varies by function.
59 Modified
60 04/24/93 JEras - use new os_locate() to find trcolor.dat
61 07/04/92 MJRoberts - always use BIOS video mode (usebios = 1)
62 01/19/92 MJRoberts - DOS file selector
63 09/26/91 JEras - os/2 user exit support
64 09/18/91 MJRoberts - TADS/Graphic additions
65 07/19/91 MJRoberts - deduct one from pagelength
66 05/23/91 MJRoberts - renamed os_exfrd to os_exld
67 04/06/91 JEras - os_exfrd() is for Turbo C only
68 03/10/91 MJRoberts - integrate John's qa-scripter mods
69 11/23/90 JEras - use ossgmx() to get screen size
70 11/23/90 JEras - enhance os_waitc() a little
71 11/16/90 JEras - os_init() and os_term are RUNTIME only
72 10/28/90 JEras - merge Mike's ST changes
73 10/14/90 JEras - fix shifted ldesc text after prompt bug
74 10/13/90 JEras - add USE_STDARG to avoid ref's past end of stack
75 10/13/90 JEras - fix bug in os_printf() for ldesc window
76 09/14/90 SMcAdams - make status line reverse video on mono display
77 08/07/90 SMcAdams - fix os_getc()'s use of '!'
78 05/12/90 MJRoberts - add emacs keys to history, make DOS runtime
79 03/23/90 JEras - add RUNTIME handling for DOSCOMMON and OS/2
80 10/10/89 MJRoberts - add history and editing functions
81 06/25/89 MJRoberts - add standard os_askfile, os_score, and os_status
82 06/25/89 MJRoberts - add MS-DOS ldesc display and status line
83 05/30/89 MJRoberts - add SUN_UNIX; add os_askfile
84 10/26/88 MJRoberts - add randomizer; default uses localtime()
85 03/15/88 MJRoberts - add support for Mac Aztec C, VAX/VMS
86 02/02/88 A.Greenblatt - Added support for Macintosh, Unix, Chipmunks
87 01/02/88 MJRoberts - created
88 */
89
90 #include <stdio.h>
91 #include <stddef.h>
92 #include <stdlib.h>
93 #include <ctype.h>
94 #include <string.h>
95 #include <stdarg.h>
96
97 #include <time.h>
98
99 #ifdef TURBO
100 # include <sys/stat.h>
101 # include <dir.h>
102 # include <dos.h>
103 # define _getch getch
104 # define _fileno fileno
105 # define _fstat fstat
106 #endif /* TURBO */
107
108 #ifdef DJGPP
109 # include <signal.h>
110 # include <dpmi.h>
111 # include <fcntl.h>
112 # include <conio.h>
113 # include <sys/stat.h>
114 # define _getch getch /* use the un-underscored name for 'getch */
115 # define _fileno fileno /* ...and for 'fileno' */
116 # define _fstat fstat /* ... and for 'fstat' */
117 #endif
118
119 #ifndef DJGPP
120 # include <share.h>
121 # include <dos.h>
122 #endif
123
124 #ifdef MICROSOFT
125 # ifdef T_WIN32
126 # include <conio.h>
127 # include <sys/stat.h>
128 # include <io.h>
129 # include <fcntl.h>
130 # include <Windows.h>
131 # endif
132 # ifdef RUNTIME
133 # include <dos.h>
134 # endif
135
136 /* adjust some keywords */
137 # if _MSC_VER >= 700
138 # define interrupt __interrupt
139 # define asm __asm
140 # else
141 # define interrupt _interrupt
142 # define asm _asm
143 # endif
144 # define getvect _dos_getvect
145 # define setvect _dos_setvect
146
147 #endif /* MICROSOFT */
148
149 #include "os.h"
150 #include "osgen.h"
151 #include "tio.h"
152
153 #ifdef __DPMI32__
154 # define interrupt _interrupt
155 #endif
156
157 #ifdef MSOS2
158 # define INCL_DOSMODULEMGR
159 # define INCL_DOSFILEMGR
160 # define INCL_KBD
161 # include <os2.h>
162
163 # ifdef RUNTIME
164 # ifdef __EMX__
165 # include <stdlib.h> /* getcwd */
166 # endif /* __EMX__ */
167 # ifdef MICROSOFT
168 # include <direct.h> /* getcwd */
169 # endif /* MICROSOFT */
170 # ifdef __IBMC__
171 # include <direct.h> /* getcwd */
172 # endif /* __IBMC__ */
173 # endif /* RUNTIME */
174
175 # ifdef __EMX__
176 # define getch() _read_kbd(0, 1, 0)
177 # endif /* __EMX__ */
178 # ifdef __32BIT__
179 # define DosGetProcAddr(a,b,c) DosQueryProcAddr(a,0L,b,c)
180 # endif /* __32BIT__ */
181
182 #endif /* MSOS2 */
183
184 #include "tadsexit.h"
185
186 #ifndef DJGPP
187 #ifdef __DPMI16__
188 # include <windows.h>
189 # define SCRBASE_COLOR_PTR ((char *)MK_FP(__SegB800, 0))
190 # define SCRBASE_MONO_PTR ((char *)MK_FP(__SegB000, 0))
191 #else
192 # define SCRBASE_COLOR_PTR ((char *)0xb8000000L)
193 # define SCRBASE_MONO_PTR ((char *)0xb0000000L)
194 #endif /* __DPMI16__ */
195 #endif /* !DJGPP */
196
197 /* flag: the stdin is the keyboard, so we can read from the console */
198 int G_stdin_is_kb = TRUE;
199
200 /*
201 * flag: special cursor control: the UI is driving the cursor position, so
202 * don't set it according to the text buffers
203 */
204 static osfar_t int S_keep_cursor_pos = FALSE;
205
206 /*
207 * Flag: use the Win95 international keyboard fix. If this flag is set,
208 * we'll work around a set of bugs in the windows 95 keyboard driver that
209 * prevents users from entering international characters. (We only enable
210 * the workaround when specifically asked, because the workaround itself
211 * can cause problems of its own on some systems.)
212 */
213 static osfar_t int S_os_win95_kb_fix = FALSE;
214
215
216 #ifdef USEDOSCOMMON /* this is the DOS and OS/2 implementation */
217 # ifdef RUNTIME
218 # if !defined(MSOS2) && !defined(DJGPP)
219 char *scrbase;
220 # endif /* !MSOS2 && !DJGPP */
221
222
223 static osfar_t int oldvmode;
224 extern int os_f_plain;
225 extern void osnoui_init();
226 extern void osnoui_uninit();
227
228 /* initialize screen display */
229 void os_scrini(void)
230 {
231 int xmax, ymax;
232
233 /* get the BIOS parameters for the screen size */
234 ossgmx(&ymax, &xmax);
235
236 /*
237 * set the width and height - note that ossgmx() returns the maximum
238 * column and row numbers; since these are zero-based, the width and
239 * height are (respectively) one higher than these maxima
240 */
241 G_oss_screen_width = xmax + 1;
242 G_oss_screen_height = ymax + 1;
243
244 /* if we're not in 'plain' mode, initialize the display */
245 if (!os_f_plain)
246 {
247 /* clear the screen in the default text color */
248 ossclr(0, 0, ymax, xmax, text_color);
249 }
250 }
251
252 #if defined(DJGPP) || defined(TURBO)
253 #define OSDOS_INIT_USEBIOS 0 /* don't use BIOS for djgpp */
254 #define _stat stat /* use the standard Unix names for 'struct stat' */
255 #define _S_IFREG S_IFREG /* ... and for the various S_xxx flags */
256 #define _S_IFIFO S_IFIFO
257 #endif
258
259 #ifndef OSDOS_INIT_USEBIOS
260 #define OSDOS_INIT_USEBIOS 1 /* unless other set, use BIOS by default */
261 #endif
262
263 /* special flag for using BIOS video i/o rather than screen i/o */
264 int usebios = OSDOS_INIT_USEBIOS; /* set if -b flag is set in arguments */
265
266 /* flag: use stdio-based askfile rather than char-mode graphical version */
267 static osfar_t int use_std_askfile = 0;
268
269 int os_init(int *argc, char *argv[], const char *prompt,
270 char *buf, int bufsiz)
271 {
272 int i;
273 FILE *fp;
274 char nbuf[128];
275 int found_arg;
276 struct _stat statbuf;
277
278 /* initialize the base non-UI package */
279 osnoui_init();
280
281 /*
282 * Scan for the -kbfix95 argument. If we find it, set the flag to use
283 * the win95 international keyboard workaround.
284 */
285 for (i = 0 ; i < *argc ; ++i)
286 {
287 /* check this argument */
288 if (!stricmp(argv[i], "-kbfix95"))
289 {
290 /* set the flag to turn on the fix */
291 S_os_win95_kb_fix = TRUE;
292
293 /*
294 * remove this argument from the option list - this is a
295 * DOS-specific option, so we don't want to pass it through to
296 * the portable code
297 */
298 for ( ; i + 1 < *argc ; ++i)
299 argv[i] = argv[i + 1];
300
301 /* reduce the argument count for the removed string */
302 --(*argc);
303
304 /* no need to look at subsequent arguments */
305 break;
306 }
307 }
308
309 #ifndef DJGPP
310 scrbase = SCRBASE_COLOR_PTR;
311 #endif
312
313 for (i = *argc - 1, found_arg = TRUE ; i != 0 && found_arg ; )
314 {
315 /* if this isn't a flag, we're done scanning for arguments */
316 if (argv[i][0] != '-' && argv[i][0] != '/')
317 break;
318
319 /* check for one of our flags */
320 switch(argv[i][1])
321 {
322 case 'b':
323 case 'B':
324 /* use BIOS for display operations */
325 usebios = 1;
326 #if !defined(MSOS2) && !defined(DJGPP)
327 scrbase = (char *)0xf0000000L;
328 #endif /* !MSOS2 && !DJGPP */
329 break;
330
331 case 'a':
332 case 'A':
333 /* use stdio version of askfile */
334 use_std_askfile = TRUE;
335 break;
336
337 default:
338 /* it's not one of our special added flags */
339 found_arg = FALSE;
340 break;
341 }
342
343 /* if this is one of our arguments, remove it */
344 if (found_arg)
345 {
346 --i;
347 --(*argc);
348 }
349 }
350
351 /* initialize the console */
352 oss_con_init();
353
354 /* get the status on the standard output handle */
355 if (!_fstat(_fileno(stdout), &statbuf))
356 {
357 /*
358 * if the standard output has been redirected to a file, use plain
359 * mode, and turn off MORE mode at the console formatter level
360 */
361 if ((statbuf.st_mode & _S_IFREG) != 0)
362 {
363 os_plain();
364 G_os_moremode = FALSE;
365 }
366 }
367
368 /* do some extra console setup in plain mode */
369 if (os_f_plain)
370 {
371 /* get status on the standard input handle */
372 if (!_fstat(_fileno(stdin), &statbuf))
373 {
374 /*
375 * if the standard input has been redirected to a file, turn
376 * off MORE mode at the console formatter level
377 */
378 if ((statbuf.st_mode & _S_IFREG) != 0)
379 G_os_moremode = FALSE;
380
381 /*
382 * if the standard input is a file or a pipe, note that we must
383 * read from stdin; otherwise, assume we can read directly from
384 * the console
385 */
386 if ((statbuf.st_mode & (_S_IFREG | _S_IFIFO)) != 0)
387 G_stdin_is_kb = FALSE;
388 }
389 }
390
391 /*
392 * If we have a monochrome monitor, change the color and screen buffer
393 * parameters accordingly.
394 */
395 if (ossmon())
396 {
397 #if !defined(MSOS2) && !defined(DJGPP)
398 scrbase = SCRBASE_MONO_PTR;
399 #endif /* !MSOS2 && !DJGPP */
400 sdesc_color = 0x70;
401 text_color = 7;
402 ldesc_color = 0x70;
403 debug_color = 7;
404 }
405
406 /* locate TRCOLOR.DAT and read the color values out of it */
407 if (os_locate("trcolor.dat", 11, argv[0], nbuf, sizeof(nbuf))
408 && (fp = fopen(nbuf, "r")) != 0)
409 {
410 int i;
411 static osfar_t int *colorptr[] =
412 { &text_normal_color, &text_bold_color, &sdesc_color };
413
414 for (i = 0 ; i < sizeof(colorptr)/sizeof(colorptr[0]) ; ++i)
415 {
416 fgets(nbuf, sizeof(nbuf), fp);
417 *colorptr[i] = atoi(nbuf);
418 }
419 fclose(fp);
420 }
421 text_color = text_normal_color;
422
423 /* set up the screen */
424 os_scrini();
425
426 /* initialize the status line with its background color */
427 status_mode = 1;
428 os_printz("");
429 status_mode = 0;
430
431 # ifdef USE_SCROLLBACK
432 osssbini(16*1024); /* allocate scrollback buffer */
433 # endif /* USE_SCROLLBACK */
434
435 return( 0 );
436 }
437
438 /*
439 * Uninitialize
440 */
441 void os_uninit(void)
442 {
443 /* un-initialize the base non-UI package */
444 osnoui_uninit();
445
446 /* un-initialize the console */
447 oss_con_uninit();
448 }
449
450 void os_term(int rc)
451 {
452 # ifdef USE_SCROLLBACK
453 osssbdel();
454 # endif /* USE_SCROLLBACK */
455
456 exit(rc);
457 }
458 # endif /* RUNTIME */
459
460 #ifdef TURBO
461 # define os_getch getch
462 #endif
463
464 #ifdef MICROSOFT
465 # ifdef MSDOS
466 # ifndef MSOS2
467 # ifndef T_WIN32
468
469 /* ------------------------------------------------------------------------ */
470 /*
471 * define an os_getch that can deal with ^P, ^C, unlike MSC's getch()
472 */
473 char os_getch(void)
474 {
475 char retval;
476
477 /* put the cursor at the default input position */
478 if (!S_keep_cursor_pos)
479 osssb_cursor_to_default_pos();
480
481 /* call DOS directly to get a key */
482 asm {
483 mov ah, 07h
484 int 21h
485 mov retval, al
486 }
487
488 /* return the key */
489 return retval;
490 }
491
492 # endif /* T_WIN32 */
493 # endif /* MSOS2 */
494 # endif /* MSDOS */
495 #endif /* MICROSOFT */
496
497
498 #ifndef T_WIN32
499 /* ------------------------------------------------------------------------ */
500 /*
501 * Sleep for the given interval. We'll simply loop until the system
502 * clock indicates the desired time.
503 */
504 void os_sleep_ms(long delay_in_milliseconds)
505 {
506 long done_time;
507
508 /* do any deferred redrawing */
509 osssb_redraw_if_needed();
510
511 /*
512 * calculate the time when we'll be done by adding the delay to the
513 * current time
514 */
515 done_time = os_get_sys_clock_ms() + delay_in_milliseconds;
516
517 /* loop until the system clock says we're done */
518 while (os_get_sys_clock_ms() < done_time)
519 /* do nothing but soak up CPU cycles... */;
520 }
521
522
523 /* ------------------------------------------------------------------------ */
524 /*
525 * For DOS applications that aren't running as Win32 console apps,
526 * implement a keyboard read timeout by looping until the timeout
527 * expires or a key becomes available. This hogs the CPU, but DOS
528 * doesn't provide a means of waiting for input with a timeout, so we
529 * must simply poll the keyboard.
530 *
531 * The timeout is specified in milliseconds.
532 */
533 static int oss_getc_internal(unsigned char *ch,
534 unsigned long timeout, int use_timeout)
535 {
536 unsigned long abs_timeout;
537 char retval;
538 #ifdef DJGPP
539 __dpmi_regs regs;
540 #endif
541
542 #ifdef RUNTIME
543 /* if we're in plain mode, just get a key from stdin */
544 if (os_f_plain)
545 {
546 /* reject timeout requests */
547 if (use_timeout)
548 return OS_GETS_EOF;
549
550 /* make sure output is flushed */
551 fflush(stdout);
552
553 /* get a key from stdin */
554 *ch = (G_stdin_is_kb ? _getch() : getchar());
555
556 /* return a key event */
557 return OS_GETS_OK;
558 }
559 #endif
560
561 /* do any deferred redrawing */
562 osssb_redraw_if_needed();
563
564 /*
565 * if we're not using a timeout, just return the next character in
566 * the ordinary fashion
567 */
568 if (!use_timeout)
569 {
570 /* get a keystroke directly from DOS */
571 #ifdef DJGPP
572 regs.h.ah = 7;
573 __dpmi_int(0x21, &regs);
574 retval = regs.h.al;
575 #else /* DJGPP */
576 asm {
577 mov ah, 07h
578 int 21h
579 mov retval, al
580 }
581 #endif /* DJGPP */
582
583 /*
584 * Some keyboards return code 0340 as the first byte of an
585 * extended key sequence. We make no use of any additional
586 * information this conveys (if indeed it conveys anything; it's
587 * not clear that it really does), so change it to zero here and
588 * now.
589 */
590 if ((unsigned char)retval == 0340)
591 retval = 0;
592
593 /* return it */
594 *ch = retval;
595 return OS_GETS_OK;
596 }
597
598 /*
599 * calculate the absolute time of the timeout by adding the current
600 * time to the system clock
601 */
602 abs_timeout = (unsigned long)os_get_sys_clock_ms() + timeout;
603
604 /*
605 * loop until we reach the timeout or a keystroke arrives in the
606 * keyboard buffer
607 */
608 for (;;)
609 {
610 /* check the keyboard buffer to see if a key is ready */
611 #ifdef DJGPP
612 regs.h.ah = 6;
613 regs.h.dl = 0xff;
614 __dpmi_int(0x21, &regs);
615 if (regs.x.flags & 0x40) /* Zero flag */
616 goto no_key_available;
617 retval = regs.h.al;
618 #else /* DJGPP */
619 asm {
620 mov ah, 06h
621 mov dl, 0ffh
622 int 21h
623 jz no_key_available
624
625 mov retval, al
626 }
627 #endif /* DJGPP */
628
629 /* change 0340 to 0 (same as above) */
630 if ((unsigned char)retval == 0340)
631 retval = 0;
632
633 /* we got a key - put it in *ch and return success */
634 *ch = retval;
635 return OS_GETS_OK;
636
637 no_key_available:
638 /*
639 * no key is ready yet - if we've reached the absolute timeout
640 * time, return with timeout
641 */
642 if ((unsigned long)os_get_sys_clock_ms() >= abs_timeout)
643 return OS_GETS_TIMEOUT;
644 }
645 }
646
647 #ifdef DJGPP
648 /*
649 * Get a character from the keyboard
650 */
651 char os_getch(void)
652 {
653 unsigned char ch;
654
655 /* get a keystroke without a timeout */
656 if (oss_getc_internal(&ch, 0, FALSE) == OS_GETS_OK)
657 return ch;
658 else
659 return EOF;
660 }
661 #endif /* DJGPP */
662
663 /*
664 * get a character from the keyboard with a timeout - uses the internal
665 * implementation to do the work
666 */
667 int oss_getc_timeout(unsigned char *ch, unsigned long timeout)
668 {
669 return oss_getc_internal(ch, timeout, TRUE);
670 }
671
672 #endif /* T_WIN32 */
673
674
675 /* ------------------------------------------------------------------------ */
676 /*
677 * For Win32 console apps, we need to do a whole lot more work, because
678 * console app version of the Microsoft _getch() function doesn't do a
679 * number of things we need, such as return Alt+letter keys or
680 * Alt+numeric keypad entries. So, we'll simply implement our own
681 * console reader that takes care of all of those operations.
682 */
683 #ifdef T_WIN32
684
685 /*
686 * Translate a virtual key code for a numeric keypad key into the
687 * corresponding digit. Returns a value from 0 to 9 if the key is in
688 * fact a keypad key, or something outside that range if not.
689 */
690 static unsigned char xlat_keypad(int vkey)
691 {
692 switch(vkey)
693 {
694 case VK_HOME:
695 return 7;
696
697 case VK_UP:
698 return 8;
699
700 case VK_PRIOR:
701 return 9;
702
703 case VK_LEFT:
704 return 4;
705
706 case VK_CLEAR:
707 return 5;
708
709 case VK_RIGHT:
710 return 6;
711
712 case VK_END:
713 return 1;
714
715 case VK_DOWN:
716 return 2;
717
718 case VK_NEXT:
719 return 3;
720
721 case VK_INSERT:
722 return 0;
723
724 default:
725 /* it's invalid - return something outside of the 0-9 range */
726 return 100;
727 }
728 }
729
730 /*
731 * Internal oss_getc - read a character from the keyboard, optionally
732 * timing out after the given interval.
733 */
734 static int oss_getc_internal(unsigned char *ch,
735 unsigned long timeout, int use_timeout)
736 {
737 static osfar_t HANDLE hcon = INVALID_HANDLE_VALUE;
738 DWORD oldmode;
739 static osfar_t unsigned char nxt_ret = 0;
740 DWORD abs_timeout;
741 int status_code;
742 extern int os_f_plain;
743
744 /* if we're in plain mode, just get a key from stdin */
745 if (os_f_plain)
746 {
747 /* reject timeout requests */
748 if (use_timeout)
749 return OS_GETS_EOF;
750
751 /* make sure stdout is flushed */
752 fflush(stdout);
753
754 /* if we're at EOF on stdin, return eof indication */
755 if (feof(stdin))
756 return OS_GETS_EOF;
757
758 /* get a key */
759 *ch = (G_stdin_is_kb ? _getch() : getchar());
760
761 /* return a key event */
762 return OS_GETS_OK;
763 }
764
765 /*
766 * if we have a timeout value, calculate the wall clock time when it
767 * expires
768 */
769 if (use_timeout)
770 abs_timeout = GetTickCount() + timeout;
771
772 /* if we've buffered up a character, return it */
773 if (nxt_ret != 0)
774 {
775 *ch = nxt_ret;
776 nxt_ret = 0;
777 return OS_GETS_OK;
778 }
779
780 /* open the console if we haven't already done so */
781 if (hcon == INVALID_HANDLE_VALUE)
782 {
783 hcon = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE,
784 FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
785 OPEN_EXISTING, 0, 0);
786 if (hcon == INVALID_HANDLE_VALUE)
787 {
788 *ch = _getch();
789 return OS_GETS_OK;
790 }
791 }
792
793 /* save the original console mode for restoration before we return */
794 GetConsoleMode(hcon, &oldmode);
795
796 /* set our console mode */
797 SetConsoleMode(hcon, ENABLE_WINDOW_INPUT);
798
799 /* read characters until we get something we like */
800 for (;;)
801 {
802 INPUT_RECORD inrec;
803 DWORD reccnt;
804 KEY_EVENT_RECORD *keyevt;
805 int alt, ctl, shift, extended;
806
807 /*
808 * if we have a timeout value, wait for something to become
809 * available
810 */
811 if (use_timeout)
812 {
813 DWORD cur_time;
814 DWORD interval;
815
816 /* check to see if we've already reached the timeout */
817 cur_time = GetTickCount();
818 if (cur_time >= abs_timeout)
819 {
820 /* we've reached the timeout */
821 status_code = OS_GETS_TIMEOUT;
822 goto done;
823 }
824
825 /*
826 * wait for input to become available, limiting our wait to
827 * the interval remaining before the absolute timeout
828 */
829 interval = (DWORD)(abs_timeout - cur_time);
830 if (WaitForSingleObject(hcon, interval) == WAIT_TIMEOUT)
831 {
832 /* the timeout expired - return timeout */
833 status_code = OS_GETS_TIMEOUT;
834 goto done;
835 }
836 }
837
838 /*
839 * If we're using the win95 international keyboard fix, check the
840 * next event to see if we should read it using ReadConsole or
841 * ReadConsoleInput. Each call has weird limitations, so we must
842 * check the type of event we have to determine which set of
843 * limitations we can live with for the event. Note that this is
844 * only a problem in win95; in 98 and later versions, as well as
845 * NT, ReadConsoleInput works properly in all cases, so we can use
846 * it as the single keyboard input reader.
847 *
848 * The problem we're trying to solve is that ReadConsoleInput on
849 * windows 95 does not properly handle keyboard layouts on
850 * localized systems. There are two facets to the problem.
851 * First, ReadConsoleInput prevents the special key events that
852 * switch keyboard layouts from reaching the keyboard layout
853 * manager; this makes it impossible for the user to select
854 * different keyboard layouts, which is necessary for using
855 * certain character sets. Second, ReadConsoleInput does not
856 * properly translate the keystrokes through the keyboard layout,
857 * so even when the proper layout is selected, the wrong character
858 * codes are returned.
859 *
860 * ReadConsole on win95 does not suffer from these problems; it
861 * seems to be layered above the keyboard layout manager, so the
862 * layout manager has a chance to intercept keystrokes for layout
863 * change detection and for character translation. Unfortunately,
864 * ReadConsole doesn't provide any way to read extended keys such
865 * as arrows and function keys.
866 *
867 * Note that this fix might be problematic on some systems.
868 * Microsoft support specifically warns in a tech note on the msdn
869 * site that calls to ReadConsole cannot be mixed with calls to
870 * ReadConsoleInput on a single console handle; the usual
871 * "unpredictable results" are said to apply. However, in
872 * practice, it seems that this isn't really that big a problem
873 * for most users, and other applications have reportedly been
874 * using this same mixed-API fix for some time. So, we'll code
875 * this fix, but we'll use it only when the user explicitly
876 * enables it; this seems to be the safest approach, in that it
877 * ensures that users who don't already have keyboard problems
878 * won't start having them (since they'll never enable the fix),
879 * and those that do might have a way to work around them.
880 */
881 if (S_os_win95_kb_fix)
882 {
883 int evt_ch;
884
885 /*
886 * wait for an event to become available (since Peek doesn't
887 * wait by itself)
888 */
889 WaitForSingleObject(hcon, INFINITE);
890
891 /* look at (but do not yet actually read) the next event */
892 PeekConsoleInput(hcon, &inrec, 1, &reccnt);
893
894 /* presume this will be a key event (no harm done if not) */
895 keyevt = &inrec.Event.KeyEvent;
896
897 /* get the ALT-key state */
898 alt = ((keyevt->dwControlKeyState
899 & (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED)) != 0);
900
901 /* get the character from the event, if applicable */
902 evt_ch = keyevt->uChar.AsciiChar;
903
904 /*
905 * If we have a keyboard event, and the event contains a
906 * regular printable character, use ReadConsole() to read the
907 * event. If the ALT key is down, consider it a regular
908 * character if the AsciiChar is outside of the [A-Za-z] range
909 * - if it's in the regular alphabetic range, treat it as an
910 * ALT+letter command sequence rather than as a localized
911 * character entry.
912 */
913 if (inrec.EventType == KEY_EVENT
914 && keyevt->bKeyDown
915 && evt_ch != '\0'
916 && (!alt || (alt && evt_ch > 127)))
917 {
918 /*
919 * it's a regular character - use ReadConsole to read the
920 * keystroke, since it performs the proper translations
921 */
922 ReadConsole(hcon, ch, 1, &reccnt, 0);
923
924 /* return this character */
925 status_code = OS_GETS_OK;
926 goto done;
927 }
928 }
929
930 /* read a console event */
931 if (!ReadConsoleInput(hcon, &inrec, 1, &reccnt))
932 {
933 *ch = _getch();
934 status_code = OS_GETS_OK;
935 goto done;
936 }
937
938 /* see what we have */
939 switch(inrec.EventType)
940 {
941 #ifdef RUNTIME
942 case WINDOW_BUFFER_SIZE_EVENT:
943 /* notify the ossdos32 layer of the change */
944 oss_win_resize_event();
945 break;
946 #endif
947
948 case KEY_EVENT:
949 /* note that we have a key event */
950 status_code = OS_GETS_OK;
951
952 /* get the key event */
953 keyevt = &inrec.Event.KeyEvent;
954
955 /* ignore key-up events */
956 if (!keyevt->bKeyDown)
957 continue;
958
959 /* if it's a shift key of some kind, ignore it */
960 switch(keyevt->wVirtualKeyCode)
961 {
962 case VK_SHIFT:
963 case VK_CONTROL:
964 case VK_MENU:
965 case VK_PAUSE:
966 case VK_CAPITAL:
967 case VK_NUMLOCK:
968 case VK_SCROLL:
969 continue;
970 }
971
972 /* get the control key state */
973 alt = ((keyevt->dwControlKeyState
974 & (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED)) != 0);
975 ctl = ((keyevt->dwControlKeyState
976 & (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED)) != 0);
977 shift = ((keyevt->dwControlKeyState & SHIFT_PRESSED) != 0);
978 extended = ((keyevt->dwControlKeyState & ENHANCED_KEY) != 0);
979
980 /* if it's an ascii character, return it directly */
981 if (keyevt->uChar.AsciiChar != 0)
982 {
983 unsigned char evtch = keyevt->uChar.AsciiChar;
984
985 /* if the ALT key is down, translate it */
986 if (alt)
987 {
988 /* check to see if it's alphabetic */
989 if (evtch >= 'a' && evtch <= 'z')
990 evtch -= 'a' - 'A';
991 if (evtch >= 'A' && evtch <= 'Z')
992 {
993 static osfar_t char alt_alpha[] =
994 {
995 30, 48, 46, 32, 18, 33, 34, 35, 23, 36,
996 37, 38, 50, 49, 24, 25, 16, 19, 31, 20,
997 22, 47, 17, 45, 21, 44
998 };
999
1000 /*
1001 * translate it to the traditional BIOS ALT-key
1002 * code and return it as a two-character
1003 * sequence
1004 */
1005 nxt_ret = alt_alpha[evtch - 'A'];
1006 *ch = 0;
1007 status_code = OS_GETS_OK;
1008 goto done;
1009 }
1010
1011 /* ignore other ALT key combinations */
1012 break;
1013 }
1014
1015 /* it's a normal ascii character - return it as-is */
1016 *ch = evtch;
1017 status_code = OS_GETS_OK;
1018 goto done;
1019 }
1020
1021 /*
1022 * If the ALT key is down, and we're typing on the numeric
1023 * keypad, gather up keys for a keypad sequence. We can
1024 * tell that we're typing on the keypad by checking to see
1025 * if we're hitting Home, Up, PgUp, Left, Right, End, Down,
1026 * PgDn, Ins, or Clear, and we're not typing an extended
1027 * key.
1028 */
1029 if (alt && !ctl && !shift && !extended)
1030 {
1031 /* make sure we're typing a keypad key */
1032 *ch = xlat_keypad(keyevt->wVirtualKeyCode);
1033 if (*ch < 10)
1034 {
1035 unsigned ignore_vkey;
1036 int ansi_key_seq;
1037
1038 /*
1039 * If the first key is a '0', it indicates an ANSI code
1040 * sequence rather than an OEM code sequence, in which
1041 * case we need to translate the keystroke to the OEM
1042 * set we use internally. For now, simply note it;
1043 * we'll handle the translation later.
1044 *
1045 * This leading '0' business is a Windows convention -
1046 * it's not something we invented. Windows has the
1047 * weird problem that console windows and regular
1048 * Windows apps tend to use different character sets,
1049 * so they came up with this leading '0' as a way to
1050 * let the user indicate they want to enter an "ANSI"
1051 * character (the name for the character set that
1052 * Windows apps use, even though it's not actually
1053 * ANSI) rather than the default, which is "OEM" (the
1054 * set that DOS boxes use).
1055 */
1056 ansi_key_seq = (*ch == 0);
1057
1058 /*
1059 * If we're using the ReadConsole keyboard fix for
1060 * win95, simply read the event using ReadConsole.
1061 */
1062 if (S_os_win95_kb_fix)
1063 {
1064 /* get the event */
1065 ReadConsole(hcon, ch, 1, &reccnt, 0);
1066
1067 /* return it */
1068 status_code = OS_GETS_OK;
1069 goto done;
1070 }
1071
1072 /*
1073 * ignore repeats of this key until we see a key-up
1074 * for it
1075 */
1076 ignore_vkey = keyevt->wVirtualKeyCode;
1077
1078 /* keep going until we stop hitting keypad keys */
1079 for (;;)
1080 {
1081 unsigned char nxt;
1082
1083 /* peek at the next event */
1084 WaitForSingleObject(hcon, INFINITE);
1085 if (!PeekConsoleInput(hcon, &inrec, 1, &reccnt)
1086 || reccnt == 0)
1087 break;
1088
1089 /* if it's not a keyboard event, we're done */
1090 if (inrec.EventType != KEY_EVENT)
1091 break;
1092
1093 /* if it's a key-up for the ALT key, we're done */
1094 if (!keyevt->bKeyDown
1095 && keyevt->wVirtualKeyCode == VK_MENU)
1096 {
1097 /* done reading the key */
1098 break;
1099 }
1100
1101 /*
1102 * if it's a key-up for the 'ignore' key, we can
1103 * stop ignoring this key again
1104 */
1105 if (!keyevt->bKeyDown
1106 && keyevt->wVirtualKeyCode == ignore_vkey)
1107 ignore_vkey = 0;
1108
1109 /* if it's another key up, ignore it */
1110 if (!keyevt->bKeyDown)
1111 goto skip_event;
1112
1113 /* get the control key state */
1114 alt = ((keyevt->dwControlKeyState
1115 & (RIGHT_ALT_PRESSED
1116 | LEFT_ALT_PRESSED)) != 0);
1117 ctl = ((keyevt->dwControlKeyState
1118 & (RIGHT_CTRL_PRESSED
1119 | LEFT_CTRL_PRESSED)) != 0);
1120 shift = ((keyevt->dwControlKeyState
1121 & SHIFT_PRESSED) != 0);
1122 extended = ((keyevt->dwControlKeyState
1123 & ENHANCED_KEY) != 0);
1124
1125 /* if the shift keys have changed, we're done */
1126 if (!alt || ctl || shift || extended)
1127 break;
1128
1129 /*
1130 * if it's a key down for the same key as last
1131 * time, and we haven't gotten a key-up for this
1132 * key yet, it's an auto-repeat - ignore it
1133 */
1134 if (keyevt->wVirtualKeyCode == ignore_vkey)
1135 goto skip_event;
1136
1137 /* ignore auto-repeats of this same key */
1138 ignore_vkey = keyevt->wVirtualKeyCode;
1139
1140 /* if it's not a keypad key, we're done */
1141 nxt = xlat_keypad(keyevt->wVirtualKeyCode);
1142 if (nxt > 9)
1143 break;
1144
1145 /* add this into the accumulator */
1146 *ch *= 10;
1147 *ch += nxt;
1148
1149 skip_event:
1150 /* read and discard this event */
1151 ReadConsoleInput(hcon, &inrec, 1, &reccnt);
1152 }
1153
1154 /*
1155 * if the character was specified as an ANSI character,
1156 * translate it to the OEM character set
1157 */
1158 if (ansi_key_seq)
1159 {
1160 char buf[2];
1161
1162 /* set up a null-terminated string for translation */
1163 buf[0] = *ch;
1164 buf[1] = '\0';
1165
1166 /* translate it */
1167 CharToOem(buf, buf);
1168
1169 /*
1170 * if we didn't get a character back, then the ANSI
1171 * character must have no representation in the OEM
1172 * character set; simply ignore the character
1173 */
1174 if (buf[0] == '\0')
1175 break;
1176
1177 /* use the OEM representation */
1178 *ch = buf[0];
1179 }
1180
1181 /* return the key code */
1182 status_code = OS_GETS_OK;
1183 goto done;
1184 }
1185 }
1186
1187 /*
1188 * if none of the shift keys are down, return the key as an
1189 * extended two-byte key code
1190 */
1191 if (!alt && !shift && !ctl)
1192 {
1193 /*
1194 * Return it as a two-character code; the virtual scan
1195 * key corresponds to the traditional BIOS extended key
1196 * codes, so use the virtual scan code as the second
1197 * byte of the two-byte sequence
1198 */
1199 *ch = 0;
1200 nxt_ret = (unsigned char)keyevt->wVirtualScanCode;
1201 status_code = OS_GETS_OK;
1202 goto done;
1203 }
1204
1205 /* check for shift key combinations */
1206 if (shift && !ctl && !alt)
1207 {
1208 /* presume we'll return a two-byte sequence */
1209 *ch = 0;
1210
1211 /* see what we have */
1212 switch(keyevt->wVirtualKeyCode)
1213 {
1214 case VK_F1:
1215 case VK_F2:
1216 case VK_F3:
1217 case VK_F4:
1218 case VK_F5:
1219 case VK_F6:
1220 case VK_F7:
1221 case VK_F8:
1222 case VK_F9:
1223 case VK_F10:
1224 case VK_F11:
1225 case VK_F12:
1226 nxt_ret = 84 + keyevt->wVirtualKeyCode - VK_F1;
1227 goto done;
1228
1229 default:
1230 break;
1231 }
1232 }
1233
1234 /* check for control key combinations */
1235 if (ctl && !alt && !shift)
1236 {
1237 /* presume we'll return a two-byte sequence */
1238 *ch = 0;
1239
1240 /* see what we have */
1241 switch(keyevt->wVirtualKeyCode)
1242 {
1243 case VK_PRIOR:
1244 nxt_ret = 132;
1245 goto done;
1246
1247 case VK_NEXT:
1248 nxt_ret = 118;
1249 goto done;
1250
1251 case VK_LEFT:
1252 nxt_ret = 115;
1253 goto done;
1254
1255 case VK_RIGHT:
1256 nxt_ret = 116;
1257 goto done;
1258
1259 case VK_HOME:
1260 nxt_ret = 119;
1261 goto done;
1262
1263 case VK_END:
1264 nxt_ret = 0165;
1265 goto done;
1266
1267 default:
1268 break;
1269 }
1270 }
1271
1272 break;
1273
1274 default:
1275 /* ignore other event types */
1276 break;
1277 }
1278 }
1279
1280 done:
1281 /* restore the original console mode */
1282 SetConsoleMode(hcon, oldmode);
1283
1284 /* return the result */
1285 return status_code;
1286 }
1287
1288 /*
1289 * Get a character from the keyboard
1290 */
1291 char os_getch(void)
1292 {
1293 unsigned char ch;
1294
1295 /* get a keystroke without a timeout */
1296 if (oss_getc_internal(&ch, 0, FALSE) == OS_GETS_OK)
1297 return ch;
1298 else
1299 return EOF;
1300 }
1301
1302 /*
1303 * Get a character from the keyboard, timing out after the given
1304 * interval
1305 */
1306 int oss_getc_timeout(unsigned char *ch, unsigned long timeout)
1307 {
1308 /* get a keystroke using the given timeout */
1309 return oss_getc_internal(ch, timeout, TRUE);
1310 }
1311
1312 /* ------------------------------------------------------------------------ */
1313 /*
1314 * Sleep until the specified time. This is especially easy for Win32
1315 * console apps, since we can just call the system API to do exactly
1316 * this.
1317 */
1318 void os_sleep_ms(long delay_in_milliseconds)
1319 {
1320 /* do any deferred redrawing */
1321 osssb_redraw_if_needed();
1322
1323 /* put the cursor at the default input position while pausing */
1324 if (!S_keep_cursor_pos)
1325 osssb_cursor_to_default_pos();
1326
1327 /* sleep for the desired delay */
1328 Sleep(delay_in_milliseconds);
1329 }
1330
1331
1332 #endif /* T_WIN32 */
1333
1334 /* ------------------------------------------------------------------------ */
1335 #ifndef USE_DOOR
1336 # ifndef WINDOWS
1337 void os_waitc(void)
1338 {
1339 int c;
1340
1341 /* read a character */
1342 c = os_getch();
1343
1344 /* read the second byte if it's a two-byte sequence */
1345 if ((c == 0 || (unsigned char)c == 0340))
1346 os_getch();
1347 }
1348 # endif /* !WINDOWS */
1349 #endif /* !USE_DOOR */
1350
1351 /* ------------------------------------------------------------------------ */
1352
1353 #ifndef USE_DOOR
1354 # ifndef WINDOWS
1355 # ifndef DJGPP
1356 int os_kbhit(void)
1357 {
1358 # ifdef MSOS2
1359 KBDKEYINFO ki;
1360
1361 return( KbdPeek(&ki, (HKBD)0) == 0 && (ki.fbStatus & 0x40) );
1362 # else /* !MSOS2 */
1363 int ret = 0;
1364
1365 #ifndef __DPMI32__
1366 #ifndef T_WIN32
1367 asm {
1368 push bp
1369 push di
1370 push si
1371 push ds
1372 push es
1373
1374 mov ah, 1
1375 int 16h
1376 jz no_key_ready
1377 }
1378 ret = 1;
1379 no_key_ready:
1380 asm {
1381 pop es
1382 pop ds
1383 pop si
1384 pop di
1385 pop bp
1386 }
1387 #endif /* T_WIN32 */
1388 #endif /* __DPMI32__ */
1389 return(ret);
1390 # endif /* MSOS2 */
1391 }
1392 # endif /* !DJGPP */
1393
1394 /* ------------------------------------------------------------------------ */
1395 /*
1396 * Read a character from the keyboard
1397 */
1398
1399 #ifdef RUNTIME
1400 /* ALT+letter function keys for PC, in A to Z order */
1401 char osfar_t altkeys[] =
1402 {
1403 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38, 50,
1404 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44
1405 };
1406
1407 /*
1408 * Flag: the next keystroke to be read from the keyboard is the second
1409 * half of a two-character command key sequence. This is used by the
1410 * various high-level keyboard readers to remember that an extended
1411 * command sequence is being read, hence the second keystroke must be
1412 * interpreted as a command key.
1413 *
1414 * If this value is zero, it means that no command is pending. If this
1415 * value is non-zero, it contains the command code that should be
1416 * returned on the next call to os_getc() or other keyboard readers.
1417 */
1418 static osfar_t int oss_command_pending;
1419
1420 /*
1421 * Interpret the second character of a two-character keystroke into a
1422 * command code, and store the command in oss_command_pending.
1423 * os_getc() and other similar routines use this to figure out the
1424 * portable meaning of the second character of an extended keystroke
1425 * sequence.
1426 *
1427 * This returns one of the following values:
1428 *
1429 * OSS_ICK_NORMAL - it's a normal key; the caller should simply return
1430 * this key to its caller.
1431 *
1432 * OSS_ICK_CMD - it's a single-character BIOS keystroke, but a
1433 * two-character portable command code. We will store the command code
1434 * for later retrieval; the caller should simply return 0 to its caller.
1435 *
1436 * OSS_ICK_NEED_2ND - another keyboard character is needed to complete
1437 * the keystroke. This is returned when the first key is a BIOS-level
1438 * two-character keystroke header. The caller should call
1439 * oss_interpret_2nd_key() with the next BIOS-level character and return
1440 * 0 to its caller to indicate that a command is in progress.
1441 *
1442 * OSS_ICK_BAD - invalid command sequence; the command has no portable
1443 * interpretation, so the entire sequence should simply be ignored. The
1444 * caller should suppress the keystroke and get another key from the
1445 * keyboard.
1446 */
1447 #define OSS_ICK_NORMAL 0
1448 #define OSS_ICK_CMD 1
1449 #define OSS_ICK_NEED_2ND 2
1450 #define OSS_ICK_BAD 3
1451 static int oss_interpret_command_key(int c, int translated)
1452 {
1453 /* presume we won't find a command value */
1454 oss_command_pending = 0;
1455
1456 /* check for an extended keycode */
1457 if (c == 0)
1458 {
1459 /* extended BIOS keystroke - we need a second keystroke */
1460 return OSS_ICK_NEED_2ND;
1461 }
1462 else if (c >= 32 && c <= 255)
1463 {
1464 /* it's a normal keystroke */
1465 return OSS_ICK_NORMAL;
1466 }
1467 else if (translated)
1468 {
1469 /* check to see if it's a special single-character keystroke */
1470 switch(c)
1471 {
1472 case 1: /* ^A */
1473 oss_command_pending = CMD_HOME;
1474 break;
1475 case 2: /* ^B */
1476 oss_command_pending = CMD_LEFT;
1477 break;
1478 case 4: /* ^D */
1479 oss_command_pending = CMD_DEL;
1480 break;
1481 case 5: /* ^E */
1482 oss_command_pending = CMD_END;
1483 break;
1484 case 6: /* ^F */
1485 oss_command_pending = CMD_RIGHT;
1486 break;
1487 case '\t':
1488 oss_command_pending = CMD_TAB;
1489 break;
1490 case 11: /* ^K */
1491 oss_command_pending = CMD_DEOL;
1492 break;
1493 case 14: /* ^N */
1494 oss_command_pending = CMD_DOWN;
1495 break;
1496 case 16: /* ^P */
1497 oss_command_pending = CMD_UP;
1498 break;
1499 case 21: /* ^U */
1500 case 27: /* Escape */
1501 oss_command_pending = CMD_KILL;
1502 break;
1503
1504 case 13:
1505 case 8:
1506 /* normal keystrokes */
1507 return OSS_ICK_NORMAL;
1508 }
1509
1510 /*
1511 * if we selected a command, indicate that this is a command
1512 * key; otherwise, it's invalid
1513 */
1514 if (oss_command_pending != 0)
1515 return OSS_ICK_CMD;
1516 else
1517 return OSS_ICK_BAD;
1518 }
1519 else
1520 {
1521 /*
1522 * we want a raw keystroke, so return a control/escape key
1523 * as-is, without command mapping
1524 */
1525 return OSS_ICK_NORMAL;
1526 }
1527 }
1528
1529 /*
1530 * Interpret the second key of a two-character keystroke. When
1531 * oss_interpret_command_key() returns OSS_ICK_NEED_2ND, the caller
1532 * should get another key from the low-level keyboard handler and call
1533 * this routine with the second keystroke. We'll return:
1534 *
1535 * OSS_ICK_CMD - we've stored a command. The caller should return 0 to
1536 * indicate to its caller that a two-character command sequence is in
1537 * progress. We will store the command code in oss_command_pending for
1538 * later retrieval.
1539 *
1540 * OSS_ICK_BAD - invalid command. The caller should ignore the key and
1541 * get another.
1542 */
1543 static int oss_interpret_2nd_key(int c, int translated)
1544 {
1545 int i;
1546 char *p;
1547
1548 /* check for alt keys */
1549 for (i = 0, p = altkeys ; i < 26 ; ++p, ++i)
1550 {
1551 if (c == *p)
1552 {
1553 /* set the command */
1554 oss_command_pending = CMD_ALT + i;
1555
1556 /* indicate that we found a command */
1557 return OSS_ICK_CMD;
1558 }
1559 }
1560
1561 /* check the extended keycode */
1562 switch(c)
1563 {
1564 case 0110: /* Up Arrow */
1565 oss_command_pending = CMD_UP;
1566 break;
1567 case 0120: /* Down Arrow */
1568 oss_command_pending = CMD_DOWN;
1569 break;
1570 case 0115: /* Right Arrow */
1571 oss_command_pending = CMD_RIGHT;
1572 break;
1573 case 0113: /* Left Arrow */
1574 oss_command_pending = CMD_LEFT;
1575 break;
1576 case 0117: /* End */
1577 oss_command_pending = CMD_END;
1578 break;
1579 case 0107: /* Home */
1580 oss_command_pending = CMD_HOME;
1581 break;
1582 case 0165: /* Ctrl-End */
1583 oss_command_pending = CMD_DEOL;
1584 break;
1585 case 0123: /* Del */
1586 oss_command_pending = CMD_DEL;
1587 break;
1588 case 073: /* F1 */
1589 /*
1590 * if they want a translated keystroke, return the SCROLL
1591 * command; otherwise, just return the plain F1 key
1592 */
1593 if (translated)
1594 oss_command_pending = CMD_SCR;
1595 else
1596 oss_command_pending = CMD_F1;
1597 break;
1598 case 0111: /* PgUp */
1599 oss_command_pending = CMD_PGUP;
1600 break;
1601 case 0121: /* PgDn */
1602 oss_command_pending = CMD_PGDN;
1603 break;
1604 case 0122:
1605 oss_command_pending = CMD_INS; /* Insert */
1606 break;
1607 case 132: /* control-PgUp */
1608 oss_command_pending = CMD_TOP;
1609 break;
1610 case 118: /* control-PgDn */
1611 oss_command_pending = CMD_BOT;
1612 break;
1613 case 119: /* control home */
1614 oss_command_pending = CMD_CHOME;
1615 break;
1616 case 115: /* control left arrow */
1617 oss_command_pending = CMD_WORD_LEFT;
1618 break;
1619 case 116: /* control right arrow */
1620 oss_command_pending = CMD_WORD_RIGHT;
1621 break;
1622
1623 case 60: oss_command_pending = CMD_F2; break;
1624 case 61: oss_command_pending = CMD_F3; break;
1625 case 62: oss_command_pending = CMD_F4; break;
1626 case 63: oss_command_pending = CMD_F5; break;
1627 case 64: oss_command_pending = CMD_F6; break;
1628 case 65: oss_command_pending = CMD_F7; break;
1629 case 66: oss_command_pending = CMD_F8; break;
1630 case 67: oss_command_pending = CMD_F9; break;
1631 case 68: oss_command_pending = CMD_F10; break;
1632
1633 case 85: oss_command_pending = CMD_SF2; break; /* shifted F2 */
1634
1635 default: /* Unrecognized function key */
1636 oss_command_pending = 0;
1637 break;
1638 }
1639
1640 /*
1641 * if we set a command code, indicate to the caller that we found a
1642 * command; otherwise, it's not a valid keystroke
1643 */
1644 if (oss_command_pending != 0)
1645 return OSS_ICK_CMD;
1646 else
1647 return OSS_ICK_BAD;
1648 }
1649
1650 /*
1651 * Return the pending command, clearing the pending command flag in the
1652 * process. os_getc and similar keyboard readers should check
1653 * oss_command_pending, and if that value is non-zero simply return the
1654 * result of calling this function.
1655 */
1656 int oss_get_pending_command()
1657 {
1658 int ret;
1659
1660 /* remember the value to return */
1661 ret = oss_command_pending;
1662
1663 /* clear the pending command, now that we've read it */
1664 oss_command_pending = 0;
1665
1666 /* return the command */
1667 return ret;
1668 }
1669
1670 /*
1671 * Get a key from the keyboard. If translated is true, we'll return the
1672 * high-level CMD_xxx code for the key if it has both a translated and
1673 * raw equivalent; otherwise, we'll return the raw CMD_xxx code for the
1674 * key.
1675 */
1676 static int os_getc_internal(unsigned char *ch,
1677 unsigned long timeout, int use_timeout,
1678 int translated)
1679 {
1680 int status;
1681
1682 /* do any pending redraw */
1683 osssb_redraw_if_needed();
1684
1685 /* put the cursor at the default input position */
1686 if (!S_keep_cursor_pos)
1687 osssb_cursor_to_default_pos();
1688
1689 /* if we have a command stored up from last time, read it now */
1690 if (oss_command_pending != 0)
1691 {
1692 *ch = oss_get_pending_command();
1693 return OS_GETS_OK;
1694 }
1695
1696 /* keep going until we get something good */
1697 for (;;)
1698 {
1699 /* get a key */
1700 status = oss_getc_internal(ch, timeout, use_timeout);
1701
1702 /* check the status */
1703 switch(status)
1704 {
1705 case OS_GETS_OK:
1706 /* got a key - process it normally */
1707 break;
1708
1709 default:
1710 /* anything else is an error - return it */
1711 return status;
1712 }
1713
1714 /* check to see if it's a command sequence */
1715 switch(oss_interpret_command_key(*ch, translated))
1716 {
1717 case OSS_ICK_BAD:
1718 /* invalid key - ignore it and get another */
1719 break;
1720
1721 case OSS_ICK_NORMAL:
1722 /* it's a normal key - just return it */
1723 return OS_GETS_OK;
1724
1725 case OSS_ICK_CMD:
1726 /*
1727 * it's a single-key command - return zero to indicate to
1728 * the caller that they must ask for a second key
1729 */
1730 *ch = 0;
1731 return OS_GETS_OK;
1732
1733 case OSS_ICK_NEED_2ND:
1734 /*
1735 * We need a second character code. Since the second
1736 * character is really part of the same keystroke event,
1737 * there should be no delay in getting it, so don't bother
1738 * using a timeout.
1739 */
1740 oss_getc_internal(ch, 0, FALSE);
1741 switch(oss_interpret_2nd_key(*ch, translated))
1742 {
1743 case OSS_ICK_BAD:
1744 /* invalid command - ignore it and get another keystroke */
1745 break;
1746
1747 case OSS_ICK_CMD:
1748 /*
1749 * it's a command sequence - return zero to indicate to
1750 * the caller that they must ask for a second key
1751 */
1752 *ch = 0;
1753 return OS_GETS_OK;
1754 }
1755 }
1756 }
1757 }
1758
1759 /*
1760 * Get a keystroke with a time limit
1761 */
1762 int os_getc_timeout(unsigned char *ch, unsigned long timeout)
1763 {
1764 /* call the internal routine, using the timeout */
1765 return os_getc_internal(ch, timeout, TRUE, TRUE);
1766 }
1767
1768 /*
1769 * Get a keystroke from the keyboard
1770 */
1771 int os_getc(void)
1772 {
1773 unsigned char ch;
1774
1775 /* call the internal routine, with no timeout */
1776 os_getc_internal(&ch, 0, FALSE, TRUE);
1777
1778 /* return the character we read */
1779 return ch;
1780 }
1781
1782 /*
1783 * Get a raw keystroke from the keyboard
1784 */
1785 int os_getc_raw(void)
1786 {
1787 unsigned char ch;
1788
1789 /* call the internal routine, with no timeout */
1790 os_getc_internal(&ch, 0, FALSE, FALSE);
1791
1792 /* return the character we read */
1793 return ch;
1794 }
1795
1796 /*
1797 * Get an event. The only types of events we provide currently are KEY
1798 * and TIMEOUT events.
1799 */
1800 int os_get_event(unsigned long timeout_in_milliseconds, int use_timeout,
1801 os_event_info_t *info)
1802 {
1803 int kbstat;
1804 unsigned char ch;
1805
1806 /* get a key, using the timeout if specified */
1807 kbstat = os_getc_internal(&ch, timeout_in_milliseconds, use_timeout,
1808 FALSE);
1809
1810 /* if that timed out, return a timeout event */
1811 if (kbstat == OS_GETS_TIMEOUT)
1812 return OS_EVT_TIMEOUT;
1813
1814 /* if we got a key, return it */
1815 if (kbstat == OS_GETS_OK)
1816 {
1817 /* store the first character */
1818 info->key[0] = (int)ch;
1819
1820 /*
1821 * if there's an extended character code coming, get it - note
1822 * that we don't need to bother with a timeout this time around,
1823 * since the second half of the key code is not a separate event
1824 * and is thus not subject to timing out
1825 */
1826 if (ch == 0)
1827 info->key[1] = os_getc();
1828
1829 /* return the keyboard event */
1830 return OS_EVT_KEY;
1831 }
1832
1833 /* failure - return an error indication */
1834 return OS_EVT_EOF;
1835 }
1836
1837
1838 #else /* RUNTIME */
1839
1840 /*
1841 * initialization
1842 */
1843 int os_init(int *argc, char **argv, const char *prompt, char *buf, int bufsiz)
1844 {
1845 return 0;
1846 }
1847
1848 /*
1849 * uninitialization
1850 */
1851 void os_uninit()
1852 {
1853 }
1854
1855 /*
1856 * Get a key from the keyboard - simple non-RUNTIME version
1857 */
1858 int os_getc(void)
1859 {
1860 int c;
1861
1862 /* keep going until we get a valid key */
1863 for (;;)
1864 {
1865 /* get a key */
1866 c = os_getch();
1867
1868 /* if it's valid, return it */
1869 if ((c & 0xff) != 0)
1870 return c;
1871 }
1872 }
1873
1874 /*
1875 * Get a raw (untranslated) key from the keyboard - simple non-RUNTIME
1876 * version
1877 */
1878 int os_getc_raw(void)
1879 {
1880 /* os_getc() doesn't perform any translation, so just call it */
1881 return os_getc();
1882 }
1883
1884 #endif /* RUNTIME */
1885
1886 # endif /* !WINDOWS */
1887 #endif /* !USE_DOOR */
1888
1889 extern unsigned long ossgetdisks(void);
1890 # ifdef RUNTIME
1891 # if (!defined(WINDOWS) && !defined(_Windows)) || defined(__DPMI16__)
1892 # if defined(TURBO) || defined(DJGPP)
1893 /*
1894 * implementations of library functions for directory operations -
1895 * these may be Turbo-specific, so they're ifdef'd TURBO
1896 */
1897
1898 /* maximum length of a fully-qualified filename */
1899 # define OSS_MAXPATH MAXPATH
1900
1901 /* get current working directory into buffer */
1902 # ifdef TURBO
1903 # define osspwd(buf, buflen) ((void)getcwd(buf, buflen))
1904 # endif
1905
1906 /* change directory */
1907 # define osscd(path) ((void)chdir(path))
1908
1909 /* set disk: 0 = A:, 1 = B:, 2 = C:, etc */
1910 # define osssetdisk(drivenum) ((void)setdisk(drivenum))
1911
1912 /* get disk: 0 = A:, 1 = B:, 2 = C:, etc */
1913 # define ossgetdisk() getdisk()
1914
1915 /* size of a file entry, in bytes */
1916 # define OSSFNAMELEN 14
1917
1918 # ifdef DJGPP
1919 /*
1920 * get current working directory into buffer replace all '/' with '\\'
1921 */
1922 static void osspwd(char *buf, int buflen)
1923 {
1924 char *p;
1925
1926 getcwd(buf, buflen);
1927 for (p = buf; *p; p++)
1928 if (*p == '/') *p = '\\';
1929 }
1930 # endif /* DJGPP */
1931 # endif /* TURBO || DJGPP */
1932
1933 # ifdef MSOS2
1934 /*
1935 * implementations of library functions for directory operations -
1936 * these are OS/2-specific, so they're ifdef'd MSOS2
1937 */
1938
1939 /* maximum length of a fully-qualified filename */
1940 # define OSS_MAXPATH 128
1941
1942 /* get current working directory into buffer */
1943 # define osspwd(buf, buflen) ((void)getcwd(buf, buflen))
1944 /* context structure used for file searches */
1945 #ifdef __32BIT__
1946 # ifndef __IBMC__
1947 # define DosSelectDisk(a) DosSetDefaultDisk(a)
1948 # define DosQCurDisk(a,b) DosQueryCurrentDisk(a,b)
1949 # endif /* !__IBMC__ */
1950 typedef ULONG drive_t;
1951 #else /* !__32BIT__ */
1952 # define DosFindFirst(a,b,c,d,e,f) DosFindFirst(a,b,c,d,e,f,0L)
1953 typedef USHORT drive_t;
1954 #endif /* __32BIT__ */
1955
1956 /* change directory */
1957 # define osscd(path) ((void)chdir(path))
1958
1959 /* set disk: 0 = A:, 1 = B:, 2 = C:, etc */
1960 static void osssetdisk(int drv)
1961 {
1962 DosSelectDisk((drive_t)(drv+1));
1963 }
1964
1965 /* get disk: 0 = A:, 1 = B:, 2 = C:, etc */
1966 static int ossgetdisk(void)
1967 {
1968 drive_t drv;
1969 ULONG map; /* dummy */
1970
1971 return( DosQCurDisk(&drv, &map) ? -1 : (int)(drv-1) );
1972 }
1973
1974 /* size of a file entry, in bytes */
1975 # define OSSFNAMELEN 14
1976
1977 # else /* MSOS2 */
1978 # ifdef MICROSOFT
1979
1980 # ifdef T_WIN32
1981
1982 #include <io.h>
1983 #include <direct.h>
1984
1985 #define OSS_MAXPATH _MAX_PATH
1986 #define OSSFNAMELEN _MAX_FNAME
1987 #define osspwd(buf, buflen) ((void)_getcwd(buf, buflen))
1988
1989 #define ossgetdisk() (_getdrive() - 1)
1990 #define osssetdisk(disk) _chdrive((disk) + 1)
1991 #define osscd(path) _chdir(path)
1992
1993 # else /* T_WIN32 */
1994
1995 /* definitions for Microsoft C on DOS */
1996 # define OSS_MAXPATH 128
1997
1998 /* get current working directory into buffer */
1999 # define osspwd(buf, buflen) ((void)getcwd(buf, buflen))
2000
2001 /* change directory */
2002 # define osscd(path) ((void)chdir(path))
2003
2004 static int ossgetdisk(void)
2005 {
2006 unsigned int drive;
2007 _dos_getdrive(&drive);
2008 return(drive + 1);
2009 }
2010
2011 static void osssetdisk(int drivenum)
2012 {
2013 unsigned int maxdrives;
2014 _dos_setdrive(drivenum + 1, &maxdrives);
2015 }
2016
2017 /* size of a file entry, in bytes */
2018 # define OSSFNAMELEN 14
2019
2020 # endif /* T_WIN32 */
2021 # endif /* MICROSOFT */
2022 # endif /* MSOS2 */
2023
2024 /* advance by one file in a file list */
2025 static char *fwd_file(char *cur)
2026 {
2027 return(cur + OSSFNAMELEN);
2028 }
2029
2030 /* back up by one file in a null-terminated list */
2031 static char *backup_file(char *cur, char *first)
2032 {
2033 if (cur == first)
2034 return cur;
2035 else
2036 return cur - OSSFNAMELEN;
2037 }
2038
2039 /* filename comparison routine, for directory sorter */
2040 static int fnamecmp(const void *a0, const void *b0)
2041 {
2042 char *a = (char *)a0;
2043 char *b = (char *)b0;
2044 int atyp, btyp;
2045 char c;
2046
2047 /*
2048 * Get the type of the first name. If it ends with a colon, it's a
2049 * drive letter, so it's of type 0; if it ends with a backslash, it's
2050 * a subdirectory, so it's of type 1; otherwise, it's an ordinary
2051 * filename, of type 2.
2052 */
2053 c = a[strlen(a) - 1];
2054 atyp = (c == ':' ? 0 : c == '\\' ? 1 : 2);
2055
2056 /* get hte type of the second name */
2057 c = b[strlen(b) - 1];
2058 btyp = (c == ':' ? 0 : c == '\\' ? 1 : 2);
2059
2060 /*
2061 * Broadly, we sort disk names first, then directory names, then
2062 * filenames. So, if we have different types of objects, simply sort
2063 * by group.
2064 */
2065 if (atyp != btyp)
2066 return atyp - btyp;
2067
2068 /*
2069 * if they're directories, put the special "." and ".." entries first,
2070 * even if other directories sort lexically before them
2071 */
2072 if (atyp == 1)
2073 {
2074 if (a[0] == '.' && b[0] != '.')
2075 return -1;
2076 else if (a[0] != '.' && b[0] == '.')
2077 return 1;
2078 }
2079
2080 /* they're of the same type, so sort lexically by the string */
2081 return stricmp(a, b);
2082 }
2083
2084 /*
2085 * This version is not applicable to Windows -- see oswinask.c
2086 */
2087 int os_askfile(const char *prompt, char *reply, int replen,
2088 int prompt_type, os_filetype_t file_type)
2089 {
2090 static osfar_t char *curdir; /* current directory we're using */
2091 static osfar_t int curdirl;
2092 static osfar_t char curdisk; /* current disk we're using */
2093 static osfar_t int gotdir; /* flag: we've read pwd */
2094 static osfar_t char *namebuf; /* space for filenames (big enough???) */
2095 static osfar_t int namebufl;
2096 int old_keep_cursor_pos;
2097 int rem; /* remaining space in namebuf */
2098 int newfile; /* TRUE ==> creating a new file */
2099 char lbuf[80]; /* line buffer */
2100 char *p;
2101 char *curpage; /* pointer to file at top of page */
2102 char *curfile; /* pointer to current file */
2103 int curofs; /* offset of current line on page */
2104 int pageofs; /* offset of page */
2105 int filecnt; /* number of files in the current list */
2106 int ent_x, ent_y; /* x/y location of filename entry */
2107 int min_ent_x; /* first position of filename */
2108 char *newp; /* pointer to new filename entry location */
2109 int insel; /* TRUE: using selector; FALSE: entering name */
2110 int lastofs; /* offset in list of lastfile */
2111 int retval;
2112 int bottom;
2113 int instruct_y;
2114 int list_top, list_bottom;
2115 int top;
2116 int left;
2117 int right;
2118 int y;
2119 int i;
2120 static osfar_t char *lastfile;
2121 static osfar_t char filebuf[256];
2122 char runpwd[OSS_MAXPATH];
2123 char rundisk;
2124 osdirhdl_t dirhdl;
2125 unsigned long fmode;
2126 unsigned long fattr;
2127 char *ext;
2128 size_t extlen;
2129 unsigned long drives;
2130 static osfar_t char *ext_list[] =
2131 {
2132 ".GAM", /* OSFTGAME */
2133 ".SAV", /* OSFTSAVE */
2134 ".LOG", /* OSFTLOG */
2135 ".DAT", /* OSFTSWAP */
2136 ".DAT", /* OSFTDATA */
2137 ".CMD", /* OSFTCMD */
2138 ".MSG", /* OSFTERRS */
2139 ".TXT", /* OSFTTEXT */
2140 ".DAT", /* OSFTBIN */
2141 ".TCP", /* OSFTCMAP */
2142 ".DAT", /* OSFTPREF */
2143 "", /* OSFTUNK */
2144 ".t3", /* was t3x */ /* OSFTT3IMG */
2145 ".t3o", /* OSFTT3OBJ */
2146 ".t3s", /* OSFTT3SYM */
2147 ".t3v" /* OSFTT3SAV */
2148 };
2149
2150 /* do any deferred redrawing */
2151 osssb_redraw_if_needed();
2152
2153 /* allocate the buffers if we haven't already done so */
2154 if (namebuf == 0)
2155 {
2156 namebuf = (char *)calloc(namebufl = (256 * OSSFNAMELEN), 1);
2157 curdir = (char *)calloc(curdirl = OSS_MAXPATH, 1);
2158 lastfile = (char *)calloc(OSS_MAXPATH, 1);
2159 }
2160
2161 /* simple version if in 'plain' mode */
2162 if (!namebuf || !curdir || !lastfile || os_f_plain || use_std_askfile)
2163 {
2164 /* show the prompt, and get a reply */
2165 os_printz(prompt);
2166 os_printz(" >");
2167 os_gets((uchar *)reply, replen);
2168
2169 /*
2170 * return 'cancel' if they entered a blank line, otherwise
2171 * success
2172 */
2173 return (prompt[0] == '\0' ? OS_AFE_CANCEL : OS_AFE_SUCCESS);
2174 }
2175
2176 /* we drive the cursor position from now on */
2177 old_keep_cursor_pos = S_keep_cursor_pos;
2178 S_keep_cursor_pos = TRUE;
2179
2180 /* determine if we're writing or reading (different operations) */
2181 newfile = (prompt_type == OS_AFP_SAVE);
2182 insel = !newfile;
2183
2184 /*
2185 * start with the most recently selected file, unless it was a
2186 * directory or drive letter
2187 */
2188 filebuf[0] = '\0';
2189 if (lastfile[0] != '\0')
2190 {
2191 char c = lastfile[strlen(lastfile) - 1];
2192
2193 if (c != ':' && c != '\\')
2194 strcpy(filebuf, lastfile);
2195 }
2196
2197 /* choose our extension filter based on the file type */
2198 if (file_type != OSFTUNK
2199 && (int)file_type >= 0
2200 && (int)file_type < sizeof(ext_list)/sizeof(ext_list[0]))
2201 {
2202 ext = ext_list[(int)file_type];
2203 extlen = strlen(ext);
2204 }
2205 else
2206 {
2207 ext = 0;
2208 extlen = 0;
2209 }
2210
2211 /* remember the drive upon entry */
2212 rundisk = ossgetdisk();
2213
2214 /* if we haven't set directory, read current one */
2215 if (!gotdir)
2216 {
2217 osspwd(curdir, curdirl);
2218 curdisk = ossgetdisk();
2219 gotdir = 1;
2220 }
2221 else
2222 osssetdisk(curdisk);
2223
2224 /*
2225 * display the dialog box's outline
2226 */
2227
2228 /* clear the box's screen area */
2229 top = 3;
2230 bottom = G_oss_screen_height - 4;
2231 left = 10;
2232 right = G_oss_screen_width - 11;
2233 ossclr(top, left, bottom, right, ldesc_color);
2234
2235 /* display the line across the top */
2236 lbuf[0] = (char)218;
2237 for (p = lbuf+1, i = right - left - 1 ; i ; --i)
2238 *p++ = (uchar)196;
2239 *p++ = (char)191;
2240 *p++ = (char)0;
2241 ossdsp(top, left, ldesc_color, lbuf);
2242
2243 /* figure out where the instructions go */
2244 instruct_y = bottom - 1;
2245
2246 /* if we're creating a file, we need an extra two lines */
2247 if (newfile)
2248 instruct_y -= 2;
2249
2250 /*
2251 * if we don't have drive letters available, we need a line for drive
2252 * letter ALT-X-style selection instructions
2253 */
2254 if (ossgetdisks() == 0)
2255 instruct_y -= 1;
2256
2257 /* display the left and right side lines in the file list */
2258 lbuf[0] = (char)179;
2259 for (p = lbuf+1, i = right - left - 3 ; i ; --i)
2260 *p++ = ' ';
2261 *p++ = (char)179;
2262 *p++ = ' ';
2263 *p++ = (char)179;
2264 *p++ = (char)0;
2265 for (y = top+3 ; y < instruct_y - 1 ; ++y)
2266 ossdsp(y, left, ldesc_color, lbuf);
2267
2268 /* add the lines in the current directory and instruction areas */
2269 *(p - 4) = ' ';
2270 ossdsp(top + 1, left, ldesc_color, lbuf);
2271 for (y = instruct_y ; y < bottom ; ++y)
2272 ossdsp(y, left, ldesc_color, lbuf);
2273
2274 /* display the line under the current directory */
2275 lbuf[0] = (char)195;
2276 for (p = lbuf+1, i = right - left - 3 ; i ; --i)
2277 *p++ = (char)196;
2278 *p++ = (char)194;
2279 *p++ = (char)196;
2280 *p++ = (char)180;
2281 *p++ = (char)0;
2282 ossdsp(top+2, left, ldesc_color, lbuf);
2283
2284 /* display the same line above the instructions */
2285 *(p - 4) = (char)193;
2286 ossdsp(instruct_y - 1, left, ldesc_color, lbuf);
2287
2288 /* display the bottom line */
2289 lbuf[0] = (char)192;
2290 for (p = lbuf+1, i = right - left - 1 ; i ; --i)
2291 *p++ = (char)196;
2292 *p++ = (char)217;
2293 *p++ = (char)0;
2294 ossdsp(bottom, left, ldesc_color, lbuf);
2295
2296 /* display the filename field if appropriate */
2297 y = instruct_y;
2298 if (newfile)
2299 {
2300 /* add the new filename field */
2301 ossdsp(y, left + 1, ldesc_color, "Filename:");
2302
2303 /* note the location of the filename field */
2304 ent_y = y;
2305 ent_x = min_ent_x = left + 1 + 11;
2306
2307 /* add a blank line after the filename field */
2308 y += 2;
2309 }
2310
2311 /* add the main instructions */
2312 ossdsp(y, left + 1, ldesc_color,
2313 newfile
2314 ? "<Return>=OK, <Esc>=Cancel, <Tab>=List/Filename"
2315 : "<Return>=OK, <Esc>=Cancel");
2316 ++y;
2317
2318 /*
2319 * add documentation on the ALT-X drive selection method, if we didn't
2320 * add drive letters to the main list
2321 */
2322 if (ossgetdisks() == 0)
2323 ossdsp(y, left + 1, ldesc_color,
2324 "<Alt+A>=go to drive A:, <Alt+B>=B:, etc.");
2325
2326 /* calculate the file list's top and bottom lines */
2327 list_top = top + 3;
2328 list_bottom = instruct_y - 2;
2329
2330 reread_directory:
2331 /* save current directory and cd to save directory before reading */
2332 osspwd(runpwd, sizeof(runpwd));
2333 osscd(curdir);
2334
2335 /* read the files in the directory matching the desired extension */
2336 p = namebuf;
2337 rem = namebufl - 2; /* minus 2 to make room for extra nulls */
2338 filecnt = 0;
2339 lastofs = -1; /* assume the last file is not found anywhere */
2340 if (os_open_dir(curdir, &dirhdl))
2341 {
2342 while (rem >= OSSFNAMELEN + 2 && os_read_dir(dirhdl, p, rem))
2343 {
2344 /* only include non-hidden files with the target extension */
2345 size_t len = strlen(p);
2346 if (osfmode(p, TRUE, &fmode, &fattr)
2347 && (fmode & OSFMODE_FILE) != 0
2348 && (fattr & (OSFATTR_HIDDEN | OSFATTR_SYSTEM)) == 0
2349 && (ext == 0
2350 || (len >= extlen
2351 && memicmp(p + len - extlen, ext, extlen) == 0)))
2352 {
2353 ++filecnt;
2354 rem -= /*l*/ OSSFNAMELEN;
2355 p += /*l*/ OSSFNAMELEN;
2356 }
2357 }
2358 os_close_dir(dirhdl);
2359 }
2360
2361 /* now do another pass for just the subdirectories */
2362 if (os_open_dir(curdir, &dirhdl))
2363 {
2364 while (rem >= OSSFNAMELEN + 2 && os_read_dir(dirhdl, p, rem))
2365 {
2366 /* only include non-hidden directories, but omit '.' */
2367 if (osfmode(p, TRUE, &fmode, &fattr)
2368 && (fmode & OSFMODE_DIR) != 0
2369 && (fattr & (OSFATTR_HIDDEN | OSFATTR_SYSTEM)) == 0
2370 && strcmp(p, ".") != 0)
2371 {
2372 char *pend;
2373
2374 ++filecnt;
2375 pend = p + strlen(p);
2376 *pend++ = '\\';
2377 *pend++ = '\0';
2378 rem -= /*l*/ OSSFNAMELEN;
2379 p += /*l*/ OSSFNAMELEN;
2380 }
2381 }
2382 os_close_dir(dirhdl);
2383 }
2384
2385 /* add drive letters if possible */
2386 if ((drives = ossgetdisks()) != 0)
2387 {
2388 int bit;
2389 unsigned long mask;
2390 char drive_letter;
2391
2392 /* scan for available drives */
2393 for (bit = 0, mask = 1, drive_letter = 'A' ; bit < 32 ;
2394 ++bit, ++drive_letter, mask <<= 1)
2395 {
2396 /* if this bit is set, add this drive to the list */
2397 if ((drives & mask) != 0 && rem >= OSSFNAMELEN + 2)
2398 {
2399 char *pend = p;
2400
2401 /* add the drive letter and a colon */
2402 *pend++ = drive_letter;
2403 *pend++ = ':';
2404 *pend++ = '\0';
2405 rem -= OSSFNAMELEN;
2406 p += OSSFNAMELEN;
2407
2408 /* count it */
2409 ++filecnt;
2410 }
2411 }
2412 }
2413
2414 /* terminate the list with a pair of null bytes */
2415 *p++ = '\0';
2416 *p++ = '\0';
2417
2418 /* go back to correct directory */
2419 osscd(runpwd);
2420
2421 /* now sort the directory list */
2422 qsort(namebuf, (size_t)filecnt, (size_t)OSSFNAMELEN, fnamecmp);
2423
2424 /* clear out interior of box (in case re-reading directory) */
2425 ossclr(list_top, left+1, list_bottom, right - 3, ldesc_color);
2426
2427 /* clear out the current directory area */
2428 ossclr(top+1, left+1, top+1, right-1, ldesc_color);
2429
2430 /* display current directory at top line */
2431 ossdsp(top+1, left+1, ldesc_color, curdir);
2432
2433 /* set up entry area for new filename if creating a file */
2434 if (newfile)
2435 {
2436 /* clear out the area */
2437 ossclr(ent_y, ent_x, ent_y, right - 2, text_color);
2438
2439 /* show the filename */
2440 ossdsp(ent_y, ent_x, text_color, filebuf);
2441
2442 /* initialize the editing pointer */
2443 newp = filebuf;
2444 }
2445
2446 /* look for previous file in list (so we can default it) */
2447 for (i = 0, p = namebuf ; i < filecnt ; p = fwd_file(p), ++i)
2448 {
2449 /* if this one matches the previous filename, note it */
2450 if (strcmp(lastfile, p) == 0)
2451 lastofs = i;
2452 }
2453
2454 /*
2455 * if we didn't find a previous entry, initially select the first
2456 * thing after the '..' entry, if it's present, or after the drive
2457 * letter list, if they're present
2458 */
2459 if (lastofs == -1)
2460 {
2461 /* scan the list */
2462 for (i = 0, p = namebuf ; i < filecnt ; p = fwd_file(p), ++i)
2463 {
2464 char c;
2465
2466 /*
2467 * if this is a drive letter entry or the special parent
2468 * directory "..", make sure we skip this entry, if there's a
2469 * next entry
2470 */
2471 c = p[strlen(p) - 1];
2472 if ((c == ':' || (p[0] == '.' && p[1] == '.' && p[2] == '\\'))
2473 && i + 1 < filecnt)
2474 {
2475 /* use the next entry */
2476 lastofs = i + 1;
2477 }
2478 }
2479 }
2480
2481 /* default the previous file, if it is in the list */
2482 curpage = curfile = namebuf;
2483 curofs = 0;
2484 pageofs = 0;
2485 if (lastofs != -1)
2486 {
2487 for (i = 0 ; i < lastofs ; ++i)
2488 {
2489 curfile = fwd_file(curfile);
2490 if (curofs >= list_bottom - list_top)
2491 {
2492 ++pageofs;
2493 curpage = fwd_file(curpage);
2494 }
2495 else
2496 ++curofs;
2497 }
2498 }
2499 lastofs = -1; /* default the first time through only */
2500
2501 /* display as many files as will fit */
2502 for (p = curpage, y = list_top ;
2503 *p != '\0' && y <= list_bottom && filecnt != 0 ; ++y)
2504 {
2505 ossdsp(y, left+2, ldesc_color, p);
2506 p = fwd_file(p);
2507 }
2508
2509 /* read keyboard until we're done */
2510 for (;;)
2511 {
2512 unsigned char c;
2513 int thumb_y;
2514
2515 if (filecnt - (list_bottom - list_top + 1) > 0)
2516 thumb_y = (pageofs * (list_bottom - list_top)) /
2517 (filecnt - (list_bottom - list_top));
2518 else
2519 thumb_y = 0;
2520
2521 /* highlight current file, get char, unhighlight it */
2522 if (insel)
2523 {
2524 ossdsp(list_top + curofs, left + 2, text_color, curfile);
2525 ossdsp(list_top + thumb_y, right - 1, text_color, "\260");
2526 ossdsp(list_top + thumb_y + 1, right - 1, text_color, "\260");
2527 }
2528
2529 /* locate cursor in entry area, if applicable */
2530 if (newfile && !insel)
2531 ossloc(ent_y, ent_x);
2532 else
2533 ossloc(list_top + curofs, left + 2);
2534
2535 /* get the character */
2536 c = os_getc();
2537
2538 if (insel)
2539 {
2540 ossdsp(list_top + curofs, left+2, ldesc_color, curfile);
2541 ossdsp(list_top + thumb_y, right - 1, ldesc_color, " ");
2542 ossdsp(list_top + thumb_y + 1, right - 1, ldesc_color, " ");
2543 }
2544
2545 /* check for an extended key code */
2546 if (c == '\0')
2547 {
2548 /* it's an extended key - get the extended key code */
2549 c = os_getc();
2550
2551 /*
2552 * check for drive-setting ALT keys (but only if we're using
2553 * the ALT-X method to select drives, which we use only if we
2554 * don't have a list of drive letters available in the main
2555 * file list)
2556 */
2557 if (drives == 0 && c >= CMD_ALT)
2558 {
2559 /* try setting drive, and continue if successful */
2560 osssetdisk(c - CMD_ALT);
2561 if (ossgetdisk() == c - CMD_ALT)
2562 {
2563 curdisk = ossgetdisk();
2564 osspwd(curdir, curdirl);
2565 goto reread_directory;
2566 }
2567 }
2568
2569 /* check what we have */
2570 switch(c)
2571 {
2572 case CMD_UP:
2573 /* if we're at the top of the list, we can't go up */
2574 if (curofs == 0 && pageofs == 0)
2575 break;
2576
2577 /* check where we are relative to the top of the screen */
2578 if (curofs == 0)
2579 {
2580 /* top of the screen - scroll the list */
2581 --pageofs;
2582 curpage = backup_file(curpage, namebuf);
2583 ossscu(list_top, left + 1, list_bottom, right - 3,
2584 ldesc_color);
2585 }
2586 else
2587 {
2588 /* move up a line */
2589 --curofs;
2590 }
2591
2592 /* find preceding null, then the one before that */
2593 curfile = backup_file(curfile, namebuf);
2594 break;
2595
2596 case CMD_DOWN:
2597 if (!*fwd_file(curfile)) break;
2598 if (curofs >= list_bottom - list_top)
2599 {
2600 ++pageofs;
2601 curpage = fwd_file(curpage);
2602 ossscr(list_top, left + 1,
2603 list_bottom, right - 3, ldesc_color);
2604 }
2605 else
2606 ++curofs;
2607
2608 curfile = fwd_file(curfile);
2609 break;
2610
2611 case CMD_KILL:
2612 /* cancel the dialog */
2613 retval = OS_AFE_CANCEL;
2614 goto done;
2615
2616 case CMD_EOF:
2617 retval = OS_AFE_CANCEL;
2618 goto done;
2619
2620 case CMD_TAB:
2621 if (!newfile) break;
2622 insel = !insel;
2623 break;
2624
2625 default:
2626 break;
2627 }
2628 }
2629 else if (c == '\r')
2630 {
2631 /* select current entry - if it's a directory, open it */
2632 strcpy(reply, curfile);
2633 if (insel && strlen(reply) && reply[strlen(reply) - 1] == '\\')
2634 {
2635 char *root;
2636
2637 /* set up correct directory for relative cd */
2638 osspwd(runpwd, sizeof(runpwd));
2639 osscd(curdir);
2640
2641 /* get the root name of the current directory */
2642 root = os_get_root_name(curdir);
2643
2644 /* add a trailing backslash to the current directory */
2645 if (strlen(curdir) && curdir[strlen(curdir)-1] != '\\')
2646 strcat(curdir, "\\");
2647 reply[strlen(reply) - 1] = '\0';
2648
2649 /*
2650 * If we're switching up to the parent directory, remember
2651 * the current directory as the last selected file - this
2652 * will initially select this directory in the parent list.
2653 *
2654 * If we're switching down to a subdirectory, remember
2655 * ".." as the last selected file.
2656 */
2657 if (strcmp(curfile, "..\\") == 0)
2658 {
2659 /* to parent - remember the current directory */
2660 strcpy(lastfile, root);
2661 }
2662 else
2663 {
2664 /* to child - remember the parent */
2665 strcpy(lastfile, "..\\");
2666 }
2667
2668 /* do relative cd and fetch new full path */
2669 osscd(reply);
2670 osspwd(curdir, curdirl);
2671
2672 /* back to original directory, then reread directory */
2673 osscd(runpwd);
2674 goto reread_directory;
2675 }
2676
2677 /* check to see if we're selecting a drive letter */
2678 if (insel && strlen(reply) && reply[strlen(reply) - 1] == ':')
2679 {
2680 /* try setting the drive */
2681 osssetdisk(reply[0] - 'A');
2682 if (ossgetdisk() == reply[0] - 'A')
2683 {
2684 curdisk = ossgetdisk();
2685 osspwd(curdir, curdirl);
2686 goto reread_directory;
2687 }
2688 }
2689
2690 /* if creating, and not in entry, copy file into entry */
2691 if (newfile && insel)
2692 {
2693 newp = filebuf;
2694 strcpy(filebuf, reply);
2695 ent_x = min_ent_x;
2696 insel = 0;
2697 ossclr(ent_y, ent_x, ent_y, right - 2, text_color);
2698 ossdsp(ent_y, ent_x, text_color, filebuf);
2699 }
2700 else
2701 {
2702 int l;
2703
2704 strcpy(reply, curdir);
2705 l = strlen(reply);
2706 if (!l || reply[l-1] != '\\') reply[l++] = '\\';
2707 strcpy(reply+l, newfile ? filebuf : curfile);
2708 retval = OS_AFE_SUCCESS; /* successfully selected a file */
2709 strcpy(lastfile, filebuf); /* remember file for next time */
2710 if (!strchr(lastfile, '.'))
2711 strcat(lastfile, ext);
2712 if (!strchr(reply + l, '.'))
2713 strcat(reply, ext);
2714
2715 /* if the filename is empty, treat is as cancellation */
2716 if (!lastfile[0])
2717 retval = OS_AFE_CANCEL;
2718
2719 /* that's it */
2720 goto done;
2721 }
2722 }
2723 else if (c >= ' ' && c < 127
2724 && !insel
2725 && newp - filebuf < sizeof(filebuf) - 2
2726 && newp - filebuf < replen - 2
2727 && ent_x < right - 2)
2728 {
2729 /* ordinary character - clear any existing text in the field */
2730 if (*newp != '\0')
2731 {
2732 ossclr(ent_y, min_ent_x, ent_y, right - 2, text_color);
2733 *newp = '\0';
2734 }
2735
2736 /* add the character */
2737 *newp++ = c;
2738 *newp = '\0';
2739 ossdsp(ent_y, ent_x, text_color, newp-1);
2740 ++ent_x;
2741 }
2742 else if (c == 8 && ent_x > min_ent_x)
2743 {
2744 /* backspace - delete the previous character */
2745 --ent_x;
2746 ossdsp(ent_y, ent_x, text_color, " ");
2747 *(--newp) = '\0';
2748 }
2749 else if (c == 9 && newfile)
2750 {
2751 /* tab - switch to the other field */
2752 insel = !insel;
2753 }
2754 }
2755
2756 done:
2757 /* cd back to entry disk */
2758 osssetdisk(rundisk);
2759
2760 /* now erase the file selector, redraw, and we're done */
2761 ossclr(top, left, bottom, right, ldesc_color);
2762 os_redraw();
2763
2764 /* we're done with the special cursor position control */
2765 S_keep_cursor_pos = old_keep_cursor_pos;
2766
2767 /* return the result */
2768 return retval;
2769 }
2770
2771 # endif /* !WINDOWS */
2772 # endif /* RUNTIME */
2773 #endif /* USEDOSCOMMON */
2774
2775 /*
2776 * set the title
2777 */
2778 void os_set_title(const char *ttl)
2779 {
2780 /* do nothing in this implementation */
2781 }
2782
2783 /*
2784 Check for a game to restore given as a startup parameter.
2785 Returns TRUE if a game is to be restored, FALSE otherwise.
2786 The DOS implementation of this function always returns FALSE,
2787 since there's no way to specify a saved game at the DOS
2788 command line.
2789 */
2790 int os_paramfile(char *buf)
2791 {
2792 return(0);
2793 }
2794
2795 #ifndef __DPMI32__
2796 #ifndef WINDOWS
2797 #ifndef T_WIN32
2798 # ifdef MSOS2 /* ### Look at this later. [Eras] */
2799 int os_break() { return(0); }
2800 void os_instbrk(int install) {}
2801 # else /* !MSOS2 */
2802 /*
2803 * Check for control-break. Returns status of break flag, and clears
2804 * the flag.
2805 */
2806 static osfar_t int break_set;
2807 int os_break(void)
2808 {
2809 int ret = break_set;
2810 break_set = 0;
2811 return(ret);
2812 }
2813
2814 # ifdef DJGPP
2815
2816 /* break handler */
2817 static void break_handler(int sig)
2818 {
2819 break_set = 1;
2820 }
2821
2822 /* install or remove control-break handler */
2823 void os_instbrk(int install)
2824 {
2825 if (install)
2826 signal(SIGINT, break_handler);
2827 else
2828 signal(SIGINT, SIG_DFL);
2829 }
2830
2831 # else /* DJGPP */
2832
2833 /* break handler */
2834 static interrupt void break_handler(void)
2835 {
2836 break_set = 1;
2837 }
2838
2839 /* install or remove control-break handler */
2840 void os_instbrk(int install)
2841 {
2842 static void (interrupt *old)();
2843
2844 if (install)
2845 {
2846 old = getvect((unsigned)0x1b);
2847 setvect(0x1b, break_handler);
2848 }
2849 else
2850 setvect(0x1b, old);
2851 }
2852 # endif /* DJGPP */
2853 # endif /* MSOS2 */
2854 #endif /* T_WIN32 */
2855 #endif /* !WINDOWS */
2856 #endif /* __DPMI32__ */
2857
2858
2859 #ifdef __DPMI32__
2860 int os_break()
2861 {
2862 return 0;
2863 }
2864 #endif /* __DPMI32__ */
2865
2866 /* ------------------------------------------------------------------------ */
2867 #ifndef WINDOWS
2868 /*
2869 * HTML 4 character set translation. This translates to the DOS console
2870 * character set. This is only appropriate in DOS character mode, since
2871 * Windows uses a different character set.
2872 */
2873 void os_xlat_html4(unsigned int html4_char, char *result, size_t result_len)
2874 {
2875 /* default character to use for unknown charaters */
2876 #define INV_CHAR "\372"
2877
2878 /*
2879 * Translation table - provides mappings for characters the ISO
2880 * Latin-1 subset of the HTML 4 character map (values 128-255).
2881 *
2882 * Characters marked "(approx)" are approximations where the actual
2883 * desired character is not available in the DOS console character
2884 * set, but a reasonable approximation is used. Characters marked
2885 * "(approx unaccented)" are accented characters that are not
2886 * available; these use the unaccented equivalent as an
2887 * approximation, since this will presumably convey more meaning
2888 * than a blank.
2889 *
2890 * Characters marked "(n/a)" have no equivalent (even approximating)
2891 * in the DOS character set, and are mapped to spaces.
2892 *
2893 * Characters marked "(not used)" are not used by HTML '&' markups.
2894 */
2895 static osfar_t const char *xlat_tbl[] =
2896 {
2897 INV_CHAR, /* 128 (not used) */
2898 INV_CHAR, /* 129 (not used) */
2899 "'", /* 130 - sbquo (approx) */
2900 INV_CHAR, /* 131 (not used) */
2901 "\"", /* 132 - bdquo (approx) */
2902 INV_CHAR, /* 133 (not used) */
2903 INV_CHAR, /* 134 - dagger (n/a) */
2904 INV_CHAR, /* 135 - Dagger (n/a) */
2905 INV_CHAR, /* 136 (not used) */
2906 INV_CHAR, /* 137 - permil (n/a) */
2907 INV_CHAR, /* 138 (not used) */
2908 "<", /* 139 - lsaquo (approx) */
2909 INV_CHAR, /* 140 - OElig (n/a) */
2910 INV_CHAR, /* 141 (not used) */
2911 INV_CHAR, /* 142 (not used) */
2912 INV_CHAR, /* 143 (not used) */
2913 INV_CHAR, /* 144 (not used) */
2914 "'", /* 145 - lsquo (approx) */
2915 "'", /* 146 - rsquo (approx) */
2916 "\"", /* 147 - ldquo (approx) */
2917 "\"", /* 148 - rdquo (approx) */
2918 INV_CHAR, /* 149 (not used) */
2919 "\304", /* 150 - endash */
2920 "--", /* 151 - emdash */
2921 INV_CHAR, /* 152 (not used) */
2922 "(tm)", /* 153 - trade (approx) */
2923 INV_CHAR, /* 154 (not used) */
2924 ">", /* 155 - rsaquo (approx) */
2925 INV_CHAR, /* 156 - oelig (n/a) */
2926 INV_CHAR, /* 157 (not used) */
2927 INV_CHAR, /* 158 (not used) */
2928 "Y", /* 159 - Yuml (approx unaccented) */
2929 INV_CHAR, /* 160 (not used) */
2930 "\255", /* 161 - iexcl */
2931 "\233", /* 162 - cent */
2932 "\234", /* 163 - pound */
2933 INV_CHAR, /* 164 - curren (n/a) */
2934 "\235", /* 165 - yen */
2935 "\263", /* 166 - brvbar */
2936 INV_CHAR, /* 167 - sect (n/a) */
2937 INV_CHAR, /* 168 - uml (n/a) */
2938 "(c)", /* 169 - copy (approx) */
2939 "\246", /* 170 - ordf */
2940 "\256", /* 171 - laquo */
2941 "\252", /* 172 - not */
2942 " ", /* 173 - shy (n/a) */
2943 "(R)", /* 174 - reg (approx) */
2944 INV_CHAR, /* 175 - macr (n/a) */
2945 "\370", /* 176 - deg */
2946 "\361", /* 177 - plusmn */
2947 "\375", /* 178 - sup2 */
2948 INV_CHAR, /* 179 - sup3 (n/a) */
2949 "'", /* 180 - acute (approx) */
2950 "\346", /* 181 - micro */
2951 INV_CHAR, /* 182 - para (n/a) */
2952 "\371", /* 183 - middot */
2953 ",", /* 184 - cedil (approx) */
2954 INV_CHAR, /* 185 - sup1 (n/a) */
2955 "\247", /* 186 - ordm */
2956 "\257", /* 187 - raquo */
2957 "\254", /* 188 - frac14 */
2958 "\253", /* 189 - frac12 */
2959 "3/4", /* 190 - frac34 (approx) */
2960 "\250", /* 191 - iquest */
2961 "A", /* 192 - Agrave (approx unaccented) */
2962 "A", /* 193 - Aacute (approx unaccented) */
2963 "A", /* 194 - Acirc (approx unaccented) */
2964 "A", /* 195 - Atilde (approx unaccented) */
2965 "\216", /* 196 - Auml */
2966 "\217", /* 197 - Aring */
2967 "\222", /* 198 - AElig */
2968 "\200", /* 199 - Ccedil */
2969 "E", /* 200 - Egrave (approx unaccented) */
2970 "\220", /* 201 - Eacute */
2971 "E", /* 202 - Ecirc (approx unaccented) */
2972 "E", /* 203 - Euml (approx unaccented) */
2973 "I", /* 204 - Igrave (approx unaccented) */
2974 "I", /* 205 - Iacute (approx unaccented) */
2975 "I", /* 206 - Icirc (approx unaccented) */
2976 "I", /* 207 - Iuml (approx unaccented) */
2977 INV_CHAR, /* 208 - ETH (n/a) */
2978 "\245", /* 209 - Ntilde */
2979 "O", /* 210 - Ograve (approx unaccented) */
2980 "O", /* 211 - Oacute (approx unaccented) */
2981 "O", /* 212 - Ocirc (approx unaccented) */
2982 "O", /* 213 - Otilde (approx unaccented) */
2983 "\231", /* 214 - Ouml */
2984 "x", /* 215 - times (approx) */
2985 "O", /* 216 - Oslash (approx unaccented) */
2986 "U", /* 217 - Ugrave (approx unaccented) */
2987 "U", /* 218 - Uacute (approx unaccented) */
2988 "U", /* 219 - Ucirc (approx unaccented) */
2989 "\232", /* 220 - Uuml */
2990 "Y", /* 221 - Yacute (approx unaccented) */
2991 INV_CHAR, /* 222 - THORN (n/a) */
2992 "\341", /* 223 - szlig (approx) */
2993 "\205", /* 224 - agrave */
2994 "\240", /* 225 - aacute */
2995 "\203", /* 226 - acirc */
2996 "a", /* 227 - atilde (approx unaccented) */
2997 "\204", /* 228 - auml */
2998 "\206", /* 229 - aring */
2999 "\221", /* 230 - aelig */
3000 "\207", /* 231 - ccedil */
3001 "\212", /* 232 - egrave */
3002 "\202", /* 233 - eacute */
3003 "\210", /* 234 - ecirc */
3004 "\211", /* 235 - euml */
3005 "\215", /* 236 - igrave */
3006 "\241", /* 237 - iacute */
3007 "\214", /* 238 - icirc */
3008 "\213", /* 239 - iuml */
3009 INV_CHAR, /* 240 - eth (n/a) */
3010 "\244", /* 241 - ntilde */
3011 "\225", /* 242 - ograve */
3012 "\242", /* 243 - oacute */
3013 "\223", /* 244 - ocirc */
3014 "o", /* 245 - otilde (approx unaccented) */
3015 "\224", /* 246 - ouml */
3016 "\366", /* 247 - divide */
3017 "o", /* 248 - oslash (approx unaccented) */
3018 "\227", /* 249 - ugrave */
3019 "\243", /* 250 - uacute */
3020 "\226", /* 251 - ucirc */
3021 "\201", /* 252 - uuml */
3022 "y", /* 253 - yacute (approx unaccented) */
3023 INV_CHAR, /* 254 - thorn (n/a) */
3024 "\230" /* 255 - yuml */
3025 };
3026
3027 /*
3028 * Map certain extended characters into our 128-255 range. If we
3029 * don't recognize the character, return the default invalid
3030 * charater value.
3031 */
3032 if (html4_char > 255)
3033 {
3034 switch(html4_char)
3035 {
3036 case 338: html4_char = 140; break;
3037 case 339: html4_char = 156; break;
3038 case 376: html4_char = 159; break;
3039 case 352: html4_char = 154; break;
3040 case 353: html4_char = 138; break;
3041 case 8211: html4_char = 150; break;
3042 case 8212: html4_char = 151; break;
3043 case 8216: html4_char = 145; break;
3044 case 8217: html4_char = 146; break;
3045 case 8218: html4_char = 130; break;
3046 case 8220: html4_char = 147; break;
3047 case 8221: html4_char = 148; break;
3048 case 8222: html4_char = 132; break;
3049 case 8224: html4_char = 134; break;
3050 case 8225: html4_char = 135; break;
3051 case 8240: html4_char = 137; break;
3052 case 8249: html4_char = 139; break;
3053 case 8250: html4_char = 155; break;
3054 case 8482: html4_char = 153; break;
3055
3056 default:
3057 /* unmappable character - return mid-dot character */
3058 result[0] = (unsigned char)250;
3059 result[1] = 0;
3060 return;
3061 }
3062 }
3063
3064 /*
3065 * if the character is in the regular ASCII zone, return it
3066 * untranslated
3067 */
3068 if (html4_char < 128)
3069 {
3070 result[0] = (unsigned char)html4_char;
3071 result[1] = '\0';
3072 return;
3073 }
3074
3075 /* look up the character in our table and return the translation */
3076 strcpy(result, xlat_tbl[html4_char - 128]);
3077 }
3078
3079 #endif /* WINDOWS */
3080
3081 #ifdef DJGPP
3082 #if DJGPP < 2 || (DJGPP == 2 && DJGPP_MINOR <= 1)
3083
3084 /*
3085 * Provide memicmp, since it's not a standard libc routine.
3086 */
3087 memicmp(char *s1, char *s2, int len)
3088 {
3089 int i;
3090
3091 for (i = 0; i < len; i++)
3092 {
3093 if (tolower(s1[i]) != tolower(s2[i]))
3094 return (int)tolower(s1[i]) - (int)tolower(s2[i]);
3095 }
3096 return 0;
3097 }
3098
3099 #endif
3100 #endif /* DJGPP */
3101
3102 /*
3103 * Use the system strlwr() implementation for os_strlwr()
3104 */
3105 char *os_strlwr(char *s)
3106 {
3107 return strlwr(s);
3108 }
3109
3110 /* ------------------------------------------------------------------------ */
3111 /*
3112 * Character mapping functions
3113 */
3114
3115 #ifdef T_WIN32
3116 /* ------------------------------------------------------------------------ */
3117 /*
3118 * Get the DOS codepage. For the 32-bit console app version, call the
3119 * console function.
3120 */
3121 static int oss_get_dos_codepage(void)
3122 {
3123 return GetConsoleOutputCP();
3124 }
3125
3126 /*
3127 * Get the filename character mapping for Unicode characters.
3128 *
3129 * - For the display, we use the OEM mapping, since we're running under a
3130 * DOS console, and DOS consoles use the OEM character set
3131 *
3132 * - For filenames, we use the ANSI mapping, since file operations are in
3133 * the ANSI character set
3134 *
3135 * - For file contents, we use the ANSI mapping, since most files will have
3136 * been created by Windows apps that use the Windows character set
3137 */
3138 void os_get_charmap(char *mapname, int charmap_id)
3139 {
3140 int cp;
3141 HANDLE h;
3142
3143 /* get the correct code page for the usage */
3144 switch(charmap_id)
3145 {
3146 case OS_CHARMAP_DISPLAY:
3147 /*
3148 * Try opening the console output handle. This will let us
3149 * determine if we're running under a DOS box or under a different
3150 * shell, such as Workbench. If we're running under a DOS box,
3151 * we'll use the OEM code page for console output; otherwise, we'll
3152 * assume we're running in a graphical shell, so we'll use the ANSI
3153 * code page.
3154 */
3155 if ((h = CreateFile("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE,
3156 0, OPEN_EXISTING, 0, 0)) != INVALID_HANDLE_VALUE)
3157 {
3158 /* we have a console (aka DOS box) - use the OEM code page */
3159 cp = GetOEMCP();
3160 CloseHandle(h);
3161 }
3162 else
3163 {
3164 /*
3165 * no console - assume we're running in a graphic shell, so use
3166 * the ANSI code page
3167 */
3168 cp = GetACP();
3169 }
3170 break;
3171
3172 case OS_CHARMAP_CMDLINE:
3173 /*
3174 * even though CMD.EXE uses the OEM code page for display, it seems
3175 * to pass argv strings in the ANSI code page (or this could be an
3176 * MSVC feature, but it's what we get in any case)
3177 */
3178 cp = GetACP();
3179 break;
3180
3181 case OS_CHARMAP_FILENAME:
3182 case OS_CHARMAP_FILECONTENTS:
3183 /* use the ANSI character set for filenames and contents */
3184 cp = GetACP();
3185 break;
3186
3187 default:
3188 /* for anything unknown, use the ANSI character set */
3189 cp = GetACP();
3190 break;
3191 }
3192
3193 /* build the name as "CPnnnn", where nnnn is the ANSI code page */
3194 sprintf(mapname, "CP%d", cp);
3195 }
3196
3197 #else /* T_WIN32 */
3198
3199 /*
3200 * Get the current DOS codepage. This is the plain 16-bit DOS version - we
3201 * simply call the int 21h function.
3202 */
3203 static int oss_get_dos_codepage(void)
3204 {
3205 int codepage;
3206
3207 #ifdef DJGPP
3208 __dpmi_regs regs;
3209
3210 regs.x.ax = 0x6601;
3211 __dpmi_int(0x21, &regs);
3212 codepage = regs.x.bx;
3213
3214 #else /* DJGPP */
3215 __asm {
3216 mov ax, 6601h
3217 int 21h
3218 mov [codepage], bx
3219 }
3220 #endif /* DJGPP */
3221 return codepage;
3222 }
3223
3224 /*
3225 * Get the character mapping for Unicode characters. We use the current
3226 * DOS code page for all character maps.
3227 */
3228 void os_get_charmap(char *mapname, int charmap_id)
3229 {
3230 int codepage;
3231
3232 /* get the current DOS code page */
3233 codepage = oss_get_dos_codepage();
3234
3235 /* build the name as "CPnnnn", where nnnn is the code page number */
3236 sprintf(mapname, "CP%d", codepage);
3237 }
3238
3239 #endif /* T_WIN32 */
3240
3241
3242 /* ------------------------------------------------------------------------ */
3243 /*
3244 * Generate a filename for a character mapping table. We will generate a
3245 * name starting with the current DOS code page identifier, then the
3246 * internal ID, and the suffix ".TCP". The DOS code page ID and the
3247 * internal ID will both be four characters at most, so the name will
3248 * always fit in the DOS 8.3 filename limit.
3249 */
3250 void os_gen_charmap_filename(char *filename, char *internal_id,
3251 char *argv0)
3252 {
3253 char *p;
3254 char *rootname;
3255 size_t pathlen;
3256 int codepage;
3257
3258 /* get the current DOS code page */
3259 codepage = oss_get_dos_codepage();
3260
3261 /* find the path prefix of the original executable filename */
3262 for (p = rootname = argv0 ; *p != '\0' ; ++p)
3263 {
3264 if (*p == '/' || *p == '\\' || *p == ':')
3265 rootname = p + 1;
3266 }
3267
3268 /* copy the path prefix */
3269 pathlen = rootname - argv0;
3270 memcpy(filename, argv0, pathlen);
3271
3272 /* if there's no trailing backslash, add one */
3273 if (pathlen == 0 || filename[pathlen - 1] != '\\')
3274 filename[pathlen++] = '\\';
3275
3276 /* add the code page ID, the internal ID, and the extension */
3277 sprintf(filename + pathlen, "%u", codepage);
3278 strcat(filename + pathlen, internal_id);
3279 strcat(filename + pathlen, ".tcp");
3280 }
3281
3282 /*
3283 * Receive notification that a character mapping file has been loaded. We
3284 * don't need to do anything with this information, since we require the
3285 * player to set an appropriate code page prior to starting the run-time.
3286 */
3287 void os_advise_load_charmap(char *id, char *ldesc, char *sysinfo)
3288 {
3289 /* there's nothing we need to do here */
3290 }
3291
3292 /*
3293 * Translate keystrokes from "raw" to "processed" mode. This translates
3294 * keystrokes from the raw representation used by os_getc_raw() to the
3295 * editing commands used in os_getc().
3296 */
3297 void oss_raw_key_to_cmd(os_event_info_t *evt)
3298 {
3299 /* check the first character of the keystroke event */
3300 switch(evt->key[0])
3301 {
3302 case 0:
3303 /* it's a special key - check the special key code */
3304 switch(evt->key[1])
3305 {
3306 case CMD_F1:
3307 /* toggle scrollback mode */
3308 evt->key[1] = CMD_SCR;
3309 break;
3310 }
3311 break;
3312
3313 case 1:
3314 /* ^A - start of line */
3315 evt->key[0] = 0;
3316 evt->key[1] = CMD_HOME;
3317 break;
3318
3319 case 2:
3320 /* ^B - left */
3321 evt->key[0] = 0;
3322 evt->key[1] = CMD_LEFT;
3323 break;
3324
3325 case 4:
3326 /* ^D - delete */
3327 evt->key[0] = 0;
3328 evt->key[1] = CMD_DEL;
3329 break;
3330
3331 case 5:
3332 /* ^E - end of line */
3333 evt->key[0] = 0;
3334 evt->key[1] = CMD_END;
3335 break;
3336
3337 case 6:
3338 /* ^F - right */
3339 evt->key[0] = 0;
3340 evt->key[1] = CMD_RIGHT;
3341 break;
3342
3343 case 11:
3344 /* ^K - delete to end of line */
3345 evt->key[0] = 0;
3346 evt->key[1] = CMD_DEOL;
3347 break;
3348
3349 case 21:
3350 case 27:
3351 /* ^U/Escape - delete entire line */
3352 evt->key[0] = 0;
3353 evt->key[1] = CMD_KILL;
3354 break;
3355 }
3356 }
3357
3358 #ifndef DJGPP
3359 #ifdef __TURBOC__
3360 # define _eof(handle) eof(handle)
3361 # define _fileno(stream) fileno(stream)
3362 #endif
3363
3364 /*
3365 * Is stdin at EOF? Note that we need to check the low-level OS file
3366 * handle, not the stdio stream handle - the stream handle doesn't appear
3367 * to notice that it's at EOF until an attempt has been made to read from
3368 * the stream, whereas the low-level OS handle knows without reading.
3369 */
3370 int oss_eof_on_stdin()
3371 {
3372 return _eof(_fileno(stdin)) > 0;
3373 }
3374 #endif
3375
3376
3377 /* ------------------------------------------------------------------------ */
3378 /*
3379 * Default implementation of sprintf equivalents with memory allocation.
3380 * The old Borland library doesn't have a version of sprintf with buffer
3381 * limits, nor does DJGPP, so we take a somewhat cheesy but workable
3382 * approach: we format to the NUL: device to do the length counting.
3383 */
3384 #if defined(TURBO) || defined(DJGPP)
3385 int os_asprintf(char **bufptr, const char *fmt, ...)
3386 {
3387 va_list argp;
3388 int ret;
3389
3390 va_start(argp, fmt);
3391 ret = os_vasprintf(bufptr, fmt, argp);
3392 va_end(argp);
3393
3394 return ret;
3395 }
3396
3397 int os_vasprintf(char **bufptr, const char *fmt, va_list argp)
3398 {
3399 int len;
3400 FILE *fp;
3401
3402 /* open the NUL: device so that we can do our length counting */
3403 fp = fopen("NUL", "w");
3404 if (fp == 0)
3405 return -1;
3406
3407 /* format the string to NUL: */
3408 len = fprintf(fp, fmt, argp);
3409
3410 /* done with the file */
3411 fclose(fp);
3412
3413 /* if the fprintf failed, return failure */
3414 if (len < 0)
3415 return -1;
3416
3417 /* allocate a buffer */
3418 if ((*bufptr = (char *)osmalloc(len + 1)) == 0)
3419 return -1;
3420
3421 /* format the result */
3422 return vsprintf(*bufptr, fmt, argp);
3423 }
3424 #endif /* TURBO */
3425