Fixed a couple of crashes.
[wine.git] / dlls / msvcrt / console.c
blob4130b45f53d3efd6fabc96112aaaf5f1638136e0
1 /*
2 * msvcrt.dll console functions
4 * Copyright 2000 Jon Griffiths
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Note: init and free don't need MT locking since they are called at DLL
21 * (de)attachment time, which is syncronised for us
23 #include "msvcrt.h"
24 #include "wincon.h"
26 #include "msvcrt/conio.h"
27 #include "msvcrt/malloc.h"
28 #include "msvcrt/stdio.h"
29 #include "mtdll.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
37 /* MT */
38 #define LOCK_CONSOLE _mlock(_CONIO_LOCK)
39 #define UNLOCK_CONSOLE _munlock(_CONIO_LOCK)
41 static HANDLE MSVCRT_console_in = INVALID_HANDLE_VALUE;
42 static HANDLE MSVCRT_console_out= INVALID_HANDLE_VALUE;
43 static int __MSVCRT_console_buffer = MSVCRT_EOF;
45 /* INTERNAL: Initialise console handles */
46 void msvcrt_init_console(void)
48 TRACE(":Opening console handles\n");
50 DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_INPUT_HANDLE),
51 GetCurrentProcess(), &MSVCRT_console_in, 0, FALSE, DUPLICATE_SAME_ACCESS);
53 /* FIXME: Should be initialised with:
54 * CreateFileA("CONIN$", GENERIC_READ, FILE_SHARE_READ,
55 * NULL, OPEN_EXISTING, 0, NULL);
58 MSVCRT_console_out= CreateFileA("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE,
59 NULL, OPEN_EXISTING, 0, NULL);
61 if ((MSVCRT_console_in == INVALID_HANDLE_VALUE) ||
62 (MSVCRT_console_out== INVALID_HANDLE_VALUE))
63 WARN(":Console handle Initialisation FAILED!\n");
66 /* INTERNAL: Free console handles */
67 void msvcrt_free_console(void)
69 TRACE(":Closing console handles\n");
70 CloseHandle(MSVCRT_console_in);
71 CloseHandle(MSVCRT_console_out);
74 /*********************************************************************
75 * _cputs (MSVCRT.@)
77 int _cputs(const char* str)
79 DWORD count;
80 int retval = MSVCRT_EOF;
82 LOCK_CONSOLE;
83 if (WriteConsoleA(MSVCRT_console_out, str, strlen(str), &count, NULL)
84 && count == 1)
85 retval = 0;
86 UNLOCK_CONSOLE;
87 return retval;
90 /*********************************************************************
91 * _getch (MSVCRT.@)
93 int _getch(void)
95 int retval = MSVCRT_EOF;
97 LOCK_CONSOLE;
98 if (__MSVCRT_console_buffer != MSVCRT_EOF)
100 retval = __MSVCRT_console_buffer;
101 __MSVCRT_console_buffer = MSVCRT_EOF;
103 else
105 INPUT_RECORD ir;
106 DWORD count;
107 DWORD mode = 0;
109 GetConsoleMode(MSVCRT_console_in, &mode);
110 if(mode)
111 SetConsoleMode(MSVCRT_console_in, 0);
113 do {
114 if (ReadConsoleInputA(MSVCRT_console_in, &ir, 1, &count))
116 /* Only interested in ASCII chars */
117 if (ir.EventType == KEY_EVENT &&
118 ir.Event.KeyEvent.bKeyDown &&
119 ir.Event.KeyEvent.uChar.AsciiChar)
121 retval = ir.Event.KeyEvent.uChar.AsciiChar;
122 break;
125 else
126 break;
127 } while(1);
128 if (mode)
129 SetConsoleMode(MSVCRT_console_in, mode);
131 UNLOCK_CONSOLE;
132 return retval;
135 /*********************************************************************
136 * _putch (MSVCRT.@)
138 int _putch(int c)
140 int retval = MSVCRT_EOF;
141 DWORD count;
142 LOCK_CONSOLE;
143 if (WriteConsoleA(MSVCRT_console_out, &c, 1, &count, NULL) && count == 1)
144 retval = c;
145 UNLOCK_CONSOLE;
146 return retval;
149 /*********************************************************************
150 * _getche (MSVCRT.@)
152 int _getche(void)
154 int retval;
155 LOCK_CONSOLE;
156 retval = _getch();
157 if (retval != MSVCRT_EOF)
158 retval = _putch(retval);
159 UNLOCK_CONSOLE;
160 return retval;
163 /*********************************************************************
164 * _cgets (MSVCRT.@)
166 char* _cgets(char* str)
168 char *buf = str + 2;
169 int c;
170 str[1] = 0; /* Length */
171 /* FIXME: No editing of string supported */
172 LOCK_CONSOLE;
175 if (str[1] >= str[0] || (str[1]++, c = _getche()) == MSVCRT_EOF || c == '\n')
176 break;
177 *buf++ = c & 0xff;
178 } while (1);
179 UNLOCK_CONSOLE;
180 *buf = '\0';
181 return str + 2;
184 /*********************************************************************
185 * _ungetch (MSVCRT.@)
187 int _ungetch(int c)
189 int retval = MSVCRT_EOF;
190 LOCK_CONSOLE;
191 if (c != MSVCRT_EOF && __MSVCRT_console_buffer == MSVCRT_EOF)
192 retval = __MSVCRT_console_buffer = c;
193 UNLOCK_CONSOLE;
194 return retval;
197 /*********************************************************************
198 * _kbhit (MSVCRT.@)
200 int _kbhit(void)
202 int retval = 0;
204 LOCK_CONSOLE;
205 if (__MSVCRT_console_buffer != MSVCRT_EOF)
206 retval = 1;
207 else
209 /* FIXME: There has to be a faster way than this in Win32.. */
210 INPUT_RECORD *ir = NULL;
211 DWORD count = 0, i;
213 GetNumberOfConsoleInputEvents(MSVCRT_console_in, &count);
215 if (count && (ir = MSVCRT_malloc(count * sizeof(INPUT_RECORD))) &&
216 PeekConsoleInputA(MSVCRT_console_in, ir, count, &count))
217 for(i = 0; i < count - 1; i++)
219 if (ir[i].EventType == KEY_EVENT &&
220 ir[i].Event.KeyEvent.bKeyDown &&
221 ir[i].Event.KeyEvent.uChar.AsciiChar)
223 retval = 1;
224 break;
227 if (ir)
228 MSVCRT_free(ir);
230 UNLOCK_CONSOLE;
231 return retval;
235 /*********************************************************************
236 * _cprintf (MSVCRT.@)
238 int _cprintf(const char* format, ...)
240 char buf[2048], *mem = buf;
241 int written, resize = sizeof(buf), retval;
242 va_list valist;
244 va_start( valist, format );
245 /* There are two conventions for snprintf failing:
246 * Return -1 if we truncated, or
247 * Return the number of bytes that would have been written
248 * The code below handles both cases
250 while ((written = _snprintf( mem, resize, format, valist )) == -1 ||
251 written > resize)
253 resize = (written == -1 ? resize * 2 : written + 1);
254 if (mem != buf)
255 MSVCRT_free (mem);
256 if (!(mem = (char *)MSVCRT_malloc(resize)))
257 return MSVCRT_EOF;
258 va_start( valist, format );
260 va_end(valist);
261 LOCK_CONSOLE;
262 retval = _cputs( mem );
263 UNLOCK_CONSOLE;
264 if (mem != buf)
265 MSVCRT_free (mem);
266 return retval;