OSDN Git Service

* adadecode.c: Use <> form of include for ctype.h.
[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   || defined (__CYGWIN__)
295
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
305
306 #else
307 #if defined (VMS)
308 extern char *decc$ga_stdscr;
309 static int initted = 0;
310 #endif
311 #endif
312
313 /* Implements the common processing for getc_immediate and
314    getc_immediate_nowait. */
315
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);
319
320 /* Called by Get_Immediate (Foo); */
321
322 void
323 getc_immediate (FILE *stream, int *ch, int *end_of_file)
324 {
325   int avail;
326
327   getc_immediate_common (stream, ch, end_of_file, &avail, 1);
328 }
329
330 /* Called by Get_Immediate (Foo, Available); */
331
332 void
333 getc_immediate_nowait (FILE *stream, int *ch, int *end_of_file, int *avail)
334 {
335   getc_immediate_common (stream, ch, end_of_file, avail, 0);
336 }
337
338 /* Called by getc_immediate () and getc_immediate_nowait () */
339
340 void
341 getc_immediate_common (FILE *stream,
342                        int *ch,
343                        int *end_of_file,
344                        int *avail,
345                        int waiting)
346 {
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__)
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;
358
359   if (isatty (fd))
360     {
361       tcgetattr (fd, &termios_rec);
362       memcpy (&otermios_rec, &termios_rec, sizeof (struct termios));
363
364       /* Set RAW mode, with no echo */
365       termios_rec.c_lflag = termios_rec.c_lflag & ~ICANON & ~ECHO;
366
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__)
371       eof_ch = termios_rec.c_cc[VEOF];
372
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);
388
389       while (! good_one)
390         {
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)
396             {
397               /* On Unix terminals, Ctrl-D (EOT) is an End of File. */
398               if (c == eof_ch)
399                 {
400                   *avail = 0;
401                   *end_of_file = 1;
402                   good_one = 1;
403                 }
404
405               /* Everything else is ok */
406               else if (c != eof_ch)
407                 {
408                   *avail = 1;
409                   *end_of_file = 0;
410                   good_one = 1;
411                 }
412             }
413
414           else if (! waiting)
415             {
416               *avail = 0;
417               *end_of_file = 0;
418               good_one = 1;
419             }
420           else
421             good_one = 0;
422         }
423
424       tcsetattr (fd, TCSANOW, &otermios_rec);
425       *ch = c;
426     }
427
428   else
429 #elif defined (VMS)
430   int fd = fileno (stream);
431
432   if (isatty (fd))
433     {
434       if (initted == 0)
435         {
436           decc$bsd_initscr ();
437           initted = 1;
438         }
439
440       decc$bsd_cbreak ();
441       *ch = decc$bsd_wgetch (decc$ga_stdscr);
442
443       if (*ch == 4)
444         *end_of_file = 1;
445       else
446         *end_of_file = 0;
447
448       *avail = 1;
449       decc$bsd_nocbreak ();
450     }
451   else
452 #elif defined (__MINGW32__)
453   int fd = fileno (stream);
454   int char_waiting;
455   int eot_ch = 4; /* Ctrl-D */
456
457   if (isatty (fd))
458     {
459       if (waiting)
460         {
461           *ch = getch ();
462           (*winflush_function) ();
463
464           if (*ch == eot_ch)
465             *end_of_file = 1;
466           else
467             *end_of_file = 0;
468
469           *avail = 1;
470         }
471       else /* ! waiting */
472         {
473           char_waiting = kbhit();
474
475           if (char_waiting == 1)
476             {
477               *avail = 1;
478               *ch = getch ();
479               (*winflush_function) ();
480
481               if (*ch == eot_ch)
482                 *end_of_file = 1;
483               else
484                 *end_of_file = 0;
485             }
486           else
487             {
488               *avail = 0;
489               *end_of_file = 0;
490             }
491         }
492     }
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;
506
507   if (isatty (fd))
508     {
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)
515         {
516           /* Initialization of timeOut for its use with select.  */
517           timeOut.tv_sec  = 0;
518           timeOut.tv_usec = 0;
519
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;
525
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
534             {
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;
544             }
545         }
546
547       /* We have to wait until we get a character */
548       else
549         {
550           *avail = -1;
551           *end_of_file = -1;
552
553           /* Save the current mode of FD.  */
554           option = ioctl (fd, FIOGETOPTIONS, 0);
555
556           /* Set FD in RAW mode.  */
557           status = ioctl (fd, FIOSETOPTIONS, OPT_RAW);
558           if (status != -1)
559             {
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.  */
567             }
568
569           /* Revert FD to its previous mode. */
570           status = ioctl (fd, FIOSETOPTIONS, option);
571         }
572
573       *ch = c;
574     }
575   else
576 #endif
577     {
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))
584         {
585           *end_of_file = 1;
586           *avail = 0;
587         }
588       else
589         {
590           *end_of_file = 0;
591           *avail = 1;
592         }
593     }
594 }
595
596 /* The following definitions are provided in NT to support Windows based
597    Ada programs.  */
598
599 #ifdef WINNT
600 #include <windows.h>
601
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.  */
605
606 char *rts_get_hInstance (void);
607 char *rts_get_hPrevInstance (void);
608 char *rts_get_lpCommandLine (void);
609 int   rts_get_nShowCmd (void);
610
611 char *
612 rts_get_hInstance (void)
613 {
614   return (char *)GetModuleHandleA (0);
615 }
616
617 char *
618 rts_get_hPrevInstance (void)
619 {
620   return 0;
621 }
622
623 char *
624 rts_get_lpCommandLine (void)
625 {
626   return GetCommandLineA ();
627 }
628
629 int
630 rts_get_nShowCmd (void)
631 {
632   return 1;
633 }
634
635 #endif /* WINNT */
636 #ifdef VMS
637
638 /* This gets around a problem with using the old threads library on VMS 7.0. */
639
640 #include <time.h>
641
642 extern long get_gmtoff (void);
643
644 long
645 get_gmtoff (void)
646 {
647   time_t t;
648   struct tm *ts;
649
650   t = time ((time_t) 0);
651   ts = localtime (&t);
652   return ts->tm_gmtoff;
653 }
654 #endif
655
656 /* Definition of __gnat_locatime_r used by a-calend.adb */
657
658 #if defined (_AIX) || defined (__EMX__)
659 #define Lock_Task system__soft_links__lock_task
660 extern void (*Lock_Task) (void);
661
662 #define Unlock_Task system__soft_links__unlock_task
663 extern void (*Unlock_Task) (void);
664
665 /* Provide reentrant version of localtime on Aix and OS/2. Note that AiX does
666    provide localtime_r, but in the library libc_r which doesn't get included
667    systematically, so we can't use it. */
668
669 extern struct tm *__gnat_localtime_r (const time_t *, struct tm *);
670
671 struct tm *
672 __gnat_localtime_r (const time_t *timer, struct tm *tp)
673 {
674   struct tm *tmp;
675
676   (*Lock_Task) ();
677   tmp = localtime (timer);
678   memcpy (tp, tmp, sizeof (struct tm));
679   (*Unlock_Task) ();
680   return tp;
681 }
682
683 #else
684 #if defined (__Lynx__) && defined (___THREADS_POSIX4ad4__)
685
686 /* As of LynxOS 3.1.0a patch level 040, LynuxWorks changes the
687    prototype to the C library function localtime_r from the POSIX.4
688    Draft 9 to the POSIX 1.c version. Before this change the following
689    spec is required. Only use when ___THREADS_POSIX4ad4__ is defined,
690    the Lynx convention when building against the legacy API. */
691
692 extern struct tm *__gnat_localtime_r (const time_t *, struct tm *);
693
694 struct tm *
695 __gnat_localtime_r (const time_t *timer, struct tm *tp)
696 {
697   localtime_r (tp, timer);
698   return NULL;
699 }
700
701 #else
702 #if defined (VMS) || defined (__MINGW32__)
703
704 /* __gnat_localtime_r is not needed on NT and VMS */
705
706 #else
707
708 /* All other targets provide a standard localtime_r */
709
710 extern struct tm *__gnat_localtime_r (const time_t *, struct tm *);
711
712 struct tm *
713 __gnat_localtime_r (const time_t *timer, struct tm *tp)
714 {
715   return (struct tm *) localtime_r (timer, tp);
716 }
717 #endif
718 #endif
719 #endif