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