]> git.proxmox.com Git - rustc.git/blob - src/libterm/terminfo/searcher.rs
New upstream version 1.13.0+dfsg1
[rustc.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::env;
16 use std::fs;
17 use std::path::PathBuf;
18
19 /// Return path to database entry for `term`
20 #[allow(deprecated)]
21 pub fn get_dbpath_for_term(term: &str) -> Option<PathBuf> {
22 let mut dirs_to_search = Vec::new();
23 let first_char = match term.chars().next() {
24 Some(c) => c,
25 None => return None,
26 };
27
28 // Find search directory
29 match env::var_os("TERMINFO") {
30 Some(dir) => dirs_to_search.push(PathBuf::from(dir)),
31 None => {
32 if let Some(mut homedir) = env::home_dir() {
33 // ncurses compatibility;
34 homedir.push(".terminfo");
35 dirs_to_search.push(homedir)
36 }
37 match env::var("TERMINFO_DIRS") {
38 Ok(dirs) => {
39 for i in dirs.split(':') {
40 if i == "" {
41 dirs_to_search.push(PathBuf::from("/usr/share/terminfo"));
42 } else {
43 dirs_to_search.push(PathBuf::from(i));
44 }
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 // On Haiku the database can be found at /boot/system/data/terminfo
52 Err(..) => {
53 dirs_to_search.push(PathBuf::from("/etc/terminfo"));
54 dirs_to_search.push(PathBuf::from("/lib/terminfo"));
55 dirs_to_search.push(PathBuf::from("/usr/share/terminfo"));
56 dirs_to_search.push(PathBuf::from("/boot/system/data/terminfo"));
57 }
58 }
59 }
60 };
61
62 // Look for the terminal in all of the search directories
63 for mut p in dirs_to_search {
64 if fs::metadata(&p).is_ok() {
65 p.push(&first_char.to_string());
66 p.push(&term);
67 if fs::metadata(&p).is_ok() {
68 return Some(p);
69 }
70 p.pop();
71 p.pop();
72
73 // on some installations the dir is named after the hex of the char
74 // (e.g. OS X)
75 p.push(&format!("{:x}", first_char as usize));
76 p.push(term);
77 if fs::metadata(&p).is_ok() {
78 return Some(p);
79 }
80 }
81 }
82 None
83 }
84
85 #[test]
86 #[ignore(reason = "buildbots don't have ncurses installed and I can't mock everything I need")]
87 fn test_get_dbpath_for_term() {
88 // woefully inadequate test coverage
89 // note: current tests won't work with non-standard terminfo hierarchies (e.g. OS X's)
90 use std::env;
91 // FIXME (#9639): This needs to handle non-utf8 paths
92 fn x(t: &str) -> String {
93 let p = get_dbpath_for_term(t).expect("no terminfo entry found");
94 p.to_str().unwrap().to_string()
95 }
96 assert!(x("screen") == "/usr/share/terminfo/s/screen");
97 assert!(get_dbpath_for_term("") == None);
98 env::set_var("TERMINFO_DIRS", ":");
99 assert!(x("screen") == "/usr/share/terminfo/s/screen");
100 env::remove_var("TERMINFO_DIRS");
101 }