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
use std::collections::HashMap;
use std::env;
use std::path::{Path, PathBuf};
use std::fs::{self, File};
use std::io::Read;
use capability::{Capability, Value};
use names;
use error::{self, Error};
use nom::IResult;
use parser::compiled;
#[derive(Eq, PartialEq, Clone, Debug, Default)]
pub struct Database {
name: String,
aliases: Vec<String>,
description: String,
inner: HashMap<String, Value>,
}
impl Database {
pub fn new(name: String, aliases: Vec<String>, description: String, inner: HashMap<String, Value>) -> Self {
Database {
name: name,
aliases: aliases,
description: description,
inner: inner,
}
}
pub fn from_env() -> error::Result<Self> {
if let Ok(name) = env::var("TERM") {
Self::from_name(name)
}
else {
Err(Error::NotFound)
}
}
pub fn from_name<N: AsRef<str>>(name: N) -> error::Result<Self> {
let name = name.as_ref();
let first = name.chars().next().ok_or(Error::NotFound)?;
let mut search = Vec::<PathBuf>::new();
if let Some(dir) = env::var_os("TERMINFO") {
search.push(dir.into());
}
if let Ok(dirs) = env::var("TERMINFO_DIRS") {
for dir in dirs.split(':') {
if dir.is_empty() {
search.push("/usr/share/terminfo".into());
}
else {
search.push(dir.into());
}
}
}
else {
if let Some(mut home) = env::home_dir() {
home.push(".terminfo");
search.push(home.into());
}
search.push("/etc/terminfo".into());
search.push("/lib/terminfo".into());
search.push("/usr/share/terminfo".into());
search.push("/boot/system/data/terminfo".into());
}
for path in search {
if fs::metadata(&path).is_err() {
continue;
}
{
let mut path = path.clone();
path.push(first.to_string());
path.push(name);
if fs::metadata(&path).is_ok() {
return Self::from_path(path);
}
}
{
let mut path = path.clone();
path.push(format!("{:x}", first as usize));
path.push(name);
if fs::metadata(&path).is_ok() {
return Self::from_path(path);
}
}
}
Err(Error::NotFound)
}
pub fn from_path<P: AsRef<Path>>(path: P) -> error::Result<Self> {
let mut file = File::open(path)?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;
Self::from_buffer(buffer)
}
pub fn from_buffer<T: AsRef<[u8]>>(buffer: T) -> error::Result<Self> {
if let IResult::Done(_, database) = compiled::parse(buffer.as_ref()) {
Ok(database.into())
}
else {
Err(Error::Parse)
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn aliases(&self) -> &[String] {
&self.aliases
}
pub fn description(&self) -> &str {
&self.description
}
pub fn get<'a, C: Capability<'a>>(&'a self) -> Option<C> {
C::parse(self.inner.get(C::name()))
}
pub fn raw<S: AsRef<str>>(&self, name: S) -> Option<&Value> {
let name = name.as_ref();
self.inner.get(name).or_else(||
names::ALIASES.get(name).and_then(|&name| self.inner.get(name)))
}
}