From ea44427be3af0b400e08f598f35ad48e73bc60a1 Mon Sep 17 00:00:00 2001 From: Honza Pokorny Date: Fri, 6 Jan 2017 22:06:45 -0400 Subject: [PATCH 1/5] Add "shell" option to config This allows you to configure the shell to use when alacritty starts. cc: #122 --- alacritty.yml | 5 +++++ alacritty_macos.yml | 4 ++++ src/config.rs | 9 +++++++++ src/main.rs | 2 +- src/tty.rs | 16 +++++++++++----- 5 files changed, 30 insertions(+), 6 deletions(-) diff --git a/alacritty.yml b/alacritty.yml index 0d907ea0..faa96135 100644 --- a/alacritty.yml +++ b/alacritty.yml @@ -192,3 +192,8 @@ key_bindings: mouse_bindings: - { mouse: Middle, action: PasteSelection } + +# Shell +# +# You can set this to a path to your favorite shell, e.g. /bin/fish +shell: diff --git a/alacritty_macos.yml b/alacritty_macos.yml index 25dbb1b2..b0e462c3 100644 --- a/alacritty_macos.yml +++ b/alacritty_macos.yml @@ -192,3 +192,7 @@ key_bindings: mouse_bindings: - { mouse: Middle, action: PasteSelection } +# Shell +# +# You can set this to a path to your favorite shell, e.g. /bin/fish +shell: diff --git a/src/config.rs b/src/config.rs index 36f0c363..188042ed 100644 --- a/src/config.rs +++ b/src/config.rs @@ -196,6 +196,8 @@ pub struct Config { #[serde(default="default_mouse_bindings")] mouse_bindings: Vec, + shell: Option, + /// Path where config was loaded from config_path: Option, } @@ -228,6 +230,7 @@ impl Default for Config { colors: Default::default(), key_bindings: Vec::new(), mouse_bindings: Vec::new(), + shell: None, config_path: None, } } @@ -878,6 +881,12 @@ impl Config { .map(|p| p.as_path()) } + pub fn shell(&self) -> Option<&Path> { + self.shell + .as_ref() + .map(|p| p.as_path()) + } + fn load_from>(path: P) -> Result { let path = path.into(); let raw = Config::read_file(path.as_path())?; diff --git a/src/main.rs b/src/main.rs index d53daf0f..2cf6dc5a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -93,7 +93,7 @@ fn run(mut config: Config, options: cli::Options) -> Result<(), Box> { // The pty forks a process to run the shell on the slave side of the // pseudoterminal. A file descriptor for the master side is retained for // reading/writing to the shell. - let mut pty = tty::new(display.size()); + let mut pty = tty::new(&config, display.size()); // Create the pseudoterminal I/O loop // diff --git a/src/tty.rs b/src/tty.rs index 9a4d4237..78e2a8ff 100644 --- a/src/tty.rs +++ b/src/tty.rs @@ -25,6 +25,7 @@ use libc::{self, winsize, c_int, pid_t, WNOHANG, WIFEXITED, WEXITSTATUS, SIGCHLD use term::SizeInfo; use display::OnResize; +use config::Config; /// Process ID of child process /// @@ -202,14 +203,19 @@ fn get_pw_entry(buf: &mut [i8; 1024]) -> Passwd { } /// Exec a shell -fn execsh() -> ! { +fn execsh(config: &Config) -> ! { let mut buf = [0; 1024]; let pw = get_pw_entry(&mut buf); + let shell = match config.shell() { + Some(shell) => shell.to_str().unwrap(), + None => pw.shell + }; + // setup environment env::set_var("LOGNAME", pw.name); env::set_var("USER", pw.name); - env::set_var("SHELL", pw.shell); + env::set_var("SHELL", shell); env::set_var("HOME", pw.dir); env::set_var("TERM", "xterm-256color"); // sigh @@ -223,7 +229,7 @@ fn execsh() -> ! { } // pw.shell is null terminated - let shell = unsafe { CStr::from_ptr(pw.shell.as_ptr() as *const _) }; + let shell = unsafe { CStr::from_ptr(shell.as_ptr() as *const _) }; let argv = [shell.as_ptr(), ptr::null()]; @@ -239,7 +245,7 @@ fn execsh() -> ! { } /// Create a new tty and return a handle to interact with it. -pub fn new(size: T) -> Pty { +pub fn new(config: &Config, size: T) -> Pty { let win = size.to_winsize(); let (master, slave) = openpty(win.ws_row as _, win.ws_col as _); @@ -265,7 +271,7 @@ pub fn new(size: T) -> Pty { } // Exec a shell! - execsh(); + execsh(config); }, Relation::Parent(pid) => { unsafe { From e98e6bdf644fbb30bfcfcebd95b27f1ceee6dd1f Mon Sep 17 00:00:00 2001 From: Honza Pokorny Date: Sat, 7 Jan 2017 08:43:09 -0400 Subject: [PATCH 2/5] Add doc string to 'shell' config option --- src/config.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/config.rs b/src/config.rs index 188042ed..eca151da 100644 --- a/src/config.rs +++ b/src/config.rs @@ -196,6 +196,7 @@ pub struct Config { #[serde(default="default_mouse_bindings")] mouse_bindings: Vec, + /// Path to a shell program to ru on startup shell: Option, /// Path where config was loaded from From a01ef5568fcae92f420dd9314d916b80e2507ff8 Mon Sep 17 00:00:00 2001 From: Honza Pokorny Date: Sat, 7 Jan 2017 08:43:34 -0400 Subject: [PATCH 3/5] Avoid unnecessary closure in a map call --- src/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index eca151da..a26455fb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -885,7 +885,7 @@ impl Config { pub fn shell(&self) -> Option<&Path> { self.shell .as_ref() - .map(|p| p.as_path()) + .map(PathBuf::as_path) } fn load_from>(path: P) -> Result { From 7ed4f205ad86501c86f1f51533170bed187f7b8f Mon Sep 17 00:00:00 2001 From: Honza Pokorny Date: Sat, 7 Jan 2017 08:43:58 -0400 Subject: [PATCH 4/5] Avoid unwrap when determining proper shell to use --- src/tty.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/tty.rs b/src/tty.rs index 78e2a8ff..ad89397d 100644 --- a/src/tty.rs +++ b/src/tty.rs @@ -208,7 +208,10 @@ fn execsh(config: &Config) -> ! { let pw = get_pw_entry(&mut buf); let shell = match config.shell() { - Some(shell) => shell.to_str().unwrap(), + Some(shell) => match shell.to_str() { + Some(shell) => shell, + None => die!("Invalid shell value") + }, None => pw.shell }; From 962dd61348d788fa48a6554ff8a079537d67fc0a Mon Sep 17 00:00:00 2001 From: Honza Pokorny Date: Sat, 7 Jan 2017 13:07:06 -0400 Subject: [PATCH 5/5] Typo --- src/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index a26455fb..3241b78b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -196,7 +196,7 @@ pub struct Config { #[serde(default="default_mouse_bindings")] mouse_bindings: Vec, - /// Path to a shell program to ru on startup + /// Path to a shell program to run on startup shell: Option, /// Path where config was loaded from