File: readline.adb

package info (click to toggle)
topal 72-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,004 kB
  • ctags: 102
  • sloc: ada: 9,192; ansic: 768; sh: 233; makefile: 148
file content (106 lines) | stat: -rw-r--r-- 3,521 bytes parent folder | download | duplicates (7)
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
-- Topal: GPG/GnuPG and Alpine/Pine integration
-- Copyright (C) 2001--2008  Phillip J. Brooke
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License version 3 as
-- published by the Free Software Foundation.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.

with Ada.Text_IO;
with Externals.Simple;
with Globals;
with Interfaces.C;
with Interfaces.C.Strings;

package body Readline is

   pragma Linker_Options("ada-readline-c.o");
   pragma Linker_Options("-lreadline");

   function C_Get_String (Prompt : in Interfaces.C.Char_Array)
     return Interfaces.C.Strings.Chars_Ptr;
   pragma Import(C, C_Get_String, "rl_gets");

   procedure C_Add_History (History : in Interfaces.C.Char_Array);
   pragma Import(C, C_Add_History, "add_history");

   procedure  C_Disable_Tab_Completion;
   pragma Import(C, C_Disable_Tab_Completion, "rl_disable_tab_completion");

   procedure  C_Enable_Tab_Completion;
   pragma Import(C, C_Enable_Tab_Completion, "rl_enable_tab_completion");

   procedure C_Read_History(F : in Interfaces.C.Char_Array);
   pragma Import(C, C_Read_History, "read_history");

   procedure C_Write_History(F : in Interfaces.C.Char_Array);
   pragma Import(C, C_Write_History, "write_history");

   function Get_String (Prompt                : String  := "";
                        Enable_Tab_Completion : Boolean := False)
     return String is
      use Interfaces.C;
      use Interfaces.C.Strings;

      C_Result : Chars_Ptr;
   begin
      if Enable_Tab_Completion then
         C_Enable_Tab_Completion;
      else
         C_Disable_Tab_Completion;
      end if;
      C_Result := C_Get_String(To_C(Prompt));
      return To_Ada(Value(C_Result));
   exception
      when others =>
         Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
                              "Exception raised in Readline.Get_String");
         raise;
   end Get_String;

   procedure Add_History (History      : in String;
                          Remove_Empty : in Boolean := True) is
   begin
      if not (Remove_Empty and History = "") then
         C_Add_History(Interfaces.C.To_C(History));
      end if;
   exception
      when others =>
         Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
                              "Exception raised in Readline.Add_History");
         raise;
   end Add_History;

   procedure Save_History is
   begin
      C_Write_History(Interfaces.C.To_C(Globals.ToStr(Globals.Topal_Directory) & "/history"));
   exception
      when others =>
         Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
                              "Exception raised in Readline.Save_History");
         raise;
   end Save_History;

   procedure Load_History is
      H : constant String := Globals.ToStr(Globals.Topal_Directory) & "/history";
   begin
      if Externals.Simple.Test_R(H) then
         C_Read_History(Interfaces.C.To_C(H));
      end if;
   exception
      when others =>
         Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
                              "Exception raised in Readline.Load_History");
         raise;
   end Load_History;



end Readline;