OSDN Git Service

Delete all lines containing "$Revision:".
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-os_lib.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                          G N A T . O S _ L I B                           --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --                                                                          --
10 --          Copyright (C) 1995-2002 Free Software Foundation, Inc.          --
11 --                                                                          --
12 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
13 -- terms of the  GNU General Public License as published  by the Free Soft- --
14 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
15 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
18 -- for  more details.  You should have  received  a copy of the GNU General --
19 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
20 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
21 -- MA 02111-1307, USA.                                                      --
22 --                                                                          --
23 -- As a special exception,  if other files  instantiate  generics from this --
24 -- unit, or you link  this unit with other files  to produce an executable, --
25 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
26 -- covered  by the  GNU  General  Public  License.  This exception does not --
27 -- however invalidate  any other reasons why  the executable file  might be --
28 -- covered by the  GNU Public License.                                      --
29 --                                                                          --
30 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com).   --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 --  Operating system interface facilities
35
36 --  This package contains types and procedures for interfacing to the
37 --  underlying OS. It is used by the GNAT compiler and by tools associated
38 --  with the GNAT compiler, and therefore works for the various operating
39 --  systems to which GNAT has been ported. This package will undoubtedly
40 --  grow as new services are needed by various tools.
41
42 --  This package tends to use fairly low-level Ada in order to not bring
43 --  in large portions of the RTL. For example, functions return access
44 --  to string as part of avoiding functions returning unconstrained types.
45
46 --  Except where specifically noted, these routines are portable across
47 --  all GNAT implementations on all supported operating systems.
48
49 with System;
50 with Unchecked_Deallocation;
51
52 package GNAT.OS_Lib is
53 pragma Elaborate_Body (OS_Lib);
54
55    type String_Access is access all String;
56    --  General purpose string access type. Some of the functions in this
57    --  package allocate string results on the heap, and return a value of
58    --  this type. Note that the caller is responsible for freeing this
59    --  String to avoid memory leaks.
60
61    procedure Free is new Unchecked_Deallocation
62      (Object => String, Name => String_Access);
63    --  This procedure is provided for freeing returned values of type
64    --  String_Access
65
66    type String_List is array (Positive range <>) of String_Access;
67    type String_List_Access is access all String_List;
68    --  General purpose array and pointer for list of string accesses
69
70    procedure Free (Arg : in out String_List_Access);
71    --  Frees the given array and all strings that its elements reference,
72    --  and then sets the argument to null. Provided for freeing returned
73    --  values of this type (including Argument_List_Access).
74
75    ---------------------
76    -- Time/Date Stuff --
77    ---------------------
78
79    --  The OS's notion of time is represented by the private type OS_Time.
80    --  This is the type returned by the File_Time_Stamp functions to obtain
81    --  the time stamp of a specified file. Functions and a procedure (modeled
82    --  after the similar subprograms in package Calendar) are provided for
83    --  extracting information from a value of this type. Although these are
84    --  called GM, the intention is not that they provide GMT times in all
85    --  cases but rather the actual (time-zone independent) time stamp of the
86    --  file (of course in Unix systems, this *is* in GMT form).
87
88    type OS_Time is private;
89
90    subtype Year_Type   is Integer range 1900 .. 2099;
91    subtype Month_Type  is Integer range    1 ..   12;
92    subtype Day_Type    is Integer range    1 ..   31;
93    subtype Hour_Type   is Integer range    0 ..   23;
94    subtype Minute_Type is Integer range    0 ..   59;
95    subtype Second_Type is Integer range    0 ..   59;
96
97    function GM_Year    (Date : OS_Time) return Year_Type;
98    function GM_Month   (Date : OS_Time) return Month_Type;
99    function GM_Day     (Date : OS_Time) return Day_Type;
100    function GM_Hour    (Date : OS_Time) return Hour_Type;
101    function GM_Minute  (Date : OS_Time) return Minute_Type;
102    function GM_Second  (Date : OS_Time) return Second_Type;
103
104    procedure GM_Split
105      (Date    : OS_Time;
106       Year    : out Year_Type;
107       Month   : out Month_Type;
108       Day     : out Day_Type;
109       Hour    : out Hour_Type;
110       Minute  : out Minute_Type;
111       Second  : out Second_Type);
112
113    ----------------
114    -- File Stuff --
115    ----------------
116
117    --  These routines give access to the open/creat/close/read/write level
118    --  of I/O routines in the typical C library (these functions are not
119    --  part of the ANSI C standard, but are typically available in all
120    --  systems). See also package Interfaces.C_Streams for access to the
121    --  stream level routines.
122
123    --  Note on file names. If a file name is passed as type String in any
124    --  of the following specifications, then the name is a normal Ada string
125    --  and need not be NUL-terminated. However, a trailing NUL character is
126    --  permitted, and will be ignored (more accurately, the NUL and any
127    --  characters that follow it will be ignored).
128
129    type File_Descriptor is private;
130    --  Corresponds to the int file handle values used in the C routines,
131
132    Standin  : constant File_Descriptor;
133    Standout : constant File_Descriptor;
134    Standerr : constant File_Descriptor;
135    --  File descriptors for standard input output files
136
137    Invalid_FD : constant File_Descriptor;
138    --  File descriptor returned when error in opening/creating file;
139
140    type Mode is (Binary, Text);
141    for Mode'Size use Integer'Size;
142    for Mode use (Binary => 0, Text => 1);
143    --  Used in all the Open and Create calls to specify if the file is to be
144    --  opened in binary mode or text mode. In systems like Unix, this has no
145    --  effect, but in systems capable of text mode translation, the use of
146    --  Text as the mode parameter causes the system to do CR/LF translation
147    --  and also to recognize the DOS end of file character on input. The use
148    --  of Text where appropriate allows programs to take a portable Unix view
149    --  of DOs-format files and process them appropriately.
150
151    function Open_Read
152      (Name  : String;
153       Fmode : Mode)
154       return  File_Descriptor;
155    --  Open file Name for reading, returning file descriptor File descriptor
156    --  returned is Invalid_FD if file cannot be opened.
157
158    function Open_Read_Write
159      (Name  : String;
160       Fmode : Mode)
161       return  File_Descriptor;
162    --  Open file Name for both reading and writing, returning file
163    --  descriptor. File descriptor returned is Invalid_FD if file cannot be
164    --  opened.
165
166    function Create_File
167      (Name  : String;
168       Fmode : Mode)
169       return  File_Descriptor;
170    --  Creates new file with given name for writing, returning file descriptor
171    --  for subsequent use in Write calls. File descriptor returned is
172    --  Invalid_FD if file cannot be successfully created
173
174    function Create_New_File
175      (Name  : String;
176       Fmode : Mode)
177       return  File_Descriptor;
178    --  Create new file with given name for writing, returning file descriptor
179    --  for subsequent use in Write calls. This differs from Create_File in
180    --  that it fails if the file already exists. File descriptor returned is
181    --  Invalid_FD if the file exists or cannot be created.
182
183    Temp_File_Len : constant Integer := 12;
184    --  Length of name returned by Create_Temp_File call (GNAT-XXXXXX & NUL)
185
186    subtype Temp_File_Name is String (1 .. Temp_File_Len);
187    --  String subtype set by Create_Temp_File
188
189    procedure Create_Temp_File
190      (FD   : out File_Descriptor;
191       Name : out Temp_File_Name);
192    --  Create and open for writing a temporary file. The name of the
193    --  file and the File Descriptor are returned. The File Descriptor
194    --  returned is Invalid_FD in the case of failure. No mode parameter
195    --  is provided. Since this is a temporary file, there is no point in
196    --  doing text translation on it.
197
198    procedure Close (FD : File_Descriptor);
199    pragma Import (C, Close, "close");
200    --  Close file referenced by FD
201
202    procedure Delete_File (Name : String; Success : out Boolean);
203    --  Deletes file. Success is set True or False indicating if the delete is
204    --  successful.
205
206    procedure Rename_File
207      (Old_Name : String;
208       New_Name : String;
209       Success  : out Boolean);
210    --  Rename a file. Success is set True or False indicating if the rename is
211    --  successful.
212
213    function Read
214      (FD   : File_Descriptor;
215       A    : System.Address;
216       N    : Integer)
217       return Integer;
218    pragma Import (C, Read, "read");
219    --  Read N bytes to address A from file referenced by FD. Returned value
220    --  is count of bytes actually read, which can be less than N at EOF.
221
222    function Write
223      (FD   : File_Descriptor;
224       A    : System.Address;
225       N    : Integer)
226       return Integer;
227    pragma Import (C, Write, "write");
228    --  Write N bytes from address A to file referenced by FD. The returned
229    --  value is the number of bytes written, which can be less than N if
230    --  a disk full condition was detected.
231
232    Seek_Cur : constant := 1;
233    Seek_End : constant := 2;
234    Seek_Set : constant := 0;
235    --  Used to indicate origin for Lseek call
236
237    procedure Lseek
238      (FD     : File_Descriptor;
239       offset : Long_Integer;
240       origin : Integer);
241    pragma Import (C, Lseek, "lseek");
242    --  Sets the current file pointer to the indicated offset value,
243    --  relative to the current position (origin = SEEK_CUR), end of
244    --  file (origin = SEEK_END), or start of file (origin = SEEK_SET).
245
246    function File_Length (FD : File_Descriptor) return Long_Integer;
247    pragma Import (C, File_Length, "__gnat_file_length");
248    --  Get length of file from file descriptor FD
249
250    function File_Time_Stamp (Name : String) return OS_Time;
251    --  Given the name of a file or directory, Name, obtains and returns the
252    --  time stamp. This function can be used for an unopend file.
253
254    function File_Time_Stamp (FD : File_Descriptor) return OS_Time;
255    --  Get time stamp of file from file descriptor FD
256
257    function Normalize_Pathname
258      (Name      : String;
259       Directory : String := "")
260       return      String;
261    --  Returns a file name as an absolute path name, resolving all relative
262    --  directories, and symbolic links. The parameter Directory is a fully
263    --  resolved path name for a directory, or the empty string (the default).
264    --  Name is the name of a file, which is either relative to the given
265    --  directory name, if Directory is non-null, or to the current working
266    --  directory if Directory is null. The result returned is the normalized
267    --  name of the file. For most cases, if two file names designate the same
268    --  file through different paths, Normalize_Pathname will return the same
269    --  canonical name in both cases. However, there are cases when this is
270    --  not true; for example, this is not true in Unix for two hard links
271    --  designating the same file.
272    --
273    --  If Name cannot be resolved or is null on entry (for example if there is
274    --  a circularity in symbolic links: A is a symbolic link for B, while B is
275    --  a symbolic link for A), then Normalize_Pathname returns an empty string.
276    --
277    --  In VMS, if Name follows the VMS syntax file specification, it is first
278    --  converted into Unix syntax. If the conversion fails, Normalize_Pathname
279    --  returns an empty string.
280
281    function Is_Absolute_Path (Name : String) return Boolean;
282    --  Returns True if Name is an absolute path name, i.e. it designates
283    --  a directory absolutely, rather than relative to another directory.
284
285    function Is_Regular_File (Name : String) return Boolean;
286    --  Determines if the given string, Name, is the name of an existing
287    --  regular file. Returns True if so, False otherwise.
288
289    function Is_Directory (Name : String) return Boolean;
290    --  Determines if the given string, Name, is the name of a directory.
291    --  Returns True if so, False otherwise.
292
293    function Is_Writable_File (Name : String) return Boolean;
294    --  Determines if the given string, Name, is the name of an existing
295    --  file that is writable. Returns True if so, False otherwise.
296
297    function Locate_Exec_On_Path
298      (Exec_Name : String)
299       return      String_Access;
300    --  Try to locate an executable whose name is given by Exec_Name in the
301    --  directories listed in the environment Path. If the Exec_Name doesn't
302    --  have the executable suffix, it will be appended before the search.
303    --  Otherwise works like Locate_Regular_File below.
304    --
305    --  Note that this function allocates some memory for the returned value.
306    --  This memory needs to be deallocated after use.
307
308    function Locate_Regular_File
309      (File_Name : String;
310       Path      : String)
311       return      String_Access;
312    --  Try to locate a regular file whose name is given by File_Name in the
313    --  directories listed in  Path. If a file is found, its full pathname is
314    --  returned; otherwise, a null pointer is returned. If the File_Name given
315    --  is an absolute pathname, then Locate_Regular_File just checks that the
316    --  file exists and is a regular file. Otherwise, the Path argument is
317    --  parsed according to OS conventions, and for each directory in the Path
318    --  a check is made if File_Name is a relative pathname of a regular file
319    --  from that directory.
320    --
321    --  Note that this function allocates some memory for the returned value.
322    --  This memory needs to be deallocated after use.
323
324    function Get_Debuggable_Suffix return String_Access;
325    --  Return the debuggable suffix convention. Usually this is the same as
326    --  the convention for Get_Executable_Suffix. The result is allocated on
327    --  the heap and should be freed when no longer needed to avoid storage
328    --  leaks.
329
330    function Get_Executable_Suffix return String_Access;
331    --  Return the executable suffix convention. The result is allocated on
332    --  the heap and should be freed when no longer needed to avoid storage
333    --  leaks.
334
335    function Get_Object_Suffix return String_Access;
336    --  Return the object suffix convention. The result is allocated on the
337    --  heap and should be freed when no longer needed to avoid storage leaks.
338
339    --  The following section contains low-level routines using addresses to
340    --  pass file name and executable name. In each routine the name must be
341    --  Nul-Terminated. For complete documentation refer to the equivalent
342    --  routine (but using string) defined above.
343
344    subtype C_File_Name is System.Address;
345    --  This subtype is used to document that a parameter is the address
346    --  of a null-terminated string containing the name of a file.
347
348    function Open_Read
349      (Name  : C_File_Name;
350       Fmode : Mode)
351       return  File_Descriptor;
352
353    function Open_Read_Write
354      (Name  : C_File_Name;
355       Fmode : Mode)
356       return  File_Descriptor;
357
358    function Create_File
359      (Name  : C_File_Name;
360       Fmode : Mode)
361       return  File_Descriptor;
362
363    function Create_New_File
364      (Name  : C_File_Name;
365       Fmode : Mode)
366       return  File_Descriptor;
367
368    procedure Delete_File (Name : C_File_Name; Success : out Boolean);
369
370    procedure Rename_File
371      (Old_Name : C_File_Name;
372       New_Name : C_File_Name;
373       Success  : out Boolean);
374
375    function File_Time_Stamp (Name : C_File_Name) return OS_Time;
376
377    function Is_Regular_File (Name : C_File_Name) return Boolean;
378
379    function Is_Directory (Name : C_File_Name) return Boolean;
380
381    function Is_Writable_File (Name : C_File_Name) return Boolean;
382
383    function Locate_Regular_File
384      (File_Name : C_File_Name;
385       Path      : C_File_Name)
386       return      String_Access;
387
388    ------------------
389    -- Subprocesses --
390    ------------------
391
392    subtype Argument_List is String_List;
393    --  Type used for argument list in call to Spawn. The lower bound
394    --  of the array should be 1, and the length of the array indicates
395    --  the number of arguments.
396
397    subtype Argument_List_Access is String_List_Access;
398    --  Type used to return Argument_List without dragging in secondary stack.
399    --  Note that there is a Free procedure declared for this subtype which
400    --  frees the array and all referenced strings.
401
402    procedure Normalize_Arguments (Args : in out Argument_List);
403    --  Normalize all arguments in the list. This ensure that the argument list
404    --  is compatible with the running OS and will works fine with Spawn and
405    --  Non_Blocking_Spawn for example. If Normalize_Arguments is called twice
406    --  on the same list it will do nothing the second time. Note that Spawn
407    --  and Non_Blocking_Spawn call Normalize_Arguments automatically, but
408    --  since there is a guarantee that a second call does nothing, this
409    --  internal call with have no effect if Normalize_Arguments is called
410    --  before calling Spawn. The call to Normalize_Arguments assumes that
411    --  the individual referenced arguments in Argument_List are on the heap,
412    --  and may free them and reallocate if they are modified.
413
414    procedure Spawn
415      (Program_Name : String;
416       Args         : Argument_List;
417       Success      : out Boolean);
418    --  The first parameter of function Spawn is the name of the executable.
419    --  The second parameter contains the arguments to be passed to the
420    --  program. Success is False if the named program could not be spawned
421    --  or its execution completed unsuccessfully. Note that the caller will
422    --  be blocked until the execution of the spawned program is complete.
423    --  For maximum portability, use a full path name for the Program_Name
424    --  argument. On some systems (notably Unix systems) a simple file
425    --  name may also work (if the executable can be located in the path).
426    --
427    --  Note: Arguments in Args that contain spaces and/or quotes such as
428    --  "--GCC=gcc -v" or "--GCC=""gcc -v""" are not portable across all
429    --  operating systems, and would not have the desired effect if they
430    --  were passed directly to the operating system. To avoid this problem,
431    --  Spawn makes an internal call to Normalize_Arguments, which ensures
432    --  that such arguments are modified in a manner that ensures that the
433    --  desired effect is obtained on all operating systems. The caller may
434    --  call Normalize_Arguments explicitly before the call (e.g. to print
435    --  out the exact form of arguments passed to the operating system). In
436    --  this case the guarantee a second call to Normalize_Arguments has no
437    --  effect ensures that the internal call will not affect the result.
438    --  Note that the implicit call to Normalize_Arguments may free and
439    --  reallocate some of the individual arguments.
440    --
441    --  This function will always set Success to False under VxWorks and
442    --  other similar operating systems which have no notion of the concept
443    --  of a dynamically executable file.
444
445    function Spawn
446      (Program_Name : String;
447       Args         : Argument_List)
448       return         Integer;
449    --  Similar to the above procedure, but returns the actual status returned
450    --  by the operating system, or -1 under VxWorks and any other similar
451    --  operating systems which have no notion of separately spawnable programs.
452
453    type Process_Id is private;
454    --  A private type used to identify a process activated by the following
455    --  non-blocking call. The only meaningful operation on this type is a
456    --  comparison for equality.
457
458    Invalid_Pid : constant Process_Id;
459    --  A special value used to indicate errors, as described below.
460
461    function Non_Blocking_Spawn
462      (Program_Name : String;
463       Args         : Argument_List)
464       return         Process_Id;
465    --  This is a non blocking call. The Process_Id of the spawned process
466    --  is returned. Parameters are to be used as in Spawn. If Invalid_Id
467    --  is returned the program could not be spawned.
468    --
469    --  This function will always return Invalid_Id under VxWorks, since
470    --  there is no notion of executables under this OS.
471
472    procedure Wait_Process (Pid : out Process_Id; Success : out Boolean);
473    --  Wait for the completion of any of the processes created by previous
474    --  calls to Non_Blocking_Spawn. The caller will be suspended until one
475    --  of these processes terminates (normally or abnormally). If any of
476    --  these subprocesses terminates prior to the call to Wait_Process (and
477    --  has not been returned by a previous call to Wait_Process), then the
478    --  call to Wait_Process is immediate. Pid identifies the process that
479    --  has terminated (matching the value returned from Non_Blocking_Spawn).
480    --  Success is set to True if this sub-process terminated successfully.
481    --  If Pid = Invalid_Id, there were no subprocesses left to wait on.
482    --
483    --  This function will always set success to False under VxWorks, since
484    --  there is no notion of executables under this OS.
485
486    function Argument_String_To_List
487      (Arg_String : String)
488       return       Argument_List_Access;
489    --  Take a string that is a program and it's arguments and parse it into
490    --  an Argument_List. Note that the result is allocated on the heap, and
491    --  must be freed by the programmer (when it is no longer needed) to avoid
492    --  memory leaks.
493
494    -------------------
495    -- Miscellaneous --
496    -------------------
497
498    function Getenv (Name : String) return String_Access;
499    --  Get the value of the environment variable. Returns an access
500    --  to the empty string if the environment variable does not exist
501    --  or has an explicit null value (in some operating systems these
502    --  are distinct cases, in others they are not; this interface
503    --  abstracts away that difference. The argument is allocated on
504    --  the heap (even in the null case), and needs to be freed explicitly
505    --  when no longer needed to avoid memory leaks.
506
507    procedure Setenv (Name : String; Value : String);
508    --  Set the value of the environment variable Name to Value. This call
509    --  modifies the current environment, but does not modify the parent
510    --  process environment. After a call to Setenv, Getenv (Name) will
511    --  always return a String_Access referencing the same String as Value.
512    --  This is true also for the null string case (the actual effect may
513    --  be to either set an explicit null as the value, or to remove the
514    --  entry, this is operating system dependent). Note that any following
515    --  calls to Spawn will pass an environment to the spawned process that
516    --  includes the changes made by Setenv calls. This procedure is not
517    --  available under VMS.
518
519    procedure OS_Exit (Status : Integer);
520    pragma Import (C, OS_Exit, "__gnat_os_exit");
521    pragma No_Return (OS_Exit);
522    --  Exit to OS with given status code (program is terminated)
523
524    procedure OS_Abort;
525    pragma Import (C, OS_Abort, "abort");
526    pragma No_Return (OS_Abort);
527    --  Exit to OS signalling an abort (traceback or other appropriate
528    --  diagnostic information should be given if possible, or entry made
529    --  to the debugger if that is possible).
530
531    function Errno return Integer;
532    pragma Import (C, Errno, "__get_errno");
533    --  Return the task-safe last error number.
534
535    procedure Set_Errno (Errno : Integer);
536    pragma Import (C, Set_Errno, "__set_errno");
537    --  Set the task-safe error number.
538
539    Directory_Separator : constant Character;
540    --  The character that is used to separate parts of a pathname.
541
542    Path_Separator : constant Character;
543    --  The character to separate paths in an environment variable value.
544
545 private
546    pragma Import (C, Path_Separator, "__gnat_path_separator");
547    pragma Import (C, Directory_Separator, "__gnat_dir_separator");
548
549    type OS_Time is new Integer;
550
551    type File_Descriptor is new Integer;
552
553    Standin    : constant File_Descriptor :=  0;
554    Standout   : constant File_Descriptor :=  1;
555    Standerr   : constant File_Descriptor :=  2;
556    Invalid_FD : constant File_Descriptor := -1;
557
558    type Process_Id is new Integer;
559    Invalid_Pid : constant Process_Id := -1;
560
561 end GNAT.OS_Lib;