1//! ncurses-compatible database discovery.
2//!
3//! Does not support hashed database, only filesystem!
45use std::path::PathBuf;
6use std::{env, fs};
78#[cfg(test)]
9mod tests;
1011/// Returns path to database entry for `term`
12#[allow(deprecated)]
13pub(crate) fn get_dbpath_for_term(term: &str) -> Option<PathBuf> {
14let mut dirs_to_search = Vec::new();
15let first_char = term.chars().next()?;
1617// Find search directory
18if let Some(dir) = env::var_os("TERMINFO") {
19 dirs_to_search.push(PathBuf::from(dir));
20 }
2122if let Ok(dirs) = env::var("TERMINFO_DIRS") {
23for i in dirs.split(':') {
24if i.is_empty() {
25 dirs_to_search.push(PathBuf::from("/usr/share/terminfo"));
26 } else {
27 dirs_to_search.push(PathBuf::from(i));
28 }
29 }
30 } else {
31// Found nothing in TERMINFO_DIRS, use the default paths:
32 // According to /etc/terminfo/README, after looking at
33 // ~/.terminfo, ncurses will search /etc/terminfo, then
34 // /lib/terminfo, and eventually /usr/share/terminfo.
35 // On Haiku the database can be found at /boot/system/data/terminfo
36if let Some(mut homedir) = env::home_dir() {
37 homedir.push(".terminfo");
38 dirs_to_search.push(homedir)
39 }
4041 dirs_to_search.push(PathBuf::from("/etc/terminfo"));
42 dirs_to_search.push(PathBuf::from("/lib/terminfo"));
43 dirs_to_search.push(PathBuf::from("/usr/share/terminfo"));
44 dirs_to_search.push(PathBuf::from("/boot/system/data/terminfo"));
45 }
4647// Look for the terminal in all of the search directories
48for mut p in dirs_to_search {
49if fs::metadata(&p).is_ok() {
50 p.push(&first_char.to_string());
51 p.push(term);
52if fs::metadata(&p).is_ok() {
53return Some(p);
54 }
55 p.pop();
56 p.pop();
5758// on some installations the dir is named after the hex of the char
59 // (e.g., macOS)
60p.push(&format!("{:x}", first_char as usize));
61 p.push(term);
62if fs::metadata(&p).is_ok() {
63return Some(p);
64 }
65 }
66 }
67None
68}