OSDN Git Service

* sysdep.c: Problem discovered during IA64 VMS port.
[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-2003 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,  59 Temple Place - Suite 330,  Boston, *
20  * MA 02111-1307, 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 /* winflusfunction is set first to the winflushinit function which will check
216    the OS version 95/98 or NT/2000 */
217
218 static void (*winflush_function) (void) = winflush_init;
219
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. */
222
223 static void
224 winflush_init (void)
225 {
226   DWORD dwVersion = GetVersion();
227
228   if (dwVersion < 0x80000000)                /* Windows NT/2000 */
229     winflush_function = winflush_nt;
230   else                                       /* Windows 95/98   */
231     winflush_function = winflush_95;
232
233   (*winflush_function)();      /* Perform the 'flush' */
234
235 }
236
237 static void winflush_95 (void)
238 {
239   FlushConsoleInputBuffer (GetStdHandle (STD_INPUT_HANDLE));
240 }
241
242 static void winflush_nt (void)
243 {
244   /* Does nothing as there is no problem under NT.  */
245 }
246 #endif
247
248 #else
249
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;
263
264 /* These functions do nothing in non-DOS systems. */
265
266 void
267 __gnat_set_binary_mode (int handle ATTRIBUTE_UNUSED)
268 {
269 }
270
271 void
272 __gnat_set_text_mode (int handle ATTRIBUTE_UNUSED)
273 {
274 }
275 char *
276 __gnat_ttyname (int filedes)
277 {
278 #ifndef __vxworks
279   extern char *ttyname (int);
280
281   return ttyname (filedes);
282
283 #else
284   return "";
285
286 #endif
287 }
288 #endif
289 \f
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
295 #ifdef __MINGW32__
296 #if OLD_MINGW
297 #include <termios.h>
298 #else
299 #include <conio.h>  /* for getch(), kbhit() */
300 #endif
301 #else
302 #include <termios.h>
303 #endif
304
305 #else
306 #if defined (VMS)
307 extern char *decc$ga_stdscr;
308 static int initted = 0;
309 #endif
310 #endif
311
312 /* Implements the common processing for getc_immediate and
313    getc_immediate_nowait. */
314
315 extern void getc_immediate (FILE *, int *, int *);
316 extern void getc_immediate_nowait (FILE *, int *, int *, int *);
317 extern void getc_immediate_common (FILE *, int *, int *, int *, int);
318
319 /* Called by Get_Immediate (Foo); */
320
321 void
322 getc_immediate (FILE *stream, int *ch, int *end_of_file)
323 {
324   int avail;
325
326   getc_immediate_common (stream, ch, end_of_file, &avail, 1);
327 }
328
329 /* Called by Get_Immediate (Foo, Available); */
330
331 void
332 getc_immediate_nowait (FILE *stream, int *ch, int *end_of_file, int *avail)
333 {
334   getc_immediate_common (stream, ch, end_of_file, avail, 0);
335 }
336
337 /* Called by getc_immediate () and getc_immediate_nowait () */
338
339 void
340 getc_immediate_common (FILE *stream,
341                        int *ch,
342                        int *end_of_file,
343                        int *avail,
344                        int waiting)
345 {
346 #if defined (linux) || defined (sun) || defined (sgi) || defined (__EMX__) \
347     || (defined (__osf__) && ! defined (__alpha_vxworks)) \
348     || defined (__CYGWIN32__) || defined (__MACHTEN__) || defined (hpux) \
349     || defined (_AIX) || (defined (__svr4__) && defined (i386)) \
350     || defined (__Lynx__)
351   char c;
352   int nread;
353   int good_one = 0;
354   int eof_ch = 4; /* Ctrl-D */
355   int fd = fileno (stream);
356   struct termios otermios_rec, termios_rec;
357
358   if (isatty (fd))
359     {
360       tcgetattr (fd, &termios_rec);
361       memcpy (&otermios_rec, &termios_rec, sizeof (struct termios));
362
363       /* Set RAW mode, with no echo */
364       termios_rec.c_lflag = termios_rec.c_lflag & ~ICANON & ~ECHO;
365
366 #if defined(linux) || defined (sun) || defined (sgi) || defined (__EMX__) \
367     || defined (__osf__) || defined (__MACHTEN__) || defined (hpux) \
368     || defined (_AIX) || (defined (__svr4__) && defined (i386)) \
369     || defined (__Lynx__)
370       eof_ch = termios_rec.c_cc[VEOF];
371
372       /* If waiting (i.e. Get_Immediate (Char)), set MIN = 1 and wait for
373          a character forever. This doesn't seem to effect Ctrl-Z or
374          Ctrl-C processing except on OS/2 where Ctrl-C won't work right
375          unless we do a read loop. Luckily we can delay a bit between
376          iterations. If not waiting (i.e. Get_Immediate (Char, Available)),
377          don't wait for anything but timeout immediately. */
378 #ifdef __EMX__
379       termios_rec.c_cc[VMIN] = 0;
380       termios_rec.c_cc[VTIME] = waiting;
381 #else
382       termios_rec.c_cc[VMIN] = waiting;
383       termios_rec.c_cc[VTIME] = 0;
384 #endif
385 #endif
386       tcsetattr (fd, TCSANOW, &termios_rec);
387
388       while (! good_one)
389         {
390           /* Read is used here instead of fread, because fread doesn't
391              work on Solaris5 and Sunos4 in this situation.  Maybe because we
392              are mixing calls that use file descriptors and streams. */
393           nread = read (fd, &c, 1);
394           if (nread > 0)
395             {
396               /* On Unix terminals, Ctrl-D (EOT) is an End of File. */
397               if (c == eof_ch)
398                 {
399                   *avail = 0;
400                   *end_of_file = 1;
401                   good_one = 1;
402                 }
403
404               /* Everything else is ok */
405               else if (c != eof_ch)
406                 {
407                   *avail = 1;
408                   *end_of_file = 0;
409                   good_one = 1;
410                 }
411             }
412
413           else if (! waiting)
414             {
415               *avail = 0;
416               *end_of_file = 0;
417               good_one = 1;
418             }
419           else
420             good_one = 0;
421         }
422
423       tcsetattr (fd, TCSANOW, &otermios_rec);
424       *ch = c;
425     }
426
427   else
428 #elif defined (VMS)
429   int fd = fileno (stream);
430
431   if (isatty (fd))
432     {
433       if (initted == 0)
434         {
435           decc$bsd_initscr ();
436           initted = 1;
437         }
438
439       decc$bsd_cbreak ();
440       *ch = decc$bsd_wgetch (decc$ga_stdscr);
441
442       if (*ch == 4)
443         *end_of_file = 1;
444       else
445         *end_of_file = 0;
446
447       *avail = 1;
448       decc$bsd_nocbreak ();
449     }
450   else
451 #elif defined (__MINGW32__)
452   int fd = fileno (stream);
453   int char_waiting;
454   int eot_ch = 4; /* Ctrl-D */
455
456   if (isatty (fd))
457     {
458       if (waiting)
459         {
460           *ch = getch ();
461           (*winflush_function) ();
462
463           if (*ch == eot_ch)
464             *end_of_file = 1;
465           else
466             *end_of_file = 0;
467
468           *avail = 1;
469         }
470       else /* ! waiting */
471         {
472           char_waiting = kbhit();
473
474           if (char_waiting == 1)
475             {
476               *avail = 1;
477               *ch = getch ();
478               (*winflush_function) ();
479
480               if (*ch == eot_ch)
481                 *end_of_file = 1;
482               else
483                 *end_of_file = 0;
484             }
485           else
486             {
487               *avail = 0;
488               *end_of_file = 0;
489             }
490         }
491     }
492   else
493 #elif defined (__vxworks)
494   /* Bit masks of file descriptors to read from.  */
495   struct fd_set readFds;
496   /* Timeout before select returns if nothing can be read.  */
497   struct timeval timeOut;
498   char c;
499   int fd = fileno (stream);
500   int nread;
501   int option;
502   int readable;
503   int status;
504   int width;
505
506   if (isatty (fd))
507     {
508       /* If we do not want to wait, we have to set up fd in RAW mode. This
509          should be done outside this function as setting fd in RAW mode under
510          vxWorks flushes the buffer of fd. If the RAW mode was set here, the
511          buffer would be empty and we would always return that no character
512          is available */
513       if (! waiting)
514         {
515           /* Initialization of timeOut for its use with select.  */
516           timeOut.tv_sec  = 0;
517           timeOut.tv_usec = 0;
518
519           /* Initialization of readFds for its use with select;
520              FD is the only file descriptor to be monitored */
521           FD_ZERO (&readFds);
522           FD_SET (fd, &readFds);
523           width = 2;
524
525           /* We do all this processing to emulate a non blocking read.  */
526           readable = select (width, &readFds, NULL, NULL, &timeOut);
527           if (readable == ERROR)
528             *avail = -1, *end_of_file = -1;
529           /* No character available in input.  */
530           else if (readable == 0)
531             *avail = 0, *end_of_file = 0;
532           else
533             {
534               nread = read (fd, &c, 1);
535               if (nread > 0)
536                 *avail = 1, *end_of_file = 0;
537               /* End Of File. */
538               else if (nread == 0)
539                 *avail = 0, *end_of_file = 1;
540               /* Error.  */
541               else
542                 *avail = -1, *end_of_file = -1;
543             }
544         }
545
546       /* We have to wait until we get a character */
547       else
548         {
549           *avail = -1;
550           *end_of_file = -1;
551
552           /* Save the current mode of FD.  */
553           option = ioctl (fd, FIOGETOPTIONS, 0);
554
555           /* Set FD in RAW mode.  */
556           status = ioctl (fd, FIOSETOPTIONS, OPT_RAW);
557           if (status != -1)
558             {
559               nread = read (fd, &c, 1);
560               if (nread > 0)
561                 *avail = 1, *end_of_file = 0;
562               /* End of file.  */
563               else if (nread == 0)
564                 *avail = 0, *end_of_file = 1;
565               /* Else there is an ERROR.  */
566             }
567
568           /* Revert FD to its previous mode. */
569           status = ioctl (fd, FIOSETOPTIONS, option);
570         }
571
572       *ch = c;
573     }
574   else
575 #endif
576     {
577       /* If we're not on a terminal, then we don't need any fancy processing.
578          Also this is the only thing that's left if we're not on one of the
579          supported systems; which means that for non supported systems,
580          get_immediate may wait for a carriage return on terminals. */
581       *ch = fgetc (stream);
582       if (feof (stream))
583         {
584           *end_of_file = 1;
585           *avail = 0;
586         }
587       else
588         {
589           *end_of_file = 0;
590           *avail = 1;
591         }
592     }
593 }
594
595 /* The following definitions are provided in NT to support Windows based
596    Ada programs.  */
597
598 #ifdef WINNT
599 #include <windows.h>
600
601 /* Provide functions to echo the values passed to WinMain (windows bindings
602    will want to import these).  We use the same names as the routines used
603    by AdaMagic for compatibility.  */
604
605 char *rts_get_hInstance (void);
606 char *rts_get_hPrevInstance (void);
607 char *rts_get_lpCommandLine (void);
608 int   rts_get_nShowCmd (void);
609
610 char *
611 rts_get_hInstance (void)
612 {
613   return (char *)GetModuleHandleA (0);
614 }
615
616 char *
617 rts_get_hPrevInstance (void)
618 {
619   return 0;
620 }
621
622 char *
623 rts_get_lpCommandLine (void)
624 {
625   return GetCommandLineA ();
626 }
627
628 int
629 rts_get_nShowCmd (void)
630 {
631   return 1;
632 }
633
634 #endif /* WINNT */
635 #ifdef VMS
636
637 /* This gets around a problem with using the old threads library on VMS 7.0. */
638
639 #include <time.h>
640
641 extern long get_gmtoff (void);
642
643 long
644 get_gmtoff (void)
645 {
646   time_t t;
647   struct tm *ts;
648
649   t = time ((time_t) 0);
650   ts = localtime (&t);
651   return ts->tm_gmtoff;
652 }
653 #endif
654
655 /* Definition of __gnat_locatime_r used by a-calend.adb */
656
657 #if defined (_AIX) || defined (__EMX__)
658 #define Lock_Task system__soft_links__lock_task
659 extern void (*Lock_Task) (void);
660
661 #define Unlock_Task system__soft_links__unlock_task
662 extern void (*Unlock_Task) (void);
663
664 /* Provide reentrant version of localtime on Aix and OS/2. Note that AiX does
665    provide localtime_r, but in the library libc_r which doesn't get included
666    systematically, so we can't use it. */
667
668 extern struct tm *__gnat_localtime_r (const time_t *, struct tm *);
669
670 struct tm *
671 __gnat_localtime_r (const time_t *timer, struct tm *tp)
672 {
673   struct tm *tmp;
674
675   (*Lock_Task) ();
676   tmp = localtime (timer);
677   memcpy (tp, tmp, sizeof (struct tm));
678   (*Unlock_Task) ();
679   return tp;
680 }
681
682 #else
683 #if defined (__Lynx__) && defined (___THREADS_POSIX4ad4__)
684
685 /* As of LynxOS 3.1.0a patch level 040, LynuxWorks changes the
686    prototype to the C library function localtime_r from the POSIX.4
687    Draft 9 to the POSIX 1.c version. Before this change the following
688    spec is required. Only use when ___THREADS_POSIX4ad4__ is defined,
689    the Lynx convention when building against the legacy API. */
690
691 extern struct tm *__gnat_localtime_r (const time_t *, struct tm *);
692
693 struct tm *
694 __gnat_localtime_r (const time_t *timer, struct tm *tp)
695 {
696   localtime_r (tp, timer);
697   return NULL;
698 }
699
700 #else
701 #if defined (VMS) || defined (__MINGW32__)
702
703 /* __gnat_localtime_r is not needed on NT and VMS */
704
705 #else
706
707 /* All other targets provide a standard localtime_r */
708
709 extern struct tm *__gnat_localtime_r (const time_t *, struct tm *);
710
711 struct tm *
712 __gnat_localtime_r (const time_t *timer, struct tm *tp)
713 {
714   return (struct tm *) localtime_r (timer, tp);
715 }
716 #endif
717 #endif
718 #endif