Split the MSVCRT implementation headers from the public headers.
[wine] / dlls / msvcrt / main.c
1 /*
2  * msvcrt.dll initialisation 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 #include "msvcrt.h"
21
22 #include "wine/debug.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
25
26 /* Index to TLS */
27 DWORD msvcrt_tls_index;
28
29 static inline BOOL msvcrt_init_tls(void);
30 static inline BOOL msvcrt_free_tls(void);
31 const char* msvcrt_get_reason(DWORD reason) WINE_UNUSED;
32
33 /*********************************************************************
34  *                  Init
35  */
36 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
37 {
38   thread_data_t *tls;
39
40   TRACE("(%p, %s, %p) pid(%lx), tid(%lx), tls(%ld)\n",
41         hinstDLL, msvcrt_get_reason(fdwReason), lpvReserved,
42         GetCurrentProcessId(), GetCurrentThreadId(),
43         (long)msvcrt_tls_index);
44
45   switch (fdwReason)
46   {
47   case DLL_PROCESS_ATTACH:
48     if (!msvcrt_init_tls())
49       return FALSE;
50     msvcrt_init_mt_locks();
51     msvcrt_init_io();
52     msvcrt_init_console();
53     msvcrt_init_args();
54     MSVCRT_setlocale(0, "C");
55     TRACE("finished process init\n");
56     break;
57   case DLL_THREAD_ATTACH:
58     break;
59   case DLL_PROCESS_DETACH:
60     msvcrt_free_mt_locks();
61     msvcrt_free_io();
62     msvcrt_free_console();
63     msvcrt_free_args();
64     if (!msvcrt_free_tls())
65       return FALSE;
66     TRACE("finished process free\n");
67     break;
68   case DLL_THREAD_DETACH:
69     /* Free TLS */
70     tls = TlsGetValue(msvcrt_tls_index);
71     if (tls) HeapFree(GetProcessHeap(), 0, tls);
72     TRACE("finished thread free\n");
73     break;
74   }
75   return TRUE;
76 }
77
78 static inline BOOL msvcrt_init_tls(void)
79 {
80   msvcrt_tls_index = TlsAlloc();
81
82   if (msvcrt_tls_index == TLS_OUT_OF_INDEXES)
83   {
84     ERR("TlsAlloc() failed!\n");
85     return FALSE;
86   }
87   return TRUE;
88 }
89
90 static inline BOOL msvcrt_free_tls(void)
91 {
92   if (!TlsFree(msvcrt_tls_index))
93   {
94     ERR("TlsFree() failed!\n");
95     return FALSE;
96   }
97   return TRUE;
98 }
99
100 const char* msvcrt_get_reason(DWORD reason)
101 {
102   switch (reason)
103   {
104   case DLL_PROCESS_ATTACH: return "DLL_PROCESS_ATTACH";
105   case DLL_PROCESS_DETACH: return "DLL_PROCESS_DETACH";
106   case DLL_THREAD_ATTACH:  return "DLL_THREAD_ATTACH";
107   case DLL_THREAD_DETACH:  return "DLL_THREAD_DETACH";
108   }
109   return "UNKNOWN";
110 }
111
112
113 /*********************************************************************
114  *              $I10_OUTPUT (MSVCRT.@)
115  * Function not really understood but needed to make the DLL work
116  */
117 void MSVCRT_I10_OUTPUT(void)
118 {
119   /* FIXME: This is probably data, not a function */
120 }
121
122 /*********************************************************************
123  *              __unDNameEx (MSVCRT.@)
124  *
125  * Demangle a C++ identifier.
126  *
127  * PARAMS
128  *  OutStr   [O] If not NULL, the place to put the demangled string
129  *  mangled  [I] Mangled name of the function
130  *  OutStrLen[I] Length of OutStr
131  *  memget   [I] Function to allocate memory with
132  *  memfree  [I] Function to free memory with
133  *  unknown  [?] Unknown, possibly a call back
134  *  flags    [I] Flags determining demangled format
135  *
136  * RETURNS
137  *  Success: A string pointing to the unmangled name, allocated with memget.
138  *  Failure: NULL.
139  */
140 char* __unDNameEx(char * OutStr, const char* mangled, int OutStrLen,
141                   malloc_func_t memget, free_func_t memfree,
142                   void * unknown, unsigned short int flags)
143 {
144   FIXME("(%p,%s,%d,%p,%p,%p,%x) stub!\n",
145           OutStr, mangled, OutStrLen, memget, memfree, unknown, flags);
146
147   /* FIXME: The code in tools/winebuild/msmangle.c is pretty complete and
148    * could be used here.
149    */
150
151   /* Experimentation reveals the following flag meanings when set:
152    * 0x0001 - Don't show __ in calling convention
153    * 0x0002 - Don't show calling convention at all
154    * 0x0004 - Don't show function/method return value
155    * 0x0010 - Same as 0x1
156    * 0x0080 - Don't show access specifier (public/protected/private)
157    * 0x0200 - Don't show static specifier
158    * 0x0800 - Unknown, passed by type_info::name()
159    * 0x1000 - Only report the variable/class name
160    * 0x2000 - Unknown, passed by type_info::name()
161    */
162   /* Duplicate the mangled name; for comparisons it doesn't matter anyway */
163   if( OutStr == NULL) {
164       OutStrLen = strlen(mangled) + 1;
165       OutStr = memget( OutStrLen);
166   }
167   strncpy( OutStr, mangled, OutStrLen);
168   return OutStr;
169 }
170
171
172 /*********************************************************************
173  *              __unDName (MSVCRT.@)
174  */
175 char* __unDName(char * OutStr, const char* mangled, int OutStrLen,
176                 malloc_func_t memget, free_func_t memfree,
177                 unsigned short int flags)
178 {
179    return __unDNameEx( OutStr, mangled, OutStrLen, memget, memfree, 0, flags);
180 }