objc/
[official-gcc.git] / gcc / ada / sysdep.c
blob03818b19605cd45149d53c7422061c4068cdcf90
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * S Y S D E P *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2005 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 2, 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. See the GNU General Public License *
17 * for more details. You should have received a copy of the GNU General *
18 * Public License distributed with GNAT; see file COPYING. If not, write *
19 * to the Free Software Foundation, 51 Franklin Street, Fifth Floor, *
20 * Boston, MA 02110-1301, USA. *
21 * *
22 * As a special exception, if you link this file with other files to *
23 * produce an executable, this file does not by itself cause the resulting *
24 * executable to be covered by the GNU General Public License. This except- *
25 * ion does not however invalidate any other reasons why the executable *
26 * file might be covered by the GNU Public License. *
27 * *
28 * GNAT was originally developed by the GNAT team at New York University. *
29 * Extensive contributions were provided by Ada Core Technologies Inc. *
30 * *
31 ****************************************************************************/
33 /* This file contains system dependent symbols that are referenced in the
34 GNAT Run Time Library */
36 #ifdef __vxworks
37 #include "ioLib.h"
38 #include "selectLib.h"
39 #include "vxWorks.h"
40 #endif
41 #ifdef IN_RTS
42 #define POSIX
43 #include "tconfig.h"
44 #include "tsystem.h"
45 #include <fcntl.h>
46 #include <sys/stat.h>
47 #include <time.h>
48 #ifdef VMS
49 #include <unixio.h>
50 #endif
51 #else
52 #include "config.h"
53 #include "system.h"
54 #endif
56 #include "adaint.h"
59 mode_read_text
60 open text file for reading
61 rt for DOS and Windows NT, r for Unix
63 mode_write_text
64 truncate to zero length or create text file for writing
65 wt for DOS and Windows NT, w for Unix
67 mode_append_text
68 append; open or create text file for writing at end-of-file
69 at for DOS and Windows NT, a for Unix
71 mode_read_binary
72 open binary file for reading
73 rb for DOS and Windows NT, r for Unix
75 mode_write_binary
76 truncate to zero length or create binary file for writing
77 wb for DOS and Windows NT, w for Unix
79 mode_append_binary
80 append; open or create binary file for writing at end-of-file
81 ab for DOS and Windows NT, a for Unix
83 mode_read_text_plus
84 open text file for update (reading and writing)
85 r+t for DOS and Windows NT, r+ for Unix
87 mode_write_text_plus
88 truncate to zero length or create text file for update
89 w+t for DOS and Windows NT, w+ for Unix
91 mode_append_text_plus
92 append; open or create text file for update, writing at end-of-file
93 a+t for DOS and Windows NT, a+ for Unix
95 mode_read_binary_plus
96 open binary file for update (reading and writing)
97 r+b for DOS and Windows NT, r+ for Unix
99 mode_write_binary_plus
100 truncate to zero length or create binary file for update
101 w+b for DOS and Windows NT, w+ for Unix
103 mode_append_binary_plus
104 append; open or create binary file for update, writing at end-of-file
105 a+b for DOS and Windows NT, a+ for Unix
107 Notes:
109 (1) Opening a file with read mode fails if the file does not exist or
110 cannot be read.
112 (2) Opening a file with append mode causes all subsequent writes to the
113 file to be forced to the then current end-of-file, regardless of
114 intervening calls to the fseek function.
116 (3) When a file is opened with update mode, both input and output may be
117 performed on the associated stream. However, output may not be directly
118 followed by input without an intervening call to the fflush function or
119 to a file positioning function (fseek, fsetpos, or rewind), and input
120 may not be directly followed by output without an intervening call to a
121 file positioning function, unless the input operation encounters
122 end-of-file.
124 The other target dependent declarations here are for the two functions
125 __gnat_set_binary_mode and __gnat_set_text_mode:
127 void __gnat_set_binary_mode (int handle);
128 void __gnat_set_text_mode (int handle);
130 These functions have no effect in Unix (or similar systems where there is
131 no distinction between binary and text files), but in DOS (and similar
132 systems where text mode does CR/LF translation), these functions allow
133 the mode of the stream with the given handle (fileno can be used to get
134 the handle of a stream) to be changed dynamically. The returned result
135 is 0 if no error occurs and -1 if an error occurs.
137 Finally there is a boolean (character) variable
139 char __gnat_text_translation_required;
141 which is zero (false) in Unix mode, and one (true) in DOS mode, with a
142 true value indicating that text translation is required on text files
143 and that fopen supports the trailing t and b modifiers.
147 #if defined(WINNT) || defined (MSDOS) || defined (__EMX__)
148 static const char *mode_read_text = "rt";
149 static const char *mode_write_text = "wt";
150 static const char *mode_append_text = "at";
151 static const char *mode_read_binary = "rb";
152 static const char *mode_write_binary = "wb";
153 static const char *mode_append_binary = "ab";
154 static const char *mode_read_text_plus = "r+t";
155 static const char *mode_write_text_plus = "w+t";
156 static const char *mode_append_text_plus = "a+t";
157 static const char *mode_read_binary_plus = "r+b";
158 static const char *mode_write_binary_plus = "w+b";
159 static const char *mode_append_binary_plus = "a+b";
160 const char __gnat_text_translation_required = 1;
162 void
163 __gnat_set_binary_mode (int handle)
165 _setmode (handle, O_BINARY);
168 void
169 __gnat_set_text_mode (int handle)
171 _setmode (handle, O_TEXT);
174 #ifdef __MINGW32__
175 #include <windows.h>
177 /* Return the name of the tty. Under windows there is no name for
178 the tty, so this function, if connected to a tty, returns the generic name
179 "console". */
181 char *
182 __gnat_ttyname (int filedes)
184 if (isatty (filedes))
185 return "console";
186 else
187 return NULL;
190 /* This function is needed to fix a bug under Win95/98. Under these plateforms
191 doing :
192 ch1 = getch();
193 ch2 = fgetc (stdin);
195 will put the same character into ch1 and ch2. It seem that the character
196 read by getch() is not correctly removed from the buffer. Even a
197 fflush(stdin) does not fix the bug. This bug does not appear under Window
198 NT. So we have two version of this routine below one for 95/98 and one for
199 NT/2000 version of Windows. There is also a special routine (winflushinit)
200 that will be called only the first time to check which version of Windows
201 we are running running on to set the right routine to use.
203 This problem occurs when using Text_IO.Get_Line after Text_IO.Get_Immediate
204 for example.
206 Calling FlushConsoleInputBuffer just after getch() fix the bug under
207 95/98. */
209 static void winflush_init (void);
211 static void winflush_95 (void);
213 static void winflush_nt (void);
215 /* winflusfunction is set first to the winflushinit function which will check
216 the OS version 95/98 or NT/2000 */
218 static void (*winflush_function) (void) = winflush_init;
220 /* This function does the runtime check of the OS version and then sets
221 winflush_function to the appropriate function and then call it. */
223 static void
224 winflush_init (void)
226 DWORD dwVersion = GetVersion();
228 if (dwVersion < 0x80000000) /* Windows NT/2000 */
229 winflush_function = winflush_nt;
230 else /* Windows 95/98 */
231 winflush_function = winflush_95;
233 (*winflush_function)(); /* Perform the 'flush' */
237 static void winflush_95 (void)
239 FlushConsoleInputBuffer (GetStdHandle (STD_INPUT_HANDLE));
242 static void winflush_nt (void)
244 /* Does nothing as there is no problem under NT. */
246 #endif
248 #else
250 static const char *mode_read_text = "r";
251 static const char *mode_write_text = "w";
252 static const char *mode_append_text = "a";
253 static const char *mode_read_binary = "r";
254 static const char *mode_write_binary = "w";
255 static const char *mode_append_binary = "a";
256 static const char *mode_read_text_plus = "r+";
257 static const char *mode_write_text_plus = "w+";
258 static const char *mode_append_text_plus = "a+";
259 static const char *mode_read_binary_plus = "r+";
260 static const char *mode_write_binary_plus = "w+";
261 static const char *mode_append_binary_plus = "a+";
262 const char __gnat_text_translation_required = 0;
264 /* These functions do nothing in non-DOS systems. */
266 void
267 __gnat_set_binary_mode (int handle ATTRIBUTE_UNUSED)
271 void
272 __gnat_set_text_mode (int handle ATTRIBUTE_UNUSED)
275 char *
276 __gnat_ttyname (int filedes)
278 #ifndef __vxworks
279 extern char *ttyname (int);
281 return ttyname (filedes);
283 #else
284 return "";
286 #endif
288 #endif
290 #if defined (linux) || defined (sun) || defined (sgi) || defined (__EMX__) \
291 || (defined (__osf__) && ! defined (__alpha_vxworks)) || defined (WINNT) \
292 || defined (__MACHTEN__) || defined (__hpux__) || defined (_AIX) \
293 || (defined (__svr4__) && defined (i386)) || defined (__Lynx__) \
294 || defined (__CYGWIN__) || defined (__FreeBSD__)
296 #ifdef __MINGW32__
297 #if OLD_MINGW
298 #include <termios.h>
299 #else
300 #include <conio.h> /* for getch(), kbhit() */
301 #endif
302 #else
303 #include <termios.h>
304 #endif
306 #else
307 #if defined (VMS)
308 extern char *decc$ga_stdscr;
309 static int initted = 0;
310 #endif
311 #endif
313 /* Implements the common processing for getc_immediate and
314 getc_immediate_nowait. */
316 extern void getc_immediate (FILE *, int *, int *);
317 extern void getc_immediate_nowait (FILE *, int *, int *, int *);
318 extern void getc_immediate_common (FILE *, int *, int *, int *, int);
320 /* Called by Get_Immediate (Foo); */
322 void
323 getc_immediate (FILE *stream, int *ch, int *end_of_file)
325 int avail;
327 getc_immediate_common (stream, ch, end_of_file, &avail, 1);
330 /* Called by Get_Immediate (Foo, Available); */
332 void
333 getc_immediate_nowait (FILE *stream, int *ch, int *end_of_file, int *avail)
335 getc_immediate_common (stream, ch, end_of_file, avail, 0);
338 /* Called by getc_immediate () and getc_immediate_nowait () */
340 void
341 getc_immediate_common (FILE *stream,
342 int *ch,
343 int *end_of_file,
344 int *avail,
345 int waiting)
347 #if defined (linux) || defined (sun) || defined (sgi) || defined (__EMX__) \
348 || (defined (__osf__) && ! defined (__alpha_vxworks)) \
349 || defined (__CYGWIN32__) || defined (__MACHTEN__) || defined (__hpux__) \
350 || defined (_AIX) || (defined (__svr4__) && defined (i386)) \
351 || defined (__Lynx__) || defined (__FreeBSD__)
352 char c;
353 int nread;
354 int good_one = 0;
355 int eof_ch = 4; /* Ctrl-D */
356 int fd = fileno (stream);
357 struct termios otermios_rec, termios_rec;
359 if (isatty (fd))
361 tcgetattr (fd, &termios_rec);
362 memcpy (&otermios_rec, &termios_rec, sizeof (struct termios));
364 /* Set RAW mode, with no echo */
365 termios_rec.c_lflag = termios_rec.c_lflag & ~ICANON & ~ECHO;
367 #if defined(linux) || defined (sun) || defined (sgi) || defined (__EMX__) \
368 || defined (__osf__) || defined (__MACHTEN__) || defined (__hpux__) \
369 || defined (_AIX) || (defined (__svr4__) && defined (i386)) \
370 || defined (__Lynx__) || defined (__FreeBSD__)
371 eof_ch = termios_rec.c_cc[VEOF];
373 /* If waiting (i.e. Get_Immediate (Char)), set MIN = 1 and wait for
374 a character forever. This doesn't seem to effect Ctrl-Z or
375 Ctrl-C processing except on OS/2 where Ctrl-C won't work right
376 unless we do a read loop. Luckily we can delay a bit between
377 iterations. If not waiting (i.e. Get_Immediate (Char, Available)),
378 don't wait for anything but timeout immediately. */
379 #ifdef __EMX__
380 termios_rec.c_cc[VMIN] = 0;
381 termios_rec.c_cc[VTIME] = waiting;
382 #else
383 termios_rec.c_cc[VMIN] = waiting;
384 termios_rec.c_cc[VTIME] = 0;
385 #endif
386 #endif
387 tcsetattr (fd, TCSANOW, &termios_rec);
389 while (! good_one)
391 /* Read is used here instead of fread, because fread doesn't
392 work on Solaris5 and Sunos4 in this situation. Maybe because we
393 are mixing calls that use file descriptors and streams. */
394 nread = read (fd, &c, 1);
395 if (nread > 0)
397 /* On Unix terminals, Ctrl-D (EOT) is an End of File. */
398 if (c == eof_ch)
400 *avail = 0;
401 *end_of_file = 1;
402 good_one = 1;
405 /* Everything else is ok */
406 else if (c != eof_ch)
408 *avail = 1;
409 *end_of_file = 0;
410 good_one = 1;
414 else if (! waiting)
416 *avail = 0;
417 *end_of_file = 0;
418 good_one = 1;
420 else
421 good_one = 0;
424 tcsetattr (fd, TCSANOW, &otermios_rec);
425 *ch = c;
428 else
429 #elif defined (VMS)
430 int fd = fileno (stream);
432 if (isatty (fd))
434 if (initted == 0)
436 decc$bsd_initscr ();
437 initted = 1;
440 decc$bsd_cbreak ();
441 *ch = decc$bsd_wgetch (decc$ga_stdscr);
443 if (*ch == 4)
444 *end_of_file = 1;
445 else
446 *end_of_file = 0;
448 *avail = 1;
449 decc$bsd_nocbreak ();
451 else
452 #elif defined (__MINGW32__)
453 int fd = fileno (stream);
454 int char_waiting;
455 int eot_ch = 4; /* Ctrl-D */
457 if (isatty (fd))
459 if (waiting)
461 *ch = getch ();
462 (*winflush_function) ();
464 if (*ch == eot_ch)
465 *end_of_file = 1;
466 else
467 *end_of_file = 0;
469 *avail = 1;
471 else /* ! waiting */
473 char_waiting = kbhit();
475 if (char_waiting == 1)
477 *avail = 1;
478 *ch = getch ();
479 (*winflush_function) ();
481 if (*ch == eot_ch)
482 *end_of_file = 1;
483 else
484 *end_of_file = 0;
486 else
488 *avail = 0;
489 *end_of_file = 0;
493 else
494 #elif defined (__vxworks)
495 /* Bit masks of file descriptors to read from. */
496 struct fd_set readFds;
497 /* Timeout before select returns if nothing can be read. */
498 struct timeval timeOut;
499 char c;
500 int fd = fileno (stream);
501 int nread;
502 int option;
503 int readable;
504 int status;
505 int width;
507 if (isatty (fd))
509 /* If we do not want to wait, we have to set up fd in RAW mode. This
510 should be done outside this function as setting fd in RAW mode under
511 vxWorks flushes the buffer of fd. If the RAW mode was set here, the
512 buffer would be empty and we would always return that no character
513 is available */
514 if (! waiting)
516 /* Initialization of timeOut for its use with select. */
517 timeOut.tv_sec = 0;
518 timeOut.tv_usec = 0;
520 /* Initialization of readFds for its use with select;
521 FD is the only file descriptor to be monitored */
522 FD_ZERO (&readFds);
523 FD_SET (fd, &readFds);
524 width = 2;
526 /* We do all this processing to emulate a non blocking read. */
527 readable = select (width, &readFds, NULL, NULL, &timeOut);
528 if (readable == ERROR)
529 *avail = -1, *end_of_file = -1;
530 /* No character available in input. */
531 else if (readable == 0)
532 *avail = 0, *end_of_file = 0;
533 else
535 nread = read (fd, &c, 1);
536 if (nread > 0)
537 *avail = 1, *end_of_file = 0;
538 /* End Of File. */
539 else if (nread == 0)
540 *avail = 0, *end_of_file = 1;
541 /* Error. */
542 else
543 *avail = -1, *end_of_file = -1;
547 /* We have to wait until we get a character */
548 else
550 *avail = -1;
551 *end_of_file = -1;
553 /* Save the current mode of FD. */
554 option = ioctl (fd, FIOGETOPTIONS, 0);
556 /* Set FD in RAW mode. */
557 status = ioctl (fd, FIOSETOPTIONS, OPT_RAW);
558 if (status != -1)
560 nread = read (fd, &c, 1);
561 if (nread > 0)
562 *avail = 1, *end_of_file = 0;
563 /* End of file. */
564 else if (nread == 0)
565 *avail = 0, *end_of_file = 1;
566 /* Else there is an ERROR. */
569 /* Revert FD to its previous mode. */
570 status = ioctl (fd, FIOSETOPTIONS, option);
573 *ch = c;
575 else
576 #endif
578 /* If we're not on a terminal, then we don't need any fancy processing.
579 Also this is the only thing that's left if we're not on one of the
580 supported systems; which means that for non supported systems,
581 get_immediate may wait for a carriage return on terminals. */
582 *ch = fgetc (stream);
583 if (feof (stream))
585 *end_of_file = 1;
586 *avail = 0;
588 else
590 *end_of_file = 0;
591 *avail = 1;
596 /* The following definitions are provided in NT to support Windows based
597 Ada programs. */
599 #ifdef WINNT
600 #include <windows.h>
602 /* Provide functions to echo the values passed to WinMain (windows bindings
603 will want to import these). We use the same names as the routines used
604 by AdaMagic for compatibility. */
606 char *rts_get_hInstance (void);
607 char *rts_get_hPrevInstance (void);
608 char *rts_get_lpCommandLine (void);
609 int rts_get_nShowCmd (void);
611 char *
612 rts_get_hInstance (void)
614 return (char *)GetModuleHandleA (0);
617 char *
618 rts_get_hPrevInstance (void)
620 return 0;
623 char *
624 rts_get_lpCommandLine (void)
626 return GetCommandLineA ();
630 rts_get_nShowCmd (void)
632 return 1;
635 #endif /* WINNT */
636 #ifdef VMS
638 /* This gets around a problem with using the old threads library on VMS 7.0. */
640 #include <time.h>
642 extern long get_gmtoff (void);
644 long
645 get_gmtoff (void)
647 time_t t;
648 struct tm *ts;
650 t = time ((time_t) 0);
651 ts = localtime (&t);
652 return ts->tm_gmtoff;
654 #endif
656 /* Definition of __gnat_locatime_r used by a-calend.adb */
658 #if defined (__EMX__)
659 #define Lock_Task system__soft_links__lock_task
660 extern void (*Lock_Task) (void);
662 #define Unlock_Task system__soft_links__unlock_task
663 extern void (*Unlock_Task) (void);
665 /* Provide reentrant version of localtime on OS/2. */
667 extern struct tm *__gnat_localtime_r (const time_t *, struct tm *);
669 struct tm *
670 __gnat_localtime_r (const time_t *timer, struct tm *tp)
672 struct tm *tmp;
674 (*Lock_Task) ();
675 tmp = localtime (timer);
676 memcpy (tp, tmp, sizeof (struct tm));
677 (*Unlock_Task) ();
678 return tp;
681 #else
682 #if defined (__Lynx__) && defined (___THREADS_POSIX4ad4__)
684 /* As of LynxOS 3.1.0a patch level 040, LynuxWorks changes the
685 prototype to the C library function localtime_r from the POSIX.4
686 Draft 9 to the POSIX 1.c version. Before this change the following
687 spec is required. Only use when ___THREADS_POSIX4ad4__ is defined,
688 the Lynx convention when building against the legacy API. */
690 extern struct tm *__gnat_localtime_r (const time_t *, struct tm *);
692 struct tm *
693 __gnat_localtime_r (const time_t *timer, struct tm *tp)
695 localtime_r (tp, timer);
696 return NULL;
699 #else
700 #if defined (VMS) || defined (__MINGW32__)
702 /* __gnat_localtime_r is not needed on NT and VMS */
704 #else
706 /* All other targets provide a standard localtime_r */
708 extern struct tm *__gnat_localtime_r (const time_t *, struct tm *);
710 struct tm *
711 __gnat_localtime_r (const time_t *timer, struct tm *tp)
713 return (struct tm *) localtime_r (timer, tp);
715 #endif
716 #endif
717 #endif