Skip to content

Commit

Permalink
Merge pull request #136 from Haliris/shell_buster
Browse files Browse the repository at this point in the history
Shell psycho, also resets terminal properly when execute_builtin is called in main process.
  • Loading branch information
BenjaminHThomas authored Jul 30, 2024
2 parents b496926 + f920574 commit afc04f6
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 4 deletions.
141 changes: 141 additions & 0 deletions shell_psycho.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#!/bin/bash

MINISHELL_PATH="./minishell"
TEST_COMMANDS="test_commands.txt"
TEMP_DIR="./shell_comparison"
FIFO_PATH="$TEMP_DIR/minishell_fifo"

mkdir -p "$TEMP_DIR"
mkfifo "$FIFO_PATH"

# Start minishell in the background, reading from the FIFO
$MINISHELL_PATH < "$FIFO_PATH" > "$TEMP_DIR/minishell_output" 2>&1 &
MINISHELL_PID=$!

echo "
_________.__ .__ .__ __________ .__
/ _____/| |__ ____ | | | | \______ \_________.__. ____ | |__ ____
\_____ \ | | \_/ __ \| | | | | ___/ ___< | |/ ___\| | \ / _ \
/ \| Y \ ___/| |_| |__ | | \___ \ \___ \ \___| Y ( <_> )
/_______ /|___| /\___ >____/____/ |____| /____ >/ ____|\___ >___| /\____/
\/ \/ \/ \/ \/ \/ \/
"
echo "....... ............................... ..........................',;,,,'.................'''''',,,,,,;;;;;;::::ccccclllloo
............................... .......................................,;;;;:;,'...........''''''',,,,,,;;;;;:::::cccclllllooo
......................... ............................................',;;;cll:,'.......'''''',,,,,,;;;;;::::ccccccllloooood
........... ........ .................................................'',,;codxdc'....'''''',,,,,,;;;;;::::cccccllllooooddd
................. ..... ....''..............................................';codk00o,''''''',,,,,,,;;;;;:::cccccllllloooddddd
............ ... ...,;;::;;;;,,,,',,,,'''''.........................''',;codkKXXk:'''''',,,,,;;;;;::::cccccllllooooddddxx
............ .... ..',;:::::::::::::::;;;,,,,,'''''....................'',:ccldkOXN0o:;,'',,,,;;;;;:::::cccclllloooooddddxxx
............ ...... .',;:::::::ccc:::::::::;;;,,,'''''''..................'',:odxxkO0K0xoc;',,,,;;;;;:::cccccllllloooooddddxxxk
....... ............ ........ .';::::::::::::::::::::::;;;;,,,''''''''...............',,cdOKK0Okkkkdc,,,,,,,;;;:::cccccllllloooodddddxxxkk
....... ........... ........ .';:;:::::::::::::::::::::::;;,,,,,,,,'''''''...........';:cokXNX0kxkkdc;,,,,,,;;;:::ccccclllooooodddddxxxxkk
.......................... ...... ..;::;::::::::::::::::::::::::;;,,,,,,,,,,'''''..........';:cox0NNKkdxO0Od;,,,;;;;:::ccccccllloooodddddxxxxkkk
....................... ......... ..,;:::::::ccccc::::::::::::::::;;;;;;;;,,,,'''''..........';cod0NWXkolxKXXx:,;;;;::::ccccllllooooodddddxxxkkkk
...................... ........ ..';:::::ccccccccccccc:ccccc:::c::;;;;;;;;;,,,,''''..........,:lx0NWXxlccxKNXk:;;;;:::ccccllllloooodddddxxxxkkkO
...................... ........ ...,:::cccccccccclccccc:ccccccccc::;;;;;;;;;;,,,,,'''.........',:xKNWXkoc:cxKNNk:;:::::ccccclllloooodddddxxxkkkkO
...................... ......... ...';:ccccclllllllcccccccccccccccc:::::::;;;;;;,,,,,'''.........',o0NWNOoc::lkXWNkc::::ccccclllooooodddddxxxxkkkOO
....... .......... ....,:cccccllllllllllllcccccccccc::::cc:::;;;;;;;,,,,'''''........'cOXWN0dc::cd0NWNkc:::cccclllooooodddddxxxxkkkkOO
......................... ....,:ccclllllloolloolllccccccccc:::cccc::;;;;;;;,,,,'''''.........:xKWWKkoc:clkXNMXdc::ccccllloooooddddxxxxxkkkOOO
...................................,:clllllllooollllllcccccccccc::::cc:::;;;;;;;,,,,''''''........,oKWWXOdlclox0NWWKo::ccccllloooooddddxxxxkkkOOOO
...................................''',:cllllllooolllolllccccccccccc::::::::;;;;;;;;,,'''''''........':kNWN0koloxkOXNWXxccccccllloooooddddxxxkkkkOOO0
................. ..................''',;cllllooooooollolllccccccccccc:::c:::::;;;;;;,,''''''..........,dXWWKOxddxOKXNWWOlccccclllooooodddxxxxkkkkOOOO
............... .................'',,;clllooooooollllllccccc:::cccc:::::::::;;;,,,,,''''''..........':ONWNKkxxdkKXNWWKoccccclllooooddddxxxxkkkkOOO0
................ ................','',;;:cllllloollllllllcccccc::cccccccc::::::;;;;,,,'''''...........';kNWNKOkkkOKNNWWXxc:ccclllooooddddxxxkkkkOOOO0
.................................',;;;;;::clllloollllooollllcccc:::ccccccccccc::::;;,,,'''''...........':kNWWXOkOO0KNWWWXxcccccllloooodddxxxxkkkkOOOO0
................... .............,;;:::::ccllloolloooooooollcccc::::cccccccccc:::::;,,,,''''..........',cONWWXkxO0KXNWWWKoccccclllooodddddxxxkkkkOOOO0
................... .............,;;:::cccllloooooodooddddolllccccc:ccccccccccc::::;;,,,''......'.....,;l0NWWXkdkKXNNWWWKdcllcllllooodddddxxxkkkkOOOO0
.................. ............,;::::ccccllloooodoooddddoooollcc::cccccllllccc:::;;,,,,''.''''.....',ckXNWMNkox0XWWWWN0dclllllllooodddddxxxkkkkOOO00
.......... ... .. .....,;::::cccclllloooooooodoooooooollccclccclollccc:::;;,,,,,'''''''...'';lONWWMW0dx0NWWWWKxlllclllllooodddddxxxkkkOOOO00
.. ....... ... .. .....,;;:::::ccc:cccllloooooooooooloolccllc:cloolccc:::;;,,,,''''''''..'',:oOXWMWWXkk0NWWWNOdlllclllloooooddddxxxkkkOOOO00
.................. . . ....',;;::::;;;,'''',,,,;:ccclloollllcccllc::looolccc::;;,'''..........'',:lk0XWMWXkkKNWWNXkocllllllloooooddddxxxxkkkOOO00
.................. . ..',;;;;:;,,'''..'......''',;:::ccc::colc:;;loocc::;,,''...............',cxOKNWMNOkKNNNXKxlcccllllllooooddddxxxxkkkOOO00
.............. ..,,;;;;,;;;::::;;,,,,''.....'',,;;;:lolc:,':cc:;;,'.........'''''''''..';cx0NWMN0k0XKKN0occccclllllooooddddxxxxkkkOOOO0
................ ..,,;;,,,;:clllllllllllc:;,,''',,,;;coooc;,.'',,'........',;;;::;;,,,'''',;cd0NMW0xkK00NOoccccclllllooooddddxxxxkkkOOOO0
.................. .',,;;,,;:cllooolllc:;;,,'''',;;::cloodol:,...............',,'',,''''''',;;,:oKWWKxxO0kOOoccccccllllloooodddxxxxkkkOOO00
................... ..',;;;;;:cccc:;,,'.,'.......',,;cllooddol:,'...............',...........',,'':OWWXxdkOdxOo:ccccclllllooooddddxxxxkkkOOO0
..................... ..',;;::ccc:,'..',,','...''''';::clloooool:,'............',,,,''''''''......'':ONWNkdkOxdko:ccccccllllooooodddxxxxkkkkOOO
................. ..',;;::cc:::::::c::;;;,,,,,,,;:cllllooolc:;'...........'',,;;;;;,,''''....''',dXWNkdxkOxxo::ccccclllloooooddddxxxxkkkOOO
.............. ...',;:::cccclllllooolcc::;;;;;;:cllloooolc:;'............'',;;;;;,,'.......''',oKNN0xkO0Oxl::cccccclllooooooddddxxxkkkkOO
.............. ...',;;:ccccllooooooooolllllc:::ccllloooll:;;'.............'',,,,,,,''''..'''',;o0NWKkkOO0kl::ccccccllllloooooddddxxxkkkOO
.............. ..',,;:cclllloooddddddddoolccclloolooolll::;'..............''',,,,,,,'''''''',;o0NNOddkxkOo:::ccccccllllloooodddddxxxkkkk
. ................ ..'',;:cclllllooddddddoolcccloooooooollllc:;'...............''',,,,,,'''''''',:dKWXxloxlckx::::cccccclllllooooddddxxxxkkk
................... .....',;:ccllollooooooooollllodddoooooollllc:;'................'''',,,,,,,,''''';dKWKocdd:;dxc::::cccccclllllloooddddxxxxkk
................... .....',;::cllloooodddddoooooodddoooooooollll:,'................'''''',,,,''''''';xXWOlcxxc;cdl:::::ccccccclllllooooddddxxxk
................... ....',;;:ccllloodddddooooooddddooooooolllll:,'.................''''''''''''''.';xXNOoxOdolldc;:::::::cccccclllloooddddxxxx
................... ....',;:ccclloooddooooooooddddooooollllllc:;,...................'''''''''....',dKWX0XO:lodd:;;:::::::ccccccllllooooddddxx
.................... .. ....',;::ccllllooddddoooooddddooollllllolc:;,'...................'''''''....'',oKWNWWO:;:oo;;;;;::::::ccccccllllloooodddx
.................... ...........',;:::cccllooddddoooooddddoolllllloddlc:;''...................''''.......',oKWWNWO;,co:;;;;;;;;:::::::ccccllllooooddd
..................... ..........',;::cccclooododdddooooollllllllodxdlcc:,'...........................''.',oKWWWWk;;c:,;;;;;;;;;;::::::cccclllllooodd
..................................',;::cccclloooooddddoolllllllllllooolcc:;,'.........................'''.',oKNWWWk:;;,,,,,,;;;;;;;;;:::::cccclllllooo
..................................',;::ccccclloodddooollllloolc:::cllllcc:,,'.........................','.';dKNNWNd,,,,,,,,,,,;;;;;;;;;:::::cccclllloo
..................................',;:::cccclloooddoollllooolllccccccc:::;'................'''........,,,'';dKXXNXl,,,,,,,,,,,,,,;;;;;;;;:::::ccccllll
..................................',,;::cccclllloddoolloooolllllllccc:;,,,'................'''........','.';oO0KN0:',',,',,,,,,,,,,,;;;;;;;:::::ccccll
..................................',,;::ccccllllooolllolllllllllllllc;'';c:;,................''.......','',:oxk0Kx;','','',,,,,,,,,,,,;;;;;;;::::ccccc
.................................'',;;:cccccllllollllllllllccccclllc;',clc:;'.......................'''',:cccoOkc'''''''''''',,,,,,,,,,,;;;;;;:::::cc
.................................'',,;::cccclllllccccccccccccccclll:,,;clcc:,,'.....................''.',::::ldl,''''''''''''''',,,,,,,,,,;;;;;::::::
..................................',,;;::cccclllccccccccccccllllllc:;,;;:::;,,''''..................''..,:;;;:c,''..''''''''''''''',,,,,,,,,,;;;;;:::
...................................'',,;::ccccllccccccccclllllllllllc:::;;;;;,,''....................,,..,;;,;;,''.....'.''''''''''''',,,,,,,,,;;;;;;;
....................................',,;:::cclllc:cc:::;::::::::::::;;;;,''..........................',..,;,',,'.............'''''''''''',,,,,,,,,;;;;
.....................................',,;::cclllc:::::;::::::;;;;;;;,,,''.......................''...',..',',,..................'''''''''''',,,,,,,,,;
......................................',;;:::clllc::cccccccllcccccccc:::;;,,,,''...............''....''..',,,'..'...................''''''''',,,,,,,,,
.....................................',;;;::cclc::cccccccccccc::::;;;;;,,,,''''........................';:;.........................'''''''''''',,,,
....................................'',;;;::::::cc::cccccccc::;;,,,,,,''',,''........................',;l;............................''''''''''',,
.....................................',,;;;:::::cccccc::::::::;;,,,,,,,',,,'''......................',';o;..............................'''''''''''
.......................................',,,;;:::cccccccc::c::::::;;;::;;;;;,,'''.....................,'';l:'..................................''''''
.....................................',,;;::::cccccccccccccclllccccc::::;;,,''...................''..,oo''...................................''''
................................ ....'',;;;;;;::cccccllccccclllcccc::::;;,,,''.......................'dk;.,'.....................................
............................... ....''',;;;;;;;::cccclllccclcllccc::::;;,,''........................'o0o..,,....................................
............................ ......',,;;;;;;;:::cccccccllcc:::;;;;;,,,''........................'l0O,..::'..................................
.. ..................... .....'''',,,,,,;;::::::::cc:::;,,,,,,,,,,''........................,lOO:..':c:,'...............................
. ........ ......'''''',,,,,,;;::::;;;;;;;,'''''''''''.........................,lkk;...,cl:;,,''...........................
... ...'''''',,,,,,,,,;;;;;;;;,,,,,'''''................................,cxd,....;cc:,..''''''......................
...''''''',,,,,,;,,,,,,,,''''''''...................................':l:.....,:c:;'.........',,,,'..............
...''',,,,',,,,,;;;;,,,,,,,,,''''''..................................,;'......;cc:,'...........',;;;;;,'........
..''',,,;,,,,;;;;;;;,,,,,,,,''''''...................................... .....,::,'................';:c::;,....
..''',,;;;;;;;;;;;;;;;;;,,,,'''''''.................................... ......,;;'...................',;clll:;"

# Function to send a command to minishell
send_command() {
echo "$1" > "$FIFO_PATH"
sleep 0.1
}

# Read and discard the initial prompt
send_command ""

while IFS= read -r cmd
do
echo "Testing command: $cmd"

# Run in Bash
bash -c "$cmd" > "$TEMP_DIR/bash_output" 2> "$TEMP_DIR/bash_error"

# Clear previous minishell output
truncate -s 0 "$TEMP_DIR/minishell_output"

# Send command to minishell
send_command "$cmd"

# Wait for output
sleep 0.1

# Process minishell output
sed '1d;/minishell>/d' "$TEMP_DIR/minishell_output" > "$TEMP_DIR/minishell_processed"

if ! diff -q "$TEMP_DIR/bash_output" "$TEMP_DIR/minishell_processed" >/dev/null
then
echo "Diff KO :( : $cmd"
echo "Diff:"
diff "$TEMP_DIR/bash_output" "$TEMP_DIR/minishell_processed"
echo
else
echo "Diff ok! :)"
fi

done < "$TEST_COMMANDS"

# Clean up
kill $MINISHELL_PID
rm -rf output.txt output2.txt output3.txt output4.txt
rm -rf "$TEMP_DIR"
18 changes: 16 additions & 2 deletions src/built_ins/built_ins.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,27 @@
/* ::: :::::::: */
/* built_ins.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* By: marvin <marvin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/16 17:59:53 by jteissie #+# #+# */
/* Updated: 2024/07/29 12:03:06 by jteissie ### ########.fr */
/* Updated: 2024/07/29 22:47:45 by marvin ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

static int reset_terminal(t_data *data)
{
int dup_status;

dup_status = 0;
dup_status += dup2(data->std_fd[0], STDIN_FILENO);
dup_status += dup2(data->std_fd[1], STDOUT_FILENO);
if (dup_status < 0)
return (PANIC);
return (SUCCESS);
}

static void read_and_discard_pipein(int pipe[])
{
char buffer[1024];
Expand Down Expand Up @@ -87,4 +99,6 @@ void execute_builtin(t_vector *vector, t_data *data, int mode, int pipe[])
call_builtin(command, data, mode, pipe);
if (command)
free_strarray(command);
if (mode == PARENT && reset_terminal(data) == PANIC)
return ;
}
4 changes: 2 additions & 2 deletions src/execution/process_command.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* process_command.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* By: marvin <marvin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/10 13:07:11 by jteissie #+# #+# */
/* Updated: 2024/07/29 12:22:34 by jteissie ### ########.fr */
/* Updated: 2024/07/29 22:42:49 by marvin ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
32 changes: 32 additions & 0 deletions test_commands.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
echo "Hello, World!"
echo Hello World
echo $USER
echo $HOME
echo "This is a test"
echo 'This is a test'
echo $HOME
echo "$HOME is the home directory"
echo '$HOME is the home directory'
echo "Hello, World!" | tr '[:lower:]' '[:upper:]'
ls | grep minishell
cat Makefile | grep SRC | wc -l
echo $? + $?
echo "SPACES ARE COOL "
echo "very"'annoying' quotes!
echo "EVEN MORE ANNOYING $HOMECOUCOU'$HOME'"'$HOME'"$"HOME"$HOME"
echo "MORE MORE MORE "$DISPLAY$HOME" $? "HOME" $ $ $HOMECOUCOU'$HOME'"'$HOME'"$"HOME"$HOME"
ls > output2.txt > output3.txt > output4.txt > /dev/stdout
cat output2.txt
cat output3.txt
cat output4.txt
< Makefile < src/main.c ls > output2.txt -a > output4.txt
cat output4.txt
cat /etc/passwd | wc -l
ls -l | grep minishell | wc -l
echo "Hello, World!" > output.txt
cat output.txt
cat < output.txt
echo "Appending to file" >> output.txt
cat output.txt
ls -la
pwd

0 comments on commit afc04f6

Please sign in to comment.