libsimpleio_1.22163.1_c99a1be4/src/bindings/posix.ads

  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
-- Copyright (C)2020-2023, Philip Munts dba Munts Technologies.
--
-- Redistribution and use in source and binary forms, with or without
-- modification, are permitted provided that the following conditions are met:
--
-- * Redistributions of source code must retain the above copyright notice,
--   this list of conditions and the following disclaimer.
--
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
-- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-- POSSIBILITY OF SUCH DAMAGE.

-- String actual parameters *MUST* be NUL terminated, e.g. "FOO" & ASCII.NUL

WITH Interfaces.C;
WITH System;

PACKAGE Posix IS

  -- Direct bindings to Posix library functions

  TYPE ssize_t IS NEW Long_Integer;

  TYPE pollfd IS RECORD
    fd      : Interfaces.C.int;
    events  : Interfaces.C.short;
    revents : Interfaces.C.short;
  END RECORD;

  O_RDONLY : CONSTANT := 16#00000000#;
  O_WRONLY : CONSTANT := 16#00000001#;
  O_RDWR   : CONSTANT := 16#00000002#;
  O_CREAT  : CONSTANT := 16#00000200#;
  O_TRUNC  : CONSTANT := 16#00001000#;
  O_APPEND : CONSTANT := 16#00002000#;

  POLLIN   : CONSTANT := 16#0001#;
  POLLOUT  : CONSTANT := 16#0004#;
  POLLERR  : CONSTANT := 16#0008#;
  POLLHUP  : CONSTANT := 16#0010#;
  POLLNVAL : CONSTANT := 16#0020#;

  ETIMEOUT : CONSTANT := 100000;

  FUNCTION C_open
   (path      : Interfaces.C.char_array;
    flags     : Interfaces.C.int;
    mode      : Interfaces.C.int := 0) RETURN Interfaces.C.int;

  FUNCTION C_close(fd : Interfaces.C.int) RETURN Interfaces.C.int;

  FUNCTION C_read
   (fd        : Interfaces.C.int;
    src       : System.Address;
    len       : Interfaces.C.size_t) RETURN ssize_t;

  FUNCTION C_write
   (fd        : Interfaces.C.int;
    dst       : System.Address;
    len       : Interfaces.C.size_t) RETURN ssize_t;

  FUNCTION C_poll
   (fds       : System.Address;
    nfds      : Interfaces.C.int;
    timeoutms : Interfaces.C.int) RETURN Interfaces.C.int;

  PROCEDURE C_strerror_r
   (err       : Interfaces.C.int;
    dst       : IN OUT Interfaces.C.char_array;
    len       : Interfaces.C.size_t);

  FUNCTION C_errno RETURN Interfaces.C.int;

  PRAGMA Import(C, C_open,       "open");
  PRAGMA Import(C, C_close,      "close");
  PRAGMA Import(C, C_read,       "read");
  PRAGMA Import(C, C_write,      "write");
  PRAGMA Import(C, C_poll,       "poll");
  PRAGMA Import(C, C_errno,      "__get_errno"); -- From GNAT runtime library
  PRAGMA Import(C, C_strerror_r, "strerror_r");  -- __xpg_strerror_r for Linux

  -- Nicer Ada wrappers

  PROCEDURE Open
   (filename  : String;
    flags     : Natural;
    mode      : Natural;
    fd        : OUT Integer;
    error     : OUT Integer);

  PROCEDURE Open_Read
   (filename  : String;
    fd        : OUT Integer;
    error     : OUT Integer);

  PROCEDURE Open_Write
   (filename  : String;
    fd        : OUT Integer;
    error     : OUT Integer);

  PROCEDURE Open_ReadWrite
   (filename  : String;
    fd        : OUT Integer;
    error     : OUT Integer);

  PROCEDURE Close
   (fd        : Integer;
    error     : OUT Integer);

  PROCEDURE Read
   (fd        : Integer;
    buf       : System.Address;
    len       : Integer;
    count     : OUT Natural;
    error     : OUT Integer);

  PROCEDURE Write
   (fd        : Integer;
    buf       : System.Address;
    len       : Integer;
    count     : OUT Natural;
    error     : OUT Integer);

  TYPE fds_array    IS ARRAY (Positive RANGE <>) OF Natural;
  TYPE events_array IS ARRAY (Positive RANGE <>) OF Natural;

  PROCEDURE Poll
   (files     : fds_array;
    events    : events_array;
    revents   : OUT events_array;
    timeoutms : Natural;
    error     : OUT Integer);

  PROCEDURE Poll
   (fd        : Integer;
    events    : Natural;
    revents   : OUT Natural;
    timeoutms : Natural;
    error     : OUT Integer);

  PROCEDURE Poll
   (fd        : Integer;
    timeoutms : Natural;
    error     : OUT Integer);

  FUNCTION errno RETURN Integer;

  FUNCTION strerror(err : Integer) RETURN String;

END Posix;