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