blob: a3fbe257cde20192e3168dd204bae6cb29c52ce0 [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301------------------------------------------------------------------------------
2-- --
3-- GNAT ncurses Binding --
4-- --
5-- Terminal_Interface.Curses.Terminfo --
6-- --
7-- B O D Y --
8-- --
9------------------------------------------------------------------------------
10-- Copyright (c) 1998-2000,2006 Free Software Foundation, Inc. --
11-- --
12-- Permission is hereby granted, free of charge, to any person obtaining a --
13-- copy of this software and associated documentation files (the --
14-- "Software"), to deal in the Software without restriction, including --
15-- without limitation the rights to use, copy, modify, merge, publish, --
16-- distribute, distribute with modifications, sublicense, and/or sell --
17-- copies of the Software, and to permit persons to whom the Software is --
18-- furnished to do so, subject to the following conditions: --
19-- --
20-- The above copyright notice and this permission notice shall be included --
21-- in all copies or substantial portions of the Software. --
22-- --
23-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS --
24-- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF --
25-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. --
26-- IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, --
27-- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR --
28-- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR --
29-- THE USE OR OTHER DEALINGS IN THE SOFTWARE. --
30-- --
31-- Except as contained in this notice, the name(s) of the above copyright --
32-- holders shall not be used in advertising or otherwise to promote the --
33-- sale, use or other dealings in this Software without prior written --
34-- authorization. --
35------------------------------------------------------------------------------
36-- Author: Juergen Pfeifer, 1996
37-- Version Control:
38-- $Revision: 1.5 $
39-- $Date: 2006/06/25 14:30:22 $
40-- Binding Version 01.00
41------------------------------------------------------------------------------
42
43with Terminal_Interface.Curses.Aux; use Terminal_Interface.Curses.Aux;
44with Interfaces.C; use Interfaces.C;
45with Interfaces.C.Strings; use Interfaces.C.Strings;
46with Ada.Unchecked_Conversion;
47
48package body Terminal_Interface.Curses.Terminfo is
49
50 function Is_MinusOne_Pointer (P : in chars_ptr) return Boolean;
51
52 function Is_MinusOne_Pointer (P : in chars_ptr) return Boolean is
53 type Weird_Address is new System.Storage_Elements.Integer_Address;
54 Invalid_Pointer : constant Weird_Address := -1;
55 function To_Weird is new Ada.Unchecked_Conversion
56 (Source => chars_ptr, Target => Weird_Address);
57 begin
58 if To_Weird (P) = Invalid_Pointer then
59 return True;
60 else
61 return False;
62 end if;
63 end Is_MinusOne_Pointer;
64 pragma Inline (Is_MinusOne_Pointer);
65
66------------------------------------------------------------------------------
67 function Get_Flag (Name : String) return Boolean
68 is
69 function tigetflag (id : char_array) return Curses_Bool;
70 pragma Import (C, tigetflag);
71 Txt : char_array (0 .. Name'Length);
72 Length : size_t;
73 begin
74 To_C (Name, Txt, Length);
75 if tigetflag (Txt) = Curses_Bool (Curses_True) then
76 return True;
77 else
78 return False;
79 end if;
80 end Get_Flag;
81
82------------------------------------------------------------------------------
83 procedure Get_String (Name : String;
84 Value : out Terminfo_String;
85 Result : out Boolean)
86 is
87 function tigetstr (id : char_array) return chars_ptr;
88 pragma Import (C, tigetstr, "tigetstr");
89 Txt : char_array (0 .. Name'Length);
90 Length : size_t;
91 Txt2 : chars_ptr;
92 begin
93 To_C (Name, Txt, Length);
94 Txt2 := tigetstr (Txt);
95 if Txt2 = Null_Ptr then
96 Result := False;
97 elsif Is_MinusOne_Pointer (Txt2) then
98 raise Curses_Exception;
99 else
100 Value := Terminfo_String (Fill_String (Txt2));
101 Result := True;
102 end if;
103 end Get_String;
104
105------------------------------------------------------------------------------
106 function Has_String (Name : String) return Boolean
107 is
108 function tigetstr (id : char_array) return chars_ptr;
109 pragma Import (C, tigetstr, "tigetstr");
110 Txt : char_array (0 .. Name'Length);
111 Length : size_t;
112 Txt2 : chars_ptr;
113 begin
114 To_C (Name, Txt, Length);
115 Txt2 := tigetstr (Txt);
116 if Txt2 = Null_Ptr then
117 return False;
118 elsif Is_MinusOne_Pointer (Txt2) then
119 raise Curses_Exception;
120 else
121 return True;
122 end if;
123 end Has_String;
124
125------------------------------------------------------------------------------
126 function Get_Number (Name : String) return Integer is
127 function tigetstr (s : char_array) return C_Int;
128 pragma Import (C, tigetstr);
129 Txt : char_array (0 .. Name'Length);
130 Length : size_t;
131 begin
132 To_C (Name, Txt, Length);
133 return Integer (tigetstr (Txt));
134 end Get_Number;
135
136------------------------------------------------------------------------------
137 procedure Put_String (Str : Terminfo_String;
138 affcnt : Natural := 1;
139 putc : putctype := null) is
140 function tputs (str : char_array;
141 affcnt : C_Int;
142 putc : putctype) return C_Int;
143 function putp (str : char_array) return C_Int;
144 pragma Import (C, tputs);
145 pragma Import (C, putp);
146 Txt : char_array (0 .. Str'Length);
147 Length : size_t;
148 Err : C_Int;
149 begin
150 To_C (String (Str), Txt, Length);
151 if putc = null then
152 Err := putp (Txt);
153 else
154 Err := tputs (Txt, C_Int (affcnt), putc);
155 end if;
156 if Err = Curses_Err then
157 raise Curses_Exception;
158 end if;
159 end Put_String;
160
161end Terminal_Interface.Curses.Terminfo;