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!
16 use std::io::fs::PathExtensions;
20 /// Return path to database entry for `term`
21 pub fn get_dbpath_for_term(term: &str) -> Option<Box<Path>> {
26 let homedir = os::homedir();
28 let mut dirs_to_search = Vec::new();
29 let first_char = term.char_at(0);
31 // Find search directory
32 match getenv("TERMINFO") {
33 Some(dir) => dirs_to_search.push(Path::new(dir)),
35 if homedir.is_some() {
36 // ncurses compatibility;
37 dirs_to_search.push(homedir.unwrap().join(".terminfo"))
39 match getenv("TERMINFO_DIRS") {
40 Some(dirs) => for i in dirs.split(':') {
42 dirs_to_search.push(Path::new("/usr/share/terminfo"));
44 dirs_to_search.push(Path::new(i));
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.
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"));
60 // Look for the terminal in all of the search directories
61 for p in dirs_to_search.iter() {
63 let f = first_char.to_string();
64 let newp = p.join_many(&[f.as_slice(), term]);
66 return Some(box newp);
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]);
72 return Some(box newp);
79 /// Return open file for `term`
80 pub fn open(term: &str) -> Result<File, String> {
81 match get_dbpath_for_term(term) {
83 match File::open(&*x) {
85 Err(e) => Err(format!("error opening file: {}", e)),
89 Err(format!("could not find terminfo entry for {}", term))
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()
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");
113 #[ignore(reason = "see test_get_dbpath_for_term")]
115 open("screen").unwrap();
116 let t = open("nonexistent terminal that hopefully does not exist");