msvcrt: symbol undecoration: Now correctly parsing the template forms.
[wine] / dlls / msvcrt / console.c
1 /*
2  * msvcrt.dll console functions
3  *
4  * Copyright 2000 Jon Griffiths
5  *
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.
10  *
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.
15  *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
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
22  */
23
24 #include "msvcrt.h"
25 #include "wincon.h"
26 #include "mtdll.h"
27 #include "wine/debug.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
30
31
32
33 /* MT */
34 #define LOCK_CONSOLE   _mlock(_CONIO_LOCK)
35 #define UNLOCK_CONSOLE _munlock(_CONIO_LOCK)
36
37 static HANDLE MSVCRT_console_in = INVALID_HANDLE_VALUE;
38 static HANDLE MSVCRT_console_out= INVALID_HANDLE_VALUE;
39 static int __MSVCRT_console_buffer = MSVCRT_EOF;
40
41 /* INTERNAL: Initialise console handles */
42 void msvcrt_init_console(void)
43 {
44   TRACE(":Opening console handles\n");
45
46   MSVCRT_console_in = CreateFileA("CONIN$", GENERIC_READ, FILE_SHARE_READ,
47                                   NULL, OPEN_EXISTING, 0, NULL);
48   MSVCRT_console_out= CreateFileA("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE,
49                                     NULL, OPEN_EXISTING, 0, NULL);
50
51   if ((MSVCRT_console_in == INVALID_HANDLE_VALUE) ||
52       (MSVCRT_console_out== INVALID_HANDLE_VALUE))
53     WARN(":Console handle Initialisation FAILED!\n");
54 }
55
56 /* INTERNAL: Free console handles */
57 void msvcrt_free_console(void)
58 {
59   TRACE(":Closing console handles\n");
60   CloseHandle(MSVCRT_console_in);
61   CloseHandle(MSVCRT_console_out);
62 }
63
64 /*********************************************************************
65  *              _cputs (MSVCRT.@)
66  */
67 int CDECL _cputs(const char* str)
68 {
69   DWORD count;
70   int retval = MSVCRT_EOF;
71
72   LOCK_CONSOLE;
73   if (WriteConsoleA(MSVCRT_console_out, str, strlen(str), &count, NULL)
74       && count == 1)
75     retval = 0;
76   UNLOCK_CONSOLE;
77   return retval;
78 }
79
80 /*********************************************************************
81  *              _getch (MSVCRT.@)
82  */
83 int CDECL _getch(void)
84 {
85   int retval = MSVCRT_EOF;
86
87   LOCK_CONSOLE;
88   if (__MSVCRT_console_buffer != MSVCRT_EOF)
89   {
90     retval = __MSVCRT_console_buffer;
91     __MSVCRT_console_buffer = MSVCRT_EOF;
92   }
93   else
94   {
95     INPUT_RECORD ir;
96     DWORD count;
97     DWORD mode = 0;
98
99     GetConsoleMode(MSVCRT_console_in, &mode);
100     if(mode)
101       SetConsoleMode(MSVCRT_console_in, 0);
102
103     do {
104       if (ReadConsoleInputA(MSVCRT_console_in, &ir, 1, &count))
105       {
106         /* Only interested in ASCII chars */
107         if (ir.EventType == KEY_EVENT &&
108             ir.Event.KeyEvent.bKeyDown &&
109             ir.Event.KeyEvent.uChar.AsciiChar)
110         {
111           retval = ir.Event.KeyEvent.uChar.AsciiChar;
112           break;
113         }
114       }
115       else
116         break;
117     } while(1);
118     if (mode)
119       SetConsoleMode(MSVCRT_console_in, mode);
120   }
121   UNLOCK_CONSOLE;
122   return retval;
123 }
124
125 /*********************************************************************
126  *              _putch (MSVCRT.@)
127  */
128 int CDECL _putch(int c)
129 {
130   int retval = MSVCRT_EOF;
131   DWORD count;
132   LOCK_CONSOLE;
133   if (WriteConsoleA(MSVCRT_console_out, &c, 1, &count, NULL) && count == 1)
134     retval = c;
135   UNLOCK_CONSOLE;
136   return retval;
137 }
138
139 /*********************************************************************
140  *              _getche (MSVCRT.@)
141  */
142 int CDECL _getche(void)
143 {
144   int retval;
145   LOCK_CONSOLE;
146   retval = _getch();
147   if (retval != MSVCRT_EOF)
148     retval = _putch(retval);
149   UNLOCK_CONSOLE;
150   return retval;
151 }
152
153 /*********************************************************************
154  *              _cgets (MSVCRT.@)
155  */
156 char* CDECL _cgets(char* str)
157 {
158   char *buf = str + 2;
159   DWORD got;
160   DWORD conmode = 0;
161
162   TRACE("(%p)\n", str);
163   str[1] = 0; /* Length */
164   LOCK_CONSOLE;
165   GetConsoleMode(MSVCRT_console_in, &conmode);
166   SetConsoleMode(MSVCRT_console_in, ENABLE_LINE_INPUT|ENABLE_ECHO_INPUT|ENABLE_PROCESSED_INPUT);
167
168   if(ReadConsoleA(MSVCRT_console_in, buf, str[0], &got, NULL)) {
169     if(buf[got-2] == '\r') {
170       buf[got-2] = 0;
171       str[1] = got-2;
172     }
173     else if(got == 1 && buf[got-1] == '\n') {
174       buf[0] = 0;
175       str[1] = 0;
176     }
177     else if(got == str[0] && buf[got-1] == '\r') {
178       buf[got-1] = 0;
179       str[1] = got-1;
180     }
181     else
182       str[1] = got;
183   }
184   else
185     buf = NULL;
186   SetConsoleMode(MSVCRT_console_in, conmode);
187   UNLOCK_CONSOLE;
188   return buf;
189 }
190
191 /*********************************************************************
192  *              _ungetch (MSVCRT.@)
193  */
194 int CDECL _ungetch(int c)
195 {
196   int retval = MSVCRT_EOF;
197   LOCK_CONSOLE;
198   if (c != MSVCRT_EOF && __MSVCRT_console_buffer == MSVCRT_EOF)
199     retval = __MSVCRT_console_buffer = c;
200   UNLOCK_CONSOLE;
201   return retval;
202 }
203
204 /*********************************************************************
205  *              _kbhit (MSVCRT.@)
206  */
207 int CDECL _kbhit(void)
208 {
209   int retval = 0;
210
211   LOCK_CONSOLE;
212   if (__MSVCRT_console_buffer != MSVCRT_EOF)
213     retval = 1;
214   else
215   {
216     /* FIXME: There has to be a faster way than this in Win32.. */
217     INPUT_RECORD *ir = NULL;
218     DWORD count = 0, i;
219
220     GetNumberOfConsoleInputEvents(MSVCRT_console_in, &count);
221
222     if (count && (ir = MSVCRT_malloc(count * sizeof(INPUT_RECORD))) &&
223         PeekConsoleInputA(MSVCRT_console_in, ir, count, &count))
224       for(i = 0; i < count - 1; i++)
225       {
226         if (ir[i].EventType == KEY_EVENT &&
227             ir[i].Event.KeyEvent.bKeyDown &&
228             ir[i].Event.KeyEvent.uChar.AsciiChar)
229         {
230           retval = 1;
231           break;
232         }
233       }
234     MSVCRT_free(ir);
235   }
236   UNLOCK_CONSOLE;
237   return retval;
238 }
239
240
241 /*********************************************************************
242  *              _cprintf (MSVCRT.@)
243  */
244 int CDECL _cprintf(const char* format, ...)
245 {
246   char buf[2048], *mem = buf;
247   int written, resize = sizeof(buf), retval;
248   va_list valist;
249
250   va_start( valist, format );
251   /* There are two conventions for snprintf failing:
252    * Return -1 if we truncated, or
253    * Return the number of bytes that would have been written
254    * The code below handles both cases
255    */
256   while ((written = MSVCRT_vsnprintf( mem, resize, format, valist )) == -1 ||
257           written > resize)
258   {
259     resize = (written == -1 ? resize * 2 : written + 1);
260     if (mem != buf)
261       MSVCRT_free (mem);
262     if (!(mem = MSVCRT_malloc(resize)))
263       return MSVCRT_EOF;
264     va_start( valist, format );
265   }
266   va_end(valist);
267   LOCK_CONSOLE;
268   retval = _cputs( mem );
269   UNLOCK_CONSOLE;
270   if (mem != buf)
271     MSVCRT_free (mem);
272   return retval;
273 }