multitudes / 42-minishell

This project is about creating a simple shell
MIT License
0 stars 0 forks source link

check export expansion - check variable names [_a-zA-Z][[_0-9a-zA-Z]]* #171

Closed multitudes closed 2 months ago

multitudes commented 3 months ago

variables names have a stricter rules.

They matches like in C the regex pattern [_a-zA-Z][[_0-9a-zA-Z]]*

ex export [_a-zA-Z][[_0-9a-zA-Z]]*=.... so the var name should match the pattern [_a-zA-Z][[_0-9a-zA-Z]]* which means, start with letter or underscore and continue with same but allowing digits

also mentioned in the book https://craftinginterpreters.com/scanning.html chapter 4.3

so this is not possible:

export 234fsd=fjskld

and this is a special case. The expansion mostly happens before the parsing in the tree. var becomes HOME then wehen we unset $var because $var was previously expanded we get unset HOME when I do export $var=home/rpriess it is really export HOME=home/rpriess

lbrusa@c3a4c7:/home/lbrusa/DEV/minishell$ export var=HOME        
lbrusa@c3a4c7:/home/lbrusa/DEV/minishell$ unset $var             
lbrusa@c3a4c7:/home/lbrusa/DEV/minishell$ export $var=home/rpriess
lbrusa@c3a4c7:/home/lbrusa/DEV/minishell$ env                     
PWD=/home/lbrusa/DEV/minishell
HOME=home/rpriess
var=HOME
SHLVL=1
PATH=/local/bin:/local/bin:/local/bin:/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_=/usr/bin/env
lbrusa@c3a4c7:/home/lbrusa/DEV/minishell$ 
ProjektPhoenix commented 3 months ago

some more cases:

echo "fjskdl$HOMEfjkslda"
export var=$HOME~$HOME
echo $var /$var2~
echo $var $var2/~
echo $var $var2
ProjektPhoenix commented 3 months ago

decide what to do with expansion of Special Parameters, such as $$, $N, $-, ... (https://www.gnu.org/software/bash/manual/bash.html#Special-Parameters). We could completely discard them or print them literally or put out a message, that they are not implemented.

ProjektPhoenix commented 3 months ago

also:

export rr=HOME
export $"$rr"=newHOME

would assign newHOME to variable HOME, presumably because the first "$" gets stripped away / replaced with empty string, and double quotes replaced by expanded $rr variable

ProjektPhoenix commented 3 months ago
export HOME = VAR2
bash: export: `=': not a valid identifier
ProjektPhoenix commented 3 months ago

another interesting case: echo VAR=~:~ returns VAR=HOMEVAL:HOMEVAL in bash echo ~:~ returns HOMEVAL:~ in bash

multitudes commented 3 months ago

some more cases: echo "fjskdl$HOMEfjkslda"

fjskdl as output is maybe because the value of $HOME contains a / character, which is interpreted as a directory separator. The shell is trying to find a file or directory named fjkslda in a directory named fjskdl$HOME. If it doesn't exist, nothing is printed after fjskdl.

export var=$HOME~$HOME echo $var /$var2~ echo $var $var2/~ echo $var $var2

also

export var=~
lbrusa@c4c1c1:~$ echo $var /$var2~
/home/lbrusa /~

export var=~$HOME
lbrusa@c4c1c1:~$ echo $var /$var2~
~/home/lbrusa /~
lbrusa@c4c1c1:~$ export var=~:$HOME
lbrusa@c4c1c1:~$ echo $var /$var2~
/home/lbrusa:/home/lbrusa /~

It seems ~ get expanded only when alone. if followed by a slash alone it expands but I think I recognize that token? ~/ If followed by$HOME it gets not expanded and merge with ~/home/...

lbrusa@c4c1c1:~$ export var=~
lbrusa@c4c1c1:~$ export var=~$var
lbrusa@c4c1c1:~$ echo $var 
~/home/lbrusa
lbrusa@c4c1c1:~$ export var=~$var
lbrusa@c4c1c1:~$ echo $var 
~~/home/lbrusa

As for the aboe it makes little sense. WE dont need to replicate the bugs. In the end it is our shell!

multitudes commented 3 months ago

another interesting case: echo VAR=~:~ returns VAR=HOMEVAL:HOMEVAL in bash echo ~:~ returns HOMEVAL:~ in bash

this makes sense though. We should aim for the resolution of vars divided by :

lbrusa@c4c1c1:~$ export var=~:~
lbrusa@c4c1c1:~$ echo $var 
/home/lbrusa:/home/lbrusa
lbrusa@c4c1c1:~$ 
multitudes commented 3 months ago

export HOME = VAR2

no spaces between var and value accepted...

ProjektPhoenix commented 3 months ago

export HOME = VAR2

no spaces between var and value accepted...

Yes, but we still need to ensure, that '=' is properly handled and not, i.e. misinterpreted as variable assignment

ProjektPhoenix commented 3 months ago

another interesting case: echo VAR=~:~ returns VAR=HOMEVAL:HOMEVAL in bash echo ~:~ returns HOMEVAL:~ in bash

this makes sense though. We should aim for the resolution of vars divided by :

lbrusa@c4c1c1:~$ export var=~:~
lbrusa@c4c1c1:~$ echo $var 
/home/lbrusa:/home/lbrusa
lbrusa@c4c1c1:~$ 

yes, of course. I put it here to ensure we do not forget.

multitudes commented 2 months ago

This were all extras s owe will open a new issue and test for the next version