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 /// Implement ncurses-compatible database discovery
12 /// Does not support hashed database, only filesystem!
18 /// Return path to database entry for `term`
19 pub fn get_dbpath_for_term(term: &str) -> Option<~Path> {
24 let homedir = os::homedir();
26 let mut dirs_to_search = ~[];
27 let first_char = term.char_at(0);
29 // Find search directory
30 match getenv("TERMINFO") {
31 Some(dir) => dirs_to_search.push(Path::new(dir)),
33 if homedir.is_some() {
34 // ncurses compatability;
35 dirs_to_search.push(homedir.unwrap().join(".terminfo"))
37 match getenv("TERMINFO_DIRS") {
38 Some(dirs) => for i in dirs.split(':') {
40 dirs_to_search.push(Path::new("/usr/share/terminfo"));
42 dirs_to_search.push(Path::new(i.to_owned()));
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.
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"));
58 // Look for the terminal in all of the search directories
59 for p in dirs_to_search.iter() {
61 let f = str::from_char(first_char);
62 let newp = p.join_many([f.as_slice(), term]);
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]);
77 /// Return open file for `term`
78 pub fn open(term: &str) -> Result<File, ~str> {
79 match get_dbpath_for_term(term) {
83 Err(e) => Err(format!("error opening file: {}", e)),
86 None => Err(format!("could not find terminfo entry for {}", term))
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()
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");
109 #[ignore(reason = "see test_get_dbpath_for_term")]
111 open("screen").unwrap();
112 let t = open("nonexistent terminal that hopefully does not exist");