Fix reversed hour and minute in int 21 ah=2c.
[wine] / dlls / winedos / int21.c
1 /*
2  * DOS interrupt 21h handler
3  *
4  * Copyright 1993, 1994 Erik Bos
5  * Copyright 1996 Alexandre Julliard
6  * Copyright 1997 Andreas Mohr
7  * Copyright 1998 Uwe Bonnes
8  * Copyright 1998, 1999 Ove Kaaven
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include "config.h"
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winternl.h"
30 #include "wine/winbase16.h"
31 #include "dosexe.h"
32 #include "miscemu.h"
33 #include "msdos.h"
34 #include "file.h"
35 #include "winerror.h"
36 #include "wine/unicode.h"
37 #include "wine/debug.h"
38
39 /*
40  * FIXME: Delete this reference when all int21 code has been moved to winedos.
41  */
42 extern void WINAPI INT_Int21Handler( CONTEXT86 *context );
43
44 WINE_DEFAULT_DEBUG_CHANNEL(int21);
45
46
47 #include "pshpack1.h"
48
49 /*
50  * Structure for DOS data that can be accessed directly from applications.
51  * Real and protected mode pointers will be returned to this structure so
52  * the structure must be correctly packed.
53  */
54 typedef struct _INT21_HEAP {
55     WORD uppercase_size;             /* Size of the following table in bytes */
56     BYTE uppercase_table[128];       /* Uppercase equivalents of chars from 0x80 to 0xff. */
57
58     WORD lowercase_size;             /* Size of the following table in bytes */
59     BYTE lowercase_table[256];       /* Lowercase equivalents of chars from 0x00 to 0xff. */
60
61     WORD collating_size;             /* Size of the following table in bytes */
62     BYTE collating_table[256];       /* Values used to sort characters from 0x00 to 0xff. */
63
64     WORD filename_size;              /* Size of the following filename data in bytes */
65     BYTE filename_reserved1;         /* 0x01 for MS-DOS 3.30-6.00 */
66     BYTE filename_lowest;            /* Lowest permissible character value for filename */
67     BYTE filename_highest;           /* Highest permissible character value for filename */
68     BYTE filename_reserved2;         /* 0x00 for MS-DOS 3.30-6.00 */
69     BYTE filename_exclude_first;     /* First illegal character in permissible range */
70     BYTE filename_exclude_last;      /* Last illegal character in permissible range */
71     BYTE filename_reserved3;         /* 0x02 for MS-DOS 3.30-6.00 */
72     BYTE filename_illegal_size;      /* Number of terminators in the following table */
73     BYTE filename_illegal_table[16]; /* Characters which terminate a filename */
74
75     WORD dbcs_size;                  /* Number of valid ranges in the following table */
76     BYTE dbcs_table[16];             /* Start/end bytes for N ranges and 00/00 as terminator */
77
78     BYTE misc_indos;                 /* Interrupt 21 nesting flag */
79 } INT21_HEAP;
80
81 #include "poppack.h"
82
83
84 /***********************************************************************
85  *           INT21_GetSystemCountryCode
86  *
87  * Return DOS country code for default system locale.
88  */
89 static WORD INT21_GetSystemCountryCode()
90 {
91     /*
92      * FIXME: Determine country code. We should probably use
93      *        DOSCONF structure for that.
94      */
95     return GetSystemDefaultLangID();
96 }
97
98
99 /***********************************************************************
100  *           INT21_FillCountryInformation
101  *
102  * Fill 34-byte buffer with country information data using
103  * default system locale.
104  */
105 static void INT21_FillCountryInformation( BYTE *buffer )
106 {
107     /* 00 - WORD: date format
108      *          00 = mm/dd/yy
109      *          01 = dd/mm/yy
110      *          02 = yy/mm/dd
111      */
112     *(WORD*)(buffer + 0) = 0; /* FIXME: Get from locale */
113
114     /* 02 - BYTE[5]: ASCIIZ currency symbol string */
115     buffer[2] = '$'; /* FIXME: Get from locale */
116     buffer[3] = 0;
117
118     /* 07 - BYTE[2]: ASCIIZ thousands separator */
119     buffer[7] = 0; /* FIXME: Get from locale */
120     buffer[8] = 0;
121
122     /* 09 - BYTE[2]: ASCIIZ decimal separator */
123     buffer[9]  = '.'; /* FIXME: Get from locale */
124     buffer[10] = 0;
125
126     /* 11 - BYTE[2]: ASCIIZ date separator */
127     buffer[11] = '/'; /* FIXME: Get from locale */
128     buffer[12] = 0;
129
130     /* 13 - BYTE[2]: ASCIIZ time separator */
131     buffer[13] = ':'; /* FIXME: Get from locale */
132     buffer[14] = 0;
133
134     /* 15 - BYTE: Currency format
135      *          bit 2 = set if currency symbol replaces decimal point
136      *          bit 1 = number of spaces between value and currency symbol
137      *          bit 0 = 0 if currency symbol precedes value
138      *                  1 if currency symbol follows value
139      */
140     buffer[15] = 0; /* FIXME: Get from locale */
141
142     /* 16 - BYTE: Number of digits after decimal in currency */
143     buffer[16] = 0; /* FIXME: Get from locale */
144
145     /* 17 - BYTE: Time format
146      *          bit 0 = 0 if 12-hour clock
147      *                  1 if 24-hour clock
148      */
149     buffer[17] = 1; /* FIXME: Get from locale */
150
151     /* 18 - DWORD: Address of case map routine */
152     *(DWORD*)(buffer + 18) = 0; /* FIXME: ptr to case map routine */
153
154     /* 22 - BYTE[2]: ASCIIZ data-list separator */
155     buffer[22] = ','; /* FIXME: Get from locale */
156     buffer[23] = 0;
157
158     /* 24 - BYTE[10]: Reserved */
159     memset( buffer + 24, 0, 10 );
160 }
161
162
163 /***********************************************************************
164  *           INT21_FillHeap
165  *
166  * Initialize DOS heap.
167  */
168 static void INT21_FillHeap( INT21_HEAP *heap )
169 {
170     static const char terminators[] = "\"\\./[]:|<>+=;,";
171     int i;
172
173     /*
174      * Uppercase table.
175      */
176     heap->uppercase_size = 128;
177     for (i = 0; i < 128; i++) 
178         heap->uppercase_table[i] = toupper( 128 + i );
179
180     /*
181      * Lowercase table.
182      */
183     heap->lowercase_size = 256;
184     for (i = 0; i < 256; i++) 
185         heap->lowercase_table[i] = tolower( i );
186     
187     /*
188      * Collating table.
189      */
190     heap->collating_size = 256;
191     for (i = 0; i < 256; i++) 
192         heap->collating_table[i] = i;
193
194     /*
195      * Filename table.
196      */
197     heap->filename_size = 8 + strlen(terminators);
198     heap->filename_illegal_size = strlen(terminators);
199     strcpy( heap->filename_illegal_table, terminators );
200
201     heap->filename_reserved1 = 0x01;
202     heap->filename_lowest = 0;           /* FIXME: correct value? */
203     heap->filename_highest = 0xff;       /* FIXME: correct value? */
204     heap->filename_reserved2 = 0x00;    
205     heap->filename_exclude_first = 0x00; /* FIXME: correct value? */
206     heap->filename_exclude_last = 0x00;  /* FIXME: correct value? */
207     heap->filename_reserved3 = 0x02;
208
209     /*
210      * DBCS lead byte table. This table is empty.
211      */
212     heap->dbcs_size = 0;
213     memset( heap->dbcs_table, 0, sizeof(heap->dbcs_table) );
214
215     /*
216      * Initialize InDos flag.
217      */
218     heap->misc_indos = 0;
219 }
220
221
222 /***********************************************************************
223  *           INT21_GetHeapSelector
224  *
225  * Get segment/selector for DOS heap (INT21_HEAP).
226  * Creates and initializes heap on first call.
227  */
228 static WORD INT21_GetHeapSelector( CONTEXT86 *context )
229 {
230     static WORD heap_segment = 0;
231     static WORD heap_selector = 0;
232     static BOOL heap_initialized = FALSE;
233
234     if (!heap_initialized)
235     {
236         INT21_HEAP *ptr = DOSVM_AllocDataUMB( sizeof(INT21_HEAP), 
237                                               &heap_segment,
238                                               &heap_selector );
239         INT21_FillHeap( ptr );
240         heap_initialized = TRUE;
241     }
242
243     if (!ISV86(context) && DOSVM_IsWin16())
244         return heap_selector;
245     else
246         return heap_segment;
247 }
248
249
250 /***********************************************************************
251  *           INT21_ExtendedCountryInformation
252  *
253  * Handler for function 0x65.
254  */
255 static void INT21_ExtendedCountryInformation( CONTEXT86 *context )
256 {
257     BYTE *dataptr = CTX_SEG_OFF_TO_LIN( context, context->SegEs, context->Edi );
258
259     TRACE( "GET EXTENDED COUNTRY INFORMATION, subfunction %02x\n",
260            AL_reg(context) );
261
262     /*
263      * Check subfunctions that are passed country and code page.
264      */
265     if (AL_reg(context) >= 0x01 && AL_reg(context) <= 0x07)
266     {
267         WORD country = DX_reg(context);
268         WORD codepage = BX_reg(context);
269
270         if (country != 0xffff && country != INT21_GetSystemCountryCode())
271             FIXME( "Requested info on non-default country %04x\n", country );
272
273         if (codepage != 0xffff && codepage != GetOEMCP())
274             FIXME( "Requested info on non-default code page %04x\n", codepage );
275     }
276
277     switch (AL_reg(context)) {
278     case 0x00: /* SET GENERAL INTERNATIONALIZATION INFO */
279         INT_BARF( context, 0x21 );
280         SET_CFLAG( context );
281         break;
282
283     case 0x01: /* GET GENERAL INTERNATIONALIZATION INFO */
284         TRACE( "Get general internationalization info\n" );
285         dataptr[0] = 0x01; /* Info ID */
286         *(WORD*)(dataptr+1) = 38; /* Size of the following info */
287         *(WORD*)(dataptr+3) = INT21_GetSystemCountryCode(); /* Country ID */
288         *(WORD*)(dataptr+5) = GetOEMCP(); /* Code page */
289         INT21_FillCountryInformation( dataptr + 7 );
290         SET_CX( context, 41 ); /* Size of returned info */
291         break;
292         
293     case 0x02: /* GET POINTER TO UPPERCASE TABLE */
294     case 0x04: /* GET POINTER TO FILENAME UPPERCASE TABLE */
295         TRACE( "Get pointer to uppercase table\n" );
296         dataptr[0] = AL_reg(context); /* Info ID */
297         *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
298                                            offsetof(INT21_HEAP, uppercase_size) );
299         SET_CX( context, 5 ); /* Size of returned info */
300         break;
301
302     case 0x03: /* GET POINTER TO LOWERCASE TABLE */
303         TRACE( "Get pointer to lowercase table\n" );
304         dataptr[0] = 0x03; /* Info ID */
305         *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
306                                            offsetof(INT21_HEAP, lowercase_size) );
307         SET_CX( context, 5 ); /* Size of returned info */
308         break;
309
310     case 0x05: /* GET POINTER TO FILENAME TERMINATOR TABLE */
311         TRACE("Get pointer to filename terminator table\n");
312         dataptr[0] = 0x05; /* Info ID */
313         *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
314                                            offsetof(INT21_HEAP, filename_size) );
315         SET_CX( context, 5 ); /* Size of returned info */
316         break;
317
318     case 0x06: /* GET POINTER TO COLLATING SEQUENCE TABLE */
319         TRACE("Get pointer to collating sequence table\n");
320         dataptr[0] = 0x06; /* Info ID */
321         *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
322                                            offsetof(INT21_HEAP, collating_size) );
323         SET_CX( context, 5 ); /* Size of returned info */
324         break;
325
326     case 0x07: /* GET POINTER TO DBCS LEAD BYTE TABLE */
327         TRACE("Get pointer to DBCS lead byte table\n");
328         dataptr[0] = 0x07; /* Info ID */
329         *(DWORD*)(dataptr+1) = MAKESEGPTR( INT21_GetHeapSelector( context ),
330                                            offsetof(INT21_HEAP, dbcs_size) );
331         SET_CX( context, 5 ); /* Size of returned info */
332         break;
333
334     case 0x20: /* CAPITALIZE CHARACTER */
335     case 0xa0: /* CAPITALIZE FILENAME CHARACTER */
336         TRACE("Convert char to uppercase\n");
337         SET_DL( context, toupper(DL_reg(context)) );
338         break;
339
340     case 0x21: /* CAPITALIZE STRING */
341     case 0xa1: /* CAPITALIZE COUNTED FILENAME STRING */
342         TRACE("Convert string to uppercase with length\n");
343         {
344             char *ptr = (char *)CTX_SEG_OFF_TO_LIN( context,
345                                                     context->SegDs,
346                                                     context->Edx );
347             WORD len = CX_reg(context);
348             while (len--) { *ptr = toupper(*ptr); ptr++; }
349         }
350         break;
351
352     case 0x22: /* CAPITALIZE ASCIIZ STRING */
353     case 0xa2: /* CAPITALIZE ASCIIZ FILENAME */
354         TRACE("Convert ASCIIZ string to uppercase\n");
355         _strupr( (LPSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx) );
356         break;
357
358     case 0x23: /* DETERMINE IF CHARACTER REPRESENTS YES/NO RESPONSE */
359         INT_BARF( context, 0x21 );
360         SET_CFLAG( context );
361         break;
362
363     default:
364         INT_BARF( context, 0x21 );
365         SET_CFLAG(context);
366         break;
367     }
368 }
369
370
371 /***********************************************************************
372  *           INT21_GetPSP
373  *
374  * Handler for functions 0x51 and 0x62.
375  */
376 static void INT21_GetPSP( CONTEXT86 *context )
377 {
378     TRACE( "GET CURRENT PSP ADDRESS (%02x)\n", AH_reg(context) );
379
380     /*
381      * FIXME: should we return the original DOS PSP upon
382      *        Windows startup ? 
383      */
384     if (!ISV86(context) && DOSVM_IsWin16())
385         SET_BX( context, LOWORD(GetCurrentPDB16()) );
386     else
387         SET_BX( context, DOSVM_psp );
388 }
389
390
391 /***********************************************************************
392  *           INT21_Ioctl
393  *
394  * Handler for function 0x44.
395  */
396 static void INT21_Ioctl( CONTEXT86 *context )
397 {
398   static const WCHAR emmxxxx0W[] = {'E','M','M','X','X','X','X','0',0};
399   const DOS_DEVICE *dev = DOSFS_GetDeviceByHandle(
400       DosFileHandleToWin32Handle(BX_reg(context)) );
401
402   if (dev && !strcmpiW( dev->name, emmxxxx0W )) {
403     EMS_Ioctl_Handler(context);
404     return;
405   }
406
407   switch (AL_reg(context))
408   {
409   case 0x0b: /* SET SHARING RETRY COUNT */
410       TRACE("IOCTL - SET SHARING RETRY COUNT pause %d retries %d\n",
411            CX_reg(context), DX_reg(context));
412       if (!CX_reg(context))
413       {
414          SET_AX( context, 1 );
415          SET_CFLAG(context);
416          break;
417       }
418       DOSMEM_LOL()->sharing_retry_delay = CX_reg(context);
419       if (!DX_reg(context))
420          DOSMEM_LOL()->sharing_retry_count = DX_reg(context);
421       RESET_CFLAG(context);
422       break;
423   default:
424       INT_Int21Handler( context );
425   }
426 }
427
428
429 /***********************************************************************
430  *           INT21_GetExtendedError
431  */
432 static void INT21_GetExtendedError( CONTEXT86 *context )
433 {
434     BYTE class, action, locus;
435     WORD error = GetLastError();
436
437     switch(error)
438     {
439     case ERROR_SUCCESS:
440         class = action = locus = 0;
441         break;
442     case ERROR_DIR_NOT_EMPTY:
443         class  = EC_Exists;
444         action = SA_Ignore;
445         locus  = EL_Disk;
446         break;
447     case ERROR_ACCESS_DENIED:
448         class  = EC_AccessDenied;
449         action = SA_Abort;
450         locus  = EL_Disk;
451         break;
452     case ERROR_CANNOT_MAKE:
453         class  = EC_AccessDenied;
454         action = SA_Abort;
455         locus  = EL_Unknown;
456         break;
457     case ERROR_DISK_FULL:
458     case ERROR_HANDLE_DISK_FULL:
459         class  = EC_MediaError;
460         action = SA_Abort;
461         locus  = EL_Disk;
462         break;
463     case ERROR_FILE_EXISTS:
464     case ERROR_ALREADY_EXISTS:
465         class  = EC_Exists;
466         action = SA_Abort;
467         locus  = EL_Disk;
468         break;
469     case ERROR_FILE_NOT_FOUND:
470         class  = EC_NotFound;
471         action = SA_Abort;
472         locus  = EL_Disk;
473         break;
474     case ER_GeneralFailure:
475         class  = EC_SystemFailure;
476         action = SA_Abort;
477         locus  = EL_Unknown;
478         break;
479     case ERROR_INVALID_DRIVE:
480         class  = EC_MediaError;
481         action = SA_Abort;
482         locus  = EL_Disk;
483         break;
484     case ERROR_INVALID_HANDLE:
485         class  = EC_ProgramError;
486         action = SA_Abort;
487         locus  = EL_Disk;
488         break;
489     case ERROR_LOCK_VIOLATION:
490         class  = EC_AccessDenied;
491         action = SA_Abort;
492         locus  = EL_Disk;
493         break;
494     case ERROR_NO_MORE_FILES:
495         class  = EC_MediaError;
496         action = SA_Abort;
497         locus  = EL_Disk;
498         break;
499     case ER_NoNetwork:
500         class  = EC_NotFound;
501         action = SA_Abort;
502         locus  = EL_Network;
503         break;
504     case ERROR_NOT_ENOUGH_MEMORY:
505         class  = EC_OutOfResource;
506         action = SA_Abort;
507         locus  = EL_Memory;
508         break;
509     case ERROR_PATH_NOT_FOUND:
510         class  = EC_NotFound;
511         action = SA_Abort;
512         locus  = EL_Disk;
513         break;
514     case ERROR_SEEK:
515         class  = EC_NotFound;
516         action = SA_Ignore;
517         locus  = EL_Disk;
518         break;
519     case ERROR_SHARING_VIOLATION:
520         class  = EC_Temporary;
521         action = SA_Retry;
522         locus  = EL_Disk;
523         break;
524     case ERROR_TOO_MANY_OPEN_FILES:
525         class  = EC_ProgramError;
526         action = SA_Abort;
527         locus  = EL_Disk;
528         break;
529     default:
530         FIXME("Unknown error %d\n", error );
531         class  = EC_SystemFailure;
532         action = SA_Abort;
533         locus  = EL_Unknown;
534         break;
535     }
536     TRACE("GET EXTENDED ERROR code 0x%02x class 0x%02x action 0x%02x locus %02x\n",
537            error, class, action, locus );
538     SET_AX( context, error );
539     SET_BH( context, class );
540     SET_BL( context, action );
541     SET_CH( context, locus );
542 }
543
544
545 /***********************************************************************
546  *           DOSVM_Int21Handler (WINEDOS16.133)
547  *
548  * Interrupt 0x21 handler.
549  */
550 void WINAPI DOSVM_Int21Handler( CONTEXT86 *context )
551 {
552     BOOL bSetDOSExtendedError = FALSE;
553
554     TRACE( "AX=%04x BX=%04x CX=%04x DX=%04x "
555            "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
556            AX_reg(context), BX_reg(context), 
557            CX_reg(context), DX_reg(context),
558            SI_reg(context), DI_reg(context),
559            (WORD)context->SegDs, (WORD)context->SegEs,
560            context->EFlags );
561
562    /*
563     * Extended error is used by (at least) functions 0x2f to 0x62.
564     * Function 0x59 returns extended error and error should not
565     * be cleared before handling the function.
566     */
567     if (AH_reg(context) >= 0x2f && AH_reg(context) != 0x59) 
568         SetLastError(0);
569
570     RESET_CFLAG(context); /* Not sure if this is a good idea. */
571
572     switch(AH_reg(context))
573     {
574     case 0x00: /* TERMINATE PROGRAM */
575         TRACE("TERMINATE PROGRAM\n");
576         if (DOSVM_IsWin16())
577             ExitThread( 0 );
578         else
579             MZ_Exit( context, FALSE, 0 );
580         break;
581
582     case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
583         {
584             BYTE ascii;
585             TRACE("DIRECT CHARACTER INPUT WITH ECHO\n");
586             DOSVM_Int16ReadChar(&ascii, NULL, FALSE);
587             SET_AL( context, ascii );
588             DOSVM_PutChar(AL_reg(context));
589         }
590         break;
591
592     case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
593         TRACE("Write Character to Standard Output\n");
594         DOSVM_PutChar(DL_reg(context));
595         break;
596
597     case 0x03: /* READ CHARACTER FROM STDAUX  */
598     case 0x04: /* WRITE CHARACTER TO STDAUX */
599     case 0x05: /* WRITE CHARACTER TO PRINTER */
600         INT_BARF( context, 0x21 );
601         break;
602
603     case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
604         /* FIXME: Use DOSDEV_Peek/Read/Write(DOSDEV_Console(),...) !! */
605         if (DL_reg(context) == 0xff) {
606             static char scan = 0;
607             TRACE("Direct Console Input\n");
608             if (scan) {
609                 /* return pending scancode */
610                 SET_AL( context, scan );
611                 RESET_ZFLAG(context);
612                 scan = 0;
613             } else {
614                 BYTE ascii;
615                 if (DOSVM_Int16ReadChar(&ascii,&scan,TRUE)) {
616                     DOSVM_Int16ReadChar(&ascii,&scan,FALSE);
617                     /* return ASCII code */
618                     SET_AL( context, ascii );
619                     RESET_ZFLAG(context);
620                     /* return scan code on next call only if ascii==0 */
621                     if (ascii) scan = 0;
622                 } else {
623                     /* nothing pending, clear everything */
624                     SET_AL( context, 0 );
625                     SET_ZFLAG(context);
626                     scan = 0; /* just in case */
627                 }
628             }
629         } else {
630             TRACE("Direct Console Output\n");
631             DOSVM_PutChar(DL_reg(context));
632         }
633         break;
634
635     case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
636         {
637             BYTE ascii;
638             /* FIXME: Use DOSDEV_Peek/Read(DOSDEV_Console(),...) !! */
639             TRACE("DIRECT CHARACTER INPUT WITHOUT ECHO\n");
640             DOSVM_Int16ReadChar(&ascii, NULL, FALSE);
641             SET_AL( context, ascii );
642         }
643         break;
644
645     case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
646         {
647             BYTE ascii;
648             /* FIXME: Use DOSDEV_Peek/Read(DOSDEV_Console(),...) !! */
649             TRACE("CHARACTER INPUT WITHOUT ECHO\n");
650             DOSVM_Int16ReadChar(&ascii, NULL, FALSE);
651             SET_AL( context, ascii );
652         }
653         break;
654
655     case 0x09: /* WRITE STRING TO STANDARD OUTPUT */
656         INT_Int21Handler( context );
657         break;
658
659     case 0x0a: /* BUFFERED INPUT */
660         INT_Int21Handler( context );
661         break;
662
663     case 0x0b: /* GET STDIN STATUS */
664         TRACE( "GET STDIN STATUS\n" );
665         {
666             BIOSDATA *data = BIOS_DATA;
667             if(data->FirstKbdCharPtr == data->NextKbdCharPtr)
668                 SET_AL( context, 0 );
669             else
670                 SET_AL( context, 0xff );
671         }
672         break;
673
674     case 0x0c: /* FLUSH BUFFER AND READ STANDARD INPUT */
675         {
676             BYTE al = AL_reg(context); /* Input function to execute after flush. */
677
678             TRACE( "FLUSH BUFFER AND READ STANDARD INPUT - 0x%02x\n", al );
679
680             /* FIXME: buffers are not flushed */
681
682             /*
683              * If AL is one of 0x01, 0x06, 0x07, 0x08, or 0x0a,
684              * int21 function identified by AL will be called.
685              */
686             if(al == 0x01 || al == 0x06 || al == 0x07 || al == 0x08 || al == 0x0a)
687             {
688                 SET_AH( context, al );
689                 DOSVM_Int21Handler( context );
690             }
691         }
692         break;
693
694     case 0x0d: /* DISK BUFFER FLUSH */
695         TRACE("DISK BUFFER FLUSH ignored\n");
696         break;
697
698     case 0x0e: /* SELECT DEFAULT DRIVE */
699         INT_Int21Handler( context );
700         break;
701
702     case 0x0f: /* OPEN FILE USING FCB */
703     case 0x10: /* CLOSE FILE USING FCB */
704         INT_BARF( context, 0x21 );
705         break;
706
707     case 0x11: /* FIND FIRST MATCHING FILE USING FCB */
708     case 0x12: /* FIND NEXT MATCHING FILE USING FCB */
709     case 0x13: /* DELETE FILE USING FCB */
710         INT_Int21Handler( context );
711         break;
712
713     case 0x14: /* SEQUENTIAL READ FROM FCB FILE */
714     case 0x15: /* SEQUENTIAL WRITE TO FCB FILE */
715     case 0x16: /* CREATE OR TRUNCATE FILE USING FCB */
716         INT_BARF( context, 0x21 );
717         break;
718
719     case 0x17: /* RENAME FILE USING FCB */
720         INT_Int21Handler( context );
721         break;
722
723     case 0x18: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
724         SET_AL( context, 0 );
725         break;
726
727     case 0x19: /* GET CURRENT DEFAULT DRIVE */
728     case 0x1a: /* SET DISK TRANSFER AREA ADDRESS */
729     case 0x1b: /* GET ALLOCATION INFORMATION FOR DEFAULT DRIVE */        
730     case 0x1c: /* GET ALLOCATION INFORMATION FOR SPECIFIC DRIVE */
731         INT_Int21Handler( context );
732         break;
733
734     case 0x1d: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
735     case 0x1e: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
736         SET_AL( context, 0 );
737         break;
738
739     case 0x1f: /* GET DRIVE PARAMETER BLOCK FOR DEFAULT DRIVE */
740         INT_Int21Handler( context );
741         break;
742
743     case 0x20: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
744         SET_AL( context, 0 );
745         break;
746
747     case 0x21: /* READ RANDOM RECORD FROM FCB FILE */
748     case 0x22: /* WRITE RANDOM RECORD TO FCB FILE */
749     case 0x23: /* GET FILE SIZE FOR FCB */
750     case 0x24: /* SET RANDOM RECORD NUMBER FOR FCB */
751         INT_BARF( context, 0x21 );
752         break;
753
754     case 0x25: /* SET INTERRUPT VECTOR */
755         TRACE("SET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
756         {
757             FARPROC16 ptr = (FARPROC16)MAKESEGPTR( context->SegDs, DX_reg(context) );
758             if (!ISV86(context) && DOSVM_IsWin16())
759                 DOSVM_SetPMHandler16(  AL_reg(context), ptr );
760             else
761                 DOSVM_SetRMHandler( AL_reg(context), ptr );
762         }
763         break;
764
765     case 0x26: /* CREATE NEW PROGRAM SEGMENT PREFIX */
766     case 0x27: /* RANDOM BLOCK READ FROM FCB FILE */
767     case 0x28: /* RANDOM BLOCK WRITE TO FCB FILE */
768         INT_BARF( context, 0x21 );
769         break;
770
771     case 0x29: /* PARSE FILENAME INTO FCB */
772         INT_Int21Handler( context );
773         break;
774
775     case 0x2a: /* GET SYSTEM DATE */
776         TRACE( "GET SYSTEM DATE\n" );
777         {
778             SYSTEMTIME systime;
779             GetLocalTime( &systime );
780             SET_CX( context, systime.wYear );
781             SET_DH( context, systime.wMonth );
782             SET_DL( context, systime.wDay );
783             SET_AL( context, systime.wDayOfWeek );
784         }
785         break;
786
787     case 0x2b: /* SET SYSTEM DATE */
788         FIXME("SetSystemDate(%02d/%02d/%04d): not allowed\n",
789               DL_reg(context), DH_reg(context), CX_reg(context) );
790         SET_AL( context, 0 );  /* Let's pretend we succeeded */
791         break;
792
793     case 0x2c: /* GET SYSTEM TIME */
794         TRACE( "GET SYSTEM TIME\n" );
795         {
796             SYSTEMTIME systime;
797             GetLocalTime( &systime );
798             SET_CH( context, systime.wHour );
799             SET_CL( context, systime.wMinute );
800             SET_DH( context, systime.wSecond );
801             SET_DL( context, systime.wMilliseconds / 10 );
802         }
803         break;
804
805     case 0x2d: /* SET SYSTEM TIME */
806         FIXME("SetSystemTime(%02d:%02d:%02d.%02d): not allowed\n",
807               CH_reg(context), CL_reg(context),
808               DH_reg(context), DL_reg(context) );
809         SET_AL( context, 0 );  /* Let's pretend we succeeded */
810         break;
811
812     case 0x2e: /* SET VERIFY FLAG */
813         TRACE("SET VERIFY FLAG ignored\n");
814         /* we cannot change the behaviour anyway, so just ignore it */
815         break;
816
817     case 0x2f: /* GET DISK TRANSFER AREA ADDRESS */
818     case 0x30: /* GET DOS VERSION */
819         INT_Int21Handler( context );
820         break;
821
822     case 0x31: /* TERMINATE AND STAY RESIDENT */
823         FIXME("TERMINATE AND STAY RESIDENT stub\n");
824         break;
825
826     case 0x32: /* GET DOS DRIVE PARAMETER BLOCK FOR SPECIFIC DRIVE */
827     case 0x33: /* MULTIPLEXED */
828         INT_Int21Handler( context );
829         break;
830
831     case 0x34: /* GET ADDRESS OF INDOS FLAG */
832         TRACE( "GET ADDRESS OF INDOS FLAG\n" );
833         context->SegEs = INT21_GetHeapSelector( context );
834         SET_BX( context, offsetof(INT21_HEAP, misc_indos) );
835         break;
836
837     case 0x35: /* GET INTERRUPT VECTOR */
838         TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
839         {
840             FARPROC16 addr;
841             if (!ISV86(context) && DOSVM_IsWin16())
842                 addr = DOSVM_GetPMHandler16( AL_reg(context) );
843             else
844                 addr = DOSVM_GetRMHandler( AL_reg(context) );
845             context->SegEs = SELECTOROF(addr);
846             SET_BX( context, OFFSETOF(addr) );
847         }
848         break;
849
850     case 0x36: /* GET FREE DISK SPACE */
851     case 0x37: /* SWITCHAR */
852         INT_Int21Handler( context );
853         break;
854
855     case 0x38: /* GET COUNTRY-SPECIFIC INFORMATION */
856         TRACE( "GET COUNTRY-SPECIFIC INFORMATION\n" );
857         if (AL_reg(context)) 
858         {
859             WORD country = AL_reg(context);
860             if (country == 0xff)
861                 country = BX_reg(context);
862             if (country != INT21_GetSystemCountryCode())
863                 FIXME( "Requested info on non-default country %04x\n", country );
864         }
865         INT21_FillCountryInformation( CTX_SEG_OFF_TO_LIN(context, 
866                                                          context->SegDs, 
867                                                          context->Edx) );
868         SET_AX( context, INT21_GetSystemCountryCode() );
869         SET_BX( context, INT21_GetSystemCountryCode() );
870         break;
871
872     case 0x39: /* "MKDIR" - CREATE SUBDIRECTORY */
873     case 0x3a: /* "RMDIR" - REMOVE SUBDIRECTORY */
874     case 0x3b: /* "CHDIR" - SET CURRENT DIRECTORY */
875     case 0x3c: /* "CREAT" - CREATE OR TRUNCATE FILE */
876     case 0x3d: /* "OPEN" - OPEN EXISTING FILE */
877     case 0x3e: /* "CLOSE" - CLOSE FILE */
878     case 0x3f: /* "READ" - READ FROM FILE OR DEVICE */
879         INT_Int21Handler( context );
880         break;
881
882     case 0x40:  /* "WRITE" - WRITE TO FILE OR DEVICE */
883         /* Writes to stdout are handled here. */
884         if (!DOSVM_IsWin16() && BX_reg(context) == 1) {
885           BYTE *ptr = CTX_SEG_OFF_TO_LIN(context,
886                                          context->SegDs,
887                                          context->Edx);
888           int i;
889
890           for(i=0; i<CX_reg(context); i++)
891             DOSVM_PutChar(ptr[i]);
892         } else
893           INT_Int21Handler( context );
894         break;
895
896     case 0x41: /* "UNLINK" - DELETE FILE */
897     case 0x42: /* "LSEEK" - SET CURRENT FILE POSITION */
898     case 0x43: /* FILE ATTRIBUTES */
899         INT_Int21Handler( context );
900         break;
901
902     case 0x44: /* IOCTL */
903         INT21_Ioctl( context );
904         break;
905
906     case 0x45: /* "DUP" - DUPLICATE FILE HANDLE */
907     case 0x46: /* "DUP2", "FORCEDUP" - FORCE DUPLICATE FILE HANDLE */
908     case 0x47: /* "CWD" - GET CURRENT DIRECTORY */
909         INT_Int21Handler( context );
910         break;
911
912     case 0x48: /* ALLOCATE MEMORY */
913         TRACE( "ALLOCATE MEMORY for %d paragraphs\n", BX_reg(context) );
914         {
915             WORD  selector = 0;
916             DWORD bytes = (DWORD)BX_reg(context) << 4;
917
918             if (!ISV86(context) && DOSVM_IsWin16())
919             {
920                 DWORD rv = GlobalDOSAlloc16( bytes );
921                 selector = LOWORD( rv );
922             }
923             else
924                 DOSMEM_GetBlock( bytes, &selector );
925                
926             if (selector)
927                 SET_AX( context, selector );
928             else
929             {
930                 SET_CFLAG(context);
931                 SET_AX( context, 0x0008 ); /* insufficient memory */
932                 SET_BX( context, DOSMEM_Available() >> 4 );
933             }
934         }
935         break;
936
937     case 0x49: /* FREE MEMORY */
938         TRACE( "FREE MEMORY segment %04lX\n", context->SegEs );
939         {
940             BOOL ok;
941             
942             if (!ISV86(context) && DOSVM_IsWin16())
943             {
944                 ok = !GlobalDOSFree16( context->SegEs );
945
946                 /* If we don't reset ES_reg, we will fail in the relay code */
947                 if (ok)
948                     context->SegEs = 0;
949             }
950             else
951                 ok = DOSMEM_FreeBlock( (void*)((DWORD)context->SegEs << 4) );
952
953             if (!ok)
954             {
955                 TRACE("FREE MEMORY failed\n");
956                 SET_CFLAG(context);
957                 SET_AX( context, 0x0009 ); /* memory block address invalid */
958             }
959         }
960         break;
961
962     case 0x4a: /* RESIZE MEMORY BLOCK */
963         INT_Int21Handler( context );
964         break;
965
966     case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
967         if(DOSVM_IsWin16()) {
968             INT_Int21Handler( context );
969             break;
970         }
971
972         TRACE("EXEC %s\n", (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx ));
973         if (!MZ_Exec( context, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx),
974                       AL_reg(context), CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Ebx) ))
975         {
976             SET_AX( context, GetLastError() );
977             SET_CFLAG(context);
978         }
979         break;
980
981     case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
982         TRACE( "EXIT with return code %d\n", AL_reg(context) );
983         if (DOSVM_IsWin16())
984             ExitThread( AL_reg(context) );
985         else
986             MZ_Exit( context, FALSE, AL_reg(context) );
987         break;
988
989     case 0x4d: /* GET RETURN CODE */
990         TRACE("GET RETURN CODE (ERRORLEVEL)\n");
991         SET_AX( context, DOSVM_retval );
992         DOSVM_retval = 0;
993         break;
994
995     case 0x4e: /* "FINDFIRST" - FIND FIRST MATCHING FILE */
996     case 0x4f: /* "FINDNEXT" - FIND NEXT MATCHING FILE */
997         INT_Int21Handler( context );
998         break;
999
1000     case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
1001         TRACE("SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
1002         DOSVM_psp = BX_reg(context);
1003         break;
1004
1005     case 0x51: /* GET PSP ADDRESS */
1006         INT21_GetPSP( context );
1007         break;
1008
1009     case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
1010         if (!ISV86(context) && DOSVM_IsWin16())
1011         {
1012             SEGPTR ptr = DOSMEM_LOL()->wine_pm_lol;
1013             context->SegEs = SELECTOROF(ptr);
1014             SET_BX( context, OFFSETOF(ptr) );
1015         }
1016         else
1017         {
1018             SEGPTR ptr = DOSMEM_LOL()->wine_rm_lol;
1019             context->SegEs = SELECTOROF(ptr);
1020             SET_BX( context, OFFSETOF(ptr) );
1021         }
1022         break;
1023
1024     case 0x54: /* Get Verify Flag */
1025         TRACE("Get Verify Flag - Not Supported\n");
1026         SET_AL( context, 0x00 );  /* pretend we can tell. 00h = off 01h = on */
1027         break;
1028
1029     case 0x56: /* "RENAME" - RENAME FILE */
1030     case 0x57: /* FILE DATE AND TIME */
1031         INT_Int21Handler( context );
1032         break;
1033
1034     case 0x58: /* GET OR SET MEMORY ALLOCATION STRATEGY */
1035         TRACE( "GET OR SET MEMORY ALLOCATION STRATEGY, subfunction %d\n", 
1036                AL_reg(context) );
1037         switch (AL_reg(context))
1038         {
1039         case 0x00: /* GET ALLOCATION STRATEGY */
1040             SET_AX( context, 1 ); /* low memory best fit */
1041             break;
1042
1043         case 0x01: /* SET ALLOCATION STRATEGY */
1044             TRACE( "Set allocation strategy to %d - ignored\n",
1045                    BL_reg(context) );
1046             break;
1047
1048         default:
1049             INT_BARF( context, 0x21 );
1050             break;
1051         }
1052         break;
1053
1054     case 0x59: /* GET EXTENDED ERROR INFO */
1055         INT21_GetExtendedError( context );
1056         break;
1057
1058     case 0x5a: /* CREATE TEMPORARY FILE */
1059     case 0x5b: /* CREATE NEW FILE */ 
1060     case 0x5c: /* "FLOCK" - RECORD LOCKING */
1061     case 0x5d: /* NETWORK 5D */
1062     case 0x5e: /* NETWORK 5E */
1063     case 0x5f: /* NETWORK 5F */
1064     case 0x60: /* "TRUENAME" - CANONICALIZE FILENAME OR PATH */
1065         INT_Int21Handler( context );
1066         break;
1067
1068     case 0x61: /* UNUSED */
1069         SET_AL( context, 0 );
1070         break;
1071
1072     case 0x62: /* GET PSP ADDRESS */
1073         INT21_GetPSP( context );
1074         break;
1075
1076     case 0x63: /* MISC. LANGUAGE SUPPORT */
1077         switch (AL_reg(context)) {
1078         case 0x00: /* GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE */
1079             TRACE( "GET DOUBLE BYTE CHARACTER SET LEAD-BYTE TABLE\n" );
1080             context->SegDs = INT21_GetHeapSelector( context );
1081             SET_SI( context, offsetof(INT21_HEAP, dbcs_table) );
1082             SET_AL( context, 0 ); /* success */
1083             break;
1084         }
1085         break;
1086
1087     case 0x64: /* OS/2 DOS BOX */
1088         INT_BARF( context, 0x21 );
1089         SET_CFLAG(context);
1090         break;
1091
1092     case 0x65: /* EXTENDED COUNTRY INFORMATION */
1093         INT21_ExtendedCountryInformation( context );
1094         break;
1095
1096     case 0x66: /* GLOBAL CODE PAGE TABLE */
1097         switch (AL_reg(context))
1098         {
1099         case 0x01:
1100             TRACE( "GET GLOBAL CODE PAGE TABLE\n" );
1101             SET_BX( context, GetOEMCP() );
1102             SET_DX( context, GetOEMCP() );
1103             break;
1104         case 0x02:
1105             FIXME( "SET GLOBAL CODE PAGE TABLE, active %d, system %d - ignored\n",
1106                    BX_reg(context), DX_reg(context) );
1107             break;
1108         }
1109         break;
1110
1111     case 0x67: /* SET HANDLE COUNT */
1112         TRACE( "SET HANDLE COUNT to %d\n", BX_reg(context) );
1113         if (SetHandleCount( BX_reg(context) ) < BX_reg(context) )
1114             bSetDOSExtendedError = TRUE;
1115         break;
1116
1117     case 0x68: /* "FFLUSH" - COMMIT FILE */
1118     case 0x69: /* DISK SERIAL NUMBER */
1119     case 0x6a: /* COMMIT FILE */
1120         INT_Int21Handler( context );
1121         break;
1122
1123     case 0x6b: /* NULL FUNCTION FOR CP/M COMPATIBILITY */
1124         SET_AL( context, 0 );
1125         break;
1126
1127     case 0x6c: /* EXTENDED OPEN/CREATE */
1128     case 0x70: /* MS-DOS 7 (Windows95) - ??? (country-specific?)*/
1129     case 0x71: /* MS-DOS 7 (Windows95) - LONG FILENAME FUNCTIONS */
1130     case 0x72: /* MS-DOS 7 (Windows95) - ??? */
1131     case 0x73: /* MULTIPLEXED: Win95 OSR2/Win98 FAT32 calls */
1132     case 0xdc: /* CONNECTION SERVICES - GET CONNECTION NUMBER */
1133     case 0xea: /* NOVELL NETWARE - RETURN SHELL VERSION */
1134         INT_Int21Handler( context );
1135         break;
1136
1137     default:
1138         INT_BARF( context, 0x21 );
1139         break;
1140
1141     } /* END OF SWITCH */
1142
1143     /* Set general error condition. */
1144     if (bSetDOSExtendedError)
1145     {
1146         SET_AX( context, GetLastError() );
1147         SET_CFLAG( context );
1148     }
1149
1150     /* Print error code if carry flag is set. */
1151     if (context->EFlags & 0x0001)
1152         TRACE("failed, error %ld\n", GetLastError() );
1153
1154     TRACE( "returning: AX=%04x BX=%04x CX=%04x DX=%04x "
1155            "SI=%04x DI=%04x DS=%04x ES=%04x EFL=%08lx\n",
1156            AX_reg(context), BX_reg(context), 
1157            CX_reg(context), DX_reg(context), 
1158            SI_reg(context), DI_reg(context),
1159            (WORD)context->SegDs, (WORD)context->SegEs,
1160            context->EFlags );
1161 }