OSDN Git Service

* s-parame-mingw.adb, s-parame-linux.adb,
[pf3gnuchains/gcc-fork.git] / gcc / ada / sysdep.c
1 /****************************************************************************
2  *                                                                          *
3  *                         GNAT COMPILER COMPONENTS                         *
4  *                                                                          *
5  *                                S Y S D E P                               *
6  *                                                                          *
7  *                          C Implementation File                           *
8  *                                                                          *
9  *         Copyright (C) 1992-2006, 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  ****************************************************************************/
32
33 /* This file contains system dependent symbols that are referenced in the
34    GNAT Run Time Library */
35
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
55
56 #include "adaint.h"
57
58 /*
59    mode_read_text
60    open text file for reading
61    rt for DOS and Windows NT, r for Unix
62
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
66
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
70
71    mode_read_binary
72    open binary file for reading
73    rb for DOS and Windows NT, r for Unix
74
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
78
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
82
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
86
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
90
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
94
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
98
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
102
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
106
107    Notes:
108
109    (1) Opening a file with read mode fails if the file does not exist or
110    cannot be read.
111
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.
115
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.
123
124    The other target dependent declarations here are for the two functions
125    __gnat_set_binary_mode and __gnat_set_text_mode:
126
127       void __gnat_set_binary_mode (int handle);
128       void __gnat_set_text_mode   (int handle);
129
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.
136
137    Finally there is a boolean (character) variable
138
139       char __gnat_text_translation_required;
140
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.
144
145 */
146
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;
161
162 void
163 __gnat_set_binary_mode (int handle)
164 {
165   _setmode (handle, O_BINARY);
166 }
167
168 void
169 __gnat_set_text_mode (int handle)
170 {
171   _setmode (handle, O_TEXT);
172 }
173
174 #ifdef __MINGW32__
175 #include <windows.h>
176
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".  */
180
181 char *
182 __gnat_ttyname (int filedes)
183 {
184   if (isatty (filedes))
185     return "console";
186   else
187     return NULL;
188 }
189
190 /* This function is needed to fix a bug under Win95/98. Under these plateforms
191    doing :
192                 ch1 = getch();
193                 ch2 = fgetc (stdin);
194
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.
202
203    This problem occurs when using Text_IO.Get_Line after Text_IO.Get_Immediate
204    for example.
205
206    Calling FlushConsoleInputBuffer just after getch() fix the bug under
207    95/98. */
208
209 static void winflush_init (void);
210
211 static void winflush_95 (void);
212
213 static void winflush_nt (void);
214
215 int __gnat_is_windows_xp (void);
216
217 /* winflusfunction is set first to the winflushinit function which will check
218    the OS version 95/98 or NT/2000 */
219
220 static void (*winflush_function) (void) = winflush_init;
221
222 /* This function does the runtime check of the OS version and then sets
223    winflush_function to the appropriate function and then call it. */
224
225 static void
226 winflush_init (void)
227 {
228   DWORD dwVersion = GetVersion();
229
230   if (dwVersion < 0x80000000)                /* Windows NT/2000 */
231     winflush_function = winflush_nt;
232   else                                       /* Windows 95/98   */
233     winflush_function = winflush_95;
234
235   (*winflush_function)();      /* Perform the 'flush' */
236
237 }
238
239 static void
240 winflush_95 (void)
241 {
242   FlushConsoleInputBuffer (GetStdHandle (STD_INPUT_HANDLE));
243 }
244
245 static void
246 winflush_nt (void)
247 {
248   /* Does nothing as there is no problem under NT.  */
249 }
250
251 int
252 __gnat_is_windows_xp (void)
253 {
254   static int is_win_xp=0, is_win_xp_checked=0;
255
256   if (!is_win_xp_checked)
257     {
258       OSVERSIONINFO version;
259
260       is_win_xp_checked = 1;
261
262       memset (&version, 0, sizeof (version));
263       version.dwOSVersionInfoSize = sizeof (version);
264
265       is_win_xp = GetVersionEx (&version)
266         && version.dwPlatformId == VER_PLATFORM_WIN32_NT
267         && (version.dwMajorVersion > 5
268             || (version.dwMajorVersion == 5 && version.dwMinorVersion >= 1));
269     }
270   return is_win_xp;
271 }
272
273 #endif
274
275 #else
276
277 static const char *mode_read_text = "r";
278 static const char *mode_write_text = "w";
279 static const char *mode_append_text = "a";
280 static const char *mode_read_binary = "r";
281 static const char *mode_write_binary = "w";
282 static const char *mode_append_binary = "a";
283 static const char *mode_read_text_plus = "r+";
284 static const char *mode_write_text_plus = "w+";
285 static const char *mode_append_text_plus = "a+";
286 static const char *mode_read_binary_plus = "r+";
287 static const char *mode_write_binary_plus = "w+";
288 static const char *mode_append_binary_plus = "a+";
289 const char __gnat_text_translation_required = 0;
290
291 /* These functions do nothing in non-DOS systems. */
292
293 void
294 __gnat_set_binary_mode (int handle ATTRIBUTE_UNUSED)
295 {
296 }
297
298 void
299 __gnat_set_text_mode (int handle ATTRIBUTE_UNUSED)
300 {
301 }
302 char *
303 __gnat_ttyname (int filedes)
304 {
305 #ifndef __vxworks
306   extern char *ttyname (int);
307
308   return ttyname (filedes);
309
310 #else
311   return "";
312
313 #endif
314 }
315 #endif
316 \f
317 #if defined (linux) || defined (sun) || defined (sgi) || defined (__EMX__) \
318   || (defined (__osf__) && ! defined (__alpha_vxworks)) || defined (WINNT) \
319   || defined (__MACHTEN__) || defined (__hpux__) || defined (_AIX) \
320   || (defined (__svr4__) && defined (i386)) || defined (__Lynx__) \
321   || defined (__CYGWIN__) || defined (__FreeBSD__)
322
323 #ifdef __MINGW32__
324 #if OLD_MINGW
325 #include <termios.h>
326 #else
327 #include <conio.h>  /* for getch(), kbhit() */
328 #endif
329 #else
330 #include <termios.h>
331 #endif
332
333 #else
334 #if defined (VMS)
335 extern char *decc$ga_stdscr;
336 static int initted = 0;
337 #endif
338 #endif
339
340 /* Implements the common processing for getc_immediate and
341    getc_immediate_nowait. */
342
343 extern void getc_immediate (FILE *, int *, int *);
344 extern void getc_immediate_nowait (FILE *, int *, int *, int *);
345 extern void getc_immediate_common (FILE *, int *, int *, int *, int);
346
347 /* Called by Get_Immediate (Foo); */
348
349 void
350 getc_immediate (FILE *stream, int *ch, int *end_of_file)
351 {
352   int avail;
353
354   getc_immediate_common (stream, ch, end_of_file, &avail, 1);
355 }
356
357 /* Called by Get_Immediate (Foo, Available); */
358
359 void
360 getc_immediate_nowait (FILE *stream, int *ch, int *end_of_file, int *avail)
361 {
362   getc_immediate_common (stream, ch, end_of_file, avail, 0);
363 }
364
365 /* Called by getc_immediate () and getc_immediate_nowait () */
366
367 void
368 getc_immediate_common (FILE *stream,
369                        int *ch,
370                        int *end_of_file,
371                        int *avail,
372                        int waiting)
373 {
374 #if defined (linux) || defined (sun) || defined (sgi) || defined (__EMX__) \
375     || (defined (__osf__) && ! defined (__alpha_vxworks)) \
376     || defined (__CYGWIN32__) || defined (__MACHTEN__) || defined (__hpux__) \
377     || defined (_AIX) || (defined (__svr4__) && defined (i386)) \
378     || defined (__Lynx__) || defined (__FreeBSD__)
379   char c;
380   int nread;
381   int good_one = 0;
382   int eof_ch = 4; /* Ctrl-D */
383   int fd = fileno (stream);
384   struct termios otermios_rec, termios_rec;
385
386   if (isatty (fd))
387     {
388       tcgetattr (fd, &termios_rec);
389       memcpy (&otermios_rec, &termios_rec, sizeof (struct termios));
390
391       /* Set RAW mode, with no echo */
392       termios_rec.c_lflag = termios_rec.c_lflag & ~ICANON & ~ECHO;
393
394 #if defined(linux) || defined (sun) || defined (sgi) || defined (__EMX__) \
395     || defined (__osf__) || defined (__MACHTEN__) || defined (__hpux__) \
396     || defined (_AIX) || (defined (__svr4__) && defined (i386)) \
397     || defined (__Lynx__) || defined (__FreeBSD__)
398       eof_ch = termios_rec.c_cc[VEOF];
399
400       /* If waiting (i.e. Get_Immediate (Char)), set MIN = 1 and wait for
401          a character forever. This doesn't seem to effect Ctrl-Z or
402          Ctrl-C processing except on OS/2 where Ctrl-C won't work right
403          unless we do a read loop. Luckily we can delay a bit between
404          iterations. If not waiting (i.e. Get_Immediate (Char, Available)),
405          don't wait for anything but timeout immediately. */
406 #ifdef __EMX__
407       termios_rec.c_cc[VMIN] = 0;
408       termios_rec.c_cc[VTIME] = waiting;
409 #else
410       termios_rec.c_cc[VMIN] = waiting;
411       termios_rec.c_cc[VTIME] = 0;
412 #endif
413 #endif
414       tcsetattr (fd, TCSANOW, &termios_rec);
415
416       while (! good_one)
417         {
418           /* Read is used here instead of fread, because fread doesn't
419              work on Solaris5 and Sunos4 in this situation.  Maybe because we
420              are mixing calls that use file descriptors and streams. */
421           nread = read (fd, &c, 1);
422           if (nread > 0)
423             {
424               /* On Unix terminals, Ctrl-D (EOT) is an End of File. */
425               if (c == eof_ch)
426                 {
427                   *avail = 0;
428                   *end_of_file = 1;
429                   good_one = 1;
430                 }
431
432               /* Everything else is ok */
433               else if (c != eof_ch)
434                 {
435                   *avail = 1;
436                   *end_of_file = 0;
437                   good_one = 1;
438                 }
439             }
440
441           else if (! waiting)
442             {
443               *avail = 0;
444               *end_of_file = 0;
445               good_one = 1;
446             }
447           else
448             good_one = 0;
449         }
450
451       tcsetattr (fd, TCSANOW, &otermios_rec);
452       *ch = c;
453     }
454
455   else
456 #elif defined (VMS)
457   int fd = fileno (stream);
458
459   if (isatty (fd))
460     {
461       if (initted == 0)
462         {
463           decc$bsd_initscr ();
464           initted = 1;
465         }
466
467       decc$bsd_cbreak ();
468       *ch = decc$bsd_wgetch (decc$ga_stdscr);
469
470       if (*ch == 4)
471         *end_of_file = 1;
472       else
473         *end_of_file = 0;
474
475       *avail = 1;
476       decc$bsd_nocbreak ();
477     }
478   else
479 #elif defined (__MINGW32__)
480   int fd = fileno (stream);
481   int char_waiting;
482   int eot_ch = 4; /* Ctrl-D */
483
484   if (isatty (fd))
485     {
486       if (waiting)
487         {
488           *ch = getch ();
489           (*winflush_function) ();
490
491           if (*ch == eot_ch)
492             *end_of_file = 1;
493           else
494             *end_of_file = 0;
495
496           *avail = 1;
497         }
498       else /* ! waiting */
499         {
500           char_waiting = kbhit();
501
502           if (char_waiting == 1)
503             {
504               *avail = 1;
505               *ch = getch ();
506               (*winflush_function) ();
507
508               if (*ch == eot_ch)
509                 *end_of_file = 1;
510               else
511                 *end_of_file = 0;
512             }
513           else
514             {
515               *avail = 0;
516               *end_of_file = 0;
517             }
518         }
519     }
520   else
521 #elif defined (__vxworks)
522   /* Bit masks of file descriptors to read from.  */
523   struct fd_set readFds;
524   /* Timeout before select returns if nothing can be read.  */
525   struct timeval timeOut;
526   char c;
527   int fd = fileno (stream);
528   int nread;
529   int option;
530   int readable;
531   int status;
532   int width;
533
534   if (isatty (fd))
535     {
536       /* If we do not want to wait, we have to set up fd in RAW mode. This
537          should be done outside this function as setting fd in RAW mode under
538          vxWorks flushes the buffer of fd. If the RAW mode was set here, the
539          buffer would be empty and we would always return that no character
540          is available */
541       if (! waiting)
542         {
543           /* Initialization of timeOut for its use with select.  */
544           timeOut.tv_sec  = 0;
545           timeOut.tv_usec = 0;
546
547           /* Initialization of readFds for its use with select;
548              FD is the only file descriptor to be monitored */
549           FD_ZERO (&readFds);
550           FD_SET (fd, &readFds);
551           width = 2;
552
553           /* We do all this processing to emulate a non blocking read.  */
554           readable = select (width, &readFds, NULL, NULL, &timeOut);
555           if (readable == ERROR)
556             *avail = -1, *end_of_file = -1;
557           /* No character available in input.  */
558           else if (readable == 0)
559             *avail = 0, *end_of_file = 0;
560           else
561             {
562               nread = read (fd, &c, 1);
563               if (nread > 0)
564                 *avail = 1, *end_of_file = 0;
565               /* End Of File. */
566               else if (nread == 0)
567                 *avail = 0, *end_of_file = 1;
568               /* Error.  */
569               else
570                 *avail = -1, *end_of_file = -1;
571             }
572         }
573
574       /* We have to wait until we get a character */
575       else
576         {
577           *avail = -1;
578           *end_of_file = -1;
579
580           /* Save the current mode of FD.  */
581           option = ioctl (fd, FIOGETOPTIONS, 0);
582
583           /* Set FD in RAW mode.  */
584           status = ioctl (fd, FIOSETOPTIONS, OPT_RAW);
585           if (status != -1)
586             {
587               nread = read (fd, &c, 1);
588               if (nread > 0)
589                 *avail = 1, *end_of_file = 0;
590               /* End of file.  */
591               else if (nread == 0)
592                 *avail = 0, *end_of_file = 1;
593               /* Else there is an ERROR.  */
594             }
595
596           /* Revert FD to its previous mode. */
597           status = ioctl (fd, FIOSETOPTIONS, option);
598         }
599
600       *ch = c;
601     }
602   else
603 #endif
604     {
605       /* If we're not on a terminal, then we don't need any fancy processing.
606          Also this is the only thing that's left if we're not on one of the
607          supported systems; which means that for non supported systems,
608          get_immediate may wait for a carriage return on terminals. */
609       *ch = fgetc (stream);
610       if (feof (stream))
611         {
612           *end_of_file = 1;
613           *avail = 0;
614         }
615       else
616         {
617           *end_of_file = 0;
618           *avail = 1;
619         }
620     }
621 }
622
623 /* The following definitions are provided in NT to support Windows based
624    Ada programs.  */
625
626 #ifdef WINNT
627 #include <windows.h>
628
629 /* Provide functions to echo the values passed to WinMain (windows bindings
630    will want to import these).  We use the same names as the routines used
631    by AdaMagic for compatibility.  */
632
633 char *rts_get_hInstance (void);
634 char *rts_get_hPrevInstance (void);
635 char *rts_get_lpCommandLine (void);
636 int   rts_get_nShowCmd (void);
637
638 char *
639 rts_get_hInstance (void)
640 {
641   return (char *)GetModuleHandleA (0);
642 }
643
644 char *
645 rts_get_hPrevInstance (void)
646 {
647   return 0;
648 }
649
650 char *
651 rts_get_lpCommandLine (void)
652 {
653   return GetCommandLineA ();
654 }
655
656 int
657 rts_get_nShowCmd (void)
658 {
659   return 1;
660 }
661
662 #endif /* WINNT */
663 #ifdef VMS
664
665 /* This gets around a problem with using the old threads library on VMS 7.0. */
666
667 #include <time.h>
668
669 extern long get_gmtoff (void);
670
671 long
672 get_gmtoff (void)
673 {
674   time_t t;
675   struct tm *ts;
676
677   t = time ((time_t) 0);
678   ts = localtime (&t);
679   return ts->tm_gmtoff;
680 }
681 #endif
682
683 /* Definition of __gnat_locatime_r used by a-calend.adb */
684
685 #if defined (__EMX__)
686 #define Lock_Task system__soft_links__lock_task
687 extern void (*Lock_Task) (void);
688
689 #define Unlock_Task system__soft_links__unlock_task
690 extern void (*Unlock_Task) (void);
691
692 /* Provide reentrant version of localtime on OS/2. */
693
694 extern struct tm *__gnat_localtime_r (const time_t *, struct tm *);
695
696 struct tm *
697 __gnat_localtime_r (const time_t *timer, struct tm *tp)
698 {
699   struct tm *tmp;
700
701   (*Lock_Task) ();
702   tmp = localtime (timer);
703   memcpy (tp, tmp, sizeof (struct tm));
704   (*Unlock_Task) ();
705   return tp;
706 }
707
708 #else
709 #if defined (__Lynx__) && defined (___THREADS_POSIX4ad4__)
710
711 /* As of LynxOS 3.1.0a patch level 040, LynuxWorks changes the
712    prototype to the C library function localtime_r from the POSIX.4
713    Draft 9 to the POSIX 1.c version. Before this change the following
714    spec is required. Only use when ___THREADS_POSIX4ad4__ is defined,
715    the Lynx convention when building against the legacy API. */
716
717 extern struct tm *__gnat_localtime_r (const time_t *, struct tm *);
718
719 struct tm *
720 __gnat_localtime_r (const time_t *timer, struct tm *tp)
721 {
722   localtime_r (tp, timer);
723   return NULL;
724 }
725
726 #else
727 #if defined (VMS) || defined (__MINGW32__)
728
729 /* __gnat_localtime_r is not needed on NT and VMS */
730
731 #else
732
733 /* All other targets provide a standard localtime_r */
734
735 extern struct tm *__gnat_localtime_r (const time_t *, struct tm *);
736
737 struct tm *
738 __gnat_localtime_r (const time_t *timer, struct tm *tp)
739 {
740   return (struct tm *) localtime_r (timer, tp);
741 }
742 #endif
743 #endif
744 #endif