]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/initialize.c
[multiple changes]
[thirdparty/gcc.git] / gcc / ada / initialize.c
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 ****************************************************************************/
31
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. */
36
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
42
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
53
54 #include "raise.h"
55 #include <fcntl.h>
56
57 #ifdef __cplusplus
58 extern "C" {
59 #endif
60
61 /******************************************/
62 /* __gnat_initialize (NT-mingw32 Version) */
63 /******************************************/
64
65 char __gnat_wide_text_translation_required = 0;
66 // wide text translation, 0=none, 1=activated
67
68 #if defined (__MINGW32__)
69 #include "mingw32.h"
70 #include <windows.h>
71
72 extern void __gnat_init_float (void);
73 extern void __gnat_install_SEH_handler (void *);
74
75 extern int gnat_argc;
76 extern char **gnat_argv;
77
78 #ifdef GNAT_UNICODE_SUPPORT
79
80 #define EXPAND_ARGV_RATE 128
81
82 static void
83 append_arg (int *index, LPWSTR dir, LPWSTR value,
84 char ***argv, int *last, int quoted)
85 {
86 int size;
87 LPWSTR fullvalue;
88 int vallen = _tcslen (value);
89 int dirlen;
90
91 if (dir == NULL)
92 {
93 /* no dir prefix */
94 dirlen = 0;
95 fullvalue = (LPWSTR) xmalloc ((vallen + 1) * sizeof(TCHAR));
96 }
97 else
98 {
99 /* Add dir first */
100 dirlen = _tcslen (dir);
101
102 fullvalue = (LPWSTR) xmalloc ((dirlen + vallen + 1) * sizeof(TCHAR));
103 _tcscpy (fullvalue, dir);
104 }
105
106 /* Append value */
107
108 if (quoted)
109 {
110 _tcsncpy (fullvalue + dirlen, value + 1, vallen - 1);
111 fullvalue [dirlen + vallen - sizeof(TCHAR)] = _T('\0');
112 }
113 else
114 _tcscpy (fullvalue + dirlen, value);
115
116 if (*last <= *index)
117 {
118 *last += EXPAND_ARGV_RATE;
119 *argv = (char **) xrealloc (*argv, (*last) * sizeof (char *));
120 }
121
122 size = WS2SC (NULL, fullvalue, 0);
123 (*argv)[*index] = (char *) xmalloc (size + sizeof(TCHAR));
124 WS2SC ((*argv)[*index], fullvalue, size);
125
126 free (fullvalue);
127
128 (*index)++;
129 }
130 #endif
131
132 void
133 __gnat_initialize (void *eh ATTRIBUTE_UNUSED)
134 {
135 /* Initialize floating-point coprocessor. This call is needed because
136 the MS libraries default to 64-bit precision instead of 80-bit
137 precision, and we require the full precision for proper operation,
138 given that we have set Max_Digits etc with this in mind */
139 __gnat_init_float ();
140
141 #ifdef GNAT_UNICODE_SUPPORT
142 /* Set current code page for filenames handling. */
143 {
144 char *codepage = getenv ("GNAT_CODE_PAGE");
145
146 /* Default code page is UTF-8. */
147 CurrentCodePage = CP_UTF8;
148
149 if (codepage != NULL)
150 {
151 if (strcmp (codepage, "CP_ACP") == 0)
152 CurrentCodePage = CP_ACP;
153 else if (strcmp (codepage, "CP_UTF8") == 0)
154 CurrentCodePage = CP_UTF8;
155 }
156 }
157
158 /* Set current encoding for the IO. */
159 {
160 char *ccsencoding = getenv ("GNAT_CCS_ENCODING");
161
162 /* Default CCS Encoding. */
163 CurrentCCSEncoding = _O_TEXT;
164 __gnat_wide_text_translation_required = 0;
165
166 if (ccsencoding != NULL)
167 {
168 if (strcmp (ccsencoding, "U16TEXT") == 0)
169 {
170 CurrentCCSEncoding = _O_U16TEXT;
171 __gnat_wide_text_translation_required = 1;
172 }
173 else if (strcmp (ccsencoding, "TEXT") == 0)
174 {
175 CurrentCCSEncoding = _O_TEXT;
176 __gnat_wide_text_translation_required = 0;
177 }
178 else if (strcmp (ccsencoding, "WTEXT") == 0)
179 {
180 CurrentCCSEncoding = _O_WTEXT;
181 __gnat_wide_text_translation_required = 1;
182 }
183 else if (strcmp (ccsencoding, "U8TEXT") == 0)
184 {
185 CurrentCCSEncoding = _O_U8TEXT;
186 __gnat_wide_text_translation_required = 1;
187 }
188 }
189 }
190
191 /* Adjust gnat_argv to support Unicode characters. */
192 {
193 LPWSTR *wargv;
194 int wargc;
195 int k;
196 int last;
197 int argc_expanded = 0;
198 TCHAR result [MAX_PATH];
199 int quoted;
200
201 wargv = CommandLineToArgvW (GetCommandLineW(), &wargc);
202
203 if (wargv != NULL)
204 {
205 /* Set gnat_argv with arguments encoded in UTF-8. */
206 last = wargc + 1;
207 gnat_argv = (char **) xmalloc ((last) * sizeof (char *));
208
209 /* argv[0] is the executable full path-name. */
210
211 SearchPath (NULL, wargv[0], _T(".exe"), MAX_PATH, result, NULL);
212 append_arg (&argc_expanded, NULL, result, &gnat_argv, &last, 0);
213
214 for (k=1; k<wargc; k++)
215 {
216 quoted = (wargv[k][0] == _T('\''));
217
218 /* Check for wildcard expansion if the argument is not quoted. */
219 if (!quoted
220 && (_tcsstr (wargv[k], _T("?")) != 0 ||
221 _tcsstr (wargv[k], _T("*")) != 0))
222 {
223 /* Wilcards are present, append all corresponding matches. */
224 WIN32_FIND_DATA FileData;
225 HANDLE hDir = FindFirstFile (wargv[k], &FileData);
226 LPWSTR dir = NULL;
227 LPWSTR ldir = _tcsrchr (wargv[k], _T('\\'));
228
229 if (ldir == NULL)
230 ldir = _tcsrchr (wargv[k], _T('/'));
231
232 if (hDir == INVALID_HANDLE_VALUE)
233 {
234 /* No match, append arg as-is. */
235 append_arg (&argc_expanded, NULL, wargv[k],
236 &gnat_argv, &last, quoted);
237 }
238 else
239 {
240 if (ldir != NULL)
241 {
242 int n = ldir - wargv[k] + 1;
243 dir = (LPWSTR) xmalloc ((n + 1) * sizeof (TCHAR));
244 _tcsncpy (dir, wargv[k], n);
245 dir[n] = _T('\0');
246 }
247
248 /* Append first match and all remaining ones. */
249
250 do {
251 /* Do not add . and .. special entries */
252
253 if (_tcscmp (FileData.cFileName, _T(".")) != 0
254 && _tcscmp (FileData.cFileName, _T("..")) != 0)
255 append_arg (&argc_expanded, dir, FileData.cFileName,
256 &gnat_argv, &last, 0);
257 } while (FindNextFile (hDir, &FileData));
258
259 FindClose (hDir);
260
261 if (dir != NULL)
262 free (dir);
263 }
264 }
265 else
266 {
267 /* No wildcard. Store parameter as-is. Remove quote if
268 needed. */
269 append_arg (&argc_expanded, NULL, wargv[k],
270 &gnat_argv, &last, quoted);
271 }
272 }
273
274 LocalFree (wargv);
275 gnat_argc = argc_expanded;
276 gnat_argv = (char **) xrealloc
277 (gnat_argv, argc_expanded * sizeof (char *));
278 }
279 }
280 #endif
281
282 /* Note that we do not activate this for the compiler itself to avoid a
283 bootstrap path problem. Older version of gnatbind will generate a call
284 to __gnat_initialize() without argument. Therefore we cannot use eh in
285 this case. It will be possible to remove the following #ifdef at some
286 point. */
287 #ifdef IN_RTS
288 /* Install the Structured Exception handler. */
289 if (eh)
290 __gnat_install_SEH_handler (eh);
291 #endif
292 }
293
294 /******************************************/
295 /* __gnat_initialize (init_float version) */
296 /******************************************/
297
298 #elif defined (__Lynx__) || defined (__FreeBSD__) || defined(__NetBSD__) \
299 || defined (__OpenBSD__)
300
301 extern void __gnat_init_float (void);
302
303 void
304 __gnat_initialize (void *eh ATTRIBUTE_UNUSED)
305 {
306 __gnat_init_float ();
307 }
308
309 /***************************************/
310 /* __gnat_initialize (VxWorks Version) */
311 /***************************************/
312
313 #elif defined(__vxworks)
314
315 extern void __gnat_init_float (void);
316
317 void
318 __gnat_initialize (void *eh)
319 {
320 __gnat_init_float ();
321 }
322
323 #elif defined(_T_HPUX10) || (!defined(IN_RTS) && defined(_X_HPUX10))
324
325 /************************************************/
326 /* __gnat_initialize (PA-RISC HP-UX 10 Version) */
327 /************************************************/
328
329 extern void __main (void);
330
331 void
332 __gnat_initialize (void *eh ATTRIBUTE_UNUSED)
333 {
334 __main ();
335 }
336
337 #else
338
339 /* For all other versions of GNAT, the initialize routine and handler
340 installation do nothing */
341
342 /***************************************/
343 /* __gnat_initialize (Default Version) */
344 /***************************************/
345
346 void
347 __gnat_initialize (void *eh ATTRIBUTE_UNUSED)
348 {
349 }
350
351 #endif
352
353 #ifdef __cplusplus
354 }
355 #endif