gnatprove_11.2.3_f7ece6d3/share/examples/spark/tokeneer/keyboard-interfac.adb

  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
------------------------------------------------------------------
-- Tokeneer ID Station Core Software
--
-- Copyright (2003) United States Government, as represented
-- by the Director, National Security Agency. All rights reserved.
--
-- This material was originally developed by Praxis High Integrity
-- Systems Ltd. under contract to the National Security Agency.
------------------------------------------------------------------

------------------------------------------------------------------
-- Keyboard.Interfac
--
-- Implementation Notes:
--    Not SPARK as it uses a task to allow polling.
--
------------------------------------------------------------------
with Interfaces.C;
with Ada.Characters.Latin_1;
with Ada.Characters.Handling;

with System;

package body Keyboard.Interfac
  with SPARK_Mode => Off
is

   --PromptCoord : constant Win32.WinCon.Coord := (2, 4);

   ----------------------------------------------------------------------
   -- KeyedDataStore Protected Object
   --
   -- Description:
   --    This protected object provides a shared data repository for
   --    the Keyboard reader Task and the remainder of the system.
   --    The Reader Task Sets the value of KeyedData, the remainder
   --    of the system can then read the values.
   --
   ---------------------------------------------------------------------
   protected KeyedDataStore is

      ----------------------------------------------------------------------
      -- SetData
      --
      -- Description:
      --    Adds Keyed Data to the Store.
      --
      ---------------------------------------------------------------------
      procedure SetData (KeyedData : in Keyboard.DataTextT;
                         Length    : in Natural);

      ----------------------------------------------------------------------
      -- GetData
      --
      -- Description:
      --    Reads Keyed Data from the Store and clears the currently
      --    held Keyed Data so it is not read twice.
      --
      ---------------------------------------------------------------------
      procedure GetData (KeyedData : out Keyboard.DataTextT;
                         Length    : out Keyboard.DataLengthT;
                         Presence  : out CommonTypes.PresenceT);

      ----------------------------------------------------------------------
      -- Refresh
      --
      -- Description:
      --    Refreshes the data if it becomes too old.
      --
      ---------------------------------------------------------------------
      procedure Refresh;

   private
      TheData     : Keyboard.DataTextT := (others => ' ');
      TheLength   : Keyboard.DataLengthT := 0;
      ThePresence : CommonTypes.PresenceT := CommonTypes.Absent;
      IsFresh     : Boolean := False;
   end KeyedDataStore;

   ----------------------------------------------------------------------
   -- KeyedDataStore Protected Object
   --
   -- Implementation Notes:
   --     None.
   --
   ---------------------------------------------------------------------
   protected body KeyedDataStore is

      ----------------------------------------------------------------------
      -- SetData
      --
      -- Implementation Notes:
      --    Removes the CR and LF from the end of the read data.
      --    A blank line with no characters other than <Return>
      --    will not count at present data.
      ---------------------------------------------------------------------
      procedure SetData (KeyedData : in Keyboard.DataTextT;
                         Length    : in Natural)
      is
      begin
         TheData := KeyedData;
         if Length <= Keyboard.DataLengthT'Last then
            TheLength := Length;
         else
            TheLength := Keyboard.DataLengthT'Last;
         end if;

         -- Remove the CR & LF. A blank line does not count as present data.
         if TheLength > 2 then
            ThePresence := CommonTypes.Present;
            TheLength := TheLength - 2;
            IsFresh := True;
         else
            ThePresence := CommonTypes.Absent;
            TheLength := 0;
         end if;

      end SetData;

      ----------------------------------------------------------------------
      -- GetData
      --
      -- Implementation Notes:
      --    None.
      --
      ---------------------------------------------------------------------
      procedure GetData (KeyedData : out Keyboard.DataTextT;
                         Length    : out Keyboard.DataLengthT;
                         Presence  : out CommonTypes.PresenceT) is
      begin
         KeyedData := TheData;
         Length := TheLength;
         Presence := ThePresence;

         ThePresence := CommonTypes.Absent;
         TheLength := 0;

      end GetData;

      ----------------------------------------------------------------------
      -- Refresh
      --
      -- Implementation Notes:
      --    None.
      --
      ---------------------------------------------------------------------
      procedure Refresh is
      begin
         if not IsFresh then
            TheData := (others => ' ');
            ThePresence := CommonTypes.Absent;
            TheLength := 0;
         else
            IsFresh := False;
         end if;
      end Refresh;

   end KeyedDataStore;


   ------------------------------------------------------------------
   -- Types
   --
   ------------------------------------------------------------------
   ----------------------------------------------------------------------
   -- Reader Task
   --
   -- Description:
   --    This task encapsulates the process of reading the keyboard.
   --    It is required due to the blocking nature of the console read.
   --    Once Started it will continually read the Keyboard, placing the
   --    latest command in the KeyedDataStore.
   --    The Stop Entry allows task termination.
   --
   -- Implementation Notes:
   --    None.
   ---------------------------------------------------------------------

   task Reader is
      entry Start;
      entry Stop;
   end;

   ----------------------------------------------------------------------
   -- Read
   --
   -- Description:
   --    This operation reads a line from the Win32 Console. The operation
   --    blocks until the user presses <return>.
   --
   -- Implementation Notes:
   --    None.
   ---------------------------------------------------------------------
   procedure Read
     (Buffer : out String;
      Len    : out Natural;
      OK     : out Boolean)
   is
      pragma Unreferenced (Buffer);
   begin
      OK := True;
      Len := 0;
   end Read;

   ----------------------------------------------------------------------
   -- Reader Task
   --
   -- Implementation Notes:
   --    None.
   ---------------------------------------------------------------------
   task body Reader is
      ReadData   : Keyboard.DataTextT;
      DataLength : Natural;
      ReadOK     : Boolean;
   begin
      select
         accept Start;
         loop
            select
               accept Stop;
               exit;
            else
               Read(ReadData, DataLength, ReadOK);
               if ReadOK then
                  KeyedDataStore.SetData(ReadData, DataLength);
               end if;
            end select;
         end loop;
      or
         terminate;
      end select;
   end Reader;

   ------------------------------------------------------------------
   -- ReadKeyboardData
   --
   -- Implementation Notes:
   --    Converts to Upper case so that checks are case insensitive.
   --
   ------------------------------------------------------------------

   procedure ReadKeyboardData
     (KeyedDataPresence : out CommonTypes.PresenceT;
      KeyedData         : out Keyboard.DataTextT;
      KeyedDataLength   : out Keyboard.DataLengthT)
   is
      LocalKeyedData : Keyboard.DataTextT;

   begin
      KeyedData := (others => ' ');

      KeyedDataStore.GetData
        (LocalKeyedData,
         KeyedDataLength,
         KeyedDataPresence);

      if KeyedDataLength > 0 then
         for I in DataI range 1 .. KeyedDataLength loop
            KeyedData(I) :=
              Ada.Characters.Handling.To_Upper(LocalKeyedData(I));
         end loop;
      end if;
   end ReadKeyboardData;

   ------------------------------------------------------------------
   -- Init
   --
   -- Implementation Notes:
   --    None.
   --
   ------------------------------------------------------------------
   procedure Init is
   begin
      Reader.Start;
   end Init;

   ------------------------------------------------------------------
   -- Finalise
   --
   -- Implementation Notes:
   --    In order to force the current Win32.WinCon.Read to terminate
   --    we destroy the console.
   --    It is then possible to stop the Reader Task.
   --
   ------------------------------------------------------------------
   procedure Finalise is
   begin
      Reader.Stop;
   end Finalise;

   ------------------------------------------------------------------
   -- Poll
   --
   -- Implementation Notes:
   --    None.
   --
   ------------------------------------------------------------------
   procedure Poll is
   begin
      KeyedDataStore.Refresh;
   end Poll;

end Keyboard.Interfac;