Paths for config

This commit is contained in:
2026-05-13 22:45:28 -04:00
commit 461bca8338
4 changed files with 178 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

7
Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "cacoon"
version = "0.1.0"

6
Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "cacoon"
version = "0.1.0"
edition = "2024"
[dependencies]

164
src/main.rs Normal file
View File

@@ -0,0 +1,164 @@
use std::env;
use std::fs;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::process::Command;
fn main() {
let mut shell = ShellState::new();
if let Err(err) = run_startup(&mut shell) {
eprintln!("cacoon: startup: {err}");
}
repl(&mut shell);
}
struct ShellState {
// Later: aliases, variables, functions, options, etc.
}
impl ShellState {
fn new() -> Self {
Self {}
}
}
struct SilkInterpreter;
impl SilkInterpreter {
fn new() -> Self {
Self
}
fn eval_file(
&mut self,
_path: &Path,
_source: &str,
_shell: &mut ShellState,
) -> Result<(), String> {
// Stub for now.
//
// Later this should parse/evaluate Silk and mutate ShellState:
// - aliases
// - exported vars
// - prompt config
// - shell options
// - functions
Ok(())
}
}
fn run_startup(shell: &mut ShellState) -> Result<(), String> {
let Some(config_path) = find_startup_config() else {
return Ok(());
};
let source = fs::read_to_string(&config_path)
.map_err(|err| format!("failed to read {}: {err}", config_path.display()))?;
let mut silk = SilkInterpreter::new();
silk.eval_file(&config_path, &source, shell)
.map_err(|err| format!("failed to evaluate {}: {err}", config_path.display()))?;
Ok(())
}
fn find_startup_config() -> Option<PathBuf> {
startup_config_candidates()
.into_iter()
.find(|path| path.is_file())
}
fn startup_config_candidates() -> Vec<PathBuf> {
let mut paths = Vec::new();
if let Some(home) = env::var_os("HOME") {
let home = PathBuf::from(home);
paths.push(home.join(".config/cacoonrc"));
paths.push(home.join(".cacoon/cacoonrc"));
paths.push(home.join(".cacoonrc"));
}
paths.push(PathBuf::from("/etc/cacoon/cacoonrc"));
paths
}
fn repl(_shell: &mut ShellState) {
loop {
print_prompt();
let mut line = String::new();
if io::stdin().read_line(&mut line).is_err() {
eprintln!("failed to read input");
continue;
}
let line = line.trim();
if line.is_empty() {
continue;
}
let mut parts = line.split_whitespace();
let cmd = parts.next().unwrap();
let args: Vec<&str> = parts.collect();
match cmd {
"cd" => {
let target = args
.first()
.map(PathBuf::from)
.or_else(|| env::var_os("HOME").map(PathBuf::from))
.unwrap_or_else(|| PathBuf::from("/"));
if let Err(err) = env::set_current_dir(&target) {
eprintln!("cd: {err}");
}
}
"pwd" => match env::current_dir() {
Ok(path) => println!("{}", path.display()),
Err(err) => eprintln!("pwd: {err}"),
},
"exit" => {
break;
}
"help" => {
println!("builtins: cd pwd exit help");
}
_ => {
match Command::new(cmd).args(args).status() {
Ok(status) => {
if !status.success() {
if let Some(code) = status.code() {
eprintln!("process exited with status {code}");
} else {
eprintln!("process terminated by signal");
}
}
}
Err(err) => {
eprintln!("{cmd}: {err}");
}
}
}
}
}
}
fn print_prompt() {
let cwd = env::current_dir()
.map(|p| p.display().to_string())
.unwrap_or_else(|_| "?".to_string());
print!("{cwd} $ ");
io::stdout().flush().unwrap();
}