New module 'msvc-inval'.
[gnulib.git] / lib / msvc-inval.c
1 /* Invalid parameter handler for MSVC runtime libraries.
2    Copyright (C) 2011 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License along
15    with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 #include <config.h>
19
20 /* Specification.  */
21 #include "msvc-inval.h"
22
23 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
24
25 # ifdef STATUS_GNULIB_INVALID_PARAMETER
26
27 /* Get declarations of the Win32 API functions.  */
28 #  define WIN32_LEAN_AND_MEAN
29 #  include <windows.h>
30
31 static void cdecl
32 gl_msvc_invalid_parameter_handler (const wchar_t *expression,
33                                    const wchar_t *function,
34                                    const wchar_t *file,
35                                    unsigned int line,
36                                    uintptr_t dummy)
37 {
38   RaiseException (STATUS_GNULIB_INVALID_PARAMETER, 0, 0, NULL);
39 }
40
41 static int gl_msvc_inval_initialized /* = 0 */;
42
43 void
44 gl_msvc_inval_ensure_handler (void)
45 {
46   if (gl_msvc_inval_initialized == 0)
47     {
48       _set_invalid_parameter_handler (gl_msvc_invalid_parameter_handler);
49       gl_msvc_inval_initialized = 1;
50     }
51 }
52
53 # else
54
55 jmp_buf gl_msvc_inval_restart;
56
57 void cdecl
58 gl_msvc_invalid_parameter_handler (const wchar_t *expression,
59                                    const wchar_t *function,
60                                    const wchar_t *file,
61                                    unsigned int line,
62                                    uintptr_t dummy)
63 {
64   longjmp (gl_msvc_inval_restart, 1);
65 }
66
67 # endif
68
69 #endif