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!
19 /// Return path to database entry for `term`
20 pub fn get_dbpath_for_term(term: &str) -> Option<Box<Path>> {
25 let homedir = os::homedir();
27 let mut dirs_to_search = Vec::new();
28 let first_char = term.char_at(0);
30 // Find search directory
31 match getenv("TERMINFO") {
32 Some(dir) => dirs_to_search.push(Path::new(dir)),
34 if homedir.is_some() {
35 // ncurses compatability;
36 dirs_to_search.push(homedir.unwrap().join(".terminfo"))
38 match getenv("TERMINFO_DIRS") {
39 Some(dirs) => for i in dirs.as_slice().split(':') {
41 dirs_to_search.push(Path::new("/usr/share/terminfo"));
43 dirs_to_search.push(Path::new(i.to_string()));
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.
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"));
59 // Look for the terminal in all of the search directories
60 for p in dirs_to_search.iter() {
62 let f = str::from_char(first_char);
63 let newp = p.join_many([f.as_slice(), term]);
65 return Some(box newp);
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.as_slice(), term]);
71 return Some(box newp);
78 /// Return open file for `term`
79 pub fn open(term: &str) -> Result<File, String> {
80 match get_dbpath_for_term(term) {
84 Err(e) => Err(format_strbuf!("error opening file: {}", e)),
88 Err(format_strbuf!("could not find terminfo entry for {}", term))
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()
104 assert!(x("screen") == "/usr/share/terminfo/s/screen".to_string());
105 assert!(get_dbpath_for_term("") == None);
106 setenv("TERMINFO_DIRS", ":");
107 assert!(x("screen") == "/usr/share/terminfo/s/screen".to_string());
108 unsetenv("TERMINFO_DIRS");
112 #[ignore(reason = "see test_get_dbpath_for_term")]
114 open("screen").unwrap();
115 let t = open("nonexistent terminal that hopefully does not exist");