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