]> git.lizzy.rs Git - rust.git/blob - src/libterm/terminfo/searcher.rs
rollup merge of #21830: japaric/for-cleanup
[rust.git] / 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.
4 //
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.
10
11 //! ncurses-compatible database discovery
12 //!
13 //! Does not support hashed database, only filesystem!
14
15 use std::old_io::File;
16 use std::old_io::fs::PathExtensions;
17 use std::env;
18
19 /// Return path to database entry for `term`
20 pub fn get_dbpath_for_term(term: &str) -> Option<Box<Path>> {
21     if term.len() == 0 {
22         return None;
23     }
24
25     let homedir = env::home_dir();
26
27     let mut dirs_to_search = Vec::new();
28     let first_char = term.char_at(0);
29
30     // Find search directory
31     match env::var_string("TERMINFO") {
32         Ok(dir) => dirs_to_search.push(Path::new(dir)),
33         Err(..) => {
34             if homedir.is_some() {
35                 // ncurses compatibility;
36                 dirs_to_search.push(homedir.unwrap().join(".terminfo"))
37             }
38             match env::var_string("TERMINFO_DIRS") {
39                 Ok(dirs) => for i in dirs.split(':') {
40                     if i == "" {
41                         dirs_to_search.push(Path::new("/usr/share/terminfo"));
42                     } else {
43                         dirs_to_search.push(Path::new(i));
44                     }
45                 },
46                 // Found nothing in TERMINFO_DIRS, use the default paths:
47                 // According to  /etc/terminfo/README, after looking at
48                 // ~/.terminfo, ncurses will search /etc/terminfo, then
49                 // /lib/terminfo, and eventually /usr/share/terminfo.
50                 Err(..) => {
51                     dirs_to_search.push(Path::new("/etc/terminfo"));
52                     dirs_to_search.push(Path::new("/lib/terminfo"));
53                     dirs_to_search.push(Path::new("/usr/share/terminfo"));
54                 }
55             }
56         }
57     };
58
59     // Look for the terminal in all of the search directories
60     for p in &dirs_to_search {
61         if p.exists() {
62             let f = first_char.to_string();
63             let newp = p.join_many(&[&f[], term]);
64             if newp.exists() {
65                 return Some(box newp);
66             }
67             // on some installations the dir is named after the hex of the char (e.g. OS X)
68             let f = format!("{:x}", first_char as uint);
69             let newp = p.join_many(&[&f[], term]);
70             if newp.exists() {
71                 return Some(box newp);
72             }
73         }
74     }
75     None
76 }
77
78 /// Return open file for `term`
79 pub fn open(term: &str) -> Result<File, String> {
80     match get_dbpath_for_term(term) {
81         Some(x) => {
82             match File::open(&*x) {
83                 Ok(file) => Ok(file),
84                 Err(e) => Err(format!("error opening file: {:?}", e)),
85             }
86         }
87         None => {
88             Err(format!("could not find terminfo entry for {:?}", term))
89         }
90     }
91 }
92
93 #[test]
94 #[ignore(reason = "buildbots don't have ncurses installed and I can't mock everything I need")]
95 fn test_get_dbpath_for_term() {
96     // woefully inadequate test coverage
97     // note: current tests won't work with non-standard terminfo hierarchies (e.g. OS X's)
98     use std::os::{setenv, unsetenv};
99     // FIXME (#9639): This needs to handle non-utf8 paths
100     fn x(t: &str) -> String {
101         let p = get_dbpath_for_term(t).expect("no terminfo entry found");
102         p.as_str().unwrap().to_string()
103     };
104     assert!(x("screen") == "/usr/share/terminfo/s/screen");
105     assert!(get_dbpath_for_term("") == None);
106     setenv("TERMINFO_DIRS", ":");
107     assert!(x("screen") == "/usr/share/terminfo/s/screen");
108     unsetenv("TERMINFO_DIRS");
109 }
110
111 #[test]
112 #[ignore(reason = "see test_get_dbpath_for_term")]
113 fn test_open() {
114     open("screen").unwrap();
115     let t = open("nonexistent terminal that hopefully does not exist");
116     assert!(t.is_err());
117 }