home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / adav313.zip / gnat-3_13p-os2-bin-20010916.zip / emx / gnatlib / g-os_lib.ads < prev    next >
Text File  |  2000-07-19  |  17KB  |  387 lines

  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. --                            $Revision: 1.52 $                             --
  10. --                                                                          --
  11. --           Copyright (C) 1995-1998 Ada Core Technologies, Inc.            --
  12. --                                                                          --
  13. -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
  21. -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
  22. -- MA 02111-1307, USA.                                                      --
  23. --                                                                          --
  24. -- As a special exception,  if other files  instantiate  generics from this --
  25. -- unit, or you link  this unit with other files  to produce an executable, --
  26. -- this  unit  does not  by itself cause  the resulting  executable  to  be --
  27. -- covered  by the  GNU  General  Public  License.  This exception does not --
  28. -- however invalidate  any other reasons why  the executable file  might be --
  29. -- covered by the  GNU Public License.                                      --
  30. --                                                                          --
  31. -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com).   --
  32. --                                                                          --
  33. ------------------------------------------------------------------------------
  34.  
  35. --  Operating system interface facilities
  36.  
  37. --  This package contains types and procedures for interfacing to the
  38. --  underlying OS. It is used by the GNAT compiler and by tools associated
  39. --  with the GNAT compiler, and therefore works for the various operating
  40. --  systems to which GNAT has been ported. This package will undoubtedly
  41. --  grow as new services are needed by various tools.
  42.  
  43. --  This package tends to use fairly low-level Ada in order to not bring
  44. --  in large portions of the RTL. For example, functions return access
  45. --  to string as part of avoiding functions returning unconstrained types;
  46. --  types related to dates are defined here instead of using the types
  47. --  from Calendar, since use of Calendar forces linking in of tasking code.
  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.  
  57.    procedure Free is new Unchecked_Deallocation
  58.      (Object => String, Name => String_Access);
  59.  
  60.    ---------------------
  61.    -- Time/Date Stuff --
  62.    ---------------------
  63.  
  64.    --  The OS's notion of time is represented by the private type OS_Time.
  65.    --  This is the type returned by the File_Time_Stamp functions to obtain
  66.    --  the time stamp of a specified file. Functions and a procedure (modeled
  67.    --  after the similar subprograms in package Calendar) are provided for
  68.    --  extracting information from a value of this type. Although these are
  69.    --  called GM, the intention is not that they provide GMT times in all
  70.    --  cases but rather the actual (time-zone independent) time stamp of the
  71.    --  file (of course in Unix systems, this *is* in GMT form).
  72.  
  73.    type OS_Time is private;
  74.  
  75.    subtype Year_Type   is Integer range 1900 .. 2099;
  76.    subtype Month_Type  is Integer range    1 ..   12;
  77.    subtype Day_Type    is Integer range    1 ..   31;
  78.    subtype Hour_Type   is Integer range    0 ..   23;
  79.    subtype Minute_Type is Integer range    0 ..   59;
  80.    subtype Second_Type is Integer range    0 ..   59;
  81.  
  82.    function GM_Year    (Date : OS_Time) return Year_Type;
  83.    function GM_Month   (Date : OS_Time) return Month_Type;
  84.    function GM_Day     (Date : OS_Time) return Day_Type;
  85.    function GM_Hour    (Date : OS_Time) return Hour_Type;
  86.    function GM_Minute  (Date : OS_Time) return Minute_Type;
  87.    function GM_Second  (Date : OS_Time) return Second_Type;
  88.  
  89.    procedure GM_Split
  90.      (Date    : OS_Time;
  91.       Year    : out Year_Type;
  92.       Month   : out Month_Type;
  93.       Day     : out Day_Type;
  94.       Hour    : out Hour_Type;
  95.       Minute  : out Minute_Type;
  96.       Second  : out Second_Type);
  97.  
  98.    ----------------
  99.    -- File Stuff --
  100.    ----------------
  101.  
  102.    --  These routines give access to the open/creat/close/read/write level
  103.    --  of I/O routines in the typical C library (these functions are not
  104.    --  part of the ANSI C standard, but are typically available in all
  105.    --  systems). See also package Interfaces.C_Streams for access to the
  106.    --  stream level routines.
  107.  
  108.    type File_Descriptor is private;
  109.    --  Corresponds to the int file handle values used in the C routines,
  110.  
  111.    Standin  : constant File_Descriptor;
  112.    Standout : constant File_Descriptor;
  113.    Standerr : constant File_Descriptor;
  114.    --  File descriptors for standard input output files
  115.  
  116.    Invalid_FD : constant File_Descriptor;
  117.    --  File descriptor returned when error in opening/creating file;
  118.  
  119.    type Mode is (Binary, Text);
  120.    for Mode'Size use Integer'Size;
  121.    for Mode use (Binary => 0, Text => 1);
  122.    --  Used in all the Open and Create calls to specify if the file is to be
  123.    --  opened in binary mode or text mode. In systems like Unix, this has no
  124.    --  effect, but in systems capable of text mode translation, the use of
  125.    --  Text as the mode parameter causes the system to do CR/LF translation
  126.    --  and also to recognize the DOS end of file character on input. The use
  127.    --  of Text where appropriate allows programs to take a portable Unix view
  128.    --  of DOs-format files and process them appropriately.
  129.  
  130.    function Open_Read
  131.      (Name  : System.Address;
  132.       Fmode : Mode)
  133.       return  File_Descriptor;
  134.    pragma Import (C, Open_Read, "open_read");
  135.    --  Open file Name (NUL-terminated) for reading, returning file descriptor
  136.    --  File descriptor returned is Invalid_FD if file cannot be opened.
  137.  
  138.    function Open_Read_Write
  139.      (Name  : System.Address;
  140.       Fmode : Mode)
  141.       return  File_Descriptor;
  142.    pragma Import (C, Open_Read_Write, "open_rw");
  143.    --  Open file Name (NUL-terminated) for both reading and writing,
  144.    --  returning file descriptor. File descriptor returned is Invalid_FD if
  145.    --  file cannot be opened.
  146.  
  147.    function Create_File
  148.      (Name  : System.Address;
  149.       Fmode : Mode)
  150.       return  File_Descriptor;
  151.    pragma Import (C, Create_File, "open_create");
  152.    --  Creates new file with given name (NUL-terminated) for writing, returning
  153.    --  file descriptor for subsequent use in Write calls. File descriptor
  154.    --  returned is Invalid_FD if file cannot be successfully created.
  155.  
  156.    function Create_New_File
  157.      (Name  : System.Address;
  158.       Fmode : Mode)
  159.       return  File_Descriptor;
  160.    pragma Import (C, Create_New_File, "open_new");
  161.    --  Create new file with given name (NUL-terminated) for writing,
  162.    --  returning file descriptor for subsequent use in Write calls. This
  163.    --  differs from Create_File in that it fails if the file already exists.
  164.    --  File descriptor returned is Invalid_FD if the file exists or cannot
  165.    --  be created.
  166.  
  167.    Temp_File_Len : constant Integer := 12;
  168.    --  Length of name returned by Create_Temp_File call (GNAT-XXXXXX & NUL)
  169.  
  170.    subtype Temp_File_Name is String (1 .. Temp_File_Len);
  171.    --  String subtype set by Create_Temp_File
  172.  
  173.    procedure Create_Temp_File
  174.      (FD   : out File_Descriptor;
  175.       Name : out Temp_File_Name);
  176.    --  Create and open for writing a temporary file. The name of the
  177.    --  file and the File Descriptor are returned. The File Descriptor
  178.    --  returned is Invalid_FD in the case of failure. No mode parameter
  179.    --  is provided. Since this is a temporary file, there is no point in
  180.    --  doing text translation on it.
  181.  
  182.    procedure Close (FD : File_Descriptor);
  183.    pragma Import (C, Close, "close");
  184.    --  Close file referenced by FD
  185.  
  186.    procedure Delete_File (Name : System.Address; Success : out Boolean);
  187.    --  Deletes a file. Name is the address of the NUL-terminated file name
  188.    --  Success is set True or False indicating if the delete is successful.
  189.  
  190.    function Read
  191.      (FD   : File_Descriptor;
  192.       A    : System.Address;
  193.       N    : Integer)
  194.       return Integer;
  195.    pragma Import (C, Read, "read");
  196.    --  Read N bytes to address A from file referenced by FD. Returned value
  197.    --  is count of bytes actually read, which can be less than N at EOF.
  198.  
  199.    function Write
  200.      (FD   : File_Descriptor;
  201.       A    : System.Address;
  202.       N    : Integer)
  203.       return Integer;
  204.    pragma Import (C, Write, "write");
  205.    --  Write N bytes from address A to file referenced by FD. The returned
  206.    --  value is the number of bytes written, which can be less than N if
  207.    --  a disk full condition was detected.
  208.  
  209.    Seek_Cur : constant := 1;
  210.    Seek_End : constant := 2;
  211.    Seek_Set : constant := 0;
  212.    --  Used to indicate origin for Lseek call
  213.  
  214.    procedure Lseek
  215.      (FD     : File_Descriptor;
  216.       offset : Long_Integer;
  217.       origin : Integer);
  218.    pragma Import (C, Lseek, "lseek");
  219.    --  Sets the current file pointer to the indicated offset value,
  220.    --  relative to the current position (origin = SEEK_CUR), end of
  221.    --  file (origin = SEEK_END), or start of file (origin = SEEK_SET).
  222.  
  223.    function File_Length (FD : File_Descriptor) return Long_Integer;
  224.    pragma Import (C, File_Length, "file_length");
  225.    --  Get length of file from file descriptor FD
  226.  
  227.    function File_Time_Stamp (Name : String) return OS_Time;
  228.    --  Given the name of a file, Name, obtains and returns the time stamp.
  229.    --  This function can be used for an unopend file.
  230.  
  231.    function File_Time_Stamp (FD : File_Descriptor) return OS_Time;
  232.    --  Get time stamp of file from file descriptor FD
  233.  
  234.    function Is_Regular_File (Name : String) return Boolean;
  235.    --  Determines if the given string, Name, is the name of an existing
  236.    --  regular file. Returns True if so, False otherwise.
  237.  
  238.    function Is_Directory (Name : String) return Boolean;
  239.    --  Determines if the given string, Name, is the name of a directory.
  240.    --  Returns True if so, False otherwise.
  241.  
  242.    function Is_Writable_File (Name : String) return Boolean;
  243.    --  Determines if the given string, Name, is the name of an existing
  244.    --  file that is writable. Returns True if so, False otherwise.
  245.  
  246.    function Locate_Exec_On_Path
  247.      (Exec_Name : String)
  248.       return String_Access;
  249.    --  Try to locate an executable whose name is given by Exec_Name in the
  250.    --  directories listed in the environment Path. If the Exec_Name doesn't
  251.    --  have the executable suffix, it will be appended before the search.
  252.    --  Otherwise works like Locate_Regular_File below.
  253.  
  254.    function Locate_Regular_File
  255.      (File_Name : String;
  256.       Path      : String)
  257.       return String_Access;
  258.    --  Try to locate a regular file whose name is given by File_Name in the
  259.    --  directories listed in  Path. If a file is found, its full pathname is
  260.    --  returned; otherwise, a null pointer is returned. If the File_Name given
  261.    --  is an absolute pathname, then Locate_Regular_File just checks that the
  262.    --  file exists and is a regular file. Otherwise, the Path argument is
  263.    --  parsed according to OS conventions, and for each directory in the Path
  264.    --  a check is made if File_Name is a relative pathname of a regular file
  265.    --  from that directory.
  266.  
  267.    function Get_Debuggable_Suffix return String_Access;
  268.    --  Return the debuggable suffix convention. Usually this is the same as
  269.    --   the convention for Get_Executable_Suffix.
  270.  
  271.    function Get_Executable_Suffix return String_Access;
  272.    --  Return the executable suffix convention.
  273.  
  274.    function Get_Object_Suffix return String_Access;
  275.    --  Return the object suffix convention.
  276.  
  277.    ------------------
  278.    -- Subprocesses --
  279.    ------------------
  280.  
  281.    type Argument_List is array (Positive range <>) of String_Access;
  282.    --  Type used for argument list in call to Spawn. The lower bound
  283.    --  of the array should be 1, and the length of the array indicates
  284.    --  the number of arguments.
  285.  
  286.    type Argument_List_Access is access all Argument_List;
  287.    --  Type used to return an Argument_List without dragging in secondary
  288.    --  stack.
  289.  
  290.    procedure Spawn
  291.      (Program_Name : String;
  292.       Args         : Argument_List;
  293.       Success      : out Boolean);
  294.    --  The first parameter of function Spawn is the full path name of the
  295.    --  executable. The second parameter contains the arguments to be passed
  296.    --  to the program. Success is false if the program could not be spawned
  297.    --  or its execution completed unsuccessfully. Note that the caller will
  298.    --  be blocked until the execution of the spawned program is complete.
  299.  
  300.    type Process_Id is private;
  301.    --  A private type used to identify a process activated by the following
  302.    --  non-blocking call. The only meaningful operation on this type is a
  303.    --  comparison for equality.
  304.  
  305.    Invalid_Pid : constant Process_Id;
  306.    --  A special value used to indicate errors, as described below.
  307.  
  308.    function Non_Blocking_Spawn
  309.      (Program_Name : String;
  310.       Args         : Argument_List)
  311.       return         Process_Id;
  312.    --  This is a non blocking call. The Process_Id of the spawned process
  313.    --  is returned. Parameters are to be used as in Spawn. If Invalid_Id
  314.    --  is returned the program could not be spawned.
  315.  
  316.    procedure Wait_Process (Pid : out Process_Id; Success : out Boolean);
  317.    --  Wait for the completion of any of the processes created by previous
  318.    --  calls to Non_Blocking_Spawn. The caller will be suspended until one
  319.    --  of these processes terminates (normally or abnormally). If any of
  320.    --  these subprocesses terminates prior to the call to Wait_Process (and
  321.    --  has not been returned by a previous call to Wait_Process), then the
  322.    --  call to Wait_Process is immediate. Pid identifies the process that
  323.    --  has terminated (matching the value returned from Non_Blocking_Spawn).
  324.    --  Success is set to True if this sub-process terminated successfully.
  325.    --  If Pid = Invalid_Id, there were no subprocesses left to wait on.
  326.  
  327.    function Argument_String_To_List
  328.      (Arg_String : String)
  329.       return Argument_List_Access;
  330.    --  Take a string that is a program and it's arguments and parse it into
  331.    --  an Argument_List.
  332.  
  333.    -------------------
  334.    -- Miscellaneous --
  335.    -------------------
  336.  
  337.    function Getenv (Name : String) return String_Access;
  338.    --  Get the value of the environment variable. Returns the empty
  339.    --  string if the environment variable does not exist.
  340.  
  341.    procedure OS_Exit  (Status : Integer);
  342.    pragma Import (C, OS_Exit, "os_exit");
  343.    --  Exit to OS with given status code (program is terminated)
  344.  
  345.    procedure OS_Abort;
  346.    pragma Import (C, OS_Abort, "abort");
  347.    --  Exit to OS signalling an abort (traceback or other appropriate
  348.    --  diagnostic information should be given if possible, or entry made
  349.    --  to the debugger if that is possible).
  350.  
  351.    function Errno return Integer;
  352.    pragma Import (C, Errno, "__get_errno");
  353.    --  Return the task-safe last error number.
  354.  
  355.    procedure Set_Errno (Errno : Integer);
  356.    pragma Import (C, Set_Errno, "__set_errno");
  357.    --  Set the task-safe error number.
  358.  
  359.    Directory_Separator : constant Character;
  360.    --  The character that is used to separate parts of a pathname.
  361.  
  362.    Path_Separator      : constant Character;
  363.    --  The character to separate paths in an environment variable value.
  364.  
  365. private
  366.    function Get_Dirsep_Char return Character;
  367.    pragma Import (C, Get_Dirsep_Char, "Get_Dirsep_Char");
  368.    Directory_Separator : constant Character := Get_Dirsep_Char;
  369.  
  370.    function Get_Pathsep_Char return Character;
  371.    pragma Import (C, Get_Pathsep_Char, "Get_Pathsep_Char");
  372.    Path_Separator      : constant Character := Get_Pathsep_Char;
  373.  
  374.    type OS_Time is new Integer;
  375.  
  376.    type File_Descriptor is new Integer;
  377.  
  378.    Standin    : constant File_Descriptor :=  0;
  379.    Standout   : constant File_Descriptor :=  1;
  380.    Standerr   : constant File_Descriptor :=  2;
  381.    Invalid_FD : constant File_Descriptor := -1;
  382.  
  383.    type Process_Id is new Integer;
  384.    Invalid_Pid : constant Process_Id := -1;
  385.  
  386. end GNAT.OS_Lib;
  387.