Skip to main content

term/terminfo/
searcher.rs

1// Copyright 2019 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
15use std::env;
16use std::fs;
17use std::path::PathBuf;
18
19// The default terminfo location should be /usr/lib/terminfo but that's not guaranteed, so we check
20// a few more locations. See https://tldp.org/HOWTO/Text-Terminal-HOWTO-16.html#ss16.2
21const DEFAULT_LOCATIONS: &[&str] = &[
22    "/etc/terminfo",
23    "/usr/share/terminfo",
24    "/usr/lib/terminfo",
25    "/lib/terminfo",
26    #[cfg(target_os = "haiku")]
27    "/boot/system/data/terminfo",
28];
29
30/// Return path to database entry for `term`
31pub fn get_dbpath_for_term(term: &str) -> Option<PathBuf> {
32    let mut dirs_to_search = Vec::new();
33    let mut default_locations = DEFAULT_LOCATIONS.iter().map(PathBuf::from);
34    let first_char = term.chars().next()?;
35
36    // From the manual.
37    //
38    // > The  environment  variable TERMINFO is checked first, for a terminal
39    // > database containing the terminal description.
40    if let Some(dir) = env::var_os("TERMINFO") {
41        dirs_to_search.push(PathBuf::from(dir));
42    }
43
44    // > Next, ncurses looks in $HOME/.terminfo for a compiled description.
45    #[allow(deprecated)]
46    if let Some(mut homedir) = env::home_dir() {
47        homedir.push(".terminfo");
48        dirs_to_search.push(homedir)
49    }
50
51    // > Next, if the environment variable TERMINFO_DIRS is set, ncurses interprets
52    // > the contents of that variable as a list of colon-separated pathnames of
53    // > terminal databases to be searched.
54    // >
55    // > An  empty  pathname  (i.e.,  if  the  variable begins or ends with a
56    // > colon, or contains adjacent colons) is interpreted as the system location
57    // > /usr/share/terminfo.
58    if let Ok(dirs) = env::var("TERMINFO_DIRS") {
59        for i in dirs.split(':') {
60            if i.is_empty() {
61                dirs_to_search.extend(&mut default_locations);
62            } else {
63                dirs_to_search.push(PathBuf::from(i));
64            }
65        }
66    }
67
68    // > Finally, ncurses searches these compiled-in locations...
69    //
70    // NOTE: We only append these to `dirs_to_search` once. If we've already added these
71    // directories as specified in `TERMINFO_DIRS`, this operation will be a no-op.
72    dirs_to_search.extend(&mut default_locations);
73
74    // Look for the terminal in all of the search directories
75    for mut p in dirs_to_search {
76        if fs::metadata(&p).is_ok() {
77            p.push(first_char.to_string());
78            p.push(term);
79            if fs::metadata(&p).is_ok() {
80                return Some(p);
81            }
82            p.pop();
83            p.pop();
84
85            // on some installations the dir is named after the hex of the char
86            // (e.g. OS X)
87            p.push(format!("{:x}", first_char as usize));
88            p.push(term);
89            if fs::metadata(&p).is_ok() {
90                return Some(p);
91            }
92        }
93    }
94    None
95}