Added LGPL standard comment, and copyright notices where necessary.
[wine/multimedia.git] / dlls / msvcrt / console.c
blobe7e2461e95fb97e49ac980bddcbc96457182dc39
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 MSVCRT_console_in = GetStdHandle(STD_INPUT_HANDLE);
52 /* FIXME: Should be initialised with:
53 * CreateFileA("CONIN$", GENERIC_READ, FILE_SHARE_READ,
54 * NULL, OPEN_EXISTING, 0, (HANDLE)NULL);
57 MSVCRT_console_out= CreateFileA("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE,
58 NULL, OPEN_EXISTING, 0, (HANDLE)NULL);
60 if ((MSVCRT_console_in == INVALID_HANDLE_VALUE) ||
61 (MSVCRT_console_out== INVALID_HANDLE_VALUE))
62 WARN(":Console handle Initialisation FAILED!\n");
65 /* INTERNAL: Free console handles */
66 void msvcrt_free_console(void)
68 TRACE(":Closing console handles\n");
69 CloseHandle(MSVCRT_console_in);
70 CloseHandle(MSVCRT_console_out);
73 /*********************************************************************
74 * _cputs (MSVCRT.@)
76 int _cputs(const char* str)
78 DWORD count;
79 int retval = MSVCRT_EOF;
81 LOCK_CONSOLE;
82 if (WriteConsoleA(MSVCRT_console_out, str, strlen(str), &count, NULL)
83 && count == 1)
84 retval = 0;
85 UNLOCK_CONSOLE;
86 return retval;
89 /*********************************************************************
90 * _getch (MSVCRT.@)
92 int _getch(void)
94 int retval = MSVCRT_EOF;
96 LOCK_CONSOLE;
97 if (__MSVCRT_console_buffer != MSVCRT_EOF)
99 retval = __MSVCRT_console_buffer;
100 __MSVCRT_console_buffer = MSVCRT_EOF;
102 else
104 INPUT_RECORD ir;
105 DWORD count;
106 DWORD mode = 0;
108 GetConsoleMode(MSVCRT_console_in, &mode);
109 if(mode)
110 SetConsoleMode(MSVCRT_console_in, 0);
112 do {
113 if (ReadConsoleInputA(MSVCRT_console_in, &ir, 1, &count))
115 /* Only interested in ASCII chars */
116 if (ir.EventType == KEY_EVENT &&
117 ir.Event.KeyEvent.bKeyDown &&
118 ir.Event.KeyEvent.uChar.AsciiChar)
120 retval = ir.Event.KeyEvent.uChar.AsciiChar;
121 break;
124 else
125 break;
126 } while(1);
127 if (mode)
128 SetConsoleMode(MSVCRT_console_in, mode);
130 UNLOCK_CONSOLE;
131 return retval;
134 /*********************************************************************
135 * _putch (MSVCRT.@)
137 int _putch(int c)
139 int retval = MSVCRT_EOF;
140 DWORD count;
141 LOCK_CONSOLE;
142 if (WriteConsoleA(MSVCRT_console_out, &c, 1, &count, NULL) && count == 1)
143 retval = c;
144 UNLOCK_CONSOLE;
145 return retval;
148 /*********************************************************************
149 * _getche (MSVCRT.@)
151 int _getche(void)
153 int retval;
154 LOCK_CONSOLE;
155 retval = _getch();
156 if (retval != MSVCRT_EOF)
157 retval = _putch(retval);
158 UNLOCK_CONSOLE;
159 return retval;
162 /*********************************************************************
163 * _cgets (MSVCRT.@)
165 char* _cgets(char* str)
167 char *buf = str + 2;
168 int c;
169 str[1] = 0; /* Length */
170 /* FIXME: No editing of string supported */
171 LOCK_CONSOLE;
174 if (str[1] >= str[0] || (str[1]++, c = _getche()) == MSVCRT_EOF || c == '\n')
175 break;
176 *buf++ = c & 0xff;
177 } while (1);
178 UNLOCK_CONSOLE;
179 *buf = '\0';
180 return str + 2;
183 /*********************************************************************
184 * _ungetch (MSVCRT.@)
186 int _ungetch(int c)
188 int retval = MSVCRT_EOF;
189 LOCK_CONSOLE;
190 if (c != MSVCRT_EOF && __MSVCRT_console_buffer == MSVCRT_EOF)
191 retval = __MSVCRT_console_buffer = c;
192 UNLOCK_CONSOLE;
193 return retval;
196 /* helper function for _cscanf. Returns the value of character c in the
197 * given base, or -1 if the given character is not a digit of the base.
199 static int char2digit(char c, int base) {
200 if ((c>='0') && (c<='9') && (c<='0'+base-1)) return (c-'0');
201 if (base<=10) return -1;
202 if ((c>='A') && (c<='Z') && (c<='A'+base-11)) return (c-'A'+10);
203 if ((c>='a') && (c<='z') && (c<='a'+base-11)) return (c-'a'+10);
204 return -1;
207 /*********************************************************************
208 * _cscanf (MSVCRT.@)
210 int _cscanf(const char* format, ...)
212 /* NOTE: If you extend this function, extend MSVCRT_fscanf in file.c too */
213 int rd = 0;
214 int nch;
215 va_list ap;
216 if (!*format) return 0;
217 WARN("\"%s\": semi-stub\n", format);
218 va_start(ap, format);
219 LOCK_CONSOLE;
220 nch = _getch();
221 while (*format) {
222 /* a whitespace character in the format string causes scanf to read,
223 * but not store, all consecutive white-space characters in the input
224 * up to the next non-white-space character. One white space character
225 * in the input matches any number (including zero) and combination of
226 * white-space characters in the input. */
227 if (isspace(*format)) {
228 /* skip whitespace */
229 while ((nch!=MSVCRT_EOF) && isspace(nch))
230 nch = _getch();
232 /* a format specification causes scanf to read and convert characters
233 * in the input into values of a specified type. The value is assigned
234 * to an argument in the argument list. Format specifications have
235 * the form %[*][width][{h | l | I64 | L}]type */
236 /* FIXME: unimplemented: h/l/I64/L modifiers and some type specs. */
237 else if (*format == '%') {
238 int st = 0; int suppress = 0; int width = 0;
239 int base, number_signed;
240 format++;
241 /* look for leading asterisk, which means 'suppress assignment of
242 * this field'. */
243 if (*format=='*') {
244 format++;
245 suppress=1;
247 /* look for width specification */
248 while (isdigit(*format)) {
249 width*=10;
250 width+=*format++ - '0';
252 if (width==0) width=-1; /* no width spec seen */
253 switch(*format) {
254 case '%': /* read a percent symbol */
255 if (nch!='%') break;
256 nch = _getch();
257 break;
258 case 'x':
259 case 'X': /* hexadecimal integer. */
260 base = 16; number_signed = 0;
261 goto number;
262 case 'o': /* octal integer */
263 base = 8; number_signed = 0;
264 goto number;
265 case 'u': /* unsigned decimal integer */
266 base = 10; number_signed = 0;
267 goto number;
268 case 'd': /* signed decimal integer */
269 base = 10; number_signed = 1;
270 goto number;
271 case 'i': /* generic integer */
272 base = 0; number_signed = 1;
273 number: {
274 /* read an integer */
275 int*val = suppress ? NULL : va_arg(ap, int*);
276 int cur = 0; int negative = 0; int seendigit=0;
277 /* skip initial whitespace */
278 while ((nch!=MSVCRT_EOF) && isspace(nch))
279 nch = _getch();
280 /* get sign */
281 if (number_signed && (nch == '-' || nch == '+')) {
282 negative = (nch=='-');
283 nch = _getch();
284 if (width>0) width--;
286 /* look for leading indication of base */
287 if (width!=0 && nch == '0') {
288 nch = _getch();
289 if (width>0) width--;
290 seendigit=1;
291 if (width!=0 && (nch=='x' || nch=='X')) {
292 if (base==0)
293 base=16;
294 if (base==16) {
295 nch = _getch();
296 if (width>0) width--;
297 seendigit=0;
299 } else if (base==0)
300 base = 8;
302 if (base==0)
303 base=10;
304 /* throw away leading zeros */
305 while (width!=0 && nch=='0') {
306 nch = _getch();
307 if (width>0) width--;
308 seendigit=1;
310 /* get first digit. Keep working copy negative, as the
311 * range of negative numbers in two's complement notation
312 * is one larger than the range of positive numbers. */
313 if (width!=0 && char2digit(nch, base)!=-1) {
314 cur = -char2digit(nch, base);
315 nch = _getch();
316 if (width>0) width--;
317 seendigit=1;
319 /* read until no more digits */
320 while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
321 cur = cur*base + char2digit(nch, base);
322 nch = _getch();
323 if (width>0) width--;
324 seendigit=1;
326 /* negate parsed number if non-negative */
327 if (!negative) cur=-cur;
328 /* okay, done! */
329 if (!seendigit) break; /* not a valid number */
330 st = 1;
331 if (!suppress) *val = cur;
333 break;
334 case 'e':
335 case 'E':
336 case 'f':
337 case 'g':
338 case 'G': { /* read a float */
339 float*val = suppress ? NULL : va_arg(ap, float*);
340 float cur = 0;
341 int negative = 0;
342 /* skip initial whitespace */
343 while ((nch!=MSVCRT_EOF) && isspace(nch))
344 nch = _getch();
345 /* get sign. */
346 if (nch == '-' || nch == '+') {
347 negative = (nch=='-');
348 if (width>0) width--;
349 if (width==0) break;
350 nch = _getch();
352 /* get first digit. */
353 if (!isdigit(nch)) break;
354 cur = (nch - '0') * (negative ? -1 : 1);
355 nch = _getch();
356 if (width>0) width--;
357 /* read until no more digits */
358 while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
359 cur = cur*10 + (nch - '0');
360 nch = _getch();
361 if (width>0) width--;
363 /* handle decimals */
364 if (width!=0 && nch == '.') {
365 float dec = 1;
366 nch = _getch();
367 if (width>0) width--;
368 while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
369 dec /= 10;
370 cur += dec * (nch - '0');
371 nch = _getch();
372 if (width>0) width--;
375 /* handle exponent */
376 if (width!=0 && (nch == 'e' || nch == 'E')) {
377 int exponent = 0, negexp = 0;
378 float expcnt;
379 nch = _getch();
380 if (width>0) width--;
381 /* possible sign on the exponent */
382 if (width!=0 && (nch=='+' || nch=='-')) {
383 negexp = (nch=='-');
384 nch = _getch();
385 if (width>0) width--;
387 /* exponent digits */
388 while (width!=0 && (nch!=MSVCRT_EOF) && isdigit(nch)) {
389 exponent *= 10;
390 exponent += (nch - '0');
391 nch = _getch();
392 if (width>0) width--;
394 /* update 'cur' with this exponent. */
395 expcnt = negexp ? .1 : 10;
396 while (exponent!=0) {
397 if (exponent&1)
398 cur*=expcnt;
399 exponent/=2;
400 expcnt=expcnt*expcnt;
403 st = 1;
404 if (!suppress) *val = cur;
406 break;
407 case 's': { /* read a word */
408 char*str = suppress ? NULL : va_arg(ap, char*);
409 char*sptr = str;
410 /* skip initial whitespace */
411 while ((nch!=MSVCRT_EOF) && isspace(nch))
412 nch = _getch();
413 /* read until whitespace */
414 while (width!=0 && (nch!=MSVCRT_EOF) && !isspace(nch)) {
415 if (!suppress) *sptr++ = nch;
416 st++;
417 nch = _getch();
418 if (width>0) width--;
420 /* terminate */
421 if (!suppress) *sptr = 0;
422 TRACE("read word: %s\n", str);
424 break;
425 default: FIXME("unhandled: %%%c\n", *format);
426 /* From spec: "if a percent sign is followed by a character
427 * that has no meaning as a format-control character, that
428 * character and the following characters are treated as
429 * an ordinary sequence of characters, that is, a sequence
430 * of characters that must match the input. For example,
431 * to specify that a percent-sign character is to be input,
432 * use %%."
433 * LEAVING AS-IS because we catch bugs better that way. */
435 if (st && !suppress) rd++;
436 else break;
438 /* a non-white-space character causes scanf to read, but not store,
439 * a matching non-white-space character. */
440 else {
441 /* check for character match */
442 if (nch == *format)
443 nch = _getch();
444 else break;
446 format++;
448 if (nch != MSVCRT_EOF)
449 _ungetch(nch);
450 UNLOCK_CONSOLE;
451 va_end(ap);
452 TRACE("returning %d\n", rd);
453 return rd;
456 /*********************************************************************
457 * _kbhit (MSVCRT.@)
459 int _kbhit(void)
461 int retval = 0;
463 LOCK_CONSOLE;
464 if (__MSVCRT_console_buffer != MSVCRT_EOF)
465 retval = 1;
466 else
468 /* FIXME: There has to be a faster way than this in Win32.. */
469 INPUT_RECORD *ir = NULL;
470 DWORD count = 0, i;
472 GetNumberOfConsoleInputEvents(MSVCRT_console_in, &count);
474 if (count && (ir = MSVCRT_malloc(count * sizeof(INPUT_RECORD))) &&
475 PeekConsoleInputA(MSVCRT_console_in, ir, count, &count))
476 for(i = 0; i < count - 1; i++)
478 if (ir[i].EventType == KEY_EVENT &&
479 ir[i].Event.KeyEvent.bKeyDown &&
480 ir[i].Event.KeyEvent.uChar.AsciiChar)
482 retval = 1;
483 break;
486 if (ir)
487 MSVCRT_free(ir);
489 UNLOCK_CONSOLE;
490 return retval;
494 /*********************************************************************
495 * _cprintf (MSVCRT.@)
497 int _cprintf(const char* format, ...)
499 char buf[2048], *mem = buf;
500 int written, resize = sizeof(buf), retval;
501 va_list valist;
503 va_start( valist, format );
504 /* There are two conventions for snprintf failing:
505 * Return -1 if we truncated, or
506 * Return the number of bytes that would have been written
507 * The code below handles both cases
509 while ((written = _snprintf( mem, resize, format, valist )) == -1 ||
510 written > resize)
512 resize = (written == -1 ? resize * 2 : written + 1);
513 if (mem != buf)
514 MSVCRT_free (mem);
515 if (!(mem = (char *)MSVCRT_malloc(resize)))
516 return MSVCRT_EOF;
517 va_start( valist, format );
519 va_end(valist);
520 LOCK_CONSOLE;
521 retval = _cputs( mem );
522 UNLOCK_CONSOLE;
523 if (mem != buf)
524 MSVCRT_free (mem);
525 return retval;