This file is indexed.

/usr/share/ada/adainclude/gnatcoll/gnatcoll-terminal.adb is in libgnatcoll16.1.0-dev 17.0.2017-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
------------------------------------------------------------------------------
--                             G N A T C O L L                              --
--                                                                          --
--                     Copyright (C) 2005-2017, AdaCore                     --
--                                                                          --
-- This library is free software;  you can redistribute it and/or modify it --
-- under terms of the  GNU General Public License  as published by the Free --
-- Software  Foundation;  either version 3,  or (at your  option) any later --
-- version. This library is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY;  without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE.                            --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Text_IO;    use Ada.Text_IO;
with GNAT.OS_Lib;    use GNAT.OS_Lib;

package body GNATCOLL.Terminal is

   On_Windows : constant Boolean := GNAT.OS_Lib.Directory_Separator = '\';

   Color_To_Win32 : constant array (ANSI_Color) of Integer :=
      (Unchanged => -1,
       Black     => 0,
       Red       => 4,
       Green     => 2,
       Yellow    => 6,
       Blue      => 1,
       Magenta   => 5,
       Cyan      => 3,
       Grey      => 7,
       Reset     => -1);

   Style_To_Win32 : constant array (ANSI_Style) of Integer :=
      (Unchanged => -1,
       Bright    => 16#08#,
       Dim       => 16#00#,  --  same as Normal
       Normal    => 16#00#,
       Reset_All => -1);

   procedure Win_Set_Console
      (Self  : Terminal_Info'Class;
       Attrs : Integer);
   --  Windows-specific implementation to change the attributes of the console

   procedure Decode_Windows_Attributes
      (Self  : in out Terminal_Info'Class;
       Attrs : Integer);
   --  Decode the value of the attributes returned by Windows, into the
   --  default parameters for the terminal.

   procedure Auto_Detect_Colors
      (Self    : in out Terminal_Info'Class;
       Support : Supports_Color);
   --  Auto-detect whether colors are supported

   function getConsoleScreenBufferInfo (Stderr : Integer) return Integer;
   pragma Import (C, getConsoleScreenBufferInfo,
                  "gnatcoll_get_console_screen_buffer_info");

   function terminal_has_colors (Fd : File_Descriptor) return Integer;
   pragma Import (C, terminal_has_colors, "gnatcoll_terminal_has_colors");
   --  Whether Fd is a terminal that supports color output

   -------------------------------
   -- Decode_Windows_Attributes --
   -------------------------------

   procedure Decode_Windows_Attributes
      (Self  : in out Terminal_Info'Class;
       Attrs : Integer)
   is
      type Mod_32 is mod 2 ** 32;
      A : Mod_32;
   begin
      Self.Default_Fore := Black;
      Self.Default_Back := Grey;
      Self.Default_Style := Normal;

      if not On_Windows then
         return;
      elsif Attrs = -1 then
         Self.Colors := Unsupported;
      else
         A := Mod_32 (Attrs);
         case A and 7 is
            when 0      => Self.Default_Fore := Black;
            when 1      => Self.Default_Fore := Blue;
            when 2      => Self.Default_Fore := Green;
            when 3      => Self.Default_Fore := Cyan;
            when 4      => Self.Default_Fore := Red;
            when 5      => Self.Default_Fore := Magenta;
            when 6      => Self.Default_Fore := Yellow;
            when others => Self.Default_Fore := Grey;
         end case;

         case (A / 16) and 7 is
            when 0      => Self.Default_Back := Black;
            when 1      => Self.Default_Back := Blue;
            when 2      => Self.Default_Back := Green;
            when 3      => Self.Default_Back := Cyan;
            when 4      => Self.Default_Back := Red;
            when 5      => Self.Default_Back := Magenta;
            when 6      => Self.Default_Back := Yellow;
            when others => Self.Default_Back := Grey;
         end case;

         if (A and 16#08#) /= 0 then
            Self.Default_Style := Bright;
         else
            Self.Default_Style := Normal;
         end if;
      end if;
   end Decode_Windows_Attributes;

   ------------------------
   -- Auto_Detect_Colors --
   ------------------------

   procedure Auto_Detect_Colors
      (Self    : in out Terminal_Info'Class;
       Support : Supports_Color)
   is
      Env : String_Access;
   begin
      case Support is
         when No =>
            Self.Colors := Unsupported;

         when Yes =>
            if On_Windows then
               Env := Getenv ("ANSICON");
               if Env = null or else Env.all = "" then
                  Self.Colors := WIN32_Sequences;
               else
                  Self.Colors := ANSI_Sequences;
               end if;
               Free (Env);
            else
               Self.Colors := ANSI_Sequences;
            end if;

         when Auto =>
            if On_Windows then
               --  This procedure is only called for stdout or stderr
               Env := Getenv ("ANSICON");
               if Env = null or else Env.all = "" then
                  Self.Colors := WIN32_Sequences;
               else
                  Self.Colors := ANSI_Sequences;
               end if;
               Free (Env);

            else
               if Self.FD = Stdout
                  and then terminal_has_colors (Standout) /= 0
               then
                  Self.Colors := ANSI_Sequences;
               elsif Self.FD = Stderr
                  and then terminal_has_colors (Standerr) /= 0
               then
                  Self.Colors := ANSI_Sequences;
               else
                  Self.Colors := Unsupported;
               end if;
            end if;
      end case;
   end Auto_Detect_Colors;

   ---------------------
   -- Init_For_Stdout --
   ---------------------

   procedure Init_For_Stdout
      (Self   : in out Terminal_Info;
       Colors : Supports_Color := Auto)
   is
   begin
      Self.FD := Stdout;
      Auto_Detect_Colors (Self, Colors);
      Decode_Windows_Attributes
         (Self, getConsoleScreenBufferInfo (Stderr => 0));
   end Init_For_Stdout;

   ---------------------
   -- Init_For_Stderr --
   ---------------------

   procedure Init_For_Stderr
      (Self   : in out Terminal_Info;
       Colors : Supports_Color := Auto)
   is
   begin
      Self.FD := Stderr;
      Auto_Detect_Colors (Self, Colors);
      Decode_Windows_Attributes
         (Self, getConsoleScreenBufferInfo (Stderr => 1));
   end Init_For_Stderr;

   -------------------
   -- Init_For_File --
   -------------------

   procedure Init_For_File
      (Self   : in out Terminal_Info;
       Colors : Supports_Color := Auto)
   is
   begin
      Self.FD := File;

      case Colors is
         when Yes =>
            --  Have to use ANSI sequences, since WIN32 sequences call
            --  subprograms on the terminal itself.
            Self.Colors := ANSI_Sequences;
         when No | Auto =>
            Self.Colors := Unsupported;
      end case;
   end Init_For_File;

   ----------------
   -- Has_Colors --
   ----------------

   function Has_Colors (Self : Terminal_Info) return Boolean is
   begin
      return Self.Colors /= Unsupported;
   end Has_Colors;

   ---------------------
   -- Win_Set_Console --
   ---------------------

   procedure Win_Set_Console
      (Self  : Terminal_Info'Class;
       Attrs : Integer)
   is
      procedure Set_Console_Text_Attribute (Stderr : Integer; Attrs : Integer);
      pragma Import (C, Set_Console_Text_Attribute,
                     "gnatcoll_set_console_text_attribute");
   begin
      Set_Console_Text_Attribute (Boolean'Pos (Self.FD = Stderr), Attrs);
   end Win_Set_Console;

   ------------
   -- Set_Fg --
   ------------

   procedure Set_Fg
      (Self  : in out Terminal_Info;
       Color : ANSI_Color;
       Term  : Ada.Text_IO.File_Type := Ada.Text_IO.Standard_Output)
   is
   begin
      Set_Color (Self, Term, Color, Unchanged, Unchanged);
   end Set_Fg;

   ------------
   -- Set_Bg --
   ------------

   procedure Set_Bg
      (Self  : in out Terminal_Info;
       Color : ANSI_Color;
       Term  : Ada.Text_IO.File_Type := Ada.Text_IO.Standard_Output)
   is
   begin
      Set_Color (Self, Term, Unchanged, Color, Unchanged);
   end Set_Bg;

   ---------------
   -- Set_Style --
   ---------------

   procedure Set_Style
      (Self  : in out Terminal_Info;
       Style : ANSI_Style;
       Term  : Ada.Text_IO.File_Type := Ada.Text_IO.Standard_Output)
   is
   begin
      Set_Color (Self, Term, Unchanged, Unchanged, Style);
   end Set_Style;

   ---------------
   -- Set_Color --
   ---------------

   procedure Set_Color
      (Self       : in out Terminal_Info;
       Term       : Ada.Text_IO.File_Type := Ada.Text_IO.Standard_Output;
       Foreground : ANSI_Color := Unchanged;
       Background : ANSI_Color := Unchanged;
       Style      : ANSI_Style := Unchanged)
   is
      Attrs : Integer := 0;
   begin
      case Self.Colors is
         when Unsupported =>
            null;

         when ANSI_Sequences =>
            case Style is
               when Unchanged => null;
               when Bright    => Put (Term, ASCII.ESC & "[1m");
               when Dim       => Put (Term, ASCII.ESC & "[2m");
               when Normal    => Put (Term, ASCII.ESC & "[22m");
               when Reset_All => Put (Term, ASCII.ESC & "[0m");
            end case;

            case Foreground is
               when Unchanged => null;
               when Black     => Put (Term, ASCII.ESC & "[30m");
               when Red       => Put (Term, ASCII.ESC & "[31m");
               when Green     => Put (Term, ASCII.ESC & "[32m");
               when Yellow    => Put (Term, ASCII.ESC & "[33m");
               when Blue      => Put (Term, ASCII.ESC & "[34m");
               when Magenta   => Put (Term, ASCII.ESC & "[35m");
               when Cyan      => Put (Term, ASCII.ESC & "[36m");
               when Grey      => Put (Term, ASCII.ESC & "[37m");
               when Reset     => Put (Term, ASCII.ESC & "[39m");
            end case;

            case Background is
               when Unchanged => null;
               when Black     => Put (Term, ASCII.ESC & "[40m");
               when Red       => Put (Term, ASCII.ESC & "[41m");
               when Green     => Put (Term, ASCII.ESC & "[42m");
               when Yellow    => Put (Term, ASCII.ESC & "[43m");
               when Blue      => Put (Term, ASCII.ESC & "[44m");
               when Magenta   => Put (Term, ASCII.ESC & "[45m");
               when Cyan      => Put (Term, ASCII.ESC & "[46m");
               when Grey      => Put (Term, ASCII.ESC & "[47m");
               when Reset     => Put (Term, ASCII.ESC & "[49m");
            end case;

         when WIN32_Sequences =>
            if Style = Reset_All then
               Self.Style := Self.Default_Style;
               Self.Fore  := Self.Default_Fore;
               Self.Back  := Self.Default_Back;
            elsif Style /= Unchanged then
               Self.Style := Style;
            end if;

            if Foreground = Reset then
               Self.Fore := Self.Default_Fore;
            elsif Foreground /= Unchanged then
               Self.Fore := Foreground;
            end if;

            if Background = Reset then
               Self.Back := Self.Default_Back;
            elsif Background /= Unchanged then
               Self.Back := Background;
            end if;

            Attrs := Attrs + Style_To_Win32 (Self.Style) +
               Color_To_Win32 (Self.Fore) +
               Color_To_Win32 (Self.Back) * 16;

            Win_Set_Console (Self, Attrs);
      end case;
   end Set_Color;

   -----------------------
   -- Beginning_Of_Line --
   -----------------------

   procedure Beginning_Of_Line (Self : in out Terminal_Info) is
      procedure Internal (Stderr : Integer);
      pragma Import (C, Internal, "gnatcoll_beginning_of_line");
   begin
      if Self.FD = File or else Self.Colors = Unsupported then
         null;
      else
         Internal (Boolean'Pos (Self.FD = Stderr));
      end if;
   end Beginning_Of_Line;

   --------------------------
   -- Clear_To_End_Of_Line --
   --------------------------

   procedure Clear_To_End_Of_Line (Self : in out Terminal_Info) is
      procedure Internal (Stderr : Integer);
      pragma Import (C, Internal, "gnatcoll_clear_to_end_of_line");
   begin
      if Self.FD = File or else Self.Colors = Unsupported then
         null;
      else
         Internal (Boolean'Pos (Self.FD = Stderr));
      end if;
   end Clear_To_End_Of_Line;

   ---------------
   -- Get_Width --
   ---------------

   function Get_Width (Self : Terminal_Info) return Integer is
      function Internal (Stderr : Integer) return Integer;
      pragma Import (C, Internal, "gnatcoll_terminal_width");
   begin
      if Self.FD = File or else Self.Colors = Unsupported then
         return -1;
      else
         return Internal (Boolean'Pos (Self.FD = Stderr));
      end if;
   end Get_Width;
end GNATCOLL.Terminal;