summaryrefslogtreecommitdiff
path: root/gcc/ada/sysdep.c
blob: d40e84d0bec82457c362abff9f3c07fcb7e537b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
/****************************************************************************
 *                                                                          *
 *                         GNAT COMPILER COMPONENTS                         *
 *                                                                          *
 *                                S Y S D E P                               *
 *                                                                          *
 *                          C Implementation File                           *
 *                                                                          *
 *                                                                          *
 *          Copyright (C) 1992-2002 Free Software Foundation, Inc.          *
 *                                                                          *
 * GNAT is free software;  you can  redistribute it  and/or modify it under *
 * terms of the  GNU General Public License as published  by the Free Soft- *
 * ware  Foundation;  either version 2,  or (at your option) any later ver- *
 * sion.  GNAT is distributed in the hope that it will be useful, but WITH- *
 * OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY *
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License *
 * for  more details.  You should have  received  a copy of the GNU General *
 * Public License  distributed with GNAT;  see file COPYING.  If not, write *
 * to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, *
 * MA 02111-1307, USA.                                                      *
 *                                                                          *
 * As a  special  exception,  if you  link  this file  with other  files to *
 * produce an executable,  this file does not by itself cause the resulting *
 * executable to be covered by the GNU General Public License. This except- *
 * ion does not  however invalidate  any other reasons  why the  executable *
 * file might be covered by the  GNU Public License.                        *
 *                                                                          *
 * GNAT was originally developed  by the GNAT team at  New York University. *
 * Extensive contributions were provided by Ada Core Technologies Inc.      *
 *                                                                          *
 ****************************************************************************/

/* This file contains system dependent symbols that are referenced in the
   GNAT Run Time Library */

#ifdef __vxworks
#include "ioLib.h"
#include "selectLib.h"
#include "vxWorks.h"
#endif
#ifdef IN_RTS
#define POSIX
#include "tconfig.h"
#include "tsystem.h"
#include <fcntl.h>
#include <sys/stat.h>
#include "time.h"
#else
#include "config.h"
#include "system.h"
#endif

#include "adaint.h"

/*
   mode_read_text
   open text file for reading
   rt for DOS and Windows NT, r for Unix

   mode_write_text
   truncate to zero length or create text file for writing
   wt for DOS and Windows NT, w for Unix

   mode_append_text
   append; open or create text file for writing at end-of-file
   at for DOS and Windows NT, a for Unix

   mode_read_binary
   open binary file for reading
   rb for DOS and Windows NT, r for Unix

   mode_write_binary
   truncate to zero length or create binary file for writing
   wb for DOS and Windows NT, w for Unix

   mode_append_binary
   append; open or create binary file for writing at end-of-file
   ab for DOS and Windows NT, a for Unix

   mode_read_text_plus
   open text file for update (reading and writing)
   r+t for DOS and Windows NT, r+ for Unix

   mode_write_text_plus
   truncate to zero length or create text file for update
   w+t for DOS and Windows NT, w+ for Unix

   mode_append_text_plus
   append; open or create text file for update, writing at end-of-file
   a+t for DOS and Windows NT, a+ for Unix

   mode_read_binary_plus
   open binary file for update (reading and writing)
   r+b for DOS and Windows NT, r+ for Unix

   mode_write_binary_plus
   truncate to zero length or create binary file for update
   w+b for DOS and Windows NT, w+ for Unix

   mode_append_binary_plus
   append; open or create binary file for update, writing at end-of-file
   a+b for DOS and Windows NT, a+ for Unix

   Notes:

   (1) Opening a file with read mode fails if the file does not exist or
   cannot be read.

   (2) Opening a file with append mode causes all subsequent writes to the
   file to be forced to the then current end-of-file, regardless of
   intervening calls to the fseek function.

   (3) When a file is opened with update mode, both input and output may be
   performed on the associated stream.  However, output may not be directly
   followed by input without an intervening call to the fflush function or
   to a file positioning function (fseek, fsetpos, or rewind), and input
   may not be directly followed by output without an intervening call to a
   file positioning function, unless the input operation encounters
   end-of-file.

   The other target dependent declarations here are for the two functions
   __gnat_set_binary_mode and __gnat_set_text_mode:

      void __gnat_set_binary_mode (int handle);
      void __gnat_set_text_mode   (int handle);

   These functions have no effect in Unix (or similar systems where there is
   no distinction between binary and text files), but in DOS (and similar
   systems where text mode does CR/LF translation), these functions allow
   the mode of the stream with the given handle (fileno can be used to get
   the handle of a stream) to be changed dynamically. The returned result
   is 0 if no error occurs and -1 if an error occurs.

   Finally there is a boolean (character) variable

      char __gnat_text_translation_required;

   which is zero (false) in Unix mode, and one (true) in DOS mode, with a
   true value indicating that text translation is required on text files
   and that fopen supports the trailing t and b modifiers.

*/

#if defined(WINNT) || defined (MSDOS) || defined (__EMX__)
static const char *mode_read_text = "rt";
static const char *mode_write_text = "wt";
static const char *mode_append_text = "at";
static const char *mode_read_binary = "rb";
static const char *mode_write_binary = "wb";
static const char *mode_append_binary = "ab";
static const char *mode_read_text_plus = "r+t";
static const char *mode_write_text_plus = "w+t";
static const char *mode_append_text_plus = "a+t";
static const char *mode_read_binary_plus = "r+b";
static const char *mode_write_binary_plus = "w+b";
static const char *mode_append_binary_plus = "a+b";
const char __gnat_text_translation_required = 1;

void
__gnat_set_binary_mode (handle)
     int handle;
{
  _setmode (handle, O_BINARY);
}

void
__gnat_set_text_mode (handle)
     int handle;
{
  _setmode (handle, O_TEXT);
}

#ifdef __MINGW32__
#include <windows.h>

/* Return the name of the tty.   Under windows there is no name for
   the tty, so this function, if connected to a tty, returns the generic name
   "console".  */

char *
__gnat_ttyname (filedes)
     int filedes;
{
  if (isatty (filedes))
    return "console";
  else
    return NULL;
}

/* This function is needed to fix a bug under Win95/98. Under these plateforms
   doing :
                ch1 = getch();
		ch2 = fgetc (stdin);

   will put the same character into ch1 and ch2. It seem that the character
   read by getch() is not correctly removed from the buffer. Even a
   fflush(stdin) does not fix the bug. This bug does not appear under Window
   NT. So we have two version of this routine below one for 95/98 and one for
   NT/2000 version of Windows. There is also a special routine (winflushinit)
   that will be called only the first time to check which version of Windows
   we are running running on to set the right routine to use.

   This problem occurs when using Text_IO.Get_Line after Text_IO.Get_Immediate
   for example.

   Calling FlushConsoleInputBuffer just after getch() fix the bug under 
   95/98. */

static void winflush_init PARAMS ((void));

static void winflush_95 PARAMS ((void));

static void winflush_nt PARAMS ((void));

/* winflusfunction is set first to the winflushinit function which will check
   the OS version 95/98 or NT/2000 */

static void (*winflush_function) PARAMS ((void)) = winflush_init;

/* This function does the runtime check of the OS version and then sets
   winflush_function to the appropriate function and then call it. */ 

static void
winflush_init ()
{ 
  DWORD dwVersion = GetVersion();

  if (dwVersion < 0x80000000)                /* Windows NT/2000 */
    winflush_function = winflush_nt;
  else                                       /* Windows 95/98   */
    winflush_function = winflush_95;

  (*winflush_function)();      /* Perform the 'flush' */

}

static void winflush_95 ()
{ 
  FlushConsoleInputBuffer (GetStdHandle (STD_INPUT_HANDLE));
}

static void winflush_nt ()
{
  /* Does nothing as there is no problem under NT.  */
}
#endif

#else

static const char *mode_read_text = "r";
static const char *mode_write_text = "w";
static const char *mode_append_text = "a";
static const char *mode_read_binary = "r";
static const char *mode_write_binary = "w";
static const char *mode_append_binary = "a";
static const char *mode_read_text_plus = "r+";
static const char *mode_write_text_plus = "w+";
static const char *mode_append_text_plus = "a+";
static const char *mode_read_binary_plus = "r+";
static const char *mode_write_binary_plus = "w+";
static const char *mode_append_binary_plus = "a+";
const char __gnat_text_translation_required = 0;

/* These functions do nothing in non-DOS systems. */

void
__gnat_set_binary_mode (handle)
     int handle ATTRIBUTE_UNUSED;
{
}

void
__gnat_set_text_mode (handle)
     int handle ATTRIBUTE_UNUSED;
{
}
char *
__gnat_ttyname (filedes)
     int filedes;
{
#ifndef __vxworks
  extern char *ttyname PARAMS ((int));

  return ttyname (filedes);

#else
  return "";

#endif
}
#endif

#if defined (linux) || defined (sun) || defined (sgi) || defined (__EMX__) \
  || (defined (__osf__) && ! defined (__alpha_vxworks)) || defined (WINNT) \
  || defined (__MACHTEN__) || defined (hpux) || defined (_AIX) \
  || (defined (__svr4__) && defined (i386)) || defined (__Lynx__) \
  || defined (__CYGWIN__)
#include <termios.h>

#else
#if defined (VMS)
extern char *decc$ga_stdscr;
static int initted = 0;
#endif
#endif

/* Implements the common processing for getc_immediate and
   getc_immediate_nowait. */

extern void getc_immediate		PARAMS ((FILE *, int *, int *));
extern void getc_immediate_nowait	PARAMS ((FILE *, int *, int *, int *));
extern void getc_immediate_common	PARAMS ((FILE *, int *, int *,
						 int *, int));

/* Called by Get_Immediate (Foo); */

void
getc_immediate (stream, ch, end_of_file)
     FILE *stream;
     int *ch;
     int *end_of_file;
{
  int avail;

  getc_immediate_common (stream, ch, end_of_file, &avail, 1);
}

/* Called by Get_Immediate (Foo, Available); */

void
getc_immediate_nowait (stream, ch, end_of_file, avail)
     FILE *stream;
     int *ch;
     int *end_of_file;
     int *avail;
{
  getc_immediate_common (stream, ch, end_of_file, avail, 0);
}

/* Called by getc_immediate () and getc_immediate_nowait () */

void
getc_immediate_common (stream, ch, end_of_file, avail, waiting)
     FILE *stream;
     int *ch;
     int *end_of_file;
     int *avail;
     int waiting;
{
#if defined (linux) || defined (sun) || defined (sgi) || defined (__EMX__) \
    || (defined (__osf__) && ! defined (__alpha_vxworks)) \
    || defined (__CYGWIN__) || defined (__MACHTEN__) || defined (hpux) \
    || defined (_AIX) || (defined (__svr4__) && defined (i386)) \
    || defined (__Lynx__)
  char c;
  int nread;
  int good_one = 0;
  int eof_ch = 4; /* Ctrl-D */
  int fd = fileno (stream);
  struct termios otermios_rec, termios_rec;

  if (isatty (fd))
    {
      tcgetattr (fd, &termios_rec);
      memcpy (&otermios_rec, &termios_rec, sizeof (struct termios));

      /* Set RAW mode, with no echo */
      termios_rec.c_lflag = termios_rec.c_lflag & ~ICANON & ~ECHO;

#if defined(linux) || defined (sun) || defined (sgi) || defined (__EMX__) \
    || defined (__osf__) || defined (__MACHTEN__) || defined (hpux) \
    || defined (_AIX) || (defined (__svr4__) && defined (i386)) \
    || defined (__Lynx__)
      eof_ch = termios_rec.c_cc[VEOF];

      /* If waiting (i.e. Get_Immediate (Char)), set MIN = 1 and wait for
         a character forever. This doesn't seem to effect Ctrl-Z or
         Ctrl-C processing except on OS/2 where Ctrl-C won't work right
         unless we do a read loop. Luckily we can delay a bit between
         iterations. If not waiting (i.e. Get_Immediate (Char, Available)),
         don't wait for anything but timeout immediately. */
#ifdef __EMX__
      termios_rec.c_cc[VMIN] = 0;
      termios_rec.c_cc[VTIME] = waiting;
#else
      termios_rec.c_cc[VMIN] = waiting;
      termios_rec.c_cc[VTIME] = 0;
#endif
#endif
      tcsetattr (fd, TCSANOW, &termios_rec);

      while (! good_one)
        {
          /* Read is used here instead of fread, because fread doesn't
             work on Solaris5 and Sunos4 in this situation.  Maybe because we
             are mixing calls that use file descriptors and streams. */
          nread = read (fd, &c, 1);
          if (nread > 0)
            {
              /* On Unix terminals, Ctrl-D (EOT) is an End of File. */
              if (c == eof_ch)
                {
                  *avail = 0;
                  *end_of_file = 1;
                  good_one = 1;
                }

              /* Everything else is ok */
              else if (c != eof_ch)
                {
                  *avail = 1;
                  *end_of_file = 0;
                  good_one = 1;
                }
            }

          else if (! waiting)
            {
              *avail = 0;
              *end_of_file = 0;
              good_one = 1;
            }
          else
	    good_one = 0;
        }

      tcsetattr (fd, TCSANOW, &otermios_rec);
      *ch = c;
    }

  else
#elif defined (VMS)
  int fd = fileno (stream);

  if (isatty (fd))
    {
      if (initted == 0)
	{
	  decc$bsd_initscr ();
	  initted = 1;
	}

      decc$bsd_cbreak ();
      *ch = decc$bsd_wgetch (decc$ga_stdscr);

      if (*ch == 4)
	*end_of_file = 1;
      else
	*end_of_file = 0;

      *avail = 1;
      decc$bsd_nocbreak ();
    }
  else
#elif defined (__MINGW32__)
  int fd = fileno (stream);
  int char_waiting;
  int eot_ch = 4; /* Ctrl-D */

  if (isatty (fd))
    {
      if (waiting)
	{
	  *ch = getch ();
	  (*winflush_function) ();

	  if (*ch == eot_ch)
	    *end_of_file = 1;
	  else
	    *end_of_file = 0;

	  *avail = 1;
	}
      else /* ! waiting */
	{
	  char_waiting = kbhit();

	  if (char_waiting == 1)
	    {
	      *avail = 1;
	      *ch = getch ();
	      (*winflush_function) ();

	      if (*ch == eot_ch)
		*end_of_file = 1;
	      else
		*end_of_file = 0;
	    }
	  else
	    {
	      *avail = 0;
	      *end_of_file = 0;
	    }
	}
    }
  else
#elif defined (__vxworks)
  /* Bit masks of file descriptors to read from.  */
  struct fd_set readFds;
  /* Timeout before select returns if nothing can be read.  */ 
  struct timeval timeOut;
  char c;
  int fd = fileno (stream);
  int nread;
  int option;
  int readable;
  int status;
  int width;

  if (isatty (fd)) 
    {
      /* If we do not want to wait, we have to set up fd in RAW mode. This
	 should be done outside this function as setting fd in RAW mode under
	 vxWorks flushes the buffer of fd. If the RAW mode was set here, the
	 buffer would be empty and we would always return that no character
	 is available */
      if (! waiting) 
	{
	  /* Initialization of timeOut for its use with select.  */
	  timeOut.tv_sec  = 0;
	  timeOut.tv_usec = 0;

	  /* Initialization of readFds for its use with select;
	     FD is the only file descriptor to be monitored */
	  FD_ZERO (&readFds);
	  FD_SET (fd, &readFds);
	  width = 2;

	  /* We do all this processing to emulate a non blocking read.  */
	  readable = select (width, &readFds, NULL, NULL, &timeOut);
	  if (readable == ERROR)
	    *avail = -1, *end_of_file = -1;
	  /* No character available in input.  */
	  else if (readable == 0)
	    *avail = 0, *end_of_file = 0;
	  else
	    {
	      nread = read (fd, &c, 1);
	      if (nread > 0) 
		*avail = 1, *end_of_file = 0;
	      /* End Of File. */
	      else if (nread == 0) 
		*avail = 0, *end_of_file = 1;
	      /* Error.  */
	      else 
		*avail = -1, *end_of_file = -1;
	    }   
	}

      /* We have to wait until we get a character */
      else 
	{
	  *avail = -1;
	  *end_of_file = -1;

	  /* Save the current mode of FD.  */
	  option = ioctl (fd, FIOGETOPTIONS, 0);

	  /* Set FD in RAW mode.  */
	  status = ioctl (fd, FIOSETOPTIONS, OPT_RAW);
	  if (status != -1) 
	    {
	      nread = read (fd, &c, 1);
	      if (nread > 0) 
		*avail = 1, *end_of_file = 0;
	      /* End of file.  */
	      else if (nread == 0) 
		*avail = 0, *end_of_file = 1;
	      /* Else there is an ERROR.  */
	    }

	  /* Revert FD to its previous mode. */
	  status = ioctl (fd, FIOSETOPTIONS, option);
	}

      *ch = c;
    }
  else
#endif
    {
      /* If we're not on a terminal, then we don't need any fancy processing.
	 Also this is the only thing that's left if we're not on one of the
	 supported systems; which means that for non supported systems,
         get_immediate may wait for a carriage return on terminals. */
      *ch = fgetc (stream);
      if (feof (stream))
        {
          *end_of_file = 1;
          *avail = 0;
        }
      else
        {
          *end_of_file = 0;
          *avail = 1;
        }
    }
}

/* The following definitions are provided in NT to support Windows based
   Ada programs.  */

#ifdef WINNT
#include <windows.h>

/* Provide functions to echo the values passed to WinMain (windows bindings
   will want to import these).  We use the same names as the routines used
   by AdaMagic for compatibility.  */

char *rts_get_hInstance     PARAMS ((void));
char *rts_get_hPrevInstance PARAMS ((void));
char *rts_get_lpCommandLine PARAMS ((void));
int   rts_get_nShowCmd      PARAMS ((void));

char *
rts_get_hInstance () 
{ 
  return GetModuleHandleA (0); 
}

char *
rts_get_hPrevInstance () 
{ 
  return 0; 
}

char *
rts_get_lpCommandLine () 
{ 
  return GetCommandLineA (); 
}

int   
rts_get_nShowCmd () 
{ 
  return 1; 
}

#endif /* WINNT */
#ifdef VMS

/* This gets around a problem with using the old threads library on VMS 7.0. */

#include <time.h>

extern long get_gmtoff PARAMS ((void));

long
get_gmtoff ()
{
  time_t t;
  struct tm *ts;

  t = time ((time_t) 0);
  ts = localtime (&t);
  return ts->tm_gmtoff;
}
#endif

/* Definition of __gnat_locatime_r used by a-calend.adb */

#if defined (_AIX) || defined (__EMX__)
#define Lock_Task system__soft_links__lock_task
extern void (*Lock_Task) PARAMS ((void));

#define Unlock_Task system__soft_links__unlock_task
extern void (*Unlock_Task) PARAMS ((void));

/* Provide reentrant version of localtime on Aix and OS/2. Note that AiX does
   provide localtime_r, but in the library libc_r which doesn't get included
   systematically, so we can't use it. */

extern void struct tm *__gnat_localtime_r PARAMS ((const time_t *,
						   struct tm *));

struct tm *
__gnat_localtime_r (timer, tp)
     const time_t *timer;
     struct tm *tp;
{
  struct tm *tmp;

  (*Lock_Task) ();
  tmp = localtime (timer);
  memcpy (tp, tmp, sizeof (struct tm));
  (*Unlock_Task) ();
  return tp;
}

#else
#if defined (__Lynx__) && defined (___THREADS_POSIX4ad4__)

/* As of LynxOS 3.1.0a patch level 040, LynuxWorks changes the
   prototype to the C library function localtime_r from the POSIX.4
   Draft 9 to the POSIX 1.c version. Before this change the following
   spec is required. Only use when ___THREADS_POSIX4ad4__ is defined,
   the Lynx convention when building against the legacy API. */

extern struct tm *__gnat_localtime_r PARAMS ((const time_t *, struct tm *));

struct tm *
__gnat_localtime_r (timer, tp)
     const time_t *timer;
     struct tm *tp;
{
  localtime_r (tp, timer);
  return NULL;
}

#else
#if defined (VMS) || defined (__MINGW32__)

/* __gnat_localtime_r is not needed on NT and VMS */

#else

/* All other targets provide a standard localtime_r */

extern struct tm *__gnat_localtime_r PARAMS ((const time_t *, struct tm *));

struct tm *
__gnat_localtime_r (timer, tp)
     const time_t *timer;
     struct tm *tp;
{
  return (struct tm *) localtime_r (timer, tp);
}
#endif
#endif
#endif