Fixed some dll separation issues.
[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 "ntddk.h"
30 #include "wine/winbase16.h"
31 #include "dosexe.h"
32 #include "miscemu.h"
33 #include "msdos.h"
34 #include "file.h"
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(int21);
39
40 void WINAPI DOSVM_Int21Handler_Ioctl( CONTEXT86 *context )
41 {
42   static const WCHAR emmxxxx0W[] = {'E','M','M','X','X','X','X','0',0};
43   const DOS_DEVICE *dev = DOSFS_GetDeviceByHandle(
44       DosFileHandleToWin32Handle(BX_reg(context)) );
45
46   if (dev && !strcmpiW( dev->name, emmxxxx0W )) {
47     EMS_Ioctl_Handler(context);
48     return;
49   }
50
51   switch (AL_reg(context))
52   {
53   case 0x0b: /* SET SHARING RETRY COUNT */
54       TRACE("IOCTL - SET SHARING RETRY COUNT pause %d retries %d\n",
55            CX_reg(context), DX_reg(context));
56       if (!CX_reg(context))
57       {
58          AX_reg(context) = 1;
59          SET_CFLAG(context);
60          break;
61       }
62       DOSMEM_LOL()->sharing_retry_delay = CX_reg(context);
63       if (!DX_reg(context))
64          DOSMEM_LOL()->sharing_retry_count = DX_reg(context);
65       RESET_CFLAG(context);
66       break;
67   default:
68       DOS3Call( context );
69   }
70 }
71
72 /***********************************************************************
73  *           DOSVM_Int21Handler
74  *
75  * int 21h real-mode handler. Most calls are passed directly to DOS3Call.
76  */
77 void WINAPI DOSVM_Int21Handler( CONTEXT86 *context )
78 {
79     RESET_CFLAG(context);  /* Not sure if this is a good idea */
80
81     if(AH_reg(context) == 0x0c) /* FLUSH BUFFER AND READ STANDARD INPUT */
82     {
83         BYTE al = AL_reg(context); /* Input function to execute after flush. */
84
85         /* FIXME: buffers are not flushed */
86
87         /*
88          * If AL is not one of 0x01, 0x06, 0x07, 0x08, or 0x0a,
89          * the buffer is flushed but no input is attempted.
90          */
91         if(al != 0x01 && al != 0x06 && al != 0x07 && al != 0x08 && al != 0x0a)
92             return;
93
94         AH_reg(context) = al;
95     }
96
97     switch(AH_reg(context))
98     {
99     case 0x00: /* TERMINATE PROGRAM */
100         TRACE("TERMINATE PROGRAM\n");
101         MZ_Exit( context, FALSE, 0 );
102         break;
103
104     case 0x01: /* READ CHARACTER FROM STANDARD INPUT, WITH ECHO */
105         TRACE("DIRECT CHARACTER INPUT WITH ECHO\n");
106         DOSVM_Int16ReadChar(&AL_reg(context), NULL, FALSE);
107         DOSVM_PutChar(AL_reg(context));
108         break;
109
110     case 0x02: /* WRITE CHARACTER TO STANDARD OUTPUT */
111         TRACE("Write Character to Standard Output\n");
112         DOSVM_PutChar(DL_reg(context));
113         break;
114
115     case 0x06: /* DIRECT CONSOLE IN/OUTPUT */
116         /* FIXME: Use DOSDEV_Peek/Read/Write(DOSDEV_Console(),...) !! */
117         if (DL_reg(context) == 0xff) {
118             static char scan = 0;
119             TRACE("Direct Console Input\n");
120             if (scan) {
121                 /* return pending scancode */
122                 AL_reg(context) = scan;
123                 RESET_ZFLAG(context);
124                 scan = 0;
125             } else {
126                 char ascii;
127                 if (DOSVM_Int16ReadChar(&ascii,&scan,TRUE)) {
128                     DOSVM_Int16ReadChar(&ascii,&scan,FALSE);
129                     /* return ASCII code */
130                     AL_reg(context) = ascii;
131                     RESET_ZFLAG(context);
132                     /* return scan code on next call only if ascii==0 */
133                     if (ascii) scan = 0;
134                 } else {
135                     /* nothing pending, clear everything */
136                     AL_reg(context) = 0;
137                     SET_ZFLAG(context);
138                     scan = 0; /* just in case */
139                 }
140             }
141         } else {
142             TRACE("Direct Console Output\n");
143             DOSVM_PutChar(DL_reg(context));
144         }
145         break;
146
147     case 0x07: /* DIRECT CHARACTER INPUT WITHOUT ECHO */
148         /* FIXME: Use DOSDEV_Peek/Read(DOSDEV_Console(),...) !! */
149         TRACE("DIRECT CHARACTER INPUT WITHOUT ECHO\n");
150         DOSVM_Int16ReadChar(&AL_reg(context), NULL, FALSE);
151         break;
152
153     case 0x08: /* CHARACTER INPUT WITHOUT ECHO */
154         /* FIXME: Use DOSDEV_Peek/Read(DOSDEV_Console(),...) !! */
155         TRACE("CHARACTER INPUT WITHOUT ECHO\n");
156         DOSVM_Int16ReadChar(&AL_reg(context), NULL, FALSE);
157         break;
158
159     case 0x0b: /* GET STDIN STATUS */
160         {
161             BIOSDATA *data = BIOS_DATA;
162             if(data->FirstKbdCharPtr == data->NextKbdCharPtr)
163                 AL_reg(context) = 0;
164             else
165                 AL_reg(context) = 0xff;
166         }
167         break;
168
169     case 0x25: /* SET INTERRUPT VECTOR */
170         DOSVM_SetRMHandler( AL_reg(context),
171                             (FARPROC16)MAKESEGPTR( context->SegDs, DX_reg(context)));
172         break;
173
174     case 0x35: /* GET INTERRUPT VECTOR */
175         TRACE("GET INTERRUPT VECTOR 0x%02x\n",AL_reg(context));
176         {
177             FARPROC16 addr = DOSVM_GetRMHandler( AL_reg(context) );
178             context->SegEs = SELECTOROF(addr);
179             BX_reg(context) = OFFSETOF(addr);
180         }
181         break;
182
183     case 0x40: /* WRITE TO FILE OR DEVICE */
184         /* Writes to stdout are handled here. */
185         if (BX_reg(context) == 1) {
186           BYTE *ptr = CTX_SEG_OFF_TO_LIN(context,
187                                          context->SegDs,
188                                          context->Edx);
189           int i;
190
191           for(i=0; i<CX_reg(context); i++)
192             DOSVM_PutChar(ptr[i]);
193         } else
194           DOS3Call( context );
195         break;
196
197     case 0x44: /* IOCTL */
198         DOSVM_Int21Handler_Ioctl( context );
199         break;
200
201     case 0x4b: /* "EXEC" - LOAD AND/OR EXECUTE PROGRAM */
202         TRACE("EXEC %s\n", (LPCSTR)CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx ));
203         if (!MZ_Exec( context, CTX_SEG_OFF_TO_LIN(context, context->SegDs, context->Edx),
204                       AL_reg(context), CTX_SEG_OFF_TO_LIN(context, context->SegEs, context->Ebx) ))
205         {
206             AX_reg(context) = GetLastError();
207             SET_CFLAG(context);
208         }
209         break;
210
211     case 0x4c: /* "EXIT" - TERMINATE WITH RETURN CODE */
212         TRACE("EXIT with return code %d\n",AL_reg(context));
213         MZ_Exit( context, FALSE, AL_reg(context) );
214         break;
215
216     case 0x4d: /* GET RETURN CODE */
217         TRACE("GET RETURN CODE (ERRORLEVEL)\n");
218         AX_reg(context) = DOSVM_retval;
219         DOSVM_retval = 0;
220         break;
221
222     case 0x50: /* SET CURRENT PROCESS ID (SET PSP ADDRESS) */
223         TRACE("SET CURRENT PROCESS ID (SET PSP ADDRESS)\n");
224         DOSVM_psp = BX_reg(context);
225         break;
226
227     case 0x51: /* GET PSP ADDRESS */
228         TRACE("GET CURRENT PROCESS ID (GET PSP ADDRESS)\n");
229         /* FIXME: should we return the original DOS PSP upon */
230         /*        Windows startup ? */
231         BX_reg(context) = DOSVM_psp;
232         break;
233
234     case 0x52: /* "SYSVARS" - GET LIST OF LISTS */
235         TRACE("SYSVARS - GET LIST OF LISTS\n");
236         {
237             context->SegEs = HIWORD(DOS_LOLSeg);
238             BX_reg(context) = FIELD_OFFSET(DOS_LISTOFLISTS, ptr_first_DPB);
239         }
240         break;
241
242     case 0x62: /* GET PSP ADDRESS */
243         TRACE("GET CURRENT PSP ADDRESS\n");
244         /* FIXME: should we return the original DOS PSP upon */
245         /*        Windows startup ? */
246         BX_reg(context) = DOSVM_psp;
247         break;
248
249     default:
250         DOS3Call( context );
251     }
252 }