| 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 | |
| 15 | use std::env; |
| 16 | use std::fs; |
| 17 | use std::path::PathBuf; |
| 18 | |
| 19 | use dirs_next as dirs; |
| 20 | |
| 21 | /// Return path to database entry for `term` |
| 22 | pub fn get_dbpath_for_term(term: &str) -> Option<PathBuf> { |
| 23 | let mut dirs_to_search = Vec::new(); |
| 24 | let first_char = match term.chars().next() { |
| 25 | Some(c) => c, |
| 26 | None => return None, |
| 27 | }; |
| 28 | |
| 29 | // Find search directory |
| 30 | // The terminfo manual says: |
| 31 | // |
| 32 | // > If the environment variable TERMINFO is set, it is interpreted |
| 33 | // > as the pathname of a directory containing the compiled description |
| 34 | // > you are working on. Only that directory is searched. |
| 35 | // |
| 36 | // However, the ncurses manual says: |
| 37 | // |
| 38 | // > If the environment variable TERMINFO is defined, any program using |
| 39 | // > curses checks for a local terminal definition before checking in |
| 40 | // > the standard place. |
| 41 | // |
| 42 | // Given that ncurses is the defacto standard, we follow the ncurses manual. |
| 43 | if let Some(dir) = env::var_os("TERMINFO" ) { |
| 44 | dirs_to_search.push(PathBuf::from(dir)); |
| 45 | } |
| 46 | |
| 47 | if let Ok(dirs) = env::var("TERMINFO_DIRS" ) { |
| 48 | for i in dirs.split(':' ) { |
| 49 | if i == "" { |
| 50 | dirs_to_search.push(PathBuf::from("/usr/share/terminfo" )); |
| 51 | } else { |
| 52 | dirs_to_search.push(PathBuf::from(i)); |
| 53 | } |
| 54 | } |
| 55 | } else { |
| 56 | // Found nothing in TERMINFO_DIRS, use the default paths: |
| 57 | // According to /etc/terminfo/README, after looking at |
| 58 | // ~/.terminfo, ncurses will search /etc/terminfo, then |
| 59 | // /lib/terminfo, and eventually /usr/share/terminfo. |
| 60 | // On Haiku the database can be found at /boot/system/data/terminfo |
| 61 | if let Some(mut homedir) = dirs::home_dir() { |
| 62 | homedir.push(".terminfo" ); |
| 63 | dirs_to_search.push(homedir) |
| 64 | } |
| 65 | |
| 66 | dirs_to_search.push(PathBuf::from("/etc/terminfo" )); |
| 67 | dirs_to_search.push(PathBuf::from("/lib/terminfo" )); |
| 68 | dirs_to_search.push(PathBuf::from("/usr/share/terminfo" )); |
| 69 | dirs_to_search.push(PathBuf::from("/boot/system/data/terminfo" )); |
| 70 | } |
| 71 | |
| 72 | // Look for the terminal in all of the search directories |
| 73 | for mut p in dirs_to_search { |
| 74 | if fs::metadata(&p).is_ok() { |
| 75 | p.push(&first_char.to_string()); |
| 76 | p.push(&term); |
| 77 | if fs::metadata(&p).is_ok() { |
| 78 | return Some(p); |
| 79 | } |
| 80 | p.pop(); |
| 81 | p.pop(); |
| 82 | |
| 83 | // on some installations the dir is named after the hex of the char |
| 84 | // (e.g. OS X) |
| 85 | p.push(&format!(" {:x}" , first_char as usize)); |
| 86 | p.push(term); |
| 87 | if fs::metadata(&p).is_ok() { |
| 88 | return Some(p); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | None |
| 93 | } |
| 94 | |