sophiakoulen / minishell

A simplified bash-like shell, with pipes, redirections and variable expansion.
4 stars 1 forks source link

export is broken when variable contains spaces #138

Open sophiakoulen opened 1 year ago

sophiakoulen commented 1 year ago

On my windows pc with WSL2:

minishell$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/Program Files/Microsoft VS Code/bin:/mnt/c/Program Files/Git/cmd:/mnt/c/Program Files/MySQL/MySQL Shell 8.0/bin/:/mnt/c/Users/sophia/AppData/Local/Microsoft/WindowsApps
minishell$ export PATH=$PATH
minishell: export: `(x86)/NVIDIA': not a valid identifier
minishell: export: `Corporation/PhysX/Common:/mnt/c/Program': not a valid identifier
minishell: export: `Files/NVIDIA': not a valid identifier
minishell: export: `Corporation/NVIDIA': not a valid identifier
minishell: export: `NvDLISR:/mnt/c/Program': not a valid identifier
minishell: export: `Files/Microsoft': not a valid identifier
minishell: export: `Code/bin:/mnt/c/Program': not a valid identifier
minishell: export: `Files/Git/cmd:/mnt/c/Program': not a valid identifier
minishell: export: `Files/MySQL/MySQL': not a valid identifier
minishell: export: `8.0/bin/:/mnt/c/Users/sophia/AppData/Local/Microsoft/WindowsApps': not a valid identifier
minishell$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program
minishell$ export PATH=$PATH
minishell$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program
minishell$
sophiakoulen commented 1 year ago

Okay, i understand the issue now. export is broken when the variable contains spaces. Example: export var="a! b$ c?" creates a variable that contains spaces. now, when doing export var2=$var, the field splitting expands this to: export var2=a! b$ c? so b$ and c? will be considered separate variables.

sophiakoulen commented 1 year ago

This is a tricky one. Because we cannot solve this by escaping spaces, since spaces do not need to be escaped. The core of the issue, is that bash parses assignments in a particular way.

What i can think of is creating an item with flag ASSIGNMENT, so that we know that we should not perform field-splitting on it. But to do it right this demands quite some thinking, and updating the tokenizer and the parser and the expansion and the export builtin. Maybe not realistic to fix this before eval.

sophiakoulen commented 1 year ago

Well, maybe it is doable. We just need to parse the = token as meaning something like assignment value. t_item with flag ASSIGNMENT_VALUE will not be splitted. (We already handle not splitting with heredocs). And once we expanded stuff export is agnostic about this.