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