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