clash_brush_core/shell/initscripts.rs
1//! Init script support for shells.
2
3use std::path::{Path, PathBuf};
4
5use crate::{Shell, error, extensions, interp};
6
7/// Behavior for loading profile files.
8#[derive(Default)]
9pub enum ProfileLoadBehavior {
10 /// Load the default profile files.
11 #[default]
12 LoadDefault,
13 /// Skip loading profile files.
14 Skip,
15}
16
17impl ProfileLoadBehavior {
18 /// Returns whether profile loading should be skipped.
19 pub const fn skip(&self) -> bool {
20 matches!(self, Self::Skip)
21 }
22}
23
24/// Behavior for loading rc files.
25#[derive(Default)]
26pub enum RcLoadBehavior {
27 /// Load the default rc files.
28 #[default]
29 LoadDefault,
30 /// Load a custom rc file; do not load defaults.
31 LoadCustom(PathBuf),
32 /// Skip loading rc files.
33 Skip,
34}
35
36impl RcLoadBehavior {
37 /// Returns whether rc loading should be skipped.
38 pub const fn skip(&self) -> bool {
39 matches!(self, Self::Skip)
40 }
41}
42
43impl<SE: extensions::ShellExtensions> Shell<SE> {
44 /// Loads and executes standard shell configuration files (i.e., rc and profile).
45 ///
46 /// # Arguments
47 ///
48 /// * `profile_behavior` - Behavior for loading profile files.
49 /// * `rc_behavior` - Behavior for loading rc files.
50 pub async fn load_config(
51 &mut self,
52 profile_behavior: &ProfileLoadBehavior,
53 rc_behavior: &RcLoadBehavior,
54 ) -> Result<(), error::Error> {
55 let mut params = self.default_exec_params();
56 params.process_group_policy = interp::ProcessGroupPolicy::SameProcessGroup;
57
58 if self.options.login_shell {
59 // --noprofile means skip this.
60 if matches!(profile_behavior, ProfileLoadBehavior::Skip) {
61 return Ok(());
62 }
63
64 //
65 // Source /etc/profile if it exists.
66 //
67 // Next source the first of these that exists and is readable (if any):
68 // * ~/.bash_profile
69 // * ~/.bash_login
70 // * ~/.profile
71 //
72 self.source_if_exists(Path::new("/etc/profile"), ¶ms)
73 .await?;
74 if let Some(home_path) = self.home_dir() {
75 if self.options.sh_mode {
76 self.source_if_exists(home_path.join(".profile").as_path(), ¶ms)
77 .await?;
78 } else {
79 if !self
80 .source_if_exists(home_path.join(".bash_profile").as_path(), ¶ms)
81 .await?
82 && !self
83 .source_if_exists(home_path.join(".bash_login").as_path(), ¶ms)
84 .await?
85 {
86 self.source_if_exists(home_path.join(".profile").as_path(), ¶ms)
87 .await?;
88 }
89 }
90 }
91 } else {
92 if self.options.interactive {
93 match rc_behavior {
94 _ if self.options.sh_mode => (),
95 RcLoadBehavior::Skip => (),
96 RcLoadBehavior::LoadCustom(rc_file) => {
97 // If an explicit rc file is provided, source it.
98 self.source_if_exists(rc_file, ¶ms).await?;
99 }
100 RcLoadBehavior::LoadDefault => {
101 //
102 // Otherwise, for non-login interactive shells, load in this order:
103 //
104 // /etc/bash.bashrc
105 // ~/.bashrc
106 //
107 self.source_if_exists(Path::new("/etc/bash.bashrc"), ¶ms)
108 .await?;
109 if let Some(home_path) = self.home_dir() {
110 self.source_if_exists(home_path.join(".bashrc").as_path(), ¶ms)
111 .await?;
112 self.source_if_exists(home_path.join(".brushrc").as_path(), ¶ms)
113 .await?;
114 }
115 }
116 }
117 } else {
118 let env_var_name = if self.options.sh_mode {
119 "ENV"
120 } else {
121 "BASH_ENV"
122 };
123
124 if self.env.is_set(env_var_name) {
125 //
126 // TODO(well-known-vars): look at $ENV/BASH_ENV; source its expansion if that
127 // file exists
128 //
129 return error::unimp(
130 "load config from $ENV/BASH_ENV for non-interactive, non-login shell",
131 );
132 }
133 }
134 }
135
136 Ok(())
137 }
138}