Compare commits

...

6 commits

11 changed files with 122 additions and 13 deletions

View file

@ -4,6 +4,10 @@
These are tools I use to use Windows primarily through WSL. A lot of them These are tools I use to use Windows primarily through WSL. A lot of them
assume you have PowerShell somewhere in your `PATH` as "`pwsh`". assume you have PowerShell somewhere in your `PATH` as "`pwsh`".
## `dos-var`
Gets Windows environment variables from DOS.
## `env-share` ## `env-share`
A hacked-together utility for sending WSL variables back to Windows A hacked-together utility for sending WSL variables back to Windows
@ -36,5 +40,4 @@ A feature-barren DOS clone of `ls` that hides Windows hidden files. Relies on
- `explorer.sh`: Launch explorer from WSL. - `explorer.sh`: Launch explorer from WSL.
- `pwsh.sh`: Launch PowerShell Core. - `pwsh.sh`: Launch PowerShell Core.
- `terminal.sh`: Launch Windows Terminal. - `terminal.sh`: Launch Windows Terminal.
- `userprofile.sh`: Get the Windows user profile directory (`%USERPROFILE%`).

2
dos-var/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
Cargo.lock

15
dos-var/Cargo.toml Normal file
View file

@ -0,0 +1,15 @@
[package]
name = "dos-var"
version = "0.0.2"
edition = "2021"
[dependencies]
pico-args = "0.5.0"
[profile.release]
opt-level = 's'
codegen-units = 1
debug = false
lto = true
panic = "abort"
strip = "symbols"

10
dos-var/justfile Normal file
View file

@ -0,0 +1,10 @@
alias b := build
alias i := install
build:
cargo build --release
install DIR: build
cp ./target/release/dos-var "{{DIR}}/dos-var"

13
dos-var/src/error.rs Normal file
View file

@ -0,0 +1,13 @@
use std::process::exit;
pub fn not_found(target: &String) {
eprintln!("dos-var: %{target}% has no value");
exit(1);
}
pub fn missing_arg() {
eprintln!("dos-var: no argument found");
exit(2);
}

40
dos-var/src/main.rs Normal file
View file

@ -0,0 +1,40 @@
use std::process::Command;
use pico_args::Arguments;
mod error;
const CMD: &str = "/mnt/c/Windows/System32/cmd.exe";
const FLAG_PATH: [&str; 2] = [ "-p", "--path" ];
pub fn main() {
let mut args = Arguments::from_env();
let convert_path = args.contains(FLAG_PATH);
if let Ok(Some(target)) = args.subcommand() {
let cmd = Command::new(CMD)
.current_dir("/mnt/c/")
.arg( format!("/C echo %{target}%") )
.output();
if let Ok(output) = cmd {
if let Ok(stdout) = String::from_utf8(output.stdout) {
// trim output
let stdout = stdout.trim_end_matches("\"\r\n");
// catch empty variable case
if stdout == format!("%{target}%") { error::not_found(&target); }
// handle path flag and write
let path =
if convert_path {
stdout
.replace("\\", "/")
.replace("C:/", "/mnt/c/")
} else { stdout.to_owned() };
println!("{path}");
}
}
} else { error::missing_arg(); }
}

View file

@ -9,6 +9,7 @@ install TARGET='all' DIR="~/.bin/":
#!/usr/bin/bash #!/usr/bin/bash
if [[ {{TARGET}} == 'all' ]]; then if [[ {{TARGET}} == 'all' ]]; then
just install scripts {{DIR}} just install scripts {{DIR}}
just install dos-var {{DIR}}
just install env-share {{DIR}} just install env-share {{DIR}}
just install mkwin {{DIR}} just install mkwin {{DIR}}
just install path-convert {{DIR}} just install path-convert {{DIR}}

36
man/dos-var.1 Normal file
View file

@ -0,0 +1,36 @@
.Dd $Mdocdate$
.Dt DOS-VAR 1
.Os
.Sh NAME
.Nm dos-var
.Nd gets DOS environment variables from WSL
.Sh SYNOPSIS
.Nm
.Op Fl p
.Ar variable
.Sh
.Sh DESCRIPTION
The
.Nm
utility gets a DOS environment variable from within WSL. It can also convert paths to UNIX.
.Bl -tag -width Ds
.It Fl p , Fl -path
The target DOS variable contains a path, and will be converted.
.It Ar variable
The name of the target DOS variable.
.El
.Sh EXIT STATUS
.Bl -tag -width Ds
.It 1
The target variable is empty.
.It 2
No target variable was provided.
.El
.Sh EXAMPLES
Navigate to the Windows user directory:
.Pp
.Dl $ cd `dos-var -p USERPROFILE`
.Pp
.Sh Authors
.An -nosplit
.An Valerie Wolfe Aq Mt sleeplessval@gmail.com

View file

@ -7,5 +7,4 @@ install DIR="~/.bin":
cp explorer.sh {{DIR}}/explorer cp explorer.sh {{DIR}}/explorer
cp pwsh.sh {{DIR}}/pwsh cp pwsh.sh {{DIR}}/pwsh
cp terminal.sh {{DIR}}/terminal cp terminal.sh {{DIR}}/terminal
cp userprofile.sh {{DIR}}/userprofile

View file

@ -13,7 +13,7 @@ fi
# fall back to user installation # fall back to user installation
if [ -z ${PWSH+x} ]; then if [ -z ${PWSH+x} ]; then
PWSH="`userprofile`/AppData/Local/Microsoft/WindowsApps/pwsh.exe" PWSH="`dos-var -p USERPROFILE`/AppData/Local/Microsoft/WindowsApps/pwsh.exe"
fi fi
# run target # run target

View file

@ -1,10 +0,0 @@
#!/usr/bin/bash
# absolutely monstrous string hackery
# get %USERPROFILE% using cmd and convert that path
USERPROFILE=`/mnt/c/Windows/System32/cmd.exe '/C' 'echo %USERPROFILE%' 2> /dev/null`
USERPROFILE=${USERPROFILE//\\//} # replace '\' separators with '/'
USERPROFILE=/mnt/c/${USERPROFILE#C:/} # replace 'C:/' with '/mnt/c/'
USERPROFILE=${USERPROFILE%$'\r'} # strip carriage return
echo $USERPROFILE