]>
git.proxmox.com Git - rustc.git/blob - src/libterm/terminfo/searcher.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! ncurses-compatible database discovery
13 //! Does not support hashed database, only filesystem!
17 use std
::path
::PathBuf
;
19 /// Return path to database entry for `term`
21 pub fn get_dbpath_for_term(term
: &str) -> Option
<PathBuf
> {
22 let mut dirs_to_search
= Vec
::new();
23 let first_char
= match term
.chars().next() {
28 // Find search directory
29 match env
::var_os("TERMINFO") {
30 Some(dir
) => dirs_to_search
.push(PathBuf
::from(dir
)),
32 if let Some(mut homedir
) = env
::home_dir() {
33 // ncurses compatibility;
34 homedir
.push(".terminfo");
35 dirs_to_search
.push(homedir
)
37 match env
::var("TERMINFO_DIRS") {
39 for i
in dirs
.split('
:'
) {
41 dirs_to_search
.push(PathBuf
::from("/usr/share/terminfo"));
43 dirs_to_search
.push(PathBuf
::from(i
));
47 // Found nothing in TERMINFO_DIRS, use the default paths:
48 // According to /etc/terminfo/README, after looking at
49 // ~/.terminfo, ncurses will search /etc/terminfo, then
50 // /lib/terminfo, and eventually /usr/share/terminfo.
51 // On Haiku the database can be found at /boot/system/data/terminfo
53 dirs_to_search
.push(PathBuf
::from("/etc/terminfo"));
54 dirs_to_search
.push(PathBuf
::from("/lib/terminfo"));
55 dirs_to_search
.push(PathBuf
::from("/usr/share/terminfo"));
56 dirs_to_search
.push(PathBuf
::from("/boot/system/data/terminfo"));
62 // Look for the terminal in all of the search directories
63 for mut p
in dirs_to_search
{
64 if fs
::metadata(&p
).is_ok() {
65 p
.push(&first_char
.to_string());
67 if fs
::metadata(&p
).is_ok() {
73 // on some installations the dir is named after the hex of the char
75 p
.push(&format
!("{:x}", first_char
as usize));
77 if fs
::metadata(&p
).is_ok() {
86 #[ignore(reason = "buildbots don't have ncurses installed and I can't mock everything I need")]
87 fn test_get_dbpath_for_term() {
88 // woefully inadequate test coverage
89 // note: current tests won't work with non-standard terminfo hierarchies (e.g. OS X's)
91 // FIXME (#9639): This needs to handle non-utf8 paths
92 fn x(t
: &str) -> String
{
93 let p
= get_dbpath_for_term(t
).expect("no terminfo entry found");
94 p
.to_str().unwrap().to_string()
96 assert
!(x("screen") == "/usr/share/terminfo/s/screen");
97 assert
!(get_dbpath_for_term("") == None
);
98 env
::set_var("TERMINFO_DIRS", ":");
99 assert
!(x("screen") == "/usr/share/terminfo/s/screen");
100 env
::remove_var("TERMINFO_DIRS");