Debian Codegraph @

gcc/ada/rtinit.c

raw
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) 2014-2025, 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 implementation for __gnat_runtime_initialize ()
33 which is called in adainit() to do special initialization needed by
34 the GNAT runtime. */
35
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 /* We don't have libiberty, so use malloc. */
45 #define xmalloc(S) malloc (S)
46 #define xrealloc(V,S) realloc (V,S)
47 #else
48 #include "config.h"
49 #include "system.h"
50 #endif
51
52 #include "raise.h"
53 #include <fcntl.h>
54
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58
59 /**************************************************/
60 /* __gnat_runtime_initialize (NT-mingw32 Version) */
61 /**************************************************/
62
63 extern void __gnat_install_handler (void);
64
65 int __gnat_wide_text_translation_required = 0;
66 /* wide text translation, 0=none, 1=activated */
67
68 int __gnat_rt_init_count = 0;
69 /* number of references to the GNAT runtime, this is used to initialize
70 and finalize properly the run-time. */
71
72 #if defined (__MINGW32__)
73 #include <stdlib.h>
74 #include "mingw32.h"
75
76 extern void __gnat_init_float (void);
77
78 extern int gnat_argc;
79 extern char **gnat_argv;
80 extern CRITICAL_SECTION ProcListCS;
81 extern HANDLE ProcListEvt;
82
83 #ifdef GNAT_UNICODE_SUPPORT
84
85 #define EXPAND_ARGV_RATE 128
86
87 int __gnat_do_argv_expansion = 1;
88 #pragma weak __gnat_do_argv_expansion
89
90 /* Assuming we are pointing to the beginning of a quoted part of an
91 argument, skip until the end of the quoted part. */
92 static void skip_quoted_string (const WCHAR **current_in,
93 WCHAR **current_out)
94 {
95 /* Number of backslashes buffered. */
96 int qbs_count = 0;
97
98 /* Pointer to current input character. */
99 const WCHAR *ci = *current_in;
100
101 /* Pointer to next output character. */
102 WCHAR *co = *current_out;
103
104 /* Skip initial quote. */
105 ci++;
106
107 while (*ci)
108 {
109 if (*ci == '\\')
110 {
111 /* Buffer incoming backslashes. */
112 qbs_count++;
113 }
114 else if (*ci == '"')
115 {
116 /* Append qbs_count / 2 backslahes. */
117 for (int i=0; i<qbs_count / 2; i++)
118 {
119 *co = '\\';
120 co++;
121 }
122 if ((qbs_count & 1) == 0)
123 {
124 /* 2n backslashes means that the quotation mark is the end of
125 the quoted portion. */
126 qbs_count = 0;
127 break;
128 }
129 else
130 {
131 /* Otherwise this is a double quote literal. */
132 qbs_count = 0;
133 *co = '"'; co++;
134 }
135 }
136 else
137 {
138 /* If the character is not a double quote we should append
139 qbs_count backslashes. */
140 for (int i=0; i<qbs_count; i++)
141 {
142 *co = '\\';
143 co++;
144 }
145 *co = *ci; co++;
146 qbs_count = 0;
147 }
148 ci++;
149 }
150
151 /* Handle the case in which a nul character was found instead of a closing
152 double quote. In that case consider all the backslashes as literal
153 characters. */
154 if (*ci == '\0')
155 {
156 for (int i=0; i<qbs_count; i++)
157 {
158 *co='\\';
159 co++;
160 }
161 }
162
163 *current_in = ci;
164 *current_out = co;
165 }
166
167 /* Assuming that this is the beginning of an argument. Skip characters
168 until we reach the character right after the last argument character. */
169 static void skip_argument (const WCHAR **current_in,
170 WCHAR **current_out)
171 {
172 /* Number of backslashes buffered. */
173 int bs_count = 0;
174
175 /* Pointer to current input character. */
176 const WCHAR *ci = *current_in;
177
178 /* Pointer to next output character. */
179 WCHAR *co = *current_out;
180
181 while (*ci && ! (*ci == ' ' || *ci == '\t'))
182 {
183 if (*ci == '\\')
184 {
185 /* Buffer incoming backslashes. */
186 bs_count++;
187 }
188 else if (*ci == '"')
189 {
190 /* Append qbs_count / 2 backslahes. */
191 for (int i=0; i< bs_count / 2; i++)
192 {
193 *co = '\\'; co++;
194 }
195 if ((bs_count & 1) == 0)
196 {
197 /* 2n backslashes followed by a quotation mark means that
198 this is a start of a quoted string. */
199 skip_quoted_string (&ci, &co);
200 }
201 else
202 {
203 /* Otherwise this is quotation mark literal. */
204 *co = '"';
205 co++;
206 }
207 bs_count = 0;
208 }
209 else
210 {
211 /* This is a regular character. */
212 /* Backslashes are interpreted literally. */
213 for (int i=0; i<bs_count; i++)
214 {
215 *co = '\\';
216 co++;
217 }
218 bs_count = 0;
219 *co = *ci; co++;
220 }
221 if (*ci != '\0')
222 {
223 ci++;
224 }
225 }
226
227 for (int i=0; i<bs_count; i++)
228 {
229 *co = '\\';
230 co++;
231 }
232
233 /* End the argument with a null character. */
234 *co = '\0';
235 co++;
236
237 *current_in = ci;
238 *current_out = co;
239 }
240
241
242 void __gnat_get_argw (const WCHAR *command_line, WCHAR ***argv, int *argc)
243 {
244 WCHAR *inline_argv;
245 WCHAR *co;
246 int arg_count = 1;
247 const WCHAR *ci;
248
249 inline_argv =
250 (WCHAR *) xmalloc ((wcslen (command_line) + 1) * sizeof (WCHAR));
251 co = inline_argv;
252
253 /* Start iteration on command line characters. */
254 ci = command_line;
255
256 /* Skip command name. Note that if the command line starts with whitechars
257 then the command name will be the empty string. */
258 skip_argument (&ci, &co);
259
260 /* Count remaining arguments. */
261 while (*ci)
262 {
263 /* skip whitechar */
264 while (*ci && (*ci == ' ' || *ci == '\t')) { ci++; }
265 if (*ci)
266 {
267 skip_argument (&ci, &co);
268 arg_count++;
269 }
270 else
271 break;
272 }
273
274 /* Allocate table with pointer to each arguments */
275 argv[0] = (WCHAR **) xmalloc (arg_count * sizeof (WCHAR *));
276
277 for (int idx = 0; idx < arg_count; idx++)
278 {
279 argv[0][idx] = inline_argv;
280 while (*inline_argv)
281 {
282 inline_argv++;
283 }
284 inline_argv++;
285 }
286 *argc = arg_count;
287 }
288
289 static void
290 append_arg (int *index, LPWSTR dir, LPWSTR value,
291 char ***argv, int *last, int quoted)
292 {
293 int size;
294 LPWSTR fullvalue;
295 int vallen = _tcslen (value);
296 int dirlen;
297
298 if (dir == NULL)
299 {
300 /* no dir prefix */
301 dirlen = 0;
302 fullvalue = (LPWSTR) xmalloc ((vallen + 1) * sizeof (TCHAR));
303 }
304 else
305 {
306 /* Add dir first */
307 dirlen = _tcslen (dir);
308
309 fullvalue = (LPWSTR) xmalloc ((dirlen + vallen + 1) * sizeof (TCHAR));
310 _tcscpy (fullvalue, dir);
311 }
312
313 /* Append value */
314
315 if (quoted)
316 {
317 _tcsncpy (fullvalue + dirlen, value + 1, vallen - 1);
318 fullvalue [dirlen + vallen - sizeof (TCHAR)] = _T ('\0');
319 }
320 else
321 _tcscpy (fullvalue + dirlen, value);
322
323 if (*last <= *index)
324 {
325 *last += EXPAND_ARGV_RATE;
326 *argv = (char **) xrealloc (*argv, (*last) * sizeof (char *));
327 }
328
329 size = WS2SC (NULL, fullvalue, 0);
330 (*argv)[*index] = (char *) xmalloc (size + sizeof (TCHAR));
331 WS2SC ((*argv)[*index], fullvalue, size);
332
333 free (fullvalue);
334
335 (*index)++;
336 }
337 #endif
338
339 void
340 __gnat_runtime_initialize (int install_handler)
341 {
342 /* increment the reference counter */
343
344 __gnat_rt_init_count++;
345
346 /* if already initialized return now */
347 if (__gnat_rt_init_count > 1)
348 return;
349
350 /* Initialize floating-point coprocessor. This call is needed because
351 the MS libraries default to 64-bit precision instead of 80-bit
352 precision, and we require the full precision for proper operation,
353 given that we have set Max_Digits etc with this in mind */
354
355 __gnat_init_float ();
356
357 /* Initialize the critical section and event handle for the win32_wait()
358 implementation, see adaint.c */
359
360 InitializeCriticalSection (&ProcListCS);
361 ProcListEvt = CreateEvent (NULL, FALSE, FALSE, NULL);
362
363 #ifdef GNAT_UNICODE_SUPPORT
364 /* Set current code page for filenames handling. */
365 {
366 char *codepage = getenv ("GNAT_CODE_PAGE");
367
368 /* Default code page is UTF-8. */
369 __gnat_current_codepage = CP_UTF8;
370
371 if (codepage != NULL)
372 {
373 if (strcmp (codepage, "CP_ACP") == 0)
374 __gnat_current_codepage = CP_ACP;
375 else if (strcmp (codepage, "CP_UTF8") == 0)
376 __gnat_current_codepage = CP_UTF8;
377 }
378 }
379
380 /* Set current encoding for the IO. */
381 {
382 char *ccsencoding = getenv ("GNAT_CCS_ENCODING");
383
384 /* Default CCS Encoding. */
385 __gnat_current_ccs_encoding = _O_TEXT;
386 __gnat_wide_text_translation_required = 0;
387
388 if (ccsencoding != NULL)
389 {
390 if (strcmp (ccsencoding, "U16TEXT") == 0)
391 {
392 __gnat_current_ccs_encoding = _O_U16TEXT;
393 __gnat_wide_text_translation_required = 1;
394 }
395 else if (strcmp (ccsencoding, "TEXT") == 0)
396 {
397 __gnat_current_ccs_encoding = _O_TEXT;
398 __gnat_wide_text_translation_required = 0;
399 }
400 else if (strcmp (ccsencoding, "WTEXT") == 0)
401 {
402 __gnat_current_ccs_encoding = _O_WTEXT;
403 __gnat_wide_text_translation_required = 1;
404 }
405 else if (strcmp (ccsencoding, "U8TEXT") == 0)
406 {
407 __gnat_current_ccs_encoding = _O_U8TEXT;
408 __gnat_wide_text_translation_required = 1;
409 }
410 }
411 }
412
413 /* Adjust gnat_argv to support Unicode characters. */
414 {
415 LPWSTR *wargv;
416 int wargc;
417 int k;
418 int last;
419 int argc_expanded = 0;
420 TCHAR result [MAX_PATH];
421 int quoted;
422
423 __gnat_get_argw (GetCommandLineW (), &wargv, &wargc);
424
425 if (wargv != NULL)
426 {
427 /* Set gnat_argv with arguments encoded in UTF-8. */
428 last = wargc + 1;
429 gnat_argv = (char **) xmalloc ((last) * sizeof (char *));
430
431 /* argv[0] is the executable full path-name. */
432
433 SearchPath (NULL, wargv[0], _T(".exe"), MAX_PATH, result, NULL);
434 append_arg (&argc_expanded, NULL, result, &gnat_argv, &last, 0);
435
436 for (k=1; k<wargc; k++)
437 {
438 quoted = (wargv[k][0] == _T('\''));
439
440 /* Check for wildcard expansion if the argument is not quoted. */
441 if (!quoted && __gnat_do_argv_expansion
442 && (_tcsstr (wargv[k], _T("?")) != 0 ||
443 _tcsstr (wargv[k], _T("*")) != 0))
444 {
445 /* Wilcards are present, append all corresponding matches. */
446 WIN32_FIND_DATA FileData;
447 HANDLE hDir = FindFirstFile (wargv[k], &FileData);
448 LPWSTR dir = NULL;
449 LPWSTR ldir = _tcsrchr (wargv[k], _T('\\'));
450
451 if (ldir == NULL)
452 ldir = _tcsrchr (wargv[k], _T('/'));
453
454 if (hDir == INVALID_HANDLE_VALUE)
455 {
456 /* No match, append arg as-is. */
457 append_arg (&argc_expanded, NULL, wargv[k],
458 &gnat_argv, &last, quoted);
459 }
460 else
461 {
462 if (ldir != NULL)
463 {
464 int n = ldir - wargv[k] + 1;
465 dir = (LPWSTR) xmalloc ((n + 1) * sizeof (TCHAR));
466 _tcsncpy (dir, wargv[k], n);
467 dir[n] = _T('\0');
468 }
469
470 /* Append first match and all remaining ones. */
471
472 do {
473 /* Do not add . and .. special entries */
474
475 if (_tcscmp (FileData.cFileName, _T(".")) != 0
476 && _tcscmp (FileData.cFileName, _T("..")) != 0)
477 append_arg (&argc_expanded, dir, FileData.cFileName,
478 &gnat_argv, &last, 0);
479 } while (FindNextFile (hDir, &FileData));
480
481 FindClose (hDir);
482
483 if (dir != NULL)
484 free (dir);
485 }
486 }
487 else
488 {
489 /* No wildcard. Store parameter as-is. Remove quote if
490 needed. */
491 append_arg (&argc_expanded, NULL, wargv[k],
492 &gnat_argv, &last,
493 quoted && __gnat_do_argv_expansion);
494 }
495 }
496
497 free (wargv[0]);
498 free (wargv);
499 gnat_argc = argc_expanded;
500 gnat_argv = (char **) xrealloc
501 (gnat_argv, argc_expanded * sizeof (char *));
502 }
503 }
504 #endif
505
506 if (install_handler)
507 __gnat_install_handler();
508 }
509
510 /**************************************************/
511 /* __gnat_runtime_initialize (init_float version) */
512 /**************************************************/
513
514 #elif defined (__Lynx__) || defined (__FreeBSD__) || defined(__NetBSD__) \
515 || defined (__OpenBSD__)
516
517 extern void __gnat_init_float (void);
518
519 void
520 __gnat_runtime_initialize(int install_handler)
521 {
522 /* increment the reference counter */
523
524 __gnat_rt_init_count++;
525
526 /* if already initialized return now */
527 if (__gnat_rt_init_count > 1)
528 return;
529
530 __gnat_init_float ();
531
532 if (install_handler)
533 __gnat_install_handler();
534 }
535
536 /***********************************************/
537 /* __gnat_runtime_initialize (VxWorks Version) */
538 /***********************************************/
539
540 #elif defined(__vxworks)
541
542 extern void __gnat_init_float (void);
543
544 void
545 __gnat_runtime_initialize(int install_handler)
546 {
547 /* increment the reference counter */
548
549 __gnat_rt_init_count++;
550
551 /* if already initialized return now */
552 if (__gnat_rt_init_count > 1)
553 return;
554
555 __gnat_init_float ();
556
557 if (install_handler)
558 __gnat_install_handler();
559 }
560
561 #else
562
563 /***********************************************/
564 /* __gnat_runtime_initialize (default version) */
565 /***********************************************/
566
567 void
568 __gnat_runtime_initialize(int install_handler)
569 {
570 /* increment the reference counter */
571
572 __gnat_rt_init_count++;
573
574 /* if already initialized return now */
575 if (__gnat_rt_init_count > 1)
576 return;
577
578 if (install_handler)
579 __gnat_install_handler();
580 }
581
582 #endif
583
584 #ifdef __cplusplus
585 }
586 #endif