2014-12-12 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ada / initialize.c
blob9426c9e5aee868875c8153cfc683c99b8c737f4e
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * I N I T I A L I Z E *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2014, Free Software Foundation, Inc. *
10 * *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 3, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. *
17 * *
18 * As a special exception under Section 7 of GPL version 3, you are granted *
19 * additional permissions described in the GCC Runtime Library Exception, *
20 * version 3.1, as published by the Free Software Foundation. *
21 * *
22 * You should have received a copy of the GNU General Public License and *
23 * a copy of the GCC Runtime Library Exception along with this program; *
24 * see the files COPYING3 and COPYING.RUNTIME respectively. If not, see *
25 * <http://www.gnu.org/licenses/>. *
26 * *
27 * GNAT was originally developed by the GNAT team at New York University. *
28 * Extensive contributions were provided by Ada Core Technologies Inc. *
29 * *
30 ****************************************************************************/
32 /* This unit provides default implementation for __gnat_initialize ()
33 which is called before the elaboration of the partition. It is provided
34 in a separate file/object so that users can replace it easily.
35 The default implementation should be null on most targets. */
37 /* The following include is here to meet the published VxWorks requirement
38 that the __vxworks header appear before any other include. */
39 #ifdef __vxworks
40 #include "vxWorks.h"
41 #endif
43 #ifdef IN_RTS
44 #include "tconfig.h"
45 #include "tsystem.h"
46 /* We don't have libiberty, so use malloc. */
47 #define xmalloc(S) malloc (S)
48 #define xrealloc(V,S) realloc (V,S)
49 #else
50 #include "config.h"
51 #include "system.h"
52 #endif
54 #include "raise.h"
55 #include <fcntl.h>
57 #ifdef __cplusplus
58 extern "C" {
59 #endif
61 /******************************************/
62 /* __gnat_initialize (NT-mingw32 Version) */
63 /******************************************/
65 int __gnat_wide_text_translation_required = 0;
66 /* wide text translation, 0=none, 1=activated */
68 #if defined (__MINGW32__)
69 #include "mingw32.h"
70 #include <windows.h>
72 extern void __gnat_init_float (void);
73 extern void __gnat_install_SEH_handler (void *);
75 extern int gnat_argc;
76 extern char **gnat_argv;
77 extern CRITICAL_SECTION ProcListCS;
78 extern HANDLE ProcListEvt;
80 #ifdef GNAT_UNICODE_SUPPORT
82 #define EXPAND_ARGV_RATE 128
84 static void
85 append_arg (int *index, LPWSTR dir, LPWSTR value,
86 char ***argv, int *last, int quoted)
88 int size;
89 LPWSTR fullvalue;
90 int vallen = _tcslen (value);
91 int dirlen;
93 if (dir == NULL)
95 /* no dir prefix */
96 dirlen = 0;
97 fullvalue = (LPWSTR) xmalloc ((vallen + 1) * sizeof(TCHAR));
99 else
101 /* Add dir first */
102 dirlen = _tcslen (dir);
104 fullvalue = (LPWSTR) xmalloc ((dirlen + vallen + 1) * sizeof(TCHAR));
105 _tcscpy (fullvalue, dir);
108 /* Append value */
110 if (quoted)
112 _tcsncpy (fullvalue + dirlen, value + 1, vallen - 1);
113 fullvalue [dirlen + vallen - sizeof(TCHAR)] = _T('\0');
115 else
116 _tcscpy (fullvalue + dirlen, value);
118 if (*last <= *index)
120 *last += EXPAND_ARGV_RATE;
121 *argv = (char **) xrealloc (*argv, (*last) * sizeof (char *));
124 size = WS2SC (NULL, fullvalue, 0);
125 (*argv)[*index] = (char *) xmalloc (size + sizeof(TCHAR));
126 WS2SC ((*argv)[*index], fullvalue, size);
128 free (fullvalue);
130 (*index)++;
132 #endif
134 void
135 __gnat_initialize (void *eh ATTRIBUTE_UNUSED)
137 /* Initialize floating-point coprocessor. This call is needed because
138 the MS libraries default to 64-bit precision instead of 80-bit
139 precision, and we require the full precision for proper operation,
140 given that we have set Max_Digits etc with this in mind */
141 __gnat_init_float ();
143 /* Initialize the critical section and event handle for the win32_wait()
144 implementation, see adaint.c */
145 InitializeCriticalSection (&ProcListCS);
146 ProcListEvt = CreateEvent (NULL, FALSE, FALSE, NULL);
148 #ifdef GNAT_UNICODE_SUPPORT
149 /* Set current code page for filenames handling. */
151 char *codepage = getenv ("GNAT_CODE_PAGE");
153 /* Default code page is UTF-8. */
154 CurrentCodePage = CP_UTF8;
156 if (codepage != NULL)
158 if (strcmp (codepage, "CP_ACP") == 0)
159 CurrentCodePage = CP_ACP;
160 else if (strcmp (codepage, "CP_UTF8") == 0)
161 CurrentCodePage = CP_UTF8;
165 /* Set current encoding for the IO. */
167 char *ccsencoding = getenv ("GNAT_CCS_ENCODING");
169 /* Default CCS Encoding. */
170 CurrentCCSEncoding = _O_TEXT;
171 __gnat_wide_text_translation_required = 0;
173 if (ccsencoding != NULL)
175 if (strcmp (ccsencoding, "U16TEXT") == 0)
177 CurrentCCSEncoding = _O_U16TEXT;
178 __gnat_wide_text_translation_required = 1;
180 else if (strcmp (ccsencoding, "TEXT") == 0)
182 CurrentCCSEncoding = _O_TEXT;
183 __gnat_wide_text_translation_required = 0;
185 else if (strcmp (ccsencoding, "WTEXT") == 0)
187 CurrentCCSEncoding = _O_WTEXT;
188 __gnat_wide_text_translation_required = 1;
190 else if (strcmp (ccsencoding, "U8TEXT") == 0)
192 CurrentCCSEncoding = _O_U8TEXT;
193 __gnat_wide_text_translation_required = 1;
198 /* Adjust gnat_argv to support Unicode characters. */
200 LPWSTR *wargv;
201 int wargc;
202 int k;
203 int last;
204 int argc_expanded = 0;
205 TCHAR result [MAX_PATH];
206 int quoted;
208 wargv = CommandLineToArgvW (GetCommandLineW(), &wargc);
210 if (wargv != NULL)
212 /* Set gnat_argv with arguments encoded in UTF-8. */
213 last = wargc + 1;
214 gnat_argv = (char **) xmalloc ((last) * sizeof (char *));
216 /* argv[0] is the executable full path-name. */
218 SearchPath (NULL, wargv[0], _T(".exe"), MAX_PATH, result, NULL);
219 append_arg (&argc_expanded, NULL, result, &gnat_argv, &last, 0);
221 for (k=1; k<wargc; k++)
223 quoted = (wargv[k][0] == _T('\''));
225 /* Check for wildcard expansion if the argument is not quoted. */
226 if (!quoted
227 && (_tcsstr (wargv[k], _T("?")) != 0 ||
228 _tcsstr (wargv[k], _T("*")) != 0))
230 /* Wilcards are present, append all corresponding matches. */
231 WIN32_FIND_DATA FileData;
232 HANDLE hDir = FindFirstFile (wargv[k], &FileData);
233 LPWSTR dir = NULL;
234 LPWSTR ldir = _tcsrchr (wargv[k], _T('\\'));
236 if (ldir == NULL)
237 ldir = _tcsrchr (wargv[k], _T('/'));
239 if (hDir == INVALID_HANDLE_VALUE)
241 /* No match, append arg as-is. */
242 append_arg (&argc_expanded, NULL, wargv[k],
243 &gnat_argv, &last, quoted);
245 else
247 if (ldir != NULL)
249 int n = ldir - wargv[k] + 1;
250 dir = (LPWSTR) xmalloc ((n + 1) * sizeof (TCHAR));
251 _tcsncpy (dir, wargv[k], n);
252 dir[n] = _T('\0');
255 /* Append first match and all remaining ones. */
257 do {
258 /* Do not add . and .. special entries */
260 if (_tcscmp (FileData.cFileName, _T(".")) != 0
261 && _tcscmp (FileData.cFileName, _T("..")) != 0)
262 append_arg (&argc_expanded, dir, FileData.cFileName,
263 &gnat_argv, &last, 0);
264 } while (FindNextFile (hDir, &FileData));
266 FindClose (hDir);
268 if (dir != NULL)
269 free (dir);
272 else
274 /* No wildcard. Store parameter as-is. Remove quote if
275 needed. */
276 append_arg (&argc_expanded, NULL, wargv[k],
277 &gnat_argv, &last, quoted);
281 LocalFree (wargv);
282 gnat_argc = argc_expanded;
283 gnat_argv = (char **) xrealloc
284 (gnat_argv, argc_expanded * sizeof (char *));
287 #endif
289 /* Note that we do not activate this for the compiler itself to avoid a
290 bootstrap path problem. Older version of gnatbind will generate a call
291 to __gnat_initialize() without argument. Therefore we cannot use eh in
292 this case. It will be possible to remove the following #ifdef at some
293 point. */
294 #ifdef IN_RTS
295 /* Install the Structured Exception handler. */
296 if (eh)
297 __gnat_install_SEH_handler (eh);
298 #endif
301 /******************************************/
302 /* __gnat_initialize (init_float version) */
303 /******************************************/
305 #elif defined (__Lynx__) || defined (__FreeBSD__) || defined(__NetBSD__) \
306 || defined (__OpenBSD__)
308 extern void __gnat_init_float (void);
310 void
311 __gnat_initialize (void *eh ATTRIBUTE_UNUSED)
313 __gnat_init_float ();
316 /***************************************/
317 /* __gnat_initialize (VxWorks Version) */
318 /***************************************/
320 #elif defined(__vxworks)
322 extern void __gnat_init_float (void);
324 void
325 __gnat_initialize (void *eh)
327 __gnat_init_float ();
330 #elif defined(_T_HPUX10) || (!defined(IN_RTS) && defined(_X_HPUX10))
332 /************************************************/
333 /* __gnat_initialize (PA-RISC HP-UX 10 Version) */
334 /************************************************/
336 extern void __main (void);
338 void
339 __gnat_initialize (void *eh ATTRIBUTE_UNUSED)
341 __main ();
344 #else
346 /* For all other versions of GNAT, the initialize routine and handler
347 installation do nothing */
349 /***************************************/
350 /* __gnat_initialize (Default Version) */
351 /***************************************/
353 void
354 __gnat_initialize (void *eh ATTRIBUTE_UNUSED)
358 #endif
360 #ifdef __cplusplus
362 #endif